blob: b9b0c693ce0119f7936a87c4c13f41fc7aa02562 [file] [log] [blame]
Thomas Falcon032c5e82015-12-21 11:26:06 -06001/**************************************************************************/
2/* */
3/* IBM System i and System p Virtual NIC Device Driver */
4/* Copyright (C) 2014 IBM Corp. */
5/* Santiago Leon (santi_leon@yahoo.com) */
6/* Thomas Falcon (tlfalcon@linux.vnet.ibm.com) */
7/* John Allen (jallen@linux.vnet.ibm.com) */
8/* */
9/* This program is free software; you can redistribute it and/or modify */
10/* it under the terms of the GNU General Public License as published by */
11/* the Free Software Foundation; either version 2 of the License, or */
12/* (at your option) any later version. */
13/* */
14/* This program is distributed in the hope that it will be useful, */
15/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
16/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
17/* GNU General Public License for more details. */
18/* */
19/* You should have received a copy of the GNU General Public License */
20/* along with this program. */
21/* */
22/* This module contains the implementation of a virtual ethernet device */
23/* for use with IBM i/p Series LPAR Linux. It utilizes the logical LAN */
24/* option of the RS/6000 Platform Architecture to interface with virtual */
25/* ethernet NICs that are presented to the partition by the hypervisor. */
26/* */
27/* Messages are passed between the VNIC driver and the VNIC server using */
28/* Command/Response Queues (CRQs) and sub CRQs (sCRQs). CRQs are used to */
29/* issue and receive commands that initiate communication with the server */
30/* on driver initialization. Sub CRQs (sCRQs) are similar to CRQs, but */
31/* are used by the driver to notify the server that a packet is */
32/* ready for transmission or that a buffer has been added to receive a */
33/* packet. Subsequently, sCRQs are used by the server to notify the */
34/* driver that a packet transmission has been completed or that a packet */
35/* has been received and placed in a waiting buffer. */
36/* */
37/* In lieu of a more conventional "on-the-fly" DMA mapping strategy in */
38/* which skbs are DMA mapped and immediately unmapped when the transmit */
39/* or receive has been completed, the VNIC driver is required to use */
40/* "long term mapping". This entails that large, continuous DMA mapped */
41/* buffers are allocated on driver initialization and these buffers are */
42/* then continuously reused to pass skbs to and from the VNIC server. */
43/* */
44/**************************************************************************/
45
46#include <linux/module.h>
47#include <linux/moduleparam.h>
48#include <linux/types.h>
49#include <linux/errno.h>
50#include <linux/completion.h>
51#include <linux/ioport.h>
52#include <linux/dma-mapping.h>
53#include <linux/kernel.h>
54#include <linux/netdevice.h>
55#include <linux/etherdevice.h>
56#include <linux/skbuff.h>
57#include <linux/init.h>
58#include <linux/delay.h>
59#include <linux/mm.h>
60#include <linux/ethtool.h>
61#include <linux/proc_fs.h>
62#include <linux/in.h>
63#include <linux/ip.h>
Thomas Falconad7775d2016-04-01 17:20:34 -050064#include <linux/ipv6.h>
Thomas Falcon032c5e82015-12-21 11:26:06 -060065#include <linux/irq.h>
66#include <linux/kthread.h>
67#include <linux/seq_file.h>
Thomas Falcon032c5e82015-12-21 11:26:06 -060068#include <linux/interrupt.h>
69#include <net/net_namespace.h>
70#include <asm/hvcall.h>
71#include <linux/atomic.h>
72#include <asm/vio.h>
73#include <asm/iommu.h>
74#include <linux/uaccess.h>
75#include <asm/firmware.h>
Thomas Falcon65dc6892016-07-06 15:35:18 -050076#include <linux/workqueue.h>
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -040077#include <linux/if_vlan.h>
Thomas Falcon032c5e82015-12-21 11:26:06 -060078
79#include "ibmvnic.h"
80
81static const char ibmvnic_driver_name[] = "ibmvnic";
82static const char ibmvnic_driver_string[] = "IBM System i/p Virtual NIC Driver";
83
84MODULE_AUTHOR("Santiago Leon <santi_leon@yahoo.com>");
85MODULE_DESCRIPTION("IBM System i/p Virtual NIC Driver");
86MODULE_LICENSE("GPL");
87MODULE_VERSION(IBMVNIC_DRIVER_VERSION);
88
89static int ibmvnic_version = IBMVNIC_INITIAL_VERSION;
90static int ibmvnic_remove(struct vio_dev *);
91static void release_sub_crqs(struct ibmvnic_adapter *);
92static int ibmvnic_reset_crq(struct ibmvnic_adapter *);
93static int ibmvnic_send_crq_init(struct ibmvnic_adapter *);
94static int ibmvnic_reenable_crq_queue(struct ibmvnic_adapter *);
95static int ibmvnic_send_crq(struct ibmvnic_adapter *, union ibmvnic_crq *);
96static int send_subcrq(struct ibmvnic_adapter *adapter, u64 remote_handle,
97 union sub_crq *sub_crq);
Thomas Falconad7775d2016-04-01 17:20:34 -050098static int send_subcrq_indirect(struct ibmvnic_adapter *, u64, u64, u64);
Thomas Falcon032c5e82015-12-21 11:26:06 -060099static irqreturn_t ibmvnic_interrupt_rx(int irq, void *instance);
100static int enable_scrq_irq(struct ibmvnic_adapter *,
101 struct ibmvnic_sub_crq_queue *);
102static int disable_scrq_irq(struct ibmvnic_adapter *,
103 struct ibmvnic_sub_crq_queue *);
104static int pending_scrq(struct ibmvnic_adapter *,
105 struct ibmvnic_sub_crq_queue *);
106static union sub_crq *ibmvnic_next_scrq(struct ibmvnic_adapter *,
107 struct ibmvnic_sub_crq_queue *);
108static int ibmvnic_poll(struct napi_struct *napi, int data);
109static void send_map_query(struct ibmvnic_adapter *adapter);
110static void send_request_map(struct ibmvnic_adapter *, dma_addr_t, __be32, u8);
111static void send_request_unmap(struct ibmvnic_adapter *, u8);
John Allenbd0b6722017-03-17 17:13:40 -0500112static void send_login(struct ibmvnic_adapter *adapter);
113static void send_cap_queries(struct ibmvnic_adapter *adapter);
114static int init_sub_crq_irqs(struct ibmvnic_adapter *adapter);
John Allenea5509f2017-03-17 17:13:43 -0500115static int ibmvnic_init(struct ibmvnic_adapter *);
Nathan Fontenotf9928872017-03-30 02:48:54 -0400116static void release_crq_queue(struct ibmvnic_adapter *);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600117
118struct ibmvnic_stat {
119 char name[ETH_GSTRING_LEN];
120 int offset;
121};
122
123#define IBMVNIC_STAT_OFF(stat) (offsetof(struct ibmvnic_adapter, stats) + \
124 offsetof(struct ibmvnic_statistics, stat))
125#define IBMVNIC_GET_STAT(a, off) (*((u64 *)(((unsigned long)(a)) + off)))
126
127static const struct ibmvnic_stat ibmvnic_stats[] = {
128 {"rx_packets", IBMVNIC_STAT_OFF(rx_packets)},
129 {"rx_bytes", IBMVNIC_STAT_OFF(rx_bytes)},
130 {"tx_packets", IBMVNIC_STAT_OFF(tx_packets)},
131 {"tx_bytes", IBMVNIC_STAT_OFF(tx_bytes)},
132 {"ucast_tx_packets", IBMVNIC_STAT_OFF(ucast_tx_packets)},
133 {"ucast_rx_packets", IBMVNIC_STAT_OFF(ucast_rx_packets)},
134 {"mcast_tx_packets", IBMVNIC_STAT_OFF(mcast_tx_packets)},
135 {"mcast_rx_packets", IBMVNIC_STAT_OFF(mcast_rx_packets)},
136 {"bcast_tx_packets", IBMVNIC_STAT_OFF(bcast_tx_packets)},
137 {"bcast_rx_packets", IBMVNIC_STAT_OFF(bcast_rx_packets)},
138 {"align_errors", IBMVNIC_STAT_OFF(align_errors)},
139 {"fcs_errors", IBMVNIC_STAT_OFF(fcs_errors)},
140 {"single_collision_frames", IBMVNIC_STAT_OFF(single_collision_frames)},
141 {"multi_collision_frames", IBMVNIC_STAT_OFF(multi_collision_frames)},
142 {"sqe_test_errors", IBMVNIC_STAT_OFF(sqe_test_errors)},
143 {"deferred_tx", IBMVNIC_STAT_OFF(deferred_tx)},
144 {"late_collisions", IBMVNIC_STAT_OFF(late_collisions)},
145 {"excess_collisions", IBMVNIC_STAT_OFF(excess_collisions)},
146 {"internal_mac_tx_errors", IBMVNIC_STAT_OFF(internal_mac_tx_errors)},
147 {"carrier_sense", IBMVNIC_STAT_OFF(carrier_sense)},
148 {"too_long_frames", IBMVNIC_STAT_OFF(too_long_frames)},
149 {"internal_mac_rx_errors", IBMVNIC_STAT_OFF(internal_mac_rx_errors)},
150};
151
152static long h_reg_sub_crq(unsigned long unit_address, unsigned long token,
153 unsigned long length, unsigned long *number,
154 unsigned long *irq)
155{
156 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
157 long rc;
158
159 rc = plpar_hcall(H_REG_SUB_CRQ, retbuf, unit_address, token, length);
160 *number = retbuf[0];
161 *irq = retbuf[1];
162
163 return rc;
164}
165
Thomas Falcon032c5e82015-12-21 11:26:06 -0600166static int alloc_long_term_buff(struct ibmvnic_adapter *adapter,
167 struct ibmvnic_long_term_buff *ltb, int size)
168{
169 struct device *dev = &adapter->vdev->dev;
170
171 ltb->size = size;
172 ltb->buff = dma_alloc_coherent(dev, ltb->size, &ltb->addr,
173 GFP_KERNEL);
174
175 if (!ltb->buff) {
176 dev_err(dev, "Couldn't alloc long term buffer\n");
177 return -ENOMEM;
178 }
179 ltb->map_id = adapter->map_id;
180 adapter->map_id++;
Nathan Fontenotdb5d0b52017-02-10 13:45:05 -0500181
182 init_completion(&adapter->fw_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600183 send_request_map(adapter, ltb->addr,
184 ltb->size, ltb->map_id);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600185 wait_for_completion(&adapter->fw_done);
186 return 0;
187}
188
189static void free_long_term_buff(struct ibmvnic_adapter *adapter,
190 struct ibmvnic_long_term_buff *ltb)
191{
192 struct device *dev = &adapter->vdev->dev;
193
Nathan Fontenotc657e322017-03-30 02:49:06 -0400194 if (!ltb->buff)
195 return;
196
Nathan Fontenoted651a12017-05-03 14:04:38 -0400197 if (adapter->reset_reason != VNIC_RESET_FAILOVER &&
198 adapter->reset_reason != VNIC_RESET_MOBILITY)
Thomas Falcondfad09a2016-08-18 11:37:51 -0500199 send_request_unmap(adapter, ltb->map_id);
Brian King59af56c2017-04-19 13:44:41 -0400200 dma_free_coherent(dev, ltb->size, ltb->buff, ltb->addr);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600201}
202
Thomas Falconf185a492017-05-26 10:30:48 -0400203static void deactivate_rx_pools(struct ibmvnic_adapter *adapter)
204{
205 int i;
206
207 for (i = 0; i < be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
208 i++)
209 adapter->rx_pool[i].active = 0;
210}
211
Thomas Falcon032c5e82015-12-21 11:26:06 -0600212static void replenish_rx_pool(struct ibmvnic_adapter *adapter,
213 struct ibmvnic_rx_pool *pool)
214{
215 int count = pool->size - atomic_read(&pool->available);
216 struct device *dev = &adapter->vdev->dev;
217 int buffers_added = 0;
218 unsigned long lpar_rc;
219 union sub_crq sub_crq;
220 struct sk_buff *skb;
221 unsigned int offset;
222 dma_addr_t dma_addr;
223 unsigned char *dst;
224 u64 *handle_array;
225 int shift = 0;
226 int index;
227 int i;
228
Thomas Falconf185a492017-05-26 10:30:48 -0400229 if (!pool->active)
230 return;
231
Thomas Falcon032c5e82015-12-21 11:26:06 -0600232 handle_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
233 be32_to_cpu(adapter->login_rsp_buf->
234 off_rxadd_subcrqs));
235
236 for (i = 0; i < count; ++i) {
237 skb = alloc_skb(pool->buff_size, GFP_ATOMIC);
238 if (!skb) {
239 dev_err(dev, "Couldn't replenish rx buff\n");
240 adapter->replenish_no_mem++;
241 break;
242 }
243
244 index = pool->free_map[pool->next_free];
245
246 if (pool->rx_buff[index].skb)
247 dev_err(dev, "Inconsistent free_map!\n");
248
249 /* Copy the skb to the long term mapped DMA buffer */
250 offset = index * pool->buff_size;
251 dst = pool->long_term_buff.buff + offset;
252 memset(dst, 0, pool->buff_size);
253 dma_addr = pool->long_term_buff.addr + offset;
254 pool->rx_buff[index].data = dst;
255
256 pool->free_map[pool->next_free] = IBMVNIC_INVALID_MAP;
257 pool->rx_buff[index].dma = dma_addr;
258 pool->rx_buff[index].skb = skb;
259 pool->rx_buff[index].pool_index = pool->index;
260 pool->rx_buff[index].size = pool->buff_size;
261
262 memset(&sub_crq, 0, sizeof(sub_crq));
263 sub_crq.rx_add.first = IBMVNIC_CRQ_CMD;
264 sub_crq.rx_add.correlator =
265 cpu_to_be64((u64)&pool->rx_buff[index]);
266 sub_crq.rx_add.ioba = cpu_to_be32(dma_addr);
267 sub_crq.rx_add.map_id = pool->long_term_buff.map_id;
268
269 /* The length field of the sCRQ is defined to be 24 bits so the
270 * buffer size needs to be left shifted by a byte before it is
271 * converted to big endian to prevent the last byte from being
272 * truncated.
273 */
274#ifdef __LITTLE_ENDIAN__
275 shift = 8;
276#endif
277 sub_crq.rx_add.len = cpu_to_be32(pool->buff_size << shift);
278
279 lpar_rc = send_subcrq(adapter, handle_array[pool->index],
280 &sub_crq);
281 if (lpar_rc != H_SUCCESS)
282 goto failure;
283
284 buffers_added++;
285 adapter->replenish_add_buff_success++;
286 pool->next_free = (pool->next_free + 1) % pool->size;
287 }
288 atomic_add(buffers_added, &pool->available);
289 return;
290
291failure:
292 dev_info(dev, "replenish pools failure\n");
293 pool->free_map[pool->next_free] = index;
294 pool->rx_buff[index].skb = NULL;
295 if (!dma_mapping_error(dev, dma_addr))
296 dma_unmap_single(dev, dma_addr, pool->buff_size,
297 DMA_FROM_DEVICE);
298
299 dev_kfree_skb_any(skb);
300 adapter->replenish_add_buff_failure++;
301 atomic_add(buffers_added, &pool->available);
Thomas Falconf185a492017-05-26 10:30:48 -0400302
303 if (lpar_rc == H_CLOSED) {
304 /* Disable buffer pool replenishment and report carrier off if
305 * queue is closed. Firmware guarantees that a signal will
306 * be sent to the driver, triggering a reset.
307 */
308 deactivate_rx_pools(adapter);
309 netif_carrier_off(adapter->netdev);
310 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600311}
312
313static void replenish_pools(struct ibmvnic_adapter *adapter)
314{
315 int i;
316
Thomas Falcon032c5e82015-12-21 11:26:06 -0600317 adapter->replenish_task_cycles++;
318 for (i = 0; i < be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
319 i++) {
320 if (adapter->rx_pool[i].active)
321 replenish_rx_pool(adapter, &adapter->rx_pool[i]);
322 }
323}
324
Nathan Fontenot7bbc27a2017-03-30 02:49:23 -0400325static void release_stats_token(struct ibmvnic_adapter *adapter)
326{
327 struct device *dev = &adapter->vdev->dev;
328
329 if (!adapter->stats_token)
330 return;
331
332 dma_unmap_single(dev, adapter->stats_token,
333 sizeof(struct ibmvnic_statistics),
334 DMA_FROM_DEVICE);
335 adapter->stats_token = 0;
336}
337
338static int init_stats_token(struct ibmvnic_adapter *adapter)
339{
340 struct device *dev = &adapter->vdev->dev;
341 dma_addr_t stok;
342
343 stok = dma_map_single(dev, &adapter->stats,
344 sizeof(struct ibmvnic_statistics),
345 DMA_FROM_DEVICE);
346 if (dma_mapping_error(dev, stok)) {
347 dev_err(dev, "Couldn't map stats buffer\n");
348 return -1;
349 }
350
351 adapter->stats_token = stok;
352 return 0;
353}
354
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400355static void release_rx_pools(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -0600356{
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400357 struct ibmvnic_rx_pool *rx_pool;
358 int rx_scrqs;
359 int i, j;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600360
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400361 if (!adapter->rx_pool)
Thomas Falcon032c5e82015-12-21 11:26:06 -0600362 return;
363
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400364 rx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
365 for (i = 0; i < rx_scrqs; i++) {
366 rx_pool = &adapter->rx_pool[i];
367
368 kfree(rx_pool->free_map);
369 free_long_term_buff(adapter, &rx_pool->long_term_buff);
370
371 if (!rx_pool->rx_buff)
Nathan Fontenote0ebe9422017-05-03 14:04:50 -0400372 continue;
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400373
374 for (j = 0; j < rx_pool->size; j++) {
375 if (rx_pool->rx_buff[j].skb) {
376 dev_kfree_skb_any(rx_pool->rx_buff[i].skb);
377 rx_pool->rx_buff[i].skb = NULL;
378 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600379 }
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400380
381 kfree(rx_pool->rx_buff);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600382 }
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400383
384 kfree(adapter->rx_pool);
385 adapter->rx_pool = NULL;
386}
387
388static int init_rx_pools(struct net_device *netdev)
389{
390 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
391 struct device *dev = &adapter->vdev->dev;
392 struct ibmvnic_rx_pool *rx_pool;
393 int rxadd_subcrqs;
394 u64 *size_array;
395 int i, j;
396
397 rxadd_subcrqs =
398 be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
399 size_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
400 be32_to_cpu(adapter->login_rsp_buf->off_rxadd_buff_size));
401
402 adapter->rx_pool = kcalloc(rxadd_subcrqs,
403 sizeof(struct ibmvnic_rx_pool),
404 GFP_KERNEL);
405 if (!adapter->rx_pool) {
406 dev_err(dev, "Failed to allocate rx pools\n");
407 return -1;
408 }
409
410 for (i = 0; i < rxadd_subcrqs; i++) {
411 rx_pool = &adapter->rx_pool[i];
412
413 netdev_dbg(adapter->netdev,
414 "Initializing rx_pool %d, %lld buffs, %lld bytes each\n",
415 i, adapter->req_rx_add_entries_per_subcrq,
416 be64_to_cpu(size_array[i]));
417
418 rx_pool->size = adapter->req_rx_add_entries_per_subcrq;
419 rx_pool->index = i;
420 rx_pool->buff_size = be64_to_cpu(size_array[i]);
421 rx_pool->active = 1;
422
423 rx_pool->free_map = kcalloc(rx_pool->size, sizeof(int),
424 GFP_KERNEL);
425 if (!rx_pool->free_map) {
426 release_rx_pools(adapter);
427 return -1;
428 }
429
430 rx_pool->rx_buff = kcalloc(rx_pool->size,
431 sizeof(struct ibmvnic_rx_buff),
432 GFP_KERNEL);
433 if (!rx_pool->rx_buff) {
434 dev_err(dev, "Couldn't alloc rx buffers\n");
435 release_rx_pools(adapter);
436 return -1;
437 }
438
439 if (alloc_long_term_buff(adapter, &rx_pool->long_term_buff,
440 rx_pool->size * rx_pool->buff_size)) {
441 release_rx_pools(adapter);
442 return -1;
443 }
444
445 for (j = 0; j < rx_pool->size; ++j)
446 rx_pool->free_map[j] = j;
447
448 atomic_set(&rx_pool->available, 0);
449 rx_pool->next_alloc = 0;
450 rx_pool->next_free = 0;
451 }
452
453 return 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600454}
455
Nathan Fontenotc657e322017-03-30 02:49:06 -0400456static void release_tx_pools(struct ibmvnic_adapter *adapter)
457{
458 struct ibmvnic_tx_pool *tx_pool;
459 int i, tx_scrqs;
460
461 if (!adapter->tx_pool)
462 return;
463
464 tx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
465 for (i = 0; i < tx_scrqs; i++) {
466 tx_pool = &adapter->tx_pool[i];
467 kfree(tx_pool->tx_buff);
468 free_long_term_buff(adapter, &tx_pool->long_term_buff);
469 kfree(tx_pool->free_map);
470 }
471
472 kfree(adapter->tx_pool);
473 adapter->tx_pool = NULL;
474}
475
476static int init_tx_pools(struct net_device *netdev)
477{
478 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
479 struct device *dev = &adapter->vdev->dev;
480 struct ibmvnic_tx_pool *tx_pool;
481 int tx_subcrqs;
482 int i, j;
483
484 tx_subcrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
485 adapter->tx_pool = kcalloc(tx_subcrqs,
486 sizeof(struct ibmvnic_tx_pool), GFP_KERNEL);
487 if (!adapter->tx_pool)
488 return -1;
489
490 for (i = 0; i < tx_subcrqs; i++) {
491 tx_pool = &adapter->tx_pool[i];
492 tx_pool->tx_buff = kcalloc(adapter->req_tx_entries_per_subcrq,
493 sizeof(struct ibmvnic_tx_buff),
494 GFP_KERNEL);
495 if (!tx_pool->tx_buff) {
496 dev_err(dev, "tx pool buffer allocation failed\n");
497 release_tx_pools(adapter);
498 return -1;
499 }
500
501 if (alloc_long_term_buff(adapter, &tx_pool->long_term_buff,
502 adapter->req_tx_entries_per_subcrq *
503 adapter->req_mtu)) {
504 release_tx_pools(adapter);
505 return -1;
506 }
507
508 tx_pool->free_map = kcalloc(adapter->req_tx_entries_per_subcrq,
509 sizeof(int), GFP_KERNEL);
510 if (!tx_pool->free_map) {
511 release_tx_pools(adapter);
512 return -1;
513 }
514
515 for (j = 0; j < adapter->req_tx_entries_per_subcrq; j++)
516 tx_pool->free_map[j] = j;
517
518 tx_pool->consumer_index = 0;
519 tx_pool->producer_index = 0;
520 }
521
522 return 0;
523}
524
Nathan Fontenot661a2622017-04-19 13:44:58 -0400525static void release_error_buffers(struct ibmvnic_adapter *adapter)
526{
527 struct device *dev = &adapter->vdev->dev;
528 struct ibmvnic_error_buff *error_buff, *tmp;
529 unsigned long flags;
530
531 spin_lock_irqsave(&adapter->error_list_lock, flags);
532 list_for_each_entry_safe(error_buff, tmp, &adapter->errors, list) {
533 list_del(&error_buff->list);
534 dma_unmap_single(dev, error_buff->dma, error_buff->len,
535 DMA_FROM_DEVICE);
536 kfree(error_buff->buff);
537 kfree(error_buff);
538 }
539 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
540}
541
John Allend944c3d62017-05-26 10:30:13 -0400542static void ibmvnic_napi_enable(struct ibmvnic_adapter *adapter)
543{
544 int i;
545
546 if (adapter->napi_enabled)
547 return;
548
549 for (i = 0; i < adapter->req_rx_queues; i++)
550 napi_enable(&adapter->napi[i]);
551
552 adapter->napi_enabled = true;
553}
554
555static void ibmvnic_napi_disable(struct ibmvnic_adapter *adapter)
556{
557 int i;
558
559 if (!adapter->napi_enabled)
560 return;
561
562 for (i = 0; i < adapter->req_rx_queues; i++)
563 napi_disable(&adapter->napi[i]);
564
565 adapter->napi_enabled = false;
566}
567
John Allena57a5d22017-03-17 17:13:41 -0500568static int ibmvnic_login(struct net_device *netdev)
Thomas Falcon032c5e82015-12-21 11:26:06 -0600569{
570 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
John Allenbd0b6722017-03-17 17:13:40 -0500571 unsigned long timeout = msecs_to_jiffies(30000);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600572 struct device *dev = &adapter->vdev->dev;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600573
John Allenbd0b6722017-03-17 17:13:40 -0500574 do {
575 if (adapter->renegotiate) {
576 adapter->renegotiate = false;
Nathan Fontenotb5108882017-03-30 02:49:18 -0400577 release_sub_crqs(adapter);
John Allenbd0b6722017-03-17 17:13:40 -0500578
579 reinit_completion(&adapter->init_done);
580 send_cap_queries(adapter);
581 if (!wait_for_completion_timeout(&adapter->init_done,
582 timeout)) {
583 dev_err(dev, "Capabilities query timeout\n");
584 return -1;
585 }
586 }
587
588 reinit_completion(&adapter->init_done);
589 send_login(adapter);
590 if (!wait_for_completion_timeout(&adapter->init_done,
591 timeout)) {
592 dev_err(dev, "Login timeout\n");
593 return -1;
594 }
595 } while (adapter->renegotiate);
596
John Allena57a5d22017-03-17 17:13:41 -0500597 return 0;
598}
599
Nathan Fontenot1b8955e2017-03-30 02:49:29 -0400600static void release_resources(struct ibmvnic_adapter *adapter)
601{
Nathan Fontenotc7bac002017-05-03 14:04:44 -0400602 int i;
603
Nathan Fontenot1b8955e2017-03-30 02:49:29 -0400604 release_tx_pools(adapter);
605 release_rx_pools(adapter);
606
Nathan Fontenot1b8955e2017-03-30 02:49:29 -0400607 release_stats_token(adapter);
Nathan Fontenot661a2622017-04-19 13:44:58 -0400608 release_error_buffers(adapter);
Nathan Fontenotc7bac002017-05-03 14:04:44 -0400609
610 if (adapter->napi) {
611 for (i = 0; i < adapter->req_rx_queues; i++) {
612 if (&adapter->napi[i])
613 netif_napi_del(&adapter->napi[i]);
614 }
615 }
Nathan Fontenot1b8955e2017-03-30 02:49:29 -0400616}
617
Nathan Fontenot53da09e2017-04-21 15:39:04 -0400618static int set_link_state(struct ibmvnic_adapter *adapter, u8 link_state)
619{
620 struct net_device *netdev = adapter->netdev;
621 unsigned long timeout = msecs_to_jiffies(30000);
622 union ibmvnic_crq crq;
623 bool resend;
624 int rc;
625
Nathan Fontenot53da09e2017-04-21 15:39:04 -0400626 netdev_err(netdev, "setting link state %d\n", link_state);
627 memset(&crq, 0, sizeof(crq));
628 crq.logical_link_state.first = IBMVNIC_CRQ_CMD;
629 crq.logical_link_state.cmd = LOGICAL_LINK_STATE;
630 crq.logical_link_state.link_state = link_state;
631
632 do {
633 resend = false;
634
635 reinit_completion(&adapter->init_done);
636 rc = ibmvnic_send_crq(adapter, &crq);
637 if (rc) {
638 netdev_err(netdev, "Failed to set link state\n");
639 return rc;
640 }
641
642 if (!wait_for_completion_timeout(&adapter->init_done,
643 timeout)) {
644 netdev_err(netdev, "timeout setting link state\n");
645 return -1;
646 }
647
648 if (adapter->init_done_rc == 1) {
649 /* Partuial success, delay and re-send */
650 mdelay(1000);
651 resend = true;
652 }
653 } while (resend);
654
655 return 0;
656}
657
Thomas Falcon7f3c6e62017-04-21 15:38:40 -0400658static int set_real_num_queues(struct net_device *netdev)
659{
660 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
661 int rc;
662
663 rc = netif_set_real_num_tx_queues(netdev, adapter->req_tx_queues);
664 if (rc) {
665 netdev_err(netdev, "failed to set the number of tx queues\n");
666 return rc;
667 }
668
669 rc = netif_set_real_num_rx_queues(netdev, adapter->req_rx_queues);
670 if (rc)
671 netdev_err(netdev, "failed to set the number of rx queues\n");
672
673 return rc;
674}
675
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400676static int init_resources(struct ibmvnic_adapter *adapter)
John Allena57a5d22017-03-17 17:13:41 -0500677{
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400678 struct net_device *netdev = adapter->netdev;
679 int i, rc;
John Allena57a5d22017-03-17 17:13:41 -0500680
Thomas Falcon7f3c6e62017-04-21 15:38:40 -0400681 rc = set_real_num_queues(netdev);
682 if (rc)
683 return rc;
John Allenbd0b6722017-03-17 17:13:40 -0500684
685 rc = init_sub_crq_irqs(adapter);
686 if (rc) {
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400687 netdev_err(netdev, "failed to initialize sub crq irqs\n");
John Allenbd0b6722017-03-17 17:13:40 -0500688 return -1;
689 }
690
Nathan Fontenot5d5e84e2017-04-21 15:38:58 -0400691 rc = init_stats_token(adapter);
692 if (rc)
693 return rc;
694
Thomas Falcon032c5e82015-12-21 11:26:06 -0600695 adapter->map_id = 1;
696 adapter->napi = kcalloc(adapter->req_rx_queues,
697 sizeof(struct napi_struct), GFP_KERNEL);
698 if (!adapter->napi)
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400699 return -ENOMEM;
700
Thomas Falcon032c5e82015-12-21 11:26:06 -0600701 for (i = 0; i < adapter->req_rx_queues; i++) {
702 netif_napi_add(netdev, &adapter->napi[i], ibmvnic_poll,
703 NAPI_POLL_WEIGHT);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600704 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600705
Thomas Falcon032c5e82015-12-21 11:26:06 -0600706 send_map_query(adapter);
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400707
708 rc = init_rx_pools(netdev);
709 if (rc)
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400710 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600711
Nathan Fontenotc657e322017-03-30 02:49:06 -0400712 rc = init_tx_pools(netdev);
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400713 return rc;
714}
715
Nathan Fontenoted651a12017-05-03 14:04:38 -0400716static int __ibmvnic_open(struct net_device *netdev)
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400717{
718 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Nathan Fontenoted651a12017-05-03 14:04:38 -0400719 enum vnic_state prev_state = adapter->state;
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400720 int i, rc;
721
Nathan Fontenot90c80142017-05-03 14:04:32 -0400722 adapter->state = VNIC_OPENING;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600723 replenish_pools(adapter);
John Allend944c3d62017-05-26 10:30:13 -0400724 ibmvnic_napi_enable(adapter);
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400725
Thomas Falcon032c5e82015-12-21 11:26:06 -0600726 /* We're ready to receive frames, enable the sub-crq interrupts and
727 * set the logical link state to up
728 */
Nathan Fontenoted651a12017-05-03 14:04:38 -0400729 for (i = 0; i < adapter->req_rx_queues; i++) {
730 if (prev_state == VNIC_CLOSED)
731 enable_irq(adapter->rx_scrq[i]->irq);
732 else
733 enable_scrq_irq(adapter, adapter->rx_scrq[i]);
734 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600735
Nathan Fontenoted651a12017-05-03 14:04:38 -0400736 for (i = 0; i < adapter->req_tx_queues; i++) {
737 if (prev_state == VNIC_CLOSED)
738 enable_irq(adapter->tx_scrq[i]->irq);
739 else
740 enable_scrq_irq(adapter, adapter->tx_scrq[i]);
741 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600742
Nathan Fontenot53da09e2017-04-21 15:39:04 -0400743 rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_UP);
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400744 if (rc) {
745 for (i = 0; i < adapter->req_rx_queues; i++)
746 napi_disable(&adapter->napi[i]);
747 release_resources(adapter);
Nathan Fontenoted651a12017-05-03 14:04:38 -0400748 return rc;
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400749 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600750
Nathan Fontenoted651a12017-05-03 14:04:38 -0400751 netif_tx_start_all_queues(netdev);
752
753 if (prev_state == VNIC_CLOSED) {
754 for (i = 0; i < adapter->req_rx_queues; i++)
755 napi_schedule(&adapter->napi[i]);
756 }
757
758 adapter->state = VNIC_OPEN;
759 return rc;
760}
761
762static int ibmvnic_open(struct net_device *netdev)
763{
764 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
765 int rc;
766
767 mutex_lock(&adapter->reset_lock);
768
769 if (adapter->state != VNIC_CLOSED) {
770 rc = ibmvnic_login(netdev);
771 if (rc) {
772 mutex_unlock(&adapter->reset_lock);
773 return rc;
774 }
775
776 rc = init_resources(adapter);
777 if (rc) {
778 netdev_err(netdev, "failed to initialize resources\n");
779 release_resources(adapter);
780 mutex_unlock(&adapter->reset_lock);
781 return rc;
782 }
783 }
784
785 rc = __ibmvnic_open(netdev);
786 mutex_unlock(&adapter->reset_lock);
787
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400788 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600789}
790
Nathan Fontenotb41b83e2017-05-03 14:04:56 -0400791static void clean_tx_pools(struct ibmvnic_adapter *adapter)
792{
793 struct ibmvnic_tx_pool *tx_pool;
794 u64 tx_entries;
795 int tx_scrqs;
796 int i, j;
797
798 if (!adapter->tx_pool)
799 return;
800
801 tx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
802 tx_entries = adapter->req_tx_entries_per_subcrq;
803
804 /* Free any remaining skbs in the tx buffer pools */
805 for (i = 0; i < tx_scrqs; i++) {
806 tx_pool = &adapter->tx_pool[i];
807 if (!tx_pool)
808 continue;
809
810 for (j = 0; j < tx_entries; j++) {
811 if (tx_pool->tx_buff[j].skb) {
812 dev_kfree_skb_any(tx_pool->tx_buff[j].skb);
813 tx_pool->tx_buff[j].skb = NULL;
814 }
815 }
816 }
817}
818
Nathan Fontenoted651a12017-05-03 14:04:38 -0400819static int __ibmvnic_close(struct net_device *netdev)
John Allenea5509f2017-03-17 17:13:43 -0500820{
821 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Nathan Fontenot53da09e2017-04-21 15:39:04 -0400822 int rc = 0;
John Allenea5509f2017-03-17 17:13:43 -0500823 int i;
824
Nathan Fontenot90c80142017-05-03 14:04:32 -0400825 adapter->state = VNIC_CLOSING;
Nathan Fontenoted651a12017-05-03 14:04:38 -0400826 netif_tx_stop_all_queues(netdev);
John Allend944c3d62017-05-26 10:30:13 -0400827 ibmvnic_napi_disable(adapter);
Nathan Fontenot46293b92017-05-03 14:05:02 -0400828
829 if (adapter->tx_scrq) {
830 for (i = 0; i < adapter->req_tx_queues; i++)
831 if (adapter->tx_scrq[i]->irq)
832 disable_irq(adapter->tx_scrq[i]->irq);
833 }
834
Nathan Fontenot53da09e2017-04-21 15:39:04 -0400835 rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_DN);
Nathan Fontenot46293b92017-05-03 14:05:02 -0400836 if (rc)
837 return rc;
838
839 if (adapter->rx_scrq) {
840 for (i = 0; i < adapter->req_rx_queues; i++) {
841 int retries = 10;
842
843 while (pending_scrq(adapter, adapter->rx_scrq[i])) {
844 retries--;
845 mdelay(100);
846
847 if (retries == 0)
848 break;
849 }
850
851 if (adapter->rx_scrq[i]->irq)
852 disable_irq(adapter->rx_scrq[i]->irq);
853 }
854 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600855
Thomas Falcon10f76212017-05-26 10:30:31 -0400856 clean_tx_pools(adapter);
Nathan Fontenot90c80142017-05-03 14:04:32 -0400857 adapter->state = VNIC_CLOSED;
Nathan Fontenot53da09e2017-04-21 15:39:04 -0400858 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600859}
860
Nathan Fontenoted651a12017-05-03 14:04:38 -0400861static int ibmvnic_close(struct net_device *netdev)
862{
863 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
864 int rc;
865
866 mutex_lock(&adapter->reset_lock);
867 rc = __ibmvnic_close(netdev);
868 mutex_unlock(&adapter->reset_lock);
869
870 return rc;
871}
872
Thomas Falconad7775d2016-04-01 17:20:34 -0500873/**
874 * build_hdr_data - creates L2/L3/L4 header data buffer
875 * @hdr_field - bitfield determining needed headers
876 * @skb - socket buffer
877 * @hdr_len - array of header lengths
878 * @tot_len - total length of data
879 *
880 * Reads hdr_field to determine which headers are needed by firmware.
881 * Builds a buffer containing these headers. Saves individual header
882 * lengths and total buffer length to be used to build descriptors.
883 */
884static int build_hdr_data(u8 hdr_field, struct sk_buff *skb,
885 int *hdr_len, u8 *hdr_data)
886{
887 int len = 0;
888 u8 *hdr;
889
890 hdr_len[0] = sizeof(struct ethhdr);
891
892 if (skb->protocol == htons(ETH_P_IP)) {
893 hdr_len[1] = ip_hdr(skb)->ihl * 4;
894 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
895 hdr_len[2] = tcp_hdrlen(skb);
896 else if (ip_hdr(skb)->protocol == IPPROTO_UDP)
897 hdr_len[2] = sizeof(struct udphdr);
898 } else if (skb->protocol == htons(ETH_P_IPV6)) {
899 hdr_len[1] = sizeof(struct ipv6hdr);
900 if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
901 hdr_len[2] = tcp_hdrlen(skb);
902 else if (ipv6_hdr(skb)->nexthdr == IPPROTO_UDP)
903 hdr_len[2] = sizeof(struct udphdr);
904 }
905
906 memset(hdr_data, 0, 120);
907 if ((hdr_field >> 6) & 1) {
908 hdr = skb_mac_header(skb);
909 memcpy(hdr_data, hdr, hdr_len[0]);
910 len += hdr_len[0];
911 }
912
913 if ((hdr_field >> 5) & 1) {
914 hdr = skb_network_header(skb);
915 memcpy(hdr_data + len, hdr, hdr_len[1]);
916 len += hdr_len[1];
917 }
918
919 if ((hdr_field >> 4) & 1) {
920 hdr = skb_transport_header(skb);
921 memcpy(hdr_data + len, hdr, hdr_len[2]);
922 len += hdr_len[2];
923 }
924 return len;
925}
926
927/**
928 * create_hdr_descs - create header and header extension descriptors
929 * @hdr_field - bitfield determining needed headers
930 * @data - buffer containing header data
931 * @len - length of data buffer
932 * @hdr_len - array of individual header lengths
933 * @scrq_arr - descriptor array
934 *
935 * Creates header and, if needed, header extension descriptors and
936 * places them in a descriptor array, scrq_arr
937 */
938
939static void create_hdr_descs(u8 hdr_field, u8 *hdr_data, int len, int *hdr_len,
940 union sub_crq *scrq_arr)
941{
942 union sub_crq hdr_desc;
943 int tmp_len = len;
944 u8 *data, *cur;
945 int tmp;
946
947 while (tmp_len > 0) {
948 cur = hdr_data + len - tmp_len;
949
950 memset(&hdr_desc, 0, sizeof(hdr_desc));
951 if (cur != hdr_data) {
952 data = hdr_desc.hdr_ext.data;
953 tmp = tmp_len > 29 ? 29 : tmp_len;
954 hdr_desc.hdr_ext.first = IBMVNIC_CRQ_CMD;
955 hdr_desc.hdr_ext.type = IBMVNIC_HDR_EXT_DESC;
956 hdr_desc.hdr_ext.len = tmp;
957 } else {
958 data = hdr_desc.hdr.data;
959 tmp = tmp_len > 24 ? 24 : tmp_len;
960 hdr_desc.hdr.first = IBMVNIC_CRQ_CMD;
961 hdr_desc.hdr.type = IBMVNIC_HDR_DESC;
962 hdr_desc.hdr.len = tmp;
963 hdr_desc.hdr.l2_len = (u8)hdr_len[0];
964 hdr_desc.hdr.l3_len = cpu_to_be16((u16)hdr_len[1]);
965 hdr_desc.hdr.l4_len = (u8)hdr_len[2];
966 hdr_desc.hdr.flag = hdr_field << 1;
967 }
968 memcpy(data, cur, tmp);
969 tmp_len -= tmp;
970 *scrq_arr = hdr_desc;
971 scrq_arr++;
972 }
973}
974
975/**
976 * build_hdr_descs_arr - build a header descriptor array
977 * @skb - socket buffer
978 * @num_entries - number of descriptors to be sent
979 * @subcrq - first TX descriptor
980 * @hdr_field - bit field determining which headers will be sent
981 *
982 * This function will build a TX descriptor array with applicable
983 * L2/L3/L4 packet header descriptors to be sent by send_subcrq_indirect.
984 */
985
986static void build_hdr_descs_arr(struct ibmvnic_tx_buff *txbuff,
987 int *num_entries, u8 hdr_field)
988{
989 int hdr_len[3] = {0, 0, 0};
990 int tot_len, len;
991 u8 *hdr_data = txbuff->hdr_data;
992
993 tot_len = build_hdr_data(hdr_field, txbuff->skb, hdr_len,
994 txbuff->hdr_data);
995 len = tot_len;
996 len -= 24;
997 if (len > 0)
998 num_entries += len % 29 ? len / 29 + 1 : len / 29;
999 create_hdr_descs(hdr_field, hdr_data, tot_len, hdr_len,
1000 txbuff->indir_arr + 1);
1001}
1002
Thomas Falcon032c5e82015-12-21 11:26:06 -06001003static int ibmvnic_xmit(struct sk_buff *skb, struct net_device *netdev)
1004{
1005 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1006 int queue_num = skb_get_queue_mapping(skb);
Thomas Falconad7775d2016-04-01 17:20:34 -05001007 u8 *hdrs = (u8 *)&adapter->tx_rx_desc_req;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001008 struct device *dev = &adapter->vdev->dev;
1009 struct ibmvnic_tx_buff *tx_buff = NULL;
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001010 struct ibmvnic_sub_crq_queue *tx_scrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001011 struct ibmvnic_tx_pool *tx_pool;
1012 unsigned int tx_send_failed = 0;
1013 unsigned int tx_map_failed = 0;
1014 unsigned int tx_dropped = 0;
1015 unsigned int tx_packets = 0;
1016 unsigned int tx_bytes = 0;
1017 dma_addr_t data_dma_addr;
1018 struct netdev_queue *txq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001019 unsigned long lpar_rc;
1020 union sub_crq tx_crq;
1021 unsigned int offset;
Thomas Falconad7775d2016-04-01 17:20:34 -05001022 int num_entries = 1;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001023 unsigned char *dst;
1024 u64 *handle_array;
1025 int index = 0;
1026 int ret = 0;
1027
Nathan Fontenoted651a12017-05-03 14:04:38 -04001028 if (adapter->resetting) {
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001029 if (!netif_subqueue_stopped(netdev, skb))
1030 netif_stop_subqueue(netdev, queue_num);
1031 dev_kfree_skb_any(skb);
1032
Thomas Falcon032c5e82015-12-21 11:26:06 -06001033 tx_send_failed++;
1034 tx_dropped++;
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001035 ret = NETDEV_TX_OK;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001036 goto out;
1037 }
1038
Nathan Fontenot161b8a82017-05-03 14:05:08 -04001039 tx_pool = &adapter->tx_pool[queue_num];
1040 tx_scrq = adapter->tx_scrq[queue_num];
1041 txq = netdev_get_tx_queue(netdev, skb_get_queue_mapping(skb));
1042 handle_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
1043 be32_to_cpu(adapter->login_rsp_buf->off_txsubm_subcrqs));
1044
Thomas Falcon032c5e82015-12-21 11:26:06 -06001045 index = tx_pool->free_map[tx_pool->consumer_index];
1046 offset = index * adapter->req_mtu;
1047 dst = tx_pool->long_term_buff.buff + offset;
1048 memset(dst, 0, adapter->req_mtu);
1049 skb_copy_from_linear_data(skb, dst, skb->len);
1050 data_dma_addr = tx_pool->long_term_buff.addr + offset;
1051
1052 tx_pool->consumer_index =
1053 (tx_pool->consumer_index + 1) %
Thomas Falcon068d9f92017-03-05 12:18:42 -06001054 adapter->req_tx_entries_per_subcrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001055
1056 tx_buff = &tx_pool->tx_buff[index];
1057 tx_buff->skb = skb;
1058 tx_buff->data_dma[0] = data_dma_addr;
1059 tx_buff->data_len[0] = skb->len;
1060 tx_buff->index = index;
1061 tx_buff->pool_index = queue_num;
1062 tx_buff->last_frag = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001063
1064 memset(&tx_crq, 0, sizeof(tx_crq));
1065 tx_crq.v1.first = IBMVNIC_CRQ_CMD;
1066 tx_crq.v1.type = IBMVNIC_TX_DESC;
1067 tx_crq.v1.n_crq_elem = 1;
1068 tx_crq.v1.n_sge = 1;
1069 tx_crq.v1.flags1 = IBMVNIC_TX_COMP_NEEDED;
1070 tx_crq.v1.correlator = cpu_to_be32(index);
1071 tx_crq.v1.dma_reg = cpu_to_be16(tx_pool->long_term_buff.map_id);
1072 tx_crq.v1.sge_len = cpu_to_be32(skb->len);
1073 tx_crq.v1.ioba = cpu_to_be64(data_dma_addr);
1074
1075 if (adapter->vlan_header_insertion) {
1076 tx_crq.v1.flags2 |= IBMVNIC_TX_VLAN_INSERT;
1077 tx_crq.v1.vlan_id = cpu_to_be16(skb->vlan_tci);
1078 }
1079
1080 if (skb->protocol == htons(ETH_P_IP)) {
1081 if (ip_hdr(skb)->version == 4)
1082 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV4;
1083 else if (ip_hdr(skb)->version == 6)
1084 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV6;
1085
1086 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
1087 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_TCP;
1088 else if (ip_hdr(skb)->protocol != IPPROTO_TCP)
1089 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_UDP;
1090 }
1091
Thomas Falconad7775d2016-04-01 17:20:34 -05001092 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06001093 tx_crq.v1.flags1 |= IBMVNIC_TX_CHKSUM_OFFLOAD;
Thomas Falconad7775d2016-04-01 17:20:34 -05001094 hdrs += 2;
1095 }
1096 /* determine if l2/3/4 headers are sent to firmware */
1097 if ((*hdrs >> 7) & 1 &&
1098 (skb->protocol == htons(ETH_P_IP) ||
1099 skb->protocol == htons(ETH_P_IPV6))) {
1100 build_hdr_descs_arr(tx_buff, &num_entries, *hdrs);
1101 tx_crq.v1.n_crq_elem = num_entries;
1102 tx_buff->indir_arr[0] = tx_crq;
1103 tx_buff->indir_dma = dma_map_single(dev, tx_buff->indir_arr,
1104 sizeof(tx_buff->indir_arr),
1105 DMA_TO_DEVICE);
1106 if (dma_mapping_error(dev, tx_buff->indir_dma)) {
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001107 dev_kfree_skb_any(skb);
1108 tx_buff->skb = NULL;
Thomas Falconad7775d2016-04-01 17:20:34 -05001109 if (!firmware_has_feature(FW_FEATURE_CMO))
1110 dev_err(dev, "tx: unable to map descriptor array\n");
1111 tx_map_failed++;
1112 tx_dropped++;
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001113 ret = NETDEV_TX_OK;
Thomas Falconad7775d2016-04-01 17:20:34 -05001114 goto out;
1115 }
John Allen498cd8e2016-04-06 11:49:55 -05001116 lpar_rc = send_subcrq_indirect(adapter, handle_array[queue_num],
Thomas Falconad7775d2016-04-01 17:20:34 -05001117 (u64)tx_buff->indir_dma,
1118 (u64)num_entries);
1119 } else {
John Allen498cd8e2016-04-06 11:49:55 -05001120 lpar_rc = send_subcrq(adapter, handle_array[queue_num],
1121 &tx_crq);
Thomas Falconad7775d2016-04-01 17:20:34 -05001122 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001123 if (lpar_rc != H_SUCCESS) {
1124 dev_err(dev, "tx failed with code %ld\n", lpar_rc);
1125
1126 if (tx_pool->consumer_index == 0)
1127 tx_pool->consumer_index =
Thomas Falcon068d9f92017-03-05 12:18:42 -06001128 adapter->req_tx_entries_per_subcrq - 1;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001129 else
1130 tx_pool->consumer_index--;
1131
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001132 dev_kfree_skb_any(skb);
1133 tx_buff->skb = NULL;
1134
Thomas Falconb8c80b82017-05-26 10:30:42 -04001135 if (lpar_rc == H_CLOSED) {
1136 /* Disable TX and report carrier off if queue is closed.
1137 * Firmware guarantees that a signal will be sent to the
1138 * driver, triggering a reset or some other action.
1139 */
1140 netif_tx_stop_all_queues(netdev);
1141 netif_carrier_off(netdev);
1142 }
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001143
Thomas Falcon032c5e82015-12-21 11:26:06 -06001144 tx_send_failed++;
1145 tx_dropped++;
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001146 ret = NETDEV_TX_OK;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001147 goto out;
1148 }
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001149
Brian King58c8c0c2017-04-19 13:44:47 -04001150 if (atomic_inc_return(&tx_scrq->used)
1151 >= adapter->req_tx_entries_per_subcrq) {
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001152 netdev_info(netdev, "Stopping queue %d\n", queue_num);
1153 netif_stop_subqueue(netdev, queue_num);
1154 }
1155
Thomas Falcon032c5e82015-12-21 11:26:06 -06001156 tx_packets++;
1157 tx_bytes += skb->len;
1158 txq->trans_start = jiffies;
1159 ret = NETDEV_TX_OK;
1160
1161out:
1162 netdev->stats.tx_dropped += tx_dropped;
1163 netdev->stats.tx_bytes += tx_bytes;
1164 netdev->stats.tx_packets += tx_packets;
1165 adapter->tx_send_failed += tx_send_failed;
1166 adapter->tx_map_failed += tx_map_failed;
1167
1168 return ret;
1169}
1170
1171static void ibmvnic_set_multi(struct net_device *netdev)
1172{
1173 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1174 struct netdev_hw_addr *ha;
1175 union ibmvnic_crq crq;
1176
1177 memset(&crq, 0, sizeof(crq));
1178 crq.request_capability.first = IBMVNIC_CRQ_CMD;
1179 crq.request_capability.cmd = REQUEST_CAPABILITY;
1180
1181 if (netdev->flags & IFF_PROMISC) {
1182 if (!adapter->promisc_supported)
1183 return;
1184 } else {
1185 if (netdev->flags & IFF_ALLMULTI) {
1186 /* Accept all multicast */
1187 memset(&crq, 0, sizeof(crq));
1188 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1189 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1190 crq.multicast_ctrl.flags = IBMVNIC_ENABLE_ALL;
1191 ibmvnic_send_crq(adapter, &crq);
1192 } else if (netdev_mc_empty(netdev)) {
1193 /* Reject all multicast */
1194 memset(&crq, 0, sizeof(crq));
1195 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1196 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1197 crq.multicast_ctrl.flags = IBMVNIC_DISABLE_ALL;
1198 ibmvnic_send_crq(adapter, &crq);
1199 } else {
1200 /* Accept one or more multicast(s) */
1201 netdev_for_each_mc_addr(ha, netdev) {
1202 memset(&crq, 0, sizeof(crq));
1203 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1204 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1205 crq.multicast_ctrl.flags = IBMVNIC_ENABLE_MC;
1206 ether_addr_copy(&crq.multicast_ctrl.mac_addr[0],
1207 ha->addr);
1208 ibmvnic_send_crq(adapter, &crq);
1209 }
1210 }
1211 }
1212}
1213
1214static int ibmvnic_set_mac(struct net_device *netdev, void *p)
1215{
1216 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1217 struct sockaddr *addr = p;
1218 union ibmvnic_crq crq;
1219
1220 if (!is_valid_ether_addr(addr->sa_data))
1221 return -EADDRNOTAVAIL;
1222
1223 memset(&crq, 0, sizeof(crq));
1224 crq.change_mac_addr.first = IBMVNIC_CRQ_CMD;
1225 crq.change_mac_addr.cmd = CHANGE_MAC_ADDR;
1226 ether_addr_copy(&crq.change_mac_addr.mac_addr[0], addr->sa_data);
1227 ibmvnic_send_crq(adapter, &crq);
1228 /* netdev->dev_addr is changed in handle_change_mac_rsp function */
1229 return 0;
1230}
1231
Nathan Fontenoted651a12017-05-03 14:04:38 -04001232/**
1233 * do_reset returns zero if we are able to keep processing reset events, or
1234 * non-zero if we hit a fatal error and must halt.
1235 */
1236static int do_reset(struct ibmvnic_adapter *adapter,
1237 struct ibmvnic_rwi *rwi, u32 reset_state)
1238{
1239 struct net_device *netdev = adapter->netdev;
1240 int i, rc;
1241
1242 netif_carrier_off(netdev);
1243 adapter->reset_reason = rwi->reset_reason;
1244
1245 if (rwi->reset_reason == VNIC_RESET_MOBILITY) {
1246 rc = ibmvnic_reenable_crq_queue(adapter);
1247 if (rc)
1248 return 0;
1249 }
1250
1251 rc = __ibmvnic_close(netdev);
1252 if (rc)
1253 return rc;
1254
John Allen8cb31cf2017-05-26 10:30:37 -04001255 if (adapter->reset_reason != VNIC_RESET_NON_FATAL) {
1256 /* remove the closed state so when we call open it appears
1257 * we are coming from the probed state.
1258 */
Nathan Fontenoted651a12017-05-03 14:04:38 -04001259 adapter->state = VNIC_PROBED;
John Allen8cb31cf2017-05-26 10:30:37 -04001260
1261 release_resources(adapter);
1262 release_sub_crqs(adapter);
John Allen8cb31cf2017-05-26 10:30:37 -04001263
1264 rc = ibmvnic_init(adapter);
1265 if (rc)
1266 return 0;
1267
1268 /* If the adapter was in PROBE state prior to the reset,
1269 * exit here.
1270 */
1271 if (reset_state == VNIC_PROBED)
1272 return 0;
1273
1274 rc = ibmvnic_login(netdev);
1275 if (rc) {
1276 adapter->state = VNIC_PROBED;
1277 return 0;
1278 }
1279
1280 rtnl_lock();
1281 rc = init_resources(adapter);
1282 rtnl_unlock();
1283 if (rc)
1284 return rc;
1285
1286 if (reset_state == VNIC_CLOSED)
1287 return 0;
Nathan Fontenoted651a12017-05-03 14:04:38 -04001288 }
1289
Nathan Fontenoted651a12017-05-03 14:04:38 -04001290 rc = __ibmvnic_open(netdev);
1291 if (rc) {
1292 if (list_empty(&adapter->rwi_list))
1293 adapter->state = VNIC_CLOSED;
1294 else
1295 adapter->state = reset_state;
1296
1297 return 0;
1298 }
1299
1300 netif_carrier_on(netdev);
1301
1302 /* kick napi */
1303 for (i = 0; i < adapter->req_rx_queues; i++)
1304 napi_schedule(&adapter->napi[i]);
1305
John Allen2ce9e4e2017-05-26 10:30:25 -04001306 netdev_notify_peers(netdev);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001307 return 0;
1308}
1309
1310static struct ibmvnic_rwi *get_next_rwi(struct ibmvnic_adapter *adapter)
1311{
1312 struct ibmvnic_rwi *rwi;
1313
1314 mutex_lock(&adapter->rwi_lock);
1315
1316 if (!list_empty(&adapter->rwi_list)) {
1317 rwi = list_first_entry(&adapter->rwi_list, struct ibmvnic_rwi,
1318 list);
1319 list_del(&rwi->list);
1320 } else {
1321 rwi = NULL;
1322 }
1323
1324 mutex_unlock(&adapter->rwi_lock);
1325 return rwi;
1326}
1327
1328static void free_all_rwi(struct ibmvnic_adapter *adapter)
1329{
1330 struct ibmvnic_rwi *rwi;
1331
1332 rwi = get_next_rwi(adapter);
1333 while (rwi) {
1334 kfree(rwi);
1335 rwi = get_next_rwi(adapter);
1336 }
1337}
1338
1339static void __ibmvnic_reset(struct work_struct *work)
1340{
1341 struct ibmvnic_rwi *rwi;
1342 struct ibmvnic_adapter *adapter;
1343 struct net_device *netdev;
1344 u32 reset_state;
1345 int rc;
1346
1347 adapter = container_of(work, struct ibmvnic_adapter, ibmvnic_reset);
1348 netdev = adapter->netdev;
1349
1350 mutex_lock(&adapter->reset_lock);
1351 adapter->resetting = true;
1352 reset_state = adapter->state;
1353
1354 rwi = get_next_rwi(adapter);
1355 while (rwi) {
1356 rc = do_reset(adapter, rwi, reset_state);
1357 kfree(rwi);
1358 if (rc)
1359 break;
1360
1361 rwi = get_next_rwi(adapter);
1362 }
1363
1364 if (rc) {
1365 free_all_rwi(adapter);
Wei Yongjun6d0af072017-05-18 15:24:52 +00001366 mutex_unlock(&adapter->reset_lock);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001367 return;
1368 }
1369
1370 adapter->resetting = false;
1371 mutex_unlock(&adapter->reset_lock);
1372}
1373
1374static void ibmvnic_reset(struct ibmvnic_adapter *adapter,
1375 enum ibmvnic_reset_reason reason)
1376{
1377 struct ibmvnic_rwi *rwi, *tmp;
1378 struct net_device *netdev = adapter->netdev;
1379 struct list_head *entry;
1380
1381 if (adapter->state == VNIC_REMOVING ||
1382 adapter->state == VNIC_REMOVED) {
1383 netdev_dbg(netdev, "Adapter removing, skipping reset\n");
1384 return;
1385 }
1386
1387 mutex_lock(&adapter->rwi_lock);
1388
1389 list_for_each(entry, &adapter->rwi_list) {
1390 tmp = list_entry(entry, struct ibmvnic_rwi, list);
1391 if (tmp->reset_reason == reason) {
1392 netdev_err(netdev, "Matching reset found, skipping\n");
1393 mutex_unlock(&adapter->rwi_lock);
1394 return;
1395 }
1396 }
1397
1398 rwi = kzalloc(sizeof(*rwi), GFP_KERNEL);
1399 if (!rwi) {
1400 mutex_unlock(&adapter->rwi_lock);
1401 ibmvnic_close(netdev);
1402 return;
1403 }
1404
1405 rwi->reset_reason = reason;
1406 list_add_tail(&rwi->list, &adapter->rwi_list);
1407 mutex_unlock(&adapter->rwi_lock);
1408 schedule_work(&adapter->ibmvnic_reset);
1409}
1410
Thomas Falcon032c5e82015-12-21 11:26:06 -06001411static void ibmvnic_tx_timeout(struct net_device *dev)
1412{
1413 struct ibmvnic_adapter *adapter = netdev_priv(dev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001414
Nathan Fontenoted651a12017-05-03 14:04:38 -04001415 ibmvnic_reset(adapter, VNIC_RESET_TIMEOUT);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001416}
1417
1418static void remove_buff_from_pool(struct ibmvnic_adapter *adapter,
1419 struct ibmvnic_rx_buff *rx_buff)
1420{
1421 struct ibmvnic_rx_pool *pool = &adapter->rx_pool[rx_buff->pool_index];
1422
1423 rx_buff->skb = NULL;
1424
1425 pool->free_map[pool->next_alloc] = (int)(rx_buff - pool->rx_buff);
1426 pool->next_alloc = (pool->next_alloc + 1) % pool->size;
1427
1428 atomic_dec(&pool->available);
1429}
1430
1431static int ibmvnic_poll(struct napi_struct *napi, int budget)
1432{
1433 struct net_device *netdev = napi->dev;
1434 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1435 int scrq_num = (int)(napi - adapter->napi);
1436 int frames_processed = 0;
Nathan Fontenot152ce472017-05-26 10:30:54 -04001437
1438 if (adapter->resetting)
1439 return 0;
1440
Thomas Falcon032c5e82015-12-21 11:26:06 -06001441restart_poll:
1442 while (frames_processed < budget) {
1443 struct sk_buff *skb;
1444 struct ibmvnic_rx_buff *rx_buff;
1445 union sub_crq *next;
1446 u32 length;
1447 u16 offset;
1448 u8 flags = 0;
1449
1450 if (!pending_scrq(adapter, adapter->rx_scrq[scrq_num]))
1451 break;
1452 next = ibmvnic_next_scrq(adapter, adapter->rx_scrq[scrq_num]);
1453 rx_buff =
1454 (struct ibmvnic_rx_buff *)be64_to_cpu(next->
1455 rx_comp.correlator);
1456 /* do error checking */
1457 if (next->rx_comp.rc) {
1458 netdev_err(netdev, "rx error %x\n", next->rx_comp.rc);
1459 /* free the entry */
1460 next->rx_comp.first = 0;
1461 remove_buff_from_pool(adapter, rx_buff);
Nathan Fontenotca05e312017-05-03 14:05:14 -04001462 continue;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001463 }
1464
1465 length = be32_to_cpu(next->rx_comp.len);
1466 offset = be16_to_cpu(next->rx_comp.off_frame_data);
1467 flags = next->rx_comp.flags;
1468 skb = rx_buff->skb;
1469 skb_copy_to_linear_data(skb, rx_buff->data + offset,
1470 length);
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04001471
1472 /* VLAN Header has been stripped by the system firmware and
1473 * needs to be inserted by the driver
1474 */
1475 if (adapter->rx_vlan_header_insertion &&
1476 (flags & IBMVNIC_VLAN_STRIPPED))
1477 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
1478 ntohs(next->rx_comp.vlan_tci));
1479
Thomas Falcon032c5e82015-12-21 11:26:06 -06001480 /* free the entry */
1481 next->rx_comp.first = 0;
1482 remove_buff_from_pool(adapter, rx_buff);
1483
1484 skb_put(skb, length);
1485 skb->protocol = eth_type_trans(skb, netdev);
Thomas Falcon94ca3052017-05-03 14:05:20 -04001486 skb_record_rx_queue(skb, scrq_num);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001487
1488 if (flags & IBMVNIC_IP_CHKSUM_GOOD &&
1489 flags & IBMVNIC_TCP_UDP_CHKSUM_GOOD) {
1490 skb->ip_summed = CHECKSUM_UNNECESSARY;
1491 }
1492
1493 length = skb->len;
1494 napi_gro_receive(napi, skb); /* send it up */
1495 netdev->stats.rx_packets++;
1496 netdev->stats.rx_bytes += length;
1497 frames_processed++;
1498 }
Nathan Fontenot152ce472017-05-26 10:30:54 -04001499
1500 if (adapter->state != VNIC_CLOSING)
1501 replenish_rx_pool(adapter, &adapter->rx_pool[scrq_num]);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001502
1503 if (frames_processed < budget) {
1504 enable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
Eric Dumazet6ad20162017-01-30 08:22:01 -08001505 napi_complete_done(napi, frames_processed);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001506 if (pending_scrq(adapter, adapter->rx_scrq[scrq_num]) &&
1507 napi_reschedule(napi)) {
1508 disable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
1509 goto restart_poll;
1510 }
1511 }
1512 return frames_processed;
1513}
1514
1515#ifdef CONFIG_NET_POLL_CONTROLLER
1516static void ibmvnic_netpoll_controller(struct net_device *dev)
1517{
1518 struct ibmvnic_adapter *adapter = netdev_priv(dev);
1519 int i;
1520
1521 replenish_pools(netdev_priv(dev));
1522 for (i = 0; i < adapter->req_rx_queues; i++)
1523 ibmvnic_interrupt_rx(adapter->rx_scrq[i]->irq,
1524 adapter->rx_scrq[i]);
1525}
1526#endif
1527
1528static const struct net_device_ops ibmvnic_netdev_ops = {
1529 .ndo_open = ibmvnic_open,
1530 .ndo_stop = ibmvnic_close,
1531 .ndo_start_xmit = ibmvnic_xmit,
1532 .ndo_set_rx_mode = ibmvnic_set_multi,
1533 .ndo_set_mac_address = ibmvnic_set_mac,
1534 .ndo_validate_addr = eth_validate_addr,
Thomas Falcon032c5e82015-12-21 11:26:06 -06001535 .ndo_tx_timeout = ibmvnic_tx_timeout,
1536#ifdef CONFIG_NET_POLL_CONTROLLER
1537 .ndo_poll_controller = ibmvnic_netpoll_controller,
1538#endif
1539};
1540
1541/* ethtool functions */
1542
Philippe Reynes8a433792017-01-07 22:37:29 +01001543static int ibmvnic_get_link_ksettings(struct net_device *netdev,
1544 struct ethtool_link_ksettings *cmd)
Thomas Falcon032c5e82015-12-21 11:26:06 -06001545{
Philippe Reynes8a433792017-01-07 22:37:29 +01001546 u32 supported, advertising;
1547
1548 supported = (SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg |
Thomas Falcon032c5e82015-12-21 11:26:06 -06001549 SUPPORTED_FIBRE);
Philippe Reynes8a433792017-01-07 22:37:29 +01001550 advertising = (ADVERTISED_1000baseT_Full | ADVERTISED_Autoneg |
Thomas Falcon032c5e82015-12-21 11:26:06 -06001551 ADVERTISED_FIBRE);
Philippe Reynes8a433792017-01-07 22:37:29 +01001552 cmd->base.speed = SPEED_1000;
1553 cmd->base.duplex = DUPLEX_FULL;
1554 cmd->base.port = PORT_FIBRE;
1555 cmd->base.phy_address = 0;
1556 cmd->base.autoneg = AUTONEG_ENABLE;
1557
1558 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
1559 supported);
1560 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
1561 advertising);
1562
Thomas Falcon032c5e82015-12-21 11:26:06 -06001563 return 0;
1564}
1565
1566static void ibmvnic_get_drvinfo(struct net_device *dev,
1567 struct ethtool_drvinfo *info)
1568{
1569 strlcpy(info->driver, ibmvnic_driver_name, sizeof(info->driver));
1570 strlcpy(info->version, IBMVNIC_DRIVER_VERSION, sizeof(info->version));
1571}
1572
1573static u32 ibmvnic_get_msglevel(struct net_device *netdev)
1574{
1575 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1576
1577 return adapter->msg_enable;
1578}
1579
1580static void ibmvnic_set_msglevel(struct net_device *netdev, u32 data)
1581{
1582 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1583
1584 adapter->msg_enable = data;
1585}
1586
1587static u32 ibmvnic_get_link(struct net_device *netdev)
1588{
1589 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1590
1591 /* Don't need to send a query because we request a logical link up at
1592 * init and then we wait for link state indications
1593 */
1594 return adapter->logical_link_state;
1595}
1596
1597static void ibmvnic_get_ringparam(struct net_device *netdev,
1598 struct ethtool_ringparam *ring)
1599{
1600 ring->rx_max_pending = 0;
1601 ring->tx_max_pending = 0;
1602 ring->rx_mini_max_pending = 0;
1603 ring->rx_jumbo_max_pending = 0;
1604 ring->rx_pending = 0;
1605 ring->tx_pending = 0;
1606 ring->rx_mini_pending = 0;
1607 ring->rx_jumbo_pending = 0;
1608}
1609
1610static void ibmvnic_get_strings(struct net_device *dev, u32 stringset, u8 *data)
1611{
1612 int i;
1613
1614 if (stringset != ETH_SS_STATS)
1615 return;
1616
1617 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++, data += ETH_GSTRING_LEN)
1618 memcpy(data, ibmvnic_stats[i].name, ETH_GSTRING_LEN);
1619}
1620
1621static int ibmvnic_get_sset_count(struct net_device *dev, int sset)
1622{
1623 switch (sset) {
1624 case ETH_SS_STATS:
1625 return ARRAY_SIZE(ibmvnic_stats);
1626 default:
1627 return -EOPNOTSUPP;
1628 }
1629}
1630
1631static void ibmvnic_get_ethtool_stats(struct net_device *dev,
1632 struct ethtool_stats *stats, u64 *data)
1633{
1634 struct ibmvnic_adapter *adapter = netdev_priv(dev);
1635 union ibmvnic_crq crq;
1636 int i;
1637
1638 memset(&crq, 0, sizeof(crq));
1639 crq.request_statistics.first = IBMVNIC_CRQ_CMD;
1640 crq.request_statistics.cmd = REQUEST_STATISTICS;
1641 crq.request_statistics.ioba = cpu_to_be32(adapter->stats_token);
1642 crq.request_statistics.len =
1643 cpu_to_be32(sizeof(struct ibmvnic_statistics));
Thomas Falcon032c5e82015-12-21 11:26:06 -06001644
1645 /* Wait for data to be written */
1646 init_completion(&adapter->stats_done);
Nathan Fontenotdb5d0b52017-02-10 13:45:05 -05001647 ibmvnic_send_crq(adapter, &crq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001648 wait_for_completion(&adapter->stats_done);
1649
1650 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++)
1651 data[i] = IBMVNIC_GET_STAT(adapter, ibmvnic_stats[i].offset);
1652}
1653
1654static const struct ethtool_ops ibmvnic_ethtool_ops = {
Thomas Falcon032c5e82015-12-21 11:26:06 -06001655 .get_drvinfo = ibmvnic_get_drvinfo,
1656 .get_msglevel = ibmvnic_get_msglevel,
1657 .set_msglevel = ibmvnic_set_msglevel,
1658 .get_link = ibmvnic_get_link,
1659 .get_ringparam = ibmvnic_get_ringparam,
1660 .get_strings = ibmvnic_get_strings,
1661 .get_sset_count = ibmvnic_get_sset_count,
1662 .get_ethtool_stats = ibmvnic_get_ethtool_stats,
Philippe Reynes8a433792017-01-07 22:37:29 +01001663 .get_link_ksettings = ibmvnic_get_link_ksettings,
Thomas Falcon032c5e82015-12-21 11:26:06 -06001664};
1665
1666/* Routines for managing CRQs/sCRQs */
1667
1668static void release_sub_crq_queue(struct ibmvnic_adapter *adapter,
1669 struct ibmvnic_sub_crq_queue *scrq)
1670{
1671 struct device *dev = &adapter->vdev->dev;
1672 long rc;
1673
1674 netdev_dbg(adapter->netdev, "Releasing sub-CRQ\n");
1675
1676 /* Close the sub-crqs */
1677 do {
1678 rc = plpar_hcall_norets(H_FREE_SUB_CRQ,
1679 adapter->vdev->unit_address,
1680 scrq->crq_num);
1681 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
1682
Thomas Falconffa73852017-04-19 13:44:29 -04001683 if (rc) {
1684 netdev_err(adapter->netdev,
1685 "Failed to release sub-CRQ %16lx, rc = %ld\n",
1686 scrq->crq_num, rc);
1687 }
1688
Thomas Falcon032c5e82015-12-21 11:26:06 -06001689 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
1690 DMA_BIDIRECTIONAL);
1691 free_pages((unsigned long)scrq->msgs, 2);
1692 kfree(scrq);
1693}
1694
1695static struct ibmvnic_sub_crq_queue *init_sub_crq_queue(struct ibmvnic_adapter
1696 *adapter)
1697{
1698 struct device *dev = &adapter->vdev->dev;
1699 struct ibmvnic_sub_crq_queue *scrq;
1700 int rc;
1701
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04001702 scrq = kzalloc(sizeof(*scrq), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001703 if (!scrq)
1704 return NULL;
1705
Nathan Fontenot7f7adc52017-04-19 13:45:16 -04001706 scrq->msgs =
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04001707 (union sub_crq *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, 2);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001708 if (!scrq->msgs) {
1709 dev_warn(dev, "Couldn't allocate crq queue messages page\n");
1710 goto zero_page_failed;
1711 }
1712
1713 scrq->msg_token = dma_map_single(dev, scrq->msgs, 4 * PAGE_SIZE,
1714 DMA_BIDIRECTIONAL);
1715 if (dma_mapping_error(dev, scrq->msg_token)) {
1716 dev_warn(dev, "Couldn't map crq queue messages page\n");
1717 goto map_failed;
1718 }
1719
1720 rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
1721 4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
1722
1723 if (rc == H_RESOURCE)
1724 rc = ibmvnic_reset_crq(adapter);
1725
1726 if (rc == H_CLOSED) {
1727 dev_warn(dev, "Partner adapter not ready, waiting.\n");
1728 } else if (rc) {
1729 dev_warn(dev, "Error %d registering sub-crq\n", rc);
1730 goto reg_failed;
1731 }
1732
Thomas Falcon032c5e82015-12-21 11:26:06 -06001733 scrq->adapter = adapter;
1734 scrq->size = 4 * PAGE_SIZE / sizeof(*scrq->msgs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001735 spin_lock_init(&scrq->lock);
1736
1737 netdev_dbg(adapter->netdev,
1738 "sub-crq initialized, num %lx, hw_irq=%lx, irq=%x\n",
1739 scrq->crq_num, scrq->hw_irq, scrq->irq);
1740
1741 return scrq;
1742
Thomas Falcon032c5e82015-12-21 11:26:06 -06001743reg_failed:
1744 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
1745 DMA_BIDIRECTIONAL);
1746map_failed:
1747 free_pages((unsigned long)scrq->msgs, 2);
1748zero_page_failed:
1749 kfree(scrq);
1750
1751 return NULL;
1752}
1753
1754static void release_sub_crqs(struct ibmvnic_adapter *adapter)
1755{
1756 int i;
1757
1758 if (adapter->tx_scrq) {
Nathan Fontenotb5108882017-03-30 02:49:18 -04001759 for (i = 0; i < adapter->req_tx_queues; i++) {
1760 if (!adapter->tx_scrq[i])
1761 continue;
1762
1763 if (adapter->tx_scrq[i]->irq) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06001764 free_irq(adapter->tx_scrq[i]->irq,
1765 adapter->tx_scrq[i]);
Thomas Falcon88eb98a2016-07-06 15:35:16 -05001766 irq_dispose_mapping(adapter->tx_scrq[i]->irq);
Nathan Fontenotb5108882017-03-30 02:49:18 -04001767 adapter->tx_scrq[i]->irq = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001768 }
Nathan Fontenotb5108882017-03-30 02:49:18 -04001769
1770 release_sub_crq_queue(adapter, adapter->tx_scrq[i]);
1771 }
1772
Nathan Fontenot9501df32017-03-15 23:38:07 -04001773 kfree(adapter->tx_scrq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001774 adapter->tx_scrq = NULL;
1775 }
1776
1777 if (adapter->rx_scrq) {
Nathan Fontenotb5108882017-03-30 02:49:18 -04001778 for (i = 0; i < adapter->req_rx_queues; i++) {
1779 if (!adapter->rx_scrq[i])
1780 continue;
1781
1782 if (adapter->rx_scrq[i]->irq) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06001783 free_irq(adapter->rx_scrq[i]->irq,
1784 adapter->rx_scrq[i]);
Thomas Falcon88eb98a2016-07-06 15:35:16 -05001785 irq_dispose_mapping(adapter->rx_scrq[i]->irq);
Nathan Fontenotb5108882017-03-30 02:49:18 -04001786 adapter->rx_scrq[i]->irq = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001787 }
Nathan Fontenotb5108882017-03-30 02:49:18 -04001788
1789 release_sub_crq_queue(adapter, adapter->rx_scrq[i]);
1790 }
1791
Nathan Fontenot9501df32017-03-15 23:38:07 -04001792 kfree(adapter->rx_scrq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001793 adapter->rx_scrq = NULL;
1794 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001795}
1796
1797static int disable_scrq_irq(struct ibmvnic_adapter *adapter,
1798 struct ibmvnic_sub_crq_queue *scrq)
1799{
1800 struct device *dev = &adapter->vdev->dev;
1801 unsigned long rc;
1802
1803 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
1804 H_DISABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
1805 if (rc)
1806 dev_err(dev, "Couldn't disable scrq irq 0x%lx. rc=%ld\n",
1807 scrq->hw_irq, rc);
1808 return rc;
1809}
1810
1811static int enable_scrq_irq(struct ibmvnic_adapter *adapter,
1812 struct ibmvnic_sub_crq_queue *scrq)
1813{
1814 struct device *dev = &adapter->vdev->dev;
1815 unsigned long rc;
1816
1817 if (scrq->hw_irq > 0x100000000ULL) {
1818 dev_err(dev, "bad hw_irq = %lx\n", scrq->hw_irq);
1819 return 1;
1820 }
1821
1822 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
1823 H_ENABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
1824 if (rc)
1825 dev_err(dev, "Couldn't enable scrq irq 0x%lx. rc=%ld\n",
1826 scrq->hw_irq, rc);
1827 return rc;
1828}
1829
1830static int ibmvnic_complete_tx(struct ibmvnic_adapter *adapter,
1831 struct ibmvnic_sub_crq_queue *scrq)
1832{
1833 struct device *dev = &adapter->vdev->dev;
1834 struct ibmvnic_tx_buff *txbuff;
1835 union sub_crq *next;
1836 int index;
1837 int i, j;
Thomas Falconad7775d2016-04-01 17:20:34 -05001838 u8 first;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001839
1840restart_loop:
1841 while (pending_scrq(adapter, scrq)) {
1842 unsigned int pool = scrq->pool_index;
1843
1844 next = ibmvnic_next_scrq(adapter, scrq);
1845 for (i = 0; i < next->tx_comp.num_comps; i++) {
1846 if (next->tx_comp.rcs[i]) {
1847 dev_err(dev, "tx error %x\n",
1848 next->tx_comp.rcs[i]);
1849 continue;
1850 }
1851 index = be32_to_cpu(next->tx_comp.correlators[i]);
1852 txbuff = &adapter->tx_pool[pool].tx_buff[index];
1853
1854 for (j = 0; j < IBMVNIC_MAX_FRAGS_PER_CRQ; j++) {
1855 if (!txbuff->data_dma[j])
1856 continue;
1857
1858 txbuff->data_dma[j] = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001859 }
Thomas Falconad7775d2016-04-01 17:20:34 -05001860 /* if sub_crq was sent indirectly */
1861 first = txbuff->indir_arr[0].generic.first;
1862 if (first == IBMVNIC_CRQ_CMD) {
1863 dma_unmap_single(dev, txbuff->indir_dma,
1864 sizeof(txbuff->indir_arr),
1865 DMA_TO_DEVICE);
1866 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001867
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001868 if (txbuff->last_frag) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06001869 dev_kfree_skb_any(txbuff->skb);
Nathan Fontenot7c3e7de2017-05-03 14:05:25 -04001870 txbuff->skb = NULL;
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001871 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001872
1873 adapter->tx_pool[pool].free_map[adapter->tx_pool[pool].
1874 producer_index] = index;
1875 adapter->tx_pool[pool].producer_index =
1876 (adapter->tx_pool[pool].producer_index + 1) %
Thomas Falcon068d9f92017-03-05 12:18:42 -06001877 adapter->req_tx_entries_per_subcrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001878 }
1879 /* remove tx_comp scrq*/
1880 next->tx_comp.first = 0;
Nathan Fontenot7c3e7de2017-05-03 14:05:25 -04001881
1882 if (atomic_sub_return(next->tx_comp.num_comps, &scrq->used) <=
1883 (adapter->req_tx_entries_per_subcrq / 2) &&
1884 __netif_subqueue_stopped(adapter->netdev,
1885 scrq->pool_index)) {
1886 netif_wake_subqueue(adapter->netdev, scrq->pool_index);
1887 netdev_info(adapter->netdev, "Started queue %d\n",
1888 scrq->pool_index);
1889 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001890 }
1891
1892 enable_scrq_irq(adapter, scrq);
1893
1894 if (pending_scrq(adapter, scrq)) {
1895 disable_scrq_irq(adapter, scrq);
1896 goto restart_loop;
1897 }
1898
1899 return 0;
1900}
1901
1902static irqreturn_t ibmvnic_interrupt_tx(int irq, void *instance)
1903{
1904 struct ibmvnic_sub_crq_queue *scrq = instance;
1905 struct ibmvnic_adapter *adapter = scrq->adapter;
1906
1907 disable_scrq_irq(adapter, scrq);
1908 ibmvnic_complete_tx(adapter, scrq);
1909
1910 return IRQ_HANDLED;
1911}
1912
1913static irqreturn_t ibmvnic_interrupt_rx(int irq, void *instance)
1914{
1915 struct ibmvnic_sub_crq_queue *scrq = instance;
1916 struct ibmvnic_adapter *adapter = scrq->adapter;
1917
1918 if (napi_schedule_prep(&adapter->napi[scrq->scrq_num])) {
1919 disable_scrq_irq(adapter, scrq);
1920 __napi_schedule(&adapter->napi[scrq->scrq_num]);
1921 }
1922
1923 return IRQ_HANDLED;
1924}
1925
Thomas Falconea22d512016-07-06 15:35:17 -05001926static int init_sub_crq_irqs(struct ibmvnic_adapter *adapter)
1927{
1928 struct device *dev = &adapter->vdev->dev;
1929 struct ibmvnic_sub_crq_queue *scrq;
1930 int i = 0, j = 0;
1931 int rc = 0;
1932
1933 for (i = 0; i < adapter->req_tx_queues; i++) {
1934 scrq = adapter->tx_scrq[i];
1935 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
1936
Michael Ellerman99c17902016-09-10 19:59:05 +10001937 if (!scrq->irq) {
Thomas Falconea22d512016-07-06 15:35:17 -05001938 rc = -EINVAL;
1939 dev_err(dev, "Error mapping irq\n");
1940 goto req_tx_irq_failed;
1941 }
1942
1943 rc = request_irq(scrq->irq, ibmvnic_interrupt_tx,
1944 0, "ibmvnic_tx", scrq);
1945
1946 if (rc) {
1947 dev_err(dev, "Couldn't register tx irq 0x%x. rc=%d\n",
1948 scrq->irq, rc);
1949 irq_dispose_mapping(scrq->irq);
1950 goto req_rx_irq_failed;
1951 }
1952 }
1953
1954 for (i = 0; i < adapter->req_rx_queues; i++) {
1955 scrq = adapter->rx_scrq[i];
1956 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
Michael Ellerman99c17902016-09-10 19:59:05 +10001957 if (!scrq->irq) {
Thomas Falconea22d512016-07-06 15:35:17 -05001958 rc = -EINVAL;
1959 dev_err(dev, "Error mapping irq\n");
1960 goto req_rx_irq_failed;
1961 }
1962 rc = request_irq(scrq->irq, ibmvnic_interrupt_rx,
1963 0, "ibmvnic_rx", scrq);
1964 if (rc) {
1965 dev_err(dev, "Couldn't register rx irq 0x%x. rc=%d\n",
1966 scrq->irq, rc);
1967 irq_dispose_mapping(scrq->irq);
1968 goto req_rx_irq_failed;
1969 }
1970 }
1971 return rc;
1972
1973req_rx_irq_failed:
Thomas Falcon8bf371e2016-10-27 12:28:52 -05001974 for (j = 0; j < i; j++) {
Thomas Falconea22d512016-07-06 15:35:17 -05001975 free_irq(adapter->rx_scrq[j]->irq, adapter->rx_scrq[j]);
1976 irq_dispose_mapping(adapter->rx_scrq[j]->irq);
Thomas Falcon8bf371e2016-10-27 12:28:52 -05001977 }
Thomas Falconea22d512016-07-06 15:35:17 -05001978 i = adapter->req_tx_queues;
1979req_tx_irq_failed:
Thomas Falcon8bf371e2016-10-27 12:28:52 -05001980 for (j = 0; j < i; j++) {
Thomas Falconea22d512016-07-06 15:35:17 -05001981 free_irq(adapter->tx_scrq[j]->irq, adapter->tx_scrq[j]);
1982 irq_dispose_mapping(adapter->rx_scrq[j]->irq);
Thomas Falcon8bf371e2016-10-27 12:28:52 -05001983 }
Nathan Fontenotb5108882017-03-30 02:49:18 -04001984 release_sub_crqs(adapter);
Thomas Falconea22d512016-07-06 15:35:17 -05001985 return rc;
1986}
1987
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04001988static int init_sub_crqs(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06001989{
1990 struct device *dev = &adapter->vdev->dev;
1991 struct ibmvnic_sub_crq_queue **allqueues;
1992 int registered_queues = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001993 int total_queues;
1994 int more = 0;
Thomas Falconea22d512016-07-06 15:35:17 -05001995 int i;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001996
Thomas Falcon032c5e82015-12-21 11:26:06 -06001997 total_queues = adapter->req_tx_queues + adapter->req_rx_queues;
1998
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04001999 allqueues = kcalloc(total_queues, sizeof(*allqueues), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002000 if (!allqueues)
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002001 return -1;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002002
2003 for (i = 0; i < total_queues; i++) {
2004 allqueues[i] = init_sub_crq_queue(adapter);
2005 if (!allqueues[i]) {
2006 dev_warn(dev, "Couldn't allocate all sub-crqs\n");
2007 break;
2008 }
2009 registered_queues++;
2010 }
2011
2012 /* Make sure we were able to register the minimum number of queues */
2013 if (registered_queues <
2014 adapter->min_tx_queues + adapter->min_rx_queues) {
2015 dev_err(dev, "Fatal: Couldn't init min number of sub-crqs\n");
2016 goto tx_failed;
2017 }
2018
2019 /* Distribute the failed allocated queues*/
2020 for (i = 0; i < total_queues - registered_queues + more ; i++) {
2021 netdev_dbg(adapter->netdev, "Reducing number of queues\n");
2022 switch (i % 3) {
2023 case 0:
2024 if (adapter->req_rx_queues > adapter->min_rx_queues)
2025 adapter->req_rx_queues--;
2026 else
2027 more++;
2028 break;
2029 case 1:
2030 if (adapter->req_tx_queues > adapter->min_tx_queues)
2031 adapter->req_tx_queues--;
2032 else
2033 more++;
2034 break;
2035 }
2036 }
2037
2038 adapter->tx_scrq = kcalloc(adapter->req_tx_queues,
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04002039 sizeof(*adapter->tx_scrq), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002040 if (!adapter->tx_scrq)
2041 goto tx_failed;
2042
2043 for (i = 0; i < adapter->req_tx_queues; i++) {
2044 adapter->tx_scrq[i] = allqueues[i];
2045 adapter->tx_scrq[i]->pool_index = i;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002046 }
2047
2048 adapter->rx_scrq = kcalloc(adapter->req_rx_queues,
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04002049 sizeof(*adapter->rx_scrq), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002050 if (!adapter->rx_scrq)
2051 goto rx_failed;
2052
2053 for (i = 0; i < adapter->req_rx_queues; i++) {
2054 adapter->rx_scrq[i] = allqueues[i + adapter->req_tx_queues];
2055 adapter->rx_scrq[i]->scrq_num = i;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002056 }
2057
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002058 kfree(allqueues);
2059 return 0;
2060
2061rx_failed:
2062 kfree(adapter->tx_scrq);
2063 adapter->tx_scrq = NULL;
2064tx_failed:
2065 for (i = 0; i < registered_queues; i++)
2066 release_sub_crq_queue(adapter, allqueues[i]);
2067 kfree(allqueues);
2068 return -1;
2069}
2070
2071static void ibmvnic_send_req_caps(struct ibmvnic_adapter *adapter, int retry)
2072{
2073 struct device *dev = &adapter->vdev->dev;
2074 union ibmvnic_crq crq;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002075
2076 if (!retry) {
2077 /* Sub-CRQ entries are 32 byte long */
2078 int entries_page = 4 * PAGE_SIZE / (sizeof(u64) * 4);
2079
2080 if (adapter->min_tx_entries_per_subcrq > entries_page ||
2081 adapter->min_rx_add_entries_per_subcrq > entries_page) {
2082 dev_err(dev, "Fatal, invalid entries per sub-crq\n");
2083 return;
2084 }
2085
2086 /* Get the minimum between the queried max and the entries
2087 * that fit in our PAGE_SIZE
2088 */
2089 adapter->req_tx_entries_per_subcrq =
2090 adapter->max_tx_entries_per_subcrq > entries_page ?
2091 entries_page : adapter->max_tx_entries_per_subcrq;
2092 adapter->req_rx_add_entries_per_subcrq =
2093 adapter->max_rx_add_entries_per_subcrq > entries_page ?
2094 entries_page : adapter->max_rx_add_entries_per_subcrq;
2095
2096 adapter->req_tx_queues = adapter->opt_tx_comp_sub_queues;
2097 adapter->req_rx_queues = adapter->opt_rx_comp_queues;
2098 adapter->req_rx_add_queues = adapter->max_rx_add_queues;
2099
2100 adapter->req_mtu = adapter->netdev->mtu + ETH_HLEN;
2101 }
2102
Thomas Falcon032c5e82015-12-21 11:26:06 -06002103 memset(&crq, 0, sizeof(crq));
2104 crq.request_capability.first = IBMVNIC_CRQ_CMD;
2105 crq.request_capability.cmd = REQUEST_CAPABILITY;
2106
2107 crq.request_capability.capability = cpu_to_be16(REQ_TX_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06002108 crq.request_capability.number = cpu_to_be64(adapter->req_tx_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06002109 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002110 ibmvnic_send_crq(adapter, &crq);
2111
2112 crq.request_capability.capability = cpu_to_be16(REQ_RX_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06002113 crq.request_capability.number = cpu_to_be64(adapter->req_rx_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06002114 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002115 ibmvnic_send_crq(adapter, &crq);
2116
2117 crq.request_capability.capability = cpu_to_be16(REQ_RX_ADD_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06002118 crq.request_capability.number = cpu_to_be64(adapter->req_rx_add_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06002119 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002120 ibmvnic_send_crq(adapter, &crq);
2121
2122 crq.request_capability.capability =
2123 cpu_to_be16(REQ_TX_ENTRIES_PER_SUBCRQ);
2124 crq.request_capability.number =
Thomas Falconde89e852016-03-01 10:20:09 -06002125 cpu_to_be64(adapter->req_tx_entries_per_subcrq);
Thomas Falcon901e0402017-02-15 12:17:59 -06002126 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002127 ibmvnic_send_crq(adapter, &crq);
2128
2129 crq.request_capability.capability =
2130 cpu_to_be16(REQ_RX_ADD_ENTRIES_PER_SUBCRQ);
2131 crq.request_capability.number =
Thomas Falconde89e852016-03-01 10:20:09 -06002132 cpu_to_be64(adapter->req_rx_add_entries_per_subcrq);
Thomas Falcon901e0402017-02-15 12:17:59 -06002133 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002134 ibmvnic_send_crq(adapter, &crq);
2135
2136 crq.request_capability.capability = cpu_to_be16(REQ_MTU);
Thomas Falconde89e852016-03-01 10:20:09 -06002137 crq.request_capability.number = cpu_to_be64(adapter->req_mtu);
Thomas Falcon901e0402017-02-15 12:17:59 -06002138 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002139 ibmvnic_send_crq(adapter, &crq);
2140
2141 if (adapter->netdev->flags & IFF_PROMISC) {
2142 if (adapter->promisc_supported) {
2143 crq.request_capability.capability =
2144 cpu_to_be16(PROMISC_REQUESTED);
Thomas Falconde89e852016-03-01 10:20:09 -06002145 crq.request_capability.number = cpu_to_be64(1);
Thomas Falcon901e0402017-02-15 12:17:59 -06002146 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002147 ibmvnic_send_crq(adapter, &crq);
2148 }
2149 } else {
2150 crq.request_capability.capability =
2151 cpu_to_be16(PROMISC_REQUESTED);
Thomas Falconde89e852016-03-01 10:20:09 -06002152 crq.request_capability.number = cpu_to_be64(0);
Thomas Falcon901e0402017-02-15 12:17:59 -06002153 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002154 ibmvnic_send_crq(adapter, &crq);
2155 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002156}
2157
2158static int pending_scrq(struct ibmvnic_adapter *adapter,
2159 struct ibmvnic_sub_crq_queue *scrq)
2160{
2161 union sub_crq *entry = &scrq->msgs[scrq->cur];
2162
Nathan Fontenot90c80142017-05-03 14:04:32 -04002163 if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP ||
2164 adapter->state == VNIC_CLOSING)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002165 return 1;
2166 else
2167 return 0;
2168}
2169
2170static union sub_crq *ibmvnic_next_scrq(struct ibmvnic_adapter *adapter,
2171 struct ibmvnic_sub_crq_queue *scrq)
2172{
2173 union sub_crq *entry;
2174 unsigned long flags;
2175
2176 spin_lock_irqsave(&scrq->lock, flags);
2177 entry = &scrq->msgs[scrq->cur];
2178 if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP) {
2179 if (++scrq->cur == scrq->size)
2180 scrq->cur = 0;
2181 } else {
2182 entry = NULL;
2183 }
2184 spin_unlock_irqrestore(&scrq->lock, flags);
2185
2186 return entry;
2187}
2188
2189static union ibmvnic_crq *ibmvnic_next_crq(struct ibmvnic_adapter *adapter)
2190{
2191 struct ibmvnic_crq_queue *queue = &adapter->crq;
2192 union ibmvnic_crq *crq;
2193
2194 crq = &queue->msgs[queue->cur];
2195 if (crq->generic.first & IBMVNIC_CRQ_CMD_RSP) {
2196 if (++queue->cur == queue->size)
2197 queue->cur = 0;
2198 } else {
2199 crq = NULL;
2200 }
2201
2202 return crq;
2203}
2204
2205static int send_subcrq(struct ibmvnic_adapter *adapter, u64 remote_handle,
2206 union sub_crq *sub_crq)
2207{
2208 unsigned int ua = adapter->vdev->unit_address;
2209 struct device *dev = &adapter->vdev->dev;
2210 u64 *u64_crq = (u64 *)sub_crq;
2211 int rc;
2212
2213 netdev_dbg(adapter->netdev,
2214 "Sending sCRQ %016lx: %016lx %016lx %016lx %016lx\n",
2215 (unsigned long int)cpu_to_be64(remote_handle),
2216 (unsigned long int)cpu_to_be64(u64_crq[0]),
2217 (unsigned long int)cpu_to_be64(u64_crq[1]),
2218 (unsigned long int)cpu_to_be64(u64_crq[2]),
2219 (unsigned long int)cpu_to_be64(u64_crq[3]));
2220
2221 /* Make sure the hypervisor sees the complete request */
2222 mb();
2223
2224 rc = plpar_hcall_norets(H_SEND_SUB_CRQ, ua,
2225 cpu_to_be64(remote_handle),
2226 cpu_to_be64(u64_crq[0]),
2227 cpu_to_be64(u64_crq[1]),
2228 cpu_to_be64(u64_crq[2]),
2229 cpu_to_be64(u64_crq[3]));
2230
2231 if (rc) {
2232 if (rc == H_CLOSED)
2233 dev_warn(dev, "CRQ Queue closed\n");
2234 dev_err(dev, "Send error (rc=%d)\n", rc);
2235 }
2236
2237 return rc;
2238}
2239
Thomas Falconad7775d2016-04-01 17:20:34 -05002240static int send_subcrq_indirect(struct ibmvnic_adapter *adapter,
2241 u64 remote_handle, u64 ioba, u64 num_entries)
2242{
2243 unsigned int ua = adapter->vdev->unit_address;
2244 struct device *dev = &adapter->vdev->dev;
2245 int rc;
2246
2247 /* Make sure the hypervisor sees the complete request */
2248 mb();
2249 rc = plpar_hcall_norets(H_SEND_SUB_CRQ_INDIRECT, ua,
2250 cpu_to_be64(remote_handle),
2251 ioba, num_entries);
2252
2253 if (rc) {
2254 if (rc == H_CLOSED)
2255 dev_warn(dev, "CRQ Queue closed\n");
2256 dev_err(dev, "Send (indirect) error (rc=%d)\n", rc);
2257 }
2258
2259 return rc;
2260}
2261
Thomas Falcon032c5e82015-12-21 11:26:06 -06002262static int ibmvnic_send_crq(struct ibmvnic_adapter *adapter,
2263 union ibmvnic_crq *crq)
2264{
2265 unsigned int ua = adapter->vdev->unit_address;
2266 struct device *dev = &adapter->vdev->dev;
2267 u64 *u64_crq = (u64 *)crq;
2268 int rc;
2269
2270 netdev_dbg(adapter->netdev, "Sending CRQ: %016lx %016lx\n",
2271 (unsigned long int)cpu_to_be64(u64_crq[0]),
2272 (unsigned long int)cpu_to_be64(u64_crq[1]));
2273
2274 /* Make sure the hypervisor sees the complete request */
2275 mb();
2276
2277 rc = plpar_hcall_norets(H_SEND_CRQ, ua,
2278 cpu_to_be64(u64_crq[0]),
2279 cpu_to_be64(u64_crq[1]));
2280
2281 if (rc) {
2282 if (rc == H_CLOSED)
2283 dev_warn(dev, "CRQ Queue closed\n");
2284 dev_warn(dev, "Send error (rc=%d)\n", rc);
2285 }
2286
2287 return rc;
2288}
2289
2290static int ibmvnic_send_crq_init(struct ibmvnic_adapter *adapter)
2291{
2292 union ibmvnic_crq crq;
2293
2294 memset(&crq, 0, sizeof(crq));
2295 crq.generic.first = IBMVNIC_CRQ_INIT_CMD;
2296 crq.generic.cmd = IBMVNIC_CRQ_INIT;
2297 netdev_dbg(adapter->netdev, "Sending CRQ init\n");
2298
2299 return ibmvnic_send_crq(adapter, &crq);
2300}
2301
Thomas Falcon032c5e82015-12-21 11:26:06 -06002302static int send_version_xchg(struct ibmvnic_adapter *adapter)
2303{
2304 union ibmvnic_crq crq;
2305
2306 memset(&crq, 0, sizeof(crq));
2307 crq.version_exchange.first = IBMVNIC_CRQ_CMD;
2308 crq.version_exchange.cmd = VERSION_EXCHANGE;
2309 crq.version_exchange.version = cpu_to_be16(ibmvnic_version);
2310
2311 return ibmvnic_send_crq(adapter, &crq);
2312}
2313
2314static void send_login(struct ibmvnic_adapter *adapter)
2315{
2316 struct ibmvnic_login_rsp_buffer *login_rsp_buffer;
2317 struct ibmvnic_login_buffer *login_buffer;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002318 struct device *dev = &adapter->vdev->dev;
2319 dma_addr_t rsp_buffer_token;
2320 dma_addr_t buffer_token;
2321 size_t rsp_buffer_size;
2322 union ibmvnic_crq crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002323 size_t buffer_size;
2324 __be64 *tx_list_p;
2325 __be64 *rx_list_p;
2326 int i;
2327
2328 buffer_size =
2329 sizeof(struct ibmvnic_login_buffer) +
2330 sizeof(u64) * (adapter->req_tx_queues + adapter->req_rx_queues);
2331
2332 login_buffer = kmalloc(buffer_size, GFP_ATOMIC);
2333 if (!login_buffer)
2334 goto buf_alloc_failed;
2335
2336 buffer_token = dma_map_single(dev, login_buffer, buffer_size,
2337 DMA_TO_DEVICE);
2338 if (dma_mapping_error(dev, buffer_token)) {
2339 dev_err(dev, "Couldn't map login buffer\n");
2340 goto buf_map_failed;
2341 }
2342
John Allen498cd8e2016-04-06 11:49:55 -05002343 rsp_buffer_size = sizeof(struct ibmvnic_login_rsp_buffer) +
2344 sizeof(u64) * adapter->req_tx_queues +
2345 sizeof(u64) * adapter->req_rx_queues +
2346 sizeof(u64) * adapter->req_rx_queues +
2347 sizeof(u8) * IBMVNIC_TX_DESC_VERSIONS;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002348
2349 login_rsp_buffer = kmalloc(rsp_buffer_size, GFP_ATOMIC);
2350 if (!login_rsp_buffer)
2351 goto buf_rsp_alloc_failed;
2352
2353 rsp_buffer_token = dma_map_single(dev, login_rsp_buffer,
2354 rsp_buffer_size, DMA_FROM_DEVICE);
2355 if (dma_mapping_error(dev, rsp_buffer_token)) {
2356 dev_err(dev, "Couldn't map login rsp buffer\n");
2357 goto buf_rsp_map_failed;
2358 }
Nathan Fontenot661a2622017-04-19 13:44:58 -04002359
Thomas Falcon032c5e82015-12-21 11:26:06 -06002360 adapter->login_buf = login_buffer;
2361 adapter->login_buf_token = buffer_token;
2362 adapter->login_buf_sz = buffer_size;
2363 adapter->login_rsp_buf = login_rsp_buffer;
2364 adapter->login_rsp_buf_token = rsp_buffer_token;
2365 adapter->login_rsp_buf_sz = rsp_buffer_size;
2366
2367 login_buffer->len = cpu_to_be32(buffer_size);
2368 login_buffer->version = cpu_to_be32(INITIAL_VERSION_LB);
2369 login_buffer->num_txcomp_subcrqs = cpu_to_be32(adapter->req_tx_queues);
2370 login_buffer->off_txcomp_subcrqs =
2371 cpu_to_be32(sizeof(struct ibmvnic_login_buffer));
2372 login_buffer->num_rxcomp_subcrqs = cpu_to_be32(adapter->req_rx_queues);
2373 login_buffer->off_rxcomp_subcrqs =
2374 cpu_to_be32(sizeof(struct ibmvnic_login_buffer) +
2375 sizeof(u64) * adapter->req_tx_queues);
2376 login_buffer->login_rsp_ioba = cpu_to_be32(rsp_buffer_token);
2377 login_buffer->login_rsp_len = cpu_to_be32(rsp_buffer_size);
2378
2379 tx_list_p = (__be64 *)((char *)login_buffer +
2380 sizeof(struct ibmvnic_login_buffer));
2381 rx_list_p = (__be64 *)((char *)login_buffer +
2382 sizeof(struct ibmvnic_login_buffer) +
2383 sizeof(u64) * adapter->req_tx_queues);
2384
2385 for (i = 0; i < adapter->req_tx_queues; i++) {
2386 if (adapter->tx_scrq[i]) {
2387 tx_list_p[i] = cpu_to_be64(adapter->tx_scrq[i]->
2388 crq_num);
2389 }
2390 }
2391
2392 for (i = 0; i < adapter->req_rx_queues; i++) {
2393 if (adapter->rx_scrq[i]) {
2394 rx_list_p[i] = cpu_to_be64(adapter->rx_scrq[i]->
2395 crq_num);
2396 }
2397 }
2398
2399 netdev_dbg(adapter->netdev, "Login Buffer:\n");
2400 for (i = 0; i < (adapter->login_buf_sz - 1) / 8 + 1; i++) {
2401 netdev_dbg(adapter->netdev, "%016lx\n",
2402 ((unsigned long int *)(adapter->login_buf))[i]);
2403 }
2404
2405 memset(&crq, 0, sizeof(crq));
2406 crq.login.first = IBMVNIC_CRQ_CMD;
2407 crq.login.cmd = LOGIN;
2408 crq.login.ioba = cpu_to_be32(buffer_token);
2409 crq.login.len = cpu_to_be32(buffer_size);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002410 ibmvnic_send_crq(adapter, &crq);
2411
2412 return;
2413
Thomas Falcon032c5e82015-12-21 11:26:06 -06002414buf_rsp_map_failed:
2415 kfree(login_rsp_buffer);
2416buf_rsp_alloc_failed:
2417 dma_unmap_single(dev, buffer_token, buffer_size, DMA_TO_DEVICE);
2418buf_map_failed:
2419 kfree(login_buffer);
2420buf_alloc_failed:
2421 return;
2422}
2423
2424static void send_request_map(struct ibmvnic_adapter *adapter, dma_addr_t addr,
2425 u32 len, u8 map_id)
2426{
2427 union ibmvnic_crq crq;
2428
2429 memset(&crq, 0, sizeof(crq));
2430 crq.request_map.first = IBMVNIC_CRQ_CMD;
2431 crq.request_map.cmd = REQUEST_MAP;
2432 crq.request_map.map_id = map_id;
2433 crq.request_map.ioba = cpu_to_be32(addr);
2434 crq.request_map.len = cpu_to_be32(len);
2435 ibmvnic_send_crq(adapter, &crq);
2436}
2437
2438static void send_request_unmap(struct ibmvnic_adapter *adapter, u8 map_id)
2439{
2440 union ibmvnic_crq crq;
2441
2442 memset(&crq, 0, sizeof(crq));
2443 crq.request_unmap.first = IBMVNIC_CRQ_CMD;
2444 crq.request_unmap.cmd = REQUEST_UNMAP;
2445 crq.request_unmap.map_id = map_id;
2446 ibmvnic_send_crq(adapter, &crq);
2447}
2448
2449static void send_map_query(struct ibmvnic_adapter *adapter)
2450{
2451 union ibmvnic_crq crq;
2452
2453 memset(&crq, 0, sizeof(crq));
2454 crq.query_map.first = IBMVNIC_CRQ_CMD;
2455 crq.query_map.cmd = QUERY_MAP;
2456 ibmvnic_send_crq(adapter, &crq);
2457}
2458
2459/* Send a series of CRQs requesting various capabilities of the VNIC server */
2460static void send_cap_queries(struct ibmvnic_adapter *adapter)
2461{
2462 union ibmvnic_crq crq;
2463
Thomas Falcon901e0402017-02-15 12:17:59 -06002464 atomic_set(&adapter->running_cap_crqs, 0);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002465 memset(&crq, 0, sizeof(crq));
2466 crq.query_capability.first = IBMVNIC_CRQ_CMD;
2467 crq.query_capability.cmd = QUERY_CAPABILITY;
2468
2469 crq.query_capability.capability = cpu_to_be16(MIN_TX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002470 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002471 ibmvnic_send_crq(adapter, &crq);
2472
2473 crq.query_capability.capability = cpu_to_be16(MIN_RX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002474 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002475 ibmvnic_send_crq(adapter, &crq);
2476
2477 crq.query_capability.capability = cpu_to_be16(MIN_RX_ADD_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002478 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002479 ibmvnic_send_crq(adapter, &crq);
2480
2481 crq.query_capability.capability = cpu_to_be16(MAX_TX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002482 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002483 ibmvnic_send_crq(adapter, &crq);
2484
2485 crq.query_capability.capability = cpu_to_be16(MAX_RX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002486 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002487 ibmvnic_send_crq(adapter, &crq);
2488
2489 crq.query_capability.capability = cpu_to_be16(MAX_RX_ADD_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002490 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002491 ibmvnic_send_crq(adapter, &crq);
2492
2493 crq.query_capability.capability =
2494 cpu_to_be16(MIN_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06002495 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002496 ibmvnic_send_crq(adapter, &crq);
2497
2498 crq.query_capability.capability =
2499 cpu_to_be16(MIN_RX_ADD_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06002500 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002501 ibmvnic_send_crq(adapter, &crq);
2502
2503 crq.query_capability.capability =
2504 cpu_to_be16(MAX_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06002505 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002506 ibmvnic_send_crq(adapter, &crq);
2507
2508 crq.query_capability.capability =
2509 cpu_to_be16(MAX_RX_ADD_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06002510 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002511 ibmvnic_send_crq(adapter, &crq);
2512
2513 crq.query_capability.capability = cpu_to_be16(TCP_IP_OFFLOAD);
Thomas Falcon901e0402017-02-15 12:17:59 -06002514 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002515 ibmvnic_send_crq(adapter, &crq);
2516
2517 crq.query_capability.capability = cpu_to_be16(PROMISC_SUPPORTED);
Thomas Falcon901e0402017-02-15 12:17:59 -06002518 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002519 ibmvnic_send_crq(adapter, &crq);
2520
2521 crq.query_capability.capability = cpu_to_be16(MIN_MTU);
Thomas Falcon901e0402017-02-15 12:17:59 -06002522 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002523 ibmvnic_send_crq(adapter, &crq);
2524
2525 crq.query_capability.capability = cpu_to_be16(MAX_MTU);
Thomas Falcon901e0402017-02-15 12:17:59 -06002526 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002527 ibmvnic_send_crq(adapter, &crq);
2528
2529 crq.query_capability.capability = cpu_to_be16(MAX_MULTICAST_FILTERS);
Thomas Falcon901e0402017-02-15 12:17:59 -06002530 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002531 ibmvnic_send_crq(adapter, &crq);
2532
2533 crq.query_capability.capability = cpu_to_be16(VLAN_HEADER_INSERTION);
Thomas Falcon901e0402017-02-15 12:17:59 -06002534 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002535 ibmvnic_send_crq(adapter, &crq);
2536
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04002537 crq.query_capability.capability = cpu_to_be16(RX_VLAN_HEADER_INSERTION);
2538 atomic_inc(&adapter->running_cap_crqs);
2539 ibmvnic_send_crq(adapter, &crq);
2540
Thomas Falcon032c5e82015-12-21 11:26:06 -06002541 crq.query_capability.capability = cpu_to_be16(MAX_TX_SG_ENTRIES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002542 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002543 ibmvnic_send_crq(adapter, &crq);
2544
2545 crq.query_capability.capability = cpu_to_be16(RX_SG_SUPPORTED);
Thomas Falcon901e0402017-02-15 12:17:59 -06002546 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002547 ibmvnic_send_crq(adapter, &crq);
2548
2549 crq.query_capability.capability = cpu_to_be16(OPT_TX_COMP_SUB_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002550 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002551 ibmvnic_send_crq(adapter, &crq);
2552
2553 crq.query_capability.capability = cpu_to_be16(OPT_RX_COMP_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002554 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002555 ibmvnic_send_crq(adapter, &crq);
2556
2557 crq.query_capability.capability =
2558 cpu_to_be16(OPT_RX_BUFADD_Q_PER_RX_COMP_Q);
Thomas Falcon901e0402017-02-15 12:17:59 -06002559 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002560 ibmvnic_send_crq(adapter, &crq);
2561
2562 crq.query_capability.capability =
2563 cpu_to_be16(OPT_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06002564 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002565 ibmvnic_send_crq(adapter, &crq);
2566
2567 crq.query_capability.capability =
2568 cpu_to_be16(OPT_RXBA_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06002569 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002570 ibmvnic_send_crq(adapter, &crq);
2571
2572 crq.query_capability.capability = cpu_to_be16(TX_RX_DESC_REQ);
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
2577static void handle_query_ip_offload_rsp(struct ibmvnic_adapter *adapter)
2578{
2579 struct device *dev = &adapter->vdev->dev;
2580 struct ibmvnic_query_ip_offload_buffer *buf = &adapter->ip_offload_buf;
2581 union ibmvnic_crq crq;
2582 int i;
2583
2584 dma_unmap_single(dev, adapter->ip_offload_tok,
2585 sizeof(adapter->ip_offload_buf), DMA_FROM_DEVICE);
2586
2587 netdev_dbg(adapter->netdev, "Query IP Offload Buffer:\n");
2588 for (i = 0; i < (sizeof(adapter->ip_offload_buf) - 1) / 8 + 1; i++)
2589 netdev_dbg(adapter->netdev, "%016lx\n",
2590 ((unsigned long int *)(buf))[i]);
2591
2592 netdev_dbg(adapter->netdev, "ipv4_chksum = %d\n", buf->ipv4_chksum);
2593 netdev_dbg(adapter->netdev, "ipv6_chksum = %d\n", buf->ipv6_chksum);
2594 netdev_dbg(adapter->netdev, "tcp_ipv4_chksum = %d\n",
2595 buf->tcp_ipv4_chksum);
2596 netdev_dbg(adapter->netdev, "tcp_ipv6_chksum = %d\n",
2597 buf->tcp_ipv6_chksum);
2598 netdev_dbg(adapter->netdev, "udp_ipv4_chksum = %d\n",
2599 buf->udp_ipv4_chksum);
2600 netdev_dbg(adapter->netdev, "udp_ipv6_chksum = %d\n",
2601 buf->udp_ipv6_chksum);
2602 netdev_dbg(adapter->netdev, "large_tx_ipv4 = %d\n",
2603 buf->large_tx_ipv4);
2604 netdev_dbg(adapter->netdev, "large_tx_ipv6 = %d\n",
2605 buf->large_tx_ipv6);
2606 netdev_dbg(adapter->netdev, "large_rx_ipv4 = %d\n",
2607 buf->large_rx_ipv4);
2608 netdev_dbg(adapter->netdev, "large_rx_ipv6 = %d\n",
2609 buf->large_rx_ipv6);
2610 netdev_dbg(adapter->netdev, "max_ipv4_hdr_sz = %d\n",
2611 buf->max_ipv4_header_size);
2612 netdev_dbg(adapter->netdev, "max_ipv6_hdr_sz = %d\n",
2613 buf->max_ipv6_header_size);
2614 netdev_dbg(adapter->netdev, "max_tcp_hdr_size = %d\n",
2615 buf->max_tcp_header_size);
2616 netdev_dbg(adapter->netdev, "max_udp_hdr_size = %d\n",
2617 buf->max_udp_header_size);
2618 netdev_dbg(adapter->netdev, "max_large_tx_size = %d\n",
2619 buf->max_large_tx_size);
2620 netdev_dbg(adapter->netdev, "max_large_rx_size = %d\n",
2621 buf->max_large_rx_size);
2622 netdev_dbg(adapter->netdev, "ipv6_ext_hdr = %d\n",
2623 buf->ipv6_extension_header);
2624 netdev_dbg(adapter->netdev, "tcp_pseudosum_req = %d\n",
2625 buf->tcp_pseudosum_req);
2626 netdev_dbg(adapter->netdev, "num_ipv6_ext_hd = %d\n",
2627 buf->num_ipv6_ext_headers);
2628 netdev_dbg(adapter->netdev, "off_ipv6_ext_hd = %d\n",
2629 buf->off_ipv6_ext_headers);
2630
2631 adapter->ip_offload_ctrl_tok =
2632 dma_map_single(dev, &adapter->ip_offload_ctrl,
2633 sizeof(adapter->ip_offload_ctrl), DMA_TO_DEVICE);
2634
2635 if (dma_mapping_error(dev, adapter->ip_offload_ctrl_tok)) {
2636 dev_err(dev, "Couldn't map ip offload control buffer\n");
2637 return;
2638 }
2639
2640 adapter->ip_offload_ctrl.version = cpu_to_be32(INITIAL_VERSION_IOB);
2641 adapter->ip_offload_ctrl.tcp_ipv4_chksum = buf->tcp_ipv4_chksum;
2642 adapter->ip_offload_ctrl.udp_ipv4_chksum = buf->udp_ipv4_chksum;
2643 adapter->ip_offload_ctrl.tcp_ipv6_chksum = buf->tcp_ipv6_chksum;
2644 adapter->ip_offload_ctrl.udp_ipv6_chksum = buf->udp_ipv6_chksum;
2645
2646 /* large_tx/rx disabled for now, additional features needed */
2647 adapter->ip_offload_ctrl.large_tx_ipv4 = 0;
2648 adapter->ip_offload_ctrl.large_tx_ipv6 = 0;
2649 adapter->ip_offload_ctrl.large_rx_ipv4 = 0;
2650 adapter->ip_offload_ctrl.large_rx_ipv6 = 0;
2651
2652 adapter->netdev->features = NETIF_F_GSO;
2653
2654 if (buf->tcp_ipv4_chksum || buf->udp_ipv4_chksum)
2655 adapter->netdev->features |= NETIF_F_IP_CSUM;
2656
2657 if (buf->tcp_ipv6_chksum || buf->udp_ipv6_chksum)
2658 adapter->netdev->features |= NETIF_F_IPV6_CSUM;
2659
Thomas Falcon9be02cd2016-04-01 17:20:35 -05002660 if ((adapter->netdev->features &
2661 (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)))
2662 adapter->netdev->features |= NETIF_F_RXCSUM;
2663
Thomas Falcon032c5e82015-12-21 11:26:06 -06002664 memset(&crq, 0, sizeof(crq));
2665 crq.control_ip_offload.first = IBMVNIC_CRQ_CMD;
2666 crq.control_ip_offload.cmd = CONTROL_IP_OFFLOAD;
2667 crq.control_ip_offload.len =
2668 cpu_to_be32(sizeof(adapter->ip_offload_ctrl));
2669 crq.control_ip_offload.ioba = cpu_to_be32(adapter->ip_offload_ctrl_tok);
2670 ibmvnic_send_crq(adapter, &crq);
2671}
2672
2673static void handle_error_info_rsp(union ibmvnic_crq *crq,
2674 struct ibmvnic_adapter *adapter)
2675{
2676 struct device *dev = &adapter->vdev->dev;
Wei Yongjun96183182016-06-27 20:48:53 +08002677 struct ibmvnic_error_buff *error_buff, *tmp;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002678 unsigned long flags;
2679 bool found = false;
2680 int i;
2681
2682 if (!crq->request_error_rsp.rc.code) {
2683 dev_info(dev, "Request Error Rsp returned with rc=%x\n",
2684 crq->request_error_rsp.rc.code);
2685 return;
2686 }
2687
2688 spin_lock_irqsave(&adapter->error_list_lock, flags);
Wei Yongjun96183182016-06-27 20:48:53 +08002689 list_for_each_entry_safe(error_buff, tmp, &adapter->errors, list)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002690 if (error_buff->error_id == crq->request_error_rsp.error_id) {
2691 found = true;
2692 list_del(&error_buff->list);
2693 break;
2694 }
2695 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
2696
2697 if (!found) {
2698 dev_err(dev, "Couldn't find error id %x\n",
Thomas Falcon75224c92017-02-15 10:33:33 -06002699 be32_to_cpu(crq->request_error_rsp.error_id));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002700 return;
2701 }
2702
2703 dev_err(dev, "Detailed info for error id %x:",
Thomas Falcon75224c92017-02-15 10:33:33 -06002704 be32_to_cpu(crq->request_error_rsp.error_id));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002705
2706 for (i = 0; i < error_buff->len; i++) {
2707 pr_cont("%02x", (int)error_buff->buff[i]);
2708 if (i % 8 == 7)
2709 pr_cont(" ");
2710 }
2711 pr_cont("\n");
2712
2713 dma_unmap_single(dev, error_buff->dma, error_buff->len,
2714 DMA_FROM_DEVICE);
2715 kfree(error_buff->buff);
2716 kfree(error_buff);
2717}
2718
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04002719static void request_error_information(struct ibmvnic_adapter *adapter,
2720 union ibmvnic_crq *err_crq)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002721{
Thomas Falcon032c5e82015-12-21 11:26:06 -06002722 struct device *dev = &adapter->vdev->dev;
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04002723 struct net_device *netdev = adapter->netdev;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002724 struct ibmvnic_error_buff *error_buff;
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04002725 unsigned long timeout = msecs_to_jiffies(30000);
2726 union ibmvnic_crq crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002727 unsigned long flags;
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04002728 int rc, detail_len;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002729
2730 error_buff = kmalloc(sizeof(*error_buff), GFP_ATOMIC);
2731 if (!error_buff)
2732 return;
2733
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04002734 detail_len = be32_to_cpu(err_crq->error_indication.detail_error_sz);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002735 error_buff->buff = kmalloc(detail_len, GFP_ATOMIC);
2736 if (!error_buff->buff) {
2737 kfree(error_buff);
2738 return;
2739 }
2740
2741 error_buff->dma = dma_map_single(dev, error_buff->buff, detail_len,
2742 DMA_FROM_DEVICE);
2743 if (dma_mapping_error(dev, error_buff->dma)) {
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04002744 netdev_err(netdev, "Couldn't map error buffer\n");
Thomas Falcon032c5e82015-12-21 11:26:06 -06002745 kfree(error_buff->buff);
2746 kfree(error_buff);
2747 return;
2748 }
2749
Thomas Falcon032c5e82015-12-21 11:26:06 -06002750 error_buff->len = detail_len;
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04002751 error_buff->error_id = err_crq->error_indication.error_id;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002752
2753 spin_lock_irqsave(&adapter->error_list_lock, flags);
2754 list_add_tail(&error_buff->list, &adapter->errors);
2755 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
2756
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04002757 memset(&crq, 0, sizeof(crq));
2758 crq.request_error_info.first = IBMVNIC_CRQ_CMD;
2759 crq.request_error_info.cmd = REQUEST_ERROR_INFO;
2760 crq.request_error_info.ioba = cpu_to_be32(error_buff->dma);
2761 crq.request_error_info.len = cpu_to_be32(detail_len);
2762 crq.request_error_info.error_id = err_crq->error_indication.error_id;
2763
2764 rc = ibmvnic_send_crq(adapter, &crq);
2765 if (rc) {
2766 netdev_err(netdev, "failed to request error information\n");
2767 goto err_info_fail;
2768 }
2769
2770 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
2771 netdev_err(netdev, "timeout waiting for error information\n");
2772 goto err_info_fail;
2773 }
2774
2775 return;
2776
2777err_info_fail:
2778 spin_lock_irqsave(&adapter->error_list_lock, flags);
2779 list_del(&error_buff->list);
2780 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
2781
2782 kfree(error_buff->buff);
2783 kfree(error_buff);
2784}
2785
2786static void handle_error_indication(union ibmvnic_crq *crq,
2787 struct ibmvnic_adapter *adapter)
2788{
2789 struct device *dev = &adapter->vdev->dev;
2790
2791 dev_err(dev, "Firmware reports %serror id %x, cause %d\n",
2792 crq->error_indication.flags
2793 & IBMVNIC_FATAL_ERROR ? "FATAL " : "",
2794 be32_to_cpu(crq->error_indication.error_id),
2795 be16_to_cpu(crq->error_indication.error_cause));
2796
2797 if (be32_to_cpu(crq->error_indication.error_id))
2798 request_error_information(adapter, crq);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002799
2800 if (crq->error_indication.flags & IBMVNIC_FATAL_ERROR)
2801 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
John Allen8cb31cf2017-05-26 10:30:37 -04002802 else
2803 ibmvnic_reset(adapter, VNIC_RESET_NON_FATAL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002804}
2805
2806static void handle_change_mac_rsp(union ibmvnic_crq *crq,
2807 struct ibmvnic_adapter *adapter)
2808{
2809 struct net_device *netdev = adapter->netdev;
2810 struct device *dev = &adapter->vdev->dev;
2811 long rc;
2812
2813 rc = crq->change_mac_addr_rsp.rc.code;
2814 if (rc) {
2815 dev_err(dev, "Error %ld in CHANGE_MAC_ADDR_RSP\n", rc);
2816 return;
2817 }
2818 memcpy(netdev->dev_addr, &crq->change_mac_addr_rsp.mac_addr[0],
2819 ETH_ALEN);
2820}
2821
2822static void handle_request_cap_rsp(union ibmvnic_crq *crq,
2823 struct ibmvnic_adapter *adapter)
2824{
2825 struct device *dev = &adapter->vdev->dev;
2826 u64 *req_value;
2827 char *name;
2828
Thomas Falcon901e0402017-02-15 12:17:59 -06002829 atomic_dec(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002830 switch (be16_to_cpu(crq->request_capability_rsp.capability)) {
2831 case REQ_TX_QUEUES:
2832 req_value = &adapter->req_tx_queues;
2833 name = "tx";
2834 break;
2835 case REQ_RX_QUEUES:
2836 req_value = &adapter->req_rx_queues;
2837 name = "rx";
2838 break;
2839 case REQ_RX_ADD_QUEUES:
2840 req_value = &adapter->req_rx_add_queues;
2841 name = "rx_add";
2842 break;
2843 case REQ_TX_ENTRIES_PER_SUBCRQ:
2844 req_value = &adapter->req_tx_entries_per_subcrq;
2845 name = "tx_entries_per_subcrq";
2846 break;
2847 case REQ_RX_ADD_ENTRIES_PER_SUBCRQ:
2848 req_value = &adapter->req_rx_add_entries_per_subcrq;
2849 name = "rx_add_entries_per_subcrq";
2850 break;
2851 case REQ_MTU:
2852 req_value = &adapter->req_mtu;
2853 name = "mtu";
2854 break;
2855 case PROMISC_REQUESTED:
2856 req_value = &adapter->promisc;
2857 name = "promisc";
2858 break;
2859 default:
2860 dev_err(dev, "Got invalid cap request rsp %d\n",
2861 crq->request_capability.capability);
2862 return;
2863 }
2864
2865 switch (crq->request_capability_rsp.rc.code) {
2866 case SUCCESS:
2867 break;
2868 case PARTIALSUCCESS:
2869 dev_info(dev, "req=%lld, rsp=%ld in %s queue, retrying.\n",
2870 *req_value,
Thomas Falcon28f4d162017-02-15 10:32:11 -06002871 (long int)be64_to_cpu(crq->request_capability_rsp.
Thomas Falcon032c5e82015-12-21 11:26:06 -06002872 number), name);
Nathan Fontenotb5108882017-03-30 02:49:18 -04002873 release_sub_crqs(adapter);
Thomas Falcon28f4d162017-02-15 10:32:11 -06002874 *req_value = be64_to_cpu(crq->request_capability_rsp.number);
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002875 ibmvnic_send_req_caps(adapter, 1);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002876 return;
2877 default:
2878 dev_err(dev, "Error %d in request cap rsp\n",
2879 crq->request_capability_rsp.rc.code);
2880 return;
2881 }
2882
2883 /* Done receiving requested capabilities, query IP offload support */
Thomas Falcon901e0402017-02-15 12:17:59 -06002884 if (atomic_read(&adapter->running_cap_crqs) == 0) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06002885 union ibmvnic_crq newcrq;
2886 int buf_sz = sizeof(struct ibmvnic_query_ip_offload_buffer);
2887 struct ibmvnic_query_ip_offload_buffer *ip_offload_buf =
2888 &adapter->ip_offload_buf;
2889
Thomas Falcon249168a2017-02-15 12:18:00 -06002890 adapter->wait_capability = false;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002891 adapter->ip_offload_tok = dma_map_single(dev, ip_offload_buf,
2892 buf_sz,
2893 DMA_FROM_DEVICE);
2894
2895 if (dma_mapping_error(dev, adapter->ip_offload_tok)) {
2896 if (!firmware_has_feature(FW_FEATURE_CMO))
2897 dev_err(dev, "Couldn't map offload buffer\n");
2898 return;
2899 }
2900
2901 memset(&newcrq, 0, sizeof(newcrq));
2902 newcrq.query_ip_offload.first = IBMVNIC_CRQ_CMD;
2903 newcrq.query_ip_offload.cmd = QUERY_IP_OFFLOAD;
2904 newcrq.query_ip_offload.len = cpu_to_be32(buf_sz);
2905 newcrq.query_ip_offload.ioba =
2906 cpu_to_be32(adapter->ip_offload_tok);
2907
2908 ibmvnic_send_crq(adapter, &newcrq);
2909 }
2910}
2911
2912static int handle_login_rsp(union ibmvnic_crq *login_rsp_crq,
2913 struct ibmvnic_adapter *adapter)
2914{
2915 struct device *dev = &adapter->vdev->dev;
2916 struct ibmvnic_login_rsp_buffer *login_rsp = adapter->login_rsp_buf;
2917 struct ibmvnic_login_buffer *login = adapter->login_buf;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002918 int i;
2919
2920 dma_unmap_single(dev, adapter->login_buf_token, adapter->login_buf_sz,
2921 DMA_BIDIRECTIONAL);
2922 dma_unmap_single(dev, adapter->login_rsp_buf_token,
2923 adapter->login_rsp_buf_sz, DMA_BIDIRECTIONAL);
2924
John Allen498cd8e2016-04-06 11:49:55 -05002925 /* If the number of queues requested can't be allocated by the
2926 * server, the login response will return with code 1. We will need
2927 * to resend the login buffer with fewer queues requested.
2928 */
2929 if (login_rsp_crq->generic.rc.code) {
2930 adapter->renegotiate = true;
2931 complete(&adapter->init_done);
2932 return 0;
2933 }
2934
Thomas Falcon032c5e82015-12-21 11:26:06 -06002935 netdev_dbg(adapter->netdev, "Login Response Buffer:\n");
2936 for (i = 0; i < (adapter->login_rsp_buf_sz - 1) / 8 + 1; i++) {
2937 netdev_dbg(adapter->netdev, "%016lx\n",
2938 ((unsigned long int *)(adapter->login_rsp_buf))[i]);
2939 }
2940
2941 /* Sanity checks */
2942 if (login->num_txcomp_subcrqs != login_rsp->num_txsubm_subcrqs ||
2943 (be32_to_cpu(login->num_rxcomp_subcrqs) *
2944 adapter->req_rx_add_queues !=
2945 be32_to_cpu(login_rsp->num_rxadd_subcrqs))) {
2946 dev_err(dev, "FATAL: Inconsistent login and login rsp\n");
2947 ibmvnic_remove(adapter->vdev);
2948 return -EIO;
2949 }
2950 complete(&adapter->init_done);
2951
Thomas Falcon032c5e82015-12-21 11:26:06 -06002952 return 0;
2953}
2954
2955static void handle_request_map_rsp(union ibmvnic_crq *crq,
2956 struct ibmvnic_adapter *adapter)
2957{
2958 struct device *dev = &adapter->vdev->dev;
2959 u8 map_id = crq->request_map_rsp.map_id;
2960 int tx_subcrqs;
2961 int rx_subcrqs;
2962 long rc;
2963 int i;
2964
2965 tx_subcrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
2966 rx_subcrqs = be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
2967
2968 rc = crq->request_map_rsp.rc.code;
2969 if (rc) {
2970 dev_err(dev, "Error %ld in REQUEST_MAP_RSP\n", rc);
2971 adapter->map_id--;
2972 /* need to find and zero tx/rx_pool map_id */
2973 for (i = 0; i < tx_subcrqs; i++) {
2974 if (adapter->tx_pool[i].long_term_buff.map_id == map_id)
2975 adapter->tx_pool[i].long_term_buff.map_id = 0;
2976 }
2977 for (i = 0; i < rx_subcrqs; i++) {
2978 if (adapter->rx_pool[i].long_term_buff.map_id == map_id)
2979 adapter->rx_pool[i].long_term_buff.map_id = 0;
2980 }
2981 }
2982 complete(&adapter->fw_done);
2983}
2984
2985static void handle_request_unmap_rsp(union ibmvnic_crq *crq,
2986 struct ibmvnic_adapter *adapter)
2987{
2988 struct device *dev = &adapter->vdev->dev;
2989 long rc;
2990
2991 rc = crq->request_unmap_rsp.rc.code;
2992 if (rc)
2993 dev_err(dev, "Error %ld in REQUEST_UNMAP_RSP\n", rc);
2994}
2995
2996static void handle_query_map_rsp(union ibmvnic_crq *crq,
2997 struct ibmvnic_adapter *adapter)
2998{
2999 struct net_device *netdev = adapter->netdev;
3000 struct device *dev = &adapter->vdev->dev;
3001 long rc;
3002
3003 rc = crq->query_map_rsp.rc.code;
3004 if (rc) {
3005 dev_err(dev, "Error %ld in QUERY_MAP_RSP\n", rc);
3006 return;
3007 }
3008 netdev_dbg(netdev, "page_size = %d\ntot_pages = %d\nfree_pages = %d\n",
3009 crq->query_map_rsp.page_size, crq->query_map_rsp.tot_pages,
3010 crq->query_map_rsp.free_pages);
3011}
3012
3013static void handle_query_cap_rsp(union ibmvnic_crq *crq,
3014 struct ibmvnic_adapter *adapter)
3015{
3016 struct net_device *netdev = adapter->netdev;
3017 struct device *dev = &adapter->vdev->dev;
3018 long rc;
3019
Thomas Falcon901e0402017-02-15 12:17:59 -06003020 atomic_dec(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003021 netdev_dbg(netdev, "Outstanding queries: %d\n",
Thomas Falcon901e0402017-02-15 12:17:59 -06003022 atomic_read(&adapter->running_cap_crqs));
Thomas Falcon032c5e82015-12-21 11:26:06 -06003023 rc = crq->query_capability.rc.code;
3024 if (rc) {
3025 dev_err(dev, "Error %ld in QUERY_CAP_RSP\n", rc);
3026 goto out;
3027 }
3028
3029 switch (be16_to_cpu(crq->query_capability.capability)) {
3030 case MIN_TX_QUEUES:
3031 adapter->min_tx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003032 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003033 netdev_dbg(netdev, "min_tx_queues = %lld\n",
3034 adapter->min_tx_queues);
3035 break;
3036 case MIN_RX_QUEUES:
3037 adapter->min_rx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003038 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003039 netdev_dbg(netdev, "min_rx_queues = %lld\n",
3040 adapter->min_rx_queues);
3041 break;
3042 case MIN_RX_ADD_QUEUES:
3043 adapter->min_rx_add_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003044 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003045 netdev_dbg(netdev, "min_rx_add_queues = %lld\n",
3046 adapter->min_rx_add_queues);
3047 break;
3048 case MAX_TX_QUEUES:
3049 adapter->max_tx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003050 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003051 netdev_dbg(netdev, "max_tx_queues = %lld\n",
3052 adapter->max_tx_queues);
3053 break;
3054 case MAX_RX_QUEUES:
3055 adapter->max_rx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003056 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003057 netdev_dbg(netdev, "max_rx_queues = %lld\n",
3058 adapter->max_rx_queues);
3059 break;
3060 case MAX_RX_ADD_QUEUES:
3061 adapter->max_rx_add_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003062 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003063 netdev_dbg(netdev, "max_rx_add_queues = %lld\n",
3064 adapter->max_rx_add_queues);
3065 break;
3066 case MIN_TX_ENTRIES_PER_SUBCRQ:
3067 adapter->min_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06003068 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003069 netdev_dbg(netdev, "min_tx_entries_per_subcrq = %lld\n",
3070 adapter->min_tx_entries_per_subcrq);
3071 break;
3072 case MIN_RX_ADD_ENTRIES_PER_SUBCRQ:
3073 adapter->min_rx_add_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06003074 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003075 netdev_dbg(netdev, "min_rx_add_entrs_per_subcrq = %lld\n",
3076 adapter->min_rx_add_entries_per_subcrq);
3077 break;
3078 case MAX_TX_ENTRIES_PER_SUBCRQ:
3079 adapter->max_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06003080 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003081 netdev_dbg(netdev, "max_tx_entries_per_subcrq = %lld\n",
3082 adapter->max_tx_entries_per_subcrq);
3083 break;
3084 case MAX_RX_ADD_ENTRIES_PER_SUBCRQ:
3085 adapter->max_rx_add_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06003086 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003087 netdev_dbg(netdev, "max_rx_add_entrs_per_subcrq = %lld\n",
3088 adapter->max_rx_add_entries_per_subcrq);
3089 break;
3090 case TCP_IP_OFFLOAD:
3091 adapter->tcp_ip_offload =
Thomas Falconde89e852016-03-01 10:20:09 -06003092 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003093 netdev_dbg(netdev, "tcp_ip_offload = %lld\n",
3094 adapter->tcp_ip_offload);
3095 break;
3096 case PROMISC_SUPPORTED:
3097 adapter->promisc_supported =
Thomas Falconde89e852016-03-01 10:20:09 -06003098 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003099 netdev_dbg(netdev, "promisc_supported = %lld\n",
3100 adapter->promisc_supported);
3101 break;
3102 case MIN_MTU:
Thomas Falconde89e852016-03-01 10:20:09 -06003103 adapter->min_mtu = be64_to_cpu(crq->query_capability.number);
Thomas Falconf39f0d12017-02-14 10:22:59 -06003104 netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003105 netdev_dbg(netdev, "min_mtu = %lld\n", adapter->min_mtu);
3106 break;
3107 case MAX_MTU:
Thomas Falconde89e852016-03-01 10:20:09 -06003108 adapter->max_mtu = be64_to_cpu(crq->query_capability.number);
Thomas Falconf39f0d12017-02-14 10:22:59 -06003109 netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003110 netdev_dbg(netdev, "max_mtu = %lld\n", adapter->max_mtu);
3111 break;
3112 case MAX_MULTICAST_FILTERS:
3113 adapter->max_multicast_filters =
Thomas Falconde89e852016-03-01 10:20:09 -06003114 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003115 netdev_dbg(netdev, "max_multicast_filters = %lld\n",
3116 adapter->max_multicast_filters);
3117 break;
3118 case VLAN_HEADER_INSERTION:
3119 adapter->vlan_header_insertion =
Thomas Falconde89e852016-03-01 10:20:09 -06003120 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003121 if (adapter->vlan_header_insertion)
3122 netdev->features |= NETIF_F_HW_VLAN_STAG_TX;
3123 netdev_dbg(netdev, "vlan_header_insertion = %lld\n",
3124 adapter->vlan_header_insertion);
3125 break;
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04003126 case RX_VLAN_HEADER_INSERTION:
3127 adapter->rx_vlan_header_insertion =
3128 be64_to_cpu(crq->query_capability.number);
3129 netdev_dbg(netdev, "rx_vlan_header_insertion = %lld\n",
3130 adapter->rx_vlan_header_insertion);
3131 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003132 case MAX_TX_SG_ENTRIES:
3133 adapter->max_tx_sg_entries =
Thomas Falconde89e852016-03-01 10:20:09 -06003134 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003135 netdev_dbg(netdev, "max_tx_sg_entries = %lld\n",
3136 adapter->max_tx_sg_entries);
3137 break;
3138 case RX_SG_SUPPORTED:
3139 adapter->rx_sg_supported =
Thomas Falconde89e852016-03-01 10:20:09 -06003140 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003141 netdev_dbg(netdev, "rx_sg_supported = %lld\n",
3142 adapter->rx_sg_supported);
3143 break;
3144 case OPT_TX_COMP_SUB_QUEUES:
3145 adapter->opt_tx_comp_sub_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003146 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003147 netdev_dbg(netdev, "opt_tx_comp_sub_queues = %lld\n",
3148 adapter->opt_tx_comp_sub_queues);
3149 break;
3150 case OPT_RX_COMP_QUEUES:
3151 adapter->opt_rx_comp_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003152 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003153 netdev_dbg(netdev, "opt_rx_comp_queues = %lld\n",
3154 adapter->opt_rx_comp_queues);
3155 break;
3156 case OPT_RX_BUFADD_Q_PER_RX_COMP_Q:
3157 adapter->opt_rx_bufadd_q_per_rx_comp_q =
Thomas Falconde89e852016-03-01 10:20:09 -06003158 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003159 netdev_dbg(netdev, "opt_rx_bufadd_q_per_rx_comp_q = %lld\n",
3160 adapter->opt_rx_bufadd_q_per_rx_comp_q);
3161 break;
3162 case OPT_TX_ENTRIES_PER_SUBCRQ:
3163 adapter->opt_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06003164 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003165 netdev_dbg(netdev, "opt_tx_entries_per_subcrq = %lld\n",
3166 adapter->opt_tx_entries_per_subcrq);
3167 break;
3168 case OPT_RXBA_ENTRIES_PER_SUBCRQ:
3169 adapter->opt_rxba_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06003170 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003171 netdev_dbg(netdev, "opt_rxba_entries_per_subcrq = %lld\n",
3172 adapter->opt_rxba_entries_per_subcrq);
3173 break;
3174 case TX_RX_DESC_REQ:
3175 adapter->tx_rx_desc_req = crq->query_capability.number;
3176 netdev_dbg(netdev, "tx_rx_desc_req = %llx\n",
3177 adapter->tx_rx_desc_req);
3178 break;
3179
3180 default:
3181 netdev_err(netdev, "Got invalid cap rsp %d\n",
3182 crq->query_capability.capability);
3183 }
3184
3185out:
Thomas Falcon249168a2017-02-15 12:18:00 -06003186 if (atomic_read(&adapter->running_cap_crqs) == 0) {
3187 adapter->wait_capability = false;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003188 ibmvnic_send_req_caps(adapter, 0);
Thomas Falcon249168a2017-02-15 12:18:00 -06003189 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06003190}
3191
Thomas Falcon032c5e82015-12-21 11:26:06 -06003192static void ibmvnic_handle_crq(union ibmvnic_crq *crq,
3193 struct ibmvnic_adapter *adapter)
3194{
3195 struct ibmvnic_generic_crq *gen_crq = &crq->generic;
3196 struct net_device *netdev = adapter->netdev;
3197 struct device *dev = &adapter->vdev->dev;
Murilo Fossa Vicentini993a82b2017-04-19 13:44:35 -04003198 u64 *u64_crq = (u64 *)crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003199 long rc;
3200
3201 netdev_dbg(netdev, "Handling CRQ: %016lx %016lx\n",
Murilo Fossa Vicentini993a82b2017-04-19 13:44:35 -04003202 (unsigned long int)cpu_to_be64(u64_crq[0]),
3203 (unsigned long int)cpu_to_be64(u64_crq[1]));
Thomas Falcon032c5e82015-12-21 11:26:06 -06003204 switch (gen_crq->first) {
3205 case IBMVNIC_CRQ_INIT_RSP:
3206 switch (gen_crq->cmd) {
3207 case IBMVNIC_CRQ_INIT:
3208 dev_info(dev, "Partner initialized\n");
John Allen017892c12017-05-26 10:30:19 -04003209 adapter->from_passive_init = true;
3210 complete(&adapter->init_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003211 break;
3212 case IBMVNIC_CRQ_INIT_COMPLETE:
3213 dev_info(dev, "Partner initialization complete\n");
3214 send_version_xchg(adapter);
3215 break;
3216 default:
3217 dev_err(dev, "Unknown crq cmd: %d\n", gen_crq->cmd);
3218 }
3219 return;
3220 case IBMVNIC_CRQ_XPORT_EVENT:
Nathan Fontenoted651a12017-05-03 14:04:38 -04003221 netif_carrier_off(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003222 if (gen_crq->cmd == IBMVNIC_PARTITION_MIGRATED) {
Nathan Fontenoted651a12017-05-03 14:04:38 -04003223 dev_info(dev, "Migrated, re-enabling adapter\n");
3224 ibmvnic_reset(adapter, VNIC_RESET_MOBILITY);
Thomas Falcondfad09a2016-08-18 11:37:51 -05003225 } else if (gen_crq->cmd == IBMVNIC_DEVICE_FAILOVER) {
3226 dev_info(dev, "Backing device failover detected\n");
Nathan Fontenoted651a12017-05-03 14:04:38 -04003227 ibmvnic_reset(adapter, VNIC_RESET_FAILOVER);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003228 } else {
3229 /* The adapter lost the connection */
3230 dev_err(dev, "Virtual Adapter failed (rc=%d)\n",
3231 gen_crq->cmd);
Nathan Fontenoted651a12017-05-03 14:04:38 -04003232 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003233 }
3234 return;
3235 case IBMVNIC_CRQ_CMD_RSP:
3236 break;
3237 default:
3238 dev_err(dev, "Got an invalid msg type 0x%02x\n",
3239 gen_crq->first);
3240 return;
3241 }
3242
3243 switch (gen_crq->cmd) {
3244 case VERSION_EXCHANGE_RSP:
3245 rc = crq->version_exchange_rsp.rc.code;
3246 if (rc) {
3247 dev_err(dev, "Error %ld in VERSION_EXCHG_RSP\n", rc);
3248 break;
3249 }
3250 dev_info(dev, "Partner protocol version is %d\n",
3251 crq->version_exchange_rsp.version);
3252 if (be16_to_cpu(crq->version_exchange_rsp.version) <
3253 ibmvnic_version)
3254 ibmvnic_version =
3255 be16_to_cpu(crq->version_exchange_rsp.version);
3256 send_cap_queries(adapter);
3257 break;
3258 case QUERY_CAPABILITY_RSP:
3259 handle_query_cap_rsp(crq, adapter);
3260 break;
3261 case QUERY_MAP_RSP:
3262 handle_query_map_rsp(crq, adapter);
3263 break;
3264 case REQUEST_MAP_RSP:
3265 handle_request_map_rsp(crq, adapter);
3266 break;
3267 case REQUEST_UNMAP_RSP:
3268 handle_request_unmap_rsp(crq, adapter);
3269 break;
3270 case REQUEST_CAPABILITY_RSP:
3271 handle_request_cap_rsp(crq, adapter);
3272 break;
3273 case LOGIN_RSP:
3274 netdev_dbg(netdev, "Got Login Response\n");
3275 handle_login_rsp(crq, adapter);
3276 break;
3277 case LOGICAL_LINK_STATE_RSP:
Nathan Fontenot53da09e2017-04-21 15:39:04 -04003278 netdev_dbg(netdev,
3279 "Got Logical Link State Response, state: %d rc: %d\n",
3280 crq->logical_link_state_rsp.link_state,
3281 crq->logical_link_state_rsp.rc.code);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003282 adapter->logical_link_state =
3283 crq->logical_link_state_rsp.link_state;
Nathan Fontenot53da09e2017-04-21 15:39:04 -04003284 adapter->init_done_rc = crq->logical_link_state_rsp.rc.code;
3285 complete(&adapter->init_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003286 break;
3287 case LINK_STATE_INDICATION:
3288 netdev_dbg(netdev, "Got Logical Link State Indication\n");
3289 adapter->phys_link_state =
3290 crq->link_state_indication.phys_link_state;
3291 adapter->logical_link_state =
3292 crq->link_state_indication.logical_link_state;
3293 break;
3294 case CHANGE_MAC_ADDR_RSP:
3295 netdev_dbg(netdev, "Got MAC address change Response\n");
3296 handle_change_mac_rsp(crq, adapter);
3297 break;
3298 case ERROR_INDICATION:
3299 netdev_dbg(netdev, "Got Error Indication\n");
3300 handle_error_indication(crq, adapter);
3301 break;
3302 case REQUEST_ERROR_RSP:
3303 netdev_dbg(netdev, "Got Error Detail Response\n");
3304 handle_error_info_rsp(crq, adapter);
3305 break;
3306 case REQUEST_STATISTICS_RSP:
3307 netdev_dbg(netdev, "Got Statistics Response\n");
3308 complete(&adapter->stats_done);
3309 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003310 case QUERY_IP_OFFLOAD_RSP:
3311 netdev_dbg(netdev, "Got Query IP offload Response\n");
3312 handle_query_ip_offload_rsp(adapter);
3313 break;
3314 case MULTICAST_CTRL_RSP:
3315 netdev_dbg(netdev, "Got multicast control Response\n");
3316 break;
3317 case CONTROL_IP_OFFLOAD_RSP:
3318 netdev_dbg(netdev, "Got Control IP offload Response\n");
3319 dma_unmap_single(dev, adapter->ip_offload_ctrl_tok,
3320 sizeof(adapter->ip_offload_ctrl),
3321 DMA_TO_DEVICE);
John Allenbd0b6722017-03-17 17:13:40 -05003322 complete(&adapter->init_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003323 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003324 case COLLECT_FW_TRACE_RSP:
3325 netdev_dbg(netdev, "Got Collect firmware trace Response\n");
3326 complete(&adapter->fw_done);
3327 break;
3328 default:
3329 netdev_err(netdev, "Got an invalid cmd type 0x%02x\n",
3330 gen_crq->cmd);
3331 }
3332}
3333
3334static irqreturn_t ibmvnic_interrupt(int irq, void *instance)
3335{
3336 struct ibmvnic_adapter *adapter = instance;
Thomas Falcon6c267b32017-02-15 12:17:58 -06003337
Thomas Falcon6c267b32017-02-15 12:17:58 -06003338 tasklet_schedule(&adapter->tasklet);
Thomas Falcon6c267b32017-02-15 12:17:58 -06003339 return IRQ_HANDLED;
3340}
3341
3342static void ibmvnic_tasklet(void *data)
3343{
3344 struct ibmvnic_adapter *adapter = data;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003345 struct ibmvnic_crq_queue *queue = &adapter->crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003346 union ibmvnic_crq *crq;
3347 unsigned long flags;
3348 bool done = false;
3349
3350 spin_lock_irqsave(&queue->lock, flags);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003351 while (!done) {
3352 /* Pull all the valid messages off the CRQ */
3353 while ((crq = ibmvnic_next_crq(adapter)) != NULL) {
3354 ibmvnic_handle_crq(crq, adapter);
3355 crq->generic.first = 0;
3356 }
Brian Kinged7ecbf2017-04-19 13:44:53 -04003357
3358 /* remain in tasklet until all
3359 * capabilities responses are received
3360 */
3361 if (!adapter->wait_capability)
3362 done = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003363 }
Thomas Falcon249168a2017-02-15 12:18:00 -06003364 /* if capabilities CRQ's were sent in this tasklet, the following
3365 * tasklet must wait until all responses are received
3366 */
3367 if (atomic_read(&adapter->running_cap_crqs) != 0)
3368 adapter->wait_capability = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003369 spin_unlock_irqrestore(&queue->lock, flags);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003370}
3371
3372static int ibmvnic_reenable_crq_queue(struct ibmvnic_adapter *adapter)
3373{
3374 struct vio_dev *vdev = adapter->vdev;
3375 int rc;
3376
3377 do {
3378 rc = plpar_hcall_norets(H_ENABLE_CRQ, vdev->unit_address);
3379 } while (rc == H_IN_PROGRESS || rc == H_BUSY || H_IS_LONG_BUSY(rc));
3380
3381 if (rc)
3382 dev_err(&vdev->dev, "Error enabling adapter (rc=%d)\n", rc);
3383
3384 return rc;
3385}
3386
3387static int ibmvnic_reset_crq(struct ibmvnic_adapter *adapter)
3388{
3389 struct ibmvnic_crq_queue *crq = &adapter->crq;
3390 struct device *dev = &adapter->vdev->dev;
3391 struct vio_dev *vdev = adapter->vdev;
3392 int rc;
3393
3394 /* Close the CRQ */
3395 do {
3396 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
3397 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
3398
3399 /* Clean out the queue */
3400 memset(crq->msgs, 0, PAGE_SIZE);
3401 crq->cur = 0;
3402
3403 /* And re-open it again */
3404 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
3405 crq->msg_token, PAGE_SIZE);
3406
3407 if (rc == H_CLOSED)
3408 /* Adapter is good, but other end is not ready */
3409 dev_warn(dev, "Partner adapter not ready\n");
3410 else if (rc != 0)
3411 dev_warn(dev, "Couldn't register crq (rc=%d)\n", rc);
3412
3413 return rc;
3414}
3415
Nathan Fontenotf9928872017-03-30 02:48:54 -04003416static void release_crq_queue(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003417{
3418 struct ibmvnic_crq_queue *crq = &adapter->crq;
3419 struct vio_dev *vdev = adapter->vdev;
3420 long rc;
3421
Nathan Fontenotf9928872017-03-30 02:48:54 -04003422 if (!crq->msgs)
3423 return;
3424
Thomas Falcon032c5e82015-12-21 11:26:06 -06003425 netdev_dbg(adapter->netdev, "Releasing CRQ\n");
3426 free_irq(vdev->irq, adapter);
Thomas Falcon6c267b32017-02-15 12:17:58 -06003427 tasklet_kill(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003428 do {
3429 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
3430 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
3431
3432 dma_unmap_single(&vdev->dev, crq->msg_token, PAGE_SIZE,
3433 DMA_BIDIRECTIONAL);
3434 free_page((unsigned long)crq->msgs);
Nathan Fontenotf9928872017-03-30 02:48:54 -04003435 crq->msgs = NULL;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003436}
3437
Nathan Fontenotf9928872017-03-30 02:48:54 -04003438static int init_crq_queue(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003439{
3440 struct ibmvnic_crq_queue *crq = &adapter->crq;
3441 struct device *dev = &adapter->vdev->dev;
3442 struct vio_dev *vdev = adapter->vdev;
3443 int rc, retrc = -ENOMEM;
3444
Nathan Fontenotf9928872017-03-30 02:48:54 -04003445 if (crq->msgs)
3446 return 0;
3447
Thomas Falcon032c5e82015-12-21 11:26:06 -06003448 crq->msgs = (union ibmvnic_crq *)get_zeroed_page(GFP_KERNEL);
3449 /* Should we allocate more than one page? */
3450
3451 if (!crq->msgs)
3452 return -ENOMEM;
3453
3454 crq->size = PAGE_SIZE / sizeof(*crq->msgs);
3455 crq->msg_token = dma_map_single(dev, crq->msgs, PAGE_SIZE,
3456 DMA_BIDIRECTIONAL);
3457 if (dma_mapping_error(dev, crq->msg_token))
3458 goto map_failed;
3459
3460 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
3461 crq->msg_token, PAGE_SIZE);
3462
3463 if (rc == H_RESOURCE)
3464 /* maybe kexecing and resource is busy. try a reset */
3465 rc = ibmvnic_reset_crq(adapter);
3466 retrc = rc;
3467
3468 if (rc == H_CLOSED) {
3469 dev_warn(dev, "Partner adapter not ready\n");
3470 } else if (rc) {
3471 dev_warn(dev, "Error %d opening adapter\n", rc);
3472 goto reg_crq_failed;
3473 }
3474
3475 retrc = 0;
3476
Thomas Falcon6c267b32017-02-15 12:17:58 -06003477 tasklet_init(&adapter->tasklet, (void *)ibmvnic_tasklet,
3478 (unsigned long)adapter);
3479
Thomas Falcon032c5e82015-12-21 11:26:06 -06003480 netdev_dbg(adapter->netdev, "registering irq 0x%x\n", vdev->irq);
3481 rc = request_irq(vdev->irq, ibmvnic_interrupt, 0, IBMVNIC_NAME,
3482 adapter);
3483 if (rc) {
3484 dev_err(dev, "Couldn't register irq 0x%x. rc=%d\n",
3485 vdev->irq, rc);
3486 goto req_irq_failed;
3487 }
3488
3489 rc = vio_enable_interrupts(vdev);
3490 if (rc) {
3491 dev_err(dev, "Error %d enabling interrupts\n", rc);
3492 goto req_irq_failed;
3493 }
3494
3495 crq->cur = 0;
3496 spin_lock_init(&crq->lock);
3497
3498 return retrc;
3499
3500req_irq_failed:
Thomas Falcon6c267b32017-02-15 12:17:58 -06003501 tasklet_kill(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003502 do {
3503 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
3504 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
3505reg_crq_failed:
3506 dma_unmap_single(dev, crq->msg_token, PAGE_SIZE, DMA_BIDIRECTIONAL);
3507map_failed:
3508 free_page((unsigned long)crq->msgs);
Nathan Fontenotf9928872017-03-30 02:48:54 -04003509 crq->msgs = NULL;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003510 return retrc;
3511}
3512
John Allenf6ef6402017-03-17 17:13:42 -05003513static int ibmvnic_init(struct ibmvnic_adapter *adapter)
3514{
3515 struct device *dev = &adapter->vdev->dev;
3516 unsigned long timeout = msecs_to_jiffies(30000);
John Allenf6ef6402017-03-17 17:13:42 -05003517 int rc;
3518
Nathan Fontenot28cde752017-05-26 10:31:00 -04003519 if (adapter->resetting) {
3520 rc = ibmvnic_reset_crq(adapter);
3521 if (!rc)
3522 rc = vio_enable_interrupts(adapter->vdev);
3523 } else {
3524 rc = init_crq_queue(adapter);
3525 }
3526
John Allenf6ef6402017-03-17 17:13:42 -05003527 if (rc) {
3528 dev_err(dev, "Couldn't initialize crq. rc=%d\n", rc);
3529 return rc;
3530 }
3531
John Allen017892c12017-05-26 10:30:19 -04003532 adapter->from_passive_init = false;
3533
John Allenf6ef6402017-03-17 17:13:42 -05003534 init_completion(&adapter->init_done);
3535 ibmvnic_send_crq_init(adapter);
3536 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
3537 dev_err(dev, "Initialization sequence timed out\n");
John Allen017892c12017-05-26 10:30:19 -04003538 return -1;
3539 }
3540
3541 if (adapter->from_passive_init) {
3542 adapter->state = VNIC_OPEN;
3543 adapter->from_passive_init = false;
John Allenf6ef6402017-03-17 17:13:42 -05003544 return -1;
3545 }
3546
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04003547 rc = init_sub_crqs(adapter);
3548 if (rc) {
3549 dev_err(dev, "Initialization of sub crqs failed\n");
3550 release_crq_queue(adapter);
3551 }
3552
3553 return rc;
John Allenf6ef6402017-03-17 17:13:42 -05003554}
3555
Thomas Falcon032c5e82015-12-21 11:26:06 -06003556static int ibmvnic_probe(struct vio_dev *dev, const struct vio_device_id *id)
3557{
3558 struct ibmvnic_adapter *adapter;
3559 struct net_device *netdev;
3560 unsigned char *mac_addr_p;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003561 int rc;
3562
3563 dev_dbg(&dev->dev, "entering ibmvnic_probe for UA 0x%x\n",
3564 dev->unit_address);
3565
3566 mac_addr_p = (unsigned char *)vio_get_attribute(dev,
3567 VETH_MAC_ADDR, NULL);
3568 if (!mac_addr_p) {
3569 dev_err(&dev->dev,
3570 "(%s:%3.3d) ERROR: Can't find MAC_ADDR attribute\n",
3571 __FILE__, __LINE__);
3572 return 0;
3573 }
3574
3575 netdev = alloc_etherdev_mq(sizeof(struct ibmvnic_adapter),
3576 IBMVNIC_MAX_TX_QUEUES);
3577 if (!netdev)
3578 return -ENOMEM;
3579
3580 adapter = netdev_priv(netdev);
Nathan Fontenot90c80142017-05-03 14:04:32 -04003581 adapter->state = VNIC_PROBING;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003582 dev_set_drvdata(&dev->dev, netdev);
3583 adapter->vdev = dev;
3584 adapter->netdev = netdev;
3585
3586 ether_addr_copy(adapter->mac_addr, mac_addr_p);
3587 ether_addr_copy(netdev->dev_addr, adapter->mac_addr);
3588 netdev->irq = dev->irq;
3589 netdev->netdev_ops = &ibmvnic_netdev_ops;
3590 netdev->ethtool_ops = &ibmvnic_ethtool_ops;
3591 SET_NETDEV_DEV(netdev, &dev->dev);
3592
3593 spin_lock_init(&adapter->stats_lock);
3594
Thomas Falcon032c5e82015-12-21 11:26:06 -06003595 INIT_LIST_HEAD(&adapter->errors);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003596 spin_lock_init(&adapter->error_list_lock);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003597
Nathan Fontenoted651a12017-05-03 14:04:38 -04003598 INIT_WORK(&adapter->ibmvnic_reset, __ibmvnic_reset);
3599 INIT_LIST_HEAD(&adapter->rwi_list);
3600 mutex_init(&adapter->reset_lock);
3601 mutex_init(&adapter->rwi_lock);
3602 adapter->resetting = false;
3603
John Allenf6ef6402017-03-17 17:13:42 -05003604 rc = ibmvnic_init(adapter);
3605 if (rc) {
3606 free_netdev(netdev);
3607 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003608 }
3609
Thomas Falconf39f0d12017-02-14 10:22:59 -06003610 netdev->mtu = adapter->req_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003611
3612 rc = register_netdev(netdev);
3613 if (rc) {
3614 dev_err(&dev->dev, "failed to register netdev rc=%d\n", rc);
John Allenf6ef6402017-03-17 17:13:42 -05003615 free_netdev(netdev);
3616 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003617 }
3618 dev_info(&dev->dev, "ibmvnic registered\n");
3619
Nathan Fontenot90c80142017-05-03 14:04:32 -04003620 adapter->state = VNIC_PROBED;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003621 return 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003622}
3623
3624static int ibmvnic_remove(struct vio_dev *dev)
3625{
3626 struct net_device *netdev = dev_get_drvdata(&dev->dev);
Nathan Fontenot37489052017-04-19 13:45:04 -04003627 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003628
Nathan Fontenot90c80142017-05-03 14:04:32 -04003629 adapter->state = VNIC_REMOVING;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003630 unregister_netdev(netdev);
Nathan Fontenoted651a12017-05-03 14:04:38 -04003631 mutex_lock(&adapter->reset_lock);
Nathan Fontenot37489052017-04-19 13:45:04 -04003632
3633 release_resources(adapter);
3634 release_sub_crqs(adapter);
3635 release_crq_queue(adapter);
3636
Nathan Fontenot90c80142017-05-03 14:04:32 -04003637 adapter->state = VNIC_REMOVED;
3638
Nathan Fontenoted651a12017-05-03 14:04:38 -04003639 mutex_unlock(&adapter->reset_lock);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003640 free_netdev(netdev);
3641 dev_set_drvdata(&dev->dev, NULL);
3642
3643 return 0;
3644}
3645
3646static unsigned long ibmvnic_get_desired_dma(struct vio_dev *vdev)
3647{
3648 struct net_device *netdev = dev_get_drvdata(&vdev->dev);
3649 struct ibmvnic_adapter *adapter;
3650 struct iommu_table *tbl;
3651 unsigned long ret = 0;
3652 int i;
3653
3654 tbl = get_iommu_table_base(&vdev->dev);
3655
3656 /* netdev inits at probe time along with the structures we need below*/
3657 if (!netdev)
3658 return IOMMU_PAGE_ALIGN(IBMVNIC_IO_ENTITLEMENT_DEFAULT, tbl);
3659
3660 adapter = netdev_priv(netdev);
3661
3662 ret += PAGE_SIZE; /* the crq message queue */
Thomas Falcon032c5e82015-12-21 11:26:06 -06003663 ret += IOMMU_PAGE_ALIGN(sizeof(struct ibmvnic_statistics), tbl);
3664
3665 for (i = 0; i < adapter->req_tx_queues + adapter->req_rx_queues; i++)
3666 ret += 4 * PAGE_SIZE; /* the scrq message queue */
3667
3668 for (i = 0; i < be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
3669 i++)
3670 ret += adapter->rx_pool[i].size *
3671 IOMMU_PAGE_ALIGN(adapter->rx_pool[i].buff_size, tbl);
3672
3673 return ret;
3674}
3675
3676static int ibmvnic_resume(struct device *dev)
3677{
3678 struct net_device *netdev = dev_get_drvdata(dev);
3679 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
3680 int i;
3681
3682 /* kick the interrupt handlers just in case we lost an interrupt */
3683 for (i = 0; i < adapter->req_rx_queues; i++)
3684 ibmvnic_interrupt_rx(adapter->rx_scrq[i]->irq,
3685 adapter->rx_scrq[i]);
3686
3687 return 0;
3688}
3689
3690static struct vio_device_id ibmvnic_device_table[] = {
3691 {"network", "IBM,vnic"},
3692 {"", "" }
3693};
3694MODULE_DEVICE_TABLE(vio, ibmvnic_device_table);
3695
3696static const struct dev_pm_ops ibmvnic_pm_ops = {
3697 .resume = ibmvnic_resume
3698};
3699
3700static struct vio_driver ibmvnic_driver = {
3701 .id_table = ibmvnic_device_table,
3702 .probe = ibmvnic_probe,
3703 .remove = ibmvnic_remove,
3704 .get_desired_dma = ibmvnic_get_desired_dma,
3705 .name = ibmvnic_driver_name,
3706 .pm = &ibmvnic_pm_ops,
3707};
3708
3709/* module functions */
3710static int __init ibmvnic_module_init(void)
3711{
3712 pr_info("%s: %s %s\n", ibmvnic_driver_name, ibmvnic_driver_string,
3713 IBMVNIC_DRIVER_VERSION);
3714
3715 return vio_register_driver(&ibmvnic_driver);
3716}
3717
3718static void __exit ibmvnic_module_exit(void)
3719{
3720 vio_unregister_driver(&ibmvnic_driver);
3721}
3722
3723module_init(ibmvnic_module_init);
3724module_exit(ibmvnic_module_exit);