blob: c3e5305604c8412bf889172133f2de3379f745fd [file] [log] [blame]
Thomas Falcon032c5e82015-12-21 11:26:06 -06001/**************************************************************************/
2/* */
3/* IBM System i and System p Virtual NIC Device Driver */
4/* Copyright (C) 2014 IBM Corp. */
5/* Santiago Leon (santi_leon@yahoo.com) */
6/* Thomas Falcon (tlfalcon@linux.vnet.ibm.com) */
7/* John Allen (jallen@linux.vnet.ibm.com) */
8/* */
9/* This program is free software; you can redistribute it and/or modify */
10/* it under the terms of the GNU General Public License as published by */
11/* the Free Software Foundation; either version 2 of the License, or */
12/* (at your option) any later version. */
13/* */
14/* This program is distributed in the hope that it will be useful, */
15/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
16/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
17/* GNU General Public License for more details. */
18/* */
19/* You should have received a copy of the GNU General Public License */
20/* along with this program. */
21/* */
22/* This module contains the implementation of a virtual ethernet device */
23/* for use with IBM i/p Series LPAR Linux. It utilizes the logical LAN */
24/* option of the RS/6000 Platform Architecture to interface with virtual */
25/* ethernet NICs that are presented to the partition by the hypervisor. */
26/* */
27/* Messages are passed between the VNIC driver and the VNIC server using */
28/* Command/Response Queues (CRQs) and sub CRQs (sCRQs). CRQs are used to */
29/* issue and receive commands that initiate communication with the server */
30/* on driver initialization. Sub CRQs (sCRQs) are similar to CRQs, but */
31/* are used by the driver to notify the server that a packet is */
32/* ready for transmission or that a buffer has been added to receive a */
33/* packet. Subsequently, sCRQs are used by the server to notify the */
34/* driver that a packet transmission has been completed or that a packet */
35/* has been received and placed in a waiting buffer. */
36/* */
37/* In lieu of a more conventional "on-the-fly" DMA mapping strategy in */
38/* which skbs are DMA mapped and immediately unmapped when the transmit */
39/* or receive has been completed, the VNIC driver is required to use */
40/* "long term mapping". This entails that large, continuous DMA mapped */
41/* buffers are allocated on driver initialization and these buffers are */
42/* then continuously reused to pass skbs to and from the VNIC server. */
43/* */
44/**************************************************************************/
45
46#include <linux/module.h>
47#include <linux/moduleparam.h>
48#include <linux/types.h>
49#include <linux/errno.h>
50#include <linux/completion.h>
51#include <linux/ioport.h>
52#include <linux/dma-mapping.h>
53#include <linux/kernel.h>
54#include <linux/netdevice.h>
55#include <linux/etherdevice.h>
56#include <linux/skbuff.h>
57#include <linux/init.h>
58#include <linux/delay.h>
59#include <linux/mm.h>
60#include <linux/ethtool.h>
61#include <linux/proc_fs.h>
62#include <linux/in.h>
63#include <linux/ip.h>
Thomas Falconad7775d2016-04-01 17:20:34 -050064#include <linux/ipv6.h>
Thomas Falcon032c5e82015-12-21 11:26:06 -060065#include <linux/irq.h>
66#include <linux/kthread.h>
67#include <linux/seq_file.h>
Thomas Falcon032c5e82015-12-21 11:26:06 -060068#include <linux/interrupt.h>
69#include <net/net_namespace.h>
70#include <asm/hvcall.h>
71#include <linux/atomic.h>
72#include <asm/vio.h>
73#include <asm/iommu.h>
74#include <linux/uaccess.h>
75#include <asm/firmware.h>
Thomas Falcon65dc6892016-07-06 15:35:18 -050076#include <linux/workqueue.h>
Thomas Falcon032c5e82015-12-21 11:26:06 -060077
78#include "ibmvnic.h"
79
80static const char ibmvnic_driver_name[] = "ibmvnic";
81static const char ibmvnic_driver_string[] = "IBM System i/p Virtual NIC Driver";
82
83MODULE_AUTHOR("Santiago Leon <santi_leon@yahoo.com>");
84MODULE_DESCRIPTION("IBM System i/p Virtual NIC Driver");
85MODULE_LICENSE("GPL");
86MODULE_VERSION(IBMVNIC_DRIVER_VERSION);
87
88static int ibmvnic_version = IBMVNIC_INITIAL_VERSION;
89static int ibmvnic_remove(struct vio_dev *);
90static void release_sub_crqs(struct ibmvnic_adapter *);
Thomas Falconea22d512016-07-06 15:35:17 -050091static void release_sub_crqs_no_irqs(struct ibmvnic_adapter *);
Thomas Falcon032c5e82015-12-21 11:26:06 -060092static int ibmvnic_reset_crq(struct ibmvnic_adapter *);
93static int ibmvnic_send_crq_init(struct ibmvnic_adapter *);
94static int ibmvnic_reenable_crq_queue(struct ibmvnic_adapter *);
95static int ibmvnic_send_crq(struct ibmvnic_adapter *, union ibmvnic_crq *);
96static int send_subcrq(struct ibmvnic_adapter *adapter, u64 remote_handle,
97 union sub_crq *sub_crq);
Thomas Falconad7775d2016-04-01 17:20:34 -050098static int send_subcrq_indirect(struct ibmvnic_adapter *, u64, u64, u64);
Thomas Falcon032c5e82015-12-21 11:26:06 -060099static irqreturn_t ibmvnic_interrupt_rx(int irq, void *instance);
100static int enable_scrq_irq(struct ibmvnic_adapter *,
101 struct ibmvnic_sub_crq_queue *);
102static int disable_scrq_irq(struct ibmvnic_adapter *,
103 struct ibmvnic_sub_crq_queue *);
104static int pending_scrq(struct ibmvnic_adapter *,
105 struct ibmvnic_sub_crq_queue *);
106static union sub_crq *ibmvnic_next_scrq(struct ibmvnic_adapter *,
107 struct ibmvnic_sub_crq_queue *);
108static int ibmvnic_poll(struct napi_struct *napi, int data);
109static void send_map_query(struct ibmvnic_adapter *adapter);
110static void send_request_map(struct ibmvnic_adapter *, dma_addr_t, __be32, u8);
111static void send_request_unmap(struct ibmvnic_adapter *, u8);
John Allenbd0b6722017-03-17 17:13:40 -0500112static void send_login(struct ibmvnic_adapter *adapter);
113static void send_cap_queries(struct ibmvnic_adapter *adapter);
114static int init_sub_crq_irqs(struct ibmvnic_adapter *adapter);
John Allenea5509f2017-03-17 17:13:43 -0500115static int ibmvnic_init(struct ibmvnic_adapter *);
Nathan Fontenotf9928872017-03-30 02:48:54 -0400116static void release_crq_queue(struct ibmvnic_adapter *);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600117
118struct ibmvnic_stat {
119 char name[ETH_GSTRING_LEN];
120 int offset;
121};
122
123#define IBMVNIC_STAT_OFF(stat) (offsetof(struct ibmvnic_adapter, stats) + \
124 offsetof(struct ibmvnic_statistics, stat))
125#define IBMVNIC_GET_STAT(a, off) (*((u64 *)(((unsigned long)(a)) + off)))
126
127static const struct ibmvnic_stat ibmvnic_stats[] = {
128 {"rx_packets", IBMVNIC_STAT_OFF(rx_packets)},
129 {"rx_bytes", IBMVNIC_STAT_OFF(rx_bytes)},
130 {"tx_packets", IBMVNIC_STAT_OFF(tx_packets)},
131 {"tx_bytes", IBMVNIC_STAT_OFF(tx_bytes)},
132 {"ucast_tx_packets", IBMVNIC_STAT_OFF(ucast_tx_packets)},
133 {"ucast_rx_packets", IBMVNIC_STAT_OFF(ucast_rx_packets)},
134 {"mcast_tx_packets", IBMVNIC_STAT_OFF(mcast_tx_packets)},
135 {"mcast_rx_packets", IBMVNIC_STAT_OFF(mcast_rx_packets)},
136 {"bcast_tx_packets", IBMVNIC_STAT_OFF(bcast_tx_packets)},
137 {"bcast_rx_packets", IBMVNIC_STAT_OFF(bcast_rx_packets)},
138 {"align_errors", IBMVNIC_STAT_OFF(align_errors)},
139 {"fcs_errors", IBMVNIC_STAT_OFF(fcs_errors)},
140 {"single_collision_frames", IBMVNIC_STAT_OFF(single_collision_frames)},
141 {"multi_collision_frames", IBMVNIC_STAT_OFF(multi_collision_frames)},
142 {"sqe_test_errors", IBMVNIC_STAT_OFF(sqe_test_errors)},
143 {"deferred_tx", IBMVNIC_STAT_OFF(deferred_tx)},
144 {"late_collisions", IBMVNIC_STAT_OFF(late_collisions)},
145 {"excess_collisions", IBMVNIC_STAT_OFF(excess_collisions)},
146 {"internal_mac_tx_errors", IBMVNIC_STAT_OFF(internal_mac_tx_errors)},
147 {"carrier_sense", IBMVNIC_STAT_OFF(carrier_sense)},
148 {"too_long_frames", IBMVNIC_STAT_OFF(too_long_frames)},
149 {"internal_mac_rx_errors", IBMVNIC_STAT_OFF(internal_mac_rx_errors)},
150};
151
152static long h_reg_sub_crq(unsigned long unit_address, unsigned long token,
153 unsigned long length, unsigned long *number,
154 unsigned long *irq)
155{
156 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
157 long rc;
158
159 rc = plpar_hcall(H_REG_SUB_CRQ, retbuf, unit_address, token, length);
160 *number = retbuf[0];
161 *irq = retbuf[1];
162
163 return rc;
164}
165
166/* net_device_ops functions */
167
168static void init_rx_pool(struct ibmvnic_adapter *adapter,
169 struct ibmvnic_rx_pool *rx_pool, int num, int index,
170 int buff_size, int active)
171{
172 netdev_dbg(adapter->netdev,
173 "Initializing rx_pool %d, %d buffs, %d bytes each\n",
174 index, num, buff_size);
175 rx_pool->size = num;
176 rx_pool->index = index;
177 rx_pool->buff_size = buff_size;
178 rx_pool->active = active;
179}
180
181static int alloc_long_term_buff(struct ibmvnic_adapter *adapter,
182 struct ibmvnic_long_term_buff *ltb, int size)
183{
184 struct device *dev = &adapter->vdev->dev;
185
186 ltb->size = size;
187 ltb->buff = dma_alloc_coherent(dev, ltb->size, &ltb->addr,
188 GFP_KERNEL);
189
190 if (!ltb->buff) {
191 dev_err(dev, "Couldn't alloc long term buffer\n");
192 return -ENOMEM;
193 }
194 ltb->map_id = adapter->map_id;
195 adapter->map_id++;
Nathan Fontenotdb5d0b52017-02-10 13:45:05 -0500196
197 init_completion(&adapter->fw_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600198 send_request_map(adapter, ltb->addr,
199 ltb->size, ltb->map_id);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600200 wait_for_completion(&adapter->fw_done);
201 return 0;
202}
203
204static void free_long_term_buff(struct ibmvnic_adapter *adapter,
205 struct ibmvnic_long_term_buff *ltb)
206{
207 struct device *dev = &adapter->vdev->dev;
208
209 dma_free_coherent(dev, ltb->size, ltb->buff, ltb->addr);
Thomas Falcondfad09a2016-08-18 11:37:51 -0500210 if (!adapter->failover)
211 send_request_unmap(adapter, ltb->map_id);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600212}
213
214static int alloc_rx_pool(struct ibmvnic_adapter *adapter,
215 struct ibmvnic_rx_pool *pool)
216{
217 struct device *dev = &adapter->vdev->dev;
218 int i;
219
220 pool->free_map = kcalloc(pool->size, sizeof(int), GFP_KERNEL);
221 if (!pool->free_map)
222 return -ENOMEM;
223
224 pool->rx_buff = kcalloc(pool->size, sizeof(struct ibmvnic_rx_buff),
225 GFP_KERNEL);
226
227 if (!pool->rx_buff) {
228 dev_err(dev, "Couldn't alloc rx buffers\n");
229 kfree(pool->free_map);
230 return -ENOMEM;
231 }
232
233 if (alloc_long_term_buff(adapter, &pool->long_term_buff,
234 pool->size * pool->buff_size)) {
235 kfree(pool->free_map);
236 kfree(pool->rx_buff);
237 return -ENOMEM;
238 }
239
240 for (i = 0; i < pool->size; ++i)
241 pool->free_map[i] = i;
242
243 atomic_set(&pool->available, 0);
244 pool->next_alloc = 0;
245 pool->next_free = 0;
246
247 return 0;
248}
249
250static void replenish_rx_pool(struct ibmvnic_adapter *adapter,
251 struct ibmvnic_rx_pool *pool)
252{
253 int count = pool->size - atomic_read(&pool->available);
254 struct device *dev = &adapter->vdev->dev;
255 int buffers_added = 0;
256 unsigned long lpar_rc;
257 union sub_crq sub_crq;
258 struct sk_buff *skb;
259 unsigned int offset;
260 dma_addr_t dma_addr;
261 unsigned char *dst;
262 u64 *handle_array;
263 int shift = 0;
264 int index;
265 int i;
266
267 handle_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
268 be32_to_cpu(adapter->login_rsp_buf->
269 off_rxadd_subcrqs));
270
271 for (i = 0; i < count; ++i) {
272 skb = alloc_skb(pool->buff_size, GFP_ATOMIC);
273 if (!skb) {
274 dev_err(dev, "Couldn't replenish rx buff\n");
275 adapter->replenish_no_mem++;
276 break;
277 }
278
279 index = pool->free_map[pool->next_free];
280
281 if (pool->rx_buff[index].skb)
282 dev_err(dev, "Inconsistent free_map!\n");
283
284 /* Copy the skb to the long term mapped DMA buffer */
285 offset = index * pool->buff_size;
286 dst = pool->long_term_buff.buff + offset;
287 memset(dst, 0, pool->buff_size);
288 dma_addr = pool->long_term_buff.addr + offset;
289 pool->rx_buff[index].data = dst;
290
291 pool->free_map[pool->next_free] = IBMVNIC_INVALID_MAP;
292 pool->rx_buff[index].dma = dma_addr;
293 pool->rx_buff[index].skb = skb;
294 pool->rx_buff[index].pool_index = pool->index;
295 pool->rx_buff[index].size = pool->buff_size;
296
297 memset(&sub_crq, 0, sizeof(sub_crq));
298 sub_crq.rx_add.first = IBMVNIC_CRQ_CMD;
299 sub_crq.rx_add.correlator =
300 cpu_to_be64((u64)&pool->rx_buff[index]);
301 sub_crq.rx_add.ioba = cpu_to_be32(dma_addr);
302 sub_crq.rx_add.map_id = pool->long_term_buff.map_id;
303
304 /* The length field of the sCRQ is defined to be 24 bits so the
305 * buffer size needs to be left shifted by a byte before it is
306 * converted to big endian to prevent the last byte from being
307 * truncated.
308 */
309#ifdef __LITTLE_ENDIAN__
310 shift = 8;
311#endif
312 sub_crq.rx_add.len = cpu_to_be32(pool->buff_size << shift);
313
314 lpar_rc = send_subcrq(adapter, handle_array[pool->index],
315 &sub_crq);
316 if (lpar_rc != H_SUCCESS)
317 goto failure;
318
319 buffers_added++;
320 adapter->replenish_add_buff_success++;
321 pool->next_free = (pool->next_free + 1) % pool->size;
322 }
323 atomic_add(buffers_added, &pool->available);
324 return;
325
326failure:
327 dev_info(dev, "replenish pools failure\n");
328 pool->free_map[pool->next_free] = index;
329 pool->rx_buff[index].skb = NULL;
330 if (!dma_mapping_error(dev, dma_addr))
331 dma_unmap_single(dev, dma_addr, pool->buff_size,
332 DMA_FROM_DEVICE);
333
334 dev_kfree_skb_any(skb);
335 adapter->replenish_add_buff_failure++;
336 atomic_add(buffers_added, &pool->available);
337}
338
339static void replenish_pools(struct ibmvnic_adapter *adapter)
340{
341 int i;
342
343 if (adapter->migrated)
344 return;
345
346 adapter->replenish_task_cycles++;
347 for (i = 0; i < be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
348 i++) {
349 if (adapter->rx_pool[i].active)
350 replenish_rx_pool(adapter, &adapter->rx_pool[i]);
351 }
352}
353
354static void free_rx_pool(struct ibmvnic_adapter *adapter,
355 struct ibmvnic_rx_pool *pool)
356{
357 int i;
358
359 kfree(pool->free_map);
360 pool->free_map = NULL;
361
362 if (!pool->rx_buff)
363 return;
364
365 for (i = 0; i < pool->size; i++) {
366 if (pool->rx_buff[i].skb) {
367 dev_kfree_skb_any(pool->rx_buff[i].skb);
368 pool->rx_buff[i].skb = NULL;
369 }
370 }
371 kfree(pool->rx_buff);
372 pool->rx_buff = NULL;
373}
374
Nathan Fontenotf0b8c962017-03-30 02:49:00 -0400375static void release_bounce_buffer(struct ibmvnic_adapter *adapter)
376{
377 struct device *dev = &adapter->vdev->dev;
378
379 if (!adapter->bounce_buffer)
380 return;
381
382 if (!dma_mapping_error(dev, adapter->bounce_buffer_dma)) {
383 dma_unmap_single(dev, adapter->bounce_buffer_dma,
384 adapter->bounce_buffer_size,
385 DMA_BIDIRECTIONAL);
386 adapter->bounce_buffer_dma = DMA_ERROR_CODE;
387 }
388
389 kfree(adapter->bounce_buffer);
390 adapter->bounce_buffer = NULL;
391}
392
393static int init_bounce_buffer(struct net_device *netdev)
394{
395 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
396 struct device *dev = &adapter->vdev->dev;
397 char *buf;
398 int buf_sz;
399 dma_addr_t map_addr;
400
401 buf_sz = (netdev->mtu + ETH_HLEN - 1) / PAGE_SIZE + 1;
402 buf = kmalloc(adapter->bounce_buffer_size, GFP_KERNEL);
403 if (!buf)
404 return -1;
405
406 map_addr = dma_map_single(dev, buf, buf_sz, DMA_TO_DEVICE);
407 if (dma_mapping_error(dev, map_addr)) {
408 dev_err(dev, "Couldn't map bounce buffer\n");
409 kfree(buf);
410 return -1;
411 }
412
413 adapter->bounce_buffer = buf;
414 adapter->bounce_buffer_size = buf_sz;
415 adapter->bounce_buffer_dma = map_addr;
416 return 0;
417}
418
John Allena57a5d22017-03-17 17:13:41 -0500419static int ibmvnic_login(struct net_device *netdev)
Thomas Falcon032c5e82015-12-21 11:26:06 -0600420{
421 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
John Allenbd0b6722017-03-17 17:13:40 -0500422 unsigned long timeout = msecs_to_jiffies(30000);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600423 struct device *dev = &adapter->vdev->dev;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600424
John Allenbd0b6722017-03-17 17:13:40 -0500425 do {
426 if (adapter->renegotiate) {
427 adapter->renegotiate = false;
428 release_sub_crqs_no_irqs(adapter);
429
430 reinit_completion(&adapter->init_done);
431 send_cap_queries(adapter);
432 if (!wait_for_completion_timeout(&adapter->init_done,
433 timeout)) {
434 dev_err(dev, "Capabilities query timeout\n");
435 return -1;
436 }
437 }
438
439 reinit_completion(&adapter->init_done);
440 send_login(adapter);
441 if (!wait_for_completion_timeout(&adapter->init_done,
442 timeout)) {
443 dev_err(dev, "Login timeout\n");
444 return -1;
445 }
446 } while (adapter->renegotiate);
447
John Allena57a5d22017-03-17 17:13:41 -0500448 return 0;
449}
450
451static int ibmvnic_open(struct net_device *netdev)
452{
453 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
454 struct device *dev = &adapter->vdev->dev;
455 struct ibmvnic_tx_pool *tx_pool;
456 union ibmvnic_crq crq;
457 int rxadd_subcrqs;
458 u64 *size_array;
459 int tx_subcrqs;
460 int rc = 0;
461 int i, j;
462
John Allenea5509f2017-03-17 17:13:43 -0500463 if (adapter->is_closed) {
464 rc = ibmvnic_init(adapter);
465 if (rc)
466 return rc;
467 }
468
John Allena57a5d22017-03-17 17:13:41 -0500469 rc = ibmvnic_login(netdev);
470 if (rc)
471 return rc;
472
John Allenbd0b6722017-03-17 17:13:40 -0500473 rc = netif_set_real_num_tx_queues(netdev, adapter->req_tx_queues);
474 if (rc) {
475 dev_err(dev, "failed to set the number of tx queues\n");
476 return -1;
477 }
478
479 rc = init_sub_crq_irqs(adapter);
480 if (rc) {
481 dev_err(dev, "failed to initialize sub crq irqs\n");
482 return -1;
483 }
484
Thomas Falcon032c5e82015-12-21 11:26:06 -0600485 rxadd_subcrqs =
486 be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
487 tx_subcrqs =
488 be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
489 size_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
490 be32_to_cpu(adapter->login_rsp_buf->
491 off_rxadd_buff_size));
492 adapter->map_id = 1;
493 adapter->napi = kcalloc(adapter->req_rx_queues,
494 sizeof(struct napi_struct), GFP_KERNEL);
495 if (!adapter->napi)
496 goto alloc_napi_failed;
497 for (i = 0; i < adapter->req_rx_queues; i++) {
498 netif_napi_add(netdev, &adapter->napi[i], ibmvnic_poll,
499 NAPI_POLL_WEIGHT);
500 napi_enable(&adapter->napi[i]);
501 }
502 adapter->rx_pool =
503 kcalloc(rxadd_subcrqs, sizeof(struct ibmvnic_rx_pool), GFP_KERNEL);
504
505 if (!adapter->rx_pool)
506 goto rx_pool_arr_alloc_failed;
507 send_map_query(adapter);
508 for (i = 0; i < rxadd_subcrqs; i++) {
509 init_rx_pool(adapter, &adapter->rx_pool[i],
Thomas Falcon068d9f92017-03-05 12:18:42 -0600510 adapter->req_rx_add_entries_per_subcrq, i,
Thomas Falcon032c5e82015-12-21 11:26:06 -0600511 be64_to_cpu(size_array[i]), 1);
512 if (alloc_rx_pool(adapter, &adapter->rx_pool[i])) {
513 dev_err(dev, "Couldn't alloc rx pool\n");
514 goto rx_pool_alloc_failed;
515 }
516 }
517 adapter->tx_pool =
518 kcalloc(tx_subcrqs, sizeof(struct ibmvnic_tx_pool), GFP_KERNEL);
519
520 if (!adapter->tx_pool)
521 goto tx_pool_arr_alloc_failed;
522 for (i = 0; i < tx_subcrqs; i++) {
523 tx_pool = &adapter->tx_pool[i];
524 tx_pool->tx_buff =
Thomas Falcon068d9f92017-03-05 12:18:42 -0600525 kcalloc(adapter->req_tx_entries_per_subcrq,
Thomas Falcon032c5e82015-12-21 11:26:06 -0600526 sizeof(struct ibmvnic_tx_buff), GFP_KERNEL);
527 if (!tx_pool->tx_buff)
528 goto tx_pool_alloc_failed;
529
530 if (alloc_long_term_buff(adapter, &tx_pool->long_term_buff,
Thomas Falcon068d9f92017-03-05 12:18:42 -0600531 adapter->req_tx_entries_per_subcrq *
Thomas Falcon032c5e82015-12-21 11:26:06 -0600532 adapter->req_mtu))
533 goto tx_ltb_alloc_failed;
534
535 tx_pool->free_map =
Thomas Falcon068d9f92017-03-05 12:18:42 -0600536 kcalloc(adapter->req_tx_entries_per_subcrq,
Thomas Falcon032c5e82015-12-21 11:26:06 -0600537 sizeof(int), GFP_KERNEL);
538 if (!tx_pool->free_map)
539 goto tx_fm_alloc_failed;
540
Thomas Falcon068d9f92017-03-05 12:18:42 -0600541 for (j = 0; j < adapter->req_tx_entries_per_subcrq; j++)
Thomas Falcon032c5e82015-12-21 11:26:06 -0600542 tx_pool->free_map[j] = j;
543
544 tx_pool->consumer_index = 0;
545 tx_pool->producer_index = 0;
546 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600547
Nathan Fontenotf0b8c962017-03-30 02:49:00 -0400548 rc = init_bounce_buffer(netdev);
549 if (rc)
550 goto bounce_init_failed;
551
Thomas Falcon032c5e82015-12-21 11:26:06 -0600552 replenish_pools(adapter);
553
554 /* We're ready to receive frames, enable the sub-crq interrupts and
555 * set the logical link state to up
556 */
557 for (i = 0; i < adapter->req_rx_queues; i++)
558 enable_scrq_irq(adapter, adapter->rx_scrq[i]);
559
560 for (i = 0; i < adapter->req_tx_queues; i++)
561 enable_scrq_irq(adapter, adapter->tx_scrq[i]);
562
563 memset(&crq, 0, sizeof(crq));
564 crq.logical_link_state.first = IBMVNIC_CRQ_CMD;
565 crq.logical_link_state.cmd = LOGICAL_LINK_STATE;
566 crq.logical_link_state.link_state = IBMVNIC_LOGICAL_LNK_UP;
567 ibmvnic_send_crq(adapter, &crq);
568
Thomas Falconb8efb892016-07-06 15:35:15 -0500569 netif_tx_start_all_queues(netdev);
John Allenea5509f2017-03-17 17:13:43 -0500570 adapter->is_closed = false;
Thomas Falconb8efb892016-07-06 15:35:15 -0500571
Thomas Falcon032c5e82015-12-21 11:26:06 -0600572 return 0;
573
Nathan Fontenotf0b8c962017-03-30 02:49:00 -0400574bounce_init_failed:
Thomas Falcon032c5e82015-12-21 11:26:06 -0600575 i = tx_subcrqs - 1;
576 kfree(adapter->tx_pool[i].free_map);
577tx_fm_alloc_failed:
578 free_long_term_buff(adapter, &adapter->tx_pool[i].long_term_buff);
579tx_ltb_alloc_failed:
580 kfree(adapter->tx_pool[i].tx_buff);
581tx_pool_alloc_failed:
582 for (j = 0; j < i; j++) {
583 kfree(adapter->tx_pool[j].tx_buff);
584 free_long_term_buff(adapter,
585 &adapter->tx_pool[j].long_term_buff);
586 kfree(adapter->tx_pool[j].free_map);
587 }
588 kfree(adapter->tx_pool);
589 adapter->tx_pool = NULL;
590tx_pool_arr_alloc_failed:
591 i = rxadd_subcrqs;
592rx_pool_alloc_failed:
593 for (j = 0; j < i; j++) {
594 free_rx_pool(adapter, &adapter->rx_pool[j]);
595 free_long_term_buff(adapter,
596 &adapter->rx_pool[j].long_term_buff);
597 }
598 kfree(adapter->rx_pool);
599 adapter->rx_pool = NULL;
600rx_pool_arr_alloc_failed:
601 for (i = 0; i < adapter->req_rx_queues; i++)
Nathan Fontenote722af62017-02-10 13:29:06 -0500602 napi_disable(&adapter->napi[i]);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600603alloc_napi_failed:
John Allenbd0b6722017-03-17 17:13:40 -0500604 release_sub_crqs(adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600605 return -ENOMEM;
606}
607
John Allenea5509f2017-03-17 17:13:43 -0500608static void ibmvnic_release_resources(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -0600609{
Thomas Falcon032c5e82015-12-21 11:26:06 -0600610 struct device *dev = &adapter->vdev->dev;
John Allenea5509f2017-03-17 17:13:43 -0500611 int tx_scrqs, rx_scrqs;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600612 int i;
613
Nathan Fontenotf0b8c962017-03-30 02:49:00 -0400614 release_bounce_buffer(adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600615
John Allenea5509f2017-03-17 17:13:43 -0500616 tx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
617 for (i = 0; i < tx_scrqs; i++) {
618 struct ibmvnic_tx_pool *tx_pool = &adapter->tx_pool[i];
619
620 kfree(tx_pool->tx_buff);
621 free_long_term_buff(adapter, &tx_pool->long_term_buff);
622 kfree(tx_pool->free_map);
623 }
624 kfree(adapter->tx_pool);
625 adapter->tx_pool = NULL;
626
627 rx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
628 for (i = 0; i < rx_scrqs; i++) {
629 struct ibmvnic_rx_pool *rx_pool = &adapter->rx_pool[i];
630
631 free_rx_pool(adapter, rx_pool);
632 free_long_term_buff(adapter, &rx_pool->long_term_buff);
633 }
634 kfree(adapter->rx_pool);
635 adapter->rx_pool = NULL;
636
637 release_sub_crqs(adapter);
Nathan Fontenotf9928872017-03-30 02:48:54 -0400638 release_crq_queue(adapter);
John Allenea5509f2017-03-17 17:13:43 -0500639
John Allenea5509f2017-03-17 17:13:43 -0500640 if (adapter->stats_token)
641 dma_unmap_single(dev, adapter->stats_token,
642 sizeof(struct ibmvnic_statistics),
643 DMA_FROM_DEVICE);
John Allenea5509f2017-03-17 17:13:43 -0500644}
645
646static int ibmvnic_close(struct net_device *netdev)
647{
648 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
649 union ibmvnic_crq crq;
650 int i;
651
652 adapter->closing = true;
653
654 for (i = 0; i < adapter->req_rx_queues; i++)
655 napi_disable(&adapter->napi[i]);
656
657 if (!adapter->failover)
658 netif_tx_stop_all_queues(netdev);
659
Thomas Falcon032c5e82015-12-21 11:26:06 -0600660 memset(&crq, 0, sizeof(crq));
661 crq.logical_link_state.first = IBMVNIC_CRQ_CMD;
662 crq.logical_link_state.cmd = LOGICAL_LINK_STATE;
663 crq.logical_link_state.link_state = IBMVNIC_LOGICAL_LNK_DN;
664 ibmvnic_send_crq(adapter, &crq);
665
John Allenea5509f2017-03-17 17:13:43 -0500666 ibmvnic_release_resources(adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600667
John Allenea5509f2017-03-17 17:13:43 -0500668 adapter->is_closed = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600669 adapter->closing = false;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600670 return 0;
671}
672
Thomas Falconad7775d2016-04-01 17:20:34 -0500673/**
674 * build_hdr_data - creates L2/L3/L4 header data buffer
675 * @hdr_field - bitfield determining needed headers
676 * @skb - socket buffer
677 * @hdr_len - array of header lengths
678 * @tot_len - total length of data
679 *
680 * Reads hdr_field to determine which headers are needed by firmware.
681 * Builds a buffer containing these headers. Saves individual header
682 * lengths and total buffer length to be used to build descriptors.
683 */
684static int build_hdr_data(u8 hdr_field, struct sk_buff *skb,
685 int *hdr_len, u8 *hdr_data)
686{
687 int len = 0;
688 u8 *hdr;
689
690 hdr_len[0] = sizeof(struct ethhdr);
691
692 if (skb->protocol == htons(ETH_P_IP)) {
693 hdr_len[1] = ip_hdr(skb)->ihl * 4;
694 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
695 hdr_len[2] = tcp_hdrlen(skb);
696 else if (ip_hdr(skb)->protocol == IPPROTO_UDP)
697 hdr_len[2] = sizeof(struct udphdr);
698 } else if (skb->protocol == htons(ETH_P_IPV6)) {
699 hdr_len[1] = sizeof(struct ipv6hdr);
700 if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
701 hdr_len[2] = tcp_hdrlen(skb);
702 else if (ipv6_hdr(skb)->nexthdr == IPPROTO_UDP)
703 hdr_len[2] = sizeof(struct udphdr);
704 }
705
706 memset(hdr_data, 0, 120);
707 if ((hdr_field >> 6) & 1) {
708 hdr = skb_mac_header(skb);
709 memcpy(hdr_data, hdr, hdr_len[0]);
710 len += hdr_len[0];
711 }
712
713 if ((hdr_field >> 5) & 1) {
714 hdr = skb_network_header(skb);
715 memcpy(hdr_data + len, hdr, hdr_len[1]);
716 len += hdr_len[1];
717 }
718
719 if ((hdr_field >> 4) & 1) {
720 hdr = skb_transport_header(skb);
721 memcpy(hdr_data + len, hdr, hdr_len[2]);
722 len += hdr_len[2];
723 }
724 return len;
725}
726
727/**
728 * create_hdr_descs - create header and header extension descriptors
729 * @hdr_field - bitfield determining needed headers
730 * @data - buffer containing header data
731 * @len - length of data buffer
732 * @hdr_len - array of individual header lengths
733 * @scrq_arr - descriptor array
734 *
735 * Creates header and, if needed, header extension descriptors and
736 * places them in a descriptor array, scrq_arr
737 */
738
739static void create_hdr_descs(u8 hdr_field, u8 *hdr_data, int len, int *hdr_len,
740 union sub_crq *scrq_arr)
741{
742 union sub_crq hdr_desc;
743 int tmp_len = len;
744 u8 *data, *cur;
745 int tmp;
746
747 while (tmp_len > 0) {
748 cur = hdr_data + len - tmp_len;
749
750 memset(&hdr_desc, 0, sizeof(hdr_desc));
751 if (cur != hdr_data) {
752 data = hdr_desc.hdr_ext.data;
753 tmp = tmp_len > 29 ? 29 : tmp_len;
754 hdr_desc.hdr_ext.first = IBMVNIC_CRQ_CMD;
755 hdr_desc.hdr_ext.type = IBMVNIC_HDR_EXT_DESC;
756 hdr_desc.hdr_ext.len = tmp;
757 } else {
758 data = hdr_desc.hdr.data;
759 tmp = tmp_len > 24 ? 24 : tmp_len;
760 hdr_desc.hdr.first = IBMVNIC_CRQ_CMD;
761 hdr_desc.hdr.type = IBMVNIC_HDR_DESC;
762 hdr_desc.hdr.len = tmp;
763 hdr_desc.hdr.l2_len = (u8)hdr_len[0];
764 hdr_desc.hdr.l3_len = cpu_to_be16((u16)hdr_len[1]);
765 hdr_desc.hdr.l4_len = (u8)hdr_len[2];
766 hdr_desc.hdr.flag = hdr_field << 1;
767 }
768 memcpy(data, cur, tmp);
769 tmp_len -= tmp;
770 *scrq_arr = hdr_desc;
771 scrq_arr++;
772 }
773}
774
775/**
776 * build_hdr_descs_arr - build a header descriptor array
777 * @skb - socket buffer
778 * @num_entries - number of descriptors to be sent
779 * @subcrq - first TX descriptor
780 * @hdr_field - bit field determining which headers will be sent
781 *
782 * This function will build a TX descriptor array with applicable
783 * L2/L3/L4 packet header descriptors to be sent by send_subcrq_indirect.
784 */
785
786static void build_hdr_descs_arr(struct ibmvnic_tx_buff *txbuff,
787 int *num_entries, u8 hdr_field)
788{
789 int hdr_len[3] = {0, 0, 0};
790 int tot_len, len;
791 u8 *hdr_data = txbuff->hdr_data;
792
793 tot_len = build_hdr_data(hdr_field, txbuff->skb, hdr_len,
794 txbuff->hdr_data);
795 len = tot_len;
796 len -= 24;
797 if (len > 0)
798 num_entries += len % 29 ? len / 29 + 1 : len / 29;
799 create_hdr_descs(hdr_field, hdr_data, tot_len, hdr_len,
800 txbuff->indir_arr + 1);
801}
802
Thomas Falcon032c5e82015-12-21 11:26:06 -0600803static int ibmvnic_xmit(struct sk_buff *skb, struct net_device *netdev)
804{
805 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
806 int queue_num = skb_get_queue_mapping(skb);
Thomas Falconad7775d2016-04-01 17:20:34 -0500807 u8 *hdrs = (u8 *)&adapter->tx_rx_desc_req;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600808 struct device *dev = &adapter->vdev->dev;
809 struct ibmvnic_tx_buff *tx_buff = NULL;
Thomas Falcon142c0ac2017-03-05 12:18:41 -0600810 struct ibmvnic_sub_crq_queue *tx_scrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600811 struct ibmvnic_tx_pool *tx_pool;
812 unsigned int tx_send_failed = 0;
813 unsigned int tx_map_failed = 0;
814 unsigned int tx_dropped = 0;
815 unsigned int tx_packets = 0;
816 unsigned int tx_bytes = 0;
817 dma_addr_t data_dma_addr;
818 struct netdev_queue *txq;
819 bool used_bounce = false;
820 unsigned long lpar_rc;
821 union sub_crq tx_crq;
822 unsigned int offset;
Thomas Falconad7775d2016-04-01 17:20:34 -0500823 int num_entries = 1;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600824 unsigned char *dst;
825 u64 *handle_array;
826 int index = 0;
827 int ret = 0;
828
829 tx_pool = &adapter->tx_pool[queue_num];
Thomas Falcon142c0ac2017-03-05 12:18:41 -0600830 tx_scrq = adapter->tx_scrq[queue_num];
Thomas Falcon032c5e82015-12-21 11:26:06 -0600831 txq = netdev_get_tx_queue(netdev, skb_get_queue_mapping(skb));
832 handle_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
833 be32_to_cpu(adapter->login_rsp_buf->
834 off_txsubm_subcrqs));
835 if (adapter->migrated) {
836 tx_send_failed++;
837 tx_dropped++;
838 ret = NETDEV_TX_BUSY;
839 goto out;
840 }
841
842 index = tx_pool->free_map[tx_pool->consumer_index];
843 offset = index * adapter->req_mtu;
844 dst = tx_pool->long_term_buff.buff + offset;
845 memset(dst, 0, adapter->req_mtu);
846 skb_copy_from_linear_data(skb, dst, skb->len);
847 data_dma_addr = tx_pool->long_term_buff.addr + offset;
848
849 tx_pool->consumer_index =
850 (tx_pool->consumer_index + 1) %
Thomas Falcon068d9f92017-03-05 12:18:42 -0600851 adapter->req_tx_entries_per_subcrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600852
853 tx_buff = &tx_pool->tx_buff[index];
854 tx_buff->skb = skb;
855 tx_buff->data_dma[0] = data_dma_addr;
856 tx_buff->data_len[0] = skb->len;
857 tx_buff->index = index;
858 tx_buff->pool_index = queue_num;
859 tx_buff->last_frag = true;
860 tx_buff->used_bounce = used_bounce;
861
862 memset(&tx_crq, 0, sizeof(tx_crq));
863 tx_crq.v1.first = IBMVNIC_CRQ_CMD;
864 tx_crq.v1.type = IBMVNIC_TX_DESC;
865 tx_crq.v1.n_crq_elem = 1;
866 tx_crq.v1.n_sge = 1;
867 tx_crq.v1.flags1 = IBMVNIC_TX_COMP_NEEDED;
868 tx_crq.v1.correlator = cpu_to_be32(index);
869 tx_crq.v1.dma_reg = cpu_to_be16(tx_pool->long_term_buff.map_id);
870 tx_crq.v1.sge_len = cpu_to_be32(skb->len);
871 tx_crq.v1.ioba = cpu_to_be64(data_dma_addr);
872
873 if (adapter->vlan_header_insertion) {
874 tx_crq.v1.flags2 |= IBMVNIC_TX_VLAN_INSERT;
875 tx_crq.v1.vlan_id = cpu_to_be16(skb->vlan_tci);
876 }
877
878 if (skb->protocol == htons(ETH_P_IP)) {
879 if (ip_hdr(skb)->version == 4)
880 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV4;
881 else if (ip_hdr(skb)->version == 6)
882 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV6;
883
884 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
885 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_TCP;
886 else if (ip_hdr(skb)->protocol != IPPROTO_TCP)
887 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_UDP;
888 }
889
Thomas Falconad7775d2016-04-01 17:20:34 -0500890 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Thomas Falcon032c5e82015-12-21 11:26:06 -0600891 tx_crq.v1.flags1 |= IBMVNIC_TX_CHKSUM_OFFLOAD;
Thomas Falconad7775d2016-04-01 17:20:34 -0500892 hdrs += 2;
893 }
894 /* determine if l2/3/4 headers are sent to firmware */
895 if ((*hdrs >> 7) & 1 &&
896 (skb->protocol == htons(ETH_P_IP) ||
897 skb->protocol == htons(ETH_P_IPV6))) {
898 build_hdr_descs_arr(tx_buff, &num_entries, *hdrs);
899 tx_crq.v1.n_crq_elem = num_entries;
900 tx_buff->indir_arr[0] = tx_crq;
901 tx_buff->indir_dma = dma_map_single(dev, tx_buff->indir_arr,
902 sizeof(tx_buff->indir_arr),
903 DMA_TO_DEVICE);
904 if (dma_mapping_error(dev, tx_buff->indir_dma)) {
905 if (!firmware_has_feature(FW_FEATURE_CMO))
906 dev_err(dev, "tx: unable to map descriptor array\n");
907 tx_map_failed++;
908 tx_dropped++;
909 ret = NETDEV_TX_BUSY;
910 goto out;
911 }
John Allen498cd8e2016-04-06 11:49:55 -0500912 lpar_rc = send_subcrq_indirect(adapter, handle_array[queue_num],
Thomas Falconad7775d2016-04-01 17:20:34 -0500913 (u64)tx_buff->indir_dma,
914 (u64)num_entries);
915 } else {
John Allen498cd8e2016-04-06 11:49:55 -0500916 lpar_rc = send_subcrq(adapter, handle_array[queue_num],
917 &tx_crq);
Thomas Falconad7775d2016-04-01 17:20:34 -0500918 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600919 if (lpar_rc != H_SUCCESS) {
920 dev_err(dev, "tx failed with code %ld\n", lpar_rc);
921
922 if (tx_pool->consumer_index == 0)
923 tx_pool->consumer_index =
Thomas Falcon068d9f92017-03-05 12:18:42 -0600924 adapter->req_tx_entries_per_subcrq - 1;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600925 else
926 tx_pool->consumer_index--;
927
928 tx_send_failed++;
929 tx_dropped++;
930 ret = NETDEV_TX_BUSY;
931 goto out;
932 }
Thomas Falcon142c0ac2017-03-05 12:18:41 -0600933
934 atomic_inc(&tx_scrq->used);
935
936 if (atomic_read(&tx_scrq->used) >= adapter->req_tx_entries_per_subcrq) {
937 netdev_info(netdev, "Stopping queue %d\n", queue_num);
938 netif_stop_subqueue(netdev, queue_num);
939 }
940
Thomas Falcon032c5e82015-12-21 11:26:06 -0600941 tx_packets++;
942 tx_bytes += skb->len;
943 txq->trans_start = jiffies;
944 ret = NETDEV_TX_OK;
945
946out:
947 netdev->stats.tx_dropped += tx_dropped;
948 netdev->stats.tx_bytes += tx_bytes;
949 netdev->stats.tx_packets += tx_packets;
950 adapter->tx_send_failed += tx_send_failed;
951 adapter->tx_map_failed += tx_map_failed;
952
953 return ret;
954}
955
956static void ibmvnic_set_multi(struct net_device *netdev)
957{
958 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
959 struct netdev_hw_addr *ha;
960 union ibmvnic_crq crq;
961
962 memset(&crq, 0, sizeof(crq));
963 crq.request_capability.first = IBMVNIC_CRQ_CMD;
964 crq.request_capability.cmd = REQUEST_CAPABILITY;
965
966 if (netdev->flags & IFF_PROMISC) {
967 if (!adapter->promisc_supported)
968 return;
969 } else {
970 if (netdev->flags & IFF_ALLMULTI) {
971 /* Accept all multicast */
972 memset(&crq, 0, sizeof(crq));
973 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
974 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
975 crq.multicast_ctrl.flags = IBMVNIC_ENABLE_ALL;
976 ibmvnic_send_crq(adapter, &crq);
977 } else if (netdev_mc_empty(netdev)) {
978 /* Reject all multicast */
979 memset(&crq, 0, sizeof(crq));
980 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
981 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
982 crq.multicast_ctrl.flags = IBMVNIC_DISABLE_ALL;
983 ibmvnic_send_crq(adapter, &crq);
984 } else {
985 /* Accept one or more multicast(s) */
986 netdev_for_each_mc_addr(ha, netdev) {
987 memset(&crq, 0, sizeof(crq));
988 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
989 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
990 crq.multicast_ctrl.flags = IBMVNIC_ENABLE_MC;
991 ether_addr_copy(&crq.multicast_ctrl.mac_addr[0],
992 ha->addr);
993 ibmvnic_send_crq(adapter, &crq);
994 }
995 }
996 }
997}
998
999static int ibmvnic_set_mac(struct net_device *netdev, void *p)
1000{
1001 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1002 struct sockaddr *addr = p;
1003 union ibmvnic_crq crq;
1004
1005 if (!is_valid_ether_addr(addr->sa_data))
1006 return -EADDRNOTAVAIL;
1007
1008 memset(&crq, 0, sizeof(crq));
1009 crq.change_mac_addr.first = IBMVNIC_CRQ_CMD;
1010 crq.change_mac_addr.cmd = CHANGE_MAC_ADDR;
1011 ether_addr_copy(&crq.change_mac_addr.mac_addr[0], addr->sa_data);
1012 ibmvnic_send_crq(adapter, &crq);
1013 /* netdev->dev_addr is changed in handle_change_mac_rsp function */
1014 return 0;
1015}
1016
Thomas Falcon032c5e82015-12-21 11:26:06 -06001017static void ibmvnic_tx_timeout(struct net_device *dev)
1018{
1019 struct ibmvnic_adapter *adapter = netdev_priv(dev);
1020 int rc;
1021
1022 /* Adapter timed out, resetting it */
1023 release_sub_crqs(adapter);
1024 rc = ibmvnic_reset_crq(adapter);
1025 if (rc)
1026 dev_err(&adapter->vdev->dev, "Adapter timeout, reset failed\n");
1027 else
1028 ibmvnic_send_crq_init(adapter);
1029}
1030
1031static void remove_buff_from_pool(struct ibmvnic_adapter *adapter,
1032 struct ibmvnic_rx_buff *rx_buff)
1033{
1034 struct ibmvnic_rx_pool *pool = &adapter->rx_pool[rx_buff->pool_index];
1035
1036 rx_buff->skb = NULL;
1037
1038 pool->free_map[pool->next_alloc] = (int)(rx_buff - pool->rx_buff);
1039 pool->next_alloc = (pool->next_alloc + 1) % pool->size;
1040
1041 atomic_dec(&pool->available);
1042}
1043
1044static int ibmvnic_poll(struct napi_struct *napi, int budget)
1045{
1046 struct net_device *netdev = napi->dev;
1047 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1048 int scrq_num = (int)(napi - adapter->napi);
1049 int frames_processed = 0;
1050restart_poll:
1051 while (frames_processed < budget) {
1052 struct sk_buff *skb;
1053 struct ibmvnic_rx_buff *rx_buff;
1054 union sub_crq *next;
1055 u32 length;
1056 u16 offset;
1057 u8 flags = 0;
1058
1059 if (!pending_scrq(adapter, adapter->rx_scrq[scrq_num]))
1060 break;
1061 next = ibmvnic_next_scrq(adapter, adapter->rx_scrq[scrq_num]);
1062 rx_buff =
1063 (struct ibmvnic_rx_buff *)be64_to_cpu(next->
1064 rx_comp.correlator);
1065 /* do error checking */
1066 if (next->rx_comp.rc) {
1067 netdev_err(netdev, "rx error %x\n", next->rx_comp.rc);
1068 /* free the entry */
1069 next->rx_comp.first = 0;
1070 remove_buff_from_pool(adapter, rx_buff);
1071 break;
1072 }
1073
1074 length = be32_to_cpu(next->rx_comp.len);
1075 offset = be16_to_cpu(next->rx_comp.off_frame_data);
1076 flags = next->rx_comp.flags;
1077 skb = rx_buff->skb;
1078 skb_copy_to_linear_data(skb, rx_buff->data + offset,
1079 length);
1080 skb->vlan_tci = be16_to_cpu(next->rx_comp.vlan_tci);
1081 /* free the entry */
1082 next->rx_comp.first = 0;
1083 remove_buff_from_pool(adapter, rx_buff);
1084
1085 skb_put(skb, length);
1086 skb->protocol = eth_type_trans(skb, netdev);
1087
1088 if (flags & IBMVNIC_IP_CHKSUM_GOOD &&
1089 flags & IBMVNIC_TCP_UDP_CHKSUM_GOOD) {
1090 skb->ip_summed = CHECKSUM_UNNECESSARY;
1091 }
1092
1093 length = skb->len;
1094 napi_gro_receive(napi, skb); /* send it up */
1095 netdev->stats.rx_packets++;
1096 netdev->stats.rx_bytes += length;
1097 frames_processed++;
1098 }
John Allen498cd8e2016-04-06 11:49:55 -05001099 replenish_rx_pool(adapter, &adapter->rx_pool[scrq_num]);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001100
1101 if (frames_processed < budget) {
1102 enable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
Eric Dumazet6ad20162017-01-30 08:22:01 -08001103 napi_complete_done(napi, frames_processed);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001104 if (pending_scrq(adapter, adapter->rx_scrq[scrq_num]) &&
1105 napi_reschedule(napi)) {
1106 disable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
1107 goto restart_poll;
1108 }
1109 }
1110 return frames_processed;
1111}
1112
1113#ifdef CONFIG_NET_POLL_CONTROLLER
1114static void ibmvnic_netpoll_controller(struct net_device *dev)
1115{
1116 struct ibmvnic_adapter *adapter = netdev_priv(dev);
1117 int i;
1118
1119 replenish_pools(netdev_priv(dev));
1120 for (i = 0; i < adapter->req_rx_queues; i++)
1121 ibmvnic_interrupt_rx(adapter->rx_scrq[i]->irq,
1122 adapter->rx_scrq[i]);
1123}
1124#endif
1125
1126static const struct net_device_ops ibmvnic_netdev_ops = {
1127 .ndo_open = ibmvnic_open,
1128 .ndo_stop = ibmvnic_close,
1129 .ndo_start_xmit = ibmvnic_xmit,
1130 .ndo_set_rx_mode = ibmvnic_set_multi,
1131 .ndo_set_mac_address = ibmvnic_set_mac,
1132 .ndo_validate_addr = eth_validate_addr,
Thomas Falcon032c5e82015-12-21 11:26:06 -06001133 .ndo_tx_timeout = ibmvnic_tx_timeout,
1134#ifdef CONFIG_NET_POLL_CONTROLLER
1135 .ndo_poll_controller = ibmvnic_netpoll_controller,
1136#endif
1137};
1138
1139/* ethtool functions */
1140
Philippe Reynes8a433792017-01-07 22:37:29 +01001141static int ibmvnic_get_link_ksettings(struct net_device *netdev,
1142 struct ethtool_link_ksettings *cmd)
Thomas Falcon032c5e82015-12-21 11:26:06 -06001143{
Philippe Reynes8a433792017-01-07 22:37:29 +01001144 u32 supported, advertising;
1145
1146 supported = (SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg |
Thomas Falcon032c5e82015-12-21 11:26:06 -06001147 SUPPORTED_FIBRE);
Philippe Reynes8a433792017-01-07 22:37:29 +01001148 advertising = (ADVERTISED_1000baseT_Full | ADVERTISED_Autoneg |
Thomas Falcon032c5e82015-12-21 11:26:06 -06001149 ADVERTISED_FIBRE);
Philippe Reynes8a433792017-01-07 22:37:29 +01001150 cmd->base.speed = SPEED_1000;
1151 cmd->base.duplex = DUPLEX_FULL;
1152 cmd->base.port = PORT_FIBRE;
1153 cmd->base.phy_address = 0;
1154 cmd->base.autoneg = AUTONEG_ENABLE;
1155
1156 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
1157 supported);
1158 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
1159 advertising);
1160
Thomas Falcon032c5e82015-12-21 11:26:06 -06001161 return 0;
1162}
1163
1164static void ibmvnic_get_drvinfo(struct net_device *dev,
1165 struct ethtool_drvinfo *info)
1166{
1167 strlcpy(info->driver, ibmvnic_driver_name, sizeof(info->driver));
1168 strlcpy(info->version, IBMVNIC_DRIVER_VERSION, sizeof(info->version));
1169}
1170
1171static u32 ibmvnic_get_msglevel(struct net_device *netdev)
1172{
1173 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1174
1175 return adapter->msg_enable;
1176}
1177
1178static void ibmvnic_set_msglevel(struct net_device *netdev, u32 data)
1179{
1180 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1181
1182 adapter->msg_enable = data;
1183}
1184
1185static u32 ibmvnic_get_link(struct net_device *netdev)
1186{
1187 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1188
1189 /* Don't need to send a query because we request a logical link up at
1190 * init and then we wait for link state indications
1191 */
1192 return adapter->logical_link_state;
1193}
1194
1195static void ibmvnic_get_ringparam(struct net_device *netdev,
1196 struct ethtool_ringparam *ring)
1197{
1198 ring->rx_max_pending = 0;
1199 ring->tx_max_pending = 0;
1200 ring->rx_mini_max_pending = 0;
1201 ring->rx_jumbo_max_pending = 0;
1202 ring->rx_pending = 0;
1203 ring->tx_pending = 0;
1204 ring->rx_mini_pending = 0;
1205 ring->rx_jumbo_pending = 0;
1206}
1207
1208static void ibmvnic_get_strings(struct net_device *dev, u32 stringset, u8 *data)
1209{
1210 int i;
1211
1212 if (stringset != ETH_SS_STATS)
1213 return;
1214
1215 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++, data += ETH_GSTRING_LEN)
1216 memcpy(data, ibmvnic_stats[i].name, ETH_GSTRING_LEN);
1217}
1218
1219static int ibmvnic_get_sset_count(struct net_device *dev, int sset)
1220{
1221 switch (sset) {
1222 case ETH_SS_STATS:
1223 return ARRAY_SIZE(ibmvnic_stats);
1224 default:
1225 return -EOPNOTSUPP;
1226 }
1227}
1228
1229static void ibmvnic_get_ethtool_stats(struct net_device *dev,
1230 struct ethtool_stats *stats, u64 *data)
1231{
1232 struct ibmvnic_adapter *adapter = netdev_priv(dev);
1233 union ibmvnic_crq crq;
1234 int i;
1235
1236 memset(&crq, 0, sizeof(crq));
1237 crq.request_statistics.first = IBMVNIC_CRQ_CMD;
1238 crq.request_statistics.cmd = REQUEST_STATISTICS;
1239 crq.request_statistics.ioba = cpu_to_be32(adapter->stats_token);
1240 crq.request_statistics.len =
1241 cpu_to_be32(sizeof(struct ibmvnic_statistics));
Thomas Falcon032c5e82015-12-21 11:26:06 -06001242
1243 /* Wait for data to be written */
1244 init_completion(&adapter->stats_done);
Nathan Fontenotdb5d0b52017-02-10 13:45:05 -05001245 ibmvnic_send_crq(adapter, &crq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001246 wait_for_completion(&adapter->stats_done);
1247
1248 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++)
1249 data[i] = IBMVNIC_GET_STAT(adapter, ibmvnic_stats[i].offset);
1250}
1251
1252static const struct ethtool_ops ibmvnic_ethtool_ops = {
Thomas Falcon032c5e82015-12-21 11:26:06 -06001253 .get_drvinfo = ibmvnic_get_drvinfo,
1254 .get_msglevel = ibmvnic_get_msglevel,
1255 .set_msglevel = ibmvnic_set_msglevel,
1256 .get_link = ibmvnic_get_link,
1257 .get_ringparam = ibmvnic_get_ringparam,
1258 .get_strings = ibmvnic_get_strings,
1259 .get_sset_count = ibmvnic_get_sset_count,
1260 .get_ethtool_stats = ibmvnic_get_ethtool_stats,
Philippe Reynes8a433792017-01-07 22:37:29 +01001261 .get_link_ksettings = ibmvnic_get_link_ksettings,
Thomas Falcon032c5e82015-12-21 11:26:06 -06001262};
1263
1264/* Routines for managing CRQs/sCRQs */
1265
1266static void release_sub_crq_queue(struct ibmvnic_adapter *adapter,
1267 struct ibmvnic_sub_crq_queue *scrq)
1268{
1269 struct device *dev = &adapter->vdev->dev;
1270 long rc;
1271
1272 netdev_dbg(adapter->netdev, "Releasing sub-CRQ\n");
1273
1274 /* Close the sub-crqs */
1275 do {
1276 rc = plpar_hcall_norets(H_FREE_SUB_CRQ,
1277 adapter->vdev->unit_address,
1278 scrq->crq_num);
1279 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
1280
1281 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
1282 DMA_BIDIRECTIONAL);
1283 free_pages((unsigned long)scrq->msgs, 2);
1284 kfree(scrq);
1285}
1286
1287static struct ibmvnic_sub_crq_queue *init_sub_crq_queue(struct ibmvnic_adapter
1288 *adapter)
1289{
1290 struct device *dev = &adapter->vdev->dev;
1291 struct ibmvnic_sub_crq_queue *scrq;
1292 int rc;
1293
1294 scrq = kmalloc(sizeof(*scrq), GFP_ATOMIC);
1295 if (!scrq)
1296 return NULL;
1297
Thomas Falcon12608c22016-10-17 15:28:09 -05001298 scrq->msgs = (union sub_crq *)__get_free_pages(GFP_ATOMIC, 2);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001299 memset(scrq->msgs, 0, 4 * PAGE_SIZE);
1300 if (!scrq->msgs) {
1301 dev_warn(dev, "Couldn't allocate crq queue messages page\n");
1302 goto zero_page_failed;
1303 }
1304
1305 scrq->msg_token = dma_map_single(dev, scrq->msgs, 4 * PAGE_SIZE,
1306 DMA_BIDIRECTIONAL);
1307 if (dma_mapping_error(dev, scrq->msg_token)) {
1308 dev_warn(dev, "Couldn't map crq queue messages page\n");
1309 goto map_failed;
1310 }
1311
1312 rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
1313 4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
1314
1315 if (rc == H_RESOURCE)
1316 rc = ibmvnic_reset_crq(adapter);
1317
1318 if (rc == H_CLOSED) {
1319 dev_warn(dev, "Partner adapter not ready, waiting.\n");
1320 } else if (rc) {
1321 dev_warn(dev, "Error %d registering sub-crq\n", rc);
1322 goto reg_failed;
1323 }
1324
Thomas Falcon032c5e82015-12-21 11:26:06 -06001325 scrq->adapter = adapter;
1326 scrq->size = 4 * PAGE_SIZE / sizeof(*scrq->msgs);
1327 scrq->cur = 0;
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001328 atomic_set(&scrq->used, 0);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001329 scrq->rx_skb_top = NULL;
1330 spin_lock_init(&scrq->lock);
1331
1332 netdev_dbg(adapter->netdev,
1333 "sub-crq initialized, num %lx, hw_irq=%lx, irq=%x\n",
1334 scrq->crq_num, scrq->hw_irq, scrq->irq);
1335
1336 return scrq;
1337
Thomas Falcon032c5e82015-12-21 11:26:06 -06001338reg_failed:
1339 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
1340 DMA_BIDIRECTIONAL);
1341map_failed:
1342 free_pages((unsigned long)scrq->msgs, 2);
1343zero_page_failed:
1344 kfree(scrq);
1345
1346 return NULL;
1347}
1348
1349static void release_sub_crqs(struct ibmvnic_adapter *adapter)
1350{
1351 int i;
1352
1353 if (adapter->tx_scrq) {
1354 for (i = 0; i < adapter->req_tx_queues; i++)
1355 if (adapter->tx_scrq[i]) {
1356 free_irq(adapter->tx_scrq[i]->irq,
1357 adapter->tx_scrq[i]);
Thomas Falcon88eb98a2016-07-06 15:35:16 -05001358 irq_dispose_mapping(adapter->tx_scrq[i]->irq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001359 release_sub_crq_queue(adapter,
1360 adapter->tx_scrq[i]);
1361 }
Nathan Fontenot9501df32017-03-15 23:38:07 -04001362 kfree(adapter->tx_scrq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001363 adapter->tx_scrq = NULL;
1364 }
1365
1366 if (adapter->rx_scrq) {
1367 for (i = 0; i < adapter->req_rx_queues; i++)
1368 if (adapter->rx_scrq[i]) {
1369 free_irq(adapter->rx_scrq[i]->irq,
1370 adapter->rx_scrq[i]);
Thomas Falcon88eb98a2016-07-06 15:35:16 -05001371 irq_dispose_mapping(adapter->rx_scrq[i]->irq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001372 release_sub_crq_queue(adapter,
1373 adapter->rx_scrq[i]);
1374 }
Nathan Fontenot9501df32017-03-15 23:38:07 -04001375 kfree(adapter->rx_scrq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001376 adapter->rx_scrq = NULL;
1377 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001378}
1379
Thomas Falconea22d512016-07-06 15:35:17 -05001380static void release_sub_crqs_no_irqs(struct ibmvnic_adapter *adapter)
1381{
1382 int i;
1383
1384 if (adapter->tx_scrq) {
1385 for (i = 0; i < adapter->req_tx_queues; i++)
1386 if (adapter->tx_scrq[i])
1387 release_sub_crq_queue(adapter,
1388 adapter->tx_scrq[i]);
1389 adapter->tx_scrq = NULL;
1390 }
1391
1392 if (adapter->rx_scrq) {
1393 for (i = 0; i < adapter->req_rx_queues; i++)
1394 if (adapter->rx_scrq[i])
1395 release_sub_crq_queue(adapter,
1396 adapter->rx_scrq[i]);
1397 adapter->rx_scrq = NULL;
1398 }
Thomas Falconea22d512016-07-06 15:35:17 -05001399}
1400
Thomas Falcon032c5e82015-12-21 11:26:06 -06001401static int disable_scrq_irq(struct ibmvnic_adapter *adapter,
1402 struct ibmvnic_sub_crq_queue *scrq)
1403{
1404 struct device *dev = &adapter->vdev->dev;
1405 unsigned long rc;
1406
1407 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
1408 H_DISABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
1409 if (rc)
1410 dev_err(dev, "Couldn't disable scrq irq 0x%lx. rc=%ld\n",
1411 scrq->hw_irq, rc);
1412 return rc;
1413}
1414
1415static int enable_scrq_irq(struct ibmvnic_adapter *adapter,
1416 struct ibmvnic_sub_crq_queue *scrq)
1417{
1418 struct device *dev = &adapter->vdev->dev;
1419 unsigned long rc;
1420
1421 if (scrq->hw_irq > 0x100000000ULL) {
1422 dev_err(dev, "bad hw_irq = %lx\n", scrq->hw_irq);
1423 return 1;
1424 }
1425
1426 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
1427 H_ENABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
1428 if (rc)
1429 dev_err(dev, "Couldn't enable scrq irq 0x%lx. rc=%ld\n",
1430 scrq->hw_irq, rc);
1431 return rc;
1432}
1433
1434static int ibmvnic_complete_tx(struct ibmvnic_adapter *adapter,
1435 struct ibmvnic_sub_crq_queue *scrq)
1436{
1437 struct device *dev = &adapter->vdev->dev;
1438 struct ibmvnic_tx_buff *txbuff;
1439 union sub_crq *next;
1440 int index;
1441 int i, j;
Thomas Falconad7775d2016-04-01 17:20:34 -05001442 u8 first;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001443
1444restart_loop:
1445 while (pending_scrq(adapter, scrq)) {
1446 unsigned int pool = scrq->pool_index;
1447
1448 next = ibmvnic_next_scrq(adapter, scrq);
1449 for (i = 0; i < next->tx_comp.num_comps; i++) {
1450 if (next->tx_comp.rcs[i]) {
1451 dev_err(dev, "tx error %x\n",
1452 next->tx_comp.rcs[i]);
1453 continue;
1454 }
1455 index = be32_to_cpu(next->tx_comp.correlators[i]);
1456 txbuff = &adapter->tx_pool[pool].tx_buff[index];
1457
1458 for (j = 0; j < IBMVNIC_MAX_FRAGS_PER_CRQ; j++) {
1459 if (!txbuff->data_dma[j])
1460 continue;
1461
1462 txbuff->data_dma[j] = 0;
1463 txbuff->used_bounce = false;
1464 }
Thomas Falconad7775d2016-04-01 17:20:34 -05001465 /* if sub_crq was sent indirectly */
1466 first = txbuff->indir_arr[0].generic.first;
1467 if (first == IBMVNIC_CRQ_CMD) {
1468 dma_unmap_single(dev, txbuff->indir_dma,
1469 sizeof(txbuff->indir_arr),
1470 DMA_TO_DEVICE);
1471 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001472
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001473 if (txbuff->last_frag) {
1474 atomic_dec(&scrq->used);
1475
1476 if (atomic_read(&scrq->used) <=
1477 (adapter->req_tx_entries_per_subcrq / 2) &&
1478 netif_subqueue_stopped(adapter->netdev,
1479 txbuff->skb)) {
1480 netif_wake_subqueue(adapter->netdev,
1481 scrq->pool_index);
1482 netdev_dbg(adapter->netdev,
1483 "Started queue %d\n",
1484 scrq->pool_index);
1485 }
1486
Thomas Falcon032c5e82015-12-21 11:26:06 -06001487 dev_kfree_skb_any(txbuff->skb);
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001488 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001489
1490 adapter->tx_pool[pool].free_map[adapter->tx_pool[pool].
1491 producer_index] = index;
1492 adapter->tx_pool[pool].producer_index =
1493 (adapter->tx_pool[pool].producer_index + 1) %
Thomas Falcon068d9f92017-03-05 12:18:42 -06001494 adapter->req_tx_entries_per_subcrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001495 }
1496 /* remove tx_comp scrq*/
1497 next->tx_comp.first = 0;
1498 }
1499
1500 enable_scrq_irq(adapter, scrq);
1501
1502 if (pending_scrq(adapter, scrq)) {
1503 disable_scrq_irq(adapter, scrq);
1504 goto restart_loop;
1505 }
1506
1507 return 0;
1508}
1509
1510static irqreturn_t ibmvnic_interrupt_tx(int irq, void *instance)
1511{
1512 struct ibmvnic_sub_crq_queue *scrq = instance;
1513 struct ibmvnic_adapter *adapter = scrq->adapter;
1514
1515 disable_scrq_irq(adapter, scrq);
1516 ibmvnic_complete_tx(adapter, scrq);
1517
1518 return IRQ_HANDLED;
1519}
1520
1521static irqreturn_t ibmvnic_interrupt_rx(int irq, void *instance)
1522{
1523 struct ibmvnic_sub_crq_queue *scrq = instance;
1524 struct ibmvnic_adapter *adapter = scrq->adapter;
1525
1526 if (napi_schedule_prep(&adapter->napi[scrq->scrq_num])) {
1527 disable_scrq_irq(adapter, scrq);
1528 __napi_schedule(&adapter->napi[scrq->scrq_num]);
1529 }
1530
1531 return IRQ_HANDLED;
1532}
1533
Thomas Falconea22d512016-07-06 15:35:17 -05001534static int init_sub_crq_irqs(struct ibmvnic_adapter *adapter)
1535{
1536 struct device *dev = &adapter->vdev->dev;
1537 struct ibmvnic_sub_crq_queue *scrq;
1538 int i = 0, j = 0;
1539 int rc = 0;
1540
1541 for (i = 0; i < adapter->req_tx_queues; i++) {
1542 scrq = adapter->tx_scrq[i];
1543 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
1544
Michael Ellerman99c17902016-09-10 19:59:05 +10001545 if (!scrq->irq) {
Thomas Falconea22d512016-07-06 15:35:17 -05001546 rc = -EINVAL;
1547 dev_err(dev, "Error mapping irq\n");
1548 goto req_tx_irq_failed;
1549 }
1550
1551 rc = request_irq(scrq->irq, ibmvnic_interrupt_tx,
1552 0, "ibmvnic_tx", scrq);
1553
1554 if (rc) {
1555 dev_err(dev, "Couldn't register tx irq 0x%x. rc=%d\n",
1556 scrq->irq, rc);
1557 irq_dispose_mapping(scrq->irq);
1558 goto req_rx_irq_failed;
1559 }
1560 }
1561
1562 for (i = 0; i < adapter->req_rx_queues; i++) {
1563 scrq = adapter->rx_scrq[i];
1564 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
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_rx_irq_failed;
1569 }
1570 rc = request_irq(scrq->irq, ibmvnic_interrupt_rx,
1571 0, "ibmvnic_rx", scrq);
1572 if (rc) {
1573 dev_err(dev, "Couldn't register rx irq 0x%x. rc=%d\n",
1574 scrq->irq, rc);
1575 irq_dispose_mapping(scrq->irq);
1576 goto req_rx_irq_failed;
1577 }
1578 }
1579 return rc;
1580
1581req_rx_irq_failed:
Thomas Falcon8bf371e2016-10-27 12:28:52 -05001582 for (j = 0; j < i; j++) {
Thomas Falconea22d512016-07-06 15:35:17 -05001583 free_irq(adapter->rx_scrq[j]->irq, adapter->rx_scrq[j]);
1584 irq_dispose_mapping(adapter->rx_scrq[j]->irq);
Thomas Falcon8bf371e2016-10-27 12:28:52 -05001585 }
Thomas Falconea22d512016-07-06 15:35:17 -05001586 i = adapter->req_tx_queues;
1587req_tx_irq_failed:
Thomas Falcon8bf371e2016-10-27 12:28:52 -05001588 for (j = 0; j < i; j++) {
Thomas Falconea22d512016-07-06 15:35:17 -05001589 free_irq(adapter->tx_scrq[j]->irq, adapter->tx_scrq[j]);
1590 irq_dispose_mapping(adapter->rx_scrq[j]->irq);
Thomas Falcon8bf371e2016-10-27 12:28:52 -05001591 }
Thomas Falconea22d512016-07-06 15:35:17 -05001592 release_sub_crqs_no_irqs(adapter);
1593 return rc;
1594}
1595
Thomas Falcon032c5e82015-12-21 11:26:06 -06001596static void init_sub_crqs(struct ibmvnic_adapter *adapter, int retry)
1597{
1598 struct device *dev = &adapter->vdev->dev;
1599 struct ibmvnic_sub_crq_queue **allqueues;
1600 int registered_queues = 0;
1601 union ibmvnic_crq crq;
1602 int total_queues;
1603 int more = 0;
Thomas Falconea22d512016-07-06 15:35:17 -05001604 int i;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001605
1606 if (!retry) {
1607 /* Sub-CRQ entries are 32 byte long */
1608 int entries_page = 4 * PAGE_SIZE / (sizeof(u64) * 4);
1609
1610 if (adapter->min_tx_entries_per_subcrq > entries_page ||
1611 adapter->min_rx_add_entries_per_subcrq > entries_page) {
1612 dev_err(dev, "Fatal, invalid entries per sub-crq\n");
1613 goto allqueues_failed;
1614 }
1615
1616 /* Get the minimum between the queried max and the entries
1617 * that fit in our PAGE_SIZE
1618 */
1619 adapter->req_tx_entries_per_subcrq =
1620 adapter->max_tx_entries_per_subcrq > entries_page ?
1621 entries_page : adapter->max_tx_entries_per_subcrq;
1622 adapter->req_rx_add_entries_per_subcrq =
1623 adapter->max_rx_add_entries_per_subcrq > entries_page ?
1624 entries_page : adapter->max_rx_add_entries_per_subcrq;
1625
John Allen6dbcd8f2016-11-07 14:27:28 -06001626 adapter->req_tx_queues = adapter->opt_tx_comp_sub_queues;
1627 adapter->req_rx_queues = adapter->opt_rx_comp_queues;
John Allen498cd8e2016-04-06 11:49:55 -05001628 adapter->req_rx_add_queues = adapter->max_rx_add_queues;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001629
Thomas Falconf39f0d12017-02-14 10:22:59 -06001630 adapter->req_mtu = adapter->netdev->mtu + ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001631 }
1632
1633 total_queues = adapter->req_tx_queues + adapter->req_rx_queues;
1634
1635 allqueues = kcalloc(total_queues, sizeof(*allqueues), GFP_ATOMIC);
1636 if (!allqueues)
1637 goto allqueues_failed;
1638
1639 for (i = 0; i < total_queues; i++) {
1640 allqueues[i] = init_sub_crq_queue(adapter);
1641 if (!allqueues[i]) {
1642 dev_warn(dev, "Couldn't allocate all sub-crqs\n");
1643 break;
1644 }
1645 registered_queues++;
1646 }
1647
1648 /* Make sure we were able to register the minimum number of queues */
1649 if (registered_queues <
1650 adapter->min_tx_queues + adapter->min_rx_queues) {
1651 dev_err(dev, "Fatal: Couldn't init min number of sub-crqs\n");
1652 goto tx_failed;
1653 }
1654
1655 /* Distribute the failed allocated queues*/
1656 for (i = 0; i < total_queues - registered_queues + more ; i++) {
1657 netdev_dbg(adapter->netdev, "Reducing number of queues\n");
1658 switch (i % 3) {
1659 case 0:
1660 if (adapter->req_rx_queues > adapter->min_rx_queues)
1661 adapter->req_rx_queues--;
1662 else
1663 more++;
1664 break;
1665 case 1:
1666 if (adapter->req_tx_queues > adapter->min_tx_queues)
1667 adapter->req_tx_queues--;
1668 else
1669 more++;
1670 break;
1671 }
1672 }
1673
1674 adapter->tx_scrq = kcalloc(adapter->req_tx_queues,
1675 sizeof(*adapter->tx_scrq), GFP_ATOMIC);
1676 if (!adapter->tx_scrq)
1677 goto tx_failed;
1678
1679 for (i = 0; i < adapter->req_tx_queues; i++) {
1680 adapter->tx_scrq[i] = allqueues[i];
1681 adapter->tx_scrq[i]->pool_index = i;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001682 }
1683
1684 adapter->rx_scrq = kcalloc(adapter->req_rx_queues,
1685 sizeof(*adapter->rx_scrq), GFP_ATOMIC);
1686 if (!adapter->rx_scrq)
1687 goto rx_failed;
1688
1689 for (i = 0; i < adapter->req_rx_queues; i++) {
1690 adapter->rx_scrq[i] = allqueues[i + adapter->req_tx_queues];
1691 adapter->rx_scrq[i]->scrq_num = i;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001692 }
1693
1694 memset(&crq, 0, sizeof(crq));
1695 crq.request_capability.first = IBMVNIC_CRQ_CMD;
1696 crq.request_capability.cmd = REQUEST_CAPABILITY;
1697
1698 crq.request_capability.capability = cpu_to_be16(REQ_TX_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06001699 crq.request_capability.number = cpu_to_be64(adapter->req_tx_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06001700 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001701 ibmvnic_send_crq(adapter, &crq);
1702
1703 crq.request_capability.capability = cpu_to_be16(REQ_RX_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06001704 crq.request_capability.number = cpu_to_be64(adapter->req_rx_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06001705 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001706 ibmvnic_send_crq(adapter, &crq);
1707
1708 crq.request_capability.capability = cpu_to_be16(REQ_RX_ADD_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06001709 crq.request_capability.number = cpu_to_be64(adapter->req_rx_add_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06001710 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001711 ibmvnic_send_crq(adapter, &crq);
1712
1713 crq.request_capability.capability =
1714 cpu_to_be16(REQ_TX_ENTRIES_PER_SUBCRQ);
1715 crq.request_capability.number =
Thomas Falconde89e852016-03-01 10:20:09 -06001716 cpu_to_be64(adapter->req_tx_entries_per_subcrq);
Thomas Falcon901e0402017-02-15 12:17:59 -06001717 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001718 ibmvnic_send_crq(adapter, &crq);
1719
1720 crq.request_capability.capability =
1721 cpu_to_be16(REQ_RX_ADD_ENTRIES_PER_SUBCRQ);
1722 crq.request_capability.number =
Thomas Falconde89e852016-03-01 10:20:09 -06001723 cpu_to_be64(adapter->req_rx_add_entries_per_subcrq);
Thomas Falcon901e0402017-02-15 12:17:59 -06001724 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001725 ibmvnic_send_crq(adapter, &crq);
1726
1727 crq.request_capability.capability = cpu_to_be16(REQ_MTU);
Thomas Falconde89e852016-03-01 10:20:09 -06001728 crq.request_capability.number = cpu_to_be64(adapter->req_mtu);
Thomas Falcon901e0402017-02-15 12:17:59 -06001729 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001730 ibmvnic_send_crq(adapter, &crq);
1731
1732 if (adapter->netdev->flags & IFF_PROMISC) {
1733 if (adapter->promisc_supported) {
1734 crq.request_capability.capability =
1735 cpu_to_be16(PROMISC_REQUESTED);
Thomas Falconde89e852016-03-01 10:20:09 -06001736 crq.request_capability.number = cpu_to_be64(1);
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 } else {
1741 crq.request_capability.capability =
1742 cpu_to_be16(PROMISC_REQUESTED);
Thomas Falconde89e852016-03-01 10:20:09 -06001743 crq.request_capability.number = cpu_to_be64(0);
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
1748 kfree(allqueues);
1749
1750 return;
1751
Thomas Falcon032c5e82015-12-21 11:26:06 -06001752rx_failed:
1753 kfree(adapter->tx_scrq);
1754 adapter->tx_scrq = NULL;
1755tx_failed:
1756 for (i = 0; i < registered_queues; i++)
1757 release_sub_crq_queue(adapter, allqueues[i]);
1758 kfree(allqueues);
1759allqueues_failed:
1760 ibmvnic_remove(adapter->vdev);
1761}
1762
1763static int pending_scrq(struct ibmvnic_adapter *adapter,
1764 struct ibmvnic_sub_crq_queue *scrq)
1765{
1766 union sub_crq *entry = &scrq->msgs[scrq->cur];
1767
1768 if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP || adapter->closing)
1769 return 1;
1770 else
1771 return 0;
1772}
1773
1774static union sub_crq *ibmvnic_next_scrq(struct ibmvnic_adapter *adapter,
1775 struct ibmvnic_sub_crq_queue *scrq)
1776{
1777 union sub_crq *entry;
1778 unsigned long flags;
1779
1780 spin_lock_irqsave(&scrq->lock, flags);
1781 entry = &scrq->msgs[scrq->cur];
1782 if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP) {
1783 if (++scrq->cur == scrq->size)
1784 scrq->cur = 0;
1785 } else {
1786 entry = NULL;
1787 }
1788 spin_unlock_irqrestore(&scrq->lock, flags);
1789
1790 return entry;
1791}
1792
1793static union ibmvnic_crq *ibmvnic_next_crq(struct ibmvnic_adapter *adapter)
1794{
1795 struct ibmvnic_crq_queue *queue = &adapter->crq;
1796 union ibmvnic_crq *crq;
1797
1798 crq = &queue->msgs[queue->cur];
1799 if (crq->generic.first & IBMVNIC_CRQ_CMD_RSP) {
1800 if (++queue->cur == queue->size)
1801 queue->cur = 0;
1802 } else {
1803 crq = NULL;
1804 }
1805
1806 return crq;
1807}
1808
1809static int send_subcrq(struct ibmvnic_adapter *adapter, u64 remote_handle,
1810 union sub_crq *sub_crq)
1811{
1812 unsigned int ua = adapter->vdev->unit_address;
1813 struct device *dev = &adapter->vdev->dev;
1814 u64 *u64_crq = (u64 *)sub_crq;
1815 int rc;
1816
1817 netdev_dbg(adapter->netdev,
1818 "Sending sCRQ %016lx: %016lx %016lx %016lx %016lx\n",
1819 (unsigned long int)cpu_to_be64(remote_handle),
1820 (unsigned long int)cpu_to_be64(u64_crq[0]),
1821 (unsigned long int)cpu_to_be64(u64_crq[1]),
1822 (unsigned long int)cpu_to_be64(u64_crq[2]),
1823 (unsigned long int)cpu_to_be64(u64_crq[3]));
1824
1825 /* Make sure the hypervisor sees the complete request */
1826 mb();
1827
1828 rc = plpar_hcall_norets(H_SEND_SUB_CRQ, ua,
1829 cpu_to_be64(remote_handle),
1830 cpu_to_be64(u64_crq[0]),
1831 cpu_to_be64(u64_crq[1]),
1832 cpu_to_be64(u64_crq[2]),
1833 cpu_to_be64(u64_crq[3]));
1834
1835 if (rc) {
1836 if (rc == H_CLOSED)
1837 dev_warn(dev, "CRQ Queue closed\n");
1838 dev_err(dev, "Send error (rc=%d)\n", rc);
1839 }
1840
1841 return rc;
1842}
1843
Thomas Falconad7775d2016-04-01 17:20:34 -05001844static int send_subcrq_indirect(struct ibmvnic_adapter *adapter,
1845 u64 remote_handle, u64 ioba, u64 num_entries)
1846{
1847 unsigned int ua = adapter->vdev->unit_address;
1848 struct device *dev = &adapter->vdev->dev;
1849 int rc;
1850
1851 /* Make sure the hypervisor sees the complete request */
1852 mb();
1853 rc = plpar_hcall_norets(H_SEND_SUB_CRQ_INDIRECT, ua,
1854 cpu_to_be64(remote_handle),
1855 ioba, num_entries);
1856
1857 if (rc) {
1858 if (rc == H_CLOSED)
1859 dev_warn(dev, "CRQ Queue closed\n");
1860 dev_err(dev, "Send (indirect) error (rc=%d)\n", rc);
1861 }
1862
1863 return rc;
1864}
1865
Thomas Falcon032c5e82015-12-21 11:26:06 -06001866static int ibmvnic_send_crq(struct ibmvnic_adapter *adapter,
1867 union ibmvnic_crq *crq)
1868{
1869 unsigned int ua = adapter->vdev->unit_address;
1870 struct device *dev = &adapter->vdev->dev;
1871 u64 *u64_crq = (u64 *)crq;
1872 int rc;
1873
1874 netdev_dbg(adapter->netdev, "Sending CRQ: %016lx %016lx\n",
1875 (unsigned long int)cpu_to_be64(u64_crq[0]),
1876 (unsigned long int)cpu_to_be64(u64_crq[1]));
1877
1878 /* Make sure the hypervisor sees the complete request */
1879 mb();
1880
1881 rc = plpar_hcall_norets(H_SEND_CRQ, ua,
1882 cpu_to_be64(u64_crq[0]),
1883 cpu_to_be64(u64_crq[1]));
1884
1885 if (rc) {
1886 if (rc == H_CLOSED)
1887 dev_warn(dev, "CRQ Queue closed\n");
1888 dev_warn(dev, "Send error (rc=%d)\n", rc);
1889 }
1890
1891 return rc;
1892}
1893
1894static int ibmvnic_send_crq_init(struct ibmvnic_adapter *adapter)
1895{
1896 union ibmvnic_crq crq;
1897
1898 memset(&crq, 0, sizeof(crq));
1899 crq.generic.first = IBMVNIC_CRQ_INIT_CMD;
1900 crq.generic.cmd = IBMVNIC_CRQ_INIT;
1901 netdev_dbg(adapter->netdev, "Sending CRQ init\n");
1902
1903 return ibmvnic_send_crq(adapter, &crq);
1904}
1905
1906static int ibmvnic_send_crq_init_complete(struct ibmvnic_adapter *adapter)
1907{
1908 union ibmvnic_crq crq;
1909
1910 memset(&crq, 0, sizeof(crq));
1911 crq.generic.first = IBMVNIC_CRQ_INIT_CMD;
1912 crq.generic.cmd = IBMVNIC_CRQ_INIT_COMPLETE;
1913 netdev_dbg(adapter->netdev, "Sending CRQ init complete\n");
1914
1915 return ibmvnic_send_crq(adapter, &crq);
1916}
1917
1918static int send_version_xchg(struct ibmvnic_adapter *adapter)
1919{
1920 union ibmvnic_crq crq;
1921
1922 memset(&crq, 0, sizeof(crq));
1923 crq.version_exchange.first = IBMVNIC_CRQ_CMD;
1924 crq.version_exchange.cmd = VERSION_EXCHANGE;
1925 crq.version_exchange.version = cpu_to_be16(ibmvnic_version);
1926
1927 return ibmvnic_send_crq(adapter, &crq);
1928}
1929
1930static void send_login(struct ibmvnic_adapter *adapter)
1931{
1932 struct ibmvnic_login_rsp_buffer *login_rsp_buffer;
1933 struct ibmvnic_login_buffer *login_buffer;
1934 struct ibmvnic_inflight_cmd *inflight_cmd;
1935 struct device *dev = &adapter->vdev->dev;
1936 dma_addr_t rsp_buffer_token;
1937 dma_addr_t buffer_token;
1938 size_t rsp_buffer_size;
1939 union ibmvnic_crq crq;
1940 unsigned long flags;
1941 size_t buffer_size;
1942 __be64 *tx_list_p;
1943 __be64 *rx_list_p;
1944 int i;
1945
1946 buffer_size =
1947 sizeof(struct ibmvnic_login_buffer) +
1948 sizeof(u64) * (adapter->req_tx_queues + adapter->req_rx_queues);
1949
1950 login_buffer = kmalloc(buffer_size, GFP_ATOMIC);
1951 if (!login_buffer)
1952 goto buf_alloc_failed;
1953
1954 buffer_token = dma_map_single(dev, login_buffer, buffer_size,
1955 DMA_TO_DEVICE);
1956 if (dma_mapping_error(dev, buffer_token)) {
1957 dev_err(dev, "Couldn't map login buffer\n");
1958 goto buf_map_failed;
1959 }
1960
John Allen498cd8e2016-04-06 11:49:55 -05001961 rsp_buffer_size = sizeof(struct ibmvnic_login_rsp_buffer) +
1962 sizeof(u64) * adapter->req_tx_queues +
1963 sizeof(u64) * adapter->req_rx_queues +
1964 sizeof(u64) * adapter->req_rx_queues +
1965 sizeof(u8) * IBMVNIC_TX_DESC_VERSIONS;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001966
1967 login_rsp_buffer = kmalloc(rsp_buffer_size, GFP_ATOMIC);
1968 if (!login_rsp_buffer)
1969 goto buf_rsp_alloc_failed;
1970
1971 rsp_buffer_token = dma_map_single(dev, login_rsp_buffer,
1972 rsp_buffer_size, DMA_FROM_DEVICE);
1973 if (dma_mapping_error(dev, rsp_buffer_token)) {
1974 dev_err(dev, "Couldn't map login rsp buffer\n");
1975 goto buf_rsp_map_failed;
1976 }
1977 inflight_cmd = kmalloc(sizeof(*inflight_cmd), GFP_ATOMIC);
1978 if (!inflight_cmd) {
1979 dev_err(dev, "Couldn't allocate inflight_cmd\n");
1980 goto inflight_alloc_failed;
1981 }
1982 adapter->login_buf = login_buffer;
1983 adapter->login_buf_token = buffer_token;
1984 adapter->login_buf_sz = buffer_size;
1985 adapter->login_rsp_buf = login_rsp_buffer;
1986 adapter->login_rsp_buf_token = rsp_buffer_token;
1987 adapter->login_rsp_buf_sz = rsp_buffer_size;
1988
1989 login_buffer->len = cpu_to_be32(buffer_size);
1990 login_buffer->version = cpu_to_be32(INITIAL_VERSION_LB);
1991 login_buffer->num_txcomp_subcrqs = cpu_to_be32(adapter->req_tx_queues);
1992 login_buffer->off_txcomp_subcrqs =
1993 cpu_to_be32(sizeof(struct ibmvnic_login_buffer));
1994 login_buffer->num_rxcomp_subcrqs = cpu_to_be32(adapter->req_rx_queues);
1995 login_buffer->off_rxcomp_subcrqs =
1996 cpu_to_be32(sizeof(struct ibmvnic_login_buffer) +
1997 sizeof(u64) * adapter->req_tx_queues);
1998 login_buffer->login_rsp_ioba = cpu_to_be32(rsp_buffer_token);
1999 login_buffer->login_rsp_len = cpu_to_be32(rsp_buffer_size);
2000
2001 tx_list_p = (__be64 *)((char *)login_buffer +
2002 sizeof(struct ibmvnic_login_buffer));
2003 rx_list_p = (__be64 *)((char *)login_buffer +
2004 sizeof(struct ibmvnic_login_buffer) +
2005 sizeof(u64) * adapter->req_tx_queues);
2006
2007 for (i = 0; i < adapter->req_tx_queues; i++) {
2008 if (adapter->tx_scrq[i]) {
2009 tx_list_p[i] = cpu_to_be64(adapter->tx_scrq[i]->
2010 crq_num);
2011 }
2012 }
2013
2014 for (i = 0; i < adapter->req_rx_queues; i++) {
2015 if (adapter->rx_scrq[i]) {
2016 rx_list_p[i] = cpu_to_be64(adapter->rx_scrq[i]->
2017 crq_num);
2018 }
2019 }
2020
2021 netdev_dbg(adapter->netdev, "Login Buffer:\n");
2022 for (i = 0; i < (adapter->login_buf_sz - 1) / 8 + 1; i++) {
2023 netdev_dbg(adapter->netdev, "%016lx\n",
2024 ((unsigned long int *)(adapter->login_buf))[i]);
2025 }
2026
2027 memset(&crq, 0, sizeof(crq));
2028 crq.login.first = IBMVNIC_CRQ_CMD;
2029 crq.login.cmd = LOGIN;
2030 crq.login.ioba = cpu_to_be32(buffer_token);
2031 crq.login.len = cpu_to_be32(buffer_size);
2032
2033 memcpy(&inflight_cmd->crq, &crq, sizeof(crq));
2034
2035 spin_lock_irqsave(&adapter->inflight_lock, flags);
2036 list_add_tail(&inflight_cmd->list, &adapter->inflight);
2037 spin_unlock_irqrestore(&adapter->inflight_lock, flags);
2038
2039 ibmvnic_send_crq(adapter, &crq);
2040
2041 return;
2042
2043inflight_alloc_failed:
2044 dma_unmap_single(dev, rsp_buffer_token, rsp_buffer_size,
2045 DMA_FROM_DEVICE);
2046buf_rsp_map_failed:
2047 kfree(login_rsp_buffer);
2048buf_rsp_alloc_failed:
2049 dma_unmap_single(dev, buffer_token, buffer_size, DMA_TO_DEVICE);
2050buf_map_failed:
2051 kfree(login_buffer);
2052buf_alloc_failed:
2053 return;
2054}
2055
2056static void send_request_map(struct ibmvnic_adapter *adapter, dma_addr_t addr,
2057 u32 len, u8 map_id)
2058{
2059 union ibmvnic_crq crq;
2060
2061 memset(&crq, 0, sizeof(crq));
2062 crq.request_map.first = IBMVNIC_CRQ_CMD;
2063 crq.request_map.cmd = REQUEST_MAP;
2064 crq.request_map.map_id = map_id;
2065 crq.request_map.ioba = cpu_to_be32(addr);
2066 crq.request_map.len = cpu_to_be32(len);
2067 ibmvnic_send_crq(adapter, &crq);
2068}
2069
2070static void send_request_unmap(struct ibmvnic_adapter *adapter, u8 map_id)
2071{
2072 union ibmvnic_crq crq;
2073
2074 memset(&crq, 0, sizeof(crq));
2075 crq.request_unmap.first = IBMVNIC_CRQ_CMD;
2076 crq.request_unmap.cmd = REQUEST_UNMAP;
2077 crq.request_unmap.map_id = map_id;
2078 ibmvnic_send_crq(adapter, &crq);
2079}
2080
2081static void send_map_query(struct ibmvnic_adapter *adapter)
2082{
2083 union ibmvnic_crq crq;
2084
2085 memset(&crq, 0, sizeof(crq));
2086 crq.query_map.first = IBMVNIC_CRQ_CMD;
2087 crq.query_map.cmd = QUERY_MAP;
2088 ibmvnic_send_crq(adapter, &crq);
2089}
2090
2091/* Send a series of CRQs requesting various capabilities of the VNIC server */
2092static void send_cap_queries(struct ibmvnic_adapter *adapter)
2093{
2094 union ibmvnic_crq crq;
2095
Thomas Falcon901e0402017-02-15 12:17:59 -06002096 atomic_set(&adapter->running_cap_crqs, 0);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002097 memset(&crq, 0, sizeof(crq));
2098 crq.query_capability.first = IBMVNIC_CRQ_CMD;
2099 crq.query_capability.cmd = QUERY_CAPABILITY;
2100
2101 crq.query_capability.capability = cpu_to_be16(MIN_TX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002102 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002103 ibmvnic_send_crq(adapter, &crq);
2104
2105 crq.query_capability.capability = cpu_to_be16(MIN_RX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002106 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002107 ibmvnic_send_crq(adapter, &crq);
2108
2109 crq.query_capability.capability = cpu_to_be16(MIN_RX_ADD_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002110 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002111 ibmvnic_send_crq(adapter, &crq);
2112
2113 crq.query_capability.capability = cpu_to_be16(MAX_TX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002114 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002115 ibmvnic_send_crq(adapter, &crq);
2116
2117 crq.query_capability.capability = cpu_to_be16(MAX_RX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002118 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002119 ibmvnic_send_crq(adapter, &crq);
2120
2121 crq.query_capability.capability = cpu_to_be16(MAX_RX_ADD_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 =
2126 cpu_to_be16(MIN_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06002127 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002128 ibmvnic_send_crq(adapter, &crq);
2129
2130 crq.query_capability.capability =
2131 cpu_to_be16(MIN_RX_ADD_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06002132 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002133 ibmvnic_send_crq(adapter, &crq);
2134
2135 crq.query_capability.capability =
2136 cpu_to_be16(MAX_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06002137 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002138 ibmvnic_send_crq(adapter, &crq);
2139
2140 crq.query_capability.capability =
2141 cpu_to_be16(MAX_RX_ADD_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06002142 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002143 ibmvnic_send_crq(adapter, &crq);
2144
2145 crq.query_capability.capability = cpu_to_be16(TCP_IP_OFFLOAD);
Thomas Falcon901e0402017-02-15 12:17:59 -06002146 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002147 ibmvnic_send_crq(adapter, &crq);
2148
2149 crq.query_capability.capability = cpu_to_be16(PROMISC_SUPPORTED);
Thomas Falcon901e0402017-02-15 12:17:59 -06002150 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002151 ibmvnic_send_crq(adapter, &crq);
2152
2153 crq.query_capability.capability = cpu_to_be16(MIN_MTU);
Thomas Falcon901e0402017-02-15 12:17:59 -06002154 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002155 ibmvnic_send_crq(adapter, &crq);
2156
2157 crq.query_capability.capability = cpu_to_be16(MAX_MTU);
Thomas Falcon901e0402017-02-15 12:17:59 -06002158 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002159 ibmvnic_send_crq(adapter, &crq);
2160
2161 crq.query_capability.capability = cpu_to_be16(MAX_MULTICAST_FILTERS);
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(VLAN_HEADER_INSERTION);
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(MAX_TX_SG_ENTRIES);
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(RX_SG_SUPPORTED);
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(OPT_TX_COMP_SUB_QUEUES);
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(OPT_RX_COMP_QUEUES);
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 =
2186 cpu_to_be16(OPT_RX_BUFADD_Q_PER_RX_COMP_Q);
Thomas Falcon901e0402017-02-15 12:17:59 -06002187 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002188 ibmvnic_send_crq(adapter, &crq);
2189
2190 crq.query_capability.capability =
2191 cpu_to_be16(OPT_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06002192 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002193 ibmvnic_send_crq(adapter, &crq);
2194
2195 crq.query_capability.capability =
2196 cpu_to_be16(OPT_RXBA_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06002197 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002198 ibmvnic_send_crq(adapter, &crq);
2199
2200 crq.query_capability.capability = cpu_to_be16(TX_RX_DESC_REQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06002201 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002202 ibmvnic_send_crq(adapter, &crq);
2203}
2204
2205static void handle_query_ip_offload_rsp(struct ibmvnic_adapter *adapter)
2206{
2207 struct device *dev = &adapter->vdev->dev;
2208 struct ibmvnic_query_ip_offload_buffer *buf = &adapter->ip_offload_buf;
2209 union ibmvnic_crq crq;
2210 int i;
2211
2212 dma_unmap_single(dev, adapter->ip_offload_tok,
2213 sizeof(adapter->ip_offload_buf), DMA_FROM_DEVICE);
2214
2215 netdev_dbg(adapter->netdev, "Query IP Offload Buffer:\n");
2216 for (i = 0; i < (sizeof(adapter->ip_offload_buf) - 1) / 8 + 1; i++)
2217 netdev_dbg(adapter->netdev, "%016lx\n",
2218 ((unsigned long int *)(buf))[i]);
2219
2220 netdev_dbg(adapter->netdev, "ipv4_chksum = %d\n", buf->ipv4_chksum);
2221 netdev_dbg(adapter->netdev, "ipv6_chksum = %d\n", buf->ipv6_chksum);
2222 netdev_dbg(adapter->netdev, "tcp_ipv4_chksum = %d\n",
2223 buf->tcp_ipv4_chksum);
2224 netdev_dbg(adapter->netdev, "tcp_ipv6_chksum = %d\n",
2225 buf->tcp_ipv6_chksum);
2226 netdev_dbg(adapter->netdev, "udp_ipv4_chksum = %d\n",
2227 buf->udp_ipv4_chksum);
2228 netdev_dbg(adapter->netdev, "udp_ipv6_chksum = %d\n",
2229 buf->udp_ipv6_chksum);
2230 netdev_dbg(adapter->netdev, "large_tx_ipv4 = %d\n",
2231 buf->large_tx_ipv4);
2232 netdev_dbg(adapter->netdev, "large_tx_ipv6 = %d\n",
2233 buf->large_tx_ipv6);
2234 netdev_dbg(adapter->netdev, "large_rx_ipv4 = %d\n",
2235 buf->large_rx_ipv4);
2236 netdev_dbg(adapter->netdev, "large_rx_ipv6 = %d\n",
2237 buf->large_rx_ipv6);
2238 netdev_dbg(adapter->netdev, "max_ipv4_hdr_sz = %d\n",
2239 buf->max_ipv4_header_size);
2240 netdev_dbg(adapter->netdev, "max_ipv6_hdr_sz = %d\n",
2241 buf->max_ipv6_header_size);
2242 netdev_dbg(adapter->netdev, "max_tcp_hdr_size = %d\n",
2243 buf->max_tcp_header_size);
2244 netdev_dbg(adapter->netdev, "max_udp_hdr_size = %d\n",
2245 buf->max_udp_header_size);
2246 netdev_dbg(adapter->netdev, "max_large_tx_size = %d\n",
2247 buf->max_large_tx_size);
2248 netdev_dbg(adapter->netdev, "max_large_rx_size = %d\n",
2249 buf->max_large_rx_size);
2250 netdev_dbg(adapter->netdev, "ipv6_ext_hdr = %d\n",
2251 buf->ipv6_extension_header);
2252 netdev_dbg(adapter->netdev, "tcp_pseudosum_req = %d\n",
2253 buf->tcp_pseudosum_req);
2254 netdev_dbg(adapter->netdev, "num_ipv6_ext_hd = %d\n",
2255 buf->num_ipv6_ext_headers);
2256 netdev_dbg(adapter->netdev, "off_ipv6_ext_hd = %d\n",
2257 buf->off_ipv6_ext_headers);
2258
2259 adapter->ip_offload_ctrl_tok =
2260 dma_map_single(dev, &adapter->ip_offload_ctrl,
2261 sizeof(adapter->ip_offload_ctrl), DMA_TO_DEVICE);
2262
2263 if (dma_mapping_error(dev, adapter->ip_offload_ctrl_tok)) {
2264 dev_err(dev, "Couldn't map ip offload control buffer\n");
2265 return;
2266 }
2267
2268 adapter->ip_offload_ctrl.version = cpu_to_be32(INITIAL_VERSION_IOB);
2269 adapter->ip_offload_ctrl.tcp_ipv4_chksum = buf->tcp_ipv4_chksum;
2270 adapter->ip_offload_ctrl.udp_ipv4_chksum = buf->udp_ipv4_chksum;
2271 adapter->ip_offload_ctrl.tcp_ipv6_chksum = buf->tcp_ipv6_chksum;
2272 adapter->ip_offload_ctrl.udp_ipv6_chksum = buf->udp_ipv6_chksum;
2273
2274 /* large_tx/rx disabled for now, additional features needed */
2275 adapter->ip_offload_ctrl.large_tx_ipv4 = 0;
2276 adapter->ip_offload_ctrl.large_tx_ipv6 = 0;
2277 adapter->ip_offload_ctrl.large_rx_ipv4 = 0;
2278 adapter->ip_offload_ctrl.large_rx_ipv6 = 0;
2279
2280 adapter->netdev->features = NETIF_F_GSO;
2281
2282 if (buf->tcp_ipv4_chksum || buf->udp_ipv4_chksum)
2283 adapter->netdev->features |= NETIF_F_IP_CSUM;
2284
2285 if (buf->tcp_ipv6_chksum || buf->udp_ipv6_chksum)
2286 adapter->netdev->features |= NETIF_F_IPV6_CSUM;
2287
Thomas Falcon9be02cd2016-04-01 17:20:35 -05002288 if ((adapter->netdev->features &
2289 (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)))
2290 adapter->netdev->features |= NETIF_F_RXCSUM;
2291
Thomas Falcon032c5e82015-12-21 11:26:06 -06002292 memset(&crq, 0, sizeof(crq));
2293 crq.control_ip_offload.first = IBMVNIC_CRQ_CMD;
2294 crq.control_ip_offload.cmd = CONTROL_IP_OFFLOAD;
2295 crq.control_ip_offload.len =
2296 cpu_to_be32(sizeof(adapter->ip_offload_ctrl));
2297 crq.control_ip_offload.ioba = cpu_to_be32(adapter->ip_offload_ctrl_tok);
2298 ibmvnic_send_crq(adapter, &crq);
2299}
2300
2301static void handle_error_info_rsp(union ibmvnic_crq *crq,
2302 struct ibmvnic_adapter *adapter)
2303{
2304 struct device *dev = &adapter->vdev->dev;
Wei Yongjun96183182016-06-27 20:48:53 +08002305 struct ibmvnic_error_buff *error_buff, *tmp;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002306 unsigned long flags;
2307 bool found = false;
2308 int i;
2309
2310 if (!crq->request_error_rsp.rc.code) {
2311 dev_info(dev, "Request Error Rsp returned with rc=%x\n",
2312 crq->request_error_rsp.rc.code);
2313 return;
2314 }
2315
2316 spin_lock_irqsave(&adapter->error_list_lock, flags);
Wei Yongjun96183182016-06-27 20:48:53 +08002317 list_for_each_entry_safe(error_buff, tmp, &adapter->errors, list)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002318 if (error_buff->error_id == crq->request_error_rsp.error_id) {
2319 found = true;
2320 list_del(&error_buff->list);
2321 break;
2322 }
2323 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
2324
2325 if (!found) {
2326 dev_err(dev, "Couldn't find error id %x\n",
Thomas Falcon75224c92017-02-15 10:33:33 -06002327 be32_to_cpu(crq->request_error_rsp.error_id));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002328 return;
2329 }
2330
2331 dev_err(dev, "Detailed info for error id %x:",
Thomas Falcon75224c92017-02-15 10:33:33 -06002332 be32_to_cpu(crq->request_error_rsp.error_id));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002333
2334 for (i = 0; i < error_buff->len; i++) {
2335 pr_cont("%02x", (int)error_buff->buff[i]);
2336 if (i % 8 == 7)
2337 pr_cont(" ");
2338 }
2339 pr_cont("\n");
2340
2341 dma_unmap_single(dev, error_buff->dma, error_buff->len,
2342 DMA_FROM_DEVICE);
2343 kfree(error_buff->buff);
2344 kfree(error_buff);
2345}
2346
Thomas Falcon032c5e82015-12-21 11:26:06 -06002347static void handle_error_indication(union ibmvnic_crq *crq,
2348 struct ibmvnic_adapter *adapter)
2349{
2350 int detail_len = be32_to_cpu(crq->error_indication.detail_error_sz);
2351 struct ibmvnic_inflight_cmd *inflight_cmd;
2352 struct device *dev = &adapter->vdev->dev;
2353 struct ibmvnic_error_buff *error_buff;
2354 union ibmvnic_crq new_crq;
2355 unsigned long flags;
2356
2357 dev_err(dev, "Firmware reports %serror id %x, cause %d\n",
2358 crq->error_indication.
2359 flags & IBMVNIC_FATAL_ERROR ? "FATAL " : "",
Thomas Falcon75224c92017-02-15 10:33:33 -06002360 be32_to_cpu(crq->error_indication.error_id),
2361 be16_to_cpu(crq->error_indication.error_cause));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002362
2363 error_buff = kmalloc(sizeof(*error_buff), GFP_ATOMIC);
2364 if (!error_buff)
2365 return;
2366
2367 error_buff->buff = kmalloc(detail_len, GFP_ATOMIC);
2368 if (!error_buff->buff) {
2369 kfree(error_buff);
2370 return;
2371 }
2372
2373 error_buff->dma = dma_map_single(dev, error_buff->buff, detail_len,
2374 DMA_FROM_DEVICE);
2375 if (dma_mapping_error(dev, error_buff->dma)) {
2376 if (!firmware_has_feature(FW_FEATURE_CMO))
2377 dev_err(dev, "Couldn't map error buffer\n");
2378 kfree(error_buff->buff);
2379 kfree(error_buff);
2380 return;
2381 }
2382
2383 inflight_cmd = kmalloc(sizeof(*inflight_cmd), GFP_ATOMIC);
2384 if (!inflight_cmd) {
2385 dma_unmap_single(dev, error_buff->dma, detail_len,
2386 DMA_FROM_DEVICE);
2387 kfree(error_buff->buff);
2388 kfree(error_buff);
2389 return;
2390 }
2391
2392 error_buff->len = detail_len;
2393 error_buff->error_id = crq->error_indication.error_id;
2394
2395 spin_lock_irqsave(&adapter->error_list_lock, flags);
2396 list_add_tail(&error_buff->list, &adapter->errors);
2397 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
2398
2399 memset(&new_crq, 0, sizeof(new_crq));
2400 new_crq.request_error_info.first = IBMVNIC_CRQ_CMD;
2401 new_crq.request_error_info.cmd = REQUEST_ERROR_INFO;
2402 new_crq.request_error_info.ioba = cpu_to_be32(error_buff->dma);
2403 new_crq.request_error_info.len = cpu_to_be32(detail_len);
2404 new_crq.request_error_info.error_id = crq->error_indication.error_id;
2405
2406 memcpy(&inflight_cmd->crq, &crq, sizeof(crq));
2407
2408 spin_lock_irqsave(&adapter->inflight_lock, flags);
2409 list_add_tail(&inflight_cmd->list, &adapter->inflight);
2410 spin_unlock_irqrestore(&adapter->inflight_lock, flags);
2411
2412 ibmvnic_send_crq(adapter, &new_crq);
2413}
2414
2415static void handle_change_mac_rsp(union ibmvnic_crq *crq,
2416 struct ibmvnic_adapter *adapter)
2417{
2418 struct net_device *netdev = adapter->netdev;
2419 struct device *dev = &adapter->vdev->dev;
2420 long rc;
2421
2422 rc = crq->change_mac_addr_rsp.rc.code;
2423 if (rc) {
2424 dev_err(dev, "Error %ld in CHANGE_MAC_ADDR_RSP\n", rc);
2425 return;
2426 }
2427 memcpy(netdev->dev_addr, &crq->change_mac_addr_rsp.mac_addr[0],
2428 ETH_ALEN);
2429}
2430
2431static void handle_request_cap_rsp(union ibmvnic_crq *crq,
2432 struct ibmvnic_adapter *adapter)
2433{
2434 struct device *dev = &adapter->vdev->dev;
2435 u64 *req_value;
2436 char *name;
2437
Thomas Falcon901e0402017-02-15 12:17:59 -06002438 atomic_dec(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002439 switch (be16_to_cpu(crq->request_capability_rsp.capability)) {
2440 case REQ_TX_QUEUES:
2441 req_value = &adapter->req_tx_queues;
2442 name = "tx";
2443 break;
2444 case REQ_RX_QUEUES:
2445 req_value = &adapter->req_rx_queues;
2446 name = "rx";
2447 break;
2448 case REQ_RX_ADD_QUEUES:
2449 req_value = &adapter->req_rx_add_queues;
2450 name = "rx_add";
2451 break;
2452 case REQ_TX_ENTRIES_PER_SUBCRQ:
2453 req_value = &adapter->req_tx_entries_per_subcrq;
2454 name = "tx_entries_per_subcrq";
2455 break;
2456 case REQ_RX_ADD_ENTRIES_PER_SUBCRQ:
2457 req_value = &adapter->req_rx_add_entries_per_subcrq;
2458 name = "rx_add_entries_per_subcrq";
2459 break;
2460 case REQ_MTU:
2461 req_value = &adapter->req_mtu;
2462 name = "mtu";
2463 break;
2464 case PROMISC_REQUESTED:
2465 req_value = &adapter->promisc;
2466 name = "promisc";
2467 break;
2468 default:
2469 dev_err(dev, "Got invalid cap request rsp %d\n",
2470 crq->request_capability.capability);
2471 return;
2472 }
2473
2474 switch (crq->request_capability_rsp.rc.code) {
2475 case SUCCESS:
2476 break;
2477 case PARTIALSUCCESS:
2478 dev_info(dev, "req=%lld, rsp=%ld in %s queue, retrying.\n",
2479 *req_value,
Thomas Falcon28f4d162017-02-15 10:32:11 -06002480 (long int)be64_to_cpu(crq->request_capability_rsp.
Thomas Falcon032c5e82015-12-21 11:26:06 -06002481 number), name);
Thomas Falconea22d512016-07-06 15:35:17 -05002482 release_sub_crqs_no_irqs(adapter);
Thomas Falcon28f4d162017-02-15 10:32:11 -06002483 *req_value = be64_to_cpu(crq->request_capability_rsp.number);
Thomas Falconea22d512016-07-06 15:35:17 -05002484 init_sub_crqs(adapter, 1);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002485 return;
2486 default:
2487 dev_err(dev, "Error %d in request cap rsp\n",
2488 crq->request_capability_rsp.rc.code);
2489 return;
2490 }
2491
2492 /* Done receiving requested capabilities, query IP offload support */
Thomas Falcon901e0402017-02-15 12:17:59 -06002493 if (atomic_read(&adapter->running_cap_crqs) == 0) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06002494 union ibmvnic_crq newcrq;
2495 int buf_sz = sizeof(struct ibmvnic_query_ip_offload_buffer);
2496 struct ibmvnic_query_ip_offload_buffer *ip_offload_buf =
2497 &adapter->ip_offload_buf;
2498
Thomas Falcon249168a2017-02-15 12:18:00 -06002499 adapter->wait_capability = false;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002500 adapter->ip_offload_tok = dma_map_single(dev, ip_offload_buf,
2501 buf_sz,
2502 DMA_FROM_DEVICE);
2503
2504 if (dma_mapping_error(dev, adapter->ip_offload_tok)) {
2505 if (!firmware_has_feature(FW_FEATURE_CMO))
2506 dev_err(dev, "Couldn't map offload buffer\n");
2507 return;
2508 }
2509
2510 memset(&newcrq, 0, sizeof(newcrq));
2511 newcrq.query_ip_offload.first = IBMVNIC_CRQ_CMD;
2512 newcrq.query_ip_offload.cmd = QUERY_IP_OFFLOAD;
2513 newcrq.query_ip_offload.len = cpu_to_be32(buf_sz);
2514 newcrq.query_ip_offload.ioba =
2515 cpu_to_be32(adapter->ip_offload_tok);
2516
2517 ibmvnic_send_crq(adapter, &newcrq);
2518 }
2519}
2520
2521static int handle_login_rsp(union ibmvnic_crq *login_rsp_crq,
2522 struct ibmvnic_adapter *adapter)
2523{
2524 struct device *dev = &adapter->vdev->dev;
2525 struct ibmvnic_login_rsp_buffer *login_rsp = adapter->login_rsp_buf;
2526 struct ibmvnic_login_buffer *login = adapter->login_buf;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002527 int i;
2528
2529 dma_unmap_single(dev, adapter->login_buf_token, adapter->login_buf_sz,
2530 DMA_BIDIRECTIONAL);
2531 dma_unmap_single(dev, adapter->login_rsp_buf_token,
2532 adapter->login_rsp_buf_sz, DMA_BIDIRECTIONAL);
2533
John Allen498cd8e2016-04-06 11:49:55 -05002534 /* If the number of queues requested can't be allocated by the
2535 * server, the login response will return with code 1. We will need
2536 * to resend the login buffer with fewer queues requested.
2537 */
2538 if (login_rsp_crq->generic.rc.code) {
2539 adapter->renegotiate = true;
2540 complete(&adapter->init_done);
2541 return 0;
2542 }
2543
Thomas Falcon032c5e82015-12-21 11:26:06 -06002544 netdev_dbg(adapter->netdev, "Login Response Buffer:\n");
2545 for (i = 0; i < (adapter->login_rsp_buf_sz - 1) / 8 + 1; i++) {
2546 netdev_dbg(adapter->netdev, "%016lx\n",
2547 ((unsigned long int *)(adapter->login_rsp_buf))[i]);
2548 }
2549
2550 /* Sanity checks */
2551 if (login->num_txcomp_subcrqs != login_rsp->num_txsubm_subcrqs ||
2552 (be32_to_cpu(login->num_rxcomp_subcrqs) *
2553 adapter->req_rx_add_queues !=
2554 be32_to_cpu(login_rsp->num_rxadd_subcrqs))) {
2555 dev_err(dev, "FATAL: Inconsistent login and login rsp\n");
2556 ibmvnic_remove(adapter->vdev);
2557 return -EIO;
2558 }
2559 complete(&adapter->init_done);
2560
Thomas Falcon032c5e82015-12-21 11:26:06 -06002561 return 0;
2562}
2563
2564static void handle_request_map_rsp(union ibmvnic_crq *crq,
2565 struct ibmvnic_adapter *adapter)
2566{
2567 struct device *dev = &adapter->vdev->dev;
2568 u8 map_id = crq->request_map_rsp.map_id;
2569 int tx_subcrqs;
2570 int rx_subcrqs;
2571 long rc;
2572 int i;
2573
2574 tx_subcrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
2575 rx_subcrqs = be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
2576
2577 rc = crq->request_map_rsp.rc.code;
2578 if (rc) {
2579 dev_err(dev, "Error %ld in REQUEST_MAP_RSP\n", rc);
2580 adapter->map_id--;
2581 /* need to find and zero tx/rx_pool map_id */
2582 for (i = 0; i < tx_subcrqs; i++) {
2583 if (adapter->tx_pool[i].long_term_buff.map_id == map_id)
2584 adapter->tx_pool[i].long_term_buff.map_id = 0;
2585 }
2586 for (i = 0; i < rx_subcrqs; i++) {
2587 if (adapter->rx_pool[i].long_term_buff.map_id == map_id)
2588 adapter->rx_pool[i].long_term_buff.map_id = 0;
2589 }
2590 }
2591 complete(&adapter->fw_done);
2592}
2593
2594static void handle_request_unmap_rsp(union ibmvnic_crq *crq,
2595 struct ibmvnic_adapter *adapter)
2596{
2597 struct device *dev = &adapter->vdev->dev;
2598 long rc;
2599
2600 rc = crq->request_unmap_rsp.rc.code;
2601 if (rc)
2602 dev_err(dev, "Error %ld in REQUEST_UNMAP_RSP\n", rc);
2603}
2604
2605static void handle_query_map_rsp(union ibmvnic_crq *crq,
2606 struct ibmvnic_adapter *adapter)
2607{
2608 struct net_device *netdev = adapter->netdev;
2609 struct device *dev = &adapter->vdev->dev;
2610 long rc;
2611
2612 rc = crq->query_map_rsp.rc.code;
2613 if (rc) {
2614 dev_err(dev, "Error %ld in QUERY_MAP_RSP\n", rc);
2615 return;
2616 }
2617 netdev_dbg(netdev, "page_size = %d\ntot_pages = %d\nfree_pages = %d\n",
2618 crq->query_map_rsp.page_size, crq->query_map_rsp.tot_pages,
2619 crq->query_map_rsp.free_pages);
2620}
2621
2622static void handle_query_cap_rsp(union ibmvnic_crq *crq,
2623 struct ibmvnic_adapter *adapter)
2624{
2625 struct net_device *netdev = adapter->netdev;
2626 struct device *dev = &adapter->vdev->dev;
2627 long rc;
2628
Thomas Falcon901e0402017-02-15 12:17:59 -06002629 atomic_dec(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002630 netdev_dbg(netdev, "Outstanding queries: %d\n",
Thomas Falcon901e0402017-02-15 12:17:59 -06002631 atomic_read(&adapter->running_cap_crqs));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002632 rc = crq->query_capability.rc.code;
2633 if (rc) {
2634 dev_err(dev, "Error %ld in QUERY_CAP_RSP\n", rc);
2635 goto out;
2636 }
2637
2638 switch (be16_to_cpu(crq->query_capability.capability)) {
2639 case MIN_TX_QUEUES:
2640 adapter->min_tx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06002641 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002642 netdev_dbg(netdev, "min_tx_queues = %lld\n",
2643 adapter->min_tx_queues);
2644 break;
2645 case MIN_RX_QUEUES:
2646 adapter->min_rx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06002647 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002648 netdev_dbg(netdev, "min_rx_queues = %lld\n",
2649 adapter->min_rx_queues);
2650 break;
2651 case MIN_RX_ADD_QUEUES:
2652 adapter->min_rx_add_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06002653 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002654 netdev_dbg(netdev, "min_rx_add_queues = %lld\n",
2655 adapter->min_rx_add_queues);
2656 break;
2657 case MAX_TX_QUEUES:
2658 adapter->max_tx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06002659 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002660 netdev_dbg(netdev, "max_tx_queues = %lld\n",
2661 adapter->max_tx_queues);
2662 break;
2663 case MAX_RX_QUEUES:
2664 adapter->max_rx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06002665 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002666 netdev_dbg(netdev, "max_rx_queues = %lld\n",
2667 adapter->max_rx_queues);
2668 break;
2669 case MAX_RX_ADD_QUEUES:
2670 adapter->max_rx_add_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06002671 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002672 netdev_dbg(netdev, "max_rx_add_queues = %lld\n",
2673 adapter->max_rx_add_queues);
2674 break;
2675 case MIN_TX_ENTRIES_PER_SUBCRQ:
2676 adapter->min_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06002677 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002678 netdev_dbg(netdev, "min_tx_entries_per_subcrq = %lld\n",
2679 adapter->min_tx_entries_per_subcrq);
2680 break;
2681 case MIN_RX_ADD_ENTRIES_PER_SUBCRQ:
2682 adapter->min_rx_add_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06002683 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002684 netdev_dbg(netdev, "min_rx_add_entrs_per_subcrq = %lld\n",
2685 adapter->min_rx_add_entries_per_subcrq);
2686 break;
2687 case MAX_TX_ENTRIES_PER_SUBCRQ:
2688 adapter->max_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06002689 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002690 netdev_dbg(netdev, "max_tx_entries_per_subcrq = %lld\n",
2691 adapter->max_tx_entries_per_subcrq);
2692 break;
2693 case MAX_RX_ADD_ENTRIES_PER_SUBCRQ:
2694 adapter->max_rx_add_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06002695 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002696 netdev_dbg(netdev, "max_rx_add_entrs_per_subcrq = %lld\n",
2697 adapter->max_rx_add_entries_per_subcrq);
2698 break;
2699 case TCP_IP_OFFLOAD:
2700 adapter->tcp_ip_offload =
Thomas Falconde89e852016-03-01 10:20:09 -06002701 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002702 netdev_dbg(netdev, "tcp_ip_offload = %lld\n",
2703 adapter->tcp_ip_offload);
2704 break;
2705 case PROMISC_SUPPORTED:
2706 adapter->promisc_supported =
Thomas Falconde89e852016-03-01 10:20:09 -06002707 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002708 netdev_dbg(netdev, "promisc_supported = %lld\n",
2709 adapter->promisc_supported);
2710 break;
2711 case MIN_MTU:
Thomas Falconde89e852016-03-01 10:20:09 -06002712 adapter->min_mtu = be64_to_cpu(crq->query_capability.number);
Thomas Falconf39f0d12017-02-14 10:22:59 -06002713 netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002714 netdev_dbg(netdev, "min_mtu = %lld\n", adapter->min_mtu);
2715 break;
2716 case MAX_MTU:
Thomas Falconde89e852016-03-01 10:20:09 -06002717 adapter->max_mtu = be64_to_cpu(crq->query_capability.number);
Thomas Falconf39f0d12017-02-14 10:22:59 -06002718 netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002719 netdev_dbg(netdev, "max_mtu = %lld\n", adapter->max_mtu);
2720 break;
2721 case MAX_MULTICAST_FILTERS:
2722 adapter->max_multicast_filters =
Thomas Falconde89e852016-03-01 10:20:09 -06002723 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002724 netdev_dbg(netdev, "max_multicast_filters = %lld\n",
2725 adapter->max_multicast_filters);
2726 break;
2727 case VLAN_HEADER_INSERTION:
2728 adapter->vlan_header_insertion =
Thomas Falconde89e852016-03-01 10:20:09 -06002729 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002730 if (adapter->vlan_header_insertion)
2731 netdev->features |= NETIF_F_HW_VLAN_STAG_TX;
2732 netdev_dbg(netdev, "vlan_header_insertion = %lld\n",
2733 adapter->vlan_header_insertion);
2734 break;
2735 case MAX_TX_SG_ENTRIES:
2736 adapter->max_tx_sg_entries =
Thomas Falconde89e852016-03-01 10:20:09 -06002737 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002738 netdev_dbg(netdev, "max_tx_sg_entries = %lld\n",
2739 adapter->max_tx_sg_entries);
2740 break;
2741 case RX_SG_SUPPORTED:
2742 adapter->rx_sg_supported =
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, "rx_sg_supported = %lld\n",
2745 adapter->rx_sg_supported);
2746 break;
2747 case OPT_TX_COMP_SUB_QUEUES:
2748 adapter->opt_tx_comp_sub_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06002749 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002750 netdev_dbg(netdev, "opt_tx_comp_sub_queues = %lld\n",
2751 adapter->opt_tx_comp_sub_queues);
2752 break;
2753 case OPT_RX_COMP_QUEUES:
2754 adapter->opt_rx_comp_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06002755 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002756 netdev_dbg(netdev, "opt_rx_comp_queues = %lld\n",
2757 adapter->opt_rx_comp_queues);
2758 break;
2759 case OPT_RX_BUFADD_Q_PER_RX_COMP_Q:
2760 adapter->opt_rx_bufadd_q_per_rx_comp_q =
Thomas Falconde89e852016-03-01 10:20:09 -06002761 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002762 netdev_dbg(netdev, "opt_rx_bufadd_q_per_rx_comp_q = %lld\n",
2763 adapter->opt_rx_bufadd_q_per_rx_comp_q);
2764 break;
2765 case OPT_TX_ENTRIES_PER_SUBCRQ:
2766 adapter->opt_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06002767 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002768 netdev_dbg(netdev, "opt_tx_entries_per_subcrq = %lld\n",
2769 adapter->opt_tx_entries_per_subcrq);
2770 break;
2771 case OPT_RXBA_ENTRIES_PER_SUBCRQ:
2772 adapter->opt_rxba_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06002773 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002774 netdev_dbg(netdev, "opt_rxba_entries_per_subcrq = %lld\n",
2775 adapter->opt_rxba_entries_per_subcrq);
2776 break;
2777 case TX_RX_DESC_REQ:
2778 adapter->tx_rx_desc_req = crq->query_capability.number;
2779 netdev_dbg(netdev, "tx_rx_desc_req = %llx\n",
2780 adapter->tx_rx_desc_req);
2781 break;
2782
2783 default:
2784 netdev_err(netdev, "Got invalid cap rsp %d\n",
2785 crq->query_capability.capability);
2786 }
2787
2788out:
Thomas Falcon249168a2017-02-15 12:18:00 -06002789 if (atomic_read(&adapter->running_cap_crqs) == 0) {
2790 adapter->wait_capability = false;
Thomas Falconea22d512016-07-06 15:35:17 -05002791 init_sub_crqs(adapter, 0);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002792 /* We're done querying the capabilities, initialize sub-crqs */
Thomas Falcon249168a2017-02-15 12:18:00 -06002793 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002794}
2795
Thomas Falcon032c5e82015-12-21 11:26:06 -06002796static void ibmvnic_free_inflight(struct ibmvnic_adapter *adapter)
2797{
Wei Yongjun96183182016-06-27 20:48:53 +08002798 struct ibmvnic_inflight_cmd *inflight_cmd, *tmp1;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002799 struct device *dev = &adapter->vdev->dev;
Wei Yongjun96183182016-06-27 20:48:53 +08002800 struct ibmvnic_error_buff *error_buff, *tmp2;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002801 unsigned long flags;
2802 unsigned long flags2;
2803
2804 spin_lock_irqsave(&adapter->inflight_lock, flags);
Wei Yongjun96183182016-06-27 20:48:53 +08002805 list_for_each_entry_safe(inflight_cmd, tmp1, &adapter->inflight, list) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06002806 switch (inflight_cmd->crq.generic.cmd) {
2807 case LOGIN:
2808 dma_unmap_single(dev, adapter->login_buf_token,
2809 adapter->login_buf_sz,
2810 DMA_BIDIRECTIONAL);
2811 dma_unmap_single(dev, adapter->login_rsp_buf_token,
2812 adapter->login_rsp_buf_sz,
2813 DMA_BIDIRECTIONAL);
2814 kfree(adapter->login_rsp_buf);
2815 kfree(adapter->login_buf);
2816 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002817 case REQUEST_ERROR_INFO:
2818 spin_lock_irqsave(&adapter->error_list_lock, flags2);
Wei Yongjun96183182016-06-27 20:48:53 +08002819 list_for_each_entry_safe(error_buff, tmp2,
2820 &adapter->errors, list) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06002821 dma_unmap_single(dev, error_buff->dma,
2822 error_buff->len,
2823 DMA_FROM_DEVICE);
2824 kfree(error_buff->buff);
2825 list_del(&error_buff->list);
2826 kfree(error_buff);
2827 }
2828 spin_unlock_irqrestore(&adapter->error_list_lock,
2829 flags2);
2830 break;
2831 }
2832 list_del(&inflight_cmd->list);
2833 kfree(inflight_cmd);
2834 }
2835 spin_unlock_irqrestore(&adapter->inflight_lock, flags);
2836}
2837
Thomas Falcon9888d7b2016-10-27 12:28:51 -05002838static void ibmvnic_xport_event(struct work_struct *work)
2839{
2840 struct ibmvnic_adapter *adapter = container_of(work,
2841 struct ibmvnic_adapter,
2842 ibmvnic_xport);
2843 struct device *dev = &adapter->vdev->dev;
2844 long rc;
2845
2846 ibmvnic_free_inflight(adapter);
2847 release_sub_crqs(adapter);
2848 if (adapter->migrated) {
2849 rc = ibmvnic_reenable_crq_queue(adapter);
2850 if (rc)
2851 dev_err(dev, "Error after enable rc=%ld\n", rc);
2852 adapter->migrated = false;
2853 rc = ibmvnic_send_crq_init(adapter);
2854 if (rc)
2855 dev_err(dev, "Error sending init rc=%ld\n", rc);
2856 }
2857}
2858
Thomas Falcon032c5e82015-12-21 11:26:06 -06002859static void ibmvnic_handle_crq(union ibmvnic_crq *crq,
2860 struct ibmvnic_adapter *adapter)
2861{
2862 struct ibmvnic_generic_crq *gen_crq = &crq->generic;
2863 struct net_device *netdev = adapter->netdev;
2864 struct device *dev = &adapter->vdev->dev;
2865 long rc;
2866
2867 netdev_dbg(netdev, "Handling CRQ: %016lx %016lx\n",
2868 ((unsigned long int *)crq)[0],
2869 ((unsigned long int *)crq)[1]);
2870 switch (gen_crq->first) {
2871 case IBMVNIC_CRQ_INIT_RSP:
2872 switch (gen_crq->cmd) {
2873 case IBMVNIC_CRQ_INIT:
2874 dev_info(dev, "Partner initialized\n");
2875 /* Send back a response */
2876 rc = ibmvnic_send_crq_init_complete(adapter);
Thomas Falcon65dc6892016-07-06 15:35:18 -05002877 if (!rc)
2878 schedule_work(&adapter->vnic_crq_init);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002879 else
2880 dev_err(dev, "Can't send initrsp rc=%ld\n", rc);
2881 break;
2882 case IBMVNIC_CRQ_INIT_COMPLETE:
2883 dev_info(dev, "Partner initialization complete\n");
2884 send_version_xchg(adapter);
2885 break;
2886 default:
2887 dev_err(dev, "Unknown crq cmd: %d\n", gen_crq->cmd);
2888 }
2889 return;
2890 case IBMVNIC_CRQ_XPORT_EVENT:
2891 if (gen_crq->cmd == IBMVNIC_PARTITION_MIGRATED) {
2892 dev_info(dev, "Re-enabling adapter\n");
2893 adapter->migrated = true;
Thomas Falcon9888d7b2016-10-27 12:28:51 -05002894 schedule_work(&adapter->ibmvnic_xport);
Thomas Falcondfad09a2016-08-18 11:37:51 -05002895 } else if (gen_crq->cmd == IBMVNIC_DEVICE_FAILOVER) {
2896 dev_info(dev, "Backing device failover detected\n");
2897 netif_carrier_off(netdev);
2898 adapter->failover = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002899 } else {
2900 /* The adapter lost the connection */
2901 dev_err(dev, "Virtual Adapter failed (rc=%d)\n",
2902 gen_crq->cmd);
Thomas Falcon9888d7b2016-10-27 12:28:51 -05002903 schedule_work(&adapter->ibmvnic_xport);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002904 }
2905 return;
2906 case IBMVNIC_CRQ_CMD_RSP:
2907 break;
2908 default:
2909 dev_err(dev, "Got an invalid msg type 0x%02x\n",
2910 gen_crq->first);
2911 return;
2912 }
2913
2914 switch (gen_crq->cmd) {
2915 case VERSION_EXCHANGE_RSP:
2916 rc = crq->version_exchange_rsp.rc.code;
2917 if (rc) {
2918 dev_err(dev, "Error %ld in VERSION_EXCHG_RSP\n", rc);
2919 break;
2920 }
2921 dev_info(dev, "Partner protocol version is %d\n",
2922 crq->version_exchange_rsp.version);
2923 if (be16_to_cpu(crq->version_exchange_rsp.version) <
2924 ibmvnic_version)
2925 ibmvnic_version =
2926 be16_to_cpu(crq->version_exchange_rsp.version);
2927 send_cap_queries(adapter);
2928 break;
2929 case QUERY_CAPABILITY_RSP:
2930 handle_query_cap_rsp(crq, adapter);
2931 break;
2932 case QUERY_MAP_RSP:
2933 handle_query_map_rsp(crq, adapter);
2934 break;
2935 case REQUEST_MAP_RSP:
2936 handle_request_map_rsp(crq, adapter);
2937 break;
2938 case REQUEST_UNMAP_RSP:
2939 handle_request_unmap_rsp(crq, adapter);
2940 break;
2941 case REQUEST_CAPABILITY_RSP:
2942 handle_request_cap_rsp(crq, adapter);
2943 break;
2944 case LOGIN_RSP:
2945 netdev_dbg(netdev, "Got Login Response\n");
2946 handle_login_rsp(crq, adapter);
2947 break;
2948 case LOGICAL_LINK_STATE_RSP:
2949 netdev_dbg(netdev, "Got Logical Link State Response\n");
2950 adapter->logical_link_state =
2951 crq->logical_link_state_rsp.link_state;
2952 break;
2953 case LINK_STATE_INDICATION:
2954 netdev_dbg(netdev, "Got Logical Link State Indication\n");
2955 adapter->phys_link_state =
2956 crq->link_state_indication.phys_link_state;
2957 adapter->logical_link_state =
2958 crq->link_state_indication.logical_link_state;
2959 break;
2960 case CHANGE_MAC_ADDR_RSP:
2961 netdev_dbg(netdev, "Got MAC address change Response\n");
2962 handle_change_mac_rsp(crq, adapter);
2963 break;
2964 case ERROR_INDICATION:
2965 netdev_dbg(netdev, "Got Error Indication\n");
2966 handle_error_indication(crq, adapter);
2967 break;
2968 case REQUEST_ERROR_RSP:
2969 netdev_dbg(netdev, "Got Error Detail Response\n");
2970 handle_error_info_rsp(crq, adapter);
2971 break;
2972 case REQUEST_STATISTICS_RSP:
2973 netdev_dbg(netdev, "Got Statistics Response\n");
2974 complete(&adapter->stats_done);
2975 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002976 case QUERY_IP_OFFLOAD_RSP:
2977 netdev_dbg(netdev, "Got Query IP offload Response\n");
2978 handle_query_ip_offload_rsp(adapter);
2979 break;
2980 case MULTICAST_CTRL_RSP:
2981 netdev_dbg(netdev, "Got multicast control Response\n");
2982 break;
2983 case CONTROL_IP_OFFLOAD_RSP:
2984 netdev_dbg(netdev, "Got Control IP offload Response\n");
2985 dma_unmap_single(dev, adapter->ip_offload_ctrl_tok,
2986 sizeof(adapter->ip_offload_ctrl),
2987 DMA_TO_DEVICE);
John Allenbd0b6722017-03-17 17:13:40 -05002988 complete(&adapter->init_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002989 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002990 case COLLECT_FW_TRACE_RSP:
2991 netdev_dbg(netdev, "Got Collect firmware trace Response\n");
2992 complete(&adapter->fw_done);
2993 break;
2994 default:
2995 netdev_err(netdev, "Got an invalid cmd type 0x%02x\n",
2996 gen_crq->cmd);
2997 }
2998}
2999
3000static irqreturn_t ibmvnic_interrupt(int irq, void *instance)
3001{
3002 struct ibmvnic_adapter *adapter = instance;
Thomas Falcon6c267b32017-02-15 12:17:58 -06003003 unsigned long flags;
3004
3005 spin_lock_irqsave(&adapter->crq.lock, flags);
3006 vio_disable_interrupts(adapter->vdev);
3007 tasklet_schedule(&adapter->tasklet);
3008 spin_unlock_irqrestore(&adapter->crq.lock, flags);
3009 return IRQ_HANDLED;
3010}
3011
3012static void ibmvnic_tasklet(void *data)
3013{
3014 struct ibmvnic_adapter *adapter = data;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003015 struct ibmvnic_crq_queue *queue = &adapter->crq;
3016 struct vio_dev *vdev = adapter->vdev;
3017 union ibmvnic_crq *crq;
3018 unsigned long flags;
3019 bool done = false;
3020
3021 spin_lock_irqsave(&queue->lock, flags);
3022 vio_disable_interrupts(vdev);
3023 while (!done) {
3024 /* Pull all the valid messages off the CRQ */
3025 while ((crq = ibmvnic_next_crq(adapter)) != NULL) {
3026 ibmvnic_handle_crq(crq, adapter);
3027 crq->generic.first = 0;
3028 }
3029 vio_enable_interrupts(vdev);
3030 crq = ibmvnic_next_crq(adapter);
3031 if (crq) {
3032 vio_disable_interrupts(vdev);
3033 ibmvnic_handle_crq(crq, adapter);
3034 crq->generic.first = 0;
3035 } else {
Thomas Falcon249168a2017-02-15 12:18:00 -06003036 /* remain in tasklet until all
3037 * capabilities responses are received
3038 */
3039 if (!adapter->wait_capability)
3040 done = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003041 }
3042 }
Thomas Falcon249168a2017-02-15 12:18:00 -06003043 /* if capabilities CRQ's were sent in this tasklet, the following
3044 * tasklet must wait until all responses are received
3045 */
3046 if (atomic_read(&adapter->running_cap_crqs) != 0)
3047 adapter->wait_capability = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003048 spin_unlock_irqrestore(&queue->lock, flags);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003049}
3050
3051static int ibmvnic_reenable_crq_queue(struct ibmvnic_adapter *adapter)
3052{
3053 struct vio_dev *vdev = adapter->vdev;
3054 int rc;
3055
3056 do {
3057 rc = plpar_hcall_norets(H_ENABLE_CRQ, vdev->unit_address);
3058 } while (rc == H_IN_PROGRESS || rc == H_BUSY || H_IS_LONG_BUSY(rc));
3059
3060 if (rc)
3061 dev_err(&vdev->dev, "Error enabling adapter (rc=%d)\n", rc);
3062
3063 return rc;
3064}
3065
3066static int ibmvnic_reset_crq(struct ibmvnic_adapter *adapter)
3067{
3068 struct ibmvnic_crq_queue *crq = &adapter->crq;
3069 struct device *dev = &adapter->vdev->dev;
3070 struct vio_dev *vdev = adapter->vdev;
3071 int rc;
3072
3073 /* Close the CRQ */
3074 do {
3075 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
3076 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
3077
3078 /* Clean out the queue */
3079 memset(crq->msgs, 0, PAGE_SIZE);
3080 crq->cur = 0;
3081
3082 /* And re-open it again */
3083 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
3084 crq->msg_token, PAGE_SIZE);
3085
3086 if (rc == H_CLOSED)
3087 /* Adapter is good, but other end is not ready */
3088 dev_warn(dev, "Partner adapter not ready\n");
3089 else if (rc != 0)
3090 dev_warn(dev, "Couldn't register crq (rc=%d)\n", rc);
3091
3092 return rc;
3093}
3094
Nathan Fontenotf9928872017-03-30 02:48:54 -04003095static void release_crq_queue(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003096{
3097 struct ibmvnic_crq_queue *crq = &adapter->crq;
3098 struct vio_dev *vdev = adapter->vdev;
3099 long rc;
3100
Nathan Fontenotf9928872017-03-30 02:48:54 -04003101 if (!crq->msgs)
3102 return;
3103
Thomas Falcon032c5e82015-12-21 11:26:06 -06003104 netdev_dbg(adapter->netdev, "Releasing CRQ\n");
3105 free_irq(vdev->irq, adapter);
Thomas Falcon6c267b32017-02-15 12:17:58 -06003106 tasklet_kill(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003107 do {
3108 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
3109 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
3110
3111 dma_unmap_single(&vdev->dev, crq->msg_token, PAGE_SIZE,
3112 DMA_BIDIRECTIONAL);
3113 free_page((unsigned long)crq->msgs);
Nathan Fontenotf9928872017-03-30 02:48:54 -04003114 crq->msgs = NULL;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003115}
3116
Nathan Fontenotf9928872017-03-30 02:48:54 -04003117static int init_crq_queue(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003118{
3119 struct ibmvnic_crq_queue *crq = &adapter->crq;
3120 struct device *dev = &adapter->vdev->dev;
3121 struct vio_dev *vdev = adapter->vdev;
3122 int rc, retrc = -ENOMEM;
3123
Nathan Fontenotf9928872017-03-30 02:48:54 -04003124 if (crq->msgs)
3125 return 0;
3126
Thomas Falcon032c5e82015-12-21 11:26:06 -06003127 crq->msgs = (union ibmvnic_crq *)get_zeroed_page(GFP_KERNEL);
3128 /* Should we allocate more than one page? */
3129
3130 if (!crq->msgs)
3131 return -ENOMEM;
3132
3133 crq->size = PAGE_SIZE / sizeof(*crq->msgs);
3134 crq->msg_token = dma_map_single(dev, crq->msgs, PAGE_SIZE,
3135 DMA_BIDIRECTIONAL);
3136 if (dma_mapping_error(dev, crq->msg_token))
3137 goto map_failed;
3138
3139 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
3140 crq->msg_token, PAGE_SIZE);
3141
3142 if (rc == H_RESOURCE)
3143 /* maybe kexecing and resource is busy. try a reset */
3144 rc = ibmvnic_reset_crq(adapter);
3145 retrc = rc;
3146
3147 if (rc == H_CLOSED) {
3148 dev_warn(dev, "Partner adapter not ready\n");
3149 } else if (rc) {
3150 dev_warn(dev, "Error %d opening adapter\n", rc);
3151 goto reg_crq_failed;
3152 }
3153
3154 retrc = 0;
3155
Thomas Falcon6c267b32017-02-15 12:17:58 -06003156 tasklet_init(&adapter->tasklet, (void *)ibmvnic_tasklet,
3157 (unsigned long)adapter);
3158
Thomas Falcon032c5e82015-12-21 11:26:06 -06003159 netdev_dbg(adapter->netdev, "registering irq 0x%x\n", vdev->irq);
3160 rc = request_irq(vdev->irq, ibmvnic_interrupt, 0, IBMVNIC_NAME,
3161 adapter);
3162 if (rc) {
3163 dev_err(dev, "Couldn't register irq 0x%x. rc=%d\n",
3164 vdev->irq, rc);
3165 goto req_irq_failed;
3166 }
3167
3168 rc = vio_enable_interrupts(vdev);
3169 if (rc) {
3170 dev_err(dev, "Error %d enabling interrupts\n", rc);
3171 goto req_irq_failed;
3172 }
3173
3174 crq->cur = 0;
3175 spin_lock_init(&crq->lock);
3176
3177 return retrc;
3178
3179req_irq_failed:
Thomas Falcon6c267b32017-02-15 12:17:58 -06003180 tasklet_kill(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003181 do {
3182 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
3183 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
3184reg_crq_failed:
3185 dma_unmap_single(dev, crq->msg_token, PAGE_SIZE, DMA_BIDIRECTIONAL);
3186map_failed:
3187 free_page((unsigned long)crq->msgs);
Nathan Fontenotf9928872017-03-30 02:48:54 -04003188 crq->msgs = NULL;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003189 return retrc;
3190}
3191
Thomas Falcon65dc6892016-07-06 15:35:18 -05003192static void handle_crq_init_rsp(struct work_struct *work)
3193{
3194 struct ibmvnic_adapter *adapter = container_of(work,
3195 struct ibmvnic_adapter,
3196 vnic_crq_init);
3197 struct device *dev = &adapter->vdev->dev;
3198 struct net_device *netdev = adapter->netdev;
3199 unsigned long timeout = msecs_to_jiffies(30000);
Thomas Falcondfad09a2016-08-18 11:37:51 -05003200 bool restart = false;
Thomas Falcon65dc6892016-07-06 15:35:18 -05003201 int rc;
3202
Thomas Falcondfad09a2016-08-18 11:37:51 -05003203 if (adapter->failover) {
3204 release_sub_crqs(adapter);
3205 if (netif_running(netdev)) {
3206 netif_tx_disable(netdev);
3207 ibmvnic_close(netdev);
3208 restart = true;
3209 }
3210 }
3211
Thomas Falcon65dc6892016-07-06 15:35:18 -05003212 reinit_completion(&adapter->init_done);
Nathan Fontenotdb5d0b52017-02-10 13:45:05 -05003213 send_version_xchg(adapter);
Thomas Falcon65dc6892016-07-06 15:35:18 -05003214 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
3215 dev_err(dev, "Passive init timeout\n");
3216 goto task_failed;
3217 }
3218
Thomas Falconf39f0d12017-02-14 10:22:59 -06003219 netdev->mtu = adapter->req_mtu - ETH_HLEN;
Thomas Falcon65dc6892016-07-06 15:35:18 -05003220
Thomas Falcondfad09a2016-08-18 11:37:51 -05003221 if (adapter->failover) {
3222 adapter->failover = false;
3223 if (restart) {
3224 rc = ibmvnic_open(netdev);
3225 if (rc)
3226 goto restart_failed;
3227 }
3228 netif_carrier_on(netdev);
3229 return;
3230 }
3231
Thomas Falcon65dc6892016-07-06 15:35:18 -05003232 rc = register_netdev(netdev);
3233 if (rc) {
3234 dev_err(dev,
3235 "failed to register netdev rc=%d\n", rc);
3236 goto register_failed;
3237 }
3238 dev_info(dev, "ibmvnic registered\n");
3239
3240 return;
3241
Thomas Falcondfad09a2016-08-18 11:37:51 -05003242restart_failed:
3243 dev_err(dev, "Failed to restart ibmvnic, rc=%d\n", rc);
Thomas Falcon65dc6892016-07-06 15:35:18 -05003244register_failed:
3245 release_sub_crqs(adapter);
3246task_failed:
3247 dev_err(dev, "Passive initialization was not successful\n");
3248}
3249
John Allenf6ef6402017-03-17 17:13:42 -05003250static int ibmvnic_init(struct ibmvnic_adapter *adapter)
3251{
3252 struct device *dev = &adapter->vdev->dev;
3253 unsigned long timeout = msecs_to_jiffies(30000);
John Allenf6ef6402017-03-17 17:13:42 -05003254 int rc;
3255
Nathan Fontenotf9928872017-03-30 02:48:54 -04003256 rc = init_crq_queue(adapter);
John Allenf6ef6402017-03-17 17:13:42 -05003257 if (rc) {
3258 dev_err(dev, "Couldn't initialize crq. rc=%d\n", rc);
3259 return rc;
3260 }
3261
3262 adapter->stats_token = dma_map_single(dev, &adapter->stats,
3263 sizeof(struct ibmvnic_statistics),
3264 DMA_FROM_DEVICE);
3265 if (dma_mapping_error(dev, adapter->stats_token)) {
Nathan Fontenotf9928872017-03-30 02:48:54 -04003266 release_crq_queue(adapter);
John Allenf6ef6402017-03-17 17:13:42 -05003267 dev_err(dev, "Couldn't map stats buffer\n");
3268 return -ENOMEM;
3269 }
3270
John Allenf6ef6402017-03-17 17:13:42 -05003271 init_completion(&adapter->init_done);
3272 ibmvnic_send_crq_init(adapter);
3273 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
3274 dev_err(dev, "Initialization sequence timed out\n");
Nathan Fontenotf9928872017-03-30 02:48:54 -04003275 release_crq_queue(adapter);
John Allenf6ef6402017-03-17 17:13:42 -05003276 return -1;
3277 }
3278
3279 return 0;
3280}
3281
Thomas Falcon032c5e82015-12-21 11:26:06 -06003282static int ibmvnic_probe(struct vio_dev *dev, const struct vio_device_id *id)
3283{
3284 struct ibmvnic_adapter *adapter;
3285 struct net_device *netdev;
3286 unsigned char *mac_addr_p;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003287 int rc;
3288
3289 dev_dbg(&dev->dev, "entering ibmvnic_probe for UA 0x%x\n",
3290 dev->unit_address);
3291
3292 mac_addr_p = (unsigned char *)vio_get_attribute(dev,
3293 VETH_MAC_ADDR, NULL);
3294 if (!mac_addr_p) {
3295 dev_err(&dev->dev,
3296 "(%s:%3.3d) ERROR: Can't find MAC_ADDR attribute\n",
3297 __FILE__, __LINE__);
3298 return 0;
3299 }
3300
3301 netdev = alloc_etherdev_mq(sizeof(struct ibmvnic_adapter),
3302 IBMVNIC_MAX_TX_QUEUES);
3303 if (!netdev)
3304 return -ENOMEM;
3305
3306 adapter = netdev_priv(netdev);
3307 dev_set_drvdata(&dev->dev, netdev);
3308 adapter->vdev = dev;
3309 adapter->netdev = netdev;
Thomas Falcondfad09a2016-08-18 11:37:51 -05003310 adapter->failover = false;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003311
3312 ether_addr_copy(adapter->mac_addr, mac_addr_p);
3313 ether_addr_copy(netdev->dev_addr, adapter->mac_addr);
3314 netdev->irq = dev->irq;
3315 netdev->netdev_ops = &ibmvnic_netdev_ops;
3316 netdev->ethtool_ops = &ibmvnic_ethtool_ops;
3317 SET_NETDEV_DEV(netdev, &dev->dev);
3318
Thomas Falcon65dc6892016-07-06 15:35:18 -05003319 INIT_WORK(&adapter->vnic_crq_init, handle_crq_init_rsp);
Thomas Falcon9888d7b2016-10-27 12:28:51 -05003320 INIT_WORK(&adapter->ibmvnic_xport, ibmvnic_xport_event);
Thomas Falcon65dc6892016-07-06 15:35:18 -05003321
Thomas Falcon032c5e82015-12-21 11:26:06 -06003322 spin_lock_init(&adapter->stats_lock);
3323
Thomas Falcon032c5e82015-12-21 11:26:06 -06003324 INIT_LIST_HEAD(&adapter->errors);
3325 INIT_LIST_HEAD(&adapter->inflight);
3326 spin_lock_init(&adapter->error_list_lock);
3327 spin_lock_init(&adapter->inflight_lock);
3328
John Allenf6ef6402017-03-17 17:13:42 -05003329 rc = ibmvnic_init(adapter);
3330 if (rc) {
3331 free_netdev(netdev);
3332 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003333 }
3334
Thomas Falconf39f0d12017-02-14 10:22:59 -06003335 netdev->mtu = adapter->req_mtu - ETH_HLEN;
John Allenea5509f2017-03-17 17:13:43 -05003336 adapter->is_closed = false;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003337
3338 rc = register_netdev(netdev);
3339 if (rc) {
3340 dev_err(&dev->dev, "failed to register netdev rc=%d\n", rc);
John Allenf6ef6402017-03-17 17:13:42 -05003341 free_netdev(netdev);
3342 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003343 }
3344 dev_info(&dev->dev, "ibmvnic registered\n");
3345
3346 return 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003347}
3348
3349static int ibmvnic_remove(struct vio_dev *dev)
3350{
3351 struct net_device *netdev = dev_get_drvdata(&dev->dev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003352
3353 unregister_netdev(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003354 free_netdev(netdev);
3355 dev_set_drvdata(&dev->dev, NULL);
3356
3357 return 0;
3358}
3359
3360static unsigned long ibmvnic_get_desired_dma(struct vio_dev *vdev)
3361{
3362 struct net_device *netdev = dev_get_drvdata(&vdev->dev);
3363 struct ibmvnic_adapter *adapter;
3364 struct iommu_table *tbl;
3365 unsigned long ret = 0;
3366 int i;
3367
3368 tbl = get_iommu_table_base(&vdev->dev);
3369
3370 /* netdev inits at probe time along with the structures we need below*/
3371 if (!netdev)
3372 return IOMMU_PAGE_ALIGN(IBMVNIC_IO_ENTITLEMENT_DEFAULT, tbl);
3373
3374 adapter = netdev_priv(netdev);
3375
3376 ret += PAGE_SIZE; /* the crq message queue */
3377 ret += adapter->bounce_buffer_size;
3378 ret += IOMMU_PAGE_ALIGN(sizeof(struct ibmvnic_statistics), tbl);
3379
3380 for (i = 0; i < adapter->req_tx_queues + adapter->req_rx_queues; i++)
3381 ret += 4 * PAGE_SIZE; /* the scrq message queue */
3382
3383 for (i = 0; i < be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
3384 i++)
3385 ret += adapter->rx_pool[i].size *
3386 IOMMU_PAGE_ALIGN(adapter->rx_pool[i].buff_size, tbl);
3387
3388 return ret;
3389}
3390
3391static int ibmvnic_resume(struct device *dev)
3392{
3393 struct net_device *netdev = dev_get_drvdata(dev);
3394 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
3395 int i;
3396
3397 /* kick the interrupt handlers just in case we lost an interrupt */
3398 for (i = 0; i < adapter->req_rx_queues; i++)
3399 ibmvnic_interrupt_rx(adapter->rx_scrq[i]->irq,
3400 adapter->rx_scrq[i]);
3401
3402 return 0;
3403}
3404
3405static struct vio_device_id ibmvnic_device_table[] = {
3406 {"network", "IBM,vnic"},
3407 {"", "" }
3408};
3409MODULE_DEVICE_TABLE(vio, ibmvnic_device_table);
3410
3411static const struct dev_pm_ops ibmvnic_pm_ops = {
3412 .resume = ibmvnic_resume
3413};
3414
3415static struct vio_driver ibmvnic_driver = {
3416 .id_table = ibmvnic_device_table,
3417 .probe = ibmvnic_probe,
3418 .remove = ibmvnic_remove,
3419 .get_desired_dma = ibmvnic_get_desired_dma,
3420 .name = ibmvnic_driver_name,
3421 .pm = &ibmvnic_pm_ops,
3422};
3423
3424/* module functions */
3425static int __init ibmvnic_module_init(void)
3426{
3427 pr_info("%s: %s %s\n", ibmvnic_driver_name, ibmvnic_driver_string,
3428 IBMVNIC_DRIVER_VERSION);
3429
3430 return vio_register_driver(&ibmvnic_driver);
3431}
3432
3433static void __exit ibmvnic_module_exit(void)
3434{
3435 vio_unregister_driver(&ibmvnic_driver);
3436}
3437
3438module_init(ibmvnic_module_init);
3439module_exit(ibmvnic_module_exit);