blob: ea0ff2841a1f5b8acf8d2935899a8fc162ee7a03 [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++)
John Allen52da5c12017-08-02 16:45:28 -05001810 data[i] = be64_to_cpu(IBMVNIC_GET_STAT(adapter,
1811 ibmvnic_stats[i].offset));
John Allen3d52b592017-08-02 16:44:14 -05001812
1813 for (j = 0; j < adapter->req_tx_queues; j++) {
1814 data[i] = adapter->tx_stats_buffers[j].packets;
1815 i++;
1816 data[i] = adapter->tx_stats_buffers[j].bytes;
1817 i++;
1818 data[i] = adapter->tx_stats_buffers[j].dropped_packets;
1819 i++;
1820 }
1821
1822 for (j = 0; j < adapter->req_rx_queues; j++) {
1823 data[i] = adapter->rx_stats_buffers[j].packets;
1824 i++;
1825 data[i] = adapter->rx_stats_buffers[j].bytes;
1826 i++;
1827 data[i] = adapter->rx_stats_buffers[j].interrupts;
1828 i++;
1829 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001830}
1831
1832static const struct ethtool_ops ibmvnic_ethtool_ops = {
Thomas Falcon032c5e82015-12-21 11:26:06 -06001833 .get_drvinfo = ibmvnic_get_drvinfo,
1834 .get_msglevel = ibmvnic_get_msglevel,
1835 .set_msglevel = ibmvnic_set_msglevel,
1836 .get_link = ibmvnic_get_link,
1837 .get_ringparam = ibmvnic_get_ringparam,
1838 .get_strings = ibmvnic_get_strings,
1839 .get_sset_count = ibmvnic_get_sset_count,
1840 .get_ethtool_stats = ibmvnic_get_ethtool_stats,
Philippe Reynes8a433792017-01-07 22:37:29 +01001841 .get_link_ksettings = ibmvnic_get_link_ksettings,
Thomas Falcon032c5e82015-12-21 11:26:06 -06001842};
1843
1844/* Routines for managing CRQs/sCRQs */
1845
Nathan Fontenot57a49432017-05-26 10:31:12 -04001846static int reset_one_sub_crq_queue(struct ibmvnic_adapter *adapter,
1847 struct ibmvnic_sub_crq_queue *scrq)
1848{
1849 int rc;
1850
1851 if (scrq->irq) {
1852 free_irq(scrq->irq, scrq);
1853 irq_dispose_mapping(scrq->irq);
1854 scrq->irq = 0;
1855 }
1856
Thomas Falconc8b2ad02017-06-14 23:50:07 -05001857 memset(scrq->msgs, 0, 4 * PAGE_SIZE);
Nathan Fontenot57a49432017-05-26 10:31:12 -04001858 scrq->cur = 0;
1859
1860 rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
1861 4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
1862 return rc;
1863}
1864
1865static int reset_sub_crq_queues(struct ibmvnic_adapter *adapter)
1866{
1867 int i, rc;
1868
1869 for (i = 0; i < adapter->req_tx_queues; i++) {
1870 rc = reset_one_sub_crq_queue(adapter, adapter->tx_scrq[i]);
1871 if (rc)
1872 return rc;
1873 }
1874
1875 for (i = 0; i < adapter->req_rx_queues; i++) {
1876 rc = reset_one_sub_crq_queue(adapter, adapter->rx_scrq[i]);
1877 if (rc)
1878 return rc;
1879 }
1880
Nathan Fontenot57a49432017-05-26 10:31:12 -04001881 return rc;
1882}
1883
Thomas Falcon032c5e82015-12-21 11:26:06 -06001884static void release_sub_crq_queue(struct ibmvnic_adapter *adapter,
1885 struct ibmvnic_sub_crq_queue *scrq)
1886{
1887 struct device *dev = &adapter->vdev->dev;
1888 long rc;
1889
1890 netdev_dbg(adapter->netdev, "Releasing sub-CRQ\n");
1891
1892 /* Close the sub-crqs */
1893 do {
1894 rc = plpar_hcall_norets(H_FREE_SUB_CRQ,
1895 adapter->vdev->unit_address,
1896 scrq->crq_num);
1897 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
1898
Thomas Falconffa73852017-04-19 13:44:29 -04001899 if (rc) {
1900 netdev_err(adapter->netdev,
1901 "Failed to release sub-CRQ %16lx, rc = %ld\n",
1902 scrq->crq_num, rc);
1903 }
1904
Thomas Falcon032c5e82015-12-21 11:26:06 -06001905 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
1906 DMA_BIDIRECTIONAL);
1907 free_pages((unsigned long)scrq->msgs, 2);
1908 kfree(scrq);
1909}
1910
1911static struct ibmvnic_sub_crq_queue *init_sub_crq_queue(struct ibmvnic_adapter
1912 *adapter)
1913{
1914 struct device *dev = &adapter->vdev->dev;
1915 struct ibmvnic_sub_crq_queue *scrq;
1916 int rc;
1917
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04001918 scrq = kzalloc(sizeof(*scrq), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001919 if (!scrq)
1920 return NULL;
1921
Nathan Fontenot7f7adc52017-04-19 13:45:16 -04001922 scrq->msgs =
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04001923 (union sub_crq *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, 2);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001924 if (!scrq->msgs) {
1925 dev_warn(dev, "Couldn't allocate crq queue messages page\n");
1926 goto zero_page_failed;
1927 }
1928
1929 scrq->msg_token = dma_map_single(dev, scrq->msgs, 4 * PAGE_SIZE,
1930 DMA_BIDIRECTIONAL);
1931 if (dma_mapping_error(dev, scrq->msg_token)) {
1932 dev_warn(dev, "Couldn't map crq queue messages page\n");
1933 goto map_failed;
1934 }
1935
1936 rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
1937 4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
1938
1939 if (rc == H_RESOURCE)
1940 rc = ibmvnic_reset_crq(adapter);
1941
1942 if (rc == H_CLOSED) {
1943 dev_warn(dev, "Partner adapter not ready, waiting.\n");
1944 } else if (rc) {
1945 dev_warn(dev, "Error %d registering sub-crq\n", rc);
1946 goto reg_failed;
1947 }
1948
Thomas Falcon032c5e82015-12-21 11:26:06 -06001949 scrq->adapter = adapter;
1950 scrq->size = 4 * PAGE_SIZE / sizeof(*scrq->msgs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001951 spin_lock_init(&scrq->lock);
1952
1953 netdev_dbg(adapter->netdev,
1954 "sub-crq initialized, num %lx, hw_irq=%lx, irq=%x\n",
1955 scrq->crq_num, scrq->hw_irq, scrq->irq);
1956
1957 return scrq;
1958
Thomas Falcon032c5e82015-12-21 11:26:06 -06001959reg_failed:
1960 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
1961 DMA_BIDIRECTIONAL);
1962map_failed:
1963 free_pages((unsigned long)scrq->msgs, 2);
1964zero_page_failed:
1965 kfree(scrq);
1966
1967 return NULL;
1968}
1969
1970static void release_sub_crqs(struct ibmvnic_adapter *adapter)
1971{
1972 int i;
1973
1974 if (adapter->tx_scrq) {
Nathan Fontenotb5108882017-03-30 02:49:18 -04001975 for (i = 0; i < adapter->req_tx_queues; i++) {
1976 if (!adapter->tx_scrq[i])
1977 continue;
1978
1979 if (adapter->tx_scrq[i]->irq) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06001980 free_irq(adapter->tx_scrq[i]->irq,
1981 adapter->tx_scrq[i]);
Thomas Falcon88eb98a2016-07-06 15:35:16 -05001982 irq_dispose_mapping(adapter->tx_scrq[i]->irq);
Nathan Fontenotb5108882017-03-30 02:49:18 -04001983 adapter->tx_scrq[i]->irq = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001984 }
Nathan Fontenotb5108882017-03-30 02:49:18 -04001985
1986 release_sub_crq_queue(adapter, adapter->tx_scrq[i]);
1987 }
1988
Nathan Fontenot9501df32017-03-15 23:38:07 -04001989 kfree(adapter->tx_scrq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001990 adapter->tx_scrq = NULL;
1991 }
1992
1993 if (adapter->rx_scrq) {
Nathan Fontenotb5108882017-03-30 02:49:18 -04001994 for (i = 0; i < adapter->req_rx_queues; i++) {
1995 if (!adapter->rx_scrq[i])
1996 continue;
1997
1998 if (adapter->rx_scrq[i]->irq) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06001999 free_irq(adapter->rx_scrq[i]->irq,
2000 adapter->rx_scrq[i]);
Thomas Falcon88eb98a2016-07-06 15:35:16 -05002001 irq_dispose_mapping(adapter->rx_scrq[i]->irq);
Nathan Fontenotb5108882017-03-30 02:49:18 -04002002 adapter->rx_scrq[i]->irq = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002003 }
Nathan Fontenotb5108882017-03-30 02:49:18 -04002004
2005 release_sub_crq_queue(adapter, adapter->rx_scrq[i]);
2006 }
2007
Nathan Fontenot9501df32017-03-15 23:38:07 -04002008 kfree(adapter->rx_scrq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002009 adapter->rx_scrq = NULL;
2010 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002011}
2012
2013static int disable_scrq_irq(struct ibmvnic_adapter *adapter,
2014 struct ibmvnic_sub_crq_queue *scrq)
2015{
2016 struct device *dev = &adapter->vdev->dev;
2017 unsigned long rc;
2018
2019 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
2020 H_DISABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
2021 if (rc)
2022 dev_err(dev, "Couldn't disable scrq irq 0x%lx. rc=%ld\n",
2023 scrq->hw_irq, rc);
2024 return rc;
2025}
2026
2027static int enable_scrq_irq(struct ibmvnic_adapter *adapter,
2028 struct ibmvnic_sub_crq_queue *scrq)
2029{
2030 struct device *dev = &adapter->vdev->dev;
2031 unsigned long rc;
2032
2033 if (scrq->hw_irq > 0x100000000ULL) {
2034 dev_err(dev, "bad hw_irq = %lx\n", scrq->hw_irq);
2035 return 1;
2036 }
2037
2038 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
2039 H_ENABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
2040 if (rc)
2041 dev_err(dev, "Couldn't enable scrq irq 0x%lx. rc=%ld\n",
2042 scrq->hw_irq, rc);
2043 return rc;
2044}
2045
2046static int ibmvnic_complete_tx(struct ibmvnic_adapter *adapter,
2047 struct ibmvnic_sub_crq_queue *scrq)
2048{
2049 struct device *dev = &adapter->vdev->dev;
2050 struct ibmvnic_tx_buff *txbuff;
2051 union sub_crq *next;
2052 int index;
2053 int i, j;
Thomas Falconad7775d2016-04-01 17:20:34 -05002054 u8 first;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002055
2056restart_loop:
2057 while (pending_scrq(adapter, scrq)) {
2058 unsigned int pool = scrq->pool_index;
2059
2060 next = ibmvnic_next_scrq(adapter, scrq);
2061 for (i = 0; i < next->tx_comp.num_comps; i++) {
2062 if (next->tx_comp.rcs[i]) {
2063 dev_err(dev, "tx error %x\n",
2064 next->tx_comp.rcs[i]);
2065 continue;
2066 }
2067 index = be32_to_cpu(next->tx_comp.correlators[i]);
2068 txbuff = &adapter->tx_pool[pool].tx_buff[index];
2069
2070 for (j = 0; j < IBMVNIC_MAX_FRAGS_PER_CRQ; j++) {
2071 if (!txbuff->data_dma[j])
2072 continue;
2073
2074 txbuff->data_dma[j] = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002075 }
Thomas Falconad7775d2016-04-01 17:20:34 -05002076 /* if sub_crq was sent indirectly */
2077 first = txbuff->indir_arr[0].generic.first;
2078 if (first == IBMVNIC_CRQ_CMD) {
2079 dma_unmap_single(dev, txbuff->indir_dma,
2080 sizeof(txbuff->indir_arr),
2081 DMA_TO_DEVICE);
2082 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002083
Thomas Falcon142c0ac2017-03-05 12:18:41 -06002084 if (txbuff->last_frag) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06002085 dev_kfree_skb_any(txbuff->skb);
Nathan Fontenot7c3e7de2017-05-03 14:05:25 -04002086 txbuff->skb = NULL;
Thomas Falcon142c0ac2017-03-05 12:18:41 -06002087 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002088
2089 adapter->tx_pool[pool].free_map[adapter->tx_pool[pool].
2090 producer_index] = index;
2091 adapter->tx_pool[pool].producer_index =
2092 (adapter->tx_pool[pool].producer_index + 1) %
Thomas Falcon068d9f92017-03-05 12:18:42 -06002093 adapter->req_tx_entries_per_subcrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002094 }
2095 /* remove tx_comp scrq*/
2096 next->tx_comp.first = 0;
Nathan Fontenot7c3e7de2017-05-03 14:05:25 -04002097
2098 if (atomic_sub_return(next->tx_comp.num_comps, &scrq->used) <=
2099 (adapter->req_tx_entries_per_subcrq / 2) &&
2100 __netif_subqueue_stopped(adapter->netdev,
2101 scrq->pool_index)) {
2102 netif_wake_subqueue(adapter->netdev, scrq->pool_index);
2103 netdev_info(adapter->netdev, "Started queue %d\n",
2104 scrq->pool_index);
2105 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002106 }
2107
2108 enable_scrq_irq(adapter, scrq);
2109
2110 if (pending_scrq(adapter, scrq)) {
2111 disable_scrq_irq(adapter, scrq);
2112 goto restart_loop;
2113 }
2114
2115 return 0;
2116}
2117
2118static irqreturn_t ibmvnic_interrupt_tx(int irq, void *instance)
2119{
2120 struct ibmvnic_sub_crq_queue *scrq = instance;
2121 struct ibmvnic_adapter *adapter = scrq->adapter;
2122
2123 disable_scrq_irq(adapter, scrq);
2124 ibmvnic_complete_tx(adapter, scrq);
2125
2126 return IRQ_HANDLED;
2127}
2128
2129static irqreturn_t ibmvnic_interrupt_rx(int irq, void *instance)
2130{
2131 struct ibmvnic_sub_crq_queue *scrq = instance;
2132 struct ibmvnic_adapter *adapter = scrq->adapter;
2133
John Allen3d52b592017-08-02 16:44:14 -05002134 adapter->rx_stats_buffers[scrq->scrq_num].interrupts++;
2135
Thomas Falcon032c5e82015-12-21 11:26:06 -06002136 if (napi_schedule_prep(&adapter->napi[scrq->scrq_num])) {
2137 disable_scrq_irq(adapter, scrq);
2138 __napi_schedule(&adapter->napi[scrq->scrq_num]);
2139 }
2140
2141 return IRQ_HANDLED;
2142}
2143
Thomas Falconea22d512016-07-06 15:35:17 -05002144static int init_sub_crq_irqs(struct ibmvnic_adapter *adapter)
2145{
2146 struct device *dev = &adapter->vdev->dev;
2147 struct ibmvnic_sub_crq_queue *scrq;
2148 int i = 0, j = 0;
2149 int rc = 0;
2150
2151 for (i = 0; i < adapter->req_tx_queues; i++) {
2152 scrq = adapter->tx_scrq[i];
2153 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
2154
Michael Ellerman99c17902016-09-10 19:59:05 +10002155 if (!scrq->irq) {
Thomas Falconea22d512016-07-06 15:35:17 -05002156 rc = -EINVAL;
2157 dev_err(dev, "Error mapping irq\n");
2158 goto req_tx_irq_failed;
2159 }
2160
2161 rc = request_irq(scrq->irq, ibmvnic_interrupt_tx,
2162 0, "ibmvnic_tx", scrq);
2163
2164 if (rc) {
2165 dev_err(dev, "Couldn't register tx irq 0x%x. rc=%d\n",
2166 scrq->irq, rc);
2167 irq_dispose_mapping(scrq->irq);
2168 goto req_rx_irq_failed;
2169 }
2170 }
2171
2172 for (i = 0; i < adapter->req_rx_queues; i++) {
2173 scrq = adapter->rx_scrq[i];
2174 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
Michael Ellerman99c17902016-09-10 19:59:05 +10002175 if (!scrq->irq) {
Thomas Falconea22d512016-07-06 15:35:17 -05002176 rc = -EINVAL;
2177 dev_err(dev, "Error mapping irq\n");
2178 goto req_rx_irq_failed;
2179 }
2180 rc = request_irq(scrq->irq, ibmvnic_interrupt_rx,
2181 0, "ibmvnic_rx", scrq);
2182 if (rc) {
2183 dev_err(dev, "Couldn't register rx irq 0x%x. rc=%d\n",
2184 scrq->irq, rc);
2185 irq_dispose_mapping(scrq->irq);
2186 goto req_rx_irq_failed;
2187 }
2188 }
2189 return rc;
2190
2191req_rx_irq_failed:
Thomas Falcon8bf371e2016-10-27 12:28:52 -05002192 for (j = 0; j < i; j++) {
Thomas Falconea22d512016-07-06 15:35:17 -05002193 free_irq(adapter->rx_scrq[j]->irq, adapter->rx_scrq[j]);
2194 irq_dispose_mapping(adapter->rx_scrq[j]->irq);
Thomas Falcon8bf371e2016-10-27 12:28:52 -05002195 }
Thomas Falconea22d512016-07-06 15:35:17 -05002196 i = adapter->req_tx_queues;
2197req_tx_irq_failed:
Thomas Falcon8bf371e2016-10-27 12:28:52 -05002198 for (j = 0; j < i; j++) {
Thomas Falconea22d512016-07-06 15:35:17 -05002199 free_irq(adapter->tx_scrq[j]->irq, adapter->tx_scrq[j]);
2200 irq_dispose_mapping(adapter->rx_scrq[j]->irq);
Thomas Falcon8bf371e2016-10-27 12:28:52 -05002201 }
Nathan Fontenotb5108882017-03-30 02:49:18 -04002202 release_sub_crqs(adapter);
Thomas Falconea22d512016-07-06 15:35:17 -05002203 return rc;
2204}
2205
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002206static int init_sub_crqs(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002207{
2208 struct device *dev = &adapter->vdev->dev;
2209 struct ibmvnic_sub_crq_queue **allqueues;
2210 int registered_queues = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002211 int total_queues;
2212 int more = 0;
Thomas Falconea22d512016-07-06 15:35:17 -05002213 int i;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002214
Thomas Falcon032c5e82015-12-21 11:26:06 -06002215 total_queues = adapter->req_tx_queues + adapter->req_rx_queues;
2216
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04002217 allqueues = kcalloc(total_queues, sizeof(*allqueues), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002218 if (!allqueues)
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002219 return -1;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002220
2221 for (i = 0; i < total_queues; i++) {
2222 allqueues[i] = init_sub_crq_queue(adapter);
2223 if (!allqueues[i]) {
2224 dev_warn(dev, "Couldn't allocate all sub-crqs\n");
2225 break;
2226 }
2227 registered_queues++;
2228 }
2229
2230 /* Make sure we were able to register the minimum number of queues */
2231 if (registered_queues <
2232 adapter->min_tx_queues + adapter->min_rx_queues) {
2233 dev_err(dev, "Fatal: Couldn't init min number of sub-crqs\n");
2234 goto tx_failed;
2235 }
2236
2237 /* Distribute the failed allocated queues*/
2238 for (i = 0; i < total_queues - registered_queues + more ; i++) {
2239 netdev_dbg(adapter->netdev, "Reducing number of queues\n");
2240 switch (i % 3) {
2241 case 0:
2242 if (adapter->req_rx_queues > adapter->min_rx_queues)
2243 adapter->req_rx_queues--;
2244 else
2245 more++;
2246 break;
2247 case 1:
2248 if (adapter->req_tx_queues > adapter->min_tx_queues)
2249 adapter->req_tx_queues--;
2250 else
2251 more++;
2252 break;
2253 }
2254 }
2255
2256 adapter->tx_scrq = kcalloc(adapter->req_tx_queues,
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04002257 sizeof(*adapter->tx_scrq), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002258 if (!adapter->tx_scrq)
2259 goto tx_failed;
2260
2261 for (i = 0; i < adapter->req_tx_queues; i++) {
2262 adapter->tx_scrq[i] = allqueues[i];
2263 adapter->tx_scrq[i]->pool_index = i;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002264 }
2265
2266 adapter->rx_scrq = kcalloc(adapter->req_rx_queues,
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04002267 sizeof(*adapter->rx_scrq), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002268 if (!adapter->rx_scrq)
2269 goto rx_failed;
2270
2271 for (i = 0; i < adapter->req_rx_queues; i++) {
2272 adapter->rx_scrq[i] = allqueues[i + adapter->req_tx_queues];
2273 adapter->rx_scrq[i]->scrq_num = i;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002274 }
2275
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002276 kfree(allqueues);
2277 return 0;
2278
2279rx_failed:
2280 kfree(adapter->tx_scrq);
2281 adapter->tx_scrq = NULL;
2282tx_failed:
2283 for (i = 0; i < registered_queues; i++)
2284 release_sub_crq_queue(adapter, allqueues[i]);
2285 kfree(allqueues);
2286 return -1;
2287}
2288
2289static void ibmvnic_send_req_caps(struct ibmvnic_adapter *adapter, int retry)
2290{
2291 struct device *dev = &adapter->vdev->dev;
2292 union ibmvnic_crq crq;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002293
2294 if (!retry) {
2295 /* Sub-CRQ entries are 32 byte long */
2296 int entries_page = 4 * PAGE_SIZE / (sizeof(u64) * 4);
2297
2298 if (adapter->min_tx_entries_per_subcrq > entries_page ||
2299 adapter->min_rx_add_entries_per_subcrq > entries_page) {
2300 dev_err(dev, "Fatal, invalid entries per sub-crq\n");
2301 return;
2302 }
2303
2304 /* Get the minimum between the queried max and the entries
2305 * that fit in our PAGE_SIZE
2306 */
2307 adapter->req_tx_entries_per_subcrq =
2308 adapter->max_tx_entries_per_subcrq > entries_page ?
2309 entries_page : adapter->max_tx_entries_per_subcrq;
2310 adapter->req_rx_add_entries_per_subcrq =
2311 adapter->max_rx_add_entries_per_subcrq > entries_page ?
2312 entries_page : adapter->max_rx_add_entries_per_subcrq;
2313
2314 adapter->req_tx_queues = adapter->opt_tx_comp_sub_queues;
2315 adapter->req_rx_queues = adapter->opt_rx_comp_queues;
2316 adapter->req_rx_add_queues = adapter->max_rx_add_queues;
2317
2318 adapter->req_mtu = adapter->netdev->mtu + ETH_HLEN;
2319 }
2320
Thomas Falcon032c5e82015-12-21 11:26:06 -06002321 memset(&crq, 0, sizeof(crq));
2322 crq.request_capability.first = IBMVNIC_CRQ_CMD;
2323 crq.request_capability.cmd = REQUEST_CAPABILITY;
2324
2325 crq.request_capability.capability = cpu_to_be16(REQ_TX_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06002326 crq.request_capability.number = cpu_to_be64(adapter->req_tx_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06002327 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002328 ibmvnic_send_crq(adapter, &crq);
2329
2330 crq.request_capability.capability = cpu_to_be16(REQ_RX_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06002331 crq.request_capability.number = cpu_to_be64(adapter->req_rx_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06002332 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002333 ibmvnic_send_crq(adapter, &crq);
2334
2335 crq.request_capability.capability = cpu_to_be16(REQ_RX_ADD_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06002336 crq.request_capability.number = cpu_to_be64(adapter->req_rx_add_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06002337 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002338 ibmvnic_send_crq(adapter, &crq);
2339
2340 crq.request_capability.capability =
2341 cpu_to_be16(REQ_TX_ENTRIES_PER_SUBCRQ);
2342 crq.request_capability.number =
Thomas Falconde89e852016-03-01 10:20:09 -06002343 cpu_to_be64(adapter->req_tx_entries_per_subcrq);
Thomas Falcon901e0402017-02-15 12:17:59 -06002344 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002345 ibmvnic_send_crq(adapter, &crq);
2346
2347 crq.request_capability.capability =
2348 cpu_to_be16(REQ_RX_ADD_ENTRIES_PER_SUBCRQ);
2349 crq.request_capability.number =
Thomas Falconde89e852016-03-01 10:20:09 -06002350 cpu_to_be64(adapter->req_rx_add_entries_per_subcrq);
Thomas Falcon901e0402017-02-15 12:17:59 -06002351 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002352 ibmvnic_send_crq(adapter, &crq);
2353
2354 crq.request_capability.capability = cpu_to_be16(REQ_MTU);
Thomas Falconde89e852016-03-01 10:20:09 -06002355 crq.request_capability.number = cpu_to_be64(adapter->req_mtu);
Thomas Falcon901e0402017-02-15 12:17:59 -06002356 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002357 ibmvnic_send_crq(adapter, &crq);
2358
2359 if (adapter->netdev->flags & IFF_PROMISC) {
2360 if (adapter->promisc_supported) {
2361 crq.request_capability.capability =
2362 cpu_to_be16(PROMISC_REQUESTED);
Thomas Falconde89e852016-03-01 10:20:09 -06002363 crq.request_capability.number = cpu_to_be64(1);
Thomas Falcon901e0402017-02-15 12:17:59 -06002364 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002365 ibmvnic_send_crq(adapter, &crq);
2366 }
2367 } else {
2368 crq.request_capability.capability =
2369 cpu_to_be16(PROMISC_REQUESTED);
Thomas Falconde89e852016-03-01 10:20:09 -06002370 crq.request_capability.number = cpu_to_be64(0);
Thomas Falcon901e0402017-02-15 12:17:59 -06002371 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002372 ibmvnic_send_crq(adapter, &crq);
2373 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002374}
2375
2376static int pending_scrq(struct ibmvnic_adapter *adapter,
2377 struct ibmvnic_sub_crq_queue *scrq)
2378{
2379 union sub_crq *entry = &scrq->msgs[scrq->cur];
2380
Thomas Falcon1cf9cc72017-06-14 23:50:08 -05002381 if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002382 return 1;
2383 else
2384 return 0;
2385}
2386
2387static union sub_crq *ibmvnic_next_scrq(struct ibmvnic_adapter *adapter,
2388 struct ibmvnic_sub_crq_queue *scrq)
2389{
2390 union sub_crq *entry;
2391 unsigned long flags;
2392
2393 spin_lock_irqsave(&scrq->lock, flags);
2394 entry = &scrq->msgs[scrq->cur];
2395 if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP) {
2396 if (++scrq->cur == scrq->size)
2397 scrq->cur = 0;
2398 } else {
2399 entry = NULL;
2400 }
2401 spin_unlock_irqrestore(&scrq->lock, flags);
2402
2403 return entry;
2404}
2405
2406static union ibmvnic_crq *ibmvnic_next_crq(struct ibmvnic_adapter *adapter)
2407{
2408 struct ibmvnic_crq_queue *queue = &adapter->crq;
2409 union ibmvnic_crq *crq;
2410
2411 crq = &queue->msgs[queue->cur];
2412 if (crq->generic.first & IBMVNIC_CRQ_CMD_RSP) {
2413 if (++queue->cur == queue->size)
2414 queue->cur = 0;
2415 } else {
2416 crq = NULL;
2417 }
2418
2419 return crq;
2420}
2421
2422static int send_subcrq(struct ibmvnic_adapter *adapter, u64 remote_handle,
2423 union sub_crq *sub_crq)
2424{
2425 unsigned int ua = adapter->vdev->unit_address;
2426 struct device *dev = &adapter->vdev->dev;
2427 u64 *u64_crq = (u64 *)sub_crq;
2428 int rc;
2429
2430 netdev_dbg(adapter->netdev,
2431 "Sending sCRQ %016lx: %016lx %016lx %016lx %016lx\n",
2432 (unsigned long int)cpu_to_be64(remote_handle),
2433 (unsigned long int)cpu_to_be64(u64_crq[0]),
2434 (unsigned long int)cpu_to_be64(u64_crq[1]),
2435 (unsigned long int)cpu_to_be64(u64_crq[2]),
2436 (unsigned long int)cpu_to_be64(u64_crq[3]));
2437
2438 /* Make sure the hypervisor sees the complete request */
2439 mb();
2440
2441 rc = plpar_hcall_norets(H_SEND_SUB_CRQ, ua,
2442 cpu_to_be64(remote_handle),
2443 cpu_to_be64(u64_crq[0]),
2444 cpu_to_be64(u64_crq[1]),
2445 cpu_to_be64(u64_crq[2]),
2446 cpu_to_be64(u64_crq[3]));
2447
2448 if (rc) {
2449 if (rc == H_CLOSED)
2450 dev_warn(dev, "CRQ Queue closed\n");
2451 dev_err(dev, "Send error (rc=%d)\n", rc);
2452 }
2453
2454 return rc;
2455}
2456
Thomas Falconad7775d2016-04-01 17:20:34 -05002457static int send_subcrq_indirect(struct ibmvnic_adapter *adapter,
2458 u64 remote_handle, u64 ioba, u64 num_entries)
2459{
2460 unsigned int ua = adapter->vdev->unit_address;
2461 struct device *dev = &adapter->vdev->dev;
2462 int rc;
2463
2464 /* Make sure the hypervisor sees the complete request */
2465 mb();
2466 rc = plpar_hcall_norets(H_SEND_SUB_CRQ_INDIRECT, ua,
2467 cpu_to_be64(remote_handle),
2468 ioba, num_entries);
2469
2470 if (rc) {
2471 if (rc == H_CLOSED)
2472 dev_warn(dev, "CRQ Queue closed\n");
2473 dev_err(dev, "Send (indirect) error (rc=%d)\n", rc);
2474 }
2475
2476 return rc;
2477}
2478
Thomas Falcon032c5e82015-12-21 11:26:06 -06002479static int ibmvnic_send_crq(struct ibmvnic_adapter *adapter,
2480 union ibmvnic_crq *crq)
2481{
2482 unsigned int ua = adapter->vdev->unit_address;
2483 struct device *dev = &adapter->vdev->dev;
2484 u64 *u64_crq = (u64 *)crq;
2485 int rc;
2486
2487 netdev_dbg(adapter->netdev, "Sending CRQ: %016lx %016lx\n",
2488 (unsigned long int)cpu_to_be64(u64_crq[0]),
2489 (unsigned long int)cpu_to_be64(u64_crq[1]));
2490
2491 /* Make sure the hypervisor sees the complete request */
2492 mb();
2493
2494 rc = plpar_hcall_norets(H_SEND_CRQ, ua,
2495 cpu_to_be64(u64_crq[0]),
2496 cpu_to_be64(u64_crq[1]));
2497
2498 if (rc) {
2499 if (rc == H_CLOSED)
2500 dev_warn(dev, "CRQ Queue closed\n");
2501 dev_warn(dev, "Send error (rc=%d)\n", rc);
2502 }
2503
2504 return rc;
2505}
2506
2507static int ibmvnic_send_crq_init(struct ibmvnic_adapter *adapter)
2508{
2509 union ibmvnic_crq crq;
2510
2511 memset(&crq, 0, sizeof(crq));
2512 crq.generic.first = IBMVNIC_CRQ_INIT_CMD;
2513 crq.generic.cmd = IBMVNIC_CRQ_INIT;
2514 netdev_dbg(adapter->netdev, "Sending CRQ init\n");
2515
2516 return ibmvnic_send_crq(adapter, &crq);
2517}
2518
Thomas Falcon032c5e82015-12-21 11:26:06 -06002519static int send_version_xchg(struct ibmvnic_adapter *adapter)
2520{
2521 union ibmvnic_crq crq;
2522
2523 memset(&crq, 0, sizeof(crq));
2524 crq.version_exchange.first = IBMVNIC_CRQ_CMD;
2525 crq.version_exchange.cmd = VERSION_EXCHANGE;
2526 crq.version_exchange.version = cpu_to_be16(ibmvnic_version);
2527
2528 return ibmvnic_send_crq(adapter, &crq);
2529}
2530
2531static void send_login(struct ibmvnic_adapter *adapter)
2532{
2533 struct ibmvnic_login_rsp_buffer *login_rsp_buffer;
2534 struct ibmvnic_login_buffer *login_buffer;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002535 struct device *dev = &adapter->vdev->dev;
2536 dma_addr_t rsp_buffer_token;
2537 dma_addr_t buffer_token;
2538 size_t rsp_buffer_size;
2539 union ibmvnic_crq crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002540 size_t buffer_size;
2541 __be64 *tx_list_p;
2542 __be64 *rx_list_p;
2543 int i;
2544
2545 buffer_size =
2546 sizeof(struct ibmvnic_login_buffer) +
2547 sizeof(u64) * (adapter->req_tx_queues + adapter->req_rx_queues);
2548
2549 login_buffer = kmalloc(buffer_size, GFP_ATOMIC);
2550 if (!login_buffer)
2551 goto buf_alloc_failed;
2552
2553 buffer_token = dma_map_single(dev, login_buffer, buffer_size,
2554 DMA_TO_DEVICE);
2555 if (dma_mapping_error(dev, buffer_token)) {
2556 dev_err(dev, "Couldn't map login buffer\n");
2557 goto buf_map_failed;
2558 }
2559
John Allen498cd8e2016-04-06 11:49:55 -05002560 rsp_buffer_size = sizeof(struct ibmvnic_login_rsp_buffer) +
2561 sizeof(u64) * adapter->req_tx_queues +
2562 sizeof(u64) * adapter->req_rx_queues +
2563 sizeof(u64) * adapter->req_rx_queues +
2564 sizeof(u8) * IBMVNIC_TX_DESC_VERSIONS;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002565
2566 login_rsp_buffer = kmalloc(rsp_buffer_size, GFP_ATOMIC);
2567 if (!login_rsp_buffer)
2568 goto buf_rsp_alloc_failed;
2569
2570 rsp_buffer_token = dma_map_single(dev, login_rsp_buffer,
2571 rsp_buffer_size, DMA_FROM_DEVICE);
2572 if (dma_mapping_error(dev, rsp_buffer_token)) {
2573 dev_err(dev, "Couldn't map login rsp buffer\n");
2574 goto buf_rsp_map_failed;
2575 }
Nathan Fontenot661a2622017-04-19 13:44:58 -04002576
Thomas Falcon032c5e82015-12-21 11:26:06 -06002577 adapter->login_buf = login_buffer;
2578 adapter->login_buf_token = buffer_token;
2579 adapter->login_buf_sz = buffer_size;
2580 adapter->login_rsp_buf = login_rsp_buffer;
2581 adapter->login_rsp_buf_token = rsp_buffer_token;
2582 adapter->login_rsp_buf_sz = rsp_buffer_size;
2583
2584 login_buffer->len = cpu_to_be32(buffer_size);
2585 login_buffer->version = cpu_to_be32(INITIAL_VERSION_LB);
2586 login_buffer->num_txcomp_subcrqs = cpu_to_be32(adapter->req_tx_queues);
2587 login_buffer->off_txcomp_subcrqs =
2588 cpu_to_be32(sizeof(struct ibmvnic_login_buffer));
2589 login_buffer->num_rxcomp_subcrqs = cpu_to_be32(adapter->req_rx_queues);
2590 login_buffer->off_rxcomp_subcrqs =
2591 cpu_to_be32(sizeof(struct ibmvnic_login_buffer) +
2592 sizeof(u64) * adapter->req_tx_queues);
2593 login_buffer->login_rsp_ioba = cpu_to_be32(rsp_buffer_token);
2594 login_buffer->login_rsp_len = cpu_to_be32(rsp_buffer_size);
2595
2596 tx_list_p = (__be64 *)((char *)login_buffer +
2597 sizeof(struct ibmvnic_login_buffer));
2598 rx_list_p = (__be64 *)((char *)login_buffer +
2599 sizeof(struct ibmvnic_login_buffer) +
2600 sizeof(u64) * adapter->req_tx_queues);
2601
2602 for (i = 0; i < adapter->req_tx_queues; i++) {
2603 if (adapter->tx_scrq[i]) {
2604 tx_list_p[i] = cpu_to_be64(adapter->tx_scrq[i]->
2605 crq_num);
2606 }
2607 }
2608
2609 for (i = 0; i < adapter->req_rx_queues; i++) {
2610 if (adapter->rx_scrq[i]) {
2611 rx_list_p[i] = cpu_to_be64(adapter->rx_scrq[i]->
2612 crq_num);
2613 }
2614 }
2615
2616 netdev_dbg(adapter->netdev, "Login Buffer:\n");
2617 for (i = 0; i < (adapter->login_buf_sz - 1) / 8 + 1; i++) {
2618 netdev_dbg(adapter->netdev, "%016lx\n",
2619 ((unsigned long int *)(adapter->login_buf))[i]);
2620 }
2621
2622 memset(&crq, 0, sizeof(crq));
2623 crq.login.first = IBMVNIC_CRQ_CMD;
2624 crq.login.cmd = LOGIN;
2625 crq.login.ioba = cpu_to_be32(buffer_token);
2626 crq.login.len = cpu_to_be32(buffer_size);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002627 ibmvnic_send_crq(adapter, &crq);
2628
2629 return;
2630
Thomas Falcon032c5e82015-12-21 11:26:06 -06002631buf_rsp_map_failed:
2632 kfree(login_rsp_buffer);
2633buf_rsp_alloc_failed:
2634 dma_unmap_single(dev, buffer_token, buffer_size, DMA_TO_DEVICE);
2635buf_map_failed:
2636 kfree(login_buffer);
2637buf_alloc_failed:
2638 return;
2639}
2640
2641static void send_request_map(struct ibmvnic_adapter *adapter, dma_addr_t addr,
2642 u32 len, u8 map_id)
2643{
2644 union ibmvnic_crq crq;
2645
2646 memset(&crq, 0, sizeof(crq));
2647 crq.request_map.first = IBMVNIC_CRQ_CMD;
2648 crq.request_map.cmd = REQUEST_MAP;
2649 crq.request_map.map_id = map_id;
2650 crq.request_map.ioba = cpu_to_be32(addr);
2651 crq.request_map.len = cpu_to_be32(len);
2652 ibmvnic_send_crq(adapter, &crq);
2653}
2654
2655static void send_request_unmap(struct ibmvnic_adapter *adapter, u8 map_id)
2656{
2657 union ibmvnic_crq crq;
2658
2659 memset(&crq, 0, sizeof(crq));
2660 crq.request_unmap.first = IBMVNIC_CRQ_CMD;
2661 crq.request_unmap.cmd = REQUEST_UNMAP;
2662 crq.request_unmap.map_id = map_id;
2663 ibmvnic_send_crq(adapter, &crq);
2664}
2665
2666static void send_map_query(struct ibmvnic_adapter *adapter)
2667{
2668 union ibmvnic_crq crq;
2669
2670 memset(&crq, 0, sizeof(crq));
2671 crq.query_map.first = IBMVNIC_CRQ_CMD;
2672 crq.query_map.cmd = QUERY_MAP;
2673 ibmvnic_send_crq(adapter, &crq);
2674}
2675
2676/* Send a series of CRQs requesting various capabilities of the VNIC server */
2677static void send_cap_queries(struct ibmvnic_adapter *adapter)
2678{
2679 union ibmvnic_crq crq;
2680
Thomas Falcon901e0402017-02-15 12:17:59 -06002681 atomic_set(&adapter->running_cap_crqs, 0);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002682 memset(&crq, 0, sizeof(crq));
2683 crq.query_capability.first = IBMVNIC_CRQ_CMD;
2684 crq.query_capability.cmd = QUERY_CAPABILITY;
2685
2686 crq.query_capability.capability = cpu_to_be16(MIN_TX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002687 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002688 ibmvnic_send_crq(adapter, &crq);
2689
2690 crq.query_capability.capability = cpu_to_be16(MIN_RX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002691 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002692 ibmvnic_send_crq(adapter, &crq);
2693
2694 crq.query_capability.capability = cpu_to_be16(MIN_RX_ADD_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002695 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002696 ibmvnic_send_crq(adapter, &crq);
2697
2698 crq.query_capability.capability = cpu_to_be16(MAX_TX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002699 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002700 ibmvnic_send_crq(adapter, &crq);
2701
2702 crq.query_capability.capability = cpu_to_be16(MAX_RX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002703 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002704 ibmvnic_send_crq(adapter, &crq);
2705
2706 crq.query_capability.capability = cpu_to_be16(MAX_RX_ADD_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002707 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002708 ibmvnic_send_crq(adapter, &crq);
2709
2710 crq.query_capability.capability =
2711 cpu_to_be16(MIN_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06002712 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002713 ibmvnic_send_crq(adapter, &crq);
2714
2715 crq.query_capability.capability =
2716 cpu_to_be16(MIN_RX_ADD_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06002717 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002718 ibmvnic_send_crq(adapter, &crq);
2719
2720 crq.query_capability.capability =
2721 cpu_to_be16(MAX_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06002722 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002723 ibmvnic_send_crq(adapter, &crq);
2724
2725 crq.query_capability.capability =
2726 cpu_to_be16(MAX_RX_ADD_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06002727 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002728 ibmvnic_send_crq(adapter, &crq);
2729
2730 crq.query_capability.capability = cpu_to_be16(TCP_IP_OFFLOAD);
Thomas Falcon901e0402017-02-15 12:17:59 -06002731 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002732 ibmvnic_send_crq(adapter, &crq);
2733
2734 crq.query_capability.capability = cpu_to_be16(PROMISC_SUPPORTED);
Thomas Falcon901e0402017-02-15 12:17:59 -06002735 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002736 ibmvnic_send_crq(adapter, &crq);
2737
2738 crq.query_capability.capability = cpu_to_be16(MIN_MTU);
Thomas Falcon901e0402017-02-15 12:17:59 -06002739 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002740 ibmvnic_send_crq(adapter, &crq);
2741
2742 crq.query_capability.capability = cpu_to_be16(MAX_MTU);
Thomas Falcon901e0402017-02-15 12:17:59 -06002743 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002744 ibmvnic_send_crq(adapter, &crq);
2745
2746 crq.query_capability.capability = cpu_to_be16(MAX_MULTICAST_FILTERS);
Thomas Falcon901e0402017-02-15 12:17:59 -06002747 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002748 ibmvnic_send_crq(adapter, &crq);
2749
2750 crq.query_capability.capability = cpu_to_be16(VLAN_HEADER_INSERTION);
Thomas Falcon901e0402017-02-15 12:17:59 -06002751 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002752 ibmvnic_send_crq(adapter, &crq);
2753
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04002754 crq.query_capability.capability = cpu_to_be16(RX_VLAN_HEADER_INSERTION);
2755 atomic_inc(&adapter->running_cap_crqs);
2756 ibmvnic_send_crq(adapter, &crq);
2757
Thomas Falcon032c5e82015-12-21 11:26:06 -06002758 crq.query_capability.capability = cpu_to_be16(MAX_TX_SG_ENTRIES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002759 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002760 ibmvnic_send_crq(adapter, &crq);
2761
2762 crq.query_capability.capability = cpu_to_be16(RX_SG_SUPPORTED);
Thomas Falcon901e0402017-02-15 12:17:59 -06002763 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002764 ibmvnic_send_crq(adapter, &crq);
2765
2766 crq.query_capability.capability = cpu_to_be16(OPT_TX_COMP_SUB_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002767 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002768 ibmvnic_send_crq(adapter, &crq);
2769
2770 crq.query_capability.capability = cpu_to_be16(OPT_RX_COMP_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002771 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002772 ibmvnic_send_crq(adapter, &crq);
2773
2774 crq.query_capability.capability =
2775 cpu_to_be16(OPT_RX_BUFADD_Q_PER_RX_COMP_Q);
Thomas Falcon901e0402017-02-15 12:17:59 -06002776 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002777 ibmvnic_send_crq(adapter, &crq);
2778
2779 crq.query_capability.capability =
2780 cpu_to_be16(OPT_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06002781 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002782 ibmvnic_send_crq(adapter, &crq);
2783
2784 crq.query_capability.capability =
2785 cpu_to_be16(OPT_RXBA_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06002786 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002787 ibmvnic_send_crq(adapter, &crq);
2788
2789 crq.query_capability.capability = cpu_to_be16(TX_RX_DESC_REQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06002790 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002791 ibmvnic_send_crq(adapter, &crq);
2792}
2793
2794static void handle_query_ip_offload_rsp(struct ibmvnic_adapter *adapter)
2795{
2796 struct device *dev = &adapter->vdev->dev;
2797 struct ibmvnic_query_ip_offload_buffer *buf = &adapter->ip_offload_buf;
2798 union ibmvnic_crq crq;
2799 int i;
2800
2801 dma_unmap_single(dev, adapter->ip_offload_tok,
2802 sizeof(adapter->ip_offload_buf), DMA_FROM_DEVICE);
2803
2804 netdev_dbg(adapter->netdev, "Query IP Offload Buffer:\n");
2805 for (i = 0; i < (sizeof(adapter->ip_offload_buf) - 1) / 8 + 1; i++)
2806 netdev_dbg(adapter->netdev, "%016lx\n",
2807 ((unsigned long int *)(buf))[i]);
2808
2809 netdev_dbg(adapter->netdev, "ipv4_chksum = %d\n", buf->ipv4_chksum);
2810 netdev_dbg(adapter->netdev, "ipv6_chksum = %d\n", buf->ipv6_chksum);
2811 netdev_dbg(adapter->netdev, "tcp_ipv4_chksum = %d\n",
2812 buf->tcp_ipv4_chksum);
2813 netdev_dbg(adapter->netdev, "tcp_ipv6_chksum = %d\n",
2814 buf->tcp_ipv6_chksum);
2815 netdev_dbg(adapter->netdev, "udp_ipv4_chksum = %d\n",
2816 buf->udp_ipv4_chksum);
2817 netdev_dbg(adapter->netdev, "udp_ipv6_chksum = %d\n",
2818 buf->udp_ipv6_chksum);
2819 netdev_dbg(adapter->netdev, "large_tx_ipv4 = %d\n",
2820 buf->large_tx_ipv4);
2821 netdev_dbg(adapter->netdev, "large_tx_ipv6 = %d\n",
2822 buf->large_tx_ipv6);
2823 netdev_dbg(adapter->netdev, "large_rx_ipv4 = %d\n",
2824 buf->large_rx_ipv4);
2825 netdev_dbg(adapter->netdev, "large_rx_ipv6 = %d\n",
2826 buf->large_rx_ipv6);
2827 netdev_dbg(adapter->netdev, "max_ipv4_hdr_sz = %d\n",
2828 buf->max_ipv4_header_size);
2829 netdev_dbg(adapter->netdev, "max_ipv6_hdr_sz = %d\n",
2830 buf->max_ipv6_header_size);
2831 netdev_dbg(adapter->netdev, "max_tcp_hdr_size = %d\n",
2832 buf->max_tcp_header_size);
2833 netdev_dbg(adapter->netdev, "max_udp_hdr_size = %d\n",
2834 buf->max_udp_header_size);
2835 netdev_dbg(adapter->netdev, "max_large_tx_size = %d\n",
2836 buf->max_large_tx_size);
2837 netdev_dbg(adapter->netdev, "max_large_rx_size = %d\n",
2838 buf->max_large_rx_size);
2839 netdev_dbg(adapter->netdev, "ipv6_ext_hdr = %d\n",
2840 buf->ipv6_extension_header);
2841 netdev_dbg(adapter->netdev, "tcp_pseudosum_req = %d\n",
2842 buf->tcp_pseudosum_req);
2843 netdev_dbg(adapter->netdev, "num_ipv6_ext_hd = %d\n",
2844 buf->num_ipv6_ext_headers);
2845 netdev_dbg(adapter->netdev, "off_ipv6_ext_hd = %d\n",
2846 buf->off_ipv6_ext_headers);
2847
2848 adapter->ip_offload_ctrl_tok =
2849 dma_map_single(dev, &adapter->ip_offload_ctrl,
2850 sizeof(adapter->ip_offload_ctrl), DMA_TO_DEVICE);
2851
2852 if (dma_mapping_error(dev, adapter->ip_offload_ctrl_tok)) {
2853 dev_err(dev, "Couldn't map ip offload control buffer\n");
2854 return;
2855 }
2856
2857 adapter->ip_offload_ctrl.version = cpu_to_be32(INITIAL_VERSION_IOB);
2858 adapter->ip_offload_ctrl.tcp_ipv4_chksum = buf->tcp_ipv4_chksum;
2859 adapter->ip_offload_ctrl.udp_ipv4_chksum = buf->udp_ipv4_chksum;
2860 adapter->ip_offload_ctrl.tcp_ipv6_chksum = buf->tcp_ipv6_chksum;
2861 adapter->ip_offload_ctrl.udp_ipv6_chksum = buf->udp_ipv6_chksum;
2862
2863 /* large_tx/rx disabled for now, additional features needed */
2864 adapter->ip_offload_ctrl.large_tx_ipv4 = 0;
2865 adapter->ip_offload_ctrl.large_tx_ipv6 = 0;
2866 adapter->ip_offload_ctrl.large_rx_ipv4 = 0;
2867 adapter->ip_offload_ctrl.large_rx_ipv6 = 0;
2868
2869 adapter->netdev->features = NETIF_F_GSO;
2870
2871 if (buf->tcp_ipv4_chksum || buf->udp_ipv4_chksum)
2872 adapter->netdev->features |= NETIF_F_IP_CSUM;
2873
2874 if (buf->tcp_ipv6_chksum || buf->udp_ipv6_chksum)
2875 adapter->netdev->features |= NETIF_F_IPV6_CSUM;
2876
Thomas Falcon9be02cd2016-04-01 17:20:35 -05002877 if ((adapter->netdev->features &
2878 (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)))
2879 adapter->netdev->features |= NETIF_F_RXCSUM;
2880
Thomas Falcon032c5e82015-12-21 11:26:06 -06002881 memset(&crq, 0, sizeof(crq));
2882 crq.control_ip_offload.first = IBMVNIC_CRQ_CMD;
2883 crq.control_ip_offload.cmd = CONTROL_IP_OFFLOAD;
2884 crq.control_ip_offload.len =
2885 cpu_to_be32(sizeof(adapter->ip_offload_ctrl));
2886 crq.control_ip_offload.ioba = cpu_to_be32(adapter->ip_offload_ctrl_tok);
2887 ibmvnic_send_crq(adapter, &crq);
2888}
2889
2890static void handle_error_info_rsp(union ibmvnic_crq *crq,
2891 struct ibmvnic_adapter *adapter)
2892{
2893 struct device *dev = &adapter->vdev->dev;
Wei Yongjun96183182016-06-27 20:48:53 +08002894 struct ibmvnic_error_buff *error_buff, *tmp;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002895 unsigned long flags;
2896 bool found = false;
2897 int i;
2898
2899 if (!crq->request_error_rsp.rc.code) {
2900 dev_info(dev, "Request Error Rsp returned with rc=%x\n",
2901 crq->request_error_rsp.rc.code);
2902 return;
2903 }
2904
2905 spin_lock_irqsave(&adapter->error_list_lock, flags);
Wei Yongjun96183182016-06-27 20:48:53 +08002906 list_for_each_entry_safe(error_buff, tmp, &adapter->errors, list)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002907 if (error_buff->error_id == crq->request_error_rsp.error_id) {
2908 found = true;
2909 list_del(&error_buff->list);
2910 break;
2911 }
2912 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
2913
2914 if (!found) {
2915 dev_err(dev, "Couldn't find error id %x\n",
Thomas Falcon75224c92017-02-15 10:33:33 -06002916 be32_to_cpu(crq->request_error_rsp.error_id));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002917 return;
2918 }
2919
2920 dev_err(dev, "Detailed info for error id %x:",
Thomas Falcon75224c92017-02-15 10:33:33 -06002921 be32_to_cpu(crq->request_error_rsp.error_id));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002922
2923 for (i = 0; i < error_buff->len; i++) {
2924 pr_cont("%02x", (int)error_buff->buff[i]);
2925 if (i % 8 == 7)
2926 pr_cont(" ");
2927 }
2928 pr_cont("\n");
2929
2930 dma_unmap_single(dev, error_buff->dma, error_buff->len,
2931 DMA_FROM_DEVICE);
2932 kfree(error_buff->buff);
2933 kfree(error_buff);
2934}
2935
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04002936static void request_error_information(struct ibmvnic_adapter *adapter,
2937 union ibmvnic_crq *err_crq)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002938{
Thomas Falcon032c5e82015-12-21 11:26:06 -06002939 struct device *dev = &adapter->vdev->dev;
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04002940 struct net_device *netdev = adapter->netdev;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002941 struct ibmvnic_error_buff *error_buff;
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04002942 unsigned long timeout = msecs_to_jiffies(30000);
2943 union ibmvnic_crq crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002944 unsigned long flags;
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04002945 int rc, detail_len;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002946
2947 error_buff = kmalloc(sizeof(*error_buff), GFP_ATOMIC);
2948 if (!error_buff)
2949 return;
2950
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04002951 detail_len = be32_to_cpu(err_crq->error_indication.detail_error_sz);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002952 error_buff->buff = kmalloc(detail_len, GFP_ATOMIC);
2953 if (!error_buff->buff) {
2954 kfree(error_buff);
2955 return;
2956 }
2957
2958 error_buff->dma = dma_map_single(dev, error_buff->buff, detail_len,
2959 DMA_FROM_DEVICE);
2960 if (dma_mapping_error(dev, error_buff->dma)) {
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04002961 netdev_err(netdev, "Couldn't map error buffer\n");
Thomas Falcon032c5e82015-12-21 11:26:06 -06002962 kfree(error_buff->buff);
2963 kfree(error_buff);
2964 return;
2965 }
2966
Thomas Falcon032c5e82015-12-21 11:26:06 -06002967 error_buff->len = detail_len;
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04002968 error_buff->error_id = err_crq->error_indication.error_id;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002969
2970 spin_lock_irqsave(&adapter->error_list_lock, flags);
2971 list_add_tail(&error_buff->list, &adapter->errors);
2972 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
2973
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04002974 memset(&crq, 0, sizeof(crq));
2975 crq.request_error_info.first = IBMVNIC_CRQ_CMD;
2976 crq.request_error_info.cmd = REQUEST_ERROR_INFO;
2977 crq.request_error_info.ioba = cpu_to_be32(error_buff->dma);
2978 crq.request_error_info.len = cpu_to_be32(detail_len);
2979 crq.request_error_info.error_id = err_crq->error_indication.error_id;
2980
2981 rc = ibmvnic_send_crq(adapter, &crq);
2982 if (rc) {
2983 netdev_err(netdev, "failed to request error information\n");
2984 goto err_info_fail;
2985 }
2986
2987 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
2988 netdev_err(netdev, "timeout waiting for error information\n");
2989 goto err_info_fail;
2990 }
2991
2992 return;
2993
2994err_info_fail:
2995 spin_lock_irqsave(&adapter->error_list_lock, flags);
2996 list_del(&error_buff->list);
2997 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
2998
2999 kfree(error_buff->buff);
3000 kfree(error_buff);
3001}
3002
3003static void handle_error_indication(union ibmvnic_crq *crq,
3004 struct ibmvnic_adapter *adapter)
3005{
3006 struct device *dev = &adapter->vdev->dev;
3007
3008 dev_err(dev, "Firmware reports %serror id %x, cause %d\n",
3009 crq->error_indication.flags
3010 & IBMVNIC_FATAL_ERROR ? "FATAL " : "",
3011 be32_to_cpu(crq->error_indication.error_id),
3012 be16_to_cpu(crq->error_indication.error_cause));
3013
3014 if (be32_to_cpu(crq->error_indication.error_id))
3015 request_error_information(adapter, crq);
Nathan Fontenoted651a12017-05-03 14:04:38 -04003016
3017 if (crq->error_indication.flags & IBMVNIC_FATAL_ERROR)
3018 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
John Allen8cb31cf2017-05-26 10:30:37 -04003019 else
3020 ibmvnic_reset(adapter, VNIC_RESET_NON_FATAL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003021}
3022
3023static void handle_change_mac_rsp(union ibmvnic_crq *crq,
3024 struct ibmvnic_adapter *adapter)
3025{
3026 struct net_device *netdev = adapter->netdev;
3027 struct device *dev = &adapter->vdev->dev;
3028 long rc;
3029
3030 rc = crq->change_mac_addr_rsp.rc.code;
3031 if (rc) {
3032 dev_err(dev, "Error %ld in CHANGE_MAC_ADDR_RSP\n", rc);
3033 return;
3034 }
3035 memcpy(netdev->dev_addr, &crq->change_mac_addr_rsp.mac_addr[0],
3036 ETH_ALEN);
3037}
3038
3039static void handle_request_cap_rsp(union ibmvnic_crq *crq,
3040 struct ibmvnic_adapter *adapter)
3041{
3042 struct device *dev = &adapter->vdev->dev;
3043 u64 *req_value;
3044 char *name;
3045
Thomas Falcon901e0402017-02-15 12:17:59 -06003046 atomic_dec(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003047 switch (be16_to_cpu(crq->request_capability_rsp.capability)) {
3048 case REQ_TX_QUEUES:
3049 req_value = &adapter->req_tx_queues;
3050 name = "tx";
3051 break;
3052 case REQ_RX_QUEUES:
3053 req_value = &adapter->req_rx_queues;
3054 name = "rx";
3055 break;
3056 case REQ_RX_ADD_QUEUES:
3057 req_value = &adapter->req_rx_add_queues;
3058 name = "rx_add";
3059 break;
3060 case REQ_TX_ENTRIES_PER_SUBCRQ:
3061 req_value = &adapter->req_tx_entries_per_subcrq;
3062 name = "tx_entries_per_subcrq";
3063 break;
3064 case REQ_RX_ADD_ENTRIES_PER_SUBCRQ:
3065 req_value = &adapter->req_rx_add_entries_per_subcrq;
3066 name = "rx_add_entries_per_subcrq";
3067 break;
3068 case REQ_MTU:
3069 req_value = &adapter->req_mtu;
3070 name = "mtu";
3071 break;
3072 case PROMISC_REQUESTED:
3073 req_value = &adapter->promisc;
3074 name = "promisc";
3075 break;
3076 default:
3077 dev_err(dev, "Got invalid cap request rsp %d\n",
3078 crq->request_capability.capability);
3079 return;
3080 }
3081
3082 switch (crq->request_capability_rsp.rc.code) {
3083 case SUCCESS:
3084 break;
3085 case PARTIALSUCCESS:
3086 dev_info(dev, "req=%lld, rsp=%ld in %s queue, retrying.\n",
3087 *req_value,
Thomas Falcon28f4d162017-02-15 10:32:11 -06003088 (long int)be64_to_cpu(crq->request_capability_rsp.
Thomas Falcon032c5e82015-12-21 11:26:06 -06003089 number), name);
Nathan Fontenotb5108882017-03-30 02:49:18 -04003090 release_sub_crqs(adapter);
Thomas Falcon28f4d162017-02-15 10:32:11 -06003091 *req_value = be64_to_cpu(crq->request_capability_rsp.number);
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003092 ibmvnic_send_req_caps(adapter, 1);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003093 return;
3094 default:
3095 dev_err(dev, "Error %d in request cap rsp\n",
3096 crq->request_capability_rsp.rc.code);
3097 return;
3098 }
3099
3100 /* Done receiving requested capabilities, query IP offload support */
Thomas Falcon901e0402017-02-15 12:17:59 -06003101 if (atomic_read(&adapter->running_cap_crqs) == 0) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06003102 union ibmvnic_crq newcrq;
3103 int buf_sz = sizeof(struct ibmvnic_query_ip_offload_buffer);
3104 struct ibmvnic_query_ip_offload_buffer *ip_offload_buf =
3105 &adapter->ip_offload_buf;
3106
Thomas Falcon249168a2017-02-15 12:18:00 -06003107 adapter->wait_capability = false;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003108 adapter->ip_offload_tok = dma_map_single(dev, ip_offload_buf,
3109 buf_sz,
3110 DMA_FROM_DEVICE);
3111
3112 if (dma_mapping_error(dev, adapter->ip_offload_tok)) {
3113 if (!firmware_has_feature(FW_FEATURE_CMO))
3114 dev_err(dev, "Couldn't map offload buffer\n");
3115 return;
3116 }
3117
3118 memset(&newcrq, 0, sizeof(newcrq));
3119 newcrq.query_ip_offload.first = IBMVNIC_CRQ_CMD;
3120 newcrq.query_ip_offload.cmd = QUERY_IP_OFFLOAD;
3121 newcrq.query_ip_offload.len = cpu_to_be32(buf_sz);
3122 newcrq.query_ip_offload.ioba =
3123 cpu_to_be32(adapter->ip_offload_tok);
3124
3125 ibmvnic_send_crq(adapter, &newcrq);
3126 }
3127}
3128
3129static int handle_login_rsp(union ibmvnic_crq *login_rsp_crq,
3130 struct ibmvnic_adapter *adapter)
3131{
3132 struct device *dev = &adapter->vdev->dev;
3133 struct ibmvnic_login_rsp_buffer *login_rsp = adapter->login_rsp_buf;
3134 struct ibmvnic_login_buffer *login = adapter->login_buf;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003135 int i;
3136
3137 dma_unmap_single(dev, adapter->login_buf_token, adapter->login_buf_sz,
3138 DMA_BIDIRECTIONAL);
3139 dma_unmap_single(dev, adapter->login_rsp_buf_token,
3140 adapter->login_rsp_buf_sz, DMA_BIDIRECTIONAL);
3141
John Allen498cd8e2016-04-06 11:49:55 -05003142 /* If the number of queues requested can't be allocated by the
3143 * server, the login response will return with code 1. We will need
3144 * to resend the login buffer with fewer queues requested.
3145 */
3146 if (login_rsp_crq->generic.rc.code) {
3147 adapter->renegotiate = true;
3148 complete(&adapter->init_done);
3149 return 0;
3150 }
3151
Thomas Falcon032c5e82015-12-21 11:26:06 -06003152 netdev_dbg(adapter->netdev, "Login Response Buffer:\n");
3153 for (i = 0; i < (adapter->login_rsp_buf_sz - 1) / 8 + 1; i++) {
3154 netdev_dbg(adapter->netdev, "%016lx\n",
3155 ((unsigned long int *)(adapter->login_rsp_buf))[i]);
3156 }
3157
3158 /* Sanity checks */
3159 if (login->num_txcomp_subcrqs != login_rsp->num_txsubm_subcrqs ||
3160 (be32_to_cpu(login->num_rxcomp_subcrqs) *
3161 adapter->req_rx_add_queues !=
3162 be32_to_cpu(login_rsp->num_rxadd_subcrqs))) {
3163 dev_err(dev, "FATAL: Inconsistent login and login rsp\n");
3164 ibmvnic_remove(adapter->vdev);
3165 return -EIO;
3166 }
3167 complete(&adapter->init_done);
3168
Thomas Falcon032c5e82015-12-21 11:26:06 -06003169 return 0;
3170}
3171
Thomas Falcon032c5e82015-12-21 11:26:06 -06003172static void handle_request_unmap_rsp(union ibmvnic_crq *crq,
3173 struct ibmvnic_adapter *adapter)
3174{
3175 struct device *dev = &adapter->vdev->dev;
3176 long rc;
3177
3178 rc = crq->request_unmap_rsp.rc.code;
3179 if (rc)
3180 dev_err(dev, "Error %ld in REQUEST_UNMAP_RSP\n", rc);
3181}
3182
3183static void handle_query_map_rsp(union ibmvnic_crq *crq,
3184 struct ibmvnic_adapter *adapter)
3185{
3186 struct net_device *netdev = adapter->netdev;
3187 struct device *dev = &adapter->vdev->dev;
3188 long rc;
3189
3190 rc = crq->query_map_rsp.rc.code;
3191 if (rc) {
3192 dev_err(dev, "Error %ld in QUERY_MAP_RSP\n", rc);
3193 return;
3194 }
3195 netdev_dbg(netdev, "page_size = %d\ntot_pages = %d\nfree_pages = %d\n",
3196 crq->query_map_rsp.page_size, crq->query_map_rsp.tot_pages,
3197 crq->query_map_rsp.free_pages);
3198}
3199
3200static void handle_query_cap_rsp(union ibmvnic_crq *crq,
3201 struct ibmvnic_adapter *adapter)
3202{
3203 struct net_device *netdev = adapter->netdev;
3204 struct device *dev = &adapter->vdev->dev;
3205 long rc;
3206
Thomas Falcon901e0402017-02-15 12:17:59 -06003207 atomic_dec(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003208 netdev_dbg(netdev, "Outstanding queries: %d\n",
Thomas Falcon901e0402017-02-15 12:17:59 -06003209 atomic_read(&adapter->running_cap_crqs));
Thomas Falcon032c5e82015-12-21 11:26:06 -06003210 rc = crq->query_capability.rc.code;
3211 if (rc) {
3212 dev_err(dev, "Error %ld in QUERY_CAP_RSP\n", rc);
3213 goto out;
3214 }
3215
3216 switch (be16_to_cpu(crq->query_capability.capability)) {
3217 case MIN_TX_QUEUES:
3218 adapter->min_tx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003219 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003220 netdev_dbg(netdev, "min_tx_queues = %lld\n",
3221 adapter->min_tx_queues);
3222 break;
3223 case MIN_RX_QUEUES:
3224 adapter->min_rx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003225 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003226 netdev_dbg(netdev, "min_rx_queues = %lld\n",
3227 adapter->min_rx_queues);
3228 break;
3229 case MIN_RX_ADD_QUEUES:
3230 adapter->min_rx_add_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003231 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003232 netdev_dbg(netdev, "min_rx_add_queues = %lld\n",
3233 adapter->min_rx_add_queues);
3234 break;
3235 case MAX_TX_QUEUES:
3236 adapter->max_tx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003237 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003238 netdev_dbg(netdev, "max_tx_queues = %lld\n",
3239 adapter->max_tx_queues);
3240 break;
3241 case MAX_RX_QUEUES:
3242 adapter->max_rx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003243 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003244 netdev_dbg(netdev, "max_rx_queues = %lld\n",
3245 adapter->max_rx_queues);
3246 break;
3247 case MAX_RX_ADD_QUEUES:
3248 adapter->max_rx_add_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003249 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003250 netdev_dbg(netdev, "max_rx_add_queues = %lld\n",
3251 adapter->max_rx_add_queues);
3252 break;
3253 case MIN_TX_ENTRIES_PER_SUBCRQ:
3254 adapter->min_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06003255 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003256 netdev_dbg(netdev, "min_tx_entries_per_subcrq = %lld\n",
3257 adapter->min_tx_entries_per_subcrq);
3258 break;
3259 case MIN_RX_ADD_ENTRIES_PER_SUBCRQ:
3260 adapter->min_rx_add_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06003261 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003262 netdev_dbg(netdev, "min_rx_add_entrs_per_subcrq = %lld\n",
3263 adapter->min_rx_add_entries_per_subcrq);
3264 break;
3265 case MAX_TX_ENTRIES_PER_SUBCRQ:
3266 adapter->max_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06003267 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003268 netdev_dbg(netdev, "max_tx_entries_per_subcrq = %lld\n",
3269 adapter->max_tx_entries_per_subcrq);
3270 break;
3271 case MAX_RX_ADD_ENTRIES_PER_SUBCRQ:
3272 adapter->max_rx_add_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06003273 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003274 netdev_dbg(netdev, "max_rx_add_entrs_per_subcrq = %lld\n",
3275 adapter->max_rx_add_entries_per_subcrq);
3276 break;
3277 case TCP_IP_OFFLOAD:
3278 adapter->tcp_ip_offload =
Thomas Falconde89e852016-03-01 10:20:09 -06003279 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003280 netdev_dbg(netdev, "tcp_ip_offload = %lld\n",
3281 adapter->tcp_ip_offload);
3282 break;
3283 case PROMISC_SUPPORTED:
3284 adapter->promisc_supported =
Thomas Falconde89e852016-03-01 10:20:09 -06003285 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003286 netdev_dbg(netdev, "promisc_supported = %lld\n",
3287 adapter->promisc_supported);
3288 break;
3289 case MIN_MTU:
Thomas Falconde89e852016-03-01 10:20:09 -06003290 adapter->min_mtu = be64_to_cpu(crq->query_capability.number);
Thomas Falconf39f0d12017-02-14 10:22:59 -06003291 netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003292 netdev_dbg(netdev, "min_mtu = %lld\n", adapter->min_mtu);
3293 break;
3294 case MAX_MTU:
Thomas Falconde89e852016-03-01 10:20:09 -06003295 adapter->max_mtu = be64_to_cpu(crq->query_capability.number);
Thomas Falconf39f0d12017-02-14 10:22:59 -06003296 netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003297 netdev_dbg(netdev, "max_mtu = %lld\n", adapter->max_mtu);
3298 break;
3299 case MAX_MULTICAST_FILTERS:
3300 adapter->max_multicast_filters =
Thomas Falconde89e852016-03-01 10:20:09 -06003301 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003302 netdev_dbg(netdev, "max_multicast_filters = %lld\n",
3303 adapter->max_multicast_filters);
3304 break;
3305 case VLAN_HEADER_INSERTION:
3306 adapter->vlan_header_insertion =
Thomas Falconde89e852016-03-01 10:20:09 -06003307 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003308 if (adapter->vlan_header_insertion)
3309 netdev->features |= NETIF_F_HW_VLAN_STAG_TX;
3310 netdev_dbg(netdev, "vlan_header_insertion = %lld\n",
3311 adapter->vlan_header_insertion);
3312 break;
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04003313 case RX_VLAN_HEADER_INSERTION:
3314 adapter->rx_vlan_header_insertion =
3315 be64_to_cpu(crq->query_capability.number);
3316 netdev_dbg(netdev, "rx_vlan_header_insertion = %lld\n",
3317 adapter->rx_vlan_header_insertion);
3318 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003319 case MAX_TX_SG_ENTRIES:
3320 adapter->max_tx_sg_entries =
Thomas Falconde89e852016-03-01 10:20:09 -06003321 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003322 netdev_dbg(netdev, "max_tx_sg_entries = %lld\n",
3323 adapter->max_tx_sg_entries);
3324 break;
3325 case RX_SG_SUPPORTED:
3326 adapter->rx_sg_supported =
Thomas Falconde89e852016-03-01 10:20:09 -06003327 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003328 netdev_dbg(netdev, "rx_sg_supported = %lld\n",
3329 adapter->rx_sg_supported);
3330 break;
3331 case OPT_TX_COMP_SUB_QUEUES:
3332 adapter->opt_tx_comp_sub_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003333 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003334 netdev_dbg(netdev, "opt_tx_comp_sub_queues = %lld\n",
3335 adapter->opt_tx_comp_sub_queues);
3336 break;
3337 case OPT_RX_COMP_QUEUES:
3338 adapter->opt_rx_comp_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003339 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003340 netdev_dbg(netdev, "opt_rx_comp_queues = %lld\n",
3341 adapter->opt_rx_comp_queues);
3342 break;
3343 case OPT_RX_BUFADD_Q_PER_RX_COMP_Q:
3344 adapter->opt_rx_bufadd_q_per_rx_comp_q =
Thomas Falconde89e852016-03-01 10:20:09 -06003345 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003346 netdev_dbg(netdev, "opt_rx_bufadd_q_per_rx_comp_q = %lld\n",
3347 adapter->opt_rx_bufadd_q_per_rx_comp_q);
3348 break;
3349 case OPT_TX_ENTRIES_PER_SUBCRQ:
3350 adapter->opt_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06003351 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003352 netdev_dbg(netdev, "opt_tx_entries_per_subcrq = %lld\n",
3353 adapter->opt_tx_entries_per_subcrq);
3354 break;
3355 case OPT_RXBA_ENTRIES_PER_SUBCRQ:
3356 adapter->opt_rxba_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06003357 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003358 netdev_dbg(netdev, "opt_rxba_entries_per_subcrq = %lld\n",
3359 adapter->opt_rxba_entries_per_subcrq);
3360 break;
3361 case TX_RX_DESC_REQ:
3362 adapter->tx_rx_desc_req = crq->query_capability.number;
3363 netdev_dbg(netdev, "tx_rx_desc_req = %llx\n",
3364 adapter->tx_rx_desc_req);
3365 break;
3366
3367 default:
3368 netdev_err(netdev, "Got invalid cap rsp %d\n",
3369 crq->query_capability.capability);
3370 }
3371
3372out:
Thomas Falcon249168a2017-02-15 12:18:00 -06003373 if (atomic_read(&adapter->running_cap_crqs) == 0) {
3374 adapter->wait_capability = false;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003375 ibmvnic_send_req_caps(adapter, 0);
Thomas Falcon249168a2017-02-15 12:18:00 -06003376 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06003377}
3378
Thomas Falcon032c5e82015-12-21 11:26:06 -06003379static void ibmvnic_handle_crq(union ibmvnic_crq *crq,
3380 struct ibmvnic_adapter *adapter)
3381{
3382 struct ibmvnic_generic_crq *gen_crq = &crq->generic;
3383 struct net_device *netdev = adapter->netdev;
3384 struct device *dev = &adapter->vdev->dev;
Murilo Fossa Vicentini993a82b2017-04-19 13:44:35 -04003385 u64 *u64_crq = (u64 *)crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003386 long rc;
3387
3388 netdev_dbg(netdev, "Handling CRQ: %016lx %016lx\n",
Murilo Fossa Vicentini993a82b2017-04-19 13:44:35 -04003389 (unsigned long int)cpu_to_be64(u64_crq[0]),
3390 (unsigned long int)cpu_to_be64(u64_crq[1]));
Thomas Falcon032c5e82015-12-21 11:26:06 -06003391 switch (gen_crq->first) {
3392 case IBMVNIC_CRQ_INIT_RSP:
3393 switch (gen_crq->cmd) {
3394 case IBMVNIC_CRQ_INIT:
3395 dev_info(dev, "Partner initialized\n");
John Allen017892c12017-05-26 10:30:19 -04003396 adapter->from_passive_init = true;
3397 complete(&adapter->init_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003398 break;
3399 case IBMVNIC_CRQ_INIT_COMPLETE:
3400 dev_info(dev, "Partner initialization complete\n");
3401 send_version_xchg(adapter);
3402 break;
3403 default:
3404 dev_err(dev, "Unknown crq cmd: %d\n", gen_crq->cmd);
3405 }
3406 return;
3407 case IBMVNIC_CRQ_XPORT_EVENT:
Nathan Fontenoted651a12017-05-03 14:04:38 -04003408 netif_carrier_off(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003409 if (gen_crq->cmd == IBMVNIC_PARTITION_MIGRATED) {
Nathan Fontenoted651a12017-05-03 14:04:38 -04003410 dev_info(dev, "Migrated, re-enabling adapter\n");
3411 ibmvnic_reset(adapter, VNIC_RESET_MOBILITY);
Thomas Falcondfad09a2016-08-18 11:37:51 -05003412 } else if (gen_crq->cmd == IBMVNIC_DEVICE_FAILOVER) {
3413 dev_info(dev, "Backing device failover detected\n");
Nathan Fontenoted651a12017-05-03 14:04:38 -04003414 ibmvnic_reset(adapter, VNIC_RESET_FAILOVER);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003415 } else {
3416 /* The adapter lost the connection */
3417 dev_err(dev, "Virtual Adapter failed (rc=%d)\n",
3418 gen_crq->cmd);
Nathan Fontenoted651a12017-05-03 14:04:38 -04003419 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003420 }
3421 return;
3422 case IBMVNIC_CRQ_CMD_RSP:
3423 break;
3424 default:
3425 dev_err(dev, "Got an invalid msg type 0x%02x\n",
3426 gen_crq->first);
3427 return;
3428 }
3429
3430 switch (gen_crq->cmd) {
3431 case VERSION_EXCHANGE_RSP:
3432 rc = crq->version_exchange_rsp.rc.code;
3433 if (rc) {
3434 dev_err(dev, "Error %ld in VERSION_EXCHG_RSP\n", rc);
3435 break;
3436 }
3437 dev_info(dev, "Partner protocol version is %d\n",
3438 crq->version_exchange_rsp.version);
3439 if (be16_to_cpu(crq->version_exchange_rsp.version) <
3440 ibmvnic_version)
3441 ibmvnic_version =
3442 be16_to_cpu(crq->version_exchange_rsp.version);
3443 send_cap_queries(adapter);
3444 break;
3445 case QUERY_CAPABILITY_RSP:
3446 handle_query_cap_rsp(crq, adapter);
3447 break;
3448 case QUERY_MAP_RSP:
3449 handle_query_map_rsp(crq, adapter);
3450 break;
3451 case REQUEST_MAP_RSP:
Thomas Falconf3be0cb2017-06-21 14:53:01 -05003452 adapter->fw_done_rc = crq->request_map_rsp.rc.code;
3453 complete(&adapter->fw_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003454 break;
3455 case REQUEST_UNMAP_RSP:
3456 handle_request_unmap_rsp(crq, adapter);
3457 break;
3458 case REQUEST_CAPABILITY_RSP:
3459 handle_request_cap_rsp(crq, adapter);
3460 break;
3461 case LOGIN_RSP:
3462 netdev_dbg(netdev, "Got Login Response\n");
3463 handle_login_rsp(crq, adapter);
3464 break;
3465 case LOGICAL_LINK_STATE_RSP:
Nathan Fontenot53da09e2017-04-21 15:39:04 -04003466 netdev_dbg(netdev,
3467 "Got Logical Link State Response, state: %d rc: %d\n",
3468 crq->logical_link_state_rsp.link_state,
3469 crq->logical_link_state_rsp.rc.code);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003470 adapter->logical_link_state =
3471 crq->logical_link_state_rsp.link_state;
Nathan Fontenot53da09e2017-04-21 15:39:04 -04003472 adapter->init_done_rc = crq->logical_link_state_rsp.rc.code;
3473 complete(&adapter->init_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003474 break;
3475 case LINK_STATE_INDICATION:
3476 netdev_dbg(netdev, "Got Logical Link State Indication\n");
3477 adapter->phys_link_state =
3478 crq->link_state_indication.phys_link_state;
3479 adapter->logical_link_state =
3480 crq->link_state_indication.logical_link_state;
3481 break;
3482 case CHANGE_MAC_ADDR_RSP:
3483 netdev_dbg(netdev, "Got MAC address change Response\n");
3484 handle_change_mac_rsp(crq, adapter);
3485 break;
3486 case ERROR_INDICATION:
3487 netdev_dbg(netdev, "Got Error Indication\n");
3488 handle_error_indication(crq, adapter);
3489 break;
3490 case REQUEST_ERROR_RSP:
3491 netdev_dbg(netdev, "Got Error Detail Response\n");
3492 handle_error_info_rsp(crq, adapter);
3493 break;
3494 case REQUEST_STATISTICS_RSP:
3495 netdev_dbg(netdev, "Got Statistics Response\n");
3496 complete(&adapter->stats_done);
3497 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003498 case QUERY_IP_OFFLOAD_RSP:
3499 netdev_dbg(netdev, "Got Query IP offload Response\n");
3500 handle_query_ip_offload_rsp(adapter);
3501 break;
3502 case MULTICAST_CTRL_RSP:
3503 netdev_dbg(netdev, "Got multicast control Response\n");
3504 break;
3505 case CONTROL_IP_OFFLOAD_RSP:
3506 netdev_dbg(netdev, "Got Control IP offload Response\n");
3507 dma_unmap_single(dev, adapter->ip_offload_ctrl_tok,
3508 sizeof(adapter->ip_offload_ctrl),
3509 DMA_TO_DEVICE);
John Allenbd0b6722017-03-17 17:13:40 -05003510 complete(&adapter->init_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003511 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003512 case COLLECT_FW_TRACE_RSP:
3513 netdev_dbg(netdev, "Got Collect firmware trace Response\n");
3514 complete(&adapter->fw_done);
3515 break;
3516 default:
3517 netdev_err(netdev, "Got an invalid cmd type 0x%02x\n",
3518 gen_crq->cmd);
3519 }
3520}
3521
3522static irqreturn_t ibmvnic_interrupt(int irq, void *instance)
3523{
3524 struct ibmvnic_adapter *adapter = instance;
Thomas Falcon6c267b32017-02-15 12:17:58 -06003525
Thomas Falcon6c267b32017-02-15 12:17:58 -06003526 tasklet_schedule(&adapter->tasklet);
Thomas Falcon6c267b32017-02-15 12:17:58 -06003527 return IRQ_HANDLED;
3528}
3529
3530static void ibmvnic_tasklet(void *data)
3531{
3532 struct ibmvnic_adapter *adapter = data;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003533 struct ibmvnic_crq_queue *queue = &adapter->crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003534 union ibmvnic_crq *crq;
3535 unsigned long flags;
3536 bool done = false;
3537
3538 spin_lock_irqsave(&queue->lock, flags);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003539 while (!done) {
3540 /* Pull all the valid messages off the CRQ */
3541 while ((crq = ibmvnic_next_crq(adapter)) != NULL) {
3542 ibmvnic_handle_crq(crq, adapter);
3543 crq->generic.first = 0;
3544 }
Brian Kinged7ecbf2017-04-19 13:44:53 -04003545
3546 /* remain in tasklet until all
3547 * capabilities responses are received
3548 */
3549 if (!adapter->wait_capability)
3550 done = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003551 }
Thomas Falcon249168a2017-02-15 12:18:00 -06003552 /* if capabilities CRQ's were sent in this tasklet, the following
3553 * tasklet must wait until all responses are received
3554 */
3555 if (atomic_read(&adapter->running_cap_crqs) != 0)
3556 adapter->wait_capability = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003557 spin_unlock_irqrestore(&queue->lock, flags);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003558}
3559
3560static int ibmvnic_reenable_crq_queue(struct ibmvnic_adapter *adapter)
3561{
3562 struct vio_dev *vdev = adapter->vdev;
3563 int rc;
3564
3565 do {
3566 rc = plpar_hcall_norets(H_ENABLE_CRQ, vdev->unit_address);
3567 } while (rc == H_IN_PROGRESS || rc == H_BUSY || H_IS_LONG_BUSY(rc));
3568
3569 if (rc)
3570 dev_err(&vdev->dev, "Error enabling adapter (rc=%d)\n", rc);
3571
3572 return rc;
3573}
3574
3575static int ibmvnic_reset_crq(struct ibmvnic_adapter *adapter)
3576{
3577 struct ibmvnic_crq_queue *crq = &adapter->crq;
3578 struct device *dev = &adapter->vdev->dev;
3579 struct vio_dev *vdev = adapter->vdev;
3580 int rc;
3581
3582 /* Close the CRQ */
3583 do {
3584 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
3585 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
3586
3587 /* Clean out the queue */
3588 memset(crq->msgs, 0, PAGE_SIZE);
3589 crq->cur = 0;
3590
3591 /* And re-open it again */
3592 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
3593 crq->msg_token, PAGE_SIZE);
3594
3595 if (rc == H_CLOSED)
3596 /* Adapter is good, but other end is not ready */
3597 dev_warn(dev, "Partner adapter not ready\n");
3598 else if (rc != 0)
3599 dev_warn(dev, "Couldn't register crq (rc=%d)\n", rc);
3600
3601 return rc;
3602}
3603
Nathan Fontenotf9928872017-03-30 02:48:54 -04003604static void release_crq_queue(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003605{
3606 struct ibmvnic_crq_queue *crq = &adapter->crq;
3607 struct vio_dev *vdev = adapter->vdev;
3608 long rc;
3609
Nathan Fontenotf9928872017-03-30 02:48:54 -04003610 if (!crq->msgs)
3611 return;
3612
Thomas Falcon032c5e82015-12-21 11:26:06 -06003613 netdev_dbg(adapter->netdev, "Releasing CRQ\n");
3614 free_irq(vdev->irq, adapter);
Thomas Falcon6c267b32017-02-15 12:17:58 -06003615 tasklet_kill(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003616 do {
3617 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
3618 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
3619
3620 dma_unmap_single(&vdev->dev, crq->msg_token, PAGE_SIZE,
3621 DMA_BIDIRECTIONAL);
3622 free_page((unsigned long)crq->msgs);
Nathan Fontenotf9928872017-03-30 02:48:54 -04003623 crq->msgs = NULL;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003624}
3625
Nathan Fontenotf9928872017-03-30 02:48:54 -04003626static int init_crq_queue(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003627{
3628 struct ibmvnic_crq_queue *crq = &adapter->crq;
3629 struct device *dev = &adapter->vdev->dev;
3630 struct vio_dev *vdev = adapter->vdev;
3631 int rc, retrc = -ENOMEM;
3632
Nathan Fontenotf9928872017-03-30 02:48:54 -04003633 if (crq->msgs)
3634 return 0;
3635
Thomas Falcon032c5e82015-12-21 11:26:06 -06003636 crq->msgs = (union ibmvnic_crq *)get_zeroed_page(GFP_KERNEL);
3637 /* Should we allocate more than one page? */
3638
3639 if (!crq->msgs)
3640 return -ENOMEM;
3641
3642 crq->size = PAGE_SIZE / sizeof(*crq->msgs);
3643 crq->msg_token = dma_map_single(dev, crq->msgs, PAGE_SIZE,
3644 DMA_BIDIRECTIONAL);
3645 if (dma_mapping_error(dev, crq->msg_token))
3646 goto map_failed;
3647
3648 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
3649 crq->msg_token, PAGE_SIZE);
3650
3651 if (rc == H_RESOURCE)
3652 /* maybe kexecing and resource is busy. try a reset */
3653 rc = ibmvnic_reset_crq(adapter);
3654 retrc = rc;
3655
3656 if (rc == H_CLOSED) {
3657 dev_warn(dev, "Partner adapter not ready\n");
3658 } else if (rc) {
3659 dev_warn(dev, "Error %d opening adapter\n", rc);
3660 goto reg_crq_failed;
3661 }
3662
3663 retrc = 0;
3664
Thomas Falcon6c267b32017-02-15 12:17:58 -06003665 tasklet_init(&adapter->tasklet, (void *)ibmvnic_tasklet,
3666 (unsigned long)adapter);
3667
Thomas Falcon032c5e82015-12-21 11:26:06 -06003668 netdev_dbg(adapter->netdev, "registering irq 0x%x\n", vdev->irq);
3669 rc = request_irq(vdev->irq, ibmvnic_interrupt, 0, IBMVNIC_NAME,
3670 adapter);
3671 if (rc) {
3672 dev_err(dev, "Couldn't register irq 0x%x. rc=%d\n",
3673 vdev->irq, rc);
3674 goto req_irq_failed;
3675 }
3676
3677 rc = vio_enable_interrupts(vdev);
3678 if (rc) {
3679 dev_err(dev, "Error %d enabling interrupts\n", rc);
3680 goto req_irq_failed;
3681 }
3682
3683 crq->cur = 0;
3684 spin_lock_init(&crq->lock);
3685
3686 return retrc;
3687
3688req_irq_failed:
Thomas Falcon6c267b32017-02-15 12:17:58 -06003689 tasklet_kill(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003690 do {
3691 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
3692 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
3693reg_crq_failed:
3694 dma_unmap_single(dev, crq->msg_token, PAGE_SIZE, DMA_BIDIRECTIONAL);
3695map_failed:
3696 free_page((unsigned long)crq->msgs);
Nathan Fontenotf9928872017-03-30 02:48:54 -04003697 crq->msgs = NULL;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003698 return retrc;
3699}
3700
John Allenf6ef6402017-03-17 17:13:42 -05003701static int ibmvnic_init(struct ibmvnic_adapter *adapter)
3702{
3703 struct device *dev = &adapter->vdev->dev;
3704 unsigned long timeout = msecs_to_jiffies(30000);
John Allenf6ef6402017-03-17 17:13:42 -05003705 int rc;
3706
Nathan Fontenot28cde752017-05-26 10:31:00 -04003707 if (adapter->resetting) {
3708 rc = ibmvnic_reset_crq(adapter);
3709 if (!rc)
3710 rc = vio_enable_interrupts(adapter->vdev);
3711 } else {
3712 rc = init_crq_queue(adapter);
3713 }
3714
John Allenf6ef6402017-03-17 17:13:42 -05003715 if (rc) {
3716 dev_err(dev, "Couldn't initialize crq. rc=%d\n", rc);
3717 return rc;
3718 }
3719
John Allen017892c12017-05-26 10:30:19 -04003720 adapter->from_passive_init = false;
3721
John Allenf6ef6402017-03-17 17:13:42 -05003722 init_completion(&adapter->init_done);
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04003723 adapter->init_done_rc = 0;
John Allenf6ef6402017-03-17 17:13:42 -05003724 ibmvnic_send_crq_init(adapter);
3725 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
3726 dev_err(dev, "Initialization sequence timed out\n");
John Allen017892c12017-05-26 10:30:19 -04003727 return -1;
3728 }
3729
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04003730 if (adapter->init_done_rc) {
3731 release_crq_queue(adapter);
3732 return adapter->init_done_rc;
3733 }
3734
John Allen017892c12017-05-26 10:30:19 -04003735 if (adapter->from_passive_init) {
3736 adapter->state = VNIC_OPEN;
3737 adapter->from_passive_init = false;
John Allenf6ef6402017-03-17 17:13:42 -05003738 return -1;
3739 }
3740
Nathan Fontenot57a49432017-05-26 10:31:12 -04003741 if (adapter->resetting)
3742 rc = reset_sub_crq_queues(adapter);
3743 else
3744 rc = init_sub_crqs(adapter);
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04003745 if (rc) {
3746 dev_err(dev, "Initialization of sub crqs failed\n");
3747 release_crq_queue(adapter);
Thomas Falcon5df969c2017-06-28 19:55:54 -05003748 return rc;
3749 }
3750
3751 rc = init_sub_crq_irqs(adapter);
3752 if (rc) {
3753 dev_err(dev, "Failed to initialize sub crq irqs\n");
3754 release_crq_queue(adapter);
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04003755 }
3756
3757 return rc;
John Allenf6ef6402017-03-17 17:13:42 -05003758}
3759
Thomas Falcon40c9db82017-06-12 12:35:04 -05003760static struct device_attribute dev_attr_failover;
3761
Thomas Falcon032c5e82015-12-21 11:26:06 -06003762static int ibmvnic_probe(struct vio_dev *dev, const struct vio_device_id *id)
3763{
3764 struct ibmvnic_adapter *adapter;
3765 struct net_device *netdev;
3766 unsigned char *mac_addr_p;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003767 int rc;
3768
3769 dev_dbg(&dev->dev, "entering ibmvnic_probe for UA 0x%x\n",
3770 dev->unit_address);
3771
3772 mac_addr_p = (unsigned char *)vio_get_attribute(dev,
3773 VETH_MAC_ADDR, NULL);
3774 if (!mac_addr_p) {
3775 dev_err(&dev->dev,
3776 "(%s:%3.3d) ERROR: Can't find MAC_ADDR attribute\n",
3777 __FILE__, __LINE__);
3778 return 0;
3779 }
3780
3781 netdev = alloc_etherdev_mq(sizeof(struct ibmvnic_adapter),
3782 IBMVNIC_MAX_TX_QUEUES);
3783 if (!netdev)
3784 return -ENOMEM;
3785
3786 adapter = netdev_priv(netdev);
Nathan Fontenot90c80142017-05-03 14:04:32 -04003787 adapter->state = VNIC_PROBING;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003788 dev_set_drvdata(&dev->dev, netdev);
3789 adapter->vdev = dev;
3790 adapter->netdev = netdev;
3791
3792 ether_addr_copy(adapter->mac_addr, mac_addr_p);
3793 ether_addr_copy(netdev->dev_addr, adapter->mac_addr);
3794 netdev->irq = dev->irq;
3795 netdev->netdev_ops = &ibmvnic_netdev_ops;
3796 netdev->ethtool_ops = &ibmvnic_ethtool_ops;
3797 SET_NETDEV_DEV(netdev, &dev->dev);
3798
3799 spin_lock_init(&adapter->stats_lock);
3800
Thomas Falcon032c5e82015-12-21 11:26:06 -06003801 INIT_LIST_HEAD(&adapter->errors);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003802 spin_lock_init(&adapter->error_list_lock);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003803
Nathan Fontenoted651a12017-05-03 14:04:38 -04003804 INIT_WORK(&adapter->ibmvnic_reset, __ibmvnic_reset);
3805 INIT_LIST_HEAD(&adapter->rwi_list);
3806 mutex_init(&adapter->reset_lock);
3807 mutex_init(&adapter->rwi_lock);
3808 adapter->resetting = false;
3809
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04003810 do {
3811 rc = ibmvnic_init(adapter);
Nathan Fontenot6d659232017-06-21 15:41:02 -05003812 if (rc && rc != EAGAIN) {
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04003813 free_netdev(netdev);
3814 return rc;
3815 }
3816 } while (rc == EAGAIN);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003817
Thomas Falconf39f0d12017-02-14 10:22:59 -06003818 netdev->mtu = adapter->req_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003819
Thomas Falcon40c9db82017-06-12 12:35:04 -05003820 rc = device_create_file(&dev->dev, &dev_attr_failover);
3821 if (rc) {
3822 free_netdev(netdev);
3823 return rc;
3824 }
3825
Thomas Falcon032c5e82015-12-21 11:26:06 -06003826 rc = register_netdev(netdev);
3827 if (rc) {
3828 dev_err(&dev->dev, "failed to register netdev rc=%d\n", rc);
Thomas Falcon40c9db82017-06-12 12:35:04 -05003829 device_remove_file(&dev->dev, &dev_attr_failover);
John Allenf6ef6402017-03-17 17:13:42 -05003830 free_netdev(netdev);
3831 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003832 }
3833 dev_info(&dev->dev, "ibmvnic registered\n");
3834
Nathan Fontenot90c80142017-05-03 14:04:32 -04003835 adapter->state = VNIC_PROBED;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003836 return 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003837}
3838
3839static int ibmvnic_remove(struct vio_dev *dev)
3840{
3841 struct net_device *netdev = dev_get_drvdata(&dev->dev);
Nathan Fontenot37489052017-04-19 13:45:04 -04003842 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003843
Nathan Fontenot90c80142017-05-03 14:04:32 -04003844 adapter->state = VNIC_REMOVING;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003845 unregister_netdev(netdev);
Nathan Fontenoted651a12017-05-03 14:04:38 -04003846 mutex_lock(&adapter->reset_lock);
Nathan Fontenot37489052017-04-19 13:45:04 -04003847
3848 release_resources(adapter);
3849 release_sub_crqs(adapter);
3850 release_crq_queue(adapter);
3851
Nathan Fontenot90c80142017-05-03 14:04:32 -04003852 adapter->state = VNIC_REMOVED;
3853
Nathan Fontenoted651a12017-05-03 14:04:38 -04003854 mutex_unlock(&adapter->reset_lock);
Thomas Falcon40c9db82017-06-12 12:35:04 -05003855 device_remove_file(&dev->dev, &dev_attr_failover);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003856 free_netdev(netdev);
3857 dev_set_drvdata(&dev->dev, NULL);
3858
3859 return 0;
3860}
3861
Thomas Falcon40c9db82017-06-12 12:35:04 -05003862static ssize_t failover_store(struct device *dev, struct device_attribute *attr,
3863 const char *buf, size_t count)
3864{
3865 struct net_device *netdev = dev_get_drvdata(dev);
3866 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
3867 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
3868 __be64 session_token;
3869 long rc;
3870
3871 if (!sysfs_streq(buf, "1"))
3872 return -EINVAL;
3873
3874 rc = plpar_hcall(H_VIOCTL, retbuf, adapter->vdev->unit_address,
3875 H_GET_SESSION_TOKEN, 0, 0, 0);
3876 if (rc) {
3877 netdev_err(netdev, "Couldn't retrieve session token, rc %ld\n",
3878 rc);
3879 return -EINVAL;
3880 }
3881
3882 session_token = (__be64)retbuf[0];
3883 netdev_dbg(netdev, "Initiating client failover, session id %llx\n",
3884 be64_to_cpu(session_token));
3885 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
3886 H_SESSION_ERR_DETECTED, session_token, 0, 0);
3887 if (rc) {
3888 netdev_err(netdev, "Client initiated failover failed, rc %ld\n",
3889 rc);
3890 return -EINVAL;
3891 }
3892
3893 return count;
3894}
3895
3896static DEVICE_ATTR(failover, 0200, NULL, failover_store);
3897
Thomas Falcon032c5e82015-12-21 11:26:06 -06003898static unsigned long ibmvnic_get_desired_dma(struct vio_dev *vdev)
3899{
3900 struct net_device *netdev = dev_get_drvdata(&vdev->dev);
3901 struct ibmvnic_adapter *adapter;
3902 struct iommu_table *tbl;
3903 unsigned long ret = 0;
3904 int i;
3905
3906 tbl = get_iommu_table_base(&vdev->dev);
3907
3908 /* netdev inits at probe time along with the structures we need below*/
3909 if (!netdev)
3910 return IOMMU_PAGE_ALIGN(IBMVNIC_IO_ENTITLEMENT_DEFAULT, tbl);
3911
3912 adapter = netdev_priv(netdev);
3913
3914 ret += PAGE_SIZE; /* the crq message queue */
Thomas Falcon032c5e82015-12-21 11:26:06 -06003915 ret += IOMMU_PAGE_ALIGN(sizeof(struct ibmvnic_statistics), tbl);
3916
3917 for (i = 0; i < adapter->req_tx_queues + adapter->req_rx_queues; i++)
3918 ret += 4 * PAGE_SIZE; /* the scrq message queue */
3919
3920 for (i = 0; i < be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
3921 i++)
3922 ret += adapter->rx_pool[i].size *
3923 IOMMU_PAGE_ALIGN(adapter->rx_pool[i].buff_size, tbl);
3924
3925 return ret;
3926}
3927
3928static int ibmvnic_resume(struct device *dev)
3929{
3930 struct net_device *netdev = dev_get_drvdata(dev);
3931 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
3932 int i;
3933
John Allencb89ba22017-06-19 11:27:53 -05003934 if (adapter->state != VNIC_OPEN)
3935 return 0;
3936
John Allena2488782017-07-24 13:26:06 -05003937 tasklet_schedule(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003938
3939 return 0;
3940}
3941
3942static struct vio_device_id ibmvnic_device_table[] = {
3943 {"network", "IBM,vnic"},
3944 {"", "" }
3945};
3946MODULE_DEVICE_TABLE(vio, ibmvnic_device_table);
3947
3948static const struct dev_pm_ops ibmvnic_pm_ops = {
3949 .resume = ibmvnic_resume
3950};
3951
3952static struct vio_driver ibmvnic_driver = {
3953 .id_table = ibmvnic_device_table,
3954 .probe = ibmvnic_probe,
3955 .remove = ibmvnic_remove,
3956 .get_desired_dma = ibmvnic_get_desired_dma,
3957 .name = ibmvnic_driver_name,
3958 .pm = &ibmvnic_pm_ops,
3959};
3960
3961/* module functions */
3962static int __init ibmvnic_module_init(void)
3963{
3964 pr_info("%s: %s %s\n", ibmvnic_driver_name, ibmvnic_driver_string,
3965 IBMVNIC_DRIVER_VERSION);
3966
3967 return vio_register_driver(&ibmvnic_driver);
3968}
3969
3970static void __exit ibmvnic_module_exit(void)
3971{
3972 vio_unregister_driver(&ibmvnic_driver);
3973}
3974
3975module_init(ibmvnic_module_init);
3976module_exit(ibmvnic_module_exit);