blob: 6774b3cbc5f96ca4d3f7e2b65b2a2257c6515f25 [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
Thomas Falcon032c5e82015-12-21 11:26:06 -0600166static int alloc_long_term_buff(struct ibmvnic_adapter *adapter,
167 struct ibmvnic_long_term_buff *ltb, int size)
168{
169 struct device *dev = &adapter->vdev->dev;
170
171 ltb->size = size;
172 ltb->buff = dma_alloc_coherent(dev, ltb->size, &ltb->addr,
173 GFP_KERNEL);
174
175 if (!ltb->buff) {
176 dev_err(dev, "Couldn't alloc long term buffer\n");
177 return -ENOMEM;
178 }
179 ltb->map_id = adapter->map_id;
180 adapter->map_id++;
Nathan Fontenotdb5d0b52017-02-10 13:45:05 -0500181
182 init_completion(&adapter->fw_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600183 send_request_map(adapter, ltb->addr,
184 ltb->size, ltb->map_id);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600185 wait_for_completion(&adapter->fw_done);
186 return 0;
187}
188
189static void free_long_term_buff(struct ibmvnic_adapter *adapter,
190 struct ibmvnic_long_term_buff *ltb)
191{
192 struct device *dev = &adapter->vdev->dev;
193
Nathan Fontenotc657e322017-03-30 02:49:06 -0400194 if (!ltb->buff)
195 return;
196
Thomas Falcon032c5e82015-12-21 11:26:06 -0600197 dma_free_coherent(dev, ltb->size, ltb->buff, ltb->addr);
Thomas Falcondfad09a2016-08-18 11:37:51 -0500198 if (!adapter->failover)
199 send_request_unmap(adapter, ltb->map_id);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600200}
201
Thomas Falcon032c5e82015-12-21 11:26:06 -0600202static void replenish_rx_pool(struct ibmvnic_adapter *adapter,
203 struct ibmvnic_rx_pool *pool)
204{
205 int count = pool->size - atomic_read(&pool->available);
206 struct device *dev = &adapter->vdev->dev;
207 int buffers_added = 0;
208 unsigned long lpar_rc;
209 union sub_crq sub_crq;
210 struct sk_buff *skb;
211 unsigned int offset;
212 dma_addr_t dma_addr;
213 unsigned char *dst;
214 u64 *handle_array;
215 int shift = 0;
216 int index;
217 int i;
218
219 handle_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
220 be32_to_cpu(adapter->login_rsp_buf->
221 off_rxadd_subcrqs));
222
223 for (i = 0; i < count; ++i) {
224 skb = alloc_skb(pool->buff_size, GFP_ATOMIC);
225 if (!skb) {
226 dev_err(dev, "Couldn't replenish rx buff\n");
227 adapter->replenish_no_mem++;
228 break;
229 }
230
231 index = pool->free_map[pool->next_free];
232
233 if (pool->rx_buff[index].skb)
234 dev_err(dev, "Inconsistent free_map!\n");
235
236 /* Copy the skb to the long term mapped DMA buffer */
237 offset = index * pool->buff_size;
238 dst = pool->long_term_buff.buff + offset;
239 memset(dst, 0, pool->buff_size);
240 dma_addr = pool->long_term_buff.addr + offset;
241 pool->rx_buff[index].data = dst;
242
243 pool->free_map[pool->next_free] = IBMVNIC_INVALID_MAP;
244 pool->rx_buff[index].dma = dma_addr;
245 pool->rx_buff[index].skb = skb;
246 pool->rx_buff[index].pool_index = pool->index;
247 pool->rx_buff[index].size = pool->buff_size;
248
249 memset(&sub_crq, 0, sizeof(sub_crq));
250 sub_crq.rx_add.first = IBMVNIC_CRQ_CMD;
251 sub_crq.rx_add.correlator =
252 cpu_to_be64((u64)&pool->rx_buff[index]);
253 sub_crq.rx_add.ioba = cpu_to_be32(dma_addr);
254 sub_crq.rx_add.map_id = pool->long_term_buff.map_id;
255
256 /* The length field of the sCRQ is defined to be 24 bits so the
257 * buffer size needs to be left shifted by a byte before it is
258 * converted to big endian to prevent the last byte from being
259 * truncated.
260 */
261#ifdef __LITTLE_ENDIAN__
262 shift = 8;
263#endif
264 sub_crq.rx_add.len = cpu_to_be32(pool->buff_size << shift);
265
266 lpar_rc = send_subcrq(adapter, handle_array[pool->index],
267 &sub_crq);
268 if (lpar_rc != H_SUCCESS)
269 goto failure;
270
271 buffers_added++;
272 adapter->replenish_add_buff_success++;
273 pool->next_free = (pool->next_free + 1) % pool->size;
274 }
275 atomic_add(buffers_added, &pool->available);
276 return;
277
278failure:
279 dev_info(dev, "replenish pools failure\n");
280 pool->free_map[pool->next_free] = index;
281 pool->rx_buff[index].skb = NULL;
282 if (!dma_mapping_error(dev, dma_addr))
283 dma_unmap_single(dev, dma_addr, pool->buff_size,
284 DMA_FROM_DEVICE);
285
286 dev_kfree_skb_any(skb);
287 adapter->replenish_add_buff_failure++;
288 atomic_add(buffers_added, &pool->available);
289}
290
291static void replenish_pools(struct ibmvnic_adapter *adapter)
292{
293 int i;
294
295 if (adapter->migrated)
296 return;
297
298 adapter->replenish_task_cycles++;
299 for (i = 0; i < be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
300 i++) {
301 if (adapter->rx_pool[i].active)
302 replenish_rx_pool(adapter, &adapter->rx_pool[i]);
303 }
304}
305
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400306static void release_rx_pools(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -0600307{
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400308 struct ibmvnic_rx_pool *rx_pool;
309 int rx_scrqs;
310 int i, j;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600311
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400312 if (!adapter->rx_pool)
Thomas Falcon032c5e82015-12-21 11:26:06 -0600313 return;
314
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400315 rx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
316 for (i = 0; i < rx_scrqs; i++) {
317 rx_pool = &adapter->rx_pool[i];
318
319 kfree(rx_pool->free_map);
320 free_long_term_buff(adapter, &rx_pool->long_term_buff);
321
322 if (!rx_pool->rx_buff)
323 continue;
324
325 for (j = 0; j < rx_pool->size; j++) {
326 if (rx_pool->rx_buff[j].skb) {
327 dev_kfree_skb_any(rx_pool->rx_buff[i].skb);
328 rx_pool->rx_buff[i].skb = NULL;
329 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600330 }
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400331
332 kfree(rx_pool->rx_buff);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600333 }
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400334
335 kfree(adapter->rx_pool);
336 adapter->rx_pool = NULL;
337}
338
339static int init_rx_pools(struct net_device *netdev)
340{
341 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
342 struct device *dev = &adapter->vdev->dev;
343 struct ibmvnic_rx_pool *rx_pool;
344 int rxadd_subcrqs;
345 u64 *size_array;
346 int i, j;
347
348 rxadd_subcrqs =
349 be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
350 size_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
351 be32_to_cpu(adapter->login_rsp_buf->off_rxadd_buff_size));
352
353 adapter->rx_pool = kcalloc(rxadd_subcrqs,
354 sizeof(struct ibmvnic_rx_pool),
355 GFP_KERNEL);
356 if (!adapter->rx_pool) {
357 dev_err(dev, "Failed to allocate rx pools\n");
358 return -1;
359 }
360
361 for (i = 0; i < rxadd_subcrqs; i++) {
362 rx_pool = &adapter->rx_pool[i];
363
364 netdev_dbg(adapter->netdev,
365 "Initializing rx_pool %d, %lld buffs, %lld bytes each\n",
366 i, adapter->req_rx_add_entries_per_subcrq,
367 be64_to_cpu(size_array[i]));
368
369 rx_pool->size = adapter->req_rx_add_entries_per_subcrq;
370 rx_pool->index = i;
371 rx_pool->buff_size = be64_to_cpu(size_array[i]);
372 rx_pool->active = 1;
373
374 rx_pool->free_map = kcalloc(rx_pool->size, sizeof(int),
375 GFP_KERNEL);
376 if (!rx_pool->free_map) {
377 release_rx_pools(adapter);
378 return -1;
379 }
380
381 rx_pool->rx_buff = kcalloc(rx_pool->size,
382 sizeof(struct ibmvnic_rx_buff),
383 GFP_KERNEL);
384 if (!rx_pool->rx_buff) {
385 dev_err(dev, "Couldn't alloc rx buffers\n");
386 release_rx_pools(adapter);
387 return -1;
388 }
389
390 if (alloc_long_term_buff(adapter, &rx_pool->long_term_buff,
391 rx_pool->size * rx_pool->buff_size)) {
392 release_rx_pools(adapter);
393 return -1;
394 }
395
396 for (j = 0; j < rx_pool->size; ++j)
397 rx_pool->free_map[j] = j;
398
399 atomic_set(&rx_pool->available, 0);
400 rx_pool->next_alloc = 0;
401 rx_pool->next_free = 0;
402 }
403
404 return 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600405}
406
Nathan Fontenotc657e322017-03-30 02:49:06 -0400407static void release_tx_pools(struct ibmvnic_adapter *adapter)
408{
409 struct ibmvnic_tx_pool *tx_pool;
410 int i, tx_scrqs;
411
412 if (!adapter->tx_pool)
413 return;
414
415 tx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
416 for (i = 0; i < tx_scrqs; i++) {
417 tx_pool = &adapter->tx_pool[i];
418 kfree(tx_pool->tx_buff);
419 free_long_term_buff(adapter, &tx_pool->long_term_buff);
420 kfree(tx_pool->free_map);
421 }
422
423 kfree(adapter->tx_pool);
424 adapter->tx_pool = NULL;
425}
426
427static int init_tx_pools(struct net_device *netdev)
428{
429 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
430 struct device *dev = &adapter->vdev->dev;
431 struct ibmvnic_tx_pool *tx_pool;
432 int tx_subcrqs;
433 int i, j;
434
435 tx_subcrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
436 adapter->tx_pool = kcalloc(tx_subcrqs,
437 sizeof(struct ibmvnic_tx_pool), GFP_KERNEL);
438 if (!adapter->tx_pool)
439 return -1;
440
441 for (i = 0; i < tx_subcrqs; i++) {
442 tx_pool = &adapter->tx_pool[i];
443 tx_pool->tx_buff = kcalloc(adapter->req_tx_entries_per_subcrq,
444 sizeof(struct ibmvnic_tx_buff),
445 GFP_KERNEL);
446 if (!tx_pool->tx_buff) {
447 dev_err(dev, "tx pool buffer allocation failed\n");
448 release_tx_pools(adapter);
449 return -1;
450 }
451
452 if (alloc_long_term_buff(adapter, &tx_pool->long_term_buff,
453 adapter->req_tx_entries_per_subcrq *
454 adapter->req_mtu)) {
455 release_tx_pools(adapter);
456 return -1;
457 }
458
459 tx_pool->free_map = kcalloc(adapter->req_tx_entries_per_subcrq,
460 sizeof(int), GFP_KERNEL);
461 if (!tx_pool->free_map) {
462 release_tx_pools(adapter);
463 return -1;
464 }
465
466 for (j = 0; j < adapter->req_tx_entries_per_subcrq; j++)
467 tx_pool->free_map[j] = j;
468
469 tx_pool->consumer_index = 0;
470 tx_pool->producer_index = 0;
471 }
472
473 return 0;
474}
475
Nathan Fontenotf0b8c962017-03-30 02:49:00 -0400476static void release_bounce_buffer(struct ibmvnic_adapter *adapter)
477{
478 struct device *dev = &adapter->vdev->dev;
479
480 if (!adapter->bounce_buffer)
481 return;
482
483 if (!dma_mapping_error(dev, adapter->bounce_buffer_dma)) {
484 dma_unmap_single(dev, adapter->bounce_buffer_dma,
485 adapter->bounce_buffer_size,
486 DMA_BIDIRECTIONAL);
487 adapter->bounce_buffer_dma = DMA_ERROR_CODE;
488 }
489
490 kfree(adapter->bounce_buffer);
491 adapter->bounce_buffer = NULL;
492}
493
494static int init_bounce_buffer(struct net_device *netdev)
495{
496 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
497 struct device *dev = &adapter->vdev->dev;
498 char *buf;
499 int buf_sz;
500 dma_addr_t map_addr;
501
502 buf_sz = (netdev->mtu + ETH_HLEN - 1) / PAGE_SIZE + 1;
503 buf = kmalloc(adapter->bounce_buffer_size, GFP_KERNEL);
504 if (!buf)
505 return -1;
506
507 map_addr = dma_map_single(dev, buf, buf_sz, DMA_TO_DEVICE);
508 if (dma_mapping_error(dev, map_addr)) {
509 dev_err(dev, "Couldn't map bounce buffer\n");
510 kfree(buf);
511 return -1;
512 }
513
514 adapter->bounce_buffer = buf;
515 adapter->bounce_buffer_size = buf_sz;
516 adapter->bounce_buffer_dma = map_addr;
517 return 0;
518}
519
John Allena57a5d22017-03-17 17:13:41 -0500520static int ibmvnic_login(struct net_device *netdev)
Thomas Falcon032c5e82015-12-21 11:26:06 -0600521{
522 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
John Allenbd0b6722017-03-17 17:13:40 -0500523 unsigned long timeout = msecs_to_jiffies(30000);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600524 struct device *dev = &adapter->vdev->dev;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600525
John Allenbd0b6722017-03-17 17:13:40 -0500526 do {
527 if (adapter->renegotiate) {
528 adapter->renegotiate = false;
529 release_sub_crqs_no_irqs(adapter);
530
531 reinit_completion(&adapter->init_done);
532 send_cap_queries(adapter);
533 if (!wait_for_completion_timeout(&adapter->init_done,
534 timeout)) {
535 dev_err(dev, "Capabilities query timeout\n");
536 return -1;
537 }
538 }
539
540 reinit_completion(&adapter->init_done);
541 send_login(adapter);
542 if (!wait_for_completion_timeout(&adapter->init_done,
543 timeout)) {
544 dev_err(dev, "Login timeout\n");
545 return -1;
546 }
547 } while (adapter->renegotiate);
548
John Allena57a5d22017-03-17 17:13:41 -0500549 return 0;
550}
551
552static int ibmvnic_open(struct net_device *netdev)
553{
554 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
555 struct device *dev = &adapter->vdev->dev;
John Allena57a5d22017-03-17 17:13:41 -0500556 union ibmvnic_crq crq;
557 int rxadd_subcrqs;
John Allena57a5d22017-03-17 17:13:41 -0500558 int tx_subcrqs;
559 int rc = 0;
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400560 int i;
John Allena57a5d22017-03-17 17:13:41 -0500561
John Allenea5509f2017-03-17 17:13:43 -0500562 if (adapter->is_closed) {
563 rc = ibmvnic_init(adapter);
564 if (rc)
565 return rc;
566 }
567
John Allena57a5d22017-03-17 17:13:41 -0500568 rc = ibmvnic_login(netdev);
569 if (rc)
570 return rc;
571
John Allenbd0b6722017-03-17 17:13:40 -0500572 rc = netif_set_real_num_tx_queues(netdev, adapter->req_tx_queues);
573 if (rc) {
574 dev_err(dev, "failed to set the number of tx queues\n");
575 return -1;
576 }
577
578 rc = init_sub_crq_irqs(adapter);
579 if (rc) {
580 dev_err(dev, "failed to initialize sub crq irqs\n");
581 return -1;
582 }
583
Thomas Falcon032c5e82015-12-21 11:26:06 -0600584 rxadd_subcrqs =
585 be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
586 tx_subcrqs =
587 be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400588
Thomas Falcon032c5e82015-12-21 11:26:06 -0600589 adapter->map_id = 1;
590 adapter->napi = kcalloc(adapter->req_rx_queues,
591 sizeof(struct napi_struct), GFP_KERNEL);
592 if (!adapter->napi)
593 goto alloc_napi_failed;
594 for (i = 0; i < adapter->req_rx_queues; i++) {
595 netif_napi_add(netdev, &adapter->napi[i], ibmvnic_poll,
596 NAPI_POLL_WEIGHT);
597 napi_enable(&adapter->napi[i]);
598 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600599
Thomas Falcon032c5e82015-12-21 11:26:06 -0600600 send_map_query(adapter);
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400601
602 rc = init_rx_pools(netdev);
603 if (rc)
604 goto rx_pool_failed;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600605
Nathan Fontenotc657e322017-03-30 02:49:06 -0400606 rc = init_tx_pools(netdev);
607 if (rc)
608 goto tx_pool_failed;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600609
Nathan Fontenotf0b8c962017-03-30 02:49:00 -0400610 rc = init_bounce_buffer(netdev);
611 if (rc)
612 goto bounce_init_failed;
613
Thomas Falcon032c5e82015-12-21 11:26:06 -0600614 replenish_pools(adapter);
615
616 /* We're ready to receive frames, enable the sub-crq interrupts and
617 * set the logical link state to up
618 */
619 for (i = 0; i < adapter->req_rx_queues; i++)
620 enable_scrq_irq(adapter, adapter->rx_scrq[i]);
621
622 for (i = 0; i < adapter->req_tx_queues; i++)
623 enable_scrq_irq(adapter, adapter->tx_scrq[i]);
624
625 memset(&crq, 0, sizeof(crq));
626 crq.logical_link_state.first = IBMVNIC_CRQ_CMD;
627 crq.logical_link_state.cmd = LOGICAL_LINK_STATE;
628 crq.logical_link_state.link_state = IBMVNIC_LOGICAL_LNK_UP;
629 ibmvnic_send_crq(adapter, &crq);
630
Thomas Falconb8efb892016-07-06 15:35:15 -0500631 netif_tx_start_all_queues(netdev);
John Allenea5509f2017-03-17 17:13:43 -0500632 adapter->is_closed = false;
Thomas Falconb8efb892016-07-06 15:35:15 -0500633
Thomas Falcon032c5e82015-12-21 11:26:06 -0600634 return 0;
635
Nathan Fontenotf0b8c962017-03-30 02:49:00 -0400636bounce_init_failed:
Thomas Falcon032c5e82015-12-21 11:26:06 -0600637 i = tx_subcrqs - 1;
638 kfree(adapter->tx_pool[i].free_map);
Nathan Fontenotc657e322017-03-30 02:49:06 -0400639tx_pool_failed:
Thomas Falcon032c5e82015-12-21 11:26:06 -0600640 i = rxadd_subcrqs;
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400641rx_pool_failed:
Thomas Falcon032c5e82015-12-21 11:26:06 -0600642 for (i = 0; i < adapter->req_rx_queues; i++)
Nathan Fontenote722af62017-02-10 13:29:06 -0500643 napi_disable(&adapter->napi[i]);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600644alloc_napi_failed:
John Allenbd0b6722017-03-17 17:13:40 -0500645 release_sub_crqs(adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600646 return -ENOMEM;
647}
648
John Allenea5509f2017-03-17 17:13:43 -0500649static void ibmvnic_release_resources(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -0600650{
Thomas Falcon032c5e82015-12-21 11:26:06 -0600651 struct device *dev = &adapter->vdev->dev;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600652
Nathan Fontenotf0b8c962017-03-30 02:49:00 -0400653 release_bounce_buffer(adapter);
Nathan Fontenotc657e322017-03-30 02:49:06 -0400654 release_tx_pools(adapter);
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400655 release_rx_pools(adapter);
John Allenea5509f2017-03-17 17:13:43 -0500656
657 release_sub_crqs(adapter);
Nathan Fontenotf9928872017-03-30 02:48:54 -0400658 release_crq_queue(adapter);
John Allenea5509f2017-03-17 17:13:43 -0500659
John Allenea5509f2017-03-17 17:13:43 -0500660 if (adapter->stats_token)
661 dma_unmap_single(dev, adapter->stats_token,
662 sizeof(struct ibmvnic_statistics),
663 DMA_FROM_DEVICE);
John Allenea5509f2017-03-17 17:13:43 -0500664}
665
666static int ibmvnic_close(struct net_device *netdev)
667{
668 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
669 union ibmvnic_crq crq;
670 int i;
671
672 adapter->closing = true;
673
674 for (i = 0; i < adapter->req_rx_queues; i++)
675 napi_disable(&adapter->napi[i]);
676
677 if (!adapter->failover)
678 netif_tx_stop_all_queues(netdev);
679
Thomas Falcon032c5e82015-12-21 11:26:06 -0600680 memset(&crq, 0, sizeof(crq));
681 crq.logical_link_state.first = IBMVNIC_CRQ_CMD;
682 crq.logical_link_state.cmd = LOGICAL_LINK_STATE;
683 crq.logical_link_state.link_state = IBMVNIC_LOGICAL_LNK_DN;
684 ibmvnic_send_crq(adapter, &crq);
685
John Allenea5509f2017-03-17 17:13:43 -0500686 ibmvnic_release_resources(adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600687
John Allenea5509f2017-03-17 17:13:43 -0500688 adapter->is_closed = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600689 adapter->closing = false;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600690 return 0;
691}
692
Thomas Falconad7775d2016-04-01 17:20:34 -0500693/**
694 * build_hdr_data - creates L2/L3/L4 header data buffer
695 * @hdr_field - bitfield determining needed headers
696 * @skb - socket buffer
697 * @hdr_len - array of header lengths
698 * @tot_len - total length of data
699 *
700 * Reads hdr_field to determine which headers are needed by firmware.
701 * Builds a buffer containing these headers. Saves individual header
702 * lengths and total buffer length to be used to build descriptors.
703 */
704static int build_hdr_data(u8 hdr_field, struct sk_buff *skb,
705 int *hdr_len, u8 *hdr_data)
706{
707 int len = 0;
708 u8 *hdr;
709
710 hdr_len[0] = sizeof(struct ethhdr);
711
712 if (skb->protocol == htons(ETH_P_IP)) {
713 hdr_len[1] = ip_hdr(skb)->ihl * 4;
714 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
715 hdr_len[2] = tcp_hdrlen(skb);
716 else if (ip_hdr(skb)->protocol == IPPROTO_UDP)
717 hdr_len[2] = sizeof(struct udphdr);
718 } else if (skb->protocol == htons(ETH_P_IPV6)) {
719 hdr_len[1] = sizeof(struct ipv6hdr);
720 if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
721 hdr_len[2] = tcp_hdrlen(skb);
722 else if (ipv6_hdr(skb)->nexthdr == IPPROTO_UDP)
723 hdr_len[2] = sizeof(struct udphdr);
724 }
725
726 memset(hdr_data, 0, 120);
727 if ((hdr_field >> 6) & 1) {
728 hdr = skb_mac_header(skb);
729 memcpy(hdr_data, hdr, hdr_len[0]);
730 len += hdr_len[0];
731 }
732
733 if ((hdr_field >> 5) & 1) {
734 hdr = skb_network_header(skb);
735 memcpy(hdr_data + len, hdr, hdr_len[1]);
736 len += hdr_len[1];
737 }
738
739 if ((hdr_field >> 4) & 1) {
740 hdr = skb_transport_header(skb);
741 memcpy(hdr_data + len, hdr, hdr_len[2]);
742 len += hdr_len[2];
743 }
744 return len;
745}
746
747/**
748 * create_hdr_descs - create header and header extension descriptors
749 * @hdr_field - bitfield determining needed headers
750 * @data - buffer containing header data
751 * @len - length of data buffer
752 * @hdr_len - array of individual header lengths
753 * @scrq_arr - descriptor array
754 *
755 * Creates header and, if needed, header extension descriptors and
756 * places them in a descriptor array, scrq_arr
757 */
758
759static void create_hdr_descs(u8 hdr_field, u8 *hdr_data, int len, int *hdr_len,
760 union sub_crq *scrq_arr)
761{
762 union sub_crq hdr_desc;
763 int tmp_len = len;
764 u8 *data, *cur;
765 int tmp;
766
767 while (tmp_len > 0) {
768 cur = hdr_data + len - tmp_len;
769
770 memset(&hdr_desc, 0, sizeof(hdr_desc));
771 if (cur != hdr_data) {
772 data = hdr_desc.hdr_ext.data;
773 tmp = tmp_len > 29 ? 29 : tmp_len;
774 hdr_desc.hdr_ext.first = IBMVNIC_CRQ_CMD;
775 hdr_desc.hdr_ext.type = IBMVNIC_HDR_EXT_DESC;
776 hdr_desc.hdr_ext.len = tmp;
777 } else {
778 data = hdr_desc.hdr.data;
779 tmp = tmp_len > 24 ? 24 : tmp_len;
780 hdr_desc.hdr.first = IBMVNIC_CRQ_CMD;
781 hdr_desc.hdr.type = IBMVNIC_HDR_DESC;
782 hdr_desc.hdr.len = tmp;
783 hdr_desc.hdr.l2_len = (u8)hdr_len[0];
784 hdr_desc.hdr.l3_len = cpu_to_be16((u16)hdr_len[1]);
785 hdr_desc.hdr.l4_len = (u8)hdr_len[2];
786 hdr_desc.hdr.flag = hdr_field << 1;
787 }
788 memcpy(data, cur, tmp);
789 tmp_len -= tmp;
790 *scrq_arr = hdr_desc;
791 scrq_arr++;
792 }
793}
794
795/**
796 * build_hdr_descs_arr - build a header descriptor array
797 * @skb - socket buffer
798 * @num_entries - number of descriptors to be sent
799 * @subcrq - first TX descriptor
800 * @hdr_field - bit field determining which headers will be sent
801 *
802 * This function will build a TX descriptor array with applicable
803 * L2/L3/L4 packet header descriptors to be sent by send_subcrq_indirect.
804 */
805
806static void build_hdr_descs_arr(struct ibmvnic_tx_buff *txbuff,
807 int *num_entries, u8 hdr_field)
808{
809 int hdr_len[3] = {0, 0, 0};
810 int tot_len, len;
811 u8 *hdr_data = txbuff->hdr_data;
812
813 tot_len = build_hdr_data(hdr_field, txbuff->skb, hdr_len,
814 txbuff->hdr_data);
815 len = tot_len;
816 len -= 24;
817 if (len > 0)
818 num_entries += len % 29 ? len / 29 + 1 : len / 29;
819 create_hdr_descs(hdr_field, hdr_data, tot_len, hdr_len,
820 txbuff->indir_arr + 1);
821}
822
Thomas Falcon032c5e82015-12-21 11:26:06 -0600823static int ibmvnic_xmit(struct sk_buff *skb, struct net_device *netdev)
824{
825 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
826 int queue_num = skb_get_queue_mapping(skb);
Thomas Falconad7775d2016-04-01 17:20:34 -0500827 u8 *hdrs = (u8 *)&adapter->tx_rx_desc_req;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600828 struct device *dev = &adapter->vdev->dev;
829 struct ibmvnic_tx_buff *tx_buff = NULL;
Thomas Falcon142c0ac2017-03-05 12:18:41 -0600830 struct ibmvnic_sub_crq_queue *tx_scrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600831 struct ibmvnic_tx_pool *tx_pool;
832 unsigned int tx_send_failed = 0;
833 unsigned int tx_map_failed = 0;
834 unsigned int tx_dropped = 0;
835 unsigned int tx_packets = 0;
836 unsigned int tx_bytes = 0;
837 dma_addr_t data_dma_addr;
838 struct netdev_queue *txq;
839 bool used_bounce = false;
840 unsigned long lpar_rc;
841 union sub_crq tx_crq;
842 unsigned int offset;
Thomas Falconad7775d2016-04-01 17:20:34 -0500843 int num_entries = 1;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600844 unsigned char *dst;
845 u64 *handle_array;
846 int index = 0;
847 int ret = 0;
848
849 tx_pool = &adapter->tx_pool[queue_num];
Thomas Falcon142c0ac2017-03-05 12:18:41 -0600850 tx_scrq = adapter->tx_scrq[queue_num];
Thomas Falcon032c5e82015-12-21 11:26:06 -0600851 txq = netdev_get_tx_queue(netdev, skb_get_queue_mapping(skb));
852 handle_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
853 be32_to_cpu(adapter->login_rsp_buf->
854 off_txsubm_subcrqs));
855 if (adapter->migrated) {
856 tx_send_failed++;
857 tx_dropped++;
858 ret = NETDEV_TX_BUSY;
859 goto out;
860 }
861
862 index = tx_pool->free_map[tx_pool->consumer_index];
863 offset = index * adapter->req_mtu;
864 dst = tx_pool->long_term_buff.buff + offset;
865 memset(dst, 0, adapter->req_mtu);
866 skb_copy_from_linear_data(skb, dst, skb->len);
867 data_dma_addr = tx_pool->long_term_buff.addr + offset;
868
869 tx_pool->consumer_index =
870 (tx_pool->consumer_index + 1) %
Thomas Falcon068d9f92017-03-05 12:18:42 -0600871 adapter->req_tx_entries_per_subcrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600872
873 tx_buff = &tx_pool->tx_buff[index];
874 tx_buff->skb = skb;
875 tx_buff->data_dma[0] = data_dma_addr;
876 tx_buff->data_len[0] = skb->len;
877 tx_buff->index = index;
878 tx_buff->pool_index = queue_num;
879 tx_buff->last_frag = true;
880 tx_buff->used_bounce = used_bounce;
881
882 memset(&tx_crq, 0, sizeof(tx_crq));
883 tx_crq.v1.first = IBMVNIC_CRQ_CMD;
884 tx_crq.v1.type = IBMVNIC_TX_DESC;
885 tx_crq.v1.n_crq_elem = 1;
886 tx_crq.v1.n_sge = 1;
887 tx_crq.v1.flags1 = IBMVNIC_TX_COMP_NEEDED;
888 tx_crq.v1.correlator = cpu_to_be32(index);
889 tx_crq.v1.dma_reg = cpu_to_be16(tx_pool->long_term_buff.map_id);
890 tx_crq.v1.sge_len = cpu_to_be32(skb->len);
891 tx_crq.v1.ioba = cpu_to_be64(data_dma_addr);
892
893 if (adapter->vlan_header_insertion) {
894 tx_crq.v1.flags2 |= IBMVNIC_TX_VLAN_INSERT;
895 tx_crq.v1.vlan_id = cpu_to_be16(skb->vlan_tci);
896 }
897
898 if (skb->protocol == htons(ETH_P_IP)) {
899 if (ip_hdr(skb)->version == 4)
900 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV4;
901 else if (ip_hdr(skb)->version == 6)
902 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV6;
903
904 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
905 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_TCP;
906 else if (ip_hdr(skb)->protocol != IPPROTO_TCP)
907 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_UDP;
908 }
909
Thomas Falconad7775d2016-04-01 17:20:34 -0500910 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Thomas Falcon032c5e82015-12-21 11:26:06 -0600911 tx_crq.v1.flags1 |= IBMVNIC_TX_CHKSUM_OFFLOAD;
Thomas Falconad7775d2016-04-01 17:20:34 -0500912 hdrs += 2;
913 }
914 /* determine if l2/3/4 headers are sent to firmware */
915 if ((*hdrs >> 7) & 1 &&
916 (skb->protocol == htons(ETH_P_IP) ||
917 skb->protocol == htons(ETH_P_IPV6))) {
918 build_hdr_descs_arr(tx_buff, &num_entries, *hdrs);
919 tx_crq.v1.n_crq_elem = num_entries;
920 tx_buff->indir_arr[0] = tx_crq;
921 tx_buff->indir_dma = dma_map_single(dev, tx_buff->indir_arr,
922 sizeof(tx_buff->indir_arr),
923 DMA_TO_DEVICE);
924 if (dma_mapping_error(dev, tx_buff->indir_dma)) {
925 if (!firmware_has_feature(FW_FEATURE_CMO))
926 dev_err(dev, "tx: unable to map descriptor array\n");
927 tx_map_failed++;
928 tx_dropped++;
929 ret = NETDEV_TX_BUSY;
930 goto out;
931 }
John Allen498cd8e2016-04-06 11:49:55 -0500932 lpar_rc = send_subcrq_indirect(adapter, handle_array[queue_num],
Thomas Falconad7775d2016-04-01 17:20:34 -0500933 (u64)tx_buff->indir_dma,
934 (u64)num_entries);
935 } else {
John Allen498cd8e2016-04-06 11:49:55 -0500936 lpar_rc = send_subcrq(adapter, handle_array[queue_num],
937 &tx_crq);
Thomas Falconad7775d2016-04-01 17:20:34 -0500938 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600939 if (lpar_rc != H_SUCCESS) {
940 dev_err(dev, "tx failed with code %ld\n", lpar_rc);
941
942 if (tx_pool->consumer_index == 0)
943 tx_pool->consumer_index =
Thomas Falcon068d9f92017-03-05 12:18:42 -0600944 adapter->req_tx_entries_per_subcrq - 1;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600945 else
946 tx_pool->consumer_index--;
947
948 tx_send_failed++;
949 tx_dropped++;
950 ret = NETDEV_TX_BUSY;
951 goto out;
952 }
Thomas Falcon142c0ac2017-03-05 12:18:41 -0600953
954 atomic_inc(&tx_scrq->used);
955
956 if (atomic_read(&tx_scrq->used) >= adapter->req_tx_entries_per_subcrq) {
957 netdev_info(netdev, "Stopping queue %d\n", queue_num);
958 netif_stop_subqueue(netdev, queue_num);
959 }
960
Thomas Falcon032c5e82015-12-21 11:26:06 -0600961 tx_packets++;
962 tx_bytes += skb->len;
963 txq->trans_start = jiffies;
964 ret = NETDEV_TX_OK;
965
966out:
967 netdev->stats.tx_dropped += tx_dropped;
968 netdev->stats.tx_bytes += tx_bytes;
969 netdev->stats.tx_packets += tx_packets;
970 adapter->tx_send_failed += tx_send_failed;
971 adapter->tx_map_failed += tx_map_failed;
972
973 return ret;
974}
975
976static void ibmvnic_set_multi(struct net_device *netdev)
977{
978 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
979 struct netdev_hw_addr *ha;
980 union ibmvnic_crq crq;
981
982 memset(&crq, 0, sizeof(crq));
983 crq.request_capability.first = IBMVNIC_CRQ_CMD;
984 crq.request_capability.cmd = REQUEST_CAPABILITY;
985
986 if (netdev->flags & IFF_PROMISC) {
987 if (!adapter->promisc_supported)
988 return;
989 } else {
990 if (netdev->flags & IFF_ALLMULTI) {
991 /* Accept all multicast */
992 memset(&crq, 0, sizeof(crq));
993 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
994 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
995 crq.multicast_ctrl.flags = IBMVNIC_ENABLE_ALL;
996 ibmvnic_send_crq(adapter, &crq);
997 } else if (netdev_mc_empty(netdev)) {
998 /* Reject all multicast */
999 memset(&crq, 0, sizeof(crq));
1000 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1001 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1002 crq.multicast_ctrl.flags = IBMVNIC_DISABLE_ALL;
1003 ibmvnic_send_crq(adapter, &crq);
1004 } else {
1005 /* Accept one or more multicast(s) */
1006 netdev_for_each_mc_addr(ha, netdev) {
1007 memset(&crq, 0, sizeof(crq));
1008 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1009 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1010 crq.multicast_ctrl.flags = IBMVNIC_ENABLE_MC;
1011 ether_addr_copy(&crq.multicast_ctrl.mac_addr[0],
1012 ha->addr);
1013 ibmvnic_send_crq(adapter, &crq);
1014 }
1015 }
1016 }
1017}
1018
1019static int ibmvnic_set_mac(struct net_device *netdev, void *p)
1020{
1021 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1022 struct sockaddr *addr = p;
1023 union ibmvnic_crq crq;
1024
1025 if (!is_valid_ether_addr(addr->sa_data))
1026 return -EADDRNOTAVAIL;
1027
1028 memset(&crq, 0, sizeof(crq));
1029 crq.change_mac_addr.first = IBMVNIC_CRQ_CMD;
1030 crq.change_mac_addr.cmd = CHANGE_MAC_ADDR;
1031 ether_addr_copy(&crq.change_mac_addr.mac_addr[0], addr->sa_data);
1032 ibmvnic_send_crq(adapter, &crq);
1033 /* netdev->dev_addr is changed in handle_change_mac_rsp function */
1034 return 0;
1035}
1036
Thomas Falcon032c5e82015-12-21 11:26:06 -06001037static void ibmvnic_tx_timeout(struct net_device *dev)
1038{
1039 struct ibmvnic_adapter *adapter = netdev_priv(dev);
1040 int rc;
1041
1042 /* Adapter timed out, resetting it */
1043 release_sub_crqs(adapter);
1044 rc = ibmvnic_reset_crq(adapter);
1045 if (rc)
1046 dev_err(&adapter->vdev->dev, "Adapter timeout, reset failed\n");
1047 else
1048 ibmvnic_send_crq_init(adapter);
1049}
1050
1051static void remove_buff_from_pool(struct ibmvnic_adapter *adapter,
1052 struct ibmvnic_rx_buff *rx_buff)
1053{
1054 struct ibmvnic_rx_pool *pool = &adapter->rx_pool[rx_buff->pool_index];
1055
1056 rx_buff->skb = NULL;
1057
1058 pool->free_map[pool->next_alloc] = (int)(rx_buff - pool->rx_buff);
1059 pool->next_alloc = (pool->next_alloc + 1) % pool->size;
1060
1061 atomic_dec(&pool->available);
1062}
1063
1064static int ibmvnic_poll(struct napi_struct *napi, int budget)
1065{
1066 struct net_device *netdev = napi->dev;
1067 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1068 int scrq_num = (int)(napi - adapter->napi);
1069 int frames_processed = 0;
1070restart_poll:
1071 while (frames_processed < budget) {
1072 struct sk_buff *skb;
1073 struct ibmvnic_rx_buff *rx_buff;
1074 union sub_crq *next;
1075 u32 length;
1076 u16 offset;
1077 u8 flags = 0;
1078
1079 if (!pending_scrq(adapter, adapter->rx_scrq[scrq_num]))
1080 break;
1081 next = ibmvnic_next_scrq(adapter, adapter->rx_scrq[scrq_num]);
1082 rx_buff =
1083 (struct ibmvnic_rx_buff *)be64_to_cpu(next->
1084 rx_comp.correlator);
1085 /* do error checking */
1086 if (next->rx_comp.rc) {
1087 netdev_err(netdev, "rx error %x\n", next->rx_comp.rc);
1088 /* free the entry */
1089 next->rx_comp.first = 0;
1090 remove_buff_from_pool(adapter, rx_buff);
1091 break;
1092 }
1093
1094 length = be32_to_cpu(next->rx_comp.len);
1095 offset = be16_to_cpu(next->rx_comp.off_frame_data);
1096 flags = next->rx_comp.flags;
1097 skb = rx_buff->skb;
1098 skb_copy_to_linear_data(skb, rx_buff->data + offset,
1099 length);
1100 skb->vlan_tci = be16_to_cpu(next->rx_comp.vlan_tci);
1101 /* free the entry */
1102 next->rx_comp.first = 0;
1103 remove_buff_from_pool(adapter, rx_buff);
1104
1105 skb_put(skb, length);
1106 skb->protocol = eth_type_trans(skb, netdev);
1107
1108 if (flags & IBMVNIC_IP_CHKSUM_GOOD &&
1109 flags & IBMVNIC_TCP_UDP_CHKSUM_GOOD) {
1110 skb->ip_summed = CHECKSUM_UNNECESSARY;
1111 }
1112
1113 length = skb->len;
1114 napi_gro_receive(napi, skb); /* send it up */
1115 netdev->stats.rx_packets++;
1116 netdev->stats.rx_bytes += length;
1117 frames_processed++;
1118 }
John Allen498cd8e2016-04-06 11:49:55 -05001119 replenish_rx_pool(adapter, &adapter->rx_pool[scrq_num]);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001120
1121 if (frames_processed < budget) {
1122 enable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
Eric Dumazet6ad20162017-01-30 08:22:01 -08001123 napi_complete_done(napi, frames_processed);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001124 if (pending_scrq(adapter, adapter->rx_scrq[scrq_num]) &&
1125 napi_reschedule(napi)) {
1126 disable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
1127 goto restart_poll;
1128 }
1129 }
1130 return frames_processed;
1131}
1132
1133#ifdef CONFIG_NET_POLL_CONTROLLER
1134static void ibmvnic_netpoll_controller(struct net_device *dev)
1135{
1136 struct ibmvnic_adapter *adapter = netdev_priv(dev);
1137 int i;
1138
1139 replenish_pools(netdev_priv(dev));
1140 for (i = 0; i < adapter->req_rx_queues; i++)
1141 ibmvnic_interrupt_rx(adapter->rx_scrq[i]->irq,
1142 adapter->rx_scrq[i]);
1143}
1144#endif
1145
1146static const struct net_device_ops ibmvnic_netdev_ops = {
1147 .ndo_open = ibmvnic_open,
1148 .ndo_stop = ibmvnic_close,
1149 .ndo_start_xmit = ibmvnic_xmit,
1150 .ndo_set_rx_mode = ibmvnic_set_multi,
1151 .ndo_set_mac_address = ibmvnic_set_mac,
1152 .ndo_validate_addr = eth_validate_addr,
Thomas Falcon032c5e82015-12-21 11:26:06 -06001153 .ndo_tx_timeout = ibmvnic_tx_timeout,
1154#ifdef CONFIG_NET_POLL_CONTROLLER
1155 .ndo_poll_controller = ibmvnic_netpoll_controller,
1156#endif
1157};
1158
1159/* ethtool functions */
1160
Philippe Reynes8a433792017-01-07 22:37:29 +01001161static int ibmvnic_get_link_ksettings(struct net_device *netdev,
1162 struct ethtool_link_ksettings *cmd)
Thomas Falcon032c5e82015-12-21 11:26:06 -06001163{
Philippe Reynes8a433792017-01-07 22:37:29 +01001164 u32 supported, advertising;
1165
1166 supported = (SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg |
Thomas Falcon032c5e82015-12-21 11:26:06 -06001167 SUPPORTED_FIBRE);
Philippe Reynes8a433792017-01-07 22:37:29 +01001168 advertising = (ADVERTISED_1000baseT_Full | ADVERTISED_Autoneg |
Thomas Falcon032c5e82015-12-21 11:26:06 -06001169 ADVERTISED_FIBRE);
Philippe Reynes8a433792017-01-07 22:37:29 +01001170 cmd->base.speed = SPEED_1000;
1171 cmd->base.duplex = DUPLEX_FULL;
1172 cmd->base.port = PORT_FIBRE;
1173 cmd->base.phy_address = 0;
1174 cmd->base.autoneg = AUTONEG_ENABLE;
1175
1176 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
1177 supported);
1178 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
1179 advertising);
1180
Thomas Falcon032c5e82015-12-21 11:26:06 -06001181 return 0;
1182}
1183
1184static void ibmvnic_get_drvinfo(struct net_device *dev,
1185 struct ethtool_drvinfo *info)
1186{
1187 strlcpy(info->driver, ibmvnic_driver_name, sizeof(info->driver));
1188 strlcpy(info->version, IBMVNIC_DRIVER_VERSION, sizeof(info->version));
1189}
1190
1191static u32 ibmvnic_get_msglevel(struct net_device *netdev)
1192{
1193 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1194
1195 return adapter->msg_enable;
1196}
1197
1198static void ibmvnic_set_msglevel(struct net_device *netdev, u32 data)
1199{
1200 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1201
1202 adapter->msg_enable = data;
1203}
1204
1205static u32 ibmvnic_get_link(struct net_device *netdev)
1206{
1207 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1208
1209 /* Don't need to send a query because we request a logical link up at
1210 * init and then we wait for link state indications
1211 */
1212 return adapter->logical_link_state;
1213}
1214
1215static void ibmvnic_get_ringparam(struct net_device *netdev,
1216 struct ethtool_ringparam *ring)
1217{
1218 ring->rx_max_pending = 0;
1219 ring->tx_max_pending = 0;
1220 ring->rx_mini_max_pending = 0;
1221 ring->rx_jumbo_max_pending = 0;
1222 ring->rx_pending = 0;
1223 ring->tx_pending = 0;
1224 ring->rx_mini_pending = 0;
1225 ring->rx_jumbo_pending = 0;
1226}
1227
1228static void ibmvnic_get_strings(struct net_device *dev, u32 stringset, u8 *data)
1229{
1230 int i;
1231
1232 if (stringset != ETH_SS_STATS)
1233 return;
1234
1235 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++, data += ETH_GSTRING_LEN)
1236 memcpy(data, ibmvnic_stats[i].name, ETH_GSTRING_LEN);
1237}
1238
1239static int ibmvnic_get_sset_count(struct net_device *dev, int sset)
1240{
1241 switch (sset) {
1242 case ETH_SS_STATS:
1243 return ARRAY_SIZE(ibmvnic_stats);
1244 default:
1245 return -EOPNOTSUPP;
1246 }
1247}
1248
1249static void ibmvnic_get_ethtool_stats(struct net_device *dev,
1250 struct ethtool_stats *stats, u64 *data)
1251{
1252 struct ibmvnic_adapter *adapter = netdev_priv(dev);
1253 union ibmvnic_crq crq;
1254 int i;
1255
1256 memset(&crq, 0, sizeof(crq));
1257 crq.request_statistics.first = IBMVNIC_CRQ_CMD;
1258 crq.request_statistics.cmd = REQUEST_STATISTICS;
1259 crq.request_statistics.ioba = cpu_to_be32(adapter->stats_token);
1260 crq.request_statistics.len =
1261 cpu_to_be32(sizeof(struct ibmvnic_statistics));
Thomas Falcon032c5e82015-12-21 11:26:06 -06001262
1263 /* Wait for data to be written */
1264 init_completion(&adapter->stats_done);
Nathan Fontenotdb5d0b52017-02-10 13:45:05 -05001265 ibmvnic_send_crq(adapter, &crq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001266 wait_for_completion(&adapter->stats_done);
1267
1268 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++)
1269 data[i] = IBMVNIC_GET_STAT(adapter, ibmvnic_stats[i].offset);
1270}
1271
1272static const struct ethtool_ops ibmvnic_ethtool_ops = {
Thomas Falcon032c5e82015-12-21 11:26:06 -06001273 .get_drvinfo = ibmvnic_get_drvinfo,
1274 .get_msglevel = ibmvnic_get_msglevel,
1275 .set_msglevel = ibmvnic_set_msglevel,
1276 .get_link = ibmvnic_get_link,
1277 .get_ringparam = ibmvnic_get_ringparam,
1278 .get_strings = ibmvnic_get_strings,
1279 .get_sset_count = ibmvnic_get_sset_count,
1280 .get_ethtool_stats = ibmvnic_get_ethtool_stats,
Philippe Reynes8a433792017-01-07 22:37:29 +01001281 .get_link_ksettings = ibmvnic_get_link_ksettings,
Thomas Falcon032c5e82015-12-21 11:26:06 -06001282};
1283
1284/* Routines for managing CRQs/sCRQs */
1285
1286static void release_sub_crq_queue(struct ibmvnic_adapter *adapter,
1287 struct ibmvnic_sub_crq_queue *scrq)
1288{
1289 struct device *dev = &adapter->vdev->dev;
1290 long rc;
1291
1292 netdev_dbg(adapter->netdev, "Releasing sub-CRQ\n");
1293
1294 /* Close the sub-crqs */
1295 do {
1296 rc = plpar_hcall_norets(H_FREE_SUB_CRQ,
1297 adapter->vdev->unit_address,
1298 scrq->crq_num);
1299 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
1300
1301 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
1302 DMA_BIDIRECTIONAL);
1303 free_pages((unsigned long)scrq->msgs, 2);
1304 kfree(scrq);
1305}
1306
1307static struct ibmvnic_sub_crq_queue *init_sub_crq_queue(struct ibmvnic_adapter
1308 *adapter)
1309{
1310 struct device *dev = &adapter->vdev->dev;
1311 struct ibmvnic_sub_crq_queue *scrq;
1312 int rc;
1313
1314 scrq = kmalloc(sizeof(*scrq), GFP_ATOMIC);
1315 if (!scrq)
1316 return NULL;
1317
Thomas Falcon12608c22016-10-17 15:28:09 -05001318 scrq->msgs = (union sub_crq *)__get_free_pages(GFP_ATOMIC, 2);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001319 memset(scrq->msgs, 0, 4 * PAGE_SIZE);
1320 if (!scrq->msgs) {
1321 dev_warn(dev, "Couldn't allocate crq queue messages page\n");
1322 goto zero_page_failed;
1323 }
1324
1325 scrq->msg_token = dma_map_single(dev, scrq->msgs, 4 * PAGE_SIZE,
1326 DMA_BIDIRECTIONAL);
1327 if (dma_mapping_error(dev, scrq->msg_token)) {
1328 dev_warn(dev, "Couldn't map crq queue messages page\n");
1329 goto map_failed;
1330 }
1331
1332 rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
1333 4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
1334
1335 if (rc == H_RESOURCE)
1336 rc = ibmvnic_reset_crq(adapter);
1337
1338 if (rc == H_CLOSED) {
1339 dev_warn(dev, "Partner adapter not ready, waiting.\n");
1340 } else if (rc) {
1341 dev_warn(dev, "Error %d registering sub-crq\n", rc);
1342 goto reg_failed;
1343 }
1344
Thomas Falcon032c5e82015-12-21 11:26:06 -06001345 scrq->adapter = adapter;
1346 scrq->size = 4 * PAGE_SIZE / sizeof(*scrq->msgs);
1347 scrq->cur = 0;
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001348 atomic_set(&scrq->used, 0);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001349 scrq->rx_skb_top = NULL;
1350 spin_lock_init(&scrq->lock);
1351
1352 netdev_dbg(adapter->netdev,
1353 "sub-crq initialized, num %lx, hw_irq=%lx, irq=%x\n",
1354 scrq->crq_num, scrq->hw_irq, scrq->irq);
1355
1356 return scrq;
1357
Thomas Falcon032c5e82015-12-21 11:26:06 -06001358reg_failed:
1359 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
1360 DMA_BIDIRECTIONAL);
1361map_failed:
1362 free_pages((unsigned long)scrq->msgs, 2);
1363zero_page_failed:
1364 kfree(scrq);
1365
1366 return NULL;
1367}
1368
1369static void release_sub_crqs(struct ibmvnic_adapter *adapter)
1370{
1371 int i;
1372
1373 if (adapter->tx_scrq) {
1374 for (i = 0; i < adapter->req_tx_queues; i++)
1375 if (adapter->tx_scrq[i]) {
1376 free_irq(adapter->tx_scrq[i]->irq,
1377 adapter->tx_scrq[i]);
Thomas Falcon88eb98a2016-07-06 15:35:16 -05001378 irq_dispose_mapping(adapter->tx_scrq[i]->irq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001379 release_sub_crq_queue(adapter,
1380 adapter->tx_scrq[i]);
1381 }
Nathan Fontenot9501df32017-03-15 23:38:07 -04001382 kfree(adapter->tx_scrq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001383 adapter->tx_scrq = NULL;
1384 }
1385
1386 if (adapter->rx_scrq) {
1387 for (i = 0; i < adapter->req_rx_queues; i++)
1388 if (adapter->rx_scrq[i]) {
1389 free_irq(adapter->rx_scrq[i]->irq,
1390 adapter->rx_scrq[i]);
Thomas Falcon88eb98a2016-07-06 15:35:16 -05001391 irq_dispose_mapping(adapter->rx_scrq[i]->irq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001392 release_sub_crq_queue(adapter,
1393 adapter->rx_scrq[i]);
1394 }
Nathan Fontenot9501df32017-03-15 23:38:07 -04001395 kfree(adapter->rx_scrq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001396 adapter->rx_scrq = NULL;
1397 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001398}
1399
Thomas Falconea22d512016-07-06 15:35:17 -05001400static void release_sub_crqs_no_irqs(struct ibmvnic_adapter *adapter)
1401{
1402 int i;
1403
1404 if (adapter->tx_scrq) {
1405 for (i = 0; i < adapter->req_tx_queues; i++)
1406 if (adapter->tx_scrq[i])
1407 release_sub_crq_queue(adapter,
1408 adapter->tx_scrq[i]);
1409 adapter->tx_scrq = NULL;
1410 }
1411
1412 if (adapter->rx_scrq) {
1413 for (i = 0; i < adapter->req_rx_queues; i++)
1414 if (adapter->rx_scrq[i])
1415 release_sub_crq_queue(adapter,
1416 adapter->rx_scrq[i]);
1417 adapter->rx_scrq = NULL;
1418 }
Thomas Falconea22d512016-07-06 15:35:17 -05001419}
1420
Thomas Falcon032c5e82015-12-21 11:26:06 -06001421static int disable_scrq_irq(struct ibmvnic_adapter *adapter,
1422 struct ibmvnic_sub_crq_queue *scrq)
1423{
1424 struct device *dev = &adapter->vdev->dev;
1425 unsigned long rc;
1426
1427 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
1428 H_DISABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
1429 if (rc)
1430 dev_err(dev, "Couldn't disable scrq irq 0x%lx. rc=%ld\n",
1431 scrq->hw_irq, rc);
1432 return rc;
1433}
1434
1435static int enable_scrq_irq(struct ibmvnic_adapter *adapter,
1436 struct ibmvnic_sub_crq_queue *scrq)
1437{
1438 struct device *dev = &adapter->vdev->dev;
1439 unsigned long rc;
1440
1441 if (scrq->hw_irq > 0x100000000ULL) {
1442 dev_err(dev, "bad hw_irq = %lx\n", scrq->hw_irq);
1443 return 1;
1444 }
1445
1446 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
1447 H_ENABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
1448 if (rc)
1449 dev_err(dev, "Couldn't enable scrq irq 0x%lx. rc=%ld\n",
1450 scrq->hw_irq, rc);
1451 return rc;
1452}
1453
1454static int ibmvnic_complete_tx(struct ibmvnic_adapter *adapter,
1455 struct ibmvnic_sub_crq_queue *scrq)
1456{
1457 struct device *dev = &adapter->vdev->dev;
1458 struct ibmvnic_tx_buff *txbuff;
1459 union sub_crq *next;
1460 int index;
1461 int i, j;
Thomas Falconad7775d2016-04-01 17:20:34 -05001462 u8 first;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001463
1464restart_loop:
1465 while (pending_scrq(adapter, scrq)) {
1466 unsigned int pool = scrq->pool_index;
1467
1468 next = ibmvnic_next_scrq(adapter, scrq);
1469 for (i = 0; i < next->tx_comp.num_comps; i++) {
1470 if (next->tx_comp.rcs[i]) {
1471 dev_err(dev, "tx error %x\n",
1472 next->tx_comp.rcs[i]);
1473 continue;
1474 }
1475 index = be32_to_cpu(next->tx_comp.correlators[i]);
1476 txbuff = &adapter->tx_pool[pool].tx_buff[index];
1477
1478 for (j = 0; j < IBMVNIC_MAX_FRAGS_PER_CRQ; j++) {
1479 if (!txbuff->data_dma[j])
1480 continue;
1481
1482 txbuff->data_dma[j] = 0;
1483 txbuff->used_bounce = false;
1484 }
Thomas Falconad7775d2016-04-01 17:20:34 -05001485 /* if sub_crq was sent indirectly */
1486 first = txbuff->indir_arr[0].generic.first;
1487 if (first == IBMVNIC_CRQ_CMD) {
1488 dma_unmap_single(dev, txbuff->indir_dma,
1489 sizeof(txbuff->indir_arr),
1490 DMA_TO_DEVICE);
1491 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001492
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001493 if (txbuff->last_frag) {
1494 atomic_dec(&scrq->used);
1495
1496 if (atomic_read(&scrq->used) <=
1497 (adapter->req_tx_entries_per_subcrq / 2) &&
1498 netif_subqueue_stopped(adapter->netdev,
1499 txbuff->skb)) {
1500 netif_wake_subqueue(adapter->netdev,
1501 scrq->pool_index);
1502 netdev_dbg(adapter->netdev,
1503 "Started queue %d\n",
1504 scrq->pool_index);
1505 }
1506
Thomas Falcon032c5e82015-12-21 11:26:06 -06001507 dev_kfree_skb_any(txbuff->skb);
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001508 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001509
1510 adapter->tx_pool[pool].free_map[adapter->tx_pool[pool].
1511 producer_index] = index;
1512 adapter->tx_pool[pool].producer_index =
1513 (adapter->tx_pool[pool].producer_index + 1) %
Thomas Falcon068d9f92017-03-05 12:18:42 -06001514 adapter->req_tx_entries_per_subcrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001515 }
1516 /* remove tx_comp scrq*/
1517 next->tx_comp.first = 0;
1518 }
1519
1520 enable_scrq_irq(adapter, scrq);
1521
1522 if (pending_scrq(adapter, scrq)) {
1523 disable_scrq_irq(adapter, scrq);
1524 goto restart_loop;
1525 }
1526
1527 return 0;
1528}
1529
1530static irqreturn_t ibmvnic_interrupt_tx(int irq, void *instance)
1531{
1532 struct ibmvnic_sub_crq_queue *scrq = instance;
1533 struct ibmvnic_adapter *adapter = scrq->adapter;
1534
1535 disable_scrq_irq(adapter, scrq);
1536 ibmvnic_complete_tx(adapter, scrq);
1537
1538 return IRQ_HANDLED;
1539}
1540
1541static irqreturn_t ibmvnic_interrupt_rx(int irq, void *instance)
1542{
1543 struct ibmvnic_sub_crq_queue *scrq = instance;
1544 struct ibmvnic_adapter *adapter = scrq->adapter;
1545
1546 if (napi_schedule_prep(&adapter->napi[scrq->scrq_num])) {
1547 disable_scrq_irq(adapter, scrq);
1548 __napi_schedule(&adapter->napi[scrq->scrq_num]);
1549 }
1550
1551 return IRQ_HANDLED;
1552}
1553
Thomas Falconea22d512016-07-06 15:35:17 -05001554static int init_sub_crq_irqs(struct ibmvnic_adapter *adapter)
1555{
1556 struct device *dev = &adapter->vdev->dev;
1557 struct ibmvnic_sub_crq_queue *scrq;
1558 int i = 0, j = 0;
1559 int rc = 0;
1560
1561 for (i = 0; i < adapter->req_tx_queues; i++) {
1562 scrq = adapter->tx_scrq[i];
1563 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
1564
Michael Ellerman99c17902016-09-10 19:59:05 +10001565 if (!scrq->irq) {
Thomas Falconea22d512016-07-06 15:35:17 -05001566 rc = -EINVAL;
1567 dev_err(dev, "Error mapping irq\n");
1568 goto req_tx_irq_failed;
1569 }
1570
1571 rc = request_irq(scrq->irq, ibmvnic_interrupt_tx,
1572 0, "ibmvnic_tx", scrq);
1573
1574 if (rc) {
1575 dev_err(dev, "Couldn't register tx irq 0x%x. rc=%d\n",
1576 scrq->irq, rc);
1577 irq_dispose_mapping(scrq->irq);
1578 goto req_rx_irq_failed;
1579 }
1580 }
1581
1582 for (i = 0; i < adapter->req_rx_queues; i++) {
1583 scrq = adapter->rx_scrq[i];
1584 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
Michael Ellerman99c17902016-09-10 19:59:05 +10001585 if (!scrq->irq) {
Thomas Falconea22d512016-07-06 15:35:17 -05001586 rc = -EINVAL;
1587 dev_err(dev, "Error mapping irq\n");
1588 goto req_rx_irq_failed;
1589 }
1590 rc = request_irq(scrq->irq, ibmvnic_interrupt_rx,
1591 0, "ibmvnic_rx", scrq);
1592 if (rc) {
1593 dev_err(dev, "Couldn't register rx irq 0x%x. rc=%d\n",
1594 scrq->irq, rc);
1595 irq_dispose_mapping(scrq->irq);
1596 goto req_rx_irq_failed;
1597 }
1598 }
1599 return rc;
1600
1601req_rx_irq_failed:
Thomas Falcon8bf371e2016-10-27 12:28:52 -05001602 for (j = 0; j < i; j++) {
Thomas Falconea22d512016-07-06 15:35:17 -05001603 free_irq(adapter->rx_scrq[j]->irq, adapter->rx_scrq[j]);
1604 irq_dispose_mapping(adapter->rx_scrq[j]->irq);
Thomas Falcon8bf371e2016-10-27 12:28:52 -05001605 }
Thomas Falconea22d512016-07-06 15:35:17 -05001606 i = adapter->req_tx_queues;
1607req_tx_irq_failed:
Thomas Falcon8bf371e2016-10-27 12:28:52 -05001608 for (j = 0; j < i; j++) {
Thomas Falconea22d512016-07-06 15:35:17 -05001609 free_irq(adapter->tx_scrq[j]->irq, adapter->tx_scrq[j]);
1610 irq_dispose_mapping(adapter->rx_scrq[j]->irq);
Thomas Falcon8bf371e2016-10-27 12:28:52 -05001611 }
Thomas Falconea22d512016-07-06 15:35:17 -05001612 release_sub_crqs_no_irqs(adapter);
1613 return rc;
1614}
1615
Thomas Falcon032c5e82015-12-21 11:26:06 -06001616static void init_sub_crqs(struct ibmvnic_adapter *adapter, int retry)
1617{
1618 struct device *dev = &adapter->vdev->dev;
1619 struct ibmvnic_sub_crq_queue **allqueues;
1620 int registered_queues = 0;
1621 union ibmvnic_crq crq;
1622 int total_queues;
1623 int more = 0;
Thomas Falconea22d512016-07-06 15:35:17 -05001624 int i;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001625
1626 if (!retry) {
1627 /* Sub-CRQ entries are 32 byte long */
1628 int entries_page = 4 * PAGE_SIZE / (sizeof(u64) * 4);
1629
1630 if (adapter->min_tx_entries_per_subcrq > entries_page ||
1631 adapter->min_rx_add_entries_per_subcrq > entries_page) {
1632 dev_err(dev, "Fatal, invalid entries per sub-crq\n");
1633 goto allqueues_failed;
1634 }
1635
1636 /* Get the minimum between the queried max and the entries
1637 * that fit in our PAGE_SIZE
1638 */
1639 adapter->req_tx_entries_per_subcrq =
1640 adapter->max_tx_entries_per_subcrq > entries_page ?
1641 entries_page : adapter->max_tx_entries_per_subcrq;
1642 adapter->req_rx_add_entries_per_subcrq =
1643 adapter->max_rx_add_entries_per_subcrq > entries_page ?
1644 entries_page : adapter->max_rx_add_entries_per_subcrq;
1645
John Allen6dbcd8f2016-11-07 14:27:28 -06001646 adapter->req_tx_queues = adapter->opt_tx_comp_sub_queues;
1647 adapter->req_rx_queues = adapter->opt_rx_comp_queues;
John Allen498cd8e2016-04-06 11:49:55 -05001648 adapter->req_rx_add_queues = adapter->max_rx_add_queues;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001649
Thomas Falconf39f0d12017-02-14 10:22:59 -06001650 adapter->req_mtu = adapter->netdev->mtu + ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001651 }
1652
1653 total_queues = adapter->req_tx_queues + adapter->req_rx_queues;
1654
1655 allqueues = kcalloc(total_queues, sizeof(*allqueues), GFP_ATOMIC);
1656 if (!allqueues)
1657 goto allqueues_failed;
1658
1659 for (i = 0; i < total_queues; i++) {
1660 allqueues[i] = init_sub_crq_queue(adapter);
1661 if (!allqueues[i]) {
1662 dev_warn(dev, "Couldn't allocate all sub-crqs\n");
1663 break;
1664 }
1665 registered_queues++;
1666 }
1667
1668 /* Make sure we were able to register the minimum number of queues */
1669 if (registered_queues <
1670 adapter->min_tx_queues + adapter->min_rx_queues) {
1671 dev_err(dev, "Fatal: Couldn't init min number of sub-crqs\n");
1672 goto tx_failed;
1673 }
1674
1675 /* Distribute the failed allocated queues*/
1676 for (i = 0; i < total_queues - registered_queues + more ; i++) {
1677 netdev_dbg(adapter->netdev, "Reducing number of queues\n");
1678 switch (i % 3) {
1679 case 0:
1680 if (adapter->req_rx_queues > adapter->min_rx_queues)
1681 adapter->req_rx_queues--;
1682 else
1683 more++;
1684 break;
1685 case 1:
1686 if (adapter->req_tx_queues > adapter->min_tx_queues)
1687 adapter->req_tx_queues--;
1688 else
1689 more++;
1690 break;
1691 }
1692 }
1693
1694 adapter->tx_scrq = kcalloc(adapter->req_tx_queues,
1695 sizeof(*adapter->tx_scrq), GFP_ATOMIC);
1696 if (!adapter->tx_scrq)
1697 goto tx_failed;
1698
1699 for (i = 0; i < adapter->req_tx_queues; i++) {
1700 adapter->tx_scrq[i] = allqueues[i];
1701 adapter->tx_scrq[i]->pool_index = i;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001702 }
1703
1704 adapter->rx_scrq = kcalloc(adapter->req_rx_queues,
1705 sizeof(*adapter->rx_scrq), GFP_ATOMIC);
1706 if (!adapter->rx_scrq)
1707 goto rx_failed;
1708
1709 for (i = 0; i < adapter->req_rx_queues; i++) {
1710 adapter->rx_scrq[i] = allqueues[i + adapter->req_tx_queues];
1711 adapter->rx_scrq[i]->scrq_num = i;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001712 }
1713
1714 memset(&crq, 0, sizeof(crq));
1715 crq.request_capability.first = IBMVNIC_CRQ_CMD;
1716 crq.request_capability.cmd = REQUEST_CAPABILITY;
1717
1718 crq.request_capability.capability = cpu_to_be16(REQ_TX_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06001719 crq.request_capability.number = cpu_to_be64(adapter->req_tx_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06001720 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001721 ibmvnic_send_crq(adapter, &crq);
1722
1723 crq.request_capability.capability = cpu_to_be16(REQ_RX_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06001724 crq.request_capability.number = cpu_to_be64(adapter->req_rx_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06001725 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001726 ibmvnic_send_crq(adapter, &crq);
1727
1728 crq.request_capability.capability = cpu_to_be16(REQ_RX_ADD_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06001729 crq.request_capability.number = cpu_to_be64(adapter->req_rx_add_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06001730 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001731 ibmvnic_send_crq(adapter, &crq);
1732
1733 crq.request_capability.capability =
1734 cpu_to_be16(REQ_TX_ENTRIES_PER_SUBCRQ);
1735 crq.request_capability.number =
Thomas Falconde89e852016-03-01 10:20:09 -06001736 cpu_to_be64(adapter->req_tx_entries_per_subcrq);
Thomas Falcon901e0402017-02-15 12:17:59 -06001737 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001738 ibmvnic_send_crq(adapter, &crq);
1739
1740 crq.request_capability.capability =
1741 cpu_to_be16(REQ_RX_ADD_ENTRIES_PER_SUBCRQ);
1742 crq.request_capability.number =
Thomas Falconde89e852016-03-01 10:20:09 -06001743 cpu_to_be64(adapter->req_rx_add_entries_per_subcrq);
Thomas Falcon901e0402017-02-15 12:17:59 -06001744 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001745 ibmvnic_send_crq(adapter, &crq);
1746
1747 crq.request_capability.capability = cpu_to_be16(REQ_MTU);
Thomas Falconde89e852016-03-01 10:20:09 -06001748 crq.request_capability.number = cpu_to_be64(adapter->req_mtu);
Thomas Falcon901e0402017-02-15 12:17:59 -06001749 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001750 ibmvnic_send_crq(adapter, &crq);
1751
1752 if (adapter->netdev->flags & IFF_PROMISC) {
1753 if (adapter->promisc_supported) {
1754 crq.request_capability.capability =
1755 cpu_to_be16(PROMISC_REQUESTED);
Thomas Falconde89e852016-03-01 10:20:09 -06001756 crq.request_capability.number = cpu_to_be64(1);
Thomas Falcon901e0402017-02-15 12:17:59 -06001757 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001758 ibmvnic_send_crq(adapter, &crq);
1759 }
1760 } else {
1761 crq.request_capability.capability =
1762 cpu_to_be16(PROMISC_REQUESTED);
Thomas Falconde89e852016-03-01 10:20:09 -06001763 crq.request_capability.number = cpu_to_be64(0);
Thomas Falcon901e0402017-02-15 12:17:59 -06001764 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001765 ibmvnic_send_crq(adapter, &crq);
1766 }
1767
1768 kfree(allqueues);
1769
1770 return;
1771
Thomas Falcon032c5e82015-12-21 11:26:06 -06001772rx_failed:
1773 kfree(adapter->tx_scrq);
1774 adapter->tx_scrq = NULL;
1775tx_failed:
1776 for (i = 0; i < registered_queues; i++)
1777 release_sub_crq_queue(adapter, allqueues[i]);
1778 kfree(allqueues);
1779allqueues_failed:
1780 ibmvnic_remove(adapter->vdev);
1781}
1782
1783static int pending_scrq(struct ibmvnic_adapter *adapter,
1784 struct ibmvnic_sub_crq_queue *scrq)
1785{
1786 union sub_crq *entry = &scrq->msgs[scrq->cur];
1787
1788 if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP || adapter->closing)
1789 return 1;
1790 else
1791 return 0;
1792}
1793
1794static union sub_crq *ibmvnic_next_scrq(struct ibmvnic_adapter *adapter,
1795 struct ibmvnic_sub_crq_queue *scrq)
1796{
1797 union sub_crq *entry;
1798 unsigned long flags;
1799
1800 spin_lock_irqsave(&scrq->lock, flags);
1801 entry = &scrq->msgs[scrq->cur];
1802 if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP) {
1803 if (++scrq->cur == scrq->size)
1804 scrq->cur = 0;
1805 } else {
1806 entry = NULL;
1807 }
1808 spin_unlock_irqrestore(&scrq->lock, flags);
1809
1810 return entry;
1811}
1812
1813static union ibmvnic_crq *ibmvnic_next_crq(struct ibmvnic_adapter *adapter)
1814{
1815 struct ibmvnic_crq_queue *queue = &adapter->crq;
1816 union ibmvnic_crq *crq;
1817
1818 crq = &queue->msgs[queue->cur];
1819 if (crq->generic.first & IBMVNIC_CRQ_CMD_RSP) {
1820 if (++queue->cur == queue->size)
1821 queue->cur = 0;
1822 } else {
1823 crq = NULL;
1824 }
1825
1826 return crq;
1827}
1828
1829static int send_subcrq(struct ibmvnic_adapter *adapter, u64 remote_handle,
1830 union sub_crq *sub_crq)
1831{
1832 unsigned int ua = adapter->vdev->unit_address;
1833 struct device *dev = &adapter->vdev->dev;
1834 u64 *u64_crq = (u64 *)sub_crq;
1835 int rc;
1836
1837 netdev_dbg(adapter->netdev,
1838 "Sending sCRQ %016lx: %016lx %016lx %016lx %016lx\n",
1839 (unsigned long int)cpu_to_be64(remote_handle),
1840 (unsigned long int)cpu_to_be64(u64_crq[0]),
1841 (unsigned long int)cpu_to_be64(u64_crq[1]),
1842 (unsigned long int)cpu_to_be64(u64_crq[2]),
1843 (unsigned long int)cpu_to_be64(u64_crq[3]));
1844
1845 /* Make sure the hypervisor sees the complete request */
1846 mb();
1847
1848 rc = plpar_hcall_norets(H_SEND_SUB_CRQ, ua,
1849 cpu_to_be64(remote_handle),
1850 cpu_to_be64(u64_crq[0]),
1851 cpu_to_be64(u64_crq[1]),
1852 cpu_to_be64(u64_crq[2]),
1853 cpu_to_be64(u64_crq[3]));
1854
1855 if (rc) {
1856 if (rc == H_CLOSED)
1857 dev_warn(dev, "CRQ Queue closed\n");
1858 dev_err(dev, "Send error (rc=%d)\n", rc);
1859 }
1860
1861 return rc;
1862}
1863
Thomas Falconad7775d2016-04-01 17:20:34 -05001864static int send_subcrq_indirect(struct ibmvnic_adapter *adapter,
1865 u64 remote_handle, u64 ioba, u64 num_entries)
1866{
1867 unsigned int ua = adapter->vdev->unit_address;
1868 struct device *dev = &adapter->vdev->dev;
1869 int rc;
1870
1871 /* Make sure the hypervisor sees the complete request */
1872 mb();
1873 rc = plpar_hcall_norets(H_SEND_SUB_CRQ_INDIRECT, ua,
1874 cpu_to_be64(remote_handle),
1875 ioba, num_entries);
1876
1877 if (rc) {
1878 if (rc == H_CLOSED)
1879 dev_warn(dev, "CRQ Queue closed\n");
1880 dev_err(dev, "Send (indirect) error (rc=%d)\n", rc);
1881 }
1882
1883 return rc;
1884}
1885
Thomas Falcon032c5e82015-12-21 11:26:06 -06001886static int ibmvnic_send_crq(struct ibmvnic_adapter *adapter,
1887 union ibmvnic_crq *crq)
1888{
1889 unsigned int ua = adapter->vdev->unit_address;
1890 struct device *dev = &adapter->vdev->dev;
1891 u64 *u64_crq = (u64 *)crq;
1892 int rc;
1893
1894 netdev_dbg(adapter->netdev, "Sending CRQ: %016lx %016lx\n",
1895 (unsigned long int)cpu_to_be64(u64_crq[0]),
1896 (unsigned long int)cpu_to_be64(u64_crq[1]));
1897
1898 /* Make sure the hypervisor sees the complete request */
1899 mb();
1900
1901 rc = plpar_hcall_norets(H_SEND_CRQ, ua,
1902 cpu_to_be64(u64_crq[0]),
1903 cpu_to_be64(u64_crq[1]));
1904
1905 if (rc) {
1906 if (rc == H_CLOSED)
1907 dev_warn(dev, "CRQ Queue closed\n");
1908 dev_warn(dev, "Send error (rc=%d)\n", rc);
1909 }
1910
1911 return rc;
1912}
1913
1914static int ibmvnic_send_crq_init(struct ibmvnic_adapter *adapter)
1915{
1916 union ibmvnic_crq crq;
1917
1918 memset(&crq, 0, sizeof(crq));
1919 crq.generic.first = IBMVNIC_CRQ_INIT_CMD;
1920 crq.generic.cmd = IBMVNIC_CRQ_INIT;
1921 netdev_dbg(adapter->netdev, "Sending CRQ init\n");
1922
1923 return ibmvnic_send_crq(adapter, &crq);
1924}
1925
1926static int ibmvnic_send_crq_init_complete(struct ibmvnic_adapter *adapter)
1927{
1928 union ibmvnic_crq crq;
1929
1930 memset(&crq, 0, sizeof(crq));
1931 crq.generic.first = IBMVNIC_CRQ_INIT_CMD;
1932 crq.generic.cmd = IBMVNIC_CRQ_INIT_COMPLETE;
1933 netdev_dbg(adapter->netdev, "Sending CRQ init complete\n");
1934
1935 return ibmvnic_send_crq(adapter, &crq);
1936}
1937
1938static int send_version_xchg(struct ibmvnic_adapter *adapter)
1939{
1940 union ibmvnic_crq crq;
1941
1942 memset(&crq, 0, sizeof(crq));
1943 crq.version_exchange.first = IBMVNIC_CRQ_CMD;
1944 crq.version_exchange.cmd = VERSION_EXCHANGE;
1945 crq.version_exchange.version = cpu_to_be16(ibmvnic_version);
1946
1947 return ibmvnic_send_crq(adapter, &crq);
1948}
1949
1950static void send_login(struct ibmvnic_adapter *adapter)
1951{
1952 struct ibmvnic_login_rsp_buffer *login_rsp_buffer;
1953 struct ibmvnic_login_buffer *login_buffer;
1954 struct ibmvnic_inflight_cmd *inflight_cmd;
1955 struct device *dev = &adapter->vdev->dev;
1956 dma_addr_t rsp_buffer_token;
1957 dma_addr_t buffer_token;
1958 size_t rsp_buffer_size;
1959 union ibmvnic_crq crq;
1960 unsigned long flags;
1961 size_t buffer_size;
1962 __be64 *tx_list_p;
1963 __be64 *rx_list_p;
1964 int i;
1965
1966 buffer_size =
1967 sizeof(struct ibmvnic_login_buffer) +
1968 sizeof(u64) * (adapter->req_tx_queues + adapter->req_rx_queues);
1969
1970 login_buffer = kmalloc(buffer_size, GFP_ATOMIC);
1971 if (!login_buffer)
1972 goto buf_alloc_failed;
1973
1974 buffer_token = dma_map_single(dev, login_buffer, buffer_size,
1975 DMA_TO_DEVICE);
1976 if (dma_mapping_error(dev, buffer_token)) {
1977 dev_err(dev, "Couldn't map login buffer\n");
1978 goto buf_map_failed;
1979 }
1980
John Allen498cd8e2016-04-06 11:49:55 -05001981 rsp_buffer_size = sizeof(struct ibmvnic_login_rsp_buffer) +
1982 sizeof(u64) * adapter->req_tx_queues +
1983 sizeof(u64) * adapter->req_rx_queues +
1984 sizeof(u64) * adapter->req_rx_queues +
1985 sizeof(u8) * IBMVNIC_TX_DESC_VERSIONS;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001986
1987 login_rsp_buffer = kmalloc(rsp_buffer_size, GFP_ATOMIC);
1988 if (!login_rsp_buffer)
1989 goto buf_rsp_alloc_failed;
1990
1991 rsp_buffer_token = dma_map_single(dev, login_rsp_buffer,
1992 rsp_buffer_size, DMA_FROM_DEVICE);
1993 if (dma_mapping_error(dev, rsp_buffer_token)) {
1994 dev_err(dev, "Couldn't map login rsp buffer\n");
1995 goto buf_rsp_map_failed;
1996 }
1997 inflight_cmd = kmalloc(sizeof(*inflight_cmd), GFP_ATOMIC);
1998 if (!inflight_cmd) {
1999 dev_err(dev, "Couldn't allocate inflight_cmd\n");
2000 goto inflight_alloc_failed;
2001 }
2002 adapter->login_buf = login_buffer;
2003 adapter->login_buf_token = buffer_token;
2004 adapter->login_buf_sz = buffer_size;
2005 adapter->login_rsp_buf = login_rsp_buffer;
2006 adapter->login_rsp_buf_token = rsp_buffer_token;
2007 adapter->login_rsp_buf_sz = rsp_buffer_size;
2008
2009 login_buffer->len = cpu_to_be32(buffer_size);
2010 login_buffer->version = cpu_to_be32(INITIAL_VERSION_LB);
2011 login_buffer->num_txcomp_subcrqs = cpu_to_be32(adapter->req_tx_queues);
2012 login_buffer->off_txcomp_subcrqs =
2013 cpu_to_be32(sizeof(struct ibmvnic_login_buffer));
2014 login_buffer->num_rxcomp_subcrqs = cpu_to_be32(adapter->req_rx_queues);
2015 login_buffer->off_rxcomp_subcrqs =
2016 cpu_to_be32(sizeof(struct ibmvnic_login_buffer) +
2017 sizeof(u64) * adapter->req_tx_queues);
2018 login_buffer->login_rsp_ioba = cpu_to_be32(rsp_buffer_token);
2019 login_buffer->login_rsp_len = cpu_to_be32(rsp_buffer_size);
2020
2021 tx_list_p = (__be64 *)((char *)login_buffer +
2022 sizeof(struct ibmvnic_login_buffer));
2023 rx_list_p = (__be64 *)((char *)login_buffer +
2024 sizeof(struct ibmvnic_login_buffer) +
2025 sizeof(u64) * adapter->req_tx_queues);
2026
2027 for (i = 0; i < adapter->req_tx_queues; i++) {
2028 if (adapter->tx_scrq[i]) {
2029 tx_list_p[i] = cpu_to_be64(adapter->tx_scrq[i]->
2030 crq_num);
2031 }
2032 }
2033
2034 for (i = 0; i < adapter->req_rx_queues; i++) {
2035 if (adapter->rx_scrq[i]) {
2036 rx_list_p[i] = cpu_to_be64(adapter->rx_scrq[i]->
2037 crq_num);
2038 }
2039 }
2040
2041 netdev_dbg(adapter->netdev, "Login Buffer:\n");
2042 for (i = 0; i < (adapter->login_buf_sz - 1) / 8 + 1; i++) {
2043 netdev_dbg(adapter->netdev, "%016lx\n",
2044 ((unsigned long int *)(adapter->login_buf))[i]);
2045 }
2046
2047 memset(&crq, 0, sizeof(crq));
2048 crq.login.first = IBMVNIC_CRQ_CMD;
2049 crq.login.cmd = LOGIN;
2050 crq.login.ioba = cpu_to_be32(buffer_token);
2051 crq.login.len = cpu_to_be32(buffer_size);
2052
2053 memcpy(&inflight_cmd->crq, &crq, sizeof(crq));
2054
2055 spin_lock_irqsave(&adapter->inflight_lock, flags);
2056 list_add_tail(&inflight_cmd->list, &adapter->inflight);
2057 spin_unlock_irqrestore(&adapter->inflight_lock, flags);
2058
2059 ibmvnic_send_crq(adapter, &crq);
2060
2061 return;
2062
2063inflight_alloc_failed:
2064 dma_unmap_single(dev, rsp_buffer_token, rsp_buffer_size,
2065 DMA_FROM_DEVICE);
2066buf_rsp_map_failed:
2067 kfree(login_rsp_buffer);
2068buf_rsp_alloc_failed:
2069 dma_unmap_single(dev, buffer_token, buffer_size, DMA_TO_DEVICE);
2070buf_map_failed:
2071 kfree(login_buffer);
2072buf_alloc_failed:
2073 return;
2074}
2075
2076static void send_request_map(struct ibmvnic_adapter *adapter, dma_addr_t addr,
2077 u32 len, u8 map_id)
2078{
2079 union ibmvnic_crq crq;
2080
2081 memset(&crq, 0, sizeof(crq));
2082 crq.request_map.first = IBMVNIC_CRQ_CMD;
2083 crq.request_map.cmd = REQUEST_MAP;
2084 crq.request_map.map_id = map_id;
2085 crq.request_map.ioba = cpu_to_be32(addr);
2086 crq.request_map.len = cpu_to_be32(len);
2087 ibmvnic_send_crq(adapter, &crq);
2088}
2089
2090static void send_request_unmap(struct ibmvnic_adapter *adapter, u8 map_id)
2091{
2092 union ibmvnic_crq crq;
2093
2094 memset(&crq, 0, sizeof(crq));
2095 crq.request_unmap.first = IBMVNIC_CRQ_CMD;
2096 crq.request_unmap.cmd = REQUEST_UNMAP;
2097 crq.request_unmap.map_id = map_id;
2098 ibmvnic_send_crq(adapter, &crq);
2099}
2100
2101static void send_map_query(struct ibmvnic_adapter *adapter)
2102{
2103 union ibmvnic_crq crq;
2104
2105 memset(&crq, 0, sizeof(crq));
2106 crq.query_map.first = IBMVNIC_CRQ_CMD;
2107 crq.query_map.cmd = QUERY_MAP;
2108 ibmvnic_send_crq(adapter, &crq);
2109}
2110
2111/* Send a series of CRQs requesting various capabilities of the VNIC server */
2112static void send_cap_queries(struct ibmvnic_adapter *adapter)
2113{
2114 union ibmvnic_crq crq;
2115
Thomas Falcon901e0402017-02-15 12:17:59 -06002116 atomic_set(&adapter->running_cap_crqs, 0);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002117 memset(&crq, 0, sizeof(crq));
2118 crq.query_capability.first = IBMVNIC_CRQ_CMD;
2119 crq.query_capability.cmd = QUERY_CAPABILITY;
2120
2121 crq.query_capability.capability = cpu_to_be16(MIN_TX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002122 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002123 ibmvnic_send_crq(adapter, &crq);
2124
2125 crq.query_capability.capability = cpu_to_be16(MIN_RX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002126 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002127 ibmvnic_send_crq(adapter, &crq);
2128
2129 crq.query_capability.capability = cpu_to_be16(MIN_RX_ADD_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002130 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002131 ibmvnic_send_crq(adapter, &crq);
2132
2133 crq.query_capability.capability = cpu_to_be16(MAX_TX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002134 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002135 ibmvnic_send_crq(adapter, &crq);
2136
2137 crq.query_capability.capability = cpu_to_be16(MAX_RX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002138 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002139 ibmvnic_send_crq(adapter, &crq);
2140
2141 crq.query_capability.capability = cpu_to_be16(MAX_RX_ADD_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002142 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002143 ibmvnic_send_crq(adapter, &crq);
2144
2145 crq.query_capability.capability =
2146 cpu_to_be16(MIN_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06002147 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002148 ibmvnic_send_crq(adapter, &crq);
2149
2150 crq.query_capability.capability =
2151 cpu_to_be16(MIN_RX_ADD_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06002152 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002153 ibmvnic_send_crq(adapter, &crq);
2154
2155 crq.query_capability.capability =
2156 cpu_to_be16(MAX_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06002157 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002158 ibmvnic_send_crq(adapter, &crq);
2159
2160 crq.query_capability.capability =
2161 cpu_to_be16(MAX_RX_ADD_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06002162 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002163 ibmvnic_send_crq(adapter, &crq);
2164
2165 crq.query_capability.capability = cpu_to_be16(TCP_IP_OFFLOAD);
Thomas Falcon901e0402017-02-15 12:17:59 -06002166 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002167 ibmvnic_send_crq(adapter, &crq);
2168
2169 crq.query_capability.capability = cpu_to_be16(PROMISC_SUPPORTED);
Thomas Falcon901e0402017-02-15 12:17:59 -06002170 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002171 ibmvnic_send_crq(adapter, &crq);
2172
2173 crq.query_capability.capability = cpu_to_be16(MIN_MTU);
Thomas Falcon901e0402017-02-15 12:17:59 -06002174 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002175 ibmvnic_send_crq(adapter, &crq);
2176
2177 crq.query_capability.capability = cpu_to_be16(MAX_MTU);
Thomas Falcon901e0402017-02-15 12:17:59 -06002178 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002179 ibmvnic_send_crq(adapter, &crq);
2180
2181 crq.query_capability.capability = cpu_to_be16(MAX_MULTICAST_FILTERS);
Thomas Falcon901e0402017-02-15 12:17:59 -06002182 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002183 ibmvnic_send_crq(adapter, &crq);
2184
2185 crq.query_capability.capability = cpu_to_be16(VLAN_HEADER_INSERTION);
Thomas Falcon901e0402017-02-15 12:17:59 -06002186 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002187 ibmvnic_send_crq(adapter, &crq);
2188
2189 crq.query_capability.capability = cpu_to_be16(MAX_TX_SG_ENTRIES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002190 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002191 ibmvnic_send_crq(adapter, &crq);
2192
2193 crq.query_capability.capability = cpu_to_be16(RX_SG_SUPPORTED);
Thomas Falcon901e0402017-02-15 12:17:59 -06002194 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002195 ibmvnic_send_crq(adapter, &crq);
2196
2197 crq.query_capability.capability = cpu_to_be16(OPT_TX_COMP_SUB_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002198 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002199 ibmvnic_send_crq(adapter, &crq);
2200
2201 crq.query_capability.capability = cpu_to_be16(OPT_RX_COMP_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002202 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002203 ibmvnic_send_crq(adapter, &crq);
2204
2205 crq.query_capability.capability =
2206 cpu_to_be16(OPT_RX_BUFADD_Q_PER_RX_COMP_Q);
Thomas Falcon901e0402017-02-15 12:17:59 -06002207 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002208 ibmvnic_send_crq(adapter, &crq);
2209
2210 crq.query_capability.capability =
2211 cpu_to_be16(OPT_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06002212 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002213 ibmvnic_send_crq(adapter, &crq);
2214
2215 crq.query_capability.capability =
2216 cpu_to_be16(OPT_RXBA_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06002217 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002218 ibmvnic_send_crq(adapter, &crq);
2219
2220 crq.query_capability.capability = cpu_to_be16(TX_RX_DESC_REQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06002221 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002222 ibmvnic_send_crq(adapter, &crq);
2223}
2224
2225static void handle_query_ip_offload_rsp(struct ibmvnic_adapter *adapter)
2226{
2227 struct device *dev = &adapter->vdev->dev;
2228 struct ibmvnic_query_ip_offload_buffer *buf = &adapter->ip_offload_buf;
2229 union ibmvnic_crq crq;
2230 int i;
2231
2232 dma_unmap_single(dev, adapter->ip_offload_tok,
2233 sizeof(adapter->ip_offload_buf), DMA_FROM_DEVICE);
2234
2235 netdev_dbg(adapter->netdev, "Query IP Offload Buffer:\n");
2236 for (i = 0; i < (sizeof(adapter->ip_offload_buf) - 1) / 8 + 1; i++)
2237 netdev_dbg(adapter->netdev, "%016lx\n",
2238 ((unsigned long int *)(buf))[i]);
2239
2240 netdev_dbg(adapter->netdev, "ipv4_chksum = %d\n", buf->ipv4_chksum);
2241 netdev_dbg(adapter->netdev, "ipv6_chksum = %d\n", buf->ipv6_chksum);
2242 netdev_dbg(adapter->netdev, "tcp_ipv4_chksum = %d\n",
2243 buf->tcp_ipv4_chksum);
2244 netdev_dbg(adapter->netdev, "tcp_ipv6_chksum = %d\n",
2245 buf->tcp_ipv6_chksum);
2246 netdev_dbg(adapter->netdev, "udp_ipv4_chksum = %d\n",
2247 buf->udp_ipv4_chksum);
2248 netdev_dbg(adapter->netdev, "udp_ipv6_chksum = %d\n",
2249 buf->udp_ipv6_chksum);
2250 netdev_dbg(adapter->netdev, "large_tx_ipv4 = %d\n",
2251 buf->large_tx_ipv4);
2252 netdev_dbg(adapter->netdev, "large_tx_ipv6 = %d\n",
2253 buf->large_tx_ipv6);
2254 netdev_dbg(adapter->netdev, "large_rx_ipv4 = %d\n",
2255 buf->large_rx_ipv4);
2256 netdev_dbg(adapter->netdev, "large_rx_ipv6 = %d\n",
2257 buf->large_rx_ipv6);
2258 netdev_dbg(adapter->netdev, "max_ipv4_hdr_sz = %d\n",
2259 buf->max_ipv4_header_size);
2260 netdev_dbg(adapter->netdev, "max_ipv6_hdr_sz = %d\n",
2261 buf->max_ipv6_header_size);
2262 netdev_dbg(adapter->netdev, "max_tcp_hdr_size = %d\n",
2263 buf->max_tcp_header_size);
2264 netdev_dbg(adapter->netdev, "max_udp_hdr_size = %d\n",
2265 buf->max_udp_header_size);
2266 netdev_dbg(adapter->netdev, "max_large_tx_size = %d\n",
2267 buf->max_large_tx_size);
2268 netdev_dbg(adapter->netdev, "max_large_rx_size = %d\n",
2269 buf->max_large_rx_size);
2270 netdev_dbg(adapter->netdev, "ipv6_ext_hdr = %d\n",
2271 buf->ipv6_extension_header);
2272 netdev_dbg(adapter->netdev, "tcp_pseudosum_req = %d\n",
2273 buf->tcp_pseudosum_req);
2274 netdev_dbg(adapter->netdev, "num_ipv6_ext_hd = %d\n",
2275 buf->num_ipv6_ext_headers);
2276 netdev_dbg(adapter->netdev, "off_ipv6_ext_hd = %d\n",
2277 buf->off_ipv6_ext_headers);
2278
2279 adapter->ip_offload_ctrl_tok =
2280 dma_map_single(dev, &adapter->ip_offload_ctrl,
2281 sizeof(adapter->ip_offload_ctrl), DMA_TO_DEVICE);
2282
2283 if (dma_mapping_error(dev, adapter->ip_offload_ctrl_tok)) {
2284 dev_err(dev, "Couldn't map ip offload control buffer\n");
2285 return;
2286 }
2287
2288 adapter->ip_offload_ctrl.version = cpu_to_be32(INITIAL_VERSION_IOB);
2289 adapter->ip_offload_ctrl.tcp_ipv4_chksum = buf->tcp_ipv4_chksum;
2290 adapter->ip_offload_ctrl.udp_ipv4_chksum = buf->udp_ipv4_chksum;
2291 adapter->ip_offload_ctrl.tcp_ipv6_chksum = buf->tcp_ipv6_chksum;
2292 adapter->ip_offload_ctrl.udp_ipv6_chksum = buf->udp_ipv6_chksum;
2293
2294 /* large_tx/rx disabled for now, additional features needed */
2295 adapter->ip_offload_ctrl.large_tx_ipv4 = 0;
2296 adapter->ip_offload_ctrl.large_tx_ipv6 = 0;
2297 adapter->ip_offload_ctrl.large_rx_ipv4 = 0;
2298 adapter->ip_offload_ctrl.large_rx_ipv6 = 0;
2299
2300 adapter->netdev->features = NETIF_F_GSO;
2301
2302 if (buf->tcp_ipv4_chksum || buf->udp_ipv4_chksum)
2303 adapter->netdev->features |= NETIF_F_IP_CSUM;
2304
2305 if (buf->tcp_ipv6_chksum || buf->udp_ipv6_chksum)
2306 adapter->netdev->features |= NETIF_F_IPV6_CSUM;
2307
Thomas Falcon9be02cd2016-04-01 17:20:35 -05002308 if ((adapter->netdev->features &
2309 (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)))
2310 adapter->netdev->features |= NETIF_F_RXCSUM;
2311
Thomas Falcon032c5e82015-12-21 11:26:06 -06002312 memset(&crq, 0, sizeof(crq));
2313 crq.control_ip_offload.first = IBMVNIC_CRQ_CMD;
2314 crq.control_ip_offload.cmd = CONTROL_IP_OFFLOAD;
2315 crq.control_ip_offload.len =
2316 cpu_to_be32(sizeof(adapter->ip_offload_ctrl));
2317 crq.control_ip_offload.ioba = cpu_to_be32(adapter->ip_offload_ctrl_tok);
2318 ibmvnic_send_crq(adapter, &crq);
2319}
2320
2321static void handle_error_info_rsp(union ibmvnic_crq *crq,
2322 struct ibmvnic_adapter *adapter)
2323{
2324 struct device *dev = &adapter->vdev->dev;
Wei Yongjun96183182016-06-27 20:48:53 +08002325 struct ibmvnic_error_buff *error_buff, *tmp;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002326 unsigned long flags;
2327 bool found = false;
2328 int i;
2329
2330 if (!crq->request_error_rsp.rc.code) {
2331 dev_info(dev, "Request Error Rsp returned with rc=%x\n",
2332 crq->request_error_rsp.rc.code);
2333 return;
2334 }
2335
2336 spin_lock_irqsave(&adapter->error_list_lock, flags);
Wei Yongjun96183182016-06-27 20:48:53 +08002337 list_for_each_entry_safe(error_buff, tmp, &adapter->errors, list)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002338 if (error_buff->error_id == crq->request_error_rsp.error_id) {
2339 found = true;
2340 list_del(&error_buff->list);
2341 break;
2342 }
2343 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
2344
2345 if (!found) {
2346 dev_err(dev, "Couldn't find error id %x\n",
Thomas Falcon75224c92017-02-15 10:33:33 -06002347 be32_to_cpu(crq->request_error_rsp.error_id));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002348 return;
2349 }
2350
2351 dev_err(dev, "Detailed info for error id %x:",
Thomas Falcon75224c92017-02-15 10:33:33 -06002352 be32_to_cpu(crq->request_error_rsp.error_id));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002353
2354 for (i = 0; i < error_buff->len; i++) {
2355 pr_cont("%02x", (int)error_buff->buff[i]);
2356 if (i % 8 == 7)
2357 pr_cont(" ");
2358 }
2359 pr_cont("\n");
2360
2361 dma_unmap_single(dev, error_buff->dma, error_buff->len,
2362 DMA_FROM_DEVICE);
2363 kfree(error_buff->buff);
2364 kfree(error_buff);
2365}
2366
Thomas Falcon032c5e82015-12-21 11:26:06 -06002367static void handle_error_indication(union ibmvnic_crq *crq,
2368 struct ibmvnic_adapter *adapter)
2369{
2370 int detail_len = be32_to_cpu(crq->error_indication.detail_error_sz);
2371 struct ibmvnic_inflight_cmd *inflight_cmd;
2372 struct device *dev = &adapter->vdev->dev;
2373 struct ibmvnic_error_buff *error_buff;
2374 union ibmvnic_crq new_crq;
2375 unsigned long flags;
2376
2377 dev_err(dev, "Firmware reports %serror id %x, cause %d\n",
2378 crq->error_indication.
2379 flags & IBMVNIC_FATAL_ERROR ? "FATAL " : "",
Thomas Falcon75224c92017-02-15 10:33:33 -06002380 be32_to_cpu(crq->error_indication.error_id),
2381 be16_to_cpu(crq->error_indication.error_cause));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002382
2383 error_buff = kmalloc(sizeof(*error_buff), GFP_ATOMIC);
2384 if (!error_buff)
2385 return;
2386
2387 error_buff->buff = kmalloc(detail_len, GFP_ATOMIC);
2388 if (!error_buff->buff) {
2389 kfree(error_buff);
2390 return;
2391 }
2392
2393 error_buff->dma = dma_map_single(dev, error_buff->buff, detail_len,
2394 DMA_FROM_DEVICE);
2395 if (dma_mapping_error(dev, error_buff->dma)) {
2396 if (!firmware_has_feature(FW_FEATURE_CMO))
2397 dev_err(dev, "Couldn't map error buffer\n");
2398 kfree(error_buff->buff);
2399 kfree(error_buff);
2400 return;
2401 }
2402
2403 inflight_cmd = kmalloc(sizeof(*inflight_cmd), GFP_ATOMIC);
2404 if (!inflight_cmd) {
2405 dma_unmap_single(dev, error_buff->dma, detail_len,
2406 DMA_FROM_DEVICE);
2407 kfree(error_buff->buff);
2408 kfree(error_buff);
2409 return;
2410 }
2411
2412 error_buff->len = detail_len;
2413 error_buff->error_id = crq->error_indication.error_id;
2414
2415 spin_lock_irqsave(&adapter->error_list_lock, flags);
2416 list_add_tail(&error_buff->list, &adapter->errors);
2417 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
2418
2419 memset(&new_crq, 0, sizeof(new_crq));
2420 new_crq.request_error_info.first = IBMVNIC_CRQ_CMD;
2421 new_crq.request_error_info.cmd = REQUEST_ERROR_INFO;
2422 new_crq.request_error_info.ioba = cpu_to_be32(error_buff->dma);
2423 new_crq.request_error_info.len = cpu_to_be32(detail_len);
2424 new_crq.request_error_info.error_id = crq->error_indication.error_id;
2425
2426 memcpy(&inflight_cmd->crq, &crq, sizeof(crq));
2427
2428 spin_lock_irqsave(&adapter->inflight_lock, flags);
2429 list_add_tail(&inflight_cmd->list, &adapter->inflight);
2430 spin_unlock_irqrestore(&adapter->inflight_lock, flags);
2431
2432 ibmvnic_send_crq(adapter, &new_crq);
2433}
2434
2435static void handle_change_mac_rsp(union ibmvnic_crq *crq,
2436 struct ibmvnic_adapter *adapter)
2437{
2438 struct net_device *netdev = adapter->netdev;
2439 struct device *dev = &adapter->vdev->dev;
2440 long rc;
2441
2442 rc = crq->change_mac_addr_rsp.rc.code;
2443 if (rc) {
2444 dev_err(dev, "Error %ld in CHANGE_MAC_ADDR_RSP\n", rc);
2445 return;
2446 }
2447 memcpy(netdev->dev_addr, &crq->change_mac_addr_rsp.mac_addr[0],
2448 ETH_ALEN);
2449}
2450
2451static void handle_request_cap_rsp(union ibmvnic_crq *crq,
2452 struct ibmvnic_adapter *adapter)
2453{
2454 struct device *dev = &adapter->vdev->dev;
2455 u64 *req_value;
2456 char *name;
2457
Thomas Falcon901e0402017-02-15 12:17:59 -06002458 atomic_dec(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002459 switch (be16_to_cpu(crq->request_capability_rsp.capability)) {
2460 case REQ_TX_QUEUES:
2461 req_value = &adapter->req_tx_queues;
2462 name = "tx";
2463 break;
2464 case REQ_RX_QUEUES:
2465 req_value = &adapter->req_rx_queues;
2466 name = "rx";
2467 break;
2468 case REQ_RX_ADD_QUEUES:
2469 req_value = &adapter->req_rx_add_queues;
2470 name = "rx_add";
2471 break;
2472 case REQ_TX_ENTRIES_PER_SUBCRQ:
2473 req_value = &adapter->req_tx_entries_per_subcrq;
2474 name = "tx_entries_per_subcrq";
2475 break;
2476 case REQ_RX_ADD_ENTRIES_PER_SUBCRQ:
2477 req_value = &adapter->req_rx_add_entries_per_subcrq;
2478 name = "rx_add_entries_per_subcrq";
2479 break;
2480 case REQ_MTU:
2481 req_value = &adapter->req_mtu;
2482 name = "mtu";
2483 break;
2484 case PROMISC_REQUESTED:
2485 req_value = &adapter->promisc;
2486 name = "promisc";
2487 break;
2488 default:
2489 dev_err(dev, "Got invalid cap request rsp %d\n",
2490 crq->request_capability.capability);
2491 return;
2492 }
2493
2494 switch (crq->request_capability_rsp.rc.code) {
2495 case SUCCESS:
2496 break;
2497 case PARTIALSUCCESS:
2498 dev_info(dev, "req=%lld, rsp=%ld in %s queue, retrying.\n",
2499 *req_value,
Thomas Falcon28f4d162017-02-15 10:32:11 -06002500 (long int)be64_to_cpu(crq->request_capability_rsp.
Thomas Falcon032c5e82015-12-21 11:26:06 -06002501 number), name);
Thomas Falconea22d512016-07-06 15:35:17 -05002502 release_sub_crqs_no_irqs(adapter);
Thomas Falcon28f4d162017-02-15 10:32:11 -06002503 *req_value = be64_to_cpu(crq->request_capability_rsp.number);
Thomas Falconea22d512016-07-06 15:35:17 -05002504 init_sub_crqs(adapter, 1);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002505 return;
2506 default:
2507 dev_err(dev, "Error %d in request cap rsp\n",
2508 crq->request_capability_rsp.rc.code);
2509 return;
2510 }
2511
2512 /* Done receiving requested capabilities, query IP offload support */
Thomas Falcon901e0402017-02-15 12:17:59 -06002513 if (atomic_read(&adapter->running_cap_crqs) == 0) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06002514 union ibmvnic_crq newcrq;
2515 int buf_sz = sizeof(struct ibmvnic_query_ip_offload_buffer);
2516 struct ibmvnic_query_ip_offload_buffer *ip_offload_buf =
2517 &adapter->ip_offload_buf;
2518
Thomas Falcon249168a2017-02-15 12:18:00 -06002519 adapter->wait_capability = false;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002520 adapter->ip_offload_tok = dma_map_single(dev, ip_offload_buf,
2521 buf_sz,
2522 DMA_FROM_DEVICE);
2523
2524 if (dma_mapping_error(dev, adapter->ip_offload_tok)) {
2525 if (!firmware_has_feature(FW_FEATURE_CMO))
2526 dev_err(dev, "Couldn't map offload buffer\n");
2527 return;
2528 }
2529
2530 memset(&newcrq, 0, sizeof(newcrq));
2531 newcrq.query_ip_offload.first = IBMVNIC_CRQ_CMD;
2532 newcrq.query_ip_offload.cmd = QUERY_IP_OFFLOAD;
2533 newcrq.query_ip_offload.len = cpu_to_be32(buf_sz);
2534 newcrq.query_ip_offload.ioba =
2535 cpu_to_be32(adapter->ip_offload_tok);
2536
2537 ibmvnic_send_crq(adapter, &newcrq);
2538 }
2539}
2540
2541static int handle_login_rsp(union ibmvnic_crq *login_rsp_crq,
2542 struct ibmvnic_adapter *adapter)
2543{
2544 struct device *dev = &adapter->vdev->dev;
2545 struct ibmvnic_login_rsp_buffer *login_rsp = adapter->login_rsp_buf;
2546 struct ibmvnic_login_buffer *login = adapter->login_buf;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002547 int i;
2548
2549 dma_unmap_single(dev, adapter->login_buf_token, adapter->login_buf_sz,
2550 DMA_BIDIRECTIONAL);
2551 dma_unmap_single(dev, adapter->login_rsp_buf_token,
2552 adapter->login_rsp_buf_sz, DMA_BIDIRECTIONAL);
2553
John Allen498cd8e2016-04-06 11:49:55 -05002554 /* If the number of queues requested can't be allocated by the
2555 * server, the login response will return with code 1. We will need
2556 * to resend the login buffer with fewer queues requested.
2557 */
2558 if (login_rsp_crq->generic.rc.code) {
2559 adapter->renegotiate = true;
2560 complete(&adapter->init_done);
2561 return 0;
2562 }
2563
Thomas Falcon032c5e82015-12-21 11:26:06 -06002564 netdev_dbg(adapter->netdev, "Login Response Buffer:\n");
2565 for (i = 0; i < (adapter->login_rsp_buf_sz - 1) / 8 + 1; i++) {
2566 netdev_dbg(adapter->netdev, "%016lx\n",
2567 ((unsigned long int *)(adapter->login_rsp_buf))[i]);
2568 }
2569
2570 /* Sanity checks */
2571 if (login->num_txcomp_subcrqs != login_rsp->num_txsubm_subcrqs ||
2572 (be32_to_cpu(login->num_rxcomp_subcrqs) *
2573 adapter->req_rx_add_queues !=
2574 be32_to_cpu(login_rsp->num_rxadd_subcrqs))) {
2575 dev_err(dev, "FATAL: Inconsistent login and login rsp\n");
2576 ibmvnic_remove(adapter->vdev);
2577 return -EIO;
2578 }
2579 complete(&adapter->init_done);
2580
Thomas Falcon032c5e82015-12-21 11:26:06 -06002581 return 0;
2582}
2583
2584static void handle_request_map_rsp(union ibmvnic_crq *crq,
2585 struct ibmvnic_adapter *adapter)
2586{
2587 struct device *dev = &adapter->vdev->dev;
2588 u8 map_id = crq->request_map_rsp.map_id;
2589 int tx_subcrqs;
2590 int rx_subcrqs;
2591 long rc;
2592 int i;
2593
2594 tx_subcrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
2595 rx_subcrqs = be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
2596
2597 rc = crq->request_map_rsp.rc.code;
2598 if (rc) {
2599 dev_err(dev, "Error %ld in REQUEST_MAP_RSP\n", rc);
2600 adapter->map_id--;
2601 /* need to find and zero tx/rx_pool map_id */
2602 for (i = 0; i < tx_subcrqs; i++) {
2603 if (adapter->tx_pool[i].long_term_buff.map_id == map_id)
2604 adapter->tx_pool[i].long_term_buff.map_id = 0;
2605 }
2606 for (i = 0; i < rx_subcrqs; i++) {
2607 if (adapter->rx_pool[i].long_term_buff.map_id == map_id)
2608 adapter->rx_pool[i].long_term_buff.map_id = 0;
2609 }
2610 }
2611 complete(&adapter->fw_done);
2612}
2613
2614static void handle_request_unmap_rsp(union ibmvnic_crq *crq,
2615 struct ibmvnic_adapter *adapter)
2616{
2617 struct device *dev = &adapter->vdev->dev;
2618 long rc;
2619
2620 rc = crq->request_unmap_rsp.rc.code;
2621 if (rc)
2622 dev_err(dev, "Error %ld in REQUEST_UNMAP_RSP\n", rc);
2623}
2624
2625static void handle_query_map_rsp(union ibmvnic_crq *crq,
2626 struct ibmvnic_adapter *adapter)
2627{
2628 struct net_device *netdev = adapter->netdev;
2629 struct device *dev = &adapter->vdev->dev;
2630 long rc;
2631
2632 rc = crq->query_map_rsp.rc.code;
2633 if (rc) {
2634 dev_err(dev, "Error %ld in QUERY_MAP_RSP\n", rc);
2635 return;
2636 }
2637 netdev_dbg(netdev, "page_size = %d\ntot_pages = %d\nfree_pages = %d\n",
2638 crq->query_map_rsp.page_size, crq->query_map_rsp.tot_pages,
2639 crq->query_map_rsp.free_pages);
2640}
2641
2642static void handle_query_cap_rsp(union ibmvnic_crq *crq,
2643 struct ibmvnic_adapter *adapter)
2644{
2645 struct net_device *netdev = adapter->netdev;
2646 struct device *dev = &adapter->vdev->dev;
2647 long rc;
2648
Thomas Falcon901e0402017-02-15 12:17:59 -06002649 atomic_dec(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002650 netdev_dbg(netdev, "Outstanding queries: %d\n",
Thomas Falcon901e0402017-02-15 12:17:59 -06002651 atomic_read(&adapter->running_cap_crqs));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002652 rc = crq->query_capability.rc.code;
2653 if (rc) {
2654 dev_err(dev, "Error %ld in QUERY_CAP_RSP\n", rc);
2655 goto out;
2656 }
2657
2658 switch (be16_to_cpu(crq->query_capability.capability)) {
2659 case MIN_TX_QUEUES:
2660 adapter->min_tx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06002661 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002662 netdev_dbg(netdev, "min_tx_queues = %lld\n",
2663 adapter->min_tx_queues);
2664 break;
2665 case MIN_RX_QUEUES:
2666 adapter->min_rx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06002667 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002668 netdev_dbg(netdev, "min_rx_queues = %lld\n",
2669 adapter->min_rx_queues);
2670 break;
2671 case MIN_RX_ADD_QUEUES:
2672 adapter->min_rx_add_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06002673 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002674 netdev_dbg(netdev, "min_rx_add_queues = %lld\n",
2675 adapter->min_rx_add_queues);
2676 break;
2677 case MAX_TX_QUEUES:
2678 adapter->max_tx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06002679 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002680 netdev_dbg(netdev, "max_tx_queues = %lld\n",
2681 adapter->max_tx_queues);
2682 break;
2683 case MAX_RX_QUEUES:
2684 adapter->max_rx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06002685 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002686 netdev_dbg(netdev, "max_rx_queues = %lld\n",
2687 adapter->max_rx_queues);
2688 break;
2689 case MAX_RX_ADD_QUEUES:
2690 adapter->max_rx_add_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06002691 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002692 netdev_dbg(netdev, "max_rx_add_queues = %lld\n",
2693 adapter->max_rx_add_queues);
2694 break;
2695 case MIN_TX_ENTRIES_PER_SUBCRQ:
2696 adapter->min_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06002697 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002698 netdev_dbg(netdev, "min_tx_entries_per_subcrq = %lld\n",
2699 adapter->min_tx_entries_per_subcrq);
2700 break;
2701 case MIN_RX_ADD_ENTRIES_PER_SUBCRQ:
2702 adapter->min_rx_add_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06002703 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002704 netdev_dbg(netdev, "min_rx_add_entrs_per_subcrq = %lld\n",
2705 adapter->min_rx_add_entries_per_subcrq);
2706 break;
2707 case MAX_TX_ENTRIES_PER_SUBCRQ:
2708 adapter->max_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06002709 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002710 netdev_dbg(netdev, "max_tx_entries_per_subcrq = %lld\n",
2711 adapter->max_tx_entries_per_subcrq);
2712 break;
2713 case MAX_RX_ADD_ENTRIES_PER_SUBCRQ:
2714 adapter->max_rx_add_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06002715 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002716 netdev_dbg(netdev, "max_rx_add_entrs_per_subcrq = %lld\n",
2717 adapter->max_rx_add_entries_per_subcrq);
2718 break;
2719 case TCP_IP_OFFLOAD:
2720 adapter->tcp_ip_offload =
Thomas Falconde89e852016-03-01 10:20:09 -06002721 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002722 netdev_dbg(netdev, "tcp_ip_offload = %lld\n",
2723 adapter->tcp_ip_offload);
2724 break;
2725 case PROMISC_SUPPORTED:
2726 adapter->promisc_supported =
Thomas Falconde89e852016-03-01 10:20:09 -06002727 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002728 netdev_dbg(netdev, "promisc_supported = %lld\n",
2729 adapter->promisc_supported);
2730 break;
2731 case MIN_MTU:
Thomas Falconde89e852016-03-01 10:20:09 -06002732 adapter->min_mtu = be64_to_cpu(crq->query_capability.number);
Thomas Falconf39f0d12017-02-14 10:22:59 -06002733 netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002734 netdev_dbg(netdev, "min_mtu = %lld\n", adapter->min_mtu);
2735 break;
2736 case MAX_MTU:
Thomas Falconde89e852016-03-01 10:20:09 -06002737 adapter->max_mtu = be64_to_cpu(crq->query_capability.number);
Thomas Falconf39f0d12017-02-14 10:22:59 -06002738 netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002739 netdev_dbg(netdev, "max_mtu = %lld\n", adapter->max_mtu);
2740 break;
2741 case MAX_MULTICAST_FILTERS:
2742 adapter->max_multicast_filters =
Thomas Falconde89e852016-03-01 10:20:09 -06002743 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002744 netdev_dbg(netdev, "max_multicast_filters = %lld\n",
2745 adapter->max_multicast_filters);
2746 break;
2747 case VLAN_HEADER_INSERTION:
2748 adapter->vlan_header_insertion =
Thomas Falconde89e852016-03-01 10:20:09 -06002749 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002750 if (adapter->vlan_header_insertion)
2751 netdev->features |= NETIF_F_HW_VLAN_STAG_TX;
2752 netdev_dbg(netdev, "vlan_header_insertion = %lld\n",
2753 adapter->vlan_header_insertion);
2754 break;
2755 case MAX_TX_SG_ENTRIES:
2756 adapter->max_tx_sg_entries =
Thomas Falconde89e852016-03-01 10:20:09 -06002757 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002758 netdev_dbg(netdev, "max_tx_sg_entries = %lld\n",
2759 adapter->max_tx_sg_entries);
2760 break;
2761 case RX_SG_SUPPORTED:
2762 adapter->rx_sg_supported =
Thomas Falconde89e852016-03-01 10:20:09 -06002763 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002764 netdev_dbg(netdev, "rx_sg_supported = %lld\n",
2765 adapter->rx_sg_supported);
2766 break;
2767 case OPT_TX_COMP_SUB_QUEUES:
2768 adapter->opt_tx_comp_sub_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06002769 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002770 netdev_dbg(netdev, "opt_tx_comp_sub_queues = %lld\n",
2771 adapter->opt_tx_comp_sub_queues);
2772 break;
2773 case OPT_RX_COMP_QUEUES:
2774 adapter->opt_rx_comp_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06002775 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002776 netdev_dbg(netdev, "opt_rx_comp_queues = %lld\n",
2777 adapter->opt_rx_comp_queues);
2778 break;
2779 case OPT_RX_BUFADD_Q_PER_RX_COMP_Q:
2780 adapter->opt_rx_bufadd_q_per_rx_comp_q =
Thomas Falconde89e852016-03-01 10:20:09 -06002781 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002782 netdev_dbg(netdev, "opt_rx_bufadd_q_per_rx_comp_q = %lld\n",
2783 adapter->opt_rx_bufadd_q_per_rx_comp_q);
2784 break;
2785 case OPT_TX_ENTRIES_PER_SUBCRQ:
2786 adapter->opt_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06002787 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002788 netdev_dbg(netdev, "opt_tx_entries_per_subcrq = %lld\n",
2789 adapter->opt_tx_entries_per_subcrq);
2790 break;
2791 case OPT_RXBA_ENTRIES_PER_SUBCRQ:
2792 adapter->opt_rxba_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06002793 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002794 netdev_dbg(netdev, "opt_rxba_entries_per_subcrq = %lld\n",
2795 adapter->opt_rxba_entries_per_subcrq);
2796 break;
2797 case TX_RX_DESC_REQ:
2798 adapter->tx_rx_desc_req = crq->query_capability.number;
2799 netdev_dbg(netdev, "tx_rx_desc_req = %llx\n",
2800 adapter->tx_rx_desc_req);
2801 break;
2802
2803 default:
2804 netdev_err(netdev, "Got invalid cap rsp %d\n",
2805 crq->query_capability.capability);
2806 }
2807
2808out:
Thomas Falcon249168a2017-02-15 12:18:00 -06002809 if (atomic_read(&adapter->running_cap_crqs) == 0) {
2810 adapter->wait_capability = false;
Thomas Falconea22d512016-07-06 15:35:17 -05002811 init_sub_crqs(adapter, 0);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002812 /* We're done querying the capabilities, initialize sub-crqs */
Thomas Falcon249168a2017-02-15 12:18:00 -06002813 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002814}
2815
Thomas Falcon032c5e82015-12-21 11:26:06 -06002816static void ibmvnic_free_inflight(struct ibmvnic_adapter *adapter)
2817{
Wei Yongjun96183182016-06-27 20:48:53 +08002818 struct ibmvnic_inflight_cmd *inflight_cmd, *tmp1;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002819 struct device *dev = &adapter->vdev->dev;
Wei Yongjun96183182016-06-27 20:48:53 +08002820 struct ibmvnic_error_buff *error_buff, *tmp2;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002821 unsigned long flags;
2822 unsigned long flags2;
2823
2824 spin_lock_irqsave(&adapter->inflight_lock, flags);
Wei Yongjun96183182016-06-27 20:48:53 +08002825 list_for_each_entry_safe(inflight_cmd, tmp1, &adapter->inflight, list) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06002826 switch (inflight_cmd->crq.generic.cmd) {
2827 case LOGIN:
2828 dma_unmap_single(dev, adapter->login_buf_token,
2829 adapter->login_buf_sz,
2830 DMA_BIDIRECTIONAL);
2831 dma_unmap_single(dev, adapter->login_rsp_buf_token,
2832 adapter->login_rsp_buf_sz,
2833 DMA_BIDIRECTIONAL);
2834 kfree(adapter->login_rsp_buf);
2835 kfree(adapter->login_buf);
2836 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002837 case REQUEST_ERROR_INFO:
2838 spin_lock_irqsave(&adapter->error_list_lock, flags2);
Wei Yongjun96183182016-06-27 20:48:53 +08002839 list_for_each_entry_safe(error_buff, tmp2,
2840 &adapter->errors, list) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06002841 dma_unmap_single(dev, error_buff->dma,
2842 error_buff->len,
2843 DMA_FROM_DEVICE);
2844 kfree(error_buff->buff);
2845 list_del(&error_buff->list);
2846 kfree(error_buff);
2847 }
2848 spin_unlock_irqrestore(&adapter->error_list_lock,
2849 flags2);
2850 break;
2851 }
2852 list_del(&inflight_cmd->list);
2853 kfree(inflight_cmd);
2854 }
2855 spin_unlock_irqrestore(&adapter->inflight_lock, flags);
2856}
2857
Thomas Falcon9888d7b2016-10-27 12:28:51 -05002858static void ibmvnic_xport_event(struct work_struct *work)
2859{
2860 struct ibmvnic_adapter *adapter = container_of(work,
2861 struct ibmvnic_adapter,
2862 ibmvnic_xport);
2863 struct device *dev = &adapter->vdev->dev;
2864 long rc;
2865
2866 ibmvnic_free_inflight(adapter);
2867 release_sub_crqs(adapter);
2868 if (adapter->migrated) {
2869 rc = ibmvnic_reenable_crq_queue(adapter);
2870 if (rc)
2871 dev_err(dev, "Error after enable rc=%ld\n", rc);
2872 adapter->migrated = false;
2873 rc = ibmvnic_send_crq_init(adapter);
2874 if (rc)
2875 dev_err(dev, "Error sending init rc=%ld\n", rc);
2876 }
2877}
2878
Thomas Falcon032c5e82015-12-21 11:26:06 -06002879static void ibmvnic_handle_crq(union ibmvnic_crq *crq,
2880 struct ibmvnic_adapter *adapter)
2881{
2882 struct ibmvnic_generic_crq *gen_crq = &crq->generic;
2883 struct net_device *netdev = adapter->netdev;
2884 struct device *dev = &adapter->vdev->dev;
2885 long rc;
2886
2887 netdev_dbg(netdev, "Handling CRQ: %016lx %016lx\n",
2888 ((unsigned long int *)crq)[0],
2889 ((unsigned long int *)crq)[1]);
2890 switch (gen_crq->first) {
2891 case IBMVNIC_CRQ_INIT_RSP:
2892 switch (gen_crq->cmd) {
2893 case IBMVNIC_CRQ_INIT:
2894 dev_info(dev, "Partner initialized\n");
2895 /* Send back a response */
2896 rc = ibmvnic_send_crq_init_complete(adapter);
Thomas Falcon65dc6892016-07-06 15:35:18 -05002897 if (!rc)
2898 schedule_work(&adapter->vnic_crq_init);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002899 else
2900 dev_err(dev, "Can't send initrsp rc=%ld\n", rc);
2901 break;
2902 case IBMVNIC_CRQ_INIT_COMPLETE:
2903 dev_info(dev, "Partner initialization complete\n");
2904 send_version_xchg(adapter);
2905 break;
2906 default:
2907 dev_err(dev, "Unknown crq cmd: %d\n", gen_crq->cmd);
2908 }
2909 return;
2910 case IBMVNIC_CRQ_XPORT_EVENT:
2911 if (gen_crq->cmd == IBMVNIC_PARTITION_MIGRATED) {
2912 dev_info(dev, "Re-enabling adapter\n");
2913 adapter->migrated = true;
Thomas Falcon9888d7b2016-10-27 12:28:51 -05002914 schedule_work(&adapter->ibmvnic_xport);
Thomas Falcondfad09a2016-08-18 11:37:51 -05002915 } else if (gen_crq->cmd == IBMVNIC_DEVICE_FAILOVER) {
2916 dev_info(dev, "Backing device failover detected\n");
2917 netif_carrier_off(netdev);
2918 adapter->failover = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002919 } else {
2920 /* The adapter lost the connection */
2921 dev_err(dev, "Virtual Adapter failed (rc=%d)\n",
2922 gen_crq->cmd);
Thomas Falcon9888d7b2016-10-27 12:28:51 -05002923 schedule_work(&adapter->ibmvnic_xport);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002924 }
2925 return;
2926 case IBMVNIC_CRQ_CMD_RSP:
2927 break;
2928 default:
2929 dev_err(dev, "Got an invalid msg type 0x%02x\n",
2930 gen_crq->first);
2931 return;
2932 }
2933
2934 switch (gen_crq->cmd) {
2935 case VERSION_EXCHANGE_RSP:
2936 rc = crq->version_exchange_rsp.rc.code;
2937 if (rc) {
2938 dev_err(dev, "Error %ld in VERSION_EXCHG_RSP\n", rc);
2939 break;
2940 }
2941 dev_info(dev, "Partner protocol version is %d\n",
2942 crq->version_exchange_rsp.version);
2943 if (be16_to_cpu(crq->version_exchange_rsp.version) <
2944 ibmvnic_version)
2945 ibmvnic_version =
2946 be16_to_cpu(crq->version_exchange_rsp.version);
2947 send_cap_queries(adapter);
2948 break;
2949 case QUERY_CAPABILITY_RSP:
2950 handle_query_cap_rsp(crq, adapter);
2951 break;
2952 case QUERY_MAP_RSP:
2953 handle_query_map_rsp(crq, adapter);
2954 break;
2955 case REQUEST_MAP_RSP:
2956 handle_request_map_rsp(crq, adapter);
2957 break;
2958 case REQUEST_UNMAP_RSP:
2959 handle_request_unmap_rsp(crq, adapter);
2960 break;
2961 case REQUEST_CAPABILITY_RSP:
2962 handle_request_cap_rsp(crq, adapter);
2963 break;
2964 case LOGIN_RSP:
2965 netdev_dbg(netdev, "Got Login Response\n");
2966 handle_login_rsp(crq, adapter);
2967 break;
2968 case LOGICAL_LINK_STATE_RSP:
2969 netdev_dbg(netdev, "Got Logical Link State Response\n");
2970 adapter->logical_link_state =
2971 crq->logical_link_state_rsp.link_state;
2972 break;
2973 case LINK_STATE_INDICATION:
2974 netdev_dbg(netdev, "Got Logical Link State Indication\n");
2975 adapter->phys_link_state =
2976 crq->link_state_indication.phys_link_state;
2977 adapter->logical_link_state =
2978 crq->link_state_indication.logical_link_state;
2979 break;
2980 case CHANGE_MAC_ADDR_RSP:
2981 netdev_dbg(netdev, "Got MAC address change Response\n");
2982 handle_change_mac_rsp(crq, adapter);
2983 break;
2984 case ERROR_INDICATION:
2985 netdev_dbg(netdev, "Got Error Indication\n");
2986 handle_error_indication(crq, adapter);
2987 break;
2988 case REQUEST_ERROR_RSP:
2989 netdev_dbg(netdev, "Got Error Detail Response\n");
2990 handle_error_info_rsp(crq, adapter);
2991 break;
2992 case REQUEST_STATISTICS_RSP:
2993 netdev_dbg(netdev, "Got Statistics Response\n");
2994 complete(&adapter->stats_done);
2995 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002996 case QUERY_IP_OFFLOAD_RSP:
2997 netdev_dbg(netdev, "Got Query IP offload Response\n");
2998 handle_query_ip_offload_rsp(adapter);
2999 break;
3000 case MULTICAST_CTRL_RSP:
3001 netdev_dbg(netdev, "Got multicast control Response\n");
3002 break;
3003 case CONTROL_IP_OFFLOAD_RSP:
3004 netdev_dbg(netdev, "Got Control IP offload Response\n");
3005 dma_unmap_single(dev, adapter->ip_offload_ctrl_tok,
3006 sizeof(adapter->ip_offload_ctrl),
3007 DMA_TO_DEVICE);
John Allenbd0b6722017-03-17 17:13:40 -05003008 complete(&adapter->init_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003009 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003010 case COLLECT_FW_TRACE_RSP:
3011 netdev_dbg(netdev, "Got Collect firmware trace Response\n");
3012 complete(&adapter->fw_done);
3013 break;
3014 default:
3015 netdev_err(netdev, "Got an invalid cmd type 0x%02x\n",
3016 gen_crq->cmd);
3017 }
3018}
3019
3020static irqreturn_t ibmvnic_interrupt(int irq, void *instance)
3021{
3022 struct ibmvnic_adapter *adapter = instance;
Thomas Falcon6c267b32017-02-15 12:17:58 -06003023 unsigned long flags;
3024
3025 spin_lock_irqsave(&adapter->crq.lock, flags);
3026 vio_disable_interrupts(adapter->vdev);
3027 tasklet_schedule(&adapter->tasklet);
3028 spin_unlock_irqrestore(&adapter->crq.lock, flags);
3029 return IRQ_HANDLED;
3030}
3031
3032static void ibmvnic_tasklet(void *data)
3033{
3034 struct ibmvnic_adapter *adapter = data;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003035 struct ibmvnic_crq_queue *queue = &adapter->crq;
3036 struct vio_dev *vdev = adapter->vdev;
3037 union ibmvnic_crq *crq;
3038 unsigned long flags;
3039 bool done = false;
3040
3041 spin_lock_irqsave(&queue->lock, flags);
3042 vio_disable_interrupts(vdev);
3043 while (!done) {
3044 /* Pull all the valid messages off the CRQ */
3045 while ((crq = ibmvnic_next_crq(adapter)) != NULL) {
3046 ibmvnic_handle_crq(crq, adapter);
3047 crq->generic.first = 0;
3048 }
3049 vio_enable_interrupts(vdev);
3050 crq = ibmvnic_next_crq(adapter);
3051 if (crq) {
3052 vio_disable_interrupts(vdev);
3053 ibmvnic_handle_crq(crq, adapter);
3054 crq->generic.first = 0;
3055 } else {
Thomas Falcon249168a2017-02-15 12:18:00 -06003056 /* remain in tasklet until all
3057 * capabilities responses are received
3058 */
3059 if (!adapter->wait_capability)
3060 done = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003061 }
3062 }
Thomas Falcon249168a2017-02-15 12:18:00 -06003063 /* if capabilities CRQ's were sent in this tasklet, the following
3064 * tasklet must wait until all responses are received
3065 */
3066 if (atomic_read(&adapter->running_cap_crqs) != 0)
3067 adapter->wait_capability = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003068 spin_unlock_irqrestore(&queue->lock, flags);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003069}
3070
3071static int ibmvnic_reenable_crq_queue(struct ibmvnic_adapter *adapter)
3072{
3073 struct vio_dev *vdev = adapter->vdev;
3074 int rc;
3075
3076 do {
3077 rc = plpar_hcall_norets(H_ENABLE_CRQ, vdev->unit_address);
3078 } while (rc == H_IN_PROGRESS || rc == H_BUSY || H_IS_LONG_BUSY(rc));
3079
3080 if (rc)
3081 dev_err(&vdev->dev, "Error enabling adapter (rc=%d)\n", rc);
3082
3083 return rc;
3084}
3085
3086static int ibmvnic_reset_crq(struct ibmvnic_adapter *adapter)
3087{
3088 struct ibmvnic_crq_queue *crq = &adapter->crq;
3089 struct device *dev = &adapter->vdev->dev;
3090 struct vio_dev *vdev = adapter->vdev;
3091 int rc;
3092
3093 /* Close the CRQ */
3094 do {
3095 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
3096 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
3097
3098 /* Clean out the queue */
3099 memset(crq->msgs, 0, PAGE_SIZE);
3100 crq->cur = 0;
3101
3102 /* And re-open it again */
3103 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
3104 crq->msg_token, PAGE_SIZE);
3105
3106 if (rc == H_CLOSED)
3107 /* Adapter is good, but other end is not ready */
3108 dev_warn(dev, "Partner adapter not ready\n");
3109 else if (rc != 0)
3110 dev_warn(dev, "Couldn't register crq (rc=%d)\n", rc);
3111
3112 return rc;
3113}
3114
Nathan Fontenotf9928872017-03-30 02:48:54 -04003115static void release_crq_queue(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003116{
3117 struct ibmvnic_crq_queue *crq = &adapter->crq;
3118 struct vio_dev *vdev = adapter->vdev;
3119 long rc;
3120
Nathan Fontenotf9928872017-03-30 02:48:54 -04003121 if (!crq->msgs)
3122 return;
3123
Thomas Falcon032c5e82015-12-21 11:26:06 -06003124 netdev_dbg(adapter->netdev, "Releasing CRQ\n");
3125 free_irq(vdev->irq, adapter);
Thomas Falcon6c267b32017-02-15 12:17:58 -06003126 tasklet_kill(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003127 do {
3128 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
3129 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
3130
3131 dma_unmap_single(&vdev->dev, crq->msg_token, PAGE_SIZE,
3132 DMA_BIDIRECTIONAL);
3133 free_page((unsigned long)crq->msgs);
Nathan Fontenotf9928872017-03-30 02:48:54 -04003134 crq->msgs = NULL;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003135}
3136
Nathan Fontenotf9928872017-03-30 02:48:54 -04003137static int init_crq_queue(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003138{
3139 struct ibmvnic_crq_queue *crq = &adapter->crq;
3140 struct device *dev = &adapter->vdev->dev;
3141 struct vio_dev *vdev = adapter->vdev;
3142 int rc, retrc = -ENOMEM;
3143
Nathan Fontenotf9928872017-03-30 02:48:54 -04003144 if (crq->msgs)
3145 return 0;
3146
Thomas Falcon032c5e82015-12-21 11:26:06 -06003147 crq->msgs = (union ibmvnic_crq *)get_zeroed_page(GFP_KERNEL);
3148 /* Should we allocate more than one page? */
3149
3150 if (!crq->msgs)
3151 return -ENOMEM;
3152
3153 crq->size = PAGE_SIZE / sizeof(*crq->msgs);
3154 crq->msg_token = dma_map_single(dev, crq->msgs, PAGE_SIZE,
3155 DMA_BIDIRECTIONAL);
3156 if (dma_mapping_error(dev, crq->msg_token))
3157 goto map_failed;
3158
3159 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
3160 crq->msg_token, PAGE_SIZE);
3161
3162 if (rc == H_RESOURCE)
3163 /* maybe kexecing and resource is busy. try a reset */
3164 rc = ibmvnic_reset_crq(adapter);
3165 retrc = rc;
3166
3167 if (rc == H_CLOSED) {
3168 dev_warn(dev, "Partner adapter not ready\n");
3169 } else if (rc) {
3170 dev_warn(dev, "Error %d opening adapter\n", rc);
3171 goto reg_crq_failed;
3172 }
3173
3174 retrc = 0;
3175
Thomas Falcon6c267b32017-02-15 12:17:58 -06003176 tasklet_init(&adapter->tasklet, (void *)ibmvnic_tasklet,
3177 (unsigned long)adapter);
3178
Thomas Falcon032c5e82015-12-21 11:26:06 -06003179 netdev_dbg(adapter->netdev, "registering irq 0x%x\n", vdev->irq);
3180 rc = request_irq(vdev->irq, ibmvnic_interrupt, 0, IBMVNIC_NAME,
3181 adapter);
3182 if (rc) {
3183 dev_err(dev, "Couldn't register irq 0x%x. rc=%d\n",
3184 vdev->irq, rc);
3185 goto req_irq_failed;
3186 }
3187
3188 rc = vio_enable_interrupts(vdev);
3189 if (rc) {
3190 dev_err(dev, "Error %d enabling interrupts\n", rc);
3191 goto req_irq_failed;
3192 }
3193
3194 crq->cur = 0;
3195 spin_lock_init(&crq->lock);
3196
3197 return retrc;
3198
3199req_irq_failed:
Thomas Falcon6c267b32017-02-15 12:17:58 -06003200 tasklet_kill(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003201 do {
3202 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
3203 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
3204reg_crq_failed:
3205 dma_unmap_single(dev, crq->msg_token, PAGE_SIZE, DMA_BIDIRECTIONAL);
3206map_failed:
3207 free_page((unsigned long)crq->msgs);
Nathan Fontenotf9928872017-03-30 02:48:54 -04003208 crq->msgs = NULL;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003209 return retrc;
3210}
3211
Thomas Falcon65dc6892016-07-06 15:35:18 -05003212static void handle_crq_init_rsp(struct work_struct *work)
3213{
3214 struct ibmvnic_adapter *adapter = container_of(work,
3215 struct ibmvnic_adapter,
3216 vnic_crq_init);
3217 struct device *dev = &adapter->vdev->dev;
3218 struct net_device *netdev = adapter->netdev;
3219 unsigned long timeout = msecs_to_jiffies(30000);
Thomas Falcondfad09a2016-08-18 11:37:51 -05003220 bool restart = false;
Thomas Falcon65dc6892016-07-06 15:35:18 -05003221 int rc;
3222
Thomas Falcondfad09a2016-08-18 11:37:51 -05003223 if (adapter->failover) {
3224 release_sub_crqs(adapter);
3225 if (netif_running(netdev)) {
3226 netif_tx_disable(netdev);
3227 ibmvnic_close(netdev);
3228 restart = true;
3229 }
3230 }
3231
Thomas Falcon65dc6892016-07-06 15:35:18 -05003232 reinit_completion(&adapter->init_done);
Nathan Fontenotdb5d0b52017-02-10 13:45:05 -05003233 send_version_xchg(adapter);
Thomas Falcon65dc6892016-07-06 15:35:18 -05003234 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
3235 dev_err(dev, "Passive init timeout\n");
3236 goto task_failed;
3237 }
3238
Thomas Falconf39f0d12017-02-14 10:22:59 -06003239 netdev->mtu = adapter->req_mtu - ETH_HLEN;
Thomas Falcon65dc6892016-07-06 15:35:18 -05003240
Thomas Falcondfad09a2016-08-18 11:37:51 -05003241 if (adapter->failover) {
3242 adapter->failover = false;
3243 if (restart) {
3244 rc = ibmvnic_open(netdev);
3245 if (rc)
3246 goto restart_failed;
3247 }
3248 netif_carrier_on(netdev);
3249 return;
3250 }
3251
Thomas Falcon65dc6892016-07-06 15:35:18 -05003252 rc = register_netdev(netdev);
3253 if (rc) {
3254 dev_err(dev,
3255 "failed to register netdev rc=%d\n", rc);
3256 goto register_failed;
3257 }
3258 dev_info(dev, "ibmvnic registered\n");
3259
3260 return;
3261
Thomas Falcondfad09a2016-08-18 11:37:51 -05003262restart_failed:
3263 dev_err(dev, "Failed to restart ibmvnic, rc=%d\n", rc);
Thomas Falcon65dc6892016-07-06 15:35:18 -05003264register_failed:
3265 release_sub_crqs(adapter);
3266task_failed:
3267 dev_err(dev, "Passive initialization was not successful\n");
3268}
3269
John Allenf6ef6402017-03-17 17:13:42 -05003270static int ibmvnic_init(struct ibmvnic_adapter *adapter)
3271{
3272 struct device *dev = &adapter->vdev->dev;
3273 unsigned long timeout = msecs_to_jiffies(30000);
John Allenf6ef6402017-03-17 17:13:42 -05003274 int rc;
3275
Nathan Fontenotf9928872017-03-30 02:48:54 -04003276 rc = init_crq_queue(adapter);
John Allenf6ef6402017-03-17 17:13:42 -05003277 if (rc) {
3278 dev_err(dev, "Couldn't initialize crq. rc=%d\n", rc);
3279 return rc;
3280 }
3281
3282 adapter->stats_token = dma_map_single(dev, &adapter->stats,
3283 sizeof(struct ibmvnic_statistics),
3284 DMA_FROM_DEVICE);
3285 if (dma_mapping_error(dev, adapter->stats_token)) {
Nathan Fontenotf9928872017-03-30 02:48:54 -04003286 release_crq_queue(adapter);
John Allenf6ef6402017-03-17 17:13:42 -05003287 dev_err(dev, "Couldn't map stats buffer\n");
3288 return -ENOMEM;
3289 }
3290
John Allenf6ef6402017-03-17 17:13:42 -05003291 init_completion(&adapter->init_done);
3292 ibmvnic_send_crq_init(adapter);
3293 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
3294 dev_err(dev, "Initialization sequence timed out\n");
Nathan Fontenotf9928872017-03-30 02:48:54 -04003295 release_crq_queue(adapter);
John Allenf6ef6402017-03-17 17:13:42 -05003296 return -1;
3297 }
3298
3299 return 0;
3300}
3301
Thomas Falcon032c5e82015-12-21 11:26:06 -06003302static int ibmvnic_probe(struct vio_dev *dev, const struct vio_device_id *id)
3303{
3304 struct ibmvnic_adapter *adapter;
3305 struct net_device *netdev;
3306 unsigned char *mac_addr_p;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003307 int rc;
3308
3309 dev_dbg(&dev->dev, "entering ibmvnic_probe for UA 0x%x\n",
3310 dev->unit_address);
3311
3312 mac_addr_p = (unsigned char *)vio_get_attribute(dev,
3313 VETH_MAC_ADDR, NULL);
3314 if (!mac_addr_p) {
3315 dev_err(&dev->dev,
3316 "(%s:%3.3d) ERROR: Can't find MAC_ADDR attribute\n",
3317 __FILE__, __LINE__);
3318 return 0;
3319 }
3320
3321 netdev = alloc_etherdev_mq(sizeof(struct ibmvnic_adapter),
3322 IBMVNIC_MAX_TX_QUEUES);
3323 if (!netdev)
3324 return -ENOMEM;
3325
3326 adapter = netdev_priv(netdev);
3327 dev_set_drvdata(&dev->dev, netdev);
3328 adapter->vdev = dev;
3329 adapter->netdev = netdev;
Thomas Falcondfad09a2016-08-18 11:37:51 -05003330 adapter->failover = false;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003331
3332 ether_addr_copy(adapter->mac_addr, mac_addr_p);
3333 ether_addr_copy(netdev->dev_addr, adapter->mac_addr);
3334 netdev->irq = dev->irq;
3335 netdev->netdev_ops = &ibmvnic_netdev_ops;
3336 netdev->ethtool_ops = &ibmvnic_ethtool_ops;
3337 SET_NETDEV_DEV(netdev, &dev->dev);
3338
Thomas Falcon65dc6892016-07-06 15:35:18 -05003339 INIT_WORK(&adapter->vnic_crq_init, handle_crq_init_rsp);
Thomas Falcon9888d7b2016-10-27 12:28:51 -05003340 INIT_WORK(&adapter->ibmvnic_xport, ibmvnic_xport_event);
Thomas Falcon65dc6892016-07-06 15:35:18 -05003341
Thomas Falcon032c5e82015-12-21 11:26:06 -06003342 spin_lock_init(&adapter->stats_lock);
3343
Thomas Falcon032c5e82015-12-21 11:26:06 -06003344 INIT_LIST_HEAD(&adapter->errors);
3345 INIT_LIST_HEAD(&adapter->inflight);
3346 spin_lock_init(&adapter->error_list_lock);
3347 spin_lock_init(&adapter->inflight_lock);
3348
John Allenf6ef6402017-03-17 17:13:42 -05003349 rc = ibmvnic_init(adapter);
3350 if (rc) {
3351 free_netdev(netdev);
3352 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003353 }
3354
Thomas Falconf39f0d12017-02-14 10:22:59 -06003355 netdev->mtu = adapter->req_mtu - ETH_HLEN;
John Allenea5509f2017-03-17 17:13:43 -05003356 adapter->is_closed = false;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003357
3358 rc = register_netdev(netdev);
3359 if (rc) {
3360 dev_err(&dev->dev, "failed to register netdev rc=%d\n", rc);
John Allenf6ef6402017-03-17 17:13:42 -05003361 free_netdev(netdev);
3362 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003363 }
3364 dev_info(&dev->dev, "ibmvnic registered\n");
3365
3366 return 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003367}
3368
3369static int ibmvnic_remove(struct vio_dev *dev)
3370{
3371 struct net_device *netdev = dev_get_drvdata(&dev->dev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003372
3373 unregister_netdev(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003374 free_netdev(netdev);
3375 dev_set_drvdata(&dev->dev, NULL);
3376
3377 return 0;
3378}
3379
3380static unsigned long ibmvnic_get_desired_dma(struct vio_dev *vdev)
3381{
3382 struct net_device *netdev = dev_get_drvdata(&vdev->dev);
3383 struct ibmvnic_adapter *adapter;
3384 struct iommu_table *tbl;
3385 unsigned long ret = 0;
3386 int i;
3387
3388 tbl = get_iommu_table_base(&vdev->dev);
3389
3390 /* netdev inits at probe time along with the structures we need below*/
3391 if (!netdev)
3392 return IOMMU_PAGE_ALIGN(IBMVNIC_IO_ENTITLEMENT_DEFAULT, tbl);
3393
3394 adapter = netdev_priv(netdev);
3395
3396 ret += PAGE_SIZE; /* the crq message queue */
3397 ret += adapter->bounce_buffer_size;
3398 ret += IOMMU_PAGE_ALIGN(sizeof(struct ibmvnic_statistics), tbl);
3399
3400 for (i = 0; i < adapter->req_tx_queues + adapter->req_rx_queues; i++)
3401 ret += 4 * PAGE_SIZE; /* the scrq message queue */
3402
3403 for (i = 0; i < be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
3404 i++)
3405 ret += adapter->rx_pool[i].size *
3406 IOMMU_PAGE_ALIGN(adapter->rx_pool[i].buff_size, tbl);
3407
3408 return ret;
3409}
3410
3411static int ibmvnic_resume(struct device *dev)
3412{
3413 struct net_device *netdev = dev_get_drvdata(dev);
3414 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
3415 int i;
3416
3417 /* kick the interrupt handlers just in case we lost an interrupt */
3418 for (i = 0; i < adapter->req_rx_queues; i++)
3419 ibmvnic_interrupt_rx(adapter->rx_scrq[i]->irq,
3420 adapter->rx_scrq[i]);
3421
3422 return 0;
3423}
3424
3425static struct vio_device_id ibmvnic_device_table[] = {
3426 {"network", "IBM,vnic"},
3427 {"", "" }
3428};
3429MODULE_DEVICE_TABLE(vio, ibmvnic_device_table);
3430
3431static const struct dev_pm_ops ibmvnic_pm_ops = {
3432 .resume = ibmvnic_resume
3433};
3434
3435static struct vio_driver ibmvnic_driver = {
3436 .id_table = ibmvnic_device_table,
3437 .probe = ibmvnic_probe,
3438 .remove = ibmvnic_remove,
3439 .get_desired_dma = ibmvnic_get_desired_dma,
3440 .name = ibmvnic_driver_name,
3441 .pm = &ibmvnic_pm_ops,
3442};
3443
3444/* module functions */
3445static int __init ibmvnic_module_init(void)
3446{
3447 pr_info("%s: %s %s\n", ibmvnic_driver_name, ibmvnic_driver_string,
3448 IBMVNIC_DRIVER_VERSION);
3449
3450 return vio_register_driver(&ibmvnic_driver);
3451}
3452
3453static void __exit ibmvnic_module_exit(void)
3454{
3455 vio_unregister_driver(&ibmvnic_driver);
3456}
3457
3458module_init(ibmvnic_module_init);
3459module_exit(ibmvnic_module_exit);