blob: 5f44c5520fbc86bd9076cc220906e0a4cdee4e90 [file] [log] [blame]
Thomas Falcon032c5e82015-12-21 11:26:06 -06001/**************************************************************************/
2/* */
3/* IBM System i and System p Virtual NIC Device Driver */
4/* Copyright (C) 2014 IBM Corp. */
5/* Santiago Leon (santi_leon@yahoo.com) */
6/* Thomas Falcon (tlfalcon@linux.vnet.ibm.com) */
7/* John Allen (jallen@linux.vnet.ibm.com) */
8/* */
9/* This program is free software; you can redistribute it and/or modify */
10/* it under the terms of the GNU General Public License as published by */
11/* the Free Software Foundation; either version 2 of the License, or */
12/* (at your option) any later version. */
13/* */
14/* This program is distributed in the hope that it will be useful, */
15/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
16/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
17/* GNU General Public License for more details. */
18/* */
19/* You should have received a copy of the GNU General Public License */
20/* along with this program. */
21/* */
22/* This module contains the implementation of a virtual ethernet device */
23/* for use with IBM i/p Series LPAR Linux. It utilizes the logical LAN */
24/* option of the RS/6000 Platform Architecture to interface with virtual */
25/* ethernet NICs that are presented to the partition by the hypervisor. */
26/* */
27/* Messages are passed between the VNIC driver and the VNIC server using */
28/* Command/Response Queues (CRQs) and sub CRQs (sCRQs). CRQs are used to */
29/* issue and receive commands that initiate communication with the server */
30/* on driver initialization. Sub CRQs (sCRQs) are similar to CRQs, but */
31/* are used by the driver to notify the server that a packet is */
32/* ready for transmission or that a buffer has been added to receive a */
33/* packet. Subsequently, sCRQs are used by the server to notify the */
34/* driver that a packet transmission has been completed or that a packet */
35/* has been received and placed in a waiting buffer. */
36/* */
37/* In lieu of a more conventional "on-the-fly" DMA mapping strategy in */
38/* which skbs are DMA mapped and immediately unmapped when the transmit */
39/* or receive has been completed, the VNIC driver is required to use */
40/* "long term mapping". This entails that large, continuous DMA mapped */
41/* buffers are allocated on driver initialization and these buffers are */
42/* then continuously reused to pass skbs to and from the VNIC server. */
43/* */
44/**************************************************************************/
45
46#include <linux/module.h>
47#include <linux/moduleparam.h>
48#include <linux/types.h>
49#include <linux/errno.h>
50#include <linux/completion.h>
51#include <linux/ioport.h>
52#include <linux/dma-mapping.h>
53#include <linux/kernel.h>
54#include <linux/netdevice.h>
55#include <linux/etherdevice.h>
56#include <linux/skbuff.h>
57#include <linux/init.h>
58#include <linux/delay.h>
59#include <linux/mm.h>
60#include <linux/ethtool.h>
61#include <linux/proc_fs.h>
62#include <linux/in.h>
63#include <linux/ip.h>
Thomas Falconad7775d2016-04-01 17:20:34 -050064#include <linux/ipv6.h>
Thomas Falcon032c5e82015-12-21 11:26:06 -060065#include <linux/irq.h>
66#include <linux/kthread.h>
67#include <linux/seq_file.h>
68#include <linux/debugfs.h>
69#include <linux/interrupt.h>
70#include <net/net_namespace.h>
71#include <asm/hvcall.h>
72#include <linux/atomic.h>
73#include <asm/vio.h>
74#include <asm/iommu.h>
75#include <linux/uaccess.h>
76#include <asm/firmware.h>
77#include <linux/seq_file.h>
Thomas Falcon65dc6892016-07-06 15:35:18 -050078#include <linux/workqueue.h>
Thomas Falcon032c5e82015-12-21 11:26:06 -060079
80#include "ibmvnic.h"
81
82static const char ibmvnic_driver_name[] = "ibmvnic";
83static const char ibmvnic_driver_string[] = "IBM System i/p Virtual NIC Driver";
84
85MODULE_AUTHOR("Santiago Leon <santi_leon@yahoo.com>");
86MODULE_DESCRIPTION("IBM System i/p Virtual NIC Driver");
87MODULE_LICENSE("GPL");
88MODULE_VERSION(IBMVNIC_DRIVER_VERSION);
89
90static int ibmvnic_version = IBMVNIC_INITIAL_VERSION;
91static int ibmvnic_remove(struct vio_dev *);
92static void release_sub_crqs(struct ibmvnic_adapter *);
Thomas Falconea22d512016-07-06 15:35:17 -050093static void release_sub_crqs_no_irqs(struct ibmvnic_adapter *);
Thomas Falcon032c5e82015-12-21 11:26:06 -060094static int ibmvnic_reset_crq(struct ibmvnic_adapter *);
95static int ibmvnic_send_crq_init(struct ibmvnic_adapter *);
96static int ibmvnic_reenable_crq_queue(struct ibmvnic_adapter *);
97static int ibmvnic_send_crq(struct ibmvnic_adapter *, union ibmvnic_crq *);
98static int send_subcrq(struct ibmvnic_adapter *adapter, u64 remote_handle,
99 union sub_crq *sub_crq);
Thomas Falconad7775d2016-04-01 17:20:34 -0500100static int send_subcrq_indirect(struct ibmvnic_adapter *, u64, u64, u64);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600101static irqreturn_t ibmvnic_interrupt_rx(int irq, void *instance);
102static int enable_scrq_irq(struct ibmvnic_adapter *,
103 struct ibmvnic_sub_crq_queue *);
104static int disable_scrq_irq(struct ibmvnic_adapter *,
105 struct ibmvnic_sub_crq_queue *);
106static int pending_scrq(struct ibmvnic_adapter *,
107 struct ibmvnic_sub_crq_queue *);
108static union sub_crq *ibmvnic_next_scrq(struct ibmvnic_adapter *,
109 struct ibmvnic_sub_crq_queue *);
110static int ibmvnic_poll(struct napi_struct *napi, int data);
111static void send_map_query(struct ibmvnic_adapter *adapter);
112static void send_request_map(struct ibmvnic_adapter *, dma_addr_t, __be32, u8);
113static void send_request_unmap(struct ibmvnic_adapter *, u8);
114
115struct ibmvnic_stat {
116 char name[ETH_GSTRING_LEN];
117 int offset;
118};
119
120#define IBMVNIC_STAT_OFF(stat) (offsetof(struct ibmvnic_adapter, stats) + \
121 offsetof(struct ibmvnic_statistics, stat))
122#define IBMVNIC_GET_STAT(a, off) (*((u64 *)(((unsigned long)(a)) + off)))
123
124static const struct ibmvnic_stat ibmvnic_stats[] = {
125 {"rx_packets", IBMVNIC_STAT_OFF(rx_packets)},
126 {"rx_bytes", IBMVNIC_STAT_OFF(rx_bytes)},
127 {"tx_packets", IBMVNIC_STAT_OFF(tx_packets)},
128 {"tx_bytes", IBMVNIC_STAT_OFF(tx_bytes)},
129 {"ucast_tx_packets", IBMVNIC_STAT_OFF(ucast_tx_packets)},
130 {"ucast_rx_packets", IBMVNIC_STAT_OFF(ucast_rx_packets)},
131 {"mcast_tx_packets", IBMVNIC_STAT_OFF(mcast_tx_packets)},
132 {"mcast_rx_packets", IBMVNIC_STAT_OFF(mcast_rx_packets)},
133 {"bcast_tx_packets", IBMVNIC_STAT_OFF(bcast_tx_packets)},
134 {"bcast_rx_packets", IBMVNIC_STAT_OFF(bcast_rx_packets)},
135 {"align_errors", IBMVNIC_STAT_OFF(align_errors)},
136 {"fcs_errors", IBMVNIC_STAT_OFF(fcs_errors)},
137 {"single_collision_frames", IBMVNIC_STAT_OFF(single_collision_frames)},
138 {"multi_collision_frames", IBMVNIC_STAT_OFF(multi_collision_frames)},
139 {"sqe_test_errors", IBMVNIC_STAT_OFF(sqe_test_errors)},
140 {"deferred_tx", IBMVNIC_STAT_OFF(deferred_tx)},
141 {"late_collisions", IBMVNIC_STAT_OFF(late_collisions)},
142 {"excess_collisions", IBMVNIC_STAT_OFF(excess_collisions)},
143 {"internal_mac_tx_errors", IBMVNIC_STAT_OFF(internal_mac_tx_errors)},
144 {"carrier_sense", IBMVNIC_STAT_OFF(carrier_sense)},
145 {"too_long_frames", IBMVNIC_STAT_OFF(too_long_frames)},
146 {"internal_mac_rx_errors", IBMVNIC_STAT_OFF(internal_mac_rx_errors)},
147};
148
149static long h_reg_sub_crq(unsigned long unit_address, unsigned long token,
150 unsigned long length, unsigned long *number,
151 unsigned long *irq)
152{
153 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
154 long rc;
155
156 rc = plpar_hcall(H_REG_SUB_CRQ, retbuf, unit_address, token, length);
157 *number = retbuf[0];
158 *irq = retbuf[1];
159
160 return rc;
161}
162
163/* net_device_ops functions */
164
165static void init_rx_pool(struct ibmvnic_adapter *adapter,
166 struct ibmvnic_rx_pool *rx_pool, int num, int index,
167 int buff_size, int active)
168{
169 netdev_dbg(adapter->netdev,
170 "Initializing rx_pool %d, %d buffs, %d bytes each\n",
171 index, num, buff_size);
172 rx_pool->size = num;
173 rx_pool->index = index;
174 rx_pool->buff_size = buff_size;
175 rx_pool->active = active;
176}
177
178static int alloc_long_term_buff(struct ibmvnic_adapter *adapter,
179 struct ibmvnic_long_term_buff *ltb, int size)
180{
181 struct device *dev = &adapter->vdev->dev;
182
183 ltb->size = size;
184 ltb->buff = dma_alloc_coherent(dev, ltb->size, &ltb->addr,
185 GFP_KERNEL);
186
187 if (!ltb->buff) {
188 dev_err(dev, "Couldn't alloc long term buffer\n");
189 return -ENOMEM;
190 }
191 ltb->map_id = adapter->map_id;
192 adapter->map_id++;
193 send_request_map(adapter, ltb->addr,
194 ltb->size, ltb->map_id);
195 init_completion(&adapter->fw_done);
196 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++)
509 napi_enable(&adapter->napi[i]);
510alloc_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
905static int ibmvnic_change_mtu(struct net_device *netdev, int new_mtu)
906{
907 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
908
909 if (new_mtu > adapter->req_mtu || new_mtu < adapter->min_mtu)
910 return -EINVAL;
911
912 netdev->mtu = new_mtu;
913 return 0;
914}
915
916static void ibmvnic_tx_timeout(struct net_device *dev)
917{
918 struct ibmvnic_adapter *adapter = netdev_priv(dev);
919 int rc;
920
921 /* Adapter timed out, resetting it */
922 release_sub_crqs(adapter);
923 rc = ibmvnic_reset_crq(adapter);
924 if (rc)
925 dev_err(&adapter->vdev->dev, "Adapter timeout, reset failed\n");
926 else
927 ibmvnic_send_crq_init(adapter);
928}
929
930static void remove_buff_from_pool(struct ibmvnic_adapter *adapter,
931 struct ibmvnic_rx_buff *rx_buff)
932{
933 struct ibmvnic_rx_pool *pool = &adapter->rx_pool[rx_buff->pool_index];
934
935 rx_buff->skb = NULL;
936
937 pool->free_map[pool->next_alloc] = (int)(rx_buff - pool->rx_buff);
938 pool->next_alloc = (pool->next_alloc + 1) % pool->size;
939
940 atomic_dec(&pool->available);
941}
942
943static int ibmvnic_poll(struct napi_struct *napi, int budget)
944{
945 struct net_device *netdev = napi->dev;
946 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
947 int scrq_num = (int)(napi - adapter->napi);
948 int frames_processed = 0;
949restart_poll:
950 while (frames_processed < budget) {
951 struct sk_buff *skb;
952 struct ibmvnic_rx_buff *rx_buff;
953 union sub_crq *next;
954 u32 length;
955 u16 offset;
956 u8 flags = 0;
957
958 if (!pending_scrq(adapter, adapter->rx_scrq[scrq_num]))
959 break;
960 next = ibmvnic_next_scrq(adapter, adapter->rx_scrq[scrq_num]);
961 rx_buff =
962 (struct ibmvnic_rx_buff *)be64_to_cpu(next->
963 rx_comp.correlator);
964 /* do error checking */
965 if (next->rx_comp.rc) {
966 netdev_err(netdev, "rx error %x\n", next->rx_comp.rc);
967 /* free the entry */
968 next->rx_comp.first = 0;
969 remove_buff_from_pool(adapter, rx_buff);
970 break;
971 }
972
973 length = be32_to_cpu(next->rx_comp.len);
974 offset = be16_to_cpu(next->rx_comp.off_frame_data);
975 flags = next->rx_comp.flags;
976 skb = rx_buff->skb;
977 skb_copy_to_linear_data(skb, rx_buff->data + offset,
978 length);
979 skb->vlan_tci = be16_to_cpu(next->rx_comp.vlan_tci);
980 /* free the entry */
981 next->rx_comp.first = 0;
982 remove_buff_from_pool(adapter, rx_buff);
983
984 skb_put(skb, length);
985 skb->protocol = eth_type_trans(skb, netdev);
986
987 if (flags & IBMVNIC_IP_CHKSUM_GOOD &&
988 flags & IBMVNIC_TCP_UDP_CHKSUM_GOOD) {
989 skb->ip_summed = CHECKSUM_UNNECESSARY;
990 }
991
992 length = skb->len;
993 napi_gro_receive(napi, skb); /* send it up */
994 netdev->stats.rx_packets++;
995 netdev->stats.rx_bytes += length;
996 frames_processed++;
997 }
John Allen498cd8e2016-04-06 11:49:55 -0500998 replenish_rx_pool(adapter, &adapter->rx_pool[scrq_num]);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600999
1000 if (frames_processed < budget) {
1001 enable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
1002 napi_complete(napi);
1003 if (pending_scrq(adapter, adapter->rx_scrq[scrq_num]) &&
1004 napi_reschedule(napi)) {
1005 disable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
1006 goto restart_poll;
1007 }
1008 }
1009 return frames_processed;
1010}
1011
1012#ifdef CONFIG_NET_POLL_CONTROLLER
1013static void ibmvnic_netpoll_controller(struct net_device *dev)
1014{
1015 struct ibmvnic_adapter *adapter = netdev_priv(dev);
1016 int i;
1017
1018 replenish_pools(netdev_priv(dev));
1019 for (i = 0; i < adapter->req_rx_queues; i++)
1020 ibmvnic_interrupt_rx(adapter->rx_scrq[i]->irq,
1021 adapter->rx_scrq[i]);
1022}
1023#endif
1024
1025static const struct net_device_ops ibmvnic_netdev_ops = {
1026 .ndo_open = ibmvnic_open,
1027 .ndo_stop = ibmvnic_close,
1028 .ndo_start_xmit = ibmvnic_xmit,
1029 .ndo_set_rx_mode = ibmvnic_set_multi,
1030 .ndo_set_mac_address = ibmvnic_set_mac,
1031 .ndo_validate_addr = eth_validate_addr,
1032 .ndo_change_mtu = ibmvnic_change_mtu,
1033 .ndo_tx_timeout = ibmvnic_tx_timeout,
1034#ifdef CONFIG_NET_POLL_CONTROLLER
1035 .ndo_poll_controller = ibmvnic_netpoll_controller,
1036#endif
1037};
1038
1039/* ethtool functions */
1040
1041static int ibmvnic_get_settings(struct net_device *netdev,
1042 struct ethtool_cmd *cmd)
1043{
1044 cmd->supported = (SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg |
1045 SUPPORTED_FIBRE);
1046 cmd->advertising = (ADVERTISED_1000baseT_Full | ADVERTISED_Autoneg |
1047 ADVERTISED_FIBRE);
1048 ethtool_cmd_speed_set(cmd, SPEED_1000);
1049 cmd->duplex = DUPLEX_FULL;
1050 cmd->port = PORT_FIBRE;
1051 cmd->phy_address = 0;
1052 cmd->transceiver = XCVR_INTERNAL;
1053 cmd->autoneg = AUTONEG_ENABLE;
1054 cmd->maxtxpkt = 0;
1055 cmd->maxrxpkt = 1;
1056 return 0;
1057}
1058
1059static void ibmvnic_get_drvinfo(struct net_device *dev,
1060 struct ethtool_drvinfo *info)
1061{
1062 strlcpy(info->driver, ibmvnic_driver_name, sizeof(info->driver));
1063 strlcpy(info->version, IBMVNIC_DRIVER_VERSION, sizeof(info->version));
1064}
1065
1066static u32 ibmvnic_get_msglevel(struct net_device *netdev)
1067{
1068 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1069
1070 return adapter->msg_enable;
1071}
1072
1073static void ibmvnic_set_msglevel(struct net_device *netdev, u32 data)
1074{
1075 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1076
1077 adapter->msg_enable = data;
1078}
1079
1080static u32 ibmvnic_get_link(struct net_device *netdev)
1081{
1082 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1083
1084 /* Don't need to send a query because we request a logical link up at
1085 * init and then we wait for link state indications
1086 */
1087 return adapter->logical_link_state;
1088}
1089
1090static void ibmvnic_get_ringparam(struct net_device *netdev,
1091 struct ethtool_ringparam *ring)
1092{
1093 ring->rx_max_pending = 0;
1094 ring->tx_max_pending = 0;
1095 ring->rx_mini_max_pending = 0;
1096 ring->rx_jumbo_max_pending = 0;
1097 ring->rx_pending = 0;
1098 ring->tx_pending = 0;
1099 ring->rx_mini_pending = 0;
1100 ring->rx_jumbo_pending = 0;
1101}
1102
1103static void ibmvnic_get_strings(struct net_device *dev, u32 stringset, u8 *data)
1104{
1105 int i;
1106
1107 if (stringset != ETH_SS_STATS)
1108 return;
1109
1110 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++, data += ETH_GSTRING_LEN)
1111 memcpy(data, ibmvnic_stats[i].name, ETH_GSTRING_LEN);
1112}
1113
1114static int ibmvnic_get_sset_count(struct net_device *dev, int sset)
1115{
1116 switch (sset) {
1117 case ETH_SS_STATS:
1118 return ARRAY_SIZE(ibmvnic_stats);
1119 default:
1120 return -EOPNOTSUPP;
1121 }
1122}
1123
1124static void ibmvnic_get_ethtool_stats(struct net_device *dev,
1125 struct ethtool_stats *stats, u64 *data)
1126{
1127 struct ibmvnic_adapter *adapter = netdev_priv(dev);
1128 union ibmvnic_crq crq;
1129 int i;
1130
1131 memset(&crq, 0, sizeof(crq));
1132 crq.request_statistics.first = IBMVNIC_CRQ_CMD;
1133 crq.request_statistics.cmd = REQUEST_STATISTICS;
1134 crq.request_statistics.ioba = cpu_to_be32(adapter->stats_token);
1135 crq.request_statistics.len =
1136 cpu_to_be32(sizeof(struct ibmvnic_statistics));
1137 ibmvnic_send_crq(adapter, &crq);
1138
1139 /* Wait for data to be written */
1140 init_completion(&adapter->stats_done);
1141 wait_for_completion(&adapter->stats_done);
1142
1143 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++)
1144 data[i] = IBMVNIC_GET_STAT(adapter, ibmvnic_stats[i].offset);
1145}
1146
1147static const struct ethtool_ops ibmvnic_ethtool_ops = {
1148 .get_settings = ibmvnic_get_settings,
1149 .get_drvinfo = ibmvnic_get_drvinfo,
1150 .get_msglevel = ibmvnic_get_msglevel,
1151 .set_msglevel = ibmvnic_set_msglevel,
1152 .get_link = ibmvnic_get_link,
1153 .get_ringparam = ibmvnic_get_ringparam,
1154 .get_strings = ibmvnic_get_strings,
1155 .get_sset_count = ibmvnic_get_sset_count,
1156 .get_ethtool_stats = ibmvnic_get_ethtool_stats,
1157};
1158
1159/* Routines for managing CRQs/sCRQs */
1160
1161static void release_sub_crq_queue(struct ibmvnic_adapter *adapter,
1162 struct ibmvnic_sub_crq_queue *scrq)
1163{
1164 struct device *dev = &adapter->vdev->dev;
1165 long rc;
1166
1167 netdev_dbg(adapter->netdev, "Releasing sub-CRQ\n");
1168
1169 /* Close the sub-crqs */
1170 do {
1171 rc = plpar_hcall_norets(H_FREE_SUB_CRQ,
1172 adapter->vdev->unit_address,
1173 scrq->crq_num);
1174 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
1175
1176 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
1177 DMA_BIDIRECTIONAL);
1178 free_pages((unsigned long)scrq->msgs, 2);
1179 kfree(scrq);
1180}
1181
1182static struct ibmvnic_sub_crq_queue *init_sub_crq_queue(struct ibmvnic_adapter
1183 *adapter)
1184{
1185 struct device *dev = &adapter->vdev->dev;
1186 struct ibmvnic_sub_crq_queue *scrq;
1187 int rc;
1188
1189 scrq = kmalloc(sizeof(*scrq), GFP_ATOMIC);
1190 if (!scrq)
1191 return NULL;
1192
Thomas Falcon12608c22016-10-17 15:28:09 -05001193 scrq->msgs = (union sub_crq *)__get_free_pages(GFP_ATOMIC, 2);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001194 memset(scrq->msgs, 0, 4 * PAGE_SIZE);
1195 if (!scrq->msgs) {
1196 dev_warn(dev, "Couldn't allocate crq queue messages page\n");
1197 goto zero_page_failed;
1198 }
1199
1200 scrq->msg_token = dma_map_single(dev, scrq->msgs, 4 * PAGE_SIZE,
1201 DMA_BIDIRECTIONAL);
1202 if (dma_mapping_error(dev, scrq->msg_token)) {
1203 dev_warn(dev, "Couldn't map crq queue messages page\n");
1204 goto map_failed;
1205 }
1206
1207 rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
1208 4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
1209
1210 if (rc == H_RESOURCE)
1211 rc = ibmvnic_reset_crq(adapter);
1212
1213 if (rc == H_CLOSED) {
1214 dev_warn(dev, "Partner adapter not ready, waiting.\n");
1215 } else if (rc) {
1216 dev_warn(dev, "Error %d registering sub-crq\n", rc);
1217 goto reg_failed;
1218 }
1219
Thomas Falcon032c5e82015-12-21 11:26:06 -06001220 scrq->adapter = adapter;
1221 scrq->size = 4 * PAGE_SIZE / sizeof(*scrq->msgs);
1222 scrq->cur = 0;
1223 scrq->rx_skb_top = NULL;
1224 spin_lock_init(&scrq->lock);
1225
1226 netdev_dbg(adapter->netdev,
1227 "sub-crq initialized, num %lx, hw_irq=%lx, irq=%x\n",
1228 scrq->crq_num, scrq->hw_irq, scrq->irq);
1229
1230 return scrq;
1231
Thomas Falcon032c5e82015-12-21 11:26:06 -06001232reg_failed:
1233 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
1234 DMA_BIDIRECTIONAL);
1235map_failed:
1236 free_pages((unsigned long)scrq->msgs, 2);
1237zero_page_failed:
1238 kfree(scrq);
1239
1240 return NULL;
1241}
1242
1243static void release_sub_crqs(struct ibmvnic_adapter *adapter)
1244{
1245 int i;
1246
1247 if (adapter->tx_scrq) {
1248 for (i = 0; i < adapter->req_tx_queues; i++)
1249 if (adapter->tx_scrq[i]) {
1250 free_irq(adapter->tx_scrq[i]->irq,
1251 adapter->tx_scrq[i]);
Thomas Falcon88eb98a2016-07-06 15:35:16 -05001252 irq_dispose_mapping(adapter->tx_scrq[i]->irq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001253 release_sub_crq_queue(adapter,
1254 adapter->tx_scrq[i]);
1255 }
1256 adapter->tx_scrq = NULL;
1257 }
1258
1259 if (adapter->rx_scrq) {
1260 for (i = 0; i < adapter->req_rx_queues; i++)
1261 if (adapter->rx_scrq[i]) {
1262 free_irq(adapter->rx_scrq[i]->irq,
1263 adapter->rx_scrq[i]);
Thomas Falcon88eb98a2016-07-06 15:35:16 -05001264 irq_dispose_mapping(adapter->rx_scrq[i]->irq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001265 release_sub_crq_queue(adapter,
1266 adapter->rx_scrq[i]);
1267 }
1268 adapter->rx_scrq = NULL;
1269 }
1270
1271 adapter->requested_caps = 0;
1272}
1273
Thomas Falconea22d512016-07-06 15:35:17 -05001274static void release_sub_crqs_no_irqs(struct ibmvnic_adapter *adapter)
1275{
1276 int i;
1277
1278 if (adapter->tx_scrq) {
1279 for (i = 0; i < adapter->req_tx_queues; i++)
1280 if (adapter->tx_scrq[i])
1281 release_sub_crq_queue(adapter,
1282 adapter->tx_scrq[i]);
1283 adapter->tx_scrq = NULL;
1284 }
1285
1286 if (adapter->rx_scrq) {
1287 for (i = 0; i < adapter->req_rx_queues; i++)
1288 if (adapter->rx_scrq[i])
1289 release_sub_crq_queue(adapter,
1290 adapter->rx_scrq[i]);
1291 adapter->rx_scrq = NULL;
1292 }
1293
1294 adapter->requested_caps = 0;
1295}
1296
Thomas Falcon032c5e82015-12-21 11:26:06 -06001297static int disable_scrq_irq(struct ibmvnic_adapter *adapter,
1298 struct ibmvnic_sub_crq_queue *scrq)
1299{
1300 struct device *dev = &adapter->vdev->dev;
1301 unsigned long rc;
1302
1303 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
1304 H_DISABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
1305 if (rc)
1306 dev_err(dev, "Couldn't disable scrq irq 0x%lx. rc=%ld\n",
1307 scrq->hw_irq, rc);
1308 return rc;
1309}
1310
1311static int enable_scrq_irq(struct ibmvnic_adapter *adapter,
1312 struct ibmvnic_sub_crq_queue *scrq)
1313{
1314 struct device *dev = &adapter->vdev->dev;
1315 unsigned long rc;
1316
1317 if (scrq->hw_irq > 0x100000000ULL) {
1318 dev_err(dev, "bad hw_irq = %lx\n", scrq->hw_irq);
1319 return 1;
1320 }
1321
1322 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
1323 H_ENABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
1324 if (rc)
1325 dev_err(dev, "Couldn't enable scrq irq 0x%lx. rc=%ld\n",
1326 scrq->hw_irq, rc);
1327 return rc;
1328}
1329
1330static int ibmvnic_complete_tx(struct ibmvnic_adapter *adapter,
1331 struct ibmvnic_sub_crq_queue *scrq)
1332{
1333 struct device *dev = &adapter->vdev->dev;
1334 struct ibmvnic_tx_buff *txbuff;
1335 union sub_crq *next;
1336 int index;
1337 int i, j;
Thomas Falconad7775d2016-04-01 17:20:34 -05001338 u8 first;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001339
1340restart_loop:
1341 while (pending_scrq(adapter, scrq)) {
1342 unsigned int pool = scrq->pool_index;
1343
1344 next = ibmvnic_next_scrq(adapter, scrq);
1345 for (i = 0; i < next->tx_comp.num_comps; i++) {
1346 if (next->tx_comp.rcs[i]) {
1347 dev_err(dev, "tx error %x\n",
1348 next->tx_comp.rcs[i]);
1349 continue;
1350 }
1351 index = be32_to_cpu(next->tx_comp.correlators[i]);
1352 txbuff = &adapter->tx_pool[pool].tx_buff[index];
1353
1354 for (j = 0; j < IBMVNIC_MAX_FRAGS_PER_CRQ; j++) {
1355 if (!txbuff->data_dma[j])
1356 continue;
1357
1358 txbuff->data_dma[j] = 0;
1359 txbuff->used_bounce = false;
1360 }
Thomas Falconad7775d2016-04-01 17:20:34 -05001361 /* if sub_crq was sent indirectly */
1362 first = txbuff->indir_arr[0].generic.first;
1363 if (first == IBMVNIC_CRQ_CMD) {
1364 dma_unmap_single(dev, txbuff->indir_dma,
1365 sizeof(txbuff->indir_arr),
1366 DMA_TO_DEVICE);
1367 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001368
1369 if (txbuff->last_frag)
1370 dev_kfree_skb_any(txbuff->skb);
1371
1372 adapter->tx_pool[pool].free_map[adapter->tx_pool[pool].
1373 producer_index] = index;
1374 adapter->tx_pool[pool].producer_index =
1375 (adapter->tx_pool[pool].producer_index + 1) %
1376 adapter->max_tx_entries_per_subcrq;
1377 }
1378 /* remove tx_comp scrq*/
1379 next->tx_comp.first = 0;
1380 }
1381
1382 enable_scrq_irq(adapter, scrq);
1383
1384 if (pending_scrq(adapter, scrq)) {
1385 disable_scrq_irq(adapter, scrq);
1386 goto restart_loop;
1387 }
1388
1389 return 0;
1390}
1391
1392static irqreturn_t ibmvnic_interrupt_tx(int irq, void *instance)
1393{
1394 struct ibmvnic_sub_crq_queue *scrq = instance;
1395 struct ibmvnic_adapter *adapter = scrq->adapter;
1396
1397 disable_scrq_irq(adapter, scrq);
1398 ibmvnic_complete_tx(adapter, scrq);
1399
1400 return IRQ_HANDLED;
1401}
1402
1403static irqreturn_t ibmvnic_interrupt_rx(int irq, void *instance)
1404{
1405 struct ibmvnic_sub_crq_queue *scrq = instance;
1406 struct ibmvnic_adapter *adapter = scrq->adapter;
1407
1408 if (napi_schedule_prep(&adapter->napi[scrq->scrq_num])) {
1409 disable_scrq_irq(adapter, scrq);
1410 __napi_schedule(&adapter->napi[scrq->scrq_num]);
1411 }
1412
1413 return IRQ_HANDLED;
1414}
1415
Thomas Falconea22d512016-07-06 15:35:17 -05001416static int init_sub_crq_irqs(struct ibmvnic_adapter *adapter)
1417{
1418 struct device *dev = &adapter->vdev->dev;
1419 struct ibmvnic_sub_crq_queue *scrq;
1420 int i = 0, j = 0;
1421 int rc = 0;
1422
1423 for (i = 0; i < adapter->req_tx_queues; i++) {
1424 scrq = adapter->tx_scrq[i];
1425 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
1426
Michael Ellerman99c17902016-09-10 19:59:05 +10001427 if (!scrq->irq) {
Thomas Falconea22d512016-07-06 15:35:17 -05001428 rc = -EINVAL;
1429 dev_err(dev, "Error mapping irq\n");
1430 goto req_tx_irq_failed;
1431 }
1432
1433 rc = request_irq(scrq->irq, ibmvnic_interrupt_tx,
1434 0, "ibmvnic_tx", scrq);
1435
1436 if (rc) {
1437 dev_err(dev, "Couldn't register tx irq 0x%x. rc=%d\n",
1438 scrq->irq, rc);
1439 irq_dispose_mapping(scrq->irq);
1440 goto req_rx_irq_failed;
1441 }
1442 }
1443
1444 for (i = 0; i < adapter->req_rx_queues; i++) {
1445 scrq = adapter->rx_scrq[i];
1446 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
Michael Ellerman99c17902016-09-10 19:59:05 +10001447 if (!scrq->irq) {
Thomas Falconea22d512016-07-06 15:35:17 -05001448 rc = -EINVAL;
1449 dev_err(dev, "Error mapping irq\n");
1450 goto req_rx_irq_failed;
1451 }
1452 rc = request_irq(scrq->irq, ibmvnic_interrupt_rx,
1453 0, "ibmvnic_rx", scrq);
1454 if (rc) {
1455 dev_err(dev, "Couldn't register rx irq 0x%x. rc=%d\n",
1456 scrq->irq, rc);
1457 irq_dispose_mapping(scrq->irq);
1458 goto req_rx_irq_failed;
1459 }
1460 }
1461 return rc;
1462
1463req_rx_irq_failed:
Thomas Falcon8bf371e2016-10-27 12:28:52 -05001464 for (j = 0; j < i; j++) {
Thomas Falconea22d512016-07-06 15:35:17 -05001465 free_irq(adapter->rx_scrq[j]->irq, adapter->rx_scrq[j]);
1466 irq_dispose_mapping(adapter->rx_scrq[j]->irq);
Thomas Falcon8bf371e2016-10-27 12:28:52 -05001467 }
Thomas Falconea22d512016-07-06 15:35:17 -05001468 i = adapter->req_tx_queues;
1469req_tx_irq_failed:
Thomas Falcon8bf371e2016-10-27 12:28:52 -05001470 for (j = 0; j < i; j++) {
Thomas Falconea22d512016-07-06 15:35:17 -05001471 free_irq(adapter->tx_scrq[j]->irq, adapter->tx_scrq[j]);
1472 irq_dispose_mapping(adapter->rx_scrq[j]->irq);
Thomas Falcon8bf371e2016-10-27 12:28:52 -05001473 }
Thomas Falconea22d512016-07-06 15:35:17 -05001474 release_sub_crqs_no_irqs(adapter);
1475 return rc;
1476}
1477
Thomas Falcon032c5e82015-12-21 11:26:06 -06001478static void init_sub_crqs(struct ibmvnic_adapter *adapter, int retry)
1479{
1480 struct device *dev = &adapter->vdev->dev;
1481 struct ibmvnic_sub_crq_queue **allqueues;
1482 int registered_queues = 0;
1483 union ibmvnic_crq crq;
1484 int total_queues;
1485 int more = 0;
Thomas Falconea22d512016-07-06 15:35:17 -05001486 int i;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001487
1488 if (!retry) {
1489 /* Sub-CRQ entries are 32 byte long */
1490 int entries_page = 4 * PAGE_SIZE / (sizeof(u64) * 4);
1491
1492 if (adapter->min_tx_entries_per_subcrq > entries_page ||
1493 adapter->min_rx_add_entries_per_subcrq > entries_page) {
1494 dev_err(dev, "Fatal, invalid entries per sub-crq\n");
1495 goto allqueues_failed;
1496 }
1497
1498 /* Get the minimum between the queried max and the entries
1499 * that fit in our PAGE_SIZE
1500 */
1501 adapter->req_tx_entries_per_subcrq =
1502 adapter->max_tx_entries_per_subcrq > entries_page ?
1503 entries_page : adapter->max_tx_entries_per_subcrq;
1504 adapter->req_rx_add_entries_per_subcrq =
1505 adapter->max_rx_add_entries_per_subcrq > entries_page ?
1506 entries_page : adapter->max_rx_add_entries_per_subcrq;
1507
1508 /* Choosing the maximum number of queues supported by firmware*/
John Allen498cd8e2016-04-06 11:49:55 -05001509 adapter->req_tx_queues = adapter->max_tx_queues;
1510 adapter->req_rx_queues = adapter->max_rx_queues;
1511 adapter->req_rx_add_queues = adapter->max_rx_add_queues;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001512
1513 adapter->req_mtu = adapter->max_mtu;
1514 }
1515
1516 total_queues = adapter->req_tx_queues + adapter->req_rx_queues;
1517
1518 allqueues = kcalloc(total_queues, sizeof(*allqueues), GFP_ATOMIC);
1519 if (!allqueues)
1520 goto allqueues_failed;
1521
1522 for (i = 0; i < total_queues; i++) {
1523 allqueues[i] = init_sub_crq_queue(adapter);
1524 if (!allqueues[i]) {
1525 dev_warn(dev, "Couldn't allocate all sub-crqs\n");
1526 break;
1527 }
1528 registered_queues++;
1529 }
1530
1531 /* Make sure we were able to register the minimum number of queues */
1532 if (registered_queues <
1533 adapter->min_tx_queues + adapter->min_rx_queues) {
1534 dev_err(dev, "Fatal: Couldn't init min number of sub-crqs\n");
1535 goto tx_failed;
1536 }
1537
1538 /* Distribute the failed allocated queues*/
1539 for (i = 0; i < total_queues - registered_queues + more ; i++) {
1540 netdev_dbg(adapter->netdev, "Reducing number of queues\n");
1541 switch (i % 3) {
1542 case 0:
1543 if (adapter->req_rx_queues > adapter->min_rx_queues)
1544 adapter->req_rx_queues--;
1545 else
1546 more++;
1547 break;
1548 case 1:
1549 if (adapter->req_tx_queues > adapter->min_tx_queues)
1550 adapter->req_tx_queues--;
1551 else
1552 more++;
1553 break;
1554 }
1555 }
1556
1557 adapter->tx_scrq = kcalloc(adapter->req_tx_queues,
1558 sizeof(*adapter->tx_scrq), GFP_ATOMIC);
1559 if (!adapter->tx_scrq)
1560 goto tx_failed;
1561
1562 for (i = 0; i < adapter->req_tx_queues; i++) {
1563 adapter->tx_scrq[i] = allqueues[i];
1564 adapter->tx_scrq[i]->pool_index = i;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001565 }
1566
1567 adapter->rx_scrq = kcalloc(adapter->req_rx_queues,
1568 sizeof(*adapter->rx_scrq), GFP_ATOMIC);
1569 if (!adapter->rx_scrq)
1570 goto rx_failed;
1571
1572 for (i = 0; i < adapter->req_rx_queues; i++) {
1573 adapter->rx_scrq[i] = allqueues[i + adapter->req_tx_queues];
1574 adapter->rx_scrq[i]->scrq_num = i;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001575 }
1576
1577 memset(&crq, 0, sizeof(crq));
1578 crq.request_capability.first = IBMVNIC_CRQ_CMD;
1579 crq.request_capability.cmd = REQUEST_CAPABILITY;
1580
1581 crq.request_capability.capability = cpu_to_be16(REQ_TX_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06001582 crq.request_capability.number = cpu_to_be64(adapter->req_tx_queues);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001583 ibmvnic_send_crq(adapter, &crq);
1584
1585 crq.request_capability.capability = cpu_to_be16(REQ_RX_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06001586 crq.request_capability.number = cpu_to_be64(adapter->req_rx_queues);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001587 ibmvnic_send_crq(adapter, &crq);
1588
1589 crq.request_capability.capability = cpu_to_be16(REQ_RX_ADD_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06001590 crq.request_capability.number = cpu_to_be64(adapter->req_rx_add_queues);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001591 ibmvnic_send_crq(adapter, &crq);
1592
1593 crq.request_capability.capability =
1594 cpu_to_be16(REQ_TX_ENTRIES_PER_SUBCRQ);
1595 crq.request_capability.number =
Thomas Falconde89e852016-03-01 10:20:09 -06001596 cpu_to_be64(adapter->req_tx_entries_per_subcrq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001597 ibmvnic_send_crq(adapter, &crq);
1598
1599 crq.request_capability.capability =
1600 cpu_to_be16(REQ_RX_ADD_ENTRIES_PER_SUBCRQ);
1601 crq.request_capability.number =
Thomas Falconde89e852016-03-01 10:20:09 -06001602 cpu_to_be64(adapter->req_rx_add_entries_per_subcrq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001603 ibmvnic_send_crq(adapter, &crq);
1604
1605 crq.request_capability.capability = cpu_to_be16(REQ_MTU);
Thomas Falconde89e852016-03-01 10:20:09 -06001606 crq.request_capability.number = cpu_to_be64(adapter->req_mtu);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001607 ibmvnic_send_crq(adapter, &crq);
1608
1609 if (adapter->netdev->flags & IFF_PROMISC) {
1610 if (adapter->promisc_supported) {
1611 crq.request_capability.capability =
1612 cpu_to_be16(PROMISC_REQUESTED);
Thomas Falconde89e852016-03-01 10:20:09 -06001613 crq.request_capability.number = cpu_to_be64(1);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001614 ibmvnic_send_crq(adapter, &crq);
1615 }
1616 } else {
1617 crq.request_capability.capability =
1618 cpu_to_be16(PROMISC_REQUESTED);
Thomas Falconde89e852016-03-01 10:20:09 -06001619 crq.request_capability.number = cpu_to_be64(0);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001620 ibmvnic_send_crq(adapter, &crq);
1621 }
1622
1623 kfree(allqueues);
1624
1625 return;
1626
Thomas Falcon032c5e82015-12-21 11:26:06 -06001627rx_failed:
1628 kfree(adapter->tx_scrq);
1629 adapter->tx_scrq = NULL;
1630tx_failed:
1631 for (i = 0; i < registered_queues; i++)
1632 release_sub_crq_queue(adapter, allqueues[i]);
1633 kfree(allqueues);
1634allqueues_failed:
1635 ibmvnic_remove(adapter->vdev);
1636}
1637
1638static int pending_scrq(struct ibmvnic_adapter *adapter,
1639 struct ibmvnic_sub_crq_queue *scrq)
1640{
1641 union sub_crq *entry = &scrq->msgs[scrq->cur];
1642
1643 if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP || adapter->closing)
1644 return 1;
1645 else
1646 return 0;
1647}
1648
1649static union sub_crq *ibmvnic_next_scrq(struct ibmvnic_adapter *adapter,
1650 struct ibmvnic_sub_crq_queue *scrq)
1651{
1652 union sub_crq *entry;
1653 unsigned long flags;
1654
1655 spin_lock_irqsave(&scrq->lock, flags);
1656 entry = &scrq->msgs[scrq->cur];
1657 if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP) {
1658 if (++scrq->cur == scrq->size)
1659 scrq->cur = 0;
1660 } else {
1661 entry = NULL;
1662 }
1663 spin_unlock_irqrestore(&scrq->lock, flags);
1664
1665 return entry;
1666}
1667
1668static union ibmvnic_crq *ibmvnic_next_crq(struct ibmvnic_adapter *adapter)
1669{
1670 struct ibmvnic_crq_queue *queue = &adapter->crq;
1671 union ibmvnic_crq *crq;
1672
1673 crq = &queue->msgs[queue->cur];
1674 if (crq->generic.first & IBMVNIC_CRQ_CMD_RSP) {
1675 if (++queue->cur == queue->size)
1676 queue->cur = 0;
1677 } else {
1678 crq = NULL;
1679 }
1680
1681 return crq;
1682}
1683
1684static int send_subcrq(struct ibmvnic_adapter *adapter, u64 remote_handle,
1685 union sub_crq *sub_crq)
1686{
1687 unsigned int ua = adapter->vdev->unit_address;
1688 struct device *dev = &adapter->vdev->dev;
1689 u64 *u64_crq = (u64 *)sub_crq;
1690 int rc;
1691
1692 netdev_dbg(adapter->netdev,
1693 "Sending sCRQ %016lx: %016lx %016lx %016lx %016lx\n",
1694 (unsigned long int)cpu_to_be64(remote_handle),
1695 (unsigned long int)cpu_to_be64(u64_crq[0]),
1696 (unsigned long int)cpu_to_be64(u64_crq[1]),
1697 (unsigned long int)cpu_to_be64(u64_crq[2]),
1698 (unsigned long int)cpu_to_be64(u64_crq[3]));
1699
1700 /* Make sure the hypervisor sees the complete request */
1701 mb();
1702
1703 rc = plpar_hcall_norets(H_SEND_SUB_CRQ, ua,
1704 cpu_to_be64(remote_handle),
1705 cpu_to_be64(u64_crq[0]),
1706 cpu_to_be64(u64_crq[1]),
1707 cpu_to_be64(u64_crq[2]),
1708 cpu_to_be64(u64_crq[3]));
1709
1710 if (rc) {
1711 if (rc == H_CLOSED)
1712 dev_warn(dev, "CRQ Queue closed\n");
1713 dev_err(dev, "Send error (rc=%d)\n", rc);
1714 }
1715
1716 return rc;
1717}
1718
Thomas Falconad7775d2016-04-01 17:20:34 -05001719static int send_subcrq_indirect(struct ibmvnic_adapter *adapter,
1720 u64 remote_handle, u64 ioba, u64 num_entries)
1721{
1722 unsigned int ua = adapter->vdev->unit_address;
1723 struct device *dev = &adapter->vdev->dev;
1724 int rc;
1725
1726 /* Make sure the hypervisor sees the complete request */
1727 mb();
1728 rc = plpar_hcall_norets(H_SEND_SUB_CRQ_INDIRECT, ua,
1729 cpu_to_be64(remote_handle),
1730 ioba, num_entries);
1731
1732 if (rc) {
1733 if (rc == H_CLOSED)
1734 dev_warn(dev, "CRQ Queue closed\n");
1735 dev_err(dev, "Send (indirect) error (rc=%d)\n", rc);
1736 }
1737
1738 return rc;
1739}
1740
Thomas Falcon032c5e82015-12-21 11:26:06 -06001741static int ibmvnic_send_crq(struct ibmvnic_adapter *adapter,
1742 union ibmvnic_crq *crq)
1743{
1744 unsigned int ua = adapter->vdev->unit_address;
1745 struct device *dev = &adapter->vdev->dev;
1746 u64 *u64_crq = (u64 *)crq;
1747 int rc;
1748
1749 netdev_dbg(adapter->netdev, "Sending CRQ: %016lx %016lx\n",
1750 (unsigned long int)cpu_to_be64(u64_crq[0]),
1751 (unsigned long int)cpu_to_be64(u64_crq[1]));
1752
1753 /* Make sure the hypervisor sees the complete request */
1754 mb();
1755
1756 rc = plpar_hcall_norets(H_SEND_CRQ, ua,
1757 cpu_to_be64(u64_crq[0]),
1758 cpu_to_be64(u64_crq[1]));
1759
1760 if (rc) {
1761 if (rc == H_CLOSED)
1762 dev_warn(dev, "CRQ Queue closed\n");
1763 dev_warn(dev, "Send error (rc=%d)\n", rc);
1764 }
1765
1766 return rc;
1767}
1768
1769static int ibmvnic_send_crq_init(struct ibmvnic_adapter *adapter)
1770{
1771 union ibmvnic_crq crq;
1772
1773 memset(&crq, 0, sizeof(crq));
1774 crq.generic.first = IBMVNIC_CRQ_INIT_CMD;
1775 crq.generic.cmd = IBMVNIC_CRQ_INIT;
1776 netdev_dbg(adapter->netdev, "Sending CRQ init\n");
1777
1778 return ibmvnic_send_crq(adapter, &crq);
1779}
1780
1781static int ibmvnic_send_crq_init_complete(struct ibmvnic_adapter *adapter)
1782{
1783 union ibmvnic_crq crq;
1784
1785 memset(&crq, 0, sizeof(crq));
1786 crq.generic.first = IBMVNIC_CRQ_INIT_CMD;
1787 crq.generic.cmd = IBMVNIC_CRQ_INIT_COMPLETE;
1788 netdev_dbg(adapter->netdev, "Sending CRQ init complete\n");
1789
1790 return ibmvnic_send_crq(adapter, &crq);
1791}
1792
1793static int send_version_xchg(struct ibmvnic_adapter *adapter)
1794{
1795 union ibmvnic_crq crq;
1796
1797 memset(&crq, 0, sizeof(crq));
1798 crq.version_exchange.first = IBMVNIC_CRQ_CMD;
1799 crq.version_exchange.cmd = VERSION_EXCHANGE;
1800 crq.version_exchange.version = cpu_to_be16(ibmvnic_version);
1801
1802 return ibmvnic_send_crq(adapter, &crq);
1803}
1804
1805static void send_login(struct ibmvnic_adapter *adapter)
1806{
1807 struct ibmvnic_login_rsp_buffer *login_rsp_buffer;
1808 struct ibmvnic_login_buffer *login_buffer;
1809 struct ibmvnic_inflight_cmd *inflight_cmd;
1810 struct device *dev = &adapter->vdev->dev;
1811 dma_addr_t rsp_buffer_token;
1812 dma_addr_t buffer_token;
1813 size_t rsp_buffer_size;
1814 union ibmvnic_crq crq;
1815 unsigned long flags;
1816 size_t buffer_size;
1817 __be64 *tx_list_p;
1818 __be64 *rx_list_p;
1819 int i;
1820
1821 buffer_size =
1822 sizeof(struct ibmvnic_login_buffer) +
1823 sizeof(u64) * (adapter->req_tx_queues + adapter->req_rx_queues);
1824
1825 login_buffer = kmalloc(buffer_size, GFP_ATOMIC);
1826 if (!login_buffer)
1827 goto buf_alloc_failed;
1828
1829 buffer_token = dma_map_single(dev, login_buffer, buffer_size,
1830 DMA_TO_DEVICE);
1831 if (dma_mapping_error(dev, buffer_token)) {
1832 dev_err(dev, "Couldn't map login buffer\n");
1833 goto buf_map_failed;
1834 }
1835
John Allen498cd8e2016-04-06 11:49:55 -05001836 rsp_buffer_size = sizeof(struct ibmvnic_login_rsp_buffer) +
1837 sizeof(u64) * adapter->req_tx_queues +
1838 sizeof(u64) * adapter->req_rx_queues +
1839 sizeof(u64) * adapter->req_rx_queues +
1840 sizeof(u8) * IBMVNIC_TX_DESC_VERSIONS;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001841
1842 login_rsp_buffer = kmalloc(rsp_buffer_size, GFP_ATOMIC);
1843 if (!login_rsp_buffer)
1844 goto buf_rsp_alloc_failed;
1845
1846 rsp_buffer_token = dma_map_single(dev, login_rsp_buffer,
1847 rsp_buffer_size, DMA_FROM_DEVICE);
1848 if (dma_mapping_error(dev, rsp_buffer_token)) {
1849 dev_err(dev, "Couldn't map login rsp buffer\n");
1850 goto buf_rsp_map_failed;
1851 }
1852 inflight_cmd = kmalloc(sizeof(*inflight_cmd), GFP_ATOMIC);
1853 if (!inflight_cmd) {
1854 dev_err(dev, "Couldn't allocate inflight_cmd\n");
1855 goto inflight_alloc_failed;
1856 }
1857 adapter->login_buf = login_buffer;
1858 adapter->login_buf_token = buffer_token;
1859 adapter->login_buf_sz = buffer_size;
1860 adapter->login_rsp_buf = login_rsp_buffer;
1861 adapter->login_rsp_buf_token = rsp_buffer_token;
1862 adapter->login_rsp_buf_sz = rsp_buffer_size;
1863
1864 login_buffer->len = cpu_to_be32(buffer_size);
1865 login_buffer->version = cpu_to_be32(INITIAL_VERSION_LB);
1866 login_buffer->num_txcomp_subcrqs = cpu_to_be32(adapter->req_tx_queues);
1867 login_buffer->off_txcomp_subcrqs =
1868 cpu_to_be32(sizeof(struct ibmvnic_login_buffer));
1869 login_buffer->num_rxcomp_subcrqs = cpu_to_be32(adapter->req_rx_queues);
1870 login_buffer->off_rxcomp_subcrqs =
1871 cpu_to_be32(sizeof(struct ibmvnic_login_buffer) +
1872 sizeof(u64) * adapter->req_tx_queues);
1873 login_buffer->login_rsp_ioba = cpu_to_be32(rsp_buffer_token);
1874 login_buffer->login_rsp_len = cpu_to_be32(rsp_buffer_size);
1875
1876 tx_list_p = (__be64 *)((char *)login_buffer +
1877 sizeof(struct ibmvnic_login_buffer));
1878 rx_list_p = (__be64 *)((char *)login_buffer +
1879 sizeof(struct ibmvnic_login_buffer) +
1880 sizeof(u64) * adapter->req_tx_queues);
1881
1882 for (i = 0; i < adapter->req_tx_queues; i++) {
1883 if (adapter->tx_scrq[i]) {
1884 tx_list_p[i] = cpu_to_be64(adapter->tx_scrq[i]->
1885 crq_num);
1886 }
1887 }
1888
1889 for (i = 0; i < adapter->req_rx_queues; i++) {
1890 if (adapter->rx_scrq[i]) {
1891 rx_list_p[i] = cpu_to_be64(adapter->rx_scrq[i]->
1892 crq_num);
1893 }
1894 }
1895
1896 netdev_dbg(adapter->netdev, "Login Buffer:\n");
1897 for (i = 0; i < (adapter->login_buf_sz - 1) / 8 + 1; i++) {
1898 netdev_dbg(adapter->netdev, "%016lx\n",
1899 ((unsigned long int *)(adapter->login_buf))[i]);
1900 }
1901
1902 memset(&crq, 0, sizeof(crq));
1903 crq.login.first = IBMVNIC_CRQ_CMD;
1904 crq.login.cmd = LOGIN;
1905 crq.login.ioba = cpu_to_be32(buffer_token);
1906 crq.login.len = cpu_to_be32(buffer_size);
1907
1908 memcpy(&inflight_cmd->crq, &crq, sizeof(crq));
1909
1910 spin_lock_irqsave(&adapter->inflight_lock, flags);
1911 list_add_tail(&inflight_cmd->list, &adapter->inflight);
1912 spin_unlock_irqrestore(&adapter->inflight_lock, flags);
1913
1914 ibmvnic_send_crq(adapter, &crq);
1915
1916 return;
1917
1918inflight_alloc_failed:
1919 dma_unmap_single(dev, rsp_buffer_token, rsp_buffer_size,
1920 DMA_FROM_DEVICE);
1921buf_rsp_map_failed:
1922 kfree(login_rsp_buffer);
1923buf_rsp_alloc_failed:
1924 dma_unmap_single(dev, buffer_token, buffer_size, DMA_TO_DEVICE);
1925buf_map_failed:
1926 kfree(login_buffer);
1927buf_alloc_failed:
1928 return;
1929}
1930
1931static void send_request_map(struct ibmvnic_adapter *adapter, dma_addr_t addr,
1932 u32 len, u8 map_id)
1933{
1934 union ibmvnic_crq crq;
1935
1936 memset(&crq, 0, sizeof(crq));
1937 crq.request_map.first = IBMVNIC_CRQ_CMD;
1938 crq.request_map.cmd = REQUEST_MAP;
1939 crq.request_map.map_id = map_id;
1940 crq.request_map.ioba = cpu_to_be32(addr);
1941 crq.request_map.len = cpu_to_be32(len);
1942 ibmvnic_send_crq(adapter, &crq);
1943}
1944
1945static void send_request_unmap(struct ibmvnic_adapter *adapter, u8 map_id)
1946{
1947 union ibmvnic_crq crq;
1948
1949 memset(&crq, 0, sizeof(crq));
1950 crq.request_unmap.first = IBMVNIC_CRQ_CMD;
1951 crq.request_unmap.cmd = REQUEST_UNMAP;
1952 crq.request_unmap.map_id = map_id;
1953 ibmvnic_send_crq(adapter, &crq);
1954}
1955
1956static void send_map_query(struct ibmvnic_adapter *adapter)
1957{
1958 union ibmvnic_crq crq;
1959
1960 memset(&crq, 0, sizeof(crq));
1961 crq.query_map.first = IBMVNIC_CRQ_CMD;
1962 crq.query_map.cmd = QUERY_MAP;
1963 ibmvnic_send_crq(adapter, &crq);
1964}
1965
1966/* Send a series of CRQs requesting various capabilities of the VNIC server */
1967static void send_cap_queries(struct ibmvnic_adapter *adapter)
1968{
1969 union ibmvnic_crq crq;
1970
1971 atomic_set(&adapter->running_cap_queries, 0);
1972 memset(&crq, 0, sizeof(crq));
1973 crq.query_capability.first = IBMVNIC_CRQ_CMD;
1974 crq.query_capability.cmd = QUERY_CAPABILITY;
1975
1976 crq.query_capability.capability = cpu_to_be16(MIN_TX_QUEUES);
1977 atomic_inc(&adapter->running_cap_queries);
1978 ibmvnic_send_crq(adapter, &crq);
1979
1980 crq.query_capability.capability = cpu_to_be16(MIN_RX_QUEUES);
1981 atomic_inc(&adapter->running_cap_queries);
1982 ibmvnic_send_crq(adapter, &crq);
1983
1984 crq.query_capability.capability = cpu_to_be16(MIN_RX_ADD_QUEUES);
1985 atomic_inc(&adapter->running_cap_queries);
1986 ibmvnic_send_crq(adapter, &crq);
1987
1988 crq.query_capability.capability = cpu_to_be16(MAX_TX_QUEUES);
1989 atomic_inc(&adapter->running_cap_queries);
1990 ibmvnic_send_crq(adapter, &crq);
1991
1992 crq.query_capability.capability = cpu_to_be16(MAX_RX_QUEUES);
1993 atomic_inc(&adapter->running_cap_queries);
1994 ibmvnic_send_crq(adapter, &crq);
1995
1996 crq.query_capability.capability = cpu_to_be16(MAX_RX_ADD_QUEUES);
1997 atomic_inc(&adapter->running_cap_queries);
1998 ibmvnic_send_crq(adapter, &crq);
1999
2000 crq.query_capability.capability =
2001 cpu_to_be16(MIN_TX_ENTRIES_PER_SUBCRQ);
2002 atomic_inc(&adapter->running_cap_queries);
2003 ibmvnic_send_crq(adapter, &crq);
2004
2005 crq.query_capability.capability =
2006 cpu_to_be16(MIN_RX_ADD_ENTRIES_PER_SUBCRQ);
2007 atomic_inc(&adapter->running_cap_queries);
2008 ibmvnic_send_crq(adapter, &crq);
2009
2010 crq.query_capability.capability =
2011 cpu_to_be16(MAX_TX_ENTRIES_PER_SUBCRQ);
2012 atomic_inc(&adapter->running_cap_queries);
2013 ibmvnic_send_crq(adapter, &crq);
2014
2015 crq.query_capability.capability =
2016 cpu_to_be16(MAX_RX_ADD_ENTRIES_PER_SUBCRQ);
2017 atomic_inc(&adapter->running_cap_queries);
2018 ibmvnic_send_crq(adapter, &crq);
2019
2020 crq.query_capability.capability = cpu_to_be16(TCP_IP_OFFLOAD);
2021 atomic_inc(&adapter->running_cap_queries);
2022 ibmvnic_send_crq(adapter, &crq);
2023
2024 crq.query_capability.capability = cpu_to_be16(PROMISC_SUPPORTED);
2025 atomic_inc(&adapter->running_cap_queries);
2026 ibmvnic_send_crq(adapter, &crq);
2027
2028 crq.query_capability.capability = cpu_to_be16(MIN_MTU);
2029 atomic_inc(&adapter->running_cap_queries);
2030 ibmvnic_send_crq(adapter, &crq);
2031
2032 crq.query_capability.capability = cpu_to_be16(MAX_MTU);
2033 atomic_inc(&adapter->running_cap_queries);
2034 ibmvnic_send_crq(adapter, &crq);
2035
2036 crq.query_capability.capability = cpu_to_be16(MAX_MULTICAST_FILTERS);
2037 atomic_inc(&adapter->running_cap_queries);
2038 ibmvnic_send_crq(adapter, &crq);
2039
2040 crq.query_capability.capability = cpu_to_be16(VLAN_HEADER_INSERTION);
2041 atomic_inc(&adapter->running_cap_queries);
2042 ibmvnic_send_crq(adapter, &crq);
2043
2044 crq.query_capability.capability = cpu_to_be16(MAX_TX_SG_ENTRIES);
2045 atomic_inc(&adapter->running_cap_queries);
2046 ibmvnic_send_crq(adapter, &crq);
2047
2048 crq.query_capability.capability = cpu_to_be16(RX_SG_SUPPORTED);
2049 atomic_inc(&adapter->running_cap_queries);
2050 ibmvnic_send_crq(adapter, &crq);
2051
2052 crq.query_capability.capability = cpu_to_be16(OPT_TX_COMP_SUB_QUEUES);
2053 atomic_inc(&adapter->running_cap_queries);
2054 ibmvnic_send_crq(adapter, &crq);
2055
2056 crq.query_capability.capability = cpu_to_be16(OPT_RX_COMP_QUEUES);
2057 atomic_inc(&adapter->running_cap_queries);
2058 ibmvnic_send_crq(adapter, &crq);
2059
2060 crq.query_capability.capability =
2061 cpu_to_be16(OPT_RX_BUFADD_Q_PER_RX_COMP_Q);
2062 atomic_inc(&adapter->running_cap_queries);
2063 ibmvnic_send_crq(adapter, &crq);
2064
2065 crq.query_capability.capability =
2066 cpu_to_be16(OPT_TX_ENTRIES_PER_SUBCRQ);
2067 atomic_inc(&adapter->running_cap_queries);
2068 ibmvnic_send_crq(adapter, &crq);
2069
2070 crq.query_capability.capability =
2071 cpu_to_be16(OPT_RXBA_ENTRIES_PER_SUBCRQ);
2072 atomic_inc(&adapter->running_cap_queries);
2073 ibmvnic_send_crq(adapter, &crq);
2074
2075 crq.query_capability.capability = cpu_to_be16(TX_RX_DESC_REQ);
2076 atomic_inc(&adapter->running_cap_queries);
2077 ibmvnic_send_crq(adapter, &crq);
2078}
2079
2080static void handle_query_ip_offload_rsp(struct ibmvnic_adapter *adapter)
2081{
2082 struct device *dev = &adapter->vdev->dev;
2083 struct ibmvnic_query_ip_offload_buffer *buf = &adapter->ip_offload_buf;
2084 union ibmvnic_crq crq;
2085 int i;
2086
2087 dma_unmap_single(dev, adapter->ip_offload_tok,
2088 sizeof(adapter->ip_offload_buf), DMA_FROM_DEVICE);
2089
2090 netdev_dbg(adapter->netdev, "Query IP Offload Buffer:\n");
2091 for (i = 0; i < (sizeof(adapter->ip_offload_buf) - 1) / 8 + 1; i++)
2092 netdev_dbg(adapter->netdev, "%016lx\n",
2093 ((unsigned long int *)(buf))[i]);
2094
2095 netdev_dbg(adapter->netdev, "ipv4_chksum = %d\n", buf->ipv4_chksum);
2096 netdev_dbg(adapter->netdev, "ipv6_chksum = %d\n", buf->ipv6_chksum);
2097 netdev_dbg(adapter->netdev, "tcp_ipv4_chksum = %d\n",
2098 buf->tcp_ipv4_chksum);
2099 netdev_dbg(adapter->netdev, "tcp_ipv6_chksum = %d\n",
2100 buf->tcp_ipv6_chksum);
2101 netdev_dbg(adapter->netdev, "udp_ipv4_chksum = %d\n",
2102 buf->udp_ipv4_chksum);
2103 netdev_dbg(adapter->netdev, "udp_ipv6_chksum = %d\n",
2104 buf->udp_ipv6_chksum);
2105 netdev_dbg(adapter->netdev, "large_tx_ipv4 = %d\n",
2106 buf->large_tx_ipv4);
2107 netdev_dbg(adapter->netdev, "large_tx_ipv6 = %d\n",
2108 buf->large_tx_ipv6);
2109 netdev_dbg(adapter->netdev, "large_rx_ipv4 = %d\n",
2110 buf->large_rx_ipv4);
2111 netdev_dbg(adapter->netdev, "large_rx_ipv6 = %d\n",
2112 buf->large_rx_ipv6);
2113 netdev_dbg(adapter->netdev, "max_ipv4_hdr_sz = %d\n",
2114 buf->max_ipv4_header_size);
2115 netdev_dbg(adapter->netdev, "max_ipv6_hdr_sz = %d\n",
2116 buf->max_ipv6_header_size);
2117 netdev_dbg(adapter->netdev, "max_tcp_hdr_size = %d\n",
2118 buf->max_tcp_header_size);
2119 netdev_dbg(adapter->netdev, "max_udp_hdr_size = %d\n",
2120 buf->max_udp_header_size);
2121 netdev_dbg(adapter->netdev, "max_large_tx_size = %d\n",
2122 buf->max_large_tx_size);
2123 netdev_dbg(adapter->netdev, "max_large_rx_size = %d\n",
2124 buf->max_large_rx_size);
2125 netdev_dbg(adapter->netdev, "ipv6_ext_hdr = %d\n",
2126 buf->ipv6_extension_header);
2127 netdev_dbg(adapter->netdev, "tcp_pseudosum_req = %d\n",
2128 buf->tcp_pseudosum_req);
2129 netdev_dbg(adapter->netdev, "num_ipv6_ext_hd = %d\n",
2130 buf->num_ipv6_ext_headers);
2131 netdev_dbg(adapter->netdev, "off_ipv6_ext_hd = %d\n",
2132 buf->off_ipv6_ext_headers);
2133
2134 adapter->ip_offload_ctrl_tok =
2135 dma_map_single(dev, &adapter->ip_offload_ctrl,
2136 sizeof(adapter->ip_offload_ctrl), DMA_TO_DEVICE);
2137
2138 if (dma_mapping_error(dev, adapter->ip_offload_ctrl_tok)) {
2139 dev_err(dev, "Couldn't map ip offload control buffer\n");
2140 return;
2141 }
2142
2143 adapter->ip_offload_ctrl.version = cpu_to_be32(INITIAL_VERSION_IOB);
2144 adapter->ip_offload_ctrl.tcp_ipv4_chksum = buf->tcp_ipv4_chksum;
2145 adapter->ip_offload_ctrl.udp_ipv4_chksum = buf->udp_ipv4_chksum;
2146 adapter->ip_offload_ctrl.tcp_ipv6_chksum = buf->tcp_ipv6_chksum;
2147 adapter->ip_offload_ctrl.udp_ipv6_chksum = buf->udp_ipv6_chksum;
2148
2149 /* large_tx/rx disabled for now, additional features needed */
2150 adapter->ip_offload_ctrl.large_tx_ipv4 = 0;
2151 adapter->ip_offload_ctrl.large_tx_ipv6 = 0;
2152 adapter->ip_offload_ctrl.large_rx_ipv4 = 0;
2153 adapter->ip_offload_ctrl.large_rx_ipv6 = 0;
2154
2155 adapter->netdev->features = NETIF_F_GSO;
2156
2157 if (buf->tcp_ipv4_chksum || buf->udp_ipv4_chksum)
2158 adapter->netdev->features |= NETIF_F_IP_CSUM;
2159
2160 if (buf->tcp_ipv6_chksum || buf->udp_ipv6_chksum)
2161 adapter->netdev->features |= NETIF_F_IPV6_CSUM;
2162
Thomas Falcon9be02cd2016-04-01 17:20:35 -05002163 if ((adapter->netdev->features &
2164 (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)))
2165 adapter->netdev->features |= NETIF_F_RXCSUM;
2166
Thomas Falcon032c5e82015-12-21 11:26:06 -06002167 memset(&crq, 0, sizeof(crq));
2168 crq.control_ip_offload.first = IBMVNIC_CRQ_CMD;
2169 crq.control_ip_offload.cmd = CONTROL_IP_OFFLOAD;
2170 crq.control_ip_offload.len =
2171 cpu_to_be32(sizeof(adapter->ip_offload_ctrl));
2172 crq.control_ip_offload.ioba = cpu_to_be32(adapter->ip_offload_ctrl_tok);
2173 ibmvnic_send_crq(adapter, &crq);
2174}
2175
2176static void handle_error_info_rsp(union ibmvnic_crq *crq,
2177 struct ibmvnic_adapter *adapter)
2178{
2179 struct device *dev = &adapter->vdev->dev;
Wei Yongjun96183182016-06-27 20:48:53 +08002180 struct ibmvnic_error_buff *error_buff, *tmp;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002181 unsigned long flags;
2182 bool found = false;
2183 int i;
2184
2185 if (!crq->request_error_rsp.rc.code) {
2186 dev_info(dev, "Request Error Rsp returned with rc=%x\n",
2187 crq->request_error_rsp.rc.code);
2188 return;
2189 }
2190
2191 spin_lock_irqsave(&adapter->error_list_lock, flags);
Wei Yongjun96183182016-06-27 20:48:53 +08002192 list_for_each_entry_safe(error_buff, tmp, &adapter->errors, list)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002193 if (error_buff->error_id == crq->request_error_rsp.error_id) {
2194 found = true;
2195 list_del(&error_buff->list);
2196 break;
2197 }
2198 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
2199
2200 if (!found) {
2201 dev_err(dev, "Couldn't find error id %x\n",
2202 crq->request_error_rsp.error_id);
2203 return;
2204 }
2205
2206 dev_err(dev, "Detailed info for error id %x:",
2207 crq->request_error_rsp.error_id);
2208
2209 for (i = 0; i < error_buff->len; i++) {
2210 pr_cont("%02x", (int)error_buff->buff[i]);
2211 if (i % 8 == 7)
2212 pr_cont(" ");
2213 }
2214 pr_cont("\n");
2215
2216 dma_unmap_single(dev, error_buff->dma, error_buff->len,
2217 DMA_FROM_DEVICE);
2218 kfree(error_buff->buff);
2219 kfree(error_buff);
2220}
2221
2222static void handle_dump_size_rsp(union ibmvnic_crq *crq,
2223 struct ibmvnic_adapter *adapter)
2224{
2225 int len = be32_to_cpu(crq->request_dump_size_rsp.len);
2226 struct ibmvnic_inflight_cmd *inflight_cmd;
2227 struct device *dev = &adapter->vdev->dev;
2228 union ibmvnic_crq newcrq;
2229 unsigned long flags;
2230
2231 /* allocate and map buffer */
2232 adapter->dump_data = kmalloc(len, GFP_KERNEL);
2233 if (!adapter->dump_data) {
2234 complete(&adapter->fw_done);
2235 return;
2236 }
2237
2238 adapter->dump_data_token = dma_map_single(dev, adapter->dump_data, len,
2239 DMA_FROM_DEVICE);
2240
2241 if (dma_mapping_error(dev, adapter->dump_data_token)) {
2242 if (!firmware_has_feature(FW_FEATURE_CMO))
2243 dev_err(dev, "Couldn't map dump data\n");
2244 kfree(adapter->dump_data);
2245 complete(&adapter->fw_done);
2246 return;
2247 }
2248
2249 inflight_cmd = kmalloc(sizeof(*inflight_cmd), GFP_ATOMIC);
2250 if (!inflight_cmd) {
2251 dma_unmap_single(dev, adapter->dump_data_token, len,
2252 DMA_FROM_DEVICE);
2253 kfree(adapter->dump_data);
2254 complete(&adapter->fw_done);
2255 return;
2256 }
2257
2258 memset(&newcrq, 0, sizeof(newcrq));
2259 newcrq.request_dump.first = IBMVNIC_CRQ_CMD;
2260 newcrq.request_dump.cmd = REQUEST_DUMP;
2261 newcrq.request_dump.ioba = cpu_to_be32(adapter->dump_data_token);
2262 newcrq.request_dump.len = cpu_to_be32(adapter->dump_data_size);
2263
2264 memcpy(&inflight_cmd->crq, &newcrq, sizeof(newcrq));
2265
2266 spin_lock_irqsave(&adapter->inflight_lock, flags);
2267 list_add_tail(&inflight_cmd->list, &adapter->inflight);
2268 spin_unlock_irqrestore(&adapter->inflight_lock, flags);
2269
2270 ibmvnic_send_crq(adapter, &newcrq);
2271}
2272
2273static void handle_error_indication(union ibmvnic_crq *crq,
2274 struct ibmvnic_adapter *adapter)
2275{
2276 int detail_len = be32_to_cpu(crq->error_indication.detail_error_sz);
2277 struct ibmvnic_inflight_cmd *inflight_cmd;
2278 struct device *dev = &adapter->vdev->dev;
2279 struct ibmvnic_error_buff *error_buff;
2280 union ibmvnic_crq new_crq;
2281 unsigned long flags;
2282
2283 dev_err(dev, "Firmware reports %serror id %x, cause %d\n",
2284 crq->error_indication.
2285 flags & IBMVNIC_FATAL_ERROR ? "FATAL " : "",
2286 crq->error_indication.error_id,
2287 crq->error_indication.error_cause);
2288
2289 error_buff = kmalloc(sizeof(*error_buff), GFP_ATOMIC);
2290 if (!error_buff)
2291 return;
2292
2293 error_buff->buff = kmalloc(detail_len, GFP_ATOMIC);
2294 if (!error_buff->buff) {
2295 kfree(error_buff);
2296 return;
2297 }
2298
2299 error_buff->dma = dma_map_single(dev, error_buff->buff, detail_len,
2300 DMA_FROM_DEVICE);
2301 if (dma_mapping_error(dev, error_buff->dma)) {
2302 if (!firmware_has_feature(FW_FEATURE_CMO))
2303 dev_err(dev, "Couldn't map error buffer\n");
2304 kfree(error_buff->buff);
2305 kfree(error_buff);
2306 return;
2307 }
2308
2309 inflight_cmd = kmalloc(sizeof(*inflight_cmd), GFP_ATOMIC);
2310 if (!inflight_cmd) {
2311 dma_unmap_single(dev, error_buff->dma, detail_len,
2312 DMA_FROM_DEVICE);
2313 kfree(error_buff->buff);
2314 kfree(error_buff);
2315 return;
2316 }
2317
2318 error_buff->len = detail_len;
2319 error_buff->error_id = crq->error_indication.error_id;
2320
2321 spin_lock_irqsave(&adapter->error_list_lock, flags);
2322 list_add_tail(&error_buff->list, &adapter->errors);
2323 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
2324
2325 memset(&new_crq, 0, sizeof(new_crq));
2326 new_crq.request_error_info.first = IBMVNIC_CRQ_CMD;
2327 new_crq.request_error_info.cmd = REQUEST_ERROR_INFO;
2328 new_crq.request_error_info.ioba = cpu_to_be32(error_buff->dma);
2329 new_crq.request_error_info.len = cpu_to_be32(detail_len);
2330 new_crq.request_error_info.error_id = crq->error_indication.error_id;
2331
2332 memcpy(&inflight_cmd->crq, &crq, sizeof(crq));
2333
2334 spin_lock_irqsave(&adapter->inflight_lock, flags);
2335 list_add_tail(&inflight_cmd->list, &adapter->inflight);
2336 spin_unlock_irqrestore(&adapter->inflight_lock, flags);
2337
2338 ibmvnic_send_crq(adapter, &new_crq);
2339}
2340
2341static void handle_change_mac_rsp(union ibmvnic_crq *crq,
2342 struct ibmvnic_adapter *adapter)
2343{
2344 struct net_device *netdev = adapter->netdev;
2345 struct device *dev = &adapter->vdev->dev;
2346 long rc;
2347
2348 rc = crq->change_mac_addr_rsp.rc.code;
2349 if (rc) {
2350 dev_err(dev, "Error %ld in CHANGE_MAC_ADDR_RSP\n", rc);
2351 return;
2352 }
2353 memcpy(netdev->dev_addr, &crq->change_mac_addr_rsp.mac_addr[0],
2354 ETH_ALEN);
2355}
2356
2357static void handle_request_cap_rsp(union ibmvnic_crq *crq,
2358 struct ibmvnic_adapter *adapter)
2359{
2360 struct device *dev = &adapter->vdev->dev;
2361 u64 *req_value;
2362 char *name;
2363
2364 switch (be16_to_cpu(crq->request_capability_rsp.capability)) {
2365 case REQ_TX_QUEUES:
2366 req_value = &adapter->req_tx_queues;
2367 name = "tx";
2368 break;
2369 case REQ_RX_QUEUES:
2370 req_value = &adapter->req_rx_queues;
2371 name = "rx";
2372 break;
2373 case REQ_RX_ADD_QUEUES:
2374 req_value = &adapter->req_rx_add_queues;
2375 name = "rx_add";
2376 break;
2377 case REQ_TX_ENTRIES_PER_SUBCRQ:
2378 req_value = &adapter->req_tx_entries_per_subcrq;
2379 name = "tx_entries_per_subcrq";
2380 break;
2381 case REQ_RX_ADD_ENTRIES_PER_SUBCRQ:
2382 req_value = &adapter->req_rx_add_entries_per_subcrq;
2383 name = "rx_add_entries_per_subcrq";
2384 break;
2385 case REQ_MTU:
2386 req_value = &adapter->req_mtu;
2387 name = "mtu";
2388 break;
2389 case PROMISC_REQUESTED:
2390 req_value = &adapter->promisc;
2391 name = "promisc";
2392 break;
2393 default:
2394 dev_err(dev, "Got invalid cap request rsp %d\n",
2395 crq->request_capability.capability);
2396 return;
2397 }
2398
2399 switch (crq->request_capability_rsp.rc.code) {
2400 case SUCCESS:
2401 break;
2402 case PARTIALSUCCESS:
2403 dev_info(dev, "req=%lld, rsp=%ld in %s queue, retrying.\n",
2404 *req_value,
2405 (long int)be32_to_cpu(crq->request_capability_rsp.
2406 number), name);
Thomas Falconea22d512016-07-06 15:35:17 -05002407 release_sub_crqs_no_irqs(adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002408 *req_value = be32_to_cpu(crq->request_capability_rsp.number);
Thomas Falconea22d512016-07-06 15:35:17 -05002409 init_sub_crqs(adapter, 1);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002410 return;
2411 default:
2412 dev_err(dev, "Error %d in request cap rsp\n",
2413 crq->request_capability_rsp.rc.code);
2414 return;
2415 }
2416
2417 /* Done receiving requested capabilities, query IP offload support */
2418 if (++adapter->requested_caps == 7) {
2419 union ibmvnic_crq newcrq;
2420 int buf_sz = sizeof(struct ibmvnic_query_ip_offload_buffer);
2421 struct ibmvnic_query_ip_offload_buffer *ip_offload_buf =
2422 &adapter->ip_offload_buf;
2423
2424 adapter->ip_offload_tok = dma_map_single(dev, ip_offload_buf,
2425 buf_sz,
2426 DMA_FROM_DEVICE);
2427
2428 if (dma_mapping_error(dev, adapter->ip_offload_tok)) {
2429 if (!firmware_has_feature(FW_FEATURE_CMO))
2430 dev_err(dev, "Couldn't map offload buffer\n");
2431 return;
2432 }
2433
2434 memset(&newcrq, 0, sizeof(newcrq));
2435 newcrq.query_ip_offload.first = IBMVNIC_CRQ_CMD;
2436 newcrq.query_ip_offload.cmd = QUERY_IP_OFFLOAD;
2437 newcrq.query_ip_offload.len = cpu_to_be32(buf_sz);
2438 newcrq.query_ip_offload.ioba =
2439 cpu_to_be32(adapter->ip_offload_tok);
2440
2441 ibmvnic_send_crq(adapter, &newcrq);
2442 }
2443}
2444
2445static int handle_login_rsp(union ibmvnic_crq *login_rsp_crq,
2446 struct ibmvnic_adapter *adapter)
2447{
2448 struct device *dev = &adapter->vdev->dev;
2449 struct ibmvnic_login_rsp_buffer *login_rsp = adapter->login_rsp_buf;
2450 struct ibmvnic_login_buffer *login = adapter->login_buf;
2451 union ibmvnic_crq crq;
2452 int i;
2453
2454 dma_unmap_single(dev, adapter->login_buf_token, adapter->login_buf_sz,
2455 DMA_BIDIRECTIONAL);
2456 dma_unmap_single(dev, adapter->login_rsp_buf_token,
2457 adapter->login_rsp_buf_sz, DMA_BIDIRECTIONAL);
2458
John Allen498cd8e2016-04-06 11:49:55 -05002459 /* If the number of queues requested can't be allocated by the
2460 * server, the login response will return with code 1. We will need
2461 * to resend the login buffer with fewer queues requested.
2462 */
2463 if (login_rsp_crq->generic.rc.code) {
2464 adapter->renegotiate = true;
2465 complete(&adapter->init_done);
2466 return 0;
2467 }
2468
Thomas Falcon032c5e82015-12-21 11:26:06 -06002469 netdev_dbg(adapter->netdev, "Login Response Buffer:\n");
2470 for (i = 0; i < (adapter->login_rsp_buf_sz - 1) / 8 + 1; i++) {
2471 netdev_dbg(adapter->netdev, "%016lx\n",
2472 ((unsigned long int *)(adapter->login_rsp_buf))[i]);
2473 }
2474
2475 /* Sanity checks */
2476 if (login->num_txcomp_subcrqs != login_rsp->num_txsubm_subcrqs ||
2477 (be32_to_cpu(login->num_rxcomp_subcrqs) *
2478 adapter->req_rx_add_queues !=
2479 be32_to_cpu(login_rsp->num_rxadd_subcrqs))) {
2480 dev_err(dev, "FATAL: Inconsistent login and login rsp\n");
2481 ibmvnic_remove(adapter->vdev);
2482 return -EIO;
2483 }
2484 complete(&adapter->init_done);
2485
2486 memset(&crq, 0, sizeof(crq));
2487 crq.request_ras_comp_num.first = IBMVNIC_CRQ_CMD;
2488 crq.request_ras_comp_num.cmd = REQUEST_RAS_COMP_NUM;
2489 ibmvnic_send_crq(adapter, &crq);
2490
2491 return 0;
2492}
2493
2494static void handle_request_map_rsp(union ibmvnic_crq *crq,
2495 struct ibmvnic_adapter *adapter)
2496{
2497 struct device *dev = &adapter->vdev->dev;
2498 u8 map_id = crq->request_map_rsp.map_id;
2499 int tx_subcrqs;
2500 int rx_subcrqs;
2501 long rc;
2502 int i;
2503
2504 tx_subcrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
2505 rx_subcrqs = be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
2506
2507 rc = crq->request_map_rsp.rc.code;
2508 if (rc) {
2509 dev_err(dev, "Error %ld in REQUEST_MAP_RSP\n", rc);
2510 adapter->map_id--;
2511 /* need to find and zero tx/rx_pool map_id */
2512 for (i = 0; i < tx_subcrqs; i++) {
2513 if (adapter->tx_pool[i].long_term_buff.map_id == map_id)
2514 adapter->tx_pool[i].long_term_buff.map_id = 0;
2515 }
2516 for (i = 0; i < rx_subcrqs; i++) {
2517 if (adapter->rx_pool[i].long_term_buff.map_id == map_id)
2518 adapter->rx_pool[i].long_term_buff.map_id = 0;
2519 }
2520 }
2521 complete(&adapter->fw_done);
2522}
2523
2524static void handle_request_unmap_rsp(union ibmvnic_crq *crq,
2525 struct ibmvnic_adapter *adapter)
2526{
2527 struct device *dev = &adapter->vdev->dev;
2528 long rc;
2529
2530 rc = crq->request_unmap_rsp.rc.code;
2531 if (rc)
2532 dev_err(dev, "Error %ld in REQUEST_UNMAP_RSP\n", rc);
2533}
2534
2535static void handle_query_map_rsp(union ibmvnic_crq *crq,
2536 struct ibmvnic_adapter *adapter)
2537{
2538 struct net_device *netdev = adapter->netdev;
2539 struct device *dev = &adapter->vdev->dev;
2540 long rc;
2541
2542 rc = crq->query_map_rsp.rc.code;
2543 if (rc) {
2544 dev_err(dev, "Error %ld in QUERY_MAP_RSP\n", rc);
2545 return;
2546 }
2547 netdev_dbg(netdev, "page_size = %d\ntot_pages = %d\nfree_pages = %d\n",
2548 crq->query_map_rsp.page_size, crq->query_map_rsp.tot_pages,
2549 crq->query_map_rsp.free_pages);
2550}
2551
2552static void handle_query_cap_rsp(union ibmvnic_crq *crq,
2553 struct ibmvnic_adapter *adapter)
2554{
2555 struct net_device *netdev = adapter->netdev;
2556 struct device *dev = &adapter->vdev->dev;
2557 long rc;
2558
2559 atomic_dec(&adapter->running_cap_queries);
2560 netdev_dbg(netdev, "Outstanding queries: %d\n",
2561 atomic_read(&adapter->running_cap_queries));
2562 rc = crq->query_capability.rc.code;
2563 if (rc) {
2564 dev_err(dev, "Error %ld in QUERY_CAP_RSP\n", rc);
2565 goto out;
2566 }
2567
2568 switch (be16_to_cpu(crq->query_capability.capability)) {
2569 case MIN_TX_QUEUES:
2570 adapter->min_tx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06002571 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002572 netdev_dbg(netdev, "min_tx_queues = %lld\n",
2573 adapter->min_tx_queues);
2574 break;
2575 case MIN_RX_QUEUES:
2576 adapter->min_rx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06002577 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002578 netdev_dbg(netdev, "min_rx_queues = %lld\n",
2579 adapter->min_rx_queues);
2580 break;
2581 case MIN_RX_ADD_QUEUES:
2582 adapter->min_rx_add_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06002583 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002584 netdev_dbg(netdev, "min_rx_add_queues = %lld\n",
2585 adapter->min_rx_add_queues);
2586 break;
2587 case MAX_TX_QUEUES:
2588 adapter->max_tx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06002589 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002590 netdev_dbg(netdev, "max_tx_queues = %lld\n",
2591 adapter->max_tx_queues);
2592 break;
2593 case MAX_RX_QUEUES:
2594 adapter->max_rx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06002595 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002596 netdev_dbg(netdev, "max_rx_queues = %lld\n",
2597 adapter->max_rx_queues);
2598 break;
2599 case MAX_RX_ADD_QUEUES:
2600 adapter->max_rx_add_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06002601 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002602 netdev_dbg(netdev, "max_rx_add_queues = %lld\n",
2603 adapter->max_rx_add_queues);
2604 break;
2605 case MIN_TX_ENTRIES_PER_SUBCRQ:
2606 adapter->min_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06002607 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002608 netdev_dbg(netdev, "min_tx_entries_per_subcrq = %lld\n",
2609 adapter->min_tx_entries_per_subcrq);
2610 break;
2611 case MIN_RX_ADD_ENTRIES_PER_SUBCRQ:
2612 adapter->min_rx_add_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06002613 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002614 netdev_dbg(netdev, "min_rx_add_entrs_per_subcrq = %lld\n",
2615 adapter->min_rx_add_entries_per_subcrq);
2616 break;
2617 case MAX_TX_ENTRIES_PER_SUBCRQ:
2618 adapter->max_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06002619 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002620 netdev_dbg(netdev, "max_tx_entries_per_subcrq = %lld\n",
2621 adapter->max_tx_entries_per_subcrq);
2622 break;
2623 case MAX_RX_ADD_ENTRIES_PER_SUBCRQ:
2624 adapter->max_rx_add_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06002625 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002626 netdev_dbg(netdev, "max_rx_add_entrs_per_subcrq = %lld\n",
2627 adapter->max_rx_add_entries_per_subcrq);
2628 break;
2629 case TCP_IP_OFFLOAD:
2630 adapter->tcp_ip_offload =
Thomas Falconde89e852016-03-01 10:20:09 -06002631 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002632 netdev_dbg(netdev, "tcp_ip_offload = %lld\n",
2633 adapter->tcp_ip_offload);
2634 break;
2635 case PROMISC_SUPPORTED:
2636 adapter->promisc_supported =
Thomas Falconde89e852016-03-01 10:20:09 -06002637 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002638 netdev_dbg(netdev, "promisc_supported = %lld\n",
2639 adapter->promisc_supported);
2640 break;
2641 case MIN_MTU:
Thomas Falconde89e852016-03-01 10:20:09 -06002642 adapter->min_mtu = be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002643 netdev_dbg(netdev, "min_mtu = %lld\n", adapter->min_mtu);
2644 break;
2645 case MAX_MTU:
Thomas Falconde89e852016-03-01 10:20:09 -06002646 adapter->max_mtu = be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002647 netdev_dbg(netdev, "max_mtu = %lld\n", adapter->max_mtu);
2648 break;
2649 case MAX_MULTICAST_FILTERS:
2650 adapter->max_multicast_filters =
Thomas Falconde89e852016-03-01 10:20:09 -06002651 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002652 netdev_dbg(netdev, "max_multicast_filters = %lld\n",
2653 adapter->max_multicast_filters);
2654 break;
2655 case VLAN_HEADER_INSERTION:
2656 adapter->vlan_header_insertion =
Thomas Falconde89e852016-03-01 10:20:09 -06002657 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002658 if (adapter->vlan_header_insertion)
2659 netdev->features |= NETIF_F_HW_VLAN_STAG_TX;
2660 netdev_dbg(netdev, "vlan_header_insertion = %lld\n",
2661 adapter->vlan_header_insertion);
2662 break;
2663 case MAX_TX_SG_ENTRIES:
2664 adapter->max_tx_sg_entries =
Thomas Falconde89e852016-03-01 10:20:09 -06002665 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002666 netdev_dbg(netdev, "max_tx_sg_entries = %lld\n",
2667 adapter->max_tx_sg_entries);
2668 break;
2669 case RX_SG_SUPPORTED:
2670 adapter->rx_sg_supported =
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, "rx_sg_supported = %lld\n",
2673 adapter->rx_sg_supported);
2674 break;
2675 case OPT_TX_COMP_SUB_QUEUES:
2676 adapter->opt_tx_comp_sub_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_tx_comp_sub_queues = %lld\n",
2679 adapter->opt_tx_comp_sub_queues);
2680 break;
2681 case OPT_RX_COMP_QUEUES:
2682 adapter->opt_rx_comp_queues =
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_comp_queues = %lld\n",
2685 adapter->opt_rx_comp_queues);
2686 break;
2687 case OPT_RX_BUFADD_Q_PER_RX_COMP_Q:
2688 adapter->opt_rx_bufadd_q_per_rx_comp_q =
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_rx_bufadd_q_per_rx_comp_q = %lld\n",
2691 adapter->opt_rx_bufadd_q_per_rx_comp_q);
2692 break;
2693 case OPT_TX_ENTRIES_PER_SUBCRQ:
2694 adapter->opt_tx_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_tx_entries_per_subcrq = %lld\n",
2697 adapter->opt_tx_entries_per_subcrq);
2698 break;
2699 case OPT_RXBA_ENTRIES_PER_SUBCRQ:
2700 adapter->opt_rxba_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06002701 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002702 netdev_dbg(netdev, "opt_rxba_entries_per_subcrq = %lld\n",
2703 adapter->opt_rxba_entries_per_subcrq);
2704 break;
2705 case TX_RX_DESC_REQ:
2706 adapter->tx_rx_desc_req = crq->query_capability.number;
2707 netdev_dbg(netdev, "tx_rx_desc_req = %llx\n",
2708 adapter->tx_rx_desc_req);
2709 break;
2710
2711 default:
2712 netdev_err(netdev, "Got invalid cap rsp %d\n",
2713 crq->query_capability.capability);
2714 }
2715
2716out:
2717 if (atomic_read(&adapter->running_cap_queries) == 0)
Thomas Falconea22d512016-07-06 15:35:17 -05002718 init_sub_crqs(adapter, 0);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002719 /* We're done querying the capabilities, initialize sub-crqs */
2720}
2721
2722static void handle_control_ras_rsp(union ibmvnic_crq *crq,
2723 struct ibmvnic_adapter *adapter)
2724{
2725 u8 correlator = crq->control_ras_rsp.correlator;
2726 struct device *dev = &adapter->vdev->dev;
2727 bool found = false;
2728 int i;
2729
2730 if (crq->control_ras_rsp.rc.code) {
2731 dev_warn(dev, "Control ras failed rc=%d\n",
2732 crq->control_ras_rsp.rc.code);
2733 return;
2734 }
2735
2736 for (i = 0; i < adapter->ras_comp_num; i++) {
2737 if (adapter->ras_comps[i].correlator == correlator) {
2738 found = true;
2739 break;
2740 }
2741 }
2742
2743 if (!found) {
2744 dev_warn(dev, "Correlator not found on control_ras_rsp\n");
2745 return;
2746 }
2747
2748 switch (crq->control_ras_rsp.op) {
2749 case IBMVNIC_TRACE_LEVEL:
2750 adapter->ras_comps[i].trace_level = crq->control_ras.level;
2751 break;
2752 case IBMVNIC_ERROR_LEVEL:
2753 adapter->ras_comps[i].error_check_level =
2754 crq->control_ras.level;
2755 break;
2756 case IBMVNIC_TRACE_PAUSE:
2757 adapter->ras_comp_int[i].paused = 1;
2758 break;
2759 case IBMVNIC_TRACE_RESUME:
2760 adapter->ras_comp_int[i].paused = 0;
2761 break;
2762 case IBMVNIC_TRACE_ON:
2763 adapter->ras_comps[i].trace_on = 1;
2764 break;
2765 case IBMVNIC_TRACE_OFF:
2766 adapter->ras_comps[i].trace_on = 0;
2767 break;
2768 case IBMVNIC_CHG_TRACE_BUFF_SZ:
2769 /* trace_buff_sz is 3 bytes, stuff it into an int */
2770 ((u8 *)(&adapter->ras_comps[i].trace_buff_size))[0] = 0;
2771 ((u8 *)(&adapter->ras_comps[i].trace_buff_size))[1] =
2772 crq->control_ras_rsp.trace_buff_sz[0];
2773 ((u8 *)(&adapter->ras_comps[i].trace_buff_size))[2] =
2774 crq->control_ras_rsp.trace_buff_sz[1];
2775 ((u8 *)(&adapter->ras_comps[i].trace_buff_size))[3] =
2776 crq->control_ras_rsp.trace_buff_sz[2];
2777 break;
2778 default:
2779 dev_err(dev, "invalid op %d on control_ras_rsp",
2780 crq->control_ras_rsp.op);
2781 }
2782}
2783
Thomas Falcon032c5e82015-12-21 11:26:06 -06002784static ssize_t trace_read(struct file *file, char __user *user_buf, size_t len,
2785 loff_t *ppos)
2786{
2787 struct ibmvnic_fw_comp_internal *ras_comp_int = file->private_data;
2788 struct ibmvnic_adapter *adapter = ras_comp_int->adapter;
2789 struct device *dev = &adapter->vdev->dev;
2790 struct ibmvnic_fw_trace_entry *trace;
2791 int num = ras_comp_int->num;
2792 union ibmvnic_crq crq;
2793 dma_addr_t trace_tok;
2794
2795 if (*ppos >= be32_to_cpu(adapter->ras_comps[num].trace_buff_size))
2796 return 0;
2797
2798 trace =
2799 dma_alloc_coherent(dev,
2800 be32_to_cpu(adapter->ras_comps[num].
2801 trace_buff_size), &trace_tok,
2802 GFP_KERNEL);
2803 if (!trace) {
2804 dev_err(dev, "Couldn't alloc trace buffer\n");
2805 return 0;
2806 }
2807
2808 memset(&crq, 0, sizeof(crq));
2809 crq.collect_fw_trace.first = IBMVNIC_CRQ_CMD;
2810 crq.collect_fw_trace.cmd = COLLECT_FW_TRACE;
2811 crq.collect_fw_trace.correlator = adapter->ras_comps[num].correlator;
2812 crq.collect_fw_trace.ioba = cpu_to_be32(trace_tok);
2813 crq.collect_fw_trace.len = adapter->ras_comps[num].trace_buff_size;
2814 ibmvnic_send_crq(adapter, &crq);
2815
2816 init_completion(&adapter->fw_done);
2817 wait_for_completion(&adapter->fw_done);
2818
2819 if (*ppos + len > be32_to_cpu(adapter->ras_comps[num].trace_buff_size))
2820 len =
2821 be32_to_cpu(adapter->ras_comps[num].trace_buff_size) -
2822 *ppos;
2823
2824 copy_to_user(user_buf, &((u8 *)trace)[*ppos], len);
2825
2826 dma_free_coherent(dev,
2827 be32_to_cpu(adapter->ras_comps[num].trace_buff_size),
2828 trace, trace_tok);
2829 *ppos += len;
2830 return len;
2831}
2832
2833static const struct file_operations trace_ops = {
2834 .owner = THIS_MODULE,
Wei Yongjun7a95e942016-08-24 13:50:03 +00002835 .open = simple_open,
Thomas Falcon032c5e82015-12-21 11:26:06 -06002836 .read = trace_read,
2837};
2838
2839static ssize_t paused_read(struct file *file, char __user *user_buf, size_t len,
2840 loff_t *ppos)
2841{
2842 struct ibmvnic_fw_comp_internal *ras_comp_int = file->private_data;
2843 struct ibmvnic_adapter *adapter = ras_comp_int->adapter;
2844 int num = ras_comp_int->num;
2845 char buff[5]; /* 1 or 0 plus \n and \0 */
2846 int size;
2847
2848 size = sprintf(buff, "%d\n", adapter->ras_comp_int[num].paused);
2849
2850 if (*ppos >= size)
2851 return 0;
2852
2853 copy_to_user(user_buf, buff, size);
2854 *ppos += size;
2855 return size;
2856}
2857
2858static ssize_t paused_write(struct file *file, const char __user *user_buf,
2859 size_t len, loff_t *ppos)
2860{
2861 struct ibmvnic_fw_comp_internal *ras_comp_int = file->private_data;
2862 struct ibmvnic_adapter *adapter = ras_comp_int->adapter;
2863 int num = ras_comp_int->num;
2864 union ibmvnic_crq crq;
2865 unsigned long val;
2866 char buff[9]; /* decimal max int plus \n and \0 */
2867
2868 copy_from_user(buff, user_buf, sizeof(buff));
2869 val = kstrtoul(buff, 10, NULL);
2870
2871 adapter->ras_comp_int[num].paused = val ? 1 : 0;
2872
2873 memset(&crq, 0, sizeof(crq));
2874 crq.control_ras.first = IBMVNIC_CRQ_CMD;
2875 crq.control_ras.cmd = CONTROL_RAS;
2876 crq.control_ras.correlator = adapter->ras_comps[num].correlator;
2877 crq.control_ras.op = val ? IBMVNIC_TRACE_PAUSE : IBMVNIC_TRACE_RESUME;
2878 ibmvnic_send_crq(adapter, &crq);
2879
2880 return len;
2881}
2882
2883static const struct file_operations paused_ops = {
2884 .owner = THIS_MODULE,
Wei Yongjun7a95e942016-08-24 13:50:03 +00002885 .open = simple_open,
Thomas Falcon032c5e82015-12-21 11:26:06 -06002886 .read = paused_read,
2887 .write = paused_write,
2888};
2889
2890static ssize_t tracing_read(struct file *file, char __user *user_buf,
2891 size_t len, loff_t *ppos)
2892{
2893 struct ibmvnic_fw_comp_internal *ras_comp_int = file->private_data;
2894 struct ibmvnic_adapter *adapter = ras_comp_int->adapter;
2895 int num = ras_comp_int->num;
2896 char buff[5]; /* 1 or 0 plus \n and \0 */
2897 int size;
2898
2899 size = sprintf(buff, "%d\n", adapter->ras_comps[num].trace_on);
2900
2901 if (*ppos >= size)
2902 return 0;
2903
2904 copy_to_user(user_buf, buff, size);
2905 *ppos += size;
2906 return size;
2907}
2908
2909static ssize_t tracing_write(struct file *file, const char __user *user_buf,
2910 size_t len, loff_t *ppos)
2911{
2912 struct ibmvnic_fw_comp_internal *ras_comp_int = file->private_data;
2913 struct ibmvnic_adapter *adapter = ras_comp_int->adapter;
2914 int num = ras_comp_int->num;
2915 union ibmvnic_crq crq;
2916 unsigned long val;
2917 char buff[9]; /* decimal max int plus \n and \0 */
2918
2919 copy_from_user(buff, user_buf, sizeof(buff));
2920 val = kstrtoul(buff, 10, NULL);
2921
2922 memset(&crq, 0, sizeof(crq));
2923 crq.control_ras.first = IBMVNIC_CRQ_CMD;
2924 crq.control_ras.cmd = CONTROL_RAS;
2925 crq.control_ras.correlator = adapter->ras_comps[num].correlator;
2926 crq.control_ras.op = val ? IBMVNIC_TRACE_ON : IBMVNIC_TRACE_OFF;
2927
2928 return len;
2929}
2930
2931static const struct file_operations tracing_ops = {
2932 .owner = THIS_MODULE,
Wei Yongjun7a95e942016-08-24 13:50:03 +00002933 .open = simple_open,
Thomas Falcon032c5e82015-12-21 11:26:06 -06002934 .read = tracing_read,
2935 .write = tracing_write,
2936};
2937
2938static ssize_t error_level_read(struct file *file, char __user *user_buf,
2939 size_t len, loff_t *ppos)
2940{
2941 struct ibmvnic_fw_comp_internal *ras_comp_int = file->private_data;
2942 struct ibmvnic_adapter *adapter = ras_comp_int->adapter;
2943 int num = ras_comp_int->num;
2944 char buff[5]; /* decimal max char plus \n and \0 */
2945 int size;
2946
2947 size = sprintf(buff, "%d\n", adapter->ras_comps[num].error_check_level);
2948
2949 if (*ppos >= size)
2950 return 0;
2951
2952 copy_to_user(user_buf, buff, size);
2953 *ppos += size;
2954 return size;
2955}
2956
2957static ssize_t error_level_write(struct file *file, const char __user *user_buf,
2958 size_t len, loff_t *ppos)
2959{
2960 struct ibmvnic_fw_comp_internal *ras_comp_int = file->private_data;
2961 struct ibmvnic_adapter *adapter = ras_comp_int->adapter;
2962 int num = ras_comp_int->num;
2963 union ibmvnic_crq crq;
2964 unsigned long val;
2965 char buff[9]; /* decimal max int plus \n and \0 */
2966
2967 copy_from_user(buff, user_buf, sizeof(buff));
2968 val = kstrtoul(buff, 10, NULL);
2969
2970 if (val > 9)
2971 val = 9;
2972
2973 memset(&crq, 0, sizeof(crq));
2974 crq.control_ras.first = IBMVNIC_CRQ_CMD;
2975 crq.control_ras.cmd = CONTROL_RAS;
2976 crq.control_ras.correlator = adapter->ras_comps[num].correlator;
2977 crq.control_ras.op = IBMVNIC_ERROR_LEVEL;
2978 crq.control_ras.level = val;
2979 ibmvnic_send_crq(adapter, &crq);
2980
2981 return len;
2982}
2983
2984static const struct file_operations error_level_ops = {
2985 .owner = THIS_MODULE,
Wei Yongjun7a95e942016-08-24 13:50:03 +00002986 .open = simple_open,
Thomas Falcon032c5e82015-12-21 11:26:06 -06002987 .read = error_level_read,
2988 .write = error_level_write,
2989};
2990
2991static ssize_t trace_level_read(struct file *file, char __user *user_buf,
2992 size_t len, loff_t *ppos)
2993{
2994 struct ibmvnic_fw_comp_internal *ras_comp_int = file->private_data;
2995 struct ibmvnic_adapter *adapter = ras_comp_int->adapter;
2996 int num = ras_comp_int->num;
2997 char buff[5]; /* decimal max char plus \n and \0 */
2998 int size;
2999
3000 size = sprintf(buff, "%d\n", adapter->ras_comps[num].trace_level);
3001 if (*ppos >= size)
3002 return 0;
3003
3004 copy_to_user(user_buf, buff, size);
3005 *ppos += size;
3006 return size;
3007}
3008
3009static ssize_t trace_level_write(struct file *file, const char __user *user_buf,
3010 size_t len, loff_t *ppos)
3011{
3012 struct ibmvnic_fw_comp_internal *ras_comp_int = file->private_data;
3013 struct ibmvnic_adapter *adapter = ras_comp_int->adapter;
3014 union ibmvnic_crq crq;
3015 unsigned long val;
3016 char buff[9]; /* decimal max int plus \n and \0 */
3017
3018 copy_from_user(buff, user_buf, sizeof(buff));
3019 val = kstrtoul(buff, 10, NULL);
3020 if (val > 9)
3021 val = 9;
3022
3023 memset(&crq, 0, sizeof(crq));
3024 crq.control_ras.first = IBMVNIC_CRQ_CMD;
3025 crq.control_ras.cmd = CONTROL_RAS;
3026 crq.control_ras.correlator =
3027 adapter->ras_comps[ras_comp_int->num].correlator;
3028 crq.control_ras.op = IBMVNIC_TRACE_LEVEL;
3029 crq.control_ras.level = val;
3030 ibmvnic_send_crq(adapter, &crq);
3031
3032 return len;
3033}
3034
3035static const struct file_operations trace_level_ops = {
3036 .owner = THIS_MODULE,
Wei Yongjun7a95e942016-08-24 13:50:03 +00003037 .open = simple_open,
Thomas Falcon032c5e82015-12-21 11:26:06 -06003038 .read = trace_level_read,
3039 .write = trace_level_write,
3040};
3041
3042static ssize_t trace_buff_size_read(struct file *file, char __user *user_buf,
3043 size_t len, loff_t *ppos)
3044{
3045 struct ibmvnic_fw_comp_internal *ras_comp_int = file->private_data;
3046 struct ibmvnic_adapter *adapter = ras_comp_int->adapter;
3047 int num = ras_comp_int->num;
3048 char buff[9]; /* decimal max int plus \n and \0 */
3049 int size;
3050
3051 size = sprintf(buff, "%d\n", adapter->ras_comps[num].trace_buff_size);
3052 if (*ppos >= size)
3053 return 0;
3054
3055 copy_to_user(user_buf, buff, size);
3056 *ppos += size;
3057 return size;
3058}
3059
3060static ssize_t trace_buff_size_write(struct file *file,
3061 const char __user *user_buf, size_t len,
3062 loff_t *ppos)
3063{
3064 struct ibmvnic_fw_comp_internal *ras_comp_int = file->private_data;
3065 struct ibmvnic_adapter *adapter = ras_comp_int->adapter;
3066 union ibmvnic_crq crq;
3067 unsigned long val;
3068 char buff[9]; /* decimal max int plus \n and \0 */
3069
3070 copy_from_user(buff, user_buf, sizeof(buff));
3071 val = kstrtoul(buff, 10, NULL);
3072
3073 memset(&crq, 0, sizeof(crq));
3074 crq.control_ras.first = IBMVNIC_CRQ_CMD;
3075 crq.control_ras.cmd = CONTROL_RAS;
3076 crq.control_ras.correlator =
3077 adapter->ras_comps[ras_comp_int->num].correlator;
3078 crq.control_ras.op = IBMVNIC_CHG_TRACE_BUFF_SZ;
3079 /* trace_buff_sz is 3 bytes, stuff an int into it */
3080 crq.control_ras.trace_buff_sz[0] = ((u8 *)(&val))[5];
3081 crq.control_ras.trace_buff_sz[1] = ((u8 *)(&val))[6];
3082 crq.control_ras.trace_buff_sz[2] = ((u8 *)(&val))[7];
3083 ibmvnic_send_crq(adapter, &crq);
3084
3085 return len;
3086}
3087
3088static const struct file_operations trace_size_ops = {
3089 .owner = THIS_MODULE,
Wei Yongjun7a95e942016-08-24 13:50:03 +00003090 .open = simple_open,
Thomas Falcon032c5e82015-12-21 11:26:06 -06003091 .read = trace_buff_size_read,
3092 .write = trace_buff_size_write,
3093};
3094
3095static void handle_request_ras_comps_rsp(union ibmvnic_crq *crq,
3096 struct ibmvnic_adapter *adapter)
3097{
3098 struct device *dev = &adapter->vdev->dev;
3099 struct dentry *dir_ent;
3100 struct dentry *ent;
3101 int i;
3102
3103 debugfs_remove_recursive(adapter->ras_comps_ent);
3104
3105 adapter->ras_comps_ent = debugfs_create_dir("ras_comps",
3106 adapter->debugfs_dir);
3107 if (!adapter->ras_comps_ent || IS_ERR(adapter->ras_comps_ent)) {
3108 dev_info(dev, "debugfs create ras_comps dir failed\n");
3109 return;
3110 }
3111
3112 for (i = 0; i < adapter->ras_comp_num; i++) {
3113 dir_ent = debugfs_create_dir(adapter->ras_comps[i].name,
3114 adapter->ras_comps_ent);
3115 if (!dir_ent || IS_ERR(dir_ent)) {
3116 dev_info(dev, "debugfs create %s dir failed\n",
3117 adapter->ras_comps[i].name);
3118 continue;
3119 }
3120
3121 adapter->ras_comp_int[i].adapter = adapter;
3122 adapter->ras_comp_int[i].num = i;
3123 adapter->ras_comp_int[i].desc_blob.data =
3124 &adapter->ras_comps[i].description;
3125 adapter->ras_comp_int[i].desc_blob.size =
3126 sizeof(adapter->ras_comps[i].description);
3127
3128 /* Don't need to remember the dentry's because the debugfs dir
3129 * gets removed recursively
3130 */
3131 ent = debugfs_create_blob("description", S_IRUGO, dir_ent,
3132 &adapter->ras_comp_int[i].desc_blob);
3133 ent = debugfs_create_file("trace_buf_size", S_IRUGO | S_IWUSR,
3134 dir_ent, &adapter->ras_comp_int[i],
3135 &trace_size_ops);
3136 ent = debugfs_create_file("trace_level",
3137 S_IRUGO |
3138 (adapter->ras_comps[i].trace_level !=
3139 0xFF ? S_IWUSR : 0),
3140 dir_ent, &adapter->ras_comp_int[i],
3141 &trace_level_ops);
3142 ent = debugfs_create_file("error_level",
3143 S_IRUGO |
3144 (adapter->
3145 ras_comps[i].error_check_level !=
3146 0xFF ? S_IWUSR : 0),
3147 dir_ent, &adapter->ras_comp_int[i],
3148 &trace_level_ops);
3149 ent = debugfs_create_file("tracing", S_IRUGO | S_IWUSR,
3150 dir_ent, &adapter->ras_comp_int[i],
3151 &tracing_ops);
3152 ent = debugfs_create_file("paused", S_IRUGO | S_IWUSR,
3153 dir_ent, &adapter->ras_comp_int[i],
3154 &paused_ops);
3155 ent = debugfs_create_file("trace", S_IRUGO, dir_ent,
3156 &adapter->ras_comp_int[i],
3157 &trace_ops);
3158 }
3159}
3160
3161static void handle_request_ras_comp_num_rsp(union ibmvnic_crq *crq,
3162 struct ibmvnic_adapter *adapter)
3163{
3164 int len = adapter->ras_comp_num * sizeof(struct ibmvnic_fw_component);
3165 struct device *dev = &adapter->vdev->dev;
3166 union ibmvnic_crq newcrq;
3167
3168 adapter->ras_comps = dma_alloc_coherent(dev, len,
3169 &adapter->ras_comps_tok,
3170 GFP_KERNEL);
3171 if (!adapter->ras_comps) {
3172 if (!firmware_has_feature(FW_FEATURE_CMO))
3173 dev_err(dev, "Couldn't alloc fw comps buffer\n");
3174 return;
3175 }
3176
3177 adapter->ras_comp_int = kmalloc(adapter->ras_comp_num *
3178 sizeof(struct ibmvnic_fw_comp_internal),
3179 GFP_KERNEL);
3180 if (!adapter->ras_comp_int)
3181 dma_free_coherent(dev, len, adapter->ras_comps,
3182 adapter->ras_comps_tok);
3183
3184 memset(&newcrq, 0, sizeof(newcrq));
3185 newcrq.request_ras_comps.first = IBMVNIC_CRQ_CMD;
3186 newcrq.request_ras_comps.cmd = REQUEST_RAS_COMPS;
3187 newcrq.request_ras_comps.ioba = cpu_to_be32(adapter->ras_comps_tok);
3188 newcrq.request_ras_comps.len = cpu_to_be32(len);
3189 ibmvnic_send_crq(adapter, &newcrq);
3190}
3191
3192static void ibmvnic_free_inflight(struct ibmvnic_adapter *adapter)
3193{
Wei Yongjun96183182016-06-27 20:48:53 +08003194 struct ibmvnic_inflight_cmd *inflight_cmd, *tmp1;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003195 struct device *dev = &adapter->vdev->dev;
Wei Yongjun96183182016-06-27 20:48:53 +08003196 struct ibmvnic_error_buff *error_buff, *tmp2;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003197 unsigned long flags;
3198 unsigned long flags2;
3199
3200 spin_lock_irqsave(&adapter->inflight_lock, flags);
Wei Yongjun96183182016-06-27 20:48:53 +08003201 list_for_each_entry_safe(inflight_cmd, tmp1, &adapter->inflight, list) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06003202 switch (inflight_cmd->crq.generic.cmd) {
3203 case LOGIN:
3204 dma_unmap_single(dev, adapter->login_buf_token,
3205 adapter->login_buf_sz,
3206 DMA_BIDIRECTIONAL);
3207 dma_unmap_single(dev, adapter->login_rsp_buf_token,
3208 adapter->login_rsp_buf_sz,
3209 DMA_BIDIRECTIONAL);
3210 kfree(adapter->login_rsp_buf);
3211 kfree(adapter->login_buf);
3212 break;
3213 case REQUEST_DUMP:
3214 complete(&adapter->fw_done);
3215 break;
3216 case REQUEST_ERROR_INFO:
3217 spin_lock_irqsave(&adapter->error_list_lock, flags2);
Wei Yongjun96183182016-06-27 20:48:53 +08003218 list_for_each_entry_safe(error_buff, tmp2,
3219 &adapter->errors, list) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06003220 dma_unmap_single(dev, error_buff->dma,
3221 error_buff->len,
3222 DMA_FROM_DEVICE);
3223 kfree(error_buff->buff);
3224 list_del(&error_buff->list);
3225 kfree(error_buff);
3226 }
3227 spin_unlock_irqrestore(&adapter->error_list_lock,
3228 flags2);
3229 break;
3230 }
3231 list_del(&inflight_cmd->list);
3232 kfree(inflight_cmd);
3233 }
3234 spin_unlock_irqrestore(&adapter->inflight_lock, flags);
3235}
3236
Thomas Falcon9888d7b2016-10-27 12:28:51 -05003237static void ibmvnic_xport_event(struct work_struct *work)
3238{
3239 struct ibmvnic_adapter *adapter = container_of(work,
3240 struct ibmvnic_adapter,
3241 ibmvnic_xport);
3242 struct device *dev = &adapter->vdev->dev;
3243 long rc;
3244
3245 ibmvnic_free_inflight(adapter);
3246 release_sub_crqs(adapter);
3247 if (adapter->migrated) {
3248 rc = ibmvnic_reenable_crq_queue(adapter);
3249 if (rc)
3250 dev_err(dev, "Error after enable rc=%ld\n", rc);
3251 adapter->migrated = false;
3252 rc = ibmvnic_send_crq_init(adapter);
3253 if (rc)
3254 dev_err(dev, "Error sending init rc=%ld\n", rc);
3255 }
3256}
3257
Thomas Falcon032c5e82015-12-21 11:26:06 -06003258static void ibmvnic_handle_crq(union ibmvnic_crq *crq,
3259 struct ibmvnic_adapter *adapter)
3260{
3261 struct ibmvnic_generic_crq *gen_crq = &crq->generic;
3262 struct net_device *netdev = adapter->netdev;
3263 struct device *dev = &adapter->vdev->dev;
3264 long rc;
3265
3266 netdev_dbg(netdev, "Handling CRQ: %016lx %016lx\n",
3267 ((unsigned long int *)crq)[0],
3268 ((unsigned long int *)crq)[1]);
3269 switch (gen_crq->first) {
3270 case IBMVNIC_CRQ_INIT_RSP:
3271 switch (gen_crq->cmd) {
3272 case IBMVNIC_CRQ_INIT:
3273 dev_info(dev, "Partner initialized\n");
3274 /* Send back a response */
3275 rc = ibmvnic_send_crq_init_complete(adapter);
Thomas Falcon65dc6892016-07-06 15:35:18 -05003276 if (!rc)
3277 schedule_work(&adapter->vnic_crq_init);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003278 else
3279 dev_err(dev, "Can't send initrsp rc=%ld\n", rc);
3280 break;
3281 case IBMVNIC_CRQ_INIT_COMPLETE:
3282 dev_info(dev, "Partner initialization complete\n");
3283 send_version_xchg(adapter);
3284 break;
3285 default:
3286 dev_err(dev, "Unknown crq cmd: %d\n", gen_crq->cmd);
3287 }
3288 return;
3289 case IBMVNIC_CRQ_XPORT_EVENT:
3290 if (gen_crq->cmd == IBMVNIC_PARTITION_MIGRATED) {
3291 dev_info(dev, "Re-enabling adapter\n");
3292 adapter->migrated = true;
Thomas Falcon9888d7b2016-10-27 12:28:51 -05003293 schedule_work(&adapter->ibmvnic_xport);
Thomas Falcondfad09a2016-08-18 11:37:51 -05003294 } else if (gen_crq->cmd == IBMVNIC_DEVICE_FAILOVER) {
3295 dev_info(dev, "Backing device failover detected\n");
3296 netif_carrier_off(netdev);
3297 adapter->failover = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003298 } else {
3299 /* The adapter lost the connection */
3300 dev_err(dev, "Virtual Adapter failed (rc=%d)\n",
3301 gen_crq->cmd);
Thomas Falcon9888d7b2016-10-27 12:28:51 -05003302 schedule_work(&adapter->ibmvnic_xport);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003303 }
3304 return;
3305 case IBMVNIC_CRQ_CMD_RSP:
3306 break;
3307 default:
3308 dev_err(dev, "Got an invalid msg type 0x%02x\n",
3309 gen_crq->first);
3310 return;
3311 }
3312
3313 switch (gen_crq->cmd) {
3314 case VERSION_EXCHANGE_RSP:
3315 rc = crq->version_exchange_rsp.rc.code;
3316 if (rc) {
3317 dev_err(dev, "Error %ld in VERSION_EXCHG_RSP\n", rc);
3318 break;
3319 }
3320 dev_info(dev, "Partner protocol version is %d\n",
3321 crq->version_exchange_rsp.version);
3322 if (be16_to_cpu(crq->version_exchange_rsp.version) <
3323 ibmvnic_version)
3324 ibmvnic_version =
3325 be16_to_cpu(crq->version_exchange_rsp.version);
3326 send_cap_queries(adapter);
3327 break;
3328 case QUERY_CAPABILITY_RSP:
3329 handle_query_cap_rsp(crq, adapter);
3330 break;
3331 case QUERY_MAP_RSP:
3332 handle_query_map_rsp(crq, adapter);
3333 break;
3334 case REQUEST_MAP_RSP:
3335 handle_request_map_rsp(crq, adapter);
3336 break;
3337 case REQUEST_UNMAP_RSP:
3338 handle_request_unmap_rsp(crq, adapter);
3339 break;
3340 case REQUEST_CAPABILITY_RSP:
3341 handle_request_cap_rsp(crq, adapter);
3342 break;
3343 case LOGIN_RSP:
3344 netdev_dbg(netdev, "Got Login Response\n");
3345 handle_login_rsp(crq, adapter);
3346 break;
3347 case LOGICAL_LINK_STATE_RSP:
3348 netdev_dbg(netdev, "Got Logical Link State Response\n");
3349 adapter->logical_link_state =
3350 crq->logical_link_state_rsp.link_state;
3351 break;
3352 case LINK_STATE_INDICATION:
3353 netdev_dbg(netdev, "Got Logical Link State Indication\n");
3354 adapter->phys_link_state =
3355 crq->link_state_indication.phys_link_state;
3356 adapter->logical_link_state =
3357 crq->link_state_indication.logical_link_state;
3358 break;
3359 case CHANGE_MAC_ADDR_RSP:
3360 netdev_dbg(netdev, "Got MAC address change Response\n");
3361 handle_change_mac_rsp(crq, adapter);
3362 break;
3363 case ERROR_INDICATION:
3364 netdev_dbg(netdev, "Got Error Indication\n");
3365 handle_error_indication(crq, adapter);
3366 break;
3367 case REQUEST_ERROR_RSP:
3368 netdev_dbg(netdev, "Got Error Detail Response\n");
3369 handle_error_info_rsp(crq, adapter);
3370 break;
3371 case REQUEST_STATISTICS_RSP:
3372 netdev_dbg(netdev, "Got Statistics Response\n");
3373 complete(&adapter->stats_done);
3374 break;
3375 case REQUEST_DUMP_SIZE_RSP:
3376 netdev_dbg(netdev, "Got Request Dump Size Response\n");
3377 handle_dump_size_rsp(crq, adapter);
3378 break;
3379 case REQUEST_DUMP_RSP:
3380 netdev_dbg(netdev, "Got Request Dump Response\n");
3381 complete(&adapter->fw_done);
3382 break;
3383 case QUERY_IP_OFFLOAD_RSP:
3384 netdev_dbg(netdev, "Got Query IP offload Response\n");
3385 handle_query_ip_offload_rsp(adapter);
3386 break;
3387 case MULTICAST_CTRL_RSP:
3388 netdev_dbg(netdev, "Got multicast control Response\n");
3389 break;
3390 case CONTROL_IP_OFFLOAD_RSP:
3391 netdev_dbg(netdev, "Got Control IP offload Response\n");
3392 dma_unmap_single(dev, adapter->ip_offload_ctrl_tok,
3393 sizeof(adapter->ip_offload_ctrl),
3394 DMA_TO_DEVICE);
3395 /* We're done with the queries, perform the login */
3396 send_login(adapter);
3397 break;
3398 case REQUEST_RAS_COMP_NUM_RSP:
3399 netdev_dbg(netdev, "Got Request RAS Comp Num Response\n");
3400 if (crq->request_ras_comp_num_rsp.rc.code == 10) {
3401 netdev_dbg(netdev, "Request RAS Comp Num not supported\n");
3402 break;
3403 }
3404 adapter->ras_comp_num =
3405 be32_to_cpu(crq->request_ras_comp_num_rsp.num_components);
3406 handle_request_ras_comp_num_rsp(crq, adapter);
3407 break;
3408 case REQUEST_RAS_COMPS_RSP:
3409 netdev_dbg(netdev, "Got Request RAS Comps Response\n");
3410 handle_request_ras_comps_rsp(crq, adapter);
3411 break;
3412 case CONTROL_RAS_RSP:
3413 netdev_dbg(netdev, "Got Control RAS Response\n");
3414 handle_control_ras_rsp(crq, adapter);
3415 break;
3416 case COLLECT_FW_TRACE_RSP:
3417 netdev_dbg(netdev, "Got Collect firmware trace Response\n");
3418 complete(&adapter->fw_done);
3419 break;
3420 default:
3421 netdev_err(netdev, "Got an invalid cmd type 0x%02x\n",
3422 gen_crq->cmd);
3423 }
3424}
3425
3426static irqreturn_t ibmvnic_interrupt(int irq, void *instance)
3427{
3428 struct ibmvnic_adapter *adapter = instance;
3429 struct ibmvnic_crq_queue *queue = &adapter->crq;
3430 struct vio_dev *vdev = adapter->vdev;
3431 union ibmvnic_crq *crq;
3432 unsigned long flags;
3433 bool done = false;
3434
3435 spin_lock_irqsave(&queue->lock, flags);
3436 vio_disable_interrupts(vdev);
3437 while (!done) {
3438 /* Pull all the valid messages off the CRQ */
3439 while ((crq = ibmvnic_next_crq(adapter)) != NULL) {
3440 ibmvnic_handle_crq(crq, adapter);
3441 crq->generic.first = 0;
3442 }
3443 vio_enable_interrupts(vdev);
3444 crq = ibmvnic_next_crq(adapter);
3445 if (crq) {
3446 vio_disable_interrupts(vdev);
3447 ibmvnic_handle_crq(crq, adapter);
3448 crq->generic.first = 0;
3449 } else {
3450 done = true;
3451 }
3452 }
3453 spin_unlock_irqrestore(&queue->lock, flags);
3454 return IRQ_HANDLED;
3455}
3456
3457static int ibmvnic_reenable_crq_queue(struct ibmvnic_adapter *adapter)
3458{
3459 struct vio_dev *vdev = adapter->vdev;
3460 int rc;
3461
3462 do {
3463 rc = plpar_hcall_norets(H_ENABLE_CRQ, vdev->unit_address);
3464 } while (rc == H_IN_PROGRESS || rc == H_BUSY || H_IS_LONG_BUSY(rc));
3465
3466 if (rc)
3467 dev_err(&vdev->dev, "Error enabling adapter (rc=%d)\n", rc);
3468
3469 return rc;
3470}
3471
3472static int ibmvnic_reset_crq(struct ibmvnic_adapter *adapter)
3473{
3474 struct ibmvnic_crq_queue *crq = &adapter->crq;
3475 struct device *dev = &adapter->vdev->dev;
3476 struct vio_dev *vdev = adapter->vdev;
3477 int rc;
3478
3479 /* Close the CRQ */
3480 do {
3481 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
3482 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
3483
3484 /* Clean out the queue */
3485 memset(crq->msgs, 0, PAGE_SIZE);
3486 crq->cur = 0;
3487
3488 /* And re-open it again */
3489 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
3490 crq->msg_token, PAGE_SIZE);
3491
3492 if (rc == H_CLOSED)
3493 /* Adapter is good, but other end is not ready */
3494 dev_warn(dev, "Partner adapter not ready\n");
3495 else if (rc != 0)
3496 dev_warn(dev, "Couldn't register crq (rc=%d)\n", rc);
3497
3498 return rc;
3499}
3500
3501static void ibmvnic_release_crq_queue(struct ibmvnic_adapter *adapter)
3502{
3503 struct ibmvnic_crq_queue *crq = &adapter->crq;
3504 struct vio_dev *vdev = adapter->vdev;
3505 long rc;
3506
3507 netdev_dbg(adapter->netdev, "Releasing CRQ\n");
3508 free_irq(vdev->irq, adapter);
3509 do {
3510 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
3511 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
3512
3513 dma_unmap_single(&vdev->dev, crq->msg_token, PAGE_SIZE,
3514 DMA_BIDIRECTIONAL);
3515 free_page((unsigned long)crq->msgs);
3516}
3517
3518static int ibmvnic_init_crq_queue(struct ibmvnic_adapter *adapter)
3519{
3520 struct ibmvnic_crq_queue *crq = &adapter->crq;
3521 struct device *dev = &adapter->vdev->dev;
3522 struct vio_dev *vdev = adapter->vdev;
3523 int rc, retrc = -ENOMEM;
3524
3525 crq->msgs = (union ibmvnic_crq *)get_zeroed_page(GFP_KERNEL);
3526 /* Should we allocate more than one page? */
3527
3528 if (!crq->msgs)
3529 return -ENOMEM;
3530
3531 crq->size = PAGE_SIZE / sizeof(*crq->msgs);
3532 crq->msg_token = dma_map_single(dev, crq->msgs, PAGE_SIZE,
3533 DMA_BIDIRECTIONAL);
3534 if (dma_mapping_error(dev, crq->msg_token))
3535 goto map_failed;
3536
3537 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
3538 crq->msg_token, PAGE_SIZE);
3539
3540 if (rc == H_RESOURCE)
3541 /* maybe kexecing and resource is busy. try a reset */
3542 rc = ibmvnic_reset_crq(adapter);
3543 retrc = rc;
3544
3545 if (rc == H_CLOSED) {
3546 dev_warn(dev, "Partner adapter not ready\n");
3547 } else if (rc) {
3548 dev_warn(dev, "Error %d opening adapter\n", rc);
3549 goto reg_crq_failed;
3550 }
3551
3552 retrc = 0;
3553
3554 netdev_dbg(adapter->netdev, "registering irq 0x%x\n", vdev->irq);
3555 rc = request_irq(vdev->irq, ibmvnic_interrupt, 0, IBMVNIC_NAME,
3556 adapter);
3557 if (rc) {
3558 dev_err(dev, "Couldn't register irq 0x%x. rc=%d\n",
3559 vdev->irq, rc);
3560 goto req_irq_failed;
3561 }
3562
3563 rc = vio_enable_interrupts(vdev);
3564 if (rc) {
3565 dev_err(dev, "Error %d enabling interrupts\n", rc);
3566 goto req_irq_failed;
3567 }
3568
3569 crq->cur = 0;
3570 spin_lock_init(&crq->lock);
3571
3572 return retrc;
3573
3574req_irq_failed:
3575 do {
3576 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
3577 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
3578reg_crq_failed:
3579 dma_unmap_single(dev, crq->msg_token, PAGE_SIZE, DMA_BIDIRECTIONAL);
3580map_failed:
3581 free_page((unsigned long)crq->msgs);
3582 return retrc;
3583}
3584
3585/* debugfs for dump */
3586static int ibmvnic_dump_show(struct seq_file *seq, void *v)
3587{
3588 struct net_device *netdev = seq->private;
3589 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
3590 struct device *dev = &adapter->vdev->dev;
3591 union ibmvnic_crq crq;
3592
3593 memset(&crq, 0, sizeof(crq));
3594 crq.request_dump_size.first = IBMVNIC_CRQ_CMD;
3595 crq.request_dump_size.cmd = REQUEST_DUMP_SIZE;
3596 ibmvnic_send_crq(adapter, &crq);
3597
3598 init_completion(&adapter->fw_done);
3599 wait_for_completion(&adapter->fw_done);
3600
3601 seq_write(seq, adapter->dump_data, adapter->dump_data_size);
3602
3603 dma_unmap_single(dev, adapter->dump_data_token, adapter->dump_data_size,
3604 DMA_BIDIRECTIONAL);
3605
3606 kfree(adapter->dump_data);
3607
3608 return 0;
3609}
3610
3611static int ibmvnic_dump_open(struct inode *inode, struct file *file)
3612{
3613 return single_open(file, ibmvnic_dump_show, inode->i_private);
3614}
3615
3616static const struct file_operations ibmvnic_dump_ops = {
3617 .owner = THIS_MODULE,
3618 .open = ibmvnic_dump_open,
3619 .read = seq_read,
3620 .llseek = seq_lseek,
3621 .release = single_release,
3622};
3623
Thomas Falcon65dc6892016-07-06 15:35:18 -05003624static void handle_crq_init_rsp(struct work_struct *work)
3625{
3626 struct ibmvnic_adapter *adapter = container_of(work,
3627 struct ibmvnic_adapter,
3628 vnic_crq_init);
3629 struct device *dev = &adapter->vdev->dev;
3630 struct net_device *netdev = adapter->netdev;
3631 unsigned long timeout = msecs_to_jiffies(30000);
Thomas Falcondfad09a2016-08-18 11:37:51 -05003632 bool restart = false;
Thomas Falcon65dc6892016-07-06 15:35:18 -05003633 int rc;
3634
Thomas Falcondfad09a2016-08-18 11:37:51 -05003635 if (adapter->failover) {
3636 release_sub_crqs(adapter);
3637 if (netif_running(netdev)) {
3638 netif_tx_disable(netdev);
3639 ibmvnic_close(netdev);
3640 restart = true;
3641 }
3642 }
3643
Thomas Falcon65dc6892016-07-06 15:35:18 -05003644 send_version_xchg(adapter);
3645 reinit_completion(&adapter->init_done);
3646 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
3647 dev_err(dev, "Passive init timeout\n");
3648 goto task_failed;
3649 }
3650
3651 do {
3652 if (adapter->renegotiate) {
3653 adapter->renegotiate = false;
3654 release_sub_crqs_no_irqs(adapter);
3655 send_cap_queries(adapter);
3656
3657 reinit_completion(&adapter->init_done);
3658 if (!wait_for_completion_timeout(&adapter->init_done,
3659 timeout)) {
3660 dev_err(dev, "Passive init timeout\n");
3661 goto task_failed;
3662 }
3663 }
3664 } while (adapter->renegotiate);
3665 rc = init_sub_crq_irqs(adapter);
3666
3667 if (rc)
3668 goto task_failed;
3669
3670 netdev->real_num_tx_queues = adapter->req_tx_queues;
Thomas Falcon87737f82016-10-17 15:28:10 -05003671 netdev->mtu = adapter->req_mtu;
Thomas Falcon65dc6892016-07-06 15:35:18 -05003672
Thomas Falcondfad09a2016-08-18 11:37:51 -05003673 if (adapter->failover) {
3674 adapter->failover = false;
3675 if (restart) {
3676 rc = ibmvnic_open(netdev);
3677 if (rc)
3678 goto restart_failed;
3679 }
3680 netif_carrier_on(netdev);
3681 return;
3682 }
3683
Thomas Falcon65dc6892016-07-06 15:35:18 -05003684 rc = register_netdev(netdev);
3685 if (rc) {
3686 dev_err(dev,
3687 "failed to register netdev rc=%d\n", rc);
3688 goto register_failed;
3689 }
3690 dev_info(dev, "ibmvnic registered\n");
3691
3692 return;
3693
Thomas Falcondfad09a2016-08-18 11:37:51 -05003694restart_failed:
3695 dev_err(dev, "Failed to restart ibmvnic, rc=%d\n", rc);
Thomas Falcon65dc6892016-07-06 15:35:18 -05003696register_failed:
3697 release_sub_crqs(adapter);
3698task_failed:
3699 dev_err(dev, "Passive initialization was not successful\n");
3700}
3701
Thomas Falcon032c5e82015-12-21 11:26:06 -06003702static int ibmvnic_probe(struct vio_dev *dev, const struct vio_device_id *id)
3703{
Thomas Falconea22d512016-07-06 15:35:17 -05003704 unsigned long timeout = msecs_to_jiffies(30000);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003705 struct ibmvnic_adapter *adapter;
3706 struct net_device *netdev;
3707 unsigned char *mac_addr_p;
3708 struct dentry *ent;
3709 char buf[16]; /* debugfs name buf */
3710 int rc;
3711
3712 dev_dbg(&dev->dev, "entering ibmvnic_probe for UA 0x%x\n",
3713 dev->unit_address);
3714
3715 mac_addr_p = (unsigned char *)vio_get_attribute(dev,
3716 VETH_MAC_ADDR, NULL);
3717 if (!mac_addr_p) {
3718 dev_err(&dev->dev,
3719 "(%s:%3.3d) ERROR: Can't find MAC_ADDR attribute\n",
3720 __FILE__, __LINE__);
3721 return 0;
3722 }
3723
3724 netdev = alloc_etherdev_mq(sizeof(struct ibmvnic_adapter),
3725 IBMVNIC_MAX_TX_QUEUES);
3726 if (!netdev)
3727 return -ENOMEM;
3728
3729 adapter = netdev_priv(netdev);
3730 dev_set_drvdata(&dev->dev, netdev);
3731 adapter->vdev = dev;
3732 adapter->netdev = netdev;
Thomas Falcondfad09a2016-08-18 11:37:51 -05003733 adapter->failover = false;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003734
3735 ether_addr_copy(adapter->mac_addr, mac_addr_p);
3736 ether_addr_copy(netdev->dev_addr, adapter->mac_addr);
3737 netdev->irq = dev->irq;
3738 netdev->netdev_ops = &ibmvnic_netdev_ops;
3739 netdev->ethtool_ops = &ibmvnic_ethtool_ops;
3740 SET_NETDEV_DEV(netdev, &dev->dev);
3741
Thomas Falcon65dc6892016-07-06 15:35:18 -05003742 INIT_WORK(&adapter->vnic_crq_init, handle_crq_init_rsp);
Thomas Falcon9888d7b2016-10-27 12:28:51 -05003743 INIT_WORK(&adapter->ibmvnic_xport, ibmvnic_xport_event);
Thomas Falcon65dc6892016-07-06 15:35:18 -05003744
Thomas Falcon032c5e82015-12-21 11:26:06 -06003745 spin_lock_init(&adapter->stats_lock);
3746
3747 rc = ibmvnic_init_crq_queue(adapter);
3748 if (rc) {
3749 dev_err(&dev->dev, "Couldn't initialize crq. rc=%d\n", rc);
3750 goto free_netdev;
3751 }
3752
3753 INIT_LIST_HEAD(&adapter->errors);
3754 INIT_LIST_HEAD(&adapter->inflight);
3755 spin_lock_init(&adapter->error_list_lock);
3756 spin_lock_init(&adapter->inflight_lock);
3757
3758 adapter->stats_token = dma_map_single(&dev->dev, &adapter->stats,
3759 sizeof(struct ibmvnic_statistics),
3760 DMA_FROM_DEVICE);
3761 if (dma_mapping_error(&dev->dev, adapter->stats_token)) {
3762 if (!firmware_has_feature(FW_FEATURE_CMO))
3763 dev_err(&dev->dev, "Couldn't map stats buffer\n");
Wei Yongjun0e872032016-08-24 13:47:58 +00003764 rc = -ENOMEM;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003765 goto free_crq;
3766 }
3767
3768 snprintf(buf, sizeof(buf), "ibmvnic_%x", dev->unit_address);
3769 ent = debugfs_create_dir(buf, NULL);
3770 if (!ent || IS_ERR(ent)) {
3771 dev_info(&dev->dev, "debugfs create directory failed\n");
3772 adapter->debugfs_dir = NULL;
3773 } else {
3774 adapter->debugfs_dir = ent;
3775 ent = debugfs_create_file("dump", S_IRUGO, adapter->debugfs_dir,
3776 netdev, &ibmvnic_dump_ops);
3777 if (!ent || IS_ERR(ent)) {
3778 dev_info(&dev->dev,
3779 "debugfs create dump file failed\n");
3780 adapter->debugfs_dump = NULL;
3781 } else {
3782 adapter->debugfs_dump = ent;
3783 }
3784 }
3785 ibmvnic_send_crq_init(adapter);
3786
3787 init_completion(&adapter->init_done);
Thomas Falconea22d512016-07-06 15:35:17 -05003788 if (!wait_for_completion_timeout(&adapter->init_done, timeout))
3789 return 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003790
John Allen498cd8e2016-04-06 11:49:55 -05003791 do {
John Allen498cd8e2016-04-06 11:49:55 -05003792 if (adapter->renegotiate) {
Thomas Falconea22d512016-07-06 15:35:17 -05003793 adapter->renegotiate = false;
3794 release_sub_crqs_no_irqs(adapter);
John Allen498cd8e2016-04-06 11:49:55 -05003795 send_cap_queries(adapter);
3796
3797 reinit_completion(&adapter->init_done);
Thomas Falconea22d512016-07-06 15:35:17 -05003798 if (!wait_for_completion_timeout(&adapter->init_done,
3799 timeout))
3800 return 0;
John Allen498cd8e2016-04-06 11:49:55 -05003801 }
3802 } while (adapter->renegotiate);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003803
Thomas Falconea22d512016-07-06 15:35:17 -05003804 rc = init_sub_crq_irqs(adapter);
3805 if (rc) {
3806 dev_err(&dev->dev, "failed to initialize sub crq irqs\n");
3807 goto free_debugfs;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003808 }
3809
3810 netdev->real_num_tx_queues = adapter->req_tx_queues;
Thomas Falcon87737f82016-10-17 15:28:10 -05003811 netdev->mtu = adapter->req_mtu;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003812
3813 rc = register_netdev(netdev);
3814 if (rc) {
3815 dev_err(&dev->dev, "failed to register netdev rc=%d\n", rc);
Thomas Falconea22d512016-07-06 15:35:17 -05003816 goto free_sub_crqs;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003817 }
3818 dev_info(&dev->dev, "ibmvnic registered\n");
3819
3820 return 0;
3821
Thomas Falconea22d512016-07-06 15:35:17 -05003822free_sub_crqs:
3823 release_sub_crqs(adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003824free_debugfs:
3825 if (adapter->debugfs_dir && !IS_ERR(adapter->debugfs_dir))
3826 debugfs_remove_recursive(adapter->debugfs_dir);
3827free_crq:
3828 ibmvnic_release_crq_queue(adapter);
3829free_netdev:
3830 free_netdev(netdev);
3831 return rc;
3832}
3833
3834static int ibmvnic_remove(struct vio_dev *dev)
3835{
3836 struct net_device *netdev = dev_get_drvdata(&dev->dev);
3837 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
3838
3839 unregister_netdev(netdev);
3840
3841 release_sub_crqs(adapter);
3842
3843 ibmvnic_release_crq_queue(adapter);
3844
3845 if (adapter->debugfs_dir && !IS_ERR(adapter->debugfs_dir))
3846 debugfs_remove_recursive(adapter->debugfs_dir);
3847
3848 if (adapter->ras_comps)
3849 dma_free_coherent(&dev->dev,
3850 adapter->ras_comp_num *
3851 sizeof(struct ibmvnic_fw_component),
3852 adapter->ras_comps, adapter->ras_comps_tok);
3853
3854 kfree(adapter->ras_comp_int);
3855
3856 free_netdev(netdev);
3857 dev_set_drvdata(&dev->dev, NULL);
3858
3859 return 0;
3860}
3861
3862static unsigned long ibmvnic_get_desired_dma(struct vio_dev *vdev)
3863{
3864 struct net_device *netdev = dev_get_drvdata(&vdev->dev);
3865 struct ibmvnic_adapter *adapter;
3866 struct iommu_table *tbl;
3867 unsigned long ret = 0;
3868 int i;
3869
3870 tbl = get_iommu_table_base(&vdev->dev);
3871
3872 /* netdev inits at probe time along with the structures we need below*/
3873 if (!netdev)
3874 return IOMMU_PAGE_ALIGN(IBMVNIC_IO_ENTITLEMENT_DEFAULT, tbl);
3875
3876 adapter = netdev_priv(netdev);
3877
3878 ret += PAGE_SIZE; /* the crq message queue */
3879 ret += adapter->bounce_buffer_size;
3880 ret += IOMMU_PAGE_ALIGN(sizeof(struct ibmvnic_statistics), tbl);
3881
3882 for (i = 0; i < adapter->req_tx_queues + adapter->req_rx_queues; i++)
3883 ret += 4 * PAGE_SIZE; /* the scrq message queue */
3884
3885 for (i = 0; i < be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
3886 i++)
3887 ret += adapter->rx_pool[i].size *
3888 IOMMU_PAGE_ALIGN(adapter->rx_pool[i].buff_size, tbl);
3889
3890 return ret;
3891}
3892
3893static int ibmvnic_resume(struct device *dev)
3894{
3895 struct net_device *netdev = dev_get_drvdata(dev);
3896 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
3897 int i;
3898
3899 /* kick the interrupt handlers just in case we lost an interrupt */
3900 for (i = 0; i < adapter->req_rx_queues; i++)
3901 ibmvnic_interrupt_rx(adapter->rx_scrq[i]->irq,
3902 adapter->rx_scrq[i]);
3903
3904 return 0;
3905}
3906
3907static struct vio_device_id ibmvnic_device_table[] = {
3908 {"network", "IBM,vnic"},
3909 {"", "" }
3910};
3911MODULE_DEVICE_TABLE(vio, ibmvnic_device_table);
3912
3913static const struct dev_pm_ops ibmvnic_pm_ops = {
3914 .resume = ibmvnic_resume
3915};
3916
3917static struct vio_driver ibmvnic_driver = {
3918 .id_table = ibmvnic_device_table,
3919 .probe = ibmvnic_probe,
3920 .remove = ibmvnic_remove,
3921 .get_desired_dma = ibmvnic_get_desired_dma,
3922 .name = ibmvnic_driver_name,
3923 .pm = &ibmvnic_pm_ops,
3924};
3925
3926/* module functions */
3927static int __init ibmvnic_module_init(void)
3928{
3929 pr_info("%s: %s %s\n", ibmvnic_driver_name, ibmvnic_driver_string,
3930 IBMVNIC_DRIVER_VERSION);
3931
3932 return vio_register_driver(&ibmvnic_driver);
3933}
3934
3935static void __exit ibmvnic_module_exit(void)
3936{
3937 vio_unregister_driver(&ibmvnic_driver);
3938}
3939
3940module_init(ibmvnic_module_init);
3941module_exit(ibmvnic_module_exit);