blob: 4bc14a90157117567842312b65d43515972ad0e4 [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);
Thomas Falcon4d96f122017-08-01 15:04:36 -0500114static int init_sub_crqs(struct ibmvnic_adapter *);
John Allenbd0b6722017-03-17 17:13:40 -0500115static int init_sub_crq_irqs(struct ibmvnic_adapter *adapter);
John Allenea5509f2017-03-17 17:13:43 -0500116static int ibmvnic_init(struct ibmvnic_adapter *);
Nathan Fontenotf9928872017-03-30 02:48:54 -0400117static void release_crq_queue(struct ibmvnic_adapter *);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600118
119struct ibmvnic_stat {
120 char name[ETH_GSTRING_LEN];
121 int offset;
122};
123
124#define IBMVNIC_STAT_OFF(stat) (offsetof(struct ibmvnic_adapter, stats) + \
125 offsetof(struct ibmvnic_statistics, stat))
126#define IBMVNIC_GET_STAT(a, off) (*((u64 *)(((unsigned long)(a)) + off)))
127
128static const struct ibmvnic_stat ibmvnic_stats[] = {
129 {"rx_packets", IBMVNIC_STAT_OFF(rx_packets)},
130 {"rx_bytes", IBMVNIC_STAT_OFF(rx_bytes)},
131 {"tx_packets", IBMVNIC_STAT_OFF(tx_packets)},
132 {"tx_bytes", IBMVNIC_STAT_OFF(tx_bytes)},
133 {"ucast_tx_packets", IBMVNIC_STAT_OFF(ucast_tx_packets)},
134 {"ucast_rx_packets", IBMVNIC_STAT_OFF(ucast_rx_packets)},
135 {"mcast_tx_packets", IBMVNIC_STAT_OFF(mcast_tx_packets)},
136 {"mcast_rx_packets", IBMVNIC_STAT_OFF(mcast_rx_packets)},
137 {"bcast_tx_packets", IBMVNIC_STAT_OFF(bcast_tx_packets)},
138 {"bcast_rx_packets", IBMVNIC_STAT_OFF(bcast_rx_packets)},
139 {"align_errors", IBMVNIC_STAT_OFF(align_errors)},
140 {"fcs_errors", IBMVNIC_STAT_OFF(fcs_errors)},
141 {"single_collision_frames", IBMVNIC_STAT_OFF(single_collision_frames)},
142 {"multi_collision_frames", IBMVNIC_STAT_OFF(multi_collision_frames)},
143 {"sqe_test_errors", IBMVNIC_STAT_OFF(sqe_test_errors)},
144 {"deferred_tx", IBMVNIC_STAT_OFF(deferred_tx)},
145 {"late_collisions", IBMVNIC_STAT_OFF(late_collisions)},
146 {"excess_collisions", IBMVNIC_STAT_OFF(excess_collisions)},
147 {"internal_mac_tx_errors", IBMVNIC_STAT_OFF(internal_mac_tx_errors)},
148 {"carrier_sense", IBMVNIC_STAT_OFF(carrier_sense)},
149 {"too_long_frames", IBMVNIC_STAT_OFF(too_long_frames)},
150 {"internal_mac_rx_errors", IBMVNIC_STAT_OFF(internal_mac_rx_errors)},
151};
152
153static long h_reg_sub_crq(unsigned long unit_address, unsigned long token,
154 unsigned long length, unsigned long *number,
155 unsigned long *irq)
156{
157 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
158 long rc;
159
160 rc = plpar_hcall(H_REG_SUB_CRQ, retbuf, unit_address, token, length);
161 *number = retbuf[0];
162 *irq = retbuf[1];
163
164 return rc;
165}
166
Thomas Falcon032c5e82015-12-21 11:26:06 -0600167static int alloc_long_term_buff(struct ibmvnic_adapter *adapter,
168 struct ibmvnic_long_term_buff *ltb, int size)
169{
170 struct device *dev = &adapter->vdev->dev;
171
172 ltb->size = size;
173 ltb->buff = dma_alloc_coherent(dev, ltb->size, &ltb->addr,
174 GFP_KERNEL);
175
176 if (!ltb->buff) {
177 dev_err(dev, "Couldn't alloc long term buffer\n");
178 return -ENOMEM;
179 }
180 ltb->map_id = adapter->map_id;
181 adapter->map_id++;
Nathan Fontenotdb5d0b52017-02-10 13:45:05 -0500182
183 init_completion(&adapter->fw_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600184 send_request_map(adapter, ltb->addr,
185 ltb->size, ltb->map_id);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600186 wait_for_completion(&adapter->fw_done);
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500187
188 if (adapter->fw_done_rc) {
189 dev_err(dev, "Couldn't map long term buffer,rc = %d\n",
190 adapter->fw_done_rc);
191 return -1;
192 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600193 return 0;
194}
195
196static void free_long_term_buff(struct ibmvnic_adapter *adapter,
197 struct ibmvnic_long_term_buff *ltb)
198{
199 struct device *dev = &adapter->vdev->dev;
200
Nathan Fontenotc657e322017-03-30 02:49:06 -0400201 if (!ltb->buff)
202 return;
203
Nathan Fontenoted651a12017-05-03 14:04:38 -0400204 if (adapter->reset_reason != VNIC_RESET_FAILOVER &&
205 adapter->reset_reason != VNIC_RESET_MOBILITY)
Thomas Falcondfad09a2016-08-18 11:37:51 -0500206 send_request_unmap(adapter, ltb->map_id);
Brian King59af56c2017-04-19 13:44:41 -0400207 dma_free_coherent(dev, ltb->size, ltb->buff, ltb->addr);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600208}
209
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500210static int reset_long_term_buff(struct ibmvnic_adapter *adapter,
211 struct ibmvnic_long_term_buff *ltb)
212{
213 memset(ltb->buff, 0, ltb->size);
214
215 init_completion(&adapter->fw_done);
216 send_request_map(adapter, ltb->addr, ltb->size, ltb->map_id);
217 wait_for_completion(&adapter->fw_done);
218
219 if (adapter->fw_done_rc) {
220 dev_info(&adapter->vdev->dev,
221 "Reset failed, attempting to free and reallocate buffer\n");
222 free_long_term_buff(adapter, ltb);
223 return alloc_long_term_buff(adapter, ltb, ltb->size);
224 }
225 return 0;
226}
227
Thomas Falconf185a492017-05-26 10:30:48 -0400228static void deactivate_rx_pools(struct ibmvnic_adapter *adapter)
229{
230 int i;
231
232 for (i = 0; i < be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
233 i++)
234 adapter->rx_pool[i].active = 0;
235}
236
Thomas Falcon032c5e82015-12-21 11:26:06 -0600237static void replenish_rx_pool(struct ibmvnic_adapter *adapter,
238 struct ibmvnic_rx_pool *pool)
239{
240 int count = pool->size - atomic_read(&pool->available);
241 struct device *dev = &adapter->vdev->dev;
242 int buffers_added = 0;
243 unsigned long lpar_rc;
244 union sub_crq sub_crq;
245 struct sk_buff *skb;
246 unsigned int offset;
247 dma_addr_t dma_addr;
248 unsigned char *dst;
249 u64 *handle_array;
250 int shift = 0;
251 int index;
252 int i;
253
Thomas Falconf185a492017-05-26 10:30:48 -0400254 if (!pool->active)
255 return;
256
Thomas Falcon032c5e82015-12-21 11:26:06 -0600257 handle_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
258 be32_to_cpu(adapter->login_rsp_buf->
259 off_rxadd_subcrqs));
260
261 for (i = 0; i < count; ++i) {
262 skb = alloc_skb(pool->buff_size, GFP_ATOMIC);
263 if (!skb) {
264 dev_err(dev, "Couldn't replenish rx buff\n");
265 adapter->replenish_no_mem++;
266 break;
267 }
268
269 index = pool->free_map[pool->next_free];
270
271 if (pool->rx_buff[index].skb)
272 dev_err(dev, "Inconsistent free_map!\n");
273
274 /* Copy the skb to the long term mapped DMA buffer */
275 offset = index * pool->buff_size;
276 dst = pool->long_term_buff.buff + offset;
277 memset(dst, 0, pool->buff_size);
278 dma_addr = pool->long_term_buff.addr + offset;
279 pool->rx_buff[index].data = dst;
280
281 pool->free_map[pool->next_free] = IBMVNIC_INVALID_MAP;
282 pool->rx_buff[index].dma = dma_addr;
283 pool->rx_buff[index].skb = skb;
284 pool->rx_buff[index].pool_index = pool->index;
285 pool->rx_buff[index].size = pool->buff_size;
286
287 memset(&sub_crq, 0, sizeof(sub_crq));
288 sub_crq.rx_add.first = IBMVNIC_CRQ_CMD;
289 sub_crq.rx_add.correlator =
290 cpu_to_be64((u64)&pool->rx_buff[index]);
291 sub_crq.rx_add.ioba = cpu_to_be32(dma_addr);
292 sub_crq.rx_add.map_id = pool->long_term_buff.map_id;
293
294 /* The length field of the sCRQ is defined to be 24 bits so the
295 * buffer size needs to be left shifted by a byte before it is
296 * converted to big endian to prevent the last byte from being
297 * truncated.
298 */
299#ifdef __LITTLE_ENDIAN__
300 shift = 8;
301#endif
302 sub_crq.rx_add.len = cpu_to_be32(pool->buff_size << shift);
303
304 lpar_rc = send_subcrq(adapter, handle_array[pool->index],
305 &sub_crq);
306 if (lpar_rc != H_SUCCESS)
307 goto failure;
308
309 buffers_added++;
310 adapter->replenish_add_buff_success++;
311 pool->next_free = (pool->next_free + 1) % pool->size;
312 }
313 atomic_add(buffers_added, &pool->available);
314 return;
315
316failure:
317 dev_info(dev, "replenish pools failure\n");
318 pool->free_map[pool->next_free] = index;
319 pool->rx_buff[index].skb = NULL;
320 if (!dma_mapping_error(dev, dma_addr))
321 dma_unmap_single(dev, dma_addr, pool->buff_size,
322 DMA_FROM_DEVICE);
323
324 dev_kfree_skb_any(skb);
325 adapter->replenish_add_buff_failure++;
326 atomic_add(buffers_added, &pool->available);
Thomas Falconf185a492017-05-26 10:30:48 -0400327
328 if (lpar_rc == H_CLOSED) {
329 /* Disable buffer pool replenishment and report carrier off if
330 * queue is closed. Firmware guarantees that a signal will
331 * be sent to the driver, triggering a reset.
332 */
333 deactivate_rx_pools(adapter);
334 netif_carrier_off(adapter->netdev);
335 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600336}
337
338static void replenish_pools(struct ibmvnic_adapter *adapter)
339{
340 int i;
341
Thomas Falcon032c5e82015-12-21 11:26:06 -0600342 adapter->replenish_task_cycles++;
343 for (i = 0; i < be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
344 i++) {
345 if (adapter->rx_pool[i].active)
346 replenish_rx_pool(adapter, &adapter->rx_pool[i]);
347 }
348}
349
John Allen3d52b592017-08-02 16:44:14 -0500350static void release_stats_buffers(struct ibmvnic_adapter *adapter)
351{
352 kfree(adapter->tx_stats_buffers);
353 kfree(adapter->rx_stats_buffers);
354}
355
356static int init_stats_buffers(struct ibmvnic_adapter *adapter)
357{
358 adapter->tx_stats_buffers =
359 kcalloc(adapter->req_tx_queues,
360 sizeof(struct ibmvnic_tx_queue_stats),
361 GFP_KERNEL);
362 if (!adapter->tx_stats_buffers)
363 return -ENOMEM;
364
365 adapter->rx_stats_buffers =
366 kcalloc(adapter->req_rx_queues,
367 sizeof(struct ibmvnic_rx_queue_stats),
368 GFP_KERNEL);
369 if (!adapter->rx_stats_buffers)
370 return -ENOMEM;
371
372 return 0;
373}
374
Nathan Fontenot7bbc27a2017-03-30 02:49:23 -0400375static void release_stats_token(struct ibmvnic_adapter *adapter)
376{
377 struct device *dev = &adapter->vdev->dev;
378
379 if (!adapter->stats_token)
380 return;
381
382 dma_unmap_single(dev, adapter->stats_token,
383 sizeof(struct ibmvnic_statistics),
384 DMA_FROM_DEVICE);
385 adapter->stats_token = 0;
386}
387
388static int init_stats_token(struct ibmvnic_adapter *adapter)
389{
390 struct device *dev = &adapter->vdev->dev;
391 dma_addr_t stok;
392
393 stok = dma_map_single(dev, &adapter->stats,
394 sizeof(struct ibmvnic_statistics),
395 DMA_FROM_DEVICE);
396 if (dma_mapping_error(dev, stok)) {
397 dev_err(dev, "Couldn't map stats buffer\n");
398 return -1;
399 }
400
401 adapter->stats_token = stok;
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500402 netdev_dbg(adapter->netdev, "Stats token initialized (%llx)\n", stok);
Nathan Fontenot7bbc27a2017-03-30 02:49:23 -0400403 return 0;
404}
405
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400406static int reset_rx_pools(struct ibmvnic_adapter *adapter)
407{
408 struct ibmvnic_rx_pool *rx_pool;
409 int rx_scrqs;
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500410 int i, j, rc;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400411
412 rx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
413 for (i = 0; i < rx_scrqs; i++) {
414 rx_pool = &adapter->rx_pool[i];
415
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500416 netdev_dbg(adapter->netdev, "Re-setting rx_pool[%d]\n", i);
417
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500418 rc = reset_long_term_buff(adapter, &rx_pool->long_term_buff);
419 if (rc)
420 return rc;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400421
422 for (j = 0; j < rx_pool->size; j++)
423 rx_pool->free_map[j] = j;
424
425 memset(rx_pool->rx_buff, 0,
426 rx_pool->size * sizeof(struct ibmvnic_rx_buff));
427
428 atomic_set(&rx_pool->available, 0);
429 rx_pool->next_alloc = 0;
430 rx_pool->next_free = 0;
Thomas Falconc3e53b92017-06-14 23:50:05 -0500431 rx_pool->active = 1;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400432 }
433
434 return 0;
435}
436
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400437static void release_rx_pools(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -0600438{
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400439 struct ibmvnic_rx_pool *rx_pool;
440 int rx_scrqs;
441 int i, j;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600442
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400443 if (!adapter->rx_pool)
Thomas Falcon032c5e82015-12-21 11:26:06 -0600444 return;
445
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400446 rx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
447 for (i = 0; i < rx_scrqs; i++) {
448 rx_pool = &adapter->rx_pool[i];
449
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500450 netdev_dbg(adapter->netdev, "Releasing rx_pool[%d]\n", i);
451
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400452 kfree(rx_pool->free_map);
453 free_long_term_buff(adapter, &rx_pool->long_term_buff);
454
455 if (!rx_pool->rx_buff)
Nathan Fontenote0ebe9422017-05-03 14:04:50 -0400456 continue;
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400457
458 for (j = 0; j < rx_pool->size; j++) {
459 if (rx_pool->rx_buff[j].skb) {
460 dev_kfree_skb_any(rx_pool->rx_buff[i].skb);
461 rx_pool->rx_buff[i].skb = NULL;
462 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600463 }
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400464
465 kfree(rx_pool->rx_buff);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600466 }
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400467
468 kfree(adapter->rx_pool);
469 adapter->rx_pool = NULL;
470}
471
472static int init_rx_pools(struct net_device *netdev)
473{
474 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
475 struct device *dev = &adapter->vdev->dev;
476 struct ibmvnic_rx_pool *rx_pool;
477 int rxadd_subcrqs;
478 u64 *size_array;
479 int i, j;
480
481 rxadd_subcrqs =
482 be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
483 size_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
484 be32_to_cpu(adapter->login_rsp_buf->off_rxadd_buff_size));
485
486 adapter->rx_pool = kcalloc(rxadd_subcrqs,
487 sizeof(struct ibmvnic_rx_pool),
488 GFP_KERNEL);
489 if (!adapter->rx_pool) {
490 dev_err(dev, "Failed to allocate rx pools\n");
491 return -1;
492 }
493
494 for (i = 0; i < rxadd_subcrqs; i++) {
495 rx_pool = &adapter->rx_pool[i];
496
497 netdev_dbg(adapter->netdev,
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500498 "Initializing rx_pool[%d], %lld buffs, %lld bytes each\n",
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400499 i, adapter->req_rx_add_entries_per_subcrq,
500 be64_to_cpu(size_array[i]));
501
502 rx_pool->size = adapter->req_rx_add_entries_per_subcrq;
503 rx_pool->index = i;
504 rx_pool->buff_size = be64_to_cpu(size_array[i]);
505 rx_pool->active = 1;
506
507 rx_pool->free_map = kcalloc(rx_pool->size, sizeof(int),
508 GFP_KERNEL);
509 if (!rx_pool->free_map) {
510 release_rx_pools(adapter);
511 return -1;
512 }
513
514 rx_pool->rx_buff = kcalloc(rx_pool->size,
515 sizeof(struct ibmvnic_rx_buff),
516 GFP_KERNEL);
517 if (!rx_pool->rx_buff) {
518 dev_err(dev, "Couldn't alloc rx buffers\n");
519 release_rx_pools(adapter);
520 return -1;
521 }
522
523 if (alloc_long_term_buff(adapter, &rx_pool->long_term_buff,
524 rx_pool->size * rx_pool->buff_size)) {
525 release_rx_pools(adapter);
526 return -1;
527 }
528
529 for (j = 0; j < rx_pool->size; ++j)
530 rx_pool->free_map[j] = j;
531
532 atomic_set(&rx_pool->available, 0);
533 rx_pool->next_alloc = 0;
534 rx_pool->next_free = 0;
535 }
536
537 return 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600538}
539
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400540static int reset_tx_pools(struct ibmvnic_adapter *adapter)
541{
542 struct ibmvnic_tx_pool *tx_pool;
543 int tx_scrqs;
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500544 int i, j, rc;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400545
546 tx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
547 for (i = 0; i < tx_scrqs; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500548 netdev_dbg(adapter->netdev, "Re-setting tx_pool[%d]\n", i);
549
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400550 tx_pool = &adapter->tx_pool[i];
551
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500552 rc = reset_long_term_buff(adapter, &tx_pool->long_term_buff);
553 if (rc)
554 return rc;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400555
556 memset(tx_pool->tx_buff, 0,
557 adapter->req_tx_entries_per_subcrq *
558 sizeof(struct ibmvnic_tx_buff));
559
560 for (j = 0; j < adapter->req_tx_entries_per_subcrq; j++)
561 tx_pool->free_map[j] = j;
562
563 tx_pool->consumer_index = 0;
564 tx_pool->producer_index = 0;
565 }
566
567 return 0;
568}
569
Nathan Fontenotc657e322017-03-30 02:49:06 -0400570static void release_tx_pools(struct ibmvnic_adapter *adapter)
571{
572 struct ibmvnic_tx_pool *tx_pool;
573 int i, tx_scrqs;
574
575 if (!adapter->tx_pool)
576 return;
577
578 tx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
579 for (i = 0; i < tx_scrqs; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500580 netdev_dbg(adapter->netdev, "Releasing tx_pool[%d]\n", i);
Nathan Fontenotc657e322017-03-30 02:49:06 -0400581 tx_pool = &adapter->tx_pool[i];
582 kfree(tx_pool->tx_buff);
583 free_long_term_buff(adapter, &tx_pool->long_term_buff);
584 kfree(tx_pool->free_map);
585 }
586
587 kfree(adapter->tx_pool);
588 adapter->tx_pool = NULL;
589}
590
591static int init_tx_pools(struct net_device *netdev)
592{
593 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
594 struct device *dev = &adapter->vdev->dev;
595 struct ibmvnic_tx_pool *tx_pool;
596 int tx_subcrqs;
597 int i, j;
598
599 tx_subcrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
600 adapter->tx_pool = kcalloc(tx_subcrqs,
601 sizeof(struct ibmvnic_tx_pool), GFP_KERNEL);
602 if (!adapter->tx_pool)
603 return -1;
604
605 for (i = 0; i < tx_subcrqs; i++) {
606 tx_pool = &adapter->tx_pool[i];
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500607
608 netdev_dbg(adapter->netdev,
609 "Initializing tx_pool[%d], %lld buffs\n",
610 i, adapter->req_tx_entries_per_subcrq);
611
Nathan Fontenotc657e322017-03-30 02:49:06 -0400612 tx_pool->tx_buff = kcalloc(adapter->req_tx_entries_per_subcrq,
613 sizeof(struct ibmvnic_tx_buff),
614 GFP_KERNEL);
615 if (!tx_pool->tx_buff) {
616 dev_err(dev, "tx pool buffer allocation failed\n");
617 release_tx_pools(adapter);
618 return -1;
619 }
620
621 if (alloc_long_term_buff(adapter, &tx_pool->long_term_buff,
622 adapter->req_tx_entries_per_subcrq *
623 adapter->req_mtu)) {
624 release_tx_pools(adapter);
625 return -1;
626 }
627
628 tx_pool->free_map = kcalloc(adapter->req_tx_entries_per_subcrq,
629 sizeof(int), GFP_KERNEL);
630 if (!tx_pool->free_map) {
631 release_tx_pools(adapter);
632 return -1;
633 }
634
635 for (j = 0; j < adapter->req_tx_entries_per_subcrq; j++)
636 tx_pool->free_map[j] = j;
637
638 tx_pool->consumer_index = 0;
639 tx_pool->producer_index = 0;
640 }
641
642 return 0;
643}
644
Nathan Fontenot661a2622017-04-19 13:44:58 -0400645static void release_error_buffers(struct ibmvnic_adapter *adapter)
646{
647 struct device *dev = &adapter->vdev->dev;
648 struct ibmvnic_error_buff *error_buff, *tmp;
649 unsigned long flags;
650
651 spin_lock_irqsave(&adapter->error_list_lock, flags);
652 list_for_each_entry_safe(error_buff, tmp, &adapter->errors, list) {
653 list_del(&error_buff->list);
654 dma_unmap_single(dev, error_buff->dma, error_buff->len,
655 DMA_FROM_DEVICE);
656 kfree(error_buff->buff);
657 kfree(error_buff);
658 }
659 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
660}
661
John Allend944c3d62017-05-26 10:30:13 -0400662static void ibmvnic_napi_enable(struct ibmvnic_adapter *adapter)
663{
664 int i;
665
666 if (adapter->napi_enabled)
667 return;
668
669 for (i = 0; i < adapter->req_rx_queues; i++)
670 napi_enable(&adapter->napi[i]);
671
672 adapter->napi_enabled = true;
673}
674
675static void ibmvnic_napi_disable(struct ibmvnic_adapter *adapter)
676{
677 int i;
678
679 if (!adapter->napi_enabled)
680 return;
681
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500682 for (i = 0; i < adapter->req_rx_queues; i++) {
683 netdev_dbg(adapter->netdev, "Disabling napi[%d]\n", i);
John Allend944c3d62017-05-26 10:30:13 -0400684 napi_disable(&adapter->napi[i]);
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500685 }
John Allend944c3d62017-05-26 10:30:13 -0400686
687 adapter->napi_enabled = false;
688}
689
John Allena57a5d22017-03-17 17:13:41 -0500690static int ibmvnic_login(struct net_device *netdev)
Thomas Falcon032c5e82015-12-21 11:26:06 -0600691{
692 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
John Allenbd0b6722017-03-17 17:13:40 -0500693 unsigned long timeout = msecs_to_jiffies(30000);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600694 struct device *dev = &adapter->vdev->dev;
Thomas Falcon4d96f122017-08-01 15:04:36 -0500695 int rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600696
John Allenbd0b6722017-03-17 17:13:40 -0500697 do {
698 if (adapter->renegotiate) {
699 adapter->renegotiate = false;
Nathan Fontenotb5108882017-03-30 02:49:18 -0400700 release_sub_crqs(adapter);
John Allenbd0b6722017-03-17 17:13:40 -0500701
702 reinit_completion(&adapter->init_done);
703 send_cap_queries(adapter);
704 if (!wait_for_completion_timeout(&adapter->init_done,
705 timeout)) {
706 dev_err(dev, "Capabilities query timeout\n");
707 return -1;
708 }
Thomas Falcon4d96f122017-08-01 15:04:36 -0500709 rc = init_sub_crqs(adapter);
710 if (rc) {
711 dev_err(dev,
712 "Initialization of SCRQ's failed\n");
713 return -1;
714 }
715 rc = init_sub_crq_irqs(adapter);
716 if (rc) {
717 dev_err(dev,
718 "Initialization of SCRQ's irqs failed\n");
719 return -1;
720 }
John Allenbd0b6722017-03-17 17:13:40 -0500721 }
722
723 reinit_completion(&adapter->init_done);
724 send_login(adapter);
725 if (!wait_for_completion_timeout(&adapter->init_done,
726 timeout)) {
727 dev_err(dev, "Login timeout\n");
728 return -1;
729 }
730 } while (adapter->renegotiate);
731
John Allena57a5d22017-03-17 17:13:41 -0500732 return 0;
733}
734
Nathan Fontenot1b8955e2017-03-30 02:49:29 -0400735static void release_resources(struct ibmvnic_adapter *adapter)
736{
Nathan Fontenotc7bac002017-05-03 14:04:44 -0400737 int i;
738
Nathan Fontenot1b8955e2017-03-30 02:49:29 -0400739 release_tx_pools(adapter);
740 release_rx_pools(adapter);
741
Nathan Fontenot1b8955e2017-03-30 02:49:29 -0400742 release_stats_token(adapter);
John Allen3d52b592017-08-02 16:44:14 -0500743 release_stats_buffers(adapter);
Nathan Fontenot661a2622017-04-19 13:44:58 -0400744 release_error_buffers(adapter);
Nathan Fontenotc7bac002017-05-03 14:04:44 -0400745
746 if (adapter->napi) {
747 for (i = 0; i < adapter->req_rx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500748 if (&adapter->napi[i]) {
749 netdev_dbg(adapter->netdev,
750 "Releasing napi[%d]\n", i);
Nathan Fontenotc7bac002017-05-03 14:04:44 -0400751 netif_napi_del(&adapter->napi[i]);
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500752 }
Nathan Fontenotc7bac002017-05-03 14:04:44 -0400753 }
754 }
Nathan Fontenot1b8955e2017-03-30 02:49:29 -0400755}
756
Nathan Fontenot53da09e2017-04-21 15:39:04 -0400757static int set_link_state(struct ibmvnic_adapter *adapter, u8 link_state)
758{
759 struct net_device *netdev = adapter->netdev;
760 unsigned long timeout = msecs_to_jiffies(30000);
761 union ibmvnic_crq crq;
762 bool resend;
763 int rc;
764
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500765 netdev_dbg(netdev, "setting link state %d\n", link_state);
766
Nathan Fontenot53da09e2017-04-21 15:39:04 -0400767 memset(&crq, 0, sizeof(crq));
768 crq.logical_link_state.first = IBMVNIC_CRQ_CMD;
769 crq.logical_link_state.cmd = LOGICAL_LINK_STATE;
770 crq.logical_link_state.link_state = link_state;
771
772 do {
773 resend = false;
774
775 reinit_completion(&adapter->init_done);
776 rc = ibmvnic_send_crq(adapter, &crq);
777 if (rc) {
778 netdev_err(netdev, "Failed to set link state\n");
779 return rc;
780 }
781
782 if (!wait_for_completion_timeout(&adapter->init_done,
783 timeout)) {
784 netdev_err(netdev, "timeout setting link state\n");
785 return -1;
786 }
787
788 if (adapter->init_done_rc == 1) {
789 /* Partuial success, delay and re-send */
790 mdelay(1000);
791 resend = true;
792 }
793 } while (resend);
794
795 return 0;
796}
797
Thomas Falcon7f3c6e62017-04-21 15:38:40 -0400798static int set_real_num_queues(struct net_device *netdev)
799{
800 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
801 int rc;
802
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500803 netdev_dbg(netdev, "Setting real tx/rx queues (%llx/%llx)\n",
804 adapter->req_tx_queues, adapter->req_rx_queues);
805
Thomas Falcon7f3c6e62017-04-21 15:38:40 -0400806 rc = netif_set_real_num_tx_queues(netdev, adapter->req_tx_queues);
807 if (rc) {
808 netdev_err(netdev, "failed to set the number of tx queues\n");
809 return rc;
810 }
811
812 rc = netif_set_real_num_rx_queues(netdev, adapter->req_rx_queues);
813 if (rc)
814 netdev_err(netdev, "failed to set the number of rx queues\n");
815
816 return rc;
817}
818
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400819static int init_resources(struct ibmvnic_adapter *adapter)
John Allena57a5d22017-03-17 17:13:41 -0500820{
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400821 struct net_device *netdev = adapter->netdev;
822 int i, rc;
John Allena57a5d22017-03-17 17:13:41 -0500823
Thomas Falcon7f3c6e62017-04-21 15:38:40 -0400824 rc = set_real_num_queues(netdev);
825 if (rc)
826 return rc;
John Allenbd0b6722017-03-17 17:13:40 -0500827
John Allen3d52b592017-08-02 16:44:14 -0500828 rc = init_stats_buffers(adapter);
829 if (rc)
830 return rc;
831
Nathan Fontenot5d5e84e2017-04-21 15:38:58 -0400832 rc = init_stats_token(adapter);
833 if (rc)
834 return rc;
835
Thomas Falcon032c5e82015-12-21 11:26:06 -0600836 adapter->map_id = 1;
837 adapter->napi = kcalloc(adapter->req_rx_queues,
838 sizeof(struct napi_struct), GFP_KERNEL);
839 if (!adapter->napi)
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400840 return -ENOMEM;
841
Thomas Falcon032c5e82015-12-21 11:26:06 -0600842 for (i = 0; i < adapter->req_rx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500843 netdev_dbg(netdev, "Adding napi[%d]\n", i);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600844 netif_napi_add(netdev, &adapter->napi[i], ibmvnic_poll,
845 NAPI_POLL_WEIGHT);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600846 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600847
Thomas Falcon032c5e82015-12-21 11:26:06 -0600848 send_map_query(adapter);
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400849
850 rc = init_rx_pools(netdev);
851 if (rc)
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400852 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600853
Nathan Fontenotc657e322017-03-30 02:49:06 -0400854 rc = init_tx_pools(netdev);
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400855 return rc;
856}
857
Nathan Fontenoted651a12017-05-03 14:04:38 -0400858static int __ibmvnic_open(struct net_device *netdev)
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400859{
860 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Nathan Fontenoted651a12017-05-03 14:04:38 -0400861 enum vnic_state prev_state = adapter->state;
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400862 int i, rc;
863
Nathan Fontenot90c80142017-05-03 14:04:32 -0400864 adapter->state = VNIC_OPENING;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600865 replenish_pools(adapter);
John Allend944c3d62017-05-26 10:30:13 -0400866 ibmvnic_napi_enable(adapter);
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400867
Thomas Falcon032c5e82015-12-21 11:26:06 -0600868 /* We're ready to receive frames, enable the sub-crq interrupts and
869 * set the logical link state to up
870 */
Nathan Fontenoted651a12017-05-03 14:04:38 -0400871 for (i = 0; i < adapter->req_rx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500872 netdev_dbg(netdev, "Enabling rx_scrq[%d] irq\n", i);
Nathan Fontenoted651a12017-05-03 14:04:38 -0400873 if (prev_state == VNIC_CLOSED)
874 enable_irq(adapter->rx_scrq[i]->irq);
875 else
876 enable_scrq_irq(adapter, adapter->rx_scrq[i]);
877 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600878
Nathan Fontenoted651a12017-05-03 14:04:38 -0400879 for (i = 0; i < adapter->req_tx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500880 netdev_dbg(netdev, "Enabling tx_scrq[%d] irq\n", i);
Nathan Fontenoted651a12017-05-03 14:04:38 -0400881 if (prev_state == VNIC_CLOSED)
882 enable_irq(adapter->tx_scrq[i]->irq);
883 else
884 enable_scrq_irq(adapter, adapter->tx_scrq[i]);
885 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600886
Nathan Fontenot53da09e2017-04-21 15:39:04 -0400887 rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_UP);
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400888 if (rc) {
889 for (i = 0; i < adapter->req_rx_queues; i++)
890 napi_disable(&adapter->napi[i]);
891 release_resources(adapter);
Nathan Fontenoted651a12017-05-03 14:04:38 -0400892 return rc;
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400893 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600894
Nathan Fontenoted651a12017-05-03 14:04:38 -0400895 netif_tx_start_all_queues(netdev);
896
897 if (prev_state == VNIC_CLOSED) {
898 for (i = 0; i < adapter->req_rx_queues; i++)
899 napi_schedule(&adapter->napi[i]);
900 }
901
902 adapter->state = VNIC_OPEN;
903 return rc;
904}
905
906static int ibmvnic_open(struct net_device *netdev)
907{
908 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
909 int rc;
910
911 mutex_lock(&adapter->reset_lock);
912
913 if (adapter->state != VNIC_CLOSED) {
914 rc = ibmvnic_login(netdev);
915 if (rc) {
916 mutex_unlock(&adapter->reset_lock);
917 return rc;
918 }
919
920 rc = init_resources(adapter);
921 if (rc) {
922 netdev_err(netdev, "failed to initialize resources\n");
923 release_resources(adapter);
924 mutex_unlock(&adapter->reset_lock);
925 return rc;
926 }
927 }
928
929 rc = __ibmvnic_open(netdev);
Mick Tarsele876a8a2017-09-28 13:53:18 -0700930 netif_carrier_on(netdev);
Nathan Fontenoted651a12017-05-03 14:04:38 -0400931 mutex_unlock(&adapter->reset_lock);
932
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400933 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600934}
935
Nathan Fontenotb41b83e2017-05-03 14:04:56 -0400936static void clean_tx_pools(struct ibmvnic_adapter *adapter)
937{
938 struct ibmvnic_tx_pool *tx_pool;
939 u64 tx_entries;
940 int tx_scrqs;
941 int i, j;
942
943 if (!adapter->tx_pool)
944 return;
945
946 tx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
947 tx_entries = adapter->req_tx_entries_per_subcrq;
948
949 /* Free any remaining skbs in the tx buffer pools */
950 for (i = 0; i < tx_scrqs; i++) {
951 tx_pool = &adapter->tx_pool[i];
952 if (!tx_pool)
953 continue;
954
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500955 netdev_dbg(adapter->netdev, "Cleaning tx_pool[%d]\n", i);
Nathan Fontenotb41b83e2017-05-03 14:04:56 -0400956 for (j = 0; j < tx_entries; j++) {
957 if (tx_pool->tx_buff[j].skb) {
958 dev_kfree_skb_any(tx_pool->tx_buff[j].skb);
959 tx_pool->tx_buff[j].skb = NULL;
960 }
961 }
962 }
963}
964
Nathan Fontenoted651a12017-05-03 14:04:38 -0400965static int __ibmvnic_close(struct net_device *netdev)
John Allenea5509f2017-03-17 17:13:43 -0500966{
967 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Nathan Fontenot53da09e2017-04-21 15:39:04 -0400968 int rc = 0;
John Allenea5509f2017-03-17 17:13:43 -0500969 int i;
970
Nathan Fontenot90c80142017-05-03 14:04:32 -0400971 adapter->state = VNIC_CLOSING;
Thomas Falcon4c2687a2017-06-14 23:50:06 -0500972
973 /* ensure that transmissions are stopped if called by do_reset */
974 if (adapter->resetting)
975 netif_tx_disable(netdev);
976 else
977 netif_tx_stop_all_queues(netdev);
978
John Allend944c3d62017-05-26 10:30:13 -0400979 ibmvnic_napi_disable(adapter);
Nathan Fontenot46293b92017-05-03 14:05:02 -0400980
981 if (adapter->tx_scrq) {
982 for (i = 0; i < adapter->req_tx_queues; i++)
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500983 if (adapter->tx_scrq[i]->irq) {
984 netdev_dbg(adapter->netdev,
985 "Disabling tx_scrq[%d] irq\n", i);
Nathan Fontenot46293b92017-05-03 14:05:02 -0400986 disable_irq(adapter->tx_scrq[i]->irq);
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500987 }
Nathan Fontenot46293b92017-05-03 14:05:02 -0400988 }
989
Nathan Fontenot53da09e2017-04-21 15:39:04 -0400990 rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_DN);
Nathan Fontenot46293b92017-05-03 14:05:02 -0400991 if (rc)
992 return rc;
993
994 if (adapter->rx_scrq) {
995 for (i = 0; i < adapter->req_rx_queues; i++) {
996 int retries = 10;
997
998 while (pending_scrq(adapter, adapter->rx_scrq[i])) {
999 retries--;
1000 mdelay(100);
1001
1002 if (retries == 0)
1003 break;
1004 }
1005
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001006 if (adapter->rx_scrq[i]->irq) {
1007 netdev_dbg(adapter->netdev,
1008 "Disabling rx_scrq[%d] irq\n", i);
Nathan Fontenot46293b92017-05-03 14:05:02 -04001009 disable_irq(adapter->rx_scrq[i]->irq);
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001010 }
Nathan Fontenot46293b92017-05-03 14:05:02 -04001011 }
1012 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001013
Thomas Falcon10f76212017-05-26 10:30:31 -04001014 clean_tx_pools(adapter);
Nathan Fontenot90c80142017-05-03 14:04:32 -04001015 adapter->state = VNIC_CLOSED;
Nathan Fontenot53da09e2017-04-21 15:39:04 -04001016 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001017}
1018
Nathan Fontenoted651a12017-05-03 14:04:38 -04001019static int ibmvnic_close(struct net_device *netdev)
1020{
1021 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1022 int rc;
1023
1024 mutex_lock(&adapter->reset_lock);
1025 rc = __ibmvnic_close(netdev);
1026 mutex_unlock(&adapter->reset_lock);
1027
1028 return rc;
1029}
1030
Thomas Falconad7775d2016-04-01 17:20:34 -05001031/**
1032 * build_hdr_data - creates L2/L3/L4 header data buffer
1033 * @hdr_field - bitfield determining needed headers
1034 * @skb - socket buffer
1035 * @hdr_len - array of header lengths
1036 * @tot_len - total length of data
1037 *
1038 * Reads hdr_field to determine which headers are needed by firmware.
1039 * Builds a buffer containing these headers. Saves individual header
1040 * lengths and total buffer length to be used to build descriptors.
1041 */
1042static int build_hdr_data(u8 hdr_field, struct sk_buff *skb,
1043 int *hdr_len, u8 *hdr_data)
1044{
1045 int len = 0;
1046 u8 *hdr;
1047
1048 hdr_len[0] = sizeof(struct ethhdr);
1049
1050 if (skb->protocol == htons(ETH_P_IP)) {
1051 hdr_len[1] = ip_hdr(skb)->ihl * 4;
1052 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
1053 hdr_len[2] = tcp_hdrlen(skb);
1054 else if (ip_hdr(skb)->protocol == IPPROTO_UDP)
1055 hdr_len[2] = sizeof(struct udphdr);
1056 } else if (skb->protocol == htons(ETH_P_IPV6)) {
1057 hdr_len[1] = sizeof(struct ipv6hdr);
1058 if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
1059 hdr_len[2] = tcp_hdrlen(skb);
1060 else if (ipv6_hdr(skb)->nexthdr == IPPROTO_UDP)
1061 hdr_len[2] = sizeof(struct udphdr);
1062 }
1063
1064 memset(hdr_data, 0, 120);
1065 if ((hdr_field >> 6) & 1) {
1066 hdr = skb_mac_header(skb);
1067 memcpy(hdr_data, hdr, hdr_len[0]);
1068 len += hdr_len[0];
1069 }
1070
1071 if ((hdr_field >> 5) & 1) {
1072 hdr = skb_network_header(skb);
1073 memcpy(hdr_data + len, hdr, hdr_len[1]);
1074 len += hdr_len[1];
1075 }
1076
1077 if ((hdr_field >> 4) & 1) {
1078 hdr = skb_transport_header(skb);
1079 memcpy(hdr_data + len, hdr, hdr_len[2]);
1080 len += hdr_len[2];
1081 }
1082 return len;
1083}
1084
1085/**
1086 * create_hdr_descs - create header and header extension descriptors
1087 * @hdr_field - bitfield determining needed headers
1088 * @data - buffer containing header data
1089 * @len - length of data buffer
1090 * @hdr_len - array of individual header lengths
1091 * @scrq_arr - descriptor array
1092 *
1093 * Creates header and, if needed, header extension descriptors and
1094 * places them in a descriptor array, scrq_arr
1095 */
1096
1097static void create_hdr_descs(u8 hdr_field, u8 *hdr_data, int len, int *hdr_len,
1098 union sub_crq *scrq_arr)
1099{
1100 union sub_crq hdr_desc;
1101 int tmp_len = len;
1102 u8 *data, *cur;
1103 int tmp;
1104
1105 while (tmp_len > 0) {
1106 cur = hdr_data + len - tmp_len;
1107
1108 memset(&hdr_desc, 0, sizeof(hdr_desc));
1109 if (cur != hdr_data) {
1110 data = hdr_desc.hdr_ext.data;
1111 tmp = tmp_len > 29 ? 29 : tmp_len;
1112 hdr_desc.hdr_ext.first = IBMVNIC_CRQ_CMD;
1113 hdr_desc.hdr_ext.type = IBMVNIC_HDR_EXT_DESC;
1114 hdr_desc.hdr_ext.len = tmp;
1115 } else {
1116 data = hdr_desc.hdr.data;
1117 tmp = tmp_len > 24 ? 24 : tmp_len;
1118 hdr_desc.hdr.first = IBMVNIC_CRQ_CMD;
1119 hdr_desc.hdr.type = IBMVNIC_HDR_DESC;
1120 hdr_desc.hdr.len = tmp;
1121 hdr_desc.hdr.l2_len = (u8)hdr_len[0];
1122 hdr_desc.hdr.l3_len = cpu_to_be16((u16)hdr_len[1]);
1123 hdr_desc.hdr.l4_len = (u8)hdr_len[2];
1124 hdr_desc.hdr.flag = hdr_field << 1;
1125 }
1126 memcpy(data, cur, tmp);
1127 tmp_len -= tmp;
1128 *scrq_arr = hdr_desc;
1129 scrq_arr++;
1130 }
1131}
1132
1133/**
1134 * build_hdr_descs_arr - build a header descriptor array
1135 * @skb - socket buffer
1136 * @num_entries - number of descriptors to be sent
1137 * @subcrq - first TX descriptor
1138 * @hdr_field - bit field determining which headers will be sent
1139 *
1140 * This function will build a TX descriptor array with applicable
1141 * L2/L3/L4 packet header descriptors to be sent by send_subcrq_indirect.
1142 */
1143
1144static void build_hdr_descs_arr(struct ibmvnic_tx_buff *txbuff,
1145 int *num_entries, u8 hdr_field)
1146{
1147 int hdr_len[3] = {0, 0, 0};
1148 int tot_len, len;
1149 u8 *hdr_data = txbuff->hdr_data;
1150
1151 tot_len = build_hdr_data(hdr_field, txbuff->skb, hdr_len,
1152 txbuff->hdr_data);
1153 len = tot_len;
1154 len -= 24;
1155 if (len > 0)
1156 num_entries += len % 29 ? len / 29 + 1 : len / 29;
1157 create_hdr_descs(hdr_field, hdr_data, tot_len, hdr_len,
1158 txbuff->indir_arr + 1);
1159}
1160
Thomas Falcon032c5e82015-12-21 11:26:06 -06001161static int ibmvnic_xmit(struct sk_buff *skb, struct net_device *netdev)
1162{
1163 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1164 int queue_num = skb_get_queue_mapping(skb);
Thomas Falconad7775d2016-04-01 17:20:34 -05001165 u8 *hdrs = (u8 *)&adapter->tx_rx_desc_req;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001166 struct device *dev = &adapter->vdev->dev;
1167 struct ibmvnic_tx_buff *tx_buff = NULL;
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001168 struct ibmvnic_sub_crq_queue *tx_scrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001169 struct ibmvnic_tx_pool *tx_pool;
1170 unsigned int tx_send_failed = 0;
1171 unsigned int tx_map_failed = 0;
1172 unsigned int tx_dropped = 0;
1173 unsigned int tx_packets = 0;
1174 unsigned int tx_bytes = 0;
1175 dma_addr_t data_dma_addr;
1176 struct netdev_queue *txq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001177 unsigned long lpar_rc;
1178 union sub_crq tx_crq;
1179 unsigned int offset;
Thomas Falconad7775d2016-04-01 17:20:34 -05001180 int num_entries = 1;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001181 unsigned char *dst;
1182 u64 *handle_array;
1183 int index = 0;
1184 int ret = 0;
1185
Nathan Fontenoted651a12017-05-03 14:04:38 -04001186 if (adapter->resetting) {
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001187 if (!netif_subqueue_stopped(netdev, skb))
1188 netif_stop_subqueue(netdev, queue_num);
1189 dev_kfree_skb_any(skb);
1190
Thomas Falcon032c5e82015-12-21 11:26:06 -06001191 tx_send_failed++;
1192 tx_dropped++;
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001193 ret = NETDEV_TX_OK;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001194 goto out;
1195 }
1196
Nathan Fontenot161b8a82017-05-03 14:05:08 -04001197 tx_pool = &adapter->tx_pool[queue_num];
1198 tx_scrq = adapter->tx_scrq[queue_num];
1199 txq = netdev_get_tx_queue(netdev, skb_get_queue_mapping(skb));
1200 handle_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
1201 be32_to_cpu(adapter->login_rsp_buf->off_txsubm_subcrqs));
1202
Thomas Falcon032c5e82015-12-21 11:26:06 -06001203 index = tx_pool->free_map[tx_pool->consumer_index];
1204 offset = index * adapter->req_mtu;
1205 dst = tx_pool->long_term_buff.buff + offset;
1206 memset(dst, 0, adapter->req_mtu);
1207 skb_copy_from_linear_data(skb, dst, skb->len);
1208 data_dma_addr = tx_pool->long_term_buff.addr + offset;
1209
1210 tx_pool->consumer_index =
1211 (tx_pool->consumer_index + 1) %
Thomas Falcon068d9f92017-03-05 12:18:42 -06001212 adapter->req_tx_entries_per_subcrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001213
1214 tx_buff = &tx_pool->tx_buff[index];
1215 tx_buff->skb = skb;
1216 tx_buff->data_dma[0] = data_dma_addr;
1217 tx_buff->data_len[0] = skb->len;
1218 tx_buff->index = index;
1219 tx_buff->pool_index = queue_num;
1220 tx_buff->last_frag = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001221
1222 memset(&tx_crq, 0, sizeof(tx_crq));
1223 tx_crq.v1.first = IBMVNIC_CRQ_CMD;
1224 tx_crq.v1.type = IBMVNIC_TX_DESC;
1225 tx_crq.v1.n_crq_elem = 1;
1226 tx_crq.v1.n_sge = 1;
1227 tx_crq.v1.flags1 = IBMVNIC_TX_COMP_NEEDED;
1228 tx_crq.v1.correlator = cpu_to_be32(index);
1229 tx_crq.v1.dma_reg = cpu_to_be16(tx_pool->long_term_buff.map_id);
1230 tx_crq.v1.sge_len = cpu_to_be32(skb->len);
1231 tx_crq.v1.ioba = cpu_to_be64(data_dma_addr);
1232
1233 if (adapter->vlan_header_insertion) {
1234 tx_crq.v1.flags2 |= IBMVNIC_TX_VLAN_INSERT;
1235 tx_crq.v1.vlan_id = cpu_to_be16(skb->vlan_tci);
1236 }
1237
1238 if (skb->protocol == htons(ETH_P_IP)) {
1239 if (ip_hdr(skb)->version == 4)
1240 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV4;
1241 else if (ip_hdr(skb)->version == 6)
1242 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV6;
1243
1244 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
1245 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_TCP;
1246 else if (ip_hdr(skb)->protocol != IPPROTO_TCP)
1247 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_UDP;
1248 }
1249
Thomas Falconad7775d2016-04-01 17:20:34 -05001250 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06001251 tx_crq.v1.flags1 |= IBMVNIC_TX_CHKSUM_OFFLOAD;
Thomas Falconad7775d2016-04-01 17:20:34 -05001252 hdrs += 2;
1253 }
1254 /* determine if l2/3/4 headers are sent to firmware */
1255 if ((*hdrs >> 7) & 1 &&
1256 (skb->protocol == htons(ETH_P_IP) ||
1257 skb->protocol == htons(ETH_P_IPV6))) {
1258 build_hdr_descs_arr(tx_buff, &num_entries, *hdrs);
1259 tx_crq.v1.n_crq_elem = num_entries;
1260 tx_buff->indir_arr[0] = tx_crq;
1261 tx_buff->indir_dma = dma_map_single(dev, tx_buff->indir_arr,
1262 sizeof(tx_buff->indir_arr),
1263 DMA_TO_DEVICE);
1264 if (dma_mapping_error(dev, tx_buff->indir_dma)) {
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001265 dev_kfree_skb_any(skb);
1266 tx_buff->skb = NULL;
Thomas Falconad7775d2016-04-01 17:20:34 -05001267 if (!firmware_has_feature(FW_FEATURE_CMO))
1268 dev_err(dev, "tx: unable to map descriptor array\n");
1269 tx_map_failed++;
1270 tx_dropped++;
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001271 ret = NETDEV_TX_OK;
Thomas Falconad7775d2016-04-01 17:20:34 -05001272 goto out;
1273 }
John Allen498cd8e2016-04-06 11:49:55 -05001274 lpar_rc = send_subcrq_indirect(adapter, handle_array[queue_num],
Thomas Falconad7775d2016-04-01 17:20:34 -05001275 (u64)tx_buff->indir_dma,
1276 (u64)num_entries);
1277 } else {
John Allen498cd8e2016-04-06 11:49:55 -05001278 lpar_rc = send_subcrq(adapter, handle_array[queue_num],
1279 &tx_crq);
Thomas Falconad7775d2016-04-01 17:20:34 -05001280 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001281 if (lpar_rc != H_SUCCESS) {
1282 dev_err(dev, "tx failed with code %ld\n", lpar_rc);
1283
1284 if (tx_pool->consumer_index == 0)
1285 tx_pool->consumer_index =
Thomas Falcon068d9f92017-03-05 12:18:42 -06001286 adapter->req_tx_entries_per_subcrq - 1;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001287 else
1288 tx_pool->consumer_index--;
1289
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001290 dev_kfree_skb_any(skb);
1291 tx_buff->skb = NULL;
1292
Thomas Falconb8c80b82017-05-26 10:30:42 -04001293 if (lpar_rc == H_CLOSED) {
1294 /* Disable TX and report carrier off if queue is closed.
1295 * Firmware guarantees that a signal will be sent to the
1296 * driver, triggering a reset or some other action.
1297 */
1298 netif_tx_stop_all_queues(netdev);
1299 netif_carrier_off(netdev);
1300 }
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001301
Thomas Falcon032c5e82015-12-21 11:26:06 -06001302 tx_send_failed++;
1303 tx_dropped++;
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001304 ret = NETDEV_TX_OK;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001305 goto out;
1306 }
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001307
Brian King58c8c0c2017-04-19 13:44:47 -04001308 if (atomic_inc_return(&tx_scrq->used)
1309 >= adapter->req_tx_entries_per_subcrq) {
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001310 netdev_info(netdev, "Stopping queue %d\n", queue_num);
1311 netif_stop_subqueue(netdev, queue_num);
1312 }
1313
Thomas Falcon032c5e82015-12-21 11:26:06 -06001314 tx_packets++;
1315 tx_bytes += skb->len;
1316 txq->trans_start = jiffies;
1317 ret = NETDEV_TX_OK;
1318
1319out:
1320 netdev->stats.tx_dropped += tx_dropped;
1321 netdev->stats.tx_bytes += tx_bytes;
1322 netdev->stats.tx_packets += tx_packets;
1323 adapter->tx_send_failed += tx_send_failed;
1324 adapter->tx_map_failed += tx_map_failed;
John Allen3d52b592017-08-02 16:44:14 -05001325 adapter->tx_stats_buffers[queue_num].packets += tx_packets;
1326 adapter->tx_stats_buffers[queue_num].bytes += tx_bytes;
1327 adapter->tx_stats_buffers[queue_num].dropped_packets += tx_dropped;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001328
1329 return ret;
1330}
1331
1332static void ibmvnic_set_multi(struct net_device *netdev)
1333{
1334 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1335 struct netdev_hw_addr *ha;
1336 union ibmvnic_crq crq;
1337
1338 memset(&crq, 0, sizeof(crq));
1339 crq.request_capability.first = IBMVNIC_CRQ_CMD;
1340 crq.request_capability.cmd = REQUEST_CAPABILITY;
1341
1342 if (netdev->flags & IFF_PROMISC) {
1343 if (!adapter->promisc_supported)
1344 return;
1345 } else {
1346 if (netdev->flags & IFF_ALLMULTI) {
1347 /* Accept all multicast */
1348 memset(&crq, 0, sizeof(crq));
1349 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1350 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1351 crq.multicast_ctrl.flags = IBMVNIC_ENABLE_ALL;
1352 ibmvnic_send_crq(adapter, &crq);
1353 } else if (netdev_mc_empty(netdev)) {
1354 /* Reject all multicast */
1355 memset(&crq, 0, sizeof(crq));
1356 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1357 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1358 crq.multicast_ctrl.flags = IBMVNIC_DISABLE_ALL;
1359 ibmvnic_send_crq(adapter, &crq);
1360 } else {
1361 /* Accept one or more multicast(s) */
1362 netdev_for_each_mc_addr(ha, netdev) {
1363 memset(&crq, 0, sizeof(crq));
1364 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1365 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1366 crq.multicast_ctrl.flags = IBMVNIC_ENABLE_MC;
1367 ether_addr_copy(&crq.multicast_ctrl.mac_addr[0],
1368 ha->addr);
1369 ibmvnic_send_crq(adapter, &crq);
1370 }
1371 }
1372 }
1373}
1374
1375static int ibmvnic_set_mac(struct net_device *netdev, void *p)
1376{
1377 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1378 struct sockaddr *addr = p;
1379 union ibmvnic_crq crq;
1380
1381 if (!is_valid_ether_addr(addr->sa_data))
1382 return -EADDRNOTAVAIL;
1383
1384 memset(&crq, 0, sizeof(crq));
1385 crq.change_mac_addr.first = IBMVNIC_CRQ_CMD;
1386 crq.change_mac_addr.cmd = CHANGE_MAC_ADDR;
1387 ether_addr_copy(&crq.change_mac_addr.mac_addr[0], addr->sa_data);
1388 ibmvnic_send_crq(adapter, &crq);
1389 /* netdev->dev_addr is changed in handle_change_mac_rsp function */
1390 return 0;
1391}
1392
Nathan Fontenoted651a12017-05-03 14:04:38 -04001393/**
1394 * do_reset returns zero if we are able to keep processing reset events, or
1395 * non-zero if we hit a fatal error and must halt.
1396 */
1397static int do_reset(struct ibmvnic_adapter *adapter,
1398 struct ibmvnic_rwi *rwi, u32 reset_state)
1399{
1400 struct net_device *netdev = adapter->netdev;
1401 int i, rc;
1402
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001403 netdev_dbg(adapter->netdev, "Re-setting driver (%d)\n",
1404 rwi->reset_reason);
1405
Nathan Fontenoted651a12017-05-03 14:04:38 -04001406 netif_carrier_off(netdev);
1407 adapter->reset_reason = rwi->reset_reason;
1408
1409 if (rwi->reset_reason == VNIC_RESET_MOBILITY) {
1410 rc = ibmvnic_reenable_crq_queue(adapter);
1411 if (rc)
1412 return 0;
1413 }
1414
1415 rc = __ibmvnic_close(netdev);
1416 if (rc)
1417 return rc;
1418
John Allen8cb31cf2017-05-26 10:30:37 -04001419 if (adapter->reset_reason != VNIC_RESET_NON_FATAL) {
1420 /* remove the closed state so when we call open it appears
1421 * we are coming from the probed state.
1422 */
Nathan Fontenoted651a12017-05-03 14:04:38 -04001423 adapter->state = VNIC_PROBED;
John Allen8cb31cf2017-05-26 10:30:37 -04001424
John Allen8cb31cf2017-05-26 10:30:37 -04001425 rc = ibmvnic_init(adapter);
1426 if (rc)
1427 return 0;
1428
1429 /* If the adapter was in PROBE state prior to the reset,
1430 * exit here.
1431 */
1432 if (reset_state == VNIC_PROBED)
1433 return 0;
1434
1435 rc = ibmvnic_login(netdev);
1436 if (rc) {
1437 adapter->state = VNIC_PROBED;
1438 return 0;
1439 }
1440
Nathan Fontenot8c0543a2017-05-26 10:31:06 -04001441 rc = reset_tx_pools(adapter);
1442 if (rc)
1443 return rc;
1444
1445 rc = reset_rx_pools(adapter);
John Allen8cb31cf2017-05-26 10:30:37 -04001446 if (rc)
1447 return rc;
1448
1449 if (reset_state == VNIC_CLOSED)
1450 return 0;
Nathan Fontenoted651a12017-05-03 14:04:38 -04001451 }
1452
Nathan Fontenoted651a12017-05-03 14:04:38 -04001453 rc = __ibmvnic_open(netdev);
1454 if (rc) {
1455 if (list_empty(&adapter->rwi_list))
1456 adapter->state = VNIC_CLOSED;
1457 else
1458 adapter->state = reset_state;
1459
1460 return 0;
1461 }
1462
1463 netif_carrier_on(netdev);
1464
1465 /* kick napi */
1466 for (i = 0; i < adapter->req_rx_queues; i++)
1467 napi_schedule(&adapter->napi[i]);
1468
Nathan Fontenot61d3e1d2017-06-12 20:47:45 -04001469 if (adapter->reset_reason != VNIC_RESET_FAILOVER)
1470 netdev_notify_peers(netdev);
1471
Nathan Fontenoted651a12017-05-03 14:04:38 -04001472 return 0;
1473}
1474
1475static struct ibmvnic_rwi *get_next_rwi(struct ibmvnic_adapter *adapter)
1476{
1477 struct ibmvnic_rwi *rwi;
1478
1479 mutex_lock(&adapter->rwi_lock);
1480
1481 if (!list_empty(&adapter->rwi_list)) {
1482 rwi = list_first_entry(&adapter->rwi_list, struct ibmvnic_rwi,
1483 list);
1484 list_del(&rwi->list);
1485 } else {
1486 rwi = NULL;
1487 }
1488
1489 mutex_unlock(&adapter->rwi_lock);
1490 return rwi;
1491}
1492
1493static void free_all_rwi(struct ibmvnic_adapter *adapter)
1494{
1495 struct ibmvnic_rwi *rwi;
1496
1497 rwi = get_next_rwi(adapter);
1498 while (rwi) {
1499 kfree(rwi);
1500 rwi = get_next_rwi(adapter);
1501 }
1502}
1503
1504static void __ibmvnic_reset(struct work_struct *work)
1505{
1506 struct ibmvnic_rwi *rwi;
1507 struct ibmvnic_adapter *adapter;
1508 struct net_device *netdev;
1509 u32 reset_state;
1510 int rc;
1511
1512 adapter = container_of(work, struct ibmvnic_adapter, ibmvnic_reset);
1513 netdev = adapter->netdev;
1514
1515 mutex_lock(&adapter->reset_lock);
1516 adapter->resetting = true;
1517 reset_state = adapter->state;
1518
1519 rwi = get_next_rwi(adapter);
1520 while (rwi) {
1521 rc = do_reset(adapter, rwi, reset_state);
1522 kfree(rwi);
1523 if (rc)
1524 break;
1525
1526 rwi = get_next_rwi(adapter);
1527 }
1528
1529 if (rc) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001530 netdev_dbg(adapter->netdev, "Reset failed\n");
Nathan Fontenoted651a12017-05-03 14:04:38 -04001531 free_all_rwi(adapter);
Wei Yongjun6d0af072017-05-18 15:24:52 +00001532 mutex_unlock(&adapter->reset_lock);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001533 return;
1534 }
1535
1536 adapter->resetting = false;
1537 mutex_unlock(&adapter->reset_lock);
1538}
1539
1540static void ibmvnic_reset(struct ibmvnic_adapter *adapter,
1541 enum ibmvnic_reset_reason reason)
1542{
1543 struct ibmvnic_rwi *rwi, *tmp;
1544 struct net_device *netdev = adapter->netdev;
1545 struct list_head *entry;
1546
1547 if (adapter->state == VNIC_REMOVING ||
1548 adapter->state == VNIC_REMOVED) {
1549 netdev_dbg(netdev, "Adapter removing, skipping reset\n");
1550 return;
1551 }
1552
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04001553 if (adapter->state == VNIC_PROBING) {
1554 netdev_warn(netdev, "Adapter reset during probe\n");
1555 adapter->init_done_rc = EAGAIN;
1556 return;
1557 }
1558
Nathan Fontenoted651a12017-05-03 14:04:38 -04001559 mutex_lock(&adapter->rwi_lock);
1560
1561 list_for_each(entry, &adapter->rwi_list) {
1562 tmp = list_entry(entry, struct ibmvnic_rwi, list);
1563 if (tmp->reset_reason == reason) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001564 netdev_dbg(netdev, "Skipping matching reset\n");
Nathan Fontenoted651a12017-05-03 14:04:38 -04001565 mutex_unlock(&adapter->rwi_lock);
1566 return;
1567 }
1568 }
1569
1570 rwi = kzalloc(sizeof(*rwi), GFP_KERNEL);
1571 if (!rwi) {
1572 mutex_unlock(&adapter->rwi_lock);
1573 ibmvnic_close(netdev);
1574 return;
1575 }
1576
1577 rwi->reset_reason = reason;
1578 list_add_tail(&rwi->list, &adapter->rwi_list);
1579 mutex_unlock(&adapter->rwi_lock);
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001580
1581 netdev_dbg(adapter->netdev, "Scheduling reset (reason %d)\n", reason);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001582 schedule_work(&adapter->ibmvnic_reset);
1583}
1584
Thomas Falcon032c5e82015-12-21 11:26:06 -06001585static void ibmvnic_tx_timeout(struct net_device *dev)
1586{
1587 struct ibmvnic_adapter *adapter = netdev_priv(dev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001588
Nathan Fontenoted651a12017-05-03 14:04:38 -04001589 ibmvnic_reset(adapter, VNIC_RESET_TIMEOUT);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001590}
1591
1592static void remove_buff_from_pool(struct ibmvnic_adapter *adapter,
1593 struct ibmvnic_rx_buff *rx_buff)
1594{
1595 struct ibmvnic_rx_pool *pool = &adapter->rx_pool[rx_buff->pool_index];
1596
1597 rx_buff->skb = NULL;
1598
1599 pool->free_map[pool->next_alloc] = (int)(rx_buff - pool->rx_buff);
1600 pool->next_alloc = (pool->next_alloc + 1) % pool->size;
1601
1602 atomic_dec(&pool->available);
1603}
1604
1605static int ibmvnic_poll(struct napi_struct *napi, int budget)
1606{
1607 struct net_device *netdev = napi->dev;
1608 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1609 int scrq_num = (int)(napi - adapter->napi);
1610 int frames_processed = 0;
Nathan Fontenot152ce472017-05-26 10:30:54 -04001611
Thomas Falcon032c5e82015-12-21 11:26:06 -06001612restart_poll:
1613 while (frames_processed < budget) {
1614 struct sk_buff *skb;
1615 struct ibmvnic_rx_buff *rx_buff;
1616 union sub_crq *next;
1617 u32 length;
1618 u16 offset;
1619 u8 flags = 0;
1620
Thomas Falcon21ecba62017-06-14 23:50:09 -05001621 if (unlikely(adapter->resetting)) {
1622 enable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
1623 napi_complete_done(napi, frames_processed);
1624 return frames_processed;
1625 }
1626
Thomas Falcon032c5e82015-12-21 11:26:06 -06001627 if (!pending_scrq(adapter, adapter->rx_scrq[scrq_num]))
1628 break;
1629 next = ibmvnic_next_scrq(adapter, adapter->rx_scrq[scrq_num]);
1630 rx_buff =
1631 (struct ibmvnic_rx_buff *)be64_to_cpu(next->
1632 rx_comp.correlator);
1633 /* do error checking */
1634 if (next->rx_comp.rc) {
John Allene1cea2e2017-08-07 15:42:30 -05001635 netdev_dbg(netdev, "rx buffer returned with rc %x\n",
1636 be16_to_cpu(next->rx_comp.rc));
Thomas Falcon032c5e82015-12-21 11:26:06 -06001637 /* free the entry */
1638 next->rx_comp.first = 0;
1639 remove_buff_from_pool(adapter, rx_buff);
Nathan Fontenotca05e312017-05-03 14:05:14 -04001640 continue;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001641 }
1642
1643 length = be32_to_cpu(next->rx_comp.len);
1644 offset = be16_to_cpu(next->rx_comp.off_frame_data);
1645 flags = next->rx_comp.flags;
1646 skb = rx_buff->skb;
1647 skb_copy_to_linear_data(skb, rx_buff->data + offset,
1648 length);
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04001649
1650 /* VLAN Header has been stripped by the system firmware and
1651 * needs to be inserted by the driver
1652 */
1653 if (adapter->rx_vlan_header_insertion &&
1654 (flags & IBMVNIC_VLAN_STRIPPED))
1655 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
1656 ntohs(next->rx_comp.vlan_tci));
1657
Thomas Falcon032c5e82015-12-21 11:26:06 -06001658 /* free the entry */
1659 next->rx_comp.first = 0;
1660 remove_buff_from_pool(adapter, rx_buff);
1661
1662 skb_put(skb, length);
1663 skb->protocol = eth_type_trans(skb, netdev);
Thomas Falcon94ca3052017-05-03 14:05:20 -04001664 skb_record_rx_queue(skb, scrq_num);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001665
1666 if (flags & IBMVNIC_IP_CHKSUM_GOOD &&
1667 flags & IBMVNIC_TCP_UDP_CHKSUM_GOOD) {
1668 skb->ip_summed = CHECKSUM_UNNECESSARY;
1669 }
1670
1671 length = skb->len;
1672 napi_gro_receive(napi, skb); /* send it up */
1673 netdev->stats.rx_packets++;
1674 netdev->stats.rx_bytes += length;
John Allen3d52b592017-08-02 16:44:14 -05001675 adapter->rx_stats_buffers[scrq_num].packets++;
1676 adapter->rx_stats_buffers[scrq_num].bytes += length;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001677 frames_processed++;
1678 }
Nathan Fontenot152ce472017-05-26 10:30:54 -04001679
1680 if (adapter->state != VNIC_CLOSING)
1681 replenish_rx_pool(adapter, &adapter->rx_pool[scrq_num]);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001682
1683 if (frames_processed < budget) {
1684 enable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
Eric Dumazet6ad20162017-01-30 08:22:01 -08001685 napi_complete_done(napi, frames_processed);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001686 if (pending_scrq(adapter, adapter->rx_scrq[scrq_num]) &&
1687 napi_reschedule(napi)) {
1688 disable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
1689 goto restart_poll;
1690 }
1691 }
1692 return frames_processed;
1693}
1694
1695#ifdef CONFIG_NET_POLL_CONTROLLER
1696static void ibmvnic_netpoll_controller(struct net_device *dev)
1697{
1698 struct ibmvnic_adapter *adapter = netdev_priv(dev);
1699 int i;
1700
1701 replenish_pools(netdev_priv(dev));
1702 for (i = 0; i < adapter->req_rx_queues; i++)
1703 ibmvnic_interrupt_rx(adapter->rx_scrq[i]->irq,
1704 adapter->rx_scrq[i]);
1705}
1706#endif
1707
John Allen3a807b72017-06-06 16:55:52 -05001708static int ibmvnic_change_mtu(struct net_device *netdev, int new_mtu)
1709{
1710 return -EOPNOTSUPP;
1711}
1712
Thomas Falcon032c5e82015-12-21 11:26:06 -06001713static const struct net_device_ops ibmvnic_netdev_ops = {
1714 .ndo_open = ibmvnic_open,
1715 .ndo_stop = ibmvnic_close,
1716 .ndo_start_xmit = ibmvnic_xmit,
1717 .ndo_set_rx_mode = ibmvnic_set_multi,
1718 .ndo_set_mac_address = ibmvnic_set_mac,
1719 .ndo_validate_addr = eth_validate_addr,
Thomas Falcon032c5e82015-12-21 11:26:06 -06001720 .ndo_tx_timeout = ibmvnic_tx_timeout,
1721#ifdef CONFIG_NET_POLL_CONTROLLER
1722 .ndo_poll_controller = ibmvnic_netpoll_controller,
1723#endif
John Allen3a807b72017-06-06 16:55:52 -05001724 .ndo_change_mtu = ibmvnic_change_mtu,
Thomas Falcon032c5e82015-12-21 11:26:06 -06001725};
1726
1727/* ethtool functions */
1728
Philippe Reynes8a433792017-01-07 22:37:29 +01001729static int ibmvnic_get_link_ksettings(struct net_device *netdev,
1730 struct ethtool_link_ksettings *cmd)
Thomas Falcon032c5e82015-12-21 11:26:06 -06001731{
Philippe Reynes8a433792017-01-07 22:37:29 +01001732 u32 supported, advertising;
1733
1734 supported = (SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg |
Thomas Falcon032c5e82015-12-21 11:26:06 -06001735 SUPPORTED_FIBRE);
Philippe Reynes8a433792017-01-07 22:37:29 +01001736 advertising = (ADVERTISED_1000baseT_Full | ADVERTISED_Autoneg |
Thomas Falcon032c5e82015-12-21 11:26:06 -06001737 ADVERTISED_FIBRE);
Philippe Reynes8a433792017-01-07 22:37:29 +01001738 cmd->base.speed = SPEED_1000;
1739 cmd->base.duplex = DUPLEX_FULL;
1740 cmd->base.port = PORT_FIBRE;
1741 cmd->base.phy_address = 0;
1742 cmd->base.autoneg = AUTONEG_ENABLE;
1743
1744 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
1745 supported);
1746 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
1747 advertising);
1748
Thomas Falcon032c5e82015-12-21 11:26:06 -06001749 return 0;
1750}
1751
1752static void ibmvnic_get_drvinfo(struct net_device *dev,
1753 struct ethtool_drvinfo *info)
1754{
1755 strlcpy(info->driver, ibmvnic_driver_name, sizeof(info->driver));
1756 strlcpy(info->version, IBMVNIC_DRIVER_VERSION, sizeof(info->version));
1757}
1758
1759static u32 ibmvnic_get_msglevel(struct net_device *netdev)
1760{
1761 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1762
1763 return adapter->msg_enable;
1764}
1765
1766static void ibmvnic_set_msglevel(struct net_device *netdev, u32 data)
1767{
1768 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1769
1770 adapter->msg_enable = data;
1771}
1772
1773static u32 ibmvnic_get_link(struct net_device *netdev)
1774{
1775 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1776
1777 /* Don't need to send a query because we request a logical link up at
1778 * init and then we wait for link state indications
1779 */
1780 return adapter->logical_link_state;
1781}
1782
1783static void ibmvnic_get_ringparam(struct net_device *netdev,
1784 struct ethtool_ringparam *ring)
1785{
John Allenbc131b32017-08-02 16:46:30 -05001786 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1787
1788 ring->rx_max_pending = adapter->max_rx_add_entries_per_subcrq;
1789 ring->tx_max_pending = adapter->max_tx_entries_per_subcrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001790 ring->rx_mini_max_pending = 0;
1791 ring->rx_jumbo_max_pending = 0;
John Allenbc131b32017-08-02 16:46:30 -05001792 ring->rx_pending = adapter->req_rx_add_entries_per_subcrq;
1793 ring->tx_pending = adapter->req_tx_entries_per_subcrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001794 ring->rx_mini_pending = 0;
1795 ring->rx_jumbo_pending = 0;
1796}
1797
John Allenc2dbeb62017-08-02 16:47:17 -05001798static void ibmvnic_get_channels(struct net_device *netdev,
1799 struct ethtool_channels *channels)
1800{
1801 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1802
1803 channels->max_rx = adapter->max_rx_queues;
1804 channels->max_tx = adapter->max_tx_queues;
1805 channels->max_other = 0;
1806 channels->max_combined = 0;
1807 channels->rx_count = adapter->req_rx_queues;
1808 channels->tx_count = adapter->req_tx_queues;
1809 channels->other_count = 0;
1810 channels->combined_count = 0;
1811}
1812
Thomas Falcon032c5e82015-12-21 11:26:06 -06001813static void ibmvnic_get_strings(struct net_device *dev, u32 stringset, u8 *data)
1814{
John Allen3d52b592017-08-02 16:44:14 -05001815 struct ibmvnic_adapter *adapter = netdev_priv(dev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001816 int i;
1817
1818 if (stringset != ETH_SS_STATS)
1819 return;
1820
1821 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++, data += ETH_GSTRING_LEN)
1822 memcpy(data, ibmvnic_stats[i].name, ETH_GSTRING_LEN);
John Allen3d52b592017-08-02 16:44:14 -05001823
1824 for (i = 0; i < adapter->req_tx_queues; i++) {
1825 snprintf(data, ETH_GSTRING_LEN, "tx%d_packets", i);
1826 data += ETH_GSTRING_LEN;
1827
1828 snprintf(data, ETH_GSTRING_LEN, "tx%d_bytes", i);
1829 data += ETH_GSTRING_LEN;
1830
1831 snprintf(data, ETH_GSTRING_LEN, "tx%d_dropped_packets", i);
1832 data += ETH_GSTRING_LEN;
1833 }
1834
1835 for (i = 0; i < adapter->req_rx_queues; i++) {
1836 snprintf(data, ETH_GSTRING_LEN, "rx%d_packets", i);
1837 data += ETH_GSTRING_LEN;
1838
1839 snprintf(data, ETH_GSTRING_LEN, "rx%d_bytes", i);
1840 data += ETH_GSTRING_LEN;
1841
1842 snprintf(data, ETH_GSTRING_LEN, "rx%d_interrupts", i);
1843 data += ETH_GSTRING_LEN;
1844 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001845}
1846
1847static int ibmvnic_get_sset_count(struct net_device *dev, int sset)
1848{
John Allen3d52b592017-08-02 16:44:14 -05001849 struct ibmvnic_adapter *adapter = netdev_priv(dev);
1850
Thomas Falcon032c5e82015-12-21 11:26:06 -06001851 switch (sset) {
1852 case ETH_SS_STATS:
John Allen3d52b592017-08-02 16:44:14 -05001853 return ARRAY_SIZE(ibmvnic_stats) +
1854 adapter->req_tx_queues * NUM_TX_STATS +
1855 adapter->req_rx_queues * NUM_RX_STATS;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001856 default:
1857 return -EOPNOTSUPP;
1858 }
1859}
1860
1861static void ibmvnic_get_ethtool_stats(struct net_device *dev,
1862 struct ethtool_stats *stats, u64 *data)
1863{
1864 struct ibmvnic_adapter *adapter = netdev_priv(dev);
1865 union ibmvnic_crq crq;
John Allen3d52b592017-08-02 16:44:14 -05001866 int i, j;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001867
1868 memset(&crq, 0, sizeof(crq));
1869 crq.request_statistics.first = IBMVNIC_CRQ_CMD;
1870 crq.request_statistics.cmd = REQUEST_STATISTICS;
1871 crq.request_statistics.ioba = cpu_to_be32(adapter->stats_token);
1872 crq.request_statistics.len =
1873 cpu_to_be32(sizeof(struct ibmvnic_statistics));
Thomas Falcon032c5e82015-12-21 11:26:06 -06001874
1875 /* Wait for data to be written */
1876 init_completion(&adapter->stats_done);
Nathan Fontenotdb5d0b52017-02-10 13:45:05 -05001877 ibmvnic_send_crq(adapter, &crq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001878 wait_for_completion(&adapter->stats_done);
1879
1880 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++)
John Allen52da5c12017-08-02 16:45:28 -05001881 data[i] = be64_to_cpu(IBMVNIC_GET_STAT(adapter,
1882 ibmvnic_stats[i].offset));
John Allen3d52b592017-08-02 16:44:14 -05001883
1884 for (j = 0; j < adapter->req_tx_queues; j++) {
1885 data[i] = adapter->tx_stats_buffers[j].packets;
1886 i++;
1887 data[i] = adapter->tx_stats_buffers[j].bytes;
1888 i++;
1889 data[i] = adapter->tx_stats_buffers[j].dropped_packets;
1890 i++;
1891 }
1892
1893 for (j = 0; j < adapter->req_rx_queues; j++) {
1894 data[i] = adapter->rx_stats_buffers[j].packets;
1895 i++;
1896 data[i] = adapter->rx_stats_buffers[j].bytes;
1897 i++;
1898 data[i] = adapter->rx_stats_buffers[j].interrupts;
1899 i++;
1900 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001901}
1902
1903static const struct ethtool_ops ibmvnic_ethtool_ops = {
Thomas Falcon032c5e82015-12-21 11:26:06 -06001904 .get_drvinfo = ibmvnic_get_drvinfo,
1905 .get_msglevel = ibmvnic_get_msglevel,
1906 .set_msglevel = ibmvnic_set_msglevel,
1907 .get_link = ibmvnic_get_link,
1908 .get_ringparam = ibmvnic_get_ringparam,
John Allenc2dbeb62017-08-02 16:47:17 -05001909 .get_channels = ibmvnic_get_channels,
Thomas Falcon032c5e82015-12-21 11:26:06 -06001910 .get_strings = ibmvnic_get_strings,
1911 .get_sset_count = ibmvnic_get_sset_count,
1912 .get_ethtool_stats = ibmvnic_get_ethtool_stats,
Philippe Reynes8a433792017-01-07 22:37:29 +01001913 .get_link_ksettings = ibmvnic_get_link_ksettings,
Thomas Falcon032c5e82015-12-21 11:26:06 -06001914};
1915
1916/* Routines for managing CRQs/sCRQs */
1917
Nathan Fontenot57a49432017-05-26 10:31:12 -04001918static int reset_one_sub_crq_queue(struct ibmvnic_adapter *adapter,
1919 struct ibmvnic_sub_crq_queue *scrq)
1920{
1921 int rc;
1922
1923 if (scrq->irq) {
1924 free_irq(scrq->irq, scrq);
1925 irq_dispose_mapping(scrq->irq);
1926 scrq->irq = 0;
1927 }
1928
Thomas Falconc8b2ad02017-06-14 23:50:07 -05001929 memset(scrq->msgs, 0, 4 * PAGE_SIZE);
Nathan Fontenot57a49432017-05-26 10:31:12 -04001930 scrq->cur = 0;
1931
1932 rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
1933 4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
1934 return rc;
1935}
1936
1937static int reset_sub_crq_queues(struct ibmvnic_adapter *adapter)
1938{
1939 int i, rc;
1940
1941 for (i = 0; i < adapter->req_tx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001942 netdev_dbg(adapter->netdev, "Re-setting tx_scrq[%d]\n", i);
Nathan Fontenot57a49432017-05-26 10:31:12 -04001943 rc = reset_one_sub_crq_queue(adapter, adapter->tx_scrq[i]);
1944 if (rc)
1945 return rc;
1946 }
1947
1948 for (i = 0; i < adapter->req_rx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001949 netdev_dbg(adapter->netdev, "Re-setting rx_scrq[%d]\n", i);
Nathan Fontenot57a49432017-05-26 10:31:12 -04001950 rc = reset_one_sub_crq_queue(adapter, adapter->rx_scrq[i]);
1951 if (rc)
1952 return rc;
1953 }
1954
Nathan Fontenot57a49432017-05-26 10:31:12 -04001955 return rc;
1956}
1957
Thomas Falcon032c5e82015-12-21 11:26:06 -06001958static void release_sub_crq_queue(struct ibmvnic_adapter *adapter,
1959 struct ibmvnic_sub_crq_queue *scrq)
1960{
1961 struct device *dev = &adapter->vdev->dev;
1962 long rc;
1963
1964 netdev_dbg(adapter->netdev, "Releasing sub-CRQ\n");
1965
1966 /* Close the sub-crqs */
1967 do {
1968 rc = plpar_hcall_norets(H_FREE_SUB_CRQ,
1969 adapter->vdev->unit_address,
1970 scrq->crq_num);
1971 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
1972
Thomas Falconffa73852017-04-19 13:44:29 -04001973 if (rc) {
1974 netdev_err(adapter->netdev,
1975 "Failed to release sub-CRQ %16lx, rc = %ld\n",
1976 scrq->crq_num, rc);
1977 }
1978
Thomas Falcon032c5e82015-12-21 11:26:06 -06001979 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
1980 DMA_BIDIRECTIONAL);
1981 free_pages((unsigned long)scrq->msgs, 2);
1982 kfree(scrq);
1983}
1984
1985static struct ibmvnic_sub_crq_queue *init_sub_crq_queue(struct ibmvnic_adapter
1986 *adapter)
1987{
1988 struct device *dev = &adapter->vdev->dev;
1989 struct ibmvnic_sub_crq_queue *scrq;
1990 int rc;
1991
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04001992 scrq = kzalloc(sizeof(*scrq), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001993 if (!scrq)
1994 return NULL;
1995
Nathan Fontenot7f7adc52017-04-19 13:45:16 -04001996 scrq->msgs =
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04001997 (union sub_crq *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, 2);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001998 if (!scrq->msgs) {
1999 dev_warn(dev, "Couldn't allocate crq queue messages page\n");
2000 goto zero_page_failed;
2001 }
2002
2003 scrq->msg_token = dma_map_single(dev, scrq->msgs, 4 * PAGE_SIZE,
2004 DMA_BIDIRECTIONAL);
2005 if (dma_mapping_error(dev, scrq->msg_token)) {
2006 dev_warn(dev, "Couldn't map crq queue messages page\n");
2007 goto map_failed;
2008 }
2009
2010 rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
2011 4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
2012
2013 if (rc == H_RESOURCE)
2014 rc = ibmvnic_reset_crq(adapter);
2015
2016 if (rc == H_CLOSED) {
2017 dev_warn(dev, "Partner adapter not ready, waiting.\n");
2018 } else if (rc) {
2019 dev_warn(dev, "Error %d registering sub-crq\n", rc);
2020 goto reg_failed;
2021 }
2022
Thomas Falcon032c5e82015-12-21 11:26:06 -06002023 scrq->adapter = adapter;
2024 scrq->size = 4 * PAGE_SIZE / sizeof(*scrq->msgs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002025 spin_lock_init(&scrq->lock);
2026
2027 netdev_dbg(adapter->netdev,
2028 "sub-crq initialized, num %lx, hw_irq=%lx, irq=%x\n",
2029 scrq->crq_num, scrq->hw_irq, scrq->irq);
2030
2031 return scrq;
2032
Thomas Falcon032c5e82015-12-21 11:26:06 -06002033reg_failed:
2034 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
2035 DMA_BIDIRECTIONAL);
2036map_failed:
2037 free_pages((unsigned long)scrq->msgs, 2);
2038zero_page_failed:
2039 kfree(scrq);
2040
2041 return NULL;
2042}
2043
2044static void release_sub_crqs(struct ibmvnic_adapter *adapter)
2045{
2046 int i;
2047
2048 if (adapter->tx_scrq) {
Nathan Fontenotb5108882017-03-30 02:49:18 -04002049 for (i = 0; i < adapter->req_tx_queues; i++) {
2050 if (!adapter->tx_scrq[i])
2051 continue;
2052
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002053 netdev_dbg(adapter->netdev, "Releasing tx_scrq[%d]\n",
2054 i);
Nathan Fontenotb5108882017-03-30 02:49:18 -04002055 if (adapter->tx_scrq[i]->irq) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06002056 free_irq(adapter->tx_scrq[i]->irq,
2057 adapter->tx_scrq[i]);
Thomas Falcon88eb98a2016-07-06 15:35:16 -05002058 irq_dispose_mapping(adapter->tx_scrq[i]->irq);
Nathan Fontenotb5108882017-03-30 02:49:18 -04002059 adapter->tx_scrq[i]->irq = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002060 }
Nathan Fontenotb5108882017-03-30 02:49:18 -04002061
2062 release_sub_crq_queue(adapter, adapter->tx_scrq[i]);
2063 }
2064
Nathan Fontenot9501df32017-03-15 23:38:07 -04002065 kfree(adapter->tx_scrq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002066 adapter->tx_scrq = NULL;
2067 }
2068
2069 if (adapter->rx_scrq) {
Nathan Fontenotb5108882017-03-30 02:49:18 -04002070 for (i = 0; i < adapter->req_rx_queues; i++) {
2071 if (!adapter->rx_scrq[i])
2072 continue;
2073
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002074 netdev_dbg(adapter->netdev, "Releasing rx_scrq[%d]\n",
2075 i);
Nathan Fontenotb5108882017-03-30 02:49:18 -04002076 if (adapter->rx_scrq[i]->irq) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06002077 free_irq(adapter->rx_scrq[i]->irq,
2078 adapter->rx_scrq[i]);
Thomas Falcon88eb98a2016-07-06 15:35:16 -05002079 irq_dispose_mapping(adapter->rx_scrq[i]->irq);
Nathan Fontenotb5108882017-03-30 02:49:18 -04002080 adapter->rx_scrq[i]->irq = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002081 }
Nathan Fontenotb5108882017-03-30 02:49:18 -04002082
2083 release_sub_crq_queue(adapter, adapter->rx_scrq[i]);
2084 }
2085
Nathan Fontenot9501df32017-03-15 23:38:07 -04002086 kfree(adapter->rx_scrq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002087 adapter->rx_scrq = NULL;
2088 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002089}
2090
2091static int disable_scrq_irq(struct ibmvnic_adapter *adapter,
2092 struct ibmvnic_sub_crq_queue *scrq)
2093{
2094 struct device *dev = &adapter->vdev->dev;
2095 unsigned long rc;
2096
2097 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
2098 H_DISABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
2099 if (rc)
2100 dev_err(dev, "Couldn't disable scrq irq 0x%lx. rc=%ld\n",
2101 scrq->hw_irq, rc);
2102 return rc;
2103}
2104
2105static int enable_scrq_irq(struct ibmvnic_adapter *adapter,
2106 struct ibmvnic_sub_crq_queue *scrq)
2107{
2108 struct device *dev = &adapter->vdev->dev;
2109 unsigned long rc;
2110
2111 if (scrq->hw_irq > 0x100000000ULL) {
2112 dev_err(dev, "bad hw_irq = %lx\n", scrq->hw_irq);
2113 return 1;
2114 }
2115
2116 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
2117 H_ENABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
2118 if (rc)
2119 dev_err(dev, "Couldn't enable scrq irq 0x%lx. rc=%ld\n",
2120 scrq->hw_irq, rc);
2121 return rc;
2122}
2123
2124static int ibmvnic_complete_tx(struct ibmvnic_adapter *adapter,
2125 struct ibmvnic_sub_crq_queue *scrq)
2126{
2127 struct device *dev = &adapter->vdev->dev;
2128 struct ibmvnic_tx_buff *txbuff;
2129 union sub_crq *next;
2130 int index;
2131 int i, j;
Thomas Falconad7775d2016-04-01 17:20:34 -05002132 u8 first;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002133
2134restart_loop:
2135 while (pending_scrq(adapter, scrq)) {
2136 unsigned int pool = scrq->pool_index;
2137
2138 next = ibmvnic_next_scrq(adapter, scrq);
2139 for (i = 0; i < next->tx_comp.num_comps; i++) {
2140 if (next->tx_comp.rcs[i]) {
2141 dev_err(dev, "tx error %x\n",
2142 next->tx_comp.rcs[i]);
2143 continue;
2144 }
2145 index = be32_to_cpu(next->tx_comp.correlators[i]);
2146 txbuff = &adapter->tx_pool[pool].tx_buff[index];
2147
2148 for (j = 0; j < IBMVNIC_MAX_FRAGS_PER_CRQ; j++) {
2149 if (!txbuff->data_dma[j])
2150 continue;
2151
2152 txbuff->data_dma[j] = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002153 }
Thomas Falconad7775d2016-04-01 17:20:34 -05002154 /* if sub_crq was sent indirectly */
2155 first = txbuff->indir_arr[0].generic.first;
2156 if (first == IBMVNIC_CRQ_CMD) {
2157 dma_unmap_single(dev, txbuff->indir_dma,
2158 sizeof(txbuff->indir_arr),
2159 DMA_TO_DEVICE);
2160 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002161
Thomas Falcon142c0ac2017-03-05 12:18:41 -06002162 if (txbuff->last_frag) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06002163 dev_kfree_skb_any(txbuff->skb);
Nathan Fontenot7c3e7de2017-05-03 14:05:25 -04002164 txbuff->skb = NULL;
Thomas Falcon142c0ac2017-03-05 12:18:41 -06002165 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002166
2167 adapter->tx_pool[pool].free_map[adapter->tx_pool[pool].
2168 producer_index] = index;
2169 adapter->tx_pool[pool].producer_index =
2170 (adapter->tx_pool[pool].producer_index + 1) %
Thomas Falcon068d9f92017-03-05 12:18:42 -06002171 adapter->req_tx_entries_per_subcrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002172 }
2173 /* remove tx_comp scrq*/
2174 next->tx_comp.first = 0;
Nathan Fontenot7c3e7de2017-05-03 14:05:25 -04002175
2176 if (atomic_sub_return(next->tx_comp.num_comps, &scrq->used) <=
2177 (adapter->req_tx_entries_per_subcrq / 2) &&
2178 __netif_subqueue_stopped(adapter->netdev,
2179 scrq->pool_index)) {
2180 netif_wake_subqueue(adapter->netdev, scrq->pool_index);
2181 netdev_info(adapter->netdev, "Started queue %d\n",
2182 scrq->pool_index);
2183 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002184 }
2185
2186 enable_scrq_irq(adapter, scrq);
2187
2188 if (pending_scrq(adapter, scrq)) {
2189 disable_scrq_irq(adapter, scrq);
2190 goto restart_loop;
2191 }
2192
2193 return 0;
2194}
2195
2196static irqreturn_t ibmvnic_interrupt_tx(int irq, void *instance)
2197{
2198 struct ibmvnic_sub_crq_queue *scrq = instance;
2199 struct ibmvnic_adapter *adapter = scrq->adapter;
2200
2201 disable_scrq_irq(adapter, scrq);
2202 ibmvnic_complete_tx(adapter, scrq);
2203
2204 return IRQ_HANDLED;
2205}
2206
2207static irqreturn_t ibmvnic_interrupt_rx(int irq, void *instance)
2208{
2209 struct ibmvnic_sub_crq_queue *scrq = instance;
2210 struct ibmvnic_adapter *adapter = scrq->adapter;
2211
John Allen3d52b592017-08-02 16:44:14 -05002212 adapter->rx_stats_buffers[scrq->scrq_num].interrupts++;
2213
Thomas Falcon032c5e82015-12-21 11:26:06 -06002214 if (napi_schedule_prep(&adapter->napi[scrq->scrq_num])) {
2215 disable_scrq_irq(adapter, scrq);
2216 __napi_schedule(&adapter->napi[scrq->scrq_num]);
2217 }
2218
2219 return IRQ_HANDLED;
2220}
2221
Thomas Falconea22d512016-07-06 15:35:17 -05002222static int init_sub_crq_irqs(struct ibmvnic_adapter *adapter)
2223{
2224 struct device *dev = &adapter->vdev->dev;
2225 struct ibmvnic_sub_crq_queue *scrq;
2226 int i = 0, j = 0;
2227 int rc = 0;
2228
2229 for (i = 0; i < adapter->req_tx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002230 netdev_dbg(adapter->netdev, "Initializing tx_scrq[%d] irq\n",
2231 i);
Thomas Falconea22d512016-07-06 15:35:17 -05002232 scrq = adapter->tx_scrq[i];
2233 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
2234
Michael Ellerman99c17902016-09-10 19:59:05 +10002235 if (!scrq->irq) {
Thomas Falconea22d512016-07-06 15:35:17 -05002236 rc = -EINVAL;
2237 dev_err(dev, "Error mapping irq\n");
2238 goto req_tx_irq_failed;
2239 }
2240
2241 rc = request_irq(scrq->irq, ibmvnic_interrupt_tx,
2242 0, "ibmvnic_tx", scrq);
2243
2244 if (rc) {
2245 dev_err(dev, "Couldn't register tx irq 0x%x. rc=%d\n",
2246 scrq->irq, rc);
2247 irq_dispose_mapping(scrq->irq);
2248 goto req_rx_irq_failed;
2249 }
2250 }
2251
2252 for (i = 0; i < adapter->req_rx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002253 netdev_dbg(adapter->netdev, "Initializing rx_scrq[%d] irq\n",
2254 i);
Thomas Falconea22d512016-07-06 15:35:17 -05002255 scrq = adapter->rx_scrq[i];
2256 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
Michael Ellerman99c17902016-09-10 19:59:05 +10002257 if (!scrq->irq) {
Thomas Falconea22d512016-07-06 15:35:17 -05002258 rc = -EINVAL;
2259 dev_err(dev, "Error mapping irq\n");
2260 goto req_rx_irq_failed;
2261 }
2262 rc = request_irq(scrq->irq, ibmvnic_interrupt_rx,
2263 0, "ibmvnic_rx", scrq);
2264 if (rc) {
2265 dev_err(dev, "Couldn't register rx irq 0x%x. rc=%d\n",
2266 scrq->irq, rc);
2267 irq_dispose_mapping(scrq->irq);
2268 goto req_rx_irq_failed;
2269 }
2270 }
2271 return rc;
2272
2273req_rx_irq_failed:
Thomas Falcon8bf371e2016-10-27 12:28:52 -05002274 for (j = 0; j < i; j++) {
Thomas Falconea22d512016-07-06 15:35:17 -05002275 free_irq(adapter->rx_scrq[j]->irq, adapter->rx_scrq[j]);
2276 irq_dispose_mapping(adapter->rx_scrq[j]->irq);
Thomas Falcon8bf371e2016-10-27 12:28:52 -05002277 }
Thomas Falconea22d512016-07-06 15:35:17 -05002278 i = adapter->req_tx_queues;
2279req_tx_irq_failed:
Thomas Falcon8bf371e2016-10-27 12:28:52 -05002280 for (j = 0; j < i; j++) {
Thomas Falconea22d512016-07-06 15:35:17 -05002281 free_irq(adapter->tx_scrq[j]->irq, adapter->tx_scrq[j]);
2282 irq_dispose_mapping(adapter->rx_scrq[j]->irq);
Thomas Falcon8bf371e2016-10-27 12:28:52 -05002283 }
Nathan Fontenotb5108882017-03-30 02:49:18 -04002284 release_sub_crqs(adapter);
Thomas Falconea22d512016-07-06 15:35:17 -05002285 return rc;
2286}
2287
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002288static int init_sub_crqs(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002289{
2290 struct device *dev = &adapter->vdev->dev;
2291 struct ibmvnic_sub_crq_queue **allqueues;
2292 int registered_queues = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002293 int total_queues;
2294 int more = 0;
Thomas Falconea22d512016-07-06 15:35:17 -05002295 int i;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002296
Thomas Falcon032c5e82015-12-21 11:26:06 -06002297 total_queues = adapter->req_tx_queues + adapter->req_rx_queues;
2298
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04002299 allqueues = kcalloc(total_queues, sizeof(*allqueues), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002300 if (!allqueues)
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002301 return -1;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002302
2303 for (i = 0; i < total_queues; i++) {
2304 allqueues[i] = init_sub_crq_queue(adapter);
2305 if (!allqueues[i]) {
2306 dev_warn(dev, "Couldn't allocate all sub-crqs\n");
2307 break;
2308 }
2309 registered_queues++;
2310 }
2311
2312 /* Make sure we were able to register the minimum number of queues */
2313 if (registered_queues <
2314 adapter->min_tx_queues + adapter->min_rx_queues) {
2315 dev_err(dev, "Fatal: Couldn't init min number of sub-crqs\n");
2316 goto tx_failed;
2317 }
2318
2319 /* Distribute the failed allocated queues*/
2320 for (i = 0; i < total_queues - registered_queues + more ; i++) {
2321 netdev_dbg(adapter->netdev, "Reducing number of queues\n");
2322 switch (i % 3) {
2323 case 0:
2324 if (adapter->req_rx_queues > adapter->min_rx_queues)
2325 adapter->req_rx_queues--;
2326 else
2327 more++;
2328 break;
2329 case 1:
2330 if (adapter->req_tx_queues > adapter->min_tx_queues)
2331 adapter->req_tx_queues--;
2332 else
2333 more++;
2334 break;
2335 }
2336 }
2337
2338 adapter->tx_scrq = kcalloc(adapter->req_tx_queues,
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04002339 sizeof(*adapter->tx_scrq), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002340 if (!adapter->tx_scrq)
2341 goto tx_failed;
2342
2343 for (i = 0; i < adapter->req_tx_queues; i++) {
2344 adapter->tx_scrq[i] = allqueues[i];
2345 adapter->tx_scrq[i]->pool_index = i;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002346 }
2347
2348 adapter->rx_scrq = kcalloc(adapter->req_rx_queues,
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04002349 sizeof(*adapter->rx_scrq), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002350 if (!adapter->rx_scrq)
2351 goto rx_failed;
2352
2353 for (i = 0; i < adapter->req_rx_queues; i++) {
2354 adapter->rx_scrq[i] = allqueues[i + adapter->req_tx_queues];
2355 adapter->rx_scrq[i]->scrq_num = i;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002356 }
2357
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002358 kfree(allqueues);
2359 return 0;
2360
2361rx_failed:
2362 kfree(adapter->tx_scrq);
2363 adapter->tx_scrq = NULL;
2364tx_failed:
2365 for (i = 0; i < registered_queues; i++)
2366 release_sub_crq_queue(adapter, allqueues[i]);
2367 kfree(allqueues);
2368 return -1;
2369}
2370
2371static void ibmvnic_send_req_caps(struct ibmvnic_adapter *adapter, int retry)
2372{
2373 struct device *dev = &adapter->vdev->dev;
2374 union ibmvnic_crq crq;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002375
2376 if (!retry) {
2377 /* Sub-CRQ entries are 32 byte long */
2378 int entries_page = 4 * PAGE_SIZE / (sizeof(u64) * 4);
2379
2380 if (adapter->min_tx_entries_per_subcrq > entries_page ||
2381 adapter->min_rx_add_entries_per_subcrq > entries_page) {
2382 dev_err(dev, "Fatal, invalid entries per sub-crq\n");
2383 return;
2384 }
2385
2386 /* Get the minimum between the queried max and the entries
2387 * that fit in our PAGE_SIZE
2388 */
2389 adapter->req_tx_entries_per_subcrq =
2390 adapter->max_tx_entries_per_subcrq > entries_page ?
2391 entries_page : adapter->max_tx_entries_per_subcrq;
2392 adapter->req_rx_add_entries_per_subcrq =
2393 adapter->max_rx_add_entries_per_subcrq > entries_page ?
2394 entries_page : adapter->max_rx_add_entries_per_subcrq;
2395
2396 adapter->req_tx_queues = adapter->opt_tx_comp_sub_queues;
2397 adapter->req_rx_queues = adapter->opt_rx_comp_queues;
2398 adapter->req_rx_add_queues = adapter->max_rx_add_queues;
2399
2400 adapter->req_mtu = adapter->netdev->mtu + ETH_HLEN;
2401 }
2402
Thomas Falcon032c5e82015-12-21 11:26:06 -06002403 memset(&crq, 0, sizeof(crq));
2404 crq.request_capability.first = IBMVNIC_CRQ_CMD;
2405 crq.request_capability.cmd = REQUEST_CAPABILITY;
2406
2407 crq.request_capability.capability = cpu_to_be16(REQ_TX_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06002408 crq.request_capability.number = cpu_to_be64(adapter->req_tx_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06002409 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002410 ibmvnic_send_crq(adapter, &crq);
2411
2412 crq.request_capability.capability = cpu_to_be16(REQ_RX_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06002413 crq.request_capability.number = cpu_to_be64(adapter->req_rx_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06002414 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002415 ibmvnic_send_crq(adapter, &crq);
2416
2417 crq.request_capability.capability = cpu_to_be16(REQ_RX_ADD_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06002418 crq.request_capability.number = cpu_to_be64(adapter->req_rx_add_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06002419 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002420 ibmvnic_send_crq(adapter, &crq);
2421
2422 crq.request_capability.capability =
2423 cpu_to_be16(REQ_TX_ENTRIES_PER_SUBCRQ);
2424 crq.request_capability.number =
Thomas Falconde89e852016-03-01 10:20:09 -06002425 cpu_to_be64(adapter->req_tx_entries_per_subcrq);
Thomas Falcon901e0402017-02-15 12:17:59 -06002426 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002427 ibmvnic_send_crq(adapter, &crq);
2428
2429 crq.request_capability.capability =
2430 cpu_to_be16(REQ_RX_ADD_ENTRIES_PER_SUBCRQ);
2431 crq.request_capability.number =
Thomas Falconde89e852016-03-01 10:20:09 -06002432 cpu_to_be64(adapter->req_rx_add_entries_per_subcrq);
Thomas Falcon901e0402017-02-15 12:17:59 -06002433 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002434 ibmvnic_send_crq(adapter, &crq);
2435
2436 crq.request_capability.capability = cpu_to_be16(REQ_MTU);
Thomas Falconde89e852016-03-01 10:20:09 -06002437 crq.request_capability.number = cpu_to_be64(adapter->req_mtu);
Thomas Falcon901e0402017-02-15 12:17:59 -06002438 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002439 ibmvnic_send_crq(adapter, &crq);
2440
2441 if (adapter->netdev->flags & IFF_PROMISC) {
2442 if (adapter->promisc_supported) {
2443 crq.request_capability.capability =
2444 cpu_to_be16(PROMISC_REQUESTED);
Thomas Falconde89e852016-03-01 10:20:09 -06002445 crq.request_capability.number = cpu_to_be64(1);
Thomas Falcon901e0402017-02-15 12:17:59 -06002446 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002447 ibmvnic_send_crq(adapter, &crq);
2448 }
2449 } else {
2450 crq.request_capability.capability =
2451 cpu_to_be16(PROMISC_REQUESTED);
Thomas Falconde89e852016-03-01 10:20:09 -06002452 crq.request_capability.number = cpu_to_be64(0);
Thomas Falcon901e0402017-02-15 12:17:59 -06002453 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002454 ibmvnic_send_crq(adapter, &crq);
2455 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002456}
2457
2458static int pending_scrq(struct ibmvnic_adapter *adapter,
2459 struct ibmvnic_sub_crq_queue *scrq)
2460{
2461 union sub_crq *entry = &scrq->msgs[scrq->cur];
2462
Thomas Falcon1cf9cc72017-06-14 23:50:08 -05002463 if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002464 return 1;
2465 else
2466 return 0;
2467}
2468
2469static union sub_crq *ibmvnic_next_scrq(struct ibmvnic_adapter *adapter,
2470 struct ibmvnic_sub_crq_queue *scrq)
2471{
2472 union sub_crq *entry;
2473 unsigned long flags;
2474
2475 spin_lock_irqsave(&scrq->lock, flags);
2476 entry = &scrq->msgs[scrq->cur];
2477 if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP) {
2478 if (++scrq->cur == scrq->size)
2479 scrq->cur = 0;
2480 } else {
2481 entry = NULL;
2482 }
2483 spin_unlock_irqrestore(&scrq->lock, flags);
2484
2485 return entry;
2486}
2487
2488static union ibmvnic_crq *ibmvnic_next_crq(struct ibmvnic_adapter *adapter)
2489{
2490 struct ibmvnic_crq_queue *queue = &adapter->crq;
2491 union ibmvnic_crq *crq;
2492
2493 crq = &queue->msgs[queue->cur];
2494 if (crq->generic.first & IBMVNIC_CRQ_CMD_RSP) {
2495 if (++queue->cur == queue->size)
2496 queue->cur = 0;
2497 } else {
2498 crq = NULL;
2499 }
2500
2501 return crq;
2502}
2503
2504static int send_subcrq(struct ibmvnic_adapter *adapter, u64 remote_handle,
2505 union sub_crq *sub_crq)
2506{
2507 unsigned int ua = adapter->vdev->unit_address;
2508 struct device *dev = &adapter->vdev->dev;
2509 u64 *u64_crq = (u64 *)sub_crq;
2510 int rc;
2511
2512 netdev_dbg(adapter->netdev,
2513 "Sending sCRQ %016lx: %016lx %016lx %016lx %016lx\n",
2514 (unsigned long int)cpu_to_be64(remote_handle),
2515 (unsigned long int)cpu_to_be64(u64_crq[0]),
2516 (unsigned long int)cpu_to_be64(u64_crq[1]),
2517 (unsigned long int)cpu_to_be64(u64_crq[2]),
2518 (unsigned long int)cpu_to_be64(u64_crq[3]));
2519
2520 /* Make sure the hypervisor sees the complete request */
2521 mb();
2522
2523 rc = plpar_hcall_norets(H_SEND_SUB_CRQ, ua,
2524 cpu_to_be64(remote_handle),
2525 cpu_to_be64(u64_crq[0]),
2526 cpu_to_be64(u64_crq[1]),
2527 cpu_to_be64(u64_crq[2]),
2528 cpu_to_be64(u64_crq[3]));
2529
2530 if (rc) {
2531 if (rc == H_CLOSED)
2532 dev_warn(dev, "CRQ Queue closed\n");
2533 dev_err(dev, "Send error (rc=%d)\n", rc);
2534 }
2535
2536 return rc;
2537}
2538
Thomas Falconad7775d2016-04-01 17:20:34 -05002539static int send_subcrq_indirect(struct ibmvnic_adapter *adapter,
2540 u64 remote_handle, u64 ioba, u64 num_entries)
2541{
2542 unsigned int ua = adapter->vdev->unit_address;
2543 struct device *dev = &adapter->vdev->dev;
2544 int rc;
2545
2546 /* Make sure the hypervisor sees the complete request */
2547 mb();
2548 rc = plpar_hcall_norets(H_SEND_SUB_CRQ_INDIRECT, ua,
2549 cpu_to_be64(remote_handle),
2550 ioba, num_entries);
2551
2552 if (rc) {
2553 if (rc == H_CLOSED)
2554 dev_warn(dev, "CRQ Queue closed\n");
2555 dev_err(dev, "Send (indirect) error (rc=%d)\n", rc);
2556 }
2557
2558 return rc;
2559}
2560
Thomas Falcon032c5e82015-12-21 11:26:06 -06002561static int ibmvnic_send_crq(struct ibmvnic_adapter *adapter,
2562 union ibmvnic_crq *crq)
2563{
2564 unsigned int ua = adapter->vdev->unit_address;
2565 struct device *dev = &adapter->vdev->dev;
2566 u64 *u64_crq = (u64 *)crq;
2567 int rc;
2568
2569 netdev_dbg(adapter->netdev, "Sending CRQ: %016lx %016lx\n",
2570 (unsigned long int)cpu_to_be64(u64_crq[0]),
2571 (unsigned long int)cpu_to_be64(u64_crq[1]));
2572
2573 /* Make sure the hypervisor sees the complete request */
2574 mb();
2575
2576 rc = plpar_hcall_norets(H_SEND_CRQ, ua,
2577 cpu_to_be64(u64_crq[0]),
2578 cpu_to_be64(u64_crq[1]));
2579
2580 if (rc) {
2581 if (rc == H_CLOSED)
2582 dev_warn(dev, "CRQ Queue closed\n");
2583 dev_warn(dev, "Send error (rc=%d)\n", rc);
2584 }
2585
2586 return rc;
2587}
2588
2589static int ibmvnic_send_crq_init(struct ibmvnic_adapter *adapter)
2590{
2591 union ibmvnic_crq crq;
2592
2593 memset(&crq, 0, sizeof(crq));
2594 crq.generic.first = IBMVNIC_CRQ_INIT_CMD;
2595 crq.generic.cmd = IBMVNIC_CRQ_INIT;
2596 netdev_dbg(adapter->netdev, "Sending CRQ init\n");
2597
2598 return ibmvnic_send_crq(adapter, &crq);
2599}
2600
Thomas Falcon032c5e82015-12-21 11:26:06 -06002601static int send_version_xchg(struct ibmvnic_adapter *adapter)
2602{
2603 union ibmvnic_crq crq;
2604
2605 memset(&crq, 0, sizeof(crq));
2606 crq.version_exchange.first = IBMVNIC_CRQ_CMD;
2607 crq.version_exchange.cmd = VERSION_EXCHANGE;
2608 crq.version_exchange.version = cpu_to_be16(ibmvnic_version);
2609
2610 return ibmvnic_send_crq(adapter, &crq);
2611}
2612
2613static void send_login(struct ibmvnic_adapter *adapter)
2614{
2615 struct ibmvnic_login_rsp_buffer *login_rsp_buffer;
2616 struct ibmvnic_login_buffer *login_buffer;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002617 struct device *dev = &adapter->vdev->dev;
2618 dma_addr_t rsp_buffer_token;
2619 dma_addr_t buffer_token;
2620 size_t rsp_buffer_size;
2621 union ibmvnic_crq crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002622 size_t buffer_size;
2623 __be64 *tx_list_p;
2624 __be64 *rx_list_p;
2625 int i;
2626
2627 buffer_size =
2628 sizeof(struct ibmvnic_login_buffer) +
2629 sizeof(u64) * (adapter->req_tx_queues + adapter->req_rx_queues);
2630
2631 login_buffer = kmalloc(buffer_size, GFP_ATOMIC);
2632 if (!login_buffer)
2633 goto buf_alloc_failed;
2634
2635 buffer_token = dma_map_single(dev, login_buffer, buffer_size,
2636 DMA_TO_DEVICE);
2637 if (dma_mapping_error(dev, buffer_token)) {
2638 dev_err(dev, "Couldn't map login buffer\n");
2639 goto buf_map_failed;
2640 }
2641
John Allen498cd8e2016-04-06 11:49:55 -05002642 rsp_buffer_size = sizeof(struct ibmvnic_login_rsp_buffer) +
2643 sizeof(u64) * adapter->req_tx_queues +
2644 sizeof(u64) * adapter->req_rx_queues +
2645 sizeof(u64) * adapter->req_rx_queues +
2646 sizeof(u8) * IBMVNIC_TX_DESC_VERSIONS;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002647
2648 login_rsp_buffer = kmalloc(rsp_buffer_size, GFP_ATOMIC);
2649 if (!login_rsp_buffer)
2650 goto buf_rsp_alloc_failed;
2651
2652 rsp_buffer_token = dma_map_single(dev, login_rsp_buffer,
2653 rsp_buffer_size, DMA_FROM_DEVICE);
2654 if (dma_mapping_error(dev, rsp_buffer_token)) {
2655 dev_err(dev, "Couldn't map login rsp buffer\n");
2656 goto buf_rsp_map_failed;
2657 }
Nathan Fontenot661a2622017-04-19 13:44:58 -04002658
Thomas Falcon032c5e82015-12-21 11:26:06 -06002659 adapter->login_buf = login_buffer;
2660 adapter->login_buf_token = buffer_token;
2661 adapter->login_buf_sz = buffer_size;
2662 adapter->login_rsp_buf = login_rsp_buffer;
2663 adapter->login_rsp_buf_token = rsp_buffer_token;
2664 adapter->login_rsp_buf_sz = rsp_buffer_size;
2665
2666 login_buffer->len = cpu_to_be32(buffer_size);
2667 login_buffer->version = cpu_to_be32(INITIAL_VERSION_LB);
2668 login_buffer->num_txcomp_subcrqs = cpu_to_be32(adapter->req_tx_queues);
2669 login_buffer->off_txcomp_subcrqs =
2670 cpu_to_be32(sizeof(struct ibmvnic_login_buffer));
2671 login_buffer->num_rxcomp_subcrqs = cpu_to_be32(adapter->req_rx_queues);
2672 login_buffer->off_rxcomp_subcrqs =
2673 cpu_to_be32(sizeof(struct ibmvnic_login_buffer) +
2674 sizeof(u64) * adapter->req_tx_queues);
2675 login_buffer->login_rsp_ioba = cpu_to_be32(rsp_buffer_token);
2676 login_buffer->login_rsp_len = cpu_to_be32(rsp_buffer_size);
2677
2678 tx_list_p = (__be64 *)((char *)login_buffer +
2679 sizeof(struct ibmvnic_login_buffer));
2680 rx_list_p = (__be64 *)((char *)login_buffer +
2681 sizeof(struct ibmvnic_login_buffer) +
2682 sizeof(u64) * adapter->req_tx_queues);
2683
2684 for (i = 0; i < adapter->req_tx_queues; i++) {
2685 if (adapter->tx_scrq[i]) {
2686 tx_list_p[i] = cpu_to_be64(adapter->tx_scrq[i]->
2687 crq_num);
2688 }
2689 }
2690
2691 for (i = 0; i < adapter->req_rx_queues; i++) {
2692 if (adapter->rx_scrq[i]) {
2693 rx_list_p[i] = cpu_to_be64(adapter->rx_scrq[i]->
2694 crq_num);
2695 }
2696 }
2697
2698 netdev_dbg(adapter->netdev, "Login Buffer:\n");
2699 for (i = 0; i < (adapter->login_buf_sz - 1) / 8 + 1; i++) {
2700 netdev_dbg(adapter->netdev, "%016lx\n",
2701 ((unsigned long int *)(adapter->login_buf))[i]);
2702 }
2703
2704 memset(&crq, 0, sizeof(crq));
2705 crq.login.first = IBMVNIC_CRQ_CMD;
2706 crq.login.cmd = LOGIN;
2707 crq.login.ioba = cpu_to_be32(buffer_token);
2708 crq.login.len = cpu_to_be32(buffer_size);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002709 ibmvnic_send_crq(adapter, &crq);
2710
2711 return;
2712
Thomas Falcon032c5e82015-12-21 11:26:06 -06002713buf_rsp_map_failed:
2714 kfree(login_rsp_buffer);
2715buf_rsp_alloc_failed:
2716 dma_unmap_single(dev, buffer_token, buffer_size, DMA_TO_DEVICE);
2717buf_map_failed:
2718 kfree(login_buffer);
2719buf_alloc_failed:
2720 return;
2721}
2722
2723static void send_request_map(struct ibmvnic_adapter *adapter, dma_addr_t addr,
2724 u32 len, u8 map_id)
2725{
2726 union ibmvnic_crq crq;
2727
2728 memset(&crq, 0, sizeof(crq));
2729 crq.request_map.first = IBMVNIC_CRQ_CMD;
2730 crq.request_map.cmd = REQUEST_MAP;
2731 crq.request_map.map_id = map_id;
2732 crq.request_map.ioba = cpu_to_be32(addr);
2733 crq.request_map.len = cpu_to_be32(len);
2734 ibmvnic_send_crq(adapter, &crq);
2735}
2736
2737static void send_request_unmap(struct ibmvnic_adapter *adapter, u8 map_id)
2738{
2739 union ibmvnic_crq crq;
2740
2741 memset(&crq, 0, sizeof(crq));
2742 crq.request_unmap.first = IBMVNIC_CRQ_CMD;
2743 crq.request_unmap.cmd = REQUEST_UNMAP;
2744 crq.request_unmap.map_id = map_id;
2745 ibmvnic_send_crq(adapter, &crq);
2746}
2747
2748static void send_map_query(struct ibmvnic_adapter *adapter)
2749{
2750 union ibmvnic_crq crq;
2751
2752 memset(&crq, 0, sizeof(crq));
2753 crq.query_map.first = IBMVNIC_CRQ_CMD;
2754 crq.query_map.cmd = QUERY_MAP;
2755 ibmvnic_send_crq(adapter, &crq);
2756}
2757
2758/* Send a series of CRQs requesting various capabilities of the VNIC server */
2759static void send_cap_queries(struct ibmvnic_adapter *adapter)
2760{
2761 union ibmvnic_crq crq;
2762
Thomas Falcon901e0402017-02-15 12:17:59 -06002763 atomic_set(&adapter->running_cap_crqs, 0);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002764 memset(&crq, 0, sizeof(crq));
2765 crq.query_capability.first = IBMVNIC_CRQ_CMD;
2766 crq.query_capability.cmd = QUERY_CAPABILITY;
2767
2768 crq.query_capability.capability = cpu_to_be16(MIN_TX_QUEUES);
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
2772 crq.query_capability.capability = cpu_to_be16(MIN_RX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002773 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002774 ibmvnic_send_crq(adapter, &crq);
2775
2776 crq.query_capability.capability = cpu_to_be16(MIN_RX_ADD_QUEUES);
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(MAX_TX_QUEUES);
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(MAX_RX_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(MAX_RX_ADD_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(MIN_TX_ENTRIES_PER_SUBCRQ);
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(MIN_RX_ADD_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(MAX_TX_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 =
2808 cpu_to_be16(MAX_RX_ADD_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06002809 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002810 ibmvnic_send_crq(adapter, &crq);
2811
2812 crq.query_capability.capability = cpu_to_be16(TCP_IP_OFFLOAD);
Thomas Falcon901e0402017-02-15 12:17:59 -06002813 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002814 ibmvnic_send_crq(adapter, &crq);
2815
2816 crq.query_capability.capability = cpu_to_be16(PROMISC_SUPPORTED);
Thomas Falcon901e0402017-02-15 12:17:59 -06002817 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002818 ibmvnic_send_crq(adapter, &crq);
2819
2820 crq.query_capability.capability = cpu_to_be16(MIN_MTU);
Thomas Falcon901e0402017-02-15 12:17:59 -06002821 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002822 ibmvnic_send_crq(adapter, &crq);
2823
2824 crq.query_capability.capability = cpu_to_be16(MAX_MTU);
Thomas Falcon901e0402017-02-15 12:17:59 -06002825 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002826 ibmvnic_send_crq(adapter, &crq);
2827
2828 crq.query_capability.capability = cpu_to_be16(MAX_MULTICAST_FILTERS);
Thomas Falcon901e0402017-02-15 12:17:59 -06002829 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002830 ibmvnic_send_crq(adapter, &crq);
2831
2832 crq.query_capability.capability = cpu_to_be16(VLAN_HEADER_INSERTION);
Thomas Falcon901e0402017-02-15 12:17:59 -06002833 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002834 ibmvnic_send_crq(adapter, &crq);
2835
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04002836 crq.query_capability.capability = cpu_to_be16(RX_VLAN_HEADER_INSERTION);
2837 atomic_inc(&adapter->running_cap_crqs);
2838 ibmvnic_send_crq(adapter, &crq);
2839
Thomas Falcon032c5e82015-12-21 11:26:06 -06002840 crq.query_capability.capability = cpu_to_be16(MAX_TX_SG_ENTRIES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002841 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002842 ibmvnic_send_crq(adapter, &crq);
2843
2844 crq.query_capability.capability = cpu_to_be16(RX_SG_SUPPORTED);
Thomas Falcon901e0402017-02-15 12:17:59 -06002845 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002846 ibmvnic_send_crq(adapter, &crq);
2847
2848 crq.query_capability.capability = cpu_to_be16(OPT_TX_COMP_SUB_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002849 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002850 ibmvnic_send_crq(adapter, &crq);
2851
2852 crq.query_capability.capability = cpu_to_be16(OPT_RX_COMP_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002853 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002854 ibmvnic_send_crq(adapter, &crq);
2855
2856 crq.query_capability.capability =
2857 cpu_to_be16(OPT_RX_BUFADD_Q_PER_RX_COMP_Q);
Thomas Falcon901e0402017-02-15 12:17:59 -06002858 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002859 ibmvnic_send_crq(adapter, &crq);
2860
2861 crq.query_capability.capability =
2862 cpu_to_be16(OPT_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06002863 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002864 ibmvnic_send_crq(adapter, &crq);
2865
2866 crq.query_capability.capability =
2867 cpu_to_be16(OPT_RXBA_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06002868 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002869 ibmvnic_send_crq(adapter, &crq);
2870
2871 crq.query_capability.capability = cpu_to_be16(TX_RX_DESC_REQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06002872 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002873 ibmvnic_send_crq(adapter, &crq);
2874}
2875
2876static void handle_query_ip_offload_rsp(struct ibmvnic_adapter *adapter)
2877{
2878 struct device *dev = &adapter->vdev->dev;
2879 struct ibmvnic_query_ip_offload_buffer *buf = &adapter->ip_offload_buf;
2880 union ibmvnic_crq crq;
2881 int i;
2882
2883 dma_unmap_single(dev, adapter->ip_offload_tok,
2884 sizeof(adapter->ip_offload_buf), DMA_FROM_DEVICE);
2885
2886 netdev_dbg(adapter->netdev, "Query IP Offload Buffer:\n");
2887 for (i = 0; i < (sizeof(adapter->ip_offload_buf) - 1) / 8 + 1; i++)
2888 netdev_dbg(adapter->netdev, "%016lx\n",
2889 ((unsigned long int *)(buf))[i]);
2890
2891 netdev_dbg(adapter->netdev, "ipv4_chksum = %d\n", buf->ipv4_chksum);
2892 netdev_dbg(adapter->netdev, "ipv6_chksum = %d\n", buf->ipv6_chksum);
2893 netdev_dbg(adapter->netdev, "tcp_ipv4_chksum = %d\n",
2894 buf->tcp_ipv4_chksum);
2895 netdev_dbg(adapter->netdev, "tcp_ipv6_chksum = %d\n",
2896 buf->tcp_ipv6_chksum);
2897 netdev_dbg(adapter->netdev, "udp_ipv4_chksum = %d\n",
2898 buf->udp_ipv4_chksum);
2899 netdev_dbg(adapter->netdev, "udp_ipv6_chksum = %d\n",
2900 buf->udp_ipv6_chksum);
2901 netdev_dbg(adapter->netdev, "large_tx_ipv4 = %d\n",
2902 buf->large_tx_ipv4);
2903 netdev_dbg(adapter->netdev, "large_tx_ipv6 = %d\n",
2904 buf->large_tx_ipv6);
2905 netdev_dbg(adapter->netdev, "large_rx_ipv4 = %d\n",
2906 buf->large_rx_ipv4);
2907 netdev_dbg(adapter->netdev, "large_rx_ipv6 = %d\n",
2908 buf->large_rx_ipv6);
2909 netdev_dbg(adapter->netdev, "max_ipv4_hdr_sz = %d\n",
2910 buf->max_ipv4_header_size);
2911 netdev_dbg(adapter->netdev, "max_ipv6_hdr_sz = %d\n",
2912 buf->max_ipv6_header_size);
2913 netdev_dbg(adapter->netdev, "max_tcp_hdr_size = %d\n",
2914 buf->max_tcp_header_size);
2915 netdev_dbg(adapter->netdev, "max_udp_hdr_size = %d\n",
2916 buf->max_udp_header_size);
2917 netdev_dbg(adapter->netdev, "max_large_tx_size = %d\n",
2918 buf->max_large_tx_size);
2919 netdev_dbg(adapter->netdev, "max_large_rx_size = %d\n",
2920 buf->max_large_rx_size);
2921 netdev_dbg(adapter->netdev, "ipv6_ext_hdr = %d\n",
2922 buf->ipv6_extension_header);
2923 netdev_dbg(adapter->netdev, "tcp_pseudosum_req = %d\n",
2924 buf->tcp_pseudosum_req);
2925 netdev_dbg(adapter->netdev, "num_ipv6_ext_hd = %d\n",
2926 buf->num_ipv6_ext_headers);
2927 netdev_dbg(adapter->netdev, "off_ipv6_ext_hd = %d\n",
2928 buf->off_ipv6_ext_headers);
2929
2930 adapter->ip_offload_ctrl_tok =
2931 dma_map_single(dev, &adapter->ip_offload_ctrl,
2932 sizeof(adapter->ip_offload_ctrl), DMA_TO_DEVICE);
2933
2934 if (dma_mapping_error(dev, adapter->ip_offload_ctrl_tok)) {
2935 dev_err(dev, "Couldn't map ip offload control buffer\n");
2936 return;
2937 }
2938
2939 adapter->ip_offload_ctrl.version = cpu_to_be32(INITIAL_VERSION_IOB);
2940 adapter->ip_offload_ctrl.tcp_ipv4_chksum = buf->tcp_ipv4_chksum;
2941 adapter->ip_offload_ctrl.udp_ipv4_chksum = buf->udp_ipv4_chksum;
2942 adapter->ip_offload_ctrl.tcp_ipv6_chksum = buf->tcp_ipv6_chksum;
2943 adapter->ip_offload_ctrl.udp_ipv6_chksum = buf->udp_ipv6_chksum;
2944
2945 /* large_tx/rx disabled for now, additional features needed */
2946 adapter->ip_offload_ctrl.large_tx_ipv4 = 0;
2947 adapter->ip_offload_ctrl.large_tx_ipv6 = 0;
2948 adapter->ip_offload_ctrl.large_rx_ipv4 = 0;
2949 adapter->ip_offload_ctrl.large_rx_ipv6 = 0;
2950
2951 adapter->netdev->features = NETIF_F_GSO;
2952
2953 if (buf->tcp_ipv4_chksum || buf->udp_ipv4_chksum)
2954 adapter->netdev->features |= NETIF_F_IP_CSUM;
2955
2956 if (buf->tcp_ipv6_chksum || buf->udp_ipv6_chksum)
2957 adapter->netdev->features |= NETIF_F_IPV6_CSUM;
2958
Thomas Falcon9be02cd2016-04-01 17:20:35 -05002959 if ((adapter->netdev->features &
2960 (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)))
2961 adapter->netdev->features |= NETIF_F_RXCSUM;
2962
Thomas Falcon032c5e82015-12-21 11:26:06 -06002963 memset(&crq, 0, sizeof(crq));
2964 crq.control_ip_offload.first = IBMVNIC_CRQ_CMD;
2965 crq.control_ip_offload.cmd = CONTROL_IP_OFFLOAD;
2966 crq.control_ip_offload.len =
2967 cpu_to_be32(sizeof(adapter->ip_offload_ctrl));
2968 crq.control_ip_offload.ioba = cpu_to_be32(adapter->ip_offload_ctrl_tok);
2969 ibmvnic_send_crq(adapter, &crq);
2970}
2971
2972static void handle_error_info_rsp(union ibmvnic_crq *crq,
2973 struct ibmvnic_adapter *adapter)
2974{
2975 struct device *dev = &adapter->vdev->dev;
Wei Yongjun96183182016-06-27 20:48:53 +08002976 struct ibmvnic_error_buff *error_buff, *tmp;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002977 unsigned long flags;
2978 bool found = false;
2979 int i;
2980
2981 if (!crq->request_error_rsp.rc.code) {
2982 dev_info(dev, "Request Error Rsp returned with rc=%x\n",
2983 crq->request_error_rsp.rc.code);
2984 return;
2985 }
2986
2987 spin_lock_irqsave(&adapter->error_list_lock, flags);
Wei Yongjun96183182016-06-27 20:48:53 +08002988 list_for_each_entry_safe(error_buff, tmp, &adapter->errors, list)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002989 if (error_buff->error_id == crq->request_error_rsp.error_id) {
2990 found = true;
2991 list_del(&error_buff->list);
2992 break;
2993 }
2994 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
2995
2996 if (!found) {
2997 dev_err(dev, "Couldn't find error id %x\n",
Thomas Falcon75224c92017-02-15 10:33:33 -06002998 be32_to_cpu(crq->request_error_rsp.error_id));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002999 return;
3000 }
3001
3002 dev_err(dev, "Detailed info for error id %x:",
Thomas Falcon75224c92017-02-15 10:33:33 -06003003 be32_to_cpu(crq->request_error_rsp.error_id));
Thomas Falcon032c5e82015-12-21 11:26:06 -06003004
3005 for (i = 0; i < error_buff->len; i++) {
3006 pr_cont("%02x", (int)error_buff->buff[i]);
3007 if (i % 8 == 7)
3008 pr_cont(" ");
3009 }
3010 pr_cont("\n");
3011
3012 dma_unmap_single(dev, error_buff->dma, error_buff->len,
3013 DMA_FROM_DEVICE);
3014 kfree(error_buff->buff);
3015 kfree(error_buff);
3016}
3017
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04003018static void request_error_information(struct ibmvnic_adapter *adapter,
3019 union ibmvnic_crq *err_crq)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003020{
Thomas Falcon032c5e82015-12-21 11:26:06 -06003021 struct device *dev = &adapter->vdev->dev;
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04003022 struct net_device *netdev = adapter->netdev;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003023 struct ibmvnic_error_buff *error_buff;
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04003024 unsigned long timeout = msecs_to_jiffies(30000);
3025 union ibmvnic_crq crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003026 unsigned long flags;
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04003027 int rc, detail_len;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003028
3029 error_buff = kmalloc(sizeof(*error_buff), GFP_ATOMIC);
3030 if (!error_buff)
3031 return;
3032
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04003033 detail_len = be32_to_cpu(err_crq->error_indication.detail_error_sz);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003034 error_buff->buff = kmalloc(detail_len, GFP_ATOMIC);
3035 if (!error_buff->buff) {
3036 kfree(error_buff);
3037 return;
3038 }
3039
3040 error_buff->dma = dma_map_single(dev, error_buff->buff, detail_len,
3041 DMA_FROM_DEVICE);
3042 if (dma_mapping_error(dev, error_buff->dma)) {
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04003043 netdev_err(netdev, "Couldn't map error buffer\n");
Thomas Falcon032c5e82015-12-21 11:26:06 -06003044 kfree(error_buff->buff);
3045 kfree(error_buff);
3046 return;
3047 }
3048
Thomas Falcon032c5e82015-12-21 11:26:06 -06003049 error_buff->len = detail_len;
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04003050 error_buff->error_id = err_crq->error_indication.error_id;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003051
3052 spin_lock_irqsave(&adapter->error_list_lock, flags);
3053 list_add_tail(&error_buff->list, &adapter->errors);
3054 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
3055
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04003056 memset(&crq, 0, sizeof(crq));
3057 crq.request_error_info.first = IBMVNIC_CRQ_CMD;
3058 crq.request_error_info.cmd = REQUEST_ERROR_INFO;
3059 crq.request_error_info.ioba = cpu_to_be32(error_buff->dma);
3060 crq.request_error_info.len = cpu_to_be32(detail_len);
3061 crq.request_error_info.error_id = err_crq->error_indication.error_id;
3062
3063 rc = ibmvnic_send_crq(adapter, &crq);
3064 if (rc) {
3065 netdev_err(netdev, "failed to request error information\n");
3066 goto err_info_fail;
3067 }
3068
3069 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
3070 netdev_err(netdev, "timeout waiting for error information\n");
3071 goto err_info_fail;
3072 }
3073
3074 return;
3075
3076err_info_fail:
3077 spin_lock_irqsave(&adapter->error_list_lock, flags);
3078 list_del(&error_buff->list);
3079 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
3080
3081 kfree(error_buff->buff);
3082 kfree(error_buff);
3083}
3084
3085static void handle_error_indication(union ibmvnic_crq *crq,
3086 struct ibmvnic_adapter *adapter)
3087{
3088 struct device *dev = &adapter->vdev->dev;
3089
3090 dev_err(dev, "Firmware reports %serror id %x, cause %d\n",
3091 crq->error_indication.flags
3092 & IBMVNIC_FATAL_ERROR ? "FATAL " : "",
3093 be32_to_cpu(crq->error_indication.error_id),
3094 be16_to_cpu(crq->error_indication.error_cause));
3095
3096 if (be32_to_cpu(crq->error_indication.error_id))
3097 request_error_information(adapter, crq);
Nathan Fontenoted651a12017-05-03 14:04:38 -04003098
3099 if (crq->error_indication.flags & IBMVNIC_FATAL_ERROR)
3100 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
John Allen8cb31cf2017-05-26 10:30:37 -04003101 else
3102 ibmvnic_reset(adapter, VNIC_RESET_NON_FATAL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003103}
3104
3105static void handle_change_mac_rsp(union ibmvnic_crq *crq,
3106 struct ibmvnic_adapter *adapter)
3107{
3108 struct net_device *netdev = adapter->netdev;
3109 struct device *dev = &adapter->vdev->dev;
3110 long rc;
3111
3112 rc = crq->change_mac_addr_rsp.rc.code;
3113 if (rc) {
3114 dev_err(dev, "Error %ld in CHANGE_MAC_ADDR_RSP\n", rc);
3115 return;
3116 }
3117 memcpy(netdev->dev_addr, &crq->change_mac_addr_rsp.mac_addr[0],
3118 ETH_ALEN);
3119}
3120
3121static void handle_request_cap_rsp(union ibmvnic_crq *crq,
3122 struct ibmvnic_adapter *adapter)
3123{
3124 struct device *dev = &adapter->vdev->dev;
3125 u64 *req_value;
3126 char *name;
3127
Thomas Falcon901e0402017-02-15 12:17:59 -06003128 atomic_dec(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003129 switch (be16_to_cpu(crq->request_capability_rsp.capability)) {
3130 case REQ_TX_QUEUES:
3131 req_value = &adapter->req_tx_queues;
3132 name = "tx";
3133 break;
3134 case REQ_RX_QUEUES:
3135 req_value = &adapter->req_rx_queues;
3136 name = "rx";
3137 break;
3138 case REQ_RX_ADD_QUEUES:
3139 req_value = &adapter->req_rx_add_queues;
3140 name = "rx_add";
3141 break;
3142 case REQ_TX_ENTRIES_PER_SUBCRQ:
3143 req_value = &adapter->req_tx_entries_per_subcrq;
3144 name = "tx_entries_per_subcrq";
3145 break;
3146 case REQ_RX_ADD_ENTRIES_PER_SUBCRQ:
3147 req_value = &adapter->req_rx_add_entries_per_subcrq;
3148 name = "rx_add_entries_per_subcrq";
3149 break;
3150 case REQ_MTU:
3151 req_value = &adapter->req_mtu;
3152 name = "mtu";
3153 break;
3154 case PROMISC_REQUESTED:
3155 req_value = &adapter->promisc;
3156 name = "promisc";
3157 break;
3158 default:
3159 dev_err(dev, "Got invalid cap request rsp %d\n",
3160 crq->request_capability.capability);
3161 return;
3162 }
3163
3164 switch (crq->request_capability_rsp.rc.code) {
3165 case SUCCESS:
3166 break;
3167 case PARTIALSUCCESS:
3168 dev_info(dev, "req=%lld, rsp=%ld in %s queue, retrying.\n",
3169 *req_value,
Thomas Falcon28f4d162017-02-15 10:32:11 -06003170 (long int)be64_to_cpu(crq->request_capability_rsp.
Thomas Falcon032c5e82015-12-21 11:26:06 -06003171 number), name);
Thomas Falcon28f4d162017-02-15 10:32:11 -06003172 *req_value = be64_to_cpu(crq->request_capability_rsp.number);
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003173 ibmvnic_send_req_caps(adapter, 1);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003174 return;
3175 default:
3176 dev_err(dev, "Error %d in request cap rsp\n",
3177 crq->request_capability_rsp.rc.code);
3178 return;
3179 }
3180
3181 /* Done receiving requested capabilities, query IP offload support */
Thomas Falcon901e0402017-02-15 12:17:59 -06003182 if (atomic_read(&adapter->running_cap_crqs) == 0) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06003183 union ibmvnic_crq newcrq;
3184 int buf_sz = sizeof(struct ibmvnic_query_ip_offload_buffer);
3185 struct ibmvnic_query_ip_offload_buffer *ip_offload_buf =
3186 &adapter->ip_offload_buf;
3187
Thomas Falcon249168a2017-02-15 12:18:00 -06003188 adapter->wait_capability = false;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003189 adapter->ip_offload_tok = dma_map_single(dev, ip_offload_buf,
3190 buf_sz,
3191 DMA_FROM_DEVICE);
3192
3193 if (dma_mapping_error(dev, adapter->ip_offload_tok)) {
3194 if (!firmware_has_feature(FW_FEATURE_CMO))
3195 dev_err(dev, "Couldn't map offload buffer\n");
3196 return;
3197 }
3198
3199 memset(&newcrq, 0, sizeof(newcrq));
3200 newcrq.query_ip_offload.first = IBMVNIC_CRQ_CMD;
3201 newcrq.query_ip_offload.cmd = QUERY_IP_OFFLOAD;
3202 newcrq.query_ip_offload.len = cpu_to_be32(buf_sz);
3203 newcrq.query_ip_offload.ioba =
3204 cpu_to_be32(adapter->ip_offload_tok);
3205
3206 ibmvnic_send_crq(adapter, &newcrq);
3207 }
3208}
3209
3210static int handle_login_rsp(union ibmvnic_crq *login_rsp_crq,
3211 struct ibmvnic_adapter *adapter)
3212{
3213 struct device *dev = &adapter->vdev->dev;
3214 struct ibmvnic_login_rsp_buffer *login_rsp = adapter->login_rsp_buf;
3215 struct ibmvnic_login_buffer *login = adapter->login_buf;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003216 int i;
3217
3218 dma_unmap_single(dev, adapter->login_buf_token, adapter->login_buf_sz,
3219 DMA_BIDIRECTIONAL);
3220 dma_unmap_single(dev, adapter->login_rsp_buf_token,
3221 adapter->login_rsp_buf_sz, DMA_BIDIRECTIONAL);
3222
John Allen498cd8e2016-04-06 11:49:55 -05003223 /* If the number of queues requested can't be allocated by the
3224 * server, the login response will return with code 1. We will need
3225 * to resend the login buffer with fewer queues requested.
3226 */
3227 if (login_rsp_crq->generic.rc.code) {
3228 adapter->renegotiate = true;
3229 complete(&adapter->init_done);
3230 return 0;
3231 }
3232
Thomas Falcon032c5e82015-12-21 11:26:06 -06003233 netdev_dbg(adapter->netdev, "Login Response Buffer:\n");
3234 for (i = 0; i < (adapter->login_rsp_buf_sz - 1) / 8 + 1; i++) {
3235 netdev_dbg(adapter->netdev, "%016lx\n",
3236 ((unsigned long int *)(adapter->login_rsp_buf))[i]);
3237 }
3238
3239 /* Sanity checks */
3240 if (login->num_txcomp_subcrqs != login_rsp->num_txsubm_subcrqs ||
3241 (be32_to_cpu(login->num_rxcomp_subcrqs) *
3242 adapter->req_rx_add_queues !=
3243 be32_to_cpu(login_rsp->num_rxadd_subcrqs))) {
3244 dev_err(dev, "FATAL: Inconsistent login and login rsp\n");
3245 ibmvnic_remove(adapter->vdev);
3246 return -EIO;
3247 }
3248 complete(&adapter->init_done);
3249
Thomas Falcon032c5e82015-12-21 11:26:06 -06003250 return 0;
3251}
3252
Thomas Falcon032c5e82015-12-21 11:26:06 -06003253static void handle_request_unmap_rsp(union ibmvnic_crq *crq,
3254 struct ibmvnic_adapter *adapter)
3255{
3256 struct device *dev = &adapter->vdev->dev;
3257 long rc;
3258
3259 rc = crq->request_unmap_rsp.rc.code;
3260 if (rc)
3261 dev_err(dev, "Error %ld in REQUEST_UNMAP_RSP\n", rc);
3262}
3263
3264static void handle_query_map_rsp(union ibmvnic_crq *crq,
3265 struct ibmvnic_adapter *adapter)
3266{
3267 struct net_device *netdev = adapter->netdev;
3268 struct device *dev = &adapter->vdev->dev;
3269 long rc;
3270
3271 rc = crq->query_map_rsp.rc.code;
3272 if (rc) {
3273 dev_err(dev, "Error %ld in QUERY_MAP_RSP\n", rc);
3274 return;
3275 }
3276 netdev_dbg(netdev, "page_size = %d\ntot_pages = %d\nfree_pages = %d\n",
3277 crq->query_map_rsp.page_size, crq->query_map_rsp.tot_pages,
3278 crq->query_map_rsp.free_pages);
3279}
3280
3281static void handle_query_cap_rsp(union ibmvnic_crq *crq,
3282 struct ibmvnic_adapter *adapter)
3283{
3284 struct net_device *netdev = adapter->netdev;
3285 struct device *dev = &adapter->vdev->dev;
3286 long rc;
3287
Thomas Falcon901e0402017-02-15 12:17:59 -06003288 atomic_dec(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003289 netdev_dbg(netdev, "Outstanding queries: %d\n",
Thomas Falcon901e0402017-02-15 12:17:59 -06003290 atomic_read(&adapter->running_cap_crqs));
Thomas Falcon032c5e82015-12-21 11:26:06 -06003291 rc = crq->query_capability.rc.code;
3292 if (rc) {
3293 dev_err(dev, "Error %ld in QUERY_CAP_RSP\n", rc);
3294 goto out;
3295 }
3296
3297 switch (be16_to_cpu(crq->query_capability.capability)) {
3298 case MIN_TX_QUEUES:
3299 adapter->min_tx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003300 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003301 netdev_dbg(netdev, "min_tx_queues = %lld\n",
3302 adapter->min_tx_queues);
3303 break;
3304 case MIN_RX_QUEUES:
3305 adapter->min_rx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003306 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003307 netdev_dbg(netdev, "min_rx_queues = %lld\n",
3308 adapter->min_rx_queues);
3309 break;
3310 case MIN_RX_ADD_QUEUES:
3311 adapter->min_rx_add_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003312 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003313 netdev_dbg(netdev, "min_rx_add_queues = %lld\n",
3314 adapter->min_rx_add_queues);
3315 break;
3316 case MAX_TX_QUEUES:
3317 adapter->max_tx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003318 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003319 netdev_dbg(netdev, "max_tx_queues = %lld\n",
3320 adapter->max_tx_queues);
3321 break;
3322 case MAX_RX_QUEUES:
3323 adapter->max_rx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003324 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003325 netdev_dbg(netdev, "max_rx_queues = %lld\n",
3326 adapter->max_rx_queues);
3327 break;
3328 case MAX_RX_ADD_QUEUES:
3329 adapter->max_rx_add_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003330 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003331 netdev_dbg(netdev, "max_rx_add_queues = %lld\n",
3332 adapter->max_rx_add_queues);
3333 break;
3334 case MIN_TX_ENTRIES_PER_SUBCRQ:
3335 adapter->min_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06003336 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003337 netdev_dbg(netdev, "min_tx_entries_per_subcrq = %lld\n",
3338 adapter->min_tx_entries_per_subcrq);
3339 break;
3340 case MIN_RX_ADD_ENTRIES_PER_SUBCRQ:
3341 adapter->min_rx_add_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06003342 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003343 netdev_dbg(netdev, "min_rx_add_entrs_per_subcrq = %lld\n",
3344 adapter->min_rx_add_entries_per_subcrq);
3345 break;
3346 case MAX_TX_ENTRIES_PER_SUBCRQ:
3347 adapter->max_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06003348 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003349 netdev_dbg(netdev, "max_tx_entries_per_subcrq = %lld\n",
3350 adapter->max_tx_entries_per_subcrq);
3351 break;
3352 case MAX_RX_ADD_ENTRIES_PER_SUBCRQ:
3353 adapter->max_rx_add_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06003354 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003355 netdev_dbg(netdev, "max_rx_add_entrs_per_subcrq = %lld\n",
3356 adapter->max_rx_add_entries_per_subcrq);
3357 break;
3358 case TCP_IP_OFFLOAD:
3359 adapter->tcp_ip_offload =
Thomas Falconde89e852016-03-01 10:20:09 -06003360 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003361 netdev_dbg(netdev, "tcp_ip_offload = %lld\n",
3362 adapter->tcp_ip_offload);
3363 break;
3364 case PROMISC_SUPPORTED:
3365 adapter->promisc_supported =
Thomas Falconde89e852016-03-01 10:20:09 -06003366 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003367 netdev_dbg(netdev, "promisc_supported = %lld\n",
3368 adapter->promisc_supported);
3369 break;
3370 case MIN_MTU:
Thomas Falconde89e852016-03-01 10:20:09 -06003371 adapter->min_mtu = be64_to_cpu(crq->query_capability.number);
Thomas Falconf39f0d12017-02-14 10:22:59 -06003372 netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003373 netdev_dbg(netdev, "min_mtu = %lld\n", adapter->min_mtu);
3374 break;
3375 case MAX_MTU:
Thomas Falconde89e852016-03-01 10:20:09 -06003376 adapter->max_mtu = be64_to_cpu(crq->query_capability.number);
Thomas Falconf39f0d12017-02-14 10:22:59 -06003377 netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003378 netdev_dbg(netdev, "max_mtu = %lld\n", adapter->max_mtu);
3379 break;
3380 case MAX_MULTICAST_FILTERS:
3381 adapter->max_multicast_filters =
Thomas Falconde89e852016-03-01 10:20:09 -06003382 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003383 netdev_dbg(netdev, "max_multicast_filters = %lld\n",
3384 adapter->max_multicast_filters);
3385 break;
3386 case VLAN_HEADER_INSERTION:
3387 adapter->vlan_header_insertion =
Thomas Falconde89e852016-03-01 10:20:09 -06003388 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003389 if (adapter->vlan_header_insertion)
3390 netdev->features |= NETIF_F_HW_VLAN_STAG_TX;
3391 netdev_dbg(netdev, "vlan_header_insertion = %lld\n",
3392 adapter->vlan_header_insertion);
3393 break;
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04003394 case RX_VLAN_HEADER_INSERTION:
3395 adapter->rx_vlan_header_insertion =
3396 be64_to_cpu(crq->query_capability.number);
3397 netdev_dbg(netdev, "rx_vlan_header_insertion = %lld\n",
3398 adapter->rx_vlan_header_insertion);
3399 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003400 case MAX_TX_SG_ENTRIES:
3401 adapter->max_tx_sg_entries =
Thomas Falconde89e852016-03-01 10:20:09 -06003402 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003403 netdev_dbg(netdev, "max_tx_sg_entries = %lld\n",
3404 adapter->max_tx_sg_entries);
3405 break;
3406 case RX_SG_SUPPORTED:
3407 adapter->rx_sg_supported =
Thomas Falconde89e852016-03-01 10:20:09 -06003408 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003409 netdev_dbg(netdev, "rx_sg_supported = %lld\n",
3410 adapter->rx_sg_supported);
3411 break;
3412 case OPT_TX_COMP_SUB_QUEUES:
3413 adapter->opt_tx_comp_sub_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003414 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003415 netdev_dbg(netdev, "opt_tx_comp_sub_queues = %lld\n",
3416 adapter->opt_tx_comp_sub_queues);
3417 break;
3418 case OPT_RX_COMP_QUEUES:
3419 adapter->opt_rx_comp_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003420 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003421 netdev_dbg(netdev, "opt_rx_comp_queues = %lld\n",
3422 adapter->opt_rx_comp_queues);
3423 break;
3424 case OPT_RX_BUFADD_Q_PER_RX_COMP_Q:
3425 adapter->opt_rx_bufadd_q_per_rx_comp_q =
Thomas Falconde89e852016-03-01 10:20:09 -06003426 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003427 netdev_dbg(netdev, "opt_rx_bufadd_q_per_rx_comp_q = %lld\n",
3428 adapter->opt_rx_bufadd_q_per_rx_comp_q);
3429 break;
3430 case OPT_TX_ENTRIES_PER_SUBCRQ:
3431 adapter->opt_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06003432 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003433 netdev_dbg(netdev, "opt_tx_entries_per_subcrq = %lld\n",
3434 adapter->opt_tx_entries_per_subcrq);
3435 break;
3436 case OPT_RXBA_ENTRIES_PER_SUBCRQ:
3437 adapter->opt_rxba_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06003438 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003439 netdev_dbg(netdev, "opt_rxba_entries_per_subcrq = %lld\n",
3440 adapter->opt_rxba_entries_per_subcrq);
3441 break;
3442 case TX_RX_DESC_REQ:
3443 adapter->tx_rx_desc_req = crq->query_capability.number;
3444 netdev_dbg(netdev, "tx_rx_desc_req = %llx\n",
3445 adapter->tx_rx_desc_req);
3446 break;
3447
3448 default:
3449 netdev_err(netdev, "Got invalid cap rsp %d\n",
3450 crq->query_capability.capability);
3451 }
3452
3453out:
Thomas Falcon249168a2017-02-15 12:18:00 -06003454 if (atomic_read(&adapter->running_cap_crqs) == 0) {
3455 adapter->wait_capability = false;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003456 ibmvnic_send_req_caps(adapter, 0);
Thomas Falcon249168a2017-02-15 12:18:00 -06003457 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06003458}
3459
Thomas Falcon032c5e82015-12-21 11:26:06 -06003460static void ibmvnic_handle_crq(union ibmvnic_crq *crq,
3461 struct ibmvnic_adapter *adapter)
3462{
3463 struct ibmvnic_generic_crq *gen_crq = &crq->generic;
3464 struct net_device *netdev = adapter->netdev;
3465 struct device *dev = &adapter->vdev->dev;
Murilo Fossa Vicentini993a82b2017-04-19 13:44:35 -04003466 u64 *u64_crq = (u64 *)crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003467 long rc;
3468
3469 netdev_dbg(netdev, "Handling CRQ: %016lx %016lx\n",
Murilo Fossa Vicentini993a82b2017-04-19 13:44:35 -04003470 (unsigned long int)cpu_to_be64(u64_crq[0]),
3471 (unsigned long int)cpu_to_be64(u64_crq[1]));
Thomas Falcon032c5e82015-12-21 11:26:06 -06003472 switch (gen_crq->first) {
3473 case IBMVNIC_CRQ_INIT_RSP:
3474 switch (gen_crq->cmd) {
3475 case IBMVNIC_CRQ_INIT:
3476 dev_info(dev, "Partner initialized\n");
John Allen017892c12017-05-26 10:30:19 -04003477 adapter->from_passive_init = true;
3478 complete(&adapter->init_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003479 break;
3480 case IBMVNIC_CRQ_INIT_COMPLETE:
3481 dev_info(dev, "Partner initialization complete\n");
3482 send_version_xchg(adapter);
3483 break;
3484 default:
3485 dev_err(dev, "Unknown crq cmd: %d\n", gen_crq->cmd);
3486 }
3487 return;
3488 case IBMVNIC_CRQ_XPORT_EVENT:
Nathan Fontenoted651a12017-05-03 14:04:38 -04003489 netif_carrier_off(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003490 if (gen_crq->cmd == IBMVNIC_PARTITION_MIGRATED) {
Nathan Fontenoted651a12017-05-03 14:04:38 -04003491 dev_info(dev, "Migrated, re-enabling adapter\n");
3492 ibmvnic_reset(adapter, VNIC_RESET_MOBILITY);
Thomas Falcondfad09a2016-08-18 11:37:51 -05003493 } else if (gen_crq->cmd == IBMVNIC_DEVICE_FAILOVER) {
3494 dev_info(dev, "Backing device failover detected\n");
Nathan Fontenoted651a12017-05-03 14:04:38 -04003495 ibmvnic_reset(adapter, VNIC_RESET_FAILOVER);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003496 } else {
3497 /* The adapter lost the connection */
3498 dev_err(dev, "Virtual Adapter failed (rc=%d)\n",
3499 gen_crq->cmd);
Nathan Fontenoted651a12017-05-03 14:04:38 -04003500 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003501 }
3502 return;
3503 case IBMVNIC_CRQ_CMD_RSP:
3504 break;
3505 default:
3506 dev_err(dev, "Got an invalid msg type 0x%02x\n",
3507 gen_crq->first);
3508 return;
3509 }
3510
3511 switch (gen_crq->cmd) {
3512 case VERSION_EXCHANGE_RSP:
3513 rc = crq->version_exchange_rsp.rc.code;
3514 if (rc) {
3515 dev_err(dev, "Error %ld in VERSION_EXCHG_RSP\n", rc);
3516 break;
3517 }
3518 dev_info(dev, "Partner protocol version is %d\n",
3519 crq->version_exchange_rsp.version);
3520 if (be16_to_cpu(crq->version_exchange_rsp.version) <
3521 ibmvnic_version)
3522 ibmvnic_version =
3523 be16_to_cpu(crq->version_exchange_rsp.version);
3524 send_cap_queries(adapter);
3525 break;
3526 case QUERY_CAPABILITY_RSP:
3527 handle_query_cap_rsp(crq, adapter);
3528 break;
3529 case QUERY_MAP_RSP:
3530 handle_query_map_rsp(crq, adapter);
3531 break;
3532 case REQUEST_MAP_RSP:
Thomas Falconf3be0cb2017-06-21 14:53:01 -05003533 adapter->fw_done_rc = crq->request_map_rsp.rc.code;
3534 complete(&adapter->fw_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003535 break;
3536 case REQUEST_UNMAP_RSP:
3537 handle_request_unmap_rsp(crq, adapter);
3538 break;
3539 case REQUEST_CAPABILITY_RSP:
3540 handle_request_cap_rsp(crq, adapter);
3541 break;
3542 case LOGIN_RSP:
3543 netdev_dbg(netdev, "Got Login Response\n");
3544 handle_login_rsp(crq, adapter);
3545 break;
3546 case LOGICAL_LINK_STATE_RSP:
Nathan Fontenot53da09e2017-04-21 15:39:04 -04003547 netdev_dbg(netdev,
3548 "Got Logical Link State Response, state: %d rc: %d\n",
3549 crq->logical_link_state_rsp.link_state,
3550 crq->logical_link_state_rsp.rc.code);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003551 adapter->logical_link_state =
3552 crq->logical_link_state_rsp.link_state;
Nathan Fontenot53da09e2017-04-21 15:39:04 -04003553 adapter->init_done_rc = crq->logical_link_state_rsp.rc.code;
3554 complete(&adapter->init_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003555 break;
3556 case LINK_STATE_INDICATION:
3557 netdev_dbg(netdev, "Got Logical Link State Indication\n");
3558 adapter->phys_link_state =
3559 crq->link_state_indication.phys_link_state;
3560 adapter->logical_link_state =
3561 crq->link_state_indication.logical_link_state;
3562 break;
3563 case CHANGE_MAC_ADDR_RSP:
3564 netdev_dbg(netdev, "Got MAC address change Response\n");
3565 handle_change_mac_rsp(crq, adapter);
3566 break;
3567 case ERROR_INDICATION:
3568 netdev_dbg(netdev, "Got Error Indication\n");
3569 handle_error_indication(crq, adapter);
3570 break;
3571 case REQUEST_ERROR_RSP:
3572 netdev_dbg(netdev, "Got Error Detail Response\n");
3573 handle_error_info_rsp(crq, adapter);
3574 break;
3575 case REQUEST_STATISTICS_RSP:
3576 netdev_dbg(netdev, "Got Statistics Response\n");
3577 complete(&adapter->stats_done);
3578 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003579 case QUERY_IP_OFFLOAD_RSP:
3580 netdev_dbg(netdev, "Got Query IP offload Response\n");
3581 handle_query_ip_offload_rsp(adapter);
3582 break;
3583 case MULTICAST_CTRL_RSP:
3584 netdev_dbg(netdev, "Got multicast control Response\n");
3585 break;
3586 case CONTROL_IP_OFFLOAD_RSP:
3587 netdev_dbg(netdev, "Got Control IP offload Response\n");
3588 dma_unmap_single(dev, adapter->ip_offload_ctrl_tok,
3589 sizeof(adapter->ip_offload_ctrl),
3590 DMA_TO_DEVICE);
John Allenbd0b6722017-03-17 17:13:40 -05003591 complete(&adapter->init_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003592 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003593 case COLLECT_FW_TRACE_RSP:
3594 netdev_dbg(netdev, "Got Collect firmware trace Response\n");
3595 complete(&adapter->fw_done);
3596 break;
3597 default:
3598 netdev_err(netdev, "Got an invalid cmd type 0x%02x\n",
3599 gen_crq->cmd);
3600 }
3601}
3602
3603static irqreturn_t ibmvnic_interrupt(int irq, void *instance)
3604{
3605 struct ibmvnic_adapter *adapter = instance;
Thomas Falcon6c267b32017-02-15 12:17:58 -06003606
Thomas Falcon6c267b32017-02-15 12:17:58 -06003607 tasklet_schedule(&adapter->tasklet);
Thomas Falcon6c267b32017-02-15 12:17:58 -06003608 return IRQ_HANDLED;
3609}
3610
3611static void ibmvnic_tasklet(void *data)
3612{
3613 struct ibmvnic_adapter *adapter = data;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003614 struct ibmvnic_crq_queue *queue = &adapter->crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003615 union ibmvnic_crq *crq;
3616 unsigned long flags;
3617 bool done = false;
3618
3619 spin_lock_irqsave(&queue->lock, flags);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003620 while (!done) {
3621 /* Pull all the valid messages off the CRQ */
3622 while ((crq = ibmvnic_next_crq(adapter)) != NULL) {
3623 ibmvnic_handle_crq(crq, adapter);
3624 crq->generic.first = 0;
3625 }
Brian Kinged7ecbf2017-04-19 13:44:53 -04003626
3627 /* remain in tasklet until all
3628 * capabilities responses are received
3629 */
3630 if (!adapter->wait_capability)
3631 done = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003632 }
Thomas Falcon249168a2017-02-15 12:18:00 -06003633 /* if capabilities CRQ's were sent in this tasklet, the following
3634 * tasklet must wait until all responses are received
3635 */
3636 if (atomic_read(&adapter->running_cap_crqs) != 0)
3637 adapter->wait_capability = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003638 spin_unlock_irqrestore(&queue->lock, flags);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003639}
3640
3641static int ibmvnic_reenable_crq_queue(struct ibmvnic_adapter *adapter)
3642{
3643 struct vio_dev *vdev = adapter->vdev;
3644 int rc;
3645
3646 do {
3647 rc = plpar_hcall_norets(H_ENABLE_CRQ, vdev->unit_address);
3648 } while (rc == H_IN_PROGRESS || rc == H_BUSY || H_IS_LONG_BUSY(rc));
3649
3650 if (rc)
3651 dev_err(&vdev->dev, "Error enabling adapter (rc=%d)\n", rc);
3652
3653 return rc;
3654}
3655
3656static int ibmvnic_reset_crq(struct ibmvnic_adapter *adapter)
3657{
3658 struct ibmvnic_crq_queue *crq = &adapter->crq;
3659 struct device *dev = &adapter->vdev->dev;
3660 struct vio_dev *vdev = adapter->vdev;
3661 int rc;
3662
3663 /* Close the CRQ */
3664 do {
3665 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
3666 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
3667
3668 /* Clean out the queue */
3669 memset(crq->msgs, 0, PAGE_SIZE);
3670 crq->cur = 0;
3671
3672 /* And re-open it again */
3673 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
3674 crq->msg_token, PAGE_SIZE);
3675
3676 if (rc == H_CLOSED)
3677 /* Adapter is good, but other end is not ready */
3678 dev_warn(dev, "Partner adapter not ready\n");
3679 else if (rc != 0)
3680 dev_warn(dev, "Couldn't register crq (rc=%d)\n", rc);
3681
3682 return rc;
3683}
3684
Nathan Fontenotf9928872017-03-30 02:48:54 -04003685static void release_crq_queue(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003686{
3687 struct ibmvnic_crq_queue *crq = &adapter->crq;
3688 struct vio_dev *vdev = adapter->vdev;
3689 long rc;
3690
Nathan Fontenotf9928872017-03-30 02:48:54 -04003691 if (!crq->msgs)
3692 return;
3693
Thomas Falcon032c5e82015-12-21 11:26:06 -06003694 netdev_dbg(adapter->netdev, "Releasing CRQ\n");
3695 free_irq(vdev->irq, adapter);
Thomas Falcon6c267b32017-02-15 12:17:58 -06003696 tasklet_kill(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003697 do {
3698 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
3699 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
3700
3701 dma_unmap_single(&vdev->dev, crq->msg_token, PAGE_SIZE,
3702 DMA_BIDIRECTIONAL);
3703 free_page((unsigned long)crq->msgs);
Nathan Fontenotf9928872017-03-30 02:48:54 -04003704 crq->msgs = NULL;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003705}
3706
Nathan Fontenotf9928872017-03-30 02:48:54 -04003707static int init_crq_queue(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003708{
3709 struct ibmvnic_crq_queue *crq = &adapter->crq;
3710 struct device *dev = &adapter->vdev->dev;
3711 struct vio_dev *vdev = adapter->vdev;
3712 int rc, retrc = -ENOMEM;
3713
Nathan Fontenotf9928872017-03-30 02:48:54 -04003714 if (crq->msgs)
3715 return 0;
3716
Thomas Falcon032c5e82015-12-21 11:26:06 -06003717 crq->msgs = (union ibmvnic_crq *)get_zeroed_page(GFP_KERNEL);
3718 /* Should we allocate more than one page? */
3719
3720 if (!crq->msgs)
3721 return -ENOMEM;
3722
3723 crq->size = PAGE_SIZE / sizeof(*crq->msgs);
3724 crq->msg_token = dma_map_single(dev, crq->msgs, PAGE_SIZE,
3725 DMA_BIDIRECTIONAL);
3726 if (dma_mapping_error(dev, crq->msg_token))
3727 goto map_failed;
3728
3729 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
3730 crq->msg_token, PAGE_SIZE);
3731
3732 if (rc == H_RESOURCE)
3733 /* maybe kexecing and resource is busy. try a reset */
3734 rc = ibmvnic_reset_crq(adapter);
3735 retrc = rc;
3736
3737 if (rc == H_CLOSED) {
3738 dev_warn(dev, "Partner adapter not ready\n");
3739 } else if (rc) {
3740 dev_warn(dev, "Error %d opening adapter\n", rc);
3741 goto reg_crq_failed;
3742 }
3743
3744 retrc = 0;
3745
Thomas Falcon6c267b32017-02-15 12:17:58 -06003746 tasklet_init(&adapter->tasklet, (void *)ibmvnic_tasklet,
3747 (unsigned long)adapter);
3748
Thomas Falcon032c5e82015-12-21 11:26:06 -06003749 netdev_dbg(adapter->netdev, "registering irq 0x%x\n", vdev->irq);
3750 rc = request_irq(vdev->irq, ibmvnic_interrupt, 0, IBMVNIC_NAME,
3751 adapter);
3752 if (rc) {
3753 dev_err(dev, "Couldn't register irq 0x%x. rc=%d\n",
3754 vdev->irq, rc);
3755 goto req_irq_failed;
3756 }
3757
3758 rc = vio_enable_interrupts(vdev);
3759 if (rc) {
3760 dev_err(dev, "Error %d enabling interrupts\n", rc);
3761 goto req_irq_failed;
3762 }
3763
3764 crq->cur = 0;
3765 spin_lock_init(&crq->lock);
3766
3767 return retrc;
3768
3769req_irq_failed:
Thomas Falcon6c267b32017-02-15 12:17:58 -06003770 tasklet_kill(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003771 do {
3772 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
3773 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
3774reg_crq_failed:
3775 dma_unmap_single(dev, crq->msg_token, PAGE_SIZE, DMA_BIDIRECTIONAL);
3776map_failed:
3777 free_page((unsigned long)crq->msgs);
Nathan Fontenotf9928872017-03-30 02:48:54 -04003778 crq->msgs = NULL;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003779 return retrc;
3780}
3781
John Allenf6ef6402017-03-17 17:13:42 -05003782static int ibmvnic_init(struct ibmvnic_adapter *adapter)
3783{
3784 struct device *dev = &adapter->vdev->dev;
3785 unsigned long timeout = msecs_to_jiffies(30000);
John Allenf6ef6402017-03-17 17:13:42 -05003786 int rc;
3787
Nathan Fontenot28cde752017-05-26 10:31:00 -04003788 if (adapter->resetting) {
3789 rc = ibmvnic_reset_crq(adapter);
3790 if (!rc)
3791 rc = vio_enable_interrupts(adapter->vdev);
3792 } else {
3793 rc = init_crq_queue(adapter);
3794 }
3795
John Allenf6ef6402017-03-17 17:13:42 -05003796 if (rc) {
3797 dev_err(dev, "Couldn't initialize crq. rc=%d\n", rc);
3798 return rc;
3799 }
3800
John Allen017892c12017-05-26 10:30:19 -04003801 adapter->from_passive_init = false;
3802
John Allenf6ef6402017-03-17 17:13:42 -05003803 init_completion(&adapter->init_done);
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04003804 adapter->init_done_rc = 0;
John Allenf6ef6402017-03-17 17:13:42 -05003805 ibmvnic_send_crq_init(adapter);
3806 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
3807 dev_err(dev, "Initialization sequence timed out\n");
John Allen017892c12017-05-26 10:30:19 -04003808 return -1;
3809 }
3810
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04003811 if (adapter->init_done_rc) {
3812 release_crq_queue(adapter);
3813 return adapter->init_done_rc;
3814 }
3815
John Allen017892c12017-05-26 10:30:19 -04003816 if (adapter->from_passive_init) {
3817 adapter->state = VNIC_OPEN;
3818 adapter->from_passive_init = false;
John Allenf6ef6402017-03-17 17:13:42 -05003819 return -1;
3820 }
3821
Nathan Fontenot57a49432017-05-26 10:31:12 -04003822 if (adapter->resetting)
3823 rc = reset_sub_crq_queues(adapter);
3824 else
3825 rc = init_sub_crqs(adapter);
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04003826 if (rc) {
3827 dev_err(dev, "Initialization of sub crqs failed\n");
3828 release_crq_queue(adapter);
Thomas Falcon5df969c2017-06-28 19:55:54 -05003829 return rc;
3830 }
3831
3832 rc = init_sub_crq_irqs(adapter);
3833 if (rc) {
3834 dev_err(dev, "Failed to initialize sub crq irqs\n");
3835 release_crq_queue(adapter);
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04003836 }
3837
3838 return rc;
John Allenf6ef6402017-03-17 17:13:42 -05003839}
3840
Thomas Falcon40c9db82017-06-12 12:35:04 -05003841static struct device_attribute dev_attr_failover;
3842
Thomas Falcon032c5e82015-12-21 11:26:06 -06003843static int ibmvnic_probe(struct vio_dev *dev, const struct vio_device_id *id)
3844{
3845 struct ibmvnic_adapter *adapter;
3846 struct net_device *netdev;
3847 unsigned char *mac_addr_p;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003848 int rc;
3849
3850 dev_dbg(&dev->dev, "entering ibmvnic_probe for UA 0x%x\n",
3851 dev->unit_address);
3852
3853 mac_addr_p = (unsigned char *)vio_get_attribute(dev,
3854 VETH_MAC_ADDR, NULL);
3855 if (!mac_addr_p) {
3856 dev_err(&dev->dev,
3857 "(%s:%3.3d) ERROR: Can't find MAC_ADDR attribute\n",
3858 __FILE__, __LINE__);
3859 return 0;
3860 }
3861
3862 netdev = alloc_etherdev_mq(sizeof(struct ibmvnic_adapter),
3863 IBMVNIC_MAX_TX_QUEUES);
3864 if (!netdev)
3865 return -ENOMEM;
3866
3867 adapter = netdev_priv(netdev);
Nathan Fontenot90c80142017-05-03 14:04:32 -04003868 adapter->state = VNIC_PROBING;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003869 dev_set_drvdata(&dev->dev, netdev);
3870 adapter->vdev = dev;
3871 adapter->netdev = netdev;
3872
3873 ether_addr_copy(adapter->mac_addr, mac_addr_p);
3874 ether_addr_copy(netdev->dev_addr, adapter->mac_addr);
3875 netdev->irq = dev->irq;
3876 netdev->netdev_ops = &ibmvnic_netdev_ops;
3877 netdev->ethtool_ops = &ibmvnic_ethtool_ops;
3878 SET_NETDEV_DEV(netdev, &dev->dev);
3879
3880 spin_lock_init(&adapter->stats_lock);
3881
Thomas Falcon032c5e82015-12-21 11:26:06 -06003882 INIT_LIST_HEAD(&adapter->errors);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003883 spin_lock_init(&adapter->error_list_lock);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003884
Nathan Fontenoted651a12017-05-03 14:04:38 -04003885 INIT_WORK(&adapter->ibmvnic_reset, __ibmvnic_reset);
3886 INIT_LIST_HEAD(&adapter->rwi_list);
3887 mutex_init(&adapter->reset_lock);
3888 mutex_init(&adapter->rwi_lock);
3889 adapter->resetting = false;
3890
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04003891 do {
3892 rc = ibmvnic_init(adapter);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05003893 if (rc && rc != EAGAIN)
3894 goto ibmvnic_init_fail;
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04003895 } while (rc == EAGAIN);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003896
Thomas Falconf39f0d12017-02-14 10:22:59 -06003897 netdev->mtu = adapter->req_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003898
Thomas Falcon40c9db82017-06-12 12:35:04 -05003899 rc = device_create_file(&dev->dev, &dev_attr_failover);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05003900 if (rc)
3901 goto ibmvnic_init_fail;
Thomas Falcon40c9db82017-06-12 12:35:04 -05003902
Mick Tarsele876a8a2017-09-28 13:53:18 -07003903 netif_carrier_off(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003904 rc = register_netdev(netdev);
3905 if (rc) {
3906 dev_err(&dev->dev, "failed to register netdev rc=%d\n", rc);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05003907 goto ibmvnic_register_fail;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003908 }
3909 dev_info(&dev->dev, "ibmvnic registered\n");
3910
Nathan Fontenot90c80142017-05-03 14:04:32 -04003911 adapter->state = VNIC_PROBED;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003912 return 0;
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05003913
3914ibmvnic_register_fail:
3915 device_remove_file(&dev->dev, &dev_attr_failover);
3916
3917ibmvnic_init_fail:
3918 release_sub_crqs(adapter);
3919 release_crq_queue(adapter);
3920 free_netdev(netdev);
3921
3922 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003923}
3924
3925static int ibmvnic_remove(struct vio_dev *dev)
3926{
3927 struct net_device *netdev = dev_get_drvdata(&dev->dev);
Nathan Fontenot37489052017-04-19 13:45:04 -04003928 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003929
Nathan Fontenot90c80142017-05-03 14:04:32 -04003930 adapter->state = VNIC_REMOVING;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003931 unregister_netdev(netdev);
Nathan Fontenoted651a12017-05-03 14:04:38 -04003932 mutex_lock(&adapter->reset_lock);
Nathan Fontenot37489052017-04-19 13:45:04 -04003933
3934 release_resources(adapter);
3935 release_sub_crqs(adapter);
3936 release_crq_queue(adapter);
3937
Nathan Fontenot90c80142017-05-03 14:04:32 -04003938 adapter->state = VNIC_REMOVED;
3939
Nathan Fontenoted651a12017-05-03 14:04:38 -04003940 mutex_unlock(&adapter->reset_lock);
Thomas Falcon40c9db82017-06-12 12:35:04 -05003941 device_remove_file(&dev->dev, &dev_attr_failover);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003942 free_netdev(netdev);
3943 dev_set_drvdata(&dev->dev, NULL);
3944
3945 return 0;
3946}
3947
Thomas Falcon40c9db82017-06-12 12:35:04 -05003948static ssize_t failover_store(struct device *dev, struct device_attribute *attr,
3949 const char *buf, size_t count)
3950{
3951 struct net_device *netdev = dev_get_drvdata(dev);
3952 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
3953 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
3954 __be64 session_token;
3955 long rc;
3956
3957 if (!sysfs_streq(buf, "1"))
3958 return -EINVAL;
3959
3960 rc = plpar_hcall(H_VIOCTL, retbuf, adapter->vdev->unit_address,
3961 H_GET_SESSION_TOKEN, 0, 0, 0);
3962 if (rc) {
3963 netdev_err(netdev, "Couldn't retrieve session token, rc %ld\n",
3964 rc);
3965 return -EINVAL;
3966 }
3967
3968 session_token = (__be64)retbuf[0];
3969 netdev_dbg(netdev, "Initiating client failover, session id %llx\n",
3970 be64_to_cpu(session_token));
3971 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
3972 H_SESSION_ERR_DETECTED, session_token, 0, 0);
3973 if (rc) {
3974 netdev_err(netdev, "Client initiated failover failed, rc %ld\n",
3975 rc);
3976 return -EINVAL;
3977 }
3978
3979 return count;
3980}
3981
3982static DEVICE_ATTR(failover, 0200, NULL, failover_store);
3983
Thomas Falcon032c5e82015-12-21 11:26:06 -06003984static unsigned long ibmvnic_get_desired_dma(struct vio_dev *vdev)
3985{
3986 struct net_device *netdev = dev_get_drvdata(&vdev->dev);
3987 struct ibmvnic_adapter *adapter;
3988 struct iommu_table *tbl;
3989 unsigned long ret = 0;
3990 int i;
3991
3992 tbl = get_iommu_table_base(&vdev->dev);
3993
3994 /* netdev inits at probe time along with the structures we need below*/
3995 if (!netdev)
3996 return IOMMU_PAGE_ALIGN(IBMVNIC_IO_ENTITLEMENT_DEFAULT, tbl);
3997
3998 adapter = netdev_priv(netdev);
3999
4000 ret += PAGE_SIZE; /* the crq message queue */
Thomas Falcon032c5e82015-12-21 11:26:06 -06004001 ret += IOMMU_PAGE_ALIGN(sizeof(struct ibmvnic_statistics), tbl);
4002
4003 for (i = 0; i < adapter->req_tx_queues + adapter->req_rx_queues; i++)
4004 ret += 4 * PAGE_SIZE; /* the scrq message queue */
4005
4006 for (i = 0; i < be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
4007 i++)
4008 ret += adapter->rx_pool[i].size *
4009 IOMMU_PAGE_ALIGN(adapter->rx_pool[i].buff_size, tbl);
4010
4011 return ret;
4012}
4013
4014static int ibmvnic_resume(struct device *dev)
4015{
4016 struct net_device *netdev = dev_get_drvdata(dev);
4017 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004018
John Allencb89ba22017-06-19 11:27:53 -05004019 if (adapter->state != VNIC_OPEN)
4020 return 0;
4021
John Allena2488782017-07-24 13:26:06 -05004022 tasklet_schedule(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004023
4024 return 0;
4025}
4026
Arvind Yadav8c37bc62017-08-17 18:52:54 +05304027static const struct vio_device_id ibmvnic_device_table[] = {
Thomas Falcon032c5e82015-12-21 11:26:06 -06004028 {"network", "IBM,vnic"},
4029 {"", "" }
4030};
4031MODULE_DEVICE_TABLE(vio, ibmvnic_device_table);
4032
4033static const struct dev_pm_ops ibmvnic_pm_ops = {
4034 .resume = ibmvnic_resume
4035};
4036
4037static struct vio_driver ibmvnic_driver = {
4038 .id_table = ibmvnic_device_table,
4039 .probe = ibmvnic_probe,
4040 .remove = ibmvnic_remove,
4041 .get_desired_dma = ibmvnic_get_desired_dma,
4042 .name = ibmvnic_driver_name,
4043 .pm = &ibmvnic_pm_ops,
4044};
4045
4046/* module functions */
4047static int __init ibmvnic_module_init(void)
4048{
4049 pr_info("%s: %s %s\n", ibmvnic_driver_name, ibmvnic_driver_string,
4050 IBMVNIC_DRIVER_VERSION);
4051
4052 return vio_register_driver(&ibmvnic_driver);
4053}
4054
4055static void __exit ibmvnic_module_exit(void)
4056{
4057 vio_unregister_driver(&ibmvnic_driver);
4058}
4059
4060module_init(ibmvnic_module_init);
4061module_exit(ibmvnic_module_exit);