blob: 760352f7f98d870fd43380a4bccef468a28fc03b [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);
1263 release_crq_queue(adapter);
1264
1265 rc = ibmvnic_init(adapter);
1266 if (rc)
1267 return 0;
1268
1269 /* If the adapter was in PROBE state prior to the reset,
1270 * exit here.
1271 */
1272 if (reset_state == VNIC_PROBED)
1273 return 0;
1274
1275 rc = ibmvnic_login(netdev);
1276 if (rc) {
1277 adapter->state = VNIC_PROBED;
1278 return 0;
1279 }
1280
1281 rtnl_lock();
1282 rc = init_resources(adapter);
1283 rtnl_unlock();
1284 if (rc)
1285 return rc;
1286
1287 if (reset_state == VNIC_CLOSED)
1288 return 0;
Nathan Fontenoted651a12017-05-03 14:04:38 -04001289 }
1290
Nathan Fontenoted651a12017-05-03 14:04:38 -04001291 rc = __ibmvnic_open(netdev);
1292 if (rc) {
1293 if (list_empty(&adapter->rwi_list))
1294 adapter->state = VNIC_CLOSED;
1295 else
1296 adapter->state = reset_state;
1297
1298 return 0;
1299 }
1300
1301 netif_carrier_on(netdev);
1302
1303 /* kick napi */
1304 for (i = 0; i < adapter->req_rx_queues; i++)
1305 napi_schedule(&adapter->napi[i]);
1306
John Allen2ce9e4e2017-05-26 10:30:25 -04001307 netdev_notify_peers(netdev);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001308 return 0;
1309}
1310
1311static struct ibmvnic_rwi *get_next_rwi(struct ibmvnic_adapter *adapter)
1312{
1313 struct ibmvnic_rwi *rwi;
1314
1315 mutex_lock(&adapter->rwi_lock);
1316
1317 if (!list_empty(&adapter->rwi_list)) {
1318 rwi = list_first_entry(&adapter->rwi_list, struct ibmvnic_rwi,
1319 list);
1320 list_del(&rwi->list);
1321 } else {
1322 rwi = NULL;
1323 }
1324
1325 mutex_unlock(&adapter->rwi_lock);
1326 return rwi;
1327}
1328
1329static void free_all_rwi(struct ibmvnic_adapter *adapter)
1330{
1331 struct ibmvnic_rwi *rwi;
1332
1333 rwi = get_next_rwi(adapter);
1334 while (rwi) {
1335 kfree(rwi);
1336 rwi = get_next_rwi(adapter);
1337 }
1338}
1339
1340static void __ibmvnic_reset(struct work_struct *work)
1341{
1342 struct ibmvnic_rwi *rwi;
1343 struct ibmvnic_adapter *adapter;
1344 struct net_device *netdev;
1345 u32 reset_state;
1346 int rc;
1347
1348 adapter = container_of(work, struct ibmvnic_adapter, ibmvnic_reset);
1349 netdev = adapter->netdev;
1350
1351 mutex_lock(&adapter->reset_lock);
1352 adapter->resetting = true;
1353 reset_state = adapter->state;
1354
1355 rwi = get_next_rwi(adapter);
1356 while (rwi) {
1357 rc = do_reset(adapter, rwi, reset_state);
1358 kfree(rwi);
1359 if (rc)
1360 break;
1361
1362 rwi = get_next_rwi(adapter);
1363 }
1364
1365 if (rc) {
1366 free_all_rwi(adapter);
Wei Yongjun6d0af072017-05-18 15:24:52 +00001367 mutex_unlock(&adapter->reset_lock);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001368 return;
1369 }
1370
1371 adapter->resetting = false;
1372 mutex_unlock(&adapter->reset_lock);
1373}
1374
1375static void ibmvnic_reset(struct ibmvnic_adapter *adapter,
1376 enum ibmvnic_reset_reason reason)
1377{
1378 struct ibmvnic_rwi *rwi, *tmp;
1379 struct net_device *netdev = adapter->netdev;
1380 struct list_head *entry;
1381
1382 if (adapter->state == VNIC_REMOVING ||
1383 adapter->state == VNIC_REMOVED) {
1384 netdev_dbg(netdev, "Adapter removing, skipping reset\n");
1385 return;
1386 }
1387
1388 mutex_lock(&adapter->rwi_lock);
1389
1390 list_for_each(entry, &adapter->rwi_list) {
1391 tmp = list_entry(entry, struct ibmvnic_rwi, list);
1392 if (tmp->reset_reason == reason) {
1393 netdev_err(netdev, "Matching reset found, skipping\n");
1394 mutex_unlock(&adapter->rwi_lock);
1395 return;
1396 }
1397 }
1398
1399 rwi = kzalloc(sizeof(*rwi), GFP_KERNEL);
1400 if (!rwi) {
1401 mutex_unlock(&adapter->rwi_lock);
1402 ibmvnic_close(netdev);
1403 return;
1404 }
1405
1406 rwi->reset_reason = reason;
1407 list_add_tail(&rwi->list, &adapter->rwi_list);
1408 mutex_unlock(&adapter->rwi_lock);
1409 schedule_work(&adapter->ibmvnic_reset);
1410}
1411
Thomas Falcon032c5e82015-12-21 11:26:06 -06001412static void ibmvnic_tx_timeout(struct net_device *dev)
1413{
1414 struct ibmvnic_adapter *adapter = netdev_priv(dev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001415
Nathan Fontenoted651a12017-05-03 14:04:38 -04001416 ibmvnic_reset(adapter, VNIC_RESET_TIMEOUT);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001417}
1418
1419static void remove_buff_from_pool(struct ibmvnic_adapter *adapter,
1420 struct ibmvnic_rx_buff *rx_buff)
1421{
1422 struct ibmvnic_rx_pool *pool = &adapter->rx_pool[rx_buff->pool_index];
1423
1424 rx_buff->skb = NULL;
1425
1426 pool->free_map[pool->next_alloc] = (int)(rx_buff - pool->rx_buff);
1427 pool->next_alloc = (pool->next_alloc + 1) % pool->size;
1428
1429 atomic_dec(&pool->available);
1430}
1431
1432static int ibmvnic_poll(struct napi_struct *napi, int budget)
1433{
1434 struct net_device *netdev = napi->dev;
1435 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1436 int scrq_num = (int)(napi - adapter->napi);
1437 int frames_processed = 0;
Nathan Fontenot152ce472017-05-26 10:30:54 -04001438
1439 if (adapter->resetting)
1440 return 0;
1441
Thomas Falcon032c5e82015-12-21 11:26:06 -06001442restart_poll:
1443 while (frames_processed < budget) {
1444 struct sk_buff *skb;
1445 struct ibmvnic_rx_buff *rx_buff;
1446 union sub_crq *next;
1447 u32 length;
1448 u16 offset;
1449 u8 flags = 0;
1450
1451 if (!pending_scrq(adapter, adapter->rx_scrq[scrq_num]))
1452 break;
1453 next = ibmvnic_next_scrq(adapter, adapter->rx_scrq[scrq_num]);
1454 rx_buff =
1455 (struct ibmvnic_rx_buff *)be64_to_cpu(next->
1456 rx_comp.correlator);
1457 /* do error checking */
1458 if (next->rx_comp.rc) {
1459 netdev_err(netdev, "rx error %x\n", next->rx_comp.rc);
1460 /* free the entry */
1461 next->rx_comp.first = 0;
1462 remove_buff_from_pool(adapter, rx_buff);
Nathan Fontenotca05e312017-05-03 14:05:14 -04001463 continue;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001464 }
1465
1466 length = be32_to_cpu(next->rx_comp.len);
1467 offset = be16_to_cpu(next->rx_comp.off_frame_data);
1468 flags = next->rx_comp.flags;
1469 skb = rx_buff->skb;
1470 skb_copy_to_linear_data(skb, rx_buff->data + offset,
1471 length);
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04001472
1473 /* VLAN Header has been stripped by the system firmware and
1474 * needs to be inserted by the driver
1475 */
1476 if (adapter->rx_vlan_header_insertion &&
1477 (flags & IBMVNIC_VLAN_STRIPPED))
1478 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
1479 ntohs(next->rx_comp.vlan_tci));
1480
Thomas Falcon032c5e82015-12-21 11:26:06 -06001481 /* free the entry */
1482 next->rx_comp.first = 0;
1483 remove_buff_from_pool(adapter, rx_buff);
1484
1485 skb_put(skb, length);
1486 skb->protocol = eth_type_trans(skb, netdev);
Thomas Falcon94ca3052017-05-03 14:05:20 -04001487 skb_record_rx_queue(skb, scrq_num);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001488
1489 if (flags & IBMVNIC_IP_CHKSUM_GOOD &&
1490 flags & IBMVNIC_TCP_UDP_CHKSUM_GOOD) {
1491 skb->ip_summed = CHECKSUM_UNNECESSARY;
1492 }
1493
1494 length = skb->len;
1495 napi_gro_receive(napi, skb); /* send it up */
1496 netdev->stats.rx_packets++;
1497 netdev->stats.rx_bytes += length;
1498 frames_processed++;
1499 }
Nathan Fontenot152ce472017-05-26 10:30:54 -04001500
1501 if (adapter->state != VNIC_CLOSING)
1502 replenish_rx_pool(adapter, &adapter->rx_pool[scrq_num]);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001503
1504 if (frames_processed < budget) {
1505 enable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
Eric Dumazet6ad20162017-01-30 08:22:01 -08001506 napi_complete_done(napi, frames_processed);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001507 if (pending_scrq(adapter, adapter->rx_scrq[scrq_num]) &&
1508 napi_reschedule(napi)) {
1509 disable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
1510 goto restart_poll;
1511 }
1512 }
1513 return frames_processed;
1514}
1515
1516#ifdef CONFIG_NET_POLL_CONTROLLER
1517static void ibmvnic_netpoll_controller(struct net_device *dev)
1518{
1519 struct ibmvnic_adapter *adapter = netdev_priv(dev);
1520 int i;
1521
1522 replenish_pools(netdev_priv(dev));
1523 for (i = 0; i < adapter->req_rx_queues; i++)
1524 ibmvnic_interrupt_rx(adapter->rx_scrq[i]->irq,
1525 adapter->rx_scrq[i]);
1526}
1527#endif
1528
1529static const struct net_device_ops ibmvnic_netdev_ops = {
1530 .ndo_open = ibmvnic_open,
1531 .ndo_stop = ibmvnic_close,
1532 .ndo_start_xmit = ibmvnic_xmit,
1533 .ndo_set_rx_mode = ibmvnic_set_multi,
1534 .ndo_set_mac_address = ibmvnic_set_mac,
1535 .ndo_validate_addr = eth_validate_addr,
Thomas Falcon032c5e82015-12-21 11:26:06 -06001536 .ndo_tx_timeout = ibmvnic_tx_timeout,
1537#ifdef CONFIG_NET_POLL_CONTROLLER
1538 .ndo_poll_controller = ibmvnic_netpoll_controller,
1539#endif
1540};
1541
1542/* ethtool functions */
1543
Philippe Reynes8a433792017-01-07 22:37:29 +01001544static int ibmvnic_get_link_ksettings(struct net_device *netdev,
1545 struct ethtool_link_ksettings *cmd)
Thomas Falcon032c5e82015-12-21 11:26:06 -06001546{
Philippe Reynes8a433792017-01-07 22:37:29 +01001547 u32 supported, advertising;
1548
1549 supported = (SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg |
Thomas Falcon032c5e82015-12-21 11:26:06 -06001550 SUPPORTED_FIBRE);
Philippe Reynes8a433792017-01-07 22:37:29 +01001551 advertising = (ADVERTISED_1000baseT_Full | ADVERTISED_Autoneg |
Thomas Falcon032c5e82015-12-21 11:26:06 -06001552 ADVERTISED_FIBRE);
Philippe Reynes8a433792017-01-07 22:37:29 +01001553 cmd->base.speed = SPEED_1000;
1554 cmd->base.duplex = DUPLEX_FULL;
1555 cmd->base.port = PORT_FIBRE;
1556 cmd->base.phy_address = 0;
1557 cmd->base.autoneg = AUTONEG_ENABLE;
1558
1559 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
1560 supported);
1561 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
1562 advertising);
1563
Thomas Falcon032c5e82015-12-21 11:26:06 -06001564 return 0;
1565}
1566
1567static void ibmvnic_get_drvinfo(struct net_device *dev,
1568 struct ethtool_drvinfo *info)
1569{
1570 strlcpy(info->driver, ibmvnic_driver_name, sizeof(info->driver));
1571 strlcpy(info->version, IBMVNIC_DRIVER_VERSION, sizeof(info->version));
1572}
1573
1574static u32 ibmvnic_get_msglevel(struct net_device *netdev)
1575{
1576 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1577
1578 return adapter->msg_enable;
1579}
1580
1581static void ibmvnic_set_msglevel(struct net_device *netdev, u32 data)
1582{
1583 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1584
1585 adapter->msg_enable = data;
1586}
1587
1588static u32 ibmvnic_get_link(struct net_device *netdev)
1589{
1590 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1591
1592 /* Don't need to send a query because we request a logical link up at
1593 * init and then we wait for link state indications
1594 */
1595 return adapter->logical_link_state;
1596}
1597
1598static void ibmvnic_get_ringparam(struct net_device *netdev,
1599 struct ethtool_ringparam *ring)
1600{
1601 ring->rx_max_pending = 0;
1602 ring->tx_max_pending = 0;
1603 ring->rx_mini_max_pending = 0;
1604 ring->rx_jumbo_max_pending = 0;
1605 ring->rx_pending = 0;
1606 ring->tx_pending = 0;
1607 ring->rx_mini_pending = 0;
1608 ring->rx_jumbo_pending = 0;
1609}
1610
1611static void ibmvnic_get_strings(struct net_device *dev, u32 stringset, u8 *data)
1612{
1613 int i;
1614
1615 if (stringset != ETH_SS_STATS)
1616 return;
1617
1618 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++, data += ETH_GSTRING_LEN)
1619 memcpy(data, ibmvnic_stats[i].name, ETH_GSTRING_LEN);
1620}
1621
1622static int ibmvnic_get_sset_count(struct net_device *dev, int sset)
1623{
1624 switch (sset) {
1625 case ETH_SS_STATS:
1626 return ARRAY_SIZE(ibmvnic_stats);
1627 default:
1628 return -EOPNOTSUPP;
1629 }
1630}
1631
1632static void ibmvnic_get_ethtool_stats(struct net_device *dev,
1633 struct ethtool_stats *stats, u64 *data)
1634{
1635 struct ibmvnic_adapter *adapter = netdev_priv(dev);
1636 union ibmvnic_crq crq;
1637 int i;
1638
1639 memset(&crq, 0, sizeof(crq));
1640 crq.request_statistics.first = IBMVNIC_CRQ_CMD;
1641 crq.request_statistics.cmd = REQUEST_STATISTICS;
1642 crq.request_statistics.ioba = cpu_to_be32(adapter->stats_token);
1643 crq.request_statistics.len =
1644 cpu_to_be32(sizeof(struct ibmvnic_statistics));
Thomas Falcon032c5e82015-12-21 11:26:06 -06001645
1646 /* Wait for data to be written */
1647 init_completion(&adapter->stats_done);
Nathan Fontenotdb5d0b52017-02-10 13:45:05 -05001648 ibmvnic_send_crq(adapter, &crq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001649 wait_for_completion(&adapter->stats_done);
1650
1651 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++)
1652 data[i] = IBMVNIC_GET_STAT(adapter, ibmvnic_stats[i].offset);
1653}
1654
1655static const struct ethtool_ops ibmvnic_ethtool_ops = {
Thomas Falcon032c5e82015-12-21 11:26:06 -06001656 .get_drvinfo = ibmvnic_get_drvinfo,
1657 .get_msglevel = ibmvnic_get_msglevel,
1658 .set_msglevel = ibmvnic_set_msglevel,
1659 .get_link = ibmvnic_get_link,
1660 .get_ringparam = ibmvnic_get_ringparam,
1661 .get_strings = ibmvnic_get_strings,
1662 .get_sset_count = ibmvnic_get_sset_count,
1663 .get_ethtool_stats = ibmvnic_get_ethtool_stats,
Philippe Reynes8a433792017-01-07 22:37:29 +01001664 .get_link_ksettings = ibmvnic_get_link_ksettings,
Thomas Falcon032c5e82015-12-21 11:26:06 -06001665};
1666
1667/* Routines for managing CRQs/sCRQs */
1668
1669static void release_sub_crq_queue(struct ibmvnic_adapter *adapter,
1670 struct ibmvnic_sub_crq_queue *scrq)
1671{
1672 struct device *dev = &adapter->vdev->dev;
1673 long rc;
1674
1675 netdev_dbg(adapter->netdev, "Releasing sub-CRQ\n");
1676
1677 /* Close the sub-crqs */
1678 do {
1679 rc = plpar_hcall_norets(H_FREE_SUB_CRQ,
1680 adapter->vdev->unit_address,
1681 scrq->crq_num);
1682 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
1683
Thomas Falconffa73852017-04-19 13:44:29 -04001684 if (rc) {
1685 netdev_err(adapter->netdev,
1686 "Failed to release sub-CRQ %16lx, rc = %ld\n",
1687 scrq->crq_num, rc);
1688 }
1689
Thomas Falcon032c5e82015-12-21 11:26:06 -06001690 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
1691 DMA_BIDIRECTIONAL);
1692 free_pages((unsigned long)scrq->msgs, 2);
1693 kfree(scrq);
1694}
1695
1696static struct ibmvnic_sub_crq_queue *init_sub_crq_queue(struct ibmvnic_adapter
1697 *adapter)
1698{
1699 struct device *dev = &adapter->vdev->dev;
1700 struct ibmvnic_sub_crq_queue *scrq;
1701 int rc;
1702
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04001703 scrq = kzalloc(sizeof(*scrq), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001704 if (!scrq)
1705 return NULL;
1706
Nathan Fontenot7f7adc52017-04-19 13:45:16 -04001707 scrq->msgs =
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04001708 (union sub_crq *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, 2);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001709 if (!scrq->msgs) {
1710 dev_warn(dev, "Couldn't allocate crq queue messages page\n");
1711 goto zero_page_failed;
1712 }
1713
1714 scrq->msg_token = dma_map_single(dev, scrq->msgs, 4 * PAGE_SIZE,
1715 DMA_BIDIRECTIONAL);
1716 if (dma_mapping_error(dev, scrq->msg_token)) {
1717 dev_warn(dev, "Couldn't map crq queue messages page\n");
1718 goto map_failed;
1719 }
1720
1721 rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
1722 4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
1723
1724 if (rc == H_RESOURCE)
1725 rc = ibmvnic_reset_crq(adapter);
1726
1727 if (rc == H_CLOSED) {
1728 dev_warn(dev, "Partner adapter not ready, waiting.\n");
1729 } else if (rc) {
1730 dev_warn(dev, "Error %d registering sub-crq\n", rc);
1731 goto reg_failed;
1732 }
1733
Thomas Falcon032c5e82015-12-21 11:26:06 -06001734 scrq->adapter = adapter;
1735 scrq->size = 4 * PAGE_SIZE / sizeof(*scrq->msgs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001736 spin_lock_init(&scrq->lock);
1737
1738 netdev_dbg(adapter->netdev,
1739 "sub-crq initialized, num %lx, hw_irq=%lx, irq=%x\n",
1740 scrq->crq_num, scrq->hw_irq, scrq->irq);
1741
1742 return scrq;
1743
Thomas Falcon032c5e82015-12-21 11:26:06 -06001744reg_failed:
1745 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
1746 DMA_BIDIRECTIONAL);
1747map_failed:
1748 free_pages((unsigned long)scrq->msgs, 2);
1749zero_page_failed:
1750 kfree(scrq);
1751
1752 return NULL;
1753}
1754
1755static void release_sub_crqs(struct ibmvnic_adapter *adapter)
1756{
1757 int i;
1758
1759 if (adapter->tx_scrq) {
Nathan Fontenotb5108882017-03-30 02:49:18 -04001760 for (i = 0; i < adapter->req_tx_queues; i++) {
1761 if (!adapter->tx_scrq[i])
1762 continue;
1763
1764 if (adapter->tx_scrq[i]->irq) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06001765 free_irq(adapter->tx_scrq[i]->irq,
1766 adapter->tx_scrq[i]);
Thomas Falcon88eb98a2016-07-06 15:35:16 -05001767 irq_dispose_mapping(adapter->tx_scrq[i]->irq);
Nathan Fontenotb5108882017-03-30 02:49:18 -04001768 adapter->tx_scrq[i]->irq = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001769 }
Nathan Fontenotb5108882017-03-30 02:49:18 -04001770
1771 release_sub_crq_queue(adapter, adapter->tx_scrq[i]);
1772 }
1773
Nathan Fontenot9501df32017-03-15 23:38:07 -04001774 kfree(adapter->tx_scrq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001775 adapter->tx_scrq = NULL;
1776 }
1777
1778 if (adapter->rx_scrq) {
Nathan Fontenotb5108882017-03-30 02:49:18 -04001779 for (i = 0; i < adapter->req_rx_queues; i++) {
1780 if (!adapter->rx_scrq[i])
1781 continue;
1782
1783 if (adapter->rx_scrq[i]->irq) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06001784 free_irq(adapter->rx_scrq[i]->irq,
1785 adapter->rx_scrq[i]);
Thomas Falcon88eb98a2016-07-06 15:35:16 -05001786 irq_dispose_mapping(adapter->rx_scrq[i]->irq);
Nathan Fontenotb5108882017-03-30 02:49:18 -04001787 adapter->rx_scrq[i]->irq = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001788 }
Nathan Fontenotb5108882017-03-30 02:49:18 -04001789
1790 release_sub_crq_queue(adapter, adapter->rx_scrq[i]);
1791 }
1792
Nathan Fontenot9501df32017-03-15 23:38:07 -04001793 kfree(adapter->rx_scrq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001794 adapter->rx_scrq = NULL;
1795 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001796}
1797
1798static int disable_scrq_irq(struct ibmvnic_adapter *adapter,
1799 struct ibmvnic_sub_crq_queue *scrq)
1800{
1801 struct device *dev = &adapter->vdev->dev;
1802 unsigned long rc;
1803
1804 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
1805 H_DISABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
1806 if (rc)
1807 dev_err(dev, "Couldn't disable scrq irq 0x%lx. rc=%ld\n",
1808 scrq->hw_irq, rc);
1809 return rc;
1810}
1811
1812static int enable_scrq_irq(struct ibmvnic_adapter *adapter,
1813 struct ibmvnic_sub_crq_queue *scrq)
1814{
1815 struct device *dev = &adapter->vdev->dev;
1816 unsigned long rc;
1817
1818 if (scrq->hw_irq > 0x100000000ULL) {
1819 dev_err(dev, "bad hw_irq = %lx\n", scrq->hw_irq);
1820 return 1;
1821 }
1822
1823 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
1824 H_ENABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
1825 if (rc)
1826 dev_err(dev, "Couldn't enable scrq irq 0x%lx. rc=%ld\n",
1827 scrq->hw_irq, rc);
1828 return rc;
1829}
1830
1831static int ibmvnic_complete_tx(struct ibmvnic_adapter *adapter,
1832 struct ibmvnic_sub_crq_queue *scrq)
1833{
1834 struct device *dev = &adapter->vdev->dev;
1835 struct ibmvnic_tx_buff *txbuff;
1836 union sub_crq *next;
1837 int index;
1838 int i, j;
Thomas Falconad7775d2016-04-01 17:20:34 -05001839 u8 first;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001840
1841restart_loop:
1842 while (pending_scrq(adapter, scrq)) {
1843 unsigned int pool = scrq->pool_index;
1844
1845 next = ibmvnic_next_scrq(adapter, scrq);
1846 for (i = 0; i < next->tx_comp.num_comps; i++) {
1847 if (next->tx_comp.rcs[i]) {
1848 dev_err(dev, "tx error %x\n",
1849 next->tx_comp.rcs[i]);
1850 continue;
1851 }
1852 index = be32_to_cpu(next->tx_comp.correlators[i]);
1853 txbuff = &adapter->tx_pool[pool].tx_buff[index];
1854
1855 for (j = 0; j < IBMVNIC_MAX_FRAGS_PER_CRQ; j++) {
1856 if (!txbuff->data_dma[j])
1857 continue;
1858
1859 txbuff->data_dma[j] = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001860 }
Thomas Falconad7775d2016-04-01 17:20:34 -05001861 /* if sub_crq was sent indirectly */
1862 first = txbuff->indir_arr[0].generic.first;
1863 if (first == IBMVNIC_CRQ_CMD) {
1864 dma_unmap_single(dev, txbuff->indir_dma,
1865 sizeof(txbuff->indir_arr),
1866 DMA_TO_DEVICE);
1867 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001868
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001869 if (txbuff->last_frag) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06001870 dev_kfree_skb_any(txbuff->skb);
Nathan Fontenot7c3e7de2017-05-03 14:05:25 -04001871 txbuff->skb = NULL;
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001872 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001873
1874 adapter->tx_pool[pool].free_map[adapter->tx_pool[pool].
1875 producer_index] = index;
1876 adapter->tx_pool[pool].producer_index =
1877 (adapter->tx_pool[pool].producer_index + 1) %
Thomas Falcon068d9f92017-03-05 12:18:42 -06001878 adapter->req_tx_entries_per_subcrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001879 }
1880 /* remove tx_comp scrq*/
1881 next->tx_comp.first = 0;
Nathan Fontenot7c3e7de2017-05-03 14:05:25 -04001882
1883 if (atomic_sub_return(next->tx_comp.num_comps, &scrq->used) <=
1884 (adapter->req_tx_entries_per_subcrq / 2) &&
1885 __netif_subqueue_stopped(adapter->netdev,
1886 scrq->pool_index)) {
1887 netif_wake_subqueue(adapter->netdev, scrq->pool_index);
1888 netdev_info(adapter->netdev, "Started queue %d\n",
1889 scrq->pool_index);
1890 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001891 }
1892
1893 enable_scrq_irq(adapter, scrq);
1894
1895 if (pending_scrq(adapter, scrq)) {
1896 disable_scrq_irq(adapter, scrq);
1897 goto restart_loop;
1898 }
1899
1900 return 0;
1901}
1902
1903static irqreturn_t ibmvnic_interrupt_tx(int irq, void *instance)
1904{
1905 struct ibmvnic_sub_crq_queue *scrq = instance;
1906 struct ibmvnic_adapter *adapter = scrq->adapter;
1907
1908 disable_scrq_irq(adapter, scrq);
1909 ibmvnic_complete_tx(adapter, scrq);
1910
1911 return IRQ_HANDLED;
1912}
1913
1914static irqreturn_t ibmvnic_interrupt_rx(int irq, void *instance)
1915{
1916 struct ibmvnic_sub_crq_queue *scrq = instance;
1917 struct ibmvnic_adapter *adapter = scrq->adapter;
1918
1919 if (napi_schedule_prep(&adapter->napi[scrq->scrq_num])) {
1920 disable_scrq_irq(adapter, scrq);
1921 __napi_schedule(&adapter->napi[scrq->scrq_num]);
1922 }
1923
1924 return IRQ_HANDLED;
1925}
1926
Thomas Falconea22d512016-07-06 15:35:17 -05001927static int init_sub_crq_irqs(struct ibmvnic_adapter *adapter)
1928{
1929 struct device *dev = &adapter->vdev->dev;
1930 struct ibmvnic_sub_crq_queue *scrq;
1931 int i = 0, j = 0;
1932 int rc = 0;
1933
1934 for (i = 0; i < adapter->req_tx_queues; i++) {
1935 scrq = adapter->tx_scrq[i];
1936 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
1937
Michael Ellerman99c17902016-09-10 19:59:05 +10001938 if (!scrq->irq) {
Thomas Falconea22d512016-07-06 15:35:17 -05001939 rc = -EINVAL;
1940 dev_err(dev, "Error mapping irq\n");
1941 goto req_tx_irq_failed;
1942 }
1943
1944 rc = request_irq(scrq->irq, ibmvnic_interrupt_tx,
1945 0, "ibmvnic_tx", scrq);
1946
1947 if (rc) {
1948 dev_err(dev, "Couldn't register tx irq 0x%x. rc=%d\n",
1949 scrq->irq, rc);
1950 irq_dispose_mapping(scrq->irq);
1951 goto req_rx_irq_failed;
1952 }
1953 }
1954
1955 for (i = 0; i < adapter->req_rx_queues; i++) {
1956 scrq = adapter->rx_scrq[i];
1957 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
Michael Ellerman99c17902016-09-10 19:59:05 +10001958 if (!scrq->irq) {
Thomas Falconea22d512016-07-06 15:35:17 -05001959 rc = -EINVAL;
1960 dev_err(dev, "Error mapping irq\n");
1961 goto req_rx_irq_failed;
1962 }
1963 rc = request_irq(scrq->irq, ibmvnic_interrupt_rx,
1964 0, "ibmvnic_rx", scrq);
1965 if (rc) {
1966 dev_err(dev, "Couldn't register rx irq 0x%x. rc=%d\n",
1967 scrq->irq, rc);
1968 irq_dispose_mapping(scrq->irq);
1969 goto req_rx_irq_failed;
1970 }
1971 }
1972 return rc;
1973
1974req_rx_irq_failed:
Thomas Falcon8bf371e2016-10-27 12:28:52 -05001975 for (j = 0; j < i; j++) {
Thomas Falconea22d512016-07-06 15:35:17 -05001976 free_irq(adapter->rx_scrq[j]->irq, adapter->rx_scrq[j]);
1977 irq_dispose_mapping(adapter->rx_scrq[j]->irq);
Thomas Falcon8bf371e2016-10-27 12:28:52 -05001978 }
Thomas Falconea22d512016-07-06 15:35:17 -05001979 i = adapter->req_tx_queues;
1980req_tx_irq_failed:
Thomas Falcon8bf371e2016-10-27 12:28:52 -05001981 for (j = 0; j < i; j++) {
Thomas Falconea22d512016-07-06 15:35:17 -05001982 free_irq(adapter->tx_scrq[j]->irq, adapter->tx_scrq[j]);
1983 irq_dispose_mapping(adapter->rx_scrq[j]->irq);
Thomas Falcon8bf371e2016-10-27 12:28:52 -05001984 }
Nathan Fontenotb5108882017-03-30 02:49:18 -04001985 release_sub_crqs(adapter);
Thomas Falconea22d512016-07-06 15:35:17 -05001986 return rc;
1987}
1988
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04001989static int init_sub_crqs(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06001990{
1991 struct device *dev = &adapter->vdev->dev;
1992 struct ibmvnic_sub_crq_queue **allqueues;
1993 int registered_queues = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001994 int total_queues;
1995 int more = 0;
Thomas Falconea22d512016-07-06 15:35:17 -05001996 int i;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001997
Thomas Falcon032c5e82015-12-21 11:26:06 -06001998 total_queues = adapter->req_tx_queues + adapter->req_rx_queues;
1999
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04002000 allqueues = kcalloc(total_queues, sizeof(*allqueues), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002001 if (!allqueues)
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002002 return -1;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002003
2004 for (i = 0; i < total_queues; i++) {
2005 allqueues[i] = init_sub_crq_queue(adapter);
2006 if (!allqueues[i]) {
2007 dev_warn(dev, "Couldn't allocate all sub-crqs\n");
2008 break;
2009 }
2010 registered_queues++;
2011 }
2012
2013 /* Make sure we were able to register the minimum number of queues */
2014 if (registered_queues <
2015 adapter->min_tx_queues + adapter->min_rx_queues) {
2016 dev_err(dev, "Fatal: Couldn't init min number of sub-crqs\n");
2017 goto tx_failed;
2018 }
2019
2020 /* Distribute the failed allocated queues*/
2021 for (i = 0; i < total_queues - registered_queues + more ; i++) {
2022 netdev_dbg(adapter->netdev, "Reducing number of queues\n");
2023 switch (i % 3) {
2024 case 0:
2025 if (adapter->req_rx_queues > adapter->min_rx_queues)
2026 adapter->req_rx_queues--;
2027 else
2028 more++;
2029 break;
2030 case 1:
2031 if (adapter->req_tx_queues > adapter->min_tx_queues)
2032 adapter->req_tx_queues--;
2033 else
2034 more++;
2035 break;
2036 }
2037 }
2038
2039 adapter->tx_scrq = kcalloc(adapter->req_tx_queues,
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04002040 sizeof(*adapter->tx_scrq), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002041 if (!adapter->tx_scrq)
2042 goto tx_failed;
2043
2044 for (i = 0; i < adapter->req_tx_queues; i++) {
2045 adapter->tx_scrq[i] = allqueues[i];
2046 adapter->tx_scrq[i]->pool_index = i;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002047 }
2048
2049 adapter->rx_scrq = kcalloc(adapter->req_rx_queues,
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04002050 sizeof(*adapter->rx_scrq), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002051 if (!adapter->rx_scrq)
2052 goto rx_failed;
2053
2054 for (i = 0; i < adapter->req_rx_queues; i++) {
2055 adapter->rx_scrq[i] = allqueues[i + adapter->req_tx_queues];
2056 adapter->rx_scrq[i]->scrq_num = i;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002057 }
2058
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002059 kfree(allqueues);
2060 return 0;
2061
2062rx_failed:
2063 kfree(adapter->tx_scrq);
2064 adapter->tx_scrq = NULL;
2065tx_failed:
2066 for (i = 0; i < registered_queues; i++)
2067 release_sub_crq_queue(adapter, allqueues[i]);
2068 kfree(allqueues);
2069 return -1;
2070}
2071
2072static void ibmvnic_send_req_caps(struct ibmvnic_adapter *adapter, int retry)
2073{
2074 struct device *dev = &adapter->vdev->dev;
2075 union ibmvnic_crq crq;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002076
2077 if (!retry) {
2078 /* Sub-CRQ entries are 32 byte long */
2079 int entries_page = 4 * PAGE_SIZE / (sizeof(u64) * 4);
2080
2081 if (adapter->min_tx_entries_per_subcrq > entries_page ||
2082 adapter->min_rx_add_entries_per_subcrq > entries_page) {
2083 dev_err(dev, "Fatal, invalid entries per sub-crq\n");
2084 return;
2085 }
2086
2087 /* Get the minimum between the queried max and the entries
2088 * that fit in our PAGE_SIZE
2089 */
2090 adapter->req_tx_entries_per_subcrq =
2091 adapter->max_tx_entries_per_subcrq > entries_page ?
2092 entries_page : adapter->max_tx_entries_per_subcrq;
2093 adapter->req_rx_add_entries_per_subcrq =
2094 adapter->max_rx_add_entries_per_subcrq > entries_page ?
2095 entries_page : adapter->max_rx_add_entries_per_subcrq;
2096
2097 adapter->req_tx_queues = adapter->opt_tx_comp_sub_queues;
2098 adapter->req_rx_queues = adapter->opt_rx_comp_queues;
2099 adapter->req_rx_add_queues = adapter->max_rx_add_queues;
2100
2101 adapter->req_mtu = adapter->netdev->mtu + ETH_HLEN;
2102 }
2103
Thomas Falcon032c5e82015-12-21 11:26:06 -06002104 memset(&crq, 0, sizeof(crq));
2105 crq.request_capability.first = IBMVNIC_CRQ_CMD;
2106 crq.request_capability.cmd = REQUEST_CAPABILITY;
2107
2108 crq.request_capability.capability = cpu_to_be16(REQ_TX_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06002109 crq.request_capability.number = cpu_to_be64(adapter->req_tx_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06002110 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002111 ibmvnic_send_crq(adapter, &crq);
2112
2113 crq.request_capability.capability = cpu_to_be16(REQ_RX_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06002114 crq.request_capability.number = cpu_to_be64(adapter->req_rx_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06002115 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002116 ibmvnic_send_crq(adapter, &crq);
2117
2118 crq.request_capability.capability = cpu_to_be16(REQ_RX_ADD_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06002119 crq.request_capability.number = cpu_to_be64(adapter->req_rx_add_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06002120 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002121 ibmvnic_send_crq(adapter, &crq);
2122
2123 crq.request_capability.capability =
2124 cpu_to_be16(REQ_TX_ENTRIES_PER_SUBCRQ);
2125 crq.request_capability.number =
Thomas Falconde89e852016-03-01 10:20:09 -06002126 cpu_to_be64(adapter->req_tx_entries_per_subcrq);
Thomas Falcon901e0402017-02-15 12:17:59 -06002127 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002128 ibmvnic_send_crq(adapter, &crq);
2129
2130 crq.request_capability.capability =
2131 cpu_to_be16(REQ_RX_ADD_ENTRIES_PER_SUBCRQ);
2132 crq.request_capability.number =
Thomas Falconde89e852016-03-01 10:20:09 -06002133 cpu_to_be64(adapter->req_rx_add_entries_per_subcrq);
Thomas Falcon901e0402017-02-15 12:17:59 -06002134 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002135 ibmvnic_send_crq(adapter, &crq);
2136
2137 crq.request_capability.capability = cpu_to_be16(REQ_MTU);
Thomas Falconde89e852016-03-01 10:20:09 -06002138 crq.request_capability.number = cpu_to_be64(adapter->req_mtu);
Thomas Falcon901e0402017-02-15 12:17:59 -06002139 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002140 ibmvnic_send_crq(adapter, &crq);
2141
2142 if (adapter->netdev->flags & IFF_PROMISC) {
2143 if (adapter->promisc_supported) {
2144 crq.request_capability.capability =
2145 cpu_to_be16(PROMISC_REQUESTED);
Thomas Falconde89e852016-03-01 10:20:09 -06002146 crq.request_capability.number = cpu_to_be64(1);
Thomas Falcon901e0402017-02-15 12:17:59 -06002147 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002148 ibmvnic_send_crq(adapter, &crq);
2149 }
2150 } else {
2151 crq.request_capability.capability =
2152 cpu_to_be16(PROMISC_REQUESTED);
Thomas Falconde89e852016-03-01 10:20:09 -06002153 crq.request_capability.number = cpu_to_be64(0);
Thomas Falcon901e0402017-02-15 12:17:59 -06002154 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002155 ibmvnic_send_crq(adapter, &crq);
2156 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002157}
2158
2159static int pending_scrq(struct ibmvnic_adapter *adapter,
2160 struct ibmvnic_sub_crq_queue *scrq)
2161{
2162 union sub_crq *entry = &scrq->msgs[scrq->cur];
2163
Nathan Fontenot90c80142017-05-03 14:04:32 -04002164 if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP ||
2165 adapter->state == VNIC_CLOSING)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002166 return 1;
2167 else
2168 return 0;
2169}
2170
2171static union sub_crq *ibmvnic_next_scrq(struct ibmvnic_adapter *adapter,
2172 struct ibmvnic_sub_crq_queue *scrq)
2173{
2174 union sub_crq *entry;
2175 unsigned long flags;
2176
2177 spin_lock_irqsave(&scrq->lock, flags);
2178 entry = &scrq->msgs[scrq->cur];
2179 if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP) {
2180 if (++scrq->cur == scrq->size)
2181 scrq->cur = 0;
2182 } else {
2183 entry = NULL;
2184 }
2185 spin_unlock_irqrestore(&scrq->lock, flags);
2186
2187 return entry;
2188}
2189
2190static union ibmvnic_crq *ibmvnic_next_crq(struct ibmvnic_adapter *adapter)
2191{
2192 struct ibmvnic_crq_queue *queue = &adapter->crq;
2193 union ibmvnic_crq *crq;
2194
2195 crq = &queue->msgs[queue->cur];
2196 if (crq->generic.first & IBMVNIC_CRQ_CMD_RSP) {
2197 if (++queue->cur == queue->size)
2198 queue->cur = 0;
2199 } else {
2200 crq = NULL;
2201 }
2202
2203 return crq;
2204}
2205
2206static int send_subcrq(struct ibmvnic_adapter *adapter, u64 remote_handle,
2207 union sub_crq *sub_crq)
2208{
2209 unsigned int ua = adapter->vdev->unit_address;
2210 struct device *dev = &adapter->vdev->dev;
2211 u64 *u64_crq = (u64 *)sub_crq;
2212 int rc;
2213
2214 netdev_dbg(adapter->netdev,
2215 "Sending sCRQ %016lx: %016lx %016lx %016lx %016lx\n",
2216 (unsigned long int)cpu_to_be64(remote_handle),
2217 (unsigned long int)cpu_to_be64(u64_crq[0]),
2218 (unsigned long int)cpu_to_be64(u64_crq[1]),
2219 (unsigned long int)cpu_to_be64(u64_crq[2]),
2220 (unsigned long int)cpu_to_be64(u64_crq[3]));
2221
2222 /* Make sure the hypervisor sees the complete request */
2223 mb();
2224
2225 rc = plpar_hcall_norets(H_SEND_SUB_CRQ, ua,
2226 cpu_to_be64(remote_handle),
2227 cpu_to_be64(u64_crq[0]),
2228 cpu_to_be64(u64_crq[1]),
2229 cpu_to_be64(u64_crq[2]),
2230 cpu_to_be64(u64_crq[3]));
2231
2232 if (rc) {
2233 if (rc == H_CLOSED)
2234 dev_warn(dev, "CRQ Queue closed\n");
2235 dev_err(dev, "Send error (rc=%d)\n", rc);
2236 }
2237
2238 return rc;
2239}
2240
Thomas Falconad7775d2016-04-01 17:20:34 -05002241static int send_subcrq_indirect(struct ibmvnic_adapter *adapter,
2242 u64 remote_handle, u64 ioba, u64 num_entries)
2243{
2244 unsigned int ua = adapter->vdev->unit_address;
2245 struct device *dev = &adapter->vdev->dev;
2246 int rc;
2247
2248 /* Make sure the hypervisor sees the complete request */
2249 mb();
2250 rc = plpar_hcall_norets(H_SEND_SUB_CRQ_INDIRECT, ua,
2251 cpu_to_be64(remote_handle),
2252 ioba, num_entries);
2253
2254 if (rc) {
2255 if (rc == H_CLOSED)
2256 dev_warn(dev, "CRQ Queue closed\n");
2257 dev_err(dev, "Send (indirect) error (rc=%d)\n", rc);
2258 }
2259
2260 return rc;
2261}
2262
Thomas Falcon032c5e82015-12-21 11:26:06 -06002263static int ibmvnic_send_crq(struct ibmvnic_adapter *adapter,
2264 union ibmvnic_crq *crq)
2265{
2266 unsigned int ua = adapter->vdev->unit_address;
2267 struct device *dev = &adapter->vdev->dev;
2268 u64 *u64_crq = (u64 *)crq;
2269 int rc;
2270
2271 netdev_dbg(adapter->netdev, "Sending CRQ: %016lx %016lx\n",
2272 (unsigned long int)cpu_to_be64(u64_crq[0]),
2273 (unsigned long int)cpu_to_be64(u64_crq[1]));
2274
2275 /* Make sure the hypervisor sees the complete request */
2276 mb();
2277
2278 rc = plpar_hcall_norets(H_SEND_CRQ, ua,
2279 cpu_to_be64(u64_crq[0]),
2280 cpu_to_be64(u64_crq[1]));
2281
2282 if (rc) {
2283 if (rc == H_CLOSED)
2284 dev_warn(dev, "CRQ Queue closed\n");
2285 dev_warn(dev, "Send error (rc=%d)\n", rc);
2286 }
2287
2288 return rc;
2289}
2290
2291static int ibmvnic_send_crq_init(struct ibmvnic_adapter *adapter)
2292{
2293 union ibmvnic_crq crq;
2294
2295 memset(&crq, 0, sizeof(crq));
2296 crq.generic.first = IBMVNIC_CRQ_INIT_CMD;
2297 crq.generic.cmd = IBMVNIC_CRQ_INIT;
2298 netdev_dbg(adapter->netdev, "Sending CRQ init\n");
2299
2300 return ibmvnic_send_crq(adapter, &crq);
2301}
2302
Thomas Falcon032c5e82015-12-21 11:26:06 -06002303static int send_version_xchg(struct ibmvnic_adapter *adapter)
2304{
2305 union ibmvnic_crq crq;
2306
2307 memset(&crq, 0, sizeof(crq));
2308 crq.version_exchange.first = IBMVNIC_CRQ_CMD;
2309 crq.version_exchange.cmd = VERSION_EXCHANGE;
2310 crq.version_exchange.version = cpu_to_be16(ibmvnic_version);
2311
2312 return ibmvnic_send_crq(adapter, &crq);
2313}
2314
2315static void send_login(struct ibmvnic_adapter *adapter)
2316{
2317 struct ibmvnic_login_rsp_buffer *login_rsp_buffer;
2318 struct ibmvnic_login_buffer *login_buffer;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002319 struct device *dev = &adapter->vdev->dev;
2320 dma_addr_t rsp_buffer_token;
2321 dma_addr_t buffer_token;
2322 size_t rsp_buffer_size;
2323 union ibmvnic_crq crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002324 size_t buffer_size;
2325 __be64 *tx_list_p;
2326 __be64 *rx_list_p;
2327 int i;
2328
2329 buffer_size =
2330 sizeof(struct ibmvnic_login_buffer) +
2331 sizeof(u64) * (adapter->req_tx_queues + adapter->req_rx_queues);
2332
2333 login_buffer = kmalloc(buffer_size, GFP_ATOMIC);
2334 if (!login_buffer)
2335 goto buf_alloc_failed;
2336
2337 buffer_token = dma_map_single(dev, login_buffer, buffer_size,
2338 DMA_TO_DEVICE);
2339 if (dma_mapping_error(dev, buffer_token)) {
2340 dev_err(dev, "Couldn't map login buffer\n");
2341 goto buf_map_failed;
2342 }
2343
John Allen498cd8e2016-04-06 11:49:55 -05002344 rsp_buffer_size = sizeof(struct ibmvnic_login_rsp_buffer) +
2345 sizeof(u64) * adapter->req_tx_queues +
2346 sizeof(u64) * adapter->req_rx_queues +
2347 sizeof(u64) * adapter->req_rx_queues +
2348 sizeof(u8) * IBMVNIC_TX_DESC_VERSIONS;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002349
2350 login_rsp_buffer = kmalloc(rsp_buffer_size, GFP_ATOMIC);
2351 if (!login_rsp_buffer)
2352 goto buf_rsp_alloc_failed;
2353
2354 rsp_buffer_token = dma_map_single(dev, login_rsp_buffer,
2355 rsp_buffer_size, DMA_FROM_DEVICE);
2356 if (dma_mapping_error(dev, rsp_buffer_token)) {
2357 dev_err(dev, "Couldn't map login rsp buffer\n");
2358 goto buf_rsp_map_failed;
2359 }
Nathan Fontenot661a2622017-04-19 13:44:58 -04002360
Thomas Falcon032c5e82015-12-21 11:26:06 -06002361 adapter->login_buf = login_buffer;
2362 adapter->login_buf_token = buffer_token;
2363 adapter->login_buf_sz = buffer_size;
2364 adapter->login_rsp_buf = login_rsp_buffer;
2365 adapter->login_rsp_buf_token = rsp_buffer_token;
2366 adapter->login_rsp_buf_sz = rsp_buffer_size;
2367
2368 login_buffer->len = cpu_to_be32(buffer_size);
2369 login_buffer->version = cpu_to_be32(INITIAL_VERSION_LB);
2370 login_buffer->num_txcomp_subcrqs = cpu_to_be32(adapter->req_tx_queues);
2371 login_buffer->off_txcomp_subcrqs =
2372 cpu_to_be32(sizeof(struct ibmvnic_login_buffer));
2373 login_buffer->num_rxcomp_subcrqs = cpu_to_be32(adapter->req_rx_queues);
2374 login_buffer->off_rxcomp_subcrqs =
2375 cpu_to_be32(sizeof(struct ibmvnic_login_buffer) +
2376 sizeof(u64) * adapter->req_tx_queues);
2377 login_buffer->login_rsp_ioba = cpu_to_be32(rsp_buffer_token);
2378 login_buffer->login_rsp_len = cpu_to_be32(rsp_buffer_size);
2379
2380 tx_list_p = (__be64 *)((char *)login_buffer +
2381 sizeof(struct ibmvnic_login_buffer));
2382 rx_list_p = (__be64 *)((char *)login_buffer +
2383 sizeof(struct ibmvnic_login_buffer) +
2384 sizeof(u64) * adapter->req_tx_queues);
2385
2386 for (i = 0; i < adapter->req_tx_queues; i++) {
2387 if (adapter->tx_scrq[i]) {
2388 tx_list_p[i] = cpu_to_be64(adapter->tx_scrq[i]->
2389 crq_num);
2390 }
2391 }
2392
2393 for (i = 0; i < adapter->req_rx_queues; i++) {
2394 if (adapter->rx_scrq[i]) {
2395 rx_list_p[i] = cpu_to_be64(adapter->rx_scrq[i]->
2396 crq_num);
2397 }
2398 }
2399
2400 netdev_dbg(adapter->netdev, "Login Buffer:\n");
2401 for (i = 0; i < (adapter->login_buf_sz - 1) / 8 + 1; i++) {
2402 netdev_dbg(adapter->netdev, "%016lx\n",
2403 ((unsigned long int *)(adapter->login_buf))[i]);
2404 }
2405
2406 memset(&crq, 0, sizeof(crq));
2407 crq.login.first = IBMVNIC_CRQ_CMD;
2408 crq.login.cmd = LOGIN;
2409 crq.login.ioba = cpu_to_be32(buffer_token);
2410 crq.login.len = cpu_to_be32(buffer_size);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002411 ibmvnic_send_crq(adapter, &crq);
2412
2413 return;
2414
Thomas Falcon032c5e82015-12-21 11:26:06 -06002415buf_rsp_map_failed:
2416 kfree(login_rsp_buffer);
2417buf_rsp_alloc_failed:
2418 dma_unmap_single(dev, buffer_token, buffer_size, DMA_TO_DEVICE);
2419buf_map_failed:
2420 kfree(login_buffer);
2421buf_alloc_failed:
2422 return;
2423}
2424
2425static void send_request_map(struct ibmvnic_adapter *adapter, dma_addr_t addr,
2426 u32 len, u8 map_id)
2427{
2428 union ibmvnic_crq crq;
2429
2430 memset(&crq, 0, sizeof(crq));
2431 crq.request_map.first = IBMVNIC_CRQ_CMD;
2432 crq.request_map.cmd = REQUEST_MAP;
2433 crq.request_map.map_id = map_id;
2434 crq.request_map.ioba = cpu_to_be32(addr);
2435 crq.request_map.len = cpu_to_be32(len);
2436 ibmvnic_send_crq(adapter, &crq);
2437}
2438
2439static void send_request_unmap(struct ibmvnic_adapter *adapter, u8 map_id)
2440{
2441 union ibmvnic_crq crq;
2442
2443 memset(&crq, 0, sizeof(crq));
2444 crq.request_unmap.first = IBMVNIC_CRQ_CMD;
2445 crq.request_unmap.cmd = REQUEST_UNMAP;
2446 crq.request_unmap.map_id = map_id;
2447 ibmvnic_send_crq(adapter, &crq);
2448}
2449
2450static void send_map_query(struct ibmvnic_adapter *adapter)
2451{
2452 union ibmvnic_crq crq;
2453
2454 memset(&crq, 0, sizeof(crq));
2455 crq.query_map.first = IBMVNIC_CRQ_CMD;
2456 crq.query_map.cmd = QUERY_MAP;
2457 ibmvnic_send_crq(adapter, &crq);
2458}
2459
2460/* Send a series of CRQs requesting various capabilities of the VNIC server */
2461static void send_cap_queries(struct ibmvnic_adapter *adapter)
2462{
2463 union ibmvnic_crq crq;
2464
Thomas Falcon901e0402017-02-15 12:17:59 -06002465 atomic_set(&adapter->running_cap_crqs, 0);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002466 memset(&crq, 0, sizeof(crq));
2467 crq.query_capability.first = IBMVNIC_CRQ_CMD;
2468 crq.query_capability.cmd = QUERY_CAPABILITY;
2469
2470 crq.query_capability.capability = cpu_to_be16(MIN_TX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002471 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002472 ibmvnic_send_crq(adapter, &crq);
2473
2474 crq.query_capability.capability = cpu_to_be16(MIN_RX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002475 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002476 ibmvnic_send_crq(adapter, &crq);
2477
2478 crq.query_capability.capability = cpu_to_be16(MIN_RX_ADD_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002479 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002480 ibmvnic_send_crq(adapter, &crq);
2481
2482 crq.query_capability.capability = cpu_to_be16(MAX_TX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002483 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002484 ibmvnic_send_crq(adapter, &crq);
2485
2486 crq.query_capability.capability = cpu_to_be16(MAX_RX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002487 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002488 ibmvnic_send_crq(adapter, &crq);
2489
2490 crq.query_capability.capability = cpu_to_be16(MAX_RX_ADD_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002491 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002492 ibmvnic_send_crq(adapter, &crq);
2493
2494 crq.query_capability.capability =
2495 cpu_to_be16(MIN_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06002496 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002497 ibmvnic_send_crq(adapter, &crq);
2498
2499 crq.query_capability.capability =
2500 cpu_to_be16(MIN_RX_ADD_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06002501 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002502 ibmvnic_send_crq(adapter, &crq);
2503
2504 crq.query_capability.capability =
2505 cpu_to_be16(MAX_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06002506 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002507 ibmvnic_send_crq(adapter, &crq);
2508
2509 crq.query_capability.capability =
2510 cpu_to_be16(MAX_RX_ADD_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06002511 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002512 ibmvnic_send_crq(adapter, &crq);
2513
2514 crq.query_capability.capability = cpu_to_be16(TCP_IP_OFFLOAD);
Thomas Falcon901e0402017-02-15 12:17:59 -06002515 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002516 ibmvnic_send_crq(adapter, &crq);
2517
2518 crq.query_capability.capability = cpu_to_be16(PROMISC_SUPPORTED);
Thomas Falcon901e0402017-02-15 12:17:59 -06002519 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002520 ibmvnic_send_crq(adapter, &crq);
2521
2522 crq.query_capability.capability = cpu_to_be16(MIN_MTU);
Thomas Falcon901e0402017-02-15 12:17:59 -06002523 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002524 ibmvnic_send_crq(adapter, &crq);
2525
2526 crq.query_capability.capability = cpu_to_be16(MAX_MTU);
Thomas Falcon901e0402017-02-15 12:17:59 -06002527 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002528 ibmvnic_send_crq(adapter, &crq);
2529
2530 crq.query_capability.capability = cpu_to_be16(MAX_MULTICAST_FILTERS);
Thomas Falcon901e0402017-02-15 12:17:59 -06002531 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002532 ibmvnic_send_crq(adapter, &crq);
2533
2534 crq.query_capability.capability = cpu_to_be16(VLAN_HEADER_INSERTION);
Thomas Falcon901e0402017-02-15 12:17:59 -06002535 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002536 ibmvnic_send_crq(adapter, &crq);
2537
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04002538 crq.query_capability.capability = cpu_to_be16(RX_VLAN_HEADER_INSERTION);
2539 atomic_inc(&adapter->running_cap_crqs);
2540 ibmvnic_send_crq(adapter, &crq);
2541
Thomas Falcon032c5e82015-12-21 11:26:06 -06002542 crq.query_capability.capability = cpu_to_be16(MAX_TX_SG_ENTRIES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002543 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002544 ibmvnic_send_crq(adapter, &crq);
2545
2546 crq.query_capability.capability = cpu_to_be16(RX_SG_SUPPORTED);
Thomas Falcon901e0402017-02-15 12:17:59 -06002547 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002548 ibmvnic_send_crq(adapter, &crq);
2549
2550 crq.query_capability.capability = cpu_to_be16(OPT_TX_COMP_SUB_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002551 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002552 ibmvnic_send_crq(adapter, &crq);
2553
2554 crq.query_capability.capability = cpu_to_be16(OPT_RX_COMP_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002555 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002556 ibmvnic_send_crq(adapter, &crq);
2557
2558 crq.query_capability.capability =
2559 cpu_to_be16(OPT_RX_BUFADD_Q_PER_RX_COMP_Q);
Thomas Falcon901e0402017-02-15 12:17:59 -06002560 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002561 ibmvnic_send_crq(adapter, &crq);
2562
2563 crq.query_capability.capability =
2564 cpu_to_be16(OPT_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06002565 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002566 ibmvnic_send_crq(adapter, &crq);
2567
2568 crq.query_capability.capability =
2569 cpu_to_be16(OPT_RXBA_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06002570 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002571 ibmvnic_send_crq(adapter, &crq);
2572
2573 crq.query_capability.capability = cpu_to_be16(TX_RX_DESC_REQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06002574 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002575 ibmvnic_send_crq(adapter, &crq);
2576}
2577
2578static void handle_query_ip_offload_rsp(struct ibmvnic_adapter *adapter)
2579{
2580 struct device *dev = &adapter->vdev->dev;
2581 struct ibmvnic_query_ip_offload_buffer *buf = &adapter->ip_offload_buf;
2582 union ibmvnic_crq crq;
2583 int i;
2584
2585 dma_unmap_single(dev, adapter->ip_offload_tok,
2586 sizeof(adapter->ip_offload_buf), DMA_FROM_DEVICE);
2587
2588 netdev_dbg(adapter->netdev, "Query IP Offload Buffer:\n");
2589 for (i = 0; i < (sizeof(adapter->ip_offload_buf) - 1) / 8 + 1; i++)
2590 netdev_dbg(adapter->netdev, "%016lx\n",
2591 ((unsigned long int *)(buf))[i]);
2592
2593 netdev_dbg(adapter->netdev, "ipv4_chksum = %d\n", buf->ipv4_chksum);
2594 netdev_dbg(adapter->netdev, "ipv6_chksum = %d\n", buf->ipv6_chksum);
2595 netdev_dbg(adapter->netdev, "tcp_ipv4_chksum = %d\n",
2596 buf->tcp_ipv4_chksum);
2597 netdev_dbg(adapter->netdev, "tcp_ipv6_chksum = %d\n",
2598 buf->tcp_ipv6_chksum);
2599 netdev_dbg(adapter->netdev, "udp_ipv4_chksum = %d\n",
2600 buf->udp_ipv4_chksum);
2601 netdev_dbg(adapter->netdev, "udp_ipv6_chksum = %d\n",
2602 buf->udp_ipv6_chksum);
2603 netdev_dbg(adapter->netdev, "large_tx_ipv4 = %d\n",
2604 buf->large_tx_ipv4);
2605 netdev_dbg(adapter->netdev, "large_tx_ipv6 = %d\n",
2606 buf->large_tx_ipv6);
2607 netdev_dbg(adapter->netdev, "large_rx_ipv4 = %d\n",
2608 buf->large_rx_ipv4);
2609 netdev_dbg(adapter->netdev, "large_rx_ipv6 = %d\n",
2610 buf->large_rx_ipv6);
2611 netdev_dbg(adapter->netdev, "max_ipv4_hdr_sz = %d\n",
2612 buf->max_ipv4_header_size);
2613 netdev_dbg(adapter->netdev, "max_ipv6_hdr_sz = %d\n",
2614 buf->max_ipv6_header_size);
2615 netdev_dbg(adapter->netdev, "max_tcp_hdr_size = %d\n",
2616 buf->max_tcp_header_size);
2617 netdev_dbg(adapter->netdev, "max_udp_hdr_size = %d\n",
2618 buf->max_udp_header_size);
2619 netdev_dbg(adapter->netdev, "max_large_tx_size = %d\n",
2620 buf->max_large_tx_size);
2621 netdev_dbg(adapter->netdev, "max_large_rx_size = %d\n",
2622 buf->max_large_rx_size);
2623 netdev_dbg(adapter->netdev, "ipv6_ext_hdr = %d\n",
2624 buf->ipv6_extension_header);
2625 netdev_dbg(adapter->netdev, "tcp_pseudosum_req = %d\n",
2626 buf->tcp_pseudosum_req);
2627 netdev_dbg(adapter->netdev, "num_ipv6_ext_hd = %d\n",
2628 buf->num_ipv6_ext_headers);
2629 netdev_dbg(adapter->netdev, "off_ipv6_ext_hd = %d\n",
2630 buf->off_ipv6_ext_headers);
2631
2632 adapter->ip_offload_ctrl_tok =
2633 dma_map_single(dev, &adapter->ip_offload_ctrl,
2634 sizeof(adapter->ip_offload_ctrl), DMA_TO_DEVICE);
2635
2636 if (dma_mapping_error(dev, adapter->ip_offload_ctrl_tok)) {
2637 dev_err(dev, "Couldn't map ip offload control buffer\n");
2638 return;
2639 }
2640
2641 adapter->ip_offload_ctrl.version = cpu_to_be32(INITIAL_VERSION_IOB);
2642 adapter->ip_offload_ctrl.tcp_ipv4_chksum = buf->tcp_ipv4_chksum;
2643 adapter->ip_offload_ctrl.udp_ipv4_chksum = buf->udp_ipv4_chksum;
2644 adapter->ip_offload_ctrl.tcp_ipv6_chksum = buf->tcp_ipv6_chksum;
2645 adapter->ip_offload_ctrl.udp_ipv6_chksum = buf->udp_ipv6_chksum;
2646
2647 /* large_tx/rx disabled for now, additional features needed */
2648 adapter->ip_offload_ctrl.large_tx_ipv4 = 0;
2649 adapter->ip_offload_ctrl.large_tx_ipv6 = 0;
2650 adapter->ip_offload_ctrl.large_rx_ipv4 = 0;
2651 adapter->ip_offload_ctrl.large_rx_ipv6 = 0;
2652
2653 adapter->netdev->features = NETIF_F_GSO;
2654
2655 if (buf->tcp_ipv4_chksum || buf->udp_ipv4_chksum)
2656 adapter->netdev->features |= NETIF_F_IP_CSUM;
2657
2658 if (buf->tcp_ipv6_chksum || buf->udp_ipv6_chksum)
2659 adapter->netdev->features |= NETIF_F_IPV6_CSUM;
2660
Thomas Falcon9be02cd2016-04-01 17:20:35 -05002661 if ((adapter->netdev->features &
2662 (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)))
2663 adapter->netdev->features |= NETIF_F_RXCSUM;
2664
Thomas Falcon032c5e82015-12-21 11:26:06 -06002665 memset(&crq, 0, sizeof(crq));
2666 crq.control_ip_offload.first = IBMVNIC_CRQ_CMD;
2667 crq.control_ip_offload.cmd = CONTROL_IP_OFFLOAD;
2668 crq.control_ip_offload.len =
2669 cpu_to_be32(sizeof(adapter->ip_offload_ctrl));
2670 crq.control_ip_offload.ioba = cpu_to_be32(adapter->ip_offload_ctrl_tok);
2671 ibmvnic_send_crq(adapter, &crq);
2672}
2673
2674static void handle_error_info_rsp(union ibmvnic_crq *crq,
2675 struct ibmvnic_adapter *adapter)
2676{
2677 struct device *dev = &adapter->vdev->dev;
Wei Yongjun96183182016-06-27 20:48:53 +08002678 struct ibmvnic_error_buff *error_buff, *tmp;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002679 unsigned long flags;
2680 bool found = false;
2681 int i;
2682
2683 if (!crq->request_error_rsp.rc.code) {
2684 dev_info(dev, "Request Error Rsp returned with rc=%x\n",
2685 crq->request_error_rsp.rc.code);
2686 return;
2687 }
2688
2689 spin_lock_irqsave(&adapter->error_list_lock, flags);
Wei Yongjun96183182016-06-27 20:48:53 +08002690 list_for_each_entry_safe(error_buff, tmp, &adapter->errors, list)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002691 if (error_buff->error_id == crq->request_error_rsp.error_id) {
2692 found = true;
2693 list_del(&error_buff->list);
2694 break;
2695 }
2696 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
2697
2698 if (!found) {
2699 dev_err(dev, "Couldn't find error id %x\n",
Thomas Falcon75224c92017-02-15 10:33:33 -06002700 be32_to_cpu(crq->request_error_rsp.error_id));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002701 return;
2702 }
2703
2704 dev_err(dev, "Detailed info for error id %x:",
Thomas Falcon75224c92017-02-15 10:33:33 -06002705 be32_to_cpu(crq->request_error_rsp.error_id));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002706
2707 for (i = 0; i < error_buff->len; i++) {
2708 pr_cont("%02x", (int)error_buff->buff[i]);
2709 if (i % 8 == 7)
2710 pr_cont(" ");
2711 }
2712 pr_cont("\n");
2713
2714 dma_unmap_single(dev, error_buff->dma, error_buff->len,
2715 DMA_FROM_DEVICE);
2716 kfree(error_buff->buff);
2717 kfree(error_buff);
2718}
2719
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04002720static void request_error_information(struct ibmvnic_adapter *adapter,
2721 union ibmvnic_crq *err_crq)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002722{
Thomas Falcon032c5e82015-12-21 11:26:06 -06002723 struct device *dev = &adapter->vdev->dev;
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04002724 struct net_device *netdev = adapter->netdev;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002725 struct ibmvnic_error_buff *error_buff;
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04002726 unsigned long timeout = msecs_to_jiffies(30000);
2727 union ibmvnic_crq crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002728 unsigned long flags;
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04002729 int rc, detail_len;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002730
2731 error_buff = kmalloc(sizeof(*error_buff), GFP_ATOMIC);
2732 if (!error_buff)
2733 return;
2734
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04002735 detail_len = be32_to_cpu(err_crq->error_indication.detail_error_sz);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002736 error_buff->buff = kmalloc(detail_len, GFP_ATOMIC);
2737 if (!error_buff->buff) {
2738 kfree(error_buff);
2739 return;
2740 }
2741
2742 error_buff->dma = dma_map_single(dev, error_buff->buff, detail_len,
2743 DMA_FROM_DEVICE);
2744 if (dma_mapping_error(dev, error_buff->dma)) {
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04002745 netdev_err(netdev, "Couldn't map error buffer\n");
Thomas Falcon032c5e82015-12-21 11:26:06 -06002746 kfree(error_buff->buff);
2747 kfree(error_buff);
2748 return;
2749 }
2750
Thomas Falcon032c5e82015-12-21 11:26:06 -06002751 error_buff->len = detail_len;
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04002752 error_buff->error_id = err_crq->error_indication.error_id;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002753
2754 spin_lock_irqsave(&adapter->error_list_lock, flags);
2755 list_add_tail(&error_buff->list, &adapter->errors);
2756 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
2757
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04002758 memset(&crq, 0, sizeof(crq));
2759 crq.request_error_info.first = IBMVNIC_CRQ_CMD;
2760 crq.request_error_info.cmd = REQUEST_ERROR_INFO;
2761 crq.request_error_info.ioba = cpu_to_be32(error_buff->dma);
2762 crq.request_error_info.len = cpu_to_be32(detail_len);
2763 crq.request_error_info.error_id = err_crq->error_indication.error_id;
2764
2765 rc = ibmvnic_send_crq(adapter, &crq);
2766 if (rc) {
2767 netdev_err(netdev, "failed to request error information\n");
2768 goto err_info_fail;
2769 }
2770
2771 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
2772 netdev_err(netdev, "timeout waiting for error information\n");
2773 goto err_info_fail;
2774 }
2775
2776 return;
2777
2778err_info_fail:
2779 spin_lock_irqsave(&adapter->error_list_lock, flags);
2780 list_del(&error_buff->list);
2781 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
2782
2783 kfree(error_buff->buff);
2784 kfree(error_buff);
2785}
2786
2787static void handle_error_indication(union ibmvnic_crq *crq,
2788 struct ibmvnic_adapter *adapter)
2789{
2790 struct device *dev = &adapter->vdev->dev;
2791
2792 dev_err(dev, "Firmware reports %serror id %x, cause %d\n",
2793 crq->error_indication.flags
2794 & IBMVNIC_FATAL_ERROR ? "FATAL " : "",
2795 be32_to_cpu(crq->error_indication.error_id),
2796 be16_to_cpu(crq->error_indication.error_cause));
2797
2798 if (be32_to_cpu(crq->error_indication.error_id))
2799 request_error_information(adapter, crq);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002800
2801 if (crq->error_indication.flags & IBMVNIC_FATAL_ERROR)
2802 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
John Allen8cb31cf2017-05-26 10:30:37 -04002803 else
2804 ibmvnic_reset(adapter, VNIC_RESET_NON_FATAL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002805}
2806
2807static void handle_change_mac_rsp(union ibmvnic_crq *crq,
2808 struct ibmvnic_adapter *adapter)
2809{
2810 struct net_device *netdev = adapter->netdev;
2811 struct device *dev = &adapter->vdev->dev;
2812 long rc;
2813
2814 rc = crq->change_mac_addr_rsp.rc.code;
2815 if (rc) {
2816 dev_err(dev, "Error %ld in CHANGE_MAC_ADDR_RSP\n", rc);
2817 return;
2818 }
2819 memcpy(netdev->dev_addr, &crq->change_mac_addr_rsp.mac_addr[0],
2820 ETH_ALEN);
2821}
2822
2823static void handle_request_cap_rsp(union ibmvnic_crq *crq,
2824 struct ibmvnic_adapter *adapter)
2825{
2826 struct device *dev = &adapter->vdev->dev;
2827 u64 *req_value;
2828 char *name;
2829
Thomas Falcon901e0402017-02-15 12:17:59 -06002830 atomic_dec(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002831 switch (be16_to_cpu(crq->request_capability_rsp.capability)) {
2832 case REQ_TX_QUEUES:
2833 req_value = &adapter->req_tx_queues;
2834 name = "tx";
2835 break;
2836 case REQ_RX_QUEUES:
2837 req_value = &adapter->req_rx_queues;
2838 name = "rx";
2839 break;
2840 case REQ_RX_ADD_QUEUES:
2841 req_value = &adapter->req_rx_add_queues;
2842 name = "rx_add";
2843 break;
2844 case REQ_TX_ENTRIES_PER_SUBCRQ:
2845 req_value = &adapter->req_tx_entries_per_subcrq;
2846 name = "tx_entries_per_subcrq";
2847 break;
2848 case REQ_RX_ADD_ENTRIES_PER_SUBCRQ:
2849 req_value = &adapter->req_rx_add_entries_per_subcrq;
2850 name = "rx_add_entries_per_subcrq";
2851 break;
2852 case REQ_MTU:
2853 req_value = &adapter->req_mtu;
2854 name = "mtu";
2855 break;
2856 case PROMISC_REQUESTED:
2857 req_value = &adapter->promisc;
2858 name = "promisc";
2859 break;
2860 default:
2861 dev_err(dev, "Got invalid cap request rsp %d\n",
2862 crq->request_capability.capability);
2863 return;
2864 }
2865
2866 switch (crq->request_capability_rsp.rc.code) {
2867 case SUCCESS:
2868 break;
2869 case PARTIALSUCCESS:
2870 dev_info(dev, "req=%lld, rsp=%ld in %s queue, retrying.\n",
2871 *req_value,
Thomas Falcon28f4d162017-02-15 10:32:11 -06002872 (long int)be64_to_cpu(crq->request_capability_rsp.
Thomas Falcon032c5e82015-12-21 11:26:06 -06002873 number), name);
Nathan Fontenotb5108882017-03-30 02:49:18 -04002874 release_sub_crqs(adapter);
Thomas Falcon28f4d162017-02-15 10:32:11 -06002875 *req_value = be64_to_cpu(crq->request_capability_rsp.number);
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002876 ibmvnic_send_req_caps(adapter, 1);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002877 return;
2878 default:
2879 dev_err(dev, "Error %d in request cap rsp\n",
2880 crq->request_capability_rsp.rc.code);
2881 return;
2882 }
2883
2884 /* Done receiving requested capabilities, query IP offload support */
Thomas Falcon901e0402017-02-15 12:17:59 -06002885 if (atomic_read(&adapter->running_cap_crqs) == 0) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06002886 union ibmvnic_crq newcrq;
2887 int buf_sz = sizeof(struct ibmvnic_query_ip_offload_buffer);
2888 struct ibmvnic_query_ip_offload_buffer *ip_offload_buf =
2889 &adapter->ip_offload_buf;
2890
Thomas Falcon249168a2017-02-15 12:18:00 -06002891 adapter->wait_capability = false;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002892 adapter->ip_offload_tok = dma_map_single(dev, ip_offload_buf,
2893 buf_sz,
2894 DMA_FROM_DEVICE);
2895
2896 if (dma_mapping_error(dev, adapter->ip_offload_tok)) {
2897 if (!firmware_has_feature(FW_FEATURE_CMO))
2898 dev_err(dev, "Couldn't map offload buffer\n");
2899 return;
2900 }
2901
2902 memset(&newcrq, 0, sizeof(newcrq));
2903 newcrq.query_ip_offload.first = IBMVNIC_CRQ_CMD;
2904 newcrq.query_ip_offload.cmd = QUERY_IP_OFFLOAD;
2905 newcrq.query_ip_offload.len = cpu_to_be32(buf_sz);
2906 newcrq.query_ip_offload.ioba =
2907 cpu_to_be32(adapter->ip_offload_tok);
2908
2909 ibmvnic_send_crq(adapter, &newcrq);
2910 }
2911}
2912
2913static int handle_login_rsp(union ibmvnic_crq *login_rsp_crq,
2914 struct ibmvnic_adapter *adapter)
2915{
2916 struct device *dev = &adapter->vdev->dev;
2917 struct ibmvnic_login_rsp_buffer *login_rsp = adapter->login_rsp_buf;
2918 struct ibmvnic_login_buffer *login = adapter->login_buf;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002919 int i;
2920
2921 dma_unmap_single(dev, adapter->login_buf_token, adapter->login_buf_sz,
2922 DMA_BIDIRECTIONAL);
2923 dma_unmap_single(dev, adapter->login_rsp_buf_token,
2924 adapter->login_rsp_buf_sz, DMA_BIDIRECTIONAL);
2925
John Allen498cd8e2016-04-06 11:49:55 -05002926 /* If the number of queues requested can't be allocated by the
2927 * server, the login response will return with code 1. We will need
2928 * to resend the login buffer with fewer queues requested.
2929 */
2930 if (login_rsp_crq->generic.rc.code) {
2931 adapter->renegotiate = true;
2932 complete(&adapter->init_done);
2933 return 0;
2934 }
2935
Thomas Falcon032c5e82015-12-21 11:26:06 -06002936 netdev_dbg(adapter->netdev, "Login Response Buffer:\n");
2937 for (i = 0; i < (adapter->login_rsp_buf_sz - 1) / 8 + 1; i++) {
2938 netdev_dbg(adapter->netdev, "%016lx\n",
2939 ((unsigned long int *)(adapter->login_rsp_buf))[i]);
2940 }
2941
2942 /* Sanity checks */
2943 if (login->num_txcomp_subcrqs != login_rsp->num_txsubm_subcrqs ||
2944 (be32_to_cpu(login->num_rxcomp_subcrqs) *
2945 adapter->req_rx_add_queues !=
2946 be32_to_cpu(login_rsp->num_rxadd_subcrqs))) {
2947 dev_err(dev, "FATAL: Inconsistent login and login rsp\n");
2948 ibmvnic_remove(adapter->vdev);
2949 return -EIO;
2950 }
2951 complete(&adapter->init_done);
2952
Thomas Falcon032c5e82015-12-21 11:26:06 -06002953 return 0;
2954}
2955
2956static void handle_request_map_rsp(union ibmvnic_crq *crq,
2957 struct ibmvnic_adapter *adapter)
2958{
2959 struct device *dev = &adapter->vdev->dev;
2960 u8 map_id = crq->request_map_rsp.map_id;
2961 int tx_subcrqs;
2962 int rx_subcrqs;
2963 long rc;
2964 int i;
2965
2966 tx_subcrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
2967 rx_subcrqs = be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
2968
2969 rc = crq->request_map_rsp.rc.code;
2970 if (rc) {
2971 dev_err(dev, "Error %ld in REQUEST_MAP_RSP\n", rc);
2972 adapter->map_id--;
2973 /* need to find and zero tx/rx_pool map_id */
2974 for (i = 0; i < tx_subcrqs; i++) {
2975 if (adapter->tx_pool[i].long_term_buff.map_id == map_id)
2976 adapter->tx_pool[i].long_term_buff.map_id = 0;
2977 }
2978 for (i = 0; i < rx_subcrqs; i++) {
2979 if (adapter->rx_pool[i].long_term_buff.map_id == map_id)
2980 adapter->rx_pool[i].long_term_buff.map_id = 0;
2981 }
2982 }
2983 complete(&adapter->fw_done);
2984}
2985
2986static void handle_request_unmap_rsp(union ibmvnic_crq *crq,
2987 struct ibmvnic_adapter *adapter)
2988{
2989 struct device *dev = &adapter->vdev->dev;
2990 long rc;
2991
2992 rc = crq->request_unmap_rsp.rc.code;
2993 if (rc)
2994 dev_err(dev, "Error %ld in REQUEST_UNMAP_RSP\n", rc);
2995}
2996
2997static void handle_query_map_rsp(union ibmvnic_crq *crq,
2998 struct ibmvnic_adapter *adapter)
2999{
3000 struct net_device *netdev = adapter->netdev;
3001 struct device *dev = &adapter->vdev->dev;
3002 long rc;
3003
3004 rc = crq->query_map_rsp.rc.code;
3005 if (rc) {
3006 dev_err(dev, "Error %ld in QUERY_MAP_RSP\n", rc);
3007 return;
3008 }
3009 netdev_dbg(netdev, "page_size = %d\ntot_pages = %d\nfree_pages = %d\n",
3010 crq->query_map_rsp.page_size, crq->query_map_rsp.tot_pages,
3011 crq->query_map_rsp.free_pages);
3012}
3013
3014static void handle_query_cap_rsp(union ibmvnic_crq *crq,
3015 struct ibmvnic_adapter *adapter)
3016{
3017 struct net_device *netdev = adapter->netdev;
3018 struct device *dev = &adapter->vdev->dev;
3019 long rc;
3020
Thomas Falcon901e0402017-02-15 12:17:59 -06003021 atomic_dec(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003022 netdev_dbg(netdev, "Outstanding queries: %d\n",
Thomas Falcon901e0402017-02-15 12:17:59 -06003023 atomic_read(&adapter->running_cap_crqs));
Thomas Falcon032c5e82015-12-21 11:26:06 -06003024 rc = crq->query_capability.rc.code;
3025 if (rc) {
3026 dev_err(dev, "Error %ld in QUERY_CAP_RSP\n", rc);
3027 goto out;
3028 }
3029
3030 switch (be16_to_cpu(crq->query_capability.capability)) {
3031 case MIN_TX_QUEUES:
3032 adapter->min_tx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003033 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003034 netdev_dbg(netdev, "min_tx_queues = %lld\n",
3035 adapter->min_tx_queues);
3036 break;
3037 case MIN_RX_QUEUES:
3038 adapter->min_rx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003039 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003040 netdev_dbg(netdev, "min_rx_queues = %lld\n",
3041 adapter->min_rx_queues);
3042 break;
3043 case MIN_RX_ADD_QUEUES:
3044 adapter->min_rx_add_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003045 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003046 netdev_dbg(netdev, "min_rx_add_queues = %lld\n",
3047 adapter->min_rx_add_queues);
3048 break;
3049 case MAX_TX_QUEUES:
3050 adapter->max_tx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003051 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003052 netdev_dbg(netdev, "max_tx_queues = %lld\n",
3053 adapter->max_tx_queues);
3054 break;
3055 case MAX_RX_QUEUES:
3056 adapter->max_rx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003057 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003058 netdev_dbg(netdev, "max_rx_queues = %lld\n",
3059 adapter->max_rx_queues);
3060 break;
3061 case MAX_RX_ADD_QUEUES:
3062 adapter->max_rx_add_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003063 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003064 netdev_dbg(netdev, "max_rx_add_queues = %lld\n",
3065 adapter->max_rx_add_queues);
3066 break;
3067 case MIN_TX_ENTRIES_PER_SUBCRQ:
3068 adapter->min_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06003069 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003070 netdev_dbg(netdev, "min_tx_entries_per_subcrq = %lld\n",
3071 adapter->min_tx_entries_per_subcrq);
3072 break;
3073 case MIN_RX_ADD_ENTRIES_PER_SUBCRQ:
3074 adapter->min_rx_add_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06003075 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003076 netdev_dbg(netdev, "min_rx_add_entrs_per_subcrq = %lld\n",
3077 adapter->min_rx_add_entries_per_subcrq);
3078 break;
3079 case MAX_TX_ENTRIES_PER_SUBCRQ:
3080 adapter->max_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06003081 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003082 netdev_dbg(netdev, "max_tx_entries_per_subcrq = %lld\n",
3083 adapter->max_tx_entries_per_subcrq);
3084 break;
3085 case MAX_RX_ADD_ENTRIES_PER_SUBCRQ:
3086 adapter->max_rx_add_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06003087 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003088 netdev_dbg(netdev, "max_rx_add_entrs_per_subcrq = %lld\n",
3089 adapter->max_rx_add_entries_per_subcrq);
3090 break;
3091 case TCP_IP_OFFLOAD:
3092 adapter->tcp_ip_offload =
Thomas Falconde89e852016-03-01 10:20:09 -06003093 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003094 netdev_dbg(netdev, "tcp_ip_offload = %lld\n",
3095 adapter->tcp_ip_offload);
3096 break;
3097 case PROMISC_SUPPORTED:
3098 adapter->promisc_supported =
Thomas Falconde89e852016-03-01 10:20:09 -06003099 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003100 netdev_dbg(netdev, "promisc_supported = %lld\n",
3101 adapter->promisc_supported);
3102 break;
3103 case MIN_MTU:
Thomas Falconde89e852016-03-01 10:20:09 -06003104 adapter->min_mtu = be64_to_cpu(crq->query_capability.number);
Thomas Falconf39f0d12017-02-14 10:22:59 -06003105 netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003106 netdev_dbg(netdev, "min_mtu = %lld\n", adapter->min_mtu);
3107 break;
3108 case MAX_MTU:
Thomas Falconde89e852016-03-01 10:20:09 -06003109 adapter->max_mtu = be64_to_cpu(crq->query_capability.number);
Thomas Falconf39f0d12017-02-14 10:22:59 -06003110 netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003111 netdev_dbg(netdev, "max_mtu = %lld\n", adapter->max_mtu);
3112 break;
3113 case MAX_MULTICAST_FILTERS:
3114 adapter->max_multicast_filters =
Thomas Falconde89e852016-03-01 10:20:09 -06003115 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003116 netdev_dbg(netdev, "max_multicast_filters = %lld\n",
3117 adapter->max_multicast_filters);
3118 break;
3119 case VLAN_HEADER_INSERTION:
3120 adapter->vlan_header_insertion =
Thomas Falconde89e852016-03-01 10:20:09 -06003121 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003122 if (adapter->vlan_header_insertion)
3123 netdev->features |= NETIF_F_HW_VLAN_STAG_TX;
3124 netdev_dbg(netdev, "vlan_header_insertion = %lld\n",
3125 adapter->vlan_header_insertion);
3126 break;
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04003127 case RX_VLAN_HEADER_INSERTION:
3128 adapter->rx_vlan_header_insertion =
3129 be64_to_cpu(crq->query_capability.number);
3130 netdev_dbg(netdev, "rx_vlan_header_insertion = %lld\n",
3131 adapter->rx_vlan_header_insertion);
3132 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003133 case MAX_TX_SG_ENTRIES:
3134 adapter->max_tx_sg_entries =
Thomas Falconde89e852016-03-01 10:20:09 -06003135 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003136 netdev_dbg(netdev, "max_tx_sg_entries = %lld\n",
3137 adapter->max_tx_sg_entries);
3138 break;
3139 case RX_SG_SUPPORTED:
3140 adapter->rx_sg_supported =
Thomas Falconde89e852016-03-01 10:20:09 -06003141 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003142 netdev_dbg(netdev, "rx_sg_supported = %lld\n",
3143 adapter->rx_sg_supported);
3144 break;
3145 case OPT_TX_COMP_SUB_QUEUES:
3146 adapter->opt_tx_comp_sub_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003147 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003148 netdev_dbg(netdev, "opt_tx_comp_sub_queues = %lld\n",
3149 adapter->opt_tx_comp_sub_queues);
3150 break;
3151 case OPT_RX_COMP_QUEUES:
3152 adapter->opt_rx_comp_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003153 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003154 netdev_dbg(netdev, "opt_rx_comp_queues = %lld\n",
3155 adapter->opt_rx_comp_queues);
3156 break;
3157 case OPT_RX_BUFADD_Q_PER_RX_COMP_Q:
3158 adapter->opt_rx_bufadd_q_per_rx_comp_q =
Thomas Falconde89e852016-03-01 10:20:09 -06003159 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003160 netdev_dbg(netdev, "opt_rx_bufadd_q_per_rx_comp_q = %lld\n",
3161 adapter->opt_rx_bufadd_q_per_rx_comp_q);
3162 break;
3163 case OPT_TX_ENTRIES_PER_SUBCRQ:
3164 adapter->opt_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06003165 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003166 netdev_dbg(netdev, "opt_tx_entries_per_subcrq = %lld\n",
3167 adapter->opt_tx_entries_per_subcrq);
3168 break;
3169 case OPT_RXBA_ENTRIES_PER_SUBCRQ:
3170 adapter->opt_rxba_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06003171 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003172 netdev_dbg(netdev, "opt_rxba_entries_per_subcrq = %lld\n",
3173 adapter->opt_rxba_entries_per_subcrq);
3174 break;
3175 case TX_RX_DESC_REQ:
3176 adapter->tx_rx_desc_req = crq->query_capability.number;
3177 netdev_dbg(netdev, "tx_rx_desc_req = %llx\n",
3178 adapter->tx_rx_desc_req);
3179 break;
3180
3181 default:
3182 netdev_err(netdev, "Got invalid cap rsp %d\n",
3183 crq->query_capability.capability);
3184 }
3185
3186out:
Thomas Falcon249168a2017-02-15 12:18:00 -06003187 if (atomic_read(&adapter->running_cap_crqs) == 0) {
3188 adapter->wait_capability = false;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003189 ibmvnic_send_req_caps(adapter, 0);
Thomas Falcon249168a2017-02-15 12:18:00 -06003190 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06003191}
3192
Thomas Falcon032c5e82015-12-21 11:26:06 -06003193static void ibmvnic_handle_crq(union ibmvnic_crq *crq,
3194 struct ibmvnic_adapter *adapter)
3195{
3196 struct ibmvnic_generic_crq *gen_crq = &crq->generic;
3197 struct net_device *netdev = adapter->netdev;
3198 struct device *dev = &adapter->vdev->dev;
Murilo Fossa Vicentini993a82b2017-04-19 13:44:35 -04003199 u64 *u64_crq = (u64 *)crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003200 long rc;
3201
3202 netdev_dbg(netdev, "Handling CRQ: %016lx %016lx\n",
Murilo Fossa Vicentini993a82b2017-04-19 13:44:35 -04003203 (unsigned long int)cpu_to_be64(u64_crq[0]),
3204 (unsigned long int)cpu_to_be64(u64_crq[1]));
Thomas Falcon032c5e82015-12-21 11:26:06 -06003205 switch (gen_crq->first) {
3206 case IBMVNIC_CRQ_INIT_RSP:
3207 switch (gen_crq->cmd) {
3208 case IBMVNIC_CRQ_INIT:
3209 dev_info(dev, "Partner initialized\n");
John Allen017892c12017-05-26 10:30:19 -04003210 adapter->from_passive_init = true;
3211 complete(&adapter->init_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003212 break;
3213 case IBMVNIC_CRQ_INIT_COMPLETE:
3214 dev_info(dev, "Partner initialization complete\n");
3215 send_version_xchg(adapter);
3216 break;
3217 default:
3218 dev_err(dev, "Unknown crq cmd: %d\n", gen_crq->cmd);
3219 }
3220 return;
3221 case IBMVNIC_CRQ_XPORT_EVENT:
Nathan Fontenoted651a12017-05-03 14:04:38 -04003222 netif_carrier_off(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003223 if (gen_crq->cmd == IBMVNIC_PARTITION_MIGRATED) {
Nathan Fontenoted651a12017-05-03 14:04:38 -04003224 dev_info(dev, "Migrated, re-enabling adapter\n");
3225 ibmvnic_reset(adapter, VNIC_RESET_MOBILITY);
Thomas Falcondfad09a2016-08-18 11:37:51 -05003226 } else if (gen_crq->cmd == IBMVNIC_DEVICE_FAILOVER) {
3227 dev_info(dev, "Backing device failover detected\n");
Nathan Fontenoted651a12017-05-03 14:04:38 -04003228 ibmvnic_reset(adapter, VNIC_RESET_FAILOVER);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003229 } else {
3230 /* The adapter lost the connection */
3231 dev_err(dev, "Virtual Adapter failed (rc=%d)\n",
3232 gen_crq->cmd);
Nathan Fontenoted651a12017-05-03 14:04:38 -04003233 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003234 }
3235 return;
3236 case IBMVNIC_CRQ_CMD_RSP:
3237 break;
3238 default:
3239 dev_err(dev, "Got an invalid msg type 0x%02x\n",
3240 gen_crq->first);
3241 return;
3242 }
3243
3244 switch (gen_crq->cmd) {
3245 case VERSION_EXCHANGE_RSP:
3246 rc = crq->version_exchange_rsp.rc.code;
3247 if (rc) {
3248 dev_err(dev, "Error %ld in VERSION_EXCHG_RSP\n", rc);
3249 break;
3250 }
3251 dev_info(dev, "Partner protocol version is %d\n",
3252 crq->version_exchange_rsp.version);
3253 if (be16_to_cpu(crq->version_exchange_rsp.version) <
3254 ibmvnic_version)
3255 ibmvnic_version =
3256 be16_to_cpu(crq->version_exchange_rsp.version);
3257 send_cap_queries(adapter);
3258 break;
3259 case QUERY_CAPABILITY_RSP:
3260 handle_query_cap_rsp(crq, adapter);
3261 break;
3262 case QUERY_MAP_RSP:
3263 handle_query_map_rsp(crq, adapter);
3264 break;
3265 case REQUEST_MAP_RSP:
3266 handle_request_map_rsp(crq, adapter);
3267 break;
3268 case REQUEST_UNMAP_RSP:
3269 handle_request_unmap_rsp(crq, adapter);
3270 break;
3271 case REQUEST_CAPABILITY_RSP:
3272 handle_request_cap_rsp(crq, adapter);
3273 break;
3274 case LOGIN_RSP:
3275 netdev_dbg(netdev, "Got Login Response\n");
3276 handle_login_rsp(crq, adapter);
3277 break;
3278 case LOGICAL_LINK_STATE_RSP:
Nathan Fontenot53da09e2017-04-21 15:39:04 -04003279 netdev_dbg(netdev,
3280 "Got Logical Link State Response, state: %d rc: %d\n",
3281 crq->logical_link_state_rsp.link_state,
3282 crq->logical_link_state_rsp.rc.code);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003283 adapter->logical_link_state =
3284 crq->logical_link_state_rsp.link_state;
Nathan Fontenot53da09e2017-04-21 15:39:04 -04003285 adapter->init_done_rc = crq->logical_link_state_rsp.rc.code;
3286 complete(&adapter->init_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003287 break;
3288 case LINK_STATE_INDICATION:
3289 netdev_dbg(netdev, "Got Logical Link State Indication\n");
3290 adapter->phys_link_state =
3291 crq->link_state_indication.phys_link_state;
3292 adapter->logical_link_state =
3293 crq->link_state_indication.logical_link_state;
3294 break;
3295 case CHANGE_MAC_ADDR_RSP:
3296 netdev_dbg(netdev, "Got MAC address change Response\n");
3297 handle_change_mac_rsp(crq, adapter);
3298 break;
3299 case ERROR_INDICATION:
3300 netdev_dbg(netdev, "Got Error Indication\n");
3301 handle_error_indication(crq, adapter);
3302 break;
3303 case REQUEST_ERROR_RSP:
3304 netdev_dbg(netdev, "Got Error Detail Response\n");
3305 handle_error_info_rsp(crq, adapter);
3306 break;
3307 case REQUEST_STATISTICS_RSP:
3308 netdev_dbg(netdev, "Got Statistics Response\n");
3309 complete(&adapter->stats_done);
3310 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003311 case QUERY_IP_OFFLOAD_RSP:
3312 netdev_dbg(netdev, "Got Query IP offload Response\n");
3313 handle_query_ip_offload_rsp(adapter);
3314 break;
3315 case MULTICAST_CTRL_RSP:
3316 netdev_dbg(netdev, "Got multicast control Response\n");
3317 break;
3318 case CONTROL_IP_OFFLOAD_RSP:
3319 netdev_dbg(netdev, "Got Control IP offload Response\n");
3320 dma_unmap_single(dev, adapter->ip_offload_ctrl_tok,
3321 sizeof(adapter->ip_offload_ctrl),
3322 DMA_TO_DEVICE);
John Allenbd0b6722017-03-17 17:13:40 -05003323 complete(&adapter->init_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003324 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003325 case COLLECT_FW_TRACE_RSP:
3326 netdev_dbg(netdev, "Got Collect firmware trace Response\n");
3327 complete(&adapter->fw_done);
3328 break;
3329 default:
3330 netdev_err(netdev, "Got an invalid cmd type 0x%02x\n",
3331 gen_crq->cmd);
3332 }
3333}
3334
3335static irqreturn_t ibmvnic_interrupt(int irq, void *instance)
3336{
3337 struct ibmvnic_adapter *adapter = instance;
Thomas Falcon6c267b32017-02-15 12:17:58 -06003338
Thomas Falcon6c267b32017-02-15 12:17:58 -06003339 tasklet_schedule(&adapter->tasklet);
Thomas Falcon6c267b32017-02-15 12:17:58 -06003340 return IRQ_HANDLED;
3341}
3342
3343static void ibmvnic_tasklet(void *data)
3344{
3345 struct ibmvnic_adapter *adapter = data;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003346 struct ibmvnic_crq_queue *queue = &adapter->crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003347 union ibmvnic_crq *crq;
3348 unsigned long flags;
3349 bool done = false;
3350
3351 spin_lock_irqsave(&queue->lock, flags);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003352 while (!done) {
3353 /* Pull all the valid messages off the CRQ */
3354 while ((crq = ibmvnic_next_crq(adapter)) != NULL) {
3355 ibmvnic_handle_crq(crq, adapter);
3356 crq->generic.first = 0;
3357 }
Brian Kinged7ecbf2017-04-19 13:44:53 -04003358
3359 /* remain in tasklet until all
3360 * capabilities responses are received
3361 */
3362 if (!adapter->wait_capability)
3363 done = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003364 }
Thomas Falcon249168a2017-02-15 12:18:00 -06003365 /* if capabilities CRQ's were sent in this tasklet, the following
3366 * tasklet must wait until all responses are received
3367 */
3368 if (atomic_read(&adapter->running_cap_crqs) != 0)
3369 adapter->wait_capability = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003370 spin_unlock_irqrestore(&queue->lock, flags);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003371}
3372
3373static int ibmvnic_reenable_crq_queue(struct ibmvnic_adapter *adapter)
3374{
3375 struct vio_dev *vdev = adapter->vdev;
3376 int rc;
3377
3378 do {
3379 rc = plpar_hcall_norets(H_ENABLE_CRQ, vdev->unit_address);
3380 } while (rc == H_IN_PROGRESS || rc == H_BUSY || H_IS_LONG_BUSY(rc));
3381
3382 if (rc)
3383 dev_err(&vdev->dev, "Error enabling adapter (rc=%d)\n", rc);
3384
3385 return rc;
3386}
3387
3388static int ibmvnic_reset_crq(struct ibmvnic_adapter *adapter)
3389{
3390 struct ibmvnic_crq_queue *crq = &adapter->crq;
3391 struct device *dev = &adapter->vdev->dev;
3392 struct vio_dev *vdev = adapter->vdev;
3393 int rc;
3394
3395 /* Close the CRQ */
3396 do {
3397 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
3398 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
3399
3400 /* Clean out the queue */
3401 memset(crq->msgs, 0, PAGE_SIZE);
3402 crq->cur = 0;
3403
3404 /* And re-open it again */
3405 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
3406 crq->msg_token, PAGE_SIZE);
3407
3408 if (rc == H_CLOSED)
3409 /* Adapter is good, but other end is not ready */
3410 dev_warn(dev, "Partner adapter not ready\n");
3411 else if (rc != 0)
3412 dev_warn(dev, "Couldn't register crq (rc=%d)\n", rc);
3413
3414 return rc;
3415}
3416
Nathan Fontenotf9928872017-03-30 02:48:54 -04003417static void release_crq_queue(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003418{
3419 struct ibmvnic_crq_queue *crq = &adapter->crq;
3420 struct vio_dev *vdev = adapter->vdev;
3421 long rc;
3422
Nathan Fontenotf9928872017-03-30 02:48:54 -04003423 if (!crq->msgs)
3424 return;
3425
Thomas Falcon032c5e82015-12-21 11:26:06 -06003426 netdev_dbg(adapter->netdev, "Releasing CRQ\n");
3427 free_irq(vdev->irq, adapter);
Thomas Falcon6c267b32017-02-15 12:17:58 -06003428 tasklet_kill(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003429 do {
3430 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
3431 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
3432
3433 dma_unmap_single(&vdev->dev, crq->msg_token, PAGE_SIZE,
3434 DMA_BIDIRECTIONAL);
3435 free_page((unsigned long)crq->msgs);
Nathan Fontenotf9928872017-03-30 02:48:54 -04003436 crq->msgs = NULL;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003437}
3438
Nathan Fontenotf9928872017-03-30 02:48:54 -04003439static int init_crq_queue(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003440{
3441 struct ibmvnic_crq_queue *crq = &adapter->crq;
3442 struct device *dev = &adapter->vdev->dev;
3443 struct vio_dev *vdev = adapter->vdev;
3444 int rc, retrc = -ENOMEM;
3445
Nathan Fontenotf9928872017-03-30 02:48:54 -04003446 if (crq->msgs)
3447 return 0;
3448
Thomas Falcon032c5e82015-12-21 11:26:06 -06003449 crq->msgs = (union ibmvnic_crq *)get_zeroed_page(GFP_KERNEL);
3450 /* Should we allocate more than one page? */
3451
3452 if (!crq->msgs)
3453 return -ENOMEM;
3454
3455 crq->size = PAGE_SIZE / sizeof(*crq->msgs);
3456 crq->msg_token = dma_map_single(dev, crq->msgs, PAGE_SIZE,
3457 DMA_BIDIRECTIONAL);
3458 if (dma_mapping_error(dev, crq->msg_token))
3459 goto map_failed;
3460
3461 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
3462 crq->msg_token, PAGE_SIZE);
3463
3464 if (rc == H_RESOURCE)
3465 /* maybe kexecing and resource is busy. try a reset */
3466 rc = ibmvnic_reset_crq(adapter);
3467 retrc = rc;
3468
3469 if (rc == H_CLOSED) {
3470 dev_warn(dev, "Partner adapter not ready\n");
3471 } else if (rc) {
3472 dev_warn(dev, "Error %d opening adapter\n", rc);
3473 goto reg_crq_failed;
3474 }
3475
3476 retrc = 0;
3477
Thomas Falcon6c267b32017-02-15 12:17:58 -06003478 tasklet_init(&adapter->tasklet, (void *)ibmvnic_tasklet,
3479 (unsigned long)adapter);
3480
Thomas Falcon032c5e82015-12-21 11:26:06 -06003481 netdev_dbg(adapter->netdev, "registering irq 0x%x\n", vdev->irq);
3482 rc = request_irq(vdev->irq, ibmvnic_interrupt, 0, IBMVNIC_NAME,
3483 adapter);
3484 if (rc) {
3485 dev_err(dev, "Couldn't register irq 0x%x. rc=%d\n",
3486 vdev->irq, rc);
3487 goto req_irq_failed;
3488 }
3489
3490 rc = vio_enable_interrupts(vdev);
3491 if (rc) {
3492 dev_err(dev, "Error %d enabling interrupts\n", rc);
3493 goto req_irq_failed;
3494 }
3495
3496 crq->cur = 0;
3497 spin_lock_init(&crq->lock);
3498
3499 return retrc;
3500
3501req_irq_failed:
Thomas Falcon6c267b32017-02-15 12:17:58 -06003502 tasklet_kill(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003503 do {
3504 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
3505 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
3506reg_crq_failed:
3507 dma_unmap_single(dev, crq->msg_token, PAGE_SIZE, DMA_BIDIRECTIONAL);
3508map_failed:
3509 free_page((unsigned long)crq->msgs);
Nathan Fontenotf9928872017-03-30 02:48:54 -04003510 crq->msgs = NULL;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003511 return retrc;
3512}
3513
John Allenf6ef6402017-03-17 17:13:42 -05003514static int ibmvnic_init(struct ibmvnic_adapter *adapter)
3515{
3516 struct device *dev = &adapter->vdev->dev;
3517 unsigned long timeout = msecs_to_jiffies(30000);
John Allenf6ef6402017-03-17 17:13:42 -05003518 int rc;
3519
Nathan Fontenotf9928872017-03-30 02:48:54 -04003520 rc = init_crq_queue(adapter);
John Allenf6ef6402017-03-17 17:13:42 -05003521 if (rc) {
3522 dev_err(dev, "Couldn't initialize crq. rc=%d\n", rc);
3523 return rc;
3524 }
3525
John Allen017892c12017-05-26 10:30:19 -04003526 adapter->from_passive_init = false;
3527
John Allenf6ef6402017-03-17 17:13:42 -05003528 init_completion(&adapter->init_done);
3529 ibmvnic_send_crq_init(adapter);
3530 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
3531 dev_err(dev, "Initialization sequence timed out\n");
John Allen017892c12017-05-26 10:30:19 -04003532 return -1;
3533 }
3534
3535 if (adapter->from_passive_init) {
3536 adapter->state = VNIC_OPEN;
3537 adapter->from_passive_init = false;
John Allenf6ef6402017-03-17 17:13:42 -05003538 return -1;
3539 }
3540
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04003541 rc = init_sub_crqs(adapter);
3542 if (rc) {
3543 dev_err(dev, "Initialization of sub crqs failed\n");
3544 release_crq_queue(adapter);
3545 }
3546
3547 return rc;
John Allenf6ef6402017-03-17 17:13:42 -05003548}
3549
Thomas Falcon032c5e82015-12-21 11:26:06 -06003550static int ibmvnic_probe(struct vio_dev *dev, const struct vio_device_id *id)
3551{
3552 struct ibmvnic_adapter *adapter;
3553 struct net_device *netdev;
3554 unsigned char *mac_addr_p;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003555 int rc;
3556
3557 dev_dbg(&dev->dev, "entering ibmvnic_probe for UA 0x%x\n",
3558 dev->unit_address);
3559
3560 mac_addr_p = (unsigned char *)vio_get_attribute(dev,
3561 VETH_MAC_ADDR, NULL);
3562 if (!mac_addr_p) {
3563 dev_err(&dev->dev,
3564 "(%s:%3.3d) ERROR: Can't find MAC_ADDR attribute\n",
3565 __FILE__, __LINE__);
3566 return 0;
3567 }
3568
3569 netdev = alloc_etherdev_mq(sizeof(struct ibmvnic_adapter),
3570 IBMVNIC_MAX_TX_QUEUES);
3571 if (!netdev)
3572 return -ENOMEM;
3573
3574 adapter = netdev_priv(netdev);
Nathan Fontenot90c80142017-05-03 14:04:32 -04003575 adapter->state = VNIC_PROBING;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003576 dev_set_drvdata(&dev->dev, netdev);
3577 adapter->vdev = dev;
3578 adapter->netdev = netdev;
3579
3580 ether_addr_copy(adapter->mac_addr, mac_addr_p);
3581 ether_addr_copy(netdev->dev_addr, adapter->mac_addr);
3582 netdev->irq = dev->irq;
3583 netdev->netdev_ops = &ibmvnic_netdev_ops;
3584 netdev->ethtool_ops = &ibmvnic_ethtool_ops;
3585 SET_NETDEV_DEV(netdev, &dev->dev);
3586
3587 spin_lock_init(&adapter->stats_lock);
3588
Thomas Falcon032c5e82015-12-21 11:26:06 -06003589 INIT_LIST_HEAD(&adapter->errors);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003590 spin_lock_init(&adapter->error_list_lock);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003591
Nathan Fontenoted651a12017-05-03 14:04:38 -04003592 INIT_WORK(&adapter->ibmvnic_reset, __ibmvnic_reset);
3593 INIT_LIST_HEAD(&adapter->rwi_list);
3594 mutex_init(&adapter->reset_lock);
3595 mutex_init(&adapter->rwi_lock);
3596 adapter->resetting = false;
3597
John Allenf6ef6402017-03-17 17:13:42 -05003598 rc = ibmvnic_init(adapter);
3599 if (rc) {
3600 free_netdev(netdev);
3601 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003602 }
3603
Thomas Falconf39f0d12017-02-14 10:22:59 -06003604 netdev->mtu = adapter->req_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003605
3606 rc = register_netdev(netdev);
3607 if (rc) {
3608 dev_err(&dev->dev, "failed to register netdev rc=%d\n", rc);
John Allenf6ef6402017-03-17 17:13:42 -05003609 free_netdev(netdev);
3610 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003611 }
3612 dev_info(&dev->dev, "ibmvnic registered\n");
3613
Nathan Fontenot90c80142017-05-03 14:04:32 -04003614 adapter->state = VNIC_PROBED;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003615 return 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003616}
3617
3618static int ibmvnic_remove(struct vio_dev *dev)
3619{
3620 struct net_device *netdev = dev_get_drvdata(&dev->dev);
Nathan Fontenot37489052017-04-19 13:45:04 -04003621 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003622
Nathan Fontenot90c80142017-05-03 14:04:32 -04003623 adapter->state = VNIC_REMOVING;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003624 unregister_netdev(netdev);
Nathan Fontenoted651a12017-05-03 14:04:38 -04003625 mutex_lock(&adapter->reset_lock);
Nathan Fontenot37489052017-04-19 13:45:04 -04003626
3627 release_resources(adapter);
3628 release_sub_crqs(adapter);
3629 release_crq_queue(adapter);
3630
Nathan Fontenot90c80142017-05-03 14:04:32 -04003631 adapter->state = VNIC_REMOVED;
3632
Nathan Fontenoted651a12017-05-03 14:04:38 -04003633 mutex_unlock(&adapter->reset_lock);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003634 free_netdev(netdev);
3635 dev_set_drvdata(&dev->dev, NULL);
3636
3637 return 0;
3638}
3639
3640static unsigned long ibmvnic_get_desired_dma(struct vio_dev *vdev)
3641{
3642 struct net_device *netdev = dev_get_drvdata(&vdev->dev);
3643 struct ibmvnic_adapter *adapter;
3644 struct iommu_table *tbl;
3645 unsigned long ret = 0;
3646 int i;
3647
3648 tbl = get_iommu_table_base(&vdev->dev);
3649
3650 /* netdev inits at probe time along with the structures we need below*/
3651 if (!netdev)
3652 return IOMMU_PAGE_ALIGN(IBMVNIC_IO_ENTITLEMENT_DEFAULT, tbl);
3653
3654 adapter = netdev_priv(netdev);
3655
3656 ret += PAGE_SIZE; /* the crq message queue */
Thomas Falcon032c5e82015-12-21 11:26:06 -06003657 ret += IOMMU_PAGE_ALIGN(sizeof(struct ibmvnic_statistics), tbl);
3658
3659 for (i = 0; i < adapter->req_tx_queues + adapter->req_rx_queues; i++)
3660 ret += 4 * PAGE_SIZE; /* the scrq message queue */
3661
3662 for (i = 0; i < be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
3663 i++)
3664 ret += adapter->rx_pool[i].size *
3665 IOMMU_PAGE_ALIGN(adapter->rx_pool[i].buff_size, tbl);
3666
3667 return ret;
3668}
3669
3670static int ibmvnic_resume(struct device *dev)
3671{
3672 struct net_device *netdev = dev_get_drvdata(dev);
3673 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
3674 int i;
3675
3676 /* kick the interrupt handlers just in case we lost an interrupt */
3677 for (i = 0; i < adapter->req_rx_queues; i++)
3678 ibmvnic_interrupt_rx(adapter->rx_scrq[i]->irq,
3679 adapter->rx_scrq[i]);
3680
3681 return 0;
3682}
3683
3684static struct vio_device_id ibmvnic_device_table[] = {
3685 {"network", "IBM,vnic"},
3686 {"", "" }
3687};
3688MODULE_DEVICE_TABLE(vio, ibmvnic_device_table);
3689
3690static const struct dev_pm_ops ibmvnic_pm_ops = {
3691 .resume = ibmvnic_resume
3692};
3693
3694static struct vio_driver ibmvnic_driver = {
3695 .id_table = ibmvnic_device_table,
3696 .probe = ibmvnic_probe,
3697 .remove = ibmvnic_remove,
3698 .get_desired_dma = ibmvnic_get_desired_dma,
3699 .name = ibmvnic_driver_name,
3700 .pm = &ibmvnic_pm_ops,
3701};
3702
3703/* module functions */
3704static int __init ibmvnic_module_init(void)
3705{
3706 pr_info("%s: %s %s\n", ibmvnic_driver_name, ibmvnic_driver_string,
3707 IBMVNIC_DRIVER_VERSION);
3708
3709 return vio_register_driver(&ibmvnic_driver);
3710}
3711
3712static void __exit ibmvnic_module_exit(void)
3713{
3714 vio_unregister_driver(&ibmvnic_driver);
3715}
3716
3717module_init(ibmvnic_module_init);
3718module_exit(ibmvnic_module_exit);