blob: 5932160eb815d91d51711cb237a65c70710f6696 [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{
John Allenbc131b32017-08-02 16:46:30 -05001732 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1733
1734 ring->rx_max_pending = adapter->max_rx_add_entries_per_subcrq;
1735 ring->tx_max_pending = adapter->max_tx_entries_per_subcrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001736 ring->rx_mini_max_pending = 0;
1737 ring->rx_jumbo_max_pending = 0;
John Allenbc131b32017-08-02 16:46:30 -05001738 ring->rx_pending = adapter->req_rx_add_entries_per_subcrq;
1739 ring->tx_pending = adapter->req_tx_entries_per_subcrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001740 ring->rx_mini_pending = 0;
1741 ring->rx_jumbo_pending = 0;
1742}
1743
John Allenc2dbeb62017-08-02 16:47:17 -05001744static void ibmvnic_get_channels(struct net_device *netdev,
1745 struct ethtool_channels *channels)
1746{
1747 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1748
1749 channels->max_rx = adapter->max_rx_queues;
1750 channels->max_tx = adapter->max_tx_queues;
1751 channels->max_other = 0;
1752 channels->max_combined = 0;
1753 channels->rx_count = adapter->req_rx_queues;
1754 channels->tx_count = adapter->req_tx_queues;
1755 channels->other_count = 0;
1756 channels->combined_count = 0;
1757}
1758
Thomas Falcon032c5e82015-12-21 11:26:06 -06001759static void ibmvnic_get_strings(struct net_device *dev, u32 stringset, u8 *data)
1760{
John Allen3d52b592017-08-02 16:44:14 -05001761 struct ibmvnic_adapter *adapter = netdev_priv(dev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001762 int i;
1763
1764 if (stringset != ETH_SS_STATS)
1765 return;
1766
1767 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++, data += ETH_GSTRING_LEN)
1768 memcpy(data, ibmvnic_stats[i].name, ETH_GSTRING_LEN);
John Allen3d52b592017-08-02 16:44:14 -05001769
1770 for (i = 0; i < adapter->req_tx_queues; i++) {
1771 snprintf(data, ETH_GSTRING_LEN, "tx%d_packets", i);
1772 data += ETH_GSTRING_LEN;
1773
1774 snprintf(data, ETH_GSTRING_LEN, "tx%d_bytes", i);
1775 data += ETH_GSTRING_LEN;
1776
1777 snprintf(data, ETH_GSTRING_LEN, "tx%d_dropped_packets", i);
1778 data += ETH_GSTRING_LEN;
1779 }
1780
1781 for (i = 0; i < adapter->req_rx_queues; i++) {
1782 snprintf(data, ETH_GSTRING_LEN, "rx%d_packets", i);
1783 data += ETH_GSTRING_LEN;
1784
1785 snprintf(data, ETH_GSTRING_LEN, "rx%d_bytes", i);
1786 data += ETH_GSTRING_LEN;
1787
1788 snprintf(data, ETH_GSTRING_LEN, "rx%d_interrupts", i);
1789 data += ETH_GSTRING_LEN;
1790 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001791}
1792
1793static int ibmvnic_get_sset_count(struct net_device *dev, int sset)
1794{
John Allen3d52b592017-08-02 16:44:14 -05001795 struct ibmvnic_adapter *adapter = netdev_priv(dev);
1796
Thomas Falcon032c5e82015-12-21 11:26:06 -06001797 switch (sset) {
1798 case ETH_SS_STATS:
John Allen3d52b592017-08-02 16:44:14 -05001799 return ARRAY_SIZE(ibmvnic_stats) +
1800 adapter->req_tx_queues * NUM_TX_STATS +
1801 adapter->req_rx_queues * NUM_RX_STATS;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001802 default:
1803 return -EOPNOTSUPP;
1804 }
1805}
1806
1807static void ibmvnic_get_ethtool_stats(struct net_device *dev,
1808 struct ethtool_stats *stats, u64 *data)
1809{
1810 struct ibmvnic_adapter *adapter = netdev_priv(dev);
1811 union ibmvnic_crq crq;
John Allen3d52b592017-08-02 16:44:14 -05001812 int i, j;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001813
1814 memset(&crq, 0, sizeof(crq));
1815 crq.request_statistics.first = IBMVNIC_CRQ_CMD;
1816 crq.request_statistics.cmd = REQUEST_STATISTICS;
1817 crq.request_statistics.ioba = cpu_to_be32(adapter->stats_token);
1818 crq.request_statistics.len =
1819 cpu_to_be32(sizeof(struct ibmvnic_statistics));
Thomas Falcon032c5e82015-12-21 11:26:06 -06001820
1821 /* Wait for data to be written */
1822 init_completion(&adapter->stats_done);
Nathan Fontenotdb5d0b52017-02-10 13:45:05 -05001823 ibmvnic_send_crq(adapter, &crq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001824 wait_for_completion(&adapter->stats_done);
1825
1826 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++)
John Allen52da5c12017-08-02 16:45:28 -05001827 data[i] = be64_to_cpu(IBMVNIC_GET_STAT(adapter,
1828 ibmvnic_stats[i].offset));
John Allen3d52b592017-08-02 16:44:14 -05001829
1830 for (j = 0; j < adapter->req_tx_queues; j++) {
1831 data[i] = adapter->tx_stats_buffers[j].packets;
1832 i++;
1833 data[i] = adapter->tx_stats_buffers[j].bytes;
1834 i++;
1835 data[i] = adapter->tx_stats_buffers[j].dropped_packets;
1836 i++;
1837 }
1838
1839 for (j = 0; j < adapter->req_rx_queues; j++) {
1840 data[i] = adapter->rx_stats_buffers[j].packets;
1841 i++;
1842 data[i] = adapter->rx_stats_buffers[j].bytes;
1843 i++;
1844 data[i] = adapter->rx_stats_buffers[j].interrupts;
1845 i++;
1846 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001847}
1848
1849static const struct ethtool_ops ibmvnic_ethtool_ops = {
Thomas Falcon032c5e82015-12-21 11:26:06 -06001850 .get_drvinfo = ibmvnic_get_drvinfo,
1851 .get_msglevel = ibmvnic_get_msglevel,
1852 .set_msglevel = ibmvnic_set_msglevel,
1853 .get_link = ibmvnic_get_link,
1854 .get_ringparam = ibmvnic_get_ringparam,
John Allenc2dbeb62017-08-02 16:47:17 -05001855 .get_channels = ibmvnic_get_channels,
Thomas Falcon032c5e82015-12-21 11:26:06 -06001856 .get_strings = ibmvnic_get_strings,
1857 .get_sset_count = ibmvnic_get_sset_count,
1858 .get_ethtool_stats = ibmvnic_get_ethtool_stats,
Philippe Reynes8a433792017-01-07 22:37:29 +01001859 .get_link_ksettings = ibmvnic_get_link_ksettings,
Thomas Falcon032c5e82015-12-21 11:26:06 -06001860};
1861
1862/* Routines for managing CRQs/sCRQs */
1863
Nathan Fontenot57a49432017-05-26 10:31:12 -04001864static int reset_one_sub_crq_queue(struct ibmvnic_adapter *adapter,
1865 struct ibmvnic_sub_crq_queue *scrq)
1866{
1867 int rc;
1868
1869 if (scrq->irq) {
1870 free_irq(scrq->irq, scrq);
1871 irq_dispose_mapping(scrq->irq);
1872 scrq->irq = 0;
1873 }
1874
Thomas Falconc8b2ad02017-06-14 23:50:07 -05001875 memset(scrq->msgs, 0, 4 * PAGE_SIZE);
Nathan Fontenot57a49432017-05-26 10:31:12 -04001876 scrq->cur = 0;
1877
1878 rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
1879 4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
1880 return rc;
1881}
1882
1883static int reset_sub_crq_queues(struct ibmvnic_adapter *adapter)
1884{
1885 int i, rc;
1886
1887 for (i = 0; i < adapter->req_tx_queues; i++) {
1888 rc = reset_one_sub_crq_queue(adapter, adapter->tx_scrq[i]);
1889 if (rc)
1890 return rc;
1891 }
1892
1893 for (i = 0; i < adapter->req_rx_queues; i++) {
1894 rc = reset_one_sub_crq_queue(adapter, adapter->rx_scrq[i]);
1895 if (rc)
1896 return rc;
1897 }
1898
Nathan Fontenot57a49432017-05-26 10:31:12 -04001899 return rc;
1900}
1901
Thomas Falcon032c5e82015-12-21 11:26:06 -06001902static void release_sub_crq_queue(struct ibmvnic_adapter *adapter,
1903 struct ibmvnic_sub_crq_queue *scrq)
1904{
1905 struct device *dev = &adapter->vdev->dev;
1906 long rc;
1907
1908 netdev_dbg(adapter->netdev, "Releasing sub-CRQ\n");
1909
1910 /* Close the sub-crqs */
1911 do {
1912 rc = plpar_hcall_norets(H_FREE_SUB_CRQ,
1913 adapter->vdev->unit_address,
1914 scrq->crq_num);
1915 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
1916
Thomas Falconffa73852017-04-19 13:44:29 -04001917 if (rc) {
1918 netdev_err(adapter->netdev,
1919 "Failed to release sub-CRQ %16lx, rc = %ld\n",
1920 scrq->crq_num, rc);
1921 }
1922
Thomas Falcon032c5e82015-12-21 11:26:06 -06001923 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
1924 DMA_BIDIRECTIONAL);
1925 free_pages((unsigned long)scrq->msgs, 2);
1926 kfree(scrq);
1927}
1928
1929static struct ibmvnic_sub_crq_queue *init_sub_crq_queue(struct ibmvnic_adapter
1930 *adapter)
1931{
1932 struct device *dev = &adapter->vdev->dev;
1933 struct ibmvnic_sub_crq_queue *scrq;
1934 int rc;
1935
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04001936 scrq = kzalloc(sizeof(*scrq), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001937 if (!scrq)
1938 return NULL;
1939
Nathan Fontenot7f7adc52017-04-19 13:45:16 -04001940 scrq->msgs =
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04001941 (union sub_crq *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, 2);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001942 if (!scrq->msgs) {
1943 dev_warn(dev, "Couldn't allocate crq queue messages page\n");
1944 goto zero_page_failed;
1945 }
1946
1947 scrq->msg_token = dma_map_single(dev, scrq->msgs, 4 * PAGE_SIZE,
1948 DMA_BIDIRECTIONAL);
1949 if (dma_mapping_error(dev, scrq->msg_token)) {
1950 dev_warn(dev, "Couldn't map crq queue messages page\n");
1951 goto map_failed;
1952 }
1953
1954 rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
1955 4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
1956
1957 if (rc == H_RESOURCE)
1958 rc = ibmvnic_reset_crq(adapter);
1959
1960 if (rc == H_CLOSED) {
1961 dev_warn(dev, "Partner adapter not ready, waiting.\n");
1962 } else if (rc) {
1963 dev_warn(dev, "Error %d registering sub-crq\n", rc);
1964 goto reg_failed;
1965 }
1966
Thomas Falcon032c5e82015-12-21 11:26:06 -06001967 scrq->adapter = adapter;
1968 scrq->size = 4 * PAGE_SIZE / sizeof(*scrq->msgs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001969 spin_lock_init(&scrq->lock);
1970
1971 netdev_dbg(adapter->netdev,
1972 "sub-crq initialized, num %lx, hw_irq=%lx, irq=%x\n",
1973 scrq->crq_num, scrq->hw_irq, scrq->irq);
1974
1975 return scrq;
1976
Thomas Falcon032c5e82015-12-21 11:26:06 -06001977reg_failed:
1978 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
1979 DMA_BIDIRECTIONAL);
1980map_failed:
1981 free_pages((unsigned long)scrq->msgs, 2);
1982zero_page_failed:
1983 kfree(scrq);
1984
1985 return NULL;
1986}
1987
1988static void release_sub_crqs(struct ibmvnic_adapter *adapter)
1989{
1990 int i;
1991
1992 if (adapter->tx_scrq) {
Nathan Fontenotb5108882017-03-30 02:49:18 -04001993 for (i = 0; i < adapter->req_tx_queues; i++) {
1994 if (!adapter->tx_scrq[i])
1995 continue;
1996
1997 if (adapter->tx_scrq[i]->irq) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06001998 free_irq(adapter->tx_scrq[i]->irq,
1999 adapter->tx_scrq[i]);
Thomas Falcon88eb98a2016-07-06 15:35:16 -05002000 irq_dispose_mapping(adapter->tx_scrq[i]->irq);
Nathan Fontenotb5108882017-03-30 02:49:18 -04002001 adapter->tx_scrq[i]->irq = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002002 }
Nathan Fontenotb5108882017-03-30 02:49:18 -04002003
2004 release_sub_crq_queue(adapter, adapter->tx_scrq[i]);
2005 }
2006
Nathan Fontenot9501df32017-03-15 23:38:07 -04002007 kfree(adapter->tx_scrq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002008 adapter->tx_scrq = NULL;
2009 }
2010
2011 if (adapter->rx_scrq) {
Nathan Fontenotb5108882017-03-30 02:49:18 -04002012 for (i = 0; i < adapter->req_rx_queues; i++) {
2013 if (!adapter->rx_scrq[i])
2014 continue;
2015
2016 if (adapter->rx_scrq[i]->irq) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06002017 free_irq(adapter->rx_scrq[i]->irq,
2018 adapter->rx_scrq[i]);
Thomas Falcon88eb98a2016-07-06 15:35:16 -05002019 irq_dispose_mapping(adapter->rx_scrq[i]->irq);
Nathan Fontenotb5108882017-03-30 02:49:18 -04002020 adapter->rx_scrq[i]->irq = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002021 }
Nathan Fontenotb5108882017-03-30 02:49:18 -04002022
2023 release_sub_crq_queue(adapter, adapter->rx_scrq[i]);
2024 }
2025
Nathan Fontenot9501df32017-03-15 23:38:07 -04002026 kfree(adapter->rx_scrq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002027 adapter->rx_scrq = NULL;
2028 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002029}
2030
2031static int disable_scrq_irq(struct ibmvnic_adapter *adapter,
2032 struct ibmvnic_sub_crq_queue *scrq)
2033{
2034 struct device *dev = &adapter->vdev->dev;
2035 unsigned long rc;
2036
2037 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
2038 H_DISABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
2039 if (rc)
2040 dev_err(dev, "Couldn't disable scrq irq 0x%lx. rc=%ld\n",
2041 scrq->hw_irq, rc);
2042 return rc;
2043}
2044
2045static int enable_scrq_irq(struct ibmvnic_adapter *adapter,
2046 struct ibmvnic_sub_crq_queue *scrq)
2047{
2048 struct device *dev = &adapter->vdev->dev;
2049 unsigned long rc;
2050
2051 if (scrq->hw_irq > 0x100000000ULL) {
2052 dev_err(dev, "bad hw_irq = %lx\n", scrq->hw_irq);
2053 return 1;
2054 }
2055
2056 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
2057 H_ENABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
2058 if (rc)
2059 dev_err(dev, "Couldn't enable scrq irq 0x%lx. rc=%ld\n",
2060 scrq->hw_irq, rc);
2061 return rc;
2062}
2063
2064static int ibmvnic_complete_tx(struct ibmvnic_adapter *adapter,
2065 struct ibmvnic_sub_crq_queue *scrq)
2066{
2067 struct device *dev = &adapter->vdev->dev;
2068 struct ibmvnic_tx_buff *txbuff;
2069 union sub_crq *next;
2070 int index;
2071 int i, j;
Thomas Falconad7775d2016-04-01 17:20:34 -05002072 u8 first;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002073
2074restart_loop:
2075 while (pending_scrq(adapter, scrq)) {
2076 unsigned int pool = scrq->pool_index;
2077
2078 next = ibmvnic_next_scrq(adapter, scrq);
2079 for (i = 0; i < next->tx_comp.num_comps; i++) {
2080 if (next->tx_comp.rcs[i]) {
2081 dev_err(dev, "tx error %x\n",
2082 next->tx_comp.rcs[i]);
2083 continue;
2084 }
2085 index = be32_to_cpu(next->tx_comp.correlators[i]);
2086 txbuff = &adapter->tx_pool[pool].tx_buff[index];
2087
2088 for (j = 0; j < IBMVNIC_MAX_FRAGS_PER_CRQ; j++) {
2089 if (!txbuff->data_dma[j])
2090 continue;
2091
2092 txbuff->data_dma[j] = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002093 }
Thomas Falconad7775d2016-04-01 17:20:34 -05002094 /* if sub_crq was sent indirectly */
2095 first = txbuff->indir_arr[0].generic.first;
2096 if (first == IBMVNIC_CRQ_CMD) {
2097 dma_unmap_single(dev, txbuff->indir_dma,
2098 sizeof(txbuff->indir_arr),
2099 DMA_TO_DEVICE);
2100 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002101
Thomas Falcon142c0ac2017-03-05 12:18:41 -06002102 if (txbuff->last_frag) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06002103 dev_kfree_skb_any(txbuff->skb);
Nathan Fontenot7c3e7de2017-05-03 14:05:25 -04002104 txbuff->skb = NULL;
Thomas Falcon142c0ac2017-03-05 12:18:41 -06002105 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002106
2107 adapter->tx_pool[pool].free_map[adapter->tx_pool[pool].
2108 producer_index] = index;
2109 adapter->tx_pool[pool].producer_index =
2110 (adapter->tx_pool[pool].producer_index + 1) %
Thomas Falcon068d9f92017-03-05 12:18:42 -06002111 adapter->req_tx_entries_per_subcrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002112 }
2113 /* remove tx_comp scrq*/
2114 next->tx_comp.first = 0;
Nathan Fontenot7c3e7de2017-05-03 14:05:25 -04002115
2116 if (atomic_sub_return(next->tx_comp.num_comps, &scrq->used) <=
2117 (adapter->req_tx_entries_per_subcrq / 2) &&
2118 __netif_subqueue_stopped(adapter->netdev,
2119 scrq->pool_index)) {
2120 netif_wake_subqueue(adapter->netdev, scrq->pool_index);
2121 netdev_info(adapter->netdev, "Started queue %d\n",
2122 scrq->pool_index);
2123 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002124 }
2125
2126 enable_scrq_irq(adapter, scrq);
2127
2128 if (pending_scrq(adapter, scrq)) {
2129 disable_scrq_irq(adapter, scrq);
2130 goto restart_loop;
2131 }
2132
2133 return 0;
2134}
2135
2136static irqreturn_t ibmvnic_interrupt_tx(int irq, void *instance)
2137{
2138 struct ibmvnic_sub_crq_queue *scrq = instance;
2139 struct ibmvnic_adapter *adapter = scrq->adapter;
2140
2141 disable_scrq_irq(adapter, scrq);
2142 ibmvnic_complete_tx(adapter, scrq);
2143
2144 return IRQ_HANDLED;
2145}
2146
2147static irqreturn_t ibmvnic_interrupt_rx(int irq, void *instance)
2148{
2149 struct ibmvnic_sub_crq_queue *scrq = instance;
2150 struct ibmvnic_adapter *adapter = scrq->adapter;
2151
John Allen3d52b592017-08-02 16:44:14 -05002152 adapter->rx_stats_buffers[scrq->scrq_num].interrupts++;
2153
Thomas Falcon032c5e82015-12-21 11:26:06 -06002154 if (napi_schedule_prep(&adapter->napi[scrq->scrq_num])) {
2155 disable_scrq_irq(adapter, scrq);
2156 __napi_schedule(&adapter->napi[scrq->scrq_num]);
2157 }
2158
2159 return IRQ_HANDLED;
2160}
2161
Thomas Falconea22d512016-07-06 15:35:17 -05002162static int init_sub_crq_irqs(struct ibmvnic_adapter *adapter)
2163{
2164 struct device *dev = &adapter->vdev->dev;
2165 struct ibmvnic_sub_crq_queue *scrq;
2166 int i = 0, j = 0;
2167 int rc = 0;
2168
2169 for (i = 0; i < adapter->req_tx_queues; i++) {
2170 scrq = adapter->tx_scrq[i];
2171 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
2172
Michael Ellerman99c17902016-09-10 19:59:05 +10002173 if (!scrq->irq) {
Thomas Falconea22d512016-07-06 15:35:17 -05002174 rc = -EINVAL;
2175 dev_err(dev, "Error mapping irq\n");
2176 goto req_tx_irq_failed;
2177 }
2178
2179 rc = request_irq(scrq->irq, ibmvnic_interrupt_tx,
2180 0, "ibmvnic_tx", scrq);
2181
2182 if (rc) {
2183 dev_err(dev, "Couldn't register tx 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
2190 for (i = 0; i < adapter->req_rx_queues; i++) {
2191 scrq = adapter->rx_scrq[i];
2192 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
Michael Ellerman99c17902016-09-10 19:59:05 +10002193 if (!scrq->irq) {
Thomas Falconea22d512016-07-06 15:35:17 -05002194 rc = -EINVAL;
2195 dev_err(dev, "Error mapping irq\n");
2196 goto req_rx_irq_failed;
2197 }
2198 rc = request_irq(scrq->irq, ibmvnic_interrupt_rx,
2199 0, "ibmvnic_rx", scrq);
2200 if (rc) {
2201 dev_err(dev, "Couldn't register rx irq 0x%x. rc=%d\n",
2202 scrq->irq, rc);
2203 irq_dispose_mapping(scrq->irq);
2204 goto req_rx_irq_failed;
2205 }
2206 }
2207 return rc;
2208
2209req_rx_irq_failed:
Thomas Falcon8bf371e2016-10-27 12:28:52 -05002210 for (j = 0; j < i; j++) {
Thomas Falconea22d512016-07-06 15:35:17 -05002211 free_irq(adapter->rx_scrq[j]->irq, adapter->rx_scrq[j]);
2212 irq_dispose_mapping(adapter->rx_scrq[j]->irq);
Thomas Falcon8bf371e2016-10-27 12:28:52 -05002213 }
Thomas Falconea22d512016-07-06 15:35:17 -05002214 i = adapter->req_tx_queues;
2215req_tx_irq_failed:
Thomas Falcon8bf371e2016-10-27 12:28:52 -05002216 for (j = 0; j < i; j++) {
Thomas Falconea22d512016-07-06 15:35:17 -05002217 free_irq(adapter->tx_scrq[j]->irq, adapter->tx_scrq[j]);
2218 irq_dispose_mapping(adapter->rx_scrq[j]->irq);
Thomas Falcon8bf371e2016-10-27 12:28:52 -05002219 }
Nathan Fontenotb5108882017-03-30 02:49:18 -04002220 release_sub_crqs(adapter);
Thomas Falconea22d512016-07-06 15:35:17 -05002221 return rc;
2222}
2223
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002224static int init_sub_crqs(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002225{
2226 struct device *dev = &adapter->vdev->dev;
2227 struct ibmvnic_sub_crq_queue **allqueues;
2228 int registered_queues = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002229 int total_queues;
2230 int more = 0;
Thomas Falconea22d512016-07-06 15:35:17 -05002231 int i;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002232
Thomas Falcon032c5e82015-12-21 11:26:06 -06002233 total_queues = adapter->req_tx_queues + adapter->req_rx_queues;
2234
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04002235 allqueues = kcalloc(total_queues, sizeof(*allqueues), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002236 if (!allqueues)
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002237 return -1;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002238
2239 for (i = 0; i < total_queues; i++) {
2240 allqueues[i] = init_sub_crq_queue(adapter);
2241 if (!allqueues[i]) {
2242 dev_warn(dev, "Couldn't allocate all sub-crqs\n");
2243 break;
2244 }
2245 registered_queues++;
2246 }
2247
2248 /* Make sure we were able to register the minimum number of queues */
2249 if (registered_queues <
2250 adapter->min_tx_queues + adapter->min_rx_queues) {
2251 dev_err(dev, "Fatal: Couldn't init min number of sub-crqs\n");
2252 goto tx_failed;
2253 }
2254
2255 /* Distribute the failed allocated queues*/
2256 for (i = 0; i < total_queues - registered_queues + more ; i++) {
2257 netdev_dbg(adapter->netdev, "Reducing number of queues\n");
2258 switch (i % 3) {
2259 case 0:
2260 if (adapter->req_rx_queues > adapter->min_rx_queues)
2261 adapter->req_rx_queues--;
2262 else
2263 more++;
2264 break;
2265 case 1:
2266 if (adapter->req_tx_queues > adapter->min_tx_queues)
2267 adapter->req_tx_queues--;
2268 else
2269 more++;
2270 break;
2271 }
2272 }
2273
2274 adapter->tx_scrq = kcalloc(adapter->req_tx_queues,
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04002275 sizeof(*adapter->tx_scrq), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002276 if (!adapter->tx_scrq)
2277 goto tx_failed;
2278
2279 for (i = 0; i < adapter->req_tx_queues; i++) {
2280 adapter->tx_scrq[i] = allqueues[i];
2281 adapter->tx_scrq[i]->pool_index = i;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002282 }
2283
2284 adapter->rx_scrq = kcalloc(adapter->req_rx_queues,
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04002285 sizeof(*adapter->rx_scrq), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002286 if (!adapter->rx_scrq)
2287 goto rx_failed;
2288
2289 for (i = 0; i < adapter->req_rx_queues; i++) {
2290 adapter->rx_scrq[i] = allqueues[i + adapter->req_tx_queues];
2291 adapter->rx_scrq[i]->scrq_num = i;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002292 }
2293
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002294 kfree(allqueues);
2295 return 0;
2296
2297rx_failed:
2298 kfree(adapter->tx_scrq);
2299 adapter->tx_scrq = NULL;
2300tx_failed:
2301 for (i = 0; i < registered_queues; i++)
2302 release_sub_crq_queue(adapter, allqueues[i]);
2303 kfree(allqueues);
2304 return -1;
2305}
2306
2307static void ibmvnic_send_req_caps(struct ibmvnic_adapter *adapter, int retry)
2308{
2309 struct device *dev = &adapter->vdev->dev;
2310 union ibmvnic_crq crq;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002311
2312 if (!retry) {
2313 /* Sub-CRQ entries are 32 byte long */
2314 int entries_page = 4 * PAGE_SIZE / (sizeof(u64) * 4);
2315
2316 if (adapter->min_tx_entries_per_subcrq > entries_page ||
2317 adapter->min_rx_add_entries_per_subcrq > entries_page) {
2318 dev_err(dev, "Fatal, invalid entries per sub-crq\n");
2319 return;
2320 }
2321
2322 /* Get the minimum between the queried max and the entries
2323 * that fit in our PAGE_SIZE
2324 */
2325 adapter->req_tx_entries_per_subcrq =
2326 adapter->max_tx_entries_per_subcrq > entries_page ?
2327 entries_page : adapter->max_tx_entries_per_subcrq;
2328 adapter->req_rx_add_entries_per_subcrq =
2329 adapter->max_rx_add_entries_per_subcrq > entries_page ?
2330 entries_page : adapter->max_rx_add_entries_per_subcrq;
2331
2332 adapter->req_tx_queues = adapter->opt_tx_comp_sub_queues;
2333 adapter->req_rx_queues = adapter->opt_rx_comp_queues;
2334 adapter->req_rx_add_queues = adapter->max_rx_add_queues;
2335
2336 adapter->req_mtu = adapter->netdev->mtu + ETH_HLEN;
2337 }
2338
Thomas Falcon032c5e82015-12-21 11:26:06 -06002339 memset(&crq, 0, sizeof(crq));
2340 crq.request_capability.first = IBMVNIC_CRQ_CMD;
2341 crq.request_capability.cmd = REQUEST_CAPABILITY;
2342
2343 crq.request_capability.capability = cpu_to_be16(REQ_TX_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06002344 crq.request_capability.number = cpu_to_be64(adapter->req_tx_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06002345 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002346 ibmvnic_send_crq(adapter, &crq);
2347
2348 crq.request_capability.capability = cpu_to_be16(REQ_RX_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06002349 crq.request_capability.number = cpu_to_be64(adapter->req_rx_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06002350 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002351 ibmvnic_send_crq(adapter, &crq);
2352
2353 crq.request_capability.capability = cpu_to_be16(REQ_RX_ADD_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06002354 crq.request_capability.number = cpu_to_be64(adapter->req_rx_add_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06002355 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002356 ibmvnic_send_crq(adapter, &crq);
2357
2358 crq.request_capability.capability =
2359 cpu_to_be16(REQ_TX_ENTRIES_PER_SUBCRQ);
2360 crq.request_capability.number =
Thomas Falconde89e852016-03-01 10:20:09 -06002361 cpu_to_be64(adapter->req_tx_entries_per_subcrq);
Thomas Falcon901e0402017-02-15 12:17:59 -06002362 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002363 ibmvnic_send_crq(adapter, &crq);
2364
2365 crq.request_capability.capability =
2366 cpu_to_be16(REQ_RX_ADD_ENTRIES_PER_SUBCRQ);
2367 crq.request_capability.number =
Thomas Falconde89e852016-03-01 10:20:09 -06002368 cpu_to_be64(adapter->req_rx_add_entries_per_subcrq);
Thomas Falcon901e0402017-02-15 12:17:59 -06002369 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002370 ibmvnic_send_crq(adapter, &crq);
2371
2372 crq.request_capability.capability = cpu_to_be16(REQ_MTU);
Thomas Falconde89e852016-03-01 10:20:09 -06002373 crq.request_capability.number = cpu_to_be64(adapter->req_mtu);
Thomas Falcon901e0402017-02-15 12:17:59 -06002374 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002375 ibmvnic_send_crq(adapter, &crq);
2376
2377 if (adapter->netdev->flags & IFF_PROMISC) {
2378 if (adapter->promisc_supported) {
2379 crq.request_capability.capability =
2380 cpu_to_be16(PROMISC_REQUESTED);
Thomas Falconde89e852016-03-01 10:20:09 -06002381 crq.request_capability.number = cpu_to_be64(1);
Thomas Falcon901e0402017-02-15 12:17:59 -06002382 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002383 ibmvnic_send_crq(adapter, &crq);
2384 }
2385 } else {
2386 crq.request_capability.capability =
2387 cpu_to_be16(PROMISC_REQUESTED);
Thomas Falconde89e852016-03-01 10:20:09 -06002388 crq.request_capability.number = cpu_to_be64(0);
Thomas Falcon901e0402017-02-15 12:17:59 -06002389 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002390 ibmvnic_send_crq(adapter, &crq);
2391 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002392}
2393
2394static int pending_scrq(struct ibmvnic_adapter *adapter,
2395 struct ibmvnic_sub_crq_queue *scrq)
2396{
2397 union sub_crq *entry = &scrq->msgs[scrq->cur];
2398
Thomas Falcon1cf9cc72017-06-14 23:50:08 -05002399 if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002400 return 1;
2401 else
2402 return 0;
2403}
2404
2405static union sub_crq *ibmvnic_next_scrq(struct ibmvnic_adapter *adapter,
2406 struct ibmvnic_sub_crq_queue *scrq)
2407{
2408 union sub_crq *entry;
2409 unsigned long flags;
2410
2411 spin_lock_irqsave(&scrq->lock, flags);
2412 entry = &scrq->msgs[scrq->cur];
2413 if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP) {
2414 if (++scrq->cur == scrq->size)
2415 scrq->cur = 0;
2416 } else {
2417 entry = NULL;
2418 }
2419 spin_unlock_irqrestore(&scrq->lock, flags);
2420
2421 return entry;
2422}
2423
2424static union ibmvnic_crq *ibmvnic_next_crq(struct ibmvnic_adapter *adapter)
2425{
2426 struct ibmvnic_crq_queue *queue = &adapter->crq;
2427 union ibmvnic_crq *crq;
2428
2429 crq = &queue->msgs[queue->cur];
2430 if (crq->generic.first & IBMVNIC_CRQ_CMD_RSP) {
2431 if (++queue->cur == queue->size)
2432 queue->cur = 0;
2433 } else {
2434 crq = NULL;
2435 }
2436
2437 return crq;
2438}
2439
2440static int send_subcrq(struct ibmvnic_adapter *adapter, u64 remote_handle,
2441 union sub_crq *sub_crq)
2442{
2443 unsigned int ua = adapter->vdev->unit_address;
2444 struct device *dev = &adapter->vdev->dev;
2445 u64 *u64_crq = (u64 *)sub_crq;
2446 int rc;
2447
2448 netdev_dbg(adapter->netdev,
2449 "Sending sCRQ %016lx: %016lx %016lx %016lx %016lx\n",
2450 (unsigned long int)cpu_to_be64(remote_handle),
2451 (unsigned long int)cpu_to_be64(u64_crq[0]),
2452 (unsigned long int)cpu_to_be64(u64_crq[1]),
2453 (unsigned long int)cpu_to_be64(u64_crq[2]),
2454 (unsigned long int)cpu_to_be64(u64_crq[3]));
2455
2456 /* Make sure the hypervisor sees the complete request */
2457 mb();
2458
2459 rc = plpar_hcall_norets(H_SEND_SUB_CRQ, ua,
2460 cpu_to_be64(remote_handle),
2461 cpu_to_be64(u64_crq[0]),
2462 cpu_to_be64(u64_crq[1]),
2463 cpu_to_be64(u64_crq[2]),
2464 cpu_to_be64(u64_crq[3]));
2465
2466 if (rc) {
2467 if (rc == H_CLOSED)
2468 dev_warn(dev, "CRQ Queue closed\n");
2469 dev_err(dev, "Send error (rc=%d)\n", rc);
2470 }
2471
2472 return rc;
2473}
2474
Thomas Falconad7775d2016-04-01 17:20:34 -05002475static int send_subcrq_indirect(struct ibmvnic_adapter *adapter,
2476 u64 remote_handle, u64 ioba, u64 num_entries)
2477{
2478 unsigned int ua = adapter->vdev->unit_address;
2479 struct device *dev = &adapter->vdev->dev;
2480 int rc;
2481
2482 /* Make sure the hypervisor sees the complete request */
2483 mb();
2484 rc = plpar_hcall_norets(H_SEND_SUB_CRQ_INDIRECT, ua,
2485 cpu_to_be64(remote_handle),
2486 ioba, num_entries);
2487
2488 if (rc) {
2489 if (rc == H_CLOSED)
2490 dev_warn(dev, "CRQ Queue closed\n");
2491 dev_err(dev, "Send (indirect) error (rc=%d)\n", rc);
2492 }
2493
2494 return rc;
2495}
2496
Thomas Falcon032c5e82015-12-21 11:26:06 -06002497static int ibmvnic_send_crq(struct ibmvnic_adapter *adapter,
2498 union ibmvnic_crq *crq)
2499{
2500 unsigned int ua = adapter->vdev->unit_address;
2501 struct device *dev = &adapter->vdev->dev;
2502 u64 *u64_crq = (u64 *)crq;
2503 int rc;
2504
2505 netdev_dbg(adapter->netdev, "Sending CRQ: %016lx %016lx\n",
2506 (unsigned long int)cpu_to_be64(u64_crq[0]),
2507 (unsigned long int)cpu_to_be64(u64_crq[1]));
2508
2509 /* Make sure the hypervisor sees the complete request */
2510 mb();
2511
2512 rc = plpar_hcall_norets(H_SEND_CRQ, ua,
2513 cpu_to_be64(u64_crq[0]),
2514 cpu_to_be64(u64_crq[1]));
2515
2516 if (rc) {
2517 if (rc == H_CLOSED)
2518 dev_warn(dev, "CRQ Queue closed\n");
2519 dev_warn(dev, "Send error (rc=%d)\n", rc);
2520 }
2521
2522 return rc;
2523}
2524
2525static int ibmvnic_send_crq_init(struct ibmvnic_adapter *adapter)
2526{
2527 union ibmvnic_crq crq;
2528
2529 memset(&crq, 0, sizeof(crq));
2530 crq.generic.first = IBMVNIC_CRQ_INIT_CMD;
2531 crq.generic.cmd = IBMVNIC_CRQ_INIT;
2532 netdev_dbg(adapter->netdev, "Sending CRQ init\n");
2533
2534 return ibmvnic_send_crq(adapter, &crq);
2535}
2536
Thomas Falcon032c5e82015-12-21 11:26:06 -06002537static int send_version_xchg(struct ibmvnic_adapter *adapter)
2538{
2539 union ibmvnic_crq crq;
2540
2541 memset(&crq, 0, sizeof(crq));
2542 crq.version_exchange.first = IBMVNIC_CRQ_CMD;
2543 crq.version_exchange.cmd = VERSION_EXCHANGE;
2544 crq.version_exchange.version = cpu_to_be16(ibmvnic_version);
2545
2546 return ibmvnic_send_crq(adapter, &crq);
2547}
2548
2549static void send_login(struct ibmvnic_adapter *adapter)
2550{
2551 struct ibmvnic_login_rsp_buffer *login_rsp_buffer;
2552 struct ibmvnic_login_buffer *login_buffer;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002553 struct device *dev = &adapter->vdev->dev;
2554 dma_addr_t rsp_buffer_token;
2555 dma_addr_t buffer_token;
2556 size_t rsp_buffer_size;
2557 union ibmvnic_crq crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002558 size_t buffer_size;
2559 __be64 *tx_list_p;
2560 __be64 *rx_list_p;
2561 int i;
2562
2563 buffer_size =
2564 sizeof(struct ibmvnic_login_buffer) +
2565 sizeof(u64) * (adapter->req_tx_queues + adapter->req_rx_queues);
2566
2567 login_buffer = kmalloc(buffer_size, GFP_ATOMIC);
2568 if (!login_buffer)
2569 goto buf_alloc_failed;
2570
2571 buffer_token = dma_map_single(dev, login_buffer, buffer_size,
2572 DMA_TO_DEVICE);
2573 if (dma_mapping_error(dev, buffer_token)) {
2574 dev_err(dev, "Couldn't map login buffer\n");
2575 goto buf_map_failed;
2576 }
2577
John Allen498cd8e2016-04-06 11:49:55 -05002578 rsp_buffer_size = sizeof(struct ibmvnic_login_rsp_buffer) +
2579 sizeof(u64) * adapter->req_tx_queues +
2580 sizeof(u64) * adapter->req_rx_queues +
2581 sizeof(u64) * adapter->req_rx_queues +
2582 sizeof(u8) * IBMVNIC_TX_DESC_VERSIONS;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002583
2584 login_rsp_buffer = kmalloc(rsp_buffer_size, GFP_ATOMIC);
2585 if (!login_rsp_buffer)
2586 goto buf_rsp_alloc_failed;
2587
2588 rsp_buffer_token = dma_map_single(dev, login_rsp_buffer,
2589 rsp_buffer_size, DMA_FROM_DEVICE);
2590 if (dma_mapping_error(dev, rsp_buffer_token)) {
2591 dev_err(dev, "Couldn't map login rsp buffer\n");
2592 goto buf_rsp_map_failed;
2593 }
Nathan Fontenot661a2622017-04-19 13:44:58 -04002594
Thomas Falcon032c5e82015-12-21 11:26:06 -06002595 adapter->login_buf = login_buffer;
2596 adapter->login_buf_token = buffer_token;
2597 adapter->login_buf_sz = buffer_size;
2598 adapter->login_rsp_buf = login_rsp_buffer;
2599 adapter->login_rsp_buf_token = rsp_buffer_token;
2600 adapter->login_rsp_buf_sz = rsp_buffer_size;
2601
2602 login_buffer->len = cpu_to_be32(buffer_size);
2603 login_buffer->version = cpu_to_be32(INITIAL_VERSION_LB);
2604 login_buffer->num_txcomp_subcrqs = cpu_to_be32(adapter->req_tx_queues);
2605 login_buffer->off_txcomp_subcrqs =
2606 cpu_to_be32(sizeof(struct ibmvnic_login_buffer));
2607 login_buffer->num_rxcomp_subcrqs = cpu_to_be32(adapter->req_rx_queues);
2608 login_buffer->off_rxcomp_subcrqs =
2609 cpu_to_be32(sizeof(struct ibmvnic_login_buffer) +
2610 sizeof(u64) * adapter->req_tx_queues);
2611 login_buffer->login_rsp_ioba = cpu_to_be32(rsp_buffer_token);
2612 login_buffer->login_rsp_len = cpu_to_be32(rsp_buffer_size);
2613
2614 tx_list_p = (__be64 *)((char *)login_buffer +
2615 sizeof(struct ibmvnic_login_buffer));
2616 rx_list_p = (__be64 *)((char *)login_buffer +
2617 sizeof(struct ibmvnic_login_buffer) +
2618 sizeof(u64) * adapter->req_tx_queues);
2619
2620 for (i = 0; i < adapter->req_tx_queues; i++) {
2621 if (adapter->tx_scrq[i]) {
2622 tx_list_p[i] = cpu_to_be64(adapter->tx_scrq[i]->
2623 crq_num);
2624 }
2625 }
2626
2627 for (i = 0; i < adapter->req_rx_queues; i++) {
2628 if (adapter->rx_scrq[i]) {
2629 rx_list_p[i] = cpu_to_be64(adapter->rx_scrq[i]->
2630 crq_num);
2631 }
2632 }
2633
2634 netdev_dbg(adapter->netdev, "Login Buffer:\n");
2635 for (i = 0; i < (adapter->login_buf_sz - 1) / 8 + 1; i++) {
2636 netdev_dbg(adapter->netdev, "%016lx\n",
2637 ((unsigned long int *)(adapter->login_buf))[i]);
2638 }
2639
2640 memset(&crq, 0, sizeof(crq));
2641 crq.login.first = IBMVNIC_CRQ_CMD;
2642 crq.login.cmd = LOGIN;
2643 crq.login.ioba = cpu_to_be32(buffer_token);
2644 crq.login.len = cpu_to_be32(buffer_size);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002645 ibmvnic_send_crq(adapter, &crq);
2646
2647 return;
2648
Thomas Falcon032c5e82015-12-21 11:26:06 -06002649buf_rsp_map_failed:
2650 kfree(login_rsp_buffer);
2651buf_rsp_alloc_failed:
2652 dma_unmap_single(dev, buffer_token, buffer_size, DMA_TO_DEVICE);
2653buf_map_failed:
2654 kfree(login_buffer);
2655buf_alloc_failed:
2656 return;
2657}
2658
2659static void send_request_map(struct ibmvnic_adapter *adapter, dma_addr_t addr,
2660 u32 len, u8 map_id)
2661{
2662 union ibmvnic_crq crq;
2663
2664 memset(&crq, 0, sizeof(crq));
2665 crq.request_map.first = IBMVNIC_CRQ_CMD;
2666 crq.request_map.cmd = REQUEST_MAP;
2667 crq.request_map.map_id = map_id;
2668 crq.request_map.ioba = cpu_to_be32(addr);
2669 crq.request_map.len = cpu_to_be32(len);
2670 ibmvnic_send_crq(adapter, &crq);
2671}
2672
2673static void send_request_unmap(struct ibmvnic_adapter *adapter, u8 map_id)
2674{
2675 union ibmvnic_crq crq;
2676
2677 memset(&crq, 0, sizeof(crq));
2678 crq.request_unmap.first = IBMVNIC_CRQ_CMD;
2679 crq.request_unmap.cmd = REQUEST_UNMAP;
2680 crq.request_unmap.map_id = map_id;
2681 ibmvnic_send_crq(adapter, &crq);
2682}
2683
2684static void send_map_query(struct ibmvnic_adapter *adapter)
2685{
2686 union ibmvnic_crq crq;
2687
2688 memset(&crq, 0, sizeof(crq));
2689 crq.query_map.first = IBMVNIC_CRQ_CMD;
2690 crq.query_map.cmd = QUERY_MAP;
2691 ibmvnic_send_crq(adapter, &crq);
2692}
2693
2694/* Send a series of CRQs requesting various capabilities of the VNIC server */
2695static void send_cap_queries(struct ibmvnic_adapter *adapter)
2696{
2697 union ibmvnic_crq crq;
2698
Thomas Falcon901e0402017-02-15 12:17:59 -06002699 atomic_set(&adapter->running_cap_crqs, 0);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002700 memset(&crq, 0, sizeof(crq));
2701 crq.query_capability.first = IBMVNIC_CRQ_CMD;
2702 crq.query_capability.cmd = QUERY_CAPABILITY;
2703
2704 crq.query_capability.capability = cpu_to_be16(MIN_TX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002705 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002706 ibmvnic_send_crq(adapter, &crq);
2707
2708 crq.query_capability.capability = cpu_to_be16(MIN_RX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002709 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002710 ibmvnic_send_crq(adapter, &crq);
2711
2712 crq.query_capability.capability = cpu_to_be16(MIN_RX_ADD_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002713 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002714 ibmvnic_send_crq(adapter, &crq);
2715
2716 crq.query_capability.capability = cpu_to_be16(MAX_TX_QUEUES);
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 = cpu_to_be16(MAX_RX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002721 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002722 ibmvnic_send_crq(adapter, &crq);
2723
2724 crq.query_capability.capability = cpu_to_be16(MAX_RX_ADD_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002725 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002726 ibmvnic_send_crq(adapter, &crq);
2727
2728 crq.query_capability.capability =
2729 cpu_to_be16(MIN_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06002730 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002731 ibmvnic_send_crq(adapter, &crq);
2732
2733 crq.query_capability.capability =
2734 cpu_to_be16(MIN_RX_ADD_ENTRIES_PER_SUBCRQ);
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 =
2739 cpu_to_be16(MAX_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06002740 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002741 ibmvnic_send_crq(adapter, &crq);
2742
2743 crq.query_capability.capability =
2744 cpu_to_be16(MAX_RX_ADD_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06002745 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002746 ibmvnic_send_crq(adapter, &crq);
2747
2748 crq.query_capability.capability = cpu_to_be16(TCP_IP_OFFLOAD);
Thomas Falcon901e0402017-02-15 12:17:59 -06002749 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002750 ibmvnic_send_crq(adapter, &crq);
2751
2752 crq.query_capability.capability = cpu_to_be16(PROMISC_SUPPORTED);
Thomas Falcon901e0402017-02-15 12:17:59 -06002753 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002754 ibmvnic_send_crq(adapter, &crq);
2755
2756 crq.query_capability.capability = cpu_to_be16(MIN_MTU);
Thomas Falcon901e0402017-02-15 12:17:59 -06002757 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002758 ibmvnic_send_crq(adapter, &crq);
2759
2760 crq.query_capability.capability = cpu_to_be16(MAX_MTU);
Thomas Falcon901e0402017-02-15 12:17:59 -06002761 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002762 ibmvnic_send_crq(adapter, &crq);
2763
2764 crq.query_capability.capability = cpu_to_be16(MAX_MULTICAST_FILTERS);
Thomas Falcon901e0402017-02-15 12:17:59 -06002765 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002766 ibmvnic_send_crq(adapter, &crq);
2767
2768 crq.query_capability.capability = cpu_to_be16(VLAN_HEADER_INSERTION);
Thomas Falcon901e0402017-02-15 12:17:59 -06002769 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002770 ibmvnic_send_crq(adapter, &crq);
2771
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04002772 crq.query_capability.capability = cpu_to_be16(RX_VLAN_HEADER_INSERTION);
2773 atomic_inc(&adapter->running_cap_crqs);
2774 ibmvnic_send_crq(adapter, &crq);
2775
Thomas Falcon032c5e82015-12-21 11:26:06 -06002776 crq.query_capability.capability = cpu_to_be16(MAX_TX_SG_ENTRIES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002777 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002778 ibmvnic_send_crq(adapter, &crq);
2779
2780 crq.query_capability.capability = cpu_to_be16(RX_SG_SUPPORTED);
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 = cpu_to_be16(OPT_TX_COMP_SUB_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002785 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002786 ibmvnic_send_crq(adapter, &crq);
2787
2788 crq.query_capability.capability = cpu_to_be16(OPT_RX_COMP_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002789 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002790 ibmvnic_send_crq(adapter, &crq);
2791
2792 crq.query_capability.capability =
2793 cpu_to_be16(OPT_RX_BUFADD_Q_PER_RX_COMP_Q);
Thomas Falcon901e0402017-02-15 12:17:59 -06002794 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002795 ibmvnic_send_crq(adapter, &crq);
2796
2797 crq.query_capability.capability =
2798 cpu_to_be16(OPT_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06002799 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002800 ibmvnic_send_crq(adapter, &crq);
2801
2802 crq.query_capability.capability =
2803 cpu_to_be16(OPT_RXBA_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06002804 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002805 ibmvnic_send_crq(adapter, &crq);
2806
2807 crq.query_capability.capability = cpu_to_be16(TX_RX_DESC_REQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06002808 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002809 ibmvnic_send_crq(adapter, &crq);
2810}
2811
2812static void handle_query_ip_offload_rsp(struct ibmvnic_adapter *adapter)
2813{
2814 struct device *dev = &adapter->vdev->dev;
2815 struct ibmvnic_query_ip_offload_buffer *buf = &adapter->ip_offload_buf;
2816 union ibmvnic_crq crq;
2817 int i;
2818
2819 dma_unmap_single(dev, adapter->ip_offload_tok,
2820 sizeof(adapter->ip_offload_buf), DMA_FROM_DEVICE);
2821
2822 netdev_dbg(adapter->netdev, "Query IP Offload Buffer:\n");
2823 for (i = 0; i < (sizeof(adapter->ip_offload_buf) - 1) / 8 + 1; i++)
2824 netdev_dbg(adapter->netdev, "%016lx\n",
2825 ((unsigned long int *)(buf))[i]);
2826
2827 netdev_dbg(adapter->netdev, "ipv4_chksum = %d\n", buf->ipv4_chksum);
2828 netdev_dbg(adapter->netdev, "ipv6_chksum = %d\n", buf->ipv6_chksum);
2829 netdev_dbg(adapter->netdev, "tcp_ipv4_chksum = %d\n",
2830 buf->tcp_ipv4_chksum);
2831 netdev_dbg(adapter->netdev, "tcp_ipv6_chksum = %d\n",
2832 buf->tcp_ipv6_chksum);
2833 netdev_dbg(adapter->netdev, "udp_ipv4_chksum = %d\n",
2834 buf->udp_ipv4_chksum);
2835 netdev_dbg(adapter->netdev, "udp_ipv6_chksum = %d\n",
2836 buf->udp_ipv6_chksum);
2837 netdev_dbg(adapter->netdev, "large_tx_ipv4 = %d\n",
2838 buf->large_tx_ipv4);
2839 netdev_dbg(adapter->netdev, "large_tx_ipv6 = %d\n",
2840 buf->large_tx_ipv6);
2841 netdev_dbg(adapter->netdev, "large_rx_ipv4 = %d\n",
2842 buf->large_rx_ipv4);
2843 netdev_dbg(adapter->netdev, "large_rx_ipv6 = %d\n",
2844 buf->large_rx_ipv6);
2845 netdev_dbg(adapter->netdev, "max_ipv4_hdr_sz = %d\n",
2846 buf->max_ipv4_header_size);
2847 netdev_dbg(adapter->netdev, "max_ipv6_hdr_sz = %d\n",
2848 buf->max_ipv6_header_size);
2849 netdev_dbg(adapter->netdev, "max_tcp_hdr_size = %d\n",
2850 buf->max_tcp_header_size);
2851 netdev_dbg(adapter->netdev, "max_udp_hdr_size = %d\n",
2852 buf->max_udp_header_size);
2853 netdev_dbg(adapter->netdev, "max_large_tx_size = %d\n",
2854 buf->max_large_tx_size);
2855 netdev_dbg(adapter->netdev, "max_large_rx_size = %d\n",
2856 buf->max_large_rx_size);
2857 netdev_dbg(adapter->netdev, "ipv6_ext_hdr = %d\n",
2858 buf->ipv6_extension_header);
2859 netdev_dbg(adapter->netdev, "tcp_pseudosum_req = %d\n",
2860 buf->tcp_pseudosum_req);
2861 netdev_dbg(adapter->netdev, "num_ipv6_ext_hd = %d\n",
2862 buf->num_ipv6_ext_headers);
2863 netdev_dbg(adapter->netdev, "off_ipv6_ext_hd = %d\n",
2864 buf->off_ipv6_ext_headers);
2865
2866 adapter->ip_offload_ctrl_tok =
2867 dma_map_single(dev, &adapter->ip_offload_ctrl,
2868 sizeof(adapter->ip_offload_ctrl), DMA_TO_DEVICE);
2869
2870 if (dma_mapping_error(dev, adapter->ip_offload_ctrl_tok)) {
2871 dev_err(dev, "Couldn't map ip offload control buffer\n");
2872 return;
2873 }
2874
2875 adapter->ip_offload_ctrl.version = cpu_to_be32(INITIAL_VERSION_IOB);
2876 adapter->ip_offload_ctrl.tcp_ipv4_chksum = buf->tcp_ipv4_chksum;
2877 adapter->ip_offload_ctrl.udp_ipv4_chksum = buf->udp_ipv4_chksum;
2878 adapter->ip_offload_ctrl.tcp_ipv6_chksum = buf->tcp_ipv6_chksum;
2879 adapter->ip_offload_ctrl.udp_ipv6_chksum = buf->udp_ipv6_chksum;
2880
2881 /* large_tx/rx disabled for now, additional features needed */
2882 adapter->ip_offload_ctrl.large_tx_ipv4 = 0;
2883 adapter->ip_offload_ctrl.large_tx_ipv6 = 0;
2884 adapter->ip_offload_ctrl.large_rx_ipv4 = 0;
2885 adapter->ip_offload_ctrl.large_rx_ipv6 = 0;
2886
2887 adapter->netdev->features = NETIF_F_GSO;
2888
2889 if (buf->tcp_ipv4_chksum || buf->udp_ipv4_chksum)
2890 adapter->netdev->features |= NETIF_F_IP_CSUM;
2891
2892 if (buf->tcp_ipv6_chksum || buf->udp_ipv6_chksum)
2893 adapter->netdev->features |= NETIF_F_IPV6_CSUM;
2894
Thomas Falcon9be02cd2016-04-01 17:20:35 -05002895 if ((adapter->netdev->features &
2896 (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)))
2897 adapter->netdev->features |= NETIF_F_RXCSUM;
2898
Thomas Falcon032c5e82015-12-21 11:26:06 -06002899 memset(&crq, 0, sizeof(crq));
2900 crq.control_ip_offload.first = IBMVNIC_CRQ_CMD;
2901 crq.control_ip_offload.cmd = CONTROL_IP_OFFLOAD;
2902 crq.control_ip_offload.len =
2903 cpu_to_be32(sizeof(adapter->ip_offload_ctrl));
2904 crq.control_ip_offload.ioba = cpu_to_be32(adapter->ip_offload_ctrl_tok);
2905 ibmvnic_send_crq(adapter, &crq);
2906}
2907
2908static void handle_error_info_rsp(union ibmvnic_crq *crq,
2909 struct ibmvnic_adapter *adapter)
2910{
2911 struct device *dev = &adapter->vdev->dev;
Wei Yongjun96183182016-06-27 20:48:53 +08002912 struct ibmvnic_error_buff *error_buff, *tmp;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002913 unsigned long flags;
2914 bool found = false;
2915 int i;
2916
2917 if (!crq->request_error_rsp.rc.code) {
2918 dev_info(dev, "Request Error Rsp returned with rc=%x\n",
2919 crq->request_error_rsp.rc.code);
2920 return;
2921 }
2922
2923 spin_lock_irqsave(&adapter->error_list_lock, flags);
Wei Yongjun96183182016-06-27 20:48:53 +08002924 list_for_each_entry_safe(error_buff, tmp, &adapter->errors, list)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002925 if (error_buff->error_id == crq->request_error_rsp.error_id) {
2926 found = true;
2927 list_del(&error_buff->list);
2928 break;
2929 }
2930 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
2931
2932 if (!found) {
2933 dev_err(dev, "Couldn't find error id %x\n",
Thomas Falcon75224c92017-02-15 10:33:33 -06002934 be32_to_cpu(crq->request_error_rsp.error_id));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002935 return;
2936 }
2937
2938 dev_err(dev, "Detailed info for error id %x:",
Thomas Falcon75224c92017-02-15 10:33:33 -06002939 be32_to_cpu(crq->request_error_rsp.error_id));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002940
2941 for (i = 0; i < error_buff->len; i++) {
2942 pr_cont("%02x", (int)error_buff->buff[i]);
2943 if (i % 8 == 7)
2944 pr_cont(" ");
2945 }
2946 pr_cont("\n");
2947
2948 dma_unmap_single(dev, error_buff->dma, error_buff->len,
2949 DMA_FROM_DEVICE);
2950 kfree(error_buff->buff);
2951 kfree(error_buff);
2952}
2953
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04002954static void request_error_information(struct ibmvnic_adapter *adapter,
2955 union ibmvnic_crq *err_crq)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002956{
Thomas Falcon032c5e82015-12-21 11:26:06 -06002957 struct device *dev = &adapter->vdev->dev;
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04002958 struct net_device *netdev = adapter->netdev;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002959 struct ibmvnic_error_buff *error_buff;
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04002960 unsigned long timeout = msecs_to_jiffies(30000);
2961 union ibmvnic_crq crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002962 unsigned long flags;
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04002963 int rc, detail_len;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002964
2965 error_buff = kmalloc(sizeof(*error_buff), GFP_ATOMIC);
2966 if (!error_buff)
2967 return;
2968
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04002969 detail_len = be32_to_cpu(err_crq->error_indication.detail_error_sz);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002970 error_buff->buff = kmalloc(detail_len, GFP_ATOMIC);
2971 if (!error_buff->buff) {
2972 kfree(error_buff);
2973 return;
2974 }
2975
2976 error_buff->dma = dma_map_single(dev, error_buff->buff, detail_len,
2977 DMA_FROM_DEVICE);
2978 if (dma_mapping_error(dev, error_buff->dma)) {
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04002979 netdev_err(netdev, "Couldn't map error buffer\n");
Thomas Falcon032c5e82015-12-21 11:26:06 -06002980 kfree(error_buff->buff);
2981 kfree(error_buff);
2982 return;
2983 }
2984
Thomas Falcon032c5e82015-12-21 11:26:06 -06002985 error_buff->len = detail_len;
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04002986 error_buff->error_id = err_crq->error_indication.error_id;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002987
2988 spin_lock_irqsave(&adapter->error_list_lock, flags);
2989 list_add_tail(&error_buff->list, &adapter->errors);
2990 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
2991
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04002992 memset(&crq, 0, sizeof(crq));
2993 crq.request_error_info.first = IBMVNIC_CRQ_CMD;
2994 crq.request_error_info.cmd = REQUEST_ERROR_INFO;
2995 crq.request_error_info.ioba = cpu_to_be32(error_buff->dma);
2996 crq.request_error_info.len = cpu_to_be32(detail_len);
2997 crq.request_error_info.error_id = err_crq->error_indication.error_id;
2998
2999 rc = ibmvnic_send_crq(adapter, &crq);
3000 if (rc) {
3001 netdev_err(netdev, "failed to request error information\n");
3002 goto err_info_fail;
3003 }
3004
3005 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
3006 netdev_err(netdev, "timeout waiting for error information\n");
3007 goto err_info_fail;
3008 }
3009
3010 return;
3011
3012err_info_fail:
3013 spin_lock_irqsave(&adapter->error_list_lock, flags);
3014 list_del(&error_buff->list);
3015 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
3016
3017 kfree(error_buff->buff);
3018 kfree(error_buff);
3019}
3020
3021static void handle_error_indication(union ibmvnic_crq *crq,
3022 struct ibmvnic_adapter *adapter)
3023{
3024 struct device *dev = &adapter->vdev->dev;
3025
3026 dev_err(dev, "Firmware reports %serror id %x, cause %d\n",
3027 crq->error_indication.flags
3028 & IBMVNIC_FATAL_ERROR ? "FATAL " : "",
3029 be32_to_cpu(crq->error_indication.error_id),
3030 be16_to_cpu(crq->error_indication.error_cause));
3031
3032 if (be32_to_cpu(crq->error_indication.error_id))
3033 request_error_information(adapter, crq);
Nathan Fontenoted651a12017-05-03 14:04:38 -04003034
3035 if (crq->error_indication.flags & IBMVNIC_FATAL_ERROR)
3036 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
John Allen8cb31cf2017-05-26 10:30:37 -04003037 else
3038 ibmvnic_reset(adapter, VNIC_RESET_NON_FATAL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003039}
3040
3041static void handle_change_mac_rsp(union ibmvnic_crq *crq,
3042 struct ibmvnic_adapter *adapter)
3043{
3044 struct net_device *netdev = adapter->netdev;
3045 struct device *dev = &adapter->vdev->dev;
3046 long rc;
3047
3048 rc = crq->change_mac_addr_rsp.rc.code;
3049 if (rc) {
3050 dev_err(dev, "Error %ld in CHANGE_MAC_ADDR_RSP\n", rc);
3051 return;
3052 }
3053 memcpy(netdev->dev_addr, &crq->change_mac_addr_rsp.mac_addr[0],
3054 ETH_ALEN);
3055}
3056
3057static void handle_request_cap_rsp(union ibmvnic_crq *crq,
3058 struct ibmvnic_adapter *adapter)
3059{
3060 struct device *dev = &adapter->vdev->dev;
3061 u64 *req_value;
3062 char *name;
3063
Thomas Falcon901e0402017-02-15 12:17:59 -06003064 atomic_dec(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003065 switch (be16_to_cpu(crq->request_capability_rsp.capability)) {
3066 case REQ_TX_QUEUES:
3067 req_value = &adapter->req_tx_queues;
3068 name = "tx";
3069 break;
3070 case REQ_RX_QUEUES:
3071 req_value = &adapter->req_rx_queues;
3072 name = "rx";
3073 break;
3074 case REQ_RX_ADD_QUEUES:
3075 req_value = &adapter->req_rx_add_queues;
3076 name = "rx_add";
3077 break;
3078 case REQ_TX_ENTRIES_PER_SUBCRQ:
3079 req_value = &adapter->req_tx_entries_per_subcrq;
3080 name = "tx_entries_per_subcrq";
3081 break;
3082 case REQ_RX_ADD_ENTRIES_PER_SUBCRQ:
3083 req_value = &adapter->req_rx_add_entries_per_subcrq;
3084 name = "rx_add_entries_per_subcrq";
3085 break;
3086 case REQ_MTU:
3087 req_value = &adapter->req_mtu;
3088 name = "mtu";
3089 break;
3090 case PROMISC_REQUESTED:
3091 req_value = &adapter->promisc;
3092 name = "promisc";
3093 break;
3094 default:
3095 dev_err(dev, "Got invalid cap request rsp %d\n",
3096 crq->request_capability.capability);
3097 return;
3098 }
3099
3100 switch (crq->request_capability_rsp.rc.code) {
3101 case SUCCESS:
3102 break;
3103 case PARTIALSUCCESS:
3104 dev_info(dev, "req=%lld, rsp=%ld in %s queue, retrying.\n",
3105 *req_value,
Thomas Falcon28f4d162017-02-15 10:32:11 -06003106 (long int)be64_to_cpu(crq->request_capability_rsp.
Thomas Falcon032c5e82015-12-21 11:26:06 -06003107 number), name);
Nathan Fontenotb5108882017-03-30 02:49:18 -04003108 release_sub_crqs(adapter);
Thomas Falcon28f4d162017-02-15 10:32:11 -06003109 *req_value = be64_to_cpu(crq->request_capability_rsp.number);
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003110 ibmvnic_send_req_caps(adapter, 1);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003111 return;
3112 default:
3113 dev_err(dev, "Error %d in request cap rsp\n",
3114 crq->request_capability_rsp.rc.code);
3115 return;
3116 }
3117
3118 /* Done receiving requested capabilities, query IP offload support */
Thomas Falcon901e0402017-02-15 12:17:59 -06003119 if (atomic_read(&adapter->running_cap_crqs) == 0) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06003120 union ibmvnic_crq newcrq;
3121 int buf_sz = sizeof(struct ibmvnic_query_ip_offload_buffer);
3122 struct ibmvnic_query_ip_offload_buffer *ip_offload_buf =
3123 &adapter->ip_offload_buf;
3124
Thomas Falcon249168a2017-02-15 12:18:00 -06003125 adapter->wait_capability = false;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003126 adapter->ip_offload_tok = dma_map_single(dev, ip_offload_buf,
3127 buf_sz,
3128 DMA_FROM_DEVICE);
3129
3130 if (dma_mapping_error(dev, adapter->ip_offload_tok)) {
3131 if (!firmware_has_feature(FW_FEATURE_CMO))
3132 dev_err(dev, "Couldn't map offload buffer\n");
3133 return;
3134 }
3135
3136 memset(&newcrq, 0, sizeof(newcrq));
3137 newcrq.query_ip_offload.first = IBMVNIC_CRQ_CMD;
3138 newcrq.query_ip_offload.cmd = QUERY_IP_OFFLOAD;
3139 newcrq.query_ip_offload.len = cpu_to_be32(buf_sz);
3140 newcrq.query_ip_offload.ioba =
3141 cpu_to_be32(adapter->ip_offload_tok);
3142
3143 ibmvnic_send_crq(adapter, &newcrq);
3144 }
3145}
3146
3147static int handle_login_rsp(union ibmvnic_crq *login_rsp_crq,
3148 struct ibmvnic_adapter *adapter)
3149{
3150 struct device *dev = &adapter->vdev->dev;
3151 struct ibmvnic_login_rsp_buffer *login_rsp = adapter->login_rsp_buf;
3152 struct ibmvnic_login_buffer *login = adapter->login_buf;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003153 int i;
3154
3155 dma_unmap_single(dev, adapter->login_buf_token, adapter->login_buf_sz,
3156 DMA_BIDIRECTIONAL);
3157 dma_unmap_single(dev, adapter->login_rsp_buf_token,
3158 adapter->login_rsp_buf_sz, DMA_BIDIRECTIONAL);
3159
John Allen498cd8e2016-04-06 11:49:55 -05003160 /* If the number of queues requested can't be allocated by the
3161 * server, the login response will return with code 1. We will need
3162 * to resend the login buffer with fewer queues requested.
3163 */
3164 if (login_rsp_crq->generic.rc.code) {
3165 adapter->renegotiate = true;
3166 complete(&adapter->init_done);
3167 return 0;
3168 }
3169
Thomas Falcon032c5e82015-12-21 11:26:06 -06003170 netdev_dbg(adapter->netdev, "Login Response Buffer:\n");
3171 for (i = 0; i < (adapter->login_rsp_buf_sz - 1) / 8 + 1; i++) {
3172 netdev_dbg(adapter->netdev, "%016lx\n",
3173 ((unsigned long int *)(adapter->login_rsp_buf))[i]);
3174 }
3175
3176 /* Sanity checks */
3177 if (login->num_txcomp_subcrqs != login_rsp->num_txsubm_subcrqs ||
3178 (be32_to_cpu(login->num_rxcomp_subcrqs) *
3179 adapter->req_rx_add_queues !=
3180 be32_to_cpu(login_rsp->num_rxadd_subcrqs))) {
3181 dev_err(dev, "FATAL: Inconsistent login and login rsp\n");
3182 ibmvnic_remove(adapter->vdev);
3183 return -EIO;
3184 }
3185 complete(&adapter->init_done);
3186
Thomas Falcon032c5e82015-12-21 11:26:06 -06003187 return 0;
3188}
3189
Thomas Falcon032c5e82015-12-21 11:26:06 -06003190static void handle_request_unmap_rsp(union ibmvnic_crq *crq,
3191 struct ibmvnic_adapter *adapter)
3192{
3193 struct device *dev = &adapter->vdev->dev;
3194 long rc;
3195
3196 rc = crq->request_unmap_rsp.rc.code;
3197 if (rc)
3198 dev_err(dev, "Error %ld in REQUEST_UNMAP_RSP\n", rc);
3199}
3200
3201static void handle_query_map_rsp(union ibmvnic_crq *crq,
3202 struct ibmvnic_adapter *adapter)
3203{
3204 struct net_device *netdev = adapter->netdev;
3205 struct device *dev = &adapter->vdev->dev;
3206 long rc;
3207
3208 rc = crq->query_map_rsp.rc.code;
3209 if (rc) {
3210 dev_err(dev, "Error %ld in QUERY_MAP_RSP\n", rc);
3211 return;
3212 }
3213 netdev_dbg(netdev, "page_size = %d\ntot_pages = %d\nfree_pages = %d\n",
3214 crq->query_map_rsp.page_size, crq->query_map_rsp.tot_pages,
3215 crq->query_map_rsp.free_pages);
3216}
3217
3218static void handle_query_cap_rsp(union ibmvnic_crq *crq,
3219 struct ibmvnic_adapter *adapter)
3220{
3221 struct net_device *netdev = adapter->netdev;
3222 struct device *dev = &adapter->vdev->dev;
3223 long rc;
3224
Thomas Falcon901e0402017-02-15 12:17:59 -06003225 atomic_dec(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003226 netdev_dbg(netdev, "Outstanding queries: %d\n",
Thomas Falcon901e0402017-02-15 12:17:59 -06003227 atomic_read(&adapter->running_cap_crqs));
Thomas Falcon032c5e82015-12-21 11:26:06 -06003228 rc = crq->query_capability.rc.code;
3229 if (rc) {
3230 dev_err(dev, "Error %ld in QUERY_CAP_RSP\n", rc);
3231 goto out;
3232 }
3233
3234 switch (be16_to_cpu(crq->query_capability.capability)) {
3235 case MIN_TX_QUEUES:
3236 adapter->min_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, "min_tx_queues = %lld\n",
3239 adapter->min_tx_queues);
3240 break;
3241 case MIN_RX_QUEUES:
3242 adapter->min_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, "min_rx_queues = %lld\n",
3245 adapter->min_rx_queues);
3246 break;
3247 case MIN_RX_ADD_QUEUES:
3248 adapter->min_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, "min_rx_add_queues = %lld\n",
3251 adapter->min_rx_add_queues);
3252 break;
3253 case MAX_TX_QUEUES:
3254 adapter->max_tx_queues =
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, "max_tx_queues = %lld\n",
3257 adapter->max_tx_queues);
3258 break;
3259 case MAX_RX_QUEUES:
3260 adapter->max_rx_queues =
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, "max_rx_queues = %lld\n",
3263 adapter->max_rx_queues);
3264 break;
3265 case MAX_RX_ADD_QUEUES:
3266 adapter->max_rx_add_queues =
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_rx_add_queues = %lld\n",
3269 adapter->max_rx_add_queues);
3270 break;
3271 case MIN_TX_ENTRIES_PER_SUBCRQ:
3272 adapter->min_tx_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, "min_tx_entries_per_subcrq = %lld\n",
3275 adapter->min_tx_entries_per_subcrq);
3276 break;
3277 case MIN_RX_ADD_ENTRIES_PER_SUBCRQ:
3278 adapter->min_rx_add_entries_per_subcrq =
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, "min_rx_add_entrs_per_subcrq = %lld\n",
3281 adapter->min_rx_add_entries_per_subcrq);
3282 break;
3283 case MAX_TX_ENTRIES_PER_SUBCRQ:
3284 adapter->max_tx_entries_per_subcrq =
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, "max_tx_entries_per_subcrq = %lld\n",
3287 adapter->max_tx_entries_per_subcrq);
3288 break;
3289 case MAX_RX_ADD_ENTRIES_PER_SUBCRQ:
3290 adapter->max_rx_add_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06003291 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003292 netdev_dbg(netdev, "max_rx_add_entrs_per_subcrq = %lld\n",
3293 adapter->max_rx_add_entries_per_subcrq);
3294 break;
3295 case TCP_IP_OFFLOAD:
3296 adapter->tcp_ip_offload =
Thomas Falconde89e852016-03-01 10:20:09 -06003297 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003298 netdev_dbg(netdev, "tcp_ip_offload = %lld\n",
3299 adapter->tcp_ip_offload);
3300 break;
3301 case PROMISC_SUPPORTED:
3302 adapter->promisc_supported =
Thomas Falconde89e852016-03-01 10:20:09 -06003303 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003304 netdev_dbg(netdev, "promisc_supported = %lld\n",
3305 adapter->promisc_supported);
3306 break;
3307 case MIN_MTU:
Thomas Falconde89e852016-03-01 10:20:09 -06003308 adapter->min_mtu = be64_to_cpu(crq->query_capability.number);
Thomas Falconf39f0d12017-02-14 10:22:59 -06003309 netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003310 netdev_dbg(netdev, "min_mtu = %lld\n", adapter->min_mtu);
3311 break;
3312 case MAX_MTU:
Thomas Falconde89e852016-03-01 10:20:09 -06003313 adapter->max_mtu = be64_to_cpu(crq->query_capability.number);
Thomas Falconf39f0d12017-02-14 10:22:59 -06003314 netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003315 netdev_dbg(netdev, "max_mtu = %lld\n", adapter->max_mtu);
3316 break;
3317 case MAX_MULTICAST_FILTERS:
3318 adapter->max_multicast_filters =
Thomas Falconde89e852016-03-01 10:20:09 -06003319 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003320 netdev_dbg(netdev, "max_multicast_filters = %lld\n",
3321 adapter->max_multicast_filters);
3322 break;
3323 case VLAN_HEADER_INSERTION:
3324 adapter->vlan_header_insertion =
Thomas Falconde89e852016-03-01 10:20:09 -06003325 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003326 if (adapter->vlan_header_insertion)
3327 netdev->features |= NETIF_F_HW_VLAN_STAG_TX;
3328 netdev_dbg(netdev, "vlan_header_insertion = %lld\n",
3329 adapter->vlan_header_insertion);
3330 break;
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04003331 case RX_VLAN_HEADER_INSERTION:
3332 adapter->rx_vlan_header_insertion =
3333 be64_to_cpu(crq->query_capability.number);
3334 netdev_dbg(netdev, "rx_vlan_header_insertion = %lld\n",
3335 adapter->rx_vlan_header_insertion);
3336 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003337 case MAX_TX_SG_ENTRIES:
3338 adapter->max_tx_sg_entries =
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, "max_tx_sg_entries = %lld\n",
3341 adapter->max_tx_sg_entries);
3342 break;
3343 case RX_SG_SUPPORTED:
3344 adapter->rx_sg_supported =
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, "rx_sg_supported = %lld\n",
3347 adapter->rx_sg_supported);
3348 break;
3349 case OPT_TX_COMP_SUB_QUEUES:
3350 adapter->opt_tx_comp_sub_queues =
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_comp_sub_queues = %lld\n",
3353 adapter->opt_tx_comp_sub_queues);
3354 break;
3355 case OPT_RX_COMP_QUEUES:
3356 adapter->opt_rx_comp_queues =
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_rx_comp_queues = %lld\n",
3359 adapter->opt_rx_comp_queues);
3360 break;
3361 case OPT_RX_BUFADD_Q_PER_RX_COMP_Q:
3362 adapter->opt_rx_bufadd_q_per_rx_comp_q =
Thomas Falconde89e852016-03-01 10:20:09 -06003363 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003364 netdev_dbg(netdev, "opt_rx_bufadd_q_per_rx_comp_q = %lld\n",
3365 adapter->opt_rx_bufadd_q_per_rx_comp_q);
3366 break;
3367 case OPT_TX_ENTRIES_PER_SUBCRQ:
3368 adapter->opt_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06003369 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003370 netdev_dbg(netdev, "opt_tx_entries_per_subcrq = %lld\n",
3371 adapter->opt_tx_entries_per_subcrq);
3372 break;
3373 case OPT_RXBA_ENTRIES_PER_SUBCRQ:
3374 adapter->opt_rxba_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06003375 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003376 netdev_dbg(netdev, "opt_rxba_entries_per_subcrq = %lld\n",
3377 adapter->opt_rxba_entries_per_subcrq);
3378 break;
3379 case TX_RX_DESC_REQ:
3380 adapter->tx_rx_desc_req = crq->query_capability.number;
3381 netdev_dbg(netdev, "tx_rx_desc_req = %llx\n",
3382 adapter->tx_rx_desc_req);
3383 break;
3384
3385 default:
3386 netdev_err(netdev, "Got invalid cap rsp %d\n",
3387 crq->query_capability.capability);
3388 }
3389
3390out:
Thomas Falcon249168a2017-02-15 12:18:00 -06003391 if (atomic_read(&adapter->running_cap_crqs) == 0) {
3392 adapter->wait_capability = false;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003393 ibmvnic_send_req_caps(adapter, 0);
Thomas Falcon249168a2017-02-15 12:18:00 -06003394 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06003395}
3396
Thomas Falcon032c5e82015-12-21 11:26:06 -06003397static void ibmvnic_handle_crq(union ibmvnic_crq *crq,
3398 struct ibmvnic_adapter *adapter)
3399{
3400 struct ibmvnic_generic_crq *gen_crq = &crq->generic;
3401 struct net_device *netdev = adapter->netdev;
3402 struct device *dev = &adapter->vdev->dev;
Murilo Fossa Vicentini993a82b2017-04-19 13:44:35 -04003403 u64 *u64_crq = (u64 *)crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003404 long rc;
3405
3406 netdev_dbg(netdev, "Handling CRQ: %016lx %016lx\n",
Murilo Fossa Vicentini993a82b2017-04-19 13:44:35 -04003407 (unsigned long int)cpu_to_be64(u64_crq[0]),
3408 (unsigned long int)cpu_to_be64(u64_crq[1]));
Thomas Falcon032c5e82015-12-21 11:26:06 -06003409 switch (gen_crq->first) {
3410 case IBMVNIC_CRQ_INIT_RSP:
3411 switch (gen_crq->cmd) {
3412 case IBMVNIC_CRQ_INIT:
3413 dev_info(dev, "Partner initialized\n");
John Allen017892c12017-05-26 10:30:19 -04003414 adapter->from_passive_init = true;
3415 complete(&adapter->init_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003416 break;
3417 case IBMVNIC_CRQ_INIT_COMPLETE:
3418 dev_info(dev, "Partner initialization complete\n");
3419 send_version_xchg(adapter);
3420 break;
3421 default:
3422 dev_err(dev, "Unknown crq cmd: %d\n", gen_crq->cmd);
3423 }
3424 return;
3425 case IBMVNIC_CRQ_XPORT_EVENT:
Nathan Fontenoted651a12017-05-03 14:04:38 -04003426 netif_carrier_off(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003427 if (gen_crq->cmd == IBMVNIC_PARTITION_MIGRATED) {
Nathan Fontenoted651a12017-05-03 14:04:38 -04003428 dev_info(dev, "Migrated, re-enabling adapter\n");
3429 ibmvnic_reset(adapter, VNIC_RESET_MOBILITY);
Thomas Falcondfad09a2016-08-18 11:37:51 -05003430 } else if (gen_crq->cmd == IBMVNIC_DEVICE_FAILOVER) {
3431 dev_info(dev, "Backing device failover detected\n");
Nathan Fontenoted651a12017-05-03 14:04:38 -04003432 ibmvnic_reset(adapter, VNIC_RESET_FAILOVER);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003433 } else {
3434 /* The adapter lost the connection */
3435 dev_err(dev, "Virtual Adapter failed (rc=%d)\n",
3436 gen_crq->cmd);
Nathan Fontenoted651a12017-05-03 14:04:38 -04003437 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003438 }
3439 return;
3440 case IBMVNIC_CRQ_CMD_RSP:
3441 break;
3442 default:
3443 dev_err(dev, "Got an invalid msg type 0x%02x\n",
3444 gen_crq->first);
3445 return;
3446 }
3447
3448 switch (gen_crq->cmd) {
3449 case VERSION_EXCHANGE_RSP:
3450 rc = crq->version_exchange_rsp.rc.code;
3451 if (rc) {
3452 dev_err(dev, "Error %ld in VERSION_EXCHG_RSP\n", rc);
3453 break;
3454 }
3455 dev_info(dev, "Partner protocol version is %d\n",
3456 crq->version_exchange_rsp.version);
3457 if (be16_to_cpu(crq->version_exchange_rsp.version) <
3458 ibmvnic_version)
3459 ibmvnic_version =
3460 be16_to_cpu(crq->version_exchange_rsp.version);
3461 send_cap_queries(adapter);
3462 break;
3463 case QUERY_CAPABILITY_RSP:
3464 handle_query_cap_rsp(crq, adapter);
3465 break;
3466 case QUERY_MAP_RSP:
3467 handle_query_map_rsp(crq, adapter);
3468 break;
3469 case REQUEST_MAP_RSP:
Thomas Falconf3be0cb2017-06-21 14:53:01 -05003470 adapter->fw_done_rc = crq->request_map_rsp.rc.code;
3471 complete(&adapter->fw_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003472 break;
3473 case REQUEST_UNMAP_RSP:
3474 handle_request_unmap_rsp(crq, adapter);
3475 break;
3476 case REQUEST_CAPABILITY_RSP:
3477 handle_request_cap_rsp(crq, adapter);
3478 break;
3479 case LOGIN_RSP:
3480 netdev_dbg(netdev, "Got Login Response\n");
3481 handle_login_rsp(crq, adapter);
3482 break;
3483 case LOGICAL_LINK_STATE_RSP:
Nathan Fontenot53da09e2017-04-21 15:39:04 -04003484 netdev_dbg(netdev,
3485 "Got Logical Link State Response, state: %d rc: %d\n",
3486 crq->logical_link_state_rsp.link_state,
3487 crq->logical_link_state_rsp.rc.code);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003488 adapter->logical_link_state =
3489 crq->logical_link_state_rsp.link_state;
Nathan Fontenot53da09e2017-04-21 15:39:04 -04003490 adapter->init_done_rc = crq->logical_link_state_rsp.rc.code;
3491 complete(&adapter->init_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003492 break;
3493 case LINK_STATE_INDICATION:
3494 netdev_dbg(netdev, "Got Logical Link State Indication\n");
3495 adapter->phys_link_state =
3496 crq->link_state_indication.phys_link_state;
3497 adapter->logical_link_state =
3498 crq->link_state_indication.logical_link_state;
3499 break;
3500 case CHANGE_MAC_ADDR_RSP:
3501 netdev_dbg(netdev, "Got MAC address change Response\n");
3502 handle_change_mac_rsp(crq, adapter);
3503 break;
3504 case ERROR_INDICATION:
3505 netdev_dbg(netdev, "Got Error Indication\n");
3506 handle_error_indication(crq, adapter);
3507 break;
3508 case REQUEST_ERROR_RSP:
3509 netdev_dbg(netdev, "Got Error Detail Response\n");
3510 handle_error_info_rsp(crq, adapter);
3511 break;
3512 case REQUEST_STATISTICS_RSP:
3513 netdev_dbg(netdev, "Got Statistics Response\n");
3514 complete(&adapter->stats_done);
3515 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003516 case QUERY_IP_OFFLOAD_RSP:
3517 netdev_dbg(netdev, "Got Query IP offload Response\n");
3518 handle_query_ip_offload_rsp(adapter);
3519 break;
3520 case MULTICAST_CTRL_RSP:
3521 netdev_dbg(netdev, "Got multicast control Response\n");
3522 break;
3523 case CONTROL_IP_OFFLOAD_RSP:
3524 netdev_dbg(netdev, "Got Control IP offload Response\n");
3525 dma_unmap_single(dev, adapter->ip_offload_ctrl_tok,
3526 sizeof(adapter->ip_offload_ctrl),
3527 DMA_TO_DEVICE);
John Allenbd0b6722017-03-17 17:13:40 -05003528 complete(&adapter->init_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003529 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003530 case COLLECT_FW_TRACE_RSP:
3531 netdev_dbg(netdev, "Got Collect firmware trace Response\n");
3532 complete(&adapter->fw_done);
3533 break;
3534 default:
3535 netdev_err(netdev, "Got an invalid cmd type 0x%02x\n",
3536 gen_crq->cmd);
3537 }
3538}
3539
3540static irqreturn_t ibmvnic_interrupt(int irq, void *instance)
3541{
3542 struct ibmvnic_adapter *adapter = instance;
Thomas Falcon6c267b32017-02-15 12:17:58 -06003543
Thomas Falcon6c267b32017-02-15 12:17:58 -06003544 tasklet_schedule(&adapter->tasklet);
Thomas Falcon6c267b32017-02-15 12:17:58 -06003545 return IRQ_HANDLED;
3546}
3547
3548static void ibmvnic_tasklet(void *data)
3549{
3550 struct ibmvnic_adapter *adapter = data;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003551 struct ibmvnic_crq_queue *queue = &adapter->crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003552 union ibmvnic_crq *crq;
3553 unsigned long flags;
3554 bool done = false;
3555
3556 spin_lock_irqsave(&queue->lock, flags);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003557 while (!done) {
3558 /* Pull all the valid messages off the CRQ */
3559 while ((crq = ibmvnic_next_crq(adapter)) != NULL) {
3560 ibmvnic_handle_crq(crq, adapter);
3561 crq->generic.first = 0;
3562 }
Brian Kinged7ecbf2017-04-19 13:44:53 -04003563
3564 /* remain in tasklet until all
3565 * capabilities responses are received
3566 */
3567 if (!adapter->wait_capability)
3568 done = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003569 }
Thomas Falcon249168a2017-02-15 12:18:00 -06003570 /* if capabilities CRQ's were sent in this tasklet, the following
3571 * tasklet must wait until all responses are received
3572 */
3573 if (atomic_read(&adapter->running_cap_crqs) != 0)
3574 adapter->wait_capability = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003575 spin_unlock_irqrestore(&queue->lock, flags);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003576}
3577
3578static int ibmvnic_reenable_crq_queue(struct ibmvnic_adapter *adapter)
3579{
3580 struct vio_dev *vdev = adapter->vdev;
3581 int rc;
3582
3583 do {
3584 rc = plpar_hcall_norets(H_ENABLE_CRQ, vdev->unit_address);
3585 } while (rc == H_IN_PROGRESS || rc == H_BUSY || H_IS_LONG_BUSY(rc));
3586
3587 if (rc)
3588 dev_err(&vdev->dev, "Error enabling adapter (rc=%d)\n", rc);
3589
3590 return rc;
3591}
3592
3593static int ibmvnic_reset_crq(struct ibmvnic_adapter *adapter)
3594{
3595 struct ibmvnic_crq_queue *crq = &adapter->crq;
3596 struct device *dev = &adapter->vdev->dev;
3597 struct vio_dev *vdev = adapter->vdev;
3598 int rc;
3599
3600 /* Close the CRQ */
3601 do {
3602 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
3603 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
3604
3605 /* Clean out the queue */
3606 memset(crq->msgs, 0, PAGE_SIZE);
3607 crq->cur = 0;
3608
3609 /* And re-open it again */
3610 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
3611 crq->msg_token, PAGE_SIZE);
3612
3613 if (rc == H_CLOSED)
3614 /* Adapter is good, but other end is not ready */
3615 dev_warn(dev, "Partner adapter not ready\n");
3616 else if (rc != 0)
3617 dev_warn(dev, "Couldn't register crq (rc=%d)\n", rc);
3618
3619 return rc;
3620}
3621
Nathan Fontenotf9928872017-03-30 02:48:54 -04003622static void release_crq_queue(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003623{
3624 struct ibmvnic_crq_queue *crq = &adapter->crq;
3625 struct vio_dev *vdev = adapter->vdev;
3626 long rc;
3627
Nathan Fontenotf9928872017-03-30 02:48:54 -04003628 if (!crq->msgs)
3629 return;
3630
Thomas Falcon032c5e82015-12-21 11:26:06 -06003631 netdev_dbg(adapter->netdev, "Releasing CRQ\n");
3632 free_irq(vdev->irq, adapter);
Thomas Falcon6c267b32017-02-15 12:17:58 -06003633 tasklet_kill(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003634 do {
3635 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
3636 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
3637
3638 dma_unmap_single(&vdev->dev, crq->msg_token, PAGE_SIZE,
3639 DMA_BIDIRECTIONAL);
3640 free_page((unsigned long)crq->msgs);
Nathan Fontenotf9928872017-03-30 02:48:54 -04003641 crq->msgs = NULL;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003642}
3643
Nathan Fontenotf9928872017-03-30 02:48:54 -04003644static int init_crq_queue(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003645{
3646 struct ibmvnic_crq_queue *crq = &adapter->crq;
3647 struct device *dev = &adapter->vdev->dev;
3648 struct vio_dev *vdev = adapter->vdev;
3649 int rc, retrc = -ENOMEM;
3650
Nathan Fontenotf9928872017-03-30 02:48:54 -04003651 if (crq->msgs)
3652 return 0;
3653
Thomas Falcon032c5e82015-12-21 11:26:06 -06003654 crq->msgs = (union ibmvnic_crq *)get_zeroed_page(GFP_KERNEL);
3655 /* Should we allocate more than one page? */
3656
3657 if (!crq->msgs)
3658 return -ENOMEM;
3659
3660 crq->size = PAGE_SIZE / sizeof(*crq->msgs);
3661 crq->msg_token = dma_map_single(dev, crq->msgs, PAGE_SIZE,
3662 DMA_BIDIRECTIONAL);
3663 if (dma_mapping_error(dev, crq->msg_token))
3664 goto map_failed;
3665
3666 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
3667 crq->msg_token, PAGE_SIZE);
3668
3669 if (rc == H_RESOURCE)
3670 /* maybe kexecing and resource is busy. try a reset */
3671 rc = ibmvnic_reset_crq(adapter);
3672 retrc = rc;
3673
3674 if (rc == H_CLOSED) {
3675 dev_warn(dev, "Partner adapter not ready\n");
3676 } else if (rc) {
3677 dev_warn(dev, "Error %d opening adapter\n", rc);
3678 goto reg_crq_failed;
3679 }
3680
3681 retrc = 0;
3682
Thomas Falcon6c267b32017-02-15 12:17:58 -06003683 tasklet_init(&adapter->tasklet, (void *)ibmvnic_tasklet,
3684 (unsigned long)adapter);
3685
Thomas Falcon032c5e82015-12-21 11:26:06 -06003686 netdev_dbg(adapter->netdev, "registering irq 0x%x\n", vdev->irq);
3687 rc = request_irq(vdev->irq, ibmvnic_interrupt, 0, IBMVNIC_NAME,
3688 adapter);
3689 if (rc) {
3690 dev_err(dev, "Couldn't register irq 0x%x. rc=%d\n",
3691 vdev->irq, rc);
3692 goto req_irq_failed;
3693 }
3694
3695 rc = vio_enable_interrupts(vdev);
3696 if (rc) {
3697 dev_err(dev, "Error %d enabling interrupts\n", rc);
3698 goto req_irq_failed;
3699 }
3700
3701 crq->cur = 0;
3702 spin_lock_init(&crq->lock);
3703
3704 return retrc;
3705
3706req_irq_failed:
Thomas Falcon6c267b32017-02-15 12:17:58 -06003707 tasklet_kill(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003708 do {
3709 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
3710 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
3711reg_crq_failed:
3712 dma_unmap_single(dev, crq->msg_token, PAGE_SIZE, DMA_BIDIRECTIONAL);
3713map_failed:
3714 free_page((unsigned long)crq->msgs);
Nathan Fontenotf9928872017-03-30 02:48:54 -04003715 crq->msgs = NULL;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003716 return retrc;
3717}
3718
John Allenf6ef6402017-03-17 17:13:42 -05003719static int ibmvnic_init(struct ibmvnic_adapter *adapter)
3720{
3721 struct device *dev = &adapter->vdev->dev;
3722 unsigned long timeout = msecs_to_jiffies(30000);
John Allenf6ef6402017-03-17 17:13:42 -05003723 int rc;
3724
Nathan Fontenot28cde752017-05-26 10:31:00 -04003725 if (adapter->resetting) {
3726 rc = ibmvnic_reset_crq(adapter);
3727 if (!rc)
3728 rc = vio_enable_interrupts(adapter->vdev);
3729 } else {
3730 rc = init_crq_queue(adapter);
3731 }
3732
John Allenf6ef6402017-03-17 17:13:42 -05003733 if (rc) {
3734 dev_err(dev, "Couldn't initialize crq. rc=%d\n", rc);
3735 return rc;
3736 }
3737
John Allen017892c12017-05-26 10:30:19 -04003738 adapter->from_passive_init = false;
3739
John Allenf6ef6402017-03-17 17:13:42 -05003740 init_completion(&adapter->init_done);
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04003741 adapter->init_done_rc = 0;
John Allenf6ef6402017-03-17 17:13:42 -05003742 ibmvnic_send_crq_init(adapter);
3743 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
3744 dev_err(dev, "Initialization sequence timed out\n");
John Allen017892c12017-05-26 10:30:19 -04003745 return -1;
3746 }
3747
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04003748 if (adapter->init_done_rc) {
3749 release_crq_queue(adapter);
3750 return adapter->init_done_rc;
3751 }
3752
John Allen017892c12017-05-26 10:30:19 -04003753 if (adapter->from_passive_init) {
3754 adapter->state = VNIC_OPEN;
3755 adapter->from_passive_init = false;
John Allenf6ef6402017-03-17 17:13:42 -05003756 return -1;
3757 }
3758
Nathan Fontenot57a49432017-05-26 10:31:12 -04003759 if (adapter->resetting)
3760 rc = reset_sub_crq_queues(adapter);
3761 else
3762 rc = init_sub_crqs(adapter);
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04003763 if (rc) {
3764 dev_err(dev, "Initialization of sub crqs failed\n");
3765 release_crq_queue(adapter);
Thomas Falcon5df969c2017-06-28 19:55:54 -05003766 return rc;
3767 }
3768
3769 rc = init_sub_crq_irqs(adapter);
3770 if (rc) {
3771 dev_err(dev, "Failed to initialize sub crq irqs\n");
3772 release_crq_queue(adapter);
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04003773 }
3774
3775 return rc;
John Allenf6ef6402017-03-17 17:13:42 -05003776}
3777
Thomas Falcon40c9db82017-06-12 12:35:04 -05003778static struct device_attribute dev_attr_failover;
3779
Thomas Falcon032c5e82015-12-21 11:26:06 -06003780static int ibmvnic_probe(struct vio_dev *dev, const struct vio_device_id *id)
3781{
3782 struct ibmvnic_adapter *adapter;
3783 struct net_device *netdev;
3784 unsigned char *mac_addr_p;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003785 int rc;
3786
3787 dev_dbg(&dev->dev, "entering ibmvnic_probe for UA 0x%x\n",
3788 dev->unit_address);
3789
3790 mac_addr_p = (unsigned char *)vio_get_attribute(dev,
3791 VETH_MAC_ADDR, NULL);
3792 if (!mac_addr_p) {
3793 dev_err(&dev->dev,
3794 "(%s:%3.3d) ERROR: Can't find MAC_ADDR attribute\n",
3795 __FILE__, __LINE__);
3796 return 0;
3797 }
3798
3799 netdev = alloc_etherdev_mq(sizeof(struct ibmvnic_adapter),
3800 IBMVNIC_MAX_TX_QUEUES);
3801 if (!netdev)
3802 return -ENOMEM;
3803
3804 adapter = netdev_priv(netdev);
Nathan Fontenot90c80142017-05-03 14:04:32 -04003805 adapter->state = VNIC_PROBING;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003806 dev_set_drvdata(&dev->dev, netdev);
3807 adapter->vdev = dev;
3808 adapter->netdev = netdev;
3809
3810 ether_addr_copy(adapter->mac_addr, mac_addr_p);
3811 ether_addr_copy(netdev->dev_addr, adapter->mac_addr);
3812 netdev->irq = dev->irq;
3813 netdev->netdev_ops = &ibmvnic_netdev_ops;
3814 netdev->ethtool_ops = &ibmvnic_ethtool_ops;
3815 SET_NETDEV_DEV(netdev, &dev->dev);
3816
3817 spin_lock_init(&adapter->stats_lock);
3818
Thomas Falcon032c5e82015-12-21 11:26:06 -06003819 INIT_LIST_HEAD(&adapter->errors);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003820 spin_lock_init(&adapter->error_list_lock);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003821
Nathan Fontenoted651a12017-05-03 14:04:38 -04003822 INIT_WORK(&adapter->ibmvnic_reset, __ibmvnic_reset);
3823 INIT_LIST_HEAD(&adapter->rwi_list);
3824 mutex_init(&adapter->reset_lock);
3825 mutex_init(&adapter->rwi_lock);
3826 adapter->resetting = false;
3827
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04003828 do {
3829 rc = ibmvnic_init(adapter);
Nathan Fontenot6d659232017-06-21 15:41:02 -05003830 if (rc && rc != EAGAIN) {
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04003831 free_netdev(netdev);
3832 return rc;
3833 }
3834 } while (rc == EAGAIN);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003835
Thomas Falconf39f0d12017-02-14 10:22:59 -06003836 netdev->mtu = adapter->req_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003837
Thomas Falcon40c9db82017-06-12 12:35:04 -05003838 rc = device_create_file(&dev->dev, &dev_attr_failover);
3839 if (rc) {
3840 free_netdev(netdev);
3841 return rc;
3842 }
3843
Thomas Falcon032c5e82015-12-21 11:26:06 -06003844 rc = register_netdev(netdev);
3845 if (rc) {
3846 dev_err(&dev->dev, "failed to register netdev rc=%d\n", rc);
Thomas Falcon40c9db82017-06-12 12:35:04 -05003847 device_remove_file(&dev->dev, &dev_attr_failover);
John Allenf6ef6402017-03-17 17:13:42 -05003848 free_netdev(netdev);
3849 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003850 }
3851 dev_info(&dev->dev, "ibmvnic registered\n");
3852
Nathan Fontenot90c80142017-05-03 14:04:32 -04003853 adapter->state = VNIC_PROBED;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003854 return 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003855}
3856
3857static int ibmvnic_remove(struct vio_dev *dev)
3858{
3859 struct net_device *netdev = dev_get_drvdata(&dev->dev);
Nathan Fontenot37489052017-04-19 13:45:04 -04003860 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003861
Nathan Fontenot90c80142017-05-03 14:04:32 -04003862 adapter->state = VNIC_REMOVING;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003863 unregister_netdev(netdev);
Nathan Fontenoted651a12017-05-03 14:04:38 -04003864 mutex_lock(&adapter->reset_lock);
Nathan Fontenot37489052017-04-19 13:45:04 -04003865
3866 release_resources(adapter);
3867 release_sub_crqs(adapter);
3868 release_crq_queue(adapter);
3869
Nathan Fontenot90c80142017-05-03 14:04:32 -04003870 adapter->state = VNIC_REMOVED;
3871
Nathan Fontenoted651a12017-05-03 14:04:38 -04003872 mutex_unlock(&adapter->reset_lock);
Thomas Falcon40c9db82017-06-12 12:35:04 -05003873 device_remove_file(&dev->dev, &dev_attr_failover);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003874 free_netdev(netdev);
3875 dev_set_drvdata(&dev->dev, NULL);
3876
3877 return 0;
3878}
3879
Thomas Falcon40c9db82017-06-12 12:35:04 -05003880static ssize_t failover_store(struct device *dev, struct device_attribute *attr,
3881 const char *buf, size_t count)
3882{
3883 struct net_device *netdev = dev_get_drvdata(dev);
3884 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
3885 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
3886 __be64 session_token;
3887 long rc;
3888
3889 if (!sysfs_streq(buf, "1"))
3890 return -EINVAL;
3891
3892 rc = plpar_hcall(H_VIOCTL, retbuf, adapter->vdev->unit_address,
3893 H_GET_SESSION_TOKEN, 0, 0, 0);
3894 if (rc) {
3895 netdev_err(netdev, "Couldn't retrieve session token, rc %ld\n",
3896 rc);
3897 return -EINVAL;
3898 }
3899
3900 session_token = (__be64)retbuf[0];
3901 netdev_dbg(netdev, "Initiating client failover, session id %llx\n",
3902 be64_to_cpu(session_token));
3903 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
3904 H_SESSION_ERR_DETECTED, session_token, 0, 0);
3905 if (rc) {
3906 netdev_err(netdev, "Client initiated failover failed, rc %ld\n",
3907 rc);
3908 return -EINVAL;
3909 }
3910
3911 return count;
3912}
3913
3914static DEVICE_ATTR(failover, 0200, NULL, failover_store);
3915
Thomas Falcon032c5e82015-12-21 11:26:06 -06003916static unsigned long ibmvnic_get_desired_dma(struct vio_dev *vdev)
3917{
3918 struct net_device *netdev = dev_get_drvdata(&vdev->dev);
3919 struct ibmvnic_adapter *adapter;
3920 struct iommu_table *tbl;
3921 unsigned long ret = 0;
3922 int i;
3923
3924 tbl = get_iommu_table_base(&vdev->dev);
3925
3926 /* netdev inits at probe time along with the structures we need below*/
3927 if (!netdev)
3928 return IOMMU_PAGE_ALIGN(IBMVNIC_IO_ENTITLEMENT_DEFAULT, tbl);
3929
3930 adapter = netdev_priv(netdev);
3931
3932 ret += PAGE_SIZE; /* the crq message queue */
Thomas Falcon032c5e82015-12-21 11:26:06 -06003933 ret += IOMMU_PAGE_ALIGN(sizeof(struct ibmvnic_statistics), tbl);
3934
3935 for (i = 0; i < adapter->req_tx_queues + adapter->req_rx_queues; i++)
3936 ret += 4 * PAGE_SIZE; /* the scrq message queue */
3937
3938 for (i = 0; i < be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
3939 i++)
3940 ret += adapter->rx_pool[i].size *
3941 IOMMU_PAGE_ALIGN(adapter->rx_pool[i].buff_size, tbl);
3942
3943 return ret;
3944}
3945
3946static int ibmvnic_resume(struct device *dev)
3947{
3948 struct net_device *netdev = dev_get_drvdata(dev);
3949 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
3950 int i;
3951
John Allencb89ba22017-06-19 11:27:53 -05003952 if (adapter->state != VNIC_OPEN)
3953 return 0;
3954
John Allena2488782017-07-24 13:26:06 -05003955 tasklet_schedule(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003956
3957 return 0;
3958}
3959
3960static struct vio_device_id ibmvnic_device_table[] = {
3961 {"network", "IBM,vnic"},
3962 {"", "" }
3963};
3964MODULE_DEVICE_TABLE(vio, ibmvnic_device_table);
3965
3966static const struct dev_pm_ops ibmvnic_pm_ops = {
3967 .resume = ibmvnic_resume
3968};
3969
3970static struct vio_driver ibmvnic_driver = {
3971 .id_table = ibmvnic_device_table,
3972 .probe = ibmvnic_probe,
3973 .remove = ibmvnic_remove,
3974 .get_desired_dma = ibmvnic_get_desired_dma,
3975 .name = ibmvnic_driver_name,
3976 .pm = &ibmvnic_pm_ops,
3977};
3978
3979/* module functions */
3980static int __init ibmvnic_module_init(void)
3981{
3982 pr_info("%s: %s %s\n", ibmvnic_driver_name, ibmvnic_driver_string,
3983 IBMVNIC_DRIVER_VERSION);
3984
3985 return vio_register_driver(&ibmvnic_driver);
3986}
3987
3988static void __exit ibmvnic_module_exit(void)
3989{
3990 vio_unregister_driver(&ibmvnic_driver);
3991}
3992
3993module_init(ibmvnic_module_init);
3994module_exit(ibmvnic_module_exit);