blob: 221d65286329a523742b0b769de3b7ac8de0e8cd [file] [log] [blame]
Thomas Falcon032c5e82015-12-21 11:26:06 -06001/**************************************************************************/
2/* */
3/* IBM System i and System p Virtual NIC Device Driver */
4/* Copyright (C) 2014 IBM Corp. */
5/* Santiago Leon (santi_leon@yahoo.com) */
6/* Thomas Falcon (tlfalcon@linux.vnet.ibm.com) */
7/* John Allen (jallen@linux.vnet.ibm.com) */
8/* */
9/* This program is free software; you can redistribute it and/or modify */
10/* it under the terms of the GNU General Public License as published by */
11/* the Free Software Foundation; either version 2 of the License, or */
12/* (at your option) any later version. */
13/* */
14/* This program is distributed in the hope that it will be useful, */
15/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
16/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
17/* GNU General Public License for more details. */
18/* */
19/* You should have received a copy of the GNU General Public License */
20/* along with this program. */
21/* */
22/* This module contains the implementation of a virtual ethernet device */
23/* for use with IBM i/p Series LPAR Linux. It utilizes the logical LAN */
24/* option of the RS/6000 Platform Architecture to interface with virtual */
25/* ethernet NICs that are presented to the partition by the hypervisor. */
26/* */
27/* Messages are passed between the VNIC driver and the VNIC server using */
28/* Command/Response Queues (CRQs) and sub CRQs (sCRQs). CRQs are used to */
29/* issue and receive commands that initiate communication with the server */
30/* on driver initialization. Sub CRQs (sCRQs) are similar to CRQs, but */
31/* are used by the driver to notify the server that a packet is */
32/* ready for transmission or that a buffer has been added to receive a */
33/* packet. Subsequently, sCRQs are used by the server to notify the */
34/* driver that a packet transmission has been completed or that a packet */
35/* has been received and placed in a waiting buffer. */
36/* */
37/* In lieu of a more conventional "on-the-fly" DMA mapping strategy in */
38/* which skbs are DMA mapped and immediately unmapped when the transmit */
39/* or receive has been completed, the VNIC driver is required to use */
40/* "long term mapping". This entails that large, continuous DMA mapped */
41/* buffers are allocated on driver initialization and these buffers are */
42/* then continuously reused to pass skbs to and from the VNIC server. */
43/* */
44/**************************************************************************/
45
46#include <linux/module.h>
47#include <linux/moduleparam.h>
48#include <linux/types.h>
49#include <linux/errno.h>
50#include <linux/completion.h>
51#include <linux/ioport.h>
52#include <linux/dma-mapping.h>
53#include <linux/kernel.h>
54#include <linux/netdevice.h>
55#include <linux/etherdevice.h>
56#include <linux/skbuff.h>
57#include <linux/init.h>
58#include <linux/delay.h>
59#include <linux/mm.h>
60#include <linux/ethtool.h>
61#include <linux/proc_fs.h>
62#include <linux/in.h>
63#include <linux/ip.h>
Thomas Falconad7775d2016-04-01 17:20:34 -050064#include <linux/ipv6.h>
Thomas Falcon032c5e82015-12-21 11:26:06 -060065#include <linux/irq.h>
66#include <linux/kthread.h>
67#include <linux/seq_file.h>
Thomas Falcon032c5e82015-12-21 11:26:06 -060068#include <linux/interrupt.h>
69#include <net/net_namespace.h>
70#include <asm/hvcall.h>
71#include <linux/atomic.h>
72#include <asm/vio.h>
73#include <asm/iommu.h>
74#include <linux/uaccess.h>
75#include <asm/firmware.h>
Thomas Falcon65dc6892016-07-06 15:35:18 -050076#include <linux/workqueue.h>
Thomas Falcon032c5e82015-12-21 11:26:06 -060077
78#include "ibmvnic.h"
79
80static const char ibmvnic_driver_name[] = "ibmvnic";
81static const char ibmvnic_driver_string[] = "IBM System i/p Virtual NIC Driver";
82
83MODULE_AUTHOR("Santiago Leon <santi_leon@yahoo.com>");
84MODULE_DESCRIPTION("IBM System i/p Virtual NIC Driver");
85MODULE_LICENSE("GPL");
86MODULE_VERSION(IBMVNIC_DRIVER_VERSION);
87
88static int ibmvnic_version = IBMVNIC_INITIAL_VERSION;
89static int ibmvnic_remove(struct vio_dev *);
90static void release_sub_crqs(struct ibmvnic_adapter *);
91static int ibmvnic_reset_crq(struct ibmvnic_adapter *);
92static int ibmvnic_send_crq_init(struct ibmvnic_adapter *);
93static int ibmvnic_reenable_crq_queue(struct ibmvnic_adapter *);
94static int ibmvnic_send_crq(struct ibmvnic_adapter *, union ibmvnic_crq *);
95static int send_subcrq(struct ibmvnic_adapter *adapter, u64 remote_handle,
96 union sub_crq *sub_crq);
Thomas Falconad7775d2016-04-01 17:20:34 -050097static int send_subcrq_indirect(struct ibmvnic_adapter *, u64, u64, u64);
Thomas Falcon032c5e82015-12-21 11:26:06 -060098static irqreturn_t ibmvnic_interrupt_rx(int irq, void *instance);
99static int enable_scrq_irq(struct ibmvnic_adapter *,
100 struct ibmvnic_sub_crq_queue *);
101static int disable_scrq_irq(struct ibmvnic_adapter *,
102 struct ibmvnic_sub_crq_queue *);
103static int pending_scrq(struct ibmvnic_adapter *,
104 struct ibmvnic_sub_crq_queue *);
105static union sub_crq *ibmvnic_next_scrq(struct ibmvnic_adapter *,
106 struct ibmvnic_sub_crq_queue *);
107static int ibmvnic_poll(struct napi_struct *napi, int data);
108static void send_map_query(struct ibmvnic_adapter *adapter);
109static void send_request_map(struct ibmvnic_adapter *, dma_addr_t, __be32, u8);
110static void send_request_unmap(struct ibmvnic_adapter *, u8);
John Allenbd0b6722017-03-17 17:13:40 -0500111static void send_login(struct ibmvnic_adapter *adapter);
112static void send_cap_queries(struct ibmvnic_adapter *adapter);
113static int init_sub_crq_irqs(struct ibmvnic_adapter *adapter);
John Allenea5509f2017-03-17 17:13:43 -0500114static int ibmvnic_init(struct ibmvnic_adapter *);
Nathan Fontenotf9928872017-03-30 02:48:54 -0400115static void release_crq_queue(struct ibmvnic_adapter *);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600116
117struct ibmvnic_stat {
118 char name[ETH_GSTRING_LEN];
119 int offset;
120};
121
122#define IBMVNIC_STAT_OFF(stat) (offsetof(struct ibmvnic_adapter, stats) + \
123 offsetof(struct ibmvnic_statistics, stat))
124#define IBMVNIC_GET_STAT(a, off) (*((u64 *)(((unsigned long)(a)) + off)))
125
126static const struct ibmvnic_stat ibmvnic_stats[] = {
127 {"rx_packets", IBMVNIC_STAT_OFF(rx_packets)},
128 {"rx_bytes", IBMVNIC_STAT_OFF(rx_bytes)},
129 {"tx_packets", IBMVNIC_STAT_OFF(tx_packets)},
130 {"tx_bytes", IBMVNIC_STAT_OFF(tx_bytes)},
131 {"ucast_tx_packets", IBMVNIC_STAT_OFF(ucast_tx_packets)},
132 {"ucast_rx_packets", IBMVNIC_STAT_OFF(ucast_rx_packets)},
133 {"mcast_tx_packets", IBMVNIC_STAT_OFF(mcast_tx_packets)},
134 {"mcast_rx_packets", IBMVNIC_STAT_OFF(mcast_rx_packets)},
135 {"bcast_tx_packets", IBMVNIC_STAT_OFF(bcast_tx_packets)},
136 {"bcast_rx_packets", IBMVNIC_STAT_OFF(bcast_rx_packets)},
137 {"align_errors", IBMVNIC_STAT_OFF(align_errors)},
138 {"fcs_errors", IBMVNIC_STAT_OFF(fcs_errors)},
139 {"single_collision_frames", IBMVNIC_STAT_OFF(single_collision_frames)},
140 {"multi_collision_frames", IBMVNIC_STAT_OFF(multi_collision_frames)},
141 {"sqe_test_errors", IBMVNIC_STAT_OFF(sqe_test_errors)},
142 {"deferred_tx", IBMVNIC_STAT_OFF(deferred_tx)},
143 {"late_collisions", IBMVNIC_STAT_OFF(late_collisions)},
144 {"excess_collisions", IBMVNIC_STAT_OFF(excess_collisions)},
145 {"internal_mac_tx_errors", IBMVNIC_STAT_OFF(internal_mac_tx_errors)},
146 {"carrier_sense", IBMVNIC_STAT_OFF(carrier_sense)},
147 {"too_long_frames", IBMVNIC_STAT_OFF(too_long_frames)},
148 {"internal_mac_rx_errors", IBMVNIC_STAT_OFF(internal_mac_rx_errors)},
149};
150
151static long h_reg_sub_crq(unsigned long unit_address, unsigned long token,
152 unsigned long length, unsigned long *number,
153 unsigned long *irq)
154{
155 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
156 long rc;
157
158 rc = plpar_hcall(H_REG_SUB_CRQ, retbuf, unit_address, token, length);
159 *number = retbuf[0];
160 *irq = retbuf[1];
161
162 return rc;
163}
164
Thomas Falcon032c5e82015-12-21 11:26:06 -0600165static int alloc_long_term_buff(struct ibmvnic_adapter *adapter,
166 struct ibmvnic_long_term_buff *ltb, int size)
167{
168 struct device *dev = &adapter->vdev->dev;
169
170 ltb->size = size;
171 ltb->buff = dma_alloc_coherent(dev, ltb->size, &ltb->addr,
172 GFP_KERNEL);
173
174 if (!ltb->buff) {
175 dev_err(dev, "Couldn't alloc long term buffer\n");
176 return -ENOMEM;
177 }
178 ltb->map_id = adapter->map_id;
179 adapter->map_id++;
Nathan Fontenotdb5d0b52017-02-10 13:45:05 -0500180
181 init_completion(&adapter->fw_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600182 send_request_map(adapter, ltb->addr,
183 ltb->size, ltb->map_id);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600184 wait_for_completion(&adapter->fw_done);
185 return 0;
186}
187
188static void free_long_term_buff(struct ibmvnic_adapter *adapter,
189 struct ibmvnic_long_term_buff *ltb)
190{
191 struct device *dev = &adapter->vdev->dev;
192
Nathan Fontenotc657e322017-03-30 02:49:06 -0400193 if (!ltb->buff)
194 return;
195
Thomas Falcondfad09a2016-08-18 11:37:51 -0500196 if (!adapter->failover)
197 send_request_unmap(adapter, ltb->map_id);
Brian King59af56c2017-04-19 13:44:41 -0400198 dma_free_coherent(dev, ltb->size, ltb->buff, ltb->addr);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600199}
200
Thomas Falcon032c5e82015-12-21 11:26:06 -0600201static void replenish_rx_pool(struct ibmvnic_adapter *adapter,
202 struct ibmvnic_rx_pool *pool)
203{
204 int count = pool->size - atomic_read(&pool->available);
205 struct device *dev = &adapter->vdev->dev;
206 int buffers_added = 0;
207 unsigned long lpar_rc;
208 union sub_crq sub_crq;
209 struct sk_buff *skb;
210 unsigned int offset;
211 dma_addr_t dma_addr;
212 unsigned char *dst;
213 u64 *handle_array;
214 int shift = 0;
215 int index;
216 int i;
217
218 handle_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
219 be32_to_cpu(adapter->login_rsp_buf->
220 off_rxadd_subcrqs));
221
222 for (i = 0; i < count; ++i) {
223 skb = alloc_skb(pool->buff_size, GFP_ATOMIC);
224 if (!skb) {
225 dev_err(dev, "Couldn't replenish rx buff\n");
226 adapter->replenish_no_mem++;
227 break;
228 }
229
230 index = pool->free_map[pool->next_free];
231
232 if (pool->rx_buff[index].skb)
233 dev_err(dev, "Inconsistent free_map!\n");
234
235 /* Copy the skb to the long term mapped DMA buffer */
236 offset = index * pool->buff_size;
237 dst = pool->long_term_buff.buff + offset;
238 memset(dst, 0, pool->buff_size);
239 dma_addr = pool->long_term_buff.addr + offset;
240 pool->rx_buff[index].data = dst;
241
242 pool->free_map[pool->next_free] = IBMVNIC_INVALID_MAP;
243 pool->rx_buff[index].dma = dma_addr;
244 pool->rx_buff[index].skb = skb;
245 pool->rx_buff[index].pool_index = pool->index;
246 pool->rx_buff[index].size = pool->buff_size;
247
248 memset(&sub_crq, 0, sizeof(sub_crq));
249 sub_crq.rx_add.first = IBMVNIC_CRQ_CMD;
250 sub_crq.rx_add.correlator =
251 cpu_to_be64((u64)&pool->rx_buff[index]);
252 sub_crq.rx_add.ioba = cpu_to_be32(dma_addr);
253 sub_crq.rx_add.map_id = pool->long_term_buff.map_id;
254
255 /* The length field of the sCRQ is defined to be 24 bits so the
256 * buffer size needs to be left shifted by a byte before it is
257 * converted to big endian to prevent the last byte from being
258 * truncated.
259 */
260#ifdef __LITTLE_ENDIAN__
261 shift = 8;
262#endif
263 sub_crq.rx_add.len = cpu_to_be32(pool->buff_size << shift);
264
265 lpar_rc = send_subcrq(adapter, handle_array[pool->index],
266 &sub_crq);
267 if (lpar_rc != H_SUCCESS)
268 goto failure;
269
270 buffers_added++;
271 adapter->replenish_add_buff_success++;
272 pool->next_free = (pool->next_free + 1) % pool->size;
273 }
274 atomic_add(buffers_added, &pool->available);
275 return;
276
277failure:
278 dev_info(dev, "replenish pools failure\n");
279 pool->free_map[pool->next_free] = index;
280 pool->rx_buff[index].skb = NULL;
281 if (!dma_mapping_error(dev, dma_addr))
282 dma_unmap_single(dev, dma_addr, pool->buff_size,
283 DMA_FROM_DEVICE);
284
285 dev_kfree_skb_any(skb);
286 adapter->replenish_add_buff_failure++;
287 atomic_add(buffers_added, &pool->available);
288}
289
290static void replenish_pools(struct ibmvnic_adapter *adapter)
291{
292 int i;
293
294 if (adapter->migrated)
295 return;
296
297 adapter->replenish_task_cycles++;
298 for (i = 0; i < be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
299 i++) {
300 if (adapter->rx_pool[i].active)
301 replenish_rx_pool(adapter, &adapter->rx_pool[i]);
302 }
303}
304
Nathan Fontenot7bbc27a2017-03-30 02:49:23 -0400305static void release_stats_token(struct ibmvnic_adapter *adapter)
306{
307 struct device *dev = &adapter->vdev->dev;
308
309 if (!adapter->stats_token)
310 return;
311
312 dma_unmap_single(dev, adapter->stats_token,
313 sizeof(struct ibmvnic_statistics),
314 DMA_FROM_DEVICE);
315 adapter->stats_token = 0;
316}
317
318static int init_stats_token(struct ibmvnic_adapter *adapter)
319{
320 struct device *dev = &adapter->vdev->dev;
321 dma_addr_t stok;
322
323 stok = dma_map_single(dev, &adapter->stats,
324 sizeof(struct ibmvnic_statistics),
325 DMA_FROM_DEVICE);
326 if (dma_mapping_error(dev, stok)) {
327 dev_err(dev, "Couldn't map stats buffer\n");
328 return -1;
329 }
330
331 adapter->stats_token = stok;
332 return 0;
333}
334
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400335static void release_rx_pools(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -0600336{
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400337 struct ibmvnic_rx_pool *rx_pool;
338 int rx_scrqs;
339 int i, j;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600340
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400341 if (!adapter->rx_pool)
Thomas Falcon032c5e82015-12-21 11:26:06 -0600342 return;
343
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400344 rx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
345 for (i = 0; i < rx_scrqs; i++) {
346 rx_pool = &adapter->rx_pool[i];
347
348 kfree(rx_pool->free_map);
349 free_long_term_buff(adapter, &rx_pool->long_term_buff);
350
351 if (!rx_pool->rx_buff)
352 continue;
353
354 for (j = 0; j < rx_pool->size; j++) {
355 if (rx_pool->rx_buff[j].skb) {
356 dev_kfree_skb_any(rx_pool->rx_buff[i].skb);
357 rx_pool->rx_buff[i].skb = NULL;
358 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600359 }
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400360
361 kfree(rx_pool->rx_buff);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600362 }
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400363
364 kfree(adapter->rx_pool);
365 adapter->rx_pool = NULL;
366}
367
368static int init_rx_pools(struct net_device *netdev)
369{
370 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
371 struct device *dev = &adapter->vdev->dev;
372 struct ibmvnic_rx_pool *rx_pool;
373 int rxadd_subcrqs;
374 u64 *size_array;
375 int i, j;
376
377 rxadd_subcrqs =
378 be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
379 size_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
380 be32_to_cpu(adapter->login_rsp_buf->off_rxadd_buff_size));
381
382 adapter->rx_pool = kcalloc(rxadd_subcrqs,
383 sizeof(struct ibmvnic_rx_pool),
384 GFP_KERNEL);
385 if (!adapter->rx_pool) {
386 dev_err(dev, "Failed to allocate rx pools\n");
387 return -1;
388 }
389
390 for (i = 0; i < rxadd_subcrqs; i++) {
391 rx_pool = &adapter->rx_pool[i];
392
393 netdev_dbg(adapter->netdev,
394 "Initializing rx_pool %d, %lld buffs, %lld bytes each\n",
395 i, adapter->req_rx_add_entries_per_subcrq,
396 be64_to_cpu(size_array[i]));
397
398 rx_pool->size = adapter->req_rx_add_entries_per_subcrq;
399 rx_pool->index = i;
400 rx_pool->buff_size = be64_to_cpu(size_array[i]);
401 rx_pool->active = 1;
402
403 rx_pool->free_map = kcalloc(rx_pool->size, sizeof(int),
404 GFP_KERNEL);
405 if (!rx_pool->free_map) {
406 release_rx_pools(adapter);
407 return -1;
408 }
409
410 rx_pool->rx_buff = kcalloc(rx_pool->size,
411 sizeof(struct ibmvnic_rx_buff),
412 GFP_KERNEL);
413 if (!rx_pool->rx_buff) {
414 dev_err(dev, "Couldn't alloc rx buffers\n");
415 release_rx_pools(adapter);
416 return -1;
417 }
418
419 if (alloc_long_term_buff(adapter, &rx_pool->long_term_buff,
420 rx_pool->size * rx_pool->buff_size)) {
421 release_rx_pools(adapter);
422 return -1;
423 }
424
425 for (j = 0; j < rx_pool->size; ++j)
426 rx_pool->free_map[j] = j;
427
428 atomic_set(&rx_pool->available, 0);
429 rx_pool->next_alloc = 0;
430 rx_pool->next_free = 0;
431 }
432
433 return 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600434}
435
Nathan Fontenotc657e322017-03-30 02:49:06 -0400436static void release_tx_pools(struct ibmvnic_adapter *adapter)
437{
438 struct ibmvnic_tx_pool *tx_pool;
439 int i, tx_scrqs;
440
441 if (!adapter->tx_pool)
442 return;
443
444 tx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
445 for (i = 0; i < tx_scrqs; i++) {
446 tx_pool = &adapter->tx_pool[i];
447 kfree(tx_pool->tx_buff);
448 free_long_term_buff(adapter, &tx_pool->long_term_buff);
449 kfree(tx_pool->free_map);
450 }
451
452 kfree(adapter->tx_pool);
453 adapter->tx_pool = NULL;
454}
455
456static int init_tx_pools(struct net_device *netdev)
457{
458 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
459 struct device *dev = &adapter->vdev->dev;
460 struct ibmvnic_tx_pool *tx_pool;
461 int tx_subcrqs;
462 int i, j;
463
464 tx_subcrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
465 adapter->tx_pool = kcalloc(tx_subcrqs,
466 sizeof(struct ibmvnic_tx_pool), GFP_KERNEL);
467 if (!adapter->tx_pool)
468 return -1;
469
470 for (i = 0; i < tx_subcrqs; i++) {
471 tx_pool = &adapter->tx_pool[i];
472 tx_pool->tx_buff = kcalloc(adapter->req_tx_entries_per_subcrq,
473 sizeof(struct ibmvnic_tx_buff),
474 GFP_KERNEL);
475 if (!tx_pool->tx_buff) {
476 dev_err(dev, "tx pool buffer allocation failed\n");
477 release_tx_pools(adapter);
478 return -1;
479 }
480
481 if (alloc_long_term_buff(adapter, &tx_pool->long_term_buff,
482 adapter->req_tx_entries_per_subcrq *
483 adapter->req_mtu)) {
484 release_tx_pools(adapter);
485 return -1;
486 }
487
488 tx_pool->free_map = kcalloc(adapter->req_tx_entries_per_subcrq,
489 sizeof(int), GFP_KERNEL);
490 if (!tx_pool->free_map) {
491 release_tx_pools(adapter);
492 return -1;
493 }
494
495 for (j = 0; j < adapter->req_tx_entries_per_subcrq; j++)
496 tx_pool->free_map[j] = j;
497
498 tx_pool->consumer_index = 0;
499 tx_pool->producer_index = 0;
500 }
501
502 return 0;
503}
504
Nathan Fontenotf0b8c962017-03-30 02:49:00 -0400505static void release_bounce_buffer(struct ibmvnic_adapter *adapter)
506{
507 struct device *dev = &adapter->vdev->dev;
508
509 if (!adapter->bounce_buffer)
510 return;
511
512 if (!dma_mapping_error(dev, adapter->bounce_buffer_dma)) {
513 dma_unmap_single(dev, adapter->bounce_buffer_dma,
514 adapter->bounce_buffer_size,
515 DMA_BIDIRECTIONAL);
516 adapter->bounce_buffer_dma = DMA_ERROR_CODE;
517 }
518
519 kfree(adapter->bounce_buffer);
520 adapter->bounce_buffer = NULL;
521}
522
523static int init_bounce_buffer(struct net_device *netdev)
524{
525 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
526 struct device *dev = &adapter->vdev->dev;
527 char *buf;
528 int buf_sz;
529 dma_addr_t map_addr;
530
531 buf_sz = (netdev->mtu + ETH_HLEN - 1) / PAGE_SIZE + 1;
532 buf = kmalloc(adapter->bounce_buffer_size, GFP_KERNEL);
533 if (!buf)
534 return -1;
535
536 map_addr = dma_map_single(dev, buf, buf_sz, DMA_TO_DEVICE);
537 if (dma_mapping_error(dev, map_addr)) {
538 dev_err(dev, "Couldn't map bounce buffer\n");
539 kfree(buf);
540 return -1;
541 }
542
543 adapter->bounce_buffer = buf;
544 adapter->bounce_buffer_size = buf_sz;
545 adapter->bounce_buffer_dma = map_addr;
546 return 0;
547}
548
Nathan Fontenot661a2622017-04-19 13:44:58 -0400549static void release_error_buffers(struct ibmvnic_adapter *adapter)
550{
551 struct device *dev = &adapter->vdev->dev;
552 struct ibmvnic_error_buff *error_buff, *tmp;
553 unsigned long flags;
554
555 spin_lock_irqsave(&adapter->error_list_lock, flags);
556 list_for_each_entry_safe(error_buff, tmp, &adapter->errors, list) {
557 list_del(&error_buff->list);
558 dma_unmap_single(dev, error_buff->dma, error_buff->len,
559 DMA_FROM_DEVICE);
560 kfree(error_buff->buff);
561 kfree(error_buff);
562 }
563 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
564}
565
John Allena57a5d22017-03-17 17:13:41 -0500566static int ibmvnic_login(struct net_device *netdev)
Thomas Falcon032c5e82015-12-21 11:26:06 -0600567{
568 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
John Allenbd0b6722017-03-17 17:13:40 -0500569 unsigned long timeout = msecs_to_jiffies(30000);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600570 struct device *dev = &adapter->vdev->dev;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600571
John Allenbd0b6722017-03-17 17:13:40 -0500572 do {
573 if (adapter->renegotiate) {
574 adapter->renegotiate = false;
Nathan Fontenotb5108882017-03-30 02:49:18 -0400575 release_sub_crqs(adapter);
John Allenbd0b6722017-03-17 17:13:40 -0500576
577 reinit_completion(&adapter->init_done);
578 send_cap_queries(adapter);
579 if (!wait_for_completion_timeout(&adapter->init_done,
580 timeout)) {
581 dev_err(dev, "Capabilities query timeout\n");
582 return -1;
583 }
584 }
585
586 reinit_completion(&adapter->init_done);
587 send_login(adapter);
588 if (!wait_for_completion_timeout(&adapter->init_done,
589 timeout)) {
590 dev_err(dev, "Login timeout\n");
591 return -1;
592 }
593 } while (adapter->renegotiate);
594
John Allena57a5d22017-03-17 17:13:41 -0500595 return 0;
596}
597
Nathan Fontenot1b8955e2017-03-30 02:49:29 -0400598static void release_resources(struct ibmvnic_adapter *adapter)
599{
600 release_bounce_buffer(adapter);
601 release_tx_pools(adapter);
602 release_rx_pools(adapter);
603
Nathan Fontenot1b8955e2017-03-30 02:49:29 -0400604 release_stats_token(adapter);
Nathan Fontenot661a2622017-04-19 13:44:58 -0400605 release_error_buffers(adapter);
Nathan Fontenot1b8955e2017-03-30 02:49:29 -0400606}
607
John Allena57a5d22017-03-17 17:13:41 -0500608static int ibmvnic_open(struct net_device *netdev)
609{
610 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
611 struct device *dev = &adapter->vdev->dev;
John Allena57a5d22017-03-17 17:13:41 -0500612 union ibmvnic_crq crq;
John Allena57a5d22017-03-17 17:13:41 -0500613 int rc = 0;
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400614 int i;
John Allena57a5d22017-03-17 17:13:41 -0500615
John Allenea5509f2017-03-17 17:13:43 -0500616 if (adapter->is_closed) {
617 rc = ibmvnic_init(adapter);
618 if (rc)
619 return rc;
620 }
621
John Allena57a5d22017-03-17 17:13:41 -0500622 rc = ibmvnic_login(netdev);
623 if (rc)
624 return rc;
625
John Allenbd0b6722017-03-17 17:13:40 -0500626 rc = netif_set_real_num_tx_queues(netdev, adapter->req_tx_queues);
627 if (rc) {
628 dev_err(dev, "failed to set the number of tx queues\n");
629 return -1;
630 }
631
632 rc = init_sub_crq_irqs(adapter);
633 if (rc) {
634 dev_err(dev, "failed to initialize sub crq irqs\n");
635 return -1;
636 }
637
Thomas Falcon032c5e82015-12-21 11:26:06 -0600638 adapter->map_id = 1;
639 adapter->napi = kcalloc(adapter->req_rx_queues,
640 sizeof(struct napi_struct), GFP_KERNEL);
641 if (!adapter->napi)
Nathan Fontenot1b8955e2017-03-30 02:49:29 -0400642 goto ibmvnic_open_fail;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600643 for (i = 0; i < adapter->req_rx_queues; i++) {
644 netif_napi_add(netdev, &adapter->napi[i], ibmvnic_poll,
645 NAPI_POLL_WEIGHT);
646 napi_enable(&adapter->napi[i]);
647 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600648
Thomas Falcon032c5e82015-12-21 11:26:06 -0600649 send_map_query(adapter);
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400650
651 rc = init_rx_pools(netdev);
652 if (rc)
Nathan Fontenot1b8955e2017-03-30 02:49:29 -0400653 goto ibmvnic_open_fail;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600654
Nathan Fontenotc657e322017-03-30 02:49:06 -0400655 rc = init_tx_pools(netdev);
656 if (rc)
Nathan Fontenot1b8955e2017-03-30 02:49:29 -0400657 goto ibmvnic_open_fail;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600658
Nathan Fontenotf0b8c962017-03-30 02:49:00 -0400659 rc = init_bounce_buffer(netdev);
660 if (rc)
Nathan Fontenot1b8955e2017-03-30 02:49:29 -0400661 goto ibmvnic_open_fail;
Nathan Fontenotf0b8c962017-03-30 02:49:00 -0400662
Thomas Falcon032c5e82015-12-21 11:26:06 -0600663 replenish_pools(adapter);
664
665 /* We're ready to receive frames, enable the sub-crq interrupts and
666 * set the logical link state to up
667 */
668 for (i = 0; i < adapter->req_rx_queues; i++)
669 enable_scrq_irq(adapter, adapter->rx_scrq[i]);
670
671 for (i = 0; i < adapter->req_tx_queues; i++)
672 enable_scrq_irq(adapter, adapter->tx_scrq[i]);
673
674 memset(&crq, 0, sizeof(crq));
675 crq.logical_link_state.first = IBMVNIC_CRQ_CMD;
676 crq.logical_link_state.cmd = LOGICAL_LINK_STATE;
677 crq.logical_link_state.link_state = IBMVNIC_LOGICAL_LNK_UP;
678 ibmvnic_send_crq(adapter, &crq);
679
Thomas Falconb8efb892016-07-06 15:35:15 -0500680 netif_tx_start_all_queues(netdev);
John Allenea5509f2017-03-17 17:13:43 -0500681 adapter->is_closed = false;
Thomas Falconb8efb892016-07-06 15:35:15 -0500682
Thomas Falcon032c5e82015-12-21 11:26:06 -0600683 return 0;
684
Nathan Fontenot1b8955e2017-03-30 02:49:29 -0400685ibmvnic_open_fail:
Thomas Falcon032c5e82015-12-21 11:26:06 -0600686 for (i = 0; i < adapter->req_rx_queues; i++)
Nathan Fontenote722af62017-02-10 13:29:06 -0500687 napi_disable(&adapter->napi[i]);
Nathan Fontenot1b8955e2017-03-30 02:49:29 -0400688 release_resources(adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600689 return -ENOMEM;
690}
691
Brian Kingdd9c20f2017-04-19 13:45:10 -0400692static void disable_sub_crqs(struct ibmvnic_adapter *adapter)
693{
694 int i;
695
696 if (adapter->tx_scrq) {
697 for (i = 0; i < adapter->req_tx_queues; i++)
698 if (adapter->tx_scrq[i])
699 disable_irq(adapter->tx_scrq[i]->irq);
700 }
701
702 if (adapter->rx_scrq) {
703 for (i = 0; i < adapter->req_rx_queues; i++)
704 if (adapter->rx_scrq[i])
705 disable_irq(adapter->rx_scrq[i]->irq);
706 }
707}
708
John Allenea5509f2017-03-17 17:13:43 -0500709static int ibmvnic_close(struct net_device *netdev)
710{
711 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
712 union ibmvnic_crq crq;
713 int i;
714
715 adapter->closing = true;
Brian Kingdd9c20f2017-04-19 13:45:10 -0400716 disable_sub_crqs(adapter);
John Allenea5509f2017-03-17 17:13:43 -0500717
718 for (i = 0; i < adapter->req_rx_queues; i++)
719 napi_disable(&adapter->napi[i]);
720
721 if (!adapter->failover)
722 netif_tx_stop_all_queues(netdev);
723
Thomas Falcon032c5e82015-12-21 11:26:06 -0600724 memset(&crq, 0, sizeof(crq));
725 crq.logical_link_state.first = IBMVNIC_CRQ_CMD;
726 crq.logical_link_state.cmd = LOGICAL_LINK_STATE;
727 crq.logical_link_state.link_state = IBMVNIC_LOGICAL_LNK_DN;
728 ibmvnic_send_crq(adapter, &crq);
729
Nathan Fontenot1b8955e2017-03-30 02:49:29 -0400730 release_resources(adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600731
John Allenea5509f2017-03-17 17:13:43 -0500732 adapter->is_closed = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600733 adapter->closing = false;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600734 return 0;
735}
736
Thomas Falconad7775d2016-04-01 17:20:34 -0500737/**
738 * build_hdr_data - creates L2/L3/L4 header data buffer
739 * @hdr_field - bitfield determining needed headers
740 * @skb - socket buffer
741 * @hdr_len - array of header lengths
742 * @tot_len - total length of data
743 *
744 * Reads hdr_field to determine which headers are needed by firmware.
745 * Builds a buffer containing these headers. Saves individual header
746 * lengths and total buffer length to be used to build descriptors.
747 */
748static int build_hdr_data(u8 hdr_field, struct sk_buff *skb,
749 int *hdr_len, u8 *hdr_data)
750{
751 int len = 0;
752 u8 *hdr;
753
754 hdr_len[0] = sizeof(struct ethhdr);
755
756 if (skb->protocol == htons(ETH_P_IP)) {
757 hdr_len[1] = ip_hdr(skb)->ihl * 4;
758 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
759 hdr_len[2] = tcp_hdrlen(skb);
760 else if (ip_hdr(skb)->protocol == IPPROTO_UDP)
761 hdr_len[2] = sizeof(struct udphdr);
762 } else if (skb->protocol == htons(ETH_P_IPV6)) {
763 hdr_len[1] = sizeof(struct ipv6hdr);
764 if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
765 hdr_len[2] = tcp_hdrlen(skb);
766 else if (ipv6_hdr(skb)->nexthdr == IPPROTO_UDP)
767 hdr_len[2] = sizeof(struct udphdr);
768 }
769
770 memset(hdr_data, 0, 120);
771 if ((hdr_field >> 6) & 1) {
772 hdr = skb_mac_header(skb);
773 memcpy(hdr_data, hdr, hdr_len[0]);
774 len += hdr_len[0];
775 }
776
777 if ((hdr_field >> 5) & 1) {
778 hdr = skb_network_header(skb);
779 memcpy(hdr_data + len, hdr, hdr_len[1]);
780 len += hdr_len[1];
781 }
782
783 if ((hdr_field >> 4) & 1) {
784 hdr = skb_transport_header(skb);
785 memcpy(hdr_data + len, hdr, hdr_len[2]);
786 len += hdr_len[2];
787 }
788 return len;
789}
790
791/**
792 * create_hdr_descs - create header and header extension descriptors
793 * @hdr_field - bitfield determining needed headers
794 * @data - buffer containing header data
795 * @len - length of data buffer
796 * @hdr_len - array of individual header lengths
797 * @scrq_arr - descriptor array
798 *
799 * Creates header and, if needed, header extension descriptors and
800 * places them in a descriptor array, scrq_arr
801 */
802
803static void create_hdr_descs(u8 hdr_field, u8 *hdr_data, int len, int *hdr_len,
804 union sub_crq *scrq_arr)
805{
806 union sub_crq hdr_desc;
807 int tmp_len = len;
808 u8 *data, *cur;
809 int tmp;
810
811 while (tmp_len > 0) {
812 cur = hdr_data + len - tmp_len;
813
814 memset(&hdr_desc, 0, sizeof(hdr_desc));
815 if (cur != hdr_data) {
816 data = hdr_desc.hdr_ext.data;
817 tmp = tmp_len > 29 ? 29 : tmp_len;
818 hdr_desc.hdr_ext.first = IBMVNIC_CRQ_CMD;
819 hdr_desc.hdr_ext.type = IBMVNIC_HDR_EXT_DESC;
820 hdr_desc.hdr_ext.len = tmp;
821 } else {
822 data = hdr_desc.hdr.data;
823 tmp = tmp_len > 24 ? 24 : tmp_len;
824 hdr_desc.hdr.first = IBMVNIC_CRQ_CMD;
825 hdr_desc.hdr.type = IBMVNIC_HDR_DESC;
826 hdr_desc.hdr.len = tmp;
827 hdr_desc.hdr.l2_len = (u8)hdr_len[0];
828 hdr_desc.hdr.l3_len = cpu_to_be16((u16)hdr_len[1]);
829 hdr_desc.hdr.l4_len = (u8)hdr_len[2];
830 hdr_desc.hdr.flag = hdr_field << 1;
831 }
832 memcpy(data, cur, tmp);
833 tmp_len -= tmp;
834 *scrq_arr = hdr_desc;
835 scrq_arr++;
836 }
837}
838
839/**
840 * build_hdr_descs_arr - build a header descriptor array
841 * @skb - socket buffer
842 * @num_entries - number of descriptors to be sent
843 * @subcrq - first TX descriptor
844 * @hdr_field - bit field determining which headers will be sent
845 *
846 * This function will build a TX descriptor array with applicable
847 * L2/L3/L4 packet header descriptors to be sent by send_subcrq_indirect.
848 */
849
850static void build_hdr_descs_arr(struct ibmvnic_tx_buff *txbuff,
851 int *num_entries, u8 hdr_field)
852{
853 int hdr_len[3] = {0, 0, 0};
854 int tot_len, len;
855 u8 *hdr_data = txbuff->hdr_data;
856
857 tot_len = build_hdr_data(hdr_field, txbuff->skb, hdr_len,
858 txbuff->hdr_data);
859 len = tot_len;
860 len -= 24;
861 if (len > 0)
862 num_entries += len % 29 ? len / 29 + 1 : len / 29;
863 create_hdr_descs(hdr_field, hdr_data, tot_len, hdr_len,
864 txbuff->indir_arr + 1);
865}
866
Thomas Falcon032c5e82015-12-21 11:26:06 -0600867static int ibmvnic_xmit(struct sk_buff *skb, struct net_device *netdev)
868{
869 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
870 int queue_num = skb_get_queue_mapping(skb);
Thomas Falconad7775d2016-04-01 17:20:34 -0500871 u8 *hdrs = (u8 *)&adapter->tx_rx_desc_req;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600872 struct device *dev = &adapter->vdev->dev;
873 struct ibmvnic_tx_buff *tx_buff = NULL;
Thomas Falcon142c0ac2017-03-05 12:18:41 -0600874 struct ibmvnic_sub_crq_queue *tx_scrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600875 struct ibmvnic_tx_pool *tx_pool;
876 unsigned int tx_send_failed = 0;
877 unsigned int tx_map_failed = 0;
878 unsigned int tx_dropped = 0;
879 unsigned int tx_packets = 0;
880 unsigned int tx_bytes = 0;
881 dma_addr_t data_dma_addr;
882 struct netdev_queue *txq;
883 bool used_bounce = false;
884 unsigned long lpar_rc;
885 union sub_crq tx_crq;
886 unsigned int offset;
Thomas Falconad7775d2016-04-01 17:20:34 -0500887 int num_entries = 1;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600888 unsigned char *dst;
889 u64 *handle_array;
890 int index = 0;
891 int ret = 0;
892
893 tx_pool = &adapter->tx_pool[queue_num];
Thomas Falcon142c0ac2017-03-05 12:18:41 -0600894 tx_scrq = adapter->tx_scrq[queue_num];
Thomas Falcon032c5e82015-12-21 11:26:06 -0600895 txq = netdev_get_tx_queue(netdev, skb_get_queue_mapping(skb));
896 handle_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
897 be32_to_cpu(adapter->login_rsp_buf->
898 off_txsubm_subcrqs));
899 if (adapter->migrated) {
900 tx_send_failed++;
901 tx_dropped++;
902 ret = NETDEV_TX_BUSY;
903 goto out;
904 }
905
906 index = tx_pool->free_map[tx_pool->consumer_index];
907 offset = index * adapter->req_mtu;
908 dst = tx_pool->long_term_buff.buff + offset;
909 memset(dst, 0, adapter->req_mtu);
910 skb_copy_from_linear_data(skb, dst, skb->len);
911 data_dma_addr = tx_pool->long_term_buff.addr + offset;
912
913 tx_pool->consumer_index =
914 (tx_pool->consumer_index + 1) %
Thomas Falcon068d9f92017-03-05 12:18:42 -0600915 adapter->req_tx_entries_per_subcrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600916
917 tx_buff = &tx_pool->tx_buff[index];
918 tx_buff->skb = skb;
919 tx_buff->data_dma[0] = data_dma_addr;
920 tx_buff->data_len[0] = skb->len;
921 tx_buff->index = index;
922 tx_buff->pool_index = queue_num;
923 tx_buff->last_frag = true;
924 tx_buff->used_bounce = used_bounce;
925
926 memset(&tx_crq, 0, sizeof(tx_crq));
927 tx_crq.v1.first = IBMVNIC_CRQ_CMD;
928 tx_crq.v1.type = IBMVNIC_TX_DESC;
929 tx_crq.v1.n_crq_elem = 1;
930 tx_crq.v1.n_sge = 1;
931 tx_crq.v1.flags1 = IBMVNIC_TX_COMP_NEEDED;
932 tx_crq.v1.correlator = cpu_to_be32(index);
933 tx_crq.v1.dma_reg = cpu_to_be16(tx_pool->long_term_buff.map_id);
934 tx_crq.v1.sge_len = cpu_to_be32(skb->len);
935 tx_crq.v1.ioba = cpu_to_be64(data_dma_addr);
936
937 if (adapter->vlan_header_insertion) {
938 tx_crq.v1.flags2 |= IBMVNIC_TX_VLAN_INSERT;
939 tx_crq.v1.vlan_id = cpu_to_be16(skb->vlan_tci);
940 }
941
942 if (skb->protocol == htons(ETH_P_IP)) {
943 if (ip_hdr(skb)->version == 4)
944 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV4;
945 else if (ip_hdr(skb)->version == 6)
946 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV6;
947
948 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
949 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_TCP;
950 else if (ip_hdr(skb)->protocol != IPPROTO_TCP)
951 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_UDP;
952 }
953
Thomas Falconad7775d2016-04-01 17:20:34 -0500954 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Thomas Falcon032c5e82015-12-21 11:26:06 -0600955 tx_crq.v1.flags1 |= IBMVNIC_TX_CHKSUM_OFFLOAD;
Thomas Falconad7775d2016-04-01 17:20:34 -0500956 hdrs += 2;
957 }
958 /* determine if l2/3/4 headers are sent to firmware */
959 if ((*hdrs >> 7) & 1 &&
960 (skb->protocol == htons(ETH_P_IP) ||
961 skb->protocol == htons(ETH_P_IPV6))) {
962 build_hdr_descs_arr(tx_buff, &num_entries, *hdrs);
963 tx_crq.v1.n_crq_elem = num_entries;
964 tx_buff->indir_arr[0] = tx_crq;
965 tx_buff->indir_dma = dma_map_single(dev, tx_buff->indir_arr,
966 sizeof(tx_buff->indir_arr),
967 DMA_TO_DEVICE);
968 if (dma_mapping_error(dev, tx_buff->indir_dma)) {
969 if (!firmware_has_feature(FW_FEATURE_CMO))
970 dev_err(dev, "tx: unable to map descriptor array\n");
971 tx_map_failed++;
972 tx_dropped++;
973 ret = NETDEV_TX_BUSY;
974 goto out;
975 }
John Allen498cd8e2016-04-06 11:49:55 -0500976 lpar_rc = send_subcrq_indirect(adapter, handle_array[queue_num],
Thomas Falconad7775d2016-04-01 17:20:34 -0500977 (u64)tx_buff->indir_dma,
978 (u64)num_entries);
979 } else {
John Allen498cd8e2016-04-06 11:49:55 -0500980 lpar_rc = send_subcrq(adapter, handle_array[queue_num],
981 &tx_crq);
Thomas Falconad7775d2016-04-01 17:20:34 -0500982 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600983 if (lpar_rc != H_SUCCESS) {
984 dev_err(dev, "tx failed with code %ld\n", lpar_rc);
985
986 if (tx_pool->consumer_index == 0)
987 tx_pool->consumer_index =
Thomas Falcon068d9f92017-03-05 12:18:42 -0600988 adapter->req_tx_entries_per_subcrq - 1;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600989 else
990 tx_pool->consumer_index--;
991
992 tx_send_failed++;
993 tx_dropped++;
994 ret = NETDEV_TX_BUSY;
995 goto out;
996 }
Thomas Falcon142c0ac2017-03-05 12:18:41 -0600997
Brian King58c8c0c2017-04-19 13:44:47 -0400998 if (atomic_inc_return(&tx_scrq->used)
999 >= adapter->req_tx_entries_per_subcrq) {
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001000 netdev_info(netdev, "Stopping queue %d\n", queue_num);
1001 netif_stop_subqueue(netdev, queue_num);
1002 }
1003
Thomas Falcon032c5e82015-12-21 11:26:06 -06001004 tx_packets++;
1005 tx_bytes += skb->len;
1006 txq->trans_start = jiffies;
1007 ret = NETDEV_TX_OK;
1008
1009out:
1010 netdev->stats.tx_dropped += tx_dropped;
1011 netdev->stats.tx_bytes += tx_bytes;
1012 netdev->stats.tx_packets += tx_packets;
1013 adapter->tx_send_failed += tx_send_failed;
1014 adapter->tx_map_failed += tx_map_failed;
1015
1016 return ret;
1017}
1018
1019static void ibmvnic_set_multi(struct net_device *netdev)
1020{
1021 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1022 struct netdev_hw_addr *ha;
1023 union ibmvnic_crq crq;
1024
1025 memset(&crq, 0, sizeof(crq));
1026 crq.request_capability.first = IBMVNIC_CRQ_CMD;
1027 crq.request_capability.cmd = REQUEST_CAPABILITY;
1028
1029 if (netdev->flags & IFF_PROMISC) {
1030 if (!adapter->promisc_supported)
1031 return;
1032 } else {
1033 if (netdev->flags & IFF_ALLMULTI) {
1034 /* Accept all multicast */
1035 memset(&crq, 0, sizeof(crq));
1036 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1037 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1038 crq.multicast_ctrl.flags = IBMVNIC_ENABLE_ALL;
1039 ibmvnic_send_crq(adapter, &crq);
1040 } else if (netdev_mc_empty(netdev)) {
1041 /* Reject all multicast */
1042 memset(&crq, 0, sizeof(crq));
1043 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1044 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1045 crq.multicast_ctrl.flags = IBMVNIC_DISABLE_ALL;
1046 ibmvnic_send_crq(adapter, &crq);
1047 } else {
1048 /* Accept one or more multicast(s) */
1049 netdev_for_each_mc_addr(ha, netdev) {
1050 memset(&crq, 0, sizeof(crq));
1051 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1052 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1053 crq.multicast_ctrl.flags = IBMVNIC_ENABLE_MC;
1054 ether_addr_copy(&crq.multicast_ctrl.mac_addr[0],
1055 ha->addr);
1056 ibmvnic_send_crq(adapter, &crq);
1057 }
1058 }
1059 }
1060}
1061
1062static int ibmvnic_set_mac(struct net_device *netdev, void *p)
1063{
1064 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1065 struct sockaddr *addr = p;
1066 union ibmvnic_crq crq;
1067
1068 if (!is_valid_ether_addr(addr->sa_data))
1069 return -EADDRNOTAVAIL;
1070
1071 memset(&crq, 0, sizeof(crq));
1072 crq.change_mac_addr.first = IBMVNIC_CRQ_CMD;
1073 crq.change_mac_addr.cmd = CHANGE_MAC_ADDR;
1074 ether_addr_copy(&crq.change_mac_addr.mac_addr[0], addr->sa_data);
1075 ibmvnic_send_crq(adapter, &crq);
1076 /* netdev->dev_addr is changed in handle_change_mac_rsp function */
1077 return 0;
1078}
1079
Thomas Falcon032c5e82015-12-21 11:26:06 -06001080static void ibmvnic_tx_timeout(struct net_device *dev)
1081{
1082 struct ibmvnic_adapter *adapter = netdev_priv(dev);
1083 int rc;
1084
1085 /* Adapter timed out, resetting it */
1086 release_sub_crqs(adapter);
1087 rc = ibmvnic_reset_crq(adapter);
1088 if (rc)
1089 dev_err(&adapter->vdev->dev, "Adapter timeout, reset failed\n");
1090 else
1091 ibmvnic_send_crq_init(adapter);
1092}
1093
1094static void remove_buff_from_pool(struct ibmvnic_adapter *adapter,
1095 struct ibmvnic_rx_buff *rx_buff)
1096{
1097 struct ibmvnic_rx_pool *pool = &adapter->rx_pool[rx_buff->pool_index];
1098
1099 rx_buff->skb = NULL;
1100
1101 pool->free_map[pool->next_alloc] = (int)(rx_buff - pool->rx_buff);
1102 pool->next_alloc = (pool->next_alloc + 1) % pool->size;
1103
1104 atomic_dec(&pool->available);
1105}
1106
1107static int ibmvnic_poll(struct napi_struct *napi, int budget)
1108{
1109 struct net_device *netdev = napi->dev;
1110 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1111 int scrq_num = (int)(napi - adapter->napi);
1112 int frames_processed = 0;
1113restart_poll:
1114 while (frames_processed < budget) {
1115 struct sk_buff *skb;
1116 struct ibmvnic_rx_buff *rx_buff;
1117 union sub_crq *next;
1118 u32 length;
1119 u16 offset;
1120 u8 flags = 0;
1121
1122 if (!pending_scrq(adapter, adapter->rx_scrq[scrq_num]))
1123 break;
1124 next = ibmvnic_next_scrq(adapter, adapter->rx_scrq[scrq_num]);
1125 rx_buff =
1126 (struct ibmvnic_rx_buff *)be64_to_cpu(next->
1127 rx_comp.correlator);
1128 /* do error checking */
1129 if (next->rx_comp.rc) {
1130 netdev_err(netdev, "rx error %x\n", next->rx_comp.rc);
1131 /* free the entry */
1132 next->rx_comp.first = 0;
1133 remove_buff_from_pool(adapter, rx_buff);
1134 break;
1135 }
1136
1137 length = be32_to_cpu(next->rx_comp.len);
1138 offset = be16_to_cpu(next->rx_comp.off_frame_data);
1139 flags = next->rx_comp.flags;
1140 skb = rx_buff->skb;
1141 skb_copy_to_linear_data(skb, rx_buff->data + offset,
1142 length);
1143 skb->vlan_tci = be16_to_cpu(next->rx_comp.vlan_tci);
1144 /* free the entry */
1145 next->rx_comp.first = 0;
1146 remove_buff_from_pool(adapter, rx_buff);
1147
1148 skb_put(skb, length);
1149 skb->protocol = eth_type_trans(skb, netdev);
1150
1151 if (flags & IBMVNIC_IP_CHKSUM_GOOD &&
1152 flags & IBMVNIC_TCP_UDP_CHKSUM_GOOD) {
1153 skb->ip_summed = CHECKSUM_UNNECESSARY;
1154 }
1155
1156 length = skb->len;
1157 napi_gro_receive(napi, skb); /* send it up */
1158 netdev->stats.rx_packets++;
1159 netdev->stats.rx_bytes += length;
1160 frames_processed++;
1161 }
John Allen498cd8e2016-04-06 11:49:55 -05001162 replenish_rx_pool(adapter, &adapter->rx_pool[scrq_num]);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001163
1164 if (frames_processed < budget) {
1165 enable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
Eric Dumazet6ad20162017-01-30 08:22:01 -08001166 napi_complete_done(napi, frames_processed);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001167 if (pending_scrq(adapter, adapter->rx_scrq[scrq_num]) &&
1168 napi_reschedule(napi)) {
1169 disable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
1170 goto restart_poll;
1171 }
1172 }
1173 return frames_processed;
1174}
1175
1176#ifdef CONFIG_NET_POLL_CONTROLLER
1177static void ibmvnic_netpoll_controller(struct net_device *dev)
1178{
1179 struct ibmvnic_adapter *adapter = netdev_priv(dev);
1180 int i;
1181
1182 replenish_pools(netdev_priv(dev));
1183 for (i = 0; i < adapter->req_rx_queues; i++)
1184 ibmvnic_interrupt_rx(adapter->rx_scrq[i]->irq,
1185 adapter->rx_scrq[i]);
1186}
1187#endif
1188
1189static const struct net_device_ops ibmvnic_netdev_ops = {
1190 .ndo_open = ibmvnic_open,
1191 .ndo_stop = ibmvnic_close,
1192 .ndo_start_xmit = ibmvnic_xmit,
1193 .ndo_set_rx_mode = ibmvnic_set_multi,
1194 .ndo_set_mac_address = ibmvnic_set_mac,
1195 .ndo_validate_addr = eth_validate_addr,
Thomas Falcon032c5e82015-12-21 11:26:06 -06001196 .ndo_tx_timeout = ibmvnic_tx_timeout,
1197#ifdef CONFIG_NET_POLL_CONTROLLER
1198 .ndo_poll_controller = ibmvnic_netpoll_controller,
1199#endif
1200};
1201
1202/* ethtool functions */
1203
Philippe Reynes8a433792017-01-07 22:37:29 +01001204static int ibmvnic_get_link_ksettings(struct net_device *netdev,
1205 struct ethtool_link_ksettings *cmd)
Thomas Falcon032c5e82015-12-21 11:26:06 -06001206{
Philippe Reynes8a433792017-01-07 22:37:29 +01001207 u32 supported, advertising;
1208
1209 supported = (SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg |
Thomas Falcon032c5e82015-12-21 11:26:06 -06001210 SUPPORTED_FIBRE);
Philippe Reynes8a433792017-01-07 22:37:29 +01001211 advertising = (ADVERTISED_1000baseT_Full | ADVERTISED_Autoneg |
Thomas Falcon032c5e82015-12-21 11:26:06 -06001212 ADVERTISED_FIBRE);
Philippe Reynes8a433792017-01-07 22:37:29 +01001213 cmd->base.speed = SPEED_1000;
1214 cmd->base.duplex = DUPLEX_FULL;
1215 cmd->base.port = PORT_FIBRE;
1216 cmd->base.phy_address = 0;
1217 cmd->base.autoneg = AUTONEG_ENABLE;
1218
1219 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
1220 supported);
1221 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
1222 advertising);
1223
Thomas Falcon032c5e82015-12-21 11:26:06 -06001224 return 0;
1225}
1226
1227static void ibmvnic_get_drvinfo(struct net_device *dev,
1228 struct ethtool_drvinfo *info)
1229{
1230 strlcpy(info->driver, ibmvnic_driver_name, sizeof(info->driver));
1231 strlcpy(info->version, IBMVNIC_DRIVER_VERSION, sizeof(info->version));
1232}
1233
1234static u32 ibmvnic_get_msglevel(struct net_device *netdev)
1235{
1236 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1237
1238 return adapter->msg_enable;
1239}
1240
1241static void ibmvnic_set_msglevel(struct net_device *netdev, u32 data)
1242{
1243 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1244
1245 adapter->msg_enable = data;
1246}
1247
1248static u32 ibmvnic_get_link(struct net_device *netdev)
1249{
1250 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1251
1252 /* Don't need to send a query because we request a logical link up at
1253 * init and then we wait for link state indications
1254 */
1255 return adapter->logical_link_state;
1256}
1257
1258static void ibmvnic_get_ringparam(struct net_device *netdev,
1259 struct ethtool_ringparam *ring)
1260{
1261 ring->rx_max_pending = 0;
1262 ring->tx_max_pending = 0;
1263 ring->rx_mini_max_pending = 0;
1264 ring->rx_jumbo_max_pending = 0;
1265 ring->rx_pending = 0;
1266 ring->tx_pending = 0;
1267 ring->rx_mini_pending = 0;
1268 ring->rx_jumbo_pending = 0;
1269}
1270
1271static void ibmvnic_get_strings(struct net_device *dev, u32 stringset, u8 *data)
1272{
1273 int i;
1274
1275 if (stringset != ETH_SS_STATS)
1276 return;
1277
1278 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++, data += ETH_GSTRING_LEN)
1279 memcpy(data, ibmvnic_stats[i].name, ETH_GSTRING_LEN);
1280}
1281
1282static int ibmvnic_get_sset_count(struct net_device *dev, int sset)
1283{
1284 switch (sset) {
1285 case ETH_SS_STATS:
1286 return ARRAY_SIZE(ibmvnic_stats);
1287 default:
1288 return -EOPNOTSUPP;
1289 }
1290}
1291
1292static void ibmvnic_get_ethtool_stats(struct net_device *dev,
1293 struct ethtool_stats *stats, u64 *data)
1294{
1295 struct ibmvnic_adapter *adapter = netdev_priv(dev);
1296 union ibmvnic_crq crq;
1297 int i;
1298
1299 memset(&crq, 0, sizeof(crq));
1300 crq.request_statistics.first = IBMVNIC_CRQ_CMD;
1301 crq.request_statistics.cmd = REQUEST_STATISTICS;
1302 crq.request_statistics.ioba = cpu_to_be32(adapter->stats_token);
1303 crq.request_statistics.len =
1304 cpu_to_be32(sizeof(struct ibmvnic_statistics));
Thomas Falcon032c5e82015-12-21 11:26:06 -06001305
1306 /* Wait for data to be written */
1307 init_completion(&adapter->stats_done);
Nathan Fontenotdb5d0b52017-02-10 13:45:05 -05001308 ibmvnic_send_crq(adapter, &crq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001309 wait_for_completion(&adapter->stats_done);
1310
1311 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++)
1312 data[i] = IBMVNIC_GET_STAT(adapter, ibmvnic_stats[i].offset);
1313}
1314
1315static const struct ethtool_ops ibmvnic_ethtool_ops = {
Thomas Falcon032c5e82015-12-21 11:26:06 -06001316 .get_drvinfo = ibmvnic_get_drvinfo,
1317 .get_msglevel = ibmvnic_get_msglevel,
1318 .set_msglevel = ibmvnic_set_msglevel,
1319 .get_link = ibmvnic_get_link,
1320 .get_ringparam = ibmvnic_get_ringparam,
1321 .get_strings = ibmvnic_get_strings,
1322 .get_sset_count = ibmvnic_get_sset_count,
1323 .get_ethtool_stats = ibmvnic_get_ethtool_stats,
Philippe Reynes8a433792017-01-07 22:37:29 +01001324 .get_link_ksettings = ibmvnic_get_link_ksettings,
Thomas Falcon032c5e82015-12-21 11:26:06 -06001325};
1326
1327/* Routines for managing CRQs/sCRQs */
1328
1329static void release_sub_crq_queue(struct ibmvnic_adapter *adapter,
1330 struct ibmvnic_sub_crq_queue *scrq)
1331{
1332 struct device *dev = &adapter->vdev->dev;
1333 long rc;
1334
1335 netdev_dbg(adapter->netdev, "Releasing sub-CRQ\n");
1336
1337 /* Close the sub-crqs */
1338 do {
1339 rc = plpar_hcall_norets(H_FREE_SUB_CRQ,
1340 adapter->vdev->unit_address,
1341 scrq->crq_num);
1342 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
1343
Thomas Falconffa73852017-04-19 13:44:29 -04001344 if (rc) {
1345 netdev_err(adapter->netdev,
1346 "Failed to release sub-CRQ %16lx, rc = %ld\n",
1347 scrq->crq_num, rc);
1348 }
1349
Thomas Falcon032c5e82015-12-21 11:26:06 -06001350 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
1351 DMA_BIDIRECTIONAL);
1352 free_pages((unsigned long)scrq->msgs, 2);
1353 kfree(scrq);
1354}
1355
1356static struct ibmvnic_sub_crq_queue *init_sub_crq_queue(struct ibmvnic_adapter
1357 *adapter)
1358{
1359 struct device *dev = &adapter->vdev->dev;
1360 struct ibmvnic_sub_crq_queue *scrq;
1361 int rc;
1362
Nathan Fontenot7f7adc52017-04-19 13:45:16 -04001363 scrq = kzalloc(sizeof(*scrq), GFP_ATOMIC);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001364 if (!scrq)
1365 return NULL;
1366
Nathan Fontenot7f7adc52017-04-19 13:45:16 -04001367 scrq->msgs =
1368 (union sub_crq *)__get_free_pages(GFP_ATOMIC | __GFP_ZERO, 2);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001369 if (!scrq->msgs) {
1370 dev_warn(dev, "Couldn't allocate crq queue messages page\n");
1371 goto zero_page_failed;
1372 }
1373
1374 scrq->msg_token = dma_map_single(dev, scrq->msgs, 4 * PAGE_SIZE,
1375 DMA_BIDIRECTIONAL);
1376 if (dma_mapping_error(dev, scrq->msg_token)) {
1377 dev_warn(dev, "Couldn't map crq queue messages page\n");
1378 goto map_failed;
1379 }
1380
1381 rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
1382 4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
1383
1384 if (rc == H_RESOURCE)
1385 rc = ibmvnic_reset_crq(adapter);
1386
1387 if (rc == H_CLOSED) {
1388 dev_warn(dev, "Partner adapter not ready, waiting.\n");
1389 } else if (rc) {
1390 dev_warn(dev, "Error %d registering sub-crq\n", rc);
1391 goto reg_failed;
1392 }
1393
Thomas Falcon032c5e82015-12-21 11:26:06 -06001394 scrq->adapter = adapter;
1395 scrq->size = 4 * PAGE_SIZE / sizeof(*scrq->msgs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001396 spin_lock_init(&scrq->lock);
1397
1398 netdev_dbg(adapter->netdev,
1399 "sub-crq initialized, num %lx, hw_irq=%lx, irq=%x\n",
1400 scrq->crq_num, scrq->hw_irq, scrq->irq);
1401
1402 return scrq;
1403
Thomas Falcon032c5e82015-12-21 11:26:06 -06001404reg_failed:
1405 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
1406 DMA_BIDIRECTIONAL);
1407map_failed:
1408 free_pages((unsigned long)scrq->msgs, 2);
1409zero_page_failed:
1410 kfree(scrq);
1411
1412 return NULL;
1413}
1414
1415static void release_sub_crqs(struct ibmvnic_adapter *adapter)
1416{
1417 int i;
1418
1419 if (adapter->tx_scrq) {
Nathan Fontenotb5108882017-03-30 02:49:18 -04001420 for (i = 0; i < adapter->req_tx_queues; i++) {
1421 if (!adapter->tx_scrq[i])
1422 continue;
1423
1424 if (adapter->tx_scrq[i]->irq) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06001425 free_irq(adapter->tx_scrq[i]->irq,
1426 adapter->tx_scrq[i]);
Thomas Falcon88eb98a2016-07-06 15:35:16 -05001427 irq_dispose_mapping(adapter->tx_scrq[i]->irq);
Nathan Fontenotb5108882017-03-30 02:49:18 -04001428 adapter->tx_scrq[i]->irq = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001429 }
Nathan Fontenotb5108882017-03-30 02:49:18 -04001430
1431 release_sub_crq_queue(adapter, adapter->tx_scrq[i]);
1432 }
1433
Nathan Fontenot9501df32017-03-15 23:38:07 -04001434 kfree(adapter->tx_scrq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001435 adapter->tx_scrq = NULL;
1436 }
1437
1438 if (adapter->rx_scrq) {
Nathan Fontenotb5108882017-03-30 02:49:18 -04001439 for (i = 0; i < adapter->req_rx_queues; i++) {
1440 if (!adapter->rx_scrq[i])
1441 continue;
1442
1443 if (adapter->rx_scrq[i]->irq) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06001444 free_irq(adapter->rx_scrq[i]->irq,
1445 adapter->rx_scrq[i]);
Thomas Falcon88eb98a2016-07-06 15:35:16 -05001446 irq_dispose_mapping(adapter->rx_scrq[i]->irq);
Nathan Fontenotb5108882017-03-30 02:49:18 -04001447 adapter->rx_scrq[i]->irq = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001448 }
Nathan Fontenotb5108882017-03-30 02:49:18 -04001449
1450 release_sub_crq_queue(adapter, adapter->rx_scrq[i]);
1451 }
1452
Nathan Fontenot9501df32017-03-15 23:38:07 -04001453 kfree(adapter->rx_scrq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001454 adapter->rx_scrq = NULL;
1455 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001456}
1457
1458static int disable_scrq_irq(struct ibmvnic_adapter *adapter,
1459 struct ibmvnic_sub_crq_queue *scrq)
1460{
1461 struct device *dev = &adapter->vdev->dev;
1462 unsigned long rc;
1463
1464 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
1465 H_DISABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
1466 if (rc)
1467 dev_err(dev, "Couldn't disable scrq irq 0x%lx. rc=%ld\n",
1468 scrq->hw_irq, rc);
1469 return rc;
1470}
1471
1472static int enable_scrq_irq(struct ibmvnic_adapter *adapter,
1473 struct ibmvnic_sub_crq_queue *scrq)
1474{
1475 struct device *dev = &adapter->vdev->dev;
1476 unsigned long rc;
1477
1478 if (scrq->hw_irq > 0x100000000ULL) {
1479 dev_err(dev, "bad hw_irq = %lx\n", scrq->hw_irq);
1480 return 1;
1481 }
1482
1483 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
1484 H_ENABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
1485 if (rc)
1486 dev_err(dev, "Couldn't enable scrq irq 0x%lx. rc=%ld\n",
1487 scrq->hw_irq, rc);
1488 return rc;
1489}
1490
1491static int ibmvnic_complete_tx(struct ibmvnic_adapter *adapter,
1492 struct ibmvnic_sub_crq_queue *scrq)
1493{
1494 struct device *dev = &adapter->vdev->dev;
1495 struct ibmvnic_tx_buff *txbuff;
1496 union sub_crq *next;
1497 int index;
1498 int i, j;
Thomas Falconad7775d2016-04-01 17:20:34 -05001499 u8 first;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001500
1501restart_loop:
1502 while (pending_scrq(adapter, scrq)) {
1503 unsigned int pool = scrq->pool_index;
1504
1505 next = ibmvnic_next_scrq(adapter, scrq);
1506 for (i = 0; i < next->tx_comp.num_comps; i++) {
1507 if (next->tx_comp.rcs[i]) {
1508 dev_err(dev, "tx error %x\n",
1509 next->tx_comp.rcs[i]);
1510 continue;
1511 }
1512 index = be32_to_cpu(next->tx_comp.correlators[i]);
1513 txbuff = &adapter->tx_pool[pool].tx_buff[index];
1514
1515 for (j = 0; j < IBMVNIC_MAX_FRAGS_PER_CRQ; j++) {
1516 if (!txbuff->data_dma[j])
1517 continue;
1518
1519 txbuff->data_dma[j] = 0;
1520 txbuff->used_bounce = false;
1521 }
Thomas Falconad7775d2016-04-01 17:20:34 -05001522 /* if sub_crq was sent indirectly */
1523 first = txbuff->indir_arr[0].generic.first;
1524 if (first == IBMVNIC_CRQ_CMD) {
1525 dma_unmap_single(dev, txbuff->indir_dma,
1526 sizeof(txbuff->indir_arr),
1527 DMA_TO_DEVICE);
1528 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001529
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001530 if (txbuff->last_frag) {
Brian King58c8c0c2017-04-19 13:44:47 -04001531 if (atomic_sub_return(next->tx_comp.num_comps,
1532 &scrq->used) <=
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001533 (adapter->req_tx_entries_per_subcrq / 2) &&
1534 netif_subqueue_stopped(adapter->netdev,
1535 txbuff->skb)) {
1536 netif_wake_subqueue(adapter->netdev,
1537 scrq->pool_index);
1538 netdev_dbg(adapter->netdev,
1539 "Started queue %d\n",
1540 scrq->pool_index);
1541 }
1542
Thomas Falcon032c5e82015-12-21 11:26:06 -06001543 dev_kfree_skb_any(txbuff->skb);
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001544 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001545
1546 adapter->tx_pool[pool].free_map[adapter->tx_pool[pool].
1547 producer_index] = index;
1548 adapter->tx_pool[pool].producer_index =
1549 (adapter->tx_pool[pool].producer_index + 1) %
Thomas Falcon068d9f92017-03-05 12:18:42 -06001550 adapter->req_tx_entries_per_subcrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001551 }
1552 /* remove tx_comp scrq*/
1553 next->tx_comp.first = 0;
1554 }
1555
1556 enable_scrq_irq(adapter, scrq);
1557
1558 if (pending_scrq(adapter, scrq)) {
1559 disable_scrq_irq(adapter, scrq);
1560 goto restart_loop;
1561 }
1562
1563 return 0;
1564}
1565
1566static irqreturn_t ibmvnic_interrupt_tx(int irq, void *instance)
1567{
1568 struct ibmvnic_sub_crq_queue *scrq = instance;
1569 struct ibmvnic_adapter *adapter = scrq->adapter;
1570
1571 disable_scrq_irq(adapter, scrq);
1572 ibmvnic_complete_tx(adapter, scrq);
1573
1574 return IRQ_HANDLED;
1575}
1576
1577static irqreturn_t ibmvnic_interrupt_rx(int irq, void *instance)
1578{
1579 struct ibmvnic_sub_crq_queue *scrq = instance;
1580 struct ibmvnic_adapter *adapter = scrq->adapter;
1581
1582 if (napi_schedule_prep(&adapter->napi[scrq->scrq_num])) {
1583 disable_scrq_irq(adapter, scrq);
1584 __napi_schedule(&adapter->napi[scrq->scrq_num]);
1585 }
1586
1587 return IRQ_HANDLED;
1588}
1589
Thomas Falconea22d512016-07-06 15:35:17 -05001590static int init_sub_crq_irqs(struct ibmvnic_adapter *adapter)
1591{
1592 struct device *dev = &adapter->vdev->dev;
1593 struct ibmvnic_sub_crq_queue *scrq;
1594 int i = 0, j = 0;
1595 int rc = 0;
1596
1597 for (i = 0; i < adapter->req_tx_queues; i++) {
1598 scrq = adapter->tx_scrq[i];
1599 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
1600
Michael Ellerman99c17902016-09-10 19:59:05 +10001601 if (!scrq->irq) {
Thomas Falconea22d512016-07-06 15:35:17 -05001602 rc = -EINVAL;
1603 dev_err(dev, "Error mapping irq\n");
1604 goto req_tx_irq_failed;
1605 }
1606
1607 rc = request_irq(scrq->irq, ibmvnic_interrupt_tx,
1608 0, "ibmvnic_tx", scrq);
1609
1610 if (rc) {
1611 dev_err(dev, "Couldn't register tx irq 0x%x. rc=%d\n",
1612 scrq->irq, rc);
1613 irq_dispose_mapping(scrq->irq);
1614 goto req_rx_irq_failed;
1615 }
1616 }
1617
1618 for (i = 0; i < adapter->req_rx_queues; i++) {
1619 scrq = adapter->rx_scrq[i];
1620 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
Michael Ellerman99c17902016-09-10 19:59:05 +10001621 if (!scrq->irq) {
Thomas Falconea22d512016-07-06 15:35:17 -05001622 rc = -EINVAL;
1623 dev_err(dev, "Error mapping irq\n");
1624 goto req_rx_irq_failed;
1625 }
1626 rc = request_irq(scrq->irq, ibmvnic_interrupt_rx,
1627 0, "ibmvnic_rx", scrq);
1628 if (rc) {
1629 dev_err(dev, "Couldn't register rx irq 0x%x. rc=%d\n",
1630 scrq->irq, rc);
1631 irq_dispose_mapping(scrq->irq);
1632 goto req_rx_irq_failed;
1633 }
1634 }
1635 return rc;
1636
1637req_rx_irq_failed:
Thomas Falcon8bf371e2016-10-27 12:28:52 -05001638 for (j = 0; j < i; j++) {
Thomas Falconea22d512016-07-06 15:35:17 -05001639 free_irq(adapter->rx_scrq[j]->irq, adapter->rx_scrq[j]);
1640 irq_dispose_mapping(adapter->rx_scrq[j]->irq);
Thomas Falcon8bf371e2016-10-27 12:28:52 -05001641 }
Thomas Falconea22d512016-07-06 15:35:17 -05001642 i = adapter->req_tx_queues;
1643req_tx_irq_failed:
Thomas Falcon8bf371e2016-10-27 12:28:52 -05001644 for (j = 0; j < i; j++) {
Thomas Falconea22d512016-07-06 15:35:17 -05001645 free_irq(adapter->tx_scrq[j]->irq, adapter->tx_scrq[j]);
1646 irq_dispose_mapping(adapter->rx_scrq[j]->irq);
Thomas Falcon8bf371e2016-10-27 12:28:52 -05001647 }
Nathan Fontenotb5108882017-03-30 02:49:18 -04001648 release_sub_crqs(adapter);
Thomas Falconea22d512016-07-06 15:35:17 -05001649 return rc;
1650}
1651
Thomas Falcon032c5e82015-12-21 11:26:06 -06001652static void init_sub_crqs(struct ibmvnic_adapter *adapter, int retry)
1653{
1654 struct device *dev = &adapter->vdev->dev;
1655 struct ibmvnic_sub_crq_queue **allqueues;
1656 int registered_queues = 0;
1657 union ibmvnic_crq crq;
1658 int total_queues;
1659 int more = 0;
Thomas Falconea22d512016-07-06 15:35:17 -05001660 int i;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001661
1662 if (!retry) {
1663 /* Sub-CRQ entries are 32 byte long */
1664 int entries_page = 4 * PAGE_SIZE / (sizeof(u64) * 4);
1665
1666 if (adapter->min_tx_entries_per_subcrq > entries_page ||
1667 adapter->min_rx_add_entries_per_subcrq > entries_page) {
1668 dev_err(dev, "Fatal, invalid entries per sub-crq\n");
1669 goto allqueues_failed;
1670 }
1671
1672 /* Get the minimum between the queried max and the entries
1673 * that fit in our PAGE_SIZE
1674 */
1675 adapter->req_tx_entries_per_subcrq =
1676 adapter->max_tx_entries_per_subcrq > entries_page ?
1677 entries_page : adapter->max_tx_entries_per_subcrq;
1678 adapter->req_rx_add_entries_per_subcrq =
1679 adapter->max_rx_add_entries_per_subcrq > entries_page ?
1680 entries_page : adapter->max_rx_add_entries_per_subcrq;
1681
John Allen6dbcd8f2016-11-07 14:27:28 -06001682 adapter->req_tx_queues = adapter->opt_tx_comp_sub_queues;
1683 adapter->req_rx_queues = adapter->opt_rx_comp_queues;
John Allen498cd8e2016-04-06 11:49:55 -05001684 adapter->req_rx_add_queues = adapter->max_rx_add_queues;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001685
Thomas Falconf39f0d12017-02-14 10:22:59 -06001686 adapter->req_mtu = adapter->netdev->mtu + ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001687 }
1688
1689 total_queues = adapter->req_tx_queues + adapter->req_rx_queues;
1690
1691 allqueues = kcalloc(total_queues, sizeof(*allqueues), GFP_ATOMIC);
1692 if (!allqueues)
1693 goto allqueues_failed;
1694
1695 for (i = 0; i < total_queues; i++) {
1696 allqueues[i] = init_sub_crq_queue(adapter);
1697 if (!allqueues[i]) {
1698 dev_warn(dev, "Couldn't allocate all sub-crqs\n");
1699 break;
1700 }
1701 registered_queues++;
1702 }
1703
1704 /* Make sure we were able to register the minimum number of queues */
1705 if (registered_queues <
1706 adapter->min_tx_queues + adapter->min_rx_queues) {
1707 dev_err(dev, "Fatal: Couldn't init min number of sub-crqs\n");
1708 goto tx_failed;
1709 }
1710
1711 /* Distribute the failed allocated queues*/
1712 for (i = 0; i < total_queues - registered_queues + more ; i++) {
1713 netdev_dbg(adapter->netdev, "Reducing number of queues\n");
1714 switch (i % 3) {
1715 case 0:
1716 if (adapter->req_rx_queues > adapter->min_rx_queues)
1717 adapter->req_rx_queues--;
1718 else
1719 more++;
1720 break;
1721 case 1:
1722 if (adapter->req_tx_queues > adapter->min_tx_queues)
1723 adapter->req_tx_queues--;
1724 else
1725 more++;
1726 break;
1727 }
1728 }
1729
1730 adapter->tx_scrq = kcalloc(adapter->req_tx_queues,
1731 sizeof(*adapter->tx_scrq), GFP_ATOMIC);
1732 if (!adapter->tx_scrq)
1733 goto tx_failed;
1734
1735 for (i = 0; i < adapter->req_tx_queues; i++) {
1736 adapter->tx_scrq[i] = allqueues[i];
1737 adapter->tx_scrq[i]->pool_index = i;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001738 }
1739
1740 adapter->rx_scrq = kcalloc(adapter->req_rx_queues,
1741 sizeof(*adapter->rx_scrq), GFP_ATOMIC);
1742 if (!adapter->rx_scrq)
1743 goto rx_failed;
1744
1745 for (i = 0; i < adapter->req_rx_queues; i++) {
1746 adapter->rx_scrq[i] = allqueues[i + adapter->req_tx_queues];
1747 adapter->rx_scrq[i]->scrq_num = i;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001748 }
1749
1750 memset(&crq, 0, sizeof(crq));
1751 crq.request_capability.first = IBMVNIC_CRQ_CMD;
1752 crq.request_capability.cmd = REQUEST_CAPABILITY;
1753
1754 crq.request_capability.capability = cpu_to_be16(REQ_TX_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06001755 crq.request_capability.number = cpu_to_be64(adapter->req_tx_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06001756 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001757 ibmvnic_send_crq(adapter, &crq);
1758
1759 crq.request_capability.capability = cpu_to_be16(REQ_RX_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06001760 crq.request_capability.number = cpu_to_be64(adapter->req_rx_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06001761 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001762 ibmvnic_send_crq(adapter, &crq);
1763
1764 crq.request_capability.capability = cpu_to_be16(REQ_RX_ADD_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06001765 crq.request_capability.number = cpu_to_be64(adapter->req_rx_add_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06001766 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001767 ibmvnic_send_crq(adapter, &crq);
1768
1769 crq.request_capability.capability =
1770 cpu_to_be16(REQ_TX_ENTRIES_PER_SUBCRQ);
1771 crq.request_capability.number =
Thomas Falconde89e852016-03-01 10:20:09 -06001772 cpu_to_be64(adapter->req_tx_entries_per_subcrq);
Thomas Falcon901e0402017-02-15 12:17:59 -06001773 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001774 ibmvnic_send_crq(adapter, &crq);
1775
1776 crq.request_capability.capability =
1777 cpu_to_be16(REQ_RX_ADD_ENTRIES_PER_SUBCRQ);
1778 crq.request_capability.number =
Thomas Falconde89e852016-03-01 10:20:09 -06001779 cpu_to_be64(adapter->req_rx_add_entries_per_subcrq);
Thomas Falcon901e0402017-02-15 12:17:59 -06001780 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001781 ibmvnic_send_crq(adapter, &crq);
1782
1783 crq.request_capability.capability = cpu_to_be16(REQ_MTU);
Thomas Falconde89e852016-03-01 10:20:09 -06001784 crq.request_capability.number = cpu_to_be64(adapter->req_mtu);
Thomas Falcon901e0402017-02-15 12:17:59 -06001785 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001786 ibmvnic_send_crq(adapter, &crq);
1787
1788 if (adapter->netdev->flags & IFF_PROMISC) {
1789 if (adapter->promisc_supported) {
1790 crq.request_capability.capability =
1791 cpu_to_be16(PROMISC_REQUESTED);
Thomas Falconde89e852016-03-01 10:20:09 -06001792 crq.request_capability.number = cpu_to_be64(1);
Thomas Falcon901e0402017-02-15 12:17:59 -06001793 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001794 ibmvnic_send_crq(adapter, &crq);
1795 }
1796 } else {
1797 crq.request_capability.capability =
1798 cpu_to_be16(PROMISC_REQUESTED);
Thomas Falconde89e852016-03-01 10:20:09 -06001799 crq.request_capability.number = cpu_to_be64(0);
Thomas Falcon901e0402017-02-15 12:17:59 -06001800 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001801 ibmvnic_send_crq(adapter, &crq);
1802 }
1803
1804 kfree(allqueues);
1805
1806 return;
1807
Thomas Falcon032c5e82015-12-21 11:26:06 -06001808rx_failed:
1809 kfree(adapter->tx_scrq);
1810 adapter->tx_scrq = NULL;
1811tx_failed:
1812 for (i = 0; i < registered_queues; i++)
1813 release_sub_crq_queue(adapter, allqueues[i]);
1814 kfree(allqueues);
1815allqueues_failed:
1816 ibmvnic_remove(adapter->vdev);
1817}
1818
1819static int pending_scrq(struct ibmvnic_adapter *adapter,
1820 struct ibmvnic_sub_crq_queue *scrq)
1821{
1822 union sub_crq *entry = &scrq->msgs[scrq->cur];
1823
1824 if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP || adapter->closing)
1825 return 1;
1826 else
1827 return 0;
1828}
1829
1830static union sub_crq *ibmvnic_next_scrq(struct ibmvnic_adapter *adapter,
1831 struct ibmvnic_sub_crq_queue *scrq)
1832{
1833 union sub_crq *entry;
1834 unsigned long flags;
1835
1836 spin_lock_irqsave(&scrq->lock, flags);
1837 entry = &scrq->msgs[scrq->cur];
1838 if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP) {
1839 if (++scrq->cur == scrq->size)
1840 scrq->cur = 0;
1841 } else {
1842 entry = NULL;
1843 }
1844 spin_unlock_irqrestore(&scrq->lock, flags);
1845
1846 return entry;
1847}
1848
1849static union ibmvnic_crq *ibmvnic_next_crq(struct ibmvnic_adapter *adapter)
1850{
1851 struct ibmvnic_crq_queue *queue = &adapter->crq;
1852 union ibmvnic_crq *crq;
1853
1854 crq = &queue->msgs[queue->cur];
1855 if (crq->generic.first & IBMVNIC_CRQ_CMD_RSP) {
1856 if (++queue->cur == queue->size)
1857 queue->cur = 0;
1858 } else {
1859 crq = NULL;
1860 }
1861
1862 return crq;
1863}
1864
1865static int send_subcrq(struct ibmvnic_adapter *adapter, u64 remote_handle,
1866 union sub_crq *sub_crq)
1867{
1868 unsigned int ua = adapter->vdev->unit_address;
1869 struct device *dev = &adapter->vdev->dev;
1870 u64 *u64_crq = (u64 *)sub_crq;
1871 int rc;
1872
1873 netdev_dbg(adapter->netdev,
1874 "Sending sCRQ %016lx: %016lx %016lx %016lx %016lx\n",
1875 (unsigned long int)cpu_to_be64(remote_handle),
1876 (unsigned long int)cpu_to_be64(u64_crq[0]),
1877 (unsigned long int)cpu_to_be64(u64_crq[1]),
1878 (unsigned long int)cpu_to_be64(u64_crq[2]),
1879 (unsigned long int)cpu_to_be64(u64_crq[3]));
1880
1881 /* Make sure the hypervisor sees the complete request */
1882 mb();
1883
1884 rc = plpar_hcall_norets(H_SEND_SUB_CRQ, ua,
1885 cpu_to_be64(remote_handle),
1886 cpu_to_be64(u64_crq[0]),
1887 cpu_to_be64(u64_crq[1]),
1888 cpu_to_be64(u64_crq[2]),
1889 cpu_to_be64(u64_crq[3]));
1890
1891 if (rc) {
1892 if (rc == H_CLOSED)
1893 dev_warn(dev, "CRQ Queue closed\n");
1894 dev_err(dev, "Send error (rc=%d)\n", rc);
1895 }
1896
1897 return rc;
1898}
1899
Thomas Falconad7775d2016-04-01 17:20:34 -05001900static int send_subcrq_indirect(struct ibmvnic_adapter *adapter,
1901 u64 remote_handle, u64 ioba, u64 num_entries)
1902{
1903 unsigned int ua = adapter->vdev->unit_address;
1904 struct device *dev = &adapter->vdev->dev;
1905 int rc;
1906
1907 /* Make sure the hypervisor sees the complete request */
1908 mb();
1909 rc = plpar_hcall_norets(H_SEND_SUB_CRQ_INDIRECT, ua,
1910 cpu_to_be64(remote_handle),
1911 ioba, num_entries);
1912
1913 if (rc) {
1914 if (rc == H_CLOSED)
1915 dev_warn(dev, "CRQ Queue closed\n");
1916 dev_err(dev, "Send (indirect) error (rc=%d)\n", rc);
1917 }
1918
1919 return rc;
1920}
1921
Thomas Falcon032c5e82015-12-21 11:26:06 -06001922static int ibmvnic_send_crq(struct ibmvnic_adapter *adapter,
1923 union ibmvnic_crq *crq)
1924{
1925 unsigned int ua = adapter->vdev->unit_address;
1926 struct device *dev = &adapter->vdev->dev;
1927 u64 *u64_crq = (u64 *)crq;
1928 int rc;
1929
1930 netdev_dbg(adapter->netdev, "Sending CRQ: %016lx %016lx\n",
1931 (unsigned long int)cpu_to_be64(u64_crq[0]),
1932 (unsigned long int)cpu_to_be64(u64_crq[1]));
1933
1934 /* Make sure the hypervisor sees the complete request */
1935 mb();
1936
1937 rc = plpar_hcall_norets(H_SEND_CRQ, ua,
1938 cpu_to_be64(u64_crq[0]),
1939 cpu_to_be64(u64_crq[1]));
1940
1941 if (rc) {
1942 if (rc == H_CLOSED)
1943 dev_warn(dev, "CRQ Queue closed\n");
1944 dev_warn(dev, "Send error (rc=%d)\n", rc);
1945 }
1946
1947 return rc;
1948}
1949
1950static int ibmvnic_send_crq_init(struct ibmvnic_adapter *adapter)
1951{
1952 union ibmvnic_crq crq;
1953
1954 memset(&crq, 0, sizeof(crq));
1955 crq.generic.first = IBMVNIC_CRQ_INIT_CMD;
1956 crq.generic.cmd = IBMVNIC_CRQ_INIT;
1957 netdev_dbg(adapter->netdev, "Sending CRQ init\n");
1958
1959 return ibmvnic_send_crq(adapter, &crq);
1960}
1961
1962static int ibmvnic_send_crq_init_complete(struct ibmvnic_adapter *adapter)
1963{
1964 union ibmvnic_crq crq;
1965
1966 memset(&crq, 0, sizeof(crq));
1967 crq.generic.first = IBMVNIC_CRQ_INIT_CMD;
1968 crq.generic.cmd = IBMVNIC_CRQ_INIT_COMPLETE;
1969 netdev_dbg(adapter->netdev, "Sending CRQ init complete\n");
1970
1971 return ibmvnic_send_crq(adapter, &crq);
1972}
1973
1974static int send_version_xchg(struct ibmvnic_adapter *adapter)
1975{
1976 union ibmvnic_crq crq;
1977
1978 memset(&crq, 0, sizeof(crq));
1979 crq.version_exchange.first = IBMVNIC_CRQ_CMD;
1980 crq.version_exchange.cmd = VERSION_EXCHANGE;
1981 crq.version_exchange.version = cpu_to_be16(ibmvnic_version);
1982
1983 return ibmvnic_send_crq(adapter, &crq);
1984}
1985
1986static void send_login(struct ibmvnic_adapter *adapter)
1987{
1988 struct ibmvnic_login_rsp_buffer *login_rsp_buffer;
1989 struct ibmvnic_login_buffer *login_buffer;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001990 struct device *dev = &adapter->vdev->dev;
1991 dma_addr_t rsp_buffer_token;
1992 dma_addr_t buffer_token;
1993 size_t rsp_buffer_size;
1994 union ibmvnic_crq crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001995 size_t buffer_size;
1996 __be64 *tx_list_p;
1997 __be64 *rx_list_p;
1998 int i;
1999
2000 buffer_size =
2001 sizeof(struct ibmvnic_login_buffer) +
2002 sizeof(u64) * (adapter->req_tx_queues + adapter->req_rx_queues);
2003
2004 login_buffer = kmalloc(buffer_size, GFP_ATOMIC);
2005 if (!login_buffer)
2006 goto buf_alloc_failed;
2007
2008 buffer_token = dma_map_single(dev, login_buffer, buffer_size,
2009 DMA_TO_DEVICE);
2010 if (dma_mapping_error(dev, buffer_token)) {
2011 dev_err(dev, "Couldn't map login buffer\n");
2012 goto buf_map_failed;
2013 }
2014
John Allen498cd8e2016-04-06 11:49:55 -05002015 rsp_buffer_size = sizeof(struct ibmvnic_login_rsp_buffer) +
2016 sizeof(u64) * adapter->req_tx_queues +
2017 sizeof(u64) * adapter->req_rx_queues +
2018 sizeof(u64) * adapter->req_rx_queues +
2019 sizeof(u8) * IBMVNIC_TX_DESC_VERSIONS;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002020
2021 login_rsp_buffer = kmalloc(rsp_buffer_size, GFP_ATOMIC);
2022 if (!login_rsp_buffer)
2023 goto buf_rsp_alloc_failed;
2024
2025 rsp_buffer_token = dma_map_single(dev, login_rsp_buffer,
2026 rsp_buffer_size, DMA_FROM_DEVICE);
2027 if (dma_mapping_error(dev, rsp_buffer_token)) {
2028 dev_err(dev, "Couldn't map login rsp buffer\n");
2029 goto buf_rsp_map_failed;
2030 }
Nathan Fontenot661a2622017-04-19 13:44:58 -04002031
Thomas Falcon032c5e82015-12-21 11:26:06 -06002032 adapter->login_buf = login_buffer;
2033 adapter->login_buf_token = buffer_token;
2034 adapter->login_buf_sz = buffer_size;
2035 adapter->login_rsp_buf = login_rsp_buffer;
2036 adapter->login_rsp_buf_token = rsp_buffer_token;
2037 adapter->login_rsp_buf_sz = rsp_buffer_size;
2038
2039 login_buffer->len = cpu_to_be32(buffer_size);
2040 login_buffer->version = cpu_to_be32(INITIAL_VERSION_LB);
2041 login_buffer->num_txcomp_subcrqs = cpu_to_be32(adapter->req_tx_queues);
2042 login_buffer->off_txcomp_subcrqs =
2043 cpu_to_be32(sizeof(struct ibmvnic_login_buffer));
2044 login_buffer->num_rxcomp_subcrqs = cpu_to_be32(adapter->req_rx_queues);
2045 login_buffer->off_rxcomp_subcrqs =
2046 cpu_to_be32(sizeof(struct ibmvnic_login_buffer) +
2047 sizeof(u64) * adapter->req_tx_queues);
2048 login_buffer->login_rsp_ioba = cpu_to_be32(rsp_buffer_token);
2049 login_buffer->login_rsp_len = cpu_to_be32(rsp_buffer_size);
2050
2051 tx_list_p = (__be64 *)((char *)login_buffer +
2052 sizeof(struct ibmvnic_login_buffer));
2053 rx_list_p = (__be64 *)((char *)login_buffer +
2054 sizeof(struct ibmvnic_login_buffer) +
2055 sizeof(u64) * adapter->req_tx_queues);
2056
2057 for (i = 0; i < adapter->req_tx_queues; i++) {
2058 if (adapter->tx_scrq[i]) {
2059 tx_list_p[i] = cpu_to_be64(adapter->tx_scrq[i]->
2060 crq_num);
2061 }
2062 }
2063
2064 for (i = 0; i < adapter->req_rx_queues; i++) {
2065 if (adapter->rx_scrq[i]) {
2066 rx_list_p[i] = cpu_to_be64(adapter->rx_scrq[i]->
2067 crq_num);
2068 }
2069 }
2070
2071 netdev_dbg(adapter->netdev, "Login Buffer:\n");
2072 for (i = 0; i < (adapter->login_buf_sz - 1) / 8 + 1; i++) {
2073 netdev_dbg(adapter->netdev, "%016lx\n",
2074 ((unsigned long int *)(adapter->login_buf))[i]);
2075 }
2076
2077 memset(&crq, 0, sizeof(crq));
2078 crq.login.first = IBMVNIC_CRQ_CMD;
2079 crq.login.cmd = LOGIN;
2080 crq.login.ioba = cpu_to_be32(buffer_token);
2081 crq.login.len = cpu_to_be32(buffer_size);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002082 ibmvnic_send_crq(adapter, &crq);
2083
2084 return;
2085
Thomas Falcon032c5e82015-12-21 11:26:06 -06002086buf_rsp_map_failed:
2087 kfree(login_rsp_buffer);
2088buf_rsp_alloc_failed:
2089 dma_unmap_single(dev, buffer_token, buffer_size, DMA_TO_DEVICE);
2090buf_map_failed:
2091 kfree(login_buffer);
2092buf_alloc_failed:
2093 return;
2094}
2095
2096static void send_request_map(struct ibmvnic_adapter *adapter, dma_addr_t addr,
2097 u32 len, u8 map_id)
2098{
2099 union ibmvnic_crq crq;
2100
2101 memset(&crq, 0, sizeof(crq));
2102 crq.request_map.first = IBMVNIC_CRQ_CMD;
2103 crq.request_map.cmd = REQUEST_MAP;
2104 crq.request_map.map_id = map_id;
2105 crq.request_map.ioba = cpu_to_be32(addr);
2106 crq.request_map.len = cpu_to_be32(len);
2107 ibmvnic_send_crq(adapter, &crq);
2108}
2109
2110static void send_request_unmap(struct ibmvnic_adapter *adapter, u8 map_id)
2111{
2112 union ibmvnic_crq crq;
2113
2114 memset(&crq, 0, sizeof(crq));
2115 crq.request_unmap.first = IBMVNIC_CRQ_CMD;
2116 crq.request_unmap.cmd = REQUEST_UNMAP;
2117 crq.request_unmap.map_id = map_id;
2118 ibmvnic_send_crq(adapter, &crq);
2119}
2120
2121static void send_map_query(struct ibmvnic_adapter *adapter)
2122{
2123 union ibmvnic_crq crq;
2124
2125 memset(&crq, 0, sizeof(crq));
2126 crq.query_map.first = IBMVNIC_CRQ_CMD;
2127 crq.query_map.cmd = QUERY_MAP;
2128 ibmvnic_send_crq(adapter, &crq);
2129}
2130
2131/* Send a series of CRQs requesting various capabilities of the VNIC server */
2132static void send_cap_queries(struct ibmvnic_adapter *adapter)
2133{
2134 union ibmvnic_crq crq;
2135
Thomas Falcon901e0402017-02-15 12:17:59 -06002136 atomic_set(&adapter->running_cap_crqs, 0);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002137 memset(&crq, 0, sizeof(crq));
2138 crq.query_capability.first = IBMVNIC_CRQ_CMD;
2139 crq.query_capability.cmd = QUERY_CAPABILITY;
2140
2141 crq.query_capability.capability = cpu_to_be16(MIN_TX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002142 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002143 ibmvnic_send_crq(adapter, &crq);
2144
2145 crq.query_capability.capability = cpu_to_be16(MIN_RX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002146 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002147 ibmvnic_send_crq(adapter, &crq);
2148
2149 crq.query_capability.capability = cpu_to_be16(MIN_RX_ADD_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002150 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002151 ibmvnic_send_crq(adapter, &crq);
2152
2153 crq.query_capability.capability = cpu_to_be16(MAX_TX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002154 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002155 ibmvnic_send_crq(adapter, &crq);
2156
2157 crq.query_capability.capability = cpu_to_be16(MAX_RX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002158 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002159 ibmvnic_send_crq(adapter, &crq);
2160
2161 crq.query_capability.capability = cpu_to_be16(MAX_RX_ADD_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002162 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002163 ibmvnic_send_crq(adapter, &crq);
2164
2165 crq.query_capability.capability =
2166 cpu_to_be16(MIN_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06002167 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002168 ibmvnic_send_crq(adapter, &crq);
2169
2170 crq.query_capability.capability =
2171 cpu_to_be16(MIN_RX_ADD_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06002172 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002173 ibmvnic_send_crq(adapter, &crq);
2174
2175 crq.query_capability.capability =
2176 cpu_to_be16(MAX_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06002177 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002178 ibmvnic_send_crq(adapter, &crq);
2179
2180 crq.query_capability.capability =
2181 cpu_to_be16(MAX_RX_ADD_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06002182 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002183 ibmvnic_send_crq(adapter, &crq);
2184
2185 crq.query_capability.capability = cpu_to_be16(TCP_IP_OFFLOAD);
Thomas Falcon901e0402017-02-15 12:17:59 -06002186 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002187 ibmvnic_send_crq(adapter, &crq);
2188
2189 crq.query_capability.capability = cpu_to_be16(PROMISC_SUPPORTED);
Thomas Falcon901e0402017-02-15 12:17:59 -06002190 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002191 ibmvnic_send_crq(adapter, &crq);
2192
2193 crq.query_capability.capability = cpu_to_be16(MIN_MTU);
Thomas Falcon901e0402017-02-15 12:17:59 -06002194 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002195 ibmvnic_send_crq(adapter, &crq);
2196
2197 crq.query_capability.capability = cpu_to_be16(MAX_MTU);
Thomas Falcon901e0402017-02-15 12:17:59 -06002198 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002199 ibmvnic_send_crq(adapter, &crq);
2200
2201 crq.query_capability.capability = cpu_to_be16(MAX_MULTICAST_FILTERS);
Thomas Falcon901e0402017-02-15 12:17:59 -06002202 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002203 ibmvnic_send_crq(adapter, &crq);
2204
2205 crq.query_capability.capability = cpu_to_be16(VLAN_HEADER_INSERTION);
Thomas Falcon901e0402017-02-15 12:17:59 -06002206 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002207 ibmvnic_send_crq(adapter, &crq);
2208
2209 crq.query_capability.capability = cpu_to_be16(MAX_TX_SG_ENTRIES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002210 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002211 ibmvnic_send_crq(adapter, &crq);
2212
2213 crq.query_capability.capability = cpu_to_be16(RX_SG_SUPPORTED);
Thomas Falcon901e0402017-02-15 12:17:59 -06002214 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002215 ibmvnic_send_crq(adapter, &crq);
2216
2217 crq.query_capability.capability = cpu_to_be16(OPT_TX_COMP_SUB_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002218 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002219 ibmvnic_send_crq(adapter, &crq);
2220
2221 crq.query_capability.capability = cpu_to_be16(OPT_RX_COMP_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002222 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002223 ibmvnic_send_crq(adapter, &crq);
2224
2225 crq.query_capability.capability =
2226 cpu_to_be16(OPT_RX_BUFADD_Q_PER_RX_COMP_Q);
Thomas Falcon901e0402017-02-15 12:17:59 -06002227 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002228 ibmvnic_send_crq(adapter, &crq);
2229
2230 crq.query_capability.capability =
2231 cpu_to_be16(OPT_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06002232 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002233 ibmvnic_send_crq(adapter, &crq);
2234
2235 crq.query_capability.capability =
2236 cpu_to_be16(OPT_RXBA_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06002237 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002238 ibmvnic_send_crq(adapter, &crq);
2239
2240 crq.query_capability.capability = cpu_to_be16(TX_RX_DESC_REQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06002241 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002242 ibmvnic_send_crq(adapter, &crq);
2243}
2244
2245static void handle_query_ip_offload_rsp(struct ibmvnic_adapter *adapter)
2246{
2247 struct device *dev = &adapter->vdev->dev;
2248 struct ibmvnic_query_ip_offload_buffer *buf = &adapter->ip_offload_buf;
2249 union ibmvnic_crq crq;
2250 int i;
2251
2252 dma_unmap_single(dev, adapter->ip_offload_tok,
2253 sizeof(adapter->ip_offload_buf), DMA_FROM_DEVICE);
2254
2255 netdev_dbg(adapter->netdev, "Query IP Offload Buffer:\n");
2256 for (i = 0; i < (sizeof(adapter->ip_offload_buf) - 1) / 8 + 1; i++)
2257 netdev_dbg(adapter->netdev, "%016lx\n",
2258 ((unsigned long int *)(buf))[i]);
2259
2260 netdev_dbg(adapter->netdev, "ipv4_chksum = %d\n", buf->ipv4_chksum);
2261 netdev_dbg(adapter->netdev, "ipv6_chksum = %d\n", buf->ipv6_chksum);
2262 netdev_dbg(adapter->netdev, "tcp_ipv4_chksum = %d\n",
2263 buf->tcp_ipv4_chksum);
2264 netdev_dbg(adapter->netdev, "tcp_ipv6_chksum = %d\n",
2265 buf->tcp_ipv6_chksum);
2266 netdev_dbg(adapter->netdev, "udp_ipv4_chksum = %d\n",
2267 buf->udp_ipv4_chksum);
2268 netdev_dbg(adapter->netdev, "udp_ipv6_chksum = %d\n",
2269 buf->udp_ipv6_chksum);
2270 netdev_dbg(adapter->netdev, "large_tx_ipv4 = %d\n",
2271 buf->large_tx_ipv4);
2272 netdev_dbg(adapter->netdev, "large_tx_ipv6 = %d\n",
2273 buf->large_tx_ipv6);
2274 netdev_dbg(adapter->netdev, "large_rx_ipv4 = %d\n",
2275 buf->large_rx_ipv4);
2276 netdev_dbg(adapter->netdev, "large_rx_ipv6 = %d\n",
2277 buf->large_rx_ipv6);
2278 netdev_dbg(adapter->netdev, "max_ipv4_hdr_sz = %d\n",
2279 buf->max_ipv4_header_size);
2280 netdev_dbg(adapter->netdev, "max_ipv6_hdr_sz = %d\n",
2281 buf->max_ipv6_header_size);
2282 netdev_dbg(adapter->netdev, "max_tcp_hdr_size = %d\n",
2283 buf->max_tcp_header_size);
2284 netdev_dbg(adapter->netdev, "max_udp_hdr_size = %d\n",
2285 buf->max_udp_header_size);
2286 netdev_dbg(adapter->netdev, "max_large_tx_size = %d\n",
2287 buf->max_large_tx_size);
2288 netdev_dbg(adapter->netdev, "max_large_rx_size = %d\n",
2289 buf->max_large_rx_size);
2290 netdev_dbg(adapter->netdev, "ipv6_ext_hdr = %d\n",
2291 buf->ipv6_extension_header);
2292 netdev_dbg(adapter->netdev, "tcp_pseudosum_req = %d\n",
2293 buf->tcp_pseudosum_req);
2294 netdev_dbg(adapter->netdev, "num_ipv6_ext_hd = %d\n",
2295 buf->num_ipv6_ext_headers);
2296 netdev_dbg(adapter->netdev, "off_ipv6_ext_hd = %d\n",
2297 buf->off_ipv6_ext_headers);
2298
2299 adapter->ip_offload_ctrl_tok =
2300 dma_map_single(dev, &adapter->ip_offload_ctrl,
2301 sizeof(adapter->ip_offload_ctrl), DMA_TO_DEVICE);
2302
2303 if (dma_mapping_error(dev, adapter->ip_offload_ctrl_tok)) {
2304 dev_err(dev, "Couldn't map ip offload control buffer\n");
2305 return;
2306 }
2307
2308 adapter->ip_offload_ctrl.version = cpu_to_be32(INITIAL_VERSION_IOB);
2309 adapter->ip_offload_ctrl.tcp_ipv4_chksum = buf->tcp_ipv4_chksum;
2310 adapter->ip_offload_ctrl.udp_ipv4_chksum = buf->udp_ipv4_chksum;
2311 adapter->ip_offload_ctrl.tcp_ipv6_chksum = buf->tcp_ipv6_chksum;
2312 adapter->ip_offload_ctrl.udp_ipv6_chksum = buf->udp_ipv6_chksum;
2313
2314 /* large_tx/rx disabled for now, additional features needed */
2315 adapter->ip_offload_ctrl.large_tx_ipv4 = 0;
2316 adapter->ip_offload_ctrl.large_tx_ipv6 = 0;
2317 adapter->ip_offload_ctrl.large_rx_ipv4 = 0;
2318 adapter->ip_offload_ctrl.large_rx_ipv6 = 0;
2319
2320 adapter->netdev->features = NETIF_F_GSO;
2321
2322 if (buf->tcp_ipv4_chksum || buf->udp_ipv4_chksum)
2323 adapter->netdev->features |= NETIF_F_IP_CSUM;
2324
2325 if (buf->tcp_ipv6_chksum || buf->udp_ipv6_chksum)
2326 adapter->netdev->features |= NETIF_F_IPV6_CSUM;
2327
Thomas Falcon9be02cd2016-04-01 17:20:35 -05002328 if ((adapter->netdev->features &
2329 (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)))
2330 adapter->netdev->features |= NETIF_F_RXCSUM;
2331
Thomas Falcon032c5e82015-12-21 11:26:06 -06002332 memset(&crq, 0, sizeof(crq));
2333 crq.control_ip_offload.first = IBMVNIC_CRQ_CMD;
2334 crq.control_ip_offload.cmd = CONTROL_IP_OFFLOAD;
2335 crq.control_ip_offload.len =
2336 cpu_to_be32(sizeof(adapter->ip_offload_ctrl));
2337 crq.control_ip_offload.ioba = cpu_to_be32(adapter->ip_offload_ctrl_tok);
2338 ibmvnic_send_crq(adapter, &crq);
2339}
2340
2341static void handle_error_info_rsp(union ibmvnic_crq *crq,
2342 struct ibmvnic_adapter *adapter)
2343{
2344 struct device *dev = &adapter->vdev->dev;
Wei Yongjun96183182016-06-27 20:48:53 +08002345 struct ibmvnic_error_buff *error_buff, *tmp;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002346 unsigned long flags;
2347 bool found = false;
2348 int i;
2349
2350 if (!crq->request_error_rsp.rc.code) {
2351 dev_info(dev, "Request Error Rsp returned with rc=%x\n",
2352 crq->request_error_rsp.rc.code);
2353 return;
2354 }
2355
2356 spin_lock_irqsave(&adapter->error_list_lock, flags);
Wei Yongjun96183182016-06-27 20:48:53 +08002357 list_for_each_entry_safe(error_buff, tmp, &adapter->errors, list)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002358 if (error_buff->error_id == crq->request_error_rsp.error_id) {
2359 found = true;
2360 list_del(&error_buff->list);
2361 break;
2362 }
2363 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
2364
2365 if (!found) {
2366 dev_err(dev, "Couldn't find error id %x\n",
Thomas Falcon75224c92017-02-15 10:33:33 -06002367 be32_to_cpu(crq->request_error_rsp.error_id));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002368 return;
2369 }
2370
2371 dev_err(dev, "Detailed info for error id %x:",
Thomas Falcon75224c92017-02-15 10:33:33 -06002372 be32_to_cpu(crq->request_error_rsp.error_id));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002373
2374 for (i = 0; i < error_buff->len; i++) {
2375 pr_cont("%02x", (int)error_buff->buff[i]);
2376 if (i % 8 == 7)
2377 pr_cont(" ");
2378 }
2379 pr_cont("\n");
2380
2381 dma_unmap_single(dev, error_buff->dma, error_buff->len,
2382 DMA_FROM_DEVICE);
2383 kfree(error_buff->buff);
2384 kfree(error_buff);
2385}
2386
Thomas Falcon032c5e82015-12-21 11:26:06 -06002387static void handle_error_indication(union ibmvnic_crq *crq,
2388 struct ibmvnic_adapter *adapter)
2389{
2390 int detail_len = be32_to_cpu(crq->error_indication.detail_error_sz);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002391 struct device *dev = &adapter->vdev->dev;
2392 struct ibmvnic_error_buff *error_buff;
2393 union ibmvnic_crq new_crq;
2394 unsigned long flags;
2395
2396 dev_err(dev, "Firmware reports %serror id %x, cause %d\n",
2397 crq->error_indication.
2398 flags & IBMVNIC_FATAL_ERROR ? "FATAL " : "",
Thomas Falcon75224c92017-02-15 10:33:33 -06002399 be32_to_cpu(crq->error_indication.error_id),
2400 be16_to_cpu(crq->error_indication.error_cause));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002401
2402 error_buff = kmalloc(sizeof(*error_buff), GFP_ATOMIC);
2403 if (!error_buff)
2404 return;
2405
2406 error_buff->buff = kmalloc(detail_len, GFP_ATOMIC);
2407 if (!error_buff->buff) {
2408 kfree(error_buff);
2409 return;
2410 }
2411
2412 error_buff->dma = dma_map_single(dev, error_buff->buff, detail_len,
2413 DMA_FROM_DEVICE);
2414 if (dma_mapping_error(dev, error_buff->dma)) {
2415 if (!firmware_has_feature(FW_FEATURE_CMO))
2416 dev_err(dev, "Couldn't map error buffer\n");
2417 kfree(error_buff->buff);
2418 kfree(error_buff);
2419 return;
2420 }
2421
Thomas Falcon032c5e82015-12-21 11:26:06 -06002422 error_buff->len = detail_len;
2423 error_buff->error_id = crq->error_indication.error_id;
2424
2425 spin_lock_irqsave(&adapter->error_list_lock, flags);
2426 list_add_tail(&error_buff->list, &adapter->errors);
2427 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
2428
2429 memset(&new_crq, 0, sizeof(new_crq));
2430 new_crq.request_error_info.first = IBMVNIC_CRQ_CMD;
2431 new_crq.request_error_info.cmd = REQUEST_ERROR_INFO;
2432 new_crq.request_error_info.ioba = cpu_to_be32(error_buff->dma);
2433 new_crq.request_error_info.len = cpu_to_be32(detail_len);
2434 new_crq.request_error_info.error_id = crq->error_indication.error_id;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002435 ibmvnic_send_crq(adapter, &new_crq);
2436}
2437
2438static void handle_change_mac_rsp(union ibmvnic_crq *crq,
2439 struct ibmvnic_adapter *adapter)
2440{
2441 struct net_device *netdev = adapter->netdev;
2442 struct device *dev = &adapter->vdev->dev;
2443 long rc;
2444
2445 rc = crq->change_mac_addr_rsp.rc.code;
2446 if (rc) {
2447 dev_err(dev, "Error %ld in CHANGE_MAC_ADDR_RSP\n", rc);
2448 return;
2449 }
2450 memcpy(netdev->dev_addr, &crq->change_mac_addr_rsp.mac_addr[0],
2451 ETH_ALEN);
2452}
2453
2454static void handle_request_cap_rsp(union ibmvnic_crq *crq,
2455 struct ibmvnic_adapter *adapter)
2456{
2457 struct device *dev = &adapter->vdev->dev;
2458 u64 *req_value;
2459 char *name;
2460
Thomas Falcon901e0402017-02-15 12:17:59 -06002461 atomic_dec(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002462 switch (be16_to_cpu(crq->request_capability_rsp.capability)) {
2463 case REQ_TX_QUEUES:
2464 req_value = &adapter->req_tx_queues;
2465 name = "tx";
2466 break;
2467 case REQ_RX_QUEUES:
2468 req_value = &adapter->req_rx_queues;
2469 name = "rx";
2470 break;
2471 case REQ_RX_ADD_QUEUES:
2472 req_value = &adapter->req_rx_add_queues;
2473 name = "rx_add";
2474 break;
2475 case REQ_TX_ENTRIES_PER_SUBCRQ:
2476 req_value = &adapter->req_tx_entries_per_subcrq;
2477 name = "tx_entries_per_subcrq";
2478 break;
2479 case REQ_RX_ADD_ENTRIES_PER_SUBCRQ:
2480 req_value = &adapter->req_rx_add_entries_per_subcrq;
2481 name = "rx_add_entries_per_subcrq";
2482 break;
2483 case REQ_MTU:
2484 req_value = &adapter->req_mtu;
2485 name = "mtu";
2486 break;
2487 case PROMISC_REQUESTED:
2488 req_value = &adapter->promisc;
2489 name = "promisc";
2490 break;
2491 default:
2492 dev_err(dev, "Got invalid cap request rsp %d\n",
2493 crq->request_capability.capability);
2494 return;
2495 }
2496
2497 switch (crq->request_capability_rsp.rc.code) {
2498 case SUCCESS:
2499 break;
2500 case PARTIALSUCCESS:
2501 dev_info(dev, "req=%lld, rsp=%ld in %s queue, retrying.\n",
2502 *req_value,
Thomas Falcon28f4d162017-02-15 10:32:11 -06002503 (long int)be64_to_cpu(crq->request_capability_rsp.
Thomas Falcon032c5e82015-12-21 11:26:06 -06002504 number), name);
Nathan Fontenotb5108882017-03-30 02:49:18 -04002505 release_sub_crqs(adapter);
Thomas Falcon28f4d162017-02-15 10:32:11 -06002506 *req_value = be64_to_cpu(crq->request_capability_rsp.number);
Thomas Falconea22d512016-07-06 15:35:17 -05002507 init_sub_crqs(adapter, 1);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002508 return;
2509 default:
2510 dev_err(dev, "Error %d in request cap rsp\n",
2511 crq->request_capability_rsp.rc.code);
2512 return;
2513 }
2514
2515 /* Done receiving requested capabilities, query IP offload support */
Thomas Falcon901e0402017-02-15 12:17:59 -06002516 if (atomic_read(&adapter->running_cap_crqs) == 0) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06002517 union ibmvnic_crq newcrq;
2518 int buf_sz = sizeof(struct ibmvnic_query_ip_offload_buffer);
2519 struct ibmvnic_query_ip_offload_buffer *ip_offload_buf =
2520 &adapter->ip_offload_buf;
2521
Thomas Falcon249168a2017-02-15 12:18:00 -06002522 adapter->wait_capability = false;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002523 adapter->ip_offload_tok = dma_map_single(dev, ip_offload_buf,
2524 buf_sz,
2525 DMA_FROM_DEVICE);
2526
2527 if (dma_mapping_error(dev, adapter->ip_offload_tok)) {
2528 if (!firmware_has_feature(FW_FEATURE_CMO))
2529 dev_err(dev, "Couldn't map offload buffer\n");
2530 return;
2531 }
2532
2533 memset(&newcrq, 0, sizeof(newcrq));
2534 newcrq.query_ip_offload.first = IBMVNIC_CRQ_CMD;
2535 newcrq.query_ip_offload.cmd = QUERY_IP_OFFLOAD;
2536 newcrq.query_ip_offload.len = cpu_to_be32(buf_sz);
2537 newcrq.query_ip_offload.ioba =
2538 cpu_to_be32(adapter->ip_offload_tok);
2539
2540 ibmvnic_send_crq(adapter, &newcrq);
2541 }
2542}
2543
2544static int handle_login_rsp(union ibmvnic_crq *login_rsp_crq,
2545 struct ibmvnic_adapter *adapter)
2546{
2547 struct device *dev = &adapter->vdev->dev;
2548 struct ibmvnic_login_rsp_buffer *login_rsp = adapter->login_rsp_buf;
2549 struct ibmvnic_login_buffer *login = adapter->login_buf;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002550 int i;
2551
2552 dma_unmap_single(dev, adapter->login_buf_token, adapter->login_buf_sz,
2553 DMA_BIDIRECTIONAL);
2554 dma_unmap_single(dev, adapter->login_rsp_buf_token,
2555 adapter->login_rsp_buf_sz, DMA_BIDIRECTIONAL);
2556
John Allen498cd8e2016-04-06 11:49:55 -05002557 /* If the number of queues requested can't be allocated by the
2558 * server, the login response will return with code 1. We will need
2559 * to resend the login buffer with fewer queues requested.
2560 */
2561 if (login_rsp_crq->generic.rc.code) {
2562 adapter->renegotiate = true;
2563 complete(&adapter->init_done);
2564 return 0;
2565 }
2566
Thomas Falcon032c5e82015-12-21 11:26:06 -06002567 netdev_dbg(adapter->netdev, "Login Response Buffer:\n");
2568 for (i = 0; i < (adapter->login_rsp_buf_sz - 1) / 8 + 1; i++) {
2569 netdev_dbg(adapter->netdev, "%016lx\n",
2570 ((unsigned long int *)(adapter->login_rsp_buf))[i]);
2571 }
2572
2573 /* Sanity checks */
2574 if (login->num_txcomp_subcrqs != login_rsp->num_txsubm_subcrqs ||
2575 (be32_to_cpu(login->num_rxcomp_subcrqs) *
2576 adapter->req_rx_add_queues !=
2577 be32_to_cpu(login_rsp->num_rxadd_subcrqs))) {
2578 dev_err(dev, "FATAL: Inconsistent login and login rsp\n");
2579 ibmvnic_remove(adapter->vdev);
2580 return -EIO;
2581 }
2582 complete(&adapter->init_done);
2583
Thomas Falcon032c5e82015-12-21 11:26:06 -06002584 return 0;
2585}
2586
2587static void handle_request_map_rsp(union ibmvnic_crq *crq,
2588 struct ibmvnic_adapter *adapter)
2589{
2590 struct device *dev = &adapter->vdev->dev;
2591 u8 map_id = crq->request_map_rsp.map_id;
2592 int tx_subcrqs;
2593 int rx_subcrqs;
2594 long rc;
2595 int i;
2596
2597 tx_subcrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
2598 rx_subcrqs = be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
2599
2600 rc = crq->request_map_rsp.rc.code;
2601 if (rc) {
2602 dev_err(dev, "Error %ld in REQUEST_MAP_RSP\n", rc);
2603 adapter->map_id--;
2604 /* need to find and zero tx/rx_pool map_id */
2605 for (i = 0; i < tx_subcrqs; i++) {
2606 if (adapter->tx_pool[i].long_term_buff.map_id == map_id)
2607 adapter->tx_pool[i].long_term_buff.map_id = 0;
2608 }
2609 for (i = 0; i < rx_subcrqs; i++) {
2610 if (adapter->rx_pool[i].long_term_buff.map_id == map_id)
2611 adapter->rx_pool[i].long_term_buff.map_id = 0;
2612 }
2613 }
2614 complete(&adapter->fw_done);
2615}
2616
2617static void handle_request_unmap_rsp(union ibmvnic_crq *crq,
2618 struct ibmvnic_adapter *adapter)
2619{
2620 struct device *dev = &adapter->vdev->dev;
2621 long rc;
2622
2623 rc = crq->request_unmap_rsp.rc.code;
2624 if (rc)
2625 dev_err(dev, "Error %ld in REQUEST_UNMAP_RSP\n", rc);
2626}
2627
2628static void handle_query_map_rsp(union ibmvnic_crq *crq,
2629 struct ibmvnic_adapter *adapter)
2630{
2631 struct net_device *netdev = adapter->netdev;
2632 struct device *dev = &adapter->vdev->dev;
2633 long rc;
2634
2635 rc = crq->query_map_rsp.rc.code;
2636 if (rc) {
2637 dev_err(dev, "Error %ld in QUERY_MAP_RSP\n", rc);
2638 return;
2639 }
2640 netdev_dbg(netdev, "page_size = %d\ntot_pages = %d\nfree_pages = %d\n",
2641 crq->query_map_rsp.page_size, crq->query_map_rsp.tot_pages,
2642 crq->query_map_rsp.free_pages);
2643}
2644
2645static void handle_query_cap_rsp(union ibmvnic_crq *crq,
2646 struct ibmvnic_adapter *adapter)
2647{
2648 struct net_device *netdev = adapter->netdev;
2649 struct device *dev = &adapter->vdev->dev;
2650 long rc;
2651
Thomas Falcon901e0402017-02-15 12:17:59 -06002652 atomic_dec(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002653 netdev_dbg(netdev, "Outstanding queries: %d\n",
Thomas Falcon901e0402017-02-15 12:17:59 -06002654 atomic_read(&adapter->running_cap_crqs));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002655 rc = crq->query_capability.rc.code;
2656 if (rc) {
2657 dev_err(dev, "Error %ld in QUERY_CAP_RSP\n", rc);
2658 goto out;
2659 }
2660
2661 switch (be16_to_cpu(crq->query_capability.capability)) {
2662 case MIN_TX_QUEUES:
2663 adapter->min_tx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06002664 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002665 netdev_dbg(netdev, "min_tx_queues = %lld\n",
2666 adapter->min_tx_queues);
2667 break;
2668 case MIN_RX_QUEUES:
2669 adapter->min_rx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06002670 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002671 netdev_dbg(netdev, "min_rx_queues = %lld\n",
2672 adapter->min_rx_queues);
2673 break;
2674 case MIN_RX_ADD_QUEUES:
2675 adapter->min_rx_add_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06002676 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002677 netdev_dbg(netdev, "min_rx_add_queues = %lld\n",
2678 adapter->min_rx_add_queues);
2679 break;
2680 case MAX_TX_QUEUES:
2681 adapter->max_tx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06002682 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002683 netdev_dbg(netdev, "max_tx_queues = %lld\n",
2684 adapter->max_tx_queues);
2685 break;
2686 case MAX_RX_QUEUES:
2687 adapter->max_rx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06002688 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002689 netdev_dbg(netdev, "max_rx_queues = %lld\n",
2690 adapter->max_rx_queues);
2691 break;
2692 case MAX_RX_ADD_QUEUES:
2693 adapter->max_rx_add_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06002694 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002695 netdev_dbg(netdev, "max_rx_add_queues = %lld\n",
2696 adapter->max_rx_add_queues);
2697 break;
2698 case MIN_TX_ENTRIES_PER_SUBCRQ:
2699 adapter->min_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06002700 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002701 netdev_dbg(netdev, "min_tx_entries_per_subcrq = %lld\n",
2702 adapter->min_tx_entries_per_subcrq);
2703 break;
2704 case MIN_RX_ADD_ENTRIES_PER_SUBCRQ:
2705 adapter->min_rx_add_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06002706 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002707 netdev_dbg(netdev, "min_rx_add_entrs_per_subcrq = %lld\n",
2708 adapter->min_rx_add_entries_per_subcrq);
2709 break;
2710 case MAX_TX_ENTRIES_PER_SUBCRQ:
2711 adapter->max_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06002712 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002713 netdev_dbg(netdev, "max_tx_entries_per_subcrq = %lld\n",
2714 adapter->max_tx_entries_per_subcrq);
2715 break;
2716 case MAX_RX_ADD_ENTRIES_PER_SUBCRQ:
2717 adapter->max_rx_add_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06002718 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002719 netdev_dbg(netdev, "max_rx_add_entrs_per_subcrq = %lld\n",
2720 adapter->max_rx_add_entries_per_subcrq);
2721 break;
2722 case TCP_IP_OFFLOAD:
2723 adapter->tcp_ip_offload =
Thomas Falconde89e852016-03-01 10:20:09 -06002724 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002725 netdev_dbg(netdev, "tcp_ip_offload = %lld\n",
2726 adapter->tcp_ip_offload);
2727 break;
2728 case PROMISC_SUPPORTED:
2729 adapter->promisc_supported =
Thomas Falconde89e852016-03-01 10:20:09 -06002730 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002731 netdev_dbg(netdev, "promisc_supported = %lld\n",
2732 adapter->promisc_supported);
2733 break;
2734 case MIN_MTU:
Thomas Falconde89e852016-03-01 10:20:09 -06002735 adapter->min_mtu = be64_to_cpu(crq->query_capability.number);
Thomas Falconf39f0d12017-02-14 10:22:59 -06002736 netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002737 netdev_dbg(netdev, "min_mtu = %lld\n", adapter->min_mtu);
2738 break;
2739 case MAX_MTU:
Thomas Falconde89e852016-03-01 10:20:09 -06002740 adapter->max_mtu = be64_to_cpu(crq->query_capability.number);
Thomas Falconf39f0d12017-02-14 10:22:59 -06002741 netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002742 netdev_dbg(netdev, "max_mtu = %lld\n", adapter->max_mtu);
2743 break;
2744 case MAX_MULTICAST_FILTERS:
2745 adapter->max_multicast_filters =
Thomas Falconde89e852016-03-01 10:20:09 -06002746 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002747 netdev_dbg(netdev, "max_multicast_filters = %lld\n",
2748 adapter->max_multicast_filters);
2749 break;
2750 case VLAN_HEADER_INSERTION:
2751 adapter->vlan_header_insertion =
Thomas Falconde89e852016-03-01 10:20:09 -06002752 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002753 if (adapter->vlan_header_insertion)
2754 netdev->features |= NETIF_F_HW_VLAN_STAG_TX;
2755 netdev_dbg(netdev, "vlan_header_insertion = %lld\n",
2756 adapter->vlan_header_insertion);
2757 break;
2758 case MAX_TX_SG_ENTRIES:
2759 adapter->max_tx_sg_entries =
Thomas Falconde89e852016-03-01 10:20:09 -06002760 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002761 netdev_dbg(netdev, "max_tx_sg_entries = %lld\n",
2762 adapter->max_tx_sg_entries);
2763 break;
2764 case RX_SG_SUPPORTED:
2765 adapter->rx_sg_supported =
Thomas Falconde89e852016-03-01 10:20:09 -06002766 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002767 netdev_dbg(netdev, "rx_sg_supported = %lld\n",
2768 adapter->rx_sg_supported);
2769 break;
2770 case OPT_TX_COMP_SUB_QUEUES:
2771 adapter->opt_tx_comp_sub_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06002772 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002773 netdev_dbg(netdev, "opt_tx_comp_sub_queues = %lld\n",
2774 adapter->opt_tx_comp_sub_queues);
2775 break;
2776 case OPT_RX_COMP_QUEUES:
2777 adapter->opt_rx_comp_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06002778 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002779 netdev_dbg(netdev, "opt_rx_comp_queues = %lld\n",
2780 adapter->opt_rx_comp_queues);
2781 break;
2782 case OPT_RX_BUFADD_Q_PER_RX_COMP_Q:
2783 adapter->opt_rx_bufadd_q_per_rx_comp_q =
Thomas Falconde89e852016-03-01 10:20:09 -06002784 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002785 netdev_dbg(netdev, "opt_rx_bufadd_q_per_rx_comp_q = %lld\n",
2786 adapter->opt_rx_bufadd_q_per_rx_comp_q);
2787 break;
2788 case OPT_TX_ENTRIES_PER_SUBCRQ:
2789 adapter->opt_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06002790 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002791 netdev_dbg(netdev, "opt_tx_entries_per_subcrq = %lld\n",
2792 adapter->opt_tx_entries_per_subcrq);
2793 break;
2794 case OPT_RXBA_ENTRIES_PER_SUBCRQ:
2795 adapter->opt_rxba_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06002796 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002797 netdev_dbg(netdev, "opt_rxba_entries_per_subcrq = %lld\n",
2798 adapter->opt_rxba_entries_per_subcrq);
2799 break;
2800 case TX_RX_DESC_REQ:
2801 adapter->tx_rx_desc_req = crq->query_capability.number;
2802 netdev_dbg(netdev, "tx_rx_desc_req = %llx\n",
2803 adapter->tx_rx_desc_req);
2804 break;
2805
2806 default:
2807 netdev_err(netdev, "Got invalid cap rsp %d\n",
2808 crq->query_capability.capability);
2809 }
2810
2811out:
Thomas Falcon249168a2017-02-15 12:18:00 -06002812 if (atomic_read(&adapter->running_cap_crqs) == 0) {
2813 adapter->wait_capability = false;
Thomas Falconea22d512016-07-06 15:35:17 -05002814 init_sub_crqs(adapter, 0);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002815 /* We're done querying the capabilities, initialize sub-crqs */
Thomas Falcon249168a2017-02-15 12:18:00 -06002816 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002817}
2818
Thomas Falcon9888d7b2016-10-27 12:28:51 -05002819static void ibmvnic_xport_event(struct work_struct *work)
2820{
2821 struct ibmvnic_adapter *adapter = container_of(work,
2822 struct ibmvnic_adapter,
2823 ibmvnic_xport);
2824 struct device *dev = &adapter->vdev->dev;
2825 long rc;
2826
Thomas Falcon9888d7b2016-10-27 12:28:51 -05002827 release_sub_crqs(adapter);
2828 if (adapter->migrated) {
2829 rc = ibmvnic_reenable_crq_queue(adapter);
2830 if (rc)
2831 dev_err(dev, "Error after enable rc=%ld\n", rc);
2832 adapter->migrated = false;
2833 rc = ibmvnic_send_crq_init(adapter);
2834 if (rc)
2835 dev_err(dev, "Error sending init rc=%ld\n", rc);
2836 }
2837}
2838
Thomas Falcon032c5e82015-12-21 11:26:06 -06002839static void ibmvnic_handle_crq(union ibmvnic_crq *crq,
2840 struct ibmvnic_adapter *adapter)
2841{
2842 struct ibmvnic_generic_crq *gen_crq = &crq->generic;
2843 struct net_device *netdev = adapter->netdev;
2844 struct device *dev = &adapter->vdev->dev;
Murilo Fossa Vicentini993a82b2017-04-19 13:44:35 -04002845 u64 *u64_crq = (u64 *)crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002846 long rc;
2847
2848 netdev_dbg(netdev, "Handling CRQ: %016lx %016lx\n",
Murilo Fossa Vicentini993a82b2017-04-19 13:44:35 -04002849 (unsigned long int)cpu_to_be64(u64_crq[0]),
2850 (unsigned long int)cpu_to_be64(u64_crq[1]));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002851 switch (gen_crq->first) {
2852 case IBMVNIC_CRQ_INIT_RSP:
2853 switch (gen_crq->cmd) {
2854 case IBMVNIC_CRQ_INIT:
2855 dev_info(dev, "Partner initialized\n");
2856 /* Send back a response */
2857 rc = ibmvnic_send_crq_init_complete(adapter);
Thomas Falcon65dc6892016-07-06 15:35:18 -05002858 if (!rc)
2859 schedule_work(&adapter->vnic_crq_init);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002860 else
2861 dev_err(dev, "Can't send initrsp rc=%ld\n", rc);
2862 break;
2863 case IBMVNIC_CRQ_INIT_COMPLETE:
2864 dev_info(dev, "Partner initialization complete\n");
2865 send_version_xchg(adapter);
2866 break;
2867 default:
2868 dev_err(dev, "Unknown crq cmd: %d\n", gen_crq->cmd);
2869 }
2870 return;
2871 case IBMVNIC_CRQ_XPORT_EVENT:
2872 if (gen_crq->cmd == IBMVNIC_PARTITION_MIGRATED) {
2873 dev_info(dev, "Re-enabling adapter\n");
2874 adapter->migrated = true;
Thomas Falcon9888d7b2016-10-27 12:28:51 -05002875 schedule_work(&adapter->ibmvnic_xport);
Thomas Falcondfad09a2016-08-18 11:37:51 -05002876 } else if (gen_crq->cmd == IBMVNIC_DEVICE_FAILOVER) {
2877 dev_info(dev, "Backing device failover detected\n");
2878 netif_carrier_off(netdev);
2879 adapter->failover = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002880 } else {
2881 /* The adapter lost the connection */
2882 dev_err(dev, "Virtual Adapter failed (rc=%d)\n",
2883 gen_crq->cmd);
Thomas Falcon9888d7b2016-10-27 12:28:51 -05002884 schedule_work(&adapter->ibmvnic_xport);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002885 }
2886 return;
2887 case IBMVNIC_CRQ_CMD_RSP:
2888 break;
2889 default:
2890 dev_err(dev, "Got an invalid msg type 0x%02x\n",
2891 gen_crq->first);
2892 return;
2893 }
2894
2895 switch (gen_crq->cmd) {
2896 case VERSION_EXCHANGE_RSP:
2897 rc = crq->version_exchange_rsp.rc.code;
2898 if (rc) {
2899 dev_err(dev, "Error %ld in VERSION_EXCHG_RSP\n", rc);
2900 break;
2901 }
2902 dev_info(dev, "Partner protocol version is %d\n",
2903 crq->version_exchange_rsp.version);
2904 if (be16_to_cpu(crq->version_exchange_rsp.version) <
2905 ibmvnic_version)
2906 ibmvnic_version =
2907 be16_to_cpu(crq->version_exchange_rsp.version);
2908 send_cap_queries(adapter);
2909 break;
2910 case QUERY_CAPABILITY_RSP:
2911 handle_query_cap_rsp(crq, adapter);
2912 break;
2913 case QUERY_MAP_RSP:
2914 handle_query_map_rsp(crq, adapter);
2915 break;
2916 case REQUEST_MAP_RSP:
2917 handle_request_map_rsp(crq, adapter);
2918 break;
2919 case REQUEST_UNMAP_RSP:
2920 handle_request_unmap_rsp(crq, adapter);
2921 break;
2922 case REQUEST_CAPABILITY_RSP:
2923 handle_request_cap_rsp(crq, adapter);
2924 break;
2925 case LOGIN_RSP:
2926 netdev_dbg(netdev, "Got Login Response\n");
2927 handle_login_rsp(crq, adapter);
2928 break;
2929 case LOGICAL_LINK_STATE_RSP:
2930 netdev_dbg(netdev, "Got Logical Link State Response\n");
2931 adapter->logical_link_state =
2932 crq->logical_link_state_rsp.link_state;
2933 break;
2934 case LINK_STATE_INDICATION:
2935 netdev_dbg(netdev, "Got Logical Link State Indication\n");
2936 adapter->phys_link_state =
2937 crq->link_state_indication.phys_link_state;
2938 adapter->logical_link_state =
2939 crq->link_state_indication.logical_link_state;
2940 break;
2941 case CHANGE_MAC_ADDR_RSP:
2942 netdev_dbg(netdev, "Got MAC address change Response\n");
2943 handle_change_mac_rsp(crq, adapter);
2944 break;
2945 case ERROR_INDICATION:
2946 netdev_dbg(netdev, "Got Error Indication\n");
2947 handle_error_indication(crq, adapter);
2948 break;
2949 case REQUEST_ERROR_RSP:
2950 netdev_dbg(netdev, "Got Error Detail Response\n");
2951 handle_error_info_rsp(crq, adapter);
2952 break;
2953 case REQUEST_STATISTICS_RSP:
2954 netdev_dbg(netdev, "Got Statistics Response\n");
2955 complete(&adapter->stats_done);
2956 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002957 case QUERY_IP_OFFLOAD_RSP:
2958 netdev_dbg(netdev, "Got Query IP offload Response\n");
2959 handle_query_ip_offload_rsp(adapter);
2960 break;
2961 case MULTICAST_CTRL_RSP:
2962 netdev_dbg(netdev, "Got multicast control Response\n");
2963 break;
2964 case CONTROL_IP_OFFLOAD_RSP:
2965 netdev_dbg(netdev, "Got Control IP offload Response\n");
2966 dma_unmap_single(dev, adapter->ip_offload_ctrl_tok,
2967 sizeof(adapter->ip_offload_ctrl),
2968 DMA_TO_DEVICE);
John Allenbd0b6722017-03-17 17:13:40 -05002969 complete(&adapter->init_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002970 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002971 case COLLECT_FW_TRACE_RSP:
2972 netdev_dbg(netdev, "Got Collect firmware trace Response\n");
2973 complete(&adapter->fw_done);
2974 break;
2975 default:
2976 netdev_err(netdev, "Got an invalid cmd type 0x%02x\n",
2977 gen_crq->cmd);
2978 }
2979}
2980
2981static irqreturn_t ibmvnic_interrupt(int irq, void *instance)
2982{
2983 struct ibmvnic_adapter *adapter = instance;
Thomas Falcon6c267b32017-02-15 12:17:58 -06002984
Thomas Falcon6c267b32017-02-15 12:17:58 -06002985 tasklet_schedule(&adapter->tasklet);
Thomas Falcon6c267b32017-02-15 12:17:58 -06002986 return IRQ_HANDLED;
2987}
2988
2989static void ibmvnic_tasklet(void *data)
2990{
2991 struct ibmvnic_adapter *adapter = data;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002992 struct ibmvnic_crq_queue *queue = &adapter->crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002993 union ibmvnic_crq *crq;
2994 unsigned long flags;
2995 bool done = false;
2996
2997 spin_lock_irqsave(&queue->lock, flags);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002998 while (!done) {
2999 /* Pull all the valid messages off the CRQ */
3000 while ((crq = ibmvnic_next_crq(adapter)) != NULL) {
3001 ibmvnic_handle_crq(crq, adapter);
3002 crq->generic.first = 0;
3003 }
Brian Kinged7ecbf2017-04-19 13:44:53 -04003004
3005 /* remain in tasklet until all
3006 * capabilities responses are received
3007 */
3008 if (!adapter->wait_capability)
3009 done = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003010 }
Thomas Falcon249168a2017-02-15 12:18:00 -06003011 /* if capabilities CRQ's were sent in this tasklet, the following
3012 * tasklet must wait until all responses are received
3013 */
3014 if (atomic_read(&adapter->running_cap_crqs) != 0)
3015 adapter->wait_capability = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003016 spin_unlock_irqrestore(&queue->lock, flags);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003017}
3018
3019static int ibmvnic_reenable_crq_queue(struct ibmvnic_adapter *adapter)
3020{
3021 struct vio_dev *vdev = adapter->vdev;
3022 int rc;
3023
3024 do {
3025 rc = plpar_hcall_norets(H_ENABLE_CRQ, vdev->unit_address);
3026 } while (rc == H_IN_PROGRESS || rc == H_BUSY || H_IS_LONG_BUSY(rc));
3027
3028 if (rc)
3029 dev_err(&vdev->dev, "Error enabling adapter (rc=%d)\n", rc);
3030
3031 return rc;
3032}
3033
3034static int ibmvnic_reset_crq(struct ibmvnic_adapter *adapter)
3035{
3036 struct ibmvnic_crq_queue *crq = &adapter->crq;
3037 struct device *dev = &adapter->vdev->dev;
3038 struct vio_dev *vdev = adapter->vdev;
3039 int rc;
3040
3041 /* Close the CRQ */
3042 do {
3043 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
3044 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
3045
3046 /* Clean out the queue */
3047 memset(crq->msgs, 0, PAGE_SIZE);
3048 crq->cur = 0;
3049
3050 /* And re-open it again */
3051 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
3052 crq->msg_token, PAGE_SIZE);
3053
3054 if (rc == H_CLOSED)
3055 /* Adapter is good, but other end is not ready */
3056 dev_warn(dev, "Partner adapter not ready\n");
3057 else if (rc != 0)
3058 dev_warn(dev, "Couldn't register crq (rc=%d)\n", rc);
3059
3060 return rc;
3061}
3062
Nathan Fontenotf9928872017-03-30 02:48:54 -04003063static void release_crq_queue(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003064{
3065 struct ibmvnic_crq_queue *crq = &adapter->crq;
3066 struct vio_dev *vdev = adapter->vdev;
3067 long rc;
3068
Nathan Fontenotf9928872017-03-30 02:48:54 -04003069 if (!crq->msgs)
3070 return;
3071
Thomas Falcon032c5e82015-12-21 11:26:06 -06003072 netdev_dbg(adapter->netdev, "Releasing CRQ\n");
3073 free_irq(vdev->irq, adapter);
Thomas Falcon6c267b32017-02-15 12:17:58 -06003074 tasklet_kill(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003075 do {
3076 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
3077 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
3078
3079 dma_unmap_single(&vdev->dev, crq->msg_token, PAGE_SIZE,
3080 DMA_BIDIRECTIONAL);
3081 free_page((unsigned long)crq->msgs);
Nathan Fontenotf9928872017-03-30 02:48:54 -04003082 crq->msgs = NULL;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003083}
3084
Nathan Fontenotf9928872017-03-30 02:48:54 -04003085static int init_crq_queue(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003086{
3087 struct ibmvnic_crq_queue *crq = &adapter->crq;
3088 struct device *dev = &adapter->vdev->dev;
3089 struct vio_dev *vdev = adapter->vdev;
3090 int rc, retrc = -ENOMEM;
3091
Nathan Fontenotf9928872017-03-30 02:48:54 -04003092 if (crq->msgs)
3093 return 0;
3094
Thomas Falcon032c5e82015-12-21 11:26:06 -06003095 crq->msgs = (union ibmvnic_crq *)get_zeroed_page(GFP_KERNEL);
3096 /* Should we allocate more than one page? */
3097
3098 if (!crq->msgs)
3099 return -ENOMEM;
3100
3101 crq->size = PAGE_SIZE / sizeof(*crq->msgs);
3102 crq->msg_token = dma_map_single(dev, crq->msgs, PAGE_SIZE,
3103 DMA_BIDIRECTIONAL);
3104 if (dma_mapping_error(dev, crq->msg_token))
3105 goto map_failed;
3106
3107 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
3108 crq->msg_token, PAGE_SIZE);
3109
3110 if (rc == H_RESOURCE)
3111 /* maybe kexecing and resource is busy. try a reset */
3112 rc = ibmvnic_reset_crq(adapter);
3113 retrc = rc;
3114
3115 if (rc == H_CLOSED) {
3116 dev_warn(dev, "Partner adapter not ready\n");
3117 } else if (rc) {
3118 dev_warn(dev, "Error %d opening adapter\n", rc);
3119 goto reg_crq_failed;
3120 }
3121
3122 retrc = 0;
3123
Thomas Falcon6c267b32017-02-15 12:17:58 -06003124 tasklet_init(&adapter->tasklet, (void *)ibmvnic_tasklet,
3125 (unsigned long)adapter);
3126
Thomas Falcon032c5e82015-12-21 11:26:06 -06003127 netdev_dbg(adapter->netdev, "registering irq 0x%x\n", vdev->irq);
3128 rc = request_irq(vdev->irq, ibmvnic_interrupt, 0, IBMVNIC_NAME,
3129 adapter);
3130 if (rc) {
3131 dev_err(dev, "Couldn't register irq 0x%x. rc=%d\n",
3132 vdev->irq, rc);
3133 goto req_irq_failed;
3134 }
3135
3136 rc = vio_enable_interrupts(vdev);
3137 if (rc) {
3138 dev_err(dev, "Error %d enabling interrupts\n", rc);
3139 goto req_irq_failed;
3140 }
3141
3142 crq->cur = 0;
3143 spin_lock_init(&crq->lock);
3144
3145 return retrc;
3146
3147req_irq_failed:
Thomas Falcon6c267b32017-02-15 12:17:58 -06003148 tasklet_kill(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003149 do {
3150 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
3151 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
3152reg_crq_failed:
3153 dma_unmap_single(dev, crq->msg_token, PAGE_SIZE, DMA_BIDIRECTIONAL);
3154map_failed:
3155 free_page((unsigned long)crq->msgs);
Nathan Fontenotf9928872017-03-30 02:48:54 -04003156 crq->msgs = NULL;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003157 return retrc;
3158}
3159
Thomas Falcon65dc6892016-07-06 15:35:18 -05003160static void handle_crq_init_rsp(struct work_struct *work)
3161{
3162 struct ibmvnic_adapter *adapter = container_of(work,
3163 struct ibmvnic_adapter,
3164 vnic_crq_init);
3165 struct device *dev = &adapter->vdev->dev;
3166 struct net_device *netdev = adapter->netdev;
3167 unsigned long timeout = msecs_to_jiffies(30000);
Thomas Falcondfad09a2016-08-18 11:37:51 -05003168 bool restart = false;
Thomas Falcon65dc6892016-07-06 15:35:18 -05003169 int rc;
3170
Thomas Falcondfad09a2016-08-18 11:37:51 -05003171 if (adapter->failover) {
3172 release_sub_crqs(adapter);
3173 if (netif_running(netdev)) {
3174 netif_tx_disable(netdev);
3175 ibmvnic_close(netdev);
3176 restart = true;
3177 }
3178 }
3179
Thomas Falcon65dc6892016-07-06 15:35:18 -05003180 reinit_completion(&adapter->init_done);
Nathan Fontenotdb5d0b52017-02-10 13:45:05 -05003181 send_version_xchg(adapter);
Thomas Falcon65dc6892016-07-06 15:35:18 -05003182 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
3183 dev_err(dev, "Passive init timeout\n");
3184 goto task_failed;
3185 }
3186
Thomas Falconf39f0d12017-02-14 10:22:59 -06003187 netdev->mtu = adapter->req_mtu - ETH_HLEN;
Thomas Falcon65dc6892016-07-06 15:35:18 -05003188
Thomas Falcondfad09a2016-08-18 11:37:51 -05003189 if (adapter->failover) {
3190 adapter->failover = false;
3191 if (restart) {
3192 rc = ibmvnic_open(netdev);
3193 if (rc)
3194 goto restart_failed;
3195 }
3196 netif_carrier_on(netdev);
3197 return;
3198 }
3199
Thomas Falcon65dc6892016-07-06 15:35:18 -05003200 rc = register_netdev(netdev);
3201 if (rc) {
3202 dev_err(dev,
3203 "failed to register netdev rc=%d\n", rc);
3204 goto register_failed;
3205 }
3206 dev_info(dev, "ibmvnic registered\n");
3207
3208 return;
3209
Thomas Falcondfad09a2016-08-18 11:37:51 -05003210restart_failed:
3211 dev_err(dev, "Failed to restart ibmvnic, rc=%d\n", rc);
Thomas Falcon65dc6892016-07-06 15:35:18 -05003212register_failed:
3213 release_sub_crqs(adapter);
3214task_failed:
3215 dev_err(dev, "Passive initialization was not successful\n");
3216}
3217
John Allenf6ef6402017-03-17 17:13:42 -05003218static int ibmvnic_init(struct ibmvnic_adapter *adapter)
3219{
3220 struct device *dev = &adapter->vdev->dev;
3221 unsigned long timeout = msecs_to_jiffies(30000);
John Allenf6ef6402017-03-17 17:13:42 -05003222 int rc;
3223
Nathan Fontenotf9928872017-03-30 02:48:54 -04003224 rc = init_crq_queue(adapter);
John Allenf6ef6402017-03-17 17:13:42 -05003225 if (rc) {
3226 dev_err(dev, "Couldn't initialize crq. rc=%d\n", rc);
3227 return rc;
3228 }
3229
Nathan Fontenot7bbc27a2017-03-30 02:49:23 -04003230 rc = init_stats_token(adapter);
3231 if (rc) {
Nathan Fontenotf9928872017-03-30 02:48:54 -04003232 release_crq_queue(adapter);
Nathan Fontenot7bbc27a2017-03-30 02:49:23 -04003233 return rc;
John Allenf6ef6402017-03-17 17:13:42 -05003234 }
3235
John Allenf6ef6402017-03-17 17:13:42 -05003236 init_completion(&adapter->init_done);
3237 ibmvnic_send_crq_init(adapter);
3238 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
3239 dev_err(dev, "Initialization sequence timed out\n");
Nathan Fontenotf9928872017-03-30 02:48:54 -04003240 release_crq_queue(adapter);
John Allenf6ef6402017-03-17 17:13:42 -05003241 return -1;
3242 }
3243
3244 return 0;
3245}
3246
Thomas Falcon032c5e82015-12-21 11:26:06 -06003247static int ibmvnic_probe(struct vio_dev *dev, const struct vio_device_id *id)
3248{
3249 struct ibmvnic_adapter *adapter;
3250 struct net_device *netdev;
3251 unsigned char *mac_addr_p;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003252 int rc;
3253
3254 dev_dbg(&dev->dev, "entering ibmvnic_probe for UA 0x%x\n",
3255 dev->unit_address);
3256
3257 mac_addr_p = (unsigned char *)vio_get_attribute(dev,
3258 VETH_MAC_ADDR, NULL);
3259 if (!mac_addr_p) {
3260 dev_err(&dev->dev,
3261 "(%s:%3.3d) ERROR: Can't find MAC_ADDR attribute\n",
3262 __FILE__, __LINE__);
3263 return 0;
3264 }
3265
3266 netdev = alloc_etherdev_mq(sizeof(struct ibmvnic_adapter),
3267 IBMVNIC_MAX_TX_QUEUES);
3268 if (!netdev)
3269 return -ENOMEM;
3270
3271 adapter = netdev_priv(netdev);
3272 dev_set_drvdata(&dev->dev, netdev);
3273 adapter->vdev = dev;
3274 adapter->netdev = netdev;
Thomas Falcondfad09a2016-08-18 11:37:51 -05003275 adapter->failover = false;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003276
3277 ether_addr_copy(adapter->mac_addr, mac_addr_p);
3278 ether_addr_copy(netdev->dev_addr, adapter->mac_addr);
3279 netdev->irq = dev->irq;
3280 netdev->netdev_ops = &ibmvnic_netdev_ops;
3281 netdev->ethtool_ops = &ibmvnic_ethtool_ops;
3282 SET_NETDEV_DEV(netdev, &dev->dev);
3283
Thomas Falcon65dc6892016-07-06 15:35:18 -05003284 INIT_WORK(&adapter->vnic_crq_init, handle_crq_init_rsp);
Thomas Falcon9888d7b2016-10-27 12:28:51 -05003285 INIT_WORK(&adapter->ibmvnic_xport, ibmvnic_xport_event);
Thomas Falcon65dc6892016-07-06 15:35:18 -05003286
Thomas Falcon032c5e82015-12-21 11:26:06 -06003287 spin_lock_init(&adapter->stats_lock);
3288
Thomas Falcon032c5e82015-12-21 11:26:06 -06003289 INIT_LIST_HEAD(&adapter->errors);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003290 spin_lock_init(&adapter->error_list_lock);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003291
John Allenf6ef6402017-03-17 17:13:42 -05003292 rc = ibmvnic_init(adapter);
3293 if (rc) {
3294 free_netdev(netdev);
3295 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003296 }
3297
Thomas Falconf39f0d12017-02-14 10:22:59 -06003298 netdev->mtu = adapter->req_mtu - ETH_HLEN;
John Allenea5509f2017-03-17 17:13:43 -05003299 adapter->is_closed = false;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003300
3301 rc = register_netdev(netdev);
3302 if (rc) {
3303 dev_err(&dev->dev, "failed to register netdev rc=%d\n", rc);
John Allenf6ef6402017-03-17 17:13:42 -05003304 free_netdev(netdev);
3305 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003306 }
3307 dev_info(&dev->dev, "ibmvnic registered\n");
3308
3309 return 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003310}
3311
3312static int ibmvnic_remove(struct vio_dev *dev)
3313{
3314 struct net_device *netdev = dev_get_drvdata(&dev->dev);
Nathan Fontenot37489052017-04-19 13:45:04 -04003315 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003316
3317 unregister_netdev(netdev);
Nathan Fontenot37489052017-04-19 13:45:04 -04003318
3319 release_resources(adapter);
3320 release_sub_crqs(adapter);
3321 release_crq_queue(adapter);
3322
Thomas Falcon032c5e82015-12-21 11:26:06 -06003323 free_netdev(netdev);
3324 dev_set_drvdata(&dev->dev, NULL);
3325
3326 return 0;
3327}
3328
3329static unsigned long ibmvnic_get_desired_dma(struct vio_dev *vdev)
3330{
3331 struct net_device *netdev = dev_get_drvdata(&vdev->dev);
3332 struct ibmvnic_adapter *adapter;
3333 struct iommu_table *tbl;
3334 unsigned long ret = 0;
3335 int i;
3336
3337 tbl = get_iommu_table_base(&vdev->dev);
3338
3339 /* netdev inits at probe time along with the structures we need below*/
3340 if (!netdev)
3341 return IOMMU_PAGE_ALIGN(IBMVNIC_IO_ENTITLEMENT_DEFAULT, tbl);
3342
3343 adapter = netdev_priv(netdev);
3344
3345 ret += PAGE_SIZE; /* the crq message queue */
3346 ret += adapter->bounce_buffer_size;
3347 ret += IOMMU_PAGE_ALIGN(sizeof(struct ibmvnic_statistics), tbl);
3348
3349 for (i = 0; i < adapter->req_tx_queues + adapter->req_rx_queues; i++)
3350 ret += 4 * PAGE_SIZE; /* the scrq message queue */
3351
3352 for (i = 0; i < be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
3353 i++)
3354 ret += adapter->rx_pool[i].size *
3355 IOMMU_PAGE_ALIGN(adapter->rx_pool[i].buff_size, tbl);
3356
3357 return ret;
3358}
3359
3360static int ibmvnic_resume(struct device *dev)
3361{
3362 struct net_device *netdev = dev_get_drvdata(dev);
3363 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
3364 int i;
3365
3366 /* kick the interrupt handlers just in case we lost an interrupt */
3367 for (i = 0; i < adapter->req_rx_queues; i++)
3368 ibmvnic_interrupt_rx(adapter->rx_scrq[i]->irq,
3369 adapter->rx_scrq[i]);
3370
3371 return 0;
3372}
3373
3374static struct vio_device_id ibmvnic_device_table[] = {
3375 {"network", "IBM,vnic"},
3376 {"", "" }
3377};
3378MODULE_DEVICE_TABLE(vio, ibmvnic_device_table);
3379
3380static const struct dev_pm_ops ibmvnic_pm_ops = {
3381 .resume = ibmvnic_resume
3382};
3383
3384static struct vio_driver ibmvnic_driver = {
3385 .id_table = ibmvnic_device_table,
3386 .probe = ibmvnic_probe,
3387 .remove = ibmvnic_remove,
3388 .get_desired_dma = ibmvnic_get_desired_dma,
3389 .name = ibmvnic_driver_name,
3390 .pm = &ibmvnic_pm_ops,
3391};
3392
3393/* module functions */
3394static int __init ibmvnic_module_init(void)
3395{
3396 pr_info("%s: %s %s\n", ibmvnic_driver_name, ibmvnic_driver_string,
3397 IBMVNIC_DRIVER_VERSION);
3398
3399 return vio_register_driver(&ibmvnic_driver);
3400}
3401
3402static void __exit ibmvnic_module_exit(void)
3403{
3404 vio_unregister_driver(&ibmvnic_driver);
3405}
3406
3407module_init(ibmvnic_module_init);
3408module_exit(ibmvnic_module_exit);