blob: 52b0c07d3ca46395306a1250e9cb0d0c81d07333 [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>
68#include <linux/debugfs.h>
69#include <linux/interrupt.h>
70#include <net/net_namespace.h>
71#include <asm/hvcall.h>
72#include <linux/atomic.h>
73#include <asm/vio.h>
74#include <asm/iommu.h>
75#include <linux/uaccess.h>
76#include <asm/firmware.h>
77#include <linux/seq_file.h>
78
79#include "ibmvnic.h"
80
81static const char ibmvnic_driver_name[] = "ibmvnic";
82static const char ibmvnic_driver_string[] = "IBM System i/p Virtual NIC Driver";
83
84MODULE_AUTHOR("Santiago Leon <santi_leon@yahoo.com>");
85MODULE_DESCRIPTION("IBM System i/p Virtual NIC Driver");
86MODULE_LICENSE("GPL");
87MODULE_VERSION(IBMVNIC_DRIVER_VERSION);
88
89static int ibmvnic_version = IBMVNIC_INITIAL_VERSION;
90static int ibmvnic_remove(struct vio_dev *);
91static void release_sub_crqs(struct ibmvnic_adapter *);
Thomas Falconea22d512016-07-06 15:35:17 -050092static void release_sub_crqs_no_irqs(struct ibmvnic_adapter *);
Thomas Falcon032c5e82015-12-21 11:26:06 -060093static int ibmvnic_reset_crq(struct ibmvnic_adapter *);
94static int ibmvnic_send_crq_init(struct ibmvnic_adapter *);
95static int ibmvnic_reenable_crq_queue(struct ibmvnic_adapter *);
96static int ibmvnic_send_crq(struct ibmvnic_adapter *, union ibmvnic_crq *);
97static int send_subcrq(struct ibmvnic_adapter *adapter, u64 remote_handle,
98 union sub_crq *sub_crq);
Thomas Falconad7775d2016-04-01 17:20:34 -050099static int send_subcrq_indirect(struct ibmvnic_adapter *, u64, u64, u64);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600100static irqreturn_t ibmvnic_interrupt_rx(int irq, void *instance);
101static int enable_scrq_irq(struct ibmvnic_adapter *,
102 struct ibmvnic_sub_crq_queue *);
103static int disable_scrq_irq(struct ibmvnic_adapter *,
104 struct ibmvnic_sub_crq_queue *);
105static int pending_scrq(struct ibmvnic_adapter *,
106 struct ibmvnic_sub_crq_queue *);
107static union sub_crq *ibmvnic_next_scrq(struct ibmvnic_adapter *,
108 struct ibmvnic_sub_crq_queue *);
109static int ibmvnic_poll(struct napi_struct *napi, int data);
110static void send_map_query(struct ibmvnic_adapter *adapter);
111static void send_request_map(struct ibmvnic_adapter *, dma_addr_t, __be32, u8);
112static void send_request_unmap(struct ibmvnic_adapter *, u8);
113
114struct ibmvnic_stat {
115 char name[ETH_GSTRING_LEN];
116 int offset;
117};
118
119#define IBMVNIC_STAT_OFF(stat) (offsetof(struct ibmvnic_adapter, stats) + \
120 offsetof(struct ibmvnic_statistics, stat))
121#define IBMVNIC_GET_STAT(a, off) (*((u64 *)(((unsigned long)(a)) + off)))
122
123static const struct ibmvnic_stat ibmvnic_stats[] = {
124 {"rx_packets", IBMVNIC_STAT_OFF(rx_packets)},
125 {"rx_bytes", IBMVNIC_STAT_OFF(rx_bytes)},
126 {"tx_packets", IBMVNIC_STAT_OFF(tx_packets)},
127 {"tx_bytes", IBMVNIC_STAT_OFF(tx_bytes)},
128 {"ucast_tx_packets", IBMVNIC_STAT_OFF(ucast_tx_packets)},
129 {"ucast_rx_packets", IBMVNIC_STAT_OFF(ucast_rx_packets)},
130 {"mcast_tx_packets", IBMVNIC_STAT_OFF(mcast_tx_packets)},
131 {"mcast_rx_packets", IBMVNIC_STAT_OFF(mcast_rx_packets)},
132 {"bcast_tx_packets", IBMVNIC_STAT_OFF(bcast_tx_packets)},
133 {"bcast_rx_packets", IBMVNIC_STAT_OFF(bcast_rx_packets)},
134 {"align_errors", IBMVNIC_STAT_OFF(align_errors)},
135 {"fcs_errors", IBMVNIC_STAT_OFF(fcs_errors)},
136 {"single_collision_frames", IBMVNIC_STAT_OFF(single_collision_frames)},
137 {"multi_collision_frames", IBMVNIC_STAT_OFF(multi_collision_frames)},
138 {"sqe_test_errors", IBMVNIC_STAT_OFF(sqe_test_errors)},
139 {"deferred_tx", IBMVNIC_STAT_OFF(deferred_tx)},
140 {"late_collisions", IBMVNIC_STAT_OFF(late_collisions)},
141 {"excess_collisions", IBMVNIC_STAT_OFF(excess_collisions)},
142 {"internal_mac_tx_errors", IBMVNIC_STAT_OFF(internal_mac_tx_errors)},
143 {"carrier_sense", IBMVNIC_STAT_OFF(carrier_sense)},
144 {"too_long_frames", IBMVNIC_STAT_OFF(too_long_frames)},
145 {"internal_mac_rx_errors", IBMVNIC_STAT_OFF(internal_mac_rx_errors)},
146};
147
148static long h_reg_sub_crq(unsigned long unit_address, unsigned long token,
149 unsigned long length, unsigned long *number,
150 unsigned long *irq)
151{
152 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
153 long rc;
154
155 rc = plpar_hcall(H_REG_SUB_CRQ, retbuf, unit_address, token, length);
156 *number = retbuf[0];
157 *irq = retbuf[1];
158
159 return rc;
160}
161
162/* net_device_ops functions */
163
164static void init_rx_pool(struct ibmvnic_adapter *adapter,
165 struct ibmvnic_rx_pool *rx_pool, int num, int index,
166 int buff_size, int active)
167{
168 netdev_dbg(adapter->netdev,
169 "Initializing rx_pool %d, %d buffs, %d bytes each\n",
170 index, num, buff_size);
171 rx_pool->size = num;
172 rx_pool->index = index;
173 rx_pool->buff_size = buff_size;
174 rx_pool->active = active;
175}
176
177static int alloc_long_term_buff(struct ibmvnic_adapter *adapter,
178 struct ibmvnic_long_term_buff *ltb, int size)
179{
180 struct device *dev = &adapter->vdev->dev;
181
182 ltb->size = size;
183 ltb->buff = dma_alloc_coherent(dev, ltb->size, &ltb->addr,
184 GFP_KERNEL);
185
186 if (!ltb->buff) {
187 dev_err(dev, "Couldn't alloc long term buffer\n");
188 return -ENOMEM;
189 }
190 ltb->map_id = adapter->map_id;
191 adapter->map_id++;
192 send_request_map(adapter, ltb->addr,
193 ltb->size, ltb->map_id);
194 init_completion(&adapter->fw_done);
195 wait_for_completion(&adapter->fw_done);
196 return 0;
197}
198
199static void free_long_term_buff(struct ibmvnic_adapter *adapter,
200 struct ibmvnic_long_term_buff *ltb)
201{
202 struct device *dev = &adapter->vdev->dev;
203
204 dma_free_coherent(dev, ltb->size, ltb->buff, ltb->addr);
205 send_request_unmap(adapter, ltb->map_id);
206}
207
208static int alloc_rx_pool(struct ibmvnic_adapter *adapter,
209 struct ibmvnic_rx_pool *pool)
210{
211 struct device *dev = &adapter->vdev->dev;
212 int i;
213
214 pool->free_map = kcalloc(pool->size, sizeof(int), GFP_KERNEL);
215 if (!pool->free_map)
216 return -ENOMEM;
217
218 pool->rx_buff = kcalloc(pool->size, sizeof(struct ibmvnic_rx_buff),
219 GFP_KERNEL);
220
221 if (!pool->rx_buff) {
222 dev_err(dev, "Couldn't alloc rx buffers\n");
223 kfree(pool->free_map);
224 return -ENOMEM;
225 }
226
227 if (alloc_long_term_buff(adapter, &pool->long_term_buff,
228 pool->size * pool->buff_size)) {
229 kfree(pool->free_map);
230 kfree(pool->rx_buff);
231 return -ENOMEM;
232 }
233
234 for (i = 0; i < pool->size; ++i)
235 pool->free_map[i] = i;
236
237 atomic_set(&pool->available, 0);
238 pool->next_alloc = 0;
239 pool->next_free = 0;
240
241 return 0;
242}
243
244static void replenish_rx_pool(struct ibmvnic_adapter *adapter,
245 struct ibmvnic_rx_pool *pool)
246{
247 int count = pool->size - atomic_read(&pool->available);
248 struct device *dev = &adapter->vdev->dev;
249 int buffers_added = 0;
250 unsigned long lpar_rc;
251 union sub_crq sub_crq;
252 struct sk_buff *skb;
253 unsigned int offset;
254 dma_addr_t dma_addr;
255 unsigned char *dst;
256 u64 *handle_array;
257 int shift = 0;
258 int index;
259 int i;
260
261 handle_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
262 be32_to_cpu(adapter->login_rsp_buf->
263 off_rxadd_subcrqs));
264
265 for (i = 0; i < count; ++i) {
266 skb = alloc_skb(pool->buff_size, GFP_ATOMIC);
267 if (!skb) {
268 dev_err(dev, "Couldn't replenish rx buff\n");
269 adapter->replenish_no_mem++;
270 break;
271 }
272
273 index = pool->free_map[pool->next_free];
274
275 if (pool->rx_buff[index].skb)
276 dev_err(dev, "Inconsistent free_map!\n");
277
278 /* Copy the skb to the long term mapped DMA buffer */
279 offset = index * pool->buff_size;
280 dst = pool->long_term_buff.buff + offset;
281 memset(dst, 0, pool->buff_size);
282 dma_addr = pool->long_term_buff.addr + offset;
283 pool->rx_buff[index].data = dst;
284
285 pool->free_map[pool->next_free] = IBMVNIC_INVALID_MAP;
286 pool->rx_buff[index].dma = dma_addr;
287 pool->rx_buff[index].skb = skb;
288 pool->rx_buff[index].pool_index = pool->index;
289 pool->rx_buff[index].size = pool->buff_size;
290
291 memset(&sub_crq, 0, sizeof(sub_crq));
292 sub_crq.rx_add.first = IBMVNIC_CRQ_CMD;
293 sub_crq.rx_add.correlator =
294 cpu_to_be64((u64)&pool->rx_buff[index]);
295 sub_crq.rx_add.ioba = cpu_to_be32(dma_addr);
296 sub_crq.rx_add.map_id = pool->long_term_buff.map_id;
297
298 /* The length field of the sCRQ is defined to be 24 bits so the
299 * buffer size needs to be left shifted by a byte before it is
300 * converted to big endian to prevent the last byte from being
301 * truncated.
302 */
303#ifdef __LITTLE_ENDIAN__
304 shift = 8;
305#endif
306 sub_crq.rx_add.len = cpu_to_be32(pool->buff_size << shift);
307
308 lpar_rc = send_subcrq(adapter, handle_array[pool->index],
309 &sub_crq);
310 if (lpar_rc != H_SUCCESS)
311 goto failure;
312
313 buffers_added++;
314 adapter->replenish_add_buff_success++;
315 pool->next_free = (pool->next_free + 1) % pool->size;
316 }
317 atomic_add(buffers_added, &pool->available);
318 return;
319
320failure:
321 dev_info(dev, "replenish pools failure\n");
322 pool->free_map[pool->next_free] = index;
323 pool->rx_buff[index].skb = NULL;
324 if (!dma_mapping_error(dev, dma_addr))
325 dma_unmap_single(dev, dma_addr, pool->buff_size,
326 DMA_FROM_DEVICE);
327
328 dev_kfree_skb_any(skb);
329 adapter->replenish_add_buff_failure++;
330 atomic_add(buffers_added, &pool->available);
331}
332
333static void replenish_pools(struct ibmvnic_adapter *adapter)
334{
335 int i;
336
337 if (adapter->migrated)
338 return;
339
340 adapter->replenish_task_cycles++;
341 for (i = 0; i < be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
342 i++) {
343 if (adapter->rx_pool[i].active)
344 replenish_rx_pool(adapter, &adapter->rx_pool[i]);
345 }
346}
347
348static void free_rx_pool(struct ibmvnic_adapter *adapter,
349 struct ibmvnic_rx_pool *pool)
350{
351 int i;
352
353 kfree(pool->free_map);
354 pool->free_map = NULL;
355
356 if (!pool->rx_buff)
357 return;
358
359 for (i = 0; i < pool->size; i++) {
360 if (pool->rx_buff[i].skb) {
361 dev_kfree_skb_any(pool->rx_buff[i].skb);
362 pool->rx_buff[i].skb = NULL;
363 }
364 }
365 kfree(pool->rx_buff);
366 pool->rx_buff = NULL;
367}
368
369static int ibmvnic_open(struct net_device *netdev)
370{
371 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
372 struct device *dev = &adapter->vdev->dev;
373 struct ibmvnic_tx_pool *tx_pool;
374 union ibmvnic_crq crq;
375 int rxadd_subcrqs;
376 u64 *size_array;
377 int tx_subcrqs;
378 int i, j;
379
380 rxadd_subcrqs =
381 be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
382 tx_subcrqs =
383 be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
384 size_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
385 be32_to_cpu(adapter->login_rsp_buf->
386 off_rxadd_buff_size));
387 adapter->map_id = 1;
388 adapter->napi = kcalloc(adapter->req_rx_queues,
389 sizeof(struct napi_struct), GFP_KERNEL);
390 if (!adapter->napi)
391 goto alloc_napi_failed;
392 for (i = 0; i < adapter->req_rx_queues; i++) {
393 netif_napi_add(netdev, &adapter->napi[i], ibmvnic_poll,
394 NAPI_POLL_WEIGHT);
395 napi_enable(&adapter->napi[i]);
396 }
397 adapter->rx_pool =
398 kcalloc(rxadd_subcrqs, sizeof(struct ibmvnic_rx_pool), GFP_KERNEL);
399
400 if (!adapter->rx_pool)
401 goto rx_pool_arr_alloc_failed;
402 send_map_query(adapter);
403 for (i = 0; i < rxadd_subcrqs; i++) {
404 init_rx_pool(adapter, &adapter->rx_pool[i],
405 IBMVNIC_BUFFS_PER_POOL, i,
406 be64_to_cpu(size_array[i]), 1);
407 if (alloc_rx_pool(adapter, &adapter->rx_pool[i])) {
408 dev_err(dev, "Couldn't alloc rx pool\n");
409 goto rx_pool_alloc_failed;
410 }
411 }
412 adapter->tx_pool =
413 kcalloc(tx_subcrqs, sizeof(struct ibmvnic_tx_pool), GFP_KERNEL);
414
415 if (!adapter->tx_pool)
416 goto tx_pool_arr_alloc_failed;
417 for (i = 0; i < tx_subcrqs; i++) {
418 tx_pool = &adapter->tx_pool[i];
419 tx_pool->tx_buff =
420 kcalloc(adapter->max_tx_entries_per_subcrq,
421 sizeof(struct ibmvnic_tx_buff), GFP_KERNEL);
422 if (!tx_pool->tx_buff)
423 goto tx_pool_alloc_failed;
424
425 if (alloc_long_term_buff(adapter, &tx_pool->long_term_buff,
426 adapter->max_tx_entries_per_subcrq *
427 adapter->req_mtu))
428 goto tx_ltb_alloc_failed;
429
430 tx_pool->free_map =
431 kcalloc(adapter->max_tx_entries_per_subcrq,
432 sizeof(int), GFP_KERNEL);
433 if (!tx_pool->free_map)
434 goto tx_fm_alloc_failed;
435
436 for (j = 0; j < adapter->max_tx_entries_per_subcrq; j++)
437 tx_pool->free_map[j] = j;
438
439 tx_pool->consumer_index = 0;
440 tx_pool->producer_index = 0;
441 }
442 adapter->bounce_buffer_size =
443 (netdev->mtu + ETH_HLEN - 1) / PAGE_SIZE + 1;
444 adapter->bounce_buffer = kmalloc(adapter->bounce_buffer_size,
445 GFP_KERNEL);
446 if (!adapter->bounce_buffer)
447 goto bounce_alloc_failed;
448
449 adapter->bounce_buffer_dma = dma_map_single(dev, adapter->bounce_buffer,
450 adapter->bounce_buffer_size,
451 DMA_TO_DEVICE);
452 if (dma_mapping_error(dev, adapter->bounce_buffer_dma)) {
453 dev_err(dev, "Couldn't map tx bounce buffer\n");
454 goto bounce_map_failed;
455 }
456 replenish_pools(adapter);
457
458 /* We're ready to receive frames, enable the sub-crq interrupts and
459 * set the logical link state to up
460 */
461 for (i = 0; i < adapter->req_rx_queues; i++)
462 enable_scrq_irq(adapter, adapter->rx_scrq[i]);
463
464 for (i = 0; i < adapter->req_tx_queues; i++)
465 enable_scrq_irq(adapter, adapter->tx_scrq[i]);
466
467 memset(&crq, 0, sizeof(crq));
468 crq.logical_link_state.first = IBMVNIC_CRQ_CMD;
469 crq.logical_link_state.cmd = LOGICAL_LINK_STATE;
470 crq.logical_link_state.link_state = IBMVNIC_LOGICAL_LNK_UP;
471 ibmvnic_send_crq(adapter, &crq);
472
Thomas Falconb8efb892016-07-06 15:35:15 -0500473 netif_tx_start_all_queues(netdev);
474
Thomas Falcon032c5e82015-12-21 11:26:06 -0600475 return 0;
476
477bounce_map_failed:
478 kfree(adapter->bounce_buffer);
479bounce_alloc_failed:
480 i = tx_subcrqs - 1;
481 kfree(adapter->tx_pool[i].free_map);
482tx_fm_alloc_failed:
483 free_long_term_buff(adapter, &adapter->tx_pool[i].long_term_buff);
484tx_ltb_alloc_failed:
485 kfree(adapter->tx_pool[i].tx_buff);
486tx_pool_alloc_failed:
487 for (j = 0; j < i; j++) {
488 kfree(adapter->tx_pool[j].tx_buff);
489 free_long_term_buff(adapter,
490 &adapter->tx_pool[j].long_term_buff);
491 kfree(adapter->tx_pool[j].free_map);
492 }
493 kfree(adapter->tx_pool);
494 adapter->tx_pool = NULL;
495tx_pool_arr_alloc_failed:
496 i = rxadd_subcrqs;
497rx_pool_alloc_failed:
498 for (j = 0; j < i; j++) {
499 free_rx_pool(adapter, &adapter->rx_pool[j]);
500 free_long_term_buff(adapter,
501 &adapter->rx_pool[j].long_term_buff);
502 }
503 kfree(adapter->rx_pool);
504 adapter->rx_pool = NULL;
505rx_pool_arr_alloc_failed:
506 for (i = 0; i < adapter->req_rx_queues; i++)
507 napi_enable(&adapter->napi[i]);
508alloc_napi_failed:
509 return -ENOMEM;
510}
511
512static int ibmvnic_close(struct net_device *netdev)
513{
514 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
515 struct device *dev = &adapter->vdev->dev;
516 union ibmvnic_crq crq;
517 int i;
518
519 adapter->closing = true;
520
521 for (i = 0; i < adapter->req_rx_queues; i++)
522 napi_disable(&adapter->napi[i]);
523
Thomas Falconb8efb892016-07-06 15:35:15 -0500524 netif_tx_stop_all_queues(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600525
526 if (adapter->bounce_buffer) {
527 if (!dma_mapping_error(dev, adapter->bounce_buffer_dma)) {
528 dma_unmap_single(&adapter->vdev->dev,
529 adapter->bounce_buffer_dma,
530 adapter->bounce_buffer_size,
531 DMA_BIDIRECTIONAL);
532 adapter->bounce_buffer_dma = DMA_ERROR_CODE;
533 }
534 kfree(adapter->bounce_buffer);
535 adapter->bounce_buffer = NULL;
536 }
537
538 memset(&crq, 0, sizeof(crq));
539 crq.logical_link_state.first = IBMVNIC_CRQ_CMD;
540 crq.logical_link_state.cmd = LOGICAL_LINK_STATE;
541 crq.logical_link_state.link_state = IBMVNIC_LOGICAL_LNK_DN;
542 ibmvnic_send_crq(adapter, &crq);
543
544 for (i = 0; i < be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
545 i++) {
546 kfree(adapter->tx_pool[i].tx_buff);
547 free_long_term_buff(adapter,
548 &adapter->tx_pool[i].long_term_buff);
549 kfree(adapter->tx_pool[i].free_map);
550 }
551 kfree(adapter->tx_pool);
552 adapter->tx_pool = NULL;
553
554 for (i = 0; i < be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
555 i++) {
556 free_rx_pool(adapter, &adapter->rx_pool[i]);
557 free_long_term_buff(adapter,
558 &adapter->rx_pool[i].long_term_buff);
559 }
560 kfree(adapter->rx_pool);
561 adapter->rx_pool = NULL;
562
563 adapter->closing = false;
564
565 return 0;
566}
567
Thomas Falconad7775d2016-04-01 17:20:34 -0500568/**
569 * build_hdr_data - creates L2/L3/L4 header data buffer
570 * @hdr_field - bitfield determining needed headers
571 * @skb - socket buffer
572 * @hdr_len - array of header lengths
573 * @tot_len - total length of data
574 *
575 * Reads hdr_field to determine which headers are needed by firmware.
576 * Builds a buffer containing these headers. Saves individual header
577 * lengths and total buffer length to be used to build descriptors.
578 */
579static int build_hdr_data(u8 hdr_field, struct sk_buff *skb,
580 int *hdr_len, u8 *hdr_data)
581{
582 int len = 0;
583 u8 *hdr;
584
585 hdr_len[0] = sizeof(struct ethhdr);
586
587 if (skb->protocol == htons(ETH_P_IP)) {
588 hdr_len[1] = ip_hdr(skb)->ihl * 4;
589 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
590 hdr_len[2] = tcp_hdrlen(skb);
591 else if (ip_hdr(skb)->protocol == IPPROTO_UDP)
592 hdr_len[2] = sizeof(struct udphdr);
593 } else if (skb->protocol == htons(ETH_P_IPV6)) {
594 hdr_len[1] = sizeof(struct ipv6hdr);
595 if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
596 hdr_len[2] = tcp_hdrlen(skb);
597 else if (ipv6_hdr(skb)->nexthdr == IPPROTO_UDP)
598 hdr_len[2] = sizeof(struct udphdr);
599 }
600
601 memset(hdr_data, 0, 120);
602 if ((hdr_field >> 6) & 1) {
603 hdr = skb_mac_header(skb);
604 memcpy(hdr_data, hdr, hdr_len[0]);
605 len += hdr_len[0];
606 }
607
608 if ((hdr_field >> 5) & 1) {
609 hdr = skb_network_header(skb);
610 memcpy(hdr_data + len, hdr, hdr_len[1]);
611 len += hdr_len[1];
612 }
613
614 if ((hdr_field >> 4) & 1) {
615 hdr = skb_transport_header(skb);
616 memcpy(hdr_data + len, hdr, hdr_len[2]);
617 len += hdr_len[2];
618 }
619 return len;
620}
621
622/**
623 * create_hdr_descs - create header and header extension descriptors
624 * @hdr_field - bitfield determining needed headers
625 * @data - buffer containing header data
626 * @len - length of data buffer
627 * @hdr_len - array of individual header lengths
628 * @scrq_arr - descriptor array
629 *
630 * Creates header and, if needed, header extension descriptors and
631 * places them in a descriptor array, scrq_arr
632 */
633
634static void create_hdr_descs(u8 hdr_field, u8 *hdr_data, int len, int *hdr_len,
635 union sub_crq *scrq_arr)
636{
637 union sub_crq hdr_desc;
638 int tmp_len = len;
639 u8 *data, *cur;
640 int tmp;
641
642 while (tmp_len > 0) {
643 cur = hdr_data + len - tmp_len;
644
645 memset(&hdr_desc, 0, sizeof(hdr_desc));
646 if (cur != hdr_data) {
647 data = hdr_desc.hdr_ext.data;
648 tmp = tmp_len > 29 ? 29 : tmp_len;
649 hdr_desc.hdr_ext.first = IBMVNIC_CRQ_CMD;
650 hdr_desc.hdr_ext.type = IBMVNIC_HDR_EXT_DESC;
651 hdr_desc.hdr_ext.len = tmp;
652 } else {
653 data = hdr_desc.hdr.data;
654 tmp = tmp_len > 24 ? 24 : tmp_len;
655 hdr_desc.hdr.first = IBMVNIC_CRQ_CMD;
656 hdr_desc.hdr.type = IBMVNIC_HDR_DESC;
657 hdr_desc.hdr.len = tmp;
658 hdr_desc.hdr.l2_len = (u8)hdr_len[0];
659 hdr_desc.hdr.l3_len = cpu_to_be16((u16)hdr_len[1]);
660 hdr_desc.hdr.l4_len = (u8)hdr_len[2];
661 hdr_desc.hdr.flag = hdr_field << 1;
662 }
663 memcpy(data, cur, tmp);
664 tmp_len -= tmp;
665 *scrq_arr = hdr_desc;
666 scrq_arr++;
667 }
668}
669
670/**
671 * build_hdr_descs_arr - build a header descriptor array
672 * @skb - socket buffer
673 * @num_entries - number of descriptors to be sent
674 * @subcrq - first TX descriptor
675 * @hdr_field - bit field determining which headers will be sent
676 *
677 * This function will build a TX descriptor array with applicable
678 * L2/L3/L4 packet header descriptors to be sent by send_subcrq_indirect.
679 */
680
681static void build_hdr_descs_arr(struct ibmvnic_tx_buff *txbuff,
682 int *num_entries, u8 hdr_field)
683{
684 int hdr_len[3] = {0, 0, 0};
685 int tot_len, len;
686 u8 *hdr_data = txbuff->hdr_data;
687
688 tot_len = build_hdr_data(hdr_field, txbuff->skb, hdr_len,
689 txbuff->hdr_data);
690 len = tot_len;
691 len -= 24;
692 if (len > 0)
693 num_entries += len % 29 ? len / 29 + 1 : len / 29;
694 create_hdr_descs(hdr_field, hdr_data, tot_len, hdr_len,
695 txbuff->indir_arr + 1);
696}
697
Thomas Falcon032c5e82015-12-21 11:26:06 -0600698static int ibmvnic_xmit(struct sk_buff *skb, struct net_device *netdev)
699{
700 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
701 int queue_num = skb_get_queue_mapping(skb);
Thomas Falconad7775d2016-04-01 17:20:34 -0500702 u8 *hdrs = (u8 *)&adapter->tx_rx_desc_req;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600703 struct device *dev = &adapter->vdev->dev;
704 struct ibmvnic_tx_buff *tx_buff = NULL;
705 struct ibmvnic_tx_pool *tx_pool;
706 unsigned int tx_send_failed = 0;
707 unsigned int tx_map_failed = 0;
708 unsigned int tx_dropped = 0;
709 unsigned int tx_packets = 0;
710 unsigned int tx_bytes = 0;
711 dma_addr_t data_dma_addr;
712 struct netdev_queue *txq;
713 bool used_bounce = false;
714 unsigned long lpar_rc;
715 union sub_crq tx_crq;
716 unsigned int offset;
Thomas Falconad7775d2016-04-01 17:20:34 -0500717 int num_entries = 1;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600718 unsigned char *dst;
719 u64 *handle_array;
720 int index = 0;
721 int ret = 0;
722
723 tx_pool = &adapter->tx_pool[queue_num];
724 txq = netdev_get_tx_queue(netdev, skb_get_queue_mapping(skb));
725 handle_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
726 be32_to_cpu(adapter->login_rsp_buf->
727 off_txsubm_subcrqs));
728 if (adapter->migrated) {
729 tx_send_failed++;
730 tx_dropped++;
731 ret = NETDEV_TX_BUSY;
732 goto out;
733 }
734
735 index = tx_pool->free_map[tx_pool->consumer_index];
736 offset = index * adapter->req_mtu;
737 dst = tx_pool->long_term_buff.buff + offset;
738 memset(dst, 0, adapter->req_mtu);
739 skb_copy_from_linear_data(skb, dst, skb->len);
740 data_dma_addr = tx_pool->long_term_buff.addr + offset;
741
742 tx_pool->consumer_index =
743 (tx_pool->consumer_index + 1) %
744 adapter->max_tx_entries_per_subcrq;
745
746 tx_buff = &tx_pool->tx_buff[index];
747 tx_buff->skb = skb;
748 tx_buff->data_dma[0] = data_dma_addr;
749 tx_buff->data_len[0] = skb->len;
750 tx_buff->index = index;
751 tx_buff->pool_index = queue_num;
752 tx_buff->last_frag = true;
753 tx_buff->used_bounce = used_bounce;
754
755 memset(&tx_crq, 0, sizeof(tx_crq));
756 tx_crq.v1.first = IBMVNIC_CRQ_CMD;
757 tx_crq.v1.type = IBMVNIC_TX_DESC;
758 tx_crq.v1.n_crq_elem = 1;
759 tx_crq.v1.n_sge = 1;
760 tx_crq.v1.flags1 = IBMVNIC_TX_COMP_NEEDED;
761 tx_crq.v1.correlator = cpu_to_be32(index);
762 tx_crq.v1.dma_reg = cpu_to_be16(tx_pool->long_term_buff.map_id);
763 tx_crq.v1.sge_len = cpu_to_be32(skb->len);
764 tx_crq.v1.ioba = cpu_to_be64(data_dma_addr);
765
766 if (adapter->vlan_header_insertion) {
767 tx_crq.v1.flags2 |= IBMVNIC_TX_VLAN_INSERT;
768 tx_crq.v1.vlan_id = cpu_to_be16(skb->vlan_tci);
769 }
770
771 if (skb->protocol == htons(ETH_P_IP)) {
772 if (ip_hdr(skb)->version == 4)
773 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV4;
774 else if (ip_hdr(skb)->version == 6)
775 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV6;
776
777 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
778 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_TCP;
779 else if (ip_hdr(skb)->protocol != IPPROTO_TCP)
780 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_UDP;
781 }
782
Thomas Falconad7775d2016-04-01 17:20:34 -0500783 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Thomas Falcon032c5e82015-12-21 11:26:06 -0600784 tx_crq.v1.flags1 |= IBMVNIC_TX_CHKSUM_OFFLOAD;
Thomas Falconad7775d2016-04-01 17:20:34 -0500785 hdrs += 2;
786 }
787 /* determine if l2/3/4 headers are sent to firmware */
788 if ((*hdrs >> 7) & 1 &&
789 (skb->protocol == htons(ETH_P_IP) ||
790 skb->protocol == htons(ETH_P_IPV6))) {
791 build_hdr_descs_arr(tx_buff, &num_entries, *hdrs);
792 tx_crq.v1.n_crq_elem = num_entries;
793 tx_buff->indir_arr[0] = tx_crq;
794 tx_buff->indir_dma = dma_map_single(dev, tx_buff->indir_arr,
795 sizeof(tx_buff->indir_arr),
796 DMA_TO_DEVICE);
797 if (dma_mapping_error(dev, tx_buff->indir_dma)) {
798 if (!firmware_has_feature(FW_FEATURE_CMO))
799 dev_err(dev, "tx: unable to map descriptor array\n");
800 tx_map_failed++;
801 tx_dropped++;
802 ret = NETDEV_TX_BUSY;
803 goto out;
804 }
John Allen498cd8e2016-04-06 11:49:55 -0500805 lpar_rc = send_subcrq_indirect(adapter, handle_array[queue_num],
Thomas Falconad7775d2016-04-01 17:20:34 -0500806 (u64)tx_buff->indir_dma,
807 (u64)num_entries);
808 } else {
John Allen498cd8e2016-04-06 11:49:55 -0500809 lpar_rc = send_subcrq(adapter, handle_array[queue_num],
810 &tx_crq);
Thomas Falconad7775d2016-04-01 17:20:34 -0500811 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600812 if (lpar_rc != H_SUCCESS) {
813 dev_err(dev, "tx failed with code %ld\n", lpar_rc);
814
815 if (tx_pool->consumer_index == 0)
816 tx_pool->consumer_index =
817 adapter->max_tx_entries_per_subcrq - 1;
818 else
819 tx_pool->consumer_index--;
820
821 tx_send_failed++;
822 tx_dropped++;
823 ret = NETDEV_TX_BUSY;
824 goto out;
825 }
826 tx_packets++;
827 tx_bytes += skb->len;
828 txq->trans_start = jiffies;
829 ret = NETDEV_TX_OK;
830
831out:
832 netdev->stats.tx_dropped += tx_dropped;
833 netdev->stats.tx_bytes += tx_bytes;
834 netdev->stats.tx_packets += tx_packets;
835 adapter->tx_send_failed += tx_send_failed;
836 adapter->tx_map_failed += tx_map_failed;
837
838 return ret;
839}
840
841static void ibmvnic_set_multi(struct net_device *netdev)
842{
843 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
844 struct netdev_hw_addr *ha;
845 union ibmvnic_crq crq;
846
847 memset(&crq, 0, sizeof(crq));
848 crq.request_capability.first = IBMVNIC_CRQ_CMD;
849 crq.request_capability.cmd = REQUEST_CAPABILITY;
850
851 if (netdev->flags & IFF_PROMISC) {
852 if (!adapter->promisc_supported)
853 return;
854 } else {
855 if (netdev->flags & IFF_ALLMULTI) {
856 /* Accept all multicast */
857 memset(&crq, 0, sizeof(crq));
858 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
859 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
860 crq.multicast_ctrl.flags = IBMVNIC_ENABLE_ALL;
861 ibmvnic_send_crq(adapter, &crq);
862 } else if (netdev_mc_empty(netdev)) {
863 /* Reject all multicast */
864 memset(&crq, 0, sizeof(crq));
865 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
866 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
867 crq.multicast_ctrl.flags = IBMVNIC_DISABLE_ALL;
868 ibmvnic_send_crq(adapter, &crq);
869 } else {
870 /* Accept one or more multicast(s) */
871 netdev_for_each_mc_addr(ha, netdev) {
872 memset(&crq, 0, sizeof(crq));
873 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
874 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
875 crq.multicast_ctrl.flags = IBMVNIC_ENABLE_MC;
876 ether_addr_copy(&crq.multicast_ctrl.mac_addr[0],
877 ha->addr);
878 ibmvnic_send_crq(adapter, &crq);
879 }
880 }
881 }
882}
883
884static int ibmvnic_set_mac(struct net_device *netdev, void *p)
885{
886 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
887 struct sockaddr *addr = p;
888 union ibmvnic_crq crq;
889
890 if (!is_valid_ether_addr(addr->sa_data))
891 return -EADDRNOTAVAIL;
892
893 memset(&crq, 0, sizeof(crq));
894 crq.change_mac_addr.first = IBMVNIC_CRQ_CMD;
895 crq.change_mac_addr.cmd = CHANGE_MAC_ADDR;
896 ether_addr_copy(&crq.change_mac_addr.mac_addr[0], addr->sa_data);
897 ibmvnic_send_crq(adapter, &crq);
898 /* netdev->dev_addr is changed in handle_change_mac_rsp function */
899 return 0;
900}
901
902static int ibmvnic_change_mtu(struct net_device *netdev, int new_mtu)
903{
904 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
905
906 if (new_mtu > adapter->req_mtu || new_mtu < adapter->min_mtu)
907 return -EINVAL;
908
909 netdev->mtu = new_mtu;
910 return 0;
911}
912
913static void ibmvnic_tx_timeout(struct net_device *dev)
914{
915 struct ibmvnic_adapter *adapter = netdev_priv(dev);
916 int rc;
917
918 /* Adapter timed out, resetting it */
919 release_sub_crqs(adapter);
920 rc = ibmvnic_reset_crq(adapter);
921 if (rc)
922 dev_err(&adapter->vdev->dev, "Adapter timeout, reset failed\n");
923 else
924 ibmvnic_send_crq_init(adapter);
925}
926
927static void remove_buff_from_pool(struct ibmvnic_adapter *adapter,
928 struct ibmvnic_rx_buff *rx_buff)
929{
930 struct ibmvnic_rx_pool *pool = &adapter->rx_pool[rx_buff->pool_index];
931
932 rx_buff->skb = NULL;
933
934 pool->free_map[pool->next_alloc] = (int)(rx_buff - pool->rx_buff);
935 pool->next_alloc = (pool->next_alloc + 1) % pool->size;
936
937 atomic_dec(&pool->available);
938}
939
940static int ibmvnic_poll(struct napi_struct *napi, int budget)
941{
942 struct net_device *netdev = napi->dev;
943 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
944 int scrq_num = (int)(napi - adapter->napi);
945 int frames_processed = 0;
946restart_poll:
947 while (frames_processed < budget) {
948 struct sk_buff *skb;
949 struct ibmvnic_rx_buff *rx_buff;
950 union sub_crq *next;
951 u32 length;
952 u16 offset;
953 u8 flags = 0;
954
955 if (!pending_scrq(adapter, adapter->rx_scrq[scrq_num]))
956 break;
957 next = ibmvnic_next_scrq(adapter, adapter->rx_scrq[scrq_num]);
958 rx_buff =
959 (struct ibmvnic_rx_buff *)be64_to_cpu(next->
960 rx_comp.correlator);
961 /* do error checking */
962 if (next->rx_comp.rc) {
963 netdev_err(netdev, "rx error %x\n", next->rx_comp.rc);
964 /* free the entry */
965 next->rx_comp.first = 0;
966 remove_buff_from_pool(adapter, rx_buff);
967 break;
968 }
969
970 length = be32_to_cpu(next->rx_comp.len);
971 offset = be16_to_cpu(next->rx_comp.off_frame_data);
972 flags = next->rx_comp.flags;
973 skb = rx_buff->skb;
974 skb_copy_to_linear_data(skb, rx_buff->data + offset,
975 length);
976 skb->vlan_tci = be16_to_cpu(next->rx_comp.vlan_tci);
977 /* free the entry */
978 next->rx_comp.first = 0;
979 remove_buff_from_pool(adapter, rx_buff);
980
981 skb_put(skb, length);
982 skb->protocol = eth_type_trans(skb, netdev);
983
984 if (flags & IBMVNIC_IP_CHKSUM_GOOD &&
985 flags & IBMVNIC_TCP_UDP_CHKSUM_GOOD) {
986 skb->ip_summed = CHECKSUM_UNNECESSARY;
987 }
988
989 length = skb->len;
990 napi_gro_receive(napi, skb); /* send it up */
991 netdev->stats.rx_packets++;
992 netdev->stats.rx_bytes += length;
993 frames_processed++;
994 }
John Allen498cd8e2016-04-06 11:49:55 -0500995 replenish_rx_pool(adapter, &adapter->rx_pool[scrq_num]);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600996
997 if (frames_processed < budget) {
998 enable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
999 napi_complete(napi);
1000 if (pending_scrq(adapter, adapter->rx_scrq[scrq_num]) &&
1001 napi_reschedule(napi)) {
1002 disable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
1003 goto restart_poll;
1004 }
1005 }
1006 return frames_processed;
1007}
1008
1009#ifdef CONFIG_NET_POLL_CONTROLLER
1010static void ibmvnic_netpoll_controller(struct net_device *dev)
1011{
1012 struct ibmvnic_adapter *adapter = netdev_priv(dev);
1013 int i;
1014
1015 replenish_pools(netdev_priv(dev));
1016 for (i = 0; i < adapter->req_rx_queues; i++)
1017 ibmvnic_interrupt_rx(adapter->rx_scrq[i]->irq,
1018 adapter->rx_scrq[i]);
1019}
1020#endif
1021
1022static const struct net_device_ops ibmvnic_netdev_ops = {
1023 .ndo_open = ibmvnic_open,
1024 .ndo_stop = ibmvnic_close,
1025 .ndo_start_xmit = ibmvnic_xmit,
1026 .ndo_set_rx_mode = ibmvnic_set_multi,
1027 .ndo_set_mac_address = ibmvnic_set_mac,
1028 .ndo_validate_addr = eth_validate_addr,
1029 .ndo_change_mtu = ibmvnic_change_mtu,
1030 .ndo_tx_timeout = ibmvnic_tx_timeout,
1031#ifdef CONFIG_NET_POLL_CONTROLLER
1032 .ndo_poll_controller = ibmvnic_netpoll_controller,
1033#endif
1034};
1035
1036/* ethtool functions */
1037
1038static int ibmvnic_get_settings(struct net_device *netdev,
1039 struct ethtool_cmd *cmd)
1040{
1041 cmd->supported = (SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg |
1042 SUPPORTED_FIBRE);
1043 cmd->advertising = (ADVERTISED_1000baseT_Full | ADVERTISED_Autoneg |
1044 ADVERTISED_FIBRE);
1045 ethtool_cmd_speed_set(cmd, SPEED_1000);
1046 cmd->duplex = DUPLEX_FULL;
1047 cmd->port = PORT_FIBRE;
1048 cmd->phy_address = 0;
1049 cmd->transceiver = XCVR_INTERNAL;
1050 cmd->autoneg = AUTONEG_ENABLE;
1051 cmd->maxtxpkt = 0;
1052 cmd->maxrxpkt = 1;
1053 return 0;
1054}
1055
1056static void ibmvnic_get_drvinfo(struct net_device *dev,
1057 struct ethtool_drvinfo *info)
1058{
1059 strlcpy(info->driver, ibmvnic_driver_name, sizeof(info->driver));
1060 strlcpy(info->version, IBMVNIC_DRIVER_VERSION, sizeof(info->version));
1061}
1062
1063static u32 ibmvnic_get_msglevel(struct net_device *netdev)
1064{
1065 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1066
1067 return adapter->msg_enable;
1068}
1069
1070static void ibmvnic_set_msglevel(struct net_device *netdev, u32 data)
1071{
1072 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1073
1074 adapter->msg_enable = data;
1075}
1076
1077static u32 ibmvnic_get_link(struct net_device *netdev)
1078{
1079 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1080
1081 /* Don't need to send a query because we request a logical link up at
1082 * init and then we wait for link state indications
1083 */
1084 return adapter->logical_link_state;
1085}
1086
1087static void ibmvnic_get_ringparam(struct net_device *netdev,
1088 struct ethtool_ringparam *ring)
1089{
1090 ring->rx_max_pending = 0;
1091 ring->tx_max_pending = 0;
1092 ring->rx_mini_max_pending = 0;
1093 ring->rx_jumbo_max_pending = 0;
1094 ring->rx_pending = 0;
1095 ring->tx_pending = 0;
1096 ring->rx_mini_pending = 0;
1097 ring->rx_jumbo_pending = 0;
1098}
1099
1100static void ibmvnic_get_strings(struct net_device *dev, u32 stringset, u8 *data)
1101{
1102 int i;
1103
1104 if (stringset != ETH_SS_STATS)
1105 return;
1106
1107 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++, data += ETH_GSTRING_LEN)
1108 memcpy(data, ibmvnic_stats[i].name, ETH_GSTRING_LEN);
1109}
1110
1111static int ibmvnic_get_sset_count(struct net_device *dev, int sset)
1112{
1113 switch (sset) {
1114 case ETH_SS_STATS:
1115 return ARRAY_SIZE(ibmvnic_stats);
1116 default:
1117 return -EOPNOTSUPP;
1118 }
1119}
1120
1121static void ibmvnic_get_ethtool_stats(struct net_device *dev,
1122 struct ethtool_stats *stats, u64 *data)
1123{
1124 struct ibmvnic_adapter *adapter = netdev_priv(dev);
1125 union ibmvnic_crq crq;
1126 int i;
1127
1128 memset(&crq, 0, sizeof(crq));
1129 crq.request_statistics.first = IBMVNIC_CRQ_CMD;
1130 crq.request_statistics.cmd = REQUEST_STATISTICS;
1131 crq.request_statistics.ioba = cpu_to_be32(adapter->stats_token);
1132 crq.request_statistics.len =
1133 cpu_to_be32(sizeof(struct ibmvnic_statistics));
1134 ibmvnic_send_crq(adapter, &crq);
1135
1136 /* Wait for data to be written */
1137 init_completion(&adapter->stats_done);
1138 wait_for_completion(&adapter->stats_done);
1139
1140 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++)
1141 data[i] = IBMVNIC_GET_STAT(adapter, ibmvnic_stats[i].offset);
1142}
1143
1144static const struct ethtool_ops ibmvnic_ethtool_ops = {
1145 .get_settings = ibmvnic_get_settings,
1146 .get_drvinfo = ibmvnic_get_drvinfo,
1147 .get_msglevel = ibmvnic_get_msglevel,
1148 .set_msglevel = ibmvnic_set_msglevel,
1149 .get_link = ibmvnic_get_link,
1150 .get_ringparam = ibmvnic_get_ringparam,
1151 .get_strings = ibmvnic_get_strings,
1152 .get_sset_count = ibmvnic_get_sset_count,
1153 .get_ethtool_stats = ibmvnic_get_ethtool_stats,
1154};
1155
1156/* Routines for managing CRQs/sCRQs */
1157
1158static void release_sub_crq_queue(struct ibmvnic_adapter *adapter,
1159 struct ibmvnic_sub_crq_queue *scrq)
1160{
1161 struct device *dev = &adapter->vdev->dev;
1162 long rc;
1163
1164 netdev_dbg(adapter->netdev, "Releasing sub-CRQ\n");
1165
1166 /* Close the sub-crqs */
1167 do {
1168 rc = plpar_hcall_norets(H_FREE_SUB_CRQ,
1169 adapter->vdev->unit_address,
1170 scrq->crq_num);
1171 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
1172
1173 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
1174 DMA_BIDIRECTIONAL);
1175 free_pages((unsigned long)scrq->msgs, 2);
1176 kfree(scrq);
1177}
1178
1179static struct ibmvnic_sub_crq_queue *init_sub_crq_queue(struct ibmvnic_adapter
1180 *adapter)
1181{
1182 struct device *dev = &adapter->vdev->dev;
1183 struct ibmvnic_sub_crq_queue *scrq;
1184 int rc;
1185
1186 scrq = kmalloc(sizeof(*scrq), GFP_ATOMIC);
1187 if (!scrq)
1188 return NULL;
1189
1190 scrq->msgs = (union sub_crq *)__get_free_pages(GFP_KERNEL, 2);
1191 memset(scrq->msgs, 0, 4 * PAGE_SIZE);
1192 if (!scrq->msgs) {
1193 dev_warn(dev, "Couldn't allocate crq queue messages page\n");
1194 goto zero_page_failed;
1195 }
1196
1197 scrq->msg_token = dma_map_single(dev, scrq->msgs, 4 * PAGE_SIZE,
1198 DMA_BIDIRECTIONAL);
1199 if (dma_mapping_error(dev, scrq->msg_token)) {
1200 dev_warn(dev, "Couldn't map crq queue messages page\n");
1201 goto map_failed;
1202 }
1203
1204 rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
1205 4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
1206
1207 if (rc == H_RESOURCE)
1208 rc = ibmvnic_reset_crq(adapter);
1209
1210 if (rc == H_CLOSED) {
1211 dev_warn(dev, "Partner adapter not ready, waiting.\n");
1212 } else if (rc) {
1213 dev_warn(dev, "Error %d registering sub-crq\n", rc);
1214 goto reg_failed;
1215 }
1216
Thomas Falcon032c5e82015-12-21 11:26:06 -06001217 scrq->adapter = adapter;
1218 scrq->size = 4 * PAGE_SIZE / sizeof(*scrq->msgs);
1219 scrq->cur = 0;
1220 scrq->rx_skb_top = NULL;
1221 spin_lock_init(&scrq->lock);
1222
1223 netdev_dbg(adapter->netdev,
1224 "sub-crq initialized, num %lx, hw_irq=%lx, irq=%x\n",
1225 scrq->crq_num, scrq->hw_irq, scrq->irq);
1226
1227 return scrq;
1228
Thomas Falcon032c5e82015-12-21 11:26:06 -06001229reg_failed:
1230 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
1231 DMA_BIDIRECTIONAL);
1232map_failed:
1233 free_pages((unsigned long)scrq->msgs, 2);
1234zero_page_failed:
1235 kfree(scrq);
1236
1237 return NULL;
1238}
1239
1240static void release_sub_crqs(struct ibmvnic_adapter *adapter)
1241{
1242 int i;
1243
1244 if (adapter->tx_scrq) {
1245 for (i = 0; i < adapter->req_tx_queues; i++)
1246 if (adapter->tx_scrq[i]) {
1247 free_irq(adapter->tx_scrq[i]->irq,
1248 adapter->tx_scrq[i]);
Thomas Falcon88eb98a2016-07-06 15:35:16 -05001249 irq_dispose_mapping(adapter->tx_scrq[i]->irq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001250 release_sub_crq_queue(adapter,
1251 adapter->tx_scrq[i]);
1252 }
1253 adapter->tx_scrq = NULL;
1254 }
1255
1256 if (adapter->rx_scrq) {
1257 for (i = 0; i < adapter->req_rx_queues; i++)
1258 if (adapter->rx_scrq[i]) {
1259 free_irq(adapter->rx_scrq[i]->irq,
1260 adapter->rx_scrq[i]);
Thomas Falcon88eb98a2016-07-06 15:35:16 -05001261 irq_dispose_mapping(adapter->rx_scrq[i]->irq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001262 release_sub_crq_queue(adapter,
1263 adapter->rx_scrq[i]);
1264 }
1265 adapter->rx_scrq = NULL;
1266 }
1267
1268 adapter->requested_caps = 0;
1269}
1270
Thomas Falconea22d512016-07-06 15:35:17 -05001271static void release_sub_crqs_no_irqs(struct ibmvnic_adapter *adapter)
1272{
1273 int i;
1274
1275 if (adapter->tx_scrq) {
1276 for (i = 0; i < adapter->req_tx_queues; i++)
1277 if (adapter->tx_scrq[i])
1278 release_sub_crq_queue(adapter,
1279 adapter->tx_scrq[i]);
1280 adapter->tx_scrq = NULL;
1281 }
1282
1283 if (adapter->rx_scrq) {
1284 for (i = 0; i < adapter->req_rx_queues; i++)
1285 if (adapter->rx_scrq[i])
1286 release_sub_crq_queue(adapter,
1287 adapter->rx_scrq[i]);
1288 adapter->rx_scrq = NULL;
1289 }
1290
1291 adapter->requested_caps = 0;
1292}
1293
Thomas Falcon032c5e82015-12-21 11:26:06 -06001294static int disable_scrq_irq(struct ibmvnic_adapter *adapter,
1295 struct ibmvnic_sub_crq_queue *scrq)
1296{
1297 struct device *dev = &adapter->vdev->dev;
1298 unsigned long rc;
1299
1300 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
1301 H_DISABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
1302 if (rc)
1303 dev_err(dev, "Couldn't disable scrq irq 0x%lx. rc=%ld\n",
1304 scrq->hw_irq, rc);
1305 return rc;
1306}
1307
1308static int enable_scrq_irq(struct ibmvnic_adapter *adapter,
1309 struct ibmvnic_sub_crq_queue *scrq)
1310{
1311 struct device *dev = &adapter->vdev->dev;
1312 unsigned long rc;
1313
1314 if (scrq->hw_irq > 0x100000000ULL) {
1315 dev_err(dev, "bad hw_irq = %lx\n", scrq->hw_irq);
1316 return 1;
1317 }
1318
1319 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
1320 H_ENABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
1321 if (rc)
1322 dev_err(dev, "Couldn't enable scrq irq 0x%lx. rc=%ld\n",
1323 scrq->hw_irq, rc);
1324 return rc;
1325}
1326
1327static int ibmvnic_complete_tx(struct ibmvnic_adapter *adapter,
1328 struct ibmvnic_sub_crq_queue *scrq)
1329{
1330 struct device *dev = &adapter->vdev->dev;
1331 struct ibmvnic_tx_buff *txbuff;
1332 union sub_crq *next;
1333 int index;
1334 int i, j;
Thomas Falconad7775d2016-04-01 17:20:34 -05001335 u8 first;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001336
1337restart_loop:
1338 while (pending_scrq(adapter, scrq)) {
1339 unsigned int pool = scrq->pool_index;
1340
1341 next = ibmvnic_next_scrq(adapter, scrq);
1342 for (i = 0; i < next->tx_comp.num_comps; i++) {
1343 if (next->tx_comp.rcs[i]) {
1344 dev_err(dev, "tx error %x\n",
1345 next->tx_comp.rcs[i]);
1346 continue;
1347 }
1348 index = be32_to_cpu(next->tx_comp.correlators[i]);
1349 txbuff = &adapter->tx_pool[pool].tx_buff[index];
1350
1351 for (j = 0; j < IBMVNIC_MAX_FRAGS_PER_CRQ; j++) {
1352 if (!txbuff->data_dma[j])
1353 continue;
1354
1355 txbuff->data_dma[j] = 0;
1356 txbuff->used_bounce = false;
1357 }
Thomas Falconad7775d2016-04-01 17:20:34 -05001358 /* if sub_crq was sent indirectly */
1359 first = txbuff->indir_arr[0].generic.first;
1360 if (first == IBMVNIC_CRQ_CMD) {
1361 dma_unmap_single(dev, txbuff->indir_dma,
1362 sizeof(txbuff->indir_arr),
1363 DMA_TO_DEVICE);
1364 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001365
1366 if (txbuff->last_frag)
1367 dev_kfree_skb_any(txbuff->skb);
1368
1369 adapter->tx_pool[pool].free_map[adapter->tx_pool[pool].
1370 producer_index] = index;
1371 adapter->tx_pool[pool].producer_index =
1372 (adapter->tx_pool[pool].producer_index + 1) %
1373 adapter->max_tx_entries_per_subcrq;
1374 }
1375 /* remove tx_comp scrq*/
1376 next->tx_comp.first = 0;
1377 }
1378
1379 enable_scrq_irq(adapter, scrq);
1380
1381 if (pending_scrq(adapter, scrq)) {
1382 disable_scrq_irq(adapter, scrq);
1383 goto restart_loop;
1384 }
1385
1386 return 0;
1387}
1388
1389static irqreturn_t ibmvnic_interrupt_tx(int irq, void *instance)
1390{
1391 struct ibmvnic_sub_crq_queue *scrq = instance;
1392 struct ibmvnic_adapter *adapter = scrq->adapter;
1393
1394 disable_scrq_irq(adapter, scrq);
1395 ibmvnic_complete_tx(adapter, scrq);
1396
1397 return IRQ_HANDLED;
1398}
1399
1400static irqreturn_t ibmvnic_interrupt_rx(int irq, void *instance)
1401{
1402 struct ibmvnic_sub_crq_queue *scrq = instance;
1403 struct ibmvnic_adapter *adapter = scrq->adapter;
1404
1405 if (napi_schedule_prep(&adapter->napi[scrq->scrq_num])) {
1406 disable_scrq_irq(adapter, scrq);
1407 __napi_schedule(&adapter->napi[scrq->scrq_num]);
1408 }
1409
1410 return IRQ_HANDLED;
1411}
1412
Thomas Falconea22d512016-07-06 15:35:17 -05001413static int init_sub_crq_irqs(struct ibmvnic_adapter *adapter)
1414{
1415 struct device *dev = &adapter->vdev->dev;
1416 struct ibmvnic_sub_crq_queue *scrq;
1417 int i = 0, j = 0;
1418 int rc = 0;
1419
1420 for (i = 0; i < adapter->req_tx_queues; i++) {
1421 scrq = adapter->tx_scrq[i];
1422 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
1423
1424 if (scrq->irq == NO_IRQ) {
1425 rc = -EINVAL;
1426 dev_err(dev, "Error mapping irq\n");
1427 goto req_tx_irq_failed;
1428 }
1429
1430 rc = request_irq(scrq->irq, ibmvnic_interrupt_tx,
1431 0, "ibmvnic_tx", scrq);
1432
1433 if (rc) {
1434 dev_err(dev, "Couldn't register tx irq 0x%x. rc=%d\n",
1435 scrq->irq, rc);
1436 irq_dispose_mapping(scrq->irq);
1437 goto req_rx_irq_failed;
1438 }
1439 }
1440
1441 for (i = 0; i < adapter->req_rx_queues; i++) {
1442 scrq = adapter->rx_scrq[i];
1443 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
1444 if (scrq->irq == NO_IRQ) {
1445 rc = -EINVAL;
1446 dev_err(dev, "Error mapping irq\n");
1447 goto req_rx_irq_failed;
1448 }
1449 rc = request_irq(scrq->irq, ibmvnic_interrupt_rx,
1450 0, "ibmvnic_rx", scrq);
1451 if (rc) {
1452 dev_err(dev, "Couldn't register rx irq 0x%x. rc=%d\n",
1453 scrq->irq, rc);
1454 irq_dispose_mapping(scrq->irq);
1455 goto req_rx_irq_failed;
1456 }
1457 }
1458 return rc;
1459
1460req_rx_irq_failed:
1461 for (j = 0; j < i; j++)
1462 free_irq(adapter->rx_scrq[j]->irq, adapter->rx_scrq[j]);
1463 irq_dispose_mapping(adapter->rx_scrq[j]->irq);
1464 i = adapter->req_tx_queues;
1465req_tx_irq_failed:
1466 for (j = 0; j < i; j++)
1467 free_irq(adapter->tx_scrq[j]->irq, adapter->tx_scrq[j]);
1468 irq_dispose_mapping(adapter->rx_scrq[j]->irq);
1469 release_sub_crqs_no_irqs(adapter);
1470 return rc;
1471}
1472
Thomas Falcon032c5e82015-12-21 11:26:06 -06001473static void init_sub_crqs(struct ibmvnic_adapter *adapter, int retry)
1474{
1475 struct device *dev = &adapter->vdev->dev;
1476 struct ibmvnic_sub_crq_queue **allqueues;
1477 int registered_queues = 0;
1478 union ibmvnic_crq crq;
1479 int total_queues;
1480 int more = 0;
Thomas Falconea22d512016-07-06 15:35:17 -05001481 int i;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001482
1483 if (!retry) {
1484 /* Sub-CRQ entries are 32 byte long */
1485 int entries_page = 4 * PAGE_SIZE / (sizeof(u64) * 4);
1486
1487 if (adapter->min_tx_entries_per_subcrq > entries_page ||
1488 adapter->min_rx_add_entries_per_subcrq > entries_page) {
1489 dev_err(dev, "Fatal, invalid entries per sub-crq\n");
1490 goto allqueues_failed;
1491 }
1492
1493 /* Get the minimum between the queried max and the entries
1494 * that fit in our PAGE_SIZE
1495 */
1496 adapter->req_tx_entries_per_subcrq =
1497 adapter->max_tx_entries_per_subcrq > entries_page ?
1498 entries_page : adapter->max_tx_entries_per_subcrq;
1499 adapter->req_rx_add_entries_per_subcrq =
1500 adapter->max_rx_add_entries_per_subcrq > entries_page ?
1501 entries_page : adapter->max_rx_add_entries_per_subcrq;
1502
1503 /* Choosing the maximum number of queues supported by firmware*/
John Allen498cd8e2016-04-06 11:49:55 -05001504 adapter->req_tx_queues = adapter->max_tx_queues;
1505 adapter->req_rx_queues = adapter->max_rx_queues;
1506 adapter->req_rx_add_queues = adapter->max_rx_add_queues;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001507
1508 adapter->req_mtu = adapter->max_mtu;
1509 }
1510
1511 total_queues = adapter->req_tx_queues + adapter->req_rx_queues;
1512
1513 allqueues = kcalloc(total_queues, sizeof(*allqueues), GFP_ATOMIC);
1514 if (!allqueues)
1515 goto allqueues_failed;
1516
1517 for (i = 0; i < total_queues; i++) {
1518 allqueues[i] = init_sub_crq_queue(adapter);
1519 if (!allqueues[i]) {
1520 dev_warn(dev, "Couldn't allocate all sub-crqs\n");
1521 break;
1522 }
1523 registered_queues++;
1524 }
1525
1526 /* Make sure we were able to register the minimum number of queues */
1527 if (registered_queues <
1528 adapter->min_tx_queues + adapter->min_rx_queues) {
1529 dev_err(dev, "Fatal: Couldn't init min number of sub-crqs\n");
1530 goto tx_failed;
1531 }
1532
1533 /* Distribute the failed allocated queues*/
1534 for (i = 0; i < total_queues - registered_queues + more ; i++) {
1535 netdev_dbg(adapter->netdev, "Reducing number of queues\n");
1536 switch (i % 3) {
1537 case 0:
1538 if (adapter->req_rx_queues > adapter->min_rx_queues)
1539 adapter->req_rx_queues--;
1540 else
1541 more++;
1542 break;
1543 case 1:
1544 if (adapter->req_tx_queues > adapter->min_tx_queues)
1545 adapter->req_tx_queues--;
1546 else
1547 more++;
1548 break;
1549 }
1550 }
1551
1552 adapter->tx_scrq = kcalloc(adapter->req_tx_queues,
1553 sizeof(*adapter->tx_scrq), GFP_ATOMIC);
1554 if (!adapter->tx_scrq)
1555 goto tx_failed;
1556
1557 for (i = 0; i < adapter->req_tx_queues; i++) {
1558 adapter->tx_scrq[i] = allqueues[i];
1559 adapter->tx_scrq[i]->pool_index = i;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001560 }
1561
1562 adapter->rx_scrq = kcalloc(adapter->req_rx_queues,
1563 sizeof(*adapter->rx_scrq), GFP_ATOMIC);
1564 if (!adapter->rx_scrq)
1565 goto rx_failed;
1566
1567 for (i = 0; i < adapter->req_rx_queues; i++) {
1568 adapter->rx_scrq[i] = allqueues[i + adapter->req_tx_queues];
1569 adapter->rx_scrq[i]->scrq_num = i;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001570 }
1571
1572 memset(&crq, 0, sizeof(crq));
1573 crq.request_capability.first = IBMVNIC_CRQ_CMD;
1574 crq.request_capability.cmd = REQUEST_CAPABILITY;
1575
1576 crq.request_capability.capability = cpu_to_be16(REQ_TX_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06001577 crq.request_capability.number = cpu_to_be64(adapter->req_tx_queues);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001578 ibmvnic_send_crq(adapter, &crq);
1579
1580 crq.request_capability.capability = cpu_to_be16(REQ_RX_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06001581 crq.request_capability.number = cpu_to_be64(adapter->req_rx_queues);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001582 ibmvnic_send_crq(adapter, &crq);
1583
1584 crq.request_capability.capability = cpu_to_be16(REQ_RX_ADD_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06001585 crq.request_capability.number = cpu_to_be64(adapter->req_rx_add_queues);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001586 ibmvnic_send_crq(adapter, &crq);
1587
1588 crq.request_capability.capability =
1589 cpu_to_be16(REQ_TX_ENTRIES_PER_SUBCRQ);
1590 crq.request_capability.number =
Thomas Falconde89e852016-03-01 10:20:09 -06001591 cpu_to_be64(adapter->req_tx_entries_per_subcrq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001592 ibmvnic_send_crq(adapter, &crq);
1593
1594 crq.request_capability.capability =
1595 cpu_to_be16(REQ_RX_ADD_ENTRIES_PER_SUBCRQ);
1596 crq.request_capability.number =
Thomas Falconde89e852016-03-01 10:20:09 -06001597 cpu_to_be64(adapter->req_rx_add_entries_per_subcrq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001598 ibmvnic_send_crq(adapter, &crq);
1599
1600 crq.request_capability.capability = cpu_to_be16(REQ_MTU);
Thomas Falconde89e852016-03-01 10:20:09 -06001601 crq.request_capability.number = cpu_to_be64(adapter->req_mtu);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001602 ibmvnic_send_crq(adapter, &crq);
1603
1604 if (adapter->netdev->flags & IFF_PROMISC) {
1605 if (adapter->promisc_supported) {
1606 crq.request_capability.capability =
1607 cpu_to_be16(PROMISC_REQUESTED);
Thomas Falconde89e852016-03-01 10:20:09 -06001608 crq.request_capability.number = cpu_to_be64(1);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001609 ibmvnic_send_crq(adapter, &crq);
1610 }
1611 } else {
1612 crq.request_capability.capability =
1613 cpu_to_be16(PROMISC_REQUESTED);
Thomas Falconde89e852016-03-01 10:20:09 -06001614 crq.request_capability.number = cpu_to_be64(0);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001615 ibmvnic_send_crq(adapter, &crq);
1616 }
1617
1618 kfree(allqueues);
1619
1620 return;
1621
Thomas Falcon032c5e82015-12-21 11:26:06 -06001622rx_failed:
1623 kfree(adapter->tx_scrq);
1624 adapter->tx_scrq = NULL;
1625tx_failed:
1626 for (i = 0; i < registered_queues; i++)
1627 release_sub_crq_queue(adapter, allqueues[i]);
1628 kfree(allqueues);
1629allqueues_failed:
1630 ibmvnic_remove(adapter->vdev);
1631}
1632
1633static int pending_scrq(struct ibmvnic_adapter *adapter,
1634 struct ibmvnic_sub_crq_queue *scrq)
1635{
1636 union sub_crq *entry = &scrq->msgs[scrq->cur];
1637
1638 if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP || adapter->closing)
1639 return 1;
1640 else
1641 return 0;
1642}
1643
1644static union sub_crq *ibmvnic_next_scrq(struct ibmvnic_adapter *adapter,
1645 struct ibmvnic_sub_crq_queue *scrq)
1646{
1647 union sub_crq *entry;
1648 unsigned long flags;
1649
1650 spin_lock_irqsave(&scrq->lock, flags);
1651 entry = &scrq->msgs[scrq->cur];
1652 if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP) {
1653 if (++scrq->cur == scrq->size)
1654 scrq->cur = 0;
1655 } else {
1656 entry = NULL;
1657 }
1658 spin_unlock_irqrestore(&scrq->lock, flags);
1659
1660 return entry;
1661}
1662
1663static union ibmvnic_crq *ibmvnic_next_crq(struct ibmvnic_adapter *adapter)
1664{
1665 struct ibmvnic_crq_queue *queue = &adapter->crq;
1666 union ibmvnic_crq *crq;
1667
1668 crq = &queue->msgs[queue->cur];
1669 if (crq->generic.first & IBMVNIC_CRQ_CMD_RSP) {
1670 if (++queue->cur == queue->size)
1671 queue->cur = 0;
1672 } else {
1673 crq = NULL;
1674 }
1675
1676 return crq;
1677}
1678
1679static int send_subcrq(struct ibmvnic_adapter *adapter, u64 remote_handle,
1680 union sub_crq *sub_crq)
1681{
1682 unsigned int ua = adapter->vdev->unit_address;
1683 struct device *dev = &adapter->vdev->dev;
1684 u64 *u64_crq = (u64 *)sub_crq;
1685 int rc;
1686
1687 netdev_dbg(adapter->netdev,
1688 "Sending sCRQ %016lx: %016lx %016lx %016lx %016lx\n",
1689 (unsigned long int)cpu_to_be64(remote_handle),
1690 (unsigned long int)cpu_to_be64(u64_crq[0]),
1691 (unsigned long int)cpu_to_be64(u64_crq[1]),
1692 (unsigned long int)cpu_to_be64(u64_crq[2]),
1693 (unsigned long int)cpu_to_be64(u64_crq[3]));
1694
1695 /* Make sure the hypervisor sees the complete request */
1696 mb();
1697
1698 rc = plpar_hcall_norets(H_SEND_SUB_CRQ, ua,
1699 cpu_to_be64(remote_handle),
1700 cpu_to_be64(u64_crq[0]),
1701 cpu_to_be64(u64_crq[1]),
1702 cpu_to_be64(u64_crq[2]),
1703 cpu_to_be64(u64_crq[3]));
1704
1705 if (rc) {
1706 if (rc == H_CLOSED)
1707 dev_warn(dev, "CRQ Queue closed\n");
1708 dev_err(dev, "Send error (rc=%d)\n", rc);
1709 }
1710
1711 return rc;
1712}
1713
Thomas Falconad7775d2016-04-01 17:20:34 -05001714static int send_subcrq_indirect(struct ibmvnic_adapter *adapter,
1715 u64 remote_handle, u64 ioba, u64 num_entries)
1716{
1717 unsigned int ua = adapter->vdev->unit_address;
1718 struct device *dev = &adapter->vdev->dev;
1719 int rc;
1720
1721 /* Make sure the hypervisor sees the complete request */
1722 mb();
1723 rc = plpar_hcall_norets(H_SEND_SUB_CRQ_INDIRECT, ua,
1724 cpu_to_be64(remote_handle),
1725 ioba, num_entries);
1726
1727 if (rc) {
1728 if (rc == H_CLOSED)
1729 dev_warn(dev, "CRQ Queue closed\n");
1730 dev_err(dev, "Send (indirect) error (rc=%d)\n", rc);
1731 }
1732
1733 return rc;
1734}
1735
Thomas Falcon032c5e82015-12-21 11:26:06 -06001736static int ibmvnic_send_crq(struct ibmvnic_adapter *adapter,
1737 union ibmvnic_crq *crq)
1738{
1739 unsigned int ua = adapter->vdev->unit_address;
1740 struct device *dev = &adapter->vdev->dev;
1741 u64 *u64_crq = (u64 *)crq;
1742 int rc;
1743
1744 netdev_dbg(adapter->netdev, "Sending CRQ: %016lx %016lx\n",
1745 (unsigned long int)cpu_to_be64(u64_crq[0]),
1746 (unsigned long int)cpu_to_be64(u64_crq[1]));
1747
1748 /* Make sure the hypervisor sees the complete request */
1749 mb();
1750
1751 rc = plpar_hcall_norets(H_SEND_CRQ, ua,
1752 cpu_to_be64(u64_crq[0]),
1753 cpu_to_be64(u64_crq[1]));
1754
1755 if (rc) {
1756 if (rc == H_CLOSED)
1757 dev_warn(dev, "CRQ Queue closed\n");
1758 dev_warn(dev, "Send error (rc=%d)\n", rc);
1759 }
1760
1761 return rc;
1762}
1763
1764static int ibmvnic_send_crq_init(struct ibmvnic_adapter *adapter)
1765{
1766 union ibmvnic_crq crq;
1767
1768 memset(&crq, 0, sizeof(crq));
1769 crq.generic.first = IBMVNIC_CRQ_INIT_CMD;
1770 crq.generic.cmd = IBMVNIC_CRQ_INIT;
1771 netdev_dbg(adapter->netdev, "Sending CRQ init\n");
1772
1773 return ibmvnic_send_crq(adapter, &crq);
1774}
1775
1776static int ibmvnic_send_crq_init_complete(struct ibmvnic_adapter *adapter)
1777{
1778 union ibmvnic_crq crq;
1779
1780 memset(&crq, 0, sizeof(crq));
1781 crq.generic.first = IBMVNIC_CRQ_INIT_CMD;
1782 crq.generic.cmd = IBMVNIC_CRQ_INIT_COMPLETE;
1783 netdev_dbg(adapter->netdev, "Sending CRQ init complete\n");
1784
1785 return ibmvnic_send_crq(adapter, &crq);
1786}
1787
1788static int send_version_xchg(struct ibmvnic_adapter *adapter)
1789{
1790 union ibmvnic_crq crq;
1791
1792 memset(&crq, 0, sizeof(crq));
1793 crq.version_exchange.first = IBMVNIC_CRQ_CMD;
1794 crq.version_exchange.cmd = VERSION_EXCHANGE;
1795 crq.version_exchange.version = cpu_to_be16(ibmvnic_version);
1796
1797 return ibmvnic_send_crq(adapter, &crq);
1798}
1799
1800static void send_login(struct ibmvnic_adapter *adapter)
1801{
1802 struct ibmvnic_login_rsp_buffer *login_rsp_buffer;
1803 struct ibmvnic_login_buffer *login_buffer;
1804 struct ibmvnic_inflight_cmd *inflight_cmd;
1805 struct device *dev = &adapter->vdev->dev;
1806 dma_addr_t rsp_buffer_token;
1807 dma_addr_t buffer_token;
1808 size_t rsp_buffer_size;
1809 union ibmvnic_crq crq;
1810 unsigned long flags;
1811 size_t buffer_size;
1812 __be64 *tx_list_p;
1813 __be64 *rx_list_p;
1814 int i;
1815
1816 buffer_size =
1817 sizeof(struct ibmvnic_login_buffer) +
1818 sizeof(u64) * (adapter->req_tx_queues + adapter->req_rx_queues);
1819
1820 login_buffer = kmalloc(buffer_size, GFP_ATOMIC);
1821 if (!login_buffer)
1822 goto buf_alloc_failed;
1823
1824 buffer_token = dma_map_single(dev, login_buffer, buffer_size,
1825 DMA_TO_DEVICE);
1826 if (dma_mapping_error(dev, buffer_token)) {
1827 dev_err(dev, "Couldn't map login buffer\n");
1828 goto buf_map_failed;
1829 }
1830
John Allen498cd8e2016-04-06 11:49:55 -05001831 rsp_buffer_size = sizeof(struct ibmvnic_login_rsp_buffer) +
1832 sizeof(u64) * adapter->req_tx_queues +
1833 sizeof(u64) * adapter->req_rx_queues +
1834 sizeof(u64) * adapter->req_rx_queues +
1835 sizeof(u8) * IBMVNIC_TX_DESC_VERSIONS;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001836
1837 login_rsp_buffer = kmalloc(rsp_buffer_size, GFP_ATOMIC);
1838 if (!login_rsp_buffer)
1839 goto buf_rsp_alloc_failed;
1840
1841 rsp_buffer_token = dma_map_single(dev, login_rsp_buffer,
1842 rsp_buffer_size, DMA_FROM_DEVICE);
1843 if (dma_mapping_error(dev, rsp_buffer_token)) {
1844 dev_err(dev, "Couldn't map login rsp buffer\n");
1845 goto buf_rsp_map_failed;
1846 }
1847 inflight_cmd = kmalloc(sizeof(*inflight_cmd), GFP_ATOMIC);
1848 if (!inflight_cmd) {
1849 dev_err(dev, "Couldn't allocate inflight_cmd\n");
1850 goto inflight_alloc_failed;
1851 }
1852 adapter->login_buf = login_buffer;
1853 adapter->login_buf_token = buffer_token;
1854 adapter->login_buf_sz = buffer_size;
1855 adapter->login_rsp_buf = login_rsp_buffer;
1856 adapter->login_rsp_buf_token = rsp_buffer_token;
1857 adapter->login_rsp_buf_sz = rsp_buffer_size;
1858
1859 login_buffer->len = cpu_to_be32(buffer_size);
1860 login_buffer->version = cpu_to_be32(INITIAL_VERSION_LB);
1861 login_buffer->num_txcomp_subcrqs = cpu_to_be32(adapter->req_tx_queues);
1862 login_buffer->off_txcomp_subcrqs =
1863 cpu_to_be32(sizeof(struct ibmvnic_login_buffer));
1864 login_buffer->num_rxcomp_subcrqs = cpu_to_be32(adapter->req_rx_queues);
1865 login_buffer->off_rxcomp_subcrqs =
1866 cpu_to_be32(sizeof(struct ibmvnic_login_buffer) +
1867 sizeof(u64) * adapter->req_tx_queues);
1868 login_buffer->login_rsp_ioba = cpu_to_be32(rsp_buffer_token);
1869 login_buffer->login_rsp_len = cpu_to_be32(rsp_buffer_size);
1870
1871 tx_list_p = (__be64 *)((char *)login_buffer +
1872 sizeof(struct ibmvnic_login_buffer));
1873 rx_list_p = (__be64 *)((char *)login_buffer +
1874 sizeof(struct ibmvnic_login_buffer) +
1875 sizeof(u64) * adapter->req_tx_queues);
1876
1877 for (i = 0; i < adapter->req_tx_queues; i++) {
1878 if (adapter->tx_scrq[i]) {
1879 tx_list_p[i] = cpu_to_be64(adapter->tx_scrq[i]->
1880 crq_num);
1881 }
1882 }
1883
1884 for (i = 0; i < adapter->req_rx_queues; i++) {
1885 if (adapter->rx_scrq[i]) {
1886 rx_list_p[i] = cpu_to_be64(adapter->rx_scrq[i]->
1887 crq_num);
1888 }
1889 }
1890
1891 netdev_dbg(adapter->netdev, "Login Buffer:\n");
1892 for (i = 0; i < (adapter->login_buf_sz - 1) / 8 + 1; i++) {
1893 netdev_dbg(adapter->netdev, "%016lx\n",
1894 ((unsigned long int *)(adapter->login_buf))[i]);
1895 }
1896
1897 memset(&crq, 0, sizeof(crq));
1898 crq.login.first = IBMVNIC_CRQ_CMD;
1899 crq.login.cmd = LOGIN;
1900 crq.login.ioba = cpu_to_be32(buffer_token);
1901 crq.login.len = cpu_to_be32(buffer_size);
1902
1903 memcpy(&inflight_cmd->crq, &crq, sizeof(crq));
1904
1905 spin_lock_irqsave(&adapter->inflight_lock, flags);
1906 list_add_tail(&inflight_cmd->list, &adapter->inflight);
1907 spin_unlock_irqrestore(&adapter->inflight_lock, flags);
1908
1909 ibmvnic_send_crq(adapter, &crq);
1910
1911 return;
1912
1913inflight_alloc_failed:
1914 dma_unmap_single(dev, rsp_buffer_token, rsp_buffer_size,
1915 DMA_FROM_DEVICE);
1916buf_rsp_map_failed:
1917 kfree(login_rsp_buffer);
1918buf_rsp_alloc_failed:
1919 dma_unmap_single(dev, buffer_token, buffer_size, DMA_TO_DEVICE);
1920buf_map_failed:
1921 kfree(login_buffer);
1922buf_alloc_failed:
1923 return;
1924}
1925
1926static void send_request_map(struct ibmvnic_adapter *adapter, dma_addr_t addr,
1927 u32 len, u8 map_id)
1928{
1929 union ibmvnic_crq crq;
1930
1931 memset(&crq, 0, sizeof(crq));
1932 crq.request_map.first = IBMVNIC_CRQ_CMD;
1933 crq.request_map.cmd = REQUEST_MAP;
1934 crq.request_map.map_id = map_id;
1935 crq.request_map.ioba = cpu_to_be32(addr);
1936 crq.request_map.len = cpu_to_be32(len);
1937 ibmvnic_send_crq(adapter, &crq);
1938}
1939
1940static void send_request_unmap(struct ibmvnic_adapter *adapter, u8 map_id)
1941{
1942 union ibmvnic_crq crq;
1943
1944 memset(&crq, 0, sizeof(crq));
1945 crq.request_unmap.first = IBMVNIC_CRQ_CMD;
1946 crq.request_unmap.cmd = REQUEST_UNMAP;
1947 crq.request_unmap.map_id = map_id;
1948 ibmvnic_send_crq(adapter, &crq);
1949}
1950
1951static void send_map_query(struct ibmvnic_adapter *adapter)
1952{
1953 union ibmvnic_crq crq;
1954
1955 memset(&crq, 0, sizeof(crq));
1956 crq.query_map.first = IBMVNIC_CRQ_CMD;
1957 crq.query_map.cmd = QUERY_MAP;
1958 ibmvnic_send_crq(adapter, &crq);
1959}
1960
1961/* Send a series of CRQs requesting various capabilities of the VNIC server */
1962static void send_cap_queries(struct ibmvnic_adapter *adapter)
1963{
1964 union ibmvnic_crq crq;
1965
1966 atomic_set(&adapter->running_cap_queries, 0);
1967 memset(&crq, 0, sizeof(crq));
1968 crq.query_capability.first = IBMVNIC_CRQ_CMD;
1969 crq.query_capability.cmd = QUERY_CAPABILITY;
1970
1971 crq.query_capability.capability = cpu_to_be16(MIN_TX_QUEUES);
1972 atomic_inc(&adapter->running_cap_queries);
1973 ibmvnic_send_crq(adapter, &crq);
1974
1975 crq.query_capability.capability = cpu_to_be16(MIN_RX_QUEUES);
1976 atomic_inc(&adapter->running_cap_queries);
1977 ibmvnic_send_crq(adapter, &crq);
1978
1979 crq.query_capability.capability = cpu_to_be16(MIN_RX_ADD_QUEUES);
1980 atomic_inc(&adapter->running_cap_queries);
1981 ibmvnic_send_crq(adapter, &crq);
1982
1983 crq.query_capability.capability = cpu_to_be16(MAX_TX_QUEUES);
1984 atomic_inc(&adapter->running_cap_queries);
1985 ibmvnic_send_crq(adapter, &crq);
1986
1987 crq.query_capability.capability = cpu_to_be16(MAX_RX_QUEUES);
1988 atomic_inc(&adapter->running_cap_queries);
1989 ibmvnic_send_crq(adapter, &crq);
1990
1991 crq.query_capability.capability = cpu_to_be16(MAX_RX_ADD_QUEUES);
1992 atomic_inc(&adapter->running_cap_queries);
1993 ibmvnic_send_crq(adapter, &crq);
1994
1995 crq.query_capability.capability =
1996 cpu_to_be16(MIN_TX_ENTRIES_PER_SUBCRQ);
1997 atomic_inc(&adapter->running_cap_queries);
1998 ibmvnic_send_crq(adapter, &crq);
1999
2000 crq.query_capability.capability =
2001 cpu_to_be16(MIN_RX_ADD_ENTRIES_PER_SUBCRQ);
2002 atomic_inc(&adapter->running_cap_queries);
2003 ibmvnic_send_crq(adapter, &crq);
2004
2005 crq.query_capability.capability =
2006 cpu_to_be16(MAX_TX_ENTRIES_PER_SUBCRQ);
2007 atomic_inc(&adapter->running_cap_queries);
2008 ibmvnic_send_crq(adapter, &crq);
2009
2010 crq.query_capability.capability =
2011 cpu_to_be16(MAX_RX_ADD_ENTRIES_PER_SUBCRQ);
2012 atomic_inc(&adapter->running_cap_queries);
2013 ibmvnic_send_crq(adapter, &crq);
2014
2015 crq.query_capability.capability = cpu_to_be16(TCP_IP_OFFLOAD);
2016 atomic_inc(&adapter->running_cap_queries);
2017 ibmvnic_send_crq(adapter, &crq);
2018
2019 crq.query_capability.capability = cpu_to_be16(PROMISC_SUPPORTED);
2020 atomic_inc(&adapter->running_cap_queries);
2021 ibmvnic_send_crq(adapter, &crq);
2022
2023 crq.query_capability.capability = cpu_to_be16(MIN_MTU);
2024 atomic_inc(&adapter->running_cap_queries);
2025 ibmvnic_send_crq(adapter, &crq);
2026
2027 crq.query_capability.capability = cpu_to_be16(MAX_MTU);
2028 atomic_inc(&adapter->running_cap_queries);
2029 ibmvnic_send_crq(adapter, &crq);
2030
2031 crq.query_capability.capability = cpu_to_be16(MAX_MULTICAST_FILTERS);
2032 atomic_inc(&adapter->running_cap_queries);
2033 ibmvnic_send_crq(adapter, &crq);
2034
2035 crq.query_capability.capability = cpu_to_be16(VLAN_HEADER_INSERTION);
2036 atomic_inc(&adapter->running_cap_queries);
2037 ibmvnic_send_crq(adapter, &crq);
2038
2039 crq.query_capability.capability = cpu_to_be16(MAX_TX_SG_ENTRIES);
2040 atomic_inc(&adapter->running_cap_queries);
2041 ibmvnic_send_crq(adapter, &crq);
2042
2043 crq.query_capability.capability = cpu_to_be16(RX_SG_SUPPORTED);
2044 atomic_inc(&adapter->running_cap_queries);
2045 ibmvnic_send_crq(adapter, &crq);
2046
2047 crq.query_capability.capability = cpu_to_be16(OPT_TX_COMP_SUB_QUEUES);
2048 atomic_inc(&adapter->running_cap_queries);
2049 ibmvnic_send_crq(adapter, &crq);
2050
2051 crq.query_capability.capability = cpu_to_be16(OPT_RX_COMP_QUEUES);
2052 atomic_inc(&adapter->running_cap_queries);
2053 ibmvnic_send_crq(adapter, &crq);
2054
2055 crq.query_capability.capability =
2056 cpu_to_be16(OPT_RX_BUFADD_Q_PER_RX_COMP_Q);
2057 atomic_inc(&adapter->running_cap_queries);
2058 ibmvnic_send_crq(adapter, &crq);
2059
2060 crq.query_capability.capability =
2061 cpu_to_be16(OPT_TX_ENTRIES_PER_SUBCRQ);
2062 atomic_inc(&adapter->running_cap_queries);
2063 ibmvnic_send_crq(adapter, &crq);
2064
2065 crq.query_capability.capability =
2066 cpu_to_be16(OPT_RXBA_ENTRIES_PER_SUBCRQ);
2067 atomic_inc(&adapter->running_cap_queries);
2068 ibmvnic_send_crq(adapter, &crq);
2069
2070 crq.query_capability.capability = cpu_to_be16(TX_RX_DESC_REQ);
2071 atomic_inc(&adapter->running_cap_queries);
2072 ibmvnic_send_crq(adapter, &crq);
2073}
2074
2075static void handle_query_ip_offload_rsp(struct ibmvnic_adapter *adapter)
2076{
2077 struct device *dev = &adapter->vdev->dev;
2078 struct ibmvnic_query_ip_offload_buffer *buf = &adapter->ip_offload_buf;
2079 union ibmvnic_crq crq;
2080 int i;
2081
2082 dma_unmap_single(dev, adapter->ip_offload_tok,
2083 sizeof(adapter->ip_offload_buf), DMA_FROM_DEVICE);
2084
2085 netdev_dbg(adapter->netdev, "Query IP Offload Buffer:\n");
2086 for (i = 0; i < (sizeof(adapter->ip_offload_buf) - 1) / 8 + 1; i++)
2087 netdev_dbg(adapter->netdev, "%016lx\n",
2088 ((unsigned long int *)(buf))[i]);
2089
2090 netdev_dbg(adapter->netdev, "ipv4_chksum = %d\n", buf->ipv4_chksum);
2091 netdev_dbg(adapter->netdev, "ipv6_chksum = %d\n", buf->ipv6_chksum);
2092 netdev_dbg(adapter->netdev, "tcp_ipv4_chksum = %d\n",
2093 buf->tcp_ipv4_chksum);
2094 netdev_dbg(adapter->netdev, "tcp_ipv6_chksum = %d\n",
2095 buf->tcp_ipv6_chksum);
2096 netdev_dbg(adapter->netdev, "udp_ipv4_chksum = %d\n",
2097 buf->udp_ipv4_chksum);
2098 netdev_dbg(adapter->netdev, "udp_ipv6_chksum = %d\n",
2099 buf->udp_ipv6_chksum);
2100 netdev_dbg(adapter->netdev, "large_tx_ipv4 = %d\n",
2101 buf->large_tx_ipv4);
2102 netdev_dbg(adapter->netdev, "large_tx_ipv6 = %d\n",
2103 buf->large_tx_ipv6);
2104 netdev_dbg(adapter->netdev, "large_rx_ipv4 = %d\n",
2105 buf->large_rx_ipv4);
2106 netdev_dbg(adapter->netdev, "large_rx_ipv6 = %d\n",
2107 buf->large_rx_ipv6);
2108 netdev_dbg(adapter->netdev, "max_ipv4_hdr_sz = %d\n",
2109 buf->max_ipv4_header_size);
2110 netdev_dbg(adapter->netdev, "max_ipv6_hdr_sz = %d\n",
2111 buf->max_ipv6_header_size);
2112 netdev_dbg(adapter->netdev, "max_tcp_hdr_size = %d\n",
2113 buf->max_tcp_header_size);
2114 netdev_dbg(adapter->netdev, "max_udp_hdr_size = %d\n",
2115 buf->max_udp_header_size);
2116 netdev_dbg(adapter->netdev, "max_large_tx_size = %d\n",
2117 buf->max_large_tx_size);
2118 netdev_dbg(adapter->netdev, "max_large_rx_size = %d\n",
2119 buf->max_large_rx_size);
2120 netdev_dbg(adapter->netdev, "ipv6_ext_hdr = %d\n",
2121 buf->ipv6_extension_header);
2122 netdev_dbg(adapter->netdev, "tcp_pseudosum_req = %d\n",
2123 buf->tcp_pseudosum_req);
2124 netdev_dbg(adapter->netdev, "num_ipv6_ext_hd = %d\n",
2125 buf->num_ipv6_ext_headers);
2126 netdev_dbg(adapter->netdev, "off_ipv6_ext_hd = %d\n",
2127 buf->off_ipv6_ext_headers);
2128
2129 adapter->ip_offload_ctrl_tok =
2130 dma_map_single(dev, &adapter->ip_offload_ctrl,
2131 sizeof(adapter->ip_offload_ctrl), DMA_TO_DEVICE);
2132
2133 if (dma_mapping_error(dev, adapter->ip_offload_ctrl_tok)) {
2134 dev_err(dev, "Couldn't map ip offload control buffer\n");
2135 return;
2136 }
2137
2138 adapter->ip_offload_ctrl.version = cpu_to_be32(INITIAL_VERSION_IOB);
2139 adapter->ip_offload_ctrl.tcp_ipv4_chksum = buf->tcp_ipv4_chksum;
2140 adapter->ip_offload_ctrl.udp_ipv4_chksum = buf->udp_ipv4_chksum;
2141 adapter->ip_offload_ctrl.tcp_ipv6_chksum = buf->tcp_ipv6_chksum;
2142 adapter->ip_offload_ctrl.udp_ipv6_chksum = buf->udp_ipv6_chksum;
2143
2144 /* large_tx/rx disabled for now, additional features needed */
2145 adapter->ip_offload_ctrl.large_tx_ipv4 = 0;
2146 adapter->ip_offload_ctrl.large_tx_ipv6 = 0;
2147 adapter->ip_offload_ctrl.large_rx_ipv4 = 0;
2148 adapter->ip_offload_ctrl.large_rx_ipv6 = 0;
2149
2150 adapter->netdev->features = NETIF_F_GSO;
2151
2152 if (buf->tcp_ipv4_chksum || buf->udp_ipv4_chksum)
2153 adapter->netdev->features |= NETIF_F_IP_CSUM;
2154
2155 if (buf->tcp_ipv6_chksum || buf->udp_ipv6_chksum)
2156 adapter->netdev->features |= NETIF_F_IPV6_CSUM;
2157
Thomas Falcon9be02cd2016-04-01 17:20:35 -05002158 if ((adapter->netdev->features &
2159 (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)))
2160 adapter->netdev->features |= NETIF_F_RXCSUM;
2161
Thomas Falcon032c5e82015-12-21 11:26:06 -06002162 memset(&crq, 0, sizeof(crq));
2163 crq.control_ip_offload.first = IBMVNIC_CRQ_CMD;
2164 crq.control_ip_offload.cmd = CONTROL_IP_OFFLOAD;
2165 crq.control_ip_offload.len =
2166 cpu_to_be32(sizeof(adapter->ip_offload_ctrl));
2167 crq.control_ip_offload.ioba = cpu_to_be32(adapter->ip_offload_ctrl_tok);
2168 ibmvnic_send_crq(adapter, &crq);
2169}
2170
2171static void handle_error_info_rsp(union ibmvnic_crq *crq,
2172 struct ibmvnic_adapter *adapter)
2173{
2174 struct device *dev = &adapter->vdev->dev;
Wei Yongjun96183182016-06-27 20:48:53 +08002175 struct ibmvnic_error_buff *error_buff, *tmp;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002176 unsigned long flags;
2177 bool found = false;
2178 int i;
2179
2180 if (!crq->request_error_rsp.rc.code) {
2181 dev_info(dev, "Request Error Rsp returned with rc=%x\n",
2182 crq->request_error_rsp.rc.code);
2183 return;
2184 }
2185
2186 spin_lock_irqsave(&adapter->error_list_lock, flags);
Wei Yongjun96183182016-06-27 20:48:53 +08002187 list_for_each_entry_safe(error_buff, tmp, &adapter->errors, list)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002188 if (error_buff->error_id == crq->request_error_rsp.error_id) {
2189 found = true;
2190 list_del(&error_buff->list);
2191 break;
2192 }
2193 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
2194
2195 if (!found) {
2196 dev_err(dev, "Couldn't find error id %x\n",
2197 crq->request_error_rsp.error_id);
2198 return;
2199 }
2200
2201 dev_err(dev, "Detailed info for error id %x:",
2202 crq->request_error_rsp.error_id);
2203
2204 for (i = 0; i < error_buff->len; i++) {
2205 pr_cont("%02x", (int)error_buff->buff[i]);
2206 if (i % 8 == 7)
2207 pr_cont(" ");
2208 }
2209 pr_cont("\n");
2210
2211 dma_unmap_single(dev, error_buff->dma, error_buff->len,
2212 DMA_FROM_DEVICE);
2213 kfree(error_buff->buff);
2214 kfree(error_buff);
2215}
2216
2217static void handle_dump_size_rsp(union ibmvnic_crq *crq,
2218 struct ibmvnic_adapter *adapter)
2219{
2220 int len = be32_to_cpu(crq->request_dump_size_rsp.len);
2221 struct ibmvnic_inflight_cmd *inflight_cmd;
2222 struct device *dev = &adapter->vdev->dev;
2223 union ibmvnic_crq newcrq;
2224 unsigned long flags;
2225
2226 /* allocate and map buffer */
2227 adapter->dump_data = kmalloc(len, GFP_KERNEL);
2228 if (!adapter->dump_data) {
2229 complete(&adapter->fw_done);
2230 return;
2231 }
2232
2233 adapter->dump_data_token = dma_map_single(dev, adapter->dump_data, len,
2234 DMA_FROM_DEVICE);
2235
2236 if (dma_mapping_error(dev, adapter->dump_data_token)) {
2237 if (!firmware_has_feature(FW_FEATURE_CMO))
2238 dev_err(dev, "Couldn't map dump data\n");
2239 kfree(adapter->dump_data);
2240 complete(&adapter->fw_done);
2241 return;
2242 }
2243
2244 inflight_cmd = kmalloc(sizeof(*inflight_cmd), GFP_ATOMIC);
2245 if (!inflight_cmd) {
2246 dma_unmap_single(dev, adapter->dump_data_token, len,
2247 DMA_FROM_DEVICE);
2248 kfree(adapter->dump_data);
2249 complete(&adapter->fw_done);
2250 return;
2251 }
2252
2253 memset(&newcrq, 0, sizeof(newcrq));
2254 newcrq.request_dump.first = IBMVNIC_CRQ_CMD;
2255 newcrq.request_dump.cmd = REQUEST_DUMP;
2256 newcrq.request_dump.ioba = cpu_to_be32(adapter->dump_data_token);
2257 newcrq.request_dump.len = cpu_to_be32(adapter->dump_data_size);
2258
2259 memcpy(&inflight_cmd->crq, &newcrq, sizeof(newcrq));
2260
2261 spin_lock_irqsave(&adapter->inflight_lock, flags);
2262 list_add_tail(&inflight_cmd->list, &adapter->inflight);
2263 spin_unlock_irqrestore(&adapter->inflight_lock, flags);
2264
2265 ibmvnic_send_crq(adapter, &newcrq);
2266}
2267
2268static void handle_error_indication(union ibmvnic_crq *crq,
2269 struct ibmvnic_adapter *adapter)
2270{
2271 int detail_len = be32_to_cpu(crq->error_indication.detail_error_sz);
2272 struct ibmvnic_inflight_cmd *inflight_cmd;
2273 struct device *dev = &adapter->vdev->dev;
2274 struct ibmvnic_error_buff *error_buff;
2275 union ibmvnic_crq new_crq;
2276 unsigned long flags;
2277
2278 dev_err(dev, "Firmware reports %serror id %x, cause %d\n",
2279 crq->error_indication.
2280 flags & IBMVNIC_FATAL_ERROR ? "FATAL " : "",
2281 crq->error_indication.error_id,
2282 crq->error_indication.error_cause);
2283
2284 error_buff = kmalloc(sizeof(*error_buff), GFP_ATOMIC);
2285 if (!error_buff)
2286 return;
2287
2288 error_buff->buff = kmalloc(detail_len, GFP_ATOMIC);
2289 if (!error_buff->buff) {
2290 kfree(error_buff);
2291 return;
2292 }
2293
2294 error_buff->dma = dma_map_single(dev, error_buff->buff, detail_len,
2295 DMA_FROM_DEVICE);
2296 if (dma_mapping_error(dev, error_buff->dma)) {
2297 if (!firmware_has_feature(FW_FEATURE_CMO))
2298 dev_err(dev, "Couldn't map error buffer\n");
2299 kfree(error_buff->buff);
2300 kfree(error_buff);
2301 return;
2302 }
2303
2304 inflight_cmd = kmalloc(sizeof(*inflight_cmd), GFP_ATOMIC);
2305 if (!inflight_cmd) {
2306 dma_unmap_single(dev, error_buff->dma, detail_len,
2307 DMA_FROM_DEVICE);
2308 kfree(error_buff->buff);
2309 kfree(error_buff);
2310 return;
2311 }
2312
2313 error_buff->len = detail_len;
2314 error_buff->error_id = crq->error_indication.error_id;
2315
2316 spin_lock_irqsave(&adapter->error_list_lock, flags);
2317 list_add_tail(&error_buff->list, &adapter->errors);
2318 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
2319
2320 memset(&new_crq, 0, sizeof(new_crq));
2321 new_crq.request_error_info.first = IBMVNIC_CRQ_CMD;
2322 new_crq.request_error_info.cmd = REQUEST_ERROR_INFO;
2323 new_crq.request_error_info.ioba = cpu_to_be32(error_buff->dma);
2324 new_crq.request_error_info.len = cpu_to_be32(detail_len);
2325 new_crq.request_error_info.error_id = crq->error_indication.error_id;
2326
2327 memcpy(&inflight_cmd->crq, &crq, sizeof(crq));
2328
2329 spin_lock_irqsave(&adapter->inflight_lock, flags);
2330 list_add_tail(&inflight_cmd->list, &adapter->inflight);
2331 spin_unlock_irqrestore(&adapter->inflight_lock, flags);
2332
2333 ibmvnic_send_crq(adapter, &new_crq);
2334}
2335
2336static void handle_change_mac_rsp(union ibmvnic_crq *crq,
2337 struct ibmvnic_adapter *adapter)
2338{
2339 struct net_device *netdev = adapter->netdev;
2340 struct device *dev = &adapter->vdev->dev;
2341 long rc;
2342
2343 rc = crq->change_mac_addr_rsp.rc.code;
2344 if (rc) {
2345 dev_err(dev, "Error %ld in CHANGE_MAC_ADDR_RSP\n", rc);
2346 return;
2347 }
2348 memcpy(netdev->dev_addr, &crq->change_mac_addr_rsp.mac_addr[0],
2349 ETH_ALEN);
2350}
2351
2352static void handle_request_cap_rsp(union ibmvnic_crq *crq,
2353 struct ibmvnic_adapter *adapter)
2354{
2355 struct device *dev = &adapter->vdev->dev;
2356 u64 *req_value;
2357 char *name;
2358
2359 switch (be16_to_cpu(crq->request_capability_rsp.capability)) {
2360 case REQ_TX_QUEUES:
2361 req_value = &adapter->req_tx_queues;
2362 name = "tx";
2363 break;
2364 case REQ_RX_QUEUES:
2365 req_value = &adapter->req_rx_queues;
2366 name = "rx";
2367 break;
2368 case REQ_RX_ADD_QUEUES:
2369 req_value = &adapter->req_rx_add_queues;
2370 name = "rx_add";
2371 break;
2372 case REQ_TX_ENTRIES_PER_SUBCRQ:
2373 req_value = &adapter->req_tx_entries_per_subcrq;
2374 name = "tx_entries_per_subcrq";
2375 break;
2376 case REQ_RX_ADD_ENTRIES_PER_SUBCRQ:
2377 req_value = &adapter->req_rx_add_entries_per_subcrq;
2378 name = "rx_add_entries_per_subcrq";
2379 break;
2380 case REQ_MTU:
2381 req_value = &adapter->req_mtu;
2382 name = "mtu";
2383 break;
2384 case PROMISC_REQUESTED:
2385 req_value = &adapter->promisc;
2386 name = "promisc";
2387 break;
2388 default:
2389 dev_err(dev, "Got invalid cap request rsp %d\n",
2390 crq->request_capability.capability);
2391 return;
2392 }
2393
2394 switch (crq->request_capability_rsp.rc.code) {
2395 case SUCCESS:
2396 break;
2397 case PARTIALSUCCESS:
2398 dev_info(dev, "req=%lld, rsp=%ld in %s queue, retrying.\n",
2399 *req_value,
2400 (long int)be32_to_cpu(crq->request_capability_rsp.
2401 number), name);
Thomas Falconea22d512016-07-06 15:35:17 -05002402 release_sub_crqs_no_irqs(adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002403 *req_value = be32_to_cpu(crq->request_capability_rsp.number);
Thomas Falconea22d512016-07-06 15:35:17 -05002404 init_sub_crqs(adapter, 1);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002405 return;
2406 default:
2407 dev_err(dev, "Error %d in request cap rsp\n",
2408 crq->request_capability_rsp.rc.code);
2409 return;
2410 }
2411
2412 /* Done receiving requested capabilities, query IP offload support */
2413 if (++adapter->requested_caps == 7) {
2414 union ibmvnic_crq newcrq;
2415 int buf_sz = sizeof(struct ibmvnic_query_ip_offload_buffer);
2416 struct ibmvnic_query_ip_offload_buffer *ip_offload_buf =
2417 &adapter->ip_offload_buf;
2418
2419 adapter->ip_offload_tok = dma_map_single(dev, ip_offload_buf,
2420 buf_sz,
2421 DMA_FROM_DEVICE);
2422
2423 if (dma_mapping_error(dev, adapter->ip_offload_tok)) {
2424 if (!firmware_has_feature(FW_FEATURE_CMO))
2425 dev_err(dev, "Couldn't map offload buffer\n");
2426 return;
2427 }
2428
2429 memset(&newcrq, 0, sizeof(newcrq));
2430 newcrq.query_ip_offload.first = IBMVNIC_CRQ_CMD;
2431 newcrq.query_ip_offload.cmd = QUERY_IP_OFFLOAD;
2432 newcrq.query_ip_offload.len = cpu_to_be32(buf_sz);
2433 newcrq.query_ip_offload.ioba =
2434 cpu_to_be32(adapter->ip_offload_tok);
2435
2436 ibmvnic_send_crq(adapter, &newcrq);
2437 }
2438}
2439
2440static int handle_login_rsp(union ibmvnic_crq *login_rsp_crq,
2441 struct ibmvnic_adapter *adapter)
2442{
2443 struct device *dev = &adapter->vdev->dev;
2444 struct ibmvnic_login_rsp_buffer *login_rsp = adapter->login_rsp_buf;
2445 struct ibmvnic_login_buffer *login = adapter->login_buf;
2446 union ibmvnic_crq crq;
2447 int i;
2448
2449 dma_unmap_single(dev, adapter->login_buf_token, adapter->login_buf_sz,
2450 DMA_BIDIRECTIONAL);
2451 dma_unmap_single(dev, adapter->login_rsp_buf_token,
2452 adapter->login_rsp_buf_sz, DMA_BIDIRECTIONAL);
2453
John Allen498cd8e2016-04-06 11:49:55 -05002454 /* If the number of queues requested can't be allocated by the
2455 * server, the login response will return with code 1. We will need
2456 * to resend the login buffer with fewer queues requested.
2457 */
2458 if (login_rsp_crq->generic.rc.code) {
2459 adapter->renegotiate = true;
2460 complete(&adapter->init_done);
2461 return 0;
2462 }
2463
Thomas Falcon032c5e82015-12-21 11:26:06 -06002464 netdev_dbg(adapter->netdev, "Login Response Buffer:\n");
2465 for (i = 0; i < (adapter->login_rsp_buf_sz - 1) / 8 + 1; i++) {
2466 netdev_dbg(adapter->netdev, "%016lx\n",
2467 ((unsigned long int *)(adapter->login_rsp_buf))[i]);
2468 }
2469
2470 /* Sanity checks */
2471 if (login->num_txcomp_subcrqs != login_rsp->num_txsubm_subcrqs ||
2472 (be32_to_cpu(login->num_rxcomp_subcrqs) *
2473 adapter->req_rx_add_queues !=
2474 be32_to_cpu(login_rsp->num_rxadd_subcrqs))) {
2475 dev_err(dev, "FATAL: Inconsistent login and login rsp\n");
2476 ibmvnic_remove(adapter->vdev);
2477 return -EIO;
2478 }
2479 complete(&adapter->init_done);
2480
2481 memset(&crq, 0, sizeof(crq));
2482 crq.request_ras_comp_num.first = IBMVNIC_CRQ_CMD;
2483 crq.request_ras_comp_num.cmd = REQUEST_RAS_COMP_NUM;
2484 ibmvnic_send_crq(adapter, &crq);
2485
2486 return 0;
2487}
2488
2489static void handle_request_map_rsp(union ibmvnic_crq *crq,
2490 struct ibmvnic_adapter *adapter)
2491{
2492 struct device *dev = &adapter->vdev->dev;
2493 u8 map_id = crq->request_map_rsp.map_id;
2494 int tx_subcrqs;
2495 int rx_subcrqs;
2496 long rc;
2497 int i;
2498
2499 tx_subcrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
2500 rx_subcrqs = be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
2501
2502 rc = crq->request_map_rsp.rc.code;
2503 if (rc) {
2504 dev_err(dev, "Error %ld in REQUEST_MAP_RSP\n", rc);
2505 adapter->map_id--;
2506 /* need to find and zero tx/rx_pool map_id */
2507 for (i = 0; i < tx_subcrqs; i++) {
2508 if (adapter->tx_pool[i].long_term_buff.map_id == map_id)
2509 adapter->tx_pool[i].long_term_buff.map_id = 0;
2510 }
2511 for (i = 0; i < rx_subcrqs; i++) {
2512 if (adapter->rx_pool[i].long_term_buff.map_id == map_id)
2513 adapter->rx_pool[i].long_term_buff.map_id = 0;
2514 }
2515 }
2516 complete(&adapter->fw_done);
2517}
2518
2519static void handle_request_unmap_rsp(union ibmvnic_crq *crq,
2520 struct ibmvnic_adapter *adapter)
2521{
2522 struct device *dev = &adapter->vdev->dev;
2523 long rc;
2524
2525 rc = crq->request_unmap_rsp.rc.code;
2526 if (rc)
2527 dev_err(dev, "Error %ld in REQUEST_UNMAP_RSP\n", rc);
2528}
2529
2530static void handle_query_map_rsp(union ibmvnic_crq *crq,
2531 struct ibmvnic_adapter *adapter)
2532{
2533 struct net_device *netdev = adapter->netdev;
2534 struct device *dev = &adapter->vdev->dev;
2535 long rc;
2536
2537 rc = crq->query_map_rsp.rc.code;
2538 if (rc) {
2539 dev_err(dev, "Error %ld in QUERY_MAP_RSP\n", rc);
2540 return;
2541 }
2542 netdev_dbg(netdev, "page_size = %d\ntot_pages = %d\nfree_pages = %d\n",
2543 crq->query_map_rsp.page_size, crq->query_map_rsp.tot_pages,
2544 crq->query_map_rsp.free_pages);
2545}
2546
2547static void handle_query_cap_rsp(union ibmvnic_crq *crq,
2548 struct ibmvnic_adapter *adapter)
2549{
2550 struct net_device *netdev = adapter->netdev;
2551 struct device *dev = &adapter->vdev->dev;
2552 long rc;
2553
2554 atomic_dec(&adapter->running_cap_queries);
2555 netdev_dbg(netdev, "Outstanding queries: %d\n",
2556 atomic_read(&adapter->running_cap_queries));
2557 rc = crq->query_capability.rc.code;
2558 if (rc) {
2559 dev_err(dev, "Error %ld in QUERY_CAP_RSP\n", rc);
2560 goto out;
2561 }
2562
2563 switch (be16_to_cpu(crq->query_capability.capability)) {
2564 case MIN_TX_QUEUES:
2565 adapter->min_tx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06002566 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002567 netdev_dbg(netdev, "min_tx_queues = %lld\n",
2568 adapter->min_tx_queues);
2569 break;
2570 case MIN_RX_QUEUES:
2571 adapter->min_rx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06002572 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002573 netdev_dbg(netdev, "min_rx_queues = %lld\n",
2574 adapter->min_rx_queues);
2575 break;
2576 case MIN_RX_ADD_QUEUES:
2577 adapter->min_rx_add_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06002578 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002579 netdev_dbg(netdev, "min_rx_add_queues = %lld\n",
2580 adapter->min_rx_add_queues);
2581 break;
2582 case MAX_TX_QUEUES:
2583 adapter->max_tx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06002584 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002585 netdev_dbg(netdev, "max_tx_queues = %lld\n",
2586 adapter->max_tx_queues);
2587 break;
2588 case MAX_RX_QUEUES:
2589 adapter->max_rx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06002590 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002591 netdev_dbg(netdev, "max_rx_queues = %lld\n",
2592 adapter->max_rx_queues);
2593 break;
2594 case MAX_RX_ADD_QUEUES:
2595 adapter->max_rx_add_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06002596 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002597 netdev_dbg(netdev, "max_rx_add_queues = %lld\n",
2598 adapter->max_rx_add_queues);
2599 break;
2600 case MIN_TX_ENTRIES_PER_SUBCRQ:
2601 adapter->min_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06002602 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002603 netdev_dbg(netdev, "min_tx_entries_per_subcrq = %lld\n",
2604 adapter->min_tx_entries_per_subcrq);
2605 break;
2606 case MIN_RX_ADD_ENTRIES_PER_SUBCRQ:
2607 adapter->min_rx_add_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06002608 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002609 netdev_dbg(netdev, "min_rx_add_entrs_per_subcrq = %lld\n",
2610 adapter->min_rx_add_entries_per_subcrq);
2611 break;
2612 case MAX_TX_ENTRIES_PER_SUBCRQ:
2613 adapter->max_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06002614 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002615 netdev_dbg(netdev, "max_tx_entries_per_subcrq = %lld\n",
2616 adapter->max_tx_entries_per_subcrq);
2617 break;
2618 case MAX_RX_ADD_ENTRIES_PER_SUBCRQ:
2619 adapter->max_rx_add_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06002620 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002621 netdev_dbg(netdev, "max_rx_add_entrs_per_subcrq = %lld\n",
2622 adapter->max_rx_add_entries_per_subcrq);
2623 break;
2624 case TCP_IP_OFFLOAD:
2625 adapter->tcp_ip_offload =
Thomas Falconde89e852016-03-01 10:20:09 -06002626 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002627 netdev_dbg(netdev, "tcp_ip_offload = %lld\n",
2628 adapter->tcp_ip_offload);
2629 break;
2630 case PROMISC_SUPPORTED:
2631 adapter->promisc_supported =
Thomas Falconde89e852016-03-01 10:20:09 -06002632 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002633 netdev_dbg(netdev, "promisc_supported = %lld\n",
2634 adapter->promisc_supported);
2635 break;
2636 case MIN_MTU:
Thomas Falconde89e852016-03-01 10:20:09 -06002637 adapter->min_mtu = be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002638 netdev_dbg(netdev, "min_mtu = %lld\n", adapter->min_mtu);
2639 break;
2640 case MAX_MTU:
Thomas Falconde89e852016-03-01 10:20:09 -06002641 adapter->max_mtu = be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002642 netdev_dbg(netdev, "max_mtu = %lld\n", adapter->max_mtu);
2643 break;
2644 case MAX_MULTICAST_FILTERS:
2645 adapter->max_multicast_filters =
Thomas Falconde89e852016-03-01 10:20:09 -06002646 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002647 netdev_dbg(netdev, "max_multicast_filters = %lld\n",
2648 adapter->max_multicast_filters);
2649 break;
2650 case VLAN_HEADER_INSERTION:
2651 adapter->vlan_header_insertion =
Thomas Falconde89e852016-03-01 10:20:09 -06002652 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002653 if (adapter->vlan_header_insertion)
2654 netdev->features |= NETIF_F_HW_VLAN_STAG_TX;
2655 netdev_dbg(netdev, "vlan_header_insertion = %lld\n",
2656 adapter->vlan_header_insertion);
2657 break;
2658 case MAX_TX_SG_ENTRIES:
2659 adapter->max_tx_sg_entries =
Thomas Falconde89e852016-03-01 10:20:09 -06002660 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002661 netdev_dbg(netdev, "max_tx_sg_entries = %lld\n",
2662 adapter->max_tx_sg_entries);
2663 break;
2664 case RX_SG_SUPPORTED:
2665 adapter->rx_sg_supported =
Thomas Falconde89e852016-03-01 10:20:09 -06002666 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002667 netdev_dbg(netdev, "rx_sg_supported = %lld\n",
2668 adapter->rx_sg_supported);
2669 break;
2670 case OPT_TX_COMP_SUB_QUEUES:
2671 adapter->opt_tx_comp_sub_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06002672 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002673 netdev_dbg(netdev, "opt_tx_comp_sub_queues = %lld\n",
2674 adapter->opt_tx_comp_sub_queues);
2675 break;
2676 case OPT_RX_COMP_QUEUES:
2677 adapter->opt_rx_comp_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06002678 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002679 netdev_dbg(netdev, "opt_rx_comp_queues = %lld\n",
2680 adapter->opt_rx_comp_queues);
2681 break;
2682 case OPT_RX_BUFADD_Q_PER_RX_COMP_Q:
2683 adapter->opt_rx_bufadd_q_per_rx_comp_q =
Thomas Falconde89e852016-03-01 10:20:09 -06002684 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002685 netdev_dbg(netdev, "opt_rx_bufadd_q_per_rx_comp_q = %lld\n",
2686 adapter->opt_rx_bufadd_q_per_rx_comp_q);
2687 break;
2688 case OPT_TX_ENTRIES_PER_SUBCRQ:
2689 adapter->opt_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06002690 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002691 netdev_dbg(netdev, "opt_tx_entries_per_subcrq = %lld\n",
2692 adapter->opt_tx_entries_per_subcrq);
2693 break;
2694 case OPT_RXBA_ENTRIES_PER_SUBCRQ:
2695 adapter->opt_rxba_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06002696 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002697 netdev_dbg(netdev, "opt_rxba_entries_per_subcrq = %lld\n",
2698 adapter->opt_rxba_entries_per_subcrq);
2699 break;
2700 case TX_RX_DESC_REQ:
2701 adapter->tx_rx_desc_req = crq->query_capability.number;
2702 netdev_dbg(netdev, "tx_rx_desc_req = %llx\n",
2703 adapter->tx_rx_desc_req);
2704 break;
2705
2706 default:
2707 netdev_err(netdev, "Got invalid cap rsp %d\n",
2708 crq->query_capability.capability);
2709 }
2710
2711out:
2712 if (atomic_read(&adapter->running_cap_queries) == 0)
Thomas Falconea22d512016-07-06 15:35:17 -05002713 init_sub_crqs(adapter, 0);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002714 /* We're done querying the capabilities, initialize sub-crqs */
2715}
2716
2717static void handle_control_ras_rsp(union ibmvnic_crq *crq,
2718 struct ibmvnic_adapter *adapter)
2719{
2720 u8 correlator = crq->control_ras_rsp.correlator;
2721 struct device *dev = &adapter->vdev->dev;
2722 bool found = false;
2723 int i;
2724
2725 if (crq->control_ras_rsp.rc.code) {
2726 dev_warn(dev, "Control ras failed rc=%d\n",
2727 crq->control_ras_rsp.rc.code);
2728 return;
2729 }
2730
2731 for (i = 0; i < adapter->ras_comp_num; i++) {
2732 if (adapter->ras_comps[i].correlator == correlator) {
2733 found = true;
2734 break;
2735 }
2736 }
2737
2738 if (!found) {
2739 dev_warn(dev, "Correlator not found on control_ras_rsp\n");
2740 return;
2741 }
2742
2743 switch (crq->control_ras_rsp.op) {
2744 case IBMVNIC_TRACE_LEVEL:
2745 adapter->ras_comps[i].trace_level = crq->control_ras.level;
2746 break;
2747 case IBMVNIC_ERROR_LEVEL:
2748 adapter->ras_comps[i].error_check_level =
2749 crq->control_ras.level;
2750 break;
2751 case IBMVNIC_TRACE_PAUSE:
2752 adapter->ras_comp_int[i].paused = 1;
2753 break;
2754 case IBMVNIC_TRACE_RESUME:
2755 adapter->ras_comp_int[i].paused = 0;
2756 break;
2757 case IBMVNIC_TRACE_ON:
2758 adapter->ras_comps[i].trace_on = 1;
2759 break;
2760 case IBMVNIC_TRACE_OFF:
2761 adapter->ras_comps[i].trace_on = 0;
2762 break;
2763 case IBMVNIC_CHG_TRACE_BUFF_SZ:
2764 /* trace_buff_sz is 3 bytes, stuff it into an int */
2765 ((u8 *)(&adapter->ras_comps[i].trace_buff_size))[0] = 0;
2766 ((u8 *)(&adapter->ras_comps[i].trace_buff_size))[1] =
2767 crq->control_ras_rsp.trace_buff_sz[0];
2768 ((u8 *)(&adapter->ras_comps[i].trace_buff_size))[2] =
2769 crq->control_ras_rsp.trace_buff_sz[1];
2770 ((u8 *)(&adapter->ras_comps[i].trace_buff_size))[3] =
2771 crq->control_ras_rsp.trace_buff_sz[2];
2772 break;
2773 default:
2774 dev_err(dev, "invalid op %d on control_ras_rsp",
2775 crq->control_ras_rsp.op);
2776 }
2777}
2778
2779static int ibmvnic_fw_comp_open(struct inode *inode, struct file *file)
2780{
2781 file->private_data = inode->i_private;
2782 return 0;
2783}
2784
2785static ssize_t trace_read(struct file *file, char __user *user_buf, size_t len,
2786 loff_t *ppos)
2787{
2788 struct ibmvnic_fw_comp_internal *ras_comp_int = file->private_data;
2789 struct ibmvnic_adapter *adapter = ras_comp_int->adapter;
2790 struct device *dev = &adapter->vdev->dev;
2791 struct ibmvnic_fw_trace_entry *trace;
2792 int num = ras_comp_int->num;
2793 union ibmvnic_crq crq;
2794 dma_addr_t trace_tok;
2795
2796 if (*ppos >= be32_to_cpu(adapter->ras_comps[num].trace_buff_size))
2797 return 0;
2798
2799 trace =
2800 dma_alloc_coherent(dev,
2801 be32_to_cpu(adapter->ras_comps[num].
2802 trace_buff_size), &trace_tok,
2803 GFP_KERNEL);
2804 if (!trace) {
2805 dev_err(dev, "Couldn't alloc trace buffer\n");
2806 return 0;
2807 }
2808
2809 memset(&crq, 0, sizeof(crq));
2810 crq.collect_fw_trace.first = IBMVNIC_CRQ_CMD;
2811 crq.collect_fw_trace.cmd = COLLECT_FW_TRACE;
2812 crq.collect_fw_trace.correlator = adapter->ras_comps[num].correlator;
2813 crq.collect_fw_trace.ioba = cpu_to_be32(trace_tok);
2814 crq.collect_fw_trace.len = adapter->ras_comps[num].trace_buff_size;
2815 ibmvnic_send_crq(adapter, &crq);
2816
2817 init_completion(&adapter->fw_done);
2818 wait_for_completion(&adapter->fw_done);
2819
2820 if (*ppos + len > be32_to_cpu(adapter->ras_comps[num].trace_buff_size))
2821 len =
2822 be32_to_cpu(adapter->ras_comps[num].trace_buff_size) -
2823 *ppos;
2824
2825 copy_to_user(user_buf, &((u8 *)trace)[*ppos], len);
2826
2827 dma_free_coherent(dev,
2828 be32_to_cpu(adapter->ras_comps[num].trace_buff_size),
2829 trace, trace_tok);
2830 *ppos += len;
2831 return len;
2832}
2833
2834static const struct file_operations trace_ops = {
2835 .owner = THIS_MODULE,
2836 .open = ibmvnic_fw_comp_open,
2837 .read = trace_read,
2838};
2839
2840static ssize_t paused_read(struct file *file, char __user *user_buf, size_t len,
2841 loff_t *ppos)
2842{
2843 struct ibmvnic_fw_comp_internal *ras_comp_int = file->private_data;
2844 struct ibmvnic_adapter *adapter = ras_comp_int->adapter;
2845 int num = ras_comp_int->num;
2846 char buff[5]; /* 1 or 0 plus \n and \0 */
2847 int size;
2848
2849 size = sprintf(buff, "%d\n", adapter->ras_comp_int[num].paused);
2850
2851 if (*ppos >= size)
2852 return 0;
2853
2854 copy_to_user(user_buf, buff, size);
2855 *ppos += size;
2856 return size;
2857}
2858
2859static ssize_t paused_write(struct file *file, const char __user *user_buf,
2860 size_t len, loff_t *ppos)
2861{
2862 struct ibmvnic_fw_comp_internal *ras_comp_int = file->private_data;
2863 struct ibmvnic_adapter *adapter = ras_comp_int->adapter;
2864 int num = ras_comp_int->num;
2865 union ibmvnic_crq crq;
2866 unsigned long val;
2867 char buff[9]; /* decimal max int plus \n and \0 */
2868
2869 copy_from_user(buff, user_buf, sizeof(buff));
2870 val = kstrtoul(buff, 10, NULL);
2871
2872 adapter->ras_comp_int[num].paused = val ? 1 : 0;
2873
2874 memset(&crq, 0, sizeof(crq));
2875 crq.control_ras.first = IBMVNIC_CRQ_CMD;
2876 crq.control_ras.cmd = CONTROL_RAS;
2877 crq.control_ras.correlator = adapter->ras_comps[num].correlator;
2878 crq.control_ras.op = val ? IBMVNIC_TRACE_PAUSE : IBMVNIC_TRACE_RESUME;
2879 ibmvnic_send_crq(adapter, &crq);
2880
2881 return len;
2882}
2883
2884static const struct file_operations paused_ops = {
2885 .owner = THIS_MODULE,
2886 .open = ibmvnic_fw_comp_open,
2887 .read = paused_read,
2888 .write = paused_write,
2889};
2890
2891static ssize_t tracing_read(struct file *file, char __user *user_buf,
2892 size_t len, loff_t *ppos)
2893{
2894 struct ibmvnic_fw_comp_internal *ras_comp_int = file->private_data;
2895 struct ibmvnic_adapter *adapter = ras_comp_int->adapter;
2896 int num = ras_comp_int->num;
2897 char buff[5]; /* 1 or 0 plus \n and \0 */
2898 int size;
2899
2900 size = sprintf(buff, "%d\n", adapter->ras_comps[num].trace_on);
2901
2902 if (*ppos >= size)
2903 return 0;
2904
2905 copy_to_user(user_buf, buff, size);
2906 *ppos += size;
2907 return size;
2908}
2909
2910static ssize_t tracing_write(struct file *file, const char __user *user_buf,
2911 size_t len, loff_t *ppos)
2912{
2913 struct ibmvnic_fw_comp_internal *ras_comp_int = file->private_data;
2914 struct ibmvnic_adapter *adapter = ras_comp_int->adapter;
2915 int num = ras_comp_int->num;
2916 union ibmvnic_crq crq;
2917 unsigned long val;
2918 char buff[9]; /* decimal max int plus \n and \0 */
2919
2920 copy_from_user(buff, user_buf, sizeof(buff));
2921 val = kstrtoul(buff, 10, NULL);
2922
2923 memset(&crq, 0, sizeof(crq));
2924 crq.control_ras.first = IBMVNIC_CRQ_CMD;
2925 crq.control_ras.cmd = CONTROL_RAS;
2926 crq.control_ras.correlator = adapter->ras_comps[num].correlator;
2927 crq.control_ras.op = val ? IBMVNIC_TRACE_ON : IBMVNIC_TRACE_OFF;
2928
2929 return len;
2930}
2931
2932static const struct file_operations tracing_ops = {
2933 .owner = THIS_MODULE,
2934 .open = ibmvnic_fw_comp_open,
2935 .read = tracing_read,
2936 .write = tracing_write,
2937};
2938
2939static ssize_t error_level_read(struct file *file, char __user *user_buf,
2940 size_t len, loff_t *ppos)
2941{
2942 struct ibmvnic_fw_comp_internal *ras_comp_int = file->private_data;
2943 struct ibmvnic_adapter *adapter = ras_comp_int->adapter;
2944 int num = ras_comp_int->num;
2945 char buff[5]; /* decimal max char plus \n and \0 */
2946 int size;
2947
2948 size = sprintf(buff, "%d\n", adapter->ras_comps[num].error_check_level);
2949
2950 if (*ppos >= size)
2951 return 0;
2952
2953 copy_to_user(user_buf, buff, size);
2954 *ppos += size;
2955 return size;
2956}
2957
2958static ssize_t error_level_write(struct file *file, const char __user *user_buf,
2959 size_t len, loff_t *ppos)
2960{
2961 struct ibmvnic_fw_comp_internal *ras_comp_int = file->private_data;
2962 struct ibmvnic_adapter *adapter = ras_comp_int->adapter;
2963 int num = ras_comp_int->num;
2964 union ibmvnic_crq crq;
2965 unsigned long val;
2966 char buff[9]; /* decimal max int plus \n and \0 */
2967
2968 copy_from_user(buff, user_buf, sizeof(buff));
2969 val = kstrtoul(buff, 10, NULL);
2970
2971 if (val > 9)
2972 val = 9;
2973
2974 memset(&crq, 0, sizeof(crq));
2975 crq.control_ras.first = IBMVNIC_CRQ_CMD;
2976 crq.control_ras.cmd = CONTROL_RAS;
2977 crq.control_ras.correlator = adapter->ras_comps[num].correlator;
2978 crq.control_ras.op = IBMVNIC_ERROR_LEVEL;
2979 crq.control_ras.level = val;
2980 ibmvnic_send_crq(adapter, &crq);
2981
2982 return len;
2983}
2984
2985static const struct file_operations error_level_ops = {
2986 .owner = THIS_MODULE,
2987 .open = ibmvnic_fw_comp_open,
2988 .read = error_level_read,
2989 .write = error_level_write,
2990};
2991
2992static ssize_t trace_level_read(struct file *file, char __user *user_buf,
2993 size_t len, loff_t *ppos)
2994{
2995 struct ibmvnic_fw_comp_internal *ras_comp_int = file->private_data;
2996 struct ibmvnic_adapter *adapter = ras_comp_int->adapter;
2997 int num = ras_comp_int->num;
2998 char buff[5]; /* decimal max char plus \n and \0 */
2999 int size;
3000
3001 size = sprintf(buff, "%d\n", adapter->ras_comps[num].trace_level);
3002 if (*ppos >= size)
3003 return 0;
3004
3005 copy_to_user(user_buf, buff, size);
3006 *ppos += size;
3007 return size;
3008}
3009
3010static ssize_t trace_level_write(struct file *file, const char __user *user_buf,
3011 size_t len, loff_t *ppos)
3012{
3013 struct ibmvnic_fw_comp_internal *ras_comp_int = file->private_data;
3014 struct ibmvnic_adapter *adapter = ras_comp_int->adapter;
3015 union ibmvnic_crq crq;
3016 unsigned long val;
3017 char buff[9]; /* decimal max int plus \n and \0 */
3018
3019 copy_from_user(buff, user_buf, sizeof(buff));
3020 val = kstrtoul(buff, 10, NULL);
3021 if (val > 9)
3022 val = 9;
3023
3024 memset(&crq, 0, sizeof(crq));
3025 crq.control_ras.first = IBMVNIC_CRQ_CMD;
3026 crq.control_ras.cmd = CONTROL_RAS;
3027 crq.control_ras.correlator =
3028 adapter->ras_comps[ras_comp_int->num].correlator;
3029 crq.control_ras.op = IBMVNIC_TRACE_LEVEL;
3030 crq.control_ras.level = val;
3031 ibmvnic_send_crq(adapter, &crq);
3032
3033 return len;
3034}
3035
3036static const struct file_operations trace_level_ops = {
3037 .owner = THIS_MODULE,
3038 .open = ibmvnic_fw_comp_open,
3039 .read = trace_level_read,
3040 .write = trace_level_write,
3041};
3042
3043static ssize_t trace_buff_size_read(struct file *file, char __user *user_buf,
3044 size_t len, loff_t *ppos)
3045{
3046 struct ibmvnic_fw_comp_internal *ras_comp_int = file->private_data;
3047 struct ibmvnic_adapter *adapter = ras_comp_int->adapter;
3048 int num = ras_comp_int->num;
3049 char buff[9]; /* decimal max int plus \n and \0 */
3050 int size;
3051
3052 size = sprintf(buff, "%d\n", adapter->ras_comps[num].trace_buff_size);
3053 if (*ppos >= size)
3054 return 0;
3055
3056 copy_to_user(user_buf, buff, size);
3057 *ppos += size;
3058 return size;
3059}
3060
3061static ssize_t trace_buff_size_write(struct file *file,
3062 const char __user *user_buf, size_t len,
3063 loff_t *ppos)
3064{
3065 struct ibmvnic_fw_comp_internal *ras_comp_int = file->private_data;
3066 struct ibmvnic_adapter *adapter = ras_comp_int->adapter;
3067 union ibmvnic_crq crq;
3068 unsigned long val;
3069 char buff[9]; /* decimal max int plus \n and \0 */
3070
3071 copy_from_user(buff, user_buf, sizeof(buff));
3072 val = kstrtoul(buff, 10, NULL);
3073
3074 memset(&crq, 0, sizeof(crq));
3075 crq.control_ras.first = IBMVNIC_CRQ_CMD;
3076 crq.control_ras.cmd = CONTROL_RAS;
3077 crq.control_ras.correlator =
3078 adapter->ras_comps[ras_comp_int->num].correlator;
3079 crq.control_ras.op = IBMVNIC_CHG_TRACE_BUFF_SZ;
3080 /* trace_buff_sz is 3 bytes, stuff an int into it */
3081 crq.control_ras.trace_buff_sz[0] = ((u8 *)(&val))[5];
3082 crq.control_ras.trace_buff_sz[1] = ((u8 *)(&val))[6];
3083 crq.control_ras.trace_buff_sz[2] = ((u8 *)(&val))[7];
3084 ibmvnic_send_crq(adapter, &crq);
3085
3086 return len;
3087}
3088
3089static const struct file_operations trace_size_ops = {
3090 .owner = THIS_MODULE,
3091 .open = ibmvnic_fw_comp_open,
3092 .read = trace_buff_size_read,
3093 .write = trace_buff_size_write,
3094};
3095
3096static void handle_request_ras_comps_rsp(union ibmvnic_crq *crq,
3097 struct ibmvnic_adapter *adapter)
3098{
3099 struct device *dev = &adapter->vdev->dev;
3100 struct dentry *dir_ent;
3101 struct dentry *ent;
3102 int i;
3103
3104 debugfs_remove_recursive(adapter->ras_comps_ent);
3105
3106 adapter->ras_comps_ent = debugfs_create_dir("ras_comps",
3107 adapter->debugfs_dir);
3108 if (!adapter->ras_comps_ent || IS_ERR(adapter->ras_comps_ent)) {
3109 dev_info(dev, "debugfs create ras_comps dir failed\n");
3110 return;
3111 }
3112
3113 for (i = 0; i < adapter->ras_comp_num; i++) {
3114 dir_ent = debugfs_create_dir(adapter->ras_comps[i].name,
3115 adapter->ras_comps_ent);
3116 if (!dir_ent || IS_ERR(dir_ent)) {
3117 dev_info(dev, "debugfs create %s dir failed\n",
3118 adapter->ras_comps[i].name);
3119 continue;
3120 }
3121
3122 adapter->ras_comp_int[i].adapter = adapter;
3123 adapter->ras_comp_int[i].num = i;
3124 adapter->ras_comp_int[i].desc_blob.data =
3125 &adapter->ras_comps[i].description;
3126 adapter->ras_comp_int[i].desc_blob.size =
3127 sizeof(adapter->ras_comps[i].description);
3128
3129 /* Don't need to remember the dentry's because the debugfs dir
3130 * gets removed recursively
3131 */
3132 ent = debugfs_create_blob("description", S_IRUGO, dir_ent,
3133 &adapter->ras_comp_int[i].desc_blob);
3134 ent = debugfs_create_file("trace_buf_size", S_IRUGO | S_IWUSR,
3135 dir_ent, &adapter->ras_comp_int[i],
3136 &trace_size_ops);
3137 ent = debugfs_create_file("trace_level",
3138 S_IRUGO |
3139 (adapter->ras_comps[i].trace_level !=
3140 0xFF ? S_IWUSR : 0),
3141 dir_ent, &adapter->ras_comp_int[i],
3142 &trace_level_ops);
3143 ent = debugfs_create_file("error_level",
3144 S_IRUGO |
3145 (adapter->
3146 ras_comps[i].error_check_level !=
3147 0xFF ? S_IWUSR : 0),
3148 dir_ent, &adapter->ras_comp_int[i],
3149 &trace_level_ops);
3150 ent = debugfs_create_file("tracing", S_IRUGO | S_IWUSR,
3151 dir_ent, &adapter->ras_comp_int[i],
3152 &tracing_ops);
3153 ent = debugfs_create_file("paused", S_IRUGO | S_IWUSR,
3154 dir_ent, &adapter->ras_comp_int[i],
3155 &paused_ops);
3156 ent = debugfs_create_file("trace", S_IRUGO, dir_ent,
3157 &adapter->ras_comp_int[i],
3158 &trace_ops);
3159 }
3160}
3161
3162static void handle_request_ras_comp_num_rsp(union ibmvnic_crq *crq,
3163 struct ibmvnic_adapter *adapter)
3164{
3165 int len = adapter->ras_comp_num * sizeof(struct ibmvnic_fw_component);
3166 struct device *dev = &adapter->vdev->dev;
3167 union ibmvnic_crq newcrq;
3168
3169 adapter->ras_comps = dma_alloc_coherent(dev, len,
3170 &adapter->ras_comps_tok,
3171 GFP_KERNEL);
3172 if (!adapter->ras_comps) {
3173 if (!firmware_has_feature(FW_FEATURE_CMO))
3174 dev_err(dev, "Couldn't alloc fw comps buffer\n");
3175 return;
3176 }
3177
3178 adapter->ras_comp_int = kmalloc(adapter->ras_comp_num *
3179 sizeof(struct ibmvnic_fw_comp_internal),
3180 GFP_KERNEL);
3181 if (!adapter->ras_comp_int)
3182 dma_free_coherent(dev, len, adapter->ras_comps,
3183 adapter->ras_comps_tok);
3184
3185 memset(&newcrq, 0, sizeof(newcrq));
3186 newcrq.request_ras_comps.first = IBMVNIC_CRQ_CMD;
3187 newcrq.request_ras_comps.cmd = REQUEST_RAS_COMPS;
3188 newcrq.request_ras_comps.ioba = cpu_to_be32(adapter->ras_comps_tok);
3189 newcrq.request_ras_comps.len = cpu_to_be32(len);
3190 ibmvnic_send_crq(adapter, &newcrq);
3191}
3192
3193static void ibmvnic_free_inflight(struct ibmvnic_adapter *adapter)
3194{
Wei Yongjun96183182016-06-27 20:48:53 +08003195 struct ibmvnic_inflight_cmd *inflight_cmd, *tmp1;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003196 struct device *dev = &adapter->vdev->dev;
Wei Yongjun96183182016-06-27 20:48:53 +08003197 struct ibmvnic_error_buff *error_buff, *tmp2;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003198 unsigned long flags;
3199 unsigned long flags2;
3200
3201 spin_lock_irqsave(&adapter->inflight_lock, flags);
Wei Yongjun96183182016-06-27 20:48:53 +08003202 list_for_each_entry_safe(inflight_cmd, tmp1, &adapter->inflight, list) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06003203 switch (inflight_cmd->crq.generic.cmd) {
3204 case LOGIN:
3205 dma_unmap_single(dev, adapter->login_buf_token,
3206 adapter->login_buf_sz,
3207 DMA_BIDIRECTIONAL);
3208 dma_unmap_single(dev, adapter->login_rsp_buf_token,
3209 adapter->login_rsp_buf_sz,
3210 DMA_BIDIRECTIONAL);
3211 kfree(adapter->login_rsp_buf);
3212 kfree(adapter->login_buf);
3213 break;
3214 case REQUEST_DUMP:
3215 complete(&adapter->fw_done);
3216 break;
3217 case REQUEST_ERROR_INFO:
3218 spin_lock_irqsave(&adapter->error_list_lock, flags2);
Wei Yongjun96183182016-06-27 20:48:53 +08003219 list_for_each_entry_safe(error_buff, tmp2,
3220 &adapter->errors, list) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06003221 dma_unmap_single(dev, error_buff->dma,
3222 error_buff->len,
3223 DMA_FROM_DEVICE);
3224 kfree(error_buff->buff);
3225 list_del(&error_buff->list);
3226 kfree(error_buff);
3227 }
3228 spin_unlock_irqrestore(&adapter->error_list_lock,
3229 flags2);
3230 break;
3231 }
3232 list_del(&inflight_cmd->list);
3233 kfree(inflight_cmd);
3234 }
3235 spin_unlock_irqrestore(&adapter->inflight_lock, flags);
3236}
3237
3238static void ibmvnic_handle_crq(union ibmvnic_crq *crq,
3239 struct ibmvnic_adapter *adapter)
3240{
3241 struct ibmvnic_generic_crq *gen_crq = &crq->generic;
3242 struct net_device *netdev = adapter->netdev;
3243 struct device *dev = &adapter->vdev->dev;
3244 long rc;
3245
3246 netdev_dbg(netdev, "Handling CRQ: %016lx %016lx\n",
3247 ((unsigned long int *)crq)[0],
3248 ((unsigned long int *)crq)[1]);
3249 switch (gen_crq->first) {
3250 case IBMVNIC_CRQ_INIT_RSP:
3251 switch (gen_crq->cmd) {
3252 case IBMVNIC_CRQ_INIT:
3253 dev_info(dev, "Partner initialized\n");
3254 /* Send back a response */
3255 rc = ibmvnic_send_crq_init_complete(adapter);
3256 if (rc == 0)
3257 send_version_xchg(adapter);
3258 else
3259 dev_err(dev, "Can't send initrsp rc=%ld\n", rc);
3260 break;
3261 case IBMVNIC_CRQ_INIT_COMPLETE:
3262 dev_info(dev, "Partner initialization complete\n");
3263 send_version_xchg(adapter);
3264 break;
3265 default:
3266 dev_err(dev, "Unknown crq cmd: %d\n", gen_crq->cmd);
3267 }
3268 return;
3269 case IBMVNIC_CRQ_XPORT_EVENT:
3270 if (gen_crq->cmd == IBMVNIC_PARTITION_MIGRATED) {
3271 dev_info(dev, "Re-enabling adapter\n");
3272 adapter->migrated = true;
3273 ibmvnic_free_inflight(adapter);
3274 release_sub_crqs(adapter);
3275 rc = ibmvnic_reenable_crq_queue(adapter);
3276 if (rc)
3277 dev_err(dev, "Error after enable rc=%ld\n", rc);
3278 adapter->migrated = false;
3279 rc = ibmvnic_send_crq_init(adapter);
3280 if (rc)
3281 dev_err(dev, "Error sending init rc=%ld\n", rc);
3282 } else {
3283 /* The adapter lost the connection */
3284 dev_err(dev, "Virtual Adapter failed (rc=%d)\n",
3285 gen_crq->cmd);
3286 ibmvnic_free_inflight(adapter);
3287 release_sub_crqs(adapter);
3288 }
3289 return;
3290 case IBMVNIC_CRQ_CMD_RSP:
3291 break;
3292 default:
3293 dev_err(dev, "Got an invalid msg type 0x%02x\n",
3294 gen_crq->first);
3295 return;
3296 }
3297
3298 switch (gen_crq->cmd) {
3299 case VERSION_EXCHANGE_RSP:
3300 rc = crq->version_exchange_rsp.rc.code;
3301 if (rc) {
3302 dev_err(dev, "Error %ld in VERSION_EXCHG_RSP\n", rc);
3303 break;
3304 }
3305 dev_info(dev, "Partner protocol version is %d\n",
3306 crq->version_exchange_rsp.version);
3307 if (be16_to_cpu(crq->version_exchange_rsp.version) <
3308 ibmvnic_version)
3309 ibmvnic_version =
3310 be16_to_cpu(crq->version_exchange_rsp.version);
3311 send_cap_queries(adapter);
3312 break;
3313 case QUERY_CAPABILITY_RSP:
3314 handle_query_cap_rsp(crq, adapter);
3315 break;
3316 case QUERY_MAP_RSP:
3317 handle_query_map_rsp(crq, adapter);
3318 break;
3319 case REQUEST_MAP_RSP:
3320 handle_request_map_rsp(crq, adapter);
3321 break;
3322 case REQUEST_UNMAP_RSP:
3323 handle_request_unmap_rsp(crq, adapter);
3324 break;
3325 case REQUEST_CAPABILITY_RSP:
3326 handle_request_cap_rsp(crq, adapter);
3327 break;
3328 case LOGIN_RSP:
3329 netdev_dbg(netdev, "Got Login Response\n");
3330 handle_login_rsp(crq, adapter);
3331 break;
3332 case LOGICAL_LINK_STATE_RSP:
3333 netdev_dbg(netdev, "Got Logical Link State Response\n");
3334 adapter->logical_link_state =
3335 crq->logical_link_state_rsp.link_state;
3336 break;
3337 case LINK_STATE_INDICATION:
3338 netdev_dbg(netdev, "Got Logical Link State Indication\n");
3339 adapter->phys_link_state =
3340 crq->link_state_indication.phys_link_state;
3341 adapter->logical_link_state =
3342 crq->link_state_indication.logical_link_state;
3343 break;
3344 case CHANGE_MAC_ADDR_RSP:
3345 netdev_dbg(netdev, "Got MAC address change Response\n");
3346 handle_change_mac_rsp(crq, adapter);
3347 break;
3348 case ERROR_INDICATION:
3349 netdev_dbg(netdev, "Got Error Indication\n");
3350 handle_error_indication(crq, adapter);
3351 break;
3352 case REQUEST_ERROR_RSP:
3353 netdev_dbg(netdev, "Got Error Detail Response\n");
3354 handle_error_info_rsp(crq, adapter);
3355 break;
3356 case REQUEST_STATISTICS_RSP:
3357 netdev_dbg(netdev, "Got Statistics Response\n");
3358 complete(&adapter->stats_done);
3359 break;
3360 case REQUEST_DUMP_SIZE_RSP:
3361 netdev_dbg(netdev, "Got Request Dump Size Response\n");
3362 handle_dump_size_rsp(crq, adapter);
3363 break;
3364 case REQUEST_DUMP_RSP:
3365 netdev_dbg(netdev, "Got Request Dump Response\n");
3366 complete(&adapter->fw_done);
3367 break;
3368 case QUERY_IP_OFFLOAD_RSP:
3369 netdev_dbg(netdev, "Got Query IP offload Response\n");
3370 handle_query_ip_offload_rsp(adapter);
3371 break;
3372 case MULTICAST_CTRL_RSP:
3373 netdev_dbg(netdev, "Got multicast control Response\n");
3374 break;
3375 case CONTROL_IP_OFFLOAD_RSP:
3376 netdev_dbg(netdev, "Got Control IP offload Response\n");
3377 dma_unmap_single(dev, adapter->ip_offload_ctrl_tok,
3378 sizeof(adapter->ip_offload_ctrl),
3379 DMA_TO_DEVICE);
3380 /* We're done with the queries, perform the login */
3381 send_login(adapter);
3382 break;
3383 case REQUEST_RAS_COMP_NUM_RSP:
3384 netdev_dbg(netdev, "Got Request RAS Comp Num Response\n");
3385 if (crq->request_ras_comp_num_rsp.rc.code == 10) {
3386 netdev_dbg(netdev, "Request RAS Comp Num not supported\n");
3387 break;
3388 }
3389 adapter->ras_comp_num =
3390 be32_to_cpu(crq->request_ras_comp_num_rsp.num_components);
3391 handle_request_ras_comp_num_rsp(crq, adapter);
3392 break;
3393 case REQUEST_RAS_COMPS_RSP:
3394 netdev_dbg(netdev, "Got Request RAS Comps Response\n");
3395 handle_request_ras_comps_rsp(crq, adapter);
3396 break;
3397 case CONTROL_RAS_RSP:
3398 netdev_dbg(netdev, "Got Control RAS Response\n");
3399 handle_control_ras_rsp(crq, adapter);
3400 break;
3401 case COLLECT_FW_TRACE_RSP:
3402 netdev_dbg(netdev, "Got Collect firmware trace Response\n");
3403 complete(&adapter->fw_done);
3404 break;
3405 default:
3406 netdev_err(netdev, "Got an invalid cmd type 0x%02x\n",
3407 gen_crq->cmd);
3408 }
3409}
3410
3411static irqreturn_t ibmvnic_interrupt(int irq, void *instance)
3412{
3413 struct ibmvnic_adapter *adapter = instance;
3414 struct ibmvnic_crq_queue *queue = &adapter->crq;
3415 struct vio_dev *vdev = adapter->vdev;
3416 union ibmvnic_crq *crq;
3417 unsigned long flags;
3418 bool done = false;
3419
3420 spin_lock_irqsave(&queue->lock, flags);
3421 vio_disable_interrupts(vdev);
3422 while (!done) {
3423 /* Pull all the valid messages off the CRQ */
3424 while ((crq = ibmvnic_next_crq(adapter)) != NULL) {
3425 ibmvnic_handle_crq(crq, adapter);
3426 crq->generic.first = 0;
3427 }
3428 vio_enable_interrupts(vdev);
3429 crq = ibmvnic_next_crq(adapter);
3430 if (crq) {
3431 vio_disable_interrupts(vdev);
3432 ibmvnic_handle_crq(crq, adapter);
3433 crq->generic.first = 0;
3434 } else {
3435 done = true;
3436 }
3437 }
3438 spin_unlock_irqrestore(&queue->lock, flags);
3439 return IRQ_HANDLED;
3440}
3441
3442static int ibmvnic_reenable_crq_queue(struct ibmvnic_adapter *adapter)
3443{
3444 struct vio_dev *vdev = adapter->vdev;
3445 int rc;
3446
3447 do {
3448 rc = plpar_hcall_norets(H_ENABLE_CRQ, vdev->unit_address);
3449 } while (rc == H_IN_PROGRESS || rc == H_BUSY || H_IS_LONG_BUSY(rc));
3450
3451 if (rc)
3452 dev_err(&vdev->dev, "Error enabling adapter (rc=%d)\n", rc);
3453
3454 return rc;
3455}
3456
3457static int ibmvnic_reset_crq(struct ibmvnic_adapter *adapter)
3458{
3459 struct ibmvnic_crq_queue *crq = &adapter->crq;
3460 struct device *dev = &adapter->vdev->dev;
3461 struct vio_dev *vdev = adapter->vdev;
3462 int rc;
3463
3464 /* Close the CRQ */
3465 do {
3466 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
3467 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
3468
3469 /* Clean out the queue */
3470 memset(crq->msgs, 0, PAGE_SIZE);
3471 crq->cur = 0;
3472
3473 /* And re-open it again */
3474 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
3475 crq->msg_token, PAGE_SIZE);
3476
3477 if (rc == H_CLOSED)
3478 /* Adapter is good, but other end is not ready */
3479 dev_warn(dev, "Partner adapter not ready\n");
3480 else if (rc != 0)
3481 dev_warn(dev, "Couldn't register crq (rc=%d)\n", rc);
3482
3483 return rc;
3484}
3485
3486static void ibmvnic_release_crq_queue(struct ibmvnic_adapter *adapter)
3487{
3488 struct ibmvnic_crq_queue *crq = &adapter->crq;
3489 struct vio_dev *vdev = adapter->vdev;
3490 long rc;
3491
3492 netdev_dbg(adapter->netdev, "Releasing CRQ\n");
3493 free_irq(vdev->irq, adapter);
3494 do {
3495 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
3496 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
3497
3498 dma_unmap_single(&vdev->dev, crq->msg_token, PAGE_SIZE,
3499 DMA_BIDIRECTIONAL);
3500 free_page((unsigned long)crq->msgs);
3501}
3502
3503static int ibmvnic_init_crq_queue(struct ibmvnic_adapter *adapter)
3504{
3505 struct ibmvnic_crq_queue *crq = &adapter->crq;
3506 struct device *dev = &adapter->vdev->dev;
3507 struct vio_dev *vdev = adapter->vdev;
3508 int rc, retrc = -ENOMEM;
3509
3510 crq->msgs = (union ibmvnic_crq *)get_zeroed_page(GFP_KERNEL);
3511 /* Should we allocate more than one page? */
3512
3513 if (!crq->msgs)
3514 return -ENOMEM;
3515
3516 crq->size = PAGE_SIZE / sizeof(*crq->msgs);
3517 crq->msg_token = dma_map_single(dev, crq->msgs, PAGE_SIZE,
3518 DMA_BIDIRECTIONAL);
3519 if (dma_mapping_error(dev, crq->msg_token))
3520 goto map_failed;
3521
3522 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
3523 crq->msg_token, PAGE_SIZE);
3524
3525 if (rc == H_RESOURCE)
3526 /* maybe kexecing and resource is busy. try a reset */
3527 rc = ibmvnic_reset_crq(adapter);
3528 retrc = rc;
3529
3530 if (rc == H_CLOSED) {
3531 dev_warn(dev, "Partner adapter not ready\n");
3532 } else if (rc) {
3533 dev_warn(dev, "Error %d opening adapter\n", rc);
3534 goto reg_crq_failed;
3535 }
3536
3537 retrc = 0;
3538
3539 netdev_dbg(adapter->netdev, "registering irq 0x%x\n", vdev->irq);
3540 rc = request_irq(vdev->irq, ibmvnic_interrupt, 0, IBMVNIC_NAME,
3541 adapter);
3542 if (rc) {
3543 dev_err(dev, "Couldn't register irq 0x%x. rc=%d\n",
3544 vdev->irq, rc);
3545 goto req_irq_failed;
3546 }
3547
3548 rc = vio_enable_interrupts(vdev);
3549 if (rc) {
3550 dev_err(dev, "Error %d enabling interrupts\n", rc);
3551 goto req_irq_failed;
3552 }
3553
3554 crq->cur = 0;
3555 spin_lock_init(&crq->lock);
3556
3557 return retrc;
3558
3559req_irq_failed:
3560 do {
3561 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
3562 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
3563reg_crq_failed:
3564 dma_unmap_single(dev, crq->msg_token, PAGE_SIZE, DMA_BIDIRECTIONAL);
3565map_failed:
3566 free_page((unsigned long)crq->msgs);
3567 return retrc;
3568}
3569
3570/* debugfs for dump */
3571static int ibmvnic_dump_show(struct seq_file *seq, void *v)
3572{
3573 struct net_device *netdev = seq->private;
3574 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
3575 struct device *dev = &adapter->vdev->dev;
3576 union ibmvnic_crq crq;
3577
3578 memset(&crq, 0, sizeof(crq));
3579 crq.request_dump_size.first = IBMVNIC_CRQ_CMD;
3580 crq.request_dump_size.cmd = REQUEST_DUMP_SIZE;
3581 ibmvnic_send_crq(adapter, &crq);
3582
3583 init_completion(&adapter->fw_done);
3584 wait_for_completion(&adapter->fw_done);
3585
3586 seq_write(seq, adapter->dump_data, adapter->dump_data_size);
3587
3588 dma_unmap_single(dev, adapter->dump_data_token, adapter->dump_data_size,
3589 DMA_BIDIRECTIONAL);
3590
3591 kfree(adapter->dump_data);
3592
3593 return 0;
3594}
3595
3596static int ibmvnic_dump_open(struct inode *inode, struct file *file)
3597{
3598 return single_open(file, ibmvnic_dump_show, inode->i_private);
3599}
3600
3601static const struct file_operations ibmvnic_dump_ops = {
3602 .owner = THIS_MODULE,
3603 .open = ibmvnic_dump_open,
3604 .read = seq_read,
3605 .llseek = seq_lseek,
3606 .release = single_release,
3607};
3608
3609static int ibmvnic_probe(struct vio_dev *dev, const struct vio_device_id *id)
3610{
Thomas Falconea22d512016-07-06 15:35:17 -05003611 unsigned long timeout = msecs_to_jiffies(30000);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003612 struct ibmvnic_adapter *adapter;
3613 struct net_device *netdev;
3614 unsigned char *mac_addr_p;
3615 struct dentry *ent;
3616 char buf[16]; /* debugfs name buf */
3617 int rc;
3618
3619 dev_dbg(&dev->dev, "entering ibmvnic_probe for UA 0x%x\n",
3620 dev->unit_address);
3621
3622 mac_addr_p = (unsigned char *)vio_get_attribute(dev,
3623 VETH_MAC_ADDR, NULL);
3624 if (!mac_addr_p) {
3625 dev_err(&dev->dev,
3626 "(%s:%3.3d) ERROR: Can't find MAC_ADDR attribute\n",
3627 __FILE__, __LINE__);
3628 return 0;
3629 }
3630
3631 netdev = alloc_etherdev_mq(sizeof(struct ibmvnic_adapter),
3632 IBMVNIC_MAX_TX_QUEUES);
3633 if (!netdev)
3634 return -ENOMEM;
3635
3636 adapter = netdev_priv(netdev);
3637 dev_set_drvdata(&dev->dev, netdev);
3638 adapter->vdev = dev;
3639 adapter->netdev = netdev;
3640
3641 ether_addr_copy(adapter->mac_addr, mac_addr_p);
3642 ether_addr_copy(netdev->dev_addr, adapter->mac_addr);
3643 netdev->irq = dev->irq;
3644 netdev->netdev_ops = &ibmvnic_netdev_ops;
3645 netdev->ethtool_ops = &ibmvnic_ethtool_ops;
3646 SET_NETDEV_DEV(netdev, &dev->dev);
3647
3648 spin_lock_init(&adapter->stats_lock);
3649
3650 rc = ibmvnic_init_crq_queue(adapter);
3651 if (rc) {
3652 dev_err(&dev->dev, "Couldn't initialize crq. rc=%d\n", rc);
3653 goto free_netdev;
3654 }
3655
3656 INIT_LIST_HEAD(&adapter->errors);
3657 INIT_LIST_HEAD(&adapter->inflight);
3658 spin_lock_init(&adapter->error_list_lock);
3659 spin_lock_init(&adapter->inflight_lock);
3660
3661 adapter->stats_token = dma_map_single(&dev->dev, &adapter->stats,
3662 sizeof(struct ibmvnic_statistics),
3663 DMA_FROM_DEVICE);
3664 if (dma_mapping_error(&dev->dev, adapter->stats_token)) {
3665 if (!firmware_has_feature(FW_FEATURE_CMO))
3666 dev_err(&dev->dev, "Couldn't map stats buffer\n");
3667 goto free_crq;
3668 }
3669
3670 snprintf(buf, sizeof(buf), "ibmvnic_%x", dev->unit_address);
3671 ent = debugfs_create_dir(buf, NULL);
3672 if (!ent || IS_ERR(ent)) {
3673 dev_info(&dev->dev, "debugfs create directory failed\n");
3674 adapter->debugfs_dir = NULL;
3675 } else {
3676 adapter->debugfs_dir = ent;
3677 ent = debugfs_create_file("dump", S_IRUGO, adapter->debugfs_dir,
3678 netdev, &ibmvnic_dump_ops);
3679 if (!ent || IS_ERR(ent)) {
3680 dev_info(&dev->dev,
3681 "debugfs create dump file failed\n");
3682 adapter->debugfs_dump = NULL;
3683 } else {
3684 adapter->debugfs_dump = ent;
3685 }
3686 }
3687 ibmvnic_send_crq_init(adapter);
3688
3689 init_completion(&adapter->init_done);
Thomas Falconea22d512016-07-06 15:35:17 -05003690 if (!wait_for_completion_timeout(&adapter->init_done, timeout))
3691 return 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003692
John Allen498cd8e2016-04-06 11:49:55 -05003693 do {
John Allen498cd8e2016-04-06 11:49:55 -05003694 if (adapter->renegotiate) {
Thomas Falconea22d512016-07-06 15:35:17 -05003695 adapter->renegotiate = false;
3696 release_sub_crqs_no_irqs(adapter);
John Allen498cd8e2016-04-06 11:49:55 -05003697 send_cap_queries(adapter);
3698
3699 reinit_completion(&adapter->init_done);
Thomas Falconea22d512016-07-06 15:35:17 -05003700 if (!wait_for_completion_timeout(&adapter->init_done,
3701 timeout))
3702 return 0;
John Allen498cd8e2016-04-06 11:49:55 -05003703 }
3704 } while (adapter->renegotiate);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003705
Thomas Falconea22d512016-07-06 15:35:17 -05003706 rc = init_sub_crq_irqs(adapter);
3707 if (rc) {
3708 dev_err(&dev->dev, "failed to initialize sub crq irqs\n");
3709 goto free_debugfs;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003710 }
3711
3712 netdev->real_num_tx_queues = adapter->req_tx_queues;
3713
3714 rc = register_netdev(netdev);
3715 if (rc) {
3716 dev_err(&dev->dev, "failed to register netdev rc=%d\n", rc);
Thomas Falconea22d512016-07-06 15:35:17 -05003717 goto free_sub_crqs;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003718 }
3719 dev_info(&dev->dev, "ibmvnic registered\n");
3720
3721 return 0;
3722
Thomas Falconea22d512016-07-06 15:35:17 -05003723free_sub_crqs:
3724 release_sub_crqs(adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003725free_debugfs:
3726 if (adapter->debugfs_dir && !IS_ERR(adapter->debugfs_dir))
3727 debugfs_remove_recursive(adapter->debugfs_dir);
3728free_crq:
3729 ibmvnic_release_crq_queue(adapter);
3730free_netdev:
3731 free_netdev(netdev);
3732 return rc;
3733}
3734
3735static int ibmvnic_remove(struct vio_dev *dev)
3736{
3737 struct net_device *netdev = dev_get_drvdata(&dev->dev);
3738 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
3739
3740 unregister_netdev(netdev);
3741
3742 release_sub_crqs(adapter);
3743
3744 ibmvnic_release_crq_queue(adapter);
3745
3746 if (adapter->debugfs_dir && !IS_ERR(adapter->debugfs_dir))
3747 debugfs_remove_recursive(adapter->debugfs_dir);
3748
3749 if (adapter->ras_comps)
3750 dma_free_coherent(&dev->dev,
3751 adapter->ras_comp_num *
3752 sizeof(struct ibmvnic_fw_component),
3753 adapter->ras_comps, adapter->ras_comps_tok);
3754
3755 kfree(adapter->ras_comp_int);
3756
3757 free_netdev(netdev);
3758 dev_set_drvdata(&dev->dev, NULL);
3759
3760 return 0;
3761}
3762
3763static unsigned long ibmvnic_get_desired_dma(struct vio_dev *vdev)
3764{
3765 struct net_device *netdev = dev_get_drvdata(&vdev->dev);
3766 struct ibmvnic_adapter *adapter;
3767 struct iommu_table *tbl;
3768 unsigned long ret = 0;
3769 int i;
3770
3771 tbl = get_iommu_table_base(&vdev->dev);
3772
3773 /* netdev inits at probe time along with the structures we need below*/
3774 if (!netdev)
3775 return IOMMU_PAGE_ALIGN(IBMVNIC_IO_ENTITLEMENT_DEFAULT, tbl);
3776
3777 adapter = netdev_priv(netdev);
3778
3779 ret += PAGE_SIZE; /* the crq message queue */
3780 ret += adapter->bounce_buffer_size;
3781 ret += IOMMU_PAGE_ALIGN(sizeof(struct ibmvnic_statistics), tbl);
3782
3783 for (i = 0; i < adapter->req_tx_queues + adapter->req_rx_queues; i++)
3784 ret += 4 * PAGE_SIZE; /* the scrq message queue */
3785
3786 for (i = 0; i < be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
3787 i++)
3788 ret += adapter->rx_pool[i].size *
3789 IOMMU_PAGE_ALIGN(adapter->rx_pool[i].buff_size, tbl);
3790
3791 return ret;
3792}
3793
3794static int ibmvnic_resume(struct device *dev)
3795{
3796 struct net_device *netdev = dev_get_drvdata(dev);
3797 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
3798 int i;
3799
3800 /* kick the interrupt handlers just in case we lost an interrupt */
3801 for (i = 0; i < adapter->req_rx_queues; i++)
3802 ibmvnic_interrupt_rx(adapter->rx_scrq[i]->irq,
3803 adapter->rx_scrq[i]);
3804
3805 return 0;
3806}
3807
3808static struct vio_device_id ibmvnic_device_table[] = {
3809 {"network", "IBM,vnic"},
3810 {"", "" }
3811};
3812MODULE_DEVICE_TABLE(vio, ibmvnic_device_table);
3813
3814static const struct dev_pm_ops ibmvnic_pm_ops = {
3815 .resume = ibmvnic_resume
3816};
3817
3818static struct vio_driver ibmvnic_driver = {
3819 .id_table = ibmvnic_device_table,
3820 .probe = ibmvnic_probe,
3821 .remove = ibmvnic_remove,
3822 .get_desired_dma = ibmvnic_get_desired_dma,
3823 .name = ibmvnic_driver_name,
3824 .pm = &ibmvnic_pm_ops,
3825};
3826
3827/* module functions */
3828static int __init ibmvnic_module_init(void)
3829{
3830 pr_info("%s: %s %s\n", ibmvnic_driver_name, ibmvnic_driver_string,
3831 IBMVNIC_DRIVER_VERSION);
3832
3833 return vio_register_driver(&ibmvnic_driver);
3834}
3835
3836static void __exit ibmvnic_module_exit(void)
3837{
3838 vio_unregister_driver(&ibmvnic_driver);
3839}
3840
3841module_init(ibmvnic_module_init);
3842module_exit(ibmvnic_module_exit);