blob: b45ade6123a44202acd273aa23b820e760992e50 [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
Thomas Falcon78b07ac2017-06-01 15:32:34 -050084MODULE_AUTHOR("Santiago Leon");
Thomas Falcon032c5e82015-12-21 11:26:06 -060085MODULE_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);
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500186
187 if (adapter->fw_done_rc) {
188 dev_err(dev, "Couldn't map long term buffer,rc = %d\n",
189 adapter->fw_done_rc);
190 return -1;
191 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600192 return 0;
193}
194
195static void free_long_term_buff(struct ibmvnic_adapter *adapter,
196 struct ibmvnic_long_term_buff *ltb)
197{
198 struct device *dev = &adapter->vdev->dev;
199
Nathan Fontenotc657e322017-03-30 02:49:06 -0400200 if (!ltb->buff)
201 return;
202
Nathan Fontenoted651a12017-05-03 14:04:38 -0400203 if (adapter->reset_reason != VNIC_RESET_FAILOVER &&
204 adapter->reset_reason != VNIC_RESET_MOBILITY)
Thomas Falcondfad09a2016-08-18 11:37:51 -0500205 send_request_unmap(adapter, ltb->map_id);
Brian King59af56c2017-04-19 13:44:41 -0400206 dma_free_coherent(dev, ltb->size, ltb->buff, ltb->addr);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600207}
208
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500209static int reset_long_term_buff(struct ibmvnic_adapter *adapter,
210 struct ibmvnic_long_term_buff *ltb)
211{
212 memset(ltb->buff, 0, ltb->size);
213
214 init_completion(&adapter->fw_done);
215 send_request_map(adapter, ltb->addr, ltb->size, ltb->map_id);
216 wait_for_completion(&adapter->fw_done);
217
218 if (adapter->fw_done_rc) {
219 dev_info(&adapter->vdev->dev,
220 "Reset failed, attempting to free and reallocate buffer\n");
221 free_long_term_buff(adapter, ltb);
222 return alloc_long_term_buff(adapter, ltb, ltb->size);
223 }
224 return 0;
225}
226
Thomas Falconf185a492017-05-26 10:30:48 -0400227static void deactivate_rx_pools(struct ibmvnic_adapter *adapter)
228{
229 int i;
230
231 for (i = 0; i < be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
232 i++)
233 adapter->rx_pool[i].active = 0;
234}
235
Thomas Falcon032c5e82015-12-21 11:26:06 -0600236static void replenish_rx_pool(struct ibmvnic_adapter *adapter,
237 struct ibmvnic_rx_pool *pool)
238{
239 int count = pool->size - atomic_read(&pool->available);
240 struct device *dev = &adapter->vdev->dev;
241 int buffers_added = 0;
242 unsigned long lpar_rc;
243 union sub_crq sub_crq;
244 struct sk_buff *skb;
245 unsigned int offset;
246 dma_addr_t dma_addr;
247 unsigned char *dst;
248 u64 *handle_array;
249 int shift = 0;
250 int index;
251 int i;
252
Thomas Falconf185a492017-05-26 10:30:48 -0400253 if (!pool->active)
254 return;
255
Thomas Falcon032c5e82015-12-21 11:26:06 -0600256 handle_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
257 be32_to_cpu(adapter->login_rsp_buf->
258 off_rxadd_subcrqs));
259
260 for (i = 0; i < count; ++i) {
261 skb = alloc_skb(pool->buff_size, GFP_ATOMIC);
262 if (!skb) {
263 dev_err(dev, "Couldn't replenish rx buff\n");
264 adapter->replenish_no_mem++;
265 break;
266 }
267
268 index = pool->free_map[pool->next_free];
269
270 if (pool->rx_buff[index].skb)
271 dev_err(dev, "Inconsistent free_map!\n");
272
273 /* Copy the skb to the long term mapped DMA buffer */
274 offset = index * pool->buff_size;
275 dst = pool->long_term_buff.buff + offset;
276 memset(dst, 0, pool->buff_size);
277 dma_addr = pool->long_term_buff.addr + offset;
278 pool->rx_buff[index].data = dst;
279
280 pool->free_map[pool->next_free] = IBMVNIC_INVALID_MAP;
281 pool->rx_buff[index].dma = dma_addr;
282 pool->rx_buff[index].skb = skb;
283 pool->rx_buff[index].pool_index = pool->index;
284 pool->rx_buff[index].size = pool->buff_size;
285
286 memset(&sub_crq, 0, sizeof(sub_crq));
287 sub_crq.rx_add.first = IBMVNIC_CRQ_CMD;
288 sub_crq.rx_add.correlator =
289 cpu_to_be64((u64)&pool->rx_buff[index]);
290 sub_crq.rx_add.ioba = cpu_to_be32(dma_addr);
291 sub_crq.rx_add.map_id = pool->long_term_buff.map_id;
292
293 /* The length field of the sCRQ is defined to be 24 bits so the
294 * buffer size needs to be left shifted by a byte before it is
295 * converted to big endian to prevent the last byte from being
296 * truncated.
297 */
298#ifdef __LITTLE_ENDIAN__
299 shift = 8;
300#endif
301 sub_crq.rx_add.len = cpu_to_be32(pool->buff_size << shift);
302
303 lpar_rc = send_subcrq(adapter, handle_array[pool->index],
304 &sub_crq);
305 if (lpar_rc != H_SUCCESS)
306 goto failure;
307
308 buffers_added++;
309 adapter->replenish_add_buff_success++;
310 pool->next_free = (pool->next_free + 1) % pool->size;
311 }
312 atomic_add(buffers_added, &pool->available);
313 return;
314
315failure:
316 dev_info(dev, "replenish pools failure\n");
317 pool->free_map[pool->next_free] = index;
318 pool->rx_buff[index].skb = NULL;
319 if (!dma_mapping_error(dev, dma_addr))
320 dma_unmap_single(dev, dma_addr, pool->buff_size,
321 DMA_FROM_DEVICE);
322
323 dev_kfree_skb_any(skb);
324 adapter->replenish_add_buff_failure++;
325 atomic_add(buffers_added, &pool->available);
Thomas Falconf185a492017-05-26 10:30:48 -0400326
327 if (lpar_rc == H_CLOSED) {
328 /* Disable buffer pool replenishment and report carrier off if
329 * queue is closed. Firmware guarantees that a signal will
330 * be sent to the driver, triggering a reset.
331 */
332 deactivate_rx_pools(adapter);
333 netif_carrier_off(adapter->netdev);
334 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600335}
336
337static void replenish_pools(struct ibmvnic_adapter *adapter)
338{
339 int i;
340
Thomas Falcon032c5e82015-12-21 11:26:06 -0600341 adapter->replenish_task_cycles++;
342 for (i = 0; i < be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
343 i++) {
344 if (adapter->rx_pool[i].active)
345 replenish_rx_pool(adapter, &adapter->rx_pool[i]);
346 }
347}
348
John Allen3d52b592017-08-02 16:44:14 -0500349static void release_stats_buffers(struct ibmvnic_adapter *adapter)
350{
351 kfree(adapter->tx_stats_buffers);
352 kfree(adapter->rx_stats_buffers);
353}
354
355static int init_stats_buffers(struct ibmvnic_adapter *adapter)
356{
357 adapter->tx_stats_buffers =
358 kcalloc(adapter->req_tx_queues,
359 sizeof(struct ibmvnic_tx_queue_stats),
360 GFP_KERNEL);
361 if (!adapter->tx_stats_buffers)
362 return -ENOMEM;
363
364 adapter->rx_stats_buffers =
365 kcalloc(adapter->req_rx_queues,
366 sizeof(struct ibmvnic_rx_queue_stats),
367 GFP_KERNEL);
368 if (!adapter->rx_stats_buffers)
369 return -ENOMEM;
370
371 return 0;
372}
373
Nathan Fontenot7bbc27a2017-03-30 02:49:23 -0400374static void release_stats_token(struct ibmvnic_adapter *adapter)
375{
376 struct device *dev = &adapter->vdev->dev;
377
378 if (!adapter->stats_token)
379 return;
380
381 dma_unmap_single(dev, adapter->stats_token,
382 sizeof(struct ibmvnic_statistics),
383 DMA_FROM_DEVICE);
384 adapter->stats_token = 0;
385}
386
387static int init_stats_token(struct ibmvnic_adapter *adapter)
388{
389 struct device *dev = &adapter->vdev->dev;
390 dma_addr_t stok;
391
392 stok = dma_map_single(dev, &adapter->stats,
393 sizeof(struct ibmvnic_statistics),
394 DMA_FROM_DEVICE);
395 if (dma_mapping_error(dev, stok)) {
396 dev_err(dev, "Couldn't map stats buffer\n");
397 return -1;
398 }
399
400 adapter->stats_token = stok;
401 return 0;
402}
403
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400404static int reset_rx_pools(struct ibmvnic_adapter *adapter)
405{
406 struct ibmvnic_rx_pool *rx_pool;
407 int rx_scrqs;
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500408 int i, j, rc;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400409
410 rx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
411 for (i = 0; i < rx_scrqs; i++) {
412 rx_pool = &adapter->rx_pool[i];
413
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500414 rc = reset_long_term_buff(adapter, &rx_pool->long_term_buff);
415 if (rc)
416 return rc;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400417
418 for (j = 0; j < rx_pool->size; j++)
419 rx_pool->free_map[j] = j;
420
421 memset(rx_pool->rx_buff, 0,
422 rx_pool->size * sizeof(struct ibmvnic_rx_buff));
423
424 atomic_set(&rx_pool->available, 0);
425 rx_pool->next_alloc = 0;
426 rx_pool->next_free = 0;
Thomas Falconc3e53b92017-06-14 23:50:05 -0500427 rx_pool->active = 1;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400428 }
429
430 return 0;
431}
432
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400433static void release_rx_pools(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -0600434{
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400435 struct ibmvnic_rx_pool *rx_pool;
436 int rx_scrqs;
437 int i, j;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600438
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400439 if (!adapter->rx_pool)
Thomas Falcon032c5e82015-12-21 11:26:06 -0600440 return;
441
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400442 rx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
443 for (i = 0; i < rx_scrqs; i++) {
444 rx_pool = &adapter->rx_pool[i];
445
446 kfree(rx_pool->free_map);
447 free_long_term_buff(adapter, &rx_pool->long_term_buff);
448
449 if (!rx_pool->rx_buff)
Nathan Fontenote0ebe9422017-05-03 14:04:50 -0400450 continue;
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400451
452 for (j = 0; j < rx_pool->size; j++) {
453 if (rx_pool->rx_buff[j].skb) {
454 dev_kfree_skb_any(rx_pool->rx_buff[i].skb);
455 rx_pool->rx_buff[i].skb = NULL;
456 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600457 }
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400458
459 kfree(rx_pool->rx_buff);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600460 }
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400461
462 kfree(adapter->rx_pool);
463 adapter->rx_pool = NULL;
464}
465
466static int init_rx_pools(struct net_device *netdev)
467{
468 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
469 struct device *dev = &adapter->vdev->dev;
470 struct ibmvnic_rx_pool *rx_pool;
471 int rxadd_subcrqs;
472 u64 *size_array;
473 int i, j;
474
475 rxadd_subcrqs =
476 be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
477 size_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
478 be32_to_cpu(adapter->login_rsp_buf->off_rxadd_buff_size));
479
480 adapter->rx_pool = kcalloc(rxadd_subcrqs,
481 sizeof(struct ibmvnic_rx_pool),
482 GFP_KERNEL);
483 if (!adapter->rx_pool) {
484 dev_err(dev, "Failed to allocate rx pools\n");
485 return -1;
486 }
487
488 for (i = 0; i < rxadd_subcrqs; i++) {
489 rx_pool = &adapter->rx_pool[i];
490
491 netdev_dbg(adapter->netdev,
492 "Initializing rx_pool %d, %lld buffs, %lld bytes each\n",
493 i, adapter->req_rx_add_entries_per_subcrq,
494 be64_to_cpu(size_array[i]));
495
496 rx_pool->size = adapter->req_rx_add_entries_per_subcrq;
497 rx_pool->index = i;
498 rx_pool->buff_size = be64_to_cpu(size_array[i]);
499 rx_pool->active = 1;
500
501 rx_pool->free_map = kcalloc(rx_pool->size, sizeof(int),
502 GFP_KERNEL);
503 if (!rx_pool->free_map) {
504 release_rx_pools(adapter);
505 return -1;
506 }
507
508 rx_pool->rx_buff = kcalloc(rx_pool->size,
509 sizeof(struct ibmvnic_rx_buff),
510 GFP_KERNEL);
511 if (!rx_pool->rx_buff) {
512 dev_err(dev, "Couldn't alloc rx buffers\n");
513 release_rx_pools(adapter);
514 return -1;
515 }
516
517 if (alloc_long_term_buff(adapter, &rx_pool->long_term_buff,
518 rx_pool->size * rx_pool->buff_size)) {
519 release_rx_pools(adapter);
520 return -1;
521 }
522
523 for (j = 0; j < rx_pool->size; ++j)
524 rx_pool->free_map[j] = j;
525
526 atomic_set(&rx_pool->available, 0);
527 rx_pool->next_alloc = 0;
528 rx_pool->next_free = 0;
529 }
530
531 return 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600532}
533
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400534static int reset_tx_pools(struct ibmvnic_adapter *adapter)
535{
536 struct ibmvnic_tx_pool *tx_pool;
537 int tx_scrqs;
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500538 int i, j, rc;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400539
540 tx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
541 for (i = 0; i < tx_scrqs; i++) {
542 tx_pool = &adapter->tx_pool[i];
543
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500544 rc = reset_long_term_buff(adapter, &tx_pool->long_term_buff);
545 if (rc)
546 return rc;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400547
548 memset(tx_pool->tx_buff, 0,
549 adapter->req_tx_entries_per_subcrq *
550 sizeof(struct ibmvnic_tx_buff));
551
552 for (j = 0; j < adapter->req_tx_entries_per_subcrq; j++)
553 tx_pool->free_map[j] = j;
554
555 tx_pool->consumer_index = 0;
556 tx_pool->producer_index = 0;
557 }
558
559 return 0;
560}
561
Nathan Fontenotc657e322017-03-30 02:49:06 -0400562static void release_tx_pools(struct ibmvnic_adapter *adapter)
563{
564 struct ibmvnic_tx_pool *tx_pool;
565 int i, tx_scrqs;
566
567 if (!adapter->tx_pool)
568 return;
569
570 tx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
571 for (i = 0; i < tx_scrqs; i++) {
572 tx_pool = &adapter->tx_pool[i];
573 kfree(tx_pool->tx_buff);
574 free_long_term_buff(adapter, &tx_pool->long_term_buff);
575 kfree(tx_pool->free_map);
576 }
577
578 kfree(adapter->tx_pool);
579 adapter->tx_pool = NULL;
580}
581
582static int init_tx_pools(struct net_device *netdev)
583{
584 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
585 struct device *dev = &adapter->vdev->dev;
586 struct ibmvnic_tx_pool *tx_pool;
587 int tx_subcrqs;
588 int i, j;
589
590 tx_subcrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
591 adapter->tx_pool = kcalloc(tx_subcrqs,
592 sizeof(struct ibmvnic_tx_pool), GFP_KERNEL);
593 if (!adapter->tx_pool)
594 return -1;
595
596 for (i = 0; i < tx_subcrqs; i++) {
597 tx_pool = &adapter->tx_pool[i];
598 tx_pool->tx_buff = kcalloc(adapter->req_tx_entries_per_subcrq,
599 sizeof(struct ibmvnic_tx_buff),
600 GFP_KERNEL);
601 if (!tx_pool->tx_buff) {
602 dev_err(dev, "tx pool buffer allocation failed\n");
603 release_tx_pools(adapter);
604 return -1;
605 }
606
607 if (alloc_long_term_buff(adapter, &tx_pool->long_term_buff,
608 adapter->req_tx_entries_per_subcrq *
609 adapter->req_mtu)) {
610 release_tx_pools(adapter);
611 return -1;
612 }
613
614 tx_pool->free_map = kcalloc(adapter->req_tx_entries_per_subcrq,
615 sizeof(int), GFP_KERNEL);
616 if (!tx_pool->free_map) {
617 release_tx_pools(adapter);
618 return -1;
619 }
620
621 for (j = 0; j < adapter->req_tx_entries_per_subcrq; j++)
622 tx_pool->free_map[j] = j;
623
624 tx_pool->consumer_index = 0;
625 tx_pool->producer_index = 0;
626 }
627
628 return 0;
629}
630
Nathan Fontenot661a2622017-04-19 13:44:58 -0400631static void release_error_buffers(struct ibmvnic_adapter *adapter)
632{
633 struct device *dev = &adapter->vdev->dev;
634 struct ibmvnic_error_buff *error_buff, *tmp;
635 unsigned long flags;
636
637 spin_lock_irqsave(&adapter->error_list_lock, flags);
638 list_for_each_entry_safe(error_buff, tmp, &adapter->errors, list) {
639 list_del(&error_buff->list);
640 dma_unmap_single(dev, error_buff->dma, error_buff->len,
641 DMA_FROM_DEVICE);
642 kfree(error_buff->buff);
643 kfree(error_buff);
644 }
645 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
646}
647
John Allend944c3d62017-05-26 10:30:13 -0400648static void ibmvnic_napi_enable(struct ibmvnic_adapter *adapter)
649{
650 int i;
651
652 if (adapter->napi_enabled)
653 return;
654
655 for (i = 0; i < adapter->req_rx_queues; i++)
656 napi_enable(&adapter->napi[i]);
657
658 adapter->napi_enabled = true;
659}
660
661static void ibmvnic_napi_disable(struct ibmvnic_adapter *adapter)
662{
663 int i;
664
665 if (!adapter->napi_enabled)
666 return;
667
668 for (i = 0; i < adapter->req_rx_queues; i++)
669 napi_disable(&adapter->napi[i]);
670
671 adapter->napi_enabled = false;
672}
673
John Allena57a5d22017-03-17 17:13:41 -0500674static int ibmvnic_login(struct net_device *netdev)
Thomas Falcon032c5e82015-12-21 11:26:06 -0600675{
676 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
John Allenbd0b6722017-03-17 17:13:40 -0500677 unsigned long timeout = msecs_to_jiffies(30000);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600678 struct device *dev = &adapter->vdev->dev;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600679
John Allenbd0b6722017-03-17 17:13:40 -0500680 do {
681 if (adapter->renegotiate) {
682 adapter->renegotiate = false;
Nathan Fontenotb5108882017-03-30 02:49:18 -0400683 release_sub_crqs(adapter);
John Allenbd0b6722017-03-17 17:13:40 -0500684
685 reinit_completion(&adapter->init_done);
686 send_cap_queries(adapter);
687 if (!wait_for_completion_timeout(&adapter->init_done,
688 timeout)) {
689 dev_err(dev, "Capabilities query timeout\n");
690 return -1;
691 }
692 }
693
694 reinit_completion(&adapter->init_done);
695 send_login(adapter);
696 if (!wait_for_completion_timeout(&adapter->init_done,
697 timeout)) {
698 dev_err(dev, "Login timeout\n");
699 return -1;
700 }
701 } while (adapter->renegotiate);
702
John Allena57a5d22017-03-17 17:13:41 -0500703 return 0;
704}
705
Nathan Fontenot1b8955e2017-03-30 02:49:29 -0400706static void release_resources(struct ibmvnic_adapter *adapter)
707{
Nathan Fontenotc7bac002017-05-03 14:04:44 -0400708 int i;
709
Nathan Fontenot1b8955e2017-03-30 02:49:29 -0400710 release_tx_pools(adapter);
711 release_rx_pools(adapter);
712
Nathan Fontenot1b8955e2017-03-30 02:49:29 -0400713 release_stats_token(adapter);
John Allen3d52b592017-08-02 16:44:14 -0500714 release_stats_buffers(adapter);
Nathan Fontenot661a2622017-04-19 13:44:58 -0400715 release_error_buffers(adapter);
Nathan Fontenotc7bac002017-05-03 14:04:44 -0400716
717 if (adapter->napi) {
718 for (i = 0; i < adapter->req_rx_queues; i++) {
719 if (&adapter->napi[i])
720 netif_napi_del(&adapter->napi[i]);
721 }
722 }
Nathan Fontenot1b8955e2017-03-30 02:49:29 -0400723}
724
Nathan Fontenot53da09e2017-04-21 15:39:04 -0400725static int set_link_state(struct ibmvnic_adapter *adapter, u8 link_state)
726{
727 struct net_device *netdev = adapter->netdev;
728 unsigned long timeout = msecs_to_jiffies(30000);
729 union ibmvnic_crq crq;
730 bool resend;
731 int rc;
732
Nathan Fontenot53da09e2017-04-21 15:39:04 -0400733 netdev_err(netdev, "setting link state %d\n", link_state);
734 memset(&crq, 0, sizeof(crq));
735 crq.logical_link_state.first = IBMVNIC_CRQ_CMD;
736 crq.logical_link_state.cmd = LOGICAL_LINK_STATE;
737 crq.logical_link_state.link_state = link_state;
738
739 do {
740 resend = false;
741
742 reinit_completion(&adapter->init_done);
743 rc = ibmvnic_send_crq(adapter, &crq);
744 if (rc) {
745 netdev_err(netdev, "Failed to set link state\n");
746 return rc;
747 }
748
749 if (!wait_for_completion_timeout(&adapter->init_done,
750 timeout)) {
751 netdev_err(netdev, "timeout setting link state\n");
752 return -1;
753 }
754
755 if (adapter->init_done_rc == 1) {
756 /* Partuial success, delay and re-send */
757 mdelay(1000);
758 resend = true;
759 }
760 } while (resend);
761
762 return 0;
763}
764
Thomas Falcon7f3c6e62017-04-21 15:38:40 -0400765static int set_real_num_queues(struct net_device *netdev)
766{
767 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
768 int rc;
769
770 rc = netif_set_real_num_tx_queues(netdev, adapter->req_tx_queues);
771 if (rc) {
772 netdev_err(netdev, "failed to set the number of tx queues\n");
773 return rc;
774 }
775
776 rc = netif_set_real_num_rx_queues(netdev, adapter->req_rx_queues);
777 if (rc)
778 netdev_err(netdev, "failed to set the number of rx queues\n");
779
780 return rc;
781}
782
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400783static int init_resources(struct ibmvnic_adapter *adapter)
John Allena57a5d22017-03-17 17:13:41 -0500784{
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400785 struct net_device *netdev = adapter->netdev;
786 int i, rc;
John Allena57a5d22017-03-17 17:13:41 -0500787
Thomas Falcon7f3c6e62017-04-21 15:38:40 -0400788 rc = set_real_num_queues(netdev);
789 if (rc)
790 return rc;
John Allenbd0b6722017-03-17 17:13:40 -0500791
John Allen3d52b592017-08-02 16:44:14 -0500792 rc = init_stats_buffers(adapter);
793 if (rc)
794 return rc;
795
Nathan Fontenot5d5e84e2017-04-21 15:38:58 -0400796 rc = init_stats_token(adapter);
797 if (rc)
798 return rc;
799
Thomas Falcon032c5e82015-12-21 11:26:06 -0600800 adapter->map_id = 1;
801 adapter->napi = kcalloc(adapter->req_rx_queues,
802 sizeof(struct napi_struct), GFP_KERNEL);
803 if (!adapter->napi)
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400804 return -ENOMEM;
805
Thomas Falcon032c5e82015-12-21 11:26:06 -0600806 for (i = 0; i < adapter->req_rx_queues; i++) {
807 netif_napi_add(netdev, &adapter->napi[i], ibmvnic_poll,
808 NAPI_POLL_WEIGHT);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600809 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600810
Thomas Falcon032c5e82015-12-21 11:26:06 -0600811 send_map_query(adapter);
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400812
813 rc = init_rx_pools(netdev);
814 if (rc)
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400815 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600816
Nathan Fontenotc657e322017-03-30 02:49:06 -0400817 rc = init_tx_pools(netdev);
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400818 return rc;
819}
820
Nathan Fontenoted651a12017-05-03 14:04:38 -0400821static int __ibmvnic_open(struct net_device *netdev)
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400822{
823 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Nathan Fontenoted651a12017-05-03 14:04:38 -0400824 enum vnic_state prev_state = adapter->state;
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400825 int i, rc;
826
Nathan Fontenot90c80142017-05-03 14:04:32 -0400827 adapter->state = VNIC_OPENING;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600828 replenish_pools(adapter);
John Allend944c3d62017-05-26 10:30:13 -0400829 ibmvnic_napi_enable(adapter);
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400830
Thomas Falcon032c5e82015-12-21 11:26:06 -0600831 /* We're ready to receive frames, enable the sub-crq interrupts and
832 * set the logical link state to up
833 */
Nathan Fontenoted651a12017-05-03 14:04:38 -0400834 for (i = 0; i < adapter->req_rx_queues; i++) {
835 if (prev_state == VNIC_CLOSED)
836 enable_irq(adapter->rx_scrq[i]->irq);
837 else
838 enable_scrq_irq(adapter, adapter->rx_scrq[i]);
839 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600840
Nathan Fontenoted651a12017-05-03 14:04:38 -0400841 for (i = 0; i < adapter->req_tx_queues; i++) {
842 if (prev_state == VNIC_CLOSED)
843 enable_irq(adapter->tx_scrq[i]->irq);
844 else
845 enable_scrq_irq(adapter, adapter->tx_scrq[i]);
846 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600847
Nathan Fontenot53da09e2017-04-21 15:39:04 -0400848 rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_UP);
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400849 if (rc) {
850 for (i = 0; i < adapter->req_rx_queues; i++)
851 napi_disable(&adapter->napi[i]);
852 release_resources(adapter);
Nathan Fontenoted651a12017-05-03 14:04:38 -0400853 return rc;
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400854 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600855
Nathan Fontenoted651a12017-05-03 14:04:38 -0400856 netif_tx_start_all_queues(netdev);
857
858 if (prev_state == VNIC_CLOSED) {
859 for (i = 0; i < adapter->req_rx_queues; i++)
860 napi_schedule(&adapter->napi[i]);
861 }
862
863 adapter->state = VNIC_OPEN;
864 return rc;
865}
866
867static int ibmvnic_open(struct net_device *netdev)
868{
869 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
870 int rc;
871
872 mutex_lock(&adapter->reset_lock);
873
874 if (adapter->state != VNIC_CLOSED) {
875 rc = ibmvnic_login(netdev);
876 if (rc) {
877 mutex_unlock(&adapter->reset_lock);
878 return rc;
879 }
880
881 rc = init_resources(adapter);
882 if (rc) {
883 netdev_err(netdev, "failed to initialize resources\n");
884 release_resources(adapter);
885 mutex_unlock(&adapter->reset_lock);
886 return rc;
887 }
888 }
889
890 rc = __ibmvnic_open(netdev);
891 mutex_unlock(&adapter->reset_lock);
892
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400893 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600894}
895
Nathan Fontenotb41b83e2017-05-03 14:04:56 -0400896static void clean_tx_pools(struct ibmvnic_adapter *adapter)
897{
898 struct ibmvnic_tx_pool *tx_pool;
899 u64 tx_entries;
900 int tx_scrqs;
901 int i, j;
902
903 if (!adapter->tx_pool)
904 return;
905
906 tx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
907 tx_entries = adapter->req_tx_entries_per_subcrq;
908
909 /* Free any remaining skbs in the tx buffer pools */
910 for (i = 0; i < tx_scrqs; i++) {
911 tx_pool = &adapter->tx_pool[i];
912 if (!tx_pool)
913 continue;
914
915 for (j = 0; j < tx_entries; j++) {
916 if (tx_pool->tx_buff[j].skb) {
917 dev_kfree_skb_any(tx_pool->tx_buff[j].skb);
918 tx_pool->tx_buff[j].skb = NULL;
919 }
920 }
921 }
922}
923
Nathan Fontenoted651a12017-05-03 14:04:38 -0400924static int __ibmvnic_close(struct net_device *netdev)
John Allenea5509f2017-03-17 17:13:43 -0500925{
926 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Nathan Fontenot53da09e2017-04-21 15:39:04 -0400927 int rc = 0;
John Allenea5509f2017-03-17 17:13:43 -0500928 int i;
929
Nathan Fontenot90c80142017-05-03 14:04:32 -0400930 adapter->state = VNIC_CLOSING;
Thomas Falcon4c2687a2017-06-14 23:50:06 -0500931
932 /* ensure that transmissions are stopped if called by do_reset */
933 if (adapter->resetting)
934 netif_tx_disable(netdev);
935 else
936 netif_tx_stop_all_queues(netdev);
937
John Allend944c3d62017-05-26 10:30:13 -0400938 ibmvnic_napi_disable(adapter);
Nathan Fontenot46293b92017-05-03 14:05:02 -0400939
940 if (adapter->tx_scrq) {
941 for (i = 0; i < adapter->req_tx_queues; i++)
942 if (adapter->tx_scrq[i]->irq)
943 disable_irq(adapter->tx_scrq[i]->irq);
944 }
945
Nathan Fontenot53da09e2017-04-21 15:39:04 -0400946 rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_DN);
Nathan Fontenot46293b92017-05-03 14:05:02 -0400947 if (rc)
948 return rc;
949
950 if (adapter->rx_scrq) {
951 for (i = 0; i < adapter->req_rx_queues; i++) {
952 int retries = 10;
953
954 while (pending_scrq(adapter, adapter->rx_scrq[i])) {
955 retries--;
956 mdelay(100);
957
958 if (retries == 0)
959 break;
960 }
961
962 if (adapter->rx_scrq[i]->irq)
963 disable_irq(adapter->rx_scrq[i]->irq);
964 }
965 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600966
Thomas Falcon10f76212017-05-26 10:30:31 -0400967 clean_tx_pools(adapter);
Nathan Fontenot90c80142017-05-03 14:04:32 -0400968 adapter->state = VNIC_CLOSED;
Nathan Fontenot53da09e2017-04-21 15:39:04 -0400969 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600970}
971
Nathan Fontenoted651a12017-05-03 14:04:38 -0400972static int ibmvnic_close(struct net_device *netdev)
973{
974 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
975 int rc;
976
977 mutex_lock(&adapter->reset_lock);
978 rc = __ibmvnic_close(netdev);
979 mutex_unlock(&adapter->reset_lock);
980
981 return rc;
982}
983
Thomas Falconad7775d2016-04-01 17:20:34 -0500984/**
985 * build_hdr_data - creates L2/L3/L4 header data buffer
986 * @hdr_field - bitfield determining needed headers
987 * @skb - socket buffer
988 * @hdr_len - array of header lengths
989 * @tot_len - total length of data
990 *
991 * Reads hdr_field to determine which headers are needed by firmware.
992 * Builds a buffer containing these headers. Saves individual header
993 * lengths and total buffer length to be used to build descriptors.
994 */
995static int build_hdr_data(u8 hdr_field, struct sk_buff *skb,
996 int *hdr_len, u8 *hdr_data)
997{
998 int len = 0;
999 u8 *hdr;
1000
1001 hdr_len[0] = sizeof(struct ethhdr);
1002
1003 if (skb->protocol == htons(ETH_P_IP)) {
1004 hdr_len[1] = ip_hdr(skb)->ihl * 4;
1005 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
1006 hdr_len[2] = tcp_hdrlen(skb);
1007 else if (ip_hdr(skb)->protocol == IPPROTO_UDP)
1008 hdr_len[2] = sizeof(struct udphdr);
1009 } else if (skb->protocol == htons(ETH_P_IPV6)) {
1010 hdr_len[1] = sizeof(struct ipv6hdr);
1011 if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
1012 hdr_len[2] = tcp_hdrlen(skb);
1013 else if (ipv6_hdr(skb)->nexthdr == IPPROTO_UDP)
1014 hdr_len[2] = sizeof(struct udphdr);
1015 }
1016
1017 memset(hdr_data, 0, 120);
1018 if ((hdr_field >> 6) & 1) {
1019 hdr = skb_mac_header(skb);
1020 memcpy(hdr_data, hdr, hdr_len[0]);
1021 len += hdr_len[0];
1022 }
1023
1024 if ((hdr_field >> 5) & 1) {
1025 hdr = skb_network_header(skb);
1026 memcpy(hdr_data + len, hdr, hdr_len[1]);
1027 len += hdr_len[1];
1028 }
1029
1030 if ((hdr_field >> 4) & 1) {
1031 hdr = skb_transport_header(skb);
1032 memcpy(hdr_data + len, hdr, hdr_len[2]);
1033 len += hdr_len[2];
1034 }
1035 return len;
1036}
1037
1038/**
1039 * create_hdr_descs - create header and header extension descriptors
1040 * @hdr_field - bitfield determining needed headers
1041 * @data - buffer containing header data
1042 * @len - length of data buffer
1043 * @hdr_len - array of individual header lengths
1044 * @scrq_arr - descriptor array
1045 *
1046 * Creates header and, if needed, header extension descriptors and
1047 * places them in a descriptor array, scrq_arr
1048 */
1049
1050static void create_hdr_descs(u8 hdr_field, u8 *hdr_data, int len, int *hdr_len,
1051 union sub_crq *scrq_arr)
1052{
1053 union sub_crq hdr_desc;
1054 int tmp_len = len;
1055 u8 *data, *cur;
1056 int tmp;
1057
1058 while (tmp_len > 0) {
1059 cur = hdr_data + len - tmp_len;
1060
1061 memset(&hdr_desc, 0, sizeof(hdr_desc));
1062 if (cur != hdr_data) {
1063 data = hdr_desc.hdr_ext.data;
1064 tmp = tmp_len > 29 ? 29 : tmp_len;
1065 hdr_desc.hdr_ext.first = IBMVNIC_CRQ_CMD;
1066 hdr_desc.hdr_ext.type = IBMVNIC_HDR_EXT_DESC;
1067 hdr_desc.hdr_ext.len = tmp;
1068 } else {
1069 data = hdr_desc.hdr.data;
1070 tmp = tmp_len > 24 ? 24 : tmp_len;
1071 hdr_desc.hdr.first = IBMVNIC_CRQ_CMD;
1072 hdr_desc.hdr.type = IBMVNIC_HDR_DESC;
1073 hdr_desc.hdr.len = tmp;
1074 hdr_desc.hdr.l2_len = (u8)hdr_len[0];
1075 hdr_desc.hdr.l3_len = cpu_to_be16((u16)hdr_len[1]);
1076 hdr_desc.hdr.l4_len = (u8)hdr_len[2];
1077 hdr_desc.hdr.flag = hdr_field << 1;
1078 }
1079 memcpy(data, cur, tmp);
1080 tmp_len -= tmp;
1081 *scrq_arr = hdr_desc;
1082 scrq_arr++;
1083 }
1084}
1085
1086/**
1087 * build_hdr_descs_arr - build a header descriptor array
1088 * @skb - socket buffer
1089 * @num_entries - number of descriptors to be sent
1090 * @subcrq - first TX descriptor
1091 * @hdr_field - bit field determining which headers will be sent
1092 *
1093 * This function will build a TX descriptor array with applicable
1094 * L2/L3/L4 packet header descriptors to be sent by send_subcrq_indirect.
1095 */
1096
1097static void build_hdr_descs_arr(struct ibmvnic_tx_buff *txbuff,
1098 int *num_entries, u8 hdr_field)
1099{
1100 int hdr_len[3] = {0, 0, 0};
1101 int tot_len, len;
1102 u8 *hdr_data = txbuff->hdr_data;
1103
1104 tot_len = build_hdr_data(hdr_field, txbuff->skb, hdr_len,
1105 txbuff->hdr_data);
1106 len = tot_len;
1107 len -= 24;
1108 if (len > 0)
1109 num_entries += len % 29 ? len / 29 + 1 : len / 29;
1110 create_hdr_descs(hdr_field, hdr_data, tot_len, hdr_len,
1111 txbuff->indir_arr + 1);
1112}
1113
Thomas Falcon032c5e82015-12-21 11:26:06 -06001114static int ibmvnic_xmit(struct sk_buff *skb, struct net_device *netdev)
1115{
1116 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1117 int queue_num = skb_get_queue_mapping(skb);
Thomas Falconad7775d2016-04-01 17:20:34 -05001118 u8 *hdrs = (u8 *)&adapter->tx_rx_desc_req;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001119 struct device *dev = &adapter->vdev->dev;
1120 struct ibmvnic_tx_buff *tx_buff = NULL;
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001121 struct ibmvnic_sub_crq_queue *tx_scrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001122 struct ibmvnic_tx_pool *tx_pool;
1123 unsigned int tx_send_failed = 0;
1124 unsigned int tx_map_failed = 0;
1125 unsigned int tx_dropped = 0;
1126 unsigned int tx_packets = 0;
1127 unsigned int tx_bytes = 0;
1128 dma_addr_t data_dma_addr;
1129 struct netdev_queue *txq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001130 unsigned long lpar_rc;
1131 union sub_crq tx_crq;
1132 unsigned int offset;
Thomas Falconad7775d2016-04-01 17:20:34 -05001133 int num_entries = 1;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001134 unsigned char *dst;
1135 u64 *handle_array;
1136 int index = 0;
1137 int ret = 0;
1138
Nathan Fontenoted651a12017-05-03 14:04:38 -04001139 if (adapter->resetting) {
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001140 if (!netif_subqueue_stopped(netdev, skb))
1141 netif_stop_subqueue(netdev, queue_num);
1142 dev_kfree_skb_any(skb);
1143
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 }
1149
Nathan Fontenot161b8a82017-05-03 14:05:08 -04001150 tx_pool = &adapter->tx_pool[queue_num];
1151 tx_scrq = adapter->tx_scrq[queue_num];
1152 txq = netdev_get_tx_queue(netdev, skb_get_queue_mapping(skb));
1153 handle_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
1154 be32_to_cpu(adapter->login_rsp_buf->off_txsubm_subcrqs));
1155
Thomas Falcon032c5e82015-12-21 11:26:06 -06001156 index = tx_pool->free_map[tx_pool->consumer_index];
1157 offset = index * adapter->req_mtu;
1158 dst = tx_pool->long_term_buff.buff + offset;
1159 memset(dst, 0, adapter->req_mtu);
1160 skb_copy_from_linear_data(skb, dst, skb->len);
1161 data_dma_addr = tx_pool->long_term_buff.addr + offset;
1162
1163 tx_pool->consumer_index =
1164 (tx_pool->consumer_index + 1) %
Thomas Falcon068d9f92017-03-05 12:18:42 -06001165 adapter->req_tx_entries_per_subcrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001166
1167 tx_buff = &tx_pool->tx_buff[index];
1168 tx_buff->skb = skb;
1169 tx_buff->data_dma[0] = data_dma_addr;
1170 tx_buff->data_len[0] = skb->len;
1171 tx_buff->index = index;
1172 tx_buff->pool_index = queue_num;
1173 tx_buff->last_frag = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001174
1175 memset(&tx_crq, 0, sizeof(tx_crq));
1176 tx_crq.v1.first = IBMVNIC_CRQ_CMD;
1177 tx_crq.v1.type = IBMVNIC_TX_DESC;
1178 tx_crq.v1.n_crq_elem = 1;
1179 tx_crq.v1.n_sge = 1;
1180 tx_crq.v1.flags1 = IBMVNIC_TX_COMP_NEEDED;
1181 tx_crq.v1.correlator = cpu_to_be32(index);
1182 tx_crq.v1.dma_reg = cpu_to_be16(tx_pool->long_term_buff.map_id);
1183 tx_crq.v1.sge_len = cpu_to_be32(skb->len);
1184 tx_crq.v1.ioba = cpu_to_be64(data_dma_addr);
1185
1186 if (adapter->vlan_header_insertion) {
1187 tx_crq.v1.flags2 |= IBMVNIC_TX_VLAN_INSERT;
1188 tx_crq.v1.vlan_id = cpu_to_be16(skb->vlan_tci);
1189 }
1190
1191 if (skb->protocol == htons(ETH_P_IP)) {
1192 if (ip_hdr(skb)->version == 4)
1193 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV4;
1194 else if (ip_hdr(skb)->version == 6)
1195 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV6;
1196
1197 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
1198 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_TCP;
1199 else if (ip_hdr(skb)->protocol != IPPROTO_TCP)
1200 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_UDP;
1201 }
1202
Thomas Falconad7775d2016-04-01 17:20:34 -05001203 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06001204 tx_crq.v1.flags1 |= IBMVNIC_TX_CHKSUM_OFFLOAD;
Thomas Falconad7775d2016-04-01 17:20:34 -05001205 hdrs += 2;
1206 }
1207 /* determine if l2/3/4 headers are sent to firmware */
1208 if ((*hdrs >> 7) & 1 &&
1209 (skb->protocol == htons(ETH_P_IP) ||
1210 skb->protocol == htons(ETH_P_IPV6))) {
1211 build_hdr_descs_arr(tx_buff, &num_entries, *hdrs);
1212 tx_crq.v1.n_crq_elem = num_entries;
1213 tx_buff->indir_arr[0] = tx_crq;
1214 tx_buff->indir_dma = dma_map_single(dev, tx_buff->indir_arr,
1215 sizeof(tx_buff->indir_arr),
1216 DMA_TO_DEVICE);
1217 if (dma_mapping_error(dev, tx_buff->indir_dma)) {
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001218 dev_kfree_skb_any(skb);
1219 tx_buff->skb = NULL;
Thomas Falconad7775d2016-04-01 17:20:34 -05001220 if (!firmware_has_feature(FW_FEATURE_CMO))
1221 dev_err(dev, "tx: unable to map descriptor array\n");
1222 tx_map_failed++;
1223 tx_dropped++;
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001224 ret = NETDEV_TX_OK;
Thomas Falconad7775d2016-04-01 17:20:34 -05001225 goto out;
1226 }
John Allen498cd8e2016-04-06 11:49:55 -05001227 lpar_rc = send_subcrq_indirect(adapter, handle_array[queue_num],
Thomas Falconad7775d2016-04-01 17:20:34 -05001228 (u64)tx_buff->indir_dma,
1229 (u64)num_entries);
1230 } else {
John Allen498cd8e2016-04-06 11:49:55 -05001231 lpar_rc = send_subcrq(adapter, handle_array[queue_num],
1232 &tx_crq);
Thomas Falconad7775d2016-04-01 17:20:34 -05001233 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001234 if (lpar_rc != H_SUCCESS) {
1235 dev_err(dev, "tx failed with code %ld\n", lpar_rc);
1236
1237 if (tx_pool->consumer_index == 0)
1238 tx_pool->consumer_index =
Thomas Falcon068d9f92017-03-05 12:18:42 -06001239 adapter->req_tx_entries_per_subcrq - 1;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001240 else
1241 tx_pool->consumer_index--;
1242
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001243 dev_kfree_skb_any(skb);
1244 tx_buff->skb = NULL;
1245
Thomas Falconb8c80b82017-05-26 10:30:42 -04001246 if (lpar_rc == H_CLOSED) {
1247 /* Disable TX and report carrier off if queue is closed.
1248 * Firmware guarantees that a signal will be sent to the
1249 * driver, triggering a reset or some other action.
1250 */
1251 netif_tx_stop_all_queues(netdev);
1252 netif_carrier_off(netdev);
1253 }
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001254
Thomas Falcon032c5e82015-12-21 11:26:06 -06001255 tx_send_failed++;
1256 tx_dropped++;
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001257 ret = NETDEV_TX_OK;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001258 goto out;
1259 }
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001260
Brian King58c8c0c2017-04-19 13:44:47 -04001261 if (atomic_inc_return(&tx_scrq->used)
1262 >= adapter->req_tx_entries_per_subcrq) {
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001263 netdev_info(netdev, "Stopping queue %d\n", queue_num);
1264 netif_stop_subqueue(netdev, queue_num);
1265 }
1266
Thomas Falcon032c5e82015-12-21 11:26:06 -06001267 tx_packets++;
1268 tx_bytes += skb->len;
1269 txq->trans_start = jiffies;
1270 ret = NETDEV_TX_OK;
1271
1272out:
1273 netdev->stats.tx_dropped += tx_dropped;
1274 netdev->stats.tx_bytes += tx_bytes;
1275 netdev->stats.tx_packets += tx_packets;
1276 adapter->tx_send_failed += tx_send_failed;
1277 adapter->tx_map_failed += tx_map_failed;
John Allen3d52b592017-08-02 16:44:14 -05001278 adapter->tx_stats_buffers[queue_num].packets += tx_packets;
1279 adapter->tx_stats_buffers[queue_num].bytes += tx_bytes;
1280 adapter->tx_stats_buffers[queue_num].dropped_packets += tx_dropped;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001281
1282 return ret;
1283}
1284
1285static void ibmvnic_set_multi(struct net_device *netdev)
1286{
1287 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1288 struct netdev_hw_addr *ha;
1289 union ibmvnic_crq crq;
1290
1291 memset(&crq, 0, sizeof(crq));
1292 crq.request_capability.first = IBMVNIC_CRQ_CMD;
1293 crq.request_capability.cmd = REQUEST_CAPABILITY;
1294
1295 if (netdev->flags & IFF_PROMISC) {
1296 if (!adapter->promisc_supported)
1297 return;
1298 } else {
1299 if (netdev->flags & IFF_ALLMULTI) {
1300 /* Accept all multicast */
1301 memset(&crq, 0, sizeof(crq));
1302 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1303 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1304 crq.multicast_ctrl.flags = IBMVNIC_ENABLE_ALL;
1305 ibmvnic_send_crq(adapter, &crq);
1306 } else if (netdev_mc_empty(netdev)) {
1307 /* Reject all multicast */
1308 memset(&crq, 0, sizeof(crq));
1309 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1310 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1311 crq.multicast_ctrl.flags = IBMVNIC_DISABLE_ALL;
1312 ibmvnic_send_crq(adapter, &crq);
1313 } else {
1314 /* Accept one or more multicast(s) */
1315 netdev_for_each_mc_addr(ha, netdev) {
1316 memset(&crq, 0, sizeof(crq));
1317 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1318 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1319 crq.multicast_ctrl.flags = IBMVNIC_ENABLE_MC;
1320 ether_addr_copy(&crq.multicast_ctrl.mac_addr[0],
1321 ha->addr);
1322 ibmvnic_send_crq(adapter, &crq);
1323 }
1324 }
1325 }
1326}
1327
1328static int ibmvnic_set_mac(struct net_device *netdev, void *p)
1329{
1330 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1331 struct sockaddr *addr = p;
1332 union ibmvnic_crq crq;
1333
1334 if (!is_valid_ether_addr(addr->sa_data))
1335 return -EADDRNOTAVAIL;
1336
1337 memset(&crq, 0, sizeof(crq));
1338 crq.change_mac_addr.first = IBMVNIC_CRQ_CMD;
1339 crq.change_mac_addr.cmd = CHANGE_MAC_ADDR;
1340 ether_addr_copy(&crq.change_mac_addr.mac_addr[0], addr->sa_data);
1341 ibmvnic_send_crq(adapter, &crq);
1342 /* netdev->dev_addr is changed in handle_change_mac_rsp function */
1343 return 0;
1344}
1345
Nathan Fontenoted651a12017-05-03 14:04:38 -04001346/**
1347 * do_reset returns zero if we are able to keep processing reset events, or
1348 * non-zero if we hit a fatal error and must halt.
1349 */
1350static int do_reset(struct ibmvnic_adapter *adapter,
1351 struct ibmvnic_rwi *rwi, u32 reset_state)
1352{
1353 struct net_device *netdev = adapter->netdev;
1354 int i, rc;
1355
1356 netif_carrier_off(netdev);
1357 adapter->reset_reason = rwi->reset_reason;
1358
1359 if (rwi->reset_reason == VNIC_RESET_MOBILITY) {
1360 rc = ibmvnic_reenable_crq_queue(adapter);
1361 if (rc)
1362 return 0;
1363 }
1364
1365 rc = __ibmvnic_close(netdev);
1366 if (rc)
1367 return rc;
1368
John Allen8cb31cf2017-05-26 10:30:37 -04001369 if (adapter->reset_reason != VNIC_RESET_NON_FATAL) {
1370 /* remove the closed state so when we call open it appears
1371 * we are coming from the probed state.
1372 */
Nathan Fontenoted651a12017-05-03 14:04:38 -04001373 adapter->state = VNIC_PROBED;
John Allen8cb31cf2017-05-26 10:30:37 -04001374
John Allen8cb31cf2017-05-26 10:30:37 -04001375 rc = ibmvnic_init(adapter);
1376 if (rc)
1377 return 0;
1378
1379 /* If the adapter was in PROBE state prior to the reset,
1380 * exit here.
1381 */
1382 if (reset_state == VNIC_PROBED)
1383 return 0;
1384
1385 rc = ibmvnic_login(netdev);
1386 if (rc) {
1387 adapter->state = VNIC_PROBED;
1388 return 0;
1389 }
1390
Nathan Fontenot8c0543a2017-05-26 10:31:06 -04001391 rc = reset_tx_pools(adapter);
1392 if (rc)
1393 return rc;
1394
1395 rc = reset_rx_pools(adapter);
John Allen8cb31cf2017-05-26 10:30:37 -04001396 if (rc)
1397 return rc;
1398
1399 if (reset_state == VNIC_CLOSED)
1400 return 0;
Nathan Fontenoted651a12017-05-03 14:04:38 -04001401 }
1402
Nathan Fontenoted651a12017-05-03 14:04:38 -04001403 rc = __ibmvnic_open(netdev);
1404 if (rc) {
1405 if (list_empty(&adapter->rwi_list))
1406 adapter->state = VNIC_CLOSED;
1407 else
1408 adapter->state = reset_state;
1409
1410 return 0;
1411 }
1412
1413 netif_carrier_on(netdev);
1414
1415 /* kick napi */
1416 for (i = 0; i < adapter->req_rx_queues; i++)
1417 napi_schedule(&adapter->napi[i]);
1418
Nathan Fontenot61d3e1d2017-06-12 20:47:45 -04001419 if (adapter->reset_reason != VNIC_RESET_FAILOVER)
1420 netdev_notify_peers(netdev);
1421
Nathan Fontenoted651a12017-05-03 14:04:38 -04001422 return 0;
1423}
1424
1425static struct ibmvnic_rwi *get_next_rwi(struct ibmvnic_adapter *adapter)
1426{
1427 struct ibmvnic_rwi *rwi;
1428
1429 mutex_lock(&adapter->rwi_lock);
1430
1431 if (!list_empty(&adapter->rwi_list)) {
1432 rwi = list_first_entry(&adapter->rwi_list, struct ibmvnic_rwi,
1433 list);
1434 list_del(&rwi->list);
1435 } else {
1436 rwi = NULL;
1437 }
1438
1439 mutex_unlock(&adapter->rwi_lock);
1440 return rwi;
1441}
1442
1443static void free_all_rwi(struct ibmvnic_adapter *adapter)
1444{
1445 struct ibmvnic_rwi *rwi;
1446
1447 rwi = get_next_rwi(adapter);
1448 while (rwi) {
1449 kfree(rwi);
1450 rwi = get_next_rwi(adapter);
1451 }
1452}
1453
1454static void __ibmvnic_reset(struct work_struct *work)
1455{
1456 struct ibmvnic_rwi *rwi;
1457 struct ibmvnic_adapter *adapter;
1458 struct net_device *netdev;
1459 u32 reset_state;
1460 int rc;
1461
1462 adapter = container_of(work, struct ibmvnic_adapter, ibmvnic_reset);
1463 netdev = adapter->netdev;
1464
1465 mutex_lock(&adapter->reset_lock);
1466 adapter->resetting = true;
1467 reset_state = adapter->state;
1468
1469 rwi = get_next_rwi(adapter);
1470 while (rwi) {
1471 rc = do_reset(adapter, rwi, reset_state);
1472 kfree(rwi);
1473 if (rc)
1474 break;
1475
1476 rwi = get_next_rwi(adapter);
1477 }
1478
1479 if (rc) {
1480 free_all_rwi(adapter);
Wei Yongjun6d0af072017-05-18 15:24:52 +00001481 mutex_unlock(&adapter->reset_lock);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001482 return;
1483 }
1484
1485 adapter->resetting = false;
1486 mutex_unlock(&adapter->reset_lock);
1487}
1488
1489static void ibmvnic_reset(struct ibmvnic_adapter *adapter,
1490 enum ibmvnic_reset_reason reason)
1491{
1492 struct ibmvnic_rwi *rwi, *tmp;
1493 struct net_device *netdev = adapter->netdev;
1494 struct list_head *entry;
1495
1496 if (adapter->state == VNIC_REMOVING ||
1497 adapter->state == VNIC_REMOVED) {
1498 netdev_dbg(netdev, "Adapter removing, skipping reset\n");
1499 return;
1500 }
1501
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04001502 if (adapter->state == VNIC_PROBING) {
1503 netdev_warn(netdev, "Adapter reset during probe\n");
1504 adapter->init_done_rc = EAGAIN;
1505 return;
1506 }
1507
Nathan Fontenoted651a12017-05-03 14:04:38 -04001508 mutex_lock(&adapter->rwi_lock);
1509
1510 list_for_each(entry, &adapter->rwi_list) {
1511 tmp = list_entry(entry, struct ibmvnic_rwi, list);
1512 if (tmp->reset_reason == reason) {
1513 netdev_err(netdev, "Matching reset found, skipping\n");
1514 mutex_unlock(&adapter->rwi_lock);
1515 return;
1516 }
1517 }
1518
1519 rwi = kzalloc(sizeof(*rwi), GFP_KERNEL);
1520 if (!rwi) {
1521 mutex_unlock(&adapter->rwi_lock);
1522 ibmvnic_close(netdev);
1523 return;
1524 }
1525
1526 rwi->reset_reason = reason;
1527 list_add_tail(&rwi->list, &adapter->rwi_list);
1528 mutex_unlock(&adapter->rwi_lock);
1529 schedule_work(&adapter->ibmvnic_reset);
1530}
1531
Thomas Falcon032c5e82015-12-21 11:26:06 -06001532static void ibmvnic_tx_timeout(struct net_device *dev)
1533{
1534 struct ibmvnic_adapter *adapter = netdev_priv(dev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001535
Nathan Fontenoted651a12017-05-03 14:04:38 -04001536 ibmvnic_reset(adapter, VNIC_RESET_TIMEOUT);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001537}
1538
1539static void remove_buff_from_pool(struct ibmvnic_adapter *adapter,
1540 struct ibmvnic_rx_buff *rx_buff)
1541{
1542 struct ibmvnic_rx_pool *pool = &adapter->rx_pool[rx_buff->pool_index];
1543
1544 rx_buff->skb = NULL;
1545
1546 pool->free_map[pool->next_alloc] = (int)(rx_buff - pool->rx_buff);
1547 pool->next_alloc = (pool->next_alloc + 1) % pool->size;
1548
1549 atomic_dec(&pool->available);
1550}
1551
1552static int ibmvnic_poll(struct napi_struct *napi, int budget)
1553{
1554 struct net_device *netdev = napi->dev;
1555 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1556 int scrq_num = (int)(napi - adapter->napi);
1557 int frames_processed = 0;
Nathan Fontenot152ce472017-05-26 10:30:54 -04001558
Thomas Falcon032c5e82015-12-21 11:26:06 -06001559restart_poll:
1560 while (frames_processed < budget) {
1561 struct sk_buff *skb;
1562 struct ibmvnic_rx_buff *rx_buff;
1563 union sub_crq *next;
1564 u32 length;
1565 u16 offset;
1566 u8 flags = 0;
1567
Thomas Falcon21ecba62017-06-14 23:50:09 -05001568 if (unlikely(adapter->resetting)) {
1569 enable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
1570 napi_complete_done(napi, frames_processed);
1571 return frames_processed;
1572 }
1573
Thomas Falcon032c5e82015-12-21 11:26:06 -06001574 if (!pending_scrq(adapter, adapter->rx_scrq[scrq_num]))
1575 break;
1576 next = ibmvnic_next_scrq(adapter, adapter->rx_scrq[scrq_num]);
1577 rx_buff =
1578 (struct ibmvnic_rx_buff *)be64_to_cpu(next->
1579 rx_comp.correlator);
1580 /* do error checking */
1581 if (next->rx_comp.rc) {
1582 netdev_err(netdev, "rx error %x\n", next->rx_comp.rc);
1583 /* free the entry */
1584 next->rx_comp.first = 0;
1585 remove_buff_from_pool(adapter, rx_buff);
Nathan Fontenotca05e312017-05-03 14:05:14 -04001586 continue;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001587 }
1588
1589 length = be32_to_cpu(next->rx_comp.len);
1590 offset = be16_to_cpu(next->rx_comp.off_frame_data);
1591 flags = next->rx_comp.flags;
1592 skb = rx_buff->skb;
1593 skb_copy_to_linear_data(skb, rx_buff->data + offset,
1594 length);
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04001595
1596 /* VLAN Header has been stripped by the system firmware and
1597 * needs to be inserted by the driver
1598 */
1599 if (adapter->rx_vlan_header_insertion &&
1600 (flags & IBMVNIC_VLAN_STRIPPED))
1601 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
1602 ntohs(next->rx_comp.vlan_tci));
1603
Thomas Falcon032c5e82015-12-21 11:26:06 -06001604 /* free the entry */
1605 next->rx_comp.first = 0;
1606 remove_buff_from_pool(adapter, rx_buff);
1607
1608 skb_put(skb, length);
1609 skb->protocol = eth_type_trans(skb, netdev);
Thomas Falcon94ca3052017-05-03 14:05:20 -04001610 skb_record_rx_queue(skb, scrq_num);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001611
1612 if (flags & IBMVNIC_IP_CHKSUM_GOOD &&
1613 flags & IBMVNIC_TCP_UDP_CHKSUM_GOOD) {
1614 skb->ip_summed = CHECKSUM_UNNECESSARY;
1615 }
1616
1617 length = skb->len;
1618 napi_gro_receive(napi, skb); /* send it up */
1619 netdev->stats.rx_packets++;
1620 netdev->stats.rx_bytes += length;
John Allen3d52b592017-08-02 16:44:14 -05001621 adapter->rx_stats_buffers[scrq_num].packets++;
1622 adapter->rx_stats_buffers[scrq_num].bytes += length;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001623 frames_processed++;
1624 }
Nathan Fontenot152ce472017-05-26 10:30:54 -04001625
1626 if (adapter->state != VNIC_CLOSING)
1627 replenish_rx_pool(adapter, &adapter->rx_pool[scrq_num]);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001628
1629 if (frames_processed < budget) {
1630 enable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
Eric Dumazet6ad20162017-01-30 08:22:01 -08001631 napi_complete_done(napi, frames_processed);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001632 if (pending_scrq(adapter, adapter->rx_scrq[scrq_num]) &&
1633 napi_reschedule(napi)) {
1634 disable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
1635 goto restart_poll;
1636 }
1637 }
1638 return frames_processed;
1639}
1640
1641#ifdef CONFIG_NET_POLL_CONTROLLER
1642static void ibmvnic_netpoll_controller(struct net_device *dev)
1643{
1644 struct ibmvnic_adapter *adapter = netdev_priv(dev);
1645 int i;
1646
1647 replenish_pools(netdev_priv(dev));
1648 for (i = 0; i < adapter->req_rx_queues; i++)
1649 ibmvnic_interrupt_rx(adapter->rx_scrq[i]->irq,
1650 adapter->rx_scrq[i]);
1651}
1652#endif
1653
John Allen3a807b72017-06-06 16:55:52 -05001654static int ibmvnic_change_mtu(struct net_device *netdev, int new_mtu)
1655{
1656 return -EOPNOTSUPP;
1657}
1658
Thomas Falcon032c5e82015-12-21 11:26:06 -06001659static const struct net_device_ops ibmvnic_netdev_ops = {
1660 .ndo_open = ibmvnic_open,
1661 .ndo_stop = ibmvnic_close,
1662 .ndo_start_xmit = ibmvnic_xmit,
1663 .ndo_set_rx_mode = ibmvnic_set_multi,
1664 .ndo_set_mac_address = ibmvnic_set_mac,
1665 .ndo_validate_addr = eth_validate_addr,
Thomas Falcon032c5e82015-12-21 11:26:06 -06001666 .ndo_tx_timeout = ibmvnic_tx_timeout,
1667#ifdef CONFIG_NET_POLL_CONTROLLER
1668 .ndo_poll_controller = ibmvnic_netpoll_controller,
1669#endif
John Allen3a807b72017-06-06 16:55:52 -05001670 .ndo_change_mtu = ibmvnic_change_mtu,
Thomas Falcon032c5e82015-12-21 11:26:06 -06001671};
1672
1673/* ethtool functions */
1674
Philippe Reynes8a433792017-01-07 22:37:29 +01001675static int ibmvnic_get_link_ksettings(struct net_device *netdev,
1676 struct ethtool_link_ksettings *cmd)
Thomas Falcon032c5e82015-12-21 11:26:06 -06001677{
Philippe Reynes8a433792017-01-07 22:37:29 +01001678 u32 supported, advertising;
1679
1680 supported = (SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg |
Thomas Falcon032c5e82015-12-21 11:26:06 -06001681 SUPPORTED_FIBRE);
Philippe Reynes8a433792017-01-07 22:37:29 +01001682 advertising = (ADVERTISED_1000baseT_Full | ADVERTISED_Autoneg |
Thomas Falcon032c5e82015-12-21 11:26:06 -06001683 ADVERTISED_FIBRE);
Philippe Reynes8a433792017-01-07 22:37:29 +01001684 cmd->base.speed = SPEED_1000;
1685 cmd->base.duplex = DUPLEX_FULL;
1686 cmd->base.port = PORT_FIBRE;
1687 cmd->base.phy_address = 0;
1688 cmd->base.autoneg = AUTONEG_ENABLE;
1689
1690 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
1691 supported);
1692 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
1693 advertising);
1694
Thomas Falcon032c5e82015-12-21 11:26:06 -06001695 return 0;
1696}
1697
1698static void ibmvnic_get_drvinfo(struct net_device *dev,
1699 struct ethtool_drvinfo *info)
1700{
1701 strlcpy(info->driver, ibmvnic_driver_name, sizeof(info->driver));
1702 strlcpy(info->version, IBMVNIC_DRIVER_VERSION, sizeof(info->version));
1703}
1704
1705static u32 ibmvnic_get_msglevel(struct net_device *netdev)
1706{
1707 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1708
1709 return adapter->msg_enable;
1710}
1711
1712static void ibmvnic_set_msglevel(struct net_device *netdev, u32 data)
1713{
1714 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1715
1716 adapter->msg_enable = data;
1717}
1718
1719static u32 ibmvnic_get_link(struct net_device *netdev)
1720{
1721 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1722
1723 /* Don't need to send a query because we request a logical link up at
1724 * init and then we wait for link state indications
1725 */
1726 return adapter->logical_link_state;
1727}
1728
1729static void ibmvnic_get_ringparam(struct net_device *netdev,
1730 struct ethtool_ringparam *ring)
1731{
1732 ring->rx_max_pending = 0;
1733 ring->tx_max_pending = 0;
1734 ring->rx_mini_max_pending = 0;
1735 ring->rx_jumbo_max_pending = 0;
1736 ring->rx_pending = 0;
1737 ring->tx_pending = 0;
1738 ring->rx_mini_pending = 0;
1739 ring->rx_jumbo_pending = 0;
1740}
1741
1742static void ibmvnic_get_strings(struct net_device *dev, u32 stringset, u8 *data)
1743{
John Allen3d52b592017-08-02 16:44:14 -05001744 struct ibmvnic_adapter *adapter = netdev_priv(dev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001745 int i;
1746
1747 if (stringset != ETH_SS_STATS)
1748 return;
1749
1750 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++, data += ETH_GSTRING_LEN)
1751 memcpy(data, ibmvnic_stats[i].name, ETH_GSTRING_LEN);
John Allen3d52b592017-08-02 16:44:14 -05001752
1753 for (i = 0; i < adapter->req_tx_queues; i++) {
1754 snprintf(data, ETH_GSTRING_LEN, "tx%d_packets", i);
1755 data += ETH_GSTRING_LEN;
1756
1757 snprintf(data, ETH_GSTRING_LEN, "tx%d_bytes", i);
1758 data += ETH_GSTRING_LEN;
1759
1760 snprintf(data, ETH_GSTRING_LEN, "tx%d_dropped_packets", i);
1761 data += ETH_GSTRING_LEN;
1762 }
1763
1764 for (i = 0; i < adapter->req_rx_queues; i++) {
1765 snprintf(data, ETH_GSTRING_LEN, "rx%d_packets", i);
1766 data += ETH_GSTRING_LEN;
1767
1768 snprintf(data, ETH_GSTRING_LEN, "rx%d_bytes", i);
1769 data += ETH_GSTRING_LEN;
1770
1771 snprintf(data, ETH_GSTRING_LEN, "rx%d_interrupts", i);
1772 data += ETH_GSTRING_LEN;
1773 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001774}
1775
1776static int ibmvnic_get_sset_count(struct net_device *dev, int sset)
1777{
John Allen3d52b592017-08-02 16:44:14 -05001778 struct ibmvnic_adapter *adapter = netdev_priv(dev);
1779
Thomas Falcon032c5e82015-12-21 11:26:06 -06001780 switch (sset) {
1781 case ETH_SS_STATS:
John Allen3d52b592017-08-02 16:44:14 -05001782 return ARRAY_SIZE(ibmvnic_stats) +
1783 adapter->req_tx_queues * NUM_TX_STATS +
1784 adapter->req_rx_queues * NUM_RX_STATS;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001785 default:
1786 return -EOPNOTSUPP;
1787 }
1788}
1789
1790static void ibmvnic_get_ethtool_stats(struct net_device *dev,
1791 struct ethtool_stats *stats, u64 *data)
1792{
1793 struct ibmvnic_adapter *adapter = netdev_priv(dev);
1794 union ibmvnic_crq crq;
John Allen3d52b592017-08-02 16:44:14 -05001795 int i, j;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001796
1797 memset(&crq, 0, sizeof(crq));
1798 crq.request_statistics.first = IBMVNIC_CRQ_CMD;
1799 crq.request_statistics.cmd = REQUEST_STATISTICS;
1800 crq.request_statistics.ioba = cpu_to_be32(adapter->stats_token);
1801 crq.request_statistics.len =
1802 cpu_to_be32(sizeof(struct ibmvnic_statistics));
Thomas Falcon032c5e82015-12-21 11:26:06 -06001803
1804 /* Wait for data to be written */
1805 init_completion(&adapter->stats_done);
Nathan Fontenotdb5d0b52017-02-10 13:45:05 -05001806 ibmvnic_send_crq(adapter, &crq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001807 wait_for_completion(&adapter->stats_done);
1808
1809 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++)
1810 data[i] = IBMVNIC_GET_STAT(adapter, ibmvnic_stats[i].offset);
John Allen3d52b592017-08-02 16:44:14 -05001811
1812 for (j = 0; j < adapter->req_tx_queues; j++) {
1813 data[i] = adapter->tx_stats_buffers[j].packets;
1814 i++;
1815 data[i] = adapter->tx_stats_buffers[j].bytes;
1816 i++;
1817 data[i] = adapter->tx_stats_buffers[j].dropped_packets;
1818 i++;
1819 }
1820
1821 for (j = 0; j < adapter->req_rx_queues; j++) {
1822 data[i] = adapter->rx_stats_buffers[j].packets;
1823 i++;
1824 data[i] = adapter->rx_stats_buffers[j].bytes;
1825 i++;
1826 data[i] = adapter->rx_stats_buffers[j].interrupts;
1827 i++;
1828 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001829}
1830
1831static const struct ethtool_ops ibmvnic_ethtool_ops = {
Thomas Falcon032c5e82015-12-21 11:26:06 -06001832 .get_drvinfo = ibmvnic_get_drvinfo,
1833 .get_msglevel = ibmvnic_get_msglevel,
1834 .set_msglevel = ibmvnic_set_msglevel,
1835 .get_link = ibmvnic_get_link,
1836 .get_ringparam = ibmvnic_get_ringparam,
1837 .get_strings = ibmvnic_get_strings,
1838 .get_sset_count = ibmvnic_get_sset_count,
1839 .get_ethtool_stats = ibmvnic_get_ethtool_stats,
Philippe Reynes8a433792017-01-07 22:37:29 +01001840 .get_link_ksettings = ibmvnic_get_link_ksettings,
Thomas Falcon032c5e82015-12-21 11:26:06 -06001841};
1842
1843/* Routines for managing CRQs/sCRQs */
1844
Nathan Fontenot57a49432017-05-26 10:31:12 -04001845static int reset_one_sub_crq_queue(struct ibmvnic_adapter *adapter,
1846 struct ibmvnic_sub_crq_queue *scrq)
1847{
1848 int rc;
1849
1850 if (scrq->irq) {
1851 free_irq(scrq->irq, scrq);
1852 irq_dispose_mapping(scrq->irq);
1853 scrq->irq = 0;
1854 }
1855
Thomas Falconc8b2ad02017-06-14 23:50:07 -05001856 memset(scrq->msgs, 0, 4 * PAGE_SIZE);
Nathan Fontenot57a49432017-05-26 10:31:12 -04001857 scrq->cur = 0;
1858
1859 rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
1860 4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
1861 return rc;
1862}
1863
1864static int reset_sub_crq_queues(struct ibmvnic_adapter *adapter)
1865{
1866 int i, rc;
1867
1868 for (i = 0; i < adapter->req_tx_queues; i++) {
1869 rc = reset_one_sub_crq_queue(adapter, adapter->tx_scrq[i]);
1870 if (rc)
1871 return rc;
1872 }
1873
1874 for (i = 0; i < adapter->req_rx_queues; i++) {
1875 rc = reset_one_sub_crq_queue(adapter, adapter->rx_scrq[i]);
1876 if (rc)
1877 return rc;
1878 }
1879
Nathan Fontenot57a49432017-05-26 10:31:12 -04001880 return rc;
1881}
1882
Thomas Falcon032c5e82015-12-21 11:26:06 -06001883static void release_sub_crq_queue(struct ibmvnic_adapter *adapter,
1884 struct ibmvnic_sub_crq_queue *scrq)
1885{
1886 struct device *dev = &adapter->vdev->dev;
1887 long rc;
1888
1889 netdev_dbg(adapter->netdev, "Releasing sub-CRQ\n");
1890
1891 /* Close the sub-crqs */
1892 do {
1893 rc = plpar_hcall_norets(H_FREE_SUB_CRQ,
1894 adapter->vdev->unit_address,
1895 scrq->crq_num);
1896 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
1897
Thomas Falconffa73852017-04-19 13:44:29 -04001898 if (rc) {
1899 netdev_err(adapter->netdev,
1900 "Failed to release sub-CRQ %16lx, rc = %ld\n",
1901 scrq->crq_num, rc);
1902 }
1903
Thomas Falcon032c5e82015-12-21 11:26:06 -06001904 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
1905 DMA_BIDIRECTIONAL);
1906 free_pages((unsigned long)scrq->msgs, 2);
1907 kfree(scrq);
1908}
1909
1910static struct ibmvnic_sub_crq_queue *init_sub_crq_queue(struct ibmvnic_adapter
1911 *adapter)
1912{
1913 struct device *dev = &adapter->vdev->dev;
1914 struct ibmvnic_sub_crq_queue *scrq;
1915 int rc;
1916
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04001917 scrq = kzalloc(sizeof(*scrq), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001918 if (!scrq)
1919 return NULL;
1920
Nathan Fontenot7f7adc52017-04-19 13:45:16 -04001921 scrq->msgs =
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04001922 (union sub_crq *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, 2);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001923 if (!scrq->msgs) {
1924 dev_warn(dev, "Couldn't allocate crq queue messages page\n");
1925 goto zero_page_failed;
1926 }
1927
1928 scrq->msg_token = dma_map_single(dev, scrq->msgs, 4 * PAGE_SIZE,
1929 DMA_BIDIRECTIONAL);
1930 if (dma_mapping_error(dev, scrq->msg_token)) {
1931 dev_warn(dev, "Couldn't map crq queue messages page\n");
1932 goto map_failed;
1933 }
1934
1935 rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
1936 4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
1937
1938 if (rc == H_RESOURCE)
1939 rc = ibmvnic_reset_crq(adapter);
1940
1941 if (rc == H_CLOSED) {
1942 dev_warn(dev, "Partner adapter not ready, waiting.\n");
1943 } else if (rc) {
1944 dev_warn(dev, "Error %d registering sub-crq\n", rc);
1945 goto reg_failed;
1946 }
1947
Thomas Falcon032c5e82015-12-21 11:26:06 -06001948 scrq->adapter = adapter;
1949 scrq->size = 4 * PAGE_SIZE / sizeof(*scrq->msgs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001950 spin_lock_init(&scrq->lock);
1951
1952 netdev_dbg(adapter->netdev,
1953 "sub-crq initialized, num %lx, hw_irq=%lx, irq=%x\n",
1954 scrq->crq_num, scrq->hw_irq, scrq->irq);
1955
1956 return scrq;
1957
Thomas Falcon032c5e82015-12-21 11:26:06 -06001958reg_failed:
1959 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
1960 DMA_BIDIRECTIONAL);
1961map_failed:
1962 free_pages((unsigned long)scrq->msgs, 2);
1963zero_page_failed:
1964 kfree(scrq);
1965
1966 return NULL;
1967}
1968
1969static void release_sub_crqs(struct ibmvnic_adapter *adapter)
1970{
1971 int i;
1972
1973 if (adapter->tx_scrq) {
Nathan Fontenotb5108882017-03-30 02:49:18 -04001974 for (i = 0; i < adapter->req_tx_queues; i++) {
1975 if (!adapter->tx_scrq[i])
1976 continue;
1977
1978 if (adapter->tx_scrq[i]->irq) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06001979 free_irq(adapter->tx_scrq[i]->irq,
1980 adapter->tx_scrq[i]);
Thomas Falcon88eb98a2016-07-06 15:35:16 -05001981 irq_dispose_mapping(adapter->tx_scrq[i]->irq);
Nathan Fontenotb5108882017-03-30 02:49:18 -04001982 adapter->tx_scrq[i]->irq = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001983 }
Nathan Fontenotb5108882017-03-30 02:49:18 -04001984
1985 release_sub_crq_queue(adapter, adapter->tx_scrq[i]);
1986 }
1987
Nathan Fontenot9501df32017-03-15 23:38:07 -04001988 kfree(adapter->tx_scrq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001989 adapter->tx_scrq = NULL;
1990 }
1991
1992 if (adapter->rx_scrq) {
Nathan Fontenotb5108882017-03-30 02:49:18 -04001993 for (i = 0; i < adapter->req_rx_queues; i++) {
1994 if (!adapter->rx_scrq[i])
1995 continue;
1996
1997 if (adapter->rx_scrq[i]->irq) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06001998 free_irq(adapter->rx_scrq[i]->irq,
1999 adapter->rx_scrq[i]);
Thomas Falcon88eb98a2016-07-06 15:35:16 -05002000 irq_dispose_mapping(adapter->rx_scrq[i]->irq);
Nathan Fontenotb5108882017-03-30 02:49:18 -04002001 adapter->rx_scrq[i]->irq = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002002 }
Nathan Fontenotb5108882017-03-30 02:49:18 -04002003
2004 release_sub_crq_queue(adapter, adapter->rx_scrq[i]);
2005 }
2006
Nathan Fontenot9501df32017-03-15 23:38:07 -04002007 kfree(adapter->rx_scrq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002008 adapter->rx_scrq = NULL;
2009 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002010}
2011
2012static int disable_scrq_irq(struct ibmvnic_adapter *adapter,
2013 struct ibmvnic_sub_crq_queue *scrq)
2014{
2015 struct device *dev = &adapter->vdev->dev;
2016 unsigned long rc;
2017
2018 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
2019 H_DISABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
2020 if (rc)
2021 dev_err(dev, "Couldn't disable scrq irq 0x%lx. rc=%ld\n",
2022 scrq->hw_irq, rc);
2023 return rc;
2024}
2025
2026static int enable_scrq_irq(struct ibmvnic_adapter *adapter,
2027 struct ibmvnic_sub_crq_queue *scrq)
2028{
2029 struct device *dev = &adapter->vdev->dev;
2030 unsigned long rc;
2031
2032 if (scrq->hw_irq > 0x100000000ULL) {
2033 dev_err(dev, "bad hw_irq = %lx\n", scrq->hw_irq);
2034 return 1;
2035 }
2036
2037 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
2038 H_ENABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
2039 if (rc)
2040 dev_err(dev, "Couldn't enable scrq irq 0x%lx. rc=%ld\n",
2041 scrq->hw_irq, rc);
2042 return rc;
2043}
2044
2045static int ibmvnic_complete_tx(struct ibmvnic_adapter *adapter,
2046 struct ibmvnic_sub_crq_queue *scrq)
2047{
2048 struct device *dev = &adapter->vdev->dev;
2049 struct ibmvnic_tx_buff *txbuff;
2050 union sub_crq *next;
2051 int index;
2052 int i, j;
Thomas Falconad7775d2016-04-01 17:20:34 -05002053 u8 first;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002054
2055restart_loop:
2056 while (pending_scrq(adapter, scrq)) {
2057 unsigned int pool = scrq->pool_index;
2058
2059 next = ibmvnic_next_scrq(adapter, scrq);
2060 for (i = 0; i < next->tx_comp.num_comps; i++) {
2061 if (next->tx_comp.rcs[i]) {
2062 dev_err(dev, "tx error %x\n",
2063 next->tx_comp.rcs[i]);
2064 continue;
2065 }
2066 index = be32_to_cpu(next->tx_comp.correlators[i]);
2067 txbuff = &adapter->tx_pool[pool].tx_buff[index];
2068
2069 for (j = 0; j < IBMVNIC_MAX_FRAGS_PER_CRQ; j++) {
2070 if (!txbuff->data_dma[j])
2071 continue;
2072
2073 txbuff->data_dma[j] = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002074 }
Thomas Falconad7775d2016-04-01 17:20:34 -05002075 /* if sub_crq was sent indirectly */
2076 first = txbuff->indir_arr[0].generic.first;
2077 if (first == IBMVNIC_CRQ_CMD) {
2078 dma_unmap_single(dev, txbuff->indir_dma,
2079 sizeof(txbuff->indir_arr),
2080 DMA_TO_DEVICE);
2081 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002082
Thomas Falcon142c0ac2017-03-05 12:18:41 -06002083 if (txbuff->last_frag) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06002084 dev_kfree_skb_any(txbuff->skb);
Nathan Fontenot7c3e7de2017-05-03 14:05:25 -04002085 txbuff->skb = NULL;
Thomas Falcon142c0ac2017-03-05 12:18:41 -06002086 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002087
2088 adapter->tx_pool[pool].free_map[adapter->tx_pool[pool].
2089 producer_index] = index;
2090 adapter->tx_pool[pool].producer_index =
2091 (adapter->tx_pool[pool].producer_index + 1) %
Thomas Falcon068d9f92017-03-05 12:18:42 -06002092 adapter->req_tx_entries_per_subcrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002093 }
2094 /* remove tx_comp scrq*/
2095 next->tx_comp.first = 0;
Nathan Fontenot7c3e7de2017-05-03 14:05:25 -04002096
2097 if (atomic_sub_return(next->tx_comp.num_comps, &scrq->used) <=
2098 (adapter->req_tx_entries_per_subcrq / 2) &&
2099 __netif_subqueue_stopped(adapter->netdev,
2100 scrq->pool_index)) {
2101 netif_wake_subqueue(adapter->netdev, scrq->pool_index);
2102 netdev_info(adapter->netdev, "Started queue %d\n",
2103 scrq->pool_index);
2104 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002105 }
2106
2107 enable_scrq_irq(adapter, scrq);
2108
2109 if (pending_scrq(adapter, scrq)) {
2110 disable_scrq_irq(adapter, scrq);
2111 goto restart_loop;
2112 }
2113
2114 return 0;
2115}
2116
2117static irqreturn_t ibmvnic_interrupt_tx(int irq, void *instance)
2118{
2119 struct ibmvnic_sub_crq_queue *scrq = instance;
2120 struct ibmvnic_adapter *adapter = scrq->adapter;
2121
2122 disable_scrq_irq(adapter, scrq);
2123 ibmvnic_complete_tx(adapter, scrq);
2124
2125 return IRQ_HANDLED;
2126}
2127
2128static irqreturn_t ibmvnic_interrupt_rx(int irq, void *instance)
2129{
2130 struct ibmvnic_sub_crq_queue *scrq = instance;
2131 struct ibmvnic_adapter *adapter = scrq->adapter;
2132
John Allen3d52b592017-08-02 16:44:14 -05002133 adapter->rx_stats_buffers[scrq->scrq_num].interrupts++;
2134
Thomas Falcon032c5e82015-12-21 11:26:06 -06002135 if (napi_schedule_prep(&adapter->napi[scrq->scrq_num])) {
2136 disable_scrq_irq(adapter, scrq);
2137 __napi_schedule(&adapter->napi[scrq->scrq_num]);
2138 }
2139
2140 return IRQ_HANDLED;
2141}
2142
Thomas Falconea22d512016-07-06 15:35:17 -05002143static int init_sub_crq_irqs(struct ibmvnic_adapter *adapter)
2144{
2145 struct device *dev = &adapter->vdev->dev;
2146 struct ibmvnic_sub_crq_queue *scrq;
2147 int i = 0, j = 0;
2148 int rc = 0;
2149
2150 for (i = 0; i < adapter->req_tx_queues; i++) {
2151 scrq = adapter->tx_scrq[i];
2152 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
2153
Michael Ellerman99c17902016-09-10 19:59:05 +10002154 if (!scrq->irq) {
Thomas Falconea22d512016-07-06 15:35:17 -05002155 rc = -EINVAL;
2156 dev_err(dev, "Error mapping irq\n");
2157 goto req_tx_irq_failed;
2158 }
2159
2160 rc = request_irq(scrq->irq, ibmvnic_interrupt_tx,
2161 0, "ibmvnic_tx", scrq);
2162
2163 if (rc) {
2164 dev_err(dev, "Couldn't register tx irq 0x%x. rc=%d\n",
2165 scrq->irq, rc);
2166 irq_dispose_mapping(scrq->irq);
2167 goto req_rx_irq_failed;
2168 }
2169 }
2170
2171 for (i = 0; i < adapter->req_rx_queues; i++) {
2172 scrq = adapter->rx_scrq[i];
2173 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
Michael Ellerman99c17902016-09-10 19:59:05 +10002174 if (!scrq->irq) {
Thomas Falconea22d512016-07-06 15:35:17 -05002175 rc = -EINVAL;
2176 dev_err(dev, "Error mapping irq\n");
2177 goto req_rx_irq_failed;
2178 }
2179 rc = request_irq(scrq->irq, ibmvnic_interrupt_rx,
2180 0, "ibmvnic_rx", scrq);
2181 if (rc) {
2182 dev_err(dev, "Couldn't register rx irq 0x%x. rc=%d\n",
2183 scrq->irq, rc);
2184 irq_dispose_mapping(scrq->irq);
2185 goto req_rx_irq_failed;
2186 }
2187 }
2188 return rc;
2189
2190req_rx_irq_failed:
Thomas Falcon8bf371e2016-10-27 12:28:52 -05002191 for (j = 0; j < i; j++) {
Thomas Falconea22d512016-07-06 15:35:17 -05002192 free_irq(adapter->rx_scrq[j]->irq, adapter->rx_scrq[j]);
2193 irq_dispose_mapping(adapter->rx_scrq[j]->irq);
Thomas Falcon8bf371e2016-10-27 12:28:52 -05002194 }
Thomas Falconea22d512016-07-06 15:35:17 -05002195 i = adapter->req_tx_queues;
2196req_tx_irq_failed:
Thomas Falcon8bf371e2016-10-27 12:28:52 -05002197 for (j = 0; j < i; j++) {
Thomas Falconea22d512016-07-06 15:35:17 -05002198 free_irq(adapter->tx_scrq[j]->irq, adapter->tx_scrq[j]);
2199 irq_dispose_mapping(adapter->rx_scrq[j]->irq);
Thomas Falcon8bf371e2016-10-27 12:28:52 -05002200 }
Nathan Fontenotb5108882017-03-30 02:49:18 -04002201 release_sub_crqs(adapter);
Thomas Falconea22d512016-07-06 15:35:17 -05002202 return rc;
2203}
2204
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002205static int init_sub_crqs(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002206{
2207 struct device *dev = &adapter->vdev->dev;
2208 struct ibmvnic_sub_crq_queue **allqueues;
2209 int registered_queues = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002210 int total_queues;
2211 int more = 0;
Thomas Falconea22d512016-07-06 15:35:17 -05002212 int i;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002213
Thomas Falcon032c5e82015-12-21 11:26:06 -06002214 total_queues = adapter->req_tx_queues + adapter->req_rx_queues;
2215
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04002216 allqueues = kcalloc(total_queues, sizeof(*allqueues), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002217 if (!allqueues)
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002218 return -1;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002219
2220 for (i = 0; i < total_queues; i++) {
2221 allqueues[i] = init_sub_crq_queue(adapter);
2222 if (!allqueues[i]) {
2223 dev_warn(dev, "Couldn't allocate all sub-crqs\n");
2224 break;
2225 }
2226 registered_queues++;
2227 }
2228
2229 /* Make sure we were able to register the minimum number of queues */
2230 if (registered_queues <
2231 adapter->min_tx_queues + adapter->min_rx_queues) {
2232 dev_err(dev, "Fatal: Couldn't init min number of sub-crqs\n");
2233 goto tx_failed;
2234 }
2235
2236 /* Distribute the failed allocated queues*/
2237 for (i = 0; i < total_queues - registered_queues + more ; i++) {
2238 netdev_dbg(adapter->netdev, "Reducing number of queues\n");
2239 switch (i % 3) {
2240 case 0:
2241 if (adapter->req_rx_queues > adapter->min_rx_queues)
2242 adapter->req_rx_queues--;
2243 else
2244 more++;
2245 break;
2246 case 1:
2247 if (adapter->req_tx_queues > adapter->min_tx_queues)
2248 adapter->req_tx_queues--;
2249 else
2250 more++;
2251 break;
2252 }
2253 }
2254
2255 adapter->tx_scrq = kcalloc(adapter->req_tx_queues,
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04002256 sizeof(*adapter->tx_scrq), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002257 if (!adapter->tx_scrq)
2258 goto tx_failed;
2259
2260 for (i = 0; i < adapter->req_tx_queues; i++) {
2261 adapter->tx_scrq[i] = allqueues[i];
2262 adapter->tx_scrq[i]->pool_index = i;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002263 }
2264
2265 adapter->rx_scrq = kcalloc(adapter->req_rx_queues,
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04002266 sizeof(*adapter->rx_scrq), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002267 if (!adapter->rx_scrq)
2268 goto rx_failed;
2269
2270 for (i = 0; i < adapter->req_rx_queues; i++) {
2271 adapter->rx_scrq[i] = allqueues[i + adapter->req_tx_queues];
2272 adapter->rx_scrq[i]->scrq_num = i;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002273 }
2274
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002275 kfree(allqueues);
2276 return 0;
2277
2278rx_failed:
2279 kfree(adapter->tx_scrq);
2280 adapter->tx_scrq = NULL;
2281tx_failed:
2282 for (i = 0; i < registered_queues; i++)
2283 release_sub_crq_queue(adapter, allqueues[i]);
2284 kfree(allqueues);
2285 return -1;
2286}
2287
2288static void ibmvnic_send_req_caps(struct ibmvnic_adapter *adapter, int retry)
2289{
2290 struct device *dev = &adapter->vdev->dev;
2291 union ibmvnic_crq crq;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002292
2293 if (!retry) {
2294 /* Sub-CRQ entries are 32 byte long */
2295 int entries_page = 4 * PAGE_SIZE / (sizeof(u64) * 4);
2296
2297 if (adapter->min_tx_entries_per_subcrq > entries_page ||
2298 adapter->min_rx_add_entries_per_subcrq > entries_page) {
2299 dev_err(dev, "Fatal, invalid entries per sub-crq\n");
2300 return;
2301 }
2302
2303 /* Get the minimum between the queried max and the entries
2304 * that fit in our PAGE_SIZE
2305 */
2306 adapter->req_tx_entries_per_subcrq =
2307 adapter->max_tx_entries_per_subcrq > entries_page ?
2308 entries_page : adapter->max_tx_entries_per_subcrq;
2309 adapter->req_rx_add_entries_per_subcrq =
2310 adapter->max_rx_add_entries_per_subcrq > entries_page ?
2311 entries_page : adapter->max_rx_add_entries_per_subcrq;
2312
2313 adapter->req_tx_queues = adapter->opt_tx_comp_sub_queues;
2314 adapter->req_rx_queues = adapter->opt_rx_comp_queues;
2315 adapter->req_rx_add_queues = adapter->max_rx_add_queues;
2316
2317 adapter->req_mtu = adapter->netdev->mtu + ETH_HLEN;
2318 }
2319
Thomas Falcon032c5e82015-12-21 11:26:06 -06002320 memset(&crq, 0, sizeof(crq));
2321 crq.request_capability.first = IBMVNIC_CRQ_CMD;
2322 crq.request_capability.cmd = REQUEST_CAPABILITY;
2323
2324 crq.request_capability.capability = cpu_to_be16(REQ_TX_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06002325 crq.request_capability.number = cpu_to_be64(adapter->req_tx_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06002326 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002327 ibmvnic_send_crq(adapter, &crq);
2328
2329 crq.request_capability.capability = cpu_to_be16(REQ_RX_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06002330 crq.request_capability.number = cpu_to_be64(adapter->req_rx_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06002331 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002332 ibmvnic_send_crq(adapter, &crq);
2333
2334 crq.request_capability.capability = cpu_to_be16(REQ_RX_ADD_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06002335 crq.request_capability.number = cpu_to_be64(adapter->req_rx_add_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06002336 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002337 ibmvnic_send_crq(adapter, &crq);
2338
2339 crq.request_capability.capability =
2340 cpu_to_be16(REQ_TX_ENTRIES_PER_SUBCRQ);
2341 crq.request_capability.number =
Thomas Falconde89e852016-03-01 10:20:09 -06002342 cpu_to_be64(adapter->req_tx_entries_per_subcrq);
Thomas Falcon901e0402017-02-15 12:17:59 -06002343 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002344 ibmvnic_send_crq(adapter, &crq);
2345
2346 crq.request_capability.capability =
2347 cpu_to_be16(REQ_RX_ADD_ENTRIES_PER_SUBCRQ);
2348 crq.request_capability.number =
Thomas Falconde89e852016-03-01 10:20:09 -06002349 cpu_to_be64(adapter->req_rx_add_entries_per_subcrq);
Thomas Falcon901e0402017-02-15 12:17:59 -06002350 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002351 ibmvnic_send_crq(adapter, &crq);
2352
2353 crq.request_capability.capability = cpu_to_be16(REQ_MTU);
Thomas Falconde89e852016-03-01 10:20:09 -06002354 crq.request_capability.number = cpu_to_be64(adapter->req_mtu);
Thomas Falcon901e0402017-02-15 12:17:59 -06002355 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002356 ibmvnic_send_crq(adapter, &crq);
2357
2358 if (adapter->netdev->flags & IFF_PROMISC) {
2359 if (adapter->promisc_supported) {
2360 crq.request_capability.capability =
2361 cpu_to_be16(PROMISC_REQUESTED);
Thomas Falconde89e852016-03-01 10:20:09 -06002362 crq.request_capability.number = cpu_to_be64(1);
Thomas Falcon901e0402017-02-15 12:17:59 -06002363 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002364 ibmvnic_send_crq(adapter, &crq);
2365 }
2366 } else {
2367 crq.request_capability.capability =
2368 cpu_to_be16(PROMISC_REQUESTED);
Thomas Falconde89e852016-03-01 10:20:09 -06002369 crq.request_capability.number = cpu_to_be64(0);
Thomas Falcon901e0402017-02-15 12:17:59 -06002370 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002371 ibmvnic_send_crq(adapter, &crq);
2372 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002373}
2374
2375static int pending_scrq(struct ibmvnic_adapter *adapter,
2376 struct ibmvnic_sub_crq_queue *scrq)
2377{
2378 union sub_crq *entry = &scrq->msgs[scrq->cur];
2379
Thomas Falcon1cf9cc72017-06-14 23:50:08 -05002380 if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002381 return 1;
2382 else
2383 return 0;
2384}
2385
2386static union sub_crq *ibmvnic_next_scrq(struct ibmvnic_adapter *adapter,
2387 struct ibmvnic_sub_crq_queue *scrq)
2388{
2389 union sub_crq *entry;
2390 unsigned long flags;
2391
2392 spin_lock_irqsave(&scrq->lock, flags);
2393 entry = &scrq->msgs[scrq->cur];
2394 if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP) {
2395 if (++scrq->cur == scrq->size)
2396 scrq->cur = 0;
2397 } else {
2398 entry = NULL;
2399 }
2400 spin_unlock_irqrestore(&scrq->lock, flags);
2401
2402 return entry;
2403}
2404
2405static union ibmvnic_crq *ibmvnic_next_crq(struct ibmvnic_adapter *adapter)
2406{
2407 struct ibmvnic_crq_queue *queue = &adapter->crq;
2408 union ibmvnic_crq *crq;
2409
2410 crq = &queue->msgs[queue->cur];
2411 if (crq->generic.first & IBMVNIC_CRQ_CMD_RSP) {
2412 if (++queue->cur == queue->size)
2413 queue->cur = 0;
2414 } else {
2415 crq = NULL;
2416 }
2417
2418 return crq;
2419}
2420
2421static int send_subcrq(struct ibmvnic_adapter *adapter, u64 remote_handle,
2422 union sub_crq *sub_crq)
2423{
2424 unsigned int ua = adapter->vdev->unit_address;
2425 struct device *dev = &adapter->vdev->dev;
2426 u64 *u64_crq = (u64 *)sub_crq;
2427 int rc;
2428
2429 netdev_dbg(adapter->netdev,
2430 "Sending sCRQ %016lx: %016lx %016lx %016lx %016lx\n",
2431 (unsigned long int)cpu_to_be64(remote_handle),
2432 (unsigned long int)cpu_to_be64(u64_crq[0]),
2433 (unsigned long int)cpu_to_be64(u64_crq[1]),
2434 (unsigned long int)cpu_to_be64(u64_crq[2]),
2435 (unsigned long int)cpu_to_be64(u64_crq[3]));
2436
2437 /* Make sure the hypervisor sees the complete request */
2438 mb();
2439
2440 rc = plpar_hcall_norets(H_SEND_SUB_CRQ, ua,
2441 cpu_to_be64(remote_handle),
2442 cpu_to_be64(u64_crq[0]),
2443 cpu_to_be64(u64_crq[1]),
2444 cpu_to_be64(u64_crq[2]),
2445 cpu_to_be64(u64_crq[3]));
2446
2447 if (rc) {
2448 if (rc == H_CLOSED)
2449 dev_warn(dev, "CRQ Queue closed\n");
2450 dev_err(dev, "Send error (rc=%d)\n", rc);
2451 }
2452
2453 return rc;
2454}
2455
Thomas Falconad7775d2016-04-01 17:20:34 -05002456static int send_subcrq_indirect(struct ibmvnic_adapter *adapter,
2457 u64 remote_handle, u64 ioba, u64 num_entries)
2458{
2459 unsigned int ua = adapter->vdev->unit_address;
2460 struct device *dev = &adapter->vdev->dev;
2461 int rc;
2462
2463 /* Make sure the hypervisor sees the complete request */
2464 mb();
2465 rc = plpar_hcall_norets(H_SEND_SUB_CRQ_INDIRECT, ua,
2466 cpu_to_be64(remote_handle),
2467 ioba, num_entries);
2468
2469 if (rc) {
2470 if (rc == H_CLOSED)
2471 dev_warn(dev, "CRQ Queue closed\n");
2472 dev_err(dev, "Send (indirect) error (rc=%d)\n", rc);
2473 }
2474
2475 return rc;
2476}
2477
Thomas Falcon032c5e82015-12-21 11:26:06 -06002478static int ibmvnic_send_crq(struct ibmvnic_adapter *adapter,
2479 union ibmvnic_crq *crq)
2480{
2481 unsigned int ua = adapter->vdev->unit_address;
2482 struct device *dev = &adapter->vdev->dev;
2483 u64 *u64_crq = (u64 *)crq;
2484 int rc;
2485
2486 netdev_dbg(adapter->netdev, "Sending CRQ: %016lx %016lx\n",
2487 (unsigned long int)cpu_to_be64(u64_crq[0]),
2488 (unsigned long int)cpu_to_be64(u64_crq[1]));
2489
2490 /* Make sure the hypervisor sees the complete request */
2491 mb();
2492
2493 rc = plpar_hcall_norets(H_SEND_CRQ, ua,
2494 cpu_to_be64(u64_crq[0]),
2495 cpu_to_be64(u64_crq[1]));
2496
2497 if (rc) {
2498 if (rc == H_CLOSED)
2499 dev_warn(dev, "CRQ Queue closed\n");
2500 dev_warn(dev, "Send error (rc=%d)\n", rc);
2501 }
2502
2503 return rc;
2504}
2505
2506static int ibmvnic_send_crq_init(struct ibmvnic_adapter *adapter)
2507{
2508 union ibmvnic_crq crq;
2509
2510 memset(&crq, 0, sizeof(crq));
2511 crq.generic.first = IBMVNIC_CRQ_INIT_CMD;
2512 crq.generic.cmd = IBMVNIC_CRQ_INIT;
2513 netdev_dbg(adapter->netdev, "Sending CRQ init\n");
2514
2515 return ibmvnic_send_crq(adapter, &crq);
2516}
2517
Thomas Falcon032c5e82015-12-21 11:26:06 -06002518static int send_version_xchg(struct ibmvnic_adapter *adapter)
2519{
2520 union ibmvnic_crq crq;
2521
2522 memset(&crq, 0, sizeof(crq));
2523 crq.version_exchange.first = IBMVNIC_CRQ_CMD;
2524 crq.version_exchange.cmd = VERSION_EXCHANGE;
2525 crq.version_exchange.version = cpu_to_be16(ibmvnic_version);
2526
2527 return ibmvnic_send_crq(adapter, &crq);
2528}
2529
2530static void send_login(struct ibmvnic_adapter *adapter)
2531{
2532 struct ibmvnic_login_rsp_buffer *login_rsp_buffer;
2533 struct ibmvnic_login_buffer *login_buffer;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002534 struct device *dev = &adapter->vdev->dev;
2535 dma_addr_t rsp_buffer_token;
2536 dma_addr_t buffer_token;
2537 size_t rsp_buffer_size;
2538 union ibmvnic_crq crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002539 size_t buffer_size;
2540 __be64 *tx_list_p;
2541 __be64 *rx_list_p;
2542 int i;
2543
2544 buffer_size =
2545 sizeof(struct ibmvnic_login_buffer) +
2546 sizeof(u64) * (adapter->req_tx_queues + adapter->req_rx_queues);
2547
2548 login_buffer = kmalloc(buffer_size, GFP_ATOMIC);
2549 if (!login_buffer)
2550 goto buf_alloc_failed;
2551
2552 buffer_token = dma_map_single(dev, login_buffer, buffer_size,
2553 DMA_TO_DEVICE);
2554 if (dma_mapping_error(dev, buffer_token)) {
2555 dev_err(dev, "Couldn't map login buffer\n");
2556 goto buf_map_failed;
2557 }
2558
John Allen498cd8e2016-04-06 11:49:55 -05002559 rsp_buffer_size = sizeof(struct ibmvnic_login_rsp_buffer) +
2560 sizeof(u64) * adapter->req_tx_queues +
2561 sizeof(u64) * adapter->req_rx_queues +
2562 sizeof(u64) * adapter->req_rx_queues +
2563 sizeof(u8) * IBMVNIC_TX_DESC_VERSIONS;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002564
2565 login_rsp_buffer = kmalloc(rsp_buffer_size, GFP_ATOMIC);
2566 if (!login_rsp_buffer)
2567 goto buf_rsp_alloc_failed;
2568
2569 rsp_buffer_token = dma_map_single(dev, login_rsp_buffer,
2570 rsp_buffer_size, DMA_FROM_DEVICE);
2571 if (dma_mapping_error(dev, rsp_buffer_token)) {
2572 dev_err(dev, "Couldn't map login rsp buffer\n");
2573 goto buf_rsp_map_failed;
2574 }
Nathan Fontenot661a2622017-04-19 13:44:58 -04002575
Thomas Falcon032c5e82015-12-21 11:26:06 -06002576 adapter->login_buf = login_buffer;
2577 adapter->login_buf_token = buffer_token;
2578 adapter->login_buf_sz = buffer_size;
2579 adapter->login_rsp_buf = login_rsp_buffer;
2580 adapter->login_rsp_buf_token = rsp_buffer_token;
2581 adapter->login_rsp_buf_sz = rsp_buffer_size;
2582
2583 login_buffer->len = cpu_to_be32(buffer_size);
2584 login_buffer->version = cpu_to_be32(INITIAL_VERSION_LB);
2585 login_buffer->num_txcomp_subcrqs = cpu_to_be32(adapter->req_tx_queues);
2586 login_buffer->off_txcomp_subcrqs =
2587 cpu_to_be32(sizeof(struct ibmvnic_login_buffer));
2588 login_buffer->num_rxcomp_subcrqs = cpu_to_be32(adapter->req_rx_queues);
2589 login_buffer->off_rxcomp_subcrqs =
2590 cpu_to_be32(sizeof(struct ibmvnic_login_buffer) +
2591 sizeof(u64) * adapter->req_tx_queues);
2592 login_buffer->login_rsp_ioba = cpu_to_be32(rsp_buffer_token);
2593 login_buffer->login_rsp_len = cpu_to_be32(rsp_buffer_size);
2594
2595 tx_list_p = (__be64 *)((char *)login_buffer +
2596 sizeof(struct ibmvnic_login_buffer));
2597 rx_list_p = (__be64 *)((char *)login_buffer +
2598 sizeof(struct ibmvnic_login_buffer) +
2599 sizeof(u64) * adapter->req_tx_queues);
2600
2601 for (i = 0; i < adapter->req_tx_queues; i++) {
2602 if (adapter->tx_scrq[i]) {
2603 tx_list_p[i] = cpu_to_be64(adapter->tx_scrq[i]->
2604 crq_num);
2605 }
2606 }
2607
2608 for (i = 0; i < adapter->req_rx_queues; i++) {
2609 if (adapter->rx_scrq[i]) {
2610 rx_list_p[i] = cpu_to_be64(adapter->rx_scrq[i]->
2611 crq_num);
2612 }
2613 }
2614
2615 netdev_dbg(adapter->netdev, "Login Buffer:\n");
2616 for (i = 0; i < (adapter->login_buf_sz - 1) / 8 + 1; i++) {
2617 netdev_dbg(adapter->netdev, "%016lx\n",
2618 ((unsigned long int *)(adapter->login_buf))[i]);
2619 }
2620
2621 memset(&crq, 0, sizeof(crq));
2622 crq.login.first = IBMVNIC_CRQ_CMD;
2623 crq.login.cmd = LOGIN;
2624 crq.login.ioba = cpu_to_be32(buffer_token);
2625 crq.login.len = cpu_to_be32(buffer_size);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002626 ibmvnic_send_crq(adapter, &crq);
2627
2628 return;
2629
Thomas Falcon032c5e82015-12-21 11:26:06 -06002630buf_rsp_map_failed:
2631 kfree(login_rsp_buffer);
2632buf_rsp_alloc_failed:
2633 dma_unmap_single(dev, buffer_token, buffer_size, DMA_TO_DEVICE);
2634buf_map_failed:
2635 kfree(login_buffer);
2636buf_alloc_failed:
2637 return;
2638}
2639
2640static void send_request_map(struct ibmvnic_adapter *adapter, dma_addr_t addr,
2641 u32 len, u8 map_id)
2642{
2643 union ibmvnic_crq crq;
2644
2645 memset(&crq, 0, sizeof(crq));
2646 crq.request_map.first = IBMVNIC_CRQ_CMD;
2647 crq.request_map.cmd = REQUEST_MAP;
2648 crq.request_map.map_id = map_id;
2649 crq.request_map.ioba = cpu_to_be32(addr);
2650 crq.request_map.len = cpu_to_be32(len);
2651 ibmvnic_send_crq(adapter, &crq);
2652}
2653
2654static void send_request_unmap(struct ibmvnic_adapter *adapter, u8 map_id)
2655{
2656 union ibmvnic_crq crq;
2657
2658 memset(&crq, 0, sizeof(crq));
2659 crq.request_unmap.first = IBMVNIC_CRQ_CMD;
2660 crq.request_unmap.cmd = REQUEST_UNMAP;
2661 crq.request_unmap.map_id = map_id;
2662 ibmvnic_send_crq(adapter, &crq);
2663}
2664
2665static void send_map_query(struct ibmvnic_adapter *adapter)
2666{
2667 union ibmvnic_crq crq;
2668
2669 memset(&crq, 0, sizeof(crq));
2670 crq.query_map.first = IBMVNIC_CRQ_CMD;
2671 crq.query_map.cmd = QUERY_MAP;
2672 ibmvnic_send_crq(adapter, &crq);
2673}
2674
2675/* Send a series of CRQs requesting various capabilities of the VNIC server */
2676static void send_cap_queries(struct ibmvnic_adapter *adapter)
2677{
2678 union ibmvnic_crq crq;
2679
Thomas Falcon901e0402017-02-15 12:17:59 -06002680 atomic_set(&adapter->running_cap_crqs, 0);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002681 memset(&crq, 0, sizeof(crq));
2682 crq.query_capability.first = IBMVNIC_CRQ_CMD;
2683 crq.query_capability.cmd = QUERY_CAPABILITY;
2684
2685 crq.query_capability.capability = cpu_to_be16(MIN_TX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002686 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002687 ibmvnic_send_crq(adapter, &crq);
2688
2689 crq.query_capability.capability = cpu_to_be16(MIN_RX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002690 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002691 ibmvnic_send_crq(adapter, &crq);
2692
2693 crq.query_capability.capability = cpu_to_be16(MIN_RX_ADD_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002694 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002695 ibmvnic_send_crq(adapter, &crq);
2696
2697 crq.query_capability.capability = cpu_to_be16(MAX_TX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002698 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002699 ibmvnic_send_crq(adapter, &crq);
2700
2701 crq.query_capability.capability = cpu_to_be16(MAX_RX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002702 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002703 ibmvnic_send_crq(adapter, &crq);
2704
2705 crq.query_capability.capability = cpu_to_be16(MAX_RX_ADD_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002706 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002707 ibmvnic_send_crq(adapter, &crq);
2708
2709 crq.query_capability.capability =
2710 cpu_to_be16(MIN_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06002711 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002712 ibmvnic_send_crq(adapter, &crq);
2713
2714 crq.query_capability.capability =
2715 cpu_to_be16(MIN_RX_ADD_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06002716 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002717 ibmvnic_send_crq(adapter, &crq);
2718
2719 crq.query_capability.capability =
2720 cpu_to_be16(MAX_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06002721 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002722 ibmvnic_send_crq(adapter, &crq);
2723
2724 crq.query_capability.capability =
2725 cpu_to_be16(MAX_RX_ADD_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06002726 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002727 ibmvnic_send_crq(adapter, &crq);
2728
2729 crq.query_capability.capability = cpu_to_be16(TCP_IP_OFFLOAD);
Thomas Falcon901e0402017-02-15 12:17:59 -06002730 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002731 ibmvnic_send_crq(adapter, &crq);
2732
2733 crq.query_capability.capability = cpu_to_be16(PROMISC_SUPPORTED);
Thomas Falcon901e0402017-02-15 12:17:59 -06002734 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002735 ibmvnic_send_crq(adapter, &crq);
2736
2737 crq.query_capability.capability = cpu_to_be16(MIN_MTU);
Thomas Falcon901e0402017-02-15 12:17:59 -06002738 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002739 ibmvnic_send_crq(adapter, &crq);
2740
2741 crq.query_capability.capability = cpu_to_be16(MAX_MTU);
Thomas Falcon901e0402017-02-15 12:17:59 -06002742 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002743 ibmvnic_send_crq(adapter, &crq);
2744
2745 crq.query_capability.capability = cpu_to_be16(MAX_MULTICAST_FILTERS);
Thomas Falcon901e0402017-02-15 12:17:59 -06002746 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002747 ibmvnic_send_crq(adapter, &crq);
2748
2749 crq.query_capability.capability = cpu_to_be16(VLAN_HEADER_INSERTION);
Thomas Falcon901e0402017-02-15 12:17:59 -06002750 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002751 ibmvnic_send_crq(adapter, &crq);
2752
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04002753 crq.query_capability.capability = cpu_to_be16(RX_VLAN_HEADER_INSERTION);
2754 atomic_inc(&adapter->running_cap_crqs);
2755 ibmvnic_send_crq(adapter, &crq);
2756
Thomas Falcon032c5e82015-12-21 11:26:06 -06002757 crq.query_capability.capability = cpu_to_be16(MAX_TX_SG_ENTRIES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002758 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002759 ibmvnic_send_crq(adapter, &crq);
2760
2761 crq.query_capability.capability = cpu_to_be16(RX_SG_SUPPORTED);
Thomas Falcon901e0402017-02-15 12:17:59 -06002762 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002763 ibmvnic_send_crq(adapter, &crq);
2764
2765 crq.query_capability.capability = cpu_to_be16(OPT_TX_COMP_SUB_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002766 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002767 ibmvnic_send_crq(adapter, &crq);
2768
2769 crq.query_capability.capability = cpu_to_be16(OPT_RX_COMP_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002770 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002771 ibmvnic_send_crq(adapter, &crq);
2772
2773 crq.query_capability.capability =
2774 cpu_to_be16(OPT_RX_BUFADD_Q_PER_RX_COMP_Q);
Thomas Falcon901e0402017-02-15 12:17:59 -06002775 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002776 ibmvnic_send_crq(adapter, &crq);
2777
2778 crq.query_capability.capability =
2779 cpu_to_be16(OPT_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06002780 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002781 ibmvnic_send_crq(adapter, &crq);
2782
2783 crq.query_capability.capability =
2784 cpu_to_be16(OPT_RXBA_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06002785 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002786 ibmvnic_send_crq(adapter, &crq);
2787
2788 crq.query_capability.capability = cpu_to_be16(TX_RX_DESC_REQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06002789 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002790 ibmvnic_send_crq(adapter, &crq);
2791}
2792
2793static void handle_query_ip_offload_rsp(struct ibmvnic_adapter *adapter)
2794{
2795 struct device *dev = &adapter->vdev->dev;
2796 struct ibmvnic_query_ip_offload_buffer *buf = &adapter->ip_offload_buf;
2797 union ibmvnic_crq crq;
2798 int i;
2799
2800 dma_unmap_single(dev, adapter->ip_offload_tok,
2801 sizeof(adapter->ip_offload_buf), DMA_FROM_DEVICE);
2802
2803 netdev_dbg(adapter->netdev, "Query IP Offload Buffer:\n");
2804 for (i = 0; i < (sizeof(adapter->ip_offload_buf) - 1) / 8 + 1; i++)
2805 netdev_dbg(adapter->netdev, "%016lx\n",
2806 ((unsigned long int *)(buf))[i]);
2807
2808 netdev_dbg(adapter->netdev, "ipv4_chksum = %d\n", buf->ipv4_chksum);
2809 netdev_dbg(adapter->netdev, "ipv6_chksum = %d\n", buf->ipv6_chksum);
2810 netdev_dbg(adapter->netdev, "tcp_ipv4_chksum = %d\n",
2811 buf->tcp_ipv4_chksum);
2812 netdev_dbg(adapter->netdev, "tcp_ipv6_chksum = %d\n",
2813 buf->tcp_ipv6_chksum);
2814 netdev_dbg(adapter->netdev, "udp_ipv4_chksum = %d\n",
2815 buf->udp_ipv4_chksum);
2816 netdev_dbg(adapter->netdev, "udp_ipv6_chksum = %d\n",
2817 buf->udp_ipv6_chksum);
2818 netdev_dbg(adapter->netdev, "large_tx_ipv4 = %d\n",
2819 buf->large_tx_ipv4);
2820 netdev_dbg(adapter->netdev, "large_tx_ipv6 = %d\n",
2821 buf->large_tx_ipv6);
2822 netdev_dbg(adapter->netdev, "large_rx_ipv4 = %d\n",
2823 buf->large_rx_ipv4);
2824 netdev_dbg(adapter->netdev, "large_rx_ipv6 = %d\n",
2825 buf->large_rx_ipv6);
2826 netdev_dbg(adapter->netdev, "max_ipv4_hdr_sz = %d\n",
2827 buf->max_ipv4_header_size);
2828 netdev_dbg(adapter->netdev, "max_ipv6_hdr_sz = %d\n",
2829 buf->max_ipv6_header_size);
2830 netdev_dbg(adapter->netdev, "max_tcp_hdr_size = %d\n",
2831 buf->max_tcp_header_size);
2832 netdev_dbg(adapter->netdev, "max_udp_hdr_size = %d\n",
2833 buf->max_udp_header_size);
2834 netdev_dbg(adapter->netdev, "max_large_tx_size = %d\n",
2835 buf->max_large_tx_size);
2836 netdev_dbg(adapter->netdev, "max_large_rx_size = %d\n",
2837 buf->max_large_rx_size);
2838 netdev_dbg(adapter->netdev, "ipv6_ext_hdr = %d\n",
2839 buf->ipv6_extension_header);
2840 netdev_dbg(adapter->netdev, "tcp_pseudosum_req = %d\n",
2841 buf->tcp_pseudosum_req);
2842 netdev_dbg(adapter->netdev, "num_ipv6_ext_hd = %d\n",
2843 buf->num_ipv6_ext_headers);
2844 netdev_dbg(adapter->netdev, "off_ipv6_ext_hd = %d\n",
2845 buf->off_ipv6_ext_headers);
2846
2847 adapter->ip_offload_ctrl_tok =
2848 dma_map_single(dev, &adapter->ip_offload_ctrl,
2849 sizeof(adapter->ip_offload_ctrl), DMA_TO_DEVICE);
2850
2851 if (dma_mapping_error(dev, adapter->ip_offload_ctrl_tok)) {
2852 dev_err(dev, "Couldn't map ip offload control buffer\n");
2853 return;
2854 }
2855
2856 adapter->ip_offload_ctrl.version = cpu_to_be32(INITIAL_VERSION_IOB);
2857 adapter->ip_offload_ctrl.tcp_ipv4_chksum = buf->tcp_ipv4_chksum;
2858 adapter->ip_offload_ctrl.udp_ipv4_chksum = buf->udp_ipv4_chksum;
2859 adapter->ip_offload_ctrl.tcp_ipv6_chksum = buf->tcp_ipv6_chksum;
2860 adapter->ip_offload_ctrl.udp_ipv6_chksum = buf->udp_ipv6_chksum;
2861
2862 /* large_tx/rx disabled for now, additional features needed */
2863 adapter->ip_offload_ctrl.large_tx_ipv4 = 0;
2864 adapter->ip_offload_ctrl.large_tx_ipv6 = 0;
2865 adapter->ip_offload_ctrl.large_rx_ipv4 = 0;
2866 adapter->ip_offload_ctrl.large_rx_ipv6 = 0;
2867
2868 adapter->netdev->features = NETIF_F_GSO;
2869
2870 if (buf->tcp_ipv4_chksum || buf->udp_ipv4_chksum)
2871 adapter->netdev->features |= NETIF_F_IP_CSUM;
2872
2873 if (buf->tcp_ipv6_chksum || buf->udp_ipv6_chksum)
2874 adapter->netdev->features |= NETIF_F_IPV6_CSUM;
2875
Thomas Falcon9be02cd2016-04-01 17:20:35 -05002876 if ((adapter->netdev->features &
2877 (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)))
2878 adapter->netdev->features |= NETIF_F_RXCSUM;
2879
Thomas Falcon032c5e82015-12-21 11:26:06 -06002880 memset(&crq, 0, sizeof(crq));
2881 crq.control_ip_offload.first = IBMVNIC_CRQ_CMD;
2882 crq.control_ip_offload.cmd = CONTROL_IP_OFFLOAD;
2883 crq.control_ip_offload.len =
2884 cpu_to_be32(sizeof(adapter->ip_offload_ctrl));
2885 crq.control_ip_offload.ioba = cpu_to_be32(adapter->ip_offload_ctrl_tok);
2886 ibmvnic_send_crq(adapter, &crq);
2887}
2888
2889static void handle_error_info_rsp(union ibmvnic_crq *crq,
2890 struct ibmvnic_adapter *adapter)
2891{
2892 struct device *dev = &adapter->vdev->dev;
Wei Yongjun96183182016-06-27 20:48:53 +08002893 struct ibmvnic_error_buff *error_buff, *tmp;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002894 unsigned long flags;
2895 bool found = false;
2896 int i;
2897
2898 if (!crq->request_error_rsp.rc.code) {
2899 dev_info(dev, "Request Error Rsp returned with rc=%x\n",
2900 crq->request_error_rsp.rc.code);
2901 return;
2902 }
2903
2904 spin_lock_irqsave(&adapter->error_list_lock, flags);
Wei Yongjun96183182016-06-27 20:48:53 +08002905 list_for_each_entry_safe(error_buff, tmp, &adapter->errors, list)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002906 if (error_buff->error_id == crq->request_error_rsp.error_id) {
2907 found = true;
2908 list_del(&error_buff->list);
2909 break;
2910 }
2911 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
2912
2913 if (!found) {
2914 dev_err(dev, "Couldn't find error id %x\n",
Thomas Falcon75224c92017-02-15 10:33:33 -06002915 be32_to_cpu(crq->request_error_rsp.error_id));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002916 return;
2917 }
2918
2919 dev_err(dev, "Detailed info for error id %x:",
Thomas Falcon75224c92017-02-15 10:33:33 -06002920 be32_to_cpu(crq->request_error_rsp.error_id));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002921
2922 for (i = 0; i < error_buff->len; i++) {
2923 pr_cont("%02x", (int)error_buff->buff[i]);
2924 if (i % 8 == 7)
2925 pr_cont(" ");
2926 }
2927 pr_cont("\n");
2928
2929 dma_unmap_single(dev, error_buff->dma, error_buff->len,
2930 DMA_FROM_DEVICE);
2931 kfree(error_buff->buff);
2932 kfree(error_buff);
2933}
2934
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04002935static void request_error_information(struct ibmvnic_adapter *adapter,
2936 union ibmvnic_crq *err_crq)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002937{
Thomas Falcon032c5e82015-12-21 11:26:06 -06002938 struct device *dev = &adapter->vdev->dev;
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04002939 struct net_device *netdev = adapter->netdev;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002940 struct ibmvnic_error_buff *error_buff;
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04002941 unsigned long timeout = msecs_to_jiffies(30000);
2942 union ibmvnic_crq crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002943 unsigned long flags;
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04002944 int rc, detail_len;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002945
2946 error_buff = kmalloc(sizeof(*error_buff), GFP_ATOMIC);
2947 if (!error_buff)
2948 return;
2949
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04002950 detail_len = be32_to_cpu(err_crq->error_indication.detail_error_sz);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002951 error_buff->buff = kmalloc(detail_len, GFP_ATOMIC);
2952 if (!error_buff->buff) {
2953 kfree(error_buff);
2954 return;
2955 }
2956
2957 error_buff->dma = dma_map_single(dev, error_buff->buff, detail_len,
2958 DMA_FROM_DEVICE);
2959 if (dma_mapping_error(dev, error_buff->dma)) {
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04002960 netdev_err(netdev, "Couldn't map error buffer\n");
Thomas Falcon032c5e82015-12-21 11:26:06 -06002961 kfree(error_buff->buff);
2962 kfree(error_buff);
2963 return;
2964 }
2965
Thomas Falcon032c5e82015-12-21 11:26:06 -06002966 error_buff->len = detail_len;
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04002967 error_buff->error_id = err_crq->error_indication.error_id;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002968
2969 spin_lock_irqsave(&adapter->error_list_lock, flags);
2970 list_add_tail(&error_buff->list, &adapter->errors);
2971 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
2972
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04002973 memset(&crq, 0, sizeof(crq));
2974 crq.request_error_info.first = IBMVNIC_CRQ_CMD;
2975 crq.request_error_info.cmd = REQUEST_ERROR_INFO;
2976 crq.request_error_info.ioba = cpu_to_be32(error_buff->dma);
2977 crq.request_error_info.len = cpu_to_be32(detail_len);
2978 crq.request_error_info.error_id = err_crq->error_indication.error_id;
2979
2980 rc = ibmvnic_send_crq(adapter, &crq);
2981 if (rc) {
2982 netdev_err(netdev, "failed to request error information\n");
2983 goto err_info_fail;
2984 }
2985
2986 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
2987 netdev_err(netdev, "timeout waiting for error information\n");
2988 goto err_info_fail;
2989 }
2990
2991 return;
2992
2993err_info_fail:
2994 spin_lock_irqsave(&adapter->error_list_lock, flags);
2995 list_del(&error_buff->list);
2996 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
2997
2998 kfree(error_buff->buff);
2999 kfree(error_buff);
3000}
3001
3002static void handle_error_indication(union ibmvnic_crq *crq,
3003 struct ibmvnic_adapter *adapter)
3004{
3005 struct device *dev = &adapter->vdev->dev;
3006
3007 dev_err(dev, "Firmware reports %serror id %x, cause %d\n",
3008 crq->error_indication.flags
3009 & IBMVNIC_FATAL_ERROR ? "FATAL " : "",
3010 be32_to_cpu(crq->error_indication.error_id),
3011 be16_to_cpu(crq->error_indication.error_cause));
3012
3013 if (be32_to_cpu(crq->error_indication.error_id))
3014 request_error_information(adapter, crq);
Nathan Fontenoted651a12017-05-03 14:04:38 -04003015
3016 if (crq->error_indication.flags & IBMVNIC_FATAL_ERROR)
3017 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
John Allen8cb31cf2017-05-26 10:30:37 -04003018 else
3019 ibmvnic_reset(adapter, VNIC_RESET_NON_FATAL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003020}
3021
3022static void handle_change_mac_rsp(union ibmvnic_crq *crq,
3023 struct ibmvnic_adapter *adapter)
3024{
3025 struct net_device *netdev = adapter->netdev;
3026 struct device *dev = &adapter->vdev->dev;
3027 long rc;
3028
3029 rc = crq->change_mac_addr_rsp.rc.code;
3030 if (rc) {
3031 dev_err(dev, "Error %ld in CHANGE_MAC_ADDR_RSP\n", rc);
3032 return;
3033 }
3034 memcpy(netdev->dev_addr, &crq->change_mac_addr_rsp.mac_addr[0],
3035 ETH_ALEN);
3036}
3037
3038static void handle_request_cap_rsp(union ibmvnic_crq *crq,
3039 struct ibmvnic_adapter *adapter)
3040{
3041 struct device *dev = &adapter->vdev->dev;
3042 u64 *req_value;
3043 char *name;
3044
Thomas Falcon901e0402017-02-15 12:17:59 -06003045 atomic_dec(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003046 switch (be16_to_cpu(crq->request_capability_rsp.capability)) {
3047 case REQ_TX_QUEUES:
3048 req_value = &adapter->req_tx_queues;
3049 name = "tx";
3050 break;
3051 case REQ_RX_QUEUES:
3052 req_value = &adapter->req_rx_queues;
3053 name = "rx";
3054 break;
3055 case REQ_RX_ADD_QUEUES:
3056 req_value = &adapter->req_rx_add_queues;
3057 name = "rx_add";
3058 break;
3059 case REQ_TX_ENTRIES_PER_SUBCRQ:
3060 req_value = &adapter->req_tx_entries_per_subcrq;
3061 name = "tx_entries_per_subcrq";
3062 break;
3063 case REQ_RX_ADD_ENTRIES_PER_SUBCRQ:
3064 req_value = &adapter->req_rx_add_entries_per_subcrq;
3065 name = "rx_add_entries_per_subcrq";
3066 break;
3067 case REQ_MTU:
3068 req_value = &adapter->req_mtu;
3069 name = "mtu";
3070 break;
3071 case PROMISC_REQUESTED:
3072 req_value = &adapter->promisc;
3073 name = "promisc";
3074 break;
3075 default:
3076 dev_err(dev, "Got invalid cap request rsp %d\n",
3077 crq->request_capability.capability);
3078 return;
3079 }
3080
3081 switch (crq->request_capability_rsp.rc.code) {
3082 case SUCCESS:
3083 break;
3084 case PARTIALSUCCESS:
3085 dev_info(dev, "req=%lld, rsp=%ld in %s queue, retrying.\n",
3086 *req_value,
Thomas Falcon28f4d162017-02-15 10:32:11 -06003087 (long int)be64_to_cpu(crq->request_capability_rsp.
Thomas Falcon032c5e82015-12-21 11:26:06 -06003088 number), name);
Nathan Fontenotb5108882017-03-30 02:49:18 -04003089 release_sub_crqs(adapter);
Thomas Falcon28f4d162017-02-15 10:32:11 -06003090 *req_value = be64_to_cpu(crq->request_capability_rsp.number);
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003091 ibmvnic_send_req_caps(adapter, 1);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003092 return;
3093 default:
3094 dev_err(dev, "Error %d in request cap rsp\n",
3095 crq->request_capability_rsp.rc.code);
3096 return;
3097 }
3098
3099 /* Done receiving requested capabilities, query IP offload support */
Thomas Falcon901e0402017-02-15 12:17:59 -06003100 if (atomic_read(&adapter->running_cap_crqs) == 0) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06003101 union ibmvnic_crq newcrq;
3102 int buf_sz = sizeof(struct ibmvnic_query_ip_offload_buffer);
3103 struct ibmvnic_query_ip_offload_buffer *ip_offload_buf =
3104 &adapter->ip_offload_buf;
3105
Thomas Falcon249168a2017-02-15 12:18:00 -06003106 adapter->wait_capability = false;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003107 adapter->ip_offload_tok = dma_map_single(dev, ip_offload_buf,
3108 buf_sz,
3109 DMA_FROM_DEVICE);
3110
3111 if (dma_mapping_error(dev, adapter->ip_offload_tok)) {
3112 if (!firmware_has_feature(FW_FEATURE_CMO))
3113 dev_err(dev, "Couldn't map offload buffer\n");
3114 return;
3115 }
3116
3117 memset(&newcrq, 0, sizeof(newcrq));
3118 newcrq.query_ip_offload.first = IBMVNIC_CRQ_CMD;
3119 newcrq.query_ip_offload.cmd = QUERY_IP_OFFLOAD;
3120 newcrq.query_ip_offload.len = cpu_to_be32(buf_sz);
3121 newcrq.query_ip_offload.ioba =
3122 cpu_to_be32(adapter->ip_offload_tok);
3123
3124 ibmvnic_send_crq(adapter, &newcrq);
3125 }
3126}
3127
3128static int handle_login_rsp(union ibmvnic_crq *login_rsp_crq,
3129 struct ibmvnic_adapter *adapter)
3130{
3131 struct device *dev = &adapter->vdev->dev;
3132 struct ibmvnic_login_rsp_buffer *login_rsp = adapter->login_rsp_buf;
3133 struct ibmvnic_login_buffer *login = adapter->login_buf;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003134 int i;
3135
3136 dma_unmap_single(dev, adapter->login_buf_token, adapter->login_buf_sz,
3137 DMA_BIDIRECTIONAL);
3138 dma_unmap_single(dev, adapter->login_rsp_buf_token,
3139 adapter->login_rsp_buf_sz, DMA_BIDIRECTIONAL);
3140
John Allen498cd8e2016-04-06 11:49:55 -05003141 /* If the number of queues requested can't be allocated by the
3142 * server, the login response will return with code 1. We will need
3143 * to resend the login buffer with fewer queues requested.
3144 */
3145 if (login_rsp_crq->generic.rc.code) {
3146 adapter->renegotiate = true;
3147 complete(&adapter->init_done);
3148 return 0;
3149 }
3150
Thomas Falcon032c5e82015-12-21 11:26:06 -06003151 netdev_dbg(adapter->netdev, "Login Response Buffer:\n");
3152 for (i = 0; i < (adapter->login_rsp_buf_sz - 1) / 8 + 1; i++) {
3153 netdev_dbg(adapter->netdev, "%016lx\n",
3154 ((unsigned long int *)(adapter->login_rsp_buf))[i]);
3155 }
3156
3157 /* Sanity checks */
3158 if (login->num_txcomp_subcrqs != login_rsp->num_txsubm_subcrqs ||
3159 (be32_to_cpu(login->num_rxcomp_subcrqs) *
3160 adapter->req_rx_add_queues !=
3161 be32_to_cpu(login_rsp->num_rxadd_subcrqs))) {
3162 dev_err(dev, "FATAL: Inconsistent login and login rsp\n");
3163 ibmvnic_remove(adapter->vdev);
3164 return -EIO;
3165 }
3166 complete(&adapter->init_done);
3167
Thomas Falcon032c5e82015-12-21 11:26:06 -06003168 return 0;
3169}
3170
Thomas Falcon032c5e82015-12-21 11:26:06 -06003171static void handle_request_unmap_rsp(union ibmvnic_crq *crq,
3172 struct ibmvnic_adapter *adapter)
3173{
3174 struct device *dev = &adapter->vdev->dev;
3175 long rc;
3176
3177 rc = crq->request_unmap_rsp.rc.code;
3178 if (rc)
3179 dev_err(dev, "Error %ld in REQUEST_UNMAP_RSP\n", rc);
3180}
3181
3182static void handle_query_map_rsp(union ibmvnic_crq *crq,
3183 struct ibmvnic_adapter *adapter)
3184{
3185 struct net_device *netdev = adapter->netdev;
3186 struct device *dev = &adapter->vdev->dev;
3187 long rc;
3188
3189 rc = crq->query_map_rsp.rc.code;
3190 if (rc) {
3191 dev_err(dev, "Error %ld in QUERY_MAP_RSP\n", rc);
3192 return;
3193 }
3194 netdev_dbg(netdev, "page_size = %d\ntot_pages = %d\nfree_pages = %d\n",
3195 crq->query_map_rsp.page_size, crq->query_map_rsp.tot_pages,
3196 crq->query_map_rsp.free_pages);
3197}
3198
3199static void handle_query_cap_rsp(union ibmvnic_crq *crq,
3200 struct ibmvnic_adapter *adapter)
3201{
3202 struct net_device *netdev = adapter->netdev;
3203 struct device *dev = &adapter->vdev->dev;
3204 long rc;
3205
Thomas Falcon901e0402017-02-15 12:17:59 -06003206 atomic_dec(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003207 netdev_dbg(netdev, "Outstanding queries: %d\n",
Thomas Falcon901e0402017-02-15 12:17:59 -06003208 atomic_read(&adapter->running_cap_crqs));
Thomas Falcon032c5e82015-12-21 11:26:06 -06003209 rc = crq->query_capability.rc.code;
3210 if (rc) {
3211 dev_err(dev, "Error %ld in QUERY_CAP_RSP\n", rc);
3212 goto out;
3213 }
3214
3215 switch (be16_to_cpu(crq->query_capability.capability)) {
3216 case MIN_TX_QUEUES:
3217 adapter->min_tx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003218 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003219 netdev_dbg(netdev, "min_tx_queues = %lld\n",
3220 adapter->min_tx_queues);
3221 break;
3222 case MIN_RX_QUEUES:
3223 adapter->min_rx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003224 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003225 netdev_dbg(netdev, "min_rx_queues = %lld\n",
3226 adapter->min_rx_queues);
3227 break;
3228 case MIN_RX_ADD_QUEUES:
3229 adapter->min_rx_add_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003230 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003231 netdev_dbg(netdev, "min_rx_add_queues = %lld\n",
3232 adapter->min_rx_add_queues);
3233 break;
3234 case MAX_TX_QUEUES:
3235 adapter->max_tx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003236 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003237 netdev_dbg(netdev, "max_tx_queues = %lld\n",
3238 adapter->max_tx_queues);
3239 break;
3240 case MAX_RX_QUEUES:
3241 adapter->max_rx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003242 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003243 netdev_dbg(netdev, "max_rx_queues = %lld\n",
3244 adapter->max_rx_queues);
3245 break;
3246 case MAX_RX_ADD_QUEUES:
3247 adapter->max_rx_add_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003248 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003249 netdev_dbg(netdev, "max_rx_add_queues = %lld\n",
3250 adapter->max_rx_add_queues);
3251 break;
3252 case MIN_TX_ENTRIES_PER_SUBCRQ:
3253 adapter->min_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06003254 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003255 netdev_dbg(netdev, "min_tx_entries_per_subcrq = %lld\n",
3256 adapter->min_tx_entries_per_subcrq);
3257 break;
3258 case MIN_RX_ADD_ENTRIES_PER_SUBCRQ:
3259 adapter->min_rx_add_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06003260 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003261 netdev_dbg(netdev, "min_rx_add_entrs_per_subcrq = %lld\n",
3262 adapter->min_rx_add_entries_per_subcrq);
3263 break;
3264 case MAX_TX_ENTRIES_PER_SUBCRQ:
3265 adapter->max_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06003266 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003267 netdev_dbg(netdev, "max_tx_entries_per_subcrq = %lld\n",
3268 adapter->max_tx_entries_per_subcrq);
3269 break;
3270 case MAX_RX_ADD_ENTRIES_PER_SUBCRQ:
3271 adapter->max_rx_add_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06003272 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003273 netdev_dbg(netdev, "max_rx_add_entrs_per_subcrq = %lld\n",
3274 adapter->max_rx_add_entries_per_subcrq);
3275 break;
3276 case TCP_IP_OFFLOAD:
3277 adapter->tcp_ip_offload =
Thomas Falconde89e852016-03-01 10:20:09 -06003278 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003279 netdev_dbg(netdev, "tcp_ip_offload = %lld\n",
3280 adapter->tcp_ip_offload);
3281 break;
3282 case PROMISC_SUPPORTED:
3283 adapter->promisc_supported =
Thomas Falconde89e852016-03-01 10:20:09 -06003284 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003285 netdev_dbg(netdev, "promisc_supported = %lld\n",
3286 adapter->promisc_supported);
3287 break;
3288 case MIN_MTU:
Thomas Falconde89e852016-03-01 10:20:09 -06003289 adapter->min_mtu = be64_to_cpu(crq->query_capability.number);
Thomas Falconf39f0d12017-02-14 10:22:59 -06003290 netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003291 netdev_dbg(netdev, "min_mtu = %lld\n", adapter->min_mtu);
3292 break;
3293 case MAX_MTU:
Thomas Falconde89e852016-03-01 10:20:09 -06003294 adapter->max_mtu = be64_to_cpu(crq->query_capability.number);
Thomas Falconf39f0d12017-02-14 10:22:59 -06003295 netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003296 netdev_dbg(netdev, "max_mtu = %lld\n", adapter->max_mtu);
3297 break;
3298 case MAX_MULTICAST_FILTERS:
3299 adapter->max_multicast_filters =
Thomas Falconde89e852016-03-01 10:20:09 -06003300 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003301 netdev_dbg(netdev, "max_multicast_filters = %lld\n",
3302 adapter->max_multicast_filters);
3303 break;
3304 case VLAN_HEADER_INSERTION:
3305 adapter->vlan_header_insertion =
Thomas Falconde89e852016-03-01 10:20:09 -06003306 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003307 if (adapter->vlan_header_insertion)
3308 netdev->features |= NETIF_F_HW_VLAN_STAG_TX;
3309 netdev_dbg(netdev, "vlan_header_insertion = %lld\n",
3310 adapter->vlan_header_insertion);
3311 break;
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04003312 case RX_VLAN_HEADER_INSERTION:
3313 adapter->rx_vlan_header_insertion =
3314 be64_to_cpu(crq->query_capability.number);
3315 netdev_dbg(netdev, "rx_vlan_header_insertion = %lld\n",
3316 adapter->rx_vlan_header_insertion);
3317 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003318 case MAX_TX_SG_ENTRIES:
3319 adapter->max_tx_sg_entries =
Thomas Falconde89e852016-03-01 10:20:09 -06003320 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003321 netdev_dbg(netdev, "max_tx_sg_entries = %lld\n",
3322 adapter->max_tx_sg_entries);
3323 break;
3324 case RX_SG_SUPPORTED:
3325 adapter->rx_sg_supported =
Thomas Falconde89e852016-03-01 10:20:09 -06003326 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003327 netdev_dbg(netdev, "rx_sg_supported = %lld\n",
3328 adapter->rx_sg_supported);
3329 break;
3330 case OPT_TX_COMP_SUB_QUEUES:
3331 adapter->opt_tx_comp_sub_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003332 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003333 netdev_dbg(netdev, "opt_tx_comp_sub_queues = %lld\n",
3334 adapter->opt_tx_comp_sub_queues);
3335 break;
3336 case OPT_RX_COMP_QUEUES:
3337 adapter->opt_rx_comp_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003338 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003339 netdev_dbg(netdev, "opt_rx_comp_queues = %lld\n",
3340 adapter->opt_rx_comp_queues);
3341 break;
3342 case OPT_RX_BUFADD_Q_PER_RX_COMP_Q:
3343 adapter->opt_rx_bufadd_q_per_rx_comp_q =
Thomas Falconde89e852016-03-01 10:20:09 -06003344 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003345 netdev_dbg(netdev, "opt_rx_bufadd_q_per_rx_comp_q = %lld\n",
3346 adapter->opt_rx_bufadd_q_per_rx_comp_q);
3347 break;
3348 case OPT_TX_ENTRIES_PER_SUBCRQ:
3349 adapter->opt_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06003350 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003351 netdev_dbg(netdev, "opt_tx_entries_per_subcrq = %lld\n",
3352 adapter->opt_tx_entries_per_subcrq);
3353 break;
3354 case OPT_RXBA_ENTRIES_PER_SUBCRQ:
3355 adapter->opt_rxba_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06003356 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003357 netdev_dbg(netdev, "opt_rxba_entries_per_subcrq = %lld\n",
3358 adapter->opt_rxba_entries_per_subcrq);
3359 break;
3360 case TX_RX_DESC_REQ:
3361 adapter->tx_rx_desc_req = crq->query_capability.number;
3362 netdev_dbg(netdev, "tx_rx_desc_req = %llx\n",
3363 adapter->tx_rx_desc_req);
3364 break;
3365
3366 default:
3367 netdev_err(netdev, "Got invalid cap rsp %d\n",
3368 crq->query_capability.capability);
3369 }
3370
3371out:
Thomas Falcon249168a2017-02-15 12:18:00 -06003372 if (atomic_read(&adapter->running_cap_crqs) == 0) {
3373 adapter->wait_capability = false;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003374 ibmvnic_send_req_caps(adapter, 0);
Thomas Falcon249168a2017-02-15 12:18:00 -06003375 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06003376}
3377
Thomas Falcon032c5e82015-12-21 11:26:06 -06003378static void ibmvnic_handle_crq(union ibmvnic_crq *crq,
3379 struct ibmvnic_adapter *adapter)
3380{
3381 struct ibmvnic_generic_crq *gen_crq = &crq->generic;
3382 struct net_device *netdev = adapter->netdev;
3383 struct device *dev = &adapter->vdev->dev;
Murilo Fossa Vicentini993a82b2017-04-19 13:44:35 -04003384 u64 *u64_crq = (u64 *)crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003385 long rc;
3386
3387 netdev_dbg(netdev, "Handling CRQ: %016lx %016lx\n",
Murilo Fossa Vicentini993a82b2017-04-19 13:44:35 -04003388 (unsigned long int)cpu_to_be64(u64_crq[0]),
3389 (unsigned long int)cpu_to_be64(u64_crq[1]));
Thomas Falcon032c5e82015-12-21 11:26:06 -06003390 switch (gen_crq->first) {
3391 case IBMVNIC_CRQ_INIT_RSP:
3392 switch (gen_crq->cmd) {
3393 case IBMVNIC_CRQ_INIT:
3394 dev_info(dev, "Partner initialized\n");
John Allen017892c12017-05-26 10:30:19 -04003395 adapter->from_passive_init = true;
3396 complete(&adapter->init_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003397 break;
3398 case IBMVNIC_CRQ_INIT_COMPLETE:
3399 dev_info(dev, "Partner initialization complete\n");
3400 send_version_xchg(adapter);
3401 break;
3402 default:
3403 dev_err(dev, "Unknown crq cmd: %d\n", gen_crq->cmd);
3404 }
3405 return;
3406 case IBMVNIC_CRQ_XPORT_EVENT:
Nathan Fontenoted651a12017-05-03 14:04:38 -04003407 netif_carrier_off(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003408 if (gen_crq->cmd == IBMVNIC_PARTITION_MIGRATED) {
Nathan Fontenoted651a12017-05-03 14:04:38 -04003409 dev_info(dev, "Migrated, re-enabling adapter\n");
3410 ibmvnic_reset(adapter, VNIC_RESET_MOBILITY);
Thomas Falcondfad09a2016-08-18 11:37:51 -05003411 } else if (gen_crq->cmd == IBMVNIC_DEVICE_FAILOVER) {
3412 dev_info(dev, "Backing device failover detected\n");
Nathan Fontenoted651a12017-05-03 14:04:38 -04003413 ibmvnic_reset(adapter, VNIC_RESET_FAILOVER);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003414 } else {
3415 /* The adapter lost the connection */
3416 dev_err(dev, "Virtual Adapter failed (rc=%d)\n",
3417 gen_crq->cmd);
Nathan Fontenoted651a12017-05-03 14:04:38 -04003418 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003419 }
3420 return;
3421 case IBMVNIC_CRQ_CMD_RSP:
3422 break;
3423 default:
3424 dev_err(dev, "Got an invalid msg type 0x%02x\n",
3425 gen_crq->first);
3426 return;
3427 }
3428
3429 switch (gen_crq->cmd) {
3430 case VERSION_EXCHANGE_RSP:
3431 rc = crq->version_exchange_rsp.rc.code;
3432 if (rc) {
3433 dev_err(dev, "Error %ld in VERSION_EXCHG_RSP\n", rc);
3434 break;
3435 }
3436 dev_info(dev, "Partner protocol version is %d\n",
3437 crq->version_exchange_rsp.version);
3438 if (be16_to_cpu(crq->version_exchange_rsp.version) <
3439 ibmvnic_version)
3440 ibmvnic_version =
3441 be16_to_cpu(crq->version_exchange_rsp.version);
3442 send_cap_queries(adapter);
3443 break;
3444 case QUERY_CAPABILITY_RSP:
3445 handle_query_cap_rsp(crq, adapter);
3446 break;
3447 case QUERY_MAP_RSP:
3448 handle_query_map_rsp(crq, adapter);
3449 break;
3450 case REQUEST_MAP_RSP:
Thomas Falconf3be0cb2017-06-21 14:53:01 -05003451 adapter->fw_done_rc = crq->request_map_rsp.rc.code;
3452 complete(&adapter->fw_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003453 break;
3454 case REQUEST_UNMAP_RSP:
3455 handle_request_unmap_rsp(crq, adapter);
3456 break;
3457 case REQUEST_CAPABILITY_RSP:
3458 handle_request_cap_rsp(crq, adapter);
3459 break;
3460 case LOGIN_RSP:
3461 netdev_dbg(netdev, "Got Login Response\n");
3462 handle_login_rsp(crq, adapter);
3463 break;
3464 case LOGICAL_LINK_STATE_RSP:
Nathan Fontenot53da09e2017-04-21 15:39:04 -04003465 netdev_dbg(netdev,
3466 "Got Logical Link State Response, state: %d rc: %d\n",
3467 crq->logical_link_state_rsp.link_state,
3468 crq->logical_link_state_rsp.rc.code);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003469 adapter->logical_link_state =
3470 crq->logical_link_state_rsp.link_state;
Nathan Fontenot53da09e2017-04-21 15:39:04 -04003471 adapter->init_done_rc = crq->logical_link_state_rsp.rc.code;
3472 complete(&adapter->init_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003473 break;
3474 case LINK_STATE_INDICATION:
3475 netdev_dbg(netdev, "Got Logical Link State Indication\n");
3476 adapter->phys_link_state =
3477 crq->link_state_indication.phys_link_state;
3478 adapter->logical_link_state =
3479 crq->link_state_indication.logical_link_state;
3480 break;
3481 case CHANGE_MAC_ADDR_RSP:
3482 netdev_dbg(netdev, "Got MAC address change Response\n");
3483 handle_change_mac_rsp(crq, adapter);
3484 break;
3485 case ERROR_INDICATION:
3486 netdev_dbg(netdev, "Got Error Indication\n");
3487 handle_error_indication(crq, adapter);
3488 break;
3489 case REQUEST_ERROR_RSP:
3490 netdev_dbg(netdev, "Got Error Detail Response\n");
3491 handle_error_info_rsp(crq, adapter);
3492 break;
3493 case REQUEST_STATISTICS_RSP:
3494 netdev_dbg(netdev, "Got Statistics Response\n");
3495 complete(&adapter->stats_done);
3496 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003497 case QUERY_IP_OFFLOAD_RSP:
3498 netdev_dbg(netdev, "Got Query IP offload Response\n");
3499 handle_query_ip_offload_rsp(adapter);
3500 break;
3501 case MULTICAST_CTRL_RSP:
3502 netdev_dbg(netdev, "Got multicast control Response\n");
3503 break;
3504 case CONTROL_IP_OFFLOAD_RSP:
3505 netdev_dbg(netdev, "Got Control IP offload Response\n");
3506 dma_unmap_single(dev, adapter->ip_offload_ctrl_tok,
3507 sizeof(adapter->ip_offload_ctrl),
3508 DMA_TO_DEVICE);
John Allenbd0b6722017-03-17 17:13:40 -05003509 complete(&adapter->init_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003510 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003511 case COLLECT_FW_TRACE_RSP:
3512 netdev_dbg(netdev, "Got Collect firmware trace Response\n");
3513 complete(&adapter->fw_done);
3514 break;
3515 default:
3516 netdev_err(netdev, "Got an invalid cmd type 0x%02x\n",
3517 gen_crq->cmd);
3518 }
3519}
3520
3521static irqreturn_t ibmvnic_interrupt(int irq, void *instance)
3522{
3523 struct ibmvnic_adapter *adapter = instance;
Thomas Falcon6c267b32017-02-15 12:17:58 -06003524
Thomas Falcon6c267b32017-02-15 12:17:58 -06003525 tasklet_schedule(&adapter->tasklet);
Thomas Falcon6c267b32017-02-15 12:17:58 -06003526 return IRQ_HANDLED;
3527}
3528
3529static void ibmvnic_tasklet(void *data)
3530{
3531 struct ibmvnic_adapter *adapter = data;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003532 struct ibmvnic_crq_queue *queue = &adapter->crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003533 union ibmvnic_crq *crq;
3534 unsigned long flags;
3535 bool done = false;
3536
3537 spin_lock_irqsave(&queue->lock, flags);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003538 while (!done) {
3539 /* Pull all the valid messages off the CRQ */
3540 while ((crq = ibmvnic_next_crq(adapter)) != NULL) {
3541 ibmvnic_handle_crq(crq, adapter);
3542 crq->generic.first = 0;
3543 }
Brian Kinged7ecbf2017-04-19 13:44:53 -04003544
3545 /* remain in tasklet until all
3546 * capabilities responses are received
3547 */
3548 if (!adapter->wait_capability)
3549 done = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003550 }
Thomas Falcon249168a2017-02-15 12:18:00 -06003551 /* if capabilities CRQ's were sent in this tasklet, the following
3552 * tasklet must wait until all responses are received
3553 */
3554 if (atomic_read(&adapter->running_cap_crqs) != 0)
3555 adapter->wait_capability = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003556 spin_unlock_irqrestore(&queue->lock, flags);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003557}
3558
3559static int ibmvnic_reenable_crq_queue(struct ibmvnic_adapter *adapter)
3560{
3561 struct vio_dev *vdev = adapter->vdev;
3562 int rc;
3563
3564 do {
3565 rc = plpar_hcall_norets(H_ENABLE_CRQ, vdev->unit_address);
3566 } while (rc == H_IN_PROGRESS || rc == H_BUSY || H_IS_LONG_BUSY(rc));
3567
3568 if (rc)
3569 dev_err(&vdev->dev, "Error enabling adapter (rc=%d)\n", rc);
3570
3571 return rc;
3572}
3573
3574static int ibmvnic_reset_crq(struct ibmvnic_adapter *adapter)
3575{
3576 struct ibmvnic_crq_queue *crq = &adapter->crq;
3577 struct device *dev = &adapter->vdev->dev;
3578 struct vio_dev *vdev = adapter->vdev;
3579 int rc;
3580
3581 /* Close the CRQ */
3582 do {
3583 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
3584 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
3585
3586 /* Clean out the queue */
3587 memset(crq->msgs, 0, PAGE_SIZE);
3588 crq->cur = 0;
3589
3590 /* And re-open it again */
3591 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
3592 crq->msg_token, PAGE_SIZE);
3593
3594 if (rc == H_CLOSED)
3595 /* Adapter is good, but other end is not ready */
3596 dev_warn(dev, "Partner adapter not ready\n");
3597 else if (rc != 0)
3598 dev_warn(dev, "Couldn't register crq (rc=%d)\n", rc);
3599
3600 return rc;
3601}
3602
Nathan Fontenotf9928872017-03-30 02:48:54 -04003603static void release_crq_queue(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003604{
3605 struct ibmvnic_crq_queue *crq = &adapter->crq;
3606 struct vio_dev *vdev = adapter->vdev;
3607 long rc;
3608
Nathan Fontenotf9928872017-03-30 02:48:54 -04003609 if (!crq->msgs)
3610 return;
3611
Thomas Falcon032c5e82015-12-21 11:26:06 -06003612 netdev_dbg(adapter->netdev, "Releasing CRQ\n");
3613 free_irq(vdev->irq, adapter);
Thomas Falcon6c267b32017-02-15 12:17:58 -06003614 tasklet_kill(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003615 do {
3616 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
3617 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
3618
3619 dma_unmap_single(&vdev->dev, crq->msg_token, PAGE_SIZE,
3620 DMA_BIDIRECTIONAL);
3621 free_page((unsigned long)crq->msgs);
Nathan Fontenotf9928872017-03-30 02:48:54 -04003622 crq->msgs = NULL;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003623}
3624
Nathan Fontenotf9928872017-03-30 02:48:54 -04003625static int init_crq_queue(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003626{
3627 struct ibmvnic_crq_queue *crq = &adapter->crq;
3628 struct device *dev = &adapter->vdev->dev;
3629 struct vio_dev *vdev = adapter->vdev;
3630 int rc, retrc = -ENOMEM;
3631
Nathan Fontenotf9928872017-03-30 02:48:54 -04003632 if (crq->msgs)
3633 return 0;
3634
Thomas Falcon032c5e82015-12-21 11:26:06 -06003635 crq->msgs = (union ibmvnic_crq *)get_zeroed_page(GFP_KERNEL);
3636 /* Should we allocate more than one page? */
3637
3638 if (!crq->msgs)
3639 return -ENOMEM;
3640
3641 crq->size = PAGE_SIZE / sizeof(*crq->msgs);
3642 crq->msg_token = dma_map_single(dev, crq->msgs, PAGE_SIZE,
3643 DMA_BIDIRECTIONAL);
3644 if (dma_mapping_error(dev, crq->msg_token))
3645 goto map_failed;
3646
3647 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
3648 crq->msg_token, PAGE_SIZE);
3649
3650 if (rc == H_RESOURCE)
3651 /* maybe kexecing and resource is busy. try a reset */
3652 rc = ibmvnic_reset_crq(adapter);
3653 retrc = rc;
3654
3655 if (rc == H_CLOSED) {
3656 dev_warn(dev, "Partner adapter not ready\n");
3657 } else if (rc) {
3658 dev_warn(dev, "Error %d opening adapter\n", rc);
3659 goto reg_crq_failed;
3660 }
3661
3662 retrc = 0;
3663
Thomas Falcon6c267b32017-02-15 12:17:58 -06003664 tasklet_init(&adapter->tasklet, (void *)ibmvnic_tasklet,
3665 (unsigned long)adapter);
3666
Thomas Falcon032c5e82015-12-21 11:26:06 -06003667 netdev_dbg(adapter->netdev, "registering irq 0x%x\n", vdev->irq);
3668 rc = request_irq(vdev->irq, ibmvnic_interrupt, 0, IBMVNIC_NAME,
3669 adapter);
3670 if (rc) {
3671 dev_err(dev, "Couldn't register irq 0x%x. rc=%d\n",
3672 vdev->irq, rc);
3673 goto req_irq_failed;
3674 }
3675
3676 rc = vio_enable_interrupts(vdev);
3677 if (rc) {
3678 dev_err(dev, "Error %d enabling interrupts\n", rc);
3679 goto req_irq_failed;
3680 }
3681
3682 crq->cur = 0;
3683 spin_lock_init(&crq->lock);
3684
3685 return retrc;
3686
3687req_irq_failed:
Thomas Falcon6c267b32017-02-15 12:17:58 -06003688 tasklet_kill(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003689 do {
3690 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
3691 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
3692reg_crq_failed:
3693 dma_unmap_single(dev, crq->msg_token, PAGE_SIZE, DMA_BIDIRECTIONAL);
3694map_failed:
3695 free_page((unsigned long)crq->msgs);
Nathan Fontenotf9928872017-03-30 02:48:54 -04003696 crq->msgs = NULL;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003697 return retrc;
3698}
3699
John Allenf6ef6402017-03-17 17:13:42 -05003700static int ibmvnic_init(struct ibmvnic_adapter *adapter)
3701{
3702 struct device *dev = &adapter->vdev->dev;
3703 unsigned long timeout = msecs_to_jiffies(30000);
John Allenf6ef6402017-03-17 17:13:42 -05003704 int rc;
3705
Nathan Fontenot28cde752017-05-26 10:31:00 -04003706 if (adapter->resetting) {
3707 rc = ibmvnic_reset_crq(adapter);
3708 if (!rc)
3709 rc = vio_enable_interrupts(adapter->vdev);
3710 } else {
3711 rc = init_crq_queue(adapter);
3712 }
3713
John Allenf6ef6402017-03-17 17:13:42 -05003714 if (rc) {
3715 dev_err(dev, "Couldn't initialize crq. rc=%d\n", rc);
3716 return rc;
3717 }
3718
John Allen017892c12017-05-26 10:30:19 -04003719 adapter->from_passive_init = false;
3720
John Allenf6ef6402017-03-17 17:13:42 -05003721 init_completion(&adapter->init_done);
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04003722 adapter->init_done_rc = 0;
John Allenf6ef6402017-03-17 17:13:42 -05003723 ibmvnic_send_crq_init(adapter);
3724 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
3725 dev_err(dev, "Initialization sequence timed out\n");
John Allen017892c12017-05-26 10:30:19 -04003726 return -1;
3727 }
3728
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04003729 if (adapter->init_done_rc) {
3730 release_crq_queue(adapter);
3731 return adapter->init_done_rc;
3732 }
3733
John Allen017892c12017-05-26 10:30:19 -04003734 if (adapter->from_passive_init) {
3735 adapter->state = VNIC_OPEN;
3736 adapter->from_passive_init = false;
John Allenf6ef6402017-03-17 17:13:42 -05003737 return -1;
3738 }
3739
Nathan Fontenot57a49432017-05-26 10:31:12 -04003740 if (adapter->resetting)
3741 rc = reset_sub_crq_queues(adapter);
3742 else
3743 rc = init_sub_crqs(adapter);
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04003744 if (rc) {
3745 dev_err(dev, "Initialization of sub crqs failed\n");
3746 release_crq_queue(adapter);
Thomas Falcon5df969c2017-06-28 19:55:54 -05003747 return rc;
3748 }
3749
3750 rc = init_sub_crq_irqs(adapter);
3751 if (rc) {
3752 dev_err(dev, "Failed to initialize sub crq irqs\n");
3753 release_crq_queue(adapter);
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04003754 }
3755
3756 return rc;
John Allenf6ef6402017-03-17 17:13:42 -05003757}
3758
Thomas Falcon40c9db82017-06-12 12:35:04 -05003759static struct device_attribute dev_attr_failover;
3760
Thomas Falcon032c5e82015-12-21 11:26:06 -06003761static int ibmvnic_probe(struct vio_dev *dev, const struct vio_device_id *id)
3762{
3763 struct ibmvnic_adapter *adapter;
3764 struct net_device *netdev;
3765 unsigned char *mac_addr_p;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003766 int rc;
3767
3768 dev_dbg(&dev->dev, "entering ibmvnic_probe for UA 0x%x\n",
3769 dev->unit_address);
3770
3771 mac_addr_p = (unsigned char *)vio_get_attribute(dev,
3772 VETH_MAC_ADDR, NULL);
3773 if (!mac_addr_p) {
3774 dev_err(&dev->dev,
3775 "(%s:%3.3d) ERROR: Can't find MAC_ADDR attribute\n",
3776 __FILE__, __LINE__);
3777 return 0;
3778 }
3779
3780 netdev = alloc_etherdev_mq(sizeof(struct ibmvnic_adapter),
3781 IBMVNIC_MAX_TX_QUEUES);
3782 if (!netdev)
3783 return -ENOMEM;
3784
3785 adapter = netdev_priv(netdev);
Nathan Fontenot90c80142017-05-03 14:04:32 -04003786 adapter->state = VNIC_PROBING;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003787 dev_set_drvdata(&dev->dev, netdev);
3788 adapter->vdev = dev;
3789 adapter->netdev = netdev;
3790
3791 ether_addr_copy(adapter->mac_addr, mac_addr_p);
3792 ether_addr_copy(netdev->dev_addr, adapter->mac_addr);
3793 netdev->irq = dev->irq;
3794 netdev->netdev_ops = &ibmvnic_netdev_ops;
3795 netdev->ethtool_ops = &ibmvnic_ethtool_ops;
3796 SET_NETDEV_DEV(netdev, &dev->dev);
3797
3798 spin_lock_init(&adapter->stats_lock);
3799
Thomas Falcon032c5e82015-12-21 11:26:06 -06003800 INIT_LIST_HEAD(&adapter->errors);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003801 spin_lock_init(&adapter->error_list_lock);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003802
Nathan Fontenoted651a12017-05-03 14:04:38 -04003803 INIT_WORK(&adapter->ibmvnic_reset, __ibmvnic_reset);
3804 INIT_LIST_HEAD(&adapter->rwi_list);
3805 mutex_init(&adapter->reset_lock);
3806 mutex_init(&adapter->rwi_lock);
3807 adapter->resetting = false;
3808
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04003809 do {
3810 rc = ibmvnic_init(adapter);
Nathan Fontenot6d659232017-06-21 15:41:02 -05003811 if (rc && rc != EAGAIN) {
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04003812 free_netdev(netdev);
3813 return rc;
3814 }
3815 } while (rc == EAGAIN);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003816
Thomas Falconf39f0d12017-02-14 10:22:59 -06003817 netdev->mtu = adapter->req_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003818
Thomas Falcon40c9db82017-06-12 12:35:04 -05003819 rc = device_create_file(&dev->dev, &dev_attr_failover);
3820 if (rc) {
3821 free_netdev(netdev);
3822 return rc;
3823 }
3824
Thomas Falcon032c5e82015-12-21 11:26:06 -06003825 rc = register_netdev(netdev);
3826 if (rc) {
3827 dev_err(&dev->dev, "failed to register netdev rc=%d\n", rc);
Thomas Falcon40c9db82017-06-12 12:35:04 -05003828 device_remove_file(&dev->dev, &dev_attr_failover);
John Allenf6ef6402017-03-17 17:13:42 -05003829 free_netdev(netdev);
3830 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003831 }
3832 dev_info(&dev->dev, "ibmvnic registered\n");
3833
Nathan Fontenot90c80142017-05-03 14:04:32 -04003834 adapter->state = VNIC_PROBED;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003835 return 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003836}
3837
3838static int ibmvnic_remove(struct vio_dev *dev)
3839{
3840 struct net_device *netdev = dev_get_drvdata(&dev->dev);
Nathan Fontenot37489052017-04-19 13:45:04 -04003841 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003842
Nathan Fontenot90c80142017-05-03 14:04:32 -04003843 adapter->state = VNIC_REMOVING;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003844 unregister_netdev(netdev);
Nathan Fontenoted651a12017-05-03 14:04:38 -04003845 mutex_lock(&adapter->reset_lock);
Nathan Fontenot37489052017-04-19 13:45:04 -04003846
3847 release_resources(adapter);
3848 release_sub_crqs(adapter);
3849 release_crq_queue(adapter);
3850
Nathan Fontenot90c80142017-05-03 14:04:32 -04003851 adapter->state = VNIC_REMOVED;
3852
Nathan Fontenoted651a12017-05-03 14:04:38 -04003853 mutex_unlock(&adapter->reset_lock);
Thomas Falcon40c9db82017-06-12 12:35:04 -05003854 device_remove_file(&dev->dev, &dev_attr_failover);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003855 free_netdev(netdev);
3856 dev_set_drvdata(&dev->dev, NULL);
3857
3858 return 0;
3859}
3860
Thomas Falcon40c9db82017-06-12 12:35:04 -05003861static ssize_t failover_store(struct device *dev, struct device_attribute *attr,
3862 const char *buf, size_t count)
3863{
3864 struct net_device *netdev = dev_get_drvdata(dev);
3865 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
3866 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
3867 __be64 session_token;
3868 long rc;
3869
3870 if (!sysfs_streq(buf, "1"))
3871 return -EINVAL;
3872
3873 rc = plpar_hcall(H_VIOCTL, retbuf, adapter->vdev->unit_address,
3874 H_GET_SESSION_TOKEN, 0, 0, 0);
3875 if (rc) {
3876 netdev_err(netdev, "Couldn't retrieve session token, rc %ld\n",
3877 rc);
3878 return -EINVAL;
3879 }
3880
3881 session_token = (__be64)retbuf[0];
3882 netdev_dbg(netdev, "Initiating client failover, session id %llx\n",
3883 be64_to_cpu(session_token));
3884 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
3885 H_SESSION_ERR_DETECTED, session_token, 0, 0);
3886 if (rc) {
3887 netdev_err(netdev, "Client initiated failover failed, rc %ld\n",
3888 rc);
3889 return -EINVAL;
3890 }
3891
3892 return count;
3893}
3894
3895static DEVICE_ATTR(failover, 0200, NULL, failover_store);
3896
Thomas Falcon032c5e82015-12-21 11:26:06 -06003897static unsigned long ibmvnic_get_desired_dma(struct vio_dev *vdev)
3898{
3899 struct net_device *netdev = dev_get_drvdata(&vdev->dev);
3900 struct ibmvnic_adapter *adapter;
3901 struct iommu_table *tbl;
3902 unsigned long ret = 0;
3903 int i;
3904
3905 tbl = get_iommu_table_base(&vdev->dev);
3906
3907 /* netdev inits at probe time along with the structures we need below*/
3908 if (!netdev)
3909 return IOMMU_PAGE_ALIGN(IBMVNIC_IO_ENTITLEMENT_DEFAULT, tbl);
3910
3911 adapter = netdev_priv(netdev);
3912
3913 ret += PAGE_SIZE; /* the crq message queue */
Thomas Falcon032c5e82015-12-21 11:26:06 -06003914 ret += IOMMU_PAGE_ALIGN(sizeof(struct ibmvnic_statistics), tbl);
3915
3916 for (i = 0; i < adapter->req_tx_queues + adapter->req_rx_queues; i++)
3917 ret += 4 * PAGE_SIZE; /* the scrq message queue */
3918
3919 for (i = 0; i < be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
3920 i++)
3921 ret += adapter->rx_pool[i].size *
3922 IOMMU_PAGE_ALIGN(adapter->rx_pool[i].buff_size, tbl);
3923
3924 return ret;
3925}
3926
3927static int ibmvnic_resume(struct device *dev)
3928{
3929 struct net_device *netdev = dev_get_drvdata(dev);
3930 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
3931 int i;
3932
John Allencb89ba22017-06-19 11:27:53 -05003933 if (adapter->state != VNIC_OPEN)
3934 return 0;
3935
John Allena2488782017-07-24 13:26:06 -05003936 tasklet_schedule(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003937
3938 return 0;
3939}
3940
3941static struct vio_device_id ibmvnic_device_table[] = {
3942 {"network", "IBM,vnic"},
3943 {"", "" }
3944};
3945MODULE_DEVICE_TABLE(vio, ibmvnic_device_table);
3946
3947static const struct dev_pm_ops ibmvnic_pm_ops = {
3948 .resume = ibmvnic_resume
3949};
3950
3951static struct vio_driver ibmvnic_driver = {
3952 .id_table = ibmvnic_device_table,
3953 .probe = ibmvnic_probe,
3954 .remove = ibmvnic_remove,
3955 .get_desired_dma = ibmvnic_get_desired_dma,
3956 .name = ibmvnic_driver_name,
3957 .pm = &ibmvnic_pm_ops,
3958};
3959
3960/* module functions */
3961static int __init ibmvnic_module_init(void)
3962{
3963 pr_info("%s: %s %s\n", ibmvnic_driver_name, ibmvnic_driver_string,
3964 IBMVNIC_DRIVER_VERSION);
3965
3966 return vio_register_driver(&ibmvnic_driver);
3967}
3968
3969static void __exit ibmvnic_module_exit(void)
3970{
3971 vio_unregister_driver(&ibmvnic_driver);
3972}
3973
3974module_init(ibmvnic_module_init);
3975module_exit(ibmvnic_module_exit);