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