blob: a9399e96e94238d2c5dbd5f16624a0cb3f3b0aa1 [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 *);
Thomas Falconea22d512016-07-06 15:35:17 -050091static void release_sub_crqs_no_irqs(struct ibmvnic_adapter *);
Thomas Falcon032c5e82015-12-21 11:26:06 -060092static int ibmvnic_reset_crq(struct ibmvnic_adapter *);
93static int ibmvnic_send_crq_init(struct ibmvnic_adapter *);
94static int ibmvnic_reenable_crq_queue(struct ibmvnic_adapter *);
95static int ibmvnic_send_crq(struct ibmvnic_adapter *, union ibmvnic_crq *);
96static int send_subcrq(struct ibmvnic_adapter *adapter, u64 remote_handle,
97 union sub_crq *sub_crq);
Thomas Falconad7775d2016-04-01 17:20:34 -050098static int send_subcrq_indirect(struct ibmvnic_adapter *, u64, u64, u64);
Thomas Falcon032c5e82015-12-21 11:26:06 -060099static irqreturn_t ibmvnic_interrupt_rx(int irq, void *instance);
100static int enable_scrq_irq(struct ibmvnic_adapter *,
101 struct ibmvnic_sub_crq_queue *);
102static int disable_scrq_irq(struct ibmvnic_adapter *,
103 struct ibmvnic_sub_crq_queue *);
104static int pending_scrq(struct ibmvnic_adapter *,
105 struct ibmvnic_sub_crq_queue *);
106static union sub_crq *ibmvnic_next_scrq(struct ibmvnic_adapter *,
107 struct ibmvnic_sub_crq_queue *);
108static int ibmvnic_poll(struct napi_struct *napi, int data);
109static void send_map_query(struct ibmvnic_adapter *adapter);
110static void send_request_map(struct ibmvnic_adapter *, dma_addr_t, __be32, u8);
111static void send_request_unmap(struct ibmvnic_adapter *, u8);
John Allenbd0b6722017-03-17 17:13:40 -0500112static void send_login(struct ibmvnic_adapter *adapter);
113static void send_cap_queries(struct ibmvnic_adapter *adapter);
114static int init_sub_crq_irqs(struct ibmvnic_adapter *adapter);
John Allenea5509f2017-03-17 17:13:43 -0500115static int ibmvnic_init(struct ibmvnic_adapter *);
Nathan Fontenotf9928872017-03-30 02:48:54 -0400116static void release_crq_queue(struct ibmvnic_adapter *);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600117
118struct ibmvnic_stat {
119 char name[ETH_GSTRING_LEN];
120 int offset;
121};
122
123#define IBMVNIC_STAT_OFF(stat) (offsetof(struct ibmvnic_adapter, stats) + \
124 offsetof(struct ibmvnic_statistics, stat))
125#define IBMVNIC_GET_STAT(a, off) (*((u64 *)(((unsigned long)(a)) + off)))
126
127static const struct ibmvnic_stat ibmvnic_stats[] = {
128 {"rx_packets", IBMVNIC_STAT_OFF(rx_packets)},
129 {"rx_bytes", IBMVNIC_STAT_OFF(rx_bytes)},
130 {"tx_packets", IBMVNIC_STAT_OFF(tx_packets)},
131 {"tx_bytes", IBMVNIC_STAT_OFF(tx_bytes)},
132 {"ucast_tx_packets", IBMVNIC_STAT_OFF(ucast_tx_packets)},
133 {"ucast_rx_packets", IBMVNIC_STAT_OFF(ucast_rx_packets)},
134 {"mcast_tx_packets", IBMVNIC_STAT_OFF(mcast_tx_packets)},
135 {"mcast_rx_packets", IBMVNIC_STAT_OFF(mcast_rx_packets)},
136 {"bcast_tx_packets", IBMVNIC_STAT_OFF(bcast_tx_packets)},
137 {"bcast_rx_packets", IBMVNIC_STAT_OFF(bcast_rx_packets)},
138 {"align_errors", IBMVNIC_STAT_OFF(align_errors)},
139 {"fcs_errors", IBMVNIC_STAT_OFF(fcs_errors)},
140 {"single_collision_frames", IBMVNIC_STAT_OFF(single_collision_frames)},
141 {"multi_collision_frames", IBMVNIC_STAT_OFF(multi_collision_frames)},
142 {"sqe_test_errors", IBMVNIC_STAT_OFF(sqe_test_errors)},
143 {"deferred_tx", IBMVNIC_STAT_OFF(deferred_tx)},
144 {"late_collisions", IBMVNIC_STAT_OFF(late_collisions)},
145 {"excess_collisions", IBMVNIC_STAT_OFF(excess_collisions)},
146 {"internal_mac_tx_errors", IBMVNIC_STAT_OFF(internal_mac_tx_errors)},
147 {"carrier_sense", IBMVNIC_STAT_OFF(carrier_sense)},
148 {"too_long_frames", IBMVNIC_STAT_OFF(too_long_frames)},
149 {"internal_mac_rx_errors", IBMVNIC_STAT_OFF(internal_mac_rx_errors)},
150};
151
152static long h_reg_sub_crq(unsigned long unit_address, unsigned long token,
153 unsigned long length, unsigned long *number,
154 unsigned long *irq)
155{
156 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
157 long rc;
158
159 rc = plpar_hcall(H_REG_SUB_CRQ, retbuf, unit_address, token, length);
160 *number = retbuf[0];
161 *irq = retbuf[1];
162
163 return rc;
164}
165
166/* net_device_ops functions */
167
168static void init_rx_pool(struct ibmvnic_adapter *adapter,
169 struct ibmvnic_rx_pool *rx_pool, int num, int index,
170 int buff_size, int active)
171{
172 netdev_dbg(adapter->netdev,
173 "Initializing rx_pool %d, %d buffs, %d bytes each\n",
174 index, num, buff_size);
175 rx_pool->size = num;
176 rx_pool->index = index;
177 rx_pool->buff_size = buff_size;
178 rx_pool->active = active;
179}
180
181static int alloc_long_term_buff(struct ibmvnic_adapter *adapter,
182 struct ibmvnic_long_term_buff *ltb, int size)
183{
184 struct device *dev = &adapter->vdev->dev;
185
186 ltb->size = size;
187 ltb->buff = dma_alloc_coherent(dev, ltb->size, &ltb->addr,
188 GFP_KERNEL);
189
190 if (!ltb->buff) {
191 dev_err(dev, "Couldn't alloc long term buffer\n");
192 return -ENOMEM;
193 }
194 ltb->map_id = adapter->map_id;
195 adapter->map_id++;
Nathan Fontenotdb5d0b52017-02-10 13:45:05 -0500196
197 init_completion(&adapter->fw_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600198 send_request_map(adapter, ltb->addr,
199 ltb->size, ltb->map_id);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600200 wait_for_completion(&adapter->fw_done);
201 return 0;
202}
203
204static void free_long_term_buff(struct ibmvnic_adapter *adapter,
205 struct ibmvnic_long_term_buff *ltb)
206{
207 struct device *dev = &adapter->vdev->dev;
208
Nathan Fontenotc657e322017-03-30 02:49:06 -0400209 if (!ltb->buff)
210 return;
211
Thomas Falcon032c5e82015-12-21 11:26:06 -0600212 dma_free_coherent(dev, ltb->size, ltb->buff, ltb->addr);
Thomas Falcondfad09a2016-08-18 11:37:51 -0500213 if (!adapter->failover)
214 send_request_unmap(adapter, ltb->map_id);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600215}
216
217static int alloc_rx_pool(struct ibmvnic_adapter *adapter,
218 struct ibmvnic_rx_pool *pool)
219{
220 struct device *dev = &adapter->vdev->dev;
221 int i;
222
223 pool->free_map = kcalloc(pool->size, sizeof(int), GFP_KERNEL);
224 if (!pool->free_map)
225 return -ENOMEM;
226
227 pool->rx_buff = kcalloc(pool->size, sizeof(struct ibmvnic_rx_buff),
228 GFP_KERNEL);
229
230 if (!pool->rx_buff) {
231 dev_err(dev, "Couldn't alloc rx buffers\n");
232 kfree(pool->free_map);
233 return -ENOMEM;
234 }
235
236 if (alloc_long_term_buff(adapter, &pool->long_term_buff,
237 pool->size * pool->buff_size)) {
238 kfree(pool->free_map);
239 kfree(pool->rx_buff);
240 return -ENOMEM;
241 }
242
243 for (i = 0; i < pool->size; ++i)
244 pool->free_map[i] = i;
245
246 atomic_set(&pool->available, 0);
247 pool->next_alloc = 0;
248 pool->next_free = 0;
249
250 return 0;
251}
252
253static void replenish_rx_pool(struct ibmvnic_adapter *adapter,
254 struct ibmvnic_rx_pool *pool)
255{
256 int count = pool->size - atomic_read(&pool->available);
257 struct device *dev = &adapter->vdev->dev;
258 int buffers_added = 0;
259 unsigned long lpar_rc;
260 union sub_crq sub_crq;
261 struct sk_buff *skb;
262 unsigned int offset;
263 dma_addr_t dma_addr;
264 unsigned char *dst;
265 u64 *handle_array;
266 int shift = 0;
267 int index;
268 int i;
269
270 handle_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
271 be32_to_cpu(adapter->login_rsp_buf->
272 off_rxadd_subcrqs));
273
274 for (i = 0; i < count; ++i) {
275 skb = alloc_skb(pool->buff_size, GFP_ATOMIC);
276 if (!skb) {
277 dev_err(dev, "Couldn't replenish rx buff\n");
278 adapter->replenish_no_mem++;
279 break;
280 }
281
282 index = pool->free_map[pool->next_free];
283
284 if (pool->rx_buff[index].skb)
285 dev_err(dev, "Inconsistent free_map!\n");
286
287 /* Copy the skb to the long term mapped DMA buffer */
288 offset = index * pool->buff_size;
289 dst = pool->long_term_buff.buff + offset;
290 memset(dst, 0, pool->buff_size);
291 dma_addr = pool->long_term_buff.addr + offset;
292 pool->rx_buff[index].data = dst;
293
294 pool->free_map[pool->next_free] = IBMVNIC_INVALID_MAP;
295 pool->rx_buff[index].dma = dma_addr;
296 pool->rx_buff[index].skb = skb;
297 pool->rx_buff[index].pool_index = pool->index;
298 pool->rx_buff[index].size = pool->buff_size;
299
300 memset(&sub_crq, 0, sizeof(sub_crq));
301 sub_crq.rx_add.first = IBMVNIC_CRQ_CMD;
302 sub_crq.rx_add.correlator =
303 cpu_to_be64((u64)&pool->rx_buff[index]);
304 sub_crq.rx_add.ioba = cpu_to_be32(dma_addr);
305 sub_crq.rx_add.map_id = pool->long_term_buff.map_id;
306
307 /* The length field of the sCRQ is defined to be 24 bits so the
308 * buffer size needs to be left shifted by a byte before it is
309 * converted to big endian to prevent the last byte from being
310 * truncated.
311 */
312#ifdef __LITTLE_ENDIAN__
313 shift = 8;
314#endif
315 sub_crq.rx_add.len = cpu_to_be32(pool->buff_size << shift);
316
317 lpar_rc = send_subcrq(adapter, handle_array[pool->index],
318 &sub_crq);
319 if (lpar_rc != H_SUCCESS)
320 goto failure;
321
322 buffers_added++;
323 adapter->replenish_add_buff_success++;
324 pool->next_free = (pool->next_free + 1) % pool->size;
325 }
326 atomic_add(buffers_added, &pool->available);
327 return;
328
329failure:
330 dev_info(dev, "replenish pools failure\n");
331 pool->free_map[pool->next_free] = index;
332 pool->rx_buff[index].skb = NULL;
333 if (!dma_mapping_error(dev, dma_addr))
334 dma_unmap_single(dev, dma_addr, pool->buff_size,
335 DMA_FROM_DEVICE);
336
337 dev_kfree_skb_any(skb);
338 adapter->replenish_add_buff_failure++;
339 atomic_add(buffers_added, &pool->available);
340}
341
342static void replenish_pools(struct ibmvnic_adapter *adapter)
343{
344 int i;
345
346 if (adapter->migrated)
347 return;
348
349 adapter->replenish_task_cycles++;
350 for (i = 0; i < be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
351 i++) {
352 if (adapter->rx_pool[i].active)
353 replenish_rx_pool(adapter, &adapter->rx_pool[i]);
354 }
355}
356
357static void free_rx_pool(struct ibmvnic_adapter *adapter,
358 struct ibmvnic_rx_pool *pool)
359{
360 int i;
361
362 kfree(pool->free_map);
363 pool->free_map = NULL;
364
365 if (!pool->rx_buff)
366 return;
367
368 for (i = 0; i < pool->size; i++) {
369 if (pool->rx_buff[i].skb) {
370 dev_kfree_skb_any(pool->rx_buff[i].skb);
371 pool->rx_buff[i].skb = NULL;
372 }
373 }
374 kfree(pool->rx_buff);
375 pool->rx_buff = NULL;
376}
377
Nathan Fontenotc657e322017-03-30 02:49:06 -0400378static void release_tx_pools(struct ibmvnic_adapter *adapter)
379{
380 struct ibmvnic_tx_pool *tx_pool;
381 int i, tx_scrqs;
382
383 if (!adapter->tx_pool)
384 return;
385
386 tx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
387 for (i = 0; i < tx_scrqs; i++) {
388 tx_pool = &adapter->tx_pool[i];
389 kfree(tx_pool->tx_buff);
390 free_long_term_buff(adapter, &tx_pool->long_term_buff);
391 kfree(tx_pool->free_map);
392 }
393
394 kfree(adapter->tx_pool);
395 adapter->tx_pool = NULL;
396}
397
398static int init_tx_pools(struct net_device *netdev)
399{
400 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
401 struct device *dev = &adapter->vdev->dev;
402 struct ibmvnic_tx_pool *tx_pool;
403 int tx_subcrqs;
404 int i, j;
405
406 tx_subcrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
407 adapter->tx_pool = kcalloc(tx_subcrqs,
408 sizeof(struct ibmvnic_tx_pool), GFP_KERNEL);
409 if (!adapter->tx_pool)
410 return -1;
411
412 for (i = 0; i < tx_subcrqs; i++) {
413 tx_pool = &adapter->tx_pool[i];
414 tx_pool->tx_buff = kcalloc(adapter->req_tx_entries_per_subcrq,
415 sizeof(struct ibmvnic_tx_buff),
416 GFP_KERNEL);
417 if (!tx_pool->tx_buff) {
418 dev_err(dev, "tx pool buffer allocation failed\n");
419 release_tx_pools(adapter);
420 return -1;
421 }
422
423 if (alloc_long_term_buff(adapter, &tx_pool->long_term_buff,
424 adapter->req_tx_entries_per_subcrq *
425 adapter->req_mtu)) {
426 release_tx_pools(adapter);
427 return -1;
428 }
429
430 tx_pool->free_map = kcalloc(adapter->req_tx_entries_per_subcrq,
431 sizeof(int), GFP_KERNEL);
432 if (!tx_pool->free_map) {
433 release_tx_pools(adapter);
434 return -1;
435 }
436
437 for (j = 0; j < adapter->req_tx_entries_per_subcrq; j++)
438 tx_pool->free_map[j] = j;
439
440 tx_pool->consumer_index = 0;
441 tx_pool->producer_index = 0;
442 }
443
444 return 0;
445}
446
Nathan Fontenotf0b8c962017-03-30 02:49:00 -0400447static void release_bounce_buffer(struct ibmvnic_adapter *adapter)
448{
449 struct device *dev = &adapter->vdev->dev;
450
451 if (!adapter->bounce_buffer)
452 return;
453
454 if (!dma_mapping_error(dev, adapter->bounce_buffer_dma)) {
455 dma_unmap_single(dev, adapter->bounce_buffer_dma,
456 adapter->bounce_buffer_size,
457 DMA_BIDIRECTIONAL);
458 adapter->bounce_buffer_dma = DMA_ERROR_CODE;
459 }
460
461 kfree(adapter->bounce_buffer);
462 adapter->bounce_buffer = NULL;
463}
464
465static int init_bounce_buffer(struct net_device *netdev)
466{
467 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
468 struct device *dev = &adapter->vdev->dev;
469 char *buf;
470 int buf_sz;
471 dma_addr_t map_addr;
472
473 buf_sz = (netdev->mtu + ETH_HLEN - 1) / PAGE_SIZE + 1;
474 buf = kmalloc(adapter->bounce_buffer_size, GFP_KERNEL);
475 if (!buf)
476 return -1;
477
478 map_addr = dma_map_single(dev, buf, buf_sz, DMA_TO_DEVICE);
479 if (dma_mapping_error(dev, map_addr)) {
480 dev_err(dev, "Couldn't map bounce buffer\n");
481 kfree(buf);
482 return -1;
483 }
484
485 adapter->bounce_buffer = buf;
486 adapter->bounce_buffer_size = buf_sz;
487 adapter->bounce_buffer_dma = map_addr;
488 return 0;
489}
490
John Allena57a5d22017-03-17 17:13:41 -0500491static int ibmvnic_login(struct net_device *netdev)
Thomas Falcon032c5e82015-12-21 11:26:06 -0600492{
493 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
John Allenbd0b6722017-03-17 17:13:40 -0500494 unsigned long timeout = msecs_to_jiffies(30000);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600495 struct device *dev = &adapter->vdev->dev;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600496
John Allenbd0b6722017-03-17 17:13:40 -0500497 do {
498 if (adapter->renegotiate) {
499 adapter->renegotiate = false;
500 release_sub_crqs_no_irqs(adapter);
501
502 reinit_completion(&adapter->init_done);
503 send_cap_queries(adapter);
504 if (!wait_for_completion_timeout(&adapter->init_done,
505 timeout)) {
506 dev_err(dev, "Capabilities query timeout\n");
507 return -1;
508 }
509 }
510
511 reinit_completion(&adapter->init_done);
512 send_login(adapter);
513 if (!wait_for_completion_timeout(&adapter->init_done,
514 timeout)) {
515 dev_err(dev, "Login timeout\n");
516 return -1;
517 }
518 } while (adapter->renegotiate);
519
John Allena57a5d22017-03-17 17:13:41 -0500520 return 0;
521}
522
523static int ibmvnic_open(struct net_device *netdev)
524{
525 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
526 struct device *dev = &adapter->vdev->dev;
John Allena57a5d22017-03-17 17:13:41 -0500527 union ibmvnic_crq crq;
528 int rxadd_subcrqs;
529 u64 *size_array;
530 int tx_subcrqs;
531 int rc = 0;
532 int i, j;
533
John Allenea5509f2017-03-17 17:13:43 -0500534 if (adapter->is_closed) {
535 rc = ibmvnic_init(adapter);
536 if (rc)
537 return rc;
538 }
539
John Allena57a5d22017-03-17 17:13:41 -0500540 rc = ibmvnic_login(netdev);
541 if (rc)
542 return rc;
543
John Allenbd0b6722017-03-17 17:13:40 -0500544 rc = netif_set_real_num_tx_queues(netdev, adapter->req_tx_queues);
545 if (rc) {
546 dev_err(dev, "failed to set the number of tx queues\n");
547 return -1;
548 }
549
550 rc = init_sub_crq_irqs(adapter);
551 if (rc) {
552 dev_err(dev, "failed to initialize sub crq irqs\n");
553 return -1;
554 }
555
Thomas Falcon032c5e82015-12-21 11:26:06 -0600556 rxadd_subcrqs =
557 be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
558 tx_subcrqs =
559 be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
560 size_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
561 be32_to_cpu(adapter->login_rsp_buf->
562 off_rxadd_buff_size));
563 adapter->map_id = 1;
564 adapter->napi = kcalloc(adapter->req_rx_queues,
565 sizeof(struct napi_struct), GFP_KERNEL);
566 if (!adapter->napi)
567 goto alloc_napi_failed;
568 for (i = 0; i < adapter->req_rx_queues; i++) {
569 netif_napi_add(netdev, &adapter->napi[i], ibmvnic_poll,
570 NAPI_POLL_WEIGHT);
571 napi_enable(&adapter->napi[i]);
572 }
573 adapter->rx_pool =
574 kcalloc(rxadd_subcrqs, sizeof(struct ibmvnic_rx_pool), GFP_KERNEL);
575
576 if (!adapter->rx_pool)
577 goto rx_pool_arr_alloc_failed;
578 send_map_query(adapter);
579 for (i = 0; i < rxadd_subcrqs; i++) {
580 init_rx_pool(adapter, &adapter->rx_pool[i],
Thomas Falcon068d9f92017-03-05 12:18:42 -0600581 adapter->req_rx_add_entries_per_subcrq, i,
Thomas Falcon032c5e82015-12-21 11:26:06 -0600582 be64_to_cpu(size_array[i]), 1);
583 if (alloc_rx_pool(adapter, &adapter->rx_pool[i])) {
584 dev_err(dev, "Couldn't alloc rx pool\n");
585 goto rx_pool_alloc_failed;
586 }
587 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600588
Nathan Fontenotc657e322017-03-30 02:49:06 -0400589 rc = init_tx_pools(netdev);
590 if (rc)
591 goto tx_pool_failed;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600592
Nathan Fontenotf0b8c962017-03-30 02:49:00 -0400593 rc = init_bounce_buffer(netdev);
594 if (rc)
595 goto bounce_init_failed;
596
Thomas Falcon032c5e82015-12-21 11:26:06 -0600597 replenish_pools(adapter);
598
599 /* We're ready to receive frames, enable the sub-crq interrupts and
600 * set the logical link state to up
601 */
602 for (i = 0; i < adapter->req_rx_queues; i++)
603 enable_scrq_irq(adapter, adapter->rx_scrq[i]);
604
605 for (i = 0; i < adapter->req_tx_queues; i++)
606 enable_scrq_irq(adapter, adapter->tx_scrq[i]);
607
608 memset(&crq, 0, sizeof(crq));
609 crq.logical_link_state.first = IBMVNIC_CRQ_CMD;
610 crq.logical_link_state.cmd = LOGICAL_LINK_STATE;
611 crq.logical_link_state.link_state = IBMVNIC_LOGICAL_LNK_UP;
612 ibmvnic_send_crq(adapter, &crq);
613
Thomas Falconb8efb892016-07-06 15:35:15 -0500614 netif_tx_start_all_queues(netdev);
John Allenea5509f2017-03-17 17:13:43 -0500615 adapter->is_closed = false;
Thomas Falconb8efb892016-07-06 15:35:15 -0500616
Thomas Falcon032c5e82015-12-21 11:26:06 -0600617 return 0;
618
Nathan Fontenotf0b8c962017-03-30 02:49:00 -0400619bounce_init_failed:
Thomas Falcon032c5e82015-12-21 11:26:06 -0600620 i = tx_subcrqs - 1;
621 kfree(adapter->tx_pool[i].free_map);
Nathan Fontenotc657e322017-03-30 02:49:06 -0400622tx_pool_failed:
Thomas Falcon032c5e82015-12-21 11:26:06 -0600623 i = rxadd_subcrqs;
624rx_pool_alloc_failed:
625 for (j = 0; j < i; j++) {
626 free_rx_pool(adapter, &adapter->rx_pool[j]);
627 free_long_term_buff(adapter,
628 &adapter->rx_pool[j].long_term_buff);
629 }
630 kfree(adapter->rx_pool);
631 adapter->rx_pool = NULL;
632rx_pool_arr_alloc_failed:
633 for (i = 0; i < adapter->req_rx_queues; i++)
Nathan Fontenote722af62017-02-10 13:29:06 -0500634 napi_disable(&adapter->napi[i]);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600635alloc_napi_failed:
John Allenbd0b6722017-03-17 17:13:40 -0500636 release_sub_crqs(adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600637 return -ENOMEM;
638}
639
John Allenea5509f2017-03-17 17:13:43 -0500640static void ibmvnic_release_resources(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -0600641{
Thomas Falcon032c5e82015-12-21 11:26:06 -0600642 struct device *dev = &adapter->vdev->dev;
Nathan Fontenotc657e322017-03-30 02:49:06 -0400643 int rx_scrqs;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600644 int i;
645
Nathan Fontenotf0b8c962017-03-30 02:49:00 -0400646 release_bounce_buffer(adapter);
Nathan Fontenotc657e322017-03-30 02:49:06 -0400647 release_tx_pools(adapter);
John Allenea5509f2017-03-17 17:13:43 -0500648
649 rx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
650 for (i = 0; i < rx_scrqs; i++) {
651 struct ibmvnic_rx_pool *rx_pool = &adapter->rx_pool[i];
652
653 free_rx_pool(adapter, rx_pool);
654 free_long_term_buff(adapter, &rx_pool->long_term_buff);
655 }
656 kfree(adapter->rx_pool);
657 adapter->rx_pool = NULL;
658
659 release_sub_crqs(adapter);
Nathan Fontenotf9928872017-03-30 02:48:54 -0400660 release_crq_queue(adapter);
John Allenea5509f2017-03-17 17:13:43 -0500661
John Allenea5509f2017-03-17 17:13:43 -0500662 if (adapter->stats_token)
663 dma_unmap_single(dev, adapter->stats_token,
664 sizeof(struct ibmvnic_statistics),
665 DMA_FROM_DEVICE);
John Allenea5509f2017-03-17 17:13:43 -0500666}
667
668static int ibmvnic_close(struct net_device *netdev)
669{
670 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
671 union ibmvnic_crq crq;
672 int i;
673
674 adapter->closing = true;
675
676 for (i = 0; i < adapter->req_rx_queues; i++)
677 napi_disable(&adapter->napi[i]);
678
679 if (!adapter->failover)
680 netif_tx_stop_all_queues(netdev);
681
Thomas Falcon032c5e82015-12-21 11:26:06 -0600682 memset(&crq, 0, sizeof(crq));
683 crq.logical_link_state.first = IBMVNIC_CRQ_CMD;
684 crq.logical_link_state.cmd = LOGICAL_LINK_STATE;
685 crq.logical_link_state.link_state = IBMVNIC_LOGICAL_LNK_DN;
686 ibmvnic_send_crq(adapter, &crq);
687
John Allenea5509f2017-03-17 17:13:43 -0500688 ibmvnic_release_resources(adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600689
John Allenea5509f2017-03-17 17:13:43 -0500690 adapter->is_closed = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600691 adapter->closing = false;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600692 return 0;
693}
694
Thomas Falconad7775d2016-04-01 17:20:34 -0500695/**
696 * build_hdr_data - creates L2/L3/L4 header data buffer
697 * @hdr_field - bitfield determining needed headers
698 * @skb - socket buffer
699 * @hdr_len - array of header lengths
700 * @tot_len - total length of data
701 *
702 * Reads hdr_field to determine which headers are needed by firmware.
703 * Builds a buffer containing these headers. Saves individual header
704 * lengths and total buffer length to be used to build descriptors.
705 */
706static int build_hdr_data(u8 hdr_field, struct sk_buff *skb,
707 int *hdr_len, u8 *hdr_data)
708{
709 int len = 0;
710 u8 *hdr;
711
712 hdr_len[0] = sizeof(struct ethhdr);
713
714 if (skb->protocol == htons(ETH_P_IP)) {
715 hdr_len[1] = ip_hdr(skb)->ihl * 4;
716 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
717 hdr_len[2] = tcp_hdrlen(skb);
718 else if (ip_hdr(skb)->protocol == IPPROTO_UDP)
719 hdr_len[2] = sizeof(struct udphdr);
720 } else if (skb->protocol == htons(ETH_P_IPV6)) {
721 hdr_len[1] = sizeof(struct ipv6hdr);
722 if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
723 hdr_len[2] = tcp_hdrlen(skb);
724 else if (ipv6_hdr(skb)->nexthdr == IPPROTO_UDP)
725 hdr_len[2] = sizeof(struct udphdr);
726 }
727
728 memset(hdr_data, 0, 120);
729 if ((hdr_field >> 6) & 1) {
730 hdr = skb_mac_header(skb);
731 memcpy(hdr_data, hdr, hdr_len[0]);
732 len += hdr_len[0];
733 }
734
735 if ((hdr_field >> 5) & 1) {
736 hdr = skb_network_header(skb);
737 memcpy(hdr_data + len, hdr, hdr_len[1]);
738 len += hdr_len[1];
739 }
740
741 if ((hdr_field >> 4) & 1) {
742 hdr = skb_transport_header(skb);
743 memcpy(hdr_data + len, hdr, hdr_len[2]);
744 len += hdr_len[2];
745 }
746 return len;
747}
748
749/**
750 * create_hdr_descs - create header and header extension descriptors
751 * @hdr_field - bitfield determining needed headers
752 * @data - buffer containing header data
753 * @len - length of data buffer
754 * @hdr_len - array of individual header lengths
755 * @scrq_arr - descriptor array
756 *
757 * Creates header and, if needed, header extension descriptors and
758 * places them in a descriptor array, scrq_arr
759 */
760
761static void create_hdr_descs(u8 hdr_field, u8 *hdr_data, int len, int *hdr_len,
762 union sub_crq *scrq_arr)
763{
764 union sub_crq hdr_desc;
765 int tmp_len = len;
766 u8 *data, *cur;
767 int tmp;
768
769 while (tmp_len > 0) {
770 cur = hdr_data + len - tmp_len;
771
772 memset(&hdr_desc, 0, sizeof(hdr_desc));
773 if (cur != hdr_data) {
774 data = hdr_desc.hdr_ext.data;
775 tmp = tmp_len > 29 ? 29 : tmp_len;
776 hdr_desc.hdr_ext.first = IBMVNIC_CRQ_CMD;
777 hdr_desc.hdr_ext.type = IBMVNIC_HDR_EXT_DESC;
778 hdr_desc.hdr_ext.len = tmp;
779 } else {
780 data = hdr_desc.hdr.data;
781 tmp = tmp_len > 24 ? 24 : tmp_len;
782 hdr_desc.hdr.first = IBMVNIC_CRQ_CMD;
783 hdr_desc.hdr.type = IBMVNIC_HDR_DESC;
784 hdr_desc.hdr.len = tmp;
785 hdr_desc.hdr.l2_len = (u8)hdr_len[0];
786 hdr_desc.hdr.l3_len = cpu_to_be16((u16)hdr_len[1]);
787 hdr_desc.hdr.l4_len = (u8)hdr_len[2];
788 hdr_desc.hdr.flag = hdr_field << 1;
789 }
790 memcpy(data, cur, tmp);
791 tmp_len -= tmp;
792 *scrq_arr = hdr_desc;
793 scrq_arr++;
794 }
795}
796
797/**
798 * build_hdr_descs_arr - build a header descriptor array
799 * @skb - socket buffer
800 * @num_entries - number of descriptors to be sent
801 * @subcrq - first TX descriptor
802 * @hdr_field - bit field determining which headers will be sent
803 *
804 * This function will build a TX descriptor array with applicable
805 * L2/L3/L4 packet header descriptors to be sent by send_subcrq_indirect.
806 */
807
808static void build_hdr_descs_arr(struct ibmvnic_tx_buff *txbuff,
809 int *num_entries, u8 hdr_field)
810{
811 int hdr_len[3] = {0, 0, 0};
812 int tot_len, len;
813 u8 *hdr_data = txbuff->hdr_data;
814
815 tot_len = build_hdr_data(hdr_field, txbuff->skb, hdr_len,
816 txbuff->hdr_data);
817 len = tot_len;
818 len -= 24;
819 if (len > 0)
820 num_entries += len % 29 ? len / 29 + 1 : len / 29;
821 create_hdr_descs(hdr_field, hdr_data, tot_len, hdr_len,
822 txbuff->indir_arr + 1);
823}
824
Thomas Falcon032c5e82015-12-21 11:26:06 -0600825static int ibmvnic_xmit(struct sk_buff *skb, struct net_device *netdev)
826{
827 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
828 int queue_num = skb_get_queue_mapping(skb);
Thomas Falconad7775d2016-04-01 17:20:34 -0500829 u8 *hdrs = (u8 *)&adapter->tx_rx_desc_req;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600830 struct device *dev = &adapter->vdev->dev;
831 struct ibmvnic_tx_buff *tx_buff = NULL;
Thomas Falcon142c0ac2017-03-05 12:18:41 -0600832 struct ibmvnic_sub_crq_queue *tx_scrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600833 struct ibmvnic_tx_pool *tx_pool;
834 unsigned int tx_send_failed = 0;
835 unsigned int tx_map_failed = 0;
836 unsigned int tx_dropped = 0;
837 unsigned int tx_packets = 0;
838 unsigned int tx_bytes = 0;
839 dma_addr_t data_dma_addr;
840 struct netdev_queue *txq;
841 bool used_bounce = false;
842 unsigned long lpar_rc;
843 union sub_crq tx_crq;
844 unsigned int offset;
Thomas Falconad7775d2016-04-01 17:20:34 -0500845 int num_entries = 1;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600846 unsigned char *dst;
847 u64 *handle_array;
848 int index = 0;
849 int ret = 0;
850
851 tx_pool = &adapter->tx_pool[queue_num];
Thomas Falcon142c0ac2017-03-05 12:18:41 -0600852 tx_scrq = adapter->tx_scrq[queue_num];
Thomas Falcon032c5e82015-12-21 11:26:06 -0600853 txq = netdev_get_tx_queue(netdev, skb_get_queue_mapping(skb));
854 handle_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
855 be32_to_cpu(adapter->login_rsp_buf->
856 off_txsubm_subcrqs));
857 if (adapter->migrated) {
858 tx_send_failed++;
859 tx_dropped++;
860 ret = NETDEV_TX_BUSY;
861 goto out;
862 }
863
864 index = tx_pool->free_map[tx_pool->consumer_index];
865 offset = index * adapter->req_mtu;
866 dst = tx_pool->long_term_buff.buff + offset;
867 memset(dst, 0, adapter->req_mtu);
868 skb_copy_from_linear_data(skb, dst, skb->len);
869 data_dma_addr = tx_pool->long_term_buff.addr + offset;
870
871 tx_pool->consumer_index =
872 (tx_pool->consumer_index + 1) %
Thomas Falcon068d9f92017-03-05 12:18:42 -0600873 adapter->req_tx_entries_per_subcrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600874
875 tx_buff = &tx_pool->tx_buff[index];
876 tx_buff->skb = skb;
877 tx_buff->data_dma[0] = data_dma_addr;
878 tx_buff->data_len[0] = skb->len;
879 tx_buff->index = index;
880 tx_buff->pool_index = queue_num;
881 tx_buff->last_frag = true;
882 tx_buff->used_bounce = used_bounce;
883
884 memset(&tx_crq, 0, sizeof(tx_crq));
885 tx_crq.v1.first = IBMVNIC_CRQ_CMD;
886 tx_crq.v1.type = IBMVNIC_TX_DESC;
887 tx_crq.v1.n_crq_elem = 1;
888 tx_crq.v1.n_sge = 1;
889 tx_crq.v1.flags1 = IBMVNIC_TX_COMP_NEEDED;
890 tx_crq.v1.correlator = cpu_to_be32(index);
891 tx_crq.v1.dma_reg = cpu_to_be16(tx_pool->long_term_buff.map_id);
892 tx_crq.v1.sge_len = cpu_to_be32(skb->len);
893 tx_crq.v1.ioba = cpu_to_be64(data_dma_addr);
894
895 if (adapter->vlan_header_insertion) {
896 tx_crq.v1.flags2 |= IBMVNIC_TX_VLAN_INSERT;
897 tx_crq.v1.vlan_id = cpu_to_be16(skb->vlan_tci);
898 }
899
900 if (skb->protocol == htons(ETH_P_IP)) {
901 if (ip_hdr(skb)->version == 4)
902 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV4;
903 else if (ip_hdr(skb)->version == 6)
904 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV6;
905
906 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
907 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_TCP;
908 else if (ip_hdr(skb)->protocol != IPPROTO_TCP)
909 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_UDP;
910 }
911
Thomas Falconad7775d2016-04-01 17:20:34 -0500912 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Thomas Falcon032c5e82015-12-21 11:26:06 -0600913 tx_crq.v1.flags1 |= IBMVNIC_TX_CHKSUM_OFFLOAD;
Thomas Falconad7775d2016-04-01 17:20:34 -0500914 hdrs += 2;
915 }
916 /* determine if l2/3/4 headers are sent to firmware */
917 if ((*hdrs >> 7) & 1 &&
918 (skb->protocol == htons(ETH_P_IP) ||
919 skb->protocol == htons(ETH_P_IPV6))) {
920 build_hdr_descs_arr(tx_buff, &num_entries, *hdrs);
921 tx_crq.v1.n_crq_elem = num_entries;
922 tx_buff->indir_arr[0] = tx_crq;
923 tx_buff->indir_dma = dma_map_single(dev, tx_buff->indir_arr,
924 sizeof(tx_buff->indir_arr),
925 DMA_TO_DEVICE);
926 if (dma_mapping_error(dev, tx_buff->indir_dma)) {
927 if (!firmware_has_feature(FW_FEATURE_CMO))
928 dev_err(dev, "tx: unable to map descriptor array\n");
929 tx_map_failed++;
930 tx_dropped++;
931 ret = NETDEV_TX_BUSY;
932 goto out;
933 }
John Allen498cd8e2016-04-06 11:49:55 -0500934 lpar_rc = send_subcrq_indirect(adapter, handle_array[queue_num],
Thomas Falconad7775d2016-04-01 17:20:34 -0500935 (u64)tx_buff->indir_dma,
936 (u64)num_entries);
937 } else {
John Allen498cd8e2016-04-06 11:49:55 -0500938 lpar_rc = send_subcrq(adapter, handle_array[queue_num],
939 &tx_crq);
Thomas Falconad7775d2016-04-01 17:20:34 -0500940 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600941 if (lpar_rc != H_SUCCESS) {
942 dev_err(dev, "tx failed with code %ld\n", lpar_rc);
943
944 if (tx_pool->consumer_index == 0)
945 tx_pool->consumer_index =
Thomas Falcon068d9f92017-03-05 12:18:42 -0600946 adapter->req_tx_entries_per_subcrq - 1;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600947 else
948 tx_pool->consumer_index--;
949
950 tx_send_failed++;
951 tx_dropped++;
952 ret = NETDEV_TX_BUSY;
953 goto out;
954 }
Thomas Falcon142c0ac2017-03-05 12:18:41 -0600955
956 atomic_inc(&tx_scrq->used);
957
958 if (atomic_read(&tx_scrq->used) >= adapter->req_tx_entries_per_subcrq) {
959 netdev_info(netdev, "Stopping queue %d\n", queue_num);
960 netif_stop_subqueue(netdev, queue_num);
961 }
962
Thomas Falcon032c5e82015-12-21 11:26:06 -0600963 tx_packets++;
964 tx_bytes += skb->len;
965 txq->trans_start = jiffies;
966 ret = NETDEV_TX_OK;
967
968out:
969 netdev->stats.tx_dropped += tx_dropped;
970 netdev->stats.tx_bytes += tx_bytes;
971 netdev->stats.tx_packets += tx_packets;
972 adapter->tx_send_failed += tx_send_failed;
973 adapter->tx_map_failed += tx_map_failed;
974
975 return ret;
976}
977
978static void ibmvnic_set_multi(struct net_device *netdev)
979{
980 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
981 struct netdev_hw_addr *ha;
982 union ibmvnic_crq crq;
983
984 memset(&crq, 0, sizeof(crq));
985 crq.request_capability.first = IBMVNIC_CRQ_CMD;
986 crq.request_capability.cmd = REQUEST_CAPABILITY;
987
988 if (netdev->flags & IFF_PROMISC) {
989 if (!adapter->promisc_supported)
990 return;
991 } else {
992 if (netdev->flags & IFF_ALLMULTI) {
993 /* Accept all multicast */
994 memset(&crq, 0, sizeof(crq));
995 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
996 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
997 crq.multicast_ctrl.flags = IBMVNIC_ENABLE_ALL;
998 ibmvnic_send_crq(adapter, &crq);
999 } else if (netdev_mc_empty(netdev)) {
1000 /* Reject all multicast */
1001 memset(&crq, 0, sizeof(crq));
1002 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1003 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1004 crq.multicast_ctrl.flags = IBMVNIC_DISABLE_ALL;
1005 ibmvnic_send_crq(adapter, &crq);
1006 } else {
1007 /* Accept one or more multicast(s) */
1008 netdev_for_each_mc_addr(ha, netdev) {
1009 memset(&crq, 0, sizeof(crq));
1010 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1011 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1012 crq.multicast_ctrl.flags = IBMVNIC_ENABLE_MC;
1013 ether_addr_copy(&crq.multicast_ctrl.mac_addr[0],
1014 ha->addr);
1015 ibmvnic_send_crq(adapter, &crq);
1016 }
1017 }
1018 }
1019}
1020
1021static int ibmvnic_set_mac(struct net_device *netdev, void *p)
1022{
1023 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1024 struct sockaddr *addr = p;
1025 union ibmvnic_crq crq;
1026
1027 if (!is_valid_ether_addr(addr->sa_data))
1028 return -EADDRNOTAVAIL;
1029
1030 memset(&crq, 0, sizeof(crq));
1031 crq.change_mac_addr.first = IBMVNIC_CRQ_CMD;
1032 crq.change_mac_addr.cmd = CHANGE_MAC_ADDR;
1033 ether_addr_copy(&crq.change_mac_addr.mac_addr[0], addr->sa_data);
1034 ibmvnic_send_crq(adapter, &crq);
1035 /* netdev->dev_addr is changed in handle_change_mac_rsp function */
1036 return 0;
1037}
1038
Thomas Falcon032c5e82015-12-21 11:26:06 -06001039static void ibmvnic_tx_timeout(struct net_device *dev)
1040{
1041 struct ibmvnic_adapter *adapter = netdev_priv(dev);
1042 int rc;
1043
1044 /* Adapter timed out, resetting it */
1045 release_sub_crqs(adapter);
1046 rc = ibmvnic_reset_crq(adapter);
1047 if (rc)
1048 dev_err(&adapter->vdev->dev, "Adapter timeout, reset failed\n");
1049 else
1050 ibmvnic_send_crq_init(adapter);
1051}
1052
1053static void remove_buff_from_pool(struct ibmvnic_adapter *adapter,
1054 struct ibmvnic_rx_buff *rx_buff)
1055{
1056 struct ibmvnic_rx_pool *pool = &adapter->rx_pool[rx_buff->pool_index];
1057
1058 rx_buff->skb = NULL;
1059
1060 pool->free_map[pool->next_alloc] = (int)(rx_buff - pool->rx_buff);
1061 pool->next_alloc = (pool->next_alloc + 1) % pool->size;
1062
1063 atomic_dec(&pool->available);
1064}
1065
1066static int ibmvnic_poll(struct napi_struct *napi, int budget)
1067{
1068 struct net_device *netdev = napi->dev;
1069 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1070 int scrq_num = (int)(napi - adapter->napi);
1071 int frames_processed = 0;
1072restart_poll:
1073 while (frames_processed < budget) {
1074 struct sk_buff *skb;
1075 struct ibmvnic_rx_buff *rx_buff;
1076 union sub_crq *next;
1077 u32 length;
1078 u16 offset;
1079 u8 flags = 0;
1080
1081 if (!pending_scrq(adapter, adapter->rx_scrq[scrq_num]))
1082 break;
1083 next = ibmvnic_next_scrq(adapter, adapter->rx_scrq[scrq_num]);
1084 rx_buff =
1085 (struct ibmvnic_rx_buff *)be64_to_cpu(next->
1086 rx_comp.correlator);
1087 /* do error checking */
1088 if (next->rx_comp.rc) {
1089 netdev_err(netdev, "rx error %x\n", next->rx_comp.rc);
1090 /* free the entry */
1091 next->rx_comp.first = 0;
1092 remove_buff_from_pool(adapter, rx_buff);
1093 break;
1094 }
1095
1096 length = be32_to_cpu(next->rx_comp.len);
1097 offset = be16_to_cpu(next->rx_comp.off_frame_data);
1098 flags = next->rx_comp.flags;
1099 skb = rx_buff->skb;
1100 skb_copy_to_linear_data(skb, rx_buff->data + offset,
1101 length);
1102 skb->vlan_tci = be16_to_cpu(next->rx_comp.vlan_tci);
1103 /* free the entry */
1104 next->rx_comp.first = 0;
1105 remove_buff_from_pool(adapter, rx_buff);
1106
1107 skb_put(skb, length);
1108 skb->protocol = eth_type_trans(skb, netdev);
1109
1110 if (flags & IBMVNIC_IP_CHKSUM_GOOD &&
1111 flags & IBMVNIC_TCP_UDP_CHKSUM_GOOD) {
1112 skb->ip_summed = CHECKSUM_UNNECESSARY;
1113 }
1114
1115 length = skb->len;
1116 napi_gro_receive(napi, skb); /* send it up */
1117 netdev->stats.rx_packets++;
1118 netdev->stats.rx_bytes += length;
1119 frames_processed++;
1120 }
John Allen498cd8e2016-04-06 11:49:55 -05001121 replenish_rx_pool(adapter, &adapter->rx_pool[scrq_num]);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001122
1123 if (frames_processed < budget) {
1124 enable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
Eric Dumazet6ad20162017-01-30 08:22:01 -08001125 napi_complete_done(napi, frames_processed);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001126 if (pending_scrq(adapter, adapter->rx_scrq[scrq_num]) &&
1127 napi_reschedule(napi)) {
1128 disable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
1129 goto restart_poll;
1130 }
1131 }
1132 return frames_processed;
1133}
1134
1135#ifdef CONFIG_NET_POLL_CONTROLLER
1136static void ibmvnic_netpoll_controller(struct net_device *dev)
1137{
1138 struct ibmvnic_adapter *adapter = netdev_priv(dev);
1139 int i;
1140
1141 replenish_pools(netdev_priv(dev));
1142 for (i = 0; i < adapter->req_rx_queues; i++)
1143 ibmvnic_interrupt_rx(adapter->rx_scrq[i]->irq,
1144 adapter->rx_scrq[i]);
1145}
1146#endif
1147
1148static const struct net_device_ops ibmvnic_netdev_ops = {
1149 .ndo_open = ibmvnic_open,
1150 .ndo_stop = ibmvnic_close,
1151 .ndo_start_xmit = ibmvnic_xmit,
1152 .ndo_set_rx_mode = ibmvnic_set_multi,
1153 .ndo_set_mac_address = ibmvnic_set_mac,
1154 .ndo_validate_addr = eth_validate_addr,
Thomas Falcon032c5e82015-12-21 11:26:06 -06001155 .ndo_tx_timeout = ibmvnic_tx_timeout,
1156#ifdef CONFIG_NET_POLL_CONTROLLER
1157 .ndo_poll_controller = ibmvnic_netpoll_controller,
1158#endif
1159};
1160
1161/* ethtool functions */
1162
Philippe Reynes8a433792017-01-07 22:37:29 +01001163static int ibmvnic_get_link_ksettings(struct net_device *netdev,
1164 struct ethtool_link_ksettings *cmd)
Thomas Falcon032c5e82015-12-21 11:26:06 -06001165{
Philippe Reynes8a433792017-01-07 22:37:29 +01001166 u32 supported, advertising;
1167
1168 supported = (SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg |
Thomas Falcon032c5e82015-12-21 11:26:06 -06001169 SUPPORTED_FIBRE);
Philippe Reynes8a433792017-01-07 22:37:29 +01001170 advertising = (ADVERTISED_1000baseT_Full | ADVERTISED_Autoneg |
Thomas Falcon032c5e82015-12-21 11:26:06 -06001171 ADVERTISED_FIBRE);
Philippe Reynes8a433792017-01-07 22:37:29 +01001172 cmd->base.speed = SPEED_1000;
1173 cmd->base.duplex = DUPLEX_FULL;
1174 cmd->base.port = PORT_FIBRE;
1175 cmd->base.phy_address = 0;
1176 cmd->base.autoneg = AUTONEG_ENABLE;
1177
1178 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
1179 supported);
1180 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
1181 advertising);
1182
Thomas Falcon032c5e82015-12-21 11:26:06 -06001183 return 0;
1184}
1185
1186static void ibmvnic_get_drvinfo(struct net_device *dev,
1187 struct ethtool_drvinfo *info)
1188{
1189 strlcpy(info->driver, ibmvnic_driver_name, sizeof(info->driver));
1190 strlcpy(info->version, IBMVNIC_DRIVER_VERSION, sizeof(info->version));
1191}
1192
1193static u32 ibmvnic_get_msglevel(struct net_device *netdev)
1194{
1195 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1196
1197 return adapter->msg_enable;
1198}
1199
1200static void ibmvnic_set_msglevel(struct net_device *netdev, u32 data)
1201{
1202 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1203
1204 adapter->msg_enable = data;
1205}
1206
1207static u32 ibmvnic_get_link(struct net_device *netdev)
1208{
1209 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1210
1211 /* Don't need to send a query because we request a logical link up at
1212 * init and then we wait for link state indications
1213 */
1214 return adapter->logical_link_state;
1215}
1216
1217static void ibmvnic_get_ringparam(struct net_device *netdev,
1218 struct ethtool_ringparam *ring)
1219{
1220 ring->rx_max_pending = 0;
1221 ring->tx_max_pending = 0;
1222 ring->rx_mini_max_pending = 0;
1223 ring->rx_jumbo_max_pending = 0;
1224 ring->rx_pending = 0;
1225 ring->tx_pending = 0;
1226 ring->rx_mini_pending = 0;
1227 ring->rx_jumbo_pending = 0;
1228}
1229
1230static void ibmvnic_get_strings(struct net_device *dev, u32 stringset, u8 *data)
1231{
1232 int i;
1233
1234 if (stringset != ETH_SS_STATS)
1235 return;
1236
1237 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++, data += ETH_GSTRING_LEN)
1238 memcpy(data, ibmvnic_stats[i].name, ETH_GSTRING_LEN);
1239}
1240
1241static int ibmvnic_get_sset_count(struct net_device *dev, int sset)
1242{
1243 switch (sset) {
1244 case ETH_SS_STATS:
1245 return ARRAY_SIZE(ibmvnic_stats);
1246 default:
1247 return -EOPNOTSUPP;
1248 }
1249}
1250
1251static void ibmvnic_get_ethtool_stats(struct net_device *dev,
1252 struct ethtool_stats *stats, u64 *data)
1253{
1254 struct ibmvnic_adapter *adapter = netdev_priv(dev);
1255 union ibmvnic_crq crq;
1256 int i;
1257
1258 memset(&crq, 0, sizeof(crq));
1259 crq.request_statistics.first = IBMVNIC_CRQ_CMD;
1260 crq.request_statistics.cmd = REQUEST_STATISTICS;
1261 crq.request_statistics.ioba = cpu_to_be32(adapter->stats_token);
1262 crq.request_statistics.len =
1263 cpu_to_be32(sizeof(struct ibmvnic_statistics));
Thomas Falcon032c5e82015-12-21 11:26:06 -06001264
1265 /* Wait for data to be written */
1266 init_completion(&adapter->stats_done);
Nathan Fontenotdb5d0b52017-02-10 13:45:05 -05001267 ibmvnic_send_crq(adapter, &crq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001268 wait_for_completion(&adapter->stats_done);
1269
1270 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++)
1271 data[i] = IBMVNIC_GET_STAT(adapter, ibmvnic_stats[i].offset);
1272}
1273
1274static const struct ethtool_ops ibmvnic_ethtool_ops = {
Thomas Falcon032c5e82015-12-21 11:26:06 -06001275 .get_drvinfo = ibmvnic_get_drvinfo,
1276 .get_msglevel = ibmvnic_get_msglevel,
1277 .set_msglevel = ibmvnic_set_msglevel,
1278 .get_link = ibmvnic_get_link,
1279 .get_ringparam = ibmvnic_get_ringparam,
1280 .get_strings = ibmvnic_get_strings,
1281 .get_sset_count = ibmvnic_get_sset_count,
1282 .get_ethtool_stats = ibmvnic_get_ethtool_stats,
Philippe Reynes8a433792017-01-07 22:37:29 +01001283 .get_link_ksettings = ibmvnic_get_link_ksettings,
Thomas Falcon032c5e82015-12-21 11:26:06 -06001284};
1285
1286/* Routines for managing CRQs/sCRQs */
1287
1288static void release_sub_crq_queue(struct ibmvnic_adapter *adapter,
1289 struct ibmvnic_sub_crq_queue *scrq)
1290{
1291 struct device *dev = &adapter->vdev->dev;
1292 long rc;
1293
1294 netdev_dbg(adapter->netdev, "Releasing sub-CRQ\n");
1295
1296 /* Close the sub-crqs */
1297 do {
1298 rc = plpar_hcall_norets(H_FREE_SUB_CRQ,
1299 adapter->vdev->unit_address,
1300 scrq->crq_num);
1301 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
1302
1303 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
1304 DMA_BIDIRECTIONAL);
1305 free_pages((unsigned long)scrq->msgs, 2);
1306 kfree(scrq);
1307}
1308
1309static struct ibmvnic_sub_crq_queue *init_sub_crq_queue(struct ibmvnic_adapter
1310 *adapter)
1311{
1312 struct device *dev = &adapter->vdev->dev;
1313 struct ibmvnic_sub_crq_queue *scrq;
1314 int rc;
1315
1316 scrq = kmalloc(sizeof(*scrq), GFP_ATOMIC);
1317 if (!scrq)
1318 return NULL;
1319
Thomas Falcon12608c22016-10-17 15:28:09 -05001320 scrq->msgs = (union sub_crq *)__get_free_pages(GFP_ATOMIC, 2);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001321 memset(scrq->msgs, 0, 4 * PAGE_SIZE);
1322 if (!scrq->msgs) {
1323 dev_warn(dev, "Couldn't allocate crq queue messages page\n");
1324 goto zero_page_failed;
1325 }
1326
1327 scrq->msg_token = dma_map_single(dev, scrq->msgs, 4 * PAGE_SIZE,
1328 DMA_BIDIRECTIONAL);
1329 if (dma_mapping_error(dev, scrq->msg_token)) {
1330 dev_warn(dev, "Couldn't map crq queue messages page\n");
1331 goto map_failed;
1332 }
1333
1334 rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
1335 4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
1336
1337 if (rc == H_RESOURCE)
1338 rc = ibmvnic_reset_crq(adapter);
1339
1340 if (rc == H_CLOSED) {
1341 dev_warn(dev, "Partner adapter not ready, waiting.\n");
1342 } else if (rc) {
1343 dev_warn(dev, "Error %d registering sub-crq\n", rc);
1344 goto reg_failed;
1345 }
1346
Thomas Falcon032c5e82015-12-21 11:26:06 -06001347 scrq->adapter = adapter;
1348 scrq->size = 4 * PAGE_SIZE / sizeof(*scrq->msgs);
1349 scrq->cur = 0;
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001350 atomic_set(&scrq->used, 0);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001351 scrq->rx_skb_top = NULL;
1352 spin_lock_init(&scrq->lock);
1353
1354 netdev_dbg(adapter->netdev,
1355 "sub-crq initialized, num %lx, hw_irq=%lx, irq=%x\n",
1356 scrq->crq_num, scrq->hw_irq, scrq->irq);
1357
1358 return scrq;
1359
Thomas Falcon032c5e82015-12-21 11:26:06 -06001360reg_failed:
1361 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
1362 DMA_BIDIRECTIONAL);
1363map_failed:
1364 free_pages((unsigned long)scrq->msgs, 2);
1365zero_page_failed:
1366 kfree(scrq);
1367
1368 return NULL;
1369}
1370
1371static void release_sub_crqs(struct ibmvnic_adapter *adapter)
1372{
1373 int i;
1374
1375 if (adapter->tx_scrq) {
1376 for (i = 0; i < adapter->req_tx_queues; i++)
1377 if (adapter->tx_scrq[i]) {
1378 free_irq(adapter->tx_scrq[i]->irq,
1379 adapter->tx_scrq[i]);
Thomas Falcon88eb98a2016-07-06 15:35:16 -05001380 irq_dispose_mapping(adapter->tx_scrq[i]->irq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001381 release_sub_crq_queue(adapter,
1382 adapter->tx_scrq[i]);
1383 }
Nathan Fontenot9501df32017-03-15 23:38:07 -04001384 kfree(adapter->tx_scrq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001385 adapter->tx_scrq = NULL;
1386 }
1387
1388 if (adapter->rx_scrq) {
1389 for (i = 0; i < adapter->req_rx_queues; i++)
1390 if (adapter->rx_scrq[i]) {
1391 free_irq(adapter->rx_scrq[i]->irq,
1392 adapter->rx_scrq[i]);
Thomas Falcon88eb98a2016-07-06 15:35:16 -05001393 irq_dispose_mapping(adapter->rx_scrq[i]->irq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001394 release_sub_crq_queue(adapter,
1395 adapter->rx_scrq[i]);
1396 }
Nathan Fontenot9501df32017-03-15 23:38:07 -04001397 kfree(adapter->rx_scrq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001398 adapter->rx_scrq = NULL;
1399 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001400}
1401
Thomas Falconea22d512016-07-06 15:35:17 -05001402static void release_sub_crqs_no_irqs(struct ibmvnic_adapter *adapter)
1403{
1404 int i;
1405
1406 if (adapter->tx_scrq) {
1407 for (i = 0; i < adapter->req_tx_queues; i++)
1408 if (adapter->tx_scrq[i])
1409 release_sub_crq_queue(adapter,
1410 adapter->tx_scrq[i]);
1411 adapter->tx_scrq = NULL;
1412 }
1413
1414 if (adapter->rx_scrq) {
1415 for (i = 0; i < adapter->req_rx_queues; i++)
1416 if (adapter->rx_scrq[i])
1417 release_sub_crq_queue(adapter,
1418 adapter->rx_scrq[i]);
1419 adapter->rx_scrq = NULL;
1420 }
Thomas Falconea22d512016-07-06 15:35:17 -05001421}
1422
Thomas Falcon032c5e82015-12-21 11:26:06 -06001423static int disable_scrq_irq(struct ibmvnic_adapter *adapter,
1424 struct ibmvnic_sub_crq_queue *scrq)
1425{
1426 struct device *dev = &adapter->vdev->dev;
1427 unsigned long rc;
1428
1429 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
1430 H_DISABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
1431 if (rc)
1432 dev_err(dev, "Couldn't disable scrq irq 0x%lx. rc=%ld\n",
1433 scrq->hw_irq, rc);
1434 return rc;
1435}
1436
1437static int enable_scrq_irq(struct ibmvnic_adapter *adapter,
1438 struct ibmvnic_sub_crq_queue *scrq)
1439{
1440 struct device *dev = &adapter->vdev->dev;
1441 unsigned long rc;
1442
1443 if (scrq->hw_irq > 0x100000000ULL) {
1444 dev_err(dev, "bad hw_irq = %lx\n", scrq->hw_irq);
1445 return 1;
1446 }
1447
1448 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
1449 H_ENABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
1450 if (rc)
1451 dev_err(dev, "Couldn't enable scrq irq 0x%lx. rc=%ld\n",
1452 scrq->hw_irq, rc);
1453 return rc;
1454}
1455
1456static int ibmvnic_complete_tx(struct ibmvnic_adapter *adapter,
1457 struct ibmvnic_sub_crq_queue *scrq)
1458{
1459 struct device *dev = &adapter->vdev->dev;
1460 struct ibmvnic_tx_buff *txbuff;
1461 union sub_crq *next;
1462 int index;
1463 int i, j;
Thomas Falconad7775d2016-04-01 17:20:34 -05001464 u8 first;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001465
1466restart_loop:
1467 while (pending_scrq(adapter, scrq)) {
1468 unsigned int pool = scrq->pool_index;
1469
1470 next = ibmvnic_next_scrq(adapter, scrq);
1471 for (i = 0; i < next->tx_comp.num_comps; i++) {
1472 if (next->tx_comp.rcs[i]) {
1473 dev_err(dev, "tx error %x\n",
1474 next->tx_comp.rcs[i]);
1475 continue;
1476 }
1477 index = be32_to_cpu(next->tx_comp.correlators[i]);
1478 txbuff = &adapter->tx_pool[pool].tx_buff[index];
1479
1480 for (j = 0; j < IBMVNIC_MAX_FRAGS_PER_CRQ; j++) {
1481 if (!txbuff->data_dma[j])
1482 continue;
1483
1484 txbuff->data_dma[j] = 0;
1485 txbuff->used_bounce = false;
1486 }
Thomas Falconad7775d2016-04-01 17:20:34 -05001487 /* if sub_crq was sent indirectly */
1488 first = txbuff->indir_arr[0].generic.first;
1489 if (first == IBMVNIC_CRQ_CMD) {
1490 dma_unmap_single(dev, txbuff->indir_dma,
1491 sizeof(txbuff->indir_arr),
1492 DMA_TO_DEVICE);
1493 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001494
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001495 if (txbuff->last_frag) {
1496 atomic_dec(&scrq->used);
1497
1498 if (atomic_read(&scrq->used) <=
1499 (adapter->req_tx_entries_per_subcrq / 2) &&
1500 netif_subqueue_stopped(adapter->netdev,
1501 txbuff->skb)) {
1502 netif_wake_subqueue(adapter->netdev,
1503 scrq->pool_index);
1504 netdev_dbg(adapter->netdev,
1505 "Started queue %d\n",
1506 scrq->pool_index);
1507 }
1508
Thomas Falcon032c5e82015-12-21 11:26:06 -06001509 dev_kfree_skb_any(txbuff->skb);
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001510 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001511
1512 adapter->tx_pool[pool].free_map[adapter->tx_pool[pool].
1513 producer_index] = index;
1514 adapter->tx_pool[pool].producer_index =
1515 (adapter->tx_pool[pool].producer_index + 1) %
Thomas Falcon068d9f92017-03-05 12:18:42 -06001516 adapter->req_tx_entries_per_subcrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001517 }
1518 /* remove tx_comp scrq*/
1519 next->tx_comp.first = 0;
1520 }
1521
1522 enable_scrq_irq(adapter, scrq);
1523
1524 if (pending_scrq(adapter, scrq)) {
1525 disable_scrq_irq(adapter, scrq);
1526 goto restart_loop;
1527 }
1528
1529 return 0;
1530}
1531
1532static irqreturn_t ibmvnic_interrupt_tx(int irq, void *instance)
1533{
1534 struct ibmvnic_sub_crq_queue *scrq = instance;
1535 struct ibmvnic_adapter *adapter = scrq->adapter;
1536
1537 disable_scrq_irq(adapter, scrq);
1538 ibmvnic_complete_tx(adapter, scrq);
1539
1540 return IRQ_HANDLED;
1541}
1542
1543static irqreturn_t ibmvnic_interrupt_rx(int irq, void *instance)
1544{
1545 struct ibmvnic_sub_crq_queue *scrq = instance;
1546 struct ibmvnic_adapter *adapter = scrq->adapter;
1547
1548 if (napi_schedule_prep(&adapter->napi[scrq->scrq_num])) {
1549 disable_scrq_irq(adapter, scrq);
1550 __napi_schedule(&adapter->napi[scrq->scrq_num]);
1551 }
1552
1553 return IRQ_HANDLED;
1554}
1555
Thomas Falconea22d512016-07-06 15:35:17 -05001556static int init_sub_crq_irqs(struct ibmvnic_adapter *adapter)
1557{
1558 struct device *dev = &adapter->vdev->dev;
1559 struct ibmvnic_sub_crq_queue *scrq;
1560 int i = 0, j = 0;
1561 int rc = 0;
1562
1563 for (i = 0; i < adapter->req_tx_queues; i++) {
1564 scrq = adapter->tx_scrq[i];
1565 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
1566
Michael Ellerman99c17902016-09-10 19:59:05 +10001567 if (!scrq->irq) {
Thomas Falconea22d512016-07-06 15:35:17 -05001568 rc = -EINVAL;
1569 dev_err(dev, "Error mapping irq\n");
1570 goto req_tx_irq_failed;
1571 }
1572
1573 rc = request_irq(scrq->irq, ibmvnic_interrupt_tx,
1574 0, "ibmvnic_tx", scrq);
1575
1576 if (rc) {
1577 dev_err(dev, "Couldn't register tx irq 0x%x. rc=%d\n",
1578 scrq->irq, rc);
1579 irq_dispose_mapping(scrq->irq);
1580 goto req_rx_irq_failed;
1581 }
1582 }
1583
1584 for (i = 0; i < adapter->req_rx_queues; i++) {
1585 scrq = adapter->rx_scrq[i];
1586 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
Michael Ellerman99c17902016-09-10 19:59:05 +10001587 if (!scrq->irq) {
Thomas Falconea22d512016-07-06 15:35:17 -05001588 rc = -EINVAL;
1589 dev_err(dev, "Error mapping irq\n");
1590 goto req_rx_irq_failed;
1591 }
1592 rc = request_irq(scrq->irq, ibmvnic_interrupt_rx,
1593 0, "ibmvnic_rx", scrq);
1594 if (rc) {
1595 dev_err(dev, "Couldn't register rx irq 0x%x. rc=%d\n",
1596 scrq->irq, rc);
1597 irq_dispose_mapping(scrq->irq);
1598 goto req_rx_irq_failed;
1599 }
1600 }
1601 return rc;
1602
1603req_rx_irq_failed:
Thomas Falcon8bf371e2016-10-27 12:28:52 -05001604 for (j = 0; j < i; j++) {
Thomas Falconea22d512016-07-06 15:35:17 -05001605 free_irq(adapter->rx_scrq[j]->irq, adapter->rx_scrq[j]);
1606 irq_dispose_mapping(adapter->rx_scrq[j]->irq);
Thomas Falcon8bf371e2016-10-27 12:28:52 -05001607 }
Thomas Falconea22d512016-07-06 15:35:17 -05001608 i = adapter->req_tx_queues;
1609req_tx_irq_failed:
Thomas Falcon8bf371e2016-10-27 12:28:52 -05001610 for (j = 0; j < i; j++) {
Thomas Falconea22d512016-07-06 15:35:17 -05001611 free_irq(adapter->tx_scrq[j]->irq, adapter->tx_scrq[j]);
1612 irq_dispose_mapping(adapter->rx_scrq[j]->irq);
Thomas Falcon8bf371e2016-10-27 12:28:52 -05001613 }
Thomas Falconea22d512016-07-06 15:35:17 -05001614 release_sub_crqs_no_irqs(adapter);
1615 return rc;
1616}
1617
Thomas Falcon032c5e82015-12-21 11:26:06 -06001618static void init_sub_crqs(struct ibmvnic_adapter *adapter, int retry)
1619{
1620 struct device *dev = &adapter->vdev->dev;
1621 struct ibmvnic_sub_crq_queue **allqueues;
1622 int registered_queues = 0;
1623 union ibmvnic_crq crq;
1624 int total_queues;
1625 int more = 0;
Thomas Falconea22d512016-07-06 15:35:17 -05001626 int i;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001627
1628 if (!retry) {
1629 /* Sub-CRQ entries are 32 byte long */
1630 int entries_page = 4 * PAGE_SIZE / (sizeof(u64) * 4);
1631
1632 if (adapter->min_tx_entries_per_subcrq > entries_page ||
1633 adapter->min_rx_add_entries_per_subcrq > entries_page) {
1634 dev_err(dev, "Fatal, invalid entries per sub-crq\n");
1635 goto allqueues_failed;
1636 }
1637
1638 /* Get the minimum between the queried max and the entries
1639 * that fit in our PAGE_SIZE
1640 */
1641 adapter->req_tx_entries_per_subcrq =
1642 adapter->max_tx_entries_per_subcrq > entries_page ?
1643 entries_page : adapter->max_tx_entries_per_subcrq;
1644 adapter->req_rx_add_entries_per_subcrq =
1645 adapter->max_rx_add_entries_per_subcrq > entries_page ?
1646 entries_page : adapter->max_rx_add_entries_per_subcrq;
1647
John Allen6dbcd8f2016-11-07 14:27:28 -06001648 adapter->req_tx_queues = adapter->opt_tx_comp_sub_queues;
1649 adapter->req_rx_queues = adapter->opt_rx_comp_queues;
John Allen498cd8e2016-04-06 11:49:55 -05001650 adapter->req_rx_add_queues = adapter->max_rx_add_queues;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001651
Thomas Falconf39f0d12017-02-14 10:22:59 -06001652 adapter->req_mtu = adapter->netdev->mtu + ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001653 }
1654
1655 total_queues = adapter->req_tx_queues + adapter->req_rx_queues;
1656
1657 allqueues = kcalloc(total_queues, sizeof(*allqueues), GFP_ATOMIC);
1658 if (!allqueues)
1659 goto allqueues_failed;
1660
1661 for (i = 0; i < total_queues; i++) {
1662 allqueues[i] = init_sub_crq_queue(adapter);
1663 if (!allqueues[i]) {
1664 dev_warn(dev, "Couldn't allocate all sub-crqs\n");
1665 break;
1666 }
1667 registered_queues++;
1668 }
1669
1670 /* Make sure we were able to register the minimum number of queues */
1671 if (registered_queues <
1672 adapter->min_tx_queues + adapter->min_rx_queues) {
1673 dev_err(dev, "Fatal: Couldn't init min number of sub-crqs\n");
1674 goto tx_failed;
1675 }
1676
1677 /* Distribute the failed allocated queues*/
1678 for (i = 0; i < total_queues - registered_queues + more ; i++) {
1679 netdev_dbg(adapter->netdev, "Reducing number of queues\n");
1680 switch (i % 3) {
1681 case 0:
1682 if (adapter->req_rx_queues > adapter->min_rx_queues)
1683 adapter->req_rx_queues--;
1684 else
1685 more++;
1686 break;
1687 case 1:
1688 if (adapter->req_tx_queues > adapter->min_tx_queues)
1689 adapter->req_tx_queues--;
1690 else
1691 more++;
1692 break;
1693 }
1694 }
1695
1696 adapter->tx_scrq = kcalloc(adapter->req_tx_queues,
1697 sizeof(*adapter->tx_scrq), GFP_ATOMIC);
1698 if (!adapter->tx_scrq)
1699 goto tx_failed;
1700
1701 for (i = 0; i < adapter->req_tx_queues; i++) {
1702 adapter->tx_scrq[i] = allqueues[i];
1703 adapter->tx_scrq[i]->pool_index = i;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001704 }
1705
1706 adapter->rx_scrq = kcalloc(adapter->req_rx_queues,
1707 sizeof(*adapter->rx_scrq), GFP_ATOMIC);
1708 if (!adapter->rx_scrq)
1709 goto rx_failed;
1710
1711 for (i = 0; i < adapter->req_rx_queues; i++) {
1712 adapter->rx_scrq[i] = allqueues[i + adapter->req_tx_queues];
1713 adapter->rx_scrq[i]->scrq_num = i;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001714 }
1715
1716 memset(&crq, 0, sizeof(crq));
1717 crq.request_capability.first = IBMVNIC_CRQ_CMD;
1718 crq.request_capability.cmd = REQUEST_CAPABILITY;
1719
1720 crq.request_capability.capability = cpu_to_be16(REQ_TX_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06001721 crq.request_capability.number = cpu_to_be64(adapter->req_tx_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06001722 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001723 ibmvnic_send_crq(adapter, &crq);
1724
1725 crq.request_capability.capability = cpu_to_be16(REQ_RX_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06001726 crq.request_capability.number = cpu_to_be64(adapter->req_rx_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06001727 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001728 ibmvnic_send_crq(adapter, &crq);
1729
1730 crq.request_capability.capability = cpu_to_be16(REQ_RX_ADD_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06001731 crq.request_capability.number = cpu_to_be64(adapter->req_rx_add_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06001732 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001733 ibmvnic_send_crq(adapter, &crq);
1734
1735 crq.request_capability.capability =
1736 cpu_to_be16(REQ_TX_ENTRIES_PER_SUBCRQ);
1737 crq.request_capability.number =
Thomas Falconde89e852016-03-01 10:20:09 -06001738 cpu_to_be64(adapter->req_tx_entries_per_subcrq);
Thomas Falcon901e0402017-02-15 12:17:59 -06001739 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001740 ibmvnic_send_crq(adapter, &crq);
1741
1742 crq.request_capability.capability =
1743 cpu_to_be16(REQ_RX_ADD_ENTRIES_PER_SUBCRQ);
1744 crq.request_capability.number =
Thomas Falconde89e852016-03-01 10:20:09 -06001745 cpu_to_be64(adapter->req_rx_add_entries_per_subcrq);
Thomas Falcon901e0402017-02-15 12:17:59 -06001746 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001747 ibmvnic_send_crq(adapter, &crq);
1748
1749 crq.request_capability.capability = cpu_to_be16(REQ_MTU);
Thomas Falconde89e852016-03-01 10:20:09 -06001750 crq.request_capability.number = cpu_to_be64(adapter->req_mtu);
Thomas Falcon901e0402017-02-15 12:17:59 -06001751 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001752 ibmvnic_send_crq(adapter, &crq);
1753
1754 if (adapter->netdev->flags & IFF_PROMISC) {
1755 if (adapter->promisc_supported) {
1756 crq.request_capability.capability =
1757 cpu_to_be16(PROMISC_REQUESTED);
Thomas Falconde89e852016-03-01 10:20:09 -06001758 crq.request_capability.number = cpu_to_be64(1);
Thomas Falcon901e0402017-02-15 12:17:59 -06001759 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001760 ibmvnic_send_crq(adapter, &crq);
1761 }
1762 } else {
1763 crq.request_capability.capability =
1764 cpu_to_be16(PROMISC_REQUESTED);
Thomas Falconde89e852016-03-01 10:20:09 -06001765 crq.request_capability.number = cpu_to_be64(0);
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
1770 kfree(allqueues);
1771
1772 return;
1773
Thomas Falcon032c5e82015-12-21 11:26:06 -06001774rx_failed:
1775 kfree(adapter->tx_scrq);
1776 adapter->tx_scrq = NULL;
1777tx_failed:
1778 for (i = 0; i < registered_queues; i++)
1779 release_sub_crq_queue(adapter, allqueues[i]);
1780 kfree(allqueues);
1781allqueues_failed:
1782 ibmvnic_remove(adapter->vdev);
1783}
1784
1785static int pending_scrq(struct ibmvnic_adapter *adapter,
1786 struct ibmvnic_sub_crq_queue *scrq)
1787{
1788 union sub_crq *entry = &scrq->msgs[scrq->cur];
1789
1790 if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP || adapter->closing)
1791 return 1;
1792 else
1793 return 0;
1794}
1795
1796static union sub_crq *ibmvnic_next_scrq(struct ibmvnic_adapter *adapter,
1797 struct ibmvnic_sub_crq_queue *scrq)
1798{
1799 union sub_crq *entry;
1800 unsigned long flags;
1801
1802 spin_lock_irqsave(&scrq->lock, flags);
1803 entry = &scrq->msgs[scrq->cur];
1804 if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP) {
1805 if (++scrq->cur == scrq->size)
1806 scrq->cur = 0;
1807 } else {
1808 entry = NULL;
1809 }
1810 spin_unlock_irqrestore(&scrq->lock, flags);
1811
1812 return entry;
1813}
1814
1815static union ibmvnic_crq *ibmvnic_next_crq(struct ibmvnic_adapter *adapter)
1816{
1817 struct ibmvnic_crq_queue *queue = &adapter->crq;
1818 union ibmvnic_crq *crq;
1819
1820 crq = &queue->msgs[queue->cur];
1821 if (crq->generic.first & IBMVNIC_CRQ_CMD_RSP) {
1822 if (++queue->cur == queue->size)
1823 queue->cur = 0;
1824 } else {
1825 crq = NULL;
1826 }
1827
1828 return crq;
1829}
1830
1831static int send_subcrq(struct ibmvnic_adapter *adapter, u64 remote_handle,
1832 union sub_crq *sub_crq)
1833{
1834 unsigned int ua = adapter->vdev->unit_address;
1835 struct device *dev = &adapter->vdev->dev;
1836 u64 *u64_crq = (u64 *)sub_crq;
1837 int rc;
1838
1839 netdev_dbg(adapter->netdev,
1840 "Sending sCRQ %016lx: %016lx %016lx %016lx %016lx\n",
1841 (unsigned long int)cpu_to_be64(remote_handle),
1842 (unsigned long int)cpu_to_be64(u64_crq[0]),
1843 (unsigned long int)cpu_to_be64(u64_crq[1]),
1844 (unsigned long int)cpu_to_be64(u64_crq[2]),
1845 (unsigned long int)cpu_to_be64(u64_crq[3]));
1846
1847 /* Make sure the hypervisor sees the complete request */
1848 mb();
1849
1850 rc = plpar_hcall_norets(H_SEND_SUB_CRQ, ua,
1851 cpu_to_be64(remote_handle),
1852 cpu_to_be64(u64_crq[0]),
1853 cpu_to_be64(u64_crq[1]),
1854 cpu_to_be64(u64_crq[2]),
1855 cpu_to_be64(u64_crq[3]));
1856
1857 if (rc) {
1858 if (rc == H_CLOSED)
1859 dev_warn(dev, "CRQ Queue closed\n");
1860 dev_err(dev, "Send error (rc=%d)\n", rc);
1861 }
1862
1863 return rc;
1864}
1865
Thomas Falconad7775d2016-04-01 17:20:34 -05001866static int send_subcrq_indirect(struct ibmvnic_adapter *adapter,
1867 u64 remote_handle, u64 ioba, u64 num_entries)
1868{
1869 unsigned int ua = adapter->vdev->unit_address;
1870 struct device *dev = &adapter->vdev->dev;
1871 int rc;
1872
1873 /* Make sure the hypervisor sees the complete request */
1874 mb();
1875 rc = plpar_hcall_norets(H_SEND_SUB_CRQ_INDIRECT, ua,
1876 cpu_to_be64(remote_handle),
1877 ioba, num_entries);
1878
1879 if (rc) {
1880 if (rc == H_CLOSED)
1881 dev_warn(dev, "CRQ Queue closed\n");
1882 dev_err(dev, "Send (indirect) error (rc=%d)\n", rc);
1883 }
1884
1885 return rc;
1886}
1887
Thomas Falcon032c5e82015-12-21 11:26:06 -06001888static int ibmvnic_send_crq(struct ibmvnic_adapter *adapter,
1889 union ibmvnic_crq *crq)
1890{
1891 unsigned int ua = adapter->vdev->unit_address;
1892 struct device *dev = &adapter->vdev->dev;
1893 u64 *u64_crq = (u64 *)crq;
1894 int rc;
1895
1896 netdev_dbg(adapter->netdev, "Sending CRQ: %016lx %016lx\n",
1897 (unsigned long int)cpu_to_be64(u64_crq[0]),
1898 (unsigned long int)cpu_to_be64(u64_crq[1]));
1899
1900 /* Make sure the hypervisor sees the complete request */
1901 mb();
1902
1903 rc = plpar_hcall_norets(H_SEND_CRQ, ua,
1904 cpu_to_be64(u64_crq[0]),
1905 cpu_to_be64(u64_crq[1]));
1906
1907 if (rc) {
1908 if (rc == H_CLOSED)
1909 dev_warn(dev, "CRQ Queue closed\n");
1910 dev_warn(dev, "Send error (rc=%d)\n", rc);
1911 }
1912
1913 return rc;
1914}
1915
1916static int ibmvnic_send_crq_init(struct ibmvnic_adapter *adapter)
1917{
1918 union ibmvnic_crq crq;
1919
1920 memset(&crq, 0, sizeof(crq));
1921 crq.generic.first = IBMVNIC_CRQ_INIT_CMD;
1922 crq.generic.cmd = IBMVNIC_CRQ_INIT;
1923 netdev_dbg(adapter->netdev, "Sending CRQ init\n");
1924
1925 return ibmvnic_send_crq(adapter, &crq);
1926}
1927
1928static int ibmvnic_send_crq_init_complete(struct ibmvnic_adapter *adapter)
1929{
1930 union ibmvnic_crq crq;
1931
1932 memset(&crq, 0, sizeof(crq));
1933 crq.generic.first = IBMVNIC_CRQ_INIT_CMD;
1934 crq.generic.cmd = IBMVNIC_CRQ_INIT_COMPLETE;
1935 netdev_dbg(adapter->netdev, "Sending CRQ init complete\n");
1936
1937 return ibmvnic_send_crq(adapter, &crq);
1938}
1939
1940static int send_version_xchg(struct ibmvnic_adapter *adapter)
1941{
1942 union ibmvnic_crq crq;
1943
1944 memset(&crq, 0, sizeof(crq));
1945 crq.version_exchange.first = IBMVNIC_CRQ_CMD;
1946 crq.version_exchange.cmd = VERSION_EXCHANGE;
1947 crq.version_exchange.version = cpu_to_be16(ibmvnic_version);
1948
1949 return ibmvnic_send_crq(adapter, &crq);
1950}
1951
1952static void send_login(struct ibmvnic_adapter *adapter)
1953{
1954 struct ibmvnic_login_rsp_buffer *login_rsp_buffer;
1955 struct ibmvnic_login_buffer *login_buffer;
1956 struct ibmvnic_inflight_cmd *inflight_cmd;
1957 struct device *dev = &adapter->vdev->dev;
1958 dma_addr_t rsp_buffer_token;
1959 dma_addr_t buffer_token;
1960 size_t rsp_buffer_size;
1961 union ibmvnic_crq crq;
1962 unsigned long flags;
1963 size_t buffer_size;
1964 __be64 *tx_list_p;
1965 __be64 *rx_list_p;
1966 int i;
1967
1968 buffer_size =
1969 sizeof(struct ibmvnic_login_buffer) +
1970 sizeof(u64) * (adapter->req_tx_queues + adapter->req_rx_queues);
1971
1972 login_buffer = kmalloc(buffer_size, GFP_ATOMIC);
1973 if (!login_buffer)
1974 goto buf_alloc_failed;
1975
1976 buffer_token = dma_map_single(dev, login_buffer, buffer_size,
1977 DMA_TO_DEVICE);
1978 if (dma_mapping_error(dev, buffer_token)) {
1979 dev_err(dev, "Couldn't map login buffer\n");
1980 goto buf_map_failed;
1981 }
1982
John Allen498cd8e2016-04-06 11:49:55 -05001983 rsp_buffer_size = sizeof(struct ibmvnic_login_rsp_buffer) +
1984 sizeof(u64) * adapter->req_tx_queues +
1985 sizeof(u64) * adapter->req_rx_queues +
1986 sizeof(u64) * adapter->req_rx_queues +
1987 sizeof(u8) * IBMVNIC_TX_DESC_VERSIONS;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001988
1989 login_rsp_buffer = kmalloc(rsp_buffer_size, GFP_ATOMIC);
1990 if (!login_rsp_buffer)
1991 goto buf_rsp_alloc_failed;
1992
1993 rsp_buffer_token = dma_map_single(dev, login_rsp_buffer,
1994 rsp_buffer_size, DMA_FROM_DEVICE);
1995 if (dma_mapping_error(dev, rsp_buffer_token)) {
1996 dev_err(dev, "Couldn't map login rsp buffer\n");
1997 goto buf_rsp_map_failed;
1998 }
1999 inflight_cmd = kmalloc(sizeof(*inflight_cmd), GFP_ATOMIC);
2000 if (!inflight_cmd) {
2001 dev_err(dev, "Couldn't allocate inflight_cmd\n");
2002 goto inflight_alloc_failed;
2003 }
2004 adapter->login_buf = login_buffer;
2005 adapter->login_buf_token = buffer_token;
2006 adapter->login_buf_sz = buffer_size;
2007 adapter->login_rsp_buf = login_rsp_buffer;
2008 adapter->login_rsp_buf_token = rsp_buffer_token;
2009 adapter->login_rsp_buf_sz = rsp_buffer_size;
2010
2011 login_buffer->len = cpu_to_be32(buffer_size);
2012 login_buffer->version = cpu_to_be32(INITIAL_VERSION_LB);
2013 login_buffer->num_txcomp_subcrqs = cpu_to_be32(adapter->req_tx_queues);
2014 login_buffer->off_txcomp_subcrqs =
2015 cpu_to_be32(sizeof(struct ibmvnic_login_buffer));
2016 login_buffer->num_rxcomp_subcrqs = cpu_to_be32(adapter->req_rx_queues);
2017 login_buffer->off_rxcomp_subcrqs =
2018 cpu_to_be32(sizeof(struct ibmvnic_login_buffer) +
2019 sizeof(u64) * adapter->req_tx_queues);
2020 login_buffer->login_rsp_ioba = cpu_to_be32(rsp_buffer_token);
2021 login_buffer->login_rsp_len = cpu_to_be32(rsp_buffer_size);
2022
2023 tx_list_p = (__be64 *)((char *)login_buffer +
2024 sizeof(struct ibmvnic_login_buffer));
2025 rx_list_p = (__be64 *)((char *)login_buffer +
2026 sizeof(struct ibmvnic_login_buffer) +
2027 sizeof(u64) * adapter->req_tx_queues);
2028
2029 for (i = 0; i < adapter->req_tx_queues; i++) {
2030 if (adapter->tx_scrq[i]) {
2031 tx_list_p[i] = cpu_to_be64(adapter->tx_scrq[i]->
2032 crq_num);
2033 }
2034 }
2035
2036 for (i = 0; i < adapter->req_rx_queues; i++) {
2037 if (adapter->rx_scrq[i]) {
2038 rx_list_p[i] = cpu_to_be64(adapter->rx_scrq[i]->
2039 crq_num);
2040 }
2041 }
2042
2043 netdev_dbg(adapter->netdev, "Login Buffer:\n");
2044 for (i = 0; i < (adapter->login_buf_sz - 1) / 8 + 1; i++) {
2045 netdev_dbg(adapter->netdev, "%016lx\n",
2046 ((unsigned long int *)(adapter->login_buf))[i]);
2047 }
2048
2049 memset(&crq, 0, sizeof(crq));
2050 crq.login.first = IBMVNIC_CRQ_CMD;
2051 crq.login.cmd = LOGIN;
2052 crq.login.ioba = cpu_to_be32(buffer_token);
2053 crq.login.len = cpu_to_be32(buffer_size);
2054
2055 memcpy(&inflight_cmd->crq, &crq, sizeof(crq));
2056
2057 spin_lock_irqsave(&adapter->inflight_lock, flags);
2058 list_add_tail(&inflight_cmd->list, &adapter->inflight);
2059 spin_unlock_irqrestore(&adapter->inflight_lock, flags);
2060
2061 ibmvnic_send_crq(adapter, &crq);
2062
2063 return;
2064
2065inflight_alloc_failed:
2066 dma_unmap_single(dev, rsp_buffer_token, rsp_buffer_size,
2067 DMA_FROM_DEVICE);
2068buf_rsp_map_failed:
2069 kfree(login_rsp_buffer);
2070buf_rsp_alloc_failed:
2071 dma_unmap_single(dev, buffer_token, buffer_size, DMA_TO_DEVICE);
2072buf_map_failed:
2073 kfree(login_buffer);
2074buf_alloc_failed:
2075 return;
2076}
2077
2078static void send_request_map(struct ibmvnic_adapter *adapter, dma_addr_t addr,
2079 u32 len, u8 map_id)
2080{
2081 union ibmvnic_crq crq;
2082
2083 memset(&crq, 0, sizeof(crq));
2084 crq.request_map.first = IBMVNIC_CRQ_CMD;
2085 crq.request_map.cmd = REQUEST_MAP;
2086 crq.request_map.map_id = map_id;
2087 crq.request_map.ioba = cpu_to_be32(addr);
2088 crq.request_map.len = cpu_to_be32(len);
2089 ibmvnic_send_crq(adapter, &crq);
2090}
2091
2092static void send_request_unmap(struct ibmvnic_adapter *adapter, u8 map_id)
2093{
2094 union ibmvnic_crq crq;
2095
2096 memset(&crq, 0, sizeof(crq));
2097 crq.request_unmap.first = IBMVNIC_CRQ_CMD;
2098 crq.request_unmap.cmd = REQUEST_UNMAP;
2099 crq.request_unmap.map_id = map_id;
2100 ibmvnic_send_crq(adapter, &crq);
2101}
2102
2103static void send_map_query(struct ibmvnic_adapter *adapter)
2104{
2105 union ibmvnic_crq crq;
2106
2107 memset(&crq, 0, sizeof(crq));
2108 crq.query_map.first = IBMVNIC_CRQ_CMD;
2109 crq.query_map.cmd = QUERY_MAP;
2110 ibmvnic_send_crq(adapter, &crq);
2111}
2112
2113/* Send a series of CRQs requesting various capabilities of the VNIC server */
2114static void send_cap_queries(struct ibmvnic_adapter *adapter)
2115{
2116 union ibmvnic_crq crq;
2117
Thomas Falcon901e0402017-02-15 12:17:59 -06002118 atomic_set(&adapter->running_cap_crqs, 0);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002119 memset(&crq, 0, sizeof(crq));
2120 crq.query_capability.first = IBMVNIC_CRQ_CMD;
2121 crq.query_capability.cmd = QUERY_CAPABILITY;
2122
2123 crq.query_capability.capability = cpu_to_be16(MIN_TX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002124 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002125 ibmvnic_send_crq(adapter, &crq);
2126
2127 crq.query_capability.capability = cpu_to_be16(MIN_RX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002128 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002129 ibmvnic_send_crq(adapter, &crq);
2130
2131 crq.query_capability.capability = cpu_to_be16(MIN_RX_ADD_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002132 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002133 ibmvnic_send_crq(adapter, &crq);
2134
2135 crq.query_capability.capability = cpu_to_be16(MAX_TX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002136 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002137 ibmvnic_send_crq(adapter, &crq);
2138
2139 crq.query_capability.capability = cpu_to_be16(MAX_RX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002140 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002141 ibmvnic_send_crq(adapter, &crq);
2142
2143 crq.query_capability.capability = cpu_to_be16(MAX_RX_ADD_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002144 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002145 ibmvnic_send_crq(adapter, &crq);
2146
2147 crq.query_capability.capability =
2148 cpu_to_be16(MIN_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06002149 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002150 ibmvnic_send_crq(adapter, &crq);
2151
2152 crq.query_capability.capability =
2153 cpu_to_be16(MIN_RX_ADD_ENTRIES_PER_SUBCRQ);
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 =
2158 cpu_to_be16(MAX_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06002159 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002160 ibmvnic_send_crq(adapter, &crq);
2161
2162 crq.query_capability.capability =
2163 cpu_to_be16(MAX_RX_ADD_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06002164 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002165 ibmvnic_send_crq(adapter, &crq);
2166
2167 crq.query_capability.capability = cpu_to_be16(TCP_IP_OFFLOAD);
Thomas Falcon901e0402017-02-15 12:17:59 -06002168 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002169 ibmvnic_send_crq(adapter, &crq);
2170
2171 crq.query_capability.capability = cpu_to_be16(PROMISC_SUPPORTED);
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 = cpu_to_be16(MIN_MTU);
Thomas Falcon901e0402017-02-15 12:17:59 -06002176 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002177 ibmvnic_send_crq(adapter, &crq);
2178
2179 crq.query_capability.capability = cpu_to_be16(MAX_MTU);
Thomas Falcon901e0402017-02-15 12:17:59 -06002180 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002181 ibmvnic_send_crq(adapter, &crq);
2182
2183 crq.query_capability.capability = cpu_to_be16(MAX_MULTICAST_FILTERS);
Thomas Falcon901e0402017-02-15 12:17:59 -06002184 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002185 ibmvnic_send_crq(adapter, &crq);
2186
2187 crq.query_capability.capability = cpu_to_be16(VLAN_HEADER_INSERTION);
Thomas Falcon901e0402017-02-15 12:17:59 -06002188 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002189 ibmvnic_send_crq(adapter, &crq);
2190
2191 crq.query_capability.capability = cpu_to_be16(MAX_TX_SG_ENTRIES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002192 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002193 ibmvnic_send_crq(adapter, &crq);
2194
2195 crq.query_capability.capability = cpu_to_be16(RX_SG_SUPPORTED);
Thomas Falcon901e0402017-02-15 12:17:59 -06002196 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002197 ibmvnic_send_crq(adapter, &crq);
2198
2199 crq.query_capability.capability = cpu_to_be16(OPT_TX_COMP_SUB_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002200 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002201 ibmvnic_send_crq(adapter, &crq);
2202
2203 crq.query_capability.capability = cpu_to_be16(OPT_RX_COMP_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002204 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002205 ibmvnic_send_crq(adapter, &crq);
2206
2207 crq.query_capability.capability =
2208 cpu_to_be16(OPT_RX_BUFADD_Q_PER_RX_COMP_Q);
Thomas Falcon901e0402017-02-15 12:17:59 -06002209 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002210 ibmvnic_send_crq(adapter, &crq);
2211
2212 crq.query_capability.capability =
2213 cpu_to_be16(OPT_TX_ENTRIES_PER_SUBCRQ);
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 =
2218 cpu_to_be16(OPT_RXBA_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06002219 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002220 ibmvnic_send_crq(adapter, &crq);
2221
2222 crq.query_capability.capability = cpu_to_be16(TX_RX_DESC_REQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06002223 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002224 ibmvnic_send_crq(adapter, &crq);
2225}
2226
2227static void handle_query_ip_offload_rsp(struct ibmvnic_adapter *adapter)
2228{
2229 struct device *dev = &adapter->vdev->dev;
2230 struct ibmvnic_query_ip_offload_buffer *buf = &adapter->ip_offload_buf;
2231 union ibmvnic_crq crq;
2232 int i;
2233
2234 dma_unmap_single(dev, adapter->ip_offload_tok,
2235 sizeof(adapter->ip_offload_buf), DMA_FROM_DEVICE);
2236
2237 netdev_dbg(adapter->netdev, "Query IP Offload Buffer:\n");
2238 for (i = 0; i < (sizeof(adapter->ip_offload_buf) - 1) / 8 + 1; i++)
2239 netdev_dbg(adapter->netdev, "%016lx\n",
2240 ((unsigned long int *)(buf))[i]);
2241
2242 netdev_dbg(adapter->netdev, "ipv4_chksum = %d\n", buf->ipv4_chksum);
2243 netdev_dbg(adapter->netdev, "ipv6_chksum = %d\n", buf->ipv6_chksum);
2244 netdev_dbg(adapter->netdev, "tcp_ipv4_chksum = %d\n",
2245 buf->tcp_ipv4_chksum);
2246 netdev_dbg(adapter->netdev, "tcp_ipv6_chksum = %d\n",
2247 buf->tcp_ipv6_chksum);
2248 netdev_dbg(adapter->netdev, "udp_ipv4_chksum = %d\n",
2249 buf->udp_ipv4_chksum);
2250 netdev_dbg(adapter->netdev, "udp_ipv6_chksum = %d\n",
2251 buf->udp_ipv6_chksum);
2252 netdev_dbg(adapter->netdev, "large_tx_ipv4 = %d\n",
2253 buf->large_tx_ipv4);
2254 netdev_dbg(adapter->netdev, "large_tx_ipv6 = %d\n",
2255 buf->large_tx_ipv6);
2256 netdev_dbg(adapter->netdev, "large_rx_ipv4 = %d\n",
2257 buf->large_rx_ipv4);
2258 netdev_dbg(adapter->netdev, "large_rx_ipv6 = %d\n",
2259 buf->large_rx_ipv6);
2260 netdev_dbg(adapter->netdev, "max_ipv4_hdr_sz = %d\n",
2261 buf->max_ipv4_header_size);
2262 netdev_dbg(adapter->netdev, "max_ipv6_hdr_sz = %d\n",
2263 buf->max_ipv6_header_size);
2264 netdev_dbg(adapter->netdev, "max_tcp_hdr_size = %d\n",
2265 buf->max_tcp_header_size);
2266 netdev_dbg(adapter->netdev, "max_udp_hdr_size = %d\n",
2267 buf->max_udp_header_size);
2268 netdev_dbg(adapter->netdev, "max_large_tx_size = %d\n",
2269 buf->max_large_tx_size);
2270 netdev_dbg(adapter->netdev, "max_large_rx_size = %d\n",
2271 buf->max_large_rx_size);
2272 netdev_dbg(adapter->netdev, "ipv6_ext_hdr = %d\n",
2273 buf->ipv6_extension_header);
2274 netdev_dbg(adapter->netdev, "tcp_pseudosum_req = %d\n",
2275 buf->tcp_pseudosum_req);
2276 netdev_dbg(adapter->netdev, "num_ipv6_ext_hd = %d\n",
2277 buf->num_ipv6_ext_headers);
2278 netdev_dbg(adapter->netdev, "off_ipv6_ext_hd = %d\n",
2279 buf->off_ipv6_ext_headers);
2280
2281 adapter->ip_offload_ctrl_tok =
2282 dma_map_single(dev, &adapter->ip_offload_ctrl,
2283 sizeof(adapter->ip_offload_ctrl), DMA_TO_DEVICE);
2284
2285 if (dma_mapping_error(dev, adapter->ip_offload_ctrl_tok)) {
2286 dev_err(dev, "Couldn't map ip offload control buffer\n");
2287 return;
2288 }
2289
2290 adapter->ip_offload_ctrl.version = cpu_to_be32(INITIAL_VERSION_IOB);
2291 adapter->ip_offload_ctrl.tcp_ipv4_chksum = buf->tcp_ipv4_chksum;
2292 adapter->ip_offload_ctrl.udp_ipv4_chksum = buf->udp_ipv4_chksum;
2293 adapter->ip_offload_ctrl.tcp_ipv6_chksum = buf->tcp_ipv6_chksum;
2294 adapter->ip_offload_ctrl.udp_ipv6_chksum = buf->udp_ipv6_chksum;
2295
2296 /* large_tx/rx disabled for now, additional features needed */
2297 adapter->ip_offload_ctrl.large_tx_ipv4 = 0;
2298 adapter->ip_offload_ctrl.large_tx_ipv6 = 0;
2299 adapter->ip_offload_ctrl.large_rx_ipv4 = 0;
2300 adapter->ip_offload_ctrl.large_rx_ipv6 = 0;
2301
2302 adapter->netdev->features = NETIF_F_GSO;
2303
2304 if (buf->tcp_ipv4_chksum || buf->udp_ipv4_chksum)
2305 adapter->netdev->features |= NETIF_F_IP_CSUM;
2306
2307 if (buf->tcp_ipv6_chksum || buf->udp_ipv6_chksum)
2308 adapter->netdev->features |= NETIF_F_IPV6_CSUM;
2309
Thomas Falcon9be02cd2016-04-01 17:20:35 -05002310 if ((adapter->netdev->features &
2311 (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)))
2312 adapter->netdev->features |= NETIF_F_RXCSUM;
2313
Thomas Falcon032c5e82015-12-21 11:26:06 -06002314 memset(&crq, 0, sizeof(crq));
2315 crq.control_ip_offload.first = IBMVNIC_CRQ_CMD;
2316 crq.control_ip_offload.cmd = CONTROL_IP_OFFLOAD;
2317 crq.control_ip_offload.len =
2318 cpu_to_be32(sizeof(adapter->ip_offload_ctrl));
2319 crq.control_ip_offload.ioba = cpu_to_be32(adapter->ip_offload_ctrl_tok);
2320 ibmvnic_send_crq(adapter, &crq);
2321}
2322
2323static void handle_error_info_rsp(union ibmvnic_crq *crq,
2324 struct ibmvnic_adapter *adapter)
2325{
2326 struct device *dev = &adapter->vdev->dev;
Wei Yongjun96183182016-06-27 20:48:53 +08002327 struct ibmvnic_error_buff *error_buff, *tmp;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002328 unsigned long flags;
2329 bool found = false;
2330 int i;
2331
2332 if (!crq->request_error_rsp.rc.code) {
2333 dev_info(dev, "Request Error Rsp returned with rc=%x\n",
2334 crq->request_error_rsp.rc.code);
2335 return;
2336 }
2337
2338 spin_lock_irqsave(&adapter->error_list_lock, flags);
Wei Yongjun96183182016-06-27 20:48:53 +08002339 list_for_each_entry_safe(error_buff, tmp, &adapter->errors, list)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002340 if (error_buff->error_id == crq->request_error_rsp.error_id) {
2341 found = true;
2342 list_del(&error_buff->list);
2343 break;
2344 }
2345 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
2346
2347 if (!found) {
2348 dev_err(dev, "Couldn't find error id %x\n",
Thomas Falcon75224c92017-02-15 10:33:33 -06002349 be32_to_cpu(crq->request_error_rsp.error_id));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002350 return;
2351 }
2352
2353 dev_err(dev, "Detailed info for error id %x:",
Thomas Falcon75224c92017-02-15 10:33:33 -06002354 be32_to_cpu(crq->request_error_rsp.error_id));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002355
2356 for (i = 0; i < error_buff->len; i++) {
2357 pr_cont("%02x", (int)error_buff->buff[i]);
2358 if (i % 8 == 7)
2359 pr_cont(" ");
2360 }
2361 pr_cont("\n");
2362
2363 dma_unmap_single(dev, error_buff->dma, error_buff->len,
2364 DMA_FROM_DEVICE);
2365 kfree(error_buff->buff);
2366 kfree(error_buff);
2367}
2368
Thomas Falcon032c5e82015-12-21 11:26:06 -06002369static void handle_error_indication(union ibmvnic_crq *crq,
2370 struct ibmvnic_adapter *adapter)
2371{
2372 int detail_len = be32_to_cpu(crq->error_indication.detail_error_sz);
2373 struct ibmvnic_inflight_cmd *inflight_cmd;
2374 struct device *dev = &adapter->vdev->dev;
2375 struct ibmvnic_error_buff *error_buff;
2376 union ibmvnic_crq new_crq;
2377 unsigned long flags;
2378
2379 dev_err(dev, "Firmware reports %serror id %x, cause %d\n",
2380 crq->error_indication.
2381 flags & IBMVNIC_FATAL_ERROR ? "FATAL " : "",
Thomas Falcon75224c92017-02-15 10:33:33 -06002382 be32_to_cpu(crq->error_indication.error_id),
2383 be16_to_cpu(crq->error_indication.error_cause));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002384
2385 error_buff = kmalloc(sizeof(*error_buff), GFP_ATOMIC);
2386 if (!error_buff)
2387 return;
2388
2389 error_buff->buff = kmalloc(detail_len, GFP_ATOMIC);
2390 if (!error_buff->buff) {
2391 kfree(error_buff);
2392 return;
2393 }
2394
2395 error_buff->dma = dma_map_single(dev, error_buff->buff, detail_len,
2396 DMA_FROM_DEVICE);
2397 if (dma_mapping_error(dev, error_buff->dma)) {
2398 if (!firmware_has_feature(FW_FEATURE_CMO))
2399 dev_err(dev, "Couldn't map error buffer\n");
2400 kfree(error_buff->buff);
2401 kfree(error_buff);
2402 return;
2403 }
2404
2405 inflight_cmd = kmalloc(sizeof(*inflight_cmd), GFP_ATOMIC);
2406 if (!inflight_cmd) {
2407 dma_unmap_single(dev, error_buff->dma, detail_len,
2408 DMA_FROM_DEVICE);
2409 kfree(error_buff->buff);
2410 kfree(error_buff);
2411 return;
2412 }
2413
2414 error_buff->len = detail_len;
2415 error_buff->error_id = crq->error_indication.error_id;
2416
2417 spin_lock_irqsave(&adapter->error_list_lock, flags);
2418 list_add_tail(&error_buff->list, &adapter->errors);
2419 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
2420
2421 memset(&new_crq, 0, sizeof(new_crq));
2422 new_crq.request_error_info.first = IBMVNIC_CRQ_CMD;
2423 new_crq.request_error_info.cmd = REQUEST_ERROR_INFO;
2424 new_crq.request_error_info.ioba = cpu_to_be32(error_buff->dma);
2425 new_crq.request_error_info.len = cpu_to_be32(detail_len);
2426 new_crq.request_error_info.error_id = crq->error_indication.error_id;
2427
2428 memcpy(&inflight_cmd->crq, &crq, sizeof(crq));
2429
2430 spin_lock_irqsave(&adapter->inflight_lock, flags);
2431 list_add_tail(&inflight_cmd->list, &adapter->inflight);
2432 spin_unlock_irqrestore(&adapter->inflight_lock, flags);
2433
2434 ibmvnic_send_crq(adapter, &new_crq);
2435}
2436
2437static void handle_change_mac_rsp(union ibmvnic_crq *crq,
2438 struct ibmvnic_adapter *adapter)
2439{
2440 struct net_device *netdev = adapter->netdev;
2441 struct device *dev = &adapter->vdev->dev;
2442 long rc;
2443
2444 rc = crq->change_mac_addr_rsp.rc.code;
2445 if (rc) {
2446 dev_err(dev, "Error %ld in CHANGE_MAC_ADDR_RSP\n", rc);
2447 return;
2448 }
2449 memcpy(netdev->dev_addr, &crq->change_mac_addr_rsp.mac_addr[0],
2450 ETH_ALEN);
2451}
2452
2453static void handle_request_cap_rsp(union ibmvnic_crq *crq,
2454 struct ibmvnic_adapter *adapter)
2455{
2456 struct device *dev = &adapter->vdev->dev;
2457 u64 *req_value;
2458 char *name;
2459
Thomas Falcon901e0402017-02-15 12:17:59 -06002460 atomic_dec(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002461 switch (be16_to_cpu(crq->request_capability_rsp.capability)) {
2462 case REQ_TX_QUEUES:
2463 req_value = &adapter->req_tx_queues;
2464 name = "tx";
2465 break;
2466 case REQ_RX_QUEUES:
2467 req_value = &adapter->req_rx_queues;
2468 name = "rx";
2469 break;
2470 case REQ_RX_ADD_QUEUES:
2471 req_value = &adapter->req_rx_add_queues;
2472 name = "rx_add";
2473 break;
2474 case REQ_TX_ENTRIES_PER_SUBCRQ:
2475 req_value = &adapter->req_tx_entries_per_subcrq;
2476 name = "tx_entries_per_subcrq";
2477 break;
2478 case REQ_RX_ADD_ENTRIES_PER_SUBCRQ:
2479 req_value = &adapter->req_rx_add_entries_per_subcrq;
2480 name = "rx_add_entries_per_subcrq";
2481 break;
2482 case REQ_MTU:
2483 req_value = &adapter->req_mtu;
2484 name = "mtu";
2485 break;
2486 case PROMISC_REQUESTED:
2487 req_value = &adapter->promisc;
2488 name = "promisc";
2489 break;
2490 default:
2491 dev_err(dev, "Got invalid cap request rsp %d\n",
2492 crq->request_capability.capability);
2493 return;
2494 }
2495
2496 switch (crq->request_capability_rsp.rc.code) {
2497 case SUCCESS:
2498 break;
2499 case PARTIALSUCCESS:
2500 dev_info(dev, "req=%lld, rsp=%ld in %s queue, retrying.\n",
2501 *req_value,
Thomas Falcon28f4d162017-02-15 10:32:11 -06002502 (long int)be64_to_cpu(crq->request_capability_rsp.
Thomas Falcon032c5e82015-12-21 11:26:06 -06002503 number), name);
Thomas Falconea22d512016-07-06 15:35:17 -05002504 release_sub_crqs_no_irqs(adapter);
Thomas Falcon28f4d162017-02-15 10:32:11 -06002505 *req_value = be64_to_cpu(crq->request_capability_rsp.number);
Thomas Falconea22d512016-07-06 15:35:17 -05002506 init_sub_crqs(adapter, 1);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002507 return;
2508 default:
2509 dev_err(dev, "Error %d in request cap rsp\n",
2510 crq->request_capability_rsp.rc.code);
2511 return;
2512 }
2513
2514 /* Done receiving requested capabilities, query IP offload support */
Thomas Falcon901e0402017-02-15 12:17:59 -06002515 if (atomic_read(&adapter->running_cap_crqs) == 0) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06002516 union ibmvnic_crq newcrq;
2517 int buf_sz = sizeof(struct ibmvnic_query_ip_offload_buffer);
2518 struct ibmvnic_query_ip_offload_buffer *ip_offload_buf =
2519 &adapter->ip_offload_buf;
2520
Thomas Falcon249168a2017-02-15 12:18:00 -06002521 adapter->wait_capability = false;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002522 adapter->ip_offload_tok = dma_map_single(dev, ip_offload_buf,
2523 buf_sz,
2524 DMA_FROM_DEVICE);
2525
2526 if (dma_mapping_error(dev, adapter->ip_offload_tok)) {
2527 if (!firmware_has_feature(FW_FEATURE_CMO))
2528 dev_err(dev, "Couldn't map offload buffer\n");
2529 return;
2530 }
2531
2532 memset(&newcrq, 0, sizeof(newcrq));
2533 newcrq.query_ip_offload.first = IBMVNIC_CRQ_CMD;
2534 newcrq.query_ip_offload.cmd = QUERY_IP_OFFLOAD;
2535 newcrq.query_ip_offload.len = cpu_to_be32(buf_sz);
2536 newcrq.query_ip_offload.ioba =
2537 cpu_to_be32(adapter->ip_offload_tok);
2538
2539 ibmvnic_send_crq(adapter, &newcrq);
2540 }
2541}
2542
2543static int handle_login_rsp(union ibmvnic_crq *login_rsp_crq,
2544 struct ibmvnic_adapter *adapter)
2545{
2546 struct device *dev = &adapter->vdev->dev;
2547 struct ibmvnic_login_rsp_buffer *login_rsp = adapter->login_rsp_buf;
2548 struct ibmvnic_login_buffer *login = adapter->login_buf;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002549 int i;
2550
2551 dma_unmap_single(dev, adapter->login_buf_token, adapter->login_buf_sz,
2552 DMA_BIDIRECTIONAL);
2553 dma_unmap_single(dev, adapter->login_rsp_buf_token,
2554 adapter->login_rsp_buf_sz, DMA_BIDIRECTIONAL);
2555
John Allen498cd8e2016-04-06 11:49:55 -05002556 /* If the number of queues requested can't be allocated by the
2557 * server, the login response will return with code 1. We will need
2558 * to resend the login buffer with fewer queues requested.
2559 */
2560 if (login_rsp_crq->generic.rc.code) {
2561 adapter->renegotiate = true;
2562 complete(&adapter->init_done);
2563 return 0;
2564 }
2565
Thomas Falcon032c5e82015-12-21 11:26:06 -06002566 netdev_dbg(adapter->netdev, "Login Response Buffer:\n");
2567 for (i = 0; i < (adapter->login_rsp_buf_sz - 1) / 8 + 1; i++) {
2568 netdev_dbg(adapter->netdev, "%016lx\n",
2569 ((unsigned long int *)(adapter->login_rsp_buf))[i]);
2570 }
2571
2572 /* Sanity checks */
2573 if (login->num_txcomp_subcrqs != login_rsp->num_txsubm_subcrqs ||
2574 (be32_to_cpu(login->num_rxcomp_subcrqs) *
2575 adapter->req_rx_add_queues !=
2576 be32_to_cpu(login_rsp->num_rxadd_subcrqs))) {
2577 dev_err(dev, "FATAL: Inconsistent login and login rsp\n");
2578 ibmvnic_remove(adapter->vdev);
2579 return -EIO;
2580 }
2581 complete(&adapter->init_done);
2582
Thomas Falcon032c5e82015-12-21 11:26:06 -06002583 return 0;
2584}
2585
2586static void handle_request_map_rsp(union ibmvnic_crq *crq,
2587 struct ibmvnic_adapter *adapter)
2588{
2589 struct device *dev = &adapter->vdev->dev;
2590 u8 map_id = crq->request_map_rsp.map_id;
2591 int tx_subcrqs;
2592 int rx_subcrqs;
2593 long rc;
2594 int i;
2595
2596 tx_subcrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
2597 rx_subcrqs = be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
2598
2599 rc = crq->request_map_rsp.rc.code;
2600 if (rc) {
2601 dev_err(dev, "Error %ld in REQUEST_MAP_RSP\n", rc);
2602 adapter->map_id--;
2603 /* need to find and zero tx/rx_pool map_id */
2604 for (i = 0; i < tx_subcrqs; i++) {
2605 if (adapter->tx_pool[i].long_term_buff.map_id == map_id)
2606 adapter->tx_pool[i].long_term_buff.map_id = 0;
2607 }
2608 for (i = 0; i < rx_subcrqs; i++) {
2609 if (adapter->rx_pool[i].long_term_buff.map_id == map_id)
2610 adapter->rx_pool[i].long_term_buff.map_id = 0;
2611 }
2612 }
2613 complete(&adapter->fw_done);
2614}
2615
2616static void handle_request_unmap_rsp(union ibmvnic_crq *crq,
2617 struct ibmvnic_adapter *adapter)
2618{
2619 struct device *dev = &adapter->vdev->dev;
2620 long rc;
2621
2622 rc = crq->request_unmap_rsp.rc.code;
2623 if (rc)
2624 dev_err(dev, "Error %ld in REQUEST_UNMAP_RSP\n", rc);
2625}
2626
2627static void handle_query_map_rsp(union ibmvnic_crq *crq,
2628 struct ibmvnic_adapter *adapter)
2629{
2630 struct net_device *netdev = adapter->netdev;
2631 struct device *dev = &adapter->vdev->dev;
2632 long rc;
2633
2634 rc = crq->query_map_rsp.rc.code;
2635 if (rc) {
2636 dev_err(dev, "Error %ld in QUERY_MAP_RSP\n", rc);
2637 return;
2638 }
2639 netdev_dbg(netdev, "page_size = %d\ntot_pages = %d\nfree_pages = %d\n",
2640 crq->query_map_rsp.page_size, crq->query_map_rsp.tot_pages,
2641 crq->query_map_rsp.free_pages);
2642}
2643
2644static void handle_query_cap_rsp(union ibmvnic_crq *crq,
2645 struct ibmvnic_adapter *adapter)
2646{
2647 struct net_device *netdev = adapter->netdev;
2648 struct device *dev = &adapter->vdev->dev;
2649 long rc;
2650
Thomas Falcon901e0402017-02-15 12:17:59 -06002651 atomic_dec(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002652 netdev_dbg(netdev, "Outstanding queries: %d\n",
Thomas Falcon901e0402017-02-15 12:17:59 -06002653 atomic_read(&adapter->running_cap_crqs));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002654 rc = crq->query_capability.rc.code;
2655 if (rc) {
2656 dev_err(dev, "Error %ld in QUERY_CAP_RSP\n", rc);
2657 goto out;
2658 }
2659
2660 switch (be16_to_cpu(crq->query_capability.capability)) {
2661 case MIN_TX_QUEUES:
2662 adapter->min_tx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06002663 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002664 netdev_dbg(netdev, "min_tx_queues = %lld\n",
2665 adapter->min_tx_queues);
2666 break;
2667 case MIN_RX_QUEUES:
2668 adapter->min_rx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06002669 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002670 netdev_dbg(netdev, "min_rx_queues = %lld\n",
2671 adapter->min_rx_queues);
2672 break;
2673 case MIN_RX_ADD_QUEUES:
2674 adapter->min_rx_add_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06002675 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002676 netdev_dbg(netdev, "min_rx_add_queues = %lld\n",
2677 adapter->min_rx_add_queues);
2678 break;
2679 case MAX_TX_QUEUES:
2680 adapter->max_tx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06002681 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002682 netdev_dbg(netdev, "max_tx_queues = %lld\n",
2683 adapter->max_tx_queues);
2684 break;
2685 case MAX_RX_QUEUES:
2686 adapter->max_rx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06002687 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002688 netdev_dbg(netdev, "max_rx_queues = %lld\n",
2689 adapter->max_rx_queues);
2690 break;
2691 case MAX_RX_ADD_QUEUES:
2692 adapter->max_rx_add_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06002693 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002694 netdev_dbg(netdev, "max_rx_add_queues = %lld\n",
2695 adapter->max_rx_add_queues);
2696 break;
2697 case MIN_TX_ENTRIES_PER_SUBCRQ:
2698 adapter->min_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06002699 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002700 netdev_dbg(netdev, "min_tx_entries_per_subcrq = %lld\n",
2701 adapter->min_tx_entries_per_subcrq);
2702 break;
2703 case MIN_RX_ADD_ENTRIES_PER_SUBCRQ:
2704 adapter->min_rx_add_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06002705 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002706 netdev_dbg(netdev, "min_rx_add_entrs_per_subcrq = %lld\n",
2707 adapter->min_rx_add_entries_per_subcrq);
2708 break;
2709 case MAX_TX_ENTRIES_PER_SUBCRQ:
2710 adapter->max_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06002711 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002712 netdev_dbg(netdev, "max_tx_entries_per_subcrq = %lld\n",
2713 adapter->max_tx_entries_per_subcrq);
2714 break;
2715 case MAX_RX_ADD_ENTRIES_PER_SUBCRQ:
2716 adapter->max_rx_add_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06002717 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002718 netdev_dbg(netdev, "max_rx_add_entrs_per_subcrq = %lld\n",
2719 adapter->max_rx_add_entries_per_subcrq);
2720 break;
2721 case TCP_IP_OFFLOAD:
2722 adapter->tcp_ip_offload =
Thomas Falconde89e852016-03-01 10:20:09 -06002723 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002724 netdev_dbg(netdev, "tcp_ip_offload = %lld\n",
2725 adapter->tcp_ip_offload);
2726 break;
2727 case PROMISC_SUPPORTED:
2728 adapter->promisc_supported =
Thomas Falconde89e852016-03-01 10:20:09 -06002729 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002730 netdev_dbg(netdev, "promisc_supported = %lld\n",
2731 adapter->promisc_supported);
2732 break;
2733 case MIN_MTU:
Thomas Falconde89e852016-03-01 10:20:09 -06002734 adapter->min_mtu = be64_to_cpu(crq->query_capability.number);
Thomas Falconf39f0d12017-02-14 10:22:59 -06002735 netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002736 netdev_dbg(netdev, "min_mtu = %lld\n", adapter->min_mtu);
2737 break;
2738 case MAX_MTU:
Thomas Falconde89e852016-03-01 10:20:09 -06002739 adapter->max_mtu = be64_to_cpu(crq->query_capability.number);
Thomas Falconf39f0d12017-02-14 10:22:59 -06002740 netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002741 netdev_dbg(netdev, "max_mtu = %lld\n", adapter->max_mtu);
2742 break;
2743 case MAX_MULTICAST_FILTERS:
2744 adapter->max_multicast_filters =
Thomas Falconde89e852016-03-01 10:20:09 -06002745 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002746 netdev_dbg(netdev, "max_multicast_filters = %lld\n",
2747 adapter->max_multicast_filters);
2748 break;
2749 case VLAN_HEADER_INSERTION:
2750 adapter->vlan_header_insertion =
Thomas Falconde89e852016-03-01 10:20:09 -06002751 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002752 if (adapter->vlan_header_insertion)
2753 netdev->features |= NETIF_F_HW_VLAN_STAG_TX;
2754 netdev_dbg(netdev, "vlan_header_insertion = %lld\n",
2755 adapter->vlan_header_insertion);
2756 break;
2757 case MAX_TX_SG_ENTRIES:
2758 adapter->max_tx_sg_entries =
Thomas Falconde89e852016-03-01 10:20:09 -06002759 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002760 netdev_dbg(netdev, "max_tx_sg_entries = %lld\n",
2761 adapter->max_tx_sg_entries);
2762 break;
2763 case RX_SG_SUPPORTED:
2764 adapter->rx_sg_supported =
Thomas Falconde89e852016-03-01 10:20:09 -06002765 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002766 netdev_dbg(netdev, "rx_sg_supported = %lld\n",
2767 adapter->rx_sg_supported);
2768 break;
2769 case OPT_TX_COMP_SUB_QUEUES:
2770 adapter->opt_tx_comp_sub_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06002771 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002772 netdev_dbg(netdev, "opt_tx_comp_sub_queues = %lld\n",
2773 adapter->opt_tx_comp_sub_queues);
2774 break;
2775 case OPT_RX_COMP_QUEUES:
2776 adapter->opt_rx_comp_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06002777 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002778 netdev_dbg(netdev, "opt_rx_comp_queues = %lld\n",
2779 adapter->opt_rx_comp_queues);
2780 break;
2781 case OPT_RX_BUFADD_Q_PER_RX_COMP_Q:
2782 adapter->opt_rx_bufadd_q_per_rx_comp_q =
Thomas Falconde89e852016-03-01 10:20:09 -06002783 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002784 netdev_dbg(netdev, "opt_rx_bufadd_q_per_rx_comp_q = %lld\n",
2785 adapter->opt_rx_bufadd_q_per_rx_comp_q);
2786 break;
2787 case OPT_TX_ENTRIES_PER_SUBCRQ:
2788 adapter->opt_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06002789 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002790 netdev_dbg(netdev, "opt_tx_entries_per_subcrq = %lld\n",
2791 adapter->opt_tx_entries_per_subcrq);
2792 break;
2793 case OPT_RXBA_ENTRIES_PER_SUBCRQ:
2794 adapter->opt_rxba_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06002795 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002796 netdev_dbg(netdev, "opt_rxba_entries_per_subcrq = %lld\n",
2797 adapter->opt_rxba_entries_per_subcrq);
2798 break;
2799 case TX_RX_DESC_REQ:
2800 adapter->tx_rx_desc_req = crq->query_capability.number;
2801 netdev_dbg(netdev, "tx_rx_desc_req = %llx\n",
2802 adapter->tx_rx_desc_req);
2803 break;
2804
2805 default:
2806 netdev_err(netdev, "Got invalid cap rsp %d\n",
2807 crq->query_capability.capability);
2808 }
2809
2810out:
Thomas Falcon249168a2017-02-15 12:18:00 -06002811 if (atomic_read(&adapter->running_cap_crqs) == 0) {
2812 adapter->wait_capability = false;
Thomas Falconea22d512016-07-06 15:35:17 -05002813 init_sub_crqs(adapter, 0);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002814 /* We're done querying the capabilities, initialize sub-crqs */
Thomas Falcon249168a2017-02-15 12:18:00 -06002815 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002816}
2817
Thomas Falcon032c5e82015-12-21 11:26:06 -06002818static void ibmvnic_free_inflight(struct ibmvnic_adapter *adapter)
2819{
Wei Yongjun96183182016-06-27 20:48:53 +08002820 struct ibmvnic_inflight_cmd *inflight_cmd, *tmp1;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002821 struct device *dev = &adapter->vdev->dev;
Wei Yongjun96183182016-06-27 20:48:53 +08002822 struct ibmvnic_error_buff *error_buff, *tmp2;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002823 unsigned long flags;
2824 unsigned long flags2;
2825
2826 spin_lock_irqsave(&adapter->inflight_lock, flags);
Wei Yongjun96183182016-06-27 20:48:53 +08002827 list_for_each_entry_safe(inflight_cmd, tmp1, &adapter->inflight, list) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06002828 switch (inflight_cmd->crq.generic.cmd) {
2829 case LOGIN:
2830 dma_unmap_single(dev, adapter->login_buf_token,
2831 adapter->login_buf_sz,
2832 DMA_BIDIRECTIONAL);
2833 dma_unmap_single(dev, adapter->login_rsp_buf_token,
2834 adapter->login_rsp_buf_sz,
2835 DMA_BIDIRECTIONAL);
2836 kfree(adapter->login_rsp_buf);
2837 kfree(adapter->login_buf);
2838 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002839 case REQUEST_ERROR_INFO:
2840 spin_lock_irqsave(&adapter->error_list_lock, flags2);
Wei Yongjun96183182016-06-27 20:48:53 +08002841 list_for_each_entry_safe(error_buff, tmp2,
2842 &adapter->errors, list) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06002843 dma_unmap_single(dev, error_buff->dma,
2844 error_buff->len,
2845 DMA_FROM_DEVICE);
2846 kfree(error_buff->buff);
2847 list_del(&error_buff->list);
2848 kfree(error_buff);
2849 }
2850 spin_unlock_irqrestore(&adapter->error_list_lock,
2851 flags2);
2852 break;
2853 }
2854 list_del(&inflight_cmd->list);
2855 kfree(inflight_cmd);
2856 }
2857 spin_unlock_irqrestore(&adapter->inflight_lock, flags);
2858}
2859
Thomas Falcon9888d7b2016-10-27 12:28:51 -05002860static void ibmvnic_xport_event(struct work_struct *work)
2861{
2862 struct ibmvnic_adapter *adapter = container_of(work,
2863 struct ibmvnic_adapter,
2864 ibmvnic_xport);
2865 struct device *dev = &adapter->vdev->dev;
2866 long rc;
2867
2868 ibmvnic_free_inflight(adapter);
2869 release_sub_crqs(adapter);
2870 if (adapter->migrated) {
2871 rc = ibmvnic_reenable_crq_queue(adapter);
2872 if (rc)
2873 dev_err(dev, "Error after enable rc=%ld\n", rc);
2874 adapter->migrated = false;
2875 rc = ibmvnic_send_crq_init(adapter);
2876 if (rc)
2877 dev_err(dev, "Error sending init rc=%ld\n", rc);
2878 }
2879}
2880
Thomas Falcon032c5e82015-12-21 11:26:06 -06002881static void ibmvnic_handle_crq(union ibmvnic_crq *crq,
2882 struct ibmvnic_adapter *adapter)
2883{
2884 struct ibmvnic_generic_crq *gen_crq = &crq->generic;
2885 struct net_device *netdev = adapter->netdev;
2886 struct device *dev = &adapter->vdev->dev;
2887 long rc;
2888
2889 netdev_dbg(netdev, "Handling CRQ: %016lx %016lx\n",
2890 ((unsigned long int *)crq)[0],
2891 ((unsigned long int *)crq)[1]);
2892 switch (gen_crq->first) {
2893 case IBMVNIC_CRQ_INIT_RSP:
2894 switch (gen_crq->cmd) {
2895 case IBMVNIC_CRQ_INIT:
2896 dev_info(dev, "Partner initialized\n");
2897 /* Send back a response */
2898 rc = ibmvnic_send_crq_init_complete(adapter);
Thomas Falcon65dc6892016-07-06 15:35:18 -05002899 if (!rc)
2900 schedule_work(&adapter->vnic_crq_init);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002901 else
2902 dev_err(dev, "Can't send initrsp rc=%ld\n", rc);
2903 break;
2904 case IBMVNIC_CRQ_INIT_COMPLETE:
2905 dev_info(dev, "Partner initialization complete\n");
2906 send_version_xchg(adapter);
2907 break;
2908 default:
2909 dev_err(dev, "Unknown crq cmd: %d\n", gen_crq->cmd);
2910 }
2911 return;
2912 case IBMVNIC_CRQ_XPORT_EVENT:
2913 if (gen_crq->cmd == IBMVNIC_PARTITION_MIGRATED) {
2914 dev_info(dev, "Re-enabling adapter\n");
2915 adapter->migrated = true;
Thomas Falcon9888d7b2016-10-27 12:28:51 -05002916 schedule_work(&adapter->ibmvnic_xport);
Thomas Falcondfad09a2016-08-18 11:37:51 -05002917 } else if (gen_crq->cmd == IBMVNIC_DEVICE_FAILOVER) {
2918 dev_info(dev, "Backing device failover detected\n");
2919 netif_carrier_off(netdev);
2920 adapter->failover = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002921 } else {
2922 /* The adapter lost the connection */
2923 dev_err(dev, "Virtual Adapter failed (rc=%d)\n",
2924 gen_crq->cmd);
Thomas Falcon9888d7b2016-10-27 12:28:51 -05002925 schedule_work(&adapter->ibmvnic_xport);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002926 }
2927 return;
2928 case IBMVNIC_CRQ_CMD_RSP:
2929 break;
2930 default:
2931 dev_err(dev, "Got an invalid msg type 0x%02x\n",
2932 gen_crq->first);
2933 return;
2934 }
2935
2936 switch (gen_crq->cmd) {
2937 case VERSION_EXCHANGE_RSP:
2938 rc = crq->version_exchange_rsp.rc.code;
2939 if (rc) {
2940 dev_err(dev, "Error %ld in VERSION_EXCHG_RSP\n", rc);
2941 break;
2942 }
2943 dev_info(dev, "Partner protocol version is %d\n",
2944 crq->version_exchange_rsp.version);
2945 if (be16_to_cpu(crq->version_exchange_rsp.version) <
2946 ibmvnic_version)
2947 ibmvnic_version =
2948 be16_to_cpu(crq->version_exchange_rsp.version);
2949 send_cap_queries(adapter);
2950 break;
2951 case QUERY_CAPABILITY_RSP:
2952 handle_query_cap_rsp(crq, adapter);
2953 break;
2954 case QUERY_MAP_RSP:
2955 handle_query_map_rsp(crq, adapter);
2956 break;
2957 case REQUEST_MAP_RSP:
2958 handle_request_map_rsp(crq, adapter);
2959 break;
2960 case REQUEST_UNMAP_RSP:
2961 handle_request_unmap_rsp(crq, adapter);
2962 break;
2963 case REQUEST_CAPABILITY_RSP:
2964 handle_request_cap_rsp(crq, adapter);
2965 break;
2966 case LOGIN_RSP:
2967 netdev_dbg(netdev, "Got Login Response\n");
2968 handle_login_rsp(crq, adapter);
2969 break;
2970 case LOGICAL_LINK_STATE_RSP:
2971 netdev_dbg(netdev, "Got Logical Link State Response\n");
2972 adapter->logical_link_state =
2973 crq->logical_link_state_rsp.link_state;
2974 break;
2975 case LINK_STATE_INDICATION:
2976 netdev_dbg(netdev, "Got Logical Link State Indication\n");
2977 adapter->phys_link_state =
2978 crq->link_state_indication.phys_link_state;
2979 adapter->logical_link_state =
2980 crq->link_state_indication.logical_link_state;
2981 break;
2982 case CHANGE_MAC_ADDR_RSP:
2983 netdev_dbg(netdev, "Got MAC address change Response\n");
2984 handle_change_mac_rsp(crq, adapter);
2985 break;
2986 case ERROR_INDICATION:
2987 netdev_dbg(netdev, "Got Error Indication\n");
2988 handle_error_indication(crq, adapter);
2989 break;
2990 case REQUEST_ERROR_RSP:
2991 netdev_dbg(netdev, "Got Error Detail Response\n");
2992 handle_error_info_rsp(crq, adapter);
2993 break;
2994 case REQUEST_STATISTICS_RSP:
2995 netdev_dbg(netdev, "Got Statistics Response\n");
2996 complete(&adapter->stats_done);
2997 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002998 case QUERY_IP_OFFLOAD_RSP:
2999 netdev_dbg(netdev, "Got Query IP offload Response\n");
3000 handle_query_ip_offload_rsp(adapter);
3001 break;
3002 case MULTICAST_CTRL_RSP:
3003 netdev_dbg(netdev, "Got multicast control Response\n");
3004 break;
3005 case CONTROL_IP_OFFLOAD_RSP:
3006 netdev_dbg(netdev, "Got Control IP offload Response\n");
3007 dma_unmap_single(dev, adapter->ip_offload_ctrl_tok,
3008 sizeof(adapter->ip_offload_ctrl),
3009 DMA_TO_DEVICE);
John Allenbd0b6722017-03-17 17:13:40 -05003010 complete(&adapter->init_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003011 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003012 case COLLECT_FW_TRACE_RSP:
3013 netdev_dbg(netdev, "Got Collect firmware trace Response\n");
3014 complete(&adapter->fw_done);
3015 break;
3016 default:
3017 netdev_err(netdev, "Got an invalid cmd type 0x%02x\n",
3018 gen_crq->cmd);
3019 }
3020}
3021
3022static irqreturn_t ibmvnic_interrupt(int irq, void *instance)
3023{
3024 struct ibmvnic_adapter *adapter = instance;
Thomas Falcon6c267b32017-02-15 12:17:58 -06003025 unsigned long flags;
3026
3027 spin_lock_irqsave(&adapter->crq.lock, flags);
3028 vio_disable_interrupts(adapter->vdev);
3029 tasklet_schedule(&adapter->tasklet);
3030 spin_unlock_irqrestore(&adapter->crq.lock, flags);
3031 return IRQ_HANDLED;
3032}
3033
3034static void ibmvnic_tasklet(void *data)
3035{
3036 struct ibmvnic_adapter *adapter = data;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003037 struct ibmvnic_crq_queue *queue = &adapter->crq;
3038 struct vio_dev *vdev = adapter->vdev;
3039 union ibmvnic_crq *crq;
3040 unsigned long flags;
3041 bool done = false;
3042
3043 spin_lock_irqsave(&queue->lock, flags);
3044 vio_disable_interrupts(vdev);
3045 while (!done) {
3046 /* Pull all the valid messages off the CRQ */
3047 while ((crq = ibmvnic_next_crq(adapter)) != NULL) {
3048 ibmvnic_handle_crq(crq, adapter);
3049 crq->generic.first = 0;
3050 }
3051 vio_enable_interrupts(vdev);
3052 crq = ibmvnic_next_crq(adapter);
3053 if (crq) {
3054 vio_disable_interrupts(vdev);
3055 ibmvnic_handle_crq(crq, adapter);
3056 crq->generic.first = 0;
3057 } else {
Thomas Falcon249168a2017-02-15 12:18:00 -06003058 /* remain in tasklet until all
3059 * capabilities responses are received
3060 */
3061 if (!adapter->wait_capability)
3062 done = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003063 }
3064 }
Thomas Falcon249168a2017-02-15 12:18:00 -06003065 /* if capabilities CRQ's were sent in this tasklet, the following
3066 * tasklet must wait until all responses are received
3067 */
3068 if (atomic_read(&adapter->running_cap_crqs) != 0)
3069 adapter->wait_capability = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003070 spin_unlock_irqrestore(&queue->lock, flags);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003071}
3072
3073static int ibmvnic_reenable_crq_queue(struct ibmvnic_adapter *adapter)
3074{
3075 struct vio_dev *vdev = adapter->vdev;
3076 int rc;
3077
3078 do {
3079 rc = plpar_hcall_norets(H_ENABLE_CRQ, vdev->unit_address);
3080 } while (rc == H_IN_PROGRESS || rc == H_BUSY || H_IS_LONG_BUSY(rc));
3081
3082 if (rc)
3083 dev_err(&vdev->dev, "Error enabling adapter (rc=%d)\n", rc);
3084
3085 return rc;
3086}
3087
3088static int ibmvnic_reset_crq(struct ibmvnic_adapter *adapter)
3089{
3090 struct ibmvnic_crq_queue *crq = &adapter->crq;
3091 struct device *dev = &adapter->vdev->dev;
3092 struct vio_dev *vdev = adapter->vdev;
3093 int rc;
3094
3095 /* Close the CRQ */
3096 do {
3097 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
3098 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
3099
3100 /* Clean out the queue */
3101 memset(crq->msgs, 0, PAGE_SIZE);
3102 crq->cur = 0;
3103
3104 /* And re-open it again */
3105 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
3106 crq->msg_token, PAGE_SIZE);
3107
3108 if (rc == H_CLOSED)
3109 /* Adapter is good, but other end is not ready */
3110 dev_warn(dev, "Partner adapter not ready\n");
3111 else if (rc != 0)
3112 dev_warn(dev, "Couldn't register crq (rc=%d)\n", rc);
3113
3114 return rc;
3115}
3116
Nathan Fontenotf9928872017-03-30 02:48:54 -04003117static void release_crq_queue(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003118{
3119 struct ibmvnic_crq_queue *crq = &adapter->crq;
3120 struct vio_dev *vdev = adapter->vdev;
3121 long rc;
3122
Nathan Fontenotf9928872017-03-30 02:48:54 -04003123 if (!crq->msgs)
3124 return;
3125
Thomas Falcon032c5e82015-12-21 11:26:06 -06003126 netdev_dbg(adapter->netdev, "Releasing CRQ\n");
3127 free_irq(vdev->irq, adapter);
Thomas Falcon6c267b32017-02-15 12:17:58 -06003128 tasklet_kill(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003129 do {
3130 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
3131 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
3132
3133 dma_unmap_single(&vdev->dev, crq->msg_token, PAGE_SIZE,
3134 DMA_BIDIRECTIONAL);
3135 free_page((unsigned long)crq->msgs);
Nathan Fontenotf9928872017-03-30 02:48:54 -04003136 crq->msgs = NULL;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003137}
3138
Nathan Fontenotf9928872017-03-30 02:48:54 -04003139static int init_crq_queue(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003140{
3141 struct ibmvnic_crq_queue *crq = &adapter->crq;
3142 struct device *dev = &adapter->vdev->dev;
3143 struct vio_dev *vdev = adapter->vdev;
3144 int rc, retrc = -ENOMEM;
3145
Nathan Fontenotf9928872017-03-30 02:48:54 -04003146 if (crq->msgs)
3147 return 0;
3148
Thomas Falcon032c5e82015-12-21 11:26:06 -06003149 crq->msgs = (union ibmvnic_crq *)get_zeroed_page(GFP_KERNEL);
3150 /* Should we allocate more than one page? */
3151
3152 if (!crq->msgs)
3153 return -ENOMEM;
3154
3155 crq->size = PAGE_SIZE / sizeof(*crq->msgs);
3156 crq->msg_token = dma_map_single(dev, crq->msgs, PAGE_SIZE,
3157 DMA_BIDIRECTIONAL);
3158 if (dma_mapping_error(dev, crq->msg_token))
3159 goto map_failed;
3160
3161 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
3162 crq->msg_token, PAGE_SIZE);
3163
3164 if (rc == H_RESOURCE)
3165 /* maybe kexecing and resource is busy. try a reset */
3166 rc = ibmvnic_reset_crq(adapter);
3167 retrc = rc;
3168
3169 if (rc == H_CLOSED) {
3170 dev_warn(dev, "Partner adapter not ready\n");
3171 } else if (rc) {
3172 dev_warn(dev, "Error %d opening adapter\n", rc);
3173 goto reg_crq_failed;
3174 }
3175
3176 retrc = 0;
3177
Thomas Falcon6c267b32017-02-15 12:17:58 -06003178 tasklet_init(&adapter->tasklet, (void *)ibmvnic_tasklet,
3179 (unsigned long)adapter);
3180
Thomas Falcon032c5e82015-12-21 11:26:06 -06003181 netdev_dbg(adapter->netdev, "registering irq 0x%x\n", vdev->irq);
3182 rc = request_irq(vdev->irq, ibmvnic_interrupt, 0, IBMVNIC_NAME,
3183 adapter);
3184 if (rc) {
3185 dev_err(dev, "Couldn't register irq 0x%x. rc=%d\n",
3186 vdev->irq, rc);
3187 goto req_irq_failed;
3188 }
3189
3190 rc = vio_enable_interrupts(vdev);
3191 if (rc) {
3192 dev_err(dev, "Error %d enabling interrupts\n", rc);
3193 goto req_irq_failed;
3194 }
3195
3196 crq->cur = 0;
3197 spin_lock_init(&crq->lock);
3198
3199 return retrc;
3200
3201req_irq_failed:
Thomas Falcon6c267b32017-02-15 12:17:58 -06003202 tasklet_kill(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003203 do {
3204 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
3205 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
3206reg_crq_failed:
3207 dma_unmap_single(dev, crq->msg_token, PAGE_SIZE, DMA_BIDIRECTIONAL);
3208map_failed:
3209 free_page((unsigned long)crq->msgs);
Nathan Fontenotf9928872017-03-30 02:48:54 -04003210 crq->msgs = NULL;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003211 return retrc;
3212}
3213
Thomas Falcon65dc6892016-07-06 15:35:18 -05003214static void handle_crq_init_rsp(struct work_struct *work)
3215{
3216 struct ibmvnic_adapter *adapter = container_of(work,
3217 struct ibmvnic_adapter,
3218 vnic_crq_init);
3219 struct device *dev = &adapter->vdev->dev;
3220 struct net_device *netdev = adapter->netdev;
3221 unsigned long timeout = msecs_to_jiffies(30000);
Thomas Falcondfad09a2016-08-18 11:37:51 -05003222 bool restart = false;
Thomas Falcon65dc6892016-07-06 15:35:18 -05003223 int rc;
3224
Thomas Falcondfad09a2016-08-18 11:37:51 -05003225 if (adapter->failover) {
3226 release_sub_crqs(adapter);
3227 if (netif_running(netdev)) {
3228 netif_tx_disable(netdev);
3229 ibmvnic_close(netdev);
3230 restart = true;
3231 }
3232 }
3233
Thomas Falcon65dc6892016-07-06 15:35:18 -05003234 reinit_completion(&adapter->init_done);
Nathan Fontenotdb5d0b52017-02-10 13:45:05 -05003235 send_version_xchg(adapter);
Thomas Falcon65dc6892016-07-06 15:35:18 -05003236 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
3237 dev_err(dev, "Passive init timeout\n");
3238 goto task_failed;
3239 }
3240
Thomas Falconf39f0d12017-02-14 10:22:59 -06003241 netdev->mtu = adapter->req_mtu - ETH_HLEN;
Thomas Falcon65dc6892016-07-06 15:35:18 -05003242
Thomas Falcondfad09a2016-08-18 11:37:51 -05003243 if (adapter->failover) {
3244 adapter->failover = false;
3245 if (restart) {
3246 rc = ibmvnic_open(netdev);
3247 if (rc)
3248 goto restart_failed;
3249 }
3250 netif_carrier_on(netdev);
3251 return;
3252 }
3253
Thomas Falcon65dc6892016-07-06 15:35:18 -05003254 rc = register_netdev(netdev);
3255 if (rc) {
3256 dev_err(dev,
3257 "failed to register netdev rc=%d\n", rc);
3258 goto register_failed;
3259 }
3260 dev_info(dev, "ibmvnic registered\n");
3261
3262 return;
3263
Thomas Falcondfad09a2016-08-18 11:37:51 -05003264restart_failed:
3265 dev_err(dev, "Failed to restart ibmvnic, rc=%d\n", rc);
Thomas Falcon65dc6892016-07-06 15:35:18 -05003266register_failed:
3267 release_sub_crqs(adapter);
3268task_failed:
3269 dev_err(dev, "Passive initialization was not successful\n");
3270}
3271
John Allenf6ef6402017-03-17 17:13:42 -05003272static int ibmvnic_init(struct ibmvnic_adapter *adapter)
3273{
3274 struct device *dev = &adapter->vdev->dev;
3275 unsigned long timeout = msecs_to_jiffies(30000);
John Allenf6ef6402017-03-17 17:13:42 -05003276 int rc;
3277
Nathan Fontenotf9928872017-03-30 02:48:54 -04003278 rc = init_crq_queue(adapter);
John Allenf6ef6402017-03-17 17:13:42 -05003279 if (rc) {
3280 dev_err(dev, "Couldn't initialize crq. rc=%d\n", rc);
3281 return rc;
3282 }
3283
3284 adapter->stats_token = dma_map_single(dev, &adapter->stats,
3285 sizeof(struct ibmvnic_statistics),
3286 DMA_FROM_DEVICE);
3287 if (dma_mapping_error(dev, adapter->stats_token)) {
Nathan Fontenotf9928872017-03-30 02:48:54 -04003288 release_crq_queue(adapter);
John Allenf6ef6402017-03-17 17:13:42 -05003289 dev_err(dev, "Couldn't map stats buffer\n");
3290 return -ENOMEM;
3291 }
3292
John Allenf6ef6402017-03-17 17:13:42 -05003293 init_completion(&adapter->init_done);
3294 ibmvnic_send_crq_init(adapter);
3295 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
3296 dev_err(dev, "Initialization sequence timed out\n");
Nathan Fontenotf9928872017-03-30 02:48:54 -04003297 release_crq_queue(adapter);
John Allenf6ef6402017-03-17 17:13:42 -05003298 return -1;
3299 }
3300
3301 return 0;
3302}
3303
Thomas Falcon032c5e82015-12-21 11:26:06 -06003304static int ibmvnic_probe(struct vio_dev *dev, const struct vio_device_id *id)
3305{
3306 struct ibmvnic_adapter *adapter;
3307 struct net_device *netdev;
3308 unsigned char *mac_addr_p;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003309 int rc;
3310
3311 dev_dbg(&dev->dev, "entering ibmvnic_probe for UA 0x%x\n",
3312 dev->unit_address);
3313
3314 mac_addr_p = (unsigned char *)vio_get_attribute(dev,
3315 VETH_MAC_ADDR, NULL);
3316 if (!mac_addr_p) {
3317 dev_err(&dev->dev,
3318 "(%s:%3.3d) ERROR: Can't find MAC_ADDR attribute\n",
3319 __FILE__, __LINE__);
3320 return 0;
3321 }
3322
3323 netdev = alloc_etherdev_mq(sizeof(struct ibmvnic_adapter),
3324 IBMVNIC_MAX_TX_QUEUES);
3325 if (!netdev)
3326 return -ENOMEM;
3327
3328 adapter = netdev_priv(netdev);
3329 dev_set_drvdata(&dev->dev, netdev);
3330 adapter->vdev = dev;
3331 adapter->netdev = netdev;
Thomas Falcondfad09a2016-08-18 11:37:51 -05003332 adapter->failover = false;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003333
3334 ether_addr_copy(adapter->mac_addr, mac_addr_p);
3335 ether_addr_copy(netdev->dev_addr, adapter->mac_addr);
3336 netdev->irq = dev->irq;
3337 netdev->netdev_ops = &ibmvnic_netdev_ops;
3338 netdev->ethtool_ops = &ibmvnic_ethtool_ops;
3339 SET_NETDEV_DEV(netdev, &dev->dev);
3340
Thomas Falcon65dc6892016-07-06 15:35:18 -05003341 INIT_WORK(&adapter->vnic_crq_init, handle_crq_init_rsp);
Thomas Falcon9888d7b2016-10-27 12:28:51 -05003342 INIT_WORK(&adapter->ibmvnic_xport, ibmvnic_xport_event);
Thomas Falcon65dc6892016-07-06 15:35:18 -05003343
Thomas Falcon032c5e82015-12-21 11:26:06 -06003344 spin_lock_init(&adapter->stats_lock);
3345
Thomas Falcon032c5e82015-12-21 11:26:06 -06003346 INIT_LIST_HEAD(&adapter->errors);
3347 INIT_LIST_HEAD(&adapter->inflight);
3348 spin_lock_init(&adapter->error_list_lock);
3349 spin_lock_init(&adapter->inflight_lock);
3350
John Allenf6ef6402017-03-17 17:13:42 -05003351 rc = ibmvnic_init(adapter);
3352 if (rc) {
3353 free_netdev(netdev);
3354 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003355 }
3356
Thomas Falconf39f0d12017-02-14 10:22:59 -06003357 netdev->mtu = adapter->req_mtu - ETH_HLEN;
John Allenea5509f2017-03-17 17:13:43 -05003358 adapter->is_closed = false;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003359
3360 rc = register_netdev(netdev);
3361 if (rc) {
3362 dev_err(&dev->dev, "failed to register netdev rc=%d\n", rc);
John Allenf6ef6402017-03-17 17:13:42 -05003363 free_netdev(netdev);
3364 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003365 }
3366 dev_info(&dev->dev, "ibmvnic registered\n");
3367
3368 return 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003369}
3370
3371static int ibmvnic_remove(struct vio_dev *dev)
3372{
3373 struct net_device *netdev = dev_get_drvdata(&dev->dev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003374
3375 unregister_netdev(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003376 free_netdev(netdev);
3377 dev_set_drvdata(&dev->dev, NULL);
3378
3379 return 0;
3380}
3381
3382static unsigned long ibmvnic_get_desired_dma(struct vio_dev *vdev)
3383{
3384 struct net_device *netdev = dev_get_drvdata(&vdev->dev);
3385 struct ibmvnic_adapter *adapter;
3386 struct iommu_table *tbl;
3387 unsigned long ret = 0;
3388 int i;
3389
3390 tbl = get_iommu_table_base(&vdev->dev);
3391
3392 /* netdev inits at probe time along with the structures we need below*/
3393 if (!netdev)
3394 return IOMMU_PAGE_ALIGN(IBMVNIC_IO_ENTITLEMENT_DEFAULT, tbl);
3395
3396 adapter = netdev_priv(netdev);
3397
3398 ret += PAGE_SIZE; /* the crq message queue */
3399 ret += adapter->bounce_buffer_size;
3400 ret += IOMMU_PAGE_ALIGN(sizeof(struct ibmvnic_statistics), tbl);
3401
3402 for (i = 0; i < adapter->req_tx_queues + adapter->req_rx_queues; i++)
3403 ret += 4 * PAGE_SIZE; /* the scrq message queue */
3404
3405 for (i = 0; i < be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
3406 i++)
3407 ret += adapter->rx_pool[i].size *
3408 IOMMU_PAGE_ALIGN(adapter->rx_pool[i].buff_size, tbl);
3409
3410 return ret;
3411}
3412
3413static int ibmvnic_resume(struct device *dev)
3414{
3415 struct net_device *netdev = dev_get_drvdata(dev);
3416 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
3417 int i;
3418
3419 /* kick the interrupt handlers just in case we lost an interrupt */
3420 for (i = 0; i < adapter->req_rx_queues; i++)
3421 ibmvnic_interrupt_rx(adapter->rx_scrq[i]->irq,
3422 adapter->rx_scrq[i]);
3423
3424 return 0;
3425}
3426
3427static struct vio_device_id ibmvnic_device_table[] = {
3428 {"network", "IBM,vnic"},
3429 {"", "" }
3430};
3431MODULE_DEVICE_TABLE(vio, ibmvnic_device_table);
3432
3433static const struct dev_pm_ops ibmvnic_pm_ops = {
3434 .resume = ibmvnic_resume
3435};
3436
3437static struct vio_driver ibmvnic_driver = {
3438 .id_table = ibmvnic_device_table,
3439 .probe = ibmvnic_probe,
3440 .remove = ibmvnic_remove,
3441 .get_desired_dma = ibmvnic_get_desired_dma,
3442 .name = ibmvnic_driver_name,
3443 .pm = &ibmvnic_pm_ops,
3444};
3445
3446/* module functions */
3447static int __init ibmvnic_module_init(void)
3448{
3449 pr_info("%s: %s %s\n", ibmvnic_driver_name, ibmvnic_driver_string,
3450 IBMVNIC_DRIVER_VERSION);
3451
3452 return vio_register_driver(&ibmvnic_driver);
3453}
3454
3455static void __exit ibmvnic_module_exit(void)
3456{
3457 vio_unregister_driver(&ibmvnic_driver);
3458}
3459
3460module_init(ibmvnic_module_init);
3461module_exit(ibmvnic_module_exit);