blob: 5661a043f5e536c086f29af92f7f0d1b201f5211 [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
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400166static void reset_long_term_buff(struct ibmvnic_adapter *adapter,
167 struct ibmvnic_long_term_buff *ltb)
168{
169 memset(ltb->buff, 0, ltb->size);
170
171 init_completion(&adapter->fw_done);
172 send_request_map(adapter, ltb->addr, ltb->size, ltb->map_id);
173 wait_for_completion(&adapter->fw_done);
174}
175
Thomas Falcon032c5e82015-12-21 11:26:06 -0600176static int alloc_long_term_buff(struct ibmvnic_adapter *adapter,
177 struct ibmvnic_long_term_buff *ltb, int size)
178{
179 struct device *dev = &adapter->vdev->dev;
180
181 ltb->size = size;
182 ltb->buff = dma_alloc_coherent(dev, ltb->size, &ltb->addr,
183 GFP_KERNEL);
184
185 if (!ltb->buff) {
186 dev_err(dev, "Couldn't alloc long term buffer\n");
187 return -ENOMEM;
188 }
189 ltb->map_id = adapter->map_id;
190 adapter->map_id++;
Nathan Fontenotdb5d0b52017-02-10 13:45:05 -0500191
192 init_completion(&adapter->fw_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600193 send_request_map(adapter, ltb->addr,
194 ltb->size, ltb->map_id);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600195 wait_for_completion(&adapter->fw_done);
196 return 0;
197}
198
199static void free_long_term_buff(struct ibmvnic_adapter *adapter,
200 struct ibmvnic_long_term_buff *ltb)
201{
202 struct device *dev = &adapter->vdev->dev;
203
Nathan Fontenotc657e322017-03-30 02:49:06 -0400204 if (!ltb->buff)
205 return;
206
Nathan Fontenoted651a12017-05-03 14:04:38 -0400207 if (adapter->reset_reason != VNIC_RESET_FAILOVER &&
208 adapter->reset_reason != VNIC_RESET_MOBILITY)
Thomas Falcondfad09a2016-08-18 11:37:51 -0500209 send_request_unmap(adapter, ltb->map_id);
Brian King59af56c2017-04-19 13:44:41 -0400210 dma_free_coherent(dev, ltb->size, ltb->buff, ltb->addr);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600211}
212
Thomas Falconf185a492017-05-26 10:30:48 -0400213static void deactivate_rx_pools(struct ibmvnic_adapter *adapter)
214{
215 int i;
216
217 for (i = 0; i < be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
218 i++)
219 adapter->rx_pool[i].active = 0;
220}
221
Thomas Falcon032c5e82015-12-21 11:26:06 -0600222static void replenish_rx_pool(struct ibmvnic_adapter *adapter,
223 struct ibmvnic_rx_pool *pool)
224{
225 int count = pool->size - atomic_read(&pool->available);
226 struct device *dev = &adapter->vdev->dev;
227 int buffers_added = 0;
228 unsigned long lpar_rc;
229 union sub_crq sub_crq;
230 struct sk_buff *skb;
231 unsigned int offset;
232 dma_addr_t dma_addr;
233 unsigned char *dst;
234 u64 *handle_array;
235 int shift = 0;
236 int index;
237 int i;
238
Thomas Falconf185a492017-05-26 10:30:48 -0400239 if (!pool->active)
240 return;
241
Thomas Falcon032c5e82015-12-21 11:26:06 -0600242 handle_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
243 be32_to_cpu(adapter->login_rsp_buf->
244 off_rxadd_subcrqs));
245
246 for (i = 0; i < count; ++i) {
247 skb = alloc_skb(pool->buff_size, GFP_ATOMIC);
248 if (!skb) {
249 dev_err(dev, "Couldn't replenish rx buff\n");
250 adapter->replenish_no_mem++;
251 break;
252 }
253
254 index = pool->free_map[pool->next_free];
255
256 if (pool->rx_buff[index].skb)
257 dev_err(dev, "Inconsistent free_map!\n");
258
259 /* Copy the skb to the long term mapped DMA buffer */
260 offset = index * pool->buff_size;
261 dst = pool->long_term_buff.buff + offset;
262 memset(dst, 0, pool->buff_size);
263 dma_addr = pool->long_term_buff.addr + offset;
264 pool->rx_buff[index].data = dst;
265
266 pool->free_map[pool->next_free] = IBMVNIC_INVALID_MAP;
267 pool->rx_buff[index].dma = dma_addr;
268 pool->rx_buff[index].skb = skb;
269 pool->rx_buff[index].pool_index = pool->index;
270 pool->rx_buff[index].size = pool->buff_size;
271
272 memset(&sub_crq, 0, sizeof(sub_crq));
273 sub_crq.rx_add.first = IBMVNIC_CRQ_CMD;
274 sub_crq.rx_add.correlator =
275 cpu_to_be64((u64)&pool->rx_buff[index]);
276 sub_crq.rx_add.ioba = cpu_to_be32(dma_addr);
277 sub_crq.rx_add.map_id = pool->long_term_buff.map_id;
278
279 /* The length field of the sCRQ is defined to be 24 bits so the
280 * buffer size needs to be left shifted by a byte before it is
281 * converted to big endian to prevent the last byte from being
282 * truncated.
283 */
284#ifdef __LITTLE_ENDIAN__
285 shift = 8;
286#endif
287 sub_crq.rx_add.len = cpu_to_be32(pool->buff_size << shift);
288
289 lpar_rc = send_subcrq(adapter, handle_array[pool->index],
290 &sub_crq);
291 if (lpar_rc != H_SUCCESS)
292 goto failure;
293
294 buffers_added++;
295 adapter->replenish_add_buff_success++;
296 pool->next_free = (pool->next_free + 1) % pool->size;
297 }
298 atomic_add(buffers_added, &pool->available);
299 return;
300
301failure:
302 dev_info(dev, "replenish pools failure\n");
303 pool->free_map[pool->next_free] = index;
304 pool->rx_buff[index].skb = NULL;
305 if (!dma_mapping_error(dev, dma_addr))
306 dma_unmap_single(dev, dma_addr, pool->buff_size,
307 DMA_FROM_DEVICE);
308
309 dev_kfree_skb_any(skb);
310 adapter->replenish_add_buff_failure++;
311 atomic_add(buffers_added, &pool->available);
Thomas Falconf185a492017-05-26 10:30:48 -0400312
313 if (lpar_rc == H_CLOSED) {
314 /* Disable buffer pool replenishment and report carrier off if
315 * queue is closed. Firmware guarantees that a signal will
316 * be sent to the driver, triggering a reset.
317 */
318 deactivate_rx_pools(adapter);
319 netif_carrier_off(adapter->netdev);
320 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600321}
322
323static void replenish_pools(struct ibmvnic_adapter *adapter)
324{
325 int i;
326
Thomas Falcon032c5e82015-12-21 11:26:06 -0600327 adapter->replenish_task_cycles++;
328 for (i = 0; i < be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
329 i++) {
330 if (adapter->rx_pool[i].active)
331 replenish_rx_pool(adapter, &adapter->rx_pool[i]);
332 }
333}
334
Nathan Fontenot7bbc27a2017-03-30 02:49:23 -0400335static void release_stats_token(struct ibmvnic_adapter *adapter)
336{
337 struct device *dev = &adapter->vdev->dev;
338
339 if (!adapter->stats_token)
340 return;
341
342 dma_unmap_single(dev, adapter->stats_token,
343 sizeof(struct ibmvnic_statistics),
344 DMA_FROM_DEVICE);
345 adapter->stats_token = 0;
346}
347
348static int init_stats_token(struct ibmvnic_adapter *adapter)
349{
350 struct device *dev = &adapter->vdev->dev;
351 dma_addr_t stok;
352
353 stok = dma_map_single(dev, &adapter->stats,
354 sizeof(struct ibmvnic_statistics),
355 DMA_FROM_DEVICE);
356 if (dma_mapping_error(dev, stok)) {
357 dev_err(dev, "Couldn't map stats buffer\n");
358 return -1;
359 }
360
361 adapter->stats_token = stok;
362 return 0;
363}
364
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400365static int reset_rx_pools(struct ibmvnic_adapter *adapter)
366{
367 struct ibmvnic_rx_pool *rx_pool;
368 int rx_scrqs;
369 int i, j;
370
371 rx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
372 for (i = 0; i < rx_scrqs; i++) {
373 rx_pool = &adapter->rx_pool[i];
374
375 reset_long_term_buff(adapter, &rx_pool->long_term_buff);
376
377 for (j = 0; j < rx_pool->size; j++)
378 rx_pool->free_map[j] = j;
379
380 memset(rx_pool->rx_buff, 0,
381 rx_pool->size * sizeof(struct ibmvnic_rx_buff));
382
383 atomic_set(&rx_pool->available, 0);
384 rx_pool->next_alloc = 0;
385 rx_pool->next_free = 0;
386 }
387
388 return 0;
389}
390
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400391static void release_rx_pools(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -0600392{
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400393 struct ibmvnic_rx_pool *rx_pool;
394 int rx_scrqs;
395 int i, j;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600396
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400397 if (!adapter->rx_pool)
Thomas Falcon032c5e82015-12-21 11:26:06 -0600398 return;
399
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400400 rx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
401 for (i = 0; i < rx_scrqs; i++) {
402 rx_pool = &adapter->rx_pool[i];
403
404 kfree(rx_pool->free_map);
405 free_long_term_buff(adapter, &rx_pool->long_term_buff);
406
407 if (!rx_pool->rx_buff)
Nathan Fontenote0ebe9422017-05-03 14:04:50 -0400408 continue;
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400409
410 for (j = 0; j < rx_pool->size; j++) {
411 if (rx_pool->rx_buff[j].skb) {
412 dev_kfree_skb_any(rx_pool->rx_buff[i].skb);
413 rx_pool->rx_buff[i].skb = NULL;
414 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600415 }
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400416
417 kfree(rx_pool->rx_buff);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600418 }
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400419
420 kfree(adapter->rx_pool);
421 adapter->rx_pool = NULL;
422}
423
424static int init_rx_pools(struct net_device *netdev)
425{
426 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
427 struct device *dev = &adapter->vdev->dev;
428 struct ibmvnic_rx_pool *rx_pool;
429 int rxadd_subcrqs;
430 u64 *size_array;
431 int i, j;
432
433 rxadd_subcrqs =
434 be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
435 size_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
436 be32_to_cpu(adapter->login_rsp_buf->off_rxadd_buff_size));
437
438 adapter->rx_pool = kcalloc(rxadd_subcrqs,
439 sizeof(struct ibmvnic_rx_pool),
440 GFP_KERNEL);
441 if (!adapter->rx_pool) {
442 dev_err(dev, "Failed to allocate rx pools\n");
443 return -1;
444 }
445
446 for (i = 0; i < rxadd_subcrqs; i++) {
447 rx_pool = &adapter->rx_pool[i];
448
449 netdev_dbg(adapter->netdev,
450 "Initializing rx_pool %d, %lld buffs, %lld bytes each\n",
451 i, adapter->req_rx_add_entries_per_subcrq,
452 be64_to_cpu(size_array[i]));
453
454 rx_pool->size = adapter->req_rx_add_entries_per_subcrq;
455 rx_pool->index = i;
456 rx_pool->buff_size = be64_to_cpu(size_array[i]);
457 rx_pool->active = 1;
458
459 rx_pool->free_map = kcalloc(rx_pool->size, sizeof(int),
460 GFP_KERNEL);
461 if (!rx_pool->free_map) {
462 release_rx_pools(adapter);
463 return -1;
464 }
465
466 rx_pool->rx_buff = kcalloc(rx_pool->size,
467 sizeof(struct ibmvnic_rx_buff),
468 GFP_KERNEL);
469 if (!rx_pool->rx_buff) {
470 dev_err(dev, "Couldn't alloc rx buffers\n");
471 release_rx_pools(adapter);
472 return -1;
473 }
474
475 if (alloc_long_term_buff(adapter, &rx_pool->long_term_buff,
476 rx_pool->size * rx_pool->buff_size)) {
477 release_rx_pools(adapter);
478 return -1;
479 }
480
481 for (j = 0; j < rx_pool->size; ++j)
482 rx_pool->free_map[j] = j;
483
484 atomic_set(&rx_pool->available, 0);
485 rx_pool->next_alloc = 0;
486 rx_pool->next_free = 0;
487 }
488
489 return 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600490}
491
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400492static int reset_tx_pools(struct ibmvnic_adapter *adapter)
493{
494 struct ibmvnic_tx_pool *tx_pool;
495 int tx_scrqs;
496 int i, j;
497
498 tx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
499 for (i = 0; i < tx_scrqs; i++) {
500 tx_pool = &adapter->tx_pool[i];
501
502 reset_long_term_buff(adapter, &tx_pool->long_term_buff);
503
504 memset(tx_pool->tx_buff, 0,
505 adapter->req_tx_entries_per_subcrq *
506 sizeof(struct ibmvnic_tx_buff));
507
508 for (j = 0; j < adapter->req_tx_entries_per_subcrq; j++)
509 tx_pool->free_map[j] = j;
510
511 tx_pool->consumer_index = 0;
512 tx_pool->producer_index = 0;
513 }
514
515 return 0;
516}
517
Nathan Fontenotc657e322017-03-30 02:49:06 -0400518static void release_tx_pools(struct ibmvnic_adapter *adapter)
519{
520 struct ibmvnic_tx_pool *tx_pool;
521 int i, tx_scrqs;
522
523 if (!adapter->tx_pool)
524 return;
525
526 tx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
527 for (i = 0; i < tx_scrqs; i++) {
528 tx_pool = &adapter->tx_pool[i];
529 kfree(tx_pool->tx_buff);
530 free_long_term_buff(adapter, &tx_pool->long_term_buff);
531 kfree(tx_pool->free_map);
532 }
533
534 kfree(adapter->tx_pool);
535 adapter->tx_pool = NULL;
536}
537
538static int init_tx_pools(struct net_device *netdev)
539{
540 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
541 struct device *dev = &adapter->vdev->dev;
542 struct ibmvnic_tx_pool *tx_pool;
543 int tx_subcrqs;
544 int i, j;
545
546 tx_subcrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
547 adapter->tx_pool = kcalloc(tx_subcrqs,
548 sizeof(struct ibmvnic_tx_pool), GFP_KERNEL);
549 if (!adapter->tx_pool)
550 return -1;
551
552 for (i = 0; i < tx_subcrqs; i++) {
553 tx_pool = &adapter->tx_pool[i];
554 tx_pool->tx_buff = kcalloc(adapter->req_tx_entries_per_subcrq,
555 sizeof(struct ibmvnic_tx_buff),
556 GFP_KERNEL);
557 if (!tx_pool->tx_buff) {
558 dev_err(dev, "tx pool buffer allocation failed\n");
559 release_tx_pools(adapter);
560 return -1;
561 }
562
563 if (alloc_long_term_buff(adapter, &tx_pool->long_term_buff,
564 adapter->req_tx_entries_per_subcrq *
565 adapter->req_mtu)) {
566 release_tx_pools(adapter);
567 return -1;
568 }
569
570 tx_pool->free_map = kcalloc(adapter->req_tx_entries_per_subcrq,
571 sizeof(int), GFP_KERNEL);
572 if (!tx_pool->free_map) {
573 release_tx_pools(adapter);
574 return -1;
575 }
576
577 for (j = 0; j < adapter->req_tx_entries_per_subcrq; j++)
578 tx_pool->free_map[j] = j;
579
580 tx_pool->consumer_index = 0;
581 tx_pool->producer_index = 0;
582 }
583
584 return 0;
585}
586
Nathan Fontenot661a2622017-04-19 13:44:58 -0400587static void release_error_buffers(struct ibmvnic_adapter *adapter)
588{
589 struct device *dev = &adapter->vdev->dev;
590 struct ibmvnic_error_buff *error_buff, *tmp;
591 unsigned long flags;
592
593 spin_lock_irqsave(&adapter->error_list_lock, flags);
594 list_for_each_entry_safe(error_buff, tmp, &adapter->errors, list) {
595 list_del(&error_buff->list);
596 dma_unmap_single(dev, error_buff->dma, error_buff->len,
597 DMA_FROM_DEVICE);
598 kfree(error_buff->buff);
599 kfree(error_buff);
600 }
601 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
602}
603
John Allend944c3d62017-05-26 10:30:13 -0400604static void ibmvnic_napi_enable(struct ibmvnic_adapter *adapter)
605{
606 int i;
607
608 if (adapter->napi_enabled)
609 return;
610
611 for (i = 0; i < adapter->req_rx_queues; i++)
612 napi_enable(&adapter->napi[i]);
613
614 adapter->napi_enabled = true;
615}
616
617static void ibmvnic_napi_disable(struct ibmvnic_adapter *adapter)
618{
619 int i;
620
621 if (!adapter->napi_enabled)
622 return;
623
624 for (i = 0; i < adapter->req_rx_queues; i++)
625 napi_disable(&adapter->napi[i]);
626
627 adapter->napi_enabled = false;
628}
629
John Allena57a5d22017-03-17 17:13:41 -0500630static int ibmvnic_login(struct net_device *netdev)
Thomas Falcon032c5e82015-12-21 11:26:06 -0600631{
632 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
John Allenbd0b6722017-03-17 17:13:40 -0500633 unsigned long timeout = msecs_to_jiffies(30000);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600634 struct device *dev = &adapter->vdev->dev;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600635
John Allenbd0b6722017-03-17 17:13:40 -0500636 do {
637 if (adapter->renegotiate) {
638 adapter->renegotiate = false;
Nathan Fontenotb5108882017-03-30 02:49:18 -0400639 release_sub_crqs(adapter);
John Allenbd0b6722017-03-17 17:13:40 -0500640
641 reinit_completion(&adapter->init_done);
642 send_cap_queries(adapter);
643 if (!wait_for_completion_timeout(&adapter->init_done,
644 timeout)) {
645 dev_err(dev, "Capabilities query timeout\n");
646 return -1;
647 }
648 }
649
650 reinit_completion(&adapter->init_done);
651 send_login(adapter);
652 if (!wait_for_completion_timeout(&adapter->init_done,
653 timeout)) {
654 dev_err(dev, "Login timeout\n");
655 return -1;
656 }
657 } while (adapter->renegotiate);
658
John Allena57a5d22017-03-17 17:13:41 -0500659 return 0;
660}
661
Nathan Fontenot1b8955e2017-03-30 02:49:29 -0400662static void release_resources(struct ibmvnic_adapter *adapter)
663{
Nathan Fontenotc7bac002017-05-03 14:04:44 -0400664 int i;
665
Nathan Fontenot1b8955e2017-03-30 02:49:29 -0400666 release_tx_pools(adapter);
667 release_rx_pools(adapter);
668
Nathan Fontenot1b8955e2017-03-30 02:49:29 -0400669 release_stats_token(adapter);
Nathan Fontenot661a2622017-04-19 13:44:58 -0400670 release_error_buffers(adapter);
Nathan Fontenotc7bac002017-05-03 14:04:44 -0400671
672 if (adapter->napi) {
673 for (i = 0; i < adapter->req_rx_queues; i++) {
674 if (&adapter->napi[i])
675 netif_napi_del(&adapter->napi[i]);
676 }
677 }
Nathan Fontenot1b8955e2017-03-30 02:49:29 -0400678}
679
Nathan Fontenot53da09e2017-04-21 15:39:04 -0400680static int set_link_state(struct ibmvnic_adapter *adapter, u8 link_state)
681{
682 struct net_device *netdev = adapter->netdev;
683 unsigned long timeout = msecs_to_jiffies(30000);
684 union ibmvnic_crq crq;
685 bool resend;
686 int rc;
687
Nathan Fontenot53da09e2017-04-21 15:39:04 -0400688 netdev_err(netdev, "setting link state %d\n", link_state);
689 memset(&crq, 0, sizeof(crq));
690 crq.logical_link_state.first = IBMVNIC_CRQ_CMD;
691 crq.logical_link_state.cmd = LOGICAL_LINK_STATE;
692 crq.logical_link_state.link_state = link_state;
693
694 do {
695 resend = false;
696
697 reinit_completion(&adapter->init_done);
698 rc = ibmvnic_send_crq(adapter, &crq);
699 if (rc) {
700 netdev_err(netdev, "Failed to set link state\n");
701 return rc;
702 }
703
704 if (!wait_for_completion_timeout(&adapter->init_done,
705 timeout)) {
706 netdev_err(netdev, "timeout setting link state\n");
707 return -1;
708 }
709
710 if (adapter->init_done_rc == 1) {
711 /* Partuial success, delay and re-send */
712 mdelay(1000);
713 resend = true;
714 }
715 } while (resend);
716
717 return 0;
718}
719
Thomas Falcon7f3c6e62017-04-21 15:38:40 -0400720static int set_real_num_queues(struct net_device *netdev)
721{
722 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
723 int rc;
724
725 rc = netif_set_real_num_tx_queues(netdev, adapter->req_tx_queues);
726 if (rc) {
727 netdev_err(netdev, "failed to set the number of tx queues\n");
728 return rc;
729 }
730
731 rc = netif_set_real_num_rx_queues(netdev, adapter->req_rx_queues);
732 if (rc)
733 netdev_err(netdev, "failed to set the number of rx queues\n");
734
735 return rc;
736}
737
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400738static int init_resources(struct ibmvnic_adapter *adapter)
John Allena57a5d22017-03-17 17:13:41 -0500739{
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400740 struct net_device *netdev = adapter->netdev;
741 int i, rc;
John Allena57a5d22017-03-17 17:13:41 -0500742
Thomas Falcon7f3c6e62017-04-21 15:38:40 -0400743 rc = set_real_num_queues(netdev);
744 if (rc)
745 return rc;
John Allenbd0b6722017-03-17 17:13:40 -0500746
747 rc = init_sub_crq_irqs(adapter);
748 if (rc) {
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400749 netdev_err(netdev, "failed to initialize sub crq irqs\n");
John Allenbd0b6722017-03-17 17:13:40 -0500750 return -1;
751 }
752
Nathan Fontenot5d5e84e2017-04-21 15:38:58 -0400753 rc = init_stats_token(adapter);
754 if (rc)
755 return rc;
756
Thomas Falcon032c5e82015-12-21 11:26:06 -0600757 adapter->map_id = 1;
758 adapter->napi = kcalloc(adapter->req_rx_queues,
759 sizeof(struct napi_struct), GFP_KERNEL);
760 if (!adapter->napi)
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400761 return -ENOMEM;
762
Thomas Falcon032c5e82015-12-21 11:26:06 -0600763 for (i = 0; i < adapter->req_rx_queues; i++) {
764 netif_napi_add(netdev, &adapter->napi[i], ibmvnic_poll,
765 NAPI_POLL_WEIGHT);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600766 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600767
Thomas Falcon032c5e82015-12-21 11:26:06 -0600768 send_map_query(adapter);
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400769
770 rc = init_rx_pools(netdev);
771 if (rc)
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400772 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600773
Nathan Fontenotc657e322017-03-30 02:49:06 -0400774 rc = init_tx_pools(netdev);
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400775 return rc;
776}
777
Nathan Fontenoted651a12017-05-03 14:04:38 -0400778static int __ibmvnic_open(struct net_device *netdev)
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400779{
780 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Nathan Fontenoted651a12017-05-03 14:04:38 -0400781 enum vnic_state prev_state = adapter->state;
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400782 int i, rc;
783
Nathan Fontenot90c80142017-05-03 14:04:32 -0400784 adapter->state = VNIC_OPENING;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600785 replenish_pools(adapter);
John Allend944c3d62017-05-26 10:30:13 -0400786 ibmvnic_napi_enable(adapter);
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400787
Thomas Falcon032c5e82015-12-21 11:26:06 -0600788 /* We're ready to receive frames, enable the sub-crq interrupts and
789 * set the logical link state to up
790 */
Nathan Fontenoted651a12017-05-03 14:04:38 -0400791 for (i = 0; i < adapter->req_rx_queues; i++) {
792 if (prev_state == VNIC_CLOSED)
793 enable_irq(adapter->rx_scrq[i]->irq);
794 else
795 enable_scrq_irq(adapter, adapter->rx_scrq[i]);
796 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600797
Nathan Fontenoted651a12017-05-03 14:04:38 -0400798 for (i = 0; i < adapter->req_tx_queues; i++) {
799 if (prev_state == VNIC_CLOSED)
800 enable_irq(adapter->tx_scrq[i]->irq);
801 else
802 enable_scrq_irq(adapter, adapter->tx_scrq[i]);
803 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600804
Nathan Fontenot53da09e2017-04-21 15:39:04 -0400805 rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_UP);
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400806 if (rc) {
807 for (i = 0; i < adapter->req_rx_queues; i++)
808 napi_disable(&adapter->napi[i]);
809 release_resources(adapter);
Nathan Fontenoted651a12017-05-03 14:04:38 -0400810 return rc;
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400811 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600812
Nathan Fontenoted651a12017-05-03 14:04:38 -0400813 netif_tx_start_all_queues(netdev);
814
815 if (prev_state == VNIC_CLOSED) {
816 for (i = 0; i < adapter->req_rx_queues; i++)
817 napi_schedule(&adapter->napi[i]);
818 }
819
820 adapter->state = VNIC_OPEN;
821 return rc;
822}
823
824static int ibmvnic_open(struct net_device *netdev)
825{
826 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
827 int rc;
828
829 mutex_lock(&adapter->reset_lock);
830
831 if (adapter->state != VNIC_CLOSED) {
832 rc = ibmvnic_login(netdev);
833 if (rc) {
834 mutex_unlock(&adapter->reset_lock);
835 return rc;
836 }
837
838 rc = init_resources(adapter);
839 if (rc) {
840 netdev_err(netdev, "failed to initialize resources\n");
841 release_resources(adapter);
842 mutex_unlock(&adapter->reset_lock);
843 return rc;
844 }
845 }
846
847 rc = __ibmvnic_open(netdev);
848 mutex_unlock(&adapter->reset_lock);
849
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400850 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600851}
852
Nathan Fontenotb41b83e2017-05-03 14:04:56 -0400853static void clean_tx_pools(struct ibmvnic_adapter *adapter)
854{
855 struct ibmvnic_tx_pool *tx_pool;
856 u64 tx_entries;
857 int tx_scrqs;
858 int i, j;
859
860 if (!adapter->tx_pool)
861 return;
862
863 tx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
864 tx_entries = adapter->req_tx_entries_per_subcrq;
865
866 /* Free any remaining skbs in the tx buffer pools */
867 for (i = 0; i < tx_scrqs; i++) {
868 tx_pool = &adapter->tx_pool[i];
869 if (!tx_pool)
870 continue;
871
872 for (j = 0; j < tx_entries; j++) {
873 if (tx_pool->tx_buff[j].skb) {
874 dev_kfree_skb_any(tx_pool->tx_buff[j].skb);
875 tx_pool->tx_buff[j].skb = NULL;
876 }
877 }
878 }
879}
880
Nathan Fontenoted651a12017-05-03 14:04:38 -0400881static int __ibmvnic_close(struct net_device *netdev)
John Allenea5509f2017-03-17 17:13:43 -0500882{
883 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Nathan Fontenot53da09e2017-04-21 15:39:04 -0400884 int rc = 0;
John Allenea5509f2017-03-17 17:13:43 -0500885 int i;
886
Nathan Fontenot90c80142017-05-03 14:04:32 -0400887 adapter->state = VNIC_CLOSING;
Nathan Fontenoted651a12017-05-03 14:04:38 -0400888 netif_tx_stop_all_queues(netdev);
John Allend944c3d62017-05-26 10:30:13 -0400889 ibmvnic_napi_disable(adapter);
Nathan Fontenot46293b92017-05-03 14:05:02 -0400890
891 if (adapter->tx_scrq) {
892 for (i = 0; i < adapter->req_tx_queues; i++)
893 if (adapter->tx_scrq[i]->irq)
894 disable_irq(adapter->tx_scrq[i]->irq);
895 }
896
Nathan Fontenot53da09e2017-04-21 15:39:04 -0400897 rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_DN);
Nathan Fontenot46293b92017-05-03 14:05:02 -0400898 if (rc)
899 return rc;
900
901 if (adapter->rx_scrq) {
902 for (i = 0; i < adapter->req_rx_queues; i++) {
903 int retries = 10;
904
905 while (pending_scrq(adapter, adapter->rx_scrq[i])) {
906 retries--;
907 mdelay(100);
908
909 if (retries == 0)
910 break;
911 }
912
913 if (adapter->rx_scrq[i]->irq)
914 disable_irq(adapter->rx_scrq[i]->irq);
915 }
916 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600917
Thomas Falcon10f76212017-05-26 10:30:31 -0400918 clean_tx_pools(adapter);
Nathan Fontenot90c80142017-05-03 14:04:32 -0400919 adapter->state = VNIC_CLOSED;
Nathan Fontenot53da09e2017-04-21 15:39:04 -0400920 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600921}
922
Nathan Fontenoted651a12017-05-03 14:04:38 -0400923static int ibmvnic_close(struct net_device *netdev)
924{
925 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
926 int rc;
927
928 mutex_lock(&adapter->reset_lock);
929 rc = __ibmvnic_close(netdev);
930 mutex_unlock(&adapter->reset_lock);
931
932 return rc;
933}
934
Thomas Falconad7775d2016-04-01 17:20:34 -0500935/**
936 * build_hdr_data - creates L2/L3/L4 header data buffer
937 * @hdr_field - bitfield determining needed headers
938 * @skb - socket buffer
939 * @hdr_len - array of header lengths
940 * @tot_len - total length of data
941 *
942 * Reads hdr_field to determine which headers are needed by firmware.
943 * Builds a buffer containing these headers. Saves individual header
944 * lengths and total buffer length to be used to build descriptors.
945 */
946static int build_hdr_data(u8 hdr_field, struct sk_buff *skb,
947 int *hdr_len, u8 *hdr_data)
948{
949 int len = 0;
950 u8 *hdr;
951
952 hdr_len[0] = sizeof(struct ethhdr);
953
954 if (skb->protocol == htons(ETH_P_IP)) {
955 hdr_len[1] = ip_hdr(skb)->ihl * 4;
956 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
957 hdr_len[2] = tcp_hdrlen(skb);
958 else if (ip_hdr(skb)->protocol == IPPROTO_UDP)
959 hdr_len[2] = sizeof(struct udphdr);
960 } else if (skb->protocol == htons(ETH_P_IPV6)) {
961 hdr_len[1] = sizeof(struct ipv6hdr);
962 if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
963 hdr_len[2] = tcp_hdrlen(skb);
964 else if (ipv6_hdr(skb)->nexthdr == IPPROTO_UDP)
965 hdr_len[2] = sizeof(struct udphdr);
966 }
967
968 memset(hdr_data, 0, 120);
969 if ((hdr_field >> 6) & 1) {
970 hdr = skb_mac_header(skb);
971 memcpy(hdr_data, hdr, hdr_len[0]);
972 len += hdr_len[0];
973 }
974
975 if ((hdr_field >> 5) & 1) {
976 hdr = skb_network_header(skb);
977 memcpy(hdr_data + len, hdr, hdr_len[1]);
978 len += hdr_len[1];
979 }
980
981 if ((hdr_field >> 4) & 1) {
982 hdr = skb_transport_header(skb);
983 memcpy(hdr_data + len, hdr, hdr_len[2]);
984 len += hdr_len[2];
985 }
986 return len;
987}
988
989/**
990 * create_hdr_descs - create header and header extension descriptors
991 * @hdr_field - bitfield determining needed headers
992 * @data - buffer containing header data
993 * @len - length of data buffer
994 * @hdr_len - array of individual header lengths
995 * @scrq_arr - descriptor array
996 *
997 * Creates header and, if needed, header extension descriptors and
998 * places them in a descriptor array, scrq_arr
999 */
1000
1001static void create_hdr_descs(u8 hdr_field, u8 *hdr_data, int len, int *hdr_len,
1002 union sub_crq *scrq_arr)
1003{
1004 union sub_crq hdr_desc;
1005 int tmp_len = len;
1006 u8 *data, *cur;
1007 int tmp;
1008
1009 while (tmp_len > 0) {
1010 cur = hdr_data + len - tmp_len;
1011
1012 memset(&hdr_desc, 0, sizeof(hdr_desc));
1013 if (cur != hdr_data) {
1014 data = hdr_desc.hdr_ext.data;
1015 tmp = tmp_len > 29 ? 29 : tmp_len;
1016 hdr_desc.hdr_ext.first = IBMVNIC_CRQ_CMD;
1017 hdr_desc.hdr_ext.type = IBMVNIC_HDR_EXT_DESC;
1018 hdr_desc.hdr_ext.len = tmp;
1019 } else {
1020 data = hdr_desc.hdr.data;
1021 tmp = tmp_len > 24 ? 24 : tmp_len;
1022 hdr_desc.hdr.first = IBMVNIC_CRQ_CMD;
1023 hdr_desc.hdr.type = IBMVNIC_HDR_DESC;
1024 hdr_desc.hdr.len = tmp;
1025 hdr_desc.hdr.l2_len = (u8)hdr_len[0];
1026 hdr_desc.hdr.l3_len = cpu_to_be16((u16)hdr_len[1]);
1027 hdr_desc.hdr.l4_len = (u8)hdr_len[2];
1028 hdr_desc.hdr.flag = hdr_field << 1;
1029 }
1030 memcpy(data, cur, tmp);
1031 tmp_len -= tmp;
1032 *scrq_arr = hdr_desc;
1033 scrq_arr++;
1034 }
1035}
1036
1037/**
1038 * build_hdr_descs_arr - build a header descriptor array
1039 * @skb - socket buffer
1040 * @num_entries - number of descriptors to be sent
1041 * @subcrq - first TX descriptor
1042 * @hdr_field - bit field determining which headers will be sent
1043 *
1044 * This function will build a TX descriptor array with applicable
1045 * L2/L3/L4 packet header descriptors to be sent by send_subcrq_indirect.
1046 */
1047
1048static void build_hdr_descs_arr(struct ibmvnic_tx_buff *txbuff,
1049 int *num_entries, u8 hdr_field)
1050{
1051 int hdr_len[3] = {0, 0, 0};
1052 int tot_len, len;
1053 u8 *hdr_data = txbuff->hdr_data;
1054
1055 tot_len = build_hdr_data(hdr_field, txbuff->skb, hdr_len,
1056 txbuff->hdr_data);
1057 len = tot_len;
1058 len -= 24;
1059 if (len > 0)
1060 num_entries += len % 29 ? len / 29 + 1 : len / 29;
1061 create_hdr_descs(hdr_field, hdr_data, tot_len, hdr_len,
1062 txbuff->indir_arr + 1);
1063}
1064
Thomas Falcon032c5e82015-12-21 11:26:06 -06001065static int ibmvnic_xmit(struct sk_buff *skb, struct net_device *netdev)
1066{
1067 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1068 int queue_num = skb_get_queue_mapping(skb);
Thomas Falconad7775d2016-04-01 17:20:34 -05001069 u8 *hdrs = (u8 *)&adapter->tx_rx_desc_req;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001070 struct device *dev = &adapter->vdev->dev;
1071 struct ibmvnic_tx_buff *tx_buff = NULL;
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001072 struct ibmvnic_sub_crq_queue *tx_scrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001073 struct ibmvnic_tx_pool *tx_pool;
1074 unsigned int tx_send_failed = 0;
1075 unsigned int tx_map_failed = 0;
1076 unsigned int tx_dropped = 0;
1077 unsigned int tx_packets = 0;
1078 unsigned int tx_bytes = 0;
1079 dma_addr_t data_dma_addr;
1080 struct netdev_queue *txq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001081 unsigned long lpar_rc;
1082 union sub_crq tx_crq;
1083 unsigned int offset;
Thomas Falconad7775d2016-04-01 17:20:34 -05001084 int num_entries = 1;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001085 unsigned char *dst;
1086 u64 *handle_array;
1087 int index = 0;
1088 int ret = 0;
1089
Nathan Fontenoted651a12017-05-03 14:04:38 -04001090 if (adapter->resetting) {
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001091 if (!netif_subqueue_stopped(netdev, skb))
1092 netif_stop_subqueue(netdev, queue_num);
1093 dev_kfree_skb_any(skb);
1094
Thomas Falcon032c5e82015-12-21 11:26:06 -06001095 tx_send_failed++;
1096 tx_dropped++;
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001097 ret = NETDEV_TX_OK;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001098 goto out;
1099 }
1100
Nathan Fontenot161b8a82017-05-03 14:05:08 -04001101 tx_pool = &adapter->tx_pool[queue_num];
1102 tx_scrq = adapter->tx_scrq[queue_num];
1103 txq = netdev_get_tx_queue(netdev, skb_get_queue_mapping(skb));
1104 handle_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
1105 be32_to_cpu(adapter->login_rsp_buf->off_txsubm_subcrqs));
1106
Thomas Falcon032c5e82015-12-21 11:26:06 -06001107 index = tx_pool->free_map[tx_pool->consumer_index];
1108 offset = index * adapter->req_mtu;
1109 dst = tx_pool->long_term_buff.buff + offset;
1110 memset(dst, 0, adapter->req_mtu);
1111 skb_copy_from_linear_data(skb, dst, skb->len);
1112 data_dma_addr = tx_pool->long_term_buff.addr + offset;
1113
1114 tx_pool->consumer_index =
1115 (tx_pool->consumer_index + 1) %
Thomas Falcon068d9f92017-03-05 12:18:42 -06001116 adapter->req_tx_entries_per_subcrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001117
1118 tx_buff = &tx_pool->tx_buff[index];
1119 tx_buff->skb = skb;
1120 tx_buff->data_dma[0] = data_dma_addr;
1121 tx_buff->data_len[0] = skb->len;
1122 tx_buff->index = index;
1123 tx_buff->pool_index = queue_num;
1124 tx_buff->last_frag = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001125
1126 memset(&tx_crq, 0, sizeof(tx_crq));
1127 tx_crq.v1.first = IBMVNIC_CRQ_CMD;
1128 tx_crq.v1.type = IBMVNIC_TX_DESC;
1129 tx_crq.v1.n_crq_elem = 1;
1130 tx_crq.v1.n_sge = 1;
1131 tx_crq.v1.flags1 = IBMVNIC_TX_COMP_NEEDED;
1132 tx_crq.v1.correlator = cpu_to_be32(index);
1133 tx_crq.v1.dma_reg = cpu_to_be16(tx_pool->long_term_buff.map_id);
1134 tx_crq.v1.sge_len = cpu_to_be32(skb->len);
1135 tx_crq.v1.ioba = cpu_to_be64(data_dma_addr);
1136
1137 if (adapter->vlan_header_insertion) {
1138 tx_crq.v1.flags2 |= IBMVNIC_TX_VLAN_INSERT;
1139 tx_crq.v1.vlan_id = cpu_to_be16(skb->vlan_tci);
1140 }
1141
1142 if (skb->protocol == htons(ETH_P_IP)) {
1143 if (ip_hdr(skb)->version == 4)
1144 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV4;
1145 else if (ip_hdr(skb)->version == 6)
1146 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV6;
1147
1148 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
1149 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_TCP;
1150 else if (ip_hdr(skb)->protocol != IPPROTO_TCP)
1151 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_UDP;
1152 }
1153
Thomas Falconad7775d2016-04-01 17:20:34 -05001154 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06001155 tx_crq.v1.flags1 |= IBMVNIC_TX_CHKSUM_OFFLOAD;
Thomas Falconad7775d2016-04-01 17:20:34 -05001156 hdrs += 2;
1157 }
1158 /* determine if l2/3/4 headers are sent to firmware */
1159 if ((*hdrs >> 7) & 1 &&
1160 (skb->protocol == htons(ETH_P_IP) ||
1161 skb->protocol == htons(ETH_P_IPV6))) {
1162 build_hdr_descs_arr(tx_buff, &num_entries, *hdrs);
1163 tx_crq.v1.n_crq_elem = num_entries;
1164 tx_buff->indir_arr[0] = tx_crq;
1165 tx_buff->indir_dma = dma_map_single(dev, tx_buff->indir_arr,
1166 sizeof(tx_buff->indir_arr),
1167 DMA_TO_DEVICE);
1168 if (dma_mapping_error(dev, tx_buff->indir_dma)) {
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001169 dev_kfree_skb_any(skb);
1170 tx_buff->skb = NULL;
Thomas Falconad7775d2016-04-01 17:20:34 -05001171 if (!firmware_has_feature(FW_FEATURE_CMO))
1172 dev_err(dev, "tx: unable to map descriptor array\n");
1173 tx_map_failed++;
1174 tx_dropped++;
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001175 ret = NETDEV_TX_OK;
Thomas Falconad7775d2016-04-01 17:20:34 -05001176 goto out;
1177 }
John Allen498cd8e2016-04-06 11:49:55 -05001178 lpar_rc = send_subcrq_indirect(adapter, handle_array[queue_num],
Thomas Falconad7775d2016-04-01 17:20:34 -05001179 (u64)tx_buff->indir_dma,
1180 (u64)num_entries);
1181 } else {
John Allen498cd8e2016-04-06 11:49:55 -05001182 lpar_rc = send_subcrq(adapter, handle_array[queue_num],
1183 &tx_crq);
Thomas Falconad7775d2016-04-01 17:20:34 -05001184 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001185 if (lpar_rc != H_SUCCESS) {
1186 dev_err(dev, "tx failed with code %ld\n", lpar_rc);
1187
1188 if (tx_pool->consumer_index == 0)
1189 tx_pool->consumer_index =
Thomas Falcon068d9f92017-03-05 12:18:42 -06001190 adapter->req_tx_entries_per_subcrq - 1;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001191 else
1192 tx_pool->consumer_index--;
1193
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001194 dev_kfree_skb_any(skb);
1195 tx_buff->skb = NULL;
1196
Thomas Falconb8c80b82017-05-26 10:30:42 -04001197 if (lpar_rc == H_CLOSED) {
1198 /* Disable TX and report carrier off if queue is closed.
1199 * Firmware guarantees that a signal will be sent to the
1200 * driver, triggering a reset or some other action.
1201 */
1202 netif_tx_stop_all_queues(netdev);
1203 netif_carrier_off(netdev);
1204 }
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001205
Thomas Falcon032c5e82015-12-21 11:26:06 -06001206 tx_send_failed++;
1207 tx_dropped++;
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001208 ret = NETDEV_TX_OK;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001209 goto out;
1210 }
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001211
Brian King58c8c0c2017-04-19 13:44:47 -04001212 if (atomic_inc_return(&tx_scrq->used)
1213 >= adapter->req_tx_entries_per_subcrq) {
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001214 netdev_info(netdev, "Stopping queue %d\n", queue_num);
1215 netif_stop_subqueue(netdev, queue_num);
1216 }
1217
Thomas Falcon032c5e82015-12-21 11:26:06 -06001218 tx_packets++;
1219 tx_bytes += skb->len;
1220 txq->trans_start = jiffies;
1221 ret = NETDEV_TX_OK;
1222
1223out:
1224 netdev->stats.tx_dropped += tx_dropped;
1225 netdev->stats.tx_bytes += tx_bytes;
1226 netdev->stats.tx_packets += tx_packets;
1227 adapter->tx_send_failed += tx_send_failed;
1228 adapter->tx_map_failed += tx_map_failed;
1229
1230 return ret;
1231}
1232
1233static void ibmvnic_set_multi(struct net_device *netdev)
1234{
1235 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1236 struct netdev_hw_addr *ha;
1237 union ibmvnic_crq crq;
1238
1239 memset(&crq, 0, sizeof(crq));
1240 crq.request_capability.first = IBMVNIC_CRQ_CMD;
1241 crq.request_capability.cmd = REQUEST_CAPABILITY;
1242
1243 if (netdev->flags & IFF_PROMISC) {
1244 if (!adapter->promisc_supported)
1245 return;
1246 } else {
1247 if (netdev->flags & IFF_ALLMULTI) {
1248 /* Accept all multicast */
1249 memset(&crq, 0, sizeof(crq));
1250 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1251 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1252 crq.multicast_ctrl.flags = IBMVNIC_ENABLE_ALL;
1253 ibmvnic_send_crq(adapter, &crq);
1254 } else if (netdev_mc_empty(netdev)) {
1255 /* Reject all multicast */
1256 memset(&crq, 0, sizeof(crq));
1257 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1258 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1259 crq.multicast_ctrl.flags = IBMVNIC_DISABLE_ALL;
1260 ibmvnic_send_crq(adapter, &crq);
1261 } else {
1262 /* Accept one or more multicast(s) */
1263 netdev_for_each_mc_addr(ha, netdev) {
1264 memset(&crq, 0, sizeof(crq));
1265 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1266 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1267 crq.multicast_ctrl.flags = IBMVNIC_ENABLE_MC;
1268 ether_addr_copy(&crq.multicast_ctrl.mac_addr[0],
1269 ha->addr);
1270 ibmvnic_send_crq(adapter, &crq);
1271 }
1272 }
1273 }
1274}
1275
1276static int ibmvnic_set_mac(struct net_device *netdev, void *p)
1277{
1278 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1279 struct sockaddr *addr = p;
1280 union ibmvnic_crq crq;
1281
1282 if (!is_valid_ether_addr(addr->sa_data))
1283 return -EADDRNOTAVAIL;
1284
1285 memset(&crq, 0, sizeof(crq));
1286 crq.change_mac_addr.first = IBMVNIC_CRQ_CMD;
1287 crq.change_mac_addr.cmd = CHANGE_MAC_ADDR;
1288 ether_addr_copy(&crq.change_mac_addr.mac_addr[0], addr->sa_data);
1289 ibmvnic_send_crq(adapter, &crq);
1290 /* netdev->dev_addr is changed in handle_change_mac_rsp function */
1291 return 0;
1292}
1293
Nathan Fontenoted651a12017-05-03 14:04:38 -04001294/**
1295 * do_reset returns zero if we are able to keep processing reset events, or
1296 * non-zero if we hit a fatal error and must halt.
1297 */
1298static int do_reset(struct ibmvnic_adapter *adapter,
1299 struct ibmvnic_rwi *rwi, u32 reset_state)
1300{
1301 struct net_device *netdev = adapter->netdev;
1302 int i, rc;
1303
1304 netif_carrier_off(netdev);
1305 adapter->reset_reason = rwi->reset_reason;
1306
1307 if (rwi->reset_reason == VNIC_RESET_MOBILITY) {
1308 rc = ibmvnic_reenable_crq_queue(adapter);
1309 if (rc)
1310 return 0;
1311 }
1312
1313 rc = __ibmvnic_close(netdev);
1314 if (rc)
1315 return rc;
1316
John Allen8cb31cf2017-05-26 10:30:37 -04001317 if (adapter->reset_reason != VNIC_RESET_NON_FATAL) {
1318 /* remove the closed state so when we call open it appears
1319 * we are coming from the probed state.
1320 */
Nathan Fontenoted651a12017-05-03 14:04:38 -04001321 adapter->state = VNIC_PROBED;
John Allen8cb31cf2017-05-26 10:30:37 -04001322
John Allen8cb31cf2017-05-26 10:30:37 -04001323 release_sub_crqs(adapter);
John Allen8cb31cf2017-05-26 10:30:37 -04001324
1325 rc = ibmvnic_init(adapter);
1326 if (rc)
1327 return 0;
1328
1329 /* If the adapter was in PROBE state prior to the reset,
1330 * exit here.
1331 */
1332 if (reset_state == VNIC_PROBED)
1333 return 0;
1334
1335 rc = ibmvnic_login(netdev);
1336 if (rc) {
1337 adapter->state = VNIC_PROBED;
1338 return 0;
1339 }
1340
Nathan Fontenot8c0543a2017-05-26 10:31:06 -04001341 rc = reset_tx_pools(adapter);
1342 if (rc)
1343 return rc;
1344
1345 rc = reset_rx_pools(adapter);
John Allen8cb31cf2017-05-26 10:30:37 -04001346 if (rc)
1347 return rc;
1348
1349 if (reset_state == VNIC_CLOSED)
1350 return 0;
Nathan Fontenoted651a12017-05-03 14:04:38 -04001351 }
1352
Nathan Fontenoted651a12017-05-03 14:04:38 -04001353 rc = __ibmvnic_open(netdev);
1354 if (rc) {
1355 if (list_empty(&adapter->rwi_list))
1356 adapter->state = VNIC_CLOSED;
1357 else
1358 adapter->state = reset_state;
1359
1360 return 0;
1361 }
1362
1363 netif_carrier_on(netdev);
1364
1365 /* kick napi */
1366 for (i = 0; i < adapter->req_rx_queues; i++)
1367 napi_schedule(&adapter->napi[i]);
1368
John Allen2ce9e4e2017-05-26 10:30:25 -04001369 netdev_notify_peers(netdev);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001370 return 0;
1371}
1372
1373static struct ibmvnic_rwi *get_next_rwi(struct ibmvnic_adapter *adapter)
1374{
1375 struct ibmvnic_rwi *rwi;
1376
1377 mutex_lock(&adapter->rwi_lock);
1378
1379 if (!list_empty(&adapter->rwi_list)) {
1380 rwi = list_first_entry(&adapter->rwi_list, struct ibmvnic_rwi,
1381 list);
1382 list_del(&rwi->list);
1383 } else {
1384 rwi = NULL;
1385 }
1386
1387 mutex_unlock(&adapter->rwi_lock);
1388 return rwi;
1389}
1390
1391static void free_all_rwi(struct ibmvnic_adapter *adapter)
1392{
1393 struct ibmvnic_rwi *rwi;
1394
1395 rwi = get_next_rwi(adapter);
1396 while (rwi) {
1397 kfree(rwi);
1398 rwi = get_next_rwi(adapter);
1399 }
1400}
1401
1402static void __ibmvnic_reset(struct work_struct *work)
1403{
1404 struct ibmvnic_rwi *rwi;
1405 struct ibmvnic_adapter *adapter;
1406 struct net_device *netdev;
1407 u32 reset_state;
1408 int rc;
1409
1410 adapter = container_of(work, struct ibmvnic_adapter, ibmvnic_reset);
1411 netdev = adapter->netdev;
1412
1413 mutex_lock(&adapter->reset_lock);
1414 adapter->resetting = true;
1415 reset_state = adapter->state;
1416
1417 rwi = get_next_rwi(adapter);
1418 while (rwi) {
1419 rc = do_reset(adapter, rwi, reset_state);
1420 kfree(rwi);
1421 if (rc)
1422 break;
1423
1424 rwi = get_next_rwi(adapter);
1425 }
1426
1427 if (rc) {
1428 free_all_rwi(adapter);
Wei Yongjun6d0af072017-05-18 15:24:52 +00001429 mutex_unlock(&adapter->reset_lock);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001430 return;
1431 }
1432
1433 adapter->resetting = false;
1434 mutex_unlock(&adapter->reset_lock);
1435}
1436
1437static void ibmvnic_reset(struct ibmvnic_adapter *adapter,
1438 enum ibmvnic_reset_reason reason)
1439{
1440 struct ibmvnic_rwi *rwi, *tmp;
1441 struct net_device *netdev = adapter->netdev;
1442 struct list_head *entry;
1443
1444 if (adapter->state == VNIC_REMOVING ||
1445 adapter->state == VNIC_REMOVED) {
1446 netdev_dbg(netdev, "Adapter removing, skipping reset\n");
1447 return;
1448 }
1449
1450 mutex_lock(&adapter->rwi_lock);
1451
1452 list_for_each(entry, &adapter->rwi_list) {
1453 tmp = list_entry(entry, struct ibmvnic_rwi, list);
1454 if (tmp->reset_reason == reason) {
1455 netdev_err(netdev, "Matching reset found, skipping\n");
1456 mutex_unlock(&adapter->rwi_lock);
1457 return;
1458 }
1459 }
1460
1461 rwi = kzalloc(sizeof(*rwi), GFP_KERNEL);
1462 if (!rwi) {
1463 mutex_unlock(&adapter->rwi_lock);
1464 ibmvnic_close(netdev);
1465 return;
1466 }
1467
1468 rwi->reset_reason = reason;
1469 list_add_tail(&rwi->list, &adapter->rwi_list);
1470 mutex_unlock(&adapter->rwi_lock);
1471 schedule_work(&adapter->ibmvnic_reset);
1472}
1473
Thomas Falcon032c5e82015-12-21 11:26:06 -06001474static void ibmvnic_tx_timeout(struct net_device *dev)
1475{
1476 struct ibmvnic_adapter *adapter = netdev_priv(dev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001477
Nathan Fontenoted651a12017-05-03 14:04:38 -04001478 ibmvnic_reset(adapter, VNIC_RESET_TIMEOUT);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001479}
1480
1481static void remove_buff_from_pool(struct ibmvnic_adapter *adapter,
1482 struct ibmvnic_rx_buff *rx_buff)
1483{
1484 struct ibmvnic_rx_pool *pool = &adapter->rx_pool[rx_buff->pool_index];
1485
1486 rx_buff->skb = NULL;
1487
1488 pool->free_map[pool->next_alloc] = (int)(rx_buff - pool->rx_buff);
1489 pool->next_alloc = (pool->next_alloc + 1) % pool->size;
1490
1491 atomic_dec(&pool->available);
1492}
1493
1494static int ibmvnic_poll(struct napi_struct *napi, int budget)
1495{
1496 struct net_device *netdev = napi->dev;
1497 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1498 int scrq_num = (int)(napi - adapter->napi);
1499 int frames_processed = 0;
Nathan Fontenot152ce472017-05-26 10:30:54 -04001500
1501 if (adapter->resetting)
1502 return 0;
1503
Thomas Falcon032c5e82015-12-21 11:26:06 -06001504restart_poll:
1505 while (frames_processed < budget) {
1506 struct sk_buff *skb;
1507 struct ibmvnic_rx_buff *rx_buff;
1508 union sub_crq *next;
1509 u32 length;
1510 u16 offset;
1511 u8 flags = 0;
1512
1513 if (!pending_scrq(adapter, adapter->rx_scrq[scrq_num]))
1514 break;
1515 next = ibmvnic_next_scrq(adapter, adapter->rx_scrq[scrq_num]);
1516 rx_buff =
1517 (struct ibmvnic_rx_buff *)be64_to_cpu(next->
1518 rx_comp.correlator);
1519 /* do error checking */
1520 if (next->rx_comp.rc) {
1521 netdev_err(netdev, "rx error %x\n", next->rx_comp.rc);
1522 /* free the entry */
1523 next->rx_comp.first = 0;
1524 remove_buff_from_pool(adapter, rx_buff);
Nathan Fontenotca05e312017-05-03 14:05:14 -04001525 continue;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001526 }
1527
1528 length = be32_to_cpu(next->rx_comp.len);
1529 offset = be16_to_cpu(next->rx_comp.off_frame_data);
1530 flags = next->rx_comp.flags;
1531 skb = rx_buff->skb;
1532 skb_copy_to_linear_data(skb, rx_buff->data + offset,
1533 length);
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04001534
1535 /* VLAN Header has been stripped by the system firmware and
1536 * needs to be inserted by the driver
1537 */
1538 if (adapter->rx_vlan_header_insertion &&
1539 (flags & IBMVNIC_VLAN_STRIPPED))
1540 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
1541 ntohs(next->rx_comp.vlan_tci));
1542
Thomas Falcon032c5e82015-12-21 11:26:06 -06001543 /* free the entry */
1544 next->rx_comp.first = 0;
1545 remove_buff_from_pool(adapter, rx_buff);
1546
1547 skb_put(skb, length);
1548 skb->protocol = eth_type_trans(skb, netdev);
Thomas Falcon94ca3052017-05-03 14:05:20 -04001549 skb_record_rx_queue(skb, scrq_num);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001550
1551 if (flags & IBMVNIC_IP_CHKSUM_GOOD &&
1552 flags & IBMVNIC_TCP_UDP_CHKSUM_GOOD) {
1553 skb->ip_summed = CHECKSUM_UNNECESSARY;
1554 }
1555
1556 length = skb->len;
1557 napi_gro_receive(napi, skb); /* send it up */
1558 netdev->stats.rx_packets++;
1559 netdev->stats.rx_bytes += length;
1560 frames_processed++;
1561 }
Nathan Fontenot152ce472017-05-26 10:30:54 -04001562
1563 if (adapter->state != VNIC_CLOSING)
1564 replenish_rx_pool(adapter, &adapter->rx_pool[scrq_num]);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001565
1566 if (frames_processed < budget) {
1567 enable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
Eric Dumazet6ad20162017-01-30 08:22:01 -08001568 napi_complete_done(napi, frames_processed);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001569 if (pending_scrq(adapter, adapter->rx_scrq[scrq_num]) &&
1570 napi_reschedule(napi)) {
1571 disable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
1572 goto restart_poll;
1573 }
1574 }
1575 return frames_processed;
1576}
1577
1578#ifdef CONFIG_NET_POLL_CONTROLLER
1579static void ibmvnic_netpoll_controller(struct net_device *dev)
1580{
1581 struct ibmvnic_adapter *adapter = netdev_priv(dev);
1582 int i;
1583
1584 replenish_pools(netdev_priv(dev));
1585 for (i = 0; i < adapter->req_rx_queues; i++)
1586 ibmvnic_interrupt_rx(adapter->rx_scrq[i]->irq,
1587 adapter->rx_scrq[i]);
1588}
1589#endif
1590
1591static const struct net_device_ops ibmvnic_netdev_ops = {
1592 .ndo_open = ibmvnic_open,
1593 .ndo_stop = ibmvnic_close,
1594 .ndo_start_xmit = ibmvnic_xmit,
1595 .ndo_set_rx_mode = ibmvnic_set_multi,
1596 .ndo_set_mac_address = ibmvnic_set_mac,
1597 .ndo_validate_addr = eth_validate_addr,
Thomas Falcon032c5e82015-12-21 11:26:06 -06001598 .ndo_tx_timeout = ibmvnic_tx_timeout,
1599#ifdef CONFIG_NET_POLL_CONTROLLER
1600 .ndo_poll_controller = ibmvnic_netpoll_controller,
1601#endif
1602};
1603
1604/* ethtool functions */
1605
Philippe Reynes8a433792017-01-07 22:37:29 +01001606static int ibmvnic_get_link_ksettings(struct net_device *netdev,
1607 struct ethtool_link_ksettings *cmd)
Thomas Falcon032c5e82015-12-21 11:26:06 -06001608{
Philippe Reynes8a433792017-01-07 22:37:29 +01001609 u32 supported, advertising;
1610
1611 supported = (SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg |
Thomas Falcon032c5e82015-12-21 11:26:06 -06001612 SUPPORTED_FIBRE);
Philippe Reynes8a433792017-01-07 22:37:29 +01001613 advertising = (ADVERTISED_1000baseT_Full | ADVERTISED_Autoneg |
Thomas Falcon032c5e82015-12-21 11:26:06 -06001614 ADVERTISED_FIBRE);
Philippe Reynes8a433792017-01-07 22:37:29 +01001615 cmd->base.speed = SPEED_1000;
1616 cmd->base.duplex = DUPLEX_FULL;
1617 cmd->base.port = PORT_FIBRE;
1618 cmd->base.phy_address = 0;
1619 cmd->base.autoneg = AUTONEG_ENABLE;
1620
1621 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
1622 supported);
1623 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
1624 advertising);
1625
Thomas Falcon032c5e82015-12-21 11:26:06 -06001626 return 0;
1627}
1628
1629static void ibmvnic_get_drvinfo(struct net_device *dev,
1630 struct ethtool_drvinfo *info)
1631{
1632 strlcpy(info->driver, ibmvnic_driver_name, sizeof(info->driver));
1633 strlcpy(info->version, IBMVNIC_DRIVER_VERSION, sizeof(info->version));
1634}
1635
1636static u32 ibmvnic_get_msglevel(struct net_device *netdev)
1637{
1638 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1639
1640 return adapter->msg_enable;
1641}
1642
1643static void ibmvnic_set_msglevel(struct net_device *netdev, u32 data)
1644{
1645 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1646
1647 adapter->msg_enable = data;
1648}
1649
1650static u32 ibmvnic_get_link(struct net_device *netdev)
1651{
1652 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1653
1654 /* Don't need to send a query because we request a logical link up at
1655 * init and then we wait for link state indications
1656 */
1657 return adapter->logical_link_state;
1658}
1659
1660static void ibmvnic_get_ringparam(struct net_device *netdev,
1661 struct ethtool_ringparam *ring)
1662{
1663 ring->rx_max_pending = 0;
1664 ring->tx_max_pending = 0;
1665 ring->rx_mini_max_pending = 0;
1666 ring->rx_jumbo_max_pending = 0;
1667 ring->rx_pending = 0;
1668 ring->tx_pending = 0;
1669 ring->rx_mini_pending = 0;
1670 ring->rx_jumbo_pending = 0;
1671}
1672
1673static void ibmvnic_get_strings(struct net_device *dev, u32 stringset, u8 *data)
1674{
1675 int i;
1676
1677 if (stringset != ETH_SS_STATS)
1678 return;
1679
1680 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++, data += ETH_GSTRING_LEN)
1681 memcpy(data, ibmvnic_stats[i].name, ETH_GSTRING_LEN);
1682}
1683
1684static int ibmvnic_get_sset_count(struct net_device *dev, int sset)
1685{
1686 switch (sset) {
1687 case ETH_SS_STATS:
1688 return ARRAY_SIZE(ibmvnic_stats);
1689 default:
1690 return -EOPNOTSUPP;
1691 }
1692}
1693
1694static void ibmvnic_get_ethtool_stats(struct net_device *dev,
1695 struct ethtool_stats *stats, u64 *data)
1696{
1697 struct ibmvnic_adapter *adapter = netdev_priv(dev);
1698 union ibmvnic_crq crq;
1699 int i;
1700
1701 memset(&crq, 0, sizeof(crq));
1702 crq.request_statistics.first = IBMVNIC_CRQ_CMD;
1703 crq.request_statistics.cmd = REQUEST_STATISTICS;
1704 crq.request_statistics.ioba = cpu_to_be32(adapter->stats_token);
1705 crq.request_statistics.len =
1706 cpu_to_be32(sizeof(struct ibmvnic_statistics));
Thomas Falcon032c5e82015-12-21 11:26:06 -06001707
1708 /* Wait for data to be written */
1709 init_completion(&adapter->stats_done);
Nathan Fontenotdb5d0b52017-02-10 13:45:05 -05001710 ibmvnic_send_crq(adapter, &crq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001711 wait_for_completion(&adapter->stats_done);
1712
1713 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++)
1714 data[i] = IBMVNIC_GET_STAT(adapter, ibmvnic_stats[i].offset);
1715}
1716
1717static const struct ethtool_ops ibmvnic_ethtool_ops = {
Thomas Falcon032c5e82015-12-21 11:26:06 -06001718 .get_drvinfo = ibmvnic_get_drvinfo,
1719 .get_msglevel = ibmvnic_get_msglevel,
1720 .set_msglevel = ibmvnic_set_msglevel,
1721 .get_link = ibmvnic_get_link,
1722 .get_ringparam = ibmvnic_get_ringparam,
1723 .get_strings = ibmvnic_get_strings,
1724 .get_sset_count = ibmvnic_get_sset_count,
1725 .get_ethtool_stats = ibmvnic_get_ethtool_stats,
Philippe Reynes8a433792017-01-07 22:37:29 +01001726 .get_link_ksettings = ibmvnic_get_link_ksettings,
Thomas Falcon032c5e82015-12-21 11:26:06 -06001727};
1728
1729/* Routines for managing CRQs/sCRQs */
1730
1731static void release_sub_crq_queue(struct ibmvnic_adapter *adapter,
1732 struct ibmvnic_sub_crq_queue *scrq)
1733{
1734 struct device *dev = &adapter->vdev->dev;
1735 long rc;
1736
1737 netdev_dbg(adapter->netdev, "Releasing sub-CRQ\n");
1738
1739 /* Close the sub-crqs */
1740 do {
1741 rc = plpar_hcall_norets(H_FREE_SUB_CRQ,
1742 adapter->vdev->unit_address,
1743 scrq->crq_num);
1744 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
1745
Thomas Falconffa73852017-04-19 13:44:29 -04001746 if (rc) {
1747 netdev_err(adapter->netdev,
1748 "Failed to release sub-CRQ %16lx, rc = %ld\n",
1749 scrq->crq_num, rc);
1750 }
1751
Thomas Falcon032c5e82015-12-21 11:26:06 -06001752 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
1753 DMA_BIDIRECTIONAL);
1754 free_pages((unsigned long)scrq->msgs, 2);
1755 kfree(scrq);
1756}
1757
1758static struct ibmvnic_sub_crq_queue *init_sub_crq_queue(struct ibmvnic_adapter
1759 *adapter)
1760{
1761 struct device *dev = &adapter->vdev->dev;
1762 struct ibmvnic_sub_crq_queue *scrq;
1763 int rc;
1764
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04001765 scrq = kzalloc(sizeof(*scrq), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001766 if (!scrq)
1767 return NULL;
1768
Nathan Fontenot7f7adc52017-04-19 13:45:16 -04001769 scrq->msgs =
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04001770 (union sub_crq *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, 2);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001771 if (!scrq->msgs) {
1772 dev_warn(dev, "Couldn't allocate crq queue messages page\n");
1773 goto zero_page_failed;
1774 }
1775
1776 scrq->msg_token = dma_map_single(dev, scrq->msgs, 4 * PAGE_SIZE,
1777 DMA_BIDIRECTIONAL);
1778 if (dma_mapping_error(dev, scrq->msg_token)) {
1779 dev_warn(dev, "Couldn't map crq queue messages page\n");
1780 goto map_failed;
1781 }
1782
1783 rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
1784 4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
1785
1786 if (rc == H_RESOURCE)
1787 rc = ibmvnic_reset_crq(adapter);
1788
1789 if (rc == H_CLOSED) {
1790 dev_warn(dev, "Partner adapter not ready, waiting.\n");
1791 } else if (rc) {
1792 dev_warn(dev, "Error %d registering sub-crq\n", rc);
1793 goto reg_failed;
1794 }
1795
Thomas Falcon032c5e82015-12-21 11:26:06 -06001796 scrq->adapter = adapter;
1797 scrq->size = 4 * PAGE_SIZE / sizeof(*scrq->msgs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001798 spin_lock_init(&scrq->lock);
1799
1800 netdev_dbg(adapter->netdev,
1801 "sub-crq initialized, num %lx, hw_irq=%lx, irq=%x\n",
1802 scrq->crq_num, scrq->hw_irq, scrq->irq);
1803
1804 return scrq;
1805
Thomas Falcon032c5e82015-12-21 11:26:06 -06001806reg_failed:
1807 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
1808 DMA_BIDIRECTIONAL);
1809map_failed:
1810 free_pages((unsigned long)scrq->msgs, 2);
1811zero_page_failed:
1812 kfree(scrq);
1813
1814 return NULL;
1815}
1816
1817static void release_sub_crqs(struct ibmvnic_adapter *adapter)
1818{
1819 int i;
1820
1821 if (adapter->tx_scrq) {
Nathan Fontenotb5108882017-03-30 02:49:18 -04001822 for (i = 0; i < adapter->req_tx_queues; i++) {
1823 if (!adapter->tx_scrq[i])
1824 continue;
1825
1826 if (adapter->tx_scrq[i]->irq) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06001827 free_irq(adapter->tx_scrq[i]->irq,
1828 adapter->tx_scrq[i]);
Thomas Falcon88eb98a2016-07-06 15:35:16 -05001829 irq_dispose_mapping(adapter->tx_scrq[i]->irq);
Nathan Fontenotb5108882017-03-30 02:49:18 -04001830 adapter->tx_scrq[i]->irq = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001831 }
Nathan Fontenotb5108882017-03-30 02:49:18 -04001832
1833 release_sub_crq_queue(adapter, adapter->tx_scrq[i]);
1834 }
1835
Nathan Fontenot9501df32017-03-15 23:38:07 -04001836 kfree(adapter->tx_scrq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001837 adapter->tx_scrq = NULL;
1838 }
1839
1840 if (adapter->rx_scrq) {
Nathan Fontenotb5108882017-03-30 02:49:18 -04001841 for (i = 0; i < adapter->req_rx_queues; i++) {
1842 if (!adapter->rx_scrq[i])
1843 continue;
1844
1845 if (adapter->rx_scrq[i]->irq) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06001846 free_irq(adapter->rx_scrq[i]->irq,
1847 adapter->rx_scrq[i]);
Thomas Falcon88eb98a2016-07-06 15:35:16 -05001848 irq_dispose_mapping(adapter->rx_scrq[i]->irq);
Nathan Fontenotb5108882017-03-30 02:49:18 -04001849 adapter->rx_scrq[i]->irq = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001850 }
Nathan Fontenotb5108882017-03-30 02:49:18 -04001851
1852 release_sub_crq_queue(adapter, adapter->rx_scrq[i]);
1853 }
1854
Nathan Fontenot9501df32017-03-15 23:38:07 -04001855 kfree(adapter->rx_scrq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001856 adapter->rx_scrq = NULL;
1857 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001858}
1859
1860static int disable_scrq_irq(struct ibmvnic_adapter *adapter,
1861 struct ibmvnic_sub_crq_queue *scrq)
1862{
1863 struct device *dev = &adapter->vdev->dev;
1864 unsigned long rc;
1865
1866 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
1867 H_DISABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
1868 if (rc)
1869 dev_err(dev, "Couldn't disable scrq irq 0x%lx. rc=%ld\n",
1870 scrq->hw_irq, rc);
1871 return rc;
1872}
1873
1874static int enable_scrq_irq(struct ibmvnic_adapter *adapter,
1875 struct ibmvnic_sub_crq_queue *scrq)
1876{
1877 struct device *dev = &adapter->vdev->dev;
1878 unsigned long rc;
1879
1880 if (scrq->hw_irq > 0x100000000ULL) {
1881 dev_err(dev, "bad hw_irq = %lx\n", scrq->hw_irq);
1882 return 1;
1883 }
1884
1885 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
1886 H_ENABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
1887 if (rc)
1888 dev_err(dev, "Couldn't enable scrq irq 0x%lx. rc=%ld\n",
1889 scrq->hw_irq, rc);
1890 return rc;
1891}
1892
1893static int ibmvnic_complete_tx(struct ibmvnic_adapter *adapter,
1894 struct ibmvnic_sub_crq_queue *scrq)
1895{
1896 struct device *dev = &adapter->vdev->dev;
1897 struct ibmvnic_tx_buff *txbuff;
1898 union sub_crq *next;
1899 int index;
1900 int i, j;
Thomas Falconad7775d2016-04-01 17:20:34 -05001901 u8 first;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001902
1903restart_loop:
1904 while (pending_scrq(adapter, scrq)) {
1905 unsigned int pool = scrq->pool_index;
1906
1907 next = ibmvnic_next_scrq(adapter, scrq);
1908 for (i = 0; i < next->tx_comp.num_comps; i++) {
1909 if (next->tx_comp.rcs[i]) {
1910 dev_err(dev, "tx error %x\n",
1911 next->tx_comp.rcs[i]);
1912 continue;
1913 }
1914 index = be32_to_cpu(next->tx_comp.correlators[i]);
1915 txbuff = &adapter->tx_pool[pool].tx_buff[index];
1916
1917 for (j = 0; j < IBMVNIC_MAX_FRAGS_PER_CRQ; j++) {
1918 if (!txbuff->data_dma[j])
1919 continue;
1920
1921 txbuff->data_dma[j] = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001922 }
Thomas Falconad7775d2016-04-01 17:20:34 -05001923 /* if sub_crq was sent indirectly */
1924 first = txbuff->indir_arr[0].generic.first;
1925 if (first == IBMVNIC_CRQ_CMD) {
1926 dma_unmap_single(dev, txbuff->indir_dma,
1927 sizeof(txbuff->indir_arr),
1928 DMA_TO_DEVICE);
1929 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001930
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001931 if (txbuff->last_frag) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06001932 dev_kfree_skb_any(txbuff->skb);
Nathan Fontenot7c3e7de2017-05-03 14:05:25 -04001933 txbuff->skb = NULL;
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001934 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001935
1936 adapter->tx_pool[pool].free_map[adapter->tx_pool[pool].
1937 producer_index] = index;
1938 adapter->tx_pool[pool].producer_index =
1939 (adapter->tx_pool[pool].producer_index + 1) %
Thomas Falcon068d9f92017-03-05 12:18:42 -06001940 adapter->req_tx_entries_per_subcrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001941 }
1942 /* remove tx_comp scrq*/
1943 next->tx_comp.first = 0;
Nathan Fontenot7c3e7de2017-05-03 14:05:25 -04001944
1945 if (atomic_sub_return(next->tx_comp.num_comps, &scrq->used) <=
1946 (adapter->req_tx_entries_per_subcrq / 2) &&
1947 __netif_subqueue_stopped(adapter->netdev,
1948 scrq->pool_index)) {
1949 netif_wake_subqueue(adapter->netdev, scrq->pool_index);
1950 netdev_info(adapter->netdev, "Started queue %d\n",
1951 scrq->pool_index);
1952 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001953 }
1954
1955 enable_scrq_irq(adapter, scrq);
1956
1957 if (pending_scrq(adapter, scrq)) {
1958 disable_scrq_irq(adapter, scrq);
1959 goto restart_loop;
1960 }
1961
1962 return 0;
1963}
1964
1965static irqreturn_t ibmvnic_interrupt_tx(int irq, void *instance)
1966{
1967 struct ibmvnic_sub_crq_queue *scrq = instance;
1968 struct ibmvnic_adapter *adapter = scrq->adapter;
1969
1970 disable_scrq_irq(adapter, scrq);
1971 ibmvnic_complete_tx(adapter, scrq);
1972
1973 return IRQ_HANDLED;
1974}
1975
1976static irqreturn_t ibmvnic_interrupt_rx(int irq, void *instance)
1977{
1978 struct ibmvnic_sub_crq_queue *scrq = instance;
1979 struct ibmvnic_adapter *adapter = scrq->adapter;
1980
1981 if (napi_schedule_prep(&adapter->napi[scrq->scrq_num])) {
1982 disable_scrq_irq(adapter, scrq);
1983 __napi_schedule(&adapter->napi[scrq->scrq_num]);
1984 }
1985
1986 return IRQ_HANDLED;
1987}
1988
Thomas Falconea22d512016-07-06 15:35:17 -05001989static int init_sub_crq_irqs(struct ibmvnic_adapter *adapter)
1990{
1991 struct device *dev = &adapter->vdev->dev;
1992 struct ibmvnic_sub_crq_queue *scrq;
1993 int i = 0, j = 0;
1994 int rc = 0;
1995
1996 for (i = 0; i < adapter->req_tx_queues; i++) {
1997 scrq = adapter->tx_scrq[i];
1998 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
1999
Michael Ellerman99c17902016-09-10 19:59:05 +10002000 if (!scrq->irq) {
Thomas Falconea22d512016-07-06 15:35:17 -05002001 rc = -EINVAL;
2002 dev_err(dev, "Error mapping irq\n");
2003 goto req_tx_irq_failed;
2004 }
2005
2006 rc = request_irq(scrq->irq, ibmvnic_interrupt_tx,
2007 0, "ibmvnic_tx", scrq);
2008
2009 if (rc) {
2010 dev_err(dev, "Couldn't register tx irq 0x%x. rc=%d\n",
2011 scrq->irq, rc);
2012 irq_dispose_mapping(scrq->irq);
2013 goto req_rx_irq_failed;
2014 }
2015 }
2016
2017 for (i = 0; i < adapter->req_rx_queues; i++) {
2018 scrq = adapter->rx_scrq[i];
2019 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
Michael Ellerman99c17902016-09-10 19:59:05 +10002020 if (!scrq->irq) {
Thomas Falconea22d512016-07-06 15:35:17 -05002021 rc = -EINVAL;
2022 dev_err(dev, "Error mapping irq\n");
2023 goto req_rx_irq_failed;
2024 }
2025 rc = request_irq(scrq->irq, ibmvnic_interrupt_rx,
2026 0, "ibmvnic_rx", scrq);
2027 if (rc) {
2028 dev_err(dev, "Couldn't register rx irq 0x%x. rc=%d\n",
2029 scrq->irq, rc);
2030 irq_dispose_mapping(scrq->irq);
2031 goto req_rx_irq_failed;
2032 }
2033 }
2034 return rc;
2035
2036req_rx_irq_failed:
Thomas Falcon8bf371e2016-10-27 12:28:52 -05002037 for (j = 0; j < i; j++) {
Thomas Falconea22d512016-07-06 15:35:17 -05002038 free_irq(adapter->rx_scrq[j]->irq, adapter->rx_scrq[j]);
2039 irq_dispose_mapping(adapter->rx_scrq[j]->irq);
Thomas Falcon8bf371e2016-10-27 12:28:52 -05002040 }
Thomas Falconea22d512016-07-06 15:35:17 -05002041 i = adapter->req_tx_queues;
2042req_tx_irq_failed:
Thomas Falcon8bf371e2016-10-27 12:28:52 -05002043 for (j = 0; j < i; j++) {
Thomas Falconea22d512016-07-06 15:35:17 -05002044 free_irq(adapter->tx_scrq[j]->irq, adapter->tx_scrq[j]);
2045 irq_dispose_mapping(adapter->rx_scrq[j]->irq);
Thomas Falcon8bf371e2016-10-27 12:28:52 -05002046 }
Nathan Fontenotb5108882017-03-30 02:49:18 -04002047 release_sub_crqs(adapter);
Thomas Falconea22d512016-07-06 15:35:17 -05002048 return rc;
2049}
2050
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002051static int init_sub_crqs(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002052{
2053 struct device *dev = &adapter->vdev->dev;
2054 struct ibmvnic_sub_crq_queue **allqueues;
2055 int registered_queues = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002056 int total_queues;
2057 int more = 0;
Thomas Falconea22d512016-07-06 15:35:17 -05002058 int i;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002059
Thomas Falcon032c5e82015-12-21 11:26:06 -06002060 total_queues = adapter->req_tx_queues + adapter->req_rx_queues;
2061
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04002062 allqueues = kcalloc(total_queues, sizeof(*allqueues), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002063 if (!allqueues)
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002064 return -1;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002065
2066 for (i = 0; i < total_queues; i++) {
2067 allqueues[i] = init_sub_crq_queue(adapter);
2068 if (!allqueues[i]) {
2069 dev_warn(dev, "Couldn't allocate all sub-crqs\n");
2070 break;
2071 }
2072 registered_queues++;
2073 }
2074
2075 /* Make sure we were able to register the minimum number of queues */
2076 if (registered_queues <
2077 adapter->min_tx_queues + adapter->min_rx_queues) {
2078 dev_err(dev, "Fatal: Couldn't init min number of sub-crqs\n");
2079 goto tx_failed;
2080 }
2081
2082 /* Distribute the failed allocated queues*/
2083 for (i = 0; i < total_queues - registered_queues + more ; i++) {
2084 netdev_dbg(adapter->netdev, "Reducing number of queues\n");
2085 switch (i % 3) {
2086 case 0:
2087 if (adapter->req_rx_queues > adapter->min_rx_queues)
2088 adapter->req_rx_queues--;
2089 else
2090 more++;
2091 break;
2092 case 1:
2093 if (adapter->req_tx_queues > adapter->min_tx_queues)
2094 adapter->req_tx_queues--;
2095 else
2096 more++;
2097 break;
2098 }
2099 }
2100
2101 adapter->tx_scrq = kcalloc(adapter->req_tx_queues,
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04002102 sizeof(*adapter->tx_scrq), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002103 if (!adapter->tx_scrq)
2104 goto tx_failed;
2105
2106 for (i = 0; i < adapter->req_tx_queues; i++) {
2107 adapter->tx_scrq[i] = allqueues[i];
2108 adapter->tx_scrq[i]->pool_index = i;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002109 }
2110
2111 adapter->rx_scrq = kcalloc(adapter->req_rx_queues,
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04002112 sizeof(*adapter->rx_scrq), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002113 if (!adapter->rx_scrq)
2114 goto rx_failed;
2115
2116 for (i = 0; i < adapter->req_rx_queues; i++) {
2117 adapter->rx_scrq[i] = allqueues[i + adapter->req_tx_queues];
2118 adapter->rx_scrq[i]->scrq_num = i;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002119 }
2120
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002121 kfree(allqueues);
2122 return 0;
2123
2124rx_failed:
2125 kfree(adapter->tx_scrq);
2126 adapter->tx_scrq = NULL;
2127tx_failed:
2128 for (i = 0; i < registered_queues; i++)
2129 release_sub_crq_queue(adapter, allqueues[i]);
2130 kfree(allqueues);
2131 return -1;
2132}
2133
2134static void ibmvnic_send_req_caps(struct ibmvnic_adapter *adapter, int retry)
2135{
2136 struct device *dev = &adapter->vdev->dev;
2137 union ibmvnic_crq crq;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002138
2139 if (!retry) {
2140 /* Sub-CRQ entries are 32 byte long */
2141 int entries_page = 4 * PAGE_SIZE / (sizeof(u64) * 4);
2142
2143 if (adapter->min_tx_entries_per_subcrq > entries_page ||
2144 adapter->min_rx_add_entries_per_subcrq > entries_page) {
2145 dev_err(dev, "Fatal, invalid entries per sub-crq\n");
2146 return;
2147 }
2148
2149 /* Get the minimum between the queried max and the entries
2150 * that fit in our PAGE_SIZE
2151 */
2152 adapter->req_tx_entries_per_subcrq =
2153 adapter->max_tx_entries_per_subcrq > entries_page ?
2154 entries_page : adapter->max_tx_entries_per_subcrq;
2155 adapter->req_rx_add_entries_per_subcrq =
2156 adapter->max_rx_add_entries_per_subcrq > entries_page ?
2157 entries_page : adapter->max_rx_add_entries_per_subcrq;
2158
2159 adapter->req_tx_queues = adapter->opt_tx_comp_sub_queues;
2160 adapter->req_rx_queues = adapter->opt_rx_comp_queues;
2161 adapter->req_rx_add_queues = adapter->max_rx_add_queues;
2162
2163 adapter->req_mtu = adapter->netdev->mtu + ETH_HLEN;
2164 }
2165
Thomas Falcon032c5e82015-12-21 11:26:06 -06002166 memset(&crq, 0, sizeof(crq));
2167 crq.request_capability.first = IBMVNIC_CRQ_CMD;
2168 crq.request_capability.cmd = REQUEST_CAPABILITY;
2169
2170 crq.request_capability.capability = cpu_to_be16(REQ_TX_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06002171 crq.request_capability.number = cpu_to_be64(adapter->req_tx_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06002172 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002173 ibmvnic_send_crq(adapter, &crq);
2174
2175 crq.request_capability.capability = cpu_to_be16(REQ_RX_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06002176 crq.request_capability.number = cpu_to_be64(adapter->req_rx_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06002177 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002178 ibmvnic_send_crq(adapter, &crq);
2179
2180 crq.request_capability.capability = cpu_to_be16(REQ_RX_ADD_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06002181 crq.request_capability.number = cpu_to_be64(adapter->req_rx_add_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06002182 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002183 ibmvnic_send_crq(adapter, &crq);
2184
2185 crq.request_capability.capability =
2186 cpu_to_be16(REQ_TX_ENTRIES_PER_SUBCRQ);
2187 crq.request_capability.number =
Thomas Falconde89e852016-03-01 10:20:09 -06002188 cpu_to_be64(adapter->req_tx_entries_per_subcrq);
Thomas Falcon901e0402017-02-15 12:17:59 -06002189 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002190 ibmvnic_send_crq(adapter, &crq);
2191
2192 crq.request_capability.capability =
2193 cpu_to_be16(REQ_RX_ADD_ENTRIES_PER_SUBCRQ);
2194 crq.request_capability.number =
Thomas Falconde89e852016-03-01 10:20:09 -06002195 cpu_to_be64(adapter->req_rx_add_entries_per_subcrq);
Thomas Falcon901e0402017-02-15 12:17:59 -06002196 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002197 ibmvnic_send_crq(adapter, &crq);
2198
2199 crq.request_capability.capability = cpu_to_be16(REQ_MTU);
Thomas Falconde89e852016-03-01 10:20:09 -06002200 crq.request_capability.number = cpu_to_be64(adapter->req_mtu);
Thomas Falcon901e0402017-02-15 12:17:59 -06002201 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002202 ibmvnic_send_crq(adapter, &crq);
2203
2204 if (adapter->netdev->flags & IFF_PROMISC) {
2205 if (adapter->promisc_supported) {
2206 crq.request_capability.capability =
2207 cpu_to_be16(PROMISC_REQUESTED);
Thomas Falconde89e852016-03-01 10:20:09 -06002208 crq.request_capability.number = cpu_to_be64(1);
Thomas Falcon901e0402017-02-15 12:17:59 -06002209 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002210 ibmvnic_send_crq(adapter, &crq);
2211 }
2212 } else {
2213 crq.request_capability.capability =
2214 cpu_to_be16(PROMISC_REQUESTED);
Thomas Falconde89e852016-03-01 10:20:09 -06002215 crq.request_capability.number = cpu_to_be64(0);
Thomas Falcon901e0402017-02-15 12:17:59 -06002216 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002217 ibmvnic_send_crq(adapter, &crq);
2218 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002219}
2220
2221static int pending_scrq(struct ibmvnic_adapter *adapter,
2222 struct ibmvnic_sub_crq_queue *scrq)
2223{
2224 union sub_crq *entry = &scrq->msgs[scrq->cur];
2225
Nathan Fontenot90c80142017-05-03 14:04:32 -04002226 if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP ||
2227 adapter->state == VNIC_CLOSING)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002228 return 1;
2229 else
2230 return 0;
2231}
2232
2233static union sub_crq *ibmvnic_next_scrq(struct ibmvnic_adapter *adapter,
2234 struct ibmvnic_sub_crq_queue *scrq)
2235{
2236 union sub_crq *entry;
2237 unsigned long flags;
2238
2239 spin_lock_irqsave(&scrq->lock, flags);
2240 entry = &scrq->msgs[scrq->cur];
2241 if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP) {
2242 if (++scrq->cur == scrq->size)
2243 scrq->cur = 0;
2244 } else {
2245 entry = NULL;
2246 }
2247 spin_unlock_irqrestore(&scrq->lock, flags);
2248
2249 return entry;
2250}
2251
2252static union ibmvnic_crq *ibmvnic_next_crq(struct ibmvnic_adapter *adapter)
2253{
2254 struct ibmvnic_crq_queue *queue = &adapter->crq;
2255 union ibmvnic_crq *crq;
2256
2257 crq = &queue->msgs[queue->cur];
2258 if (crq->generic.first & IBMVNIC_CRQ_CMD_RSP) {
2259 if (++queue->cur == queue->size)
2260 queue->cur = 0;
2261 } else {
2262 crq = NULL;
2263 }
2264
2265 return crq;
2266}
2267
2268static int send_subcrq(struct ibmvnic_adapter *adapter, u64 remote_handle,
2269 union sub_crq *sub_crq)
2270{
2271 unsigned int ua = adapter->vdev->unit_address;
2272 struct device *dev = &adapter->vdev->dev;
2273 u64 *u64_crq = (u64 *)sub_crq;
2274 int rc;
2275
2276 netdev_dbg(adapter->netdev,
2277 "Sending sCRQ %016lx: %016lx %016lx %016lx %016lx\n",
2278 (unsigned long int)cpu_to_be64(remote_handle),
2279 (unsigned long int)cpu_to_be64(u64_crq[0]),
2280 (unsigned long int)cpu_to_be64(u64_crq[1]),
2281 (unsigned long int)cpu_to_be64(u64_crq[2]),
2282 (unsigned long int)cpu_to_be64(u64_crq[3]));
2283
2284 /* Make sure the hypervisor sees the complete request */
2285 mb();
2286
2287 rc = plpar_hcall_norets(H_SEND_SUB_CRQ, ua,
2288 cpu_to_be64(remote_handle),
2289 cpu_to_be64(u64_crq[0]),
2290 cpu_to_be64(u64_crq[1]),
2291 cpu_to_be64(u64_crq[2]),
2292 cpu_to_be64(u64_crq[3]));
2293
2294 if (rc) {
2295 if (rc == H_CLOSED)
2296 dev_warn(dev, "CRQ Queue closed\n");
2297 dev_err(dev, "Send error (rc=%d)\n", rc);
2298 }
2299
2300 return rc;
2301}
2302
Thomas Falconad7775d2016-04-01 17:20:34 -05002303static int send_subcrq_indirect(struct ibmvnic_adapter *adapter,
2304 u64 remote_handle, u64 ioba, u64 num_entries)
2305{
2306 unsigned int ua = adapter->vdev->unit_address;
2307 struct device *dev = &adapter->vdev->dev;
2308 int rc;
2309
2310 /* Make sure the hypervisor sees the complete request */
2311 mb();
2312 rc = plpar_hcall_norets(H_SEND_SUB_CRQ_INDIRECT, ua,
2313 cpu_to_be64(remote_handle),
2314 ioba, num_entries);
2315
2316 if (rc) {
2317 if (rc == H_CLOSED)
2318 dev_warn(dev, "CRQ Queue closed\n");
2319 dev_err(dev, "Send (indirect) error (rc=%d)\n", rc);
2320 }
2321
2322 return rc;
2323}
2324
Thomas Falcon032c5e82015-12-21 11:26:06 -06002325static int ibmvnic_send_crq(struct ibmvnic_adapter *adapter,
2326 union ibmvnic_crq *crq)
2327{
2328 unsigned int ua = adapter->vdev->unit_address;
2329 struct device *dev = &adapter->vdev->dev;
2330 u64 *u64_crq = (u64 *)crq;
2331 int rc;
2332
2333 netdev_dbg(adapter->netdev, "Sending CRQ: %016lx %016lx\n",
2334 (unsigned long int)cpu_to_be64(u64_crq[0]),
2335 (unsigned long int)cpu_to_be64(u64_crq[1]));
2336
2337 /* Make sure the hypervisor sees the complete request */
2338 mb();
2339
2340 rc = plpar_hcall_norets(H_SEND_CRQ, ua,
2341 cpu_to_be64(u64_crq[0]),
2342 cpu_to_be64(u64_crq[1]));
2343
2344 if (rc) {
2345 if (rc == H_CLOSED)
2346 dev_warn(dev, "CRQ Queue closed\n");
2347 dev_warn(dev, "Send error (rc=%d)\n", rc);
2348 }
2349
2350 return rc;
2351}
2352
2353static int ibmvnic_send_crq_init(struct ibmvnic_adapter *adapter)
2354{
2355 union ibmvnic_crq crq;
2356
2357 memset(&crq, 0, sizeof(crq));
2358 crq.generic.first = IBMVNIC_CRQ_INIT_CMD;
2359 crq.generic.cmd = IBMVNIC_CRQ_INIT;
2360 netdev_dbg(adapter->netdev, "Sending CRQ init\n");
2361
2362 return ibmvnic_send_crq(adapter, &crq);
2363}
2364
Thomas Falcon032c5e82015-12-21 11:26:06 -06002365static int send_version_xchg(struct ibmvnic_adapter *adapter)
2366{
2367 union ibmvnic_crq crq;
2368
2369 memset(&crq, 0, sizeof(crq));
2370 crq.version_exchange.first = IBMVNIC_CRQ_CMD;
2371 crq.version_exchange.cmd = VERSION_EXCHANGE;
2372 crq.version_exchange.version = cpu_to_be16(ibmvnic_version);
2373
2374 return ibmvnic_send_crq(adapter, &crq);
2375}
2376
2377static void send_login(struct ibmvnic_adapter *adapter)
2378{
2379 struct ibmvnic_login_rsp_buffer *login_rsp_buffer;
2380 struct ibmvnic_login_buffer *login_buffer;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002381 struct device *dev = &adapter->vdev->dev;
2382 dma_addr_t rsp_buffer_token;
2383 dma_addr_t buffer_token;
2384 size_t rsp_buffer_size;
2385 union ibmvnic_crq crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002386 size_t buffer_size;
2387 __be64 *tx_list_p;
2388 __be64 *rx_list_p;
2389 int i;
2390
2391 buffer_size =
2392 sizeof(struct ibmvnic_login_buffer) +
2393 sizeof(u64) * (adapter->req_tx_queues + adapter->req_rx_queues);
2394
2395 login_buffer = kmalloc(buffer_size, GFP_ATOMIC);
2396 if (!login_buffer)
2397 goto buf_alloc_failed;
2398
2399 buffer_token = dma_map_single(dev, login_buffer, buffer_size,
2400 DMA_TO_DEVICE);
2401 if (dma_mapping_error(dev, buffer_token)) {
2402 dev_err(dev, "Couldn't map login buffer\n");
2403 goto buf_map_failed;
2404 }
2405
John Allen498cd8e2016-04-06 11:49:55 -05002406 rsp_buffer_size = sizeof(struct ibmvnic_login_rsp_buffer) +
2407 sizeof(u64) * adapter->req_tx_queues +
2408 sizeof(u64) * adapter->req_rx_queues +
2409 sizeof(u64) * adapter->req_rx_queues +
2410 sizeof(u8) * IBMVNIC_TX_DESC_VERSIONS;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002411
2412 login_rsp_buffer = kmalloc(rsp_buffer_size, GFP_ATOMIC);
2413 if (!login_rsp_buffer)
2414 goto buf_rsp_alloc_failed;
2415
2416 rsp_buffer_token = dma_map_single(dev, login_rsp_buffer,
2417 rsp_buffer_size, DMA_FROM_DEVICE);
2418 if (dma_mapping_error(dev, rsp_buffer_token)) {
2419 dev_err(dev, "Couldn't map login rsp buffer\n");
2420 goto buf_rsp_map_failed;
2421 }
Nathan Fontenot661a2622017-04-19 13:44:58 -04002422
Thomas Falcon032c5e82015-12-21 11:26:06 -06002423 adapter->login_buf = login_buffer;
2424 adapter->login_buf_token = buffer_token;
2425 adapter->login_buf_sz = buffer_size;
2426 adapter->login_rsp_buf = login_rsp_buffer;
2427 adapter->login_rsp_buf_token = rsp_buffer_token;
2428 adapter->login_rsp_buf_sz = rsp_buffer_size;
2429
2430 login_buffer->len = cpu_to_be32(buffer_size);
2431 login_buffer->version = cpu_to_be32(INITIAL_VERSION_LB);
2432 login_buffer->num_txcomp_subcrqs = cpu_to_be32(adapter->req_tx_queues);
2433 login_buffer->off_txcomp_subcrqs =
2434 cpu_to_be32(sizeof(struct ibmvnic_login_buffer));
2435 login_buffer->num_rxcomp_subcrqs = cpu_to_be32(adapter->req_rx_queues);
2436 login_buffer->off_rxcomp_subcrqs =
2437 cpu_to_be32(sizeof(struct ibmvnic_login_buffer) +
2438 sizeof(u64) * adapter->req_tx_queues);
2439 login_buffer->login_rsp_ioba = cpu_to_be32(rsp_buffer_token);
2440 login_buffer->login_rsp_len = cpu_to_be32(rsp_buffer_size);
2441
2442 tx_list_p = (__be64 *)((char *)login_buffer +
2443 sizeof(struct ibmvnic_login_buffer));
2444 rx_list_p = (__be64 *)((char *)login_buffer +
2445 sizeof(struct ibmvnic_login_buffer) +
2446 sizeof(u64) * adapter->req_tx_queues);
2447
2448 for (i = 0; i < adapter->req_tx_queues; i++) {
2449 if (adapter->tx_scrq[i]) {
2450 tx_list_p[i] = cpu_to_be64(adapter->tx_scrq[i]->
2451 crq_num);
2452 }
2453 }
2454
2455 for (i = 0; i < adapter->req_rx_queues; i++) {
2456 if (adapter->rx_scrq[i]) {
2457 rx_list_p[i] = cpu_to_be64(adapter->rx_scrq[i]->
2458 crq_num);
2459 }
2460 }
2461
2462 netdev_dbg(adapter->netdev, "Login Buffer:\n");
2463 for (i = 0; i < (adapter->login_buf_sz - 1) / 8 + 1; i++) {
2464 netdev_dbg(adapter->netdev, "%016lx\n",
2465 ((unsigned long int *)(adapter->login_buf))[i]);
2466 }
2467
2468 memset(&crq, 0, sizeof(crq));
2469 crq.login.first = IBMVNIC_CRQ_CMD;
2470 crq.login.cmd = LOGIN;
2471 crq.login.ioba = cpu_to_be32(buffer_token);
2472 crq.login.len = cpu_to_be32(buffer_size);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002473 ibmvnic_send_crq(adapter, &crq);
2474
2475 return;
2476
Thomas Falcon032c5e82015-12-21 11:26:06 -06002477buf_rsp_map_failed:
2478 kfree(login_rsp_buffer);
2479buf_rsp_alloc_failed:
2480 dma_unmap_single(dev, buffer_token, buffer_size, DMA_TO_DEVICE);
2481buf_map_failed:
2482 kfree(login_buffer);
2483buf_alloc_failed:
2484 return;
2485}
2486
2487static void send_request_map(struct ibmvnic_adapter *adapter, dma_addr_t addr,
2488 u32 len, u8 map_id)
2489{
2490 union ibmvnic_crq crq;
2491
2492 memset(&crq, 0, sizeof(crq));
2493 crq.request_map.first = IBMVNIC_CRQ_CMD;
2494 crq.request_map.cmd = REQUEST_MAP;
2495 crq.request_map.map_id = map_id;
2496 crq.request_map.ioba = cpu_to_be32(addr);
2497 crq.request_map.len = cpu_to_be32(len);
2498 ibmvnic_send_crq(adapter, &crq);
2499}
2500
2501static void send_request_unmap(struct ibmvnic_adapter *adapter, u8 map_id)
2502{
2503 union ibmvnic_crq crq;
2504
2505 memset(&crq, 0, sizeof(crq));
2506 crq.request_unmap.first = IBMVNIC_CRQ_CMD;
2507 crq.request_unmap.cmd = REQUEST_UNMAP;
2508 crq.request_unmap.map_id = map_id;
2509 ibmvnic_send_crq(adapter, &crq);
2510}
2511
2512static void send_map_query(struct ibmvnic_adapter *adapter)
2513{
2514 union ibmvnic_crq crq;
2515
2516 memset(&crq, 0, sizeof(crq));
2517 crq.query_map.first = IBMVNIC_CRQ_CMD;
2518 crq.query_map.cmd = QUERY_MAP;
2519 ibmvnic_send_crq(adapter, &crq);
2520}
2521
2522/* Send a series of CRQs requesting various capabilities of the VNIC server */
2523static void send_cap_queries(struct ibmvnic_adapter *adapter)
2524{
2525 union ibmvnic_crq crq;
2526
Thomas Falcon901e0402017-02-15 12:17:59 -06002527 atomic_set(&adapter->running_cap_crqs, 0);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002528 memset(&crq, 0, sizeof(crq));
2529 crq.query_capability.first = IBMVNIC_CRQ_CMD;
2530 crq.query_capability.cmd = QUERY_CAPABILITY;
2531
2532 crq.query_capability.capability = cpu_to_be16(MIN_TX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002533 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002534 ibmvnic_send_crq(adapter, &crq);
2535
2536 crq.query_capability.capability = cpu_to_be16(MIN_RX_QUEUES);
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(MIN_RX_ADD_QUEUES);
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 crq.query_capability.capability = cpu_to_be16(MAX_TX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002545 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002546 ibmvnic_send_crq(adapter, &crq);
2547
2548 crq.query_capability.capability = cpu_to_be16(MAX_RX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002549 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002550 ibmvnic_send_crq(adapter, &crq);
2551
2552 crq.query_capability.capability = cpu_to_be16(MAX_RX_ADD_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002553 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002554 ibmvnic_send_crq(adapter, &crq);
2555
2556 crq.query_capability.capability =
2557 cpu_to_be16(MIN_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06002558 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002559 ibmvnic_send_crq(adapter, &crq);
2560
2561 crq.query_capability.capability =
2562 cpu_to_be16(MIN_RX_ADD_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06002563 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002564 ibmvnic_send_crq(adapter, &crq);
2565
2566 crq.query_capability.capability =
2567 cpu_to_be16(MAX_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06002568 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002569 ibmvnic_send_crq(adapter, &crq);
2570
2571 crq.query_capability.capability =
2572 cpu_to_be16(MAX_RX_ADD_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06002573 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002574 ibmvnic_send_crq(adapter, &crq);
2575
2576 crq.query_capability.capability = cpu_to_be16(TCP_IP_OFFLOAD);
Thomas Falcon901e0402017-02-15 12:17:59 -06002577 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002578 ibmvnic_send_crq(adapter, &crq);
2579
2580 crq.query_capability.capability = cpu_to_be16(PROMISC_SUPPORTED);
Thomas Falcon901e0402017-02-15 12:17:59 -06002581 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002582 ibmvnic_send_crq(adapter, &crq);
2583
2584 crq.query_capability.capability = cpu_to_be16(MIN_MTU);
Thomas Falcon901e0402017-02-15 12:17:59 -06002585 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002586 ibmvnic_send_crq(adapter, &crq);
2587
2588 crq.query_capability.capability = cpu_to_be16(MAX_MTU);
Thomas Falcon901e0402017-02-15 12:17:59 -06002589 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002590 ibmvnic_send_crq(adapter, &crq);
2591
2592 crq.query_capability.capability = cpu_to_be16(MAX_MULTICAST_FILTERS);
Thomas Falcon901e0402017-02-15 12:17:59 -06002593 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002594 ibmvnic_send_crq(adapter, &crq);
2595
2596 crq.query_capability.capability = cpu_to_be16(VLAN_HEADER_INSERTION);
Thomas Falcon901e0402017-02-15 12:17:59 -06002597 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002598 ibmvnic_send_crq(adapter, &crq);
2599
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04002600 crq.query_capability.capability = cpu_to_be16(RX_VLAN_HEADER_INSERTION);
2601 atomic_inc(&adapter->running_cap_crqs);
2602 ibmvnic_send_crq(adapter, &crq);
2603
Thomas Falcon032c5e82015-12-21 11:26:06 -06002604 crq.query_capability.capability = cpu_to_be16(MAX_TX_SG_ENTRIES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002605 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002606 ibmvnic_send_crq(adapter, &crq);
2607
2608 crq.query_capability.capability = cpu_to_be16(RX_SG_SUPPORTED);
Thomas Falcon901e0402017-02-15 12:17:59 -06002609 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002610 ibmvnic_send_crq(adapter, &crq);
2611
2612 crq.query_capability.capability = cpu_to_be16(OPT_TX_COMP_SUB_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002613 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002614 ibmvnic_send_crq(adapter, &crq);
2615
2616 crq.query_capability.capability = cpu_to_be16(OPT_RX_COMP_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002617 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002618 ibmvnic_send_crq(adapter, &crq);
2619
2620 crq.query_capability.capability =
2621 cpu_to_be16(OPT_RX_BUFADD_Q_PER_RX_COMP_Q);
Thomas Falcon901e0402017-02-15 12:17:59 -06002622 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002623 ibmvnic_send_crq(adapter, &crq);
2624
2625 crq.query_capability.capability =
2626 cpu_to_be16(OPT_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06002627 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002628 ibmvnic_send_crq(adapter, &crq);
2629
2630 crq.query_capability.capability =
2631 cpu_to_be16(OPT_RXBA_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06002632 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002633 ibmvnic_send_crq(adapter, &crq);
2634
2635 crq.query_capability.capability = cpu_to_be16(TX_RX_DESC_REQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06002636 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002637 ibmvnic_send_crq(adapter, &crq);
2638}
2639
2640static void handle_query_ip_offload_rsp(struct ibmvnic_adapter *adapter)
2641{
2642 struct device *dev = &adapter->vdev->dev;
2643 struct ibmvnic_query_ip_offload_buffer *buf = &adapter->ip_offload_buf;
2644 union ibmvnic_crq crq;
2645 int i;
2646
2647 dma_unmap_single(dev, adapter->ip_offload_tok,
2648 sizeof(adapter->ip_offload_buf), DMA_FROM_DEVICE);
2649
2650 netdev_dbg(adapter->netdev, "Query IP Offload Buffer:\n");
2651 for (i = 0; i < (sizeof(adapter->ip_offload_buf) - 1) / 8 + 1; i++)
2652 netdev_dbg(adapter->netdev, "%016lx\n",
2653 ((unsigned long int *)(buf))[i]);
2654
2655 netdev_dbg(adapter->netdev, "ipv4_chksum = %d\n", buf->ipv4_chksum);
2656 netdev_dbg(adapter->netdev, "ipv6_chksum = %d\n", buf->ipv6_chksum);
2657 netdev_dbg(adapter->netdev, "tcp_ipv4_chksum = %d\n",
2658 buf->tcp_ipv4_chksum);
2659 netdev_dbg(adapter->netdev, "tcp_ipv6_chksum = %d\n",
2660 buf->tcp_ipv6_chksum);
2661 netdev_dbg(adapter->netdev, "udp_ipv4_chksum = %d\n",
2662 buf->udp_ipv4_chksum);
2663 netdev_dbg(adapter->netdev, "udp_ipv6_chksum = %d\n",
2664 buf->udp_ipv6_chksum);
2665 netdev_dbg(adapter->netdev, "large_tx_ipv4 = %d\n",
2666 buf->large_tx_ipv4);
2667 netdev_dbg(adapter->netdev, "large_tx_ipv6 = %d\n",
2668 buf->large_tx_ipv6);
2669 netdev_dbg(adapter->netdev, "large_rx_ipv4 = %d\n",
2670 buf->large_rx_ipv4);
2671 netdev_dbg(adapter->netdev, "large_rx_ipv6 = %d\n",
2672 buf->large_rx_ipv6);
2673 netdev_dbg(adapter->netdev, "max_ipv4_hdr_sz = %d\n",
2674 buf->max_ipv4_header_size);
2675 netdev_dbg(adapter->netdev, "max_ipv6_hdr_sz = %d\n",
2676 buf->max_ipv6_header_size);
2677 netdev_dbg(adapter->netdev, "max_tcp_hdr_size = %d\n",
2678 buf->max_tcp_header_size);
2679 netdev_dbg(adapter->netdev, "max_udp_hdr_size = %d\n",
2680 buf->max_udp_header_size);
2681 netdev_dbg(adapter->netdev, "max_large_tx_size = %d\n",
2682 buf->max_large_tx_size);
2683 netdev_dbg(adapter->netdev, "max_large_rx_size = %d\n",
2684 buf->max_large_rx_size);
2685 netdev_dbg(adapter->netdev, "ipv6_ext_hdr = %d\n",
2686 buf->ipv6_extension_header);
2687 netdev_dbg(adapter->netdev, "tcp_pseudosum_req = %d\n",
2688 buf->tcp_pseudosum_req);
2689 netdev_dbg(adapter->netdev, "num_ipv6_ext_hd = %d\n",
2690 buf->num_ipv6_ext_headers);
2691 netdev_dbg(adapter->netdev, "off_ipv6_ext_hd = %d\n",
2692 buf->off_ipv6_ext_headers);
2693
2694 adapter->ip_offload_ctrl_tok =
2695 dma_map_single(dev, &adapter->ip_offload_ctrl,
2696 sizeof(adapter->ip_offload_ctrl), DMA_TO_DEVICE);
2697
2698 if (dma_mapping_error(dev, adapter->ip_offload_ctrl_tok)) {
2699 dev_err(dev, "Couldn't map ip offload control buffer\n");
2700 return;
2701 }
2702
2703 adapter->ip_offload_ctrl.version = cpu_to_be32(INITIAL_VERSION_IOB);
2704 adapter->ip_offload_ctrl.tcp_ipv4_chksum = buf->tcp_ipv4_chksum;
2705 adapter->ip_offload_ctrl.udp_ipv4_chksum = buf->udp_ipv4_chksum;
2706 adapter->ip_offload_ctrl.tcp_ipv6_chksum = buf->tcp_ipv6_chksum;
2707 adapter->ip_offload_ctrl.udp_ipv6_chksum = buf->udp_ipv6_chksum;
2708
2709 /* large_tx/rx disabled for now, additional features needed */
2710 adapter->ip_offload_ctrl.large_tx_ipv4 = 0;
2711 adapter->ip_offload_ctrl.large_tx_ipv6 = 0;
2712 adapter->ip_offload_ctrl.large_rx_ipv4 = 0;
2713 adapter->ip_offload_ctrl.large_rx_ipv6 = 0;
2714
2715 adapter->netdev->features = NETIF_F_GSO;
2716
2717 if (buf->tcp_ipv4_chksum || buf->udp_ipv4_chksum)
2718 adapter->netdev->features |= NETIF_F_IP_CSUM;
2719
2720 if (buf->tcp_ipv6_chksum || buf->udp_ipv6_chksum)
2721 adapter->netdev->features |= NETIF_F_IPV6_CSUM;
2722
Thomas Falcon9be02cd2016-04-01 17:20:35 -05002723 if ((adapter->netdev->features &
2724 (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)))
2725 adapter->netdev->features |= NETIF_F_RXCSUM;
2726
Thomas Falcon032c5e82015-12-21 11:26:06 -06002727 memset(&crq, 0, sizeof(crq));
2728 crq.control_ip_offload.first = IBMVNIC_CRQ_CMD;
2729 crq.control_ip_offload.cmd = CONTROL_IP_OFFLOAD;
2730 crq.control_ip_offload.len =
2731 cpu_to_be32(sizeof(adapter->ip_offload_ctrl));
2732 crq.control_ip_offload.ioba = cpu_to_be32(adapter->ip_offload_ctrl_tok);
2733 ibmvnic_send_crq(adapter, &crq);
2734}
2735
2736static void handle_error_info_rsp(union ibmvnic_crq *crq,
2737 struct ibmvnic_adapter *adapter)
2738{
2739 struct device *dev = &adapter->vdev->dev;
Wei Yongjun96183182016-06-27 20:48:53 +08002740 struct ibmvnic_error_buff *error_buff, *tmp;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002741 unsigned long flags;
2742 bool found = false;
2743 int i;
2744
2745 if (!crq->request_error_rsp.rc.code) {
2746 dev_info(dev, "Request Error Rsp returned with rc=%x\n",
2747 crq->request_error_rsp.rc.code);
2748 return;
2749 }
2750
2751 spin_lock_irqsave(&adapter->error_list_lock, flags);
Wei Yongjun96183182016-06-27 20:48:53 +08002752 list_for_each_entry_safe(error_buff, tmp, &adapter->errors, list)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002753 if (error_buff->error_id == crq->request_error_rsp.error_id) {
2754 found = true;
2755 list_del(&error_buff->list);
2756 break;
2757 }
2758 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
2759
2760 if (!found) {
2761 dev_err(dev, "Couldn't find error id %x\n",
Thomas Falcon75224c92017-02-15 10:33:33 -06002762 be32_to_cpu(crq->request_error_rsp.error_id));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002763 return;
2764 }
2765
2766 dev_err(dev, "Detailed info for error id %x:",
Thomas Falcon75224c92017-02-15 10:33:33 -06002767 be32_to_cpu(crq->request_error_rsp.error_id));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002768
2769 for (i = 0; i < error_buff->len; i++) {
2770 pr_cont("%02x", (int)error_buff->buff[i]);
2771 if (i % 8 == 7)
2772 pr_cont(" ");
2773 }
2774 pr_cont("\n");
2775
2776 dma_unmap_single(dev, error_buff->dma, error_buff->len,
2777 DMA_FROM_DEVICE);
2778 kfree(error_buff->buff);
2779 kfree(error_buff);
2780}
2781
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04002782static void request_error_information(struct ibmvnic_adapter *adapter,
2783 union ibmvnic_crq *err_crq)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002784{
Thomas Falcon032c5e82015-12-21 11:26:06 -06002785 struct device *dev = &adapter->vdev->dev;
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04002786 struct net_device *netdev = adapter->netdev;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002787 struct ibmvnic_error_buff *error_buff;
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04002788 unsigned long timeout = msecs_to_jiffies(30000);
2789 union ibmvnic_crq crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002790 unsigned long flags;
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04002791 int rc, detail_len;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002792
2793 error_buff = kmalloc(sizeof(*error_buff), GFP_ATOMIC);
2794 if (!error_buff)
2795 return;
2796
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04002797 detail_len = be32_to_cpu(err_crq->error_indication.detail_error_sz);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002798 error_buff->buff = kmalloc(detail_len, GFP_ATOMIC);
2799 if (!error_buff->buff) {
2800 kfree(error_buff);
2801 return;
2802 }
2803
2804 error_buff->dma = dma_map_single(dev, error_buff->buff, detail_len,
2805 DMA_FROM_DEVICE);
2806 if (dma_mapping_error(dev, error_buff->dma)) {
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04002807 netdev_err(netdev, "Couldn't map error buffer\n");
Thomas Falcon032c5e82015-12-21 11:26:06 -06002808 kfree(error_buff->buff);
2809 kfree(error_buff);
2810 return;
2811 }
2812
Thomas Falcon032c5e82015-12-21 11:26:06 -06002813 error_buff->len = detail_len;
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04002814 error_buff->error_id = err_crq->error_indication.error_id;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002815
2816 spin_lock_irqsave(&adapter->error_list_lock, flags);
2817 list_add_tail(&error_buff->list, &adapter->errors);
2818 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
2819
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04002820 memset(&crq, 0, sizeof(crq));
2821 crq.request_error_info.first = IBMVNIC_CRQ_CMD;
2822 crq.request_error_info.cmd = REQUEST_ERROR_INFO;
2823 crq.request_error_info.ioba = cpu_to_be32(error_buff->dma);
2824 crq.request_error_info.len = cpu_to_be32(detail_len);
2825 crq.request_error_info.error_id = err_crq->error_indication.error_id;
2826
2827 rc = ibmvnic_send_crq(adapter, &crq);
2828 if (rc) {
2829 netdev_err(netdev, "failed to request error information\n");
2830 goto err_info_fail;
2831 }
2832
2833 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
2834 netdev_err(netdev, "timeout waiting for error information\n");
2835 goto err_info_fail;
2836 }
2837
2838 return;
2839
2840err_info_fail:
2841 spin_lock_irqsave(&adapter->error_list_lock, flags);
2842 list_del(&error_buff->list);
2843 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
2844
2845 kfree(error_buff->buff);
2846 kfree(error_buff);
2847}
2848
2849static void handle_error_indication(union ibmvnic_crq *crq,
2850 struct ibmvnic_adapter *adapter)
2851{
2852 struct device *dev = &adapter->vdev->dev;
2853
2854 dev_err(dev, "Firmware reports %serror id %x, cause %d\n",
2855 crq->error_indication.flags
2856 & IBMVNIC_FATAL_ERROR ? "FATAL " : "",
2857 be32_to_cpu(crq->error_indication.error_id),
2858 be16_to_cpu(crq->error_indication.error_cause));
2859
2860 if (be32_to_cpu(crq->error_indication.error_id))
2861 request_error_information(adapter, crq);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002862
2863 if (crq->error_indication.flags & IBMVNIC_FATAL_ERROR)
2864 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
John Allen8cb31cf2017-05-26 10:30:37 -04002865 else
2866 ibmvnic_reset(adapter, VNIC_RESET_NON_FATAL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002867}
2868
2869static void handle_change_mac_rsp(union ibmvnic_crq *crq,
2870 struct ibmvnic_adapter *adapter)
2871{
2872 struct net_device *netdev = adapter->netdev;
2873 struct device *dev = &adapter->vdev->dev;
2874 long rc;
2875
2876 rc = crq->change_mac_addr_rsp.rc.code;
2877 if (rc) {
2878 dev_err(dev, "Error %ld in CHANGE_MAC_ADDR_RSP\n", rc);
2879 return;
2880 }
2881 memcpy(netdev->dev_addr, &crq->change_mac_addr_rsp.mac_addr[0],
2882 ETH_ALEN);
2883}
2884
2885static void handle_request_cap_rsp(union ibmvnic_crq *crq,
2886 struct ibmvnic_adapter *adapter)
2887{
2888 struct device *dev = &adapter->vdev->dev;
2889 u64 *req_value;
2890 char *name;
2891
Thomas Falcon901e0402017-02-15 12:17:59 -06002892 atomic_dec(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002893 switch (be16_to_cpu(crq->request_capability_rsp.capability)) {
2894 case REQ_TX_QUEUES:
2895 req_value = &adapter->req_tx_queues;
2896 name = "tx";
2897 break;
2898 case REQ_RX_QUEUES:
2899 req_value = &adapter->req_rx_queues;
2900 name = "rx";
2901 break;
2902 case REQ_RX_ADD_QUEUES:
2903 req_value = &adapter->req_rx_add_queues;
2904 name = "rx_add";
2905 break;
2906 case REQ_TX_ENTRIES_PER_SUBCRQ:
2907 req_value = &adapter->req_tx_entries_per_subcrq;
2908 name = "tx_entries_per_subcrq";
2909 break;
2910 case REQ_RX_ADD_ENTRIES_PER_SUBCRQ:
2911 req_value = &adapter->req_rx_add_entries_per_subcrq;
2912 name = "rx_add_entries_per_subcrq";
2913 break;
2914 case REQ_MTU:
2915 req_value = &adapter->req_mtu;
2916 name = "mtu";
2917 break;
2918 case PROMISC_REQUESTED:
2919 req_value = &adapter->promisc;
2920 name = "promisc";
2921 break;
2922 default:
2923 dev_err(dev, "Got invalid cap request rsp %d\n",
2924 crq->request_capability.capability);
2925 return;
2926 }
2927
2928 switch (crq->request_capability_rsp.rc.code) {
2929 case SUCCESS:
2930 break;
2931 case PARTIALSUCCESS:
2932 dev_info(dev, "req=%lld, rsp=%ld in %s queue, retrying.\n",
2933 *req_value,
Thomas Falcon28f4d162017-02-15 10:32:11 -06002934 (long int)be64_to_cpu(crq->request_capability_rsp.
Thomas Falcon032c5e82015-12-21 11:26:06 -06002935 number), name);
Nathan Fontenotb5108882017-03-30 02:49:18 -04002936 release_sub_crqs(adapter);
Thomas Falcon28f4d162017-02-15 10:32:11 -06002937 *req_value = be64_to_cpu(crq->request_capability_rsp.number);
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002938 ibmvnic_send_req_caps(adapter, 1);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002939 return;
2940 default:
2941 dev_err(dev, "Error %d in request cap rsp\n",
2942 crq->request_capability_rsp.rc.code);
2943 return;
2944 }
2945
2946 /* Done receiving requested capabilities, query IP offload support */
Thomas Falcon901e0402017-02-15 12:17:59 -06002947 if (atomic_read(&adapter->running_cap_crqs) == 0) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06002948 union ibmvnic_crq newcrq;
2949 int buf_sz = sizeof(struct ibmvnic_query_ip_offload_buffer);
2950 struct ibmvnic_query_ip_offload_buffer *ip_offload_buf =
2951 &adapter->ip_offload_buf;
2952
Thomas Falcon249168a2017-02-15 12:18:00 -06002953 adapter->wait_capability = false;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002954 adapter->ip_offload_tok = dma_map_single(dev, ip_offload_buf,
2955 buf_sz,
2956 DMA_FROM_DEVICE);
2957
2958 if (dma_mapping_error(dev, adapter->ip_offload_tok)) {
2959 if (!firmware_has_feature(FW_FEATURE_CMO))
2960 dev_err(dev, "Couldn't map offload buffer\n");
2961 return;
2962 }
2963
2964 memset(&newcrq, 0, sizeof(newcrq));
2965 newcrq.query_ip_offload.first = IBMVNIC_CRQ_CMD;
2966 newcrq.query_ip_offload.cmd = QUERY_IP_OFFLOAD;
2967 newcrq.query_ip_offload.len = cpu_to_be32(buf_sz);
2968 newcrq.query_ip_offload.ioba =
2969 cpu_to_be32(adapter->ip_offload_tok);
2970
2971 ibmvnic_send_crq(adapter, &newcrq);
2972 }
2973}
2974
2975static int handle_login_rsp(union ibmvnic_crq *login_rsp_crq,
2976 struct ibmvnic_adapter *adapter)
2977{
2978 struct device *dev = &adapter->vdev->dev;
2979 struct ibmvnic_login_rsp_buffer *login_rsp = adapter->login_rsp_buf;
2980 struct ibmvnic_login_buffer *login = adapter->login_buf;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002981 int i;
2982
2983 dma_unmap_single(dev, adapter->login_buf_token, adapter->login_buf_sz,
2984 DMA_BIDIRECTIONAL);
2985 dma_unmap_single(dev, adapter->login_rsp_buf_token,
2986 adapter->login_rsp_buf_sz, DMA_BIDIRECTIONAL);
2987
John Allen498cd8e2016-04-06 11:49:55 -05002988 /* If the number of queues requested can't be allocated by the
2989 * server, the login response will return with code 1. We will need
2990 * to resend the login buffer with fewer queues requested.
2991 */
2992 if (login_rsp_crq->generic.rc.code) {
2993 adapter->renegotiate = true;
2994 complete(&adapter->init_done);
2995 return 0;
2996 }
2997
Thomas Falcon032c5e82015-12-21 11:26:06 -06002998 netdev_dbg(adapter->netdev, "Login Response Buffer:\n");
2999 for (i = 0; i < (adapter->login_rsp_buf_sz - 1) / 8 + 1; i++) {
3000 netdev_dbg(adapter->netdev, "%016lx\n",
3001 ((unsigned long int *)(adapter->login_rsp_buf))[i]);
3002 }
3003
3004 /* Sanity checks */
3005 if (login->num_txcomp_subcrqs != login_rsp->num_txsubm_subcrqs ||
3006 (be32_to_cpu(login->num_rxcomp_subcrqs) *
3007 adapter->req_rx_add_queues !=
3008 be32_to_cpu(login_rsp->num_rxadd_subcrqs))) {
3009 dev_err(dev, "FATAL: Inconsistent login and login rsp\n");
3010 ibmvnic_remove(adapter->vdev);
3011 return -EIO;
3012 }
3013 complete(&adapter->init_done);
3014
Thomas Falcon032c5e82015-12-21 11:26:06 -06003015 return 0;
3016}
3017
3018static void handle_request_map_rsp(union ibmvnic_crq *crq,
3019 struct ibmvnic_adapter *adapter)
3020{
3021 struct device *dev = &adapter->vdev->dev;
3022 u8 map_id = crq->request_map_rsp.map_id;
3023 int tx_subcrqs;
3024 int rx_subcrqs;
3025 long rc;
3026 int i;
3027
3028 tx_subcrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
3029 rx_subcrqs = be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
3030
3031 rc = crq->request_map_rsp.rc.code;
3032 if (rc) {
3033 dev_err(dev, "Error %ld in REQUEST_MAP_RSP\n", rc);
3034 adapter->map_id--;
3035 /* need to find and zero tx/rx_pool map_id */
3036 for (i = 0; i < tx_subcrqs; i++) {
3037 if (adapter->tx_pool[i].long_term_buff.map_id == map_id)
3038 adapter->tx_pool[i].long_term_buff.map_id = 0;
3039 }
3040 for (i = 0; i < rx_subcrqs; i++) {
3041 if (adapter->rx_pool[i].long_term_buff.map_id == map_id)
3042 adapter->rx_pool[i].long_term_buff.map_id = 0;
3043 }
3044 }
3045 complete(&adapter->fw_done);
3046}
3047
3048static void handle_request_unmap_rsp(union ibmvnic_crq *crq,
3049 struct ibmvnic_adapter *adapter)
3050{
3051 struct device *dev = &adapter->vdev->dev;
3052 long rc;
3053
3054 rc = crq->request_unmap_rsp.rc.code;
3055 if (rc)
3056 dev_err(dev, "Error %ld in REQUEST_UNMAP_RSP\n", rc);
3057}
3058
3059static void handle_query_map_rsp(union ibmvnic_crq *crq,
3060 struct ibmvnic_adapter *adapter)
3061{
3062 struct net_device *netdev = adapter->netdev;
3063 struct device *dev = &adapter->vdev->dev;
3064 long rc;
3065
3066 rc = crq->query_map_rsp.rc.code;
3067 if (rc) {
3068 dev_err(dev, "Error %ld in QUERY_MAP_RSP\n", rc);
3069 return;
3070 }
3071 netdev_dbg(netdev, "page_size = %d\ntot_pages = %d\nfree_pages = %d\n",
3072 crq->query_map_rsp.page_size, crq->query_map_rsp.tot_pages,
3073 crq->query_map_rsp.free_pages);
3074}
3075
3076static void handle_query_cap_rsp(union ibmvnic_crq *crq,
3077 struct ibmvnic_adapter *adapter)
3078{
3079 struct net_device *netdev = adapter->netdev;
3080 struct device *dev = &adapter->vdev->dev;
3081 long rc;
3082
Thomas Falcon901e0402017-02-15 12:17:59 -06003083 atomic_dec(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003084 netdev_dbg(netdev, "Outstanding queries: %d\n",
Thomas Falcon901e0402017-02-15 12:17:59 -06003085 atomic_read(&adapter->running_cap_crqs));
Thomas Falcon032c5e82015-12-21 11:26:06 -06003086 rc = crq->query_capability.rc.code;
3087 if (rc) {
3088 dev_err(dev, "Error %ld in QUERY_CAP_RSP\n", rc);
3089 goto out;
3090 }
3091
3092 switch (be16_to_cpu(crq->query_capability.capability)) {
3093 case MIN_TX_QUEUES:
3094 adapter->min_tx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003095 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003096 netdev_dbg(netdev, "min_tx_queues = %lld\n",
3097 adapter->min_tx_queues);
3098 break;
3099 case MIN_RX_QUEUES:
3100 adapter->min_rx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003101 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003102 netdev_dbg(netdev, "min_rx_queues = %lld\n",
3103 adapter->min_rx_queues);
3104 break;
3105 case MIN_RX_ADD_QUEUES:
3106 adapter->min_rx_add_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003107 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003108 netdev_dbg(netdev, "min_rx_add_queues = %lld\n",
3109 adapter->min_rx_add_queues);
3110 break;
3111 case MAX_TX_QUEUES:
3112 adapter->max_tx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003113 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003114 netdev_dbg(netdev, "max_tx_queues = %lld\n",
3115 adapter->max_tx_queues);
3116 break;
3117 case MAX_RX_QUEUES:
3118 adapter->max_rx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003119 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003120 netdev_dbg(netdev, "max_rx_queues = %lld\n",
3121 adapter->max_rx_queues);
3122 break;
3123 case MAX_RX_ADD_QUEUES:
3124 adapter->max_rx_add_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003125 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003126 netdev_dbg(netdev, "max_rx_add_queues = %lld\n",
3127 adapter->max_rx_add_queues);
3128 break;
3129 case MIN_TX_ENTRIES_PER_SUBCRQ:
3130 adapter->min_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06003131 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003132 netdev_dbg(netdev, "min_tx_entries_per_subcrq = %lld\n",
3133 adapter->min_tx_entries_per_subcrq);
3134 break;
3135 case MIN_RX_ADD_ENTRIES_PER_SUBCRQ:
3136 adapter->min_rx_add_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06003137 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003138 netdev_dbg(netdev, "min_rx_add_entrs_per_subcrq = %lld\n",
3139 adapter->min_rx_add_entries_per_subcrq);
3140 break;
3141 case MAX_TX_ENTRIES_PER_SUBCRQ:
3142 adapter->max_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06003143 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003144 netdev_dbg(netdev, "max_tx_entries_per_subcrq = %lld\n",
3145 adapter->max_tx_entries_per_subcrq);
3146 break;
3147 case MAX_RX_ADD_ENTRIES_PER_SUBCRQ:
3148 adapter->max_rx_add_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06003149 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003150 netdev_dbg(netdev, "max_rx_add_entrs_per_subcrq = %lld\n",
3151 adapter->max_rx_add_entries_per_subcrq);
3152 break;
3153 case TCP_IP_OFFLOAD:
3154 adapter->tcp_ip_offload =
Thomas Falconde89e852016-03-01 10:20:09 -06003155 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003156 netdev_dbg(netdev, "tcp_ip_offload = %lld\n",
3157 adapter->tcp_ip_offload);
3158 break;
3159 case PROMISC_SUPPORTED:
3160 adapter->promisc_supported =
Thomas Falconde89e852016-03-01 10:20:09 -06003161 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003162 netdev_dbg(netdev, "promisc_supported = %lld\n",
3163 adapter->promisc_supported);
3164 break;
3165 case MIN_MTU:
Thomas Falconde89e852016-03-01 10:20:09 -06003166 adapter->min_mtu = be64_to_cpu(crq->query_capability.number);
Thomas Falconf39f0d12017-02-14 10:22:59 -06003167 netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003168 netdev_dbg(netdev, "min_mtu = %lld\n", adapter->min_mtu);
3169 break;
3170 case MAX_MTU:
Thomas Falconde89e852016-03-01 10:20:09 -06003171 adapter->max_mtu = be64_to_cpu(crq->query_capability.number);
Thomas Falconf39f0d12017-02-14 10:22:59 -06003172 netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003173 netdev_dbg(netdev, "max_mtu = %lld\n", adapter->max_mtu);
3174 break;
3175 case MAX_MULTICAST_FILTERS:
3176 adapter->max_multicast_filters =
Thomas Falconde89e852016-03-01 10:20:09 -06003177 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003178 netdev_dbg(netdev, "max_multicast_filters = %lld\n",
3179 adapter->max_multicast_filters);
3180 break;
3181 case VLAN_HEADER_INSERTION:
3182 adapter->vlan_header_insertion =
Thomas Falconde89e852016-03-01 10:20:09 -06003183 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003184 if (adapter->vlan_header_insertion)
3185 netdev->features |= NETIF_F_HW_VLAN_STAG_TX;
3186 netdev_dbg(netdev, "vlan_header_insertion = %lld\n",
3187 adapter->vlan_header_insertion);
3188 break;
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04003189 case RX_VLAN_HEADER_INSERTION:
3190 adapter->rx_vlan_header_insertion =
3191 be64_to_cpu(crq->query_capability.number);
3192 netdev_dbg(netdev, "rx_vlan_header_insertion = %lld\n",
3193 adapter->rx_vlan_header_insertion);
3194 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003195 case MAX_TX_SG_ENTRIES:
3196 adapter->max_tx_sg_entries =
Thomas Falconde89e852016-03-01 10:20:09 -06003197 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003198 netdev_dbg(netdev, "max_tx_sg_entries = %lld\n",
3199 adapter->max_tx_sg_entries);
3200 break;
3201 case RX_SG_SUPPORTED:
3202 adapter->rx_sg_supported =
Thomas Falconde89e852016-03-01 10:20:09 -06003203 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003204 netdev_dbg(netdev, "rx_sg_supported = %lld\n",
3205 adapter->rx_sg_supported);
3206 break;
3207 case OPT_TX_COMP_SUB_QUEUES:
3208 adapter->opt_tx_comp_sub_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003209 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003210 netdev_dbg(netdev, "opt_tx_comp_sub_queues = %lld\n",
3211 adapter->opt_tx_comp_sub_queues);
3212 break;
3213 case OPT_RX_COMP_QUEUES:
3214 adapter->opt_rx_comp_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003215 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003216 netdev_dbg(netdev, "opt_rx_comp_queues = %lld\n",
3217 adapter->opt_rx_comp_queues);
3218 break;
3219 case OPT_RX_BUFADD_Q_PER_RX_COMP_Q:
3220 adapter->opt_rx_bufadd_q_per_rx_comp_q =
Thomas Falconde89e852016-03-01 10:20:09 -06003221 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003222 netdev_dbg(netdev, "opt_rx_bufadd_q_per_rx_comp_q = %lld\n",
3223 adapter->opt_rx_bufadd_q_per_rx_comp_q);
3224 break;
3225 case OPT_TX_ENTRIES_PER_SUBCRQ:
3226 adapter->opt_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06003227 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003228 netdev_dbg(netdev, "opt_tx_entries_per_subcrq = %lld\n",
3229 adapter->opt_tx_entries_per_subcrq);
3230 break;
3231 case OPT_RXBA_ENTRIES_PER_SUBCRQ:
3232 adapter->opt_rxba_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06003233 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003234 netdev_dbg(netdev, "opt_rxba_entries_per_subcrq = %lld\n",
3235 adapter->opt_rxba_entries_per_subcrq);
3236 break;
3237 case TX_RX_DESC_REQ:
3238 adapter->tx_rx_desc_req = crq->query_capability.number;
3239 netdev_dbg(netdev, "tx_rx_desc_req = %llx\n",
3240 adapter->tx_rx_desc_req);
3241 break;
3242
3243 default:
3244 netdev_err(netdev, "Got invalid cap rsp %d\n",
3245 crq->query_capability.capability);
3246 }
3247
3248out:
Thomas Falcon249168a2017-02-15 12:18:00 -06003249 if (atomic_read(&adapter->running_cap_crqs) == 0) {
3250 adapter->wait_capability = false;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003251 ibmvnic_send_req_caps(adapter, 0);
Thomas Falcon249168a2017-02-15 12:18:00 -06003252 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06003253}
3254
Thomas Falcon032c5e82015-12-21 11:26:06 -06003255static void ibmvnic_handle_crq(union ibmvnic_crq *crq,
3256 struct ibmvnic_adapter *adapter)
3257{
3258 struct ibmvnic_generic_crq *gen_crq = &crq->generic;
3259 struct net_device *netdev = adapter->netdev;
3260 struct device *dev = &adapter->vdev->dev;
Murilo Fossa Vicentini993a82b2017-04-19 13:44:35 -04003261 u64 *u64_crq = (u64 *)crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003262 long rc;
3263
3264 netdev_dbg(netdev, "Handling CRQ: %016lx %016lx\n",
Murilo Fossa Vicentini993a82b2017-04-19 13:44:35 -04003265 (unsigned long int)cpu_to_be64(u64_crq[0]),
3266 (unsigned long int)cpu_to_be64(u64_crq[1]));
Thomas Falcon032c5e82015-12-21 11:26:06 -06003267 switch (gen_crq->first) {
3268 case IBMVNIC_CRQ_INIT_RSP:
3269 switch (gen_crq->cmd) {
3270 case IBMVNIC_CRQ_INIT:
3271 dev_info(dev, "Partner initialized\n");
John Allen017892c12017-05-26 10:30:19 -04003272 adapter->from_passive_init = true;
3273 complete(&adapter->init_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003274 break;
3275 case IBMVNIC_CRQ_INIT_COMPLETE:
3276 dev_info(dev, "Partner initialization complete\n");
3277 send_version_xchg(adapter);
3278 break;
3279 default:
3280 dev_err(dev, "Unknown crq cmd: %d\n", gen_crq->cmd);
3281 }
3282 return;
3283 case IBMVNIC_CRQ_XPORT_EVENT:
Nathan Fontenoted651a12017-05-03 14:04:38 -04003284 netif_carrier_off(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003285 if (gen_crq->cmd == IBMVNIC_PARTITION_MIGRATED) {
Nathan Fontenoted651a12017-05-03 14:04:38 -04003286 dev_info(dev, "Migrated, re-enabling adapter\n");
3287 ibmvnic_reset(adapter, VNIC_RESET_MOBILITY);
Thomas Falcondfad09a2016-08-18 11:37:51 -05003288 } else if (gen_crq->cmd == IBMVNIC_DEVICE_FAILOVER) {
3289 dev_info(dev, "Backing device failover detected\n");
Nathan Fontenoted651a12017-05-03 14:04:38 -04003290 ibmvnic_reset(adapter, VNIC_RESET_FAILOVER);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003291 } else {
3292 /* The adapter lost the connection */
3293 dev_err(dev, "Virtual Adapter failed (rc=%d)\n",
3294 gen_crq->cmd);
Nathan Fontenoted651a12017-05-03 14:04:38 -04003295 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003296 }
3297 return;
3298 case IBMVNIC_CRQ_CMD_RSP:
3299 break;
3300 default:
3301 dev_err(dev, "Got an invalid msg type 0x%02x\n",
3302 gen_crq->first);
3303 return;
3304 }
3305
3306 switch (gen_crq->cmd) {
3307 case VERSION_EXCHANGE_RSP:
3308 rc = crq->version_exchange_rsp.rc.code;
3309 if (rc) {
3310 dev_err(dev, "Error %ld in VERSION_EXCHG_RSP\n", rc);
3311 break;
3312 }
3313 dev_info(dev, "Partner protocol version is %d\n",
3314 crq->version_exchange_rsp.version);
3315 if (be16_to_cpu(crq->version_exchange_rsp.version) <
3316 ibmvnic_version)
3317 ibmvnic_version =
3318 be16_to_cpu(crq->version_exchange_rsp.version);
3319 send_cap_queries(adapter);
3320 break;
3321 case QUERY_CAPABILITY_RSP:
3322 handle_query_cap_rsp(crq, adapter);
3323 break;
3324 case QUERY_MAP_RSP:
3325 handle_query_map_rsp(crq, adapter);
3326 break;
3327 case REQUEST_MAP_RSP:
3328 handle_request_map_rsp(crq, adapter);
3329 break;
3330 case REQUEST_UNMAP_RSP:
3331 handle_request_unmap_rsp(crq, adapter);
3332 break;
3333 case REQUEST_CAPABILITY_RSP:
3334 handle_request_cap_rsp(crq, adapter);
3335 break;
3336 case LOGIN_RSP:
3337 netdev_dbg(netdev, "Got Login Response\n");
3338 handle_login_rsp(crq, adapter);
3339 break;
3340 case LOGICAL_LINK_STATE_RSP:
Nathan Fontenot53da09e2017-04-21 15:39:04 -04003341 netdev_dbg(netdev,
3342 "Got Logical Link State Response, state: %d rc: %d\n",
3343 crq->logical_link_state_rsp.link_state,
3344 crq->logical_link_state_rsp.rc.code);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003345 adapter->logical_link_state =
3346 crq->logical_link_state_rsp.link_state;
Nathan Fontenot53da09e2017-04-21 15:39:04 -04003347 adapter->init_done_rc = crq->logical_link_state_rsp.rc.code;
3348 complete(&adapter->init_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003349 break;
3350 case LINK_STATE_INDICATION:
3351 netdev_dbg(netdev, "Got Logical Link State Indication\n");
3352 adapter->phys_link_state =
3353 crq->link_state_indication.phys_link_state;
3354 adapter->logical_link_state =
3355 crq->link_state_indication.logical_link_state;
3356 break;
3357 case CHANGE_MAC_ADDR_RSP:
3358 netdev_dbg(netdev, "Got MAC address change Response\n");
3359 handle_change_mac_rsp(crq, adapter);
3360 break;
3361 case ERROR_INDICATION:
3362 netdev_dbg(netdev, "Got Error Indication\n");
3363 handle_error_indication(crq, adapter);
3364 break;
3365 case REQUEST_ERROR_RSP:
3366 netdev_dbg(netdev, "Got Error Detail Response\n");
3367 handle_error_info_rsp(crq, adapter);
3368 break;
3369 case REQUEST_STATISTICS_RSP:
3370 netdev_dbg(netdev, "Got Statistics Response\n");
3371 complete(&adapter->stats_done);
3372 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003373 case QUERY_IP_OFFLOAD_RSP:
3374 netdev_dbg(netdev, "Got Query IP offload Response\n");
3375 handle_query_ip_offload_rsp(adapter);
3376 break;
3377 case MULTICAST_CTRL_RSP:
3378 netdev_dbg(netdev, "Got multicast control Response\n");
3379 break;
3380 case CONTROL_IP_OFFLOAD_RSP:
3381 netdev_dbg(netdev, "Got Control IP offload Response\n");
3382 dma_unmap_single(dev, adapter->ip_offload_ctrl_tok,
3383 sizeof(adapter->ip_offload_ctrl),
3384 DMA_TO_DEVICE);
John Allenbd0b6722017-03-17 17:13:40 -05003385 complete(&adapter->init_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003386 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003387 case COLLECT_FW_TRACE_RSP:
3388 netdev_dbg(netdev, "Got Collect firmware trace Response\n");
3389 complete(&adapter->fw_done);
3390 break;
3391 default:
3392 netdev_err(netdev, "Got an invalid cmd type 0x%02x\n",
3393 gen_crq->cmd);
3394 }
3395}
3396
3397static irqreturn_t ibmvnic_interrupt(int irq, void *instance)
3398{
3399 struct ibmvnic_adapter *adapter = instance;
Thomas Falcon6c267b32017-02-15 12:17:58 -06003400
Thomas Falcon6c267b32017-02-15 12:17:58 -06003401 tasklet_schedule(&adapter->tasklet);
Thomas Falcon6c267b32017-02-15 12:17:58 -06003402 return IRQ_HANDLED;
3403}
3404
3405static void ibmvnic_tasklet(void *data)
3406{
3407 struct ibmvnic_adapter *adapter = data;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003408 struct ibmvnic_crq_queue *queue = &adapter->crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003409 union ibmvnic_crq *crq;
3410 unsigned long flags;
3411 bool done = false;
3412
3413 spin_lock_irqsave(&queue->lock, flags);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003414 while (!done) {
3415 /* Pull all the valid messages off the CRQ */
3416 while ((crq = ibmvnic_next_crq(adapter)) != NULL) {
3417 ibmvnic_handle_crq(crq, adapter);
3418 crq->generic.first = 0;
3419 }
Brian Kinged7ecbf2017-04-19 13:44:53 -04003420
3421 /* remain in tasklet until all
3422 * capabilities responses are received
3423 */
3424 if (!adapter->wait_capability)
3425 done = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003426 }
Thomas Falcon249168a2017-02-15 12:18:00 -06003427 /* if capabilities CRQ's were sent in this tasklet, the following
3428 * tasklet must wait until all responses are received
3429 */
3430 if (atomic_read(&adapter->running_cap_crqs) != 0)
3431 adapter->wait_capability = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003432 spin_unlock_irqrestore(&queue->lock, flags);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003433}
3434
3435static int ibmvnic_reenable_crq_queue(struct ibmvnic_adapter *adapter)
3436{
3437 struct vio_dev *vdev = adapter->vdev;
3438 int rc;
3439
3440 do {
3441 rc = plpar_hcall_norets(H_ENABLE_CRQ, vdev->unit_address);
3442 } while (rc == H_IN_PROGRESS || rc == H_BUSY || H_IS_LONG_BUSY(rc));
3443
3444 if (rc)
3445 dev_err(&vdev->dev, "Error enabling adapter (rc=%d)\n", rc);
3446
3447 return rc;
3448}
3449
3450static int ibmvnic_reset_crq(struct ibmvnic_adapter *adapter)
3451{
3452 struct ibmvnic_crq_queue *crq = &adapter->crq;
3453 struct device *dev = &adapter->vdev->dev;
3454 struct vio_dev *vdev = adapter->vdev;
3455 int rc;
3456
3457 /* Close the CRQ */
3458 do {
3459 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
3460 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
3461
3462 /* Clean out the queue */
3463 memset(crq->msgs, 0, PAGE_SIZE);
3464 crq->cur = 0;
3465
3466 /* And re-open it again */
3467 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
3468 crq->msg_token, PAGE_SIZE);
3469
3470 if (rc == H_CLOSED)
3471 /* Adapter is good, but other end is not ready */
3472 dev_warn(dev, "Partner adapter not ready\n");
3473 else if (rc != 0)
3474 dev_warn(dev, "Couldn't register crq (rc=%d)\n", rc);
3475
3476 return rc;
3477}
3478
Nathan Fontenotf9928872017-03-30 02:48:54 -04003479static void release_crq_queue(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003480{
3481 struct ibmvnic_crq_queue *crq = &adapter->crq;
3482 struct vio_dev *vdev = adapter->vdev;
3483 long rc;
3484
Nathan Fontenotf9928872017-03-30 02:48:54 -04003485 if (!crq->msgs)
3486 return;
3487
Thomas Falcon032c5e82015-12-21 11:26:06 -06003488 netdev_dbg(adapter->netdev, "Releasing CRQ\n");
3489 free_irq(vdev->irq, adapter);
Thomas Falcon6c267b32017-02-15 12:17:58 -06003490 tasklet_kill(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003491 do {
3492 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
3493 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
3494
3495 dma_unmap_single(&vdev->dev, crq->msg_token, PAGE_SIZE,
3496 DMA_BIDIRECTIONAL);
3497 free_page((unsigned long)crq->msgs);
Nathan Fontenotf9928872017-03-30 02:48:54 -04003498 crq->msgs = NULL;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003499}
3500
Nathan Fontenotf9928872017-03-30 02:48:54 -04003501static int init_crq_queue(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003502{
3503 struct ibmvnic_crq_queue *crq = &adapter->crq;
3504 struct device *dev = &adapter->vdev->dev;
3505 struct vio_dev *vdev = adapter->vdev;
3506 int rc, retrc = -ENOMEM;
3507
Nathan Fontenotf9928872017-03-30 02:48:54 -04003508 if (crq->msgs)
3509 return 0;
3510
Thomas Falcon032c5e82015-12-21 11:26:06 -06003511 crq->msgs = (union ibmvnic_crq *)get_zeroed_page(GFP_KERNEL);
3512 /* Should we allocate more than one page? */
3513
3514 if (!crq->msgs)
3515 return -ENOMEM;
3516
3517 crq->size = PAGE_SIZE / sizeof(*crq->msgs);
3518 crq->msg_token = dma_map_single(dev, crq->msgs, PAGE_SIZE,
3519 DMA_BIDIRECTIONAL);
3520 if (dma_mapping_error(dev, crq->msg_token))
3521 goto map_failed;
3522
3523 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
3524 crq->msg_token, PAGE_SIZE);
3525
3526 if (rc == H_RESOURCE)
3527 /* maybe kexecing and resource is busy. try a reset */
3528 rc = ibmvnic_reset_crq(adapter);
3529 retrc = rc;
3530
3531 if (rc == H_CLOSED) {
3532 dev_warn(dev, "Partner adapter not ready\n");
3533 } else if (rc) {
3534 dev_warn(dev, "Error %d opening adapter\n", rc);
3535 goto reg_crq_failed;
3536 }
3537
3538 retrc = 0;
3539
Thomas Falcon6c267b32017-02-15 12:17:58 -06003540 tasklet_init(&adapter->tasklet, (void *)ibmvnic_tasklet,
3541 (unsigned long)adapter);
3542
Thomas Falcon032c5e82015-12-21 11:26:06 -06003543 netdev_dbg(adapter->netdev, "registering irq 0x%x\n", vdev->irq);
3544 rc = request_irq(vdev->irq, ibmvnic_interrupt, 0, IBMVNIC_NAME,
3545 adapter);
3546 if (rc) {
3547 dev_err(dev, "Couldn't register irq 0x%x. rc=%d\n",
3548 vdev->irq, rc);
3549 goto req_irq_failed;
3550 }
3551
3552 rc = vio_enable_interrupts(vdev);
3553 if (rc) {
3554 dev_err(dev, "Error %d enabling interrupts\n", rc);
3555 goto req_irq_failed;
3556 }
3557
3558 crq->cur = 0;
3559 spin_lock_init(&crq->lock);
3560
3561 return retrc;
3562
3563req_irq_failed:
Thomas Falcon6c267b32017-02-15 12:17:58 -06003564 tasklet_kill(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003565 do {
3566 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
3567 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
3568reg_crq_failed:
3569 dma_unmap_single(dev, crq->msg_token, PAGE_SIZE, DMA_BIDIRECTIONAL);
3570map_failed:
3571 free_page((unsigned long)crq->msgs);
Nathan Fontenotf9928872017-03-30 02:48:54 -04003572 crq->msgs = NULL;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003573 return retrc;
3574}
3575
John Allenf6ef6402017-03-17 17:13:42 -05003576static int ibmvnic_init(struct ibmvnic_adapter *adapter)
3577{
3578 struct device *dev = &adapter->vdev->dev;
3579 unsigned long timeout = msecs_to_jiffies(30000);
John Allenf6ef6402017-03-17 17:13:42 -05003580 int rc;
3581
Nathan Fontenot28cde752017-05-26 10:31:00 -04003582 if (adapter->resetting) {
3583 rc = ibmvnic_reset_crq(adapter);
3584 if (!rc)
3585 rc = vio_enable_interrupts(adapter->vdev);
3586 } else {
3587 rc = init_crq_queue(adapter);
3588 }
3589
John Allenf6ef6402017-03-17 17:13:42 -05003590 if (rc) {
3591 dev_err(dev, "Couldn't initialize crq. rc=%d\n", rc);
3592 return rc;
3593 }
3594
John Allen017892c12017-05-26 10:30:19 -04003595 adapter->from_passive_init = false;
3596
John Allenf6ef6402017-03-17 17:13:42 -05003597 init_completion(&adapter->init_done);
3598 ibmvnic_send_crq_init(adapter);
3599 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
3600 dev_err(dev, "Initialization sequence timed out\n");
John Allen017892c12017-05-26 10:30:19 -04003601 return -1;
3602 }
3603
3604 if (adapter->from_passive_init) {
3605 adapter->state = VNIC_OPEN;
3606 adapter->from_passive_init = false;
John Allenf6ef6402017-03-17 17:13:42 -05003607 return -1;
3608 }
3609
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04003610 rc = init_sub_crqs(adapter);
3611 if (rc) {
3612 dev_err(dev, "Initialization of sub crqs failed\n");
3613 release_crq_queue(adapter);
3614 }
3615
3616 return rc;
John Allenf6ef6402017-03-17 17:13:42 -05003617}
3618
Thomas Falcon032c5e82015-12-21 11:26:06 -06003619static int ibmvnic_probe(struct vio_dev *dev, const struct vio_device_id *id)
3620{
3621 struct ibmvnic_adapter *adapter;
3622 struct net_device *netdev;
3623 unsigned char *mac_addr_p;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003624 int rc;
3625
3626 dev_dbg(&dev->dev, "entering ibmvnic_probe for UA 0x%x\n",
3627 dev->unit_address);
3628
3629 mac_addr_p = (unsigned char *)vio_get_attribute(dev,
3630 VETH_MAC_ADDR, NULL);
3631 if (!mac_addr_p) {
3632 dev_err(&dev->dev,
3633 "(%s:%3.3d) ERROR: Can't find MAC_ADDR attribute\n",
3634 __FILE__, __LINE__);
3635 return 0;
3636 }
3637
3638 netdev = alloc_etherdev_mq(sizeof(struct ibmvnic_adapter),
3639 IBMVNIC_MAX_TX_QUEUES);
3640 if (!netdev)
3641 return -ENOMEM;
3642
3643 adapter = netdev_priv(netdev);
Nathan Fontenot90c80142017-05-03 14:04:32 -04003644 adapter->state = VNIC_PROBING;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003645 dev_set_drvdata(&dev->dev, netdev);
3646 adapter->vdev = dev;
3647 adapter->netdev = netdev;
3648
3649 ether_addr_copy(adapter->mac_addr, mac_addr_p);
3650 ether_addr_copy(netdev->dev_addr, adapter->mac_addr);
3651 netdev->irq = dev->irq;
3652 netdev->netdev_ops = &ibmvnic_netdev_ops;
3653 netdev->ethtool_ops = &ibmvnic_ethtool_ops;
3654 SET_NETDEV_DEV(netdev, &dev->dev);
3655
3656 spin_lock_init(&adapter->stats_lock);
3657
Thomas Falcon032c5e82015-12-21 11:26:06 -06003658 INIT_LIST_HEAD(&adapter->errors);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003659 spin_lock_init(&adapter->error_list_lock);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003660
Nathan Fontenoted651a12017-05-03 14:04:38 -04003661 INIT_WORK(&adapter->ibmvnic_reset, __ibmvnic_reset);
3662 INIT_LIST_HEAD(&adapter->rwi_list);
3663 mutex_init(&adapter->reset_lock);
3664 mutex_init(&adapter->rwi_lock);
3665 adapter->resetting = false;
3666
John Allenf6ef6402017-03-17 17:13:42 -05003667 rc = ibmvnic_init(adapter);
3668 if (rc) {
3669 free_netdev(netdev);
3670 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003671 }
3672
Thomas Falconf39f0d12017-02-14 10:22:59 -06003673 netdev->mtu = adapter->req_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003674
3675 rc = register_netdev(netdev);
3676 if (rc) {
3677 dev_err(&dev->dev, "failed to register netdev rc=%d\n", rc);
John Allenf6ef6402017-03-17 17:13:42 -05003678 free_netdev(netdev);
3679 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003680 }
3681 dev_info(&dev->dev, "ibmvnic registered\n");
3682
Nathan Fontenot90c80142017-05-03 14:04:32 -04003683 adapter->state = VNIC_PROBED;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003684 return 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003685}
3686
3687static int ibmvnic_remove(struct vio_dev *dev)
3688{
3689 struct net_device *netdev = dev_get_drvdata(&dev->dev);
Nathan Fontenot37489052017-04-19 13:45:04 -04003690 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003691
Nathan Fontenot90c80142017-05-03 14:04:32 -04003692 adapter->state = VNIC_REMOVING;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003693 unregister_netdev(netdev);
Nathan Fontenoted651a12017-05-03 14:04:38 -04003694 mutex_lock(&adapter->reset_lock);
Nathan Fontenot37489052017-04-19 13:45:04 -04003695
3696 release_resources(adapter);
3697 release_sub_crqs(adapter);
3698 release_crq_queue(adapter);
3699
Nathan Fontenot90c80142017-05-03 14:04:32 -04003700 adapter->state = VNIC_REMOVED;
3701
Nathan Fontenoted651a12017-05-03 14:04:38 -04003702 mutex_unlock(&adapter->reset_lock);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003703 free_netdev(netdev);
3704 dev_set_drvdata(&dev->dev, NULL);
3705
3706 return 0;
3707}
3708
3709static unsigned long ibmvnic_get_desired_dma(struct vio_dev *vdev)
3710{
3711 struct net_device *netdev = dev_get_drvdata(&vdev->dev);
3712 struct ibmvnic_adapter *adapter;
3713 struct iommu_table *tbl;
3714 unsigned long ret = 0;
3715 int i;
3716
3717 tbl = get_iommu_table_base(&vdev->dev);
3718
3719 /* netdev inits at probe time along with the structures we need below*/
3720 if (!netdev)
3721 return IOMMU_PAGE_ALIGN(IBMVNIC_IO_ENTITLEMENT_DEFAULT, tbl);
3722
3723 adapter = netdev_priv(netdev);
3724
3725 ret += PAGE_SIZE; /* the crq message queue */
Thomas Falcon032c5e82015-12-21 11:26:06 -06003726 ret += IOMMU_PAGE_ALIGN(sizeof(struct ibmvnic_statistics), tbl);
3727
3728 for (i = 0; i < adapter->req_tx_queues + adapter->req_rx_queues; i++)
3729 ret += 4 * PAGE_SIZE; /* the scrq message queue */
3730
3731 for (i = 0; i < be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
3732 i++)
3733 ret += adapter->rx_pool[i].size *
3734 IOMMU_PAGE_ALIGN(adapter->rx_pool[i].buff_size, tbl);
3735
3736 return ret;
3737}
3738
3739static int ibmvnic_resume(struct device *dev)
3740{
3741 struct net_device *netdev = dev_get_drvdata(dev);
3742 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
3743 int i;
3744
3745 /* kick the interrupt handlers just in case we lost an interrupt */
3746 for (i = 0; i < adapter->req_rx_queues; i++)
3747 ibmvnic_interrupt_rx(adapter->rx_scrq[i]->irq,
3748 adapter->rx_scrq[i]);
3749
3750 return 0;
3751}
3752
3753static struct vio_device_id ibmvnic_device_table[] = {
3754 {"network", "IBM,vnic"},
3755 {"", "" }
3756};
3757MODULE_DEVICE_TABLE(vio, ibmvnic_device_table);
3758
3759static const struct dev_pm_ops ibmvnic_pm_ops = {
3760 .resume = ibmvnic_resume
3761};
3762
3763static struct vio_driver ibmvnic_driver = {
3764 .id_table = ibmvnic_device_table,
3765 .probe = ibmvnic_probe,
3766 .remove = ibmvnic_remove,
3767 .get_desired_dma = ibmvnic_get_desired_dma,
3768 .name = ibmvnic_driver_name,
3769 .pm = &ibmvnic_pm_ops,
3770};
3771
3772/* module functions */
3773static int __init ibmvnic_module_init(void)
3774{
3775 pr_info("%s: %s %s\n", ibmvnic_driver_name, ibmvnic_driver_string,
3776 IBMVNIC_DRIVER_VERSION);
3777
3778 return vio_register_driver(&ibmvnic_driver);
3779}
3780
3781static void __exit ibmvnic_module_exit(void)
3782{
3783 vio_unregister_driver(&ibmvnic_driver);
3784}
3785
3786module_init(ibmvnic_module_init);
3787module_exit(ibmvnic_module_exit);