blob: def867aaa42284f57620c80fcba1b388e5697b29 [file] [log] [blame]
Thomas Falcon032c5e82015-12-21 11:26:06 -06001/**************************************************************************/
2/* */
3/* IBM System i and System p Virtual NIC Device Driver */
4/* Copyright (C) 2014 IBM Corp. */
5/* Santiago Leon (santi_leon@yahoo.com) */
6/* Thomas Falcon (tlfalcon@linux.vnet.ibm.com) */
7/* John Allen (jallen@linux.vnet.ibm.com) */
8/* */
9/* This program is free software; you can redistribute it and/or modify */
10/* it under the terms of the GNU General Public License as published by */
11/* the Free Software Foundation; either version 2 of the License, or */
12/* (at your option) any later version. */
13/* */
14/* This program is distributed in the hope that it will be useful, */
15/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
16/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
17/* GNU General Public License for more details. */
18/* */
19/* You should have received a copy of the GNU General Public License */
20/* along with this program. */
21/* */
22/* This module contains the implementation of a virtual ethernet device */
23/* for use with IBM i/p Series LPAR Linux. It utilizes the logical LAN */
24/* option of the RS/6000 Platform Architecture to interface with virtual */
25/* ethernet NICs that are presented to the partition by the hypervisor. */
26/* */
27/* Messages are passed between the VNIC driver and the VNIC server using */
28/* Command/Response Queues (CRQs) and sub CRQs (sCRQs). CRQs are used to */
29/* issue and receive commands that initiate communication with the server */
30/* on driver initialization. Sub CRQs (sCRQs) are similar to CRQs, but */
31/* are used by the driver to notify the server that a packet is */
32/* ready for transmission or that a buffer has been added to receive a */
33/* packet. Subsequently, sCRQs are used by the server to notify the */
34/* driver that a packet transmission has been completed or that a packet */
35/* has been received and placed in a waiting buffer. */
36/* */
37/* In lieu of a more conventional "on-the-fly" DMA mapping strategy in */
38/* which skbs are DMA mapped and immediately unmapped when the transmit */
39/* or receive has been completed, the VNIC driver is required to use */
40/* "long term mapping". This entails that large, continuous DMA mapped */
41/* buffers are allocated on driver initialization and these buffers are */
42/* then continuously reused to pass skbs to and from the VNIC server. */
43/* */
44/**************************************************************************/
45
46#include <linux/module.h>
47#include <linux/moduleparam.h>
48#include <linux/types.h>
49#include <linux/errno.h>
50#include <linux/completion.h>
51#include <linux/ioport.h>
52#include <linux/dma-mapping.h>
53#include <linux/kernel.h>
54#include <linux/netdevice.h>
55#include <linux/etherdevice.h>
56#include <linux/skbuff.h>
57#include <linux/init.h>
58#include <linux/delay.h>
59#include <linux/mm.h>
60#include <linux/ethtool.h>
61#include <linux/proc_fs.h>
62#include <linux/in.h>
63#include <linux/ip.h>
Thomas Falconad7775d2016-04-01 17:20:34 -050064#include <linux/ipv6.h>
Thomas Falcon032c5e82015-12-21 11:26:06 -060065#include <linux/irq.h>
66#include <linux/kthread.h>
67#include <linux/seq_file.h>
Thomas Falcon032c5e82015-12-21 11:26:06 -060068#include <linux/interrupt.h>
69#include <net/net_namespace.h>
70#include <asm/hvcall.h>
71#include <linux/atomic.h>
72#include <asm/vio.h>
73#include <asm/iommu.h>
74#include <linux/uaccess.h>
75#include <asm/firmware.h>
Thomas Falcon65dc6892016-07-06 15:35:18 -050076#include <linux/workqueue.h>
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -040077#include <linux/if_vlan.h>
Thomas Falcon032c5e82015-12-21 11:26:06 -060078
79#include "ibmvnic.h"
80
81static const char ibmvnic_driver_name[] = "ibmvnic";
82static const char ibmvnic_driver_string[] = "IBM System i/p Virtual NIC Driver";
83
84MODULE_AUTHOR("Santiago Leon <santi_leon@yahoo.com>");
85MODULE_DESCRIPTION("IBM System i/p Virtual NIC Driver");
86MODULE_LICENSE("GPL");
87MODULE_VERSION(IBMVNIC_DRIVER_VERSION);
88
89static int ibmvnic_version = IBMVNIC_INITIAL_VERSION;
90static int ibmvnic_remove(struct vio_dev *);
91static void release_sub_crqs(struct ibmvnic_adapter *);
92static int ibmvnic_reset_crq(struct ibmvnic_adapter *);
93static int ibmvnic_send_crq_init(struct ibmvnic_adapter *);
94static int ibmvnic_reenable_crq_queue(struct ibmvnic_adapter *);
95static int ibmvnic_send_crq(struct ibmvnic_adapter *, union ibmvnic_crq *);
96static int send_subcrq(struct ibmvnic_adapter *adapter, u64 remote_handle,
97 union sub_crq *sub_crq);
Thomas Falconad7775d2016-04-01 17:20:34 -050098static int send_subcrq_indirect(struct ibmvnic_adapter *, u64, u64, u64);
Thomas Falcon032c5e82015-12-21 11:26:06 -060099static irqreturn_t ibmvnic_interrupt_rx(int irq, void *instance);
100static int enable_scrq_irq(struct ibmvnic_adapter *,
101 struct ibmvnic_sub_crq_queue *);
102static int disable_scrq_irq(struct ibmvnic_adapter *,
103 struct ibmvnic_sub_crq_queue *);
104static int pending_scrq(struct ibmvnic_adapter *,
105 struct ibmvnic_sub_crq_queue *);
106static union sub_crq *ibmvnic_next_scrq(struct ibmvnic_adapter *,
107 struct ibmvnic_sub_crq_queue *);
108static int ibmvnic_poll(struct napi_struct *napi, int data);
109static void send_map_query(struct ibmvnic_adapter *adapter);
110static void send_request_map(struct ibmvnic_adapter *, dma_addr_t, __be32, u8);
111static void send_request_unmap(struct ibmvnic_adapter *, u8);
John Allenbd0b6722017-03-17 17:13:40 -0500112static void send_login(struct ibmvnic_adapter *adapter);
113static void send_cap_queries(struct ibmvnic_adapter *adapter);
114static int init_sub_crq_irqs(struct ibmvnic_adapter *adapter);
John Allenea5509f2017-03-17 17:13:43 -0500115static int ibmvnic_init(struct ibmvnic_adapter *);
Nathan Fontenotf9928872017-03-30 02:48:54 -0400116static void release_crq_queue(struct ibmvnic_adapter *);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600117
118struct ibmvnic_stat {
119 char name[ETH_GSTRING_LEN];
120 int offset;
121};
122
123#define IBMVNIC_STAT_OFF(stat) (offsetof(struct ibmvnic_adapter, stats) + \
124 offsetof(struct ibmvnic_statistics, stat))
125#define IBMVNIC_GET_STAT(a, off) (*((u64 *)(((unsigned long)(a)) + off)))
126
127static const struct ibmvnic_stat ibmvnic_stats[] = {
128 {"rx_packets", IBMVNIC_STAT_OFF(rx_packets)},
129 {"rx_bytes", IBMVNIC_STAT_OFF(rx_bytes)},
130 {"tx_packets", IBMVNIC_STAT_OFF(tx_packets)},
131 {"tx_bytes", IBMVNIC_STAT_OFF(tx_bytes)},
132 {"ucast_tx_packets", IBMVNIC_STAT_OFF(ucast_tx_packets)},
133 {"ucast_rx_packets", IBMVNIC_STAT_OFF(ucast_rx_packets)},
134 {"mcast_tx_packets", IBMVNIC_STAT_OFF(mcast_tx_packets)},
135 {"mcast_rx_packets", IBMVNIC_STAT_OFF(mcast_rx_packets)},
136 {"bcast_tx_packets", IBMVNIC_STAT_OFF(bcast_tx_packets)},
137 {"bcast_rx_packets", IBMVNIC_STAT_OFF(bcast_rx_packets)},
138 {"align_errors", IBMVNIC_STAT_OFF(align_errors)},
139 {"fcs_errors", IBMVNIC_STAT_OFF(fcs_errors)},
140 {"single_collision_frames", IBMVNIC_STAT_OFF(single_collision_frames)},
141 {"multi_collision_frames", IBMVNIC_STAT_OFF(multi_collision_frames)},
142 {"sqe_test_errors", IBMVNIC_STAT_OFF(sqe_test_errors)},
143 {"deferred_tx", IBMVNIC_STAT_OFF(deferred_tx)},
144 {"late_collisions", IBMVNIC_STAT_OFF(late_collisions)},
145 {"excess_collisions", IBMVNIC_STAT_OFF(excess_collisions)},
146 {"internal_mac_tx_errors", IBMVNIC_STAT_OFF(internal_mac_tx_errors)},
147 {"carrier_sense", IBMVNIC_STAT_OFF(carrier_sense)},
148 {"too_long_frames", IBMVNIC_STAT_OFF(too_long_frames)},
149 {"internal_mac_rx_errors", IBMVNIC_STAT_OFF(internal_mac_rx_errors)},
150};
151
152static long h_reg_sub_crq(unsigned long unit_address, unsigned long token,
153 unsigned long length, unsigned long *number,
154 unsigned long *irq)
155{
156 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
157 long rc;
158
159 rc = plpar_hcall(H_REG_SUB_CRQ, retbuf, unit_address, token, length);
160 *number = retbuf[0];
161 *irq = retbuf[1];
162
163 return rc;
164}
165
Thomas Falcon032c5e82015-12-21 11:26:06 -0600166static int alloc_long_term_buff(struct ibmvnic_adapter *adapter,
167 struct ibmvnic_long_term_buff *ltb, int size)
168{
169 struct device *dev = &adapter->vdev->dev;
170
171 ltb->size = size;
172 ltb->buff = dma_alloc_coherent(dev, ltb->size, &ltb->addr,
173 GFP_KERNEL);
174
175 if (!ltb->buff) {
176 dev_err(dev, "Couldn't alloc long term buffer\n");
177 return -ENOMEM;
178 }
179 ltb->map_id = adapter->map_id;
180 adapter->map_id++;
Nathan Fontenotdb5d0b52017-02-10 13:45:05 -0500181
182 init_completion(&adapter->fw_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600183 send_request_map(adapter, ltb->addr,
184 ltb->size, ltb->map_id);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600185 wait_for_completion(&adapter->fw_done);
186 return 0;
187}
188
189static void free_long_term_buff(struct ibmvnic_adapter *adapter,
190 struct ibmvnic_long_term_buff *ltb)
191{
192 struct device *dev = &adapter->vdev->dev;
193
Nathan Fontenotc657e322017-03-30 02:49:06 -0400194 if (!ltb->buff)
195 return;
196
Nathan Fontenoted651a12017-05-03 14:04:38 -0400197 if (adapter->reset_reason != VNIC_RESET_FAILOVER &&
198 adapter->reset_reason != VNIC_RESET_MOBILITY)
Thomas Falcondfad09a2016-08-18 11:37:51 -0500199 send_request_unmap(adapter, ltb->map_id);
Brian King59af56c2017-04-19 13:44:41 -0400200 dma_free_coherent(dev, ltb->size, ltb->buff, ltb->addr);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600201}
202
Thomas Falcon032c5e82015-12-21 11:26:06 -0600203static void replenish_rx_pool(struct ibmvnic_adapter *adapter,
204 struct ibmvnic_rx_pool *pool)
205{
206 int count = pool->size - atomic_read(&pool->available);
207 struct device *dev = &adapter->vdev->dev;
208 int buffers_added = 0;
209 unsigned long lpar_rc;
210 union sub_crq sub_crq;
211 struct sk_buff *skb;
212 unsigned int offset;
213 dma_addr_t dma_addr;
214 unsigned char *dst;
215 u64 *handle_array;
216 int shift = 0;
217 int index;
218 int i;
219
220 handle_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
221 be32_to_cpu(adapter->login_rsp_buf->
222 off_rxadd_subcrqs));
223
224 for (i = 0; i < count; ++i) {
225 skb = alloc_skb(pool->buff_size, GFP_ATOMIC);
226 if (!skb) {
227 dev_err(dev, "Couldn't replenish rx buff\n");
228 adapter->replenish_no_mem++;
229 break;
230 }
231
232 index = pool->free_map[pool->next_free];
233
234 if (pool->rx_buff[index].skb)
235 dev_err(dev, "Inconsistent free_map!\n");
236
237 /* Copy the skb to the long term mapped DMA buffer */
238 offset = index * pool->buff_size;
239 dst = pool->long_term_buff.buff + offset;
240 memset(dst, 0, pool->buff_size);
241 dma_addr = pool->long_term_buff.addr + offset;
242 pool->rx_buff[index].data = dst;
243
244 pool->free_map[pool->next_free] = IBMVNIC_INVALID_MAP;
245 pool->rx_buff[index].dma = dma_addr;
246 pool->rx_buff[index].skb = skb;
247 pool->rx_buff[index].pool_index = pool->index;
248 pool->rx_buff[index].size = pool->buff_size;
249
250 memset(&sub_crq, 0, sizeof(sub_crq));
251 sub_crq.rx_add.first = IBMVNIC_CRQ_CMD;
252 sub_crq.rx_add.correlator =
253 cpu_to_be64((u64)&pool->rx_buff[index]);
254 sub_crq.rx_add.ioba = cpu_to_be32(dma_addr);
255 sub_crq.rx_add.map_id = pool->long_term_buff.map_id;
256
257 /* The length field of the sCRQ is defined to be 24 bits so the
258 * buffer size needs to be left shifted by a byte before it is
259 * converted to big endian to prevent the last byte from being
260 * truncated.
261 */
262#ifdef __LITTLE_ENDIAN__
263 shift = 8;
264#endif
265 sub_crq.rx_add.len = cpu_to_be32(pool->buff_size << shift);
266
267 lpar_rc = send_subcrq(adapter, handle_array[pool->index],
268 &sub_crq);
269 if (lpar_rc != H_SUCCESS)
270 goto failure;
271
272 buffers_added++;
273 adapter->replenish_add_buff_success++;
274 pool->next_free = (pool->next_free + 1) % pool->size;
275 }
276 atomic_add(buffers_added, &pool->available);
277 return;
278
279failure:
280 dev_info(dev, "replenish pools failure\n");
281 pool->free_map[pool->next_free] = index;
282 pool->rx_buff[index].skb = NULL;
283 if (!dma_mapping_error(dev, dma_addr))
284 dma_unmap_single(dev, dma_addr, pool->buff_size,
285 DMA_FROM_DEVICE);
286
287 dev_kfree_skb_any(skb);
288 adapter->replenish_add_buff_failure++;
289 atomic_add(buffers_added, &pool->available);
290}
291
292static void replenish_pools(struct ibmvnic_adapter *adapter)
293{
294 int i;
295
Thomas Falcon032c5e82015-12-21 11:26:06 -0600296 adapter->replenish_task_cycles++;
297 for (i = 0; i < be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
298 i++) {
299 if (adapter->rx_pool[i].active)
300 replenish_rx_pool(adapter, &adapter->rx_pool[i]);
301 }
302}
303
Nathan Fontenot7bbc27a2017-03-30 02:49:23 -0400304static void release_stats_token(struct ibmvnic_adapter *adapter)
305{
306 struct device *dev = &adapter->vdev->dev;
307
308 if (!adapter->stats_token)
309 return;
310
311 dma_unmap_single(dev, adapter->stats_token,
312 sizeof(struct ibmvnic_statistics),
313 DMA_FROM_DEVICE);
314 adapter->stats_token = 0;
315}
316
317static int init_stats_token(struct ibmvnic_adapter *adapter)
318{
319 struct device *dev = &adapter->vdev->dev;
320 dma_addr_t stok;
321
322 stok = dma_map_single(dev, &adapter->stats,
323 sizeof(struct ibmvnic_statistics),
324 DMA_FROM_DEVICE);
325 if (dma_mapping_error(dev, stok)) {
326 dev_err(dev, "Couldn't map stats buffer\n");
327 return -1;
328 }
329
330 adapter->stats_token = stok;
331 return 0;
332}
333
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400334static void release_rx_pools(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -0600335{
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400336 struct ibmvnic_rx_pool *rx_pool;
337 int rx_scrqs;
338 int i, j;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600339
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400340 if (!adapter->rx_pool)
Thomas Falcon032c5e82015-12-21 11:26:06 -0600341 return;
342
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400343 rx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
344 for (i = 0; i < rx_scrqs; i++) {
345 rx_pool = &adapter->rx_pool[i];
346
347 kfree(rx_pool->free_map);
348 free_long_term_buff(adapter, &rx_pool->long_term_buff);
349
350 if (!rx_pool->rx_buff)
Nathan Fontenote0ebe9422017-05-03 14:04:50 -0400351 continue;
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400352
353 for (j = 0; j < rx_pool->size; j++) {
354 if (rx_pool->rx_buff[j].skb) {
355 dev_kfree_skb_any(rx_pool->rx_buff[i].skb);
356 rx_pool->rx_buff[i].skb = NULL;
357 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600358 }
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400359
360 kfree(rx_pool->rx_buff);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600361 }
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400362
363 kfree(adapter->rx_pool);
364 adapter->rx_pool = NULL;
365}
366
367static int init_rx_pools(struct net_device *netdev)
368{
369 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
370 struct device *dev = &adapter->vdev->dev;
371 struct ibmvnic_rx_pool *rx_pool;
372 int rxadd_subcrqs;
373 u64 *size_array;
374 int i, j;
375
376 rxadd_subcrqs =
377 be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
378 size_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
379 be32_to_cpu(adapter->login_rsp_buf->off_rxadd_buff_size));
380
381 adapter->rx_pool = kcalloc(rxadd_subcrqs,
382 sizeof(struct ibmvnic_rx_pool),
383 GFP_KERNEL);
384 if (!adapter->rx_pool) {
385 dev_err(dev, "Failed to allocate rx pools\n");
386 return -1;
387 }
388
389 for (i = 0; i < rxadd_subcrqs; i++) {
390 rx_pool = &adapter->rx_pool[i];
391
392 netdev_dbg(adapter->netdev,
393 "Initializing rx_pool %d, %lld buffs, %lld bytes each\n",
394 i, adapter->req_rx_add_entries_per_subcrq,
395 be64_to_cpu(size_array[i]));
396
397 rx_pool->size = adapter->req_rx_add_entries_per_subcrq;
398 rx_pool->index = i;
399 rx_pool->buff_size = be64_to_cpu(size_array[i]);
400 rx_pool->active = 1;
401
402 rx_pool->free_map = kcalloc(rx_pool->size, sizeof(int),
403 GFP_KERNEL);
404 if (!rx_pool->free_map) {
405 release_rx_pools(adapter);
406 return -1;
407 }
408
409 rx_pool->rx_buff = kcalloc(rx_pool->size,
410 sizeof(struct ibmvnic_rx_buff),
411 GFP_KERNEL);
412 if (!rx_pool->rx_buff) {
413 dev_err(dev, "Couldn't alloc rx buffers\n");
414 release_rx_pools(adapter);
415 return -1;
416 }
417
418 if (alloc_long_term_buff(adapter, &rx_pool->long_term_buff,
419 rx_pool->size * rx_pool->buff_size)) {
420 release_rx_pools(adapter);
421 return -1;
422 }
423
424 for (j = 0; j < rx_pool->size; ++j)
425 rx_pool->free_map[j] = j;
426
427 atomic_set(&rx_pool->available, 0);
428 rx_pool->next_alloc = 0;
429 rx_pool->next_free = 0;
430 }
431
432 return 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600433}
434
Nathan Fontenotc657e322017-03-30 02:49:06 -0400435static void release_tx_pools(struct ibmvnic_adapter *adapter)
436{
437 struct ibmvnic_tx_pool *tx_pool;
438 int i, tx_scrqs;
439
440 if (!adapter->tx_pool)
441 return;
442
443 tx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
444 for (i = 0; i < tx_scrqs; i++) {
445 tx_pool = &adapter->tx_pool[i];
446 kfree(tx_pool->tx_buff);
447 free_long_term_buff(adapter, &tx_pool->long_term_buff);
448 kfree(tx_pool->free_map);
449 }
450
451 kfree(adapter->tx_pool);
452 adapter->tx_pool = NULL;
453}
454
455static int init_tx_pools(struct net_device *netdev)
456{
457 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
458 struct device *dev = &adapter->vdev->dev;
459 struct ibmvnic_tx_pool *tx_pool;
460 int tx_subcrqs;
461 int i, j;
462
463 tx_subcrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
464 adapter->tx_pool = kcalloc(tx_subcrqs,
465 sizeof(struct ibmvnic_tx_pool), GFP_KERNEL);
466 if (!adapter->tx_pool)
467 return -1;
468
469 for (i = 0; i < tx_subcrqs; i++) {
470 tx_pool = &adapter->tx_pool[i];
471 tx_pool->tx_buff = kcalloc(adapter->req_tx_entries_per_subcrq,
472 sizeof(struct ibmvnic_tx_buff),
473 GFP_KERNEL);
474 if (!tx_pool->tx_buff) {
475 dev_err(dev, "tx pool buffer allocation failed\n");
476 release_tx_pools(adapter);
477 return -1;
478 }
479
480 if (alloc_long_term_buff(adapter, &tx_pool->long_term_buff,
481 adapter->req_tx_entries_per_subcrq *
482 adapter->req_mtu)) {
483 release_tx_pools(adapter);
484 return -1;
485 }
486
487 tx_pool->free_map = kcalloc(adapter->req_tx_entries_per_subcrq,
488 sizeof(int), GFP_KERNEL);
489 if (!tx_pool->free_map) {
490 release_tx_pools(adapter);
491 return -1;
492 }
493
494 for (j = 0; j < adapter->req_tx_entries_per_subcrq; j++)
495 tx_pool->free_map[j] = j;
496
497 tx_pool->consumer_index = 0;
498 tx_pool->producer_index = 0;
499 }
500
501 return 0;
502}
503
Nathan Fontenot661a2622017-04-19 13:44:58 -0400504static void release_error_buffers(struct ibmvnic_adapter *adapter)
505{
506 struct device *dev = &adapter->vdev->dev;
507 struct ibmvnic_error_buff *error_buff, *tmp;
508 unsigned long flags;
509
510 spin_lock_irqsave(&adapter->error_list_lock, flags);
511 list_for_each_entry_safe(error_buff, tmp, &adapter->errors, list) {
512 list_del(&error_buff->list);
513 dma_unmap_single(dev, error_buff->dma, error_buff->len,
514 DMA_FROM_DEVICE);
515 kfree(error_buff->buff);
516 kfree(error_buff);
517 }
518 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
519}
520
John Allend944c3d62017-05-26 10:30:13 -0400521static void ibmvnic_napi_enable(struct ibmvnic_adapter *adapter)
522{
523 int i;
524
525 if (adapter->napi_enabled)
526 return;
527
528 for (i = 0; i < adapter->req_rx_queues; i++)
529 napi_enable(&adapter->napi[i]);
530
531 adapter->napi_enabled = true;
532}
533
534static void ibmvnic_napi_disable(struct ibmvnic_adapter *adapter)
535{
536 int i;
537
538 if (!adapter->napi_enabled)
539 return;
540
541 for (i = 0; i < adapter->req_rx_queues; i++)
542 napi_disable(&adapter->napi[i]);
543
544 adapter->napi_enabled = false;
545}
546
John Allena57a5d22017-03-17 17:13:41 -0500547static int ibmvnic_login(struct net_device *netdev)
Thomas Falcon032c5e82015-12-21 11:26:06 -0600548{
549 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
John Allenbd0b6722017-03-17 17:13:40 -0500550 unsigned long timeout = msecs_to_jiffies(30000);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600551 struct device *dev = &adapter->vdev->dev;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600552
John Allenbd0b6722017-03-17 17:13:40 -0500553 do {
554 if (adapter->renegotiate) {
555 adapter->renegotiate = false;
Nathan Fontenotb5108882017-03-30 02:49:18 -0400556 release_sub_crqs(adapter);
John Allenbd0b6722017-03-17 17:13:40 -0500557
558 reinit_completion(&adapter->init_done);
559 send_cap_queries(adapter);
560 if (!wait_for_completion_timeout(&adapter->init_done,
561 timeout)) {
562 dev_err(dev, "Capabilities query timeout\n");
563 return -1;
564 }
565 }
566
567 reinit_completion(&adapter->init_done);
568 send_login(adapter);
569 if (!wait_for_completion_timeout(&adapter->init_done,
570 timeout)) {
571 dev_err(dev, "Login timeout\n");
572 return -1;
573 }
574 } while (adapter->renegotiate);
575
John Allena57a5d22017-03-17 17:13:41 -0500576 return 0;
577}
578
Nathan Fontenot1b8955e2017-03-30 02:49:29 -0400579static void release_resources(struct ibmvnic_adapter *adapter)
580{
Nathan Fontenotc7bac002017-05-03 14:04:44 -0400581 int i;
582
Nathan Fontenot1b8955e2017-03-30 02:49:29 -0400583 release_tx_pools(adapter);
584 release_rx_pools(adapter);
585
Nathan Fontenot1b8955e2017-03-30 02:49:29 -0400586 release_stats_token(adapter);
Nathan Fontenot661a2622017-04-19 13:44:58 -0400587 release_error_buffers(adapter);
Nathan Fontenotc7bac002017-05-03 14:04:44 -0400588
589 if (adapter->napi) {
590 for (i = 0; i < adapter->req_rx_queues; i++) {
591 if (&adapter->napi[i])
592 netif_napi_del(&adapter->napi[i]);
593 }
594 }
Nathan Fontenot1b8955e2017-03-30 02:49:29 -0400595}
596
Nathan Fontenot53da09e2017-04-21 15:39:04 -0400597static int set_link_state(struct ibmvnic_adapter *adapter, u8 link_state)
598{
599 struct net_device *netdev = adapter->netdev;
600 unsigned long timeout = msecs_to_jiffies(30000);
601 union ibmvnic_crq crq;
602 bool resend;
603 int rc;
604
Nathan Fontenot53da09e2017-04-21 15:39:04 -0400605 netdev_err(netdev, "setting link state %d\n", link_state);
606 memset(&crq, 0, sizeof(crq));
607 crq.logical_link_state.first = IBMVNIC_CRQ_CMD;
608 crq.logical_link_state.cmd = LOGICAL_LINK_STATE;
609 crq.logical_link_state.link_state = link_state;
610
611 do {
612 resend = false;
613
614 reinit_completion(&adapter->init_done);
615 rc = ibmvnic_send_crq(adapter, &crq);
616 if (rc) {
617 netdev_err(netdev, "Failed to set link state\n");
618 return rc;
619 }
620
621 if (!wait_for_completion_timeout(&adapter->init_done,
622 timeout)) {
623 netdev_err(netdev, "timeout setting link state\n");
624 return -1;
625 }
626
627 if (adapter->init_done_rc == 1) {
628 /* Partuial success, delay and re-send */
629 mdelay(1000);
630 resend = true;
631 }
632 } while (resend);
633
634 return 0;
635}
636
Thomas Falcon7f3c6e62017-04-21 15:38:40 -0400637static int set_real_num_queues(struct net_device *netdev)
638{
639 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
640 int rc;
641
642 rc = netif_set_real_num_tx_queues(netdev, adapter->req_tx_queues);
643 if (rc) {
644 netdev_err(netdev, "failed to set the number of tx queues\n");
645 return rc;
646 }
647
648 rc = netif_set_real_num_rx_queues(netdev, adapter->req_rx_queues);
649 if (rc)
650 netdev_err(netdev, "failed to set the number of rx queues\n");
651
652 return rc;
653}
654
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400655static int init_resources(struct ibmvnic_adapter *adapter)
John Allena57a5d22017-03-17 17:13:41 -0500656{
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400657 struct net_device *netdev = adapter->netdev;
658 int i, rc;
John Allena57a5d22017-03-17 17:13:41 -0500659
Thomas Falcon7f3c6e62017-04-21 15:38:40 -0400660 rc = set_real_num_queues(netdev);
661 if (rc)
662 return rc;
John Allenbd0b6722017-03-17 17:13:40 -0500663
664 rc = init_sub_crq_irqs(adapter);
665 if (rc) {
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400666 netdev_err(netdev, "failed to initialize sub crq irqs\n");
John Allenbd0b6722017-03-17 17:13:40 -0500667 return -1;
668 }
669
Nathan Fontenot5d5e84e2017-04-21 15:38:58 -0400670 rc = init_stats_token(adapter);
671 if (rc)
672 return rc;
673
Thomas Falcon032c5e82015-12-21 11:26:06 -0600674 adapter->map_id = 1;
675 adapter->napi = kcalloc(adapter->req_rx_queues,
676 sizeof(struct napi_struct), GFP_KERNEL);
677 if (!adapter->napi)
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400678 return -ENOMEM;
679
Thomas Falcon032c5e82015-12-21 11:26:06 -0600680 for (i = 0; i < adapter->req_rx_queues; i++) {
681 netif_napi_add(netdev, &adapter->napi[i], ibmvnic_poll,
682 NAPI_POLL_WEIGHT);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600683 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600684
Thomas Falcon032c5e82015-12-21 11:26:06 -0600685 send_map_query(adapter);
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400686
687 rc = init_rx_pools(netdev);
688 if (rc)
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400689 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600690
Nathan Fontenotc657e322017-03-30 02:49:06 -0400691 rc = init_tx_pools(netdev);
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400692 return rc;
693}
694
Nathan Fontenoted651a12017-05-03 14:04:38 -0400695static int __ibmvnic_open(struct net_device *netdev)
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400696{
697 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Nathan Fontenoted651a12017-05-03 14:04:38 -0400698 enum vnic_state prev_state = adapter->state;
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400699 int i, rc;
700
Nathan Fontenot90c80142017-05-03 14:04:32 -0400701 adapter->state = VNIC_OPENING;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600702 replenish_pools(adapter);
John Allend944c3d62017-05-26 10:30:13 -0400703 ibmvnic_napi_enable(adapter);
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400704
Thomas Falcon032c5e82015-12-21 11:26:06 -0600705 /* We're ready to receive frames, enable the sub-crq interrupts and
706 * set the logical link state to up
707 */
Nathan Fontenoted651a12017-05-03 14:04:38 -0400708 for (i = 0; i < adapter->req_rx_queues; i++) {
709 if (prev_state == VNIC_CLOSED)
710 enable_irq(adapter->rx_scrq[i]->irq);
711 else
712 enable_scrq_irq(adapter, adapter->rx_scrq[i]);
713 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600714
Nathan Fontenoted651a12017-05-03 14:04:38 -0400715 for (i = 0; i < adapter->req_tx_queues; i++) {
716 if (prev_state == VNIC_CLOSED)
717 enable_irq(adapter->tx_scrq[i]->irq);
718 else
719 enable_scrq_irq(adapter, adapter->tx_scrq[i]);
720 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600721
Nathan Fontenot53da09e2017-04-21 15:39:04 -0400722 rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_UP);
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400723 if (rc) {
724 for (i = 0; i < adapter->req_rx_queues; i++)
725 napi_disable(&adapter->napi[i]);
726 release_resources(adapter);
Nathan Fontenoted651a12017-05-03 14:04:38 -0400727 return rc;
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400728 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600729
Nathan Fontenoted651a12017-05-03 14:04:38 -0400730 netif_tx_start_all_queues(netdev);
731
732 if (prev_state == VNIC_CLOSED) {
733 for (i = 0; i < adapter->req_rx_queues; i++)
734 napi_schedule(&adapter->napi[i]);
735 }
736
737 adapter->state = VNIC_OPEN;
738 return rc;
739}
740
741static int ibmvnic_open(struct net_device *netdev)
742{
743 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
744 int rc;
745
746 mutex_lock(&adapter->reset_lock);
747
748 if (adapter->state != VNIC_CLOSED) {
749 rc = ibmvnic_login(netdev);
750 if (rc) {
751 mutex_unlock(&adapter->reset_lock);
752 return rc;
753 }
754
755 rc = init_resources(adapter);
756 if (rc) {
757 netdev_err(netdev, "failed to initialize resources\n");
758 release_resources(adapter);
759 mutex_unlock(&adapter->reset_lock);
760 return rc;
761 }
762 }
763
764 rc = __ibmvnic_open(netdev);
765 mutex_unlock(&adapter->reset_lock);
766
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400767 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600768}
769
Nathan Fontenotb41b83e2017-05-03 14:04:56 -0400770static void clean_tx_pools(struct ibmvnic_adapter *adapter)
771{
772 struct ibmvnic_tx_pool *tx_pool;
773 u64 tx_entries;
774 int tx_scrqs;
775 int i, j;
776
777 if (!adapter->tx_pool)
778 return;
779
780 tx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
781 tx_entries = adapter->req_tx_entries_per_subcrq;
782
783 /* Free any remaining skbs in the tx buffer pools */
784 for (i = 0; i < tx_scrqs; i++) {
785 tx_pool = &adapter->tx_pool[i];
786 if (!tx_pool)
787 continue;
788
789 for (j = 0; j < tx_entries; j++) {
790 if (tx_pool->tx_buff[j].skb) {
791 dev_kfree_skb_any(tx_pool->tx_buff[j].skb);
792 tx_pool->tx_buff[j].skb = NULL;
793 }
794 }
795 }
796}
797
Nathan Fontenoted651a12017-05-03 14:04:38 -0400798static int __ibmvnic_close(struct net_device *netdev)
John Allenea5509f2017-03-17 17:13:43 -0500799{
800 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Nathan Fontenot53da09e2017-04-21 15:39:04 -0400801 int rc = 0;
John Allenea5509f2017-03-17 17:13:43 -0500802 int i;
803
Nathan Fontenot90c80142017-05-03 14:04:32 -0400804 adapter->state = VNIC_CLOSING;
Nathan Fontenoted651a12017-05-03 14:04:38 -0400805 netif_tx_stop_all_queues(netdev);
John Allend944c3d62017-05-26 10:30:13 -0400806 ibmvnic_napi_disable(adapter);
Nathan Fontenot46293b92017-05-03 14:05:02 -0400807
808 if (adapter->tx_scrq) {
809 for (i = 0; i < adapter->req_tx_queues; i++)
810 if (adapter->tx_scrq[i]->irq)
811 disable_irq(adapter->tx_scrq[i]->irq);
812 }
813
Nathan Fontenot53da09e2017-04-21 15:39:04 -0400814 rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_DN);
Nathan Fontenot46293b92017-05-03 14:05:02 -0400815 if (rc)
816 return rc;
817
818 if (adapter->rx_scrq) {
819 for (i = 0; i < adapter->req_rx_queues; i++) {
820 int retries = 10;
821
822 while (pending_scrq(adapter, adapter->rx_scrq[i])) {
823 retries--;
824 mdelay(100);
825
826 if (retries == 0)
827 break;
828 }
829
830 if (adapter->rx_scrq[i]->irq)
831 disable_irq(adapter->rx_scrq[i]->irq);
832 }
833 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600834
Thomas Falcon10f76212017-05-26 10:30:31 -0400835 clean_tx_pools(adapter);
Nathan Fontenot90c80142017-05-03 14:04:32 -0400836 adapter->state = VNIC_CLOSED;
Nathan Fontenot53da09e2017-04-21 15:39:04 -0400837 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600838}
839
Nathan Fontenoted651a12017-05-03 14:04:38 -0400840static int ibmvnic_close(struct net_device *netdev)
841{
842 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
843 int rc;
844
845 mutex_lock(&adapter->reset_lock);
846 rc = __ibmvnic_close(netdev);
847 mutex_unlock(&adapter->reset_lock);
848
849 return rc;
850}
851
Thomas Falconad7775d2016-04-01 17:20:34 -0500852/**
853 * build_hdr_data - creates L2/L3/L4 header data buffer
854 * @hdr_field - bitfield determining needed headers
855 * @skb - socket buffer
856 * @hdr_len - array of header lengths
857 * @tot_len - total length of data
858 *
859 * Reads hdr_field to determine which headers are needed by firmware.
860 * Builds a buffer containing these headers. Saves individual header
861 * lengths and total buffer length to be used to build descriptors.
862 */
863static int build_hdr_data(u8 hdr_field, struct sk_buff *skb,
864 int *hdr_len, u8 *hdr_data)
865{
866 int len = 0;
867 u8 *hdr;
868
869 hdr_len[0] = sizeof(struct ethhdr);
870
871 if (skb->protocol == htons(ETH_P_IP)) {
872 hdr_len[1] = ip_hdr(skb)->ihl * 4;
873 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
874 hdr_len[2] = tcp_hdrlen(skb);
875 else if (ip_hdr(skb)->protocol == IPPROTO_UDP)
876 hdr_len[2] = sizeof(struct udphdr);
877 } else if (skb->protocol == htons(ETH_P_IPV6)) {
878 hdr_len[1] = sizeof(struct ipv6hdr);
879 if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
880 hdr_len[2] = tcp_hdrlen(skb);
881 else if (ipv6_hdr(skb)->nexthdr == IPPROTO_UDP)
882 hdr_len[2] = sizeof(struct udphdr);
883 }
884
885 memset(hdr_data, 0, 120);
886 if ((hdr_field >> 6) & 1) {
887 hdr = skb_mac_header(skb);
888 memcpy(hdr_data, hdr, hdr_len[0]);
889 len += hdr_len[0];
890 }
891
892 if ((hdr_field >> 5) & 1) {
893 hdr = skb_network_header(skb);
894 memcpy(hdr_data + len, hdr, hdr_len[1]);
895 len += hdr_len[1];
896 }
897
898 if ((hdr_field >> 4) & 1) {
899 hdr = skb_transport_header(skb);
900 memcpy(hdr_data + len, hdr, hdr_len[2]);
901 len += hdr_len[2];
902 }
903 return len;
904}
905
906/**
907 * create_hdr_descs - create header and header extension descriptors
908 * @hdr_field - bitfield determining needed headers
909 * @data - buffer containing header data
910 * @len - length of data buffer
911 * @hdr_len - array of individual header lengths
912 * @scrq_arr - descriptor array
913 *
914 * Creates header and, if needed, header extension descriptors and
915 * places them in a descriptor array, scrq_arr
916 */
917
918static void create_hdr_descs(u8 hdr_field, u8 *hdr_data, int len, int *hdr_len,
919 union sub_crq *scrq_arr)
920{
921 union sub_crq hdr_desc;
922 int tmp_len = len;
923 u8 *data, *cur;
924 int tmp;
925
926 while (tmp_len > 0) {
927 cur = hdr_data + len - tmp_len;
928
929 memset(&hdr_desc, 0, sizeof(hdr_desc));
930 if (cur != hdr_data) {
931 data = hdr_desc.hdr_ext.data;
932 tmp = tmp_len > 29 ? 29 : tmp_len;
933 hdr_desc.hdr_ext.first = IBMVNIC_CRQ_CMD;
934 hdr_desc.hdr_ext.type = IBMVNIC_HDR_EXT_DESC;
935 hdr_desc.hdr_ext.len = tmp;
936 } else {
937 data = hdr_desc.hdr.data;
938 tmp = tmp_len > 24 ? 24 : tmp_len;
939 hdr_desc.hdr.first = IBMVNIC_CRQ_CMD;
940 hdr_desc.hdr.type = IBMVNIC_HDR_DESC;
941 hdr_desc.hdr.len = tmp;
942 hdr_desc.hdr.l2_len = (u8)hdr_len[0];
943 hdr_desc.hdr.l3_len = cpu_to_be16((u16)hdr_len[1]);
944 hdr_desc.hdr.l4_len = (u8)hdr_len[2];
945 hdr_desc.hdr.flag = hdr_field << 1;
946 }
947 memcpy(data, cur, tmp);
948 tmp_len -= tmp;
949 *scrq_arr = hdr_desc;
950 scrq_arr++;
951 }
952}
953
954/**
955 * build_hdr_descs_arr - build a header descriptor array
956 * @skb - socket buffer
957 * @num_entries - number of descriptors to be sent
958 * @subcrq - first TX descriptor
959 * @hdr_field - bit field determining which headers will be sent
960 *
961 * This function will build a TX descriptor array with applicable
962 * L2/L3/L4 packet header descriptors to be sent by send_subcrq_indirect.
963 */
964
965static void build_hdr_descs_arr(struct ibmvnic_tx_buff *txbuff,
966 int *num_entries, u8 hdr_field)
967{
968 int hdr_len[3] = {0, 0, 0};
969 int tot_len, len;
970 u8 *hdr_data = txbuff->hdr_data;
971
972 tot_len = build_hdr_data(hdr_field, txbuff->skb, hdr_len,
973 txbuff->hdr_data);
974 len = tot_len;
975 len -= 24;
976 if (len > 0)
977 num_entries += len % 29 ? len / 29 + 1 : len / 29;
978 create_hdr_descs(hdr_field, hdr_data, tot_len, hdr_len,
979 txbuff->indir_arr + 1);
980}
981
Thomas Falcon032c5e82015-12-21 11:26:06 -0600982static int ibmvnic_xmit(struct sk_buff *skb, struct net_device *netdev)
983{
984 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
985 int queue_num = skb_get_queue_mapping(skb);
Thomas Falconad7775d2016-04-01 17:20:34 -0500986 u8 *hdrs = (u8 *)&adapter->tx_rx_desc_req;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600987 struct device *dev = &adapter->vdev->dev;
988 struct ibmvnic_tx_buff *tx_buff = NULL;
Thomas Falcon142c0ac2017-03-05 12:18:41 -0600989 struct ibmvnic_sub_crq_queue *tx_scrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600990 struct ibmvnic_tx_pool *tx_pool;
991 unsigned int tx_send_failed = 0;
992 unsigned int tx_map_failed = 0;
993 unsigned int tx_dropped = 0;
994 unsigned int tx_packets = 0;
995 unsigned int tx_bytes = 0;
996 dma_addr_t data_dma_addr;
997 struct netdev_queue *txq;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600998 unsigned long lpar_rc;
999 union sub_crq tx_crq;
1000 unsigned int offset;
Thomas Falconad7775d2016-04-01 17:20:34 -05001001 int num_entries = 1;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001002 unsigned char *dst;
1003 u64 *handle_array;
1004 int index = 0;
1005 int ret = 0;
1006
Nathan Fontenoted651a12017-05-03 14:04:38 -04001007 if (adapter->resetting) {
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001008 if (!netif_subqueue_stopped(netdev, skb))
1009 netif_stop_subqueue(netdev, queue_num);
1010 dev_kfree_skb_any(skb);
1011
Thomas Falcon032c5e82015-12-21 11:26:06 -06001012 tx_send_failed++;
1013 tx_dropped++;
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001014 ret = NETDEV_TX_OK;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001015 goto out;
1016 }
1017
Nathan Fontenot161b8a82017-05-03 14:05:08 -04001018 tx_pool = &adapter->tx_pool[queue_num];
1019 tx_scrq = adapter->tx_scrq[queue_num];
1020 txq = netdev_get_tx_queue(netdev, skb_get_queue_mapping(skb));
1021 handle_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
1022 be32_to_cpu(adapter->login_rsp_buf->off_txsubm_subcrqs));
1023
Thomas Falcon032c5e82015-12-21 11:26:06 -06001024 index = tx_pool->free_map[tx_pool->consumer_index];
1025 offset = index * adapter->req_mtu;
1026 dst = tx_pool->long_term_buff.buff + offset;
1027 memset(dst, 0, adapter->req_mtu);
1028 skb_copy_from_linear_data(skb, dst, skb->len);
1029 data_dma_addr = tx_pool->long_term_buff.addr + offset;
1030
1031 tx_pool->consumer_index =
1032 (tx_pool->consumer_index + 1) %
Thomas Falcon068d9f92017-03-05 12:18:42 -06001033 adapter->req_tx_entries_per_subcrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001034
1035 tx_buff = &tx_pool->tx_buff[index];
1036 tx_buff->skb = skb;
1037 tx_buff->data_dma[0] = data_dma_addr;
1038 tx_buff->data_len[0] = skb->len;
1039 tx_buff->index = index;
1040 tx_buff->pool_index = queue_num;
1041 tx_buff->last_frag = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001042
1043 memset(&tx_crq, 0, sizeof(tx_crq));
1044 tx_crq.v1.first = IBMVNIC_CRQ_CMD;
1045 tx_crq.v1.type = IBMVNIC_TX_DESC;
1046 tx_crq.v1.n_crq_elem = 1;
1047 tx_crq.v1.n_sge = 1;
1048 tx_crq.v1.flags1 = IBMVNIC_TX_COMP_NEEDED;
1049 tx_crq.v1.correlator = cpu_to_be32(index);
1050 tx_crq.v1.dma_reg = cpu_to_be16(tx_pool->long_term_buff.map_id);
1051 tx_crq.v1.sge_len = cpu_to_be32(skb->len);
1052 tx_crq.v1.ioba = cpu_to_be64(data_dma_addr);
1053
1054 if (adapter->vlan_header_insertion) {
1055 tx_crq.v1.flags2 |= IBMVNIC_TX_VLAN_INSERT;
1056 tx_crq.v1.vlan_id = cpu_to_be16(skb->vlan_tci);
1057 }
1058
1059 if (skb->protocol == htons(ETH_P_IP)) {
1060 if (ip_hdr(skb)->version == 4)
1061 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV4;
1062 else if (ip_hdr(skb)->version == 6)
1063 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV6;
1064
1065 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
1066 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_TCP;
1067 else if (ip_hdr(skb)->protocol != IPPROTO_TCP)
1068 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_UDP;
1069 }
1070
Thomas Falconad7775d2016-04-01 17:20:34 -05001071 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06001072 tx_crq.v1.flags1 |= IBMVNIC_TX_CHKSUM_OFFLOAD;
Thomas Falconad7775d2016-04-01 17:20:34 -05001073 hdrs += 2;
1074 }
1075 /* determine if l2/3/4 headers are sent to firmware */
1076 if ((*hdrs >> 7) & 1 &&
1077 (skb->protocol == htons(ETH_P_IP) ||
1078 skb->protocol == htons(ETH_P_IPV6))) {
1079 build_hdr_descs_arr(tx_buff, &num_entries, *hdrs);
1080 tx_crq.v1.n_crq_elem = num_entries;
1081 tx_buff->indir_arr[0] = tx_crq;
1082 tx_buff->indir_dma = dma_map_single(dev, tx_buff->indir_arr,
1083 sizeof(tx_buff->indir_arr),
1084 DMA_TO_DEVICE);
1085 if (dma_mapping_error(dev, tx_buff->indir_dma)) {
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001086 dev_kfree_skb_any(skb);
1087 tx_buff->skb = NULL;
Thomas Falconad7775d2016-04-01 17:20:34 -05001088 if (!firmware_has_feature(FW_FEATURE_CMO))
1089 dev_err(dev, "tx: unable to map descriptor array\n");
1090 tx_map_failed++;
1091 tx_dropped++;
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001092 ret = NETDEV_TX_OK;
Thomas Falconad7775d2016-04-01 17:20:34 -05001093 goto out;
1094 }
John Allen498cd8e2016-04-06 11:49:55 -05001095 lpar_rc = send_subcrq_indirect(adapter, handle_array[queue_num],
Thomas Falconad7775d2016-04-01 17:20:34 -05001096 (u64)tx_buff->indir_dma,
1097 (u64)num_entries);
1098 } else {
John Allen498cd8e2016-04-06 11:49:55 -05001099 lpar_rc = send_subcrq(adapter, handle_array[queue_num],
1100 &tx_crq);
Thomas Falconad7775d2016-04-01 17:20:34 -05001101 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001102 if (lpar_rc != H_SUCCESS) {
1103 dev_err(dev, "tx failed with code %ld\n", lpar_rc);
1104
1105 if (tx_pool->consumer_index == 0)
1106 tx_pool->consumer_index =
Thomas Falcon068d9f92017-03-05 12:18:42 -06001107 adapter->req_tx_entries_per_subcrq - 1;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001108 else
1109 tx_pool->consumer_index--;
1110
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001111 dev_kfree_skb_any(skb);
1112 tx_buff->skb = NULL;
1113
1114 if (lpar_rc == H_CLOSED)
1115 netif_stop_subqueue(netdev, queue_num);
1116
Thomas Falcon032c5e82015-12-21 11:26:06 -06001117 tx_send_failed++;
1118 tx_dropped++;
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001119 ret = NETDEV_TX_OK;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001120 goto out;
1121 }
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001122
Brian King58c8c0c2017-04-19 13:44:47 -04001123 if (atomic_inc_return(&tx_scrq->used)
1124 >= adapter->req_tx_entries_per_subcrq) {
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001125 netdev_info(netdev, "Stopping queue %d\n", queue_num);
1126 netif_stop_subqueue(netdev, queue_num);
1127 }
1128
Thomas Falcon032c5e82015-12-21 11:26:06 -06001129 tx_packets++;
1130 tx_bytes += skb->len;
1131 txq->trans_start = jiffies;
1132 ret = NETDEV_TX_OK;
1133
1134out:
1135 netdev->stats.tx_dropped += tx_dropped;
1136 netdev->stats.tx_bytes += tx_bytes;
1137 netdev->stats.tx_packets += tx_packets;
1138 adapter->tx_send_failed += tx_send_failed;
1139 adapter->tx_map_failed += tx_map_failed;
1140
1141 return ret;
1142}
1143
1144static void ibmvnic_set_multi(struct net_device *netdev)
1145{
1146 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1147 struct netdev_hw_addr *ha;
1148 union ibmvnic_crq crq;
1149
1150 memset(&crq, 0, sizeof(crq));
1151 crq.request_capability.first = IBMVNIC_CRQ_CMD;
1152 crq.request_capability.cmd = REQUEST_CAPABILITY;
1153
1154 if (netdev->flags & IFF_PROMISC) {
1155 if (!adapter->promisc_supported)
1156 return;
1157 } else {
1158 if (netdev->flags & IFF_ALLMULTI) {
1159 /* Accept all multicast */
1160 memset(&crq, 0, sizeof(crq));
1161 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1162 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1163 crq.multicast_ctrl.flags = IBMVNIC_ENABLE_ALL;
1164 ibmvnic_send_crq(adapter, &crq);
1165 } else if (netdev_mc_empty(netdev)) {
1166 /* Reject all multicast */
1167 memset(&crq, 0, sizeof(crq));
1168 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1169 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1170 crq.multicast_ctrl.flags = IBMVNIC_DISABLE_ALL;
1171 ibmvnic_send_crq(adapter, &crq);
1172 } else {
1173 /* Accept one or more multicast(s) */
1174 netdev_for_each_mc_addr(ha, netdev) {
1175 memset(&crq, 0, sizeof(crq));
1176 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1177 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1178 crq.multicast_ctrl.flags = IBMVNIC_ENABLE_MC;
1179 ether_addr_copy(&crq.multicast_ctrl.mac_addr[0],
1180 ha->addr);
1181 ibmvnic_send_crq(adapter, &crq);
1182 }
1183 }
1184 }
1185}
1186
1187static int ibmvnic_set_mac(struct net_device *netdev, void *p)
1188{
1189 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1190 struct sockaddr *addr = p;
1191 union ibmvnic_crq crq;
1192
1193 if (!is_valid_ether_addr(addr->sa_data))
1194 return -EADDRNOTAVAIL;
1195
1196 memset(&crq, 0, sizeof(crq));
1197 crq.change_mac_addr.first = IBMVNIC_CRQ_CMD;
1198 crq.change_mac_addr.cmd = CHANGE_MAC_ADDR;
1199 ether_addr_copy(&crq.change_mac_addr.mac_addr[0], addr->sa_data);
1200 ibmvnic_send_crq(adapter, &crq);
1201 /* netdev->dev_addr is changed in handle_change_mac_rsp function */
1202 return 0;
1203}
1204
Nathan Fontenoted651a12017-05-03 14:04:38 -04001205/**
1206 * do_reset returns zero if we are able to keep processing reset events, or
1207 * non-zero if we hit a fatal error and must halt.
1208 */
1209static int do_reset(struct ibmvnic_adapter *adapter,
1210 struct ibmvnic_rwi *rwi, u32 reset_state)
1211{
1212 struct net_device *netdev = adapter->netdev;
1213 int i, rc;
1214
1215 netif_carrier_off(netdev);
1216 adapter->reset_reason = rwi->reset_reason;
1217
1218 if (rwi->reset_reason == VNIC_RESET_MOBILITY) {
1219 rc = ibmvnic_reenable_crq_queue(adapter);
1220 if (rc)
1221 return 0;
1222 }
1223
1224 rc = __ibmvnic_close(netdev);
1225 if (rc)
1226 return rc;
1227
John Allen8cb31cf2017-05-26 10:30:37 -04001228 if (adapter->reset_reason != VNIC_RESET_NON_FATAL) {
1229 /* remove the closed state so when we call open it appears
1230 * we are coming from the probed state.
1231 */
Nathan Fontenoted651a12017-05-03 14:04:38 -04001232 adapter->state = VNIC_PROBED;
John Allen8cb31cf2017-05-26 10:30:37 -04001233
1234 release_resources(adapter);
1235 release_sub_crqs(adapter);
1236 release_crq_queue(adapter);
1237
1238 rc = ibmvnic_init(adapter);
1239 if (rc)
1240 return 0;
1241
1242 /* If the adapter was in PROBE state prior to the reset,
1243 * exit here.
1244 */
1245 if (reset_state == VNIC_PROBED)
1246 return 0;
1247
1248 rc = ibmvnic_login(netdev);
1249 if (rc) {
1250 adapter->state = VNIC_PROBED;
1251 return 0;
1252 }
1253
1254 rtnl_lock();
1255 rc = init_resources(adapter);
1256 rtnl_unlock();
1257 if (rc)
1258 return rc;
1259
1260 if (reset_state == VNIC_CLOSED)
1261 return 0;
Nathan Fontenoted651a12017-05-03 14:04:38 -04001262 }
1263
Nathan Fontenoted651a12017-05-03 14:04:38 -04001264 rc = __ibmvnic_open(netdev);
1265 if (rc) {
1266 if (list_empty(&adapter->rwi_list))
1267 adapter->state = VNIC_CLOSED;
1268 else
1269 adapter->state = reset_state;
1270
1271 return 0;
1272 }
1273
1274 netif_carrier_on(netdev);
1275
1276 /* kick napi */
1277 for (i = 0; i < adapter->req_rx_queues; i++)
1278 napi_schedule(&adapter->napi[i]);
1279
John Allen2ce9e4e2017-05-26 10:30:25 -04001280 netdev_notify_peers(netdev);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001281 return 0;
1282}
1283
1284static struct ibmvnic_rwi *get_next_rwi(struct ibmvnic_adapter *adapter)
1285{
1286 struct ibmvnic_rwi *rwi;
1287
1288 mutex_lock(&adapter->rwi_lock);
1289
1290 if (!list_empty(&adapter->rwi_list)) {
1291 rwi = list_first_entry(&adapter->rwi_list, struct ibmvnic_rwi,
1292 list);
1293 list_del(&rwi->list);
1294 } else {
1295 rwi = NULL;
1296 }
1297
1298 mutex_unlock(&adapter->rwi_lock);
1299 return rwi;
1300}
1301
1302static void free_all_rwi(struct ibmvnic_adapter *adapter)
1303{
1304 struct ibmvnic_rwi *rwi;
1305
1306 rwi = get_next_rwi(adapter);
1307 while (rwi) {
1308 kfree(rwi);
1309 rwi = get_next_rwi(adapter);
1310 }
1311}
1312
1313static void __ibmvnic_reset(struct work_struct *work)
1314{
1315 struct ibmvnic_rwi *rwi;
1316 struct ibmvnic_adapter *adapter;
1317 struct net_device *netdev;
1318 u32 reset_state;
1319 int rc;
1320
1321 adapter = container_of(work, struct ibmvnic_adapter, ibmvnic_reset);
1322 netdev = adapter->netdev;
1323
1324 mutex_lock(&adapter->reset_lock);
1325 adapter->resetting = true;
1326 reset_state = adapter->state;
1327
1328 rwi = get_next_rwi(adapter);
1329 while (rwi) {
1330 rc = do_reset(adapter, rwi, reset_state);
1331 kfree(rwi);
1332 if (rc)
1333 break;
1334
1335 rwi = get_next_rwi(adapter);
1336 }
1337
1338 if (rc) {
1339 free_all_rwi(adapter);
Wei Yongjun6d0af072017-05-18 15:24:52 +00001340 mutex_unlock(&adapter->reset_lock);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001341 return;
1342 }
1343
1344 adapter->resetting = false;
1345 mutex_unlock(&adapter->reset_lock);
1346}
1347
1348static void ibmvnic_reset(struct ibmvnic_adapter *adapter,
1349 enum ibmvnic_reset_reason reason)
1350{
1351 struct ibmvnic_rwi *rwi, *tmp;
1352 struct net_device *netdev = adapter->netdev;
1353 struct list_head *entry;
1354
1355 if (adapter->state == VNIC_REMOVING ||
1356 adapter->state == VNIC_REMOVED) {
1357 netdev_dbg(netdev, "Adapter removing, skipping reset\n");
1358 return;
1359 }
1360
1361 mutex_lock(&adapter->rwi_lock);
1362
1363 list_for_each(entry, &adapter->rwi_list) {
1364 tmp = list_entry(entry, struct ibmvnic_rwi, list);
1365 if (tmp->reset_reason == reason) {
1366 netdev_err(netdev, "Matching reset found, skipping\n");
1367 mutex_unlock(&adapter->rwi_lock);
1368 return;
1369 }
1370 }
1371
1372 rwi = kzalloc(sizeof(*rwi), GFP_KERNEL);
1373 if (!rwi) {
1374 mutex_unlock(&adapter->rwi_lock);
1375 ibmvnic_close(netdev);
1376 return;
1377 }
1378
1379 rwi->reset_reason = reason;
1380 list_add_tail(&rwi->list, &adapter->rwi_list);
1381 mutex_unlock(&adapter->rwi_lock);
1382 schedule_work(&adapter->ibmvnic_reset);
1383}
1384
Thomas Falcon032c5e82015-12-21 11:26:06 -06001385static void ibmvnic_tx_timeout(struct net_device *dev)
1386{
1387 struct ibmvnic_adapter *adapter = netdev_priv(dev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001388
Nathan Fontenoted651a12017-05-03 14:04:38 -04001389 ibmvnic_reset(adapter, VNIC_RESET_TIMEOUT);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001390}
1391
1392static void remove_buff_from_pool(struct ibmvnic_adapter *adapter,
1393 struct ibmvnic_rx_buff *rx_buff)
1394{
1395 struct ibmvnic_rx_pool *pool = &adapter->rx_pool[rx_buff->pool_index];
1396
1397 rx_buff->skb = NULL;
1398
1399 pool->free_map[pool->next_alloc] = (int)(rx_buff - pool->rx_buff);
1400 pool->next_alloc = (pool->next_alloc + 1) % pool->size;
1401
1402 atomic_dec(&pool->available);
1403}
1404
1405static int ibmvnic_poll(struct napi_struct *napi, int budget)
1406{
1407 struct net_device *netdev = napi->dev;
1408 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1409 int scrq_num = (int)(napi - adapter->napi);
1410 int frames_processed = 0;
1411restart_poll:
1412 while (frames_processed < budget) {
1413 struct sk_buff *skb;
1414 struct ibmvnic_rx_buff *rx_buff;
1415 union sub_crq *next;
1416 u32 length;
1417 u16 offset;
1418 u8 flags = 0;
1419
1420 if (!pending_scrq(adapter, adapter->rx_scrq[scrq_num]))
1421 break;
1422 next = ibmvnic_next_scrq(adapter, adapter->rx_scrq[scrq_num]);
1423 rx_buff =
1424 (struct ibmvnic_rx_buff *)be64_to_cpu(next->
1425 rx_comp.correlator);
1426 /* do error checking */
1427 if (next->rx_comp.rc) {
1428 netdev_err(netdev, "rx error %x\n", next->rx_comp.rc);
1429 /* free the entry */
1430 next->rx_comp.first = 0;
1431 remove_buff_from_pool(adapter, rx_buff);
Nathan Fontenotca05e312017-05-03 14:05:14 -04001432 continue;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001433 }
1434
1435 length = be32_to_cpu(next->rx_comp.len);
1436 offset = be16_to_cpu(next->rx_comp.off_frame_data);
1437 flags = next->rx_comp.flags;
1438 skb = rx_buff->skb;
1439 skb_copy_to_linear_data(skb, rx_buff->data + offset,
1440 length);
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04001441
1442 /* VLAN Header has been stripped by the system firmware and
1443 * needs to be inserted by the driver
1444 */
1445 if (adapter->rx_vlan_header_insertion &&
1446 (flags & IBMVNIC_VLAN_STRIPPED))
1447 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
1448 ntohs(next->rx_comp.vlan_tci));
1449
Thomas Falcon032c5e82015-12-21 11:26:06 -06001450 /* free the entry */
1451 next->rx_comp.first = 0;
1452 remove_buff_from_pool(adapter, rx_buff);
1453
1454 skb_put(skb, length);
1455 skb->protocol = eth_type_trans(skb, netdev);
Thomas Falcon94ca3052017-05-03 14:05:20 -04001456 skb_record_rx_queue(skb, scrq_num);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001457
1458 if (flags & IBMVNIC_IP_CHKSUM_GOOD &&
1459 flags & IBMVNIC_TCP_UDP_CHKSUM_GOOD) {
1460 skb->ip_summed = CHECKSUM_UNNECESSARY;
1461 }
1462
1463 length = skb->len;
1464 napi_gro_receive(napi, skb); /* send it up */
1465 netdev->stats.rx_packets++;
1466 netdev->stats.rx_bytes += length;
1467 frames_processed++;
1468 }
John Allen498cd8e2016-04-06 11:49:55 -05001469 replenish_rx_pool(adapter, &adapter->rx_pool[scrq_num]);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001470
1471 if (frames_processed < budget) {
1472 enable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
Eric Dumazet6ad20162017-01-30 08:22:01 -08001473 napi_complete_done(napi, frames_processed);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001474 if (pending_scrq(adapter, adapter->rx_scrq[scrq_num]) &&
1475 napi_reschedule(napi)) {
1476 disable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
1477 goto restart_poll;
1478 }
1479 }
1480 return frames_processed;
1481}
1482
1483#ifdef CONFIG_NET_POLL_CONTROLLER
1484static void ibmvnic_netpoll_controller(struct net_device *dev)
1485{
1486 struct ibmvnic_adapter *adapter = netdev_priv(dev);
1487 int i;
1488
1489 replenish_pools(netdev_priv(dev));
1490 for (i = 0; i < adapter->req_rx_queues; i++)
1491 ibmvnic_interrupt_rx(adapter->rx_scrq[i]->irq,
1492 adapter->rx_scrq[i]);
1493}
1494#endif
1495
1496static const struct net_device_ops ibmvnic_netdev_ops = {
1497 .ndo_open = ibmvnic_open,
1498 .ndo_stop = ibmvnic_close,
1499 .ndo_start_xmit = ibmvnic_xmit,
1500 .ndo_set_rx_mode = ibmvnic_set_multi,
1501 .ndo_set_mac_address = ibmvnic_set_mac,
1502 .ndo_validate_addr = eth_validate_addr,
Thomas Falcon032c5e82015-12-21 11:26:06 -06001503 .ndo_tx_timeout = ibmvnic_tx_timeout,
1504#ifdef CONFIG_NET_POLL_CONTROLLER
1505 .ndo_poll_controller = ibmvnic_netpoll_controller,
1506#endif
1507};
1508
1509/* ethtool functions */
1510
Philippe Reynes8a433792017-01-07 22:37:29 +01001511static int ibmvnic_get_link_ksettings(struct net_device *netdev,
1512 struct ethtool_link_ksettings *cmd)
Thomas Falcon032c5e82015-12-21 11:26:06 -06001513{
Philippe Reynes8a433792017-01-07 22:37:29 +01001514 u32 supported, advertising;
1515
1516 supported = (SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg |
Thomas Falcon032c5e82015-12-21 11:26:06 -06001517 SUPPORTED_FIBRE);
Philippe Reynes8a433792017-01-07 22:37:29 +01001518 advertising = (ADVERTISED_1000baseT_Full | ADVERTISED_Autoneg |
Thomas Falcon032c5e82015-12-21 11:26:06 -06001519 ADVERTISED_FIBRE);
Philippe Reynes8a433792017-01-07 22:37:29 +01001520 cmd->base.speed = SPEED_1000;
1521 cmd->base.duplex = DUPLEX_FULL;
1522 cmd->base.port = PORT_FIBRE;
1523 cmd->base.phy_address = 0;
1524 cmd->base.autoneg = AUTONEG_ENABLE;
1525
1526 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
1527 supported);
1528 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
1529 advertising);
1530
Thomas Falcon032c5e82015-12-21 11:26:06 -06001531 return 0;
1532}
1533
1534static void ibmvnic_get_drvinfo(struct net_device *dev,
1535 struct ethtool_drvinfo *info)
1536{
1537 strlcpy(info->driver, ibmvnic_driver_name, sizeof(info->driver));
1538 strlcpy(info->version, IBMVNIC_DRIVER_VERSION, sizeof(info->version));
1539}
1540
1541static u32 ibmvnic_get_msglevel(struct net_device *netdev)
1542{
1543 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1544
1545 return adapter->msg_enable;
1546}
1547
1548static void ibmvnic_set_msglevel(struct net_device *netdev, u32 data)
1549{
1550 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1551
1552 adapter->msg_enable = data;
1553}
1554
1555static u32 ibmvnic_get_link(struct net_device *netdev)
1556{
1557 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1558
1559 /* Don't need to send a query because we request a logical link up at
1560 * init and then we wait for link state indications
1561 */
1562 return adapter->logical_link_state;
1563}
1564
1565static void ibmvnic_get_ringparam(struct net_device *netdev,
1566 struct ethtool_ringparam *ring)
1567{
1568 ring->rx_max_pending = 0;
1569 ring->tx_max_pending = 0;
1570 ring->rx_mini_max_pending = 0;
1571 ring->rx_jumbo_max_pending = 0;
1572 ring->rx_pending = 0;
1573 ring->tx_pending = 0;
1574 ring->rx_mini_pending = 0;
1575 ring->rx_jumbo_pending = 0;
1576}
1577
1578static void ibmvnic_get_strings(struct net_device *dev, u32 stringset, u8 *data)
1579{
1580 int i;
1581
1582 if (stringset != ETH_SS_STATS)
1583 return;
1584
1585 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++, data += ETH_GSTRING_LEN)
1586 memcpy(data, ibmvnic_stats[i].name, ETH_GSTRING_LEN);
1587}
1588
1589static int ibmvnic_get_sset_count(struct net_device *dev, int sset)
1590{
1591 switch (sset) {
1592 case ETH_SS_STATS:
1593 return ARRAY_SIZE(ibmvnic_stats);
1594 default:
1595 return -EOPNOTSUPP;
1596 }
1597}
1598
1599static void ibmvnic_get_ethtool_stats(struct net_device *dev,
1600 struct ethtool_stats *stats, u64 *data)
1601{
1602 struct ibmvnic_adapter *adapter = netdev_priv(dev);
1603 union ibmvnic_crq crq;
1604 int i;
1605
1606 memset(&crq, 0, sizeof(crq));
1607 crq.request_statistics.first = IBMVNIC_CRQ_CMD;
1608 crq.request_statistics.cmd = REQUEST_STATISTICS;
1609 crq.request_statistics.ioba = cpu_to_be32(adapter->stats_token);
1610 crq.request_statistics.len =
1611 cpu_to_be32(sizeof(struct ibmvnic_statistics));
Thomas Falcon032c5e82015-12-21 11:26:06 -06001612
1613 /* Wait for data to be written */
1614 init_completion(&adapter->stats_done);
Nathan Fontenotdb5d0b52017-02-10 13:45:05 -05001615 ibmvnic_send_crq(adapter, &crq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001616 wait_for_completion(&adapter->stats_done);
1617
1618 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++)
1619 data[i] = IBMVNIC_GET_STAT(adapter, ibmvnic_stats[i].offset);
1620}
1621
1622static const struct ethtool_ops ibmvnic_ethtool_ops = {
Thomas Falcon032c5e82015-12-21 11:26:06 -06001623 .get_drvinfo = ibmvnic_get_drvinfo,
1624 .get_msglevel = ibmvnic_get_msglevel,
1625 .set_msglevel = ibmvnic_set_msglevel,
1626 .get_link = ibmvnic_get_link,
1627 .get_ringparam = ibmvnic_get_ringparam,
1628 .get_strings = ibmvnic_get_strings,
1629 .get_sset_count = ibmvnic_get_sset_count,
1630 .get_ethtool_stats = ibmvnic_get_ethtool_stats,
Philippe Reynes8a433792017-01-07 22:37:29 +01001631 .get_link_ksettings = ibmvnic_get_link_ksettings,
Thomas Falcon032c5e82015-12-21 11:26:06 -06001632};
1633
1634/* Routines for managing CRQs/sCRQs */
1635
1636static void release_sub_crq_queue(struct ibmvnic_adapter *adapter,
1637 struct ibmvnic_sub_crq_queue *scrq)
1638{
1639 struct device *dev = &adapter->vdev->dev;
1640 long rc;
1641
1642 netdev_dbg(adapter->netdev, "Releasing sub-CRQ\n");
1643
1644 /* Close the sub-crqs */
1645 do {
1646 rc = plpar_hcall_norets(H_FREE_SUB_CRQ,
1647 adapter->vdev->unit_address,
1648 scrq->crq_num);
1649 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
1650
Thomas Falconffa73852017-04-19 13:44:29 -04001651 if (rc) {
1652 netdev_err(adapter->netdev,
1653 "Failed to release sub-CRQ %16lx, rc = %ld\n",
1654 scrq->crq_num, rc);
1655 }
1656
Thomas Falcon032c5e82015-12-21 11:26:06 -06001657 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
1658 DMA_BIDIRECTIONAL);
1659 free_pages((unsigned long)scrq->msgs, 2);
1660 kfree(scrq);
1661}
1662
1663static struct ibmvnic_sub_crq_queue *init_sub_crq_queue(struct ibmvnic_adapter
1664 *adapter)
1665{
1666 struct device *dev = &adapter->vdev->dev;
1667 struct ibmvnic_sub_crq_queue *scrq;
1668 int rc;
1669
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04001670 scrq = kzalloc(sizeof(*scrq), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001671 if (!scrq)
1672 return NULL;
1673
Nathan Fontenot7f7adc52017-04-19 13:45:16 -04001674 scrq->msgs =
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04001675 (union sub_crq *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, 2);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001676 if (!scrq->msgs) {
1677 dev_warn(dev, "Couldn't allocate crq queue messages page\n");
1678 goto zero_page_failed;
1679 }
1680
1681 scrq->msg_token = dma_map_single(dev, scrq->msgs, 4 * PAGE_SIZE,
1682 DMA_BIDIRECTIONAL);
1683 if (dma_mapping_error(dev, scrq->msg_token)) {
1684 dev_warn(dev, "Couldn't map crq queue messages page\n");
1685 goto map_failed;
1686 }
1687
1688 rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
1689 4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
1690
1691 if (rc == H_RESOURCE)
1692 rc = ibmvnic_reset_crq(adapter);
1693
1694 if (rc == H_CLOSED) {
1695 dev_warn(dev, "Partner adapter not ready, waiting.\n");
1696 } else if (rc) {
1697 dev_warn(dev, "Error %d registering sub-crq\n", rc);
1698 goto reg_failed;
1699 }
1700
Thomas Falcon032c5e82015-12-21 11:26:06 -06001701 scrq->adapter = adapter;
1702 scrq->size = 4 * PAGE_SIZE / sizeof(*scrq->msgs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001703 spin_lock_init(&scrq->lock);
1704
1705 netdev_dbg(adapter->netdev,
1706 "sub-crq initialized, num %lx, hw_irq=%lx, irq=%x\n",
1707 scrq->crq_num, scrq->hw_irq, scrq->irq);
1708
1709 return scrq;
1710
Thomas Falcon032c5e82015-12-21 11:26:06 -06001711reg_failed:
1712 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
1713 DMA_BIDIRECTIONAL);
1714map_failed:
1715 free_pages((unsigned long)scrq->msgs, 2);
1716zero_page_failed:
1717 kfree(scrq);
1718
1719 return NULL;
1720}
1721
1722static void release_sub_crqs(struct ibmvnic_adapter *adapter)
1723{
1724 int i;
1725
1726 if (adapter->tx_scrq) {
Nathan Fontenotb5108882017-03-30 02:49:18 -04001727 for (i = 0; i < adapter->req_tx_queues; i++) {
1728 if (!adapter->tx_scrq[i])
1729 continue;
1730
1731 if (adapter->tx_scrq[i]->irq) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06001732 free_irq(adapter->tx_scrq[i]->irq,
1733 adapter->tx_scrq[i]);
Thomas Falcon88eb98a2016-07-06 15:35:16 -05001734 irq_dispose_mapping(adapter->tx_scrq[i]->irq);
Nathan Fontenotb5108882017-03-30 02:49:18 -04001735 adapter->tx_scrq[i]->irq = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001736 }
Nathan Fontenotb5108882017-03-30 02:49:18 -04001737
1738 release_sub_crq_queue(adapter, adapter->tx_scrq[i]);
1739 }
1740
Nathan Fontenot9501df32017-03-15 23:38:07 -04001741 kfree(adapter->tx_scrq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001742 adapter->tx_scrq = NULL;
1743 }
1744
1745 if (adapter->rx_scrq) {
Nathan Fontenotb5108882017-03-30 02:49:18 -04001746 for (i = 0; i < adapter->req_rx_queues; i++) {
1747 if (!adapter->rx_scrq[i])
1748 continue;
1749
1750 if (adapter->rx_scrq[i]->irq) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06001751 free_irq(adapter->rx_scrq[i]->irq,
1752 adapter->rx_scrq[i]);
Thomas Falcon88eb98a2016-07-06 15:35:16 -05001753 irq_dispose_mapping(adapter->rx_scrq[i]->irq);
Nathan Fontenotb5108882017-03-30 02:49:18 -04001754 adapter->rx_scrq[i]->irq = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001755 }
Nathan Fontenotb5108882017-03-30 02:49:18 -04001756
1757 release_sub_crq_queue(adapter, adapter->rx_scrq[i]);
1758 }
1759
Nathan Fontenot9501df32017-03-15 23:38:07 -04001760 kfree(adapter->rx_scrq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001761 adapter->rx_scrq = NULL;
1762 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001763}
1764
1765static int disable_scrq_irq(struct ibmvnic_adapter *adapter,
1766 struct ibmvnic_sub_crq_queue *scrq)
1767{
1768 struct device *dev = &adapter->vdev->dev;
1769 unsigned long rc;
1770
1771 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
1772 H_DISABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
1773 if (rc)
1774 dev_err(dev, "Couldn't disable scrq irq 0x%lx. rc=%ld\n",
1775 scrq->hw_irq, rc);
1776 return rc;
1777}
1778
1779static int enable_scrq_irq(struct ibmvnic_adapter *adapter,
1780 struct ibmvnic_sub_crq_queue *scrq)
1781{
1782 struct device *dev = &adapter->vdev->dev;
1783 unsigned long rc;
1784
1785 if (scrq->hw_irq > 0x100000000ULL) {
1786 dev_err(dev, "bad hw_irq = %lx\n", scrq->hw_irq);
1787 return 1;
1788 }
1789
1790 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
1791 H_ENABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
1792 if (rc)
1793 dev_err(dev, "Couldn't enable scrq irq 0x%lx. rc=%ld\n",
1794 scrq->hw_irq, rc);
1795 return rc;
1796}
1797
1798static int ibmvnic_complete_tx(struct ibmvnic_adapter *adapter,
1799 struct ibmvnic_sub_crq_queue *scrq)
1800{
1801 struct device *dev = &adapter->vdev->dev;
1802 struct ibmvnic_tx_buff *txbuff;
1803 union sub_crq *next;
1804 int index;
1805 int i, j;
Thomas Falconad7775d2016-04-01 17:20:34 -05001806 u8 first;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001807
1808restart_loop:
1809 while (pending_scrq(adapter, scrq)) {
1810 unsigned int pool = scrq->pool_index;
1811
1812 next = ibmvnic_next_scrq(adapter, scrq);
1813 for (i = 0; i < next->tx_comp.num_comps; i++) {
1814 if (next->tx_comp.rcs[i]) {
1815 dev_err(dev, "tx error %x\n",
1816 next->tx_comp.rcs[i]);
1817 continue;
1818 }
1819 index = be32_to_cpu(next->tx_comp.correlators[i]);
1820 txbuff = &adapter->tx_pool[pool].tx_buff[index];
1821
1822 for (j = 0; j < IBMVNIC_MAX_FRAGS_PER_CRQ; j++) {
1823 if (!txbuff->data_dma[j])
1824 continue;
1825
1826 txbuff->data_dma[j] = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001827 }
Thomas Falconad7775d2016-04-01 17:20:34 -05001828 /* if sub_crq was sent indirectly */
1829 first = txbuff->indir_arr[0].generic.first;
1830 if (first == IBMVNIC_CRQ_CMD) {
1831 dma_unmap_single(dev, txbuff->indir_dma,
1832 sizeof(txbuff->indir_arr),
1833 DMA_TO_DEVICE);
1834 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001835
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001836 if (txbuff->last_frag) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06001837 dev_kfree_skb_any(txbuff->skb);
Nathan Fontenot7c3e7de2017-05-03 14:05:25 -04001838 txbuff->skb = NULL;
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001839 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001840
1841 adapter->tx_pool[pool].free_map[adapter->tx_pool[pool].
1842 producer_index] = index;
1843 adapter->tx_pool[pool].producer_index =
1844 (adapter->tx_pool[pool].producer_index + 1) %
Thomas Falcon068d9f92017-03-05 12:18:42 -06001845 adapter->req_tx_entries_per_subcrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001846 }
1847 /* remove tx_comp scrq*/
1848 next->tx_comp.first = 0;
Nathan Fontenot7c3e7de2017-05-03 14:05:25 -04001849
1850 if (atomic_sub_return(next->tx_comp.num_comps, &scrq->used) <=
1851 (adapter->req_tx_entries_per_subcrq / 2) &&
1852 __netif_subqueue_stopped(adapter->netdev,
1853 scrq->pool_index)) {
1854 netif_wake_subqueue(adapter->netdev, scrq->pool_index);
1855 netdev_info(adapter->netdev, "Started queue %d\n",
1856 scrq->pool_index);
1857 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001858 }
1859
1860 enable_scrq_irq(adapter, scrq);
1861
1862 if (pending_scrq(adapter, scrq)) {
1863 disable_scrq_irq(adapter, scrq);
1864 goto restart_loop;
1865 }
1866
1867 return 0;
1868}
1869
1870static irqreturn_t ibmvnic_interrupt_tx(int irq, void *instance)
1871{
1872 struct ibmvnic_sub_crq_queue *scrq = instance;
1873 struct ibmvnic_adapter *adapter = scrq->adapter;
1874
1875 disable_scrq_irq(adapter, scrq);
1876 ibmvnic_complete_tx(adapter, scrq);
1877
1878 return IRQ_HANDLED;
1879}
1880
1881static irqreturn_t ibmvnic_interrupt_rx(int irq, void *instance)
1882{
1883 struct ibmvnic_sub_crq_queue *scrq = instance;
1884 struct ibmvnic_adapter *adapter = scrq->adapter;
1885
1886 if (napi_schedule_prep(&adapter->napi[scrq->scrq_num])) {
1887 disable_scrq_irq(adapter, scrq);
1888 __napi_schedule(&adapter->napi[scrq->scrq_num]);
1889 }
1890
1891 return IRQ_HANDLED;
1892}
1893
Thomas Falconea22d512016-07-06 15:35:17 -05001894static int init_sub_crq_irqs(struct ibmvnic_adapter *adapter)
1895{
1896 struct device *dev = &adapter->vdev->dev;
1897 struct ibmvnic_sub_crq_queue *scrq;
1898 int i = 0, j = 0;
1899 int rc = 0;
1900
1901 for (i = 0; i < adapter->req_tx_queues; i++) {
1902 scrq = adapter->tx_scrq[i];
1903 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
1904
Michael Ellerman99c17902016-09-10 19:59:05 +10001905 if (!scrq->irq) {
Thomas Falconea22d512016-07-06 15:35:17 -05001906 rc = -EINVAL;
1907 dev_err(dev, "Error mapping irq\n");
1908 goto req_tx_irq_failed;
1909 }
1910
1911 rc = request_irq(scrq->irq, ibmvnic_interrupt_tx,
1912 0, "ibmvnic_tx", scrq);
1913
1914 if (rc) {
1915 dev_err(dev, "Couldn't register tx irq 0x%x. rc=%d\n",
1916 scrq->irq, rc);
1917 irq_dispose_mapping(scrq->irq);
1918 goto req_rx_irq_failed;
1919 }
1920 }
1921
1922 for (i = 0; i < adapter->req_rx_queues; i++) {
1923 scrq = adapter->rx_scrq[i];
1924 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
Michael Ellerman99c17902016-09-10 19:59:05 +10001925 if (!scrq->irq) {
Thomas Falconea22d512016-07-06 15:35:17 -05001926 rc = -EINVAL;
1927 dev_err(dev, "Error mapping irq\n");
1928 goto req_rx_irq_failed;
1929 }
1930 rc = request_irq(scrq->irq, ibmvnic_interrupt_rx,
1931 0, "ibmvnic_rx", scrq);
1932 if (rc) {
1933 dev_err(dev, "Couldn't register rx irq 0x%x. rc=%d\n",
1934 scrq->irq, rc);
1935 irq_dispose_mapping(scrq->irq);
1936 goto req_rx_irq_failed;
1937 }
1938 }
1939 return rc;
1940
1941req_rx_irq_failed:
Thomas Falcon8bf371e2016-10-27 12:28:52 -05001942 for (j = 0; j < i; j++) {
Thomas Falconea22d512016-07-06 15:35:17 -05001943 free_irq(adapter->rx_scrq[j]->irq, adapter->rx_scrq[j]);
1944 irq_dispose_mapping(adapter->rx_scrq[j]->irq);
Thomas Falcon8bf371e2016-10-27 12:28:52 -05001945 }
Thomas Falconea22d512016-07-06 15:35:17 -05001946 i = adapter->req_tx_queues;
1947req_tx_irq_failed:
Thomas Falcon8bf371e2016-10-27 12:28:52 -05001948 for (j = 0; j < i; j++) {
Thomas Falconea22d512016-07-06 15:35:17 -05001949 free_irq(adapter->tx_scrq[j]->irq, adapter->tx_scrq[j]);
1950 irq_dispose_mapping(adapter->rx_scrq[j]->irq);
Thomas Falcon8bf371e2016-10-27 12:28:52 -05001951 }
Nathan Fontenotb5108882017-03-30 02:49:18 -04001952 release_sub_crqs(adapter);
Thomas Falconea22d512016-07-06 15:35:17 -05001953 return rc;
1954}
1955
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04001956static int init_sub_crqs(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06001957{
1958 struct device *dev = &adapter->vdev->dev;
1959 struct ibmvnic_sub_crq_queue **allqueues;
1960 int registered_queues = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001961 int total_queues;
1962 int more = 0;
Thomas Falconea22d512016-07-06 15:35:17 -05001963 int i;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001964
Thomas Falcon032c5e82015-12-21 11:26:06 -06001965 total_queues = adapter->req_tx_queues + adapter->req_rx_queues;
1966
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04001967 allqueues = kcalloc(total_queues, sizeof(*allqueues), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001968 if (!allqueues)
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04001969 return -1;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001970
1971 for (i = 0; i < total_queues; i++) {
1972 allqueues[i] = init_sub_crq_queue(adapter);
1973 if (!allqueues[i]) {
1974 dev_warn(dev, "Couldn't allocate all sub-crqs\n");
1975 break;
1976 }
1977 registered_queues++;
1978 }
1979
1980 /* Make sure we were able to register the minimum number of queues */
1981 if (registered_queues <
1982 adapter->min_tx_queues + adapter->min_rx_queues) {
1983 dev_err(dev, "Fatal: Couldn't init min number of sub-crqs\n");
1984 goto tx_failed;
1985 }
1986
1987 /* Distribute the failed allocated queues*/
1988 for (i = 0; i < total_queues - registered_queues + more ; i++) {
1989 netdev_dbg(adapter->netdev, "Reducing number of queues\n");
1990 switch (i % 3) {
1991 case 0:
1992 if (adapter->req_rx_queues > adapter->min_rx_queues)
1993 adapter->req_rx_queues--;
1994 else
1995 more++;
1996 break;
1997 case 1:
1998 if (adapter->req_tx_queues > adapter->min_tx_queues)
1999 adapter->req_tx_queues--;
2000 else
2001 more++;
2002 break;
2003 }
2004 }
2005
2006 adapter->tx_scrq = kcalloc(adapter->req_tx_queues,
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04002007 sizeof(*adapter->tx_scrq), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002008 if (!adapter->tx_scrq)
2009 goto tx_failed;
2010
2011 for (i = 0; i < adapter->req_tx_queues; i++) {
2012 adapter->tx_scrq[i] = allqueues[i];
2013 adapter->tx_scrq[i]->pool_index = i;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002014 }
2015
2016 adapter->rx_scrq = kcalloc(adapter->req_rx_queues,
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04002017 sizeof(*adapter->rx_scrq), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002018 if (!adapter->rx_scrq)
2019 goto rx_failed;
2020
2021 for (i = 0; i < adapter->req_rx_queues; i++) {
2022 adapter->rx_scrq[i] = allqueues[i + adapter->req_tx_queues];
2023 adapter->rx_scrq[i]->scrq_num = i;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002024 }
2025
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002026 kfree(allqueues);
2027 return 0;
2028
2029rx_failed:
2030 kfree(adapter->tx_scrq);
2031 adapter->tx_scrq = NULL;
2032tx_failed:
2033 for (i = 0; i < registered_queues; i++)
2034 release_sub_crq_queue(adapter, allqueues[i]);
2035 kfree(allqueues);
2036 return -1;
2037}
2038
2039static void ibmvnic_send_req_caps(struct ibmvnic_adapter *adapter, int retry)
2040{
2041 struct device *dev = &adapter->vdev->dev;
2042 union ibmvnic_crq crq;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002043
2044 if (!retry) {
2045 /* Sub-CRQ entries are 32 byte long */
2046 int entries_page = 4 * PAGE_SIZE / (sizeof(u64) * 4);
2047
2048 if (adapter->min_tx_entries_per_subcrq > entries_page ||
2049 adapter->min_rx_add_entries_per_subcrq > entries_page) {
2050 dev_err(dev, "Fatal, invalid entries per sub-crq\n");
2051 return;
2052 }
2053
2054 /* Get the minimum between the queried max and the entries
2055 * that fit in our PAGE_SIZE
2056 */
2057 adapter->req_tx_entries_per_subcrq =
2058 adapter->max_tx_entries_per_subcrq > entries_page ?
2059 entries_page : adapter->max_tx_entries_per_subcrq;
2060 adapter->req_rx_add_entries_per_subcrq =
2061 adapter->max_rx_add_entries_per_subcrq > entries_page ?
2062 entries_page : adapter->max_rx_add_entries_per_subcrq;
2063
2064 adapter->req_tx_queues = adapter->opt_tx_comp_sub_queues;
2065 adapter->req_rx_queues = adapter->opt_rx_comp_queues;
2066 adapter->req_rx_add_queues = adapter->max_rx_add_queues;
2067
2068 adapter->req_mtu = adapter->netdev->mtu + ETH_HLEN;
2069 }
2070
Thomas Falcon032c5e82015-12-21 11:26:06 -06002071 memset(&crq, 0, sizeof(crq));
2072 crq.request_capability.first = IBMVNIC_CRQ_CMD;
2073 crq.request_capability.cmd = REQUEST_CAPABILITY;
2074
2075 crq.request_capability.capability = cpu_to_be16(REQ_TX_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06002076 crq.request_capability.number = cpu_to_be64(adapter->req_tx_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06002077 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002078 ibmvnic_send_crq(adapter, &crq);
2079
2080 crq.request_capability.capability = cpu_to_be16(REQ_RX_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06002081 crq.request_capability.number = cpu_to_be64(adapter->req_rx_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06002082 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002083 ibmvnic_send_crq(adapter, &crq);
2084
2085 crq.request_capability.capability = cpu_to_be16(REQ_RX_ADD_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06002086 crq.request_capability.number = cpu_to_be64(adapter->req_rx_add_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06002087 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002088 ibmvnic_send_crq(adapter, &crq);
2089
2090 crq.request_capability.capability =
2091 cpu_to_be16(REQ_TX_ENTRIES_PER_SUBCRQ);
2092 crq.request_capability.number =
Thomas Falconde89e852016-03-01 10:20:09 -06002093 cpu_to_be64(adapter->req_tx_entries_per_subcrq);
Thomas Falcon901e0402017-02-15 12:17:59 -06002094 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002095 ibmvnic_send_crq(adapter, &crq);
2096
2097 crq.request_capability.capability =
2098 cpu_to_be16(REQ_RX_ADD_ENTRIES_PER_SUBCRQ);
2099 crq.request_capability.number =
Thomas Falconde89e852016-03-01 10:20:09 -06002100 cpu_to_be64(adapter->req_rx_add_entries_per_subcrq);
Thomas Falcon901e0402017-02-15 12:17:59 -06002101 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002102 ibmvnic_send_crq(adapter, &crq);
2103
2104 crq.request_capability.capability = cpu_to_be16(REQ_MTU);
Thomas Falconde89e852016-03-01 10:20:09 -06002105 crq.request_capability.number = cpu_to_be64(adapter->req_mtu);
Thomas Falcon901e0402017-02-15 12:17:59 -06002106 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002107 ibmvnic_send_crq(adapter, &crq);
2108
2109 if (adapter->netdev->flags & IFF_PROMISC) {
2110 if (adapter->promisc_supported) {
2111 crq.request_capability.capability =
2112 cpu_to_be16(PROMISC_REQUESTED);
Thomas Falconde89e852016-03-01 10:20:09 -06002113 crq.request_capability.number = cpu_to_be64(1);
Thomas Falcon901e0402017-02-15 12:17:59 -06002114 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002115 ibmvnic_send_crq(adapter, &crq);
2116 }
2117 } else {
2118 crq.request_capability.capability =
2119 cpu_to_be16(PROMISC_REQUESTED);
Thomas Falconde89e852016-03-01 10:20:09 -06002120 crq.request_capability.number = cpu_to_be64(0);
Thomas Falcon901e0402017-02-15 12:17:59 -06002121 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002122 ibmvnic_send_crq(adapter, &crq);
2123 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002124}
2125
2126static int pending_scrq(struct ibmvnic_adapter *adapter,
2127 struct ibmvnic_sub_crq_queue *scrq)
2128{
2129 union sub_crq *entry = &scrq->msgs[scrq->cur];
2130
Nathan Fontenot90c80142017-05-03 14:04:32 -04002131 if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP ||
2132 adapter->state == VNIC_CLOSING)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002133 return 1;
2134 else
2135 return 0;
2136}
2137
2138static union sub_crq *ibmvnic_next_scrq(struct ibmvnic_adapter *adapter,
2139 struct ibmvnic_sub_crq_queue *scrq)
2140{
2141 union sub_crq *entry;
2142 unsigned long flags;
2143
2144 spin_lock_irqsave(&scrq->lock, flags);
2145 entry = &scrq->msgs[scrq->cur];
2146 if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP) {
2147 if (++scrq->cur == scrq->size)
2148 scrq->cur = 0;
2149 } else {
2150 entry = NULL;
2151 }
2152 spin_unlock_irqrestore(&scrq->lock, flags);
2153
2154 return entry;
2155}
2156
2157static union ibmvnic_crq *ibmvnic_next_crq(struct ibmvnic_adapter *adapter)
2158{
2159 struct ibmvnic_crq_queue *queue = &adapter->crq;
2160 union ibmvnic_crq *crq;
2161
2162 crq = &queue->msgs[queue->cur];
2163 if (crq->generic.first & IBMVNIC_CRQ_CMD_RSP) {
2164 if (++queue->cur == queue->size)
2165 queue->cur = 0;
2166 } else {
2167 crq = NULL;
2168 }
2169
2170 return crq;
2171}
2172
2173static int send_subcrq(struct ibmvnic_adapter *adapter, u64 remote_handle,
2174 union sub_crq *sub_crq)
2175{
2176 unsigned int ua = adapter->vdev->unit_address;
2177 struct device *dev = &adapter->vdev->dev;
2178 u64 *u64_crq = (u64 *)sub_crq;
2179 int rc;
2180
2181 netdev_dbg(adapter->netdev,
2182 "Sending sCRQ %016lx: %016lx %016lx %016lx %016lx\n",
2183 (unsigned long int)cpu_to_be64(remote_handle),
2184 (unsigned long int)cpu_to_be64(u64_crq[0]),
2185 (unsigned long int)cpu_to_be64(u64_crq[1]),
2186 (unsigned long int)cpu_to_be64(u64_crq[2]),
2187 (unsigned long int)cpu_to_be64(u64_crq[3]));
2188
2189 /* Make sure the hypervisor sees the complete request */
2190 mb();
2191
2192 rc = plpar_hcall_norets(H_SEND_SUB_CRQ, ua,
2193 cpu_to_be64(remote_handle),
2194 cpu_to_be64(u64_crq[0]),
2195 cpu_to_be64(u64_crq[1]),
2196 cpu_to_be64(u64_crq[2]),
2197 cpu_to_be64(u64_crq[3]));
2198
2199 if (rc) {
2200 if (rc == H_CLOSED)
2201 dev_warn(dev, "CRQ Queue closed\n");
2202 dev_err(dev, "Send error (rc=%d)\n", rc);
2203 }
2204
2205 return rc;
2206}
2207
Thomas Falconad7775d2016-04-01 17:20:34 -05002208static int send_subcrq_indirect(struct ibmvnic_adapter *adapter,
2209 u64 remote_handle, u64 ioba, u64 num_entries)
2210{
2211 unsigned int ua = adapter->vdev->unit_address;
2212 struct device *dev = &adapter->vdev->dev;
2213 int rc;
2214
2215 /* Make sure the hypervisor sees the complete request */
2216 mb();
2217 rc = plpar_hcall_norets(H_SEND_SUB_CRQ_INDIRECT, ua,
2218 cpu_to_be64(remote_handle),
2219 ioba, num_entries);
2220
2221 if (rc) {
2222 if (rc == H_CLOSED)
2223 dev_warn(dev, "CRQ Queue closed\n");
2224 dev_err(dev, "Send (indirect) error (rc=%d)\n", rc);
2225 }
2226
2227 return rc;
2228}
2229
Thomas Falcon032c5e82015-12-21 11:26:06 -06002230static int ibmvnic_send_crq(struct ibmvnic_adapter *adapter,
2231 union ibmvnic_crq *crq)
2232{
2233 unsigned int ua = adapter->vdev->unit_address;
2234 struct device *dev = &adapter->vdev->dev;
2235 u64 *u64_crq = (u64 *)crq;
2236 int rc;
2237
2238 netdev_dbg(adapter->netdev, "Sending CRQ: %016lx %016lx\n",
2239 (unsigned long int)cpu_to_be64(u64_crq[0]),
2240 (unsigned long int)cpu_to_be64(u64_crq[1]));
2241
2242 /* Make sure the hypervisor sees the complete request */
2243 mb();
2244
2245 rc = plpar_hcall_norets(H_SEND_CRQ, ua,
2246 cpu_to_be64(u64_crq[0]),
2247 cpu_to_be64(u64_crq[1]));
2248
2249 if (rc) {
2250 if (rc == H_CLOSED)
2251 dev_warn(dev, "CRQ Queue closed\n");
2252 dev_warn(dev, "Send error (rc=%d)\n", rc);
2253 }
2254
2255 return rc;
2256}
2257
2258static int ibmvnic_send_crq_init(struct ibmvnic_adapter *adapter)
2259{
2260 union ibmvnic_crq crq;
2261
2262 memset(&crq, 0, sizeof(crq));
2263 crq.generic.first = IBMVNIC_CRQ_INIT_CMD;
2264 crq.generic.cmd = IBMVNIC_CRQ_INIT;
2265 netdev_dbg(adapter->netdev, "Sending CRQ init\n");
2266
2267 return ibmvnic_send_crq(adapter, &crq);
2268}
2269
Thomas Falcon032c5e82015-12-21 11:26:06 -06002270static int send_version_xchg(struct ibmvnic_adapter *adapter)
2271{
2272 union ibmvnic_crq crq;
2273
2274 memset(&crq, 0, sizeof(crq));
2275 crq.version_exchange.first = IBMVNIC_CRQ_CMD;
2276 crq.version_exchange.cmd = VERSION_EXCHANGE;
2277 crq.version_exchange.version = cpu_to_be16(ibmvnic_version);
2278
2279 return ibmvnic_send_crq(adapter, &crq);
2280}
2281
2282static void send_login(struct ibmvnic_adapter *adapter)
2283{
2284 struct ibmvnic_login_rsp_buffer *login_rsp_buffer;
2285 struct ibmvnic_login_buffer *login_buffer;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002286 struct device *dev = &adapter->vdev->dev;
2287 dma_addr_t rsp_buffer_token;
2288 dma_addr_t buffer_token;
2289 size_t rsp_buffer_size;
2290 union ibmvnic_crq crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002291 size_t buffer_size;
2292 __be64 *tx_list_p;
2293 __be64 *rx_list_p;
2294 int i;
2295
2296 buffer_size =
2297 sizeof(struct ibmvnic_login_buffer) +
2298 sizeof(u64) * (adapter->req_tx_queues + adapter->req_rx_queues);
2299
2300 login_buffer = kmalloc(buffer_size, GFP_ATOMIC);
2301 if (!login_buffer)
2302 goto buf_alloc_failed;
2303
2304 buffer_token = dma_map_single(dev, login_buffer, buffer_size,
2305 DMA_TO_DEVICE);
2306 if (dma_mapping_error(dev, buffer_token)) {
2307 dev_err(dev, "Couldn't map login buffer\n");
2308 goto buf_map_failed;
2309 }
2310
John Allen498cd8e2016-04-06 11:49:55 -05002311 rsp_buffer_size = sizeof(struct ibmvnic_login_rsp_buffer) +
2312 sizeof(u64) * adapter->req_tx_queues +
2313 sizeof(u64) * adapter->req_rx_queues +
2314 sizeof(u64) * adapter->req_rx_queues +
2315 sizeof(u8) * IBMVNIC_TX_DESC_VERSIONS;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002316
2317 login_rsp_buffer = kmalloc(rsp_buffer_size, GFP_ATOMIC);
2318 if (!login_rsp_buffer)
2319 goto buf_rsp_alloc_failed;
2320
2321 rsp_buffer_token = dma_map_single(dev, login_rsp_buffer,
2322 rsp_buffer_size, DMA_FROM_DEVICE);
2323 if (dma_mapping_error(dev, rsp_buffer_token)) {
2324 dev_err(dev, "Couldn't map login rsp buffer\n");
2325 goto buf_rsp_map_failed;
2326 }
Nathan Fontenot661a2622017-04-19 13:44:58 -04002327
Thomas Falcon032c5e82015-12-21 11:26:06 -06002328 adapter->login_buf = login_buffer;
2329 adapter->login_buf_token = buffer_token;
2330 adapter->login_buf_sz = buffer_size;
2331 adapter->login_rsp_buf = login_rsp_buffer;
2332 adapter->login_rsp_buf_token = rsp_buffer_token;
2333 adapter->login_rsp_buf_sz = rsp_buffer_size;
2334
2335 login_buffer->len = cpu_to_be32(buffer_size);
2336 login_buffer->version = cpu_to_be32(INITIAL_VERSION_LB);
2337 login_buffer->num_txcomp_subcrqs = cpu_to_be32(adapter->req_tx_queues);
2338 login_buffer->off_txcomp_subcrqs =
2339 cpu_to_be32(sizeof(struct ibmvnic_login_buffer));
2340 login_buffer->num_rxcomp_subcrqs = cpu_to_be32(adapter->req_rx_queues);
2341 login_buffer->off_rxcomp_subcrqs =
2342 cpu_to_be32(sizeof(struct ibmvnic_login_buffer) +
2343 sizeof(u64) * adapter->req_tx_queues);
2344 login_buffer->login_rsp_ioba = cpu_to_be32(rsp_buffer_token);
2345 login_buffer->login_rsp_len = cpu_to_be32(rsp_buffer_size);
2346
2347 tx_list_p = (__be64 *)((char *)login_buffer +
2348 sizeof(struct ibmvnic_login_buffer));
2349 rx_list_p = (__be64 *)((char *)login_buffer +
2350 sizeof(struct ibmvnic_login_buffer) +
2351 sizeof(u64) * adapter->req_tx_queues);
2352
2353 for (i = 0; i < adapter->req_tx_queues; i++) {
2354 if (adapter->tx_scrq[i]) {
2355 tx_list_p[i] = cpu_to_be64(adapter->tx_scrq[i]->
2356 crq_num);
2357 }
2358 }
2359
2360 for (i = 0; i < adapter->req_rx_queues; i++) {
2361 if (adapter->rx_scrq[i]) {
2362 rx_list_p[i] = cpu_to_be64(adapter->rx_scrq[i]->
2363 crq_num);
2364 }
2365 }
2366
2367 netdev_dbg(adapter->netdev, "Login Buffer:\n");
2368 for (i = 0; i < (adapter->login_buf_sz - 1) / 8 + 1; i++) {
2369 netdev_dbg(adapter->netdev, "%016lx\n",
2370 ((unsigned long int *)(adapter->login_buf))[i]);
2371 }
2372
2373 memset(&crq, 0, sizeof(crq));
2374 crq.login.first = IBMVNIC_CRQ_CMD;
2375 crq.login.cmd = LOGIN;
2376 crq.login.ioba = cpu_to_be32(buffer_token);
2377 crq.login.len = cpu_to_be32(buffer_size);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002378 ibmvnic_send_crq(adapter, &crq);
2379
2380 return;
2381
Thomas Falcon032c5e82015-12-21 11:26:06 -06002382buf_rsp_map_failed:
2383 kfree(login_rsp_buffer);
2384buf_rsp_alloc_failed:
2385 dma_unmap_single(dev, buffer_token, buffer_size, DMA_TO_DEVICE);
2386buf_map_failed:
2387 kfree(login_buffer);
2388buf_alloc_failed:
2389 return;
2390}
2391
2392static void send_request_map(struct ibmvnic_adapter *adapter, dma_addr_t addr,
2393 u32 len, u8 map_id)
2394{
2395 union ibmvnic_crq crq;
2396
2397 memset(&crq, 0, sizeof(crq));
2398 crq.request_map.first = IBMVNIC_CRQ_CMD;
2399 crq.request_map.cmd = REQUEST_MAP;
2400 crq.request_map.map_id = map_id;
2401 crq.request_map.ioba = cpu_to_be32(addr);
2402 crq.request_map.len = cpu_to_be32(len);
2403 ibmvnic_send_crq(adapter, &crq);
2404}
2405
2406static void send_request_unmap(struct ibmvnic_adapter *adapter, u8 map_id)
2407{
2408 union ibmvnic_crq crq;
2409
2410 memset(&crq, 0, sizeof(crq));
2411 crq.request_unmap.first = IBMVNIC_CRQ_CMD;
2412 crq.request_unmap.cmd = REQUEST_UNMAP;
2413 crq.request_unmap.map_id = map_id;
2414 ibmvnic_send_crq(adapter, &crq);
2415}
2416
2417static void send_map_query(struct ibmvnic_adapter *adapter)
2418{
2419 union ibmvnic_crq crq;
2420
2421 memset(&crq, 0, sizeof(crq));
2422 crq.query_map.first = IBMVNIC_CRQ_CMD;
2423 crq.query_map.cmd = QUERY_MAP;
2424 ibmvnic_send_crq(adapter, &crq);
2425}
2426
2427/* Send a series of CRQs requesting various capabilities of the VNIC server */
2428static void send_cap_queries(struct ibmvnic_adapter *adapter)
2429{
2430 union ibmvnic_crq crq;
2431
Thomas Falcon901e0402017-02-15 12:17:59 -06002432 atomic_set(&adapter->running_cap_crqs, 0);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002433 memset(&crq, 0, sizeof(crq));
2434 crq.query_capability.first = IBMVNIC_CRQ_CMD;
2435 crq.query_capability.cmd = QUERY_CAPABILITY;
2436
2437 crq.query_capability.capability = cpu_to_be16(MIN_TX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002438 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002439 ibmvnic_send_crq(adapter, &crq);
2440
2441 crq.query_capability.capability = cpu_to_be16(MIN_RX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002442 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002443 ibmvnic_send_crq(adapter, &crq);
2444
2445 crq.query_capability.capability = cpu_to_be16(MIN_RX_ADD_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002446 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002447 ibmvnic_send_crq(adapter, &crq);
2448
2449 crq.query_capability.capability = cpu_to_be16(MAX_TX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002450 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002451 ibmvnic_send_crq(adapter, &crq);
2452
2453 crq.query_capability.capability = cpu_to_be16(MAX_RX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002454 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002455 ibmvnic_send_crq(adapter, &crq);
2456
2457 crq.query_capability.capability = cpu_to_be16(MAX_RX_ADD_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002458 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002459 ibmvnic_send_crq(adapter, &crq);
2460
2461 crq.query_capability.capability =
2462 cpu_to_be16(MIN_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06002463 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002464 ibmvnic_send_crq(adapter, &crq);
2465
2466 crq.query_capability.capability =
2467 cpu_to_be16(MIN_RX_ADD_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06002468 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002469 ibmvnic_send_crq(adapter, &crq);
2470
2471 crq.query_capability.capability =
2472 cpu_to_be16(MAX_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06002473 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002474 ibmvnic_send_crq(adapter, &crq);
2475
2476 crq.query_capability.capability =
2477 cpu_to_be16(MAX_RX_ADD_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06002478 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002479 ibmvnic_send_crq(adapter, &crq);
2480
2481 crq.query_capability.capability = cpu_to_be16(TCP_IP_OFFLOAD);
Thomas Falcon901e0402017-02-15 12:17:59 -06002482 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002483 ibmvnic_send_crq(adapter, &crq);
2484
2485 crq.query_capability.capability = cpu_to_be16(PROMISC_SUPPORTED);
Thomas Falcon901e0402017-02-15 12:17:59 -06002486 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002487 ibmvnic_send_crq(adapter, &crq);
2488
2489 crq.query_capability.capability = cpu_to_be16(MIN_MTU);
Thomas Falcon901e0402017-02-15 12:17:59 -06002490 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002491 ibmvnic_send_crq(adapter, &crq);
2492
2493 crq.query_capability.capability = cpu_to_be16(MAX_MTU);
Thomas Falcon901e0402017-02-15 12:17:59 -06002494 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002495 ibmvnic_send_crq(adapter, &crq);
2496
2497 crq.query_capability.capability = cpu_to_be16(MAX_MULTICAST_FILTERS);
Thomas Falcon901e0402017-02-15 12:17:59 -06002498 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002499 ibmvnic_send_crq(adapter, &crq);
2500
2501 crq.query_capability.capability = cpu_to_be16(VLAN_HEADER_INSERTION);
Thomas Falcon901e0402017-02-15 12:17:59 -06002502 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002503 ibmvnic_send_crq(adapter, &crq);
2504
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04002505 crq.query_capability.capability = cpu_to_be16(RX_VLAN_HEADER_INSERTION);
2506 atomic_inc(&adapter->running_cap_crqs);
2507 ibmvnic_send_crq(adapter, &crq);
2508
Thomas Falcon032c5e82015-12-21 11:26:06 -06002509 crq.query_capability.capability = cpu_to_be16(MAX_TX_SG_ENTRIES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002510 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002511 ibmvnic_send_crq(adapter, &crq);
2512
2513 crq.query_capability.capability = cpu_to_be16(RX_SG_SUPPORTED);
Thomas Falcon901e0402017-02-15 12:17:59 -06002514 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002515 ibmvnic_send_crq(adapter, &crq);
2516
2517 crq.query_capability.capability = cpu_to_be16(OPT_TX_COMP_SUB_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002518 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002519 ibmvnic_send_crq(adapter, &crq);
2520
2521 crq.query_capability.capability = cpu_to_be16(OPT_RX_COMP_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002522 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002523 ibmvnic_send_crq(adapter, &crq);
2524
2525 crq.query_capability.capability =
2526 cpu_to_be16(OPT_RX_BUFADD_Q_PER_RX_COMP_Q);
Thomas Falcon901e0402017-02-15 12:17:59 -06002527 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002528 ibmvnic_send_crq(adapter, &crq);
2529
2530 crq.query_capability.capability =
2531 cpu_to_be16(OPT_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06002532 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002533 ibmvnic_send_crq(adapter, &crq);
2534
2535 crq.query_capability.capability =
2536 cpu_to_be16(OPT_RXBA_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06002537 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002538 ibmvnic_send_crq(adapter, &crq);
2539
2540 crq.query_capability.capability = cpu_to_be16(TX_RX_DESC_REQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06002541 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002542 ibmvnic_send_crq(adapter, &crq);
2543}
2544
2545static void handle_query_ip_offload_rsp(struct ibmvnic_adapter *adapter)
2546{
2547 struct device *dev = &adapter->vdev->dev;
2548 struct ibmvnic_query_ip_offload_buffer *buf = &adapter->ip_offload_buf;
2549 union ibmvnic_crq crq;
2550 int i;
2551
2552 dma_unmap_single(dev, adapter->ip_offload_tok,
2553 sizeof(adapter->ip_offload_buf), DMA_FROM_DEVICE);
2554
2555 netdev_dbg(adapter->netdev, "Query IP Offload Buffer:\n");
2556 for (i = 0; i < (sizeof(adapter->ip_offload_buf) - 1) / 8 + 1; i++)
2557 netdev_dbg(adapter->netdev, "%016lx\n",
2558 ((unsigned long int *)(buf))[i]);
2559
2560 netdev_dbg(adapter->netdev, "ipv4_chksum = %d\n", buf->ipv4_chksum);
2561 netdev_dbg(adapter->netdev, "ipv6_chksum = %d\n", buf->ipv6_chksum);
2562 netdev_dbg(adapter->netdev, "tcp_ipv4_chksum = %d\n",
2563 buf->tcp_ipv4_chksum);
2564 netdev_dbg(adapter->netdev, "tcp_ipv6_chksum = %d\n",
2565 buf->tcp_ipv6_chksum);
2566 netdev_dbg(adapter->netdev, "udp_ipv4_chksum = %d\n",
2567 buf->udp_ipv4_chksum);
2568 netdev_dbg(adapter->netdev, "udp_ipv6_chksum = %d\n",
2569 buf->udp_ipv6_chksum);
2570 netdev_dbg(adapter->netdev, "large_tx_ipv4 = %d\n",
2571 buf->large_tx_ipv4);
2572 netdev_dbg(adapter->netdev, "large_tx_ipv6 = %d\n",
2573 buf->large_tx_ipv6);
2574 netdev_dbg(adapter->netdev, "large_rx_ipv4 = %d\n",
2575 buf->large_rx_ipv4);
2576 netdev_dbg(adapter->netdev, "large_rx_ipv6 = %d\n",
2577 buf->large_rx_ipv6);
2578 netdev_dbg(adapter->netdev, "max_ipv4_hdr_sz = %d\n",
2579 buf->max_ipv4_header_size);
2580 netdev_dbg(adapter->netdev, "max_ipv6_hdr_sz = %d\n",
2581 buf->max_ipv6_header_size);
2582 netdev_dbg(adapter->netdev, "max_tcp_hdr_size = %d\n",
2583 buf->max_tcp_header_size);
2584 netdev_dbg(adapter->netdev, "max_udp_hdr_size = %d\n",
2585 buf->max_udp_header_size);
2586 netdev_dbg(adapter->netdev, "max_large_tx_size = %d\n",
2587 buf->max_large_tx_size);
2588 netdev_dbg(adapter->netdev, "max_large_rx_size = %d\n",
2589 buf->max_large_rx_size);
2590 netdev_dbg(adapter->netdev, "ipv6_ext_hdr = %d\n",
2591 buf->ipv6_extension_header);
2592 netdev_dbg(adapter->netdev, "tcp_pseudosum_req = %d\n",
2593 buf->tcp_pseudosum_req);
2594 netdev_dbg(adapter->netdev, "num_ipv6_ext_hd = %d\n",
2595 buf->num_ipv6_ext_headers);
2596 netdev_dbg(adapter->netdev, "off_ipv6_ext_hd = %d\n",
2597 buf->off_ipv6_ext_headers);
2598
2599 adapter->ip_offload_ctrl_tok =
2600 dma_map_single(dev, &adapter->ip_offload_ctrl,
2601 sizeof(adapter->ip_offload_ctrl), DMA_TO_DEVICE);
2602
2603 if (dma_mapping_error(dev, adapter->ip_offload_ctrl_tok)) {
2604 dev_err(dev, "Couldn't map ip offload control buffer\n");
2605 return;
2606 }
2607
2608 adapter->ip_offload_ctrl.version = cpu_to_be32(INITIAL_VERSION_IOB);
2609 adapter->ip_offload_ctrl.tcp_ipv4_chksum = buf->tcp_ipv4_chksum;
2610 adapter->ip_offload_ctrl.udp_ipv4_chksum = buf->udp_ipv4_chksum;
2611 adapter->ip_offload_ctrl.tcp_ipv6_chksum = buf->tcp_ipv6_chksum;
2612 adapter->ip_offload_ctrl.udp_ipv6_chksum = buf->udp_ipv6_chksum;
2613
2614 /* large_tx/rx disabled for now, additional features needed */
2615 adapter->ip_offload_ctrl.large_tx_ipv4 = 0;
2616 adapter->ip_offload_ctrl.large_tx_ipv6 = 0;
2617 adapter->ip_offload_ctrl.large_rx_ipv4 = 0;
2618 adapter->ip_offload_ctrl.large_rx_ipv6 = 0;
2619
2620 adapter->netdev->features = NETIF_F_GSO;
2621
2622 if (buf->tcp_ipv4_chksum || buf->udp_ipv4_chksum)
2623 adapter->netdev->features |= NETIF_F_IP_CSUM;
2624
2625 if (buf->tcp_ipv6_chksum || buf->udp_ipv6_chksum)
2626 adapter->netdev->features |= NETIF_F_IPV6_CSUM;
2627
Thomas Falcon9be02cd2016-04-01 17:20:35 -05002628 if ((adapter->netdev->features &
2629 (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)))
2630 adapter->netdev->features |= NETIF_F_RXCSUM;
2631
Thomas Falcon032c5e82015-12-21 11:26:06 -06002632 memset(&crq, 0, sizeof(crq));
2633 crq.control_ip_offload.first = IBMVNIC_CRQ_CMD;
2634 crq.control_ip_offload.cmd = CONTROL_IP_OFFLOAD;
2635 crq.control_ip_offload.len =
2636 cpu_to_be32(sizeof(adapter->ip_offload_ctrl));
2637 crq.control_ip_offload.ioba = cpu_to_be32(adapter->ip_offload_ctrl_tok);
2638 ibmvnic_send_crq(adapter, &crq);
2639}
2640
2641static void handle_error_info_rsp(union ibmvnic_crq *crq,
2642 struct ibmvnic_adapter *adapter)
2643{
2644 struct device *dev = &adapter->vdev->dev;
Wei Yongjun96183182016-06-27 20:48:53 +08002645 struct ibmvnic_error_buff *error_buff, *tmp;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002646 unsigned long flags;
2647 bool found = false;
2648 int i;
2649
2650 if (!crq->request_error_rsp.rc.code) {
2651 dev_info(dev, "Request Error Rsp returned with rc=%x\n",
2652 crq->request_error_rsp.rc.code);
2653 return;
2654 }
2655
2656 spin_lock_irqsave(&adapter->error_list_lock, flags);
Wei Yongjun96183182016-06-27 20:48:53 +08002657 list_for_each_entry_safe(error_buff, tmp, &adapter->errors, list)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002658 if (error_buff->error_id == crq->request_error_rsp.error_id) {
2659 found = true;
2660 list_del(&error_buff->list);
2661 break;
2662 }
2663 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
2664
2665 if (!found) {
2666 dev_err(dev, "Couldn't find error id %x\n",
Thomas Falcon75224c92017-02-15 10:33:33 -06002667 be32_to_cpu(crq->request_error_rsp.error_id));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002668 return;
2669 }
2670
2671 dev_err(dev, "Detailed info for error id %x:",
Thomas Falcon75224c92017-02-15 10:33:33 -06002672 be32_to_cpu(crq->request_error_rsp.error_id));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002673
2674 for (i = 0; i < error_buff->len; i++) {
2675 pr_cont("%02x", (int)error_buff->buff[i]);
2676 if (i % 8 == 7)
2677 pr_cont(" ");
2678 }
2679 pr_cont("\n");
2680
2681 dma_unmap_single(dev, error_buff->dma, error_buff->len,
2682 DMA_FROM_DEVICE);
2683 kfree(error_buff->buff);
2684 kfree(error_buff);
2685}
2686
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04002687static void request_error_information(struct ibmvnic_adapter *adapter,
2688 union ibmvnic_crq *err_crq)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002689{
Thomas Falcon032c5e82015-12-21 11:26:06 -06002690 struct device *dev = &adapter->vdev->dev;
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04002691 struct net_device *netdev = adapter->netdev;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002692 struct ibmvnic_error_buff *error_buff;
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04002693 unsigned long timeout = msecs_to_jiffies(30000);
2694 union ibmvnic_crq crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002695 unsigned long flags;
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04002696 int rc, detail_len;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002697
2698 error_buff = kmalloc(sizeof(*error_buff), GFP_ATOMIC);
2699 if (!error_buff)
2700 return;
2701
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04002702 detail_len = be32_to_cpu(err_crq->error_indication.detail_error_sz);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002703 error_buff->buff = kmalloc(detail_len, GFP_ATOMIC);
2704 if (!error_buff->buff) {
2705 kfree(error_buff);
2706 return;
2707 }
2708
2709 error_buff->dma = dma_map_single(dev, error_buff->buff, detail_len,
2710 DMA_FROM_DEVICE);
2711 if (dma_mapping_error(dev, error_buff->dma)) {
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04002712 netdev_err(netdev, "Couldn't map error buffer\n");
Thomas Falcon032c5e82015-12-21 11:26:06 -06002713 kfree(error_buff->buff);
2714 kfree(error_buff);
2715 return;
2716 }
2717
Thomas Falcon032c5e82015-12-21 11:26:06 -06002718 error_buff->len = detail_len;
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04002719 error_buff->error_id = err_crq->error_indication.error_id;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002720
2721 spin_lock_irqsave(&adapter->error_list_lock, flags);
2722 list_add_tail(&error_buff->list, &adapter->errors);
2723 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
2724
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04002725 memset(&crq, 0, sizeof(crq));
2726 crq.request_error_info.first = IBMVNIC_CRQ_CMD;
2727 crq.request_error_info.cmd = REQUEST_ERROR_INFO;
2728 crq.request_error_info.ioba = cpu_to_be32(error_buff->dma);
2729 crq.request_error_info.len = cpu_to_be32(detail_len);
2730 crq.request_error_info.error_id = err_crq->error_indication.error_id;
2731
2732 rc = ibmvnic_send_crq(adapter, &crq);
2733 if (rc) {
2734 netdev_err(netdev, "failed to request error information\n");
2735 goto err_info_fail;
2736 }
2737
2738 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
2739 netdev_err(netdev, "timeout waiting for error information\n");
2740 goto err_info_fail;
2741 }
2742
2743 return;
2744
2745err_info_fail:
2746 spin_lock_irqsave(&adapter->error_list_lock, flags);
2747 list_del(&error_buff->list);
2748 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
2749
2750 kfree(error_buff->buff);
2751 kfree(error_buff);
2752}
2753
2754static void handle_error_indication(union ibmvnic_crq *crq,
2755 struct ibmvnic_adapter *adapter)
2756{
2757 struct device *dev = &adapter->vdev->dev;
2758
2759 dev_err(dev, "Firmware reports %serror id %x, cause %d\n",
2760 crq->error_indication.flags
2761 & IBMVNIC_FATAL_ERROR ? "FATAL " : "",
2762 be32_to_cpu(crq->error_indication.error_id),
2763 be16_to_cpu(crq->error_indication.error_cause));
2764
2765 if (be32_to_cpu(crq->error_indication.error_id))
2766 request_error_information(adapter, crq);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002767
2768 if (crq->error_indication.flags & IBMVNIC_FATAL_ERROR)
2769 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
John Allen8cb31cf2017-05-26 10:30:37 -04002770 else
2771 ibmvnic_reset(adapter, VNIC_RESET_NON_FATAL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002772}
2773
2774static void handle_change_mac_rsp(union ibmvnic_crq *crq,
2775 struct ibmvnic_adapter *adapter)
2776{
2777 struct net_device *netdev = adapter->netdev;
2778 struct device *dev = &adapter->vdev->dev;
2779 long rc;
2780
2781 rc = crq->change_mac_addr_rsp.rc.code;
2782 if (rc) {
2783 dev_err(dev, "Error %ld in CHANGE_MAC_ADDR_RSP\n", rc);
2784 return;
2785 }
2786 memcpy(netdev->dev_addr, &crq->change_mac_addr_rsp.mac_addr[0],
2787 ETH_ALEN);
2788}
2789
2790static void handle_request_cap_rsp(union ibmvnic_crq *crq,
2791 struct ibmvnic_adapter *adapter)
2792{
2793 struct device *dev = &adapter->vdev->dev;
2794 u64 *req_value;
2795 char *name;
2796
Thomas Falcon901e0402017-02-15 12:17:59 -06002797 atomic_dec(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002798 switch (be16_to_cpu(crq->request_capability_rsp.capability)) {
2799 case REQ_TX_QUEUES:
2800 req_value = &adapter->req_tx_queues;
2801 name = "tx";
2802 break;
2803 case REQ_RX_QUEUES:
2804 req_value = &adapter->req_rx_queues;
2805 name = "rx";
2806 break;
2807 case REQ_RX_ADD_QUEUES:
2808 req_value = &adapter->req_rx_add_queues;
2809 name = "rx_add";
2810 break;
2811 case REQ_TX_ENTRIES_PER_SUBCRQ:
2812 req_value = &adapter->req_tx_entries_per_subcrq;
2813 name = "tx_entries_per_subcrq";
2814 break;
2815 case REQ_RX_ADD_ENTRIES_PER_SUBCRQ:
2816 req_value = &adapter->req_rx_add_entries_per_subcrq;
2817 name = "rx_add_entries_per_subcrq";
2818 break;
2819 case REQ_MTU:
2820 req_value = &adapter->req_mtu;
2821 name = "mtu";
2822 break;
2823 case PROMISC_REQUESTED:
2824 req_value = &adapter->promisc;
2825 name = "promisc";
2826 break;
2827 default:
2828 dev_err(dev, "Got invalid cap request rsp %d\n",
2829 crq->request_capability.capability);
2830 return;
2831 }
2832
2833 switch (crq->request_capability_rsp.rc.code) {
2834 case SUCCESS:
2835 break;
2836 case PARTIALSUCCESS:
2837 dev_info(dev, "req=%lld, rsp=%ld in %s queue, retrying.\n",
2838 *req_value,
Thomas Falcon28f4d162017-02-15 10:32:11 -06002839 (long int)be64_to_cpu(crq->request_capability_rsp.
Thomas Falcon032c5e82015-12-21 11:26:06 -06002840 number), name);
Nathan Fontenotb5108882017-03-30 02:49:18 -04002841 release_sub_crqs(adapter);
Thomas Falcon28f4d162017-02-15 10:32:11 -06002842 *req_value = be64_to_cpu(crq->request_capability_rsp.number);
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002843 ibmvnic_send_req_caps(adapter, 1);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002844 return;
2845 default:
2846 dev_err(dev, "Error %d in request cap rsp\n",
2847 crq->request_capability_rsp.rc.code);
2848 return;
2849 }
2850
2851 /* Done receiving requested capabilities, query IP offload support */
Thomas Falcon901e0402017-02-15 12:17:59 -06002852 if (atomic_read(&adapter->running_cap_crqs) == 0) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06002853 union ibmvnic_crq newcrq;
2854 int buf_sz = sizeof(struct ibmvnic_query_ip_offload_buffer);
2855 struct ibmvnic_query_ip_offload_buffer *ip_offload_buf =
2856 &adapter->ip_offload_buf;
2857
Thomas Falcon249168a2017-02-15 12:18:00 -06002858 adapter->wait_capability = false;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002859 adapter->ip_offload_tok = dma_map_single(dev, ip_offload_buf,
2860 buf_sz,
2861 DMA_FROM_DEVICE);
2862
2863 if (dma_mapping_error(dev, adapter->ip_offload_tok)) {
2864 if (!firmware_has_feature(FW_FEATURE_CMO))
2865 dev_err(dev, "Couldn't map offload buffer\n");
2866 return;
2867 }
2868
2869 memset(&newcrq, 0, sizeof(newcrq));
2870 newcrq.query_ip_offload.first = IBMVNIC_CRQ_CMD;
2871 newcrq.query_ip_offload.cmd = QUERY_IP_OFFLOAD;
2872 newcrq.query_ip_offload.len = cpu_to_be32(buf_sz);
2873 newcrq.query_ip_offload.ioba =
2874 cpu_to_be32(adapter->ip_offload_tok);
2875
2876 ibmvnic_send_crq(adapter, &newcrq);
2877 }
2878}
2879
2880static int handle_login_rsp(union ibmvnic_crq *login_rsp_crq,
2881 struct ibmvnic_adapter *adapter)
2882{
2883 struct device *dev = &adapter->vdev->dev;
2884 struct ibmvnic_login_rsp_buffer *login_rsp = adapter->login_rsp_buf;
2885 struct ibmvnic_login_buffer *login = adapter->login_buf;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002886 int i;
2887
2888 dma_unmap_single(dev, adapter->login_buf_token, adapter->login_buf_sz,
2889 DMA_BIDIRECTIONAL);
2890 dma_unmap_single(dev, adapter->login_rsp_buf_token,
2891 adapter->login_rsp_buf_sz, DMA_BIDIRECTIONAL);
2892
John Allen498cd8e2016-04-06 11:49:55 -05002893 /* If the number of queues requested can't be allocated by the
2894 * server, the login response will return with code 1. We will need
2895 * to resend the login buffer with fewer queues requested.
2896 */
2897 if (login_rsp_crq->generic.rc.code) {
2898 adapter->renegotiate = true;
2899 complete(&adapter->init_done);
2900 return 0;
2901 }
2902
Thomas Falcon032c5e82015-12-21 11:26:06 -06002903 netdev_dbg(adapter->netdev, "Login Response Buffer:\n");
2904 for (i = 0; i < (adapter->login_rsp_buf_sz - 1) / 8 + 1; i++) {
2905 netdev_dbg(adapter->netdev, "%016lx\n",
2906 ((unsigned long int *)(adapter->login_rsp_buf))[i]);
2907 }
2908
2909 /* Sanity checks */
2910 if (login->num_txcomp_subcrqs != login_rsp->num_txsubm_subcrqs ||
2911 (be32_to_cpu(login->num_rxcomp_subcrqs) *
2912 adapter->req_rx_add_queues !=
2913 be32_to_cpu(login_rsp->num_rxadd_subcrqs))) {
2914 dev_err(dev, "FATAL: Inconsistent login and login rsp\n");
2915 ibmvnic_remove(adapter->vdev);
2916 return -EIO;
2917 }
2918 complete(&adapter->init_done);
2919
Thomas Falcon032c5e82015-12-21 11:26:06 -06002920 return 0;
2921}
2922
2923static void handle_request_map_rsp(union ibmvnic_crq *crq,
2924 struct ibmvnic_adapter *adapter)
2925{
2926 struct device *dev = &adapter->vdev->dev;
2927 u8 map_id = crq->request_map_rsp.map_id;
2928 int tx_subcrqs;
2929 int rx_subcrqs;
2930 long rc;
2931 int i;
2932
2933 tx_subcrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
2934 rx_subcrqs = be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
2935
2936 rc = crq->request_map_rsp.rc.code;
2937 if (rc) {
2938 dev_err(dev, "Error %ld in REQUEST_MAP_RSP\n", rc);
2939 adapter->map_id--;
2940 /* need to find and zero tx/rx_pool map_id */
2941 for (i = 0; i < tx_subcrqs; i++) {
2942 if (adapter->tx_pool[i].long_term_buff.map_id == map_id)
2943 adapter->tx_pool[i].long_term_buff.map_id = 0;
2944 }
2945 for (i = 0; i < rx_subcrqs; i++) {
2946 if (adapter->rx_pool[i].long_term_buff.map_id == map_id)
2947 adapter->rx_pool[i].long_term_buff.map_id = 0;
2948 }
2949 }
2950 complete(&adapter->fw_done);
2951}
2952
2953static void handle_request_unmap_rsp(union ibmvnic_crq *crq,
2954 struct ibmvnic_adapter *adapter)
2955{
2956 struct device *dev = &adapter->vdev->dev;
2957 long rc;
2958
2959 rc = crq->request_unmap_rsp.rc.code;
2960 if (rc)
2961 dev_err(dev, "Error %ld in REQUEST_UNMAP_RSP\n", rc);
2962}
2963
2964static void handle_query_map_rsp(union ibmvnic_crq *crq,
2965 struct ibmvnic_adapter *adapter)
2966{
2967 struct net_device *netdev = adapter->netdev;
2968 struct device *dev = &adapter->vdev->dev;
2969 long rc;
2970
2971 rc = crq->query_map_rsp.rc.code;
2972 if (rc) {
2973 dev_err(dev, "Error %ld in QUERY_MAP_RSP\n", rc);
2974 return;
2975 }
2976 netdev_dbg(netdev, "page_size = %d\ntot_pages = %d\nfree_pages = %d\n",
2977 crq->query_map_rsp.page_size, crq->query_map_rsp.tot_pages,
2978 crq->query_map_rsp.free_pages);
2979}
2980
2981static void handle_query_cap_rsp(union ibmvnic_crq *crq,
2982 struct ibmvnic_adapter *adapter)
2983{
2984 struct net_device *netdev = adapter->netdev;
2985 struct device *dev = &adapter->vdev->dev;
2986 long rc;
2987
Thomas Falcon901e0402017-02-15 12:17:59 -06002988 atomic_dec(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002989 netdev_dbg(netdev, "Outstanding queries: %d\n",
Thomas Falcon901e0402017-02-15 12:17:59 -06002990 atomic_read(&adapter->running_cap_crqs));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002991 rc = crq->query_capability.rc.code;
2992 if (rc) {
2993 dev_err(dev, "Error %ld in QUERY_CAP_RSP\n", rc);
2994 goto out;
2995 }
2996
2997 switch (be16_to_cpu(crq->query_capability.capability)) {
2998 case MIN_TX_QUEUES:
2999 adapter->min_tx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003000 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003001 netdev_dbg(netdev, "min_tx_queues = %lld\n",
3002 adapter->min_tx_queues);
3003 break;
3004 case MIN_RX_QUEUES:
3005 adapter->min_rx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003006 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003007 netdev_dbg(netdev, "min_rx_queues = %lld\n",
3008 adapter->min_rx_queues);
3009 break;
3010 case MIN_RX_ADD_QUEUES:
3011 adapter->min_rx_add_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003012 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003013 netdev_dbg(netdev, "min_rx_add_queues = %lld\n",
3014 adapter->min_rx_add_queues);
3015 break;
3016 case MAX_TX_QUEUES:
3017 adapter->max_tx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003018 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003019 netdev_dbg(netdev, "max_tx_queues = %lld\n",
3020 adapter->max_tx_queues);
3021 break;
3022 case MAX_RX_QUEUES:
3023 adapter->max_rx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003024 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003025 netdev_dbg(netdev, "max_rx_queues = %lld\n",
3026 adapter->max_rx_queues);
3027 break;
3028 case MAX_RX_ADD_QUEUES:
3029 adapter->max_rx_add_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003030 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003031 netdev_dbg(netdev, "max_rx_add_queues = %lld\n",
3032 adapter->max_rx_add_queues);
3033 break;
3034 case MIN_TX_ENTRIES_PER_SUBCRQ:
3035 adapter->min_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06003036 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003037 netdev_dbg(netdev, "min_tx_entries_per_subcrq = %lld\n",
3038 adapter->min_tx_entries_per_subcrq);
3039 break;
3040 case MIN_RX_ADD_ENTRIES_PER_SUBCRQ:
3041 adapter->min_rx_add_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06003042 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003043 netdev_dbg(netdev, "min_rx_add_entrs_per_subcrq = %lld\n",
3044 adapter->min_rx_add_entries_per_subcrq);
3045 break;
3046 case MAX_TX_ENTRIES_PER_SUBCRQ:
3047 adapter->max_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06003048 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003049 netdev_dbg(netdev, "max_tx_entries_per_subcrq = %lld\n",
3050 adapter->max_tx_entries_per_subcrq);
3051 break;
3052 case MAX_RX_ADD_ENTRIES_PER_SUBCRQ:
3053 adapter->max_rx_add_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06003054 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003055 netdev_dbg(netdev, "max_rx_add_entrs_per_subcrq = %lld\n",
3056 adapter->max_rx_add_entries_per_subcrq);
3057 break;
3058 case TCP_IP_OFFLOAD:
3059 adapter->tcp_ip_offload =
Thomas Falconde89e852016-03-01 10:20:09 -06003060 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003061 netdev_dbg(netdev, "tcp_ip_offload = %lld\n",
3062 adapter->tcp_ip_offload);
3063 break;
3064 case PROMISC_SUPPORTED:
3065 adapter->promisc_supported =
Thomas Falconde89e852016-03-01 10:20:09 -06003066 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003067 netdev_dbg(netdev, "promisc_supported = %lld\n",
3068 adapter->promisc_supported);
3069 break;
3070 case MIN_MTU:
Thomas Falconde89e852016-03-01 10:20:09 -06003071 adapter->min_mtu = be64_to_cpu(crq->query_capability.number);
Thomas Falconf39f0d12017-02-14 10:22:59 -06003072 netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003073 netdev_dbg(netdev, "min_mtu = %lld\n", adapter->min_mtu);
3074 break;
3075 case MAX_MTU:
Thomas Falconde89e852016-03-01 10:20:09 -06003076 adapter->max_mtu = be64_to_cpu(crq->query_capability.number);
Thomas Falconf39f0d12017-02-14 10:22:59 -06003077 netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003078 netdev_dbg(netdev, "max_mtu = %lld\n", adapter->max_mtu);
3079 break;
3080 case MAX_MULTICAST_FILTERS:
3081 adapter->max_multicast_filters =
Thomas Falconde89e852016-03-01 10:20:09 -06003082 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003083 netdev_dbg(netdev, "max_multicast_filters = %lld\n",
3084 adapter->max_multicast_filters);
3085 break;
3086 case VLAN_HEADER_INSERTION:
3087 adapter->vlan_header_insertion =
Thomas Falconde89e852016-03-01 10:20:09 -06003088 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003089 if (adapter->vlan_header_insertion)
3090 netdev->features |= NETIF_F_HW_VLAN_STAG_TX;
3091 netdev_dbg(netdev, "vlan_header_insertion = %lld\n",
3092 adapter->vlan_header_insertion);
3093 break;
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04003094 case RX_VLAN_HEADER_INSERTION:
3095 adapter->rx_vlan_header_insertion =
3096 be64_to_cpu(crq->query_capability.number);
3097 netdev_dbg(netdev, "rx_vlan_header_insertion = %lld\n",
3098 adapter->rx_vlan_header_insertion);
3099 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003100 case MAX_TX_SG_ENTRIES:
3101 adapter->max_tx_sg_entries =
Thomas Falconde89e852016-03-01 10:20:09 -06003102 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003103 netdev_dbg(netdev, "max_tx_sg_entries = %lld\n",
3104 adapter->max_tx_sg_entries);
3105 break;
3106 case RX_SG_SUPPORTED:
3107 adapter->rx_sg_supported =
Thomas Falconde89e852016-03-01 10:20:09 -06003108 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003109 netdev_dbg(netdev, "rx_sg_supported = %lld\n",
3110 adapter->rx_sg_supported);
3111 break;
3112 case OPT_TX_COMP_SUB_QUEUES:
3113 adapter->opt_tx_comp_sub_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003114 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003115 netdev_dbg(netdev, "opt_tx_comp_sub_queues = %lld\n",
3116 adapter->opt_tx_comp_sub_queues);
3117 break;
3118 case OPT_RX_COMP_QUEUES:
3119 adapter->opt_rx_comp_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003120 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003121 netdev_dbg(netdev, "opt_rx_comp_queues = %lld\n",
3122 adapter->opt_rx_comp_queues);
3123 break;
3124 case OPT_RX_BUFADD_Q_PER_RX_COMP_Q:
3125 adapter->opt_rx_bufadd_q_per_rx_comp_q =
Thomas Falconde89e852016-03-01 10:20:09 -06003126 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003127 netdev_dbg(netdev, "opt_rx_bufadd_q_per_rx_comp_q = %lld\n",
3128 adapter->opt_rx_bufadd_q_per_rx_comp_q);
3129 break;
3130 case OPT_TX_ENTRIES_PER_SUBCRQ:
3131 adapter->opt_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06003132 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003133 netdev_dbg(netdev, "opt_tx_entries_per_subcrq = %lld\n",
3134 adapter->opt_tx_entries_per_subcrq);
3135 break;
3136 case OPT_RXBA_ENTRIES_PER_SUBCRQ:
3137 adapter->opt_rxba_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06003138 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003139 netdev_dbg(netdev, "opt_rxba_entries_per_subcrq = %lld\n",
3140 adapter->opt_rxba_entries_per_subcrq);
3141 break;
3142 case TX_RX_DESC_REQ:
3143 adapter->tx_rx_desc_req = crq->query_capability.number;
3144 netdev_dbg(netdev, "tx_rx_desc_req = %llx\n",
3145 adapter->tx_rx_desc_req);
3146 break;
3147
3148 default:
3149 netdev_err(netdev, "Got invalid cap rsp %d\n",
3150 crq->query_capability.capability);
3151 }
3152
3153out:
Thomas Falcon249168a2017-02-15 12:18:00 -06003154 if (atomic_read(&adapter->running_cap_crqs) == 0) {
3155 adapter->wait_capability = false;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003156 ibmvnic_send_req_caps(adapter, 0);
Thomas Falcon249168a2017-02-15 12:18:00 -06003157 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06003158}
3159
Thomas Falcon032c5e82015-12-21 11:26:06 -06003160static void ibmvnic_handle_crq(union ibmvnic_crq *crq,
3161 struct ibmvnic_adapter *adapter)
3162{
3163 struct ibmvnic_generic_crq *gen_crq = &crq->generic;
3164 struct net_device *netdev = adapter->netdev;
3165 struct device *dev = &adapter->vdev->dev;
Murilo Fossa Vicentini993a82b2017-04-19 13:44:35 -04003166 u64 *u64_crq = (u64 *)crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003167 long rc;
3168
3169 netdev_dbg(netdev, "Handling CRQ: %016lx %016lx\n",
Murilo Fossa Vicentini993a82b2017-04-19 13:44:35 -04003170 (unsigned long int)cpu_to_be64(u64_crq[0]),
3171 (unsigned long int)cpu_to_be64(u64_crq[1]));
Thomas Falcon032c5e82015-12-21 11:26:06 -06003172 switch (gen_crq->first) {
3173 case IBMVNIC_CRQ_INIT_RSP:
3174 switch (gen_crq->cmd) {
3175 case IBMVNIC_CRQ_INIT:
3176 dev_info(dev, "Partner initialized\n");
John Allen017892c12017-05-26 10:30:19 -04003177 adapter->from_passive_init = true;
3178 complete(&adapter->init_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003179 break;
3180 case IBMVNIC_CRQ_INIT_COMPLETE:
3181 dev_info(dev, "Partner initialization complete\n");
3182 send_version_xchg(adapter);
3183 break;
3184 default:
3185 dev_err(dev, "Unknown crq cmd: %d\n", gen_crq->cmd);
3186 }
3187 return;
3188 case IBMVNIC_CRQ_XPORT_EVENT:
Nathan Fontenoted651a12017-05-03 14:04:38 -04003189 netif_carrier_off(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003190 if (gen_crq->cmd == IBMVNIC_PARTITION_MIGRATED) {
Nathan Fontenoted651a12017-05-03 14:04:38 -04003191 dev_info(dev, "Migrated, re-enabling adapter\n");
3192 ibmvnic_reset(adapter, VNIC_RESET_MOBILITY);
Thomas Falcondfad09a2016-08-18 11:37:51 -05003193 } else if (gen_crq->cmd == IBMVNIC_DEVICE_FAILOVER) {
3194 dev_info(dev, "Backing device failover detected\n");
Nathan Fontenoted651a12017-05-03 14:04:38 -04003195 ibmvnic_reset(adapter, VNIC_RESET_FAILOVER);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003196 } else {
3197 /* The adapter lost the connection */
3198 dev_err(dev, "Virtual Adapter failed (rc=%d)\n",
3199 gen_crq->cmd);
Nathan Fontenoted651a12017-05-03 14:04:38 -04003200 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003201 }
3202 return;
3203 case IBMVNIC_CRQ_CMD_RSP:
3204 break;
3205 default:
3206 dev_err(dev, "Got an invalid msg type 0x%02x\n",
3207 gen_crq->first);
3208 return;
3209 }
3210
3211 switch (gen_crq->cmd) {
3212 case VERSION_EXCHANGE_RSP:
3213 rc = crq->version_exchange_rsp.rc.code;
3214 if (rc) {
3215 dev_err(dev, "Error %ld in VERSION_EXCHG_RSP\n", rc);
3216 break;
3217 }
3218 dev_info(dev, "Partner protocol version is %d\n",
3219 crq->version_exchange_rsp.version);
3220 if (be16_to_cpu(crq->version_exchange_rsp.version) <
3221 ibmvnic_version)
3222 ibmvnic_version =
3223 be16_to_cpu(crq->version_exchange_rsp.version);
3224 send_cap_queries(adapter);
3225 break;
3226 case QUERY_CAPABILITY_RSP:
3227 handle_query_cap_rsp(crq, adapter);
3228 break;
3229 case QUERY_MAP_RSP:
3230 handle_query_map_rsp(crq, adapter);
3231 break;
3232 case REQUEST_MAP_RSP:
3233 handle_request_map_rsp(crq, adapter);
3234 break;
3235 case REQUEST_UNMAP_RSP:
3236 handle_request_unmap_rsp(crq, adapter);
3237 break;
3238 case REQUEST_CAPABILITY_RSP:
3239 handle_request_cap_rsp(crq, adapter);
3240 break;
3241 case LOGIN_RSP:
3242 netdev_dbg(netdev, "Got Login Response\n");
3243 handle_login_rsp(crq, adapter);
3244 break;
3245 case LOGICAL_LINK_STATE_RSP:
Nathan Fontenot53da09e2017-04-21 15:39:04 -04003246 netdev_dbg(netdev,
3247 "Got Logical Link State Response, state: %d rc: %d\n",
3248 crq->logical_link_state_rsp.link_state,
3249 crq->logical_link_state_rsp.rc.code);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003250 adapter->logical_link_state =
3251 crq->logical_link_state_rsp.link_state;
Nathan Fontenot53da09e2017-04-21 15:39:04 -04003252 adapter->init_done_rc = crq->logical_link_state_rsp.rc.code;
3253 complete(&adapter->init_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003254 break;
3255 case LINK_STATE_INDICATION:
3256 netdev_dbg(netdev, "Got Logical Link State Indication\n");
3257 adapter->phys_link_state =
3258 crq->link_state_indication.phys_link_state;
3259 adapter->logical_link_state =
3260 crq->link_state_indication.logical_link_state;
3261 break;
3262 case CHANGE_MAC_ADDR_RSP:
3263 netdev_dbg(netdev, "Got MAC address change Response\n");
3264 handle_change_mac_rsp(crq, adapter);
3265 break;
3266 case ERROR_INDICATION:
3267 netdev_dbg(netdev, "Got Error Indication\n");
3268 handle_error_indication(crq, adapter);
3269 break;
3270 case REQUEST_ERROR_RSP:
3271 netdev_dbg(netdev, "Got Error Detail Response\n");
3272 handle_error_info_rsp(crq, adapter);
3273 break;
3274 case REQUEST_STATISTICS_RSP:
3275 netdev_dbg(netdev, "Got Statistics Response\n");
3276 complete(&adapter->stats_done);
3277 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003278 case QUERY_IP_OFFLOAD_RSP:
3279 netdev_dbg(netdev, "Got Query IP offload Response\n");
3280 handle_query_ip_offload_rsp(adapter);
3281 break;
3282 case MULTICAST_CTRL_RSP:
3283 netdev_dbg(netdev, "Got multicast control Response\n");
3284 break;
3285 case CONTROL_IP_OFFLOAD_RSP:
3286 netdev_dbg(netdev, "Got Control IP offload Response\n");
3287 dma_unmap_single(dev, adapter->ip_offload_ctrl_tok,
3288 sizeof(adapter->ip_offload_ctrl),
3289 DMA_TO_DEVICE);
John Allenbd0b6722017-03-17 17:13:40 -05003290 complete(&adapter->init_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003291 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003292 case COLLECT_FW_TRACE_RSP:
3293 netdev_dbg(netdev, "Got Collect firmware trace Response\n");
3294 complete(&adapter->fw_done);
3295 break;
3296 default:
3297 netdev_err(netdev, "Got an invalid cmd type 0x%02x\n",
3298 gen_crq->cmd);
3299 }
3300}
3301
3302static irqreturn_t ibmvnic_interrupt(int irq, void *instance)
3303{
3304 struct ibmvnic_adapter *adapter = instance;
Thomas Falcon6c267b32017-02-15 12:17:58 -06003305
Thomas Falcon6c267b32017-02-15 12:17:58 -06003306 tasklet_schedule(&adapter->tasklet);
Thomas Falcon6c267b32017-02-15 12:17:58 -06003307 return IRQ_HANDLED;
3308}
3309
3310static void ibmvnic_tasklet(void *data)
3311{
3312 struct ibmvnic_adapter *adapter = data;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003313 struct ibmvnic_crq_queue *queue = &adapter->crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003314 union ibmvnic_crq *crq;
3315 unsigned long flags;
3316 bool done = false;
3317
3318 spin_lock_irqsave(&queue->lock, flags);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003319 while (!done) {
3320 /* Pull all the valid messages off the CRQ */
3321 while ((crq = ibmvnic_next_crq(adapter)) != NULL) {
3322 ibmvnic_handle_crq(crq, adapter);
3323 crq->generic.first = 0;
3324 }
Brian Kinged7ecbf2017-04-19 13:44:53 -04003325
3326 /* remain in tasklet until all
3327 * capabilities responses are received
3328 */
3329 if (!adapter->wait_capability)
3330 done = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003331 }
Thomas Falcon249168a2017-02-15 12:18:00 -06003332 /* if capabilities CRQ's were sent in this tasklet, the following
3333 * tasklet must wait until all responses are received
3334 */
3335 if (atomic_read(&adapter->running_cap_crqs) != 0)
3336 adapter->wait_capability = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003337 spin_unlock_irqrestore(&queue->lock, flags);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003338}
3339
3340static int ibmvnic_reenable_crq_queue(struct ibmvnic_adapter *adapter)
3341{
3342 struct vio_dev *vdev = adapter->vdev;
3343 int rc;
3344
3345 do {
3346 rc = plpar_hcall_norets(H_ENABLE_CRQ, vdev->unit_address);
3347 } while (rc == H_IN_PROGRESS || rc == H_BUSY || H_IS_LONG_BUSY(rc));
3348
3349 if (rc)
3350 dev_err(&vdev->dev, "Error enabling adapter (rc=%d)\n", rc);
3351
3352 return rc;
3353}
3354
3355static int ibmvnic_reset_crq(struct ibmvnic_adapter *adapter)
3356{
3357 struct ibmvnic_crq_queue *crq = &adapter->crq;
3358 struct device *dev = &adapter->vdev->dev;
3359 struct vio_dev *vdev = adapter->vdev;
3360 int rc;
3361
3362 /* Close the CRQ */
3363 do {
3364 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
3365 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
3366
3367 /* Clean out the queue */
3368 memset(crq->msgs, 0, PAGE_SIZE);
3369 crq->cur = 0;
3370
3371 /* And re-open it again */
3372 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
3373 crq->msg_token, PAGE_SIZE);
3374
3375 if (rc == H_CLOSED)
3376 /* Adapter is good, but other end is not ready */
3377 dev_warn(dev, "Partner adapter not ready\n");
3378 else if (rc != 0)
3379 dev_warn(dev, "Couldn't register crq (rc=%d)\n", rc);
3380
3381 return rc;
3382}
3383
Nathan Fontenotf9928872017-03-30 02:48:54 -04003384static void release_crq_queue(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003385{
3386 struct ibmvnic_crq_queue *crq = &adapter->crq;
3387 struct vio_dev *vdev = adapter->vdev;
3388 long rc;
3389
Nathan Fontenotf9928872017-03-30 02:48:54 -04003390 if (!crq->msgs)
3391 return;
3392
Thomas Falcon032c5e82015-12-21 11:26:06 -06003393 netdev_dbg(adapter->netdev, "Releasing CRQ\n");
3394 free_irq(vdev->irq, adapter);
Thomas Falcon6c267b32017-02-15 12:17:58 -06003395 tasklet_kill(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003396 do {
3397 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
3398 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
3399
3400 dma_unmap_single(&vdev->dev, crq->msg_token, PAGE_SIZE,
3401 DMA_BIDIRECTIONAL);
3402 free_page((unsigned long)crq->msgs);
Nathan Fontenotf9928872017-03-30 02:48:54 -04003403 crq->msgs = NULL;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003404}
3405
Nathan Fontenotf9928872017-03-30 02:48:54 -04003406static int init_crq_queue(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003407{
3408 struct ibmvnic_crq_queue *crq = &adapter->crq;
3409 struct device *dev = &adapter->vdev->dev;
3410 struct vio_dev *vdev = adapter->vdev;
3411 int rc, retrc = -ENOMEM;
3412
Nathan Fontenotf9928872017-03-30 02:48:54 -04003413 if (crq->msgs)
3414 return 0;
3415
Thomas Falcon032c5e82015-12-21 11:26:06 -06003416 crq->msgs = (union ibmvnic_crq *)get_zeroed_page(GFP_KERNEL);
3417 /* Should we allocate more than one page? */
3418
3419 if (!crq->msgs)
3420 return -ENOMEM;
3421
3422 crq->size = PAGE_SIZE / sizeof(*crq->msgs);
3423 crq->msg_token = dma_map_single(dev, crq->msgs, PAGE_SIZE,
3424 DMA_BIDIRECTIONAL);
3425 if (dma_mapping_error(dev, crq->msg_token))
3426 goto map_failed;
3427
3428 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
3429 crq->msg_token, PAGE_SIZE);
3430
3431 if (rc == H_RESOURCE)
3432 /* maybe kexecing and resource is busy. try a reset */
3433 rc = ibmvnic_reset_crq(adapter);
3434 retrc = rc;
3435
3436 if (rc == H_CLOSED) {
3437 dev_warn(dev, "Partner adapter not ready\n");
3438 } else if (rc) {
3439 dev_warn(dev, "Error %d opening adapter\n", rc);
3440 goto reg_crq_failed;
3441 }
3442
3443 retrc = 0;
3444
Thomas Falcon6c267b32017-02-15 12:17:58 -06003445 tasklet_init(&adapter->tasklet, (void *)ibmvnic_tasklet,
3446 (unsigned long)adapter);
3447
Thomas Falcon032c5e82015-12-21 11:26:06 -06003448 netdev_dbg(adapter->netdev, "registering irq 0x%x\n", vdev->irq);
3449 rc = request_irq(vdev->irq, ibmvnic_interrupt, 0, IBMVNIC_NAME,
3450 adapter);
3451 if (rc) {
3452 dev_err(dev, "Couldn't register irq 0x%x. rc=%d\n",
3453 vdev->irq, rc);
3454 goto req_irq_failed;
3455 }
3456
3457 rc = vio_enable_interrupts(vdev);
3458 if (rc) {
3459 dev_err(dev, "Error %d enabling interrupts\n", rc);
3460 goto req_irq_failed;
3461 }
3462
3463 crq->cur = 0;
3464 spin_lock_init(&crq->lock);
3465
3466 return retrc;
3467
3468req_irq_failed:
Thomas Falcon6c267b32017-02-15 12:17:58 -06003469 tasklet_kill(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003470 do {
3471 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
3472 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
3473reg_crq_failed:
3474 dma_unmap_single(dev, crq->msg_token, PAGE_SIZE, DMA_BIDIRECTIONAL);
3475map_failed:
3476 free_page((unsigned long)crq->msgs);
Nathan Fontenotf9928872017-03-30 02:48:54 -04003477 crq->msgs = NULL;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003478 return retrc;
3479}
3480
John Allenf6ef6402017-03-17 17:13:42 -05003481static int ibmvnic_init(struct ibmvnic_adapter *adapter)
3482{
3483 struct device *dev = &adapter->vdev->dev;
3484 unsigned long timeout = msecs_to_jiffies(30000);
John Allenf6ef6402017-03-17 17:13:42 -05003485 int rc;
3486
Nathan Fontenotf9928872017-03-30 02:48:54 -04003487 rc = init_crq_queue(adapter);
John Allenf6ef6402017-03-17 17:13:42 -05003488 if (rc) {
3489 dev_err(dev, "Couldn't initialize crq. rc=%d\n", rc);
3490 return rc;
3491 }
3492
John Allen017892c12017-05-26 10:30:19 -04003493 adapter->from_passive_init = false;
3494
John Allenf6ef6402017-03-17 17:13:42 -05003495 init_completion(&adapter->init_done);
3496 ibmvnic_send_crq_init(adapter);
3497 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
3498 dev_err(dev, "Initialization sequence timed out\n");
John Allen017892c12017-05-26 10:30:19 -04003499 return -1;
3500 }
3501
3502 if (adapter->from_passive_init) {
3503 adapter->state = VNIC_OPEN;
3504 adapter->from_passive_init = false;
John Allenf6ef6402017-03-17 17:13:42 -05003505 return -1;
3506 }
3507
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04003508 rc = init_sub_crqs(adapter);
3509 if (rc) {
3510 dev_err(dev, "Initialization of sub crqs failed\n");
3511 release_crq_queue(adapter);
3512 }
3513
3514 return rc;
John Allenf6ef6402017-03-17 17:13:42 -05003515}
3516
Thomas Falcon032c5e82015-12-21 11:26:06 -06003517static int ibmvnic_probe(struct vio_dev *dev, const struct vio_device_id *id)
3518{
3519 struct ibmvnic_adapter *adapter;
3520 struct net_device *netdev;
3521 unsigned char *mac_addr_p;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003522 int rc;
3523
3524 dev_dbg(&dev->dev, "entering ibmvnic_probe for UA 0x%x\n",
3525 dev->unit_address);
3526
3527 mac_addr_p = (unsigned char *)vio_get_attribute(dev,
3528 VETH_MAC_ADDR, NULL);
3529 if (!mac_addr_p) {
3530 dev_err(&dev->dev,
3531 "(%s:%3.3d) ERROR: Can't find MAC_ADDR attribute\n",
3532 __FILE__, __LINE__);
3533 return 0;
3534 }
3535
3536 netdev = alloc_etherdev_mq(sizeof(struct ibmvnic_adapter),
3537 IBMVNIC_MAX_TX_QUEUES);
3538 if (!netdev)
3539 return -ENOMEM;
3540
3541 adapter = netdev_priv(netdev);
Nathan Fontenot90c80142017-05-03 14:04:32 -04003542 adapter->state = VNIC_PROBING;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003543 dev_set_drvdata(&dev->dev, netdev);
3544 adapter->vdev = dev;
3545 adapter->netdev = netdev;
3546
3547 ether_addr_copy(adapter->mac_addr, mac_addr_p);
3548 ether_addr_copy(netdev->dev_addr, adapter->mac_addr);
3549 netdev->irq = dev->irq;
3550 netdev->netdev_ops = &ibmvnic_netdev_ops;
3551 netdev->ethtool_ops = &ibmvnic_ethtool_ops;
3552 SET_NETDEV_DEV(netdev, &dev->dev);
3553
3554 spin_lock_init(&adapter->stats_lock);
3555
Thomas Falcon032c5e82015-12-21 11:26:06 -06003556 INIT_LIST_HEAD(&adapter->errors);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003557 spin_lock_init(&adapter->error_list_lock);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003558
Nathan Fontenoted651a12017-05-03 14:04:38 -04003559 INIT_WORK(&adapter->ibmvnic_reset, __ibmvnic_reset);
3560 INIT_LIST_HEAD(&adapter->rwi_list);
3561 mutex_init(&adapter->reset_lock);
3562 mutex_init(&adapter->rwi_lock);
3563 adapter->resetting = false;
3564
John Allenf6ef6402017-03-17 17:13:42 -05003565 rc = ibmvnic_init(adapter);
3566 if (rc) {
3567 free_netdev(netdev);
3568 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003569 }
3570
Thomas Falconf39f0d12017-02-14 10:22:59 -06003571 netdev->mtu = adapter->req_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003572
3573 rc = register_netdev(netdev);
3574 if (rc) {
3575 dev_err(&dev->dev, "failed to register netdev rc=%d\n", rc);
John Allenf6ef6402017-03-17 17:13:42 -05003576 free_netdev(netdev);
3577 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003578 }
3579 dev_info(&dev->dev, "ibmvnic registered\n");
3580
Nathan Fontenot90c80142017-05-03 14:04:32 -04003581 adapter->state = VNIC_PROBED;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003582 return 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003583}
3584
3585static int ibmvnic_remove(struct vio_dev *dev)
3586{
3587 struct net_device *netdev = dev_get_drvdata(&dev->dev);
Nathan Fontenot37489052017-04-19 13:45:04 -04003588 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003589
Nathan Fontenot90c80142017-05-03 14:04:32 -04003590 adapter->state = VNIC_REMOVING;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003591 unregister_netdev(netdev);
Nathan Fontenoted651a12017-05-03 14:04:38 -04003592 mutex_lock(&adapter->reset_lock);
Nathan Fontenot37489052017-04-19 13:45:04 -04003593
3594 release_resources(adapter);
3595 release_sub_crqs(adapter);
3596 release_crq_queue(adapter);
3597
Nathan Fontenot90c80142017-05-03 14:04:32 -04003598 adapter->state = VNIC_REMOVED;
3599
Nathan Fontenoted651a12017-05-03 14:04:38 -04003600 mutex_unlock(&adapter->reset_lock);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003601 free_netdev(netdev);
3602 dev_set_drvdata(&dev->dev, NULL);
3603
3604 return 0;
3605}
3606
3607static unsigned long ibmvnic_get_desired_dma(struct vio_dev *vdev)
3608{
3609 struct net_device *netdev = dev_get_drvdata(&vdev->dev);
3610 struct ibmvnic_adapter *adapter;
3611 struct iommu_table *tbl;
3612 unsigned long ret = 0;
3613 int i;
3614
3615 tbl = get_iommu_table_base(&vdev->dev);
3616
3617 /* netdev inits at probe time along with the structures we need below*/
3618 if (!netdev)
3619 return IOMMU_PAGE_ALIGN(IBMVNIC_IO_ENTITLEMENT_DEFAULT, tbl);
3620
3621 adapter = netdev_priv(netdev);
3622
3623 ret += PAGE_SIZE; /* the crq message queue */
Thomas Falcon032c5e82015-12-21 11:26:06 -06003624 ret += IOMMU_PAGE_ALIGN(sizeof(struct ibmvnic_statistics), tbl);
3625
3626 for (i = 0; i < adapter->req_tx_queues + adapter->req_rx_queues; i++)
3627 ret += 4 * PAGE_SIZE; /* the scrq message queue */
3628
3629 for (i = 0; i < be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
3630 i++)
3631 ret += adapter->rx_pool[i].size *
3632 IOMMU_PAGE_ALIGN(adapter->rx_pool[i].buff_size, tbl);
3633
3634 return ret;
3635}
3636
3637static int ibmvnic_resume(struct device *dev)
3638{
3639 struct net_device *netdev = dev_get_drvdata(dev);
3640 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
3641 int i;
3642
3643 /* kick the interrupt handlers just in case we lost an interrupt */
3644 for (i = 0; i < adapter->req_rx_queues; i++)
3645 ibmvnic_interrupt_rx(adapter->rx_scrq[i]->irq,
3646 adapter->rx_scrq[i]);
3647
3648 return 0;
3649}
3650
3651static struct vio_device_id ibmvnic_device_table[] = {
3652 {"network", "IBM,vnic"},
3653 {"", "" }
3654};
3655MODULE_DEVICE_TABLE(vio, ibmvnic_device_table);
3656
3657static const struct dev_pm_ops ibmvnic_pm_ops = {
3658 .resume = ibmvnic_resume
3659};
3660
3661static struct vio_driver ibmvnic_driver = {
3662 .id_table = ibmvnic_device_table,
3663 .probe = ibmvnic_probe,
3664 .remove = ibmvnic_remove,
3665 .get_desired_dma = ibmvnic_get_desired_dma,
3666 .name = ibmvnic_driver_name,
3667 .pm = &ibmvnic_pm_ops,
3668};
3669
3670/* module functions */
3671static int __init ibmvnic_module_init(void)
3672{
3673 pr_info("%s: %s %s\n", ibmvnic_driver_name, ibmvnic_driver_string,
3674 IBMVNIC_DRIVER_VERSION);
3675
3676 return vio_register_driver(&ibmvnic_driver);
3677}
3678
3679static void __exit ibmvnic_module_exit(void)
3680{
3681 vio_unregister_driver(&ibmvnic_driver);
3682}
3683
3684module_init(ibmvnic_module_init);
3685module_exit(ibmvnic_module_exit);