blob: b508877397e1427991ddf6833e72b8827a83d177 [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);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001207 data_dma_addr = tx_pool->long_term_buff.addr + offset;
1208
Thomas Falcon15482052017-10-17 12:36:54 -05001209 if (skb_shinfo(skb)->nr_frags) {
1210 int cur, i;
1211
1212 /* Copy the head */
1213 skb_copy_from_linear_data(skb, dst, skb_headlen(skb));
1214 cur = skb_headlen(skb);
1215
1216 /* Copy the frags */
1217 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1218 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1219
1220 memcpy(dst + cur,
1221 page_address(skb_frag_page(frag)) +
1222 frag->page_offset, skb_frag_size(frag));
1223 cur += skb_frag_size(frag);
1224 }
1225 } else {
1226 skb_copy_from_linear_data(skb, dst, skb->len);
1227 }
1228
Thomas Falcon032c5e82015-12-21 11:26:06 -06001229 tx_pool->consumer_index =
1230 (tx_pool->consumer_index + 1) %
Thomas Falcon068d9f92017-03-05 12:18:42 -06001231 adapter->req_tx_entries_per_subcrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001232
1233 tx_buff = &tx_pool->tx_buff[index];
1234 tx_buff->skb = skb;
1235 tx_buff->data_dma[0] = data_dma_addr;
1236 tx_buff->data_len[0] = skb->len;
1237 tx_buff->index = index;
1238 tx_buff->pool_index = queue_num;
1239 tx_buff->last_frag = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001240
1241 memset(&tx_crq, 0, sizeof(tx_crq));
1242 tx_crq.v1.first = IBMVNIC_CRQ_CMD;
1243 tx_crq.v1.type = IBMVNIC_TX_DESC;
1244 tx_crq.v1.n_crq_elem = 1;
1245 tx_crq.v1.n_sge = 1;
1246 tx_crq.v1.flags1 = IBMVNIC_TX_COMP_NEEDED;
1247 tx_crq.v1.correlator = cpu_to_be32(index);
1248 tx_crq.v1.dma_reg = cpu_to_be16(tx_pool->long_term_buff.map_id);
1249 tx_crq.v1.sge_len = cpu_to_be32(skb->len);
1250 tx_crq.v1.ioba = cpu_to_be64(data_dma_addr);
1251
1252 if (adapter->vlan_header_insertion) {
1253 tx_crq.v1.flags2 |= IBMVNIC_TX_VLAN_INSERT;
1254 tx_crq.v1.vlan_id = cpu_to_be16(skb->vlan_tci);
1255 }
1256
1257 if (skb->protocol == htons(ETH_P_IP)) {
1258 if (ip_hdr(skb)->version == 4)
1259 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV4;
1260 else if (ip_hdr(skb)->version == 6)
1261 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV6;
1262
1263 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
1264 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_TCP;
1265 else if (ip_hdr(skb)->protocol != IPPROTO_TCP)
1266 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_UDP;
1267 }
1268
Thomas Falconad7775d2016-04-01 17:20:34 -05001269 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06001270 tx_crq.v1.flags1 |= IBMVNIC_TX_CHKSUM_OFFLOAD;
Thomas Falconad7775d2016-04-01 17:20:34 -05001271 hdrs += 2;
1272 }
1273 /* determine if l2/3/4 headers are sent to firmware */
1274 if ((*hdrs >> 7) & 1 &&
1275 (skb->protocol == htons(ETH_P_IP) ||
1276 skb->protocol == htons(ETH_P_IPV6))) {
1277 build_hdr_descs_arr(tx_buff, &num_entries, *hdrs);
1278 tx_crq.v1.n_crq_elem = num_entries;
1279 tx_buff->indir_arr[0] = tx_crq;
1280 tx_buff->indir_dma = dma_map_single(dev, tx_buff->indir_arr,
1281 sizeof(tx_buff->indir_arr),
1282 DMA_TO_DEVICE);
1283 if (dma_mapping_error(dev, tx_buff->indir_dma)) {
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001284 dev_kfree_skb_any(skb);
1285 tx_buff->skb = NULL;
Thomas Falconad7775d2016-04-01 17:20:34 -05001286 if (!firmware_has_feature(FW_FEATURE_CMO))
1287 dev_err(dev, "tx: unable to map descriptor array\n");
1288 tx_map_failed++;
1289 tx_dropped++;
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001290 ret = NETDEV_TX_OK;
Thomas Falconad7775d2016-04-01 17:20:34 -05001291 goto out;
1292 }
John Allen498cd8e2016-04-06 11:49:55 -05001293 lpar_rc = send_subcrq_indirect(adapter, handle_array[queue_num],
Thomas Falconad7775d2016-04-01 17:20:34 -05001294 (u64)tx_buff->indir_dma,
1295 (u64)num_entries);
1296 } else {
John Allen498cd8e2016-04-06 11:49:55 -05001297 lpar_rc = send_subcrq(adapter, handle_array[queue_num],
1298 &tx_crq);
Thomas Falconad7775d2016-04-01 17:20:34 -05001299 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001300 if (lpar_rc != H_SUCCESS) {
1301 dev_err(dev, "tx failed with code %ld\n", lpar_rc);
1302
1303 if (tx_pool->consumer_index == 0)
1304 tx_pool->consumer_index =
Thomas Falcon068d9f92017-03-05 12:18:42 -06001305 adapter->req_tx_entries_per_subcrq - 1;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001306 else
1307 tx_pool->consumer_index--;
1308
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001309 dev_kfree_skb_any(skb);
1310 tx_buff->skb = NULL;
1311
Thomas Falconb8c80b82017-05-26 10:30:42 -04001312 if (lpar_rc == H_CLOSED) {
1313 /* Disable TX and report carrier off if queue is closed.
1314 * Firmware guarantees that a signal will be sent to the
1315 * driver, triggering a reset or some other action.
1316 */
1317 netif_tx_stop_all_queues(netdev);
1318 netif_carrier_off(netdev);
1319 }
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001320
Thomas Falcon032c5e82015-12-21 11:26:06 -06001321 tx_send_failed++;
1322 tx_dropped++;
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001323 ret = NETDEV_TX_OK;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001324 goto out;
1325 }
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001326
Brian King58c8c0c2017-04-19 13:44:47 -04001327 if (atomic_inc_return(&tx_scrq->used)
1328 >= adapter->req_tx_entries_per_subcrq) {
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001329 netdev_info(netdev, "Stopping queue %d\n", queue_num);
1330 netif_stop_subqueue(netdev, queue_num);
1331 }
1332
Thomas Falcon032c5e82015-12-21 11:26:06 -06001333 tx_packets++;
1334 tx_bytes += skb->len;
1335 txq->trans_start = jiffies;
1336 ret = NETDEV_TX_OK;
1337
1338out:
1339 netdev->stats.tx_dropped += tx_dropped;
1340 netdev->stats.tx_bytes += tx_bytes;
1341 netdev->stats.tx_packets += tx_packets;
1342 adapter->tx_send_failed += tx_send_failed;
1343 adapter->tx_map_failed += tx_map_failed;
John Allen3d52b592017-08-02 16:44:14 -05001344 adapter->tx_stats_buffers[queue_num].packets += tx_packets;
1345 adapter->tx_stats_buffers[queue_num].bytes += tx_bytes;
1346 adapter->tx_stats_buffers[queue_num].dropped_packets += tx_dropped;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001347
1348 return ret;
1349}
1350
1351static void ibmvnic_set_multi(struct net_device *netdev)
1352{
1353 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1354 struct netdev_hw_addr *ha;
1355 union ibmvnic_crq crq;
1356
1357 memset(&crq, 0, sizeof(crq));
1358 crq.request_capability.first = IBMVNIC_CRQ_CMD;
1359 crq.request_capability.cmd = REQUEST_CAPABILITY;
1360
1361 if (netdev->flags & IFF_PROMISC) {
1362 if (!adapter->promisc_supported)
1363 return;
1364 } else {
1365 if (netdev->flags & IFF_ALLMULTI) {
1366 /* Accept all multicast */
1367 memset(&crq, 0, sizeof(crq));
1368 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1369 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1370 crq.multicast_ctrl.flags = IBMVNIC_ENABLE_ALL;
1371 ibmvnic_send_crq(adapter, &crq);
1372 } else if (netdev_mc_empty(netdev)) {
1373 /* Reject all multicast */
1374 memset(&crq, 0, sizeof(crq));
1375 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1376 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1377 crq.multicast_ctrl.flags = IBMVNIC_DISABLE_ALL;
1378 ibmvnic_send_crq(adapter, &crq);
1379 } else {
1380 /* Accept one or more multicast(s) */
1381 netdev_for_each_mc_addr(ha, netdev) {
1382 memset(&crq, 0, sizeof(crq));
1383 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1384 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1385 crq.multicast_ctrl.flags = IBMVNIC_ENABLE_MC;
1386 ether_addr_copy(&crq.multicast_ctrl.mac_addr[0],
1387 ha->addr);
1388 ibmvnic_send_crq(adapter, &crq);
1389 }
1390 }
1391 }
1392}
1393
1394static int ibmvnic_set_mac(struct net_device *netdev, void *p)
1395{
1396 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1397 struct sockaddr *addr = p;
1398 union ibmvnic_crq crq;
1399
1400 if (!is_valid_ether_addr(addr->sa_data))
1401 return -EADDRNOTAVAIL;
1402
1403 memset(&crq, 0, sizeof(crq));
1404 crq.change_mac_addr.first = IBMVNIC_CRQ_CMD;
1405 crq.change_mac_addr.cmd = CHANGE_MAC_ADDR;
1406 ether_addr_copy(&crq.change_mac_addr.mac_addr[0], addr->sa_data);
1407 ibmvnic_send_crq(adapter, &crq);
1408 /* netdev->dev_addr is changed in handle_change_mac_rsp function */
1409 return 0;
1410}
1411
Nathan Fontenoted651a12017-05-03 14:04:38 -04001412/**
1413 * do_reset returns zero if we are able to keep processing reset events, or
1414 * non-zero if we hit a fatal error and must halt.
1415 */
1416static int do_reset(struct ibmvnic_adapter *adapter,
1417 struct ibmvnic_rwi *rwi, u32 reset_state)
1418{
1419 struct net_device *netdev = adapter->netdev;
1420 int i, rc;
1421
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001422 netdev_dbg(adapter->netdev, "Re-setting driver (%d)\n",
1423 rwi->reset_reason);
1424
Nathan Fontenoted651a12017-05-03 14:04:38 -04001425 netif_carrier_off(netdev);
1426 adapter->reset_reason = rwi->reset_reason;
1427
1428 if (rwi->reset_reason == VNIC_RESET_MOBILITY) {
1429 rc = ibmvnic_reenable_crq_queue(adapter);
1430 if (rc)
1431 return 0;
1432 }
1433
1434 rc = __ibmvnic_close(netdev);
1435 if (rc)
1436 return rc;
1437
John Allen8cb31cf2017-05-26 10:30:37 -04001438 if (adapter->reset_reason != VNIC_RESET_NON_FATAL) {
1439 /* remove the closed state so when we call open it appears
1440 * we are coming from the probed state.
1441 */
Nathan Fontenoted651a12017-05-03 14:04:38 -04001442 adapter->state = VNIC_PROBED;
John Allen8cb31cf2017-05-26 10:30:37 -04001443
John Allen8cb31cf2017-05-26 10:30:37 -04001444 rc = ibmvnic_init(adapter);
1445 if (rc)
1446 return 0;
1447
1448 /* If the adapter was in PROBE state prior to the reset,
1449 * exit here.
1450 */
1451 if (reset_state == VNIC_PROBED)
1452 return 0;
1453
1454 rc = ibmvnic_login(netdev);
1455 if (rc) {
1456 adapter->state = VNIC_PROBED;
1457 return 0;
1458 }
1459
Nathan Fontenot8c0543a2017-05-26 10:31:06 -04001460 rc = reset_tx_pools(adapter);
1461 if (rc)
1462 return rc;
1463
1464 rc = reset_rx_pools(adapter);
John Allen8cb31cf2017-05-26 10:30:37 -04001465 if (rc)
1466 return rc;
1467
1468 if (reset_state == VNIC_CLOSED)
1469 return 0;
Nathan Fontenoted651a12017-05-03 14:04:38 -04001470 }
1471
Nathan Fontenoted651a12017-05-03 14:04:38 -04001472 rc = __ibmvnic_open(netdev);
1473 if (rc) {
1474 if (list_empty(&adapter->rwi_list))
1475 adapter->state = VNIC_CLOSED;
1476 else
1477 adapter->state = reset_state;
1478
1479 return 0;
1480 }
1481
1482 netif_carrier_on(netdev);
1483
1484 /* kick napi */
1485 for (i = 0; i < adapter->req_rx_queues; i++)
1486 napi_schedule(&adapter->napi[i]);
1487
Nathan Fontenot61d3e1d2017-06-12 20:47:45 -04001488 if (adapter->reset_reason != VNIC_RESET_FAILOVER)
1489 netdev_notify_peers(netdev);
1490
Nathan Fontenoted651a12017-05-03 14:04:38 -04001491 return 0;
1492}
1493
1494static struct ibmvnic_rwi *get_next_rwi(struct ibmvnic_adapter *adapter)
1495{
1496 struct ibmvnic_rwi *rwi;
1497
1498 mutex_lock(&adapter->rwi_lock);
1499
1500 if (!list_empty(&adapter->rwi_list)) {
1501 rwi = list_first_entry(&adapter->rwi_list, struct ibmvnic_rwi,
1502 list);
1503 list_del(&rwi->list);
1504 } else {
1505 rwi = NULL;
1506 }
1507
1508 mutex_unlock(&adapter->rwi_lock);
1509 return rwi;
1510}
1511
1512static void free_all_rwi(struct ibmvnic_adapter *adapter)
1513{
1514 struct ibmvnic_rwi *rwi;
1515
1516 rwi = get_next_rwi(adapter);
1517 while (rwi) {
1518 kfree(rwi);
1519 rwi = get_next_rwi(adapter);
1520 }
1521}
1522
1523static void __ibmvnic_reset(struct work_struct *work)
1524{
1525 struct ibmvnic_rwi *rwi;
1526 struct ibmvnic_adapter *adapter;
1527 struct net_device *netdev;
1528 u32 reset_state;
1529 int rc;
1530
1531 adapter = container_of(work, struct ibmvnic_adapter, ibmvnic_reset);
1532 netdev = adapter->netdev;
1533
1534 mutex_lock(&adapter->reset_lock);
1535 adapter->resetting = true;
1536 reset_state = adapter->state;
1537
1538 rwi = get_next_rwi(adapter);
1539 while (rwi) {
1540 rc = do_reset(adapter, rwi, reset_state);
1541 kfree(rwi);
1542 if (rc)
1543 break;
1544
1545 rwi = get_next_rwi(adapter);
1546 }
1547
1548 if (rc) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001549 netdev_dbg(adapter->netdev, "Reset failed\n");
Nathan Fontenoted651a12017-05-03 14:04:38 -04001550 free_all_rwi(adapter);
Wei Yongjun6d0af072017-05-18 15:24:52 +00001551 mutex_unlock(&adapter->reset_lock);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001552 return;
1553 }
1554
1555 adapter->resetting = false;
1556 mutex_unlock(&adapter->reset_lock);
1557}
1558
1559static void ibmvnic_reset(struct ibmvnic_adapter *adapter,
1560 enum ibmvnic_reset_reason reason)
1561{
1562 struct ibmvnic_rwi *rwi, *tmp;
1563 struct net_device *netdev = adapter->netdev;
1564 struct list_head *entry;
1565
1566 if (adapter->state == VNIC_REMOVING ||
1567 adapter->state == VNIC_REMOVED) {
1568 netdev_dbg(netdev, "Adapter removing, skipping reset\n");
1569 return;
1570 }
1571
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04001572 if (adapter->state == VNIC_PROBING) {
1573 netdev_warn(netdev, "Adapter reset during probe\n");
1574 adapter->init_done_rc = EAGAIN;
1575 return;
1576 }
1577
Nathan Fontenoted651a12017-05-03 14:04:38 -04001578 mutex_lock(&adapter->rwi_lock);
1579
1580 list_for_each(entry, &adapter->rwi_list) {
1581 tmp = list_entry(entry, struct ibmvnic_rwi, list);
1582 if (tmp->reset_reason == reason) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001583 netdev_dbg(netdev, "Skipping matching reset\n");
Nathan Fontenoted651a12017-05-03 14:04:38 -04001584 mutex_unlock(&adapter->rwi_lock);
1585 return;
1586 }
1587 }
1588
1589 rwi = kzalloc(sizeof(*rwi), GFP_KERNEL);
1590 if (!rwi) {
1591 mutex_unlock(&adapter->rwi_lock);
1592 ibmvnic_close(netdev);
1593 return;
1594 }
1595
1596 rwi->reset_reason = reason;
1597 list_add_tail(&rwi->list, &adapter->rwi_list);
1598 mutex_unlock(&adapter->rwi_lock);
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001599
1600 netdev_dbg(adapter->netdev, "Scheduling reset (reason %d)\n", reason);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001601 schedule_work(&adapter->ibmvnic_reset);
1602}
1603
Thomas Falcon032c5e82015-12-21 11:26:06 -06001604static void ibmvnic_tx_timeout(struct net_device *dev)
1605{
1606 struct ibmvnic_adapter *adapter = netdev_priv(dev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001607
Nathan Fontenoted651a12017-05-03 14:04:38 -04001608 ibmvnic_reset(adapter, VNIC_RESET_TIMEOUT);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001609}
1610
1611static void remove_buff_from_pool(struct ibmvnic_adapter *adapter,
1612 struct ibmvnic_rx_buff *rx_buff)
1613{
1614 struct ibmvnic_rx_pool *pool = &adapter->rx_pool[rx_buff->pool_index];
1615
1616 rx_buff->skb = NULL;
1617
1618 pool->free_map[pool->next_alloc] = (int)(rx_buff - pool->rx_buff);
1619 pool->next_alloc = (pool->next_alloc + 1) % pool->size;
1620
1621 atomic_dec(&pool->available);
1622}
1623
1624static int ibmvnic_poll(struct napi_struct *napi, int budget)
1625{
1626 struct net_device *netdev = napi->dev;
1627 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1628 int scrq_num = (int)(napi - adapter->napi);
1629 int frames_processed = 0;
Nathan Fontenot152ce472017-05-26 10:30:54 -04001630
Thomas Falcon032c5e82015-12-21 11:26:06 -06001631restart_poll:
1632 while (frames_processed < budget) {
1633 struct sk_buff *skb;
1634 struct ibmvnic_rx_buff *rx_buff;
1635 union sub_crq *next;
1636 u32 length;
1637 u16 offset;
1638 u8 flags = 0;
1639
Thomas Falcon21ecba62017-06-14 23:50:09 -05001640 if (unlikely(adapter->resetting)) {
1641 enable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
1642 napi_complete_done(napi, frames_processed);
1643 return frames_processed;
1644 }
1645
Thomas Falcon032c5e82015-12-21 11:26:06 -06001646 if (!pending_scrq(adapter, adapter->rx_scrq[scrq_num]))
1647 break;
1648 next = ibmvnic_next_scrq(adapter, adapter->rx_scrq[scrq_num]);
1649 rx_buff =
1650 (struct ibmvnic_rx_buff *)be64_to_cpu(next->
1651 rx_comp.correlator);
1652 /* do error checking */
1653 if (next->rx_comp.rc) {
John Allene1cea2e2017-08-07 15:42:30 -05001654 netdev_dbg(netdev, "rx buffer returned with rc %x\n",
1655 be16_to_cpu(next->rx_comp.rc));
Thomas Falcon032c5e82015-12-21 11:26:06 -06001656 /* free the entry */
1657 next->rx_comp.first = 0;
1658 remove_buff_from_pool(adapter, rx_buff);
Nathan Fontenotca05e312017-05-03 14:05:14 -04001659 continue;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001660 }
1661
1662 length = be32_to_cpu(next->rx_comp.len);
1663 offset = be16_to_cpu(next->rx_comp.off_frame_data);
1664 flags = next->rx_comp.flags;
1665 skb = rx_buff->skb;
1666 skb_copy_to_linear_data(skb, rx_buff->data + offset,
1667 length);
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04001668
1669 /* VLAN Header has been stripped by the system firmware and
1670 * needs to be inserted by the driver
1671 */
1672 if (adapter->rx_vlan_header_insertion &&
1673 (flags & IBMVNIC_VLAN_STRIPPED))
1674 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
1675 ntohs(next->rx_comp.vlan_tci));
1676
Thomas Falcon032c5e82015-12-21 11:26:06 -06001677 /* free the entry */
1678 next->rx_comp.first = 0;
1679 remove_buff_from_pool(adapter, rx_buff);
1680
1681 skb_put(skb, length);
1682 skb->protocol = eth_type_trans(skb, netdev);
Thomas Falcon94ca3052017-05-03 14:05:20 -04001683 skb_record_rx_queue(skb, scrq_num);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001684
1685 if (flags & IBMVNIC_IP_CHKSUM_GOOD &&
1686 flags & IBMVNIC_TCP_UDP_CHKSUM_GOOD) {
1687 skb->ip_summed = CHECKSUM_UNNECESSARY;
1688 }
1689
1690 length = skb->len;
1691 napi_gro_receive(napi, skb); /* send it up */
1692 netdev->stats.rx_packets++;
1693 netdev->stats.rx_bytes += length;
John Allen3d52b592017-08-02 16:44:14 -05001694 adapter->rx_stats_buffers[scrq_num].packets++;
1695 adapter->rx_stats_buffers[scrq_num].bytes += length;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001696 frames_processed++;
1697 }
Nathan Fontenot152ce472017-05-26 10:30:54 -04001698
1699 if (adapter->state != VNIC_CLOSING)
1700 replenish_rx_pool(adapter, &adapter->rx_pool[scrq_num]);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001701
1702 if (frames_processed < budget) {
1703 enable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
Eric Dumazet6ad20162017-01-30 08:22:01 -08001704 napi_complete_done(napi, frames_processed);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001705 if (pending_scrq(adapter, adapter->rx_scrq[scrq_num]) &&
1706 napi_reschedule(napi)) {
1707 disable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
1708 goto restart_poll;
1709 }
1710 }
1711 return frames_processed;
1712}
1713
1714#ifdef CONFIG_NET_POLL_CONTROLLER
1715static void ibmvnic_netpoll_controller(struct net_device *dev)
1716{
1717 struct ibmvnic_adapter *adapter = netdev_priv(dev);
1718 int i;
1719
1720 replenish_pools(netdev_priv(dev));
1721 for (i = 0; i < adapter->req_rx_queues; i++)
1722 ibmvnic_interrupt_rx(adapter->rx_scrq[i]->irq,
1723 adapter->rx_scrq[i]);
1724}
1725#endif
1726
John Allen3a807b72017-06-06 16:55:52 -05001727static int ibmvnic_change_mtu(struct net_device *netdev, int new_mtu)
1728{
1729 return -EOPNOTSUPP;
1730}
1731
Thomas Falcon032c5e82015-12-21 11:26:06 -06001732static const struct net_device_ops ibmvnic_netdev_ops = {
1733 .ndo_open = ibmvnic_open,
1734 .ndo_stop = ibmvnic_close,
1735 .ndo_start_xmit = ibmvnic_xmit,
1736 .ndo_set_rx_mode = ibmvnic_set_multi,
1737 .ndo_set_mac_address = ibmvnic_set_mac,
1738 .ndo_validate_addr = eth_validate_addr,
Thomas Falcon032c5e82015-12-21 11:26:06 -06001739 .ndo_tx_timeout = ibmvnic_tx_timeout,
1740#ifdef CONFIG_NET_POLL_CONTROLLER
1741 .ndo_poll_controller = ibmvnic_netpoll_controller,
1742#endif
John Allen3a807b72017-06-06 16:55:52 -05001743 .ndo_change_mtu = ibmvnic_change_mtu,
Thomas Falcon032c5e82015-12-21 11:26:06 -06001744};
1745
1746/* ethtool functions */
1747
Philippe Reynes8a433792017-01-07 22:37:29 +01001748static int ibmvnic_get_link_ksettings(struct net_device *netdev,
1749 struct ethtool_link_ksettings *cmd)
Thomas Falcon032c5e82015-12-21 11:26:06 -06001750{
Philippe Reynes8a433792017-01-07 22:37:29 +01001751 u32 supported, advertising;
1752
1753 supported = (SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg |
Thomas Falcon032c5e82015-12-21 11:26:06 -06001754 SUPPORTED_FIBRE);
Philippe Reynes8a433792017-01-07 22:37:29 +01001755 advertising = (ADVERTISED_1000baseT_Full | ADVERTISED_Autoneg |
Thomas Falcon032c5e82015-12-21 11:26:06 -06001756 ADVERTISED_FIBRE);
Philippe Reynes8a433792017-01-07 22:37:29 +01001757 cmd->base.speed = SPEED_1000;
1758 cmd->base.duplex = DUPLEX_FULL;
1759 cmd->base.port = PORT_FIBRE;
1760 cmd->base.phy_address = 0;
1761 cmd->base.autoneg = AUTONEG_ENABLE;
1762
1763 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
1764 supported);
1765 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
1766 advertising);
1767
Thomas Falcon032c5e82015-12-21 11:26:06 -06001768 return 0;
1769}
1770
1771static void ibmvnic_get_drvinfo(struct net_device *dev,
1772 struct ethtool_drvinfo *info)
1773{
1774 strlcpy(info->driver, ibmvnic_driver_name, sizeof(info->driver));
1775 strlcpy(info->version, IBMVNIC_DRIVER_VERSION, sizeof(info->version));
1776}
1777
1778static u32 ibmvnic_get_msglevel(struct net_device *netdev)
1779{
1780 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1781
1782 return adapter->msg_enable;
1783}
1784
1785static void ibmvnic_set_msglevel(struct net_device *netdev, u32 data)
1786{
1787 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1788
1789 adapter->msg_enable = data;
1790}
1791
1792static u32 ibmvnic_get_link(struct net_device *netdev)
1793{
1794 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1795
1796 /* Don't need to send a query because we request a logical link up at
1797 * init and then we wait for link state indications
1798 */
1799 return adapter->logical_link_state;
1800}
1801
1802static void ibmvnic_get_ringparam(struct net_device *netdev,
1803 struct ethtool_ringparam *ring)
1804{
John Allenbc131b32017-08-02 16:46:30 -05001805 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1806
1807 ring->rx_max_pending = adapter->max_rx_add_entries_per_subcrq;
1808 ring->tx_max_pending = adapter->max_tx_entries_per_subcrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001809 ring->rx_mini_max_pending = 0;
1810 ring->rx_jumbo_max_pending = 0;
John Allenbc131b32017-08-02 16:46:30 -05001811 ring->rx_pending = adapter->req_rx_add_entries_per_subcrq;
1812 ring->tx_pending = adapter->req_tx_entries_per_subcrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001813 ring->rx_mini_pending = 0;
1814 ring->rx_jumbo_pending = 0;
1815}
1816
John Allenc2dbeb62017-08-02 16:47:17 -05001817static void ibmvnic_get_channels(struct net_device *netdev,
1818 struct ethtool_channels *channels)
1819{
1820 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1821
1822 channels->max_rx = adapter->max_rx_queues;
1823 channels->max_tx = adapter->max_tx_queues;
1824 channels->max_other = 0;
1825 channels->max_combined = 0;
1826 channels->rx_count = adapter->req_rx_queues;
1827 channels->tx_count = adapter->req_tx_queues;
1828 channels->other_count = 0;
1829 channels->combined_count = 0;
1830}
1831
Thomas Falcon032c5e82015-12-21 11:26:06 -06001832static void ibmvnic_get_strings(struct net_device *dev, u32 stringset, u8 *data)
1833{
John Allen3d52b592017-08-02 16:44:14 -05001834 struct ibmvnic_adapter *adapter = netdev_priv(dev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001835 int i;
1836
1837 if (stringset != ETH_SS_STATS)
1838 return;
1839
1840 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++, data += ETH_GSTRING_LEN)
1841 memcpy(data, ibmvnic_stats[i].name, ETH_GSTRING_LEN);
John Allen3d52b592017-08-02 16:44:14 -05001842
1843 for (i = 0; i < adapter->req_tx_queues; i++) {
1844 snprintf(data, ETH_GSTRING_LEN, "tx%d_packets", i);
1845 data += ETH_GSTRING_LEN;
1846
1847 snprintf(data, ETH_GSTRING_LEN, "tx%d_bytes", i);
1848 data += ETH_GSTRING_LEN;
1849
1850 snprintf(data, ETH_GSTRING_LEN, "tx%d_dropped_packets", i);
1851 data += ETH_GSTRING_LEN;
1852 }
1853
1854 for (i = 0; i < adapter->req_rx_queues; i++) {
1855 snprintf(data, ETH_GSTRING_LEN, "rx%d_packets", i);
1856 data += ETH_GSTRING_LEN;
1857
1858 snprintf(data, ETH_GSTRING_LEN, "rx%d_bytes", i);
1859 data += ETH_GSTRING_LEN;
1860
1861 snprintf(data, ETH_GSTRING_LEN, "rx%d_interrupts", i);
1862 data += ETH_GSTRING_LEN;
1863 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001864}
1865
1866static int ibmvnic_get_sset_count(struct net_device *dev, int sset)
1867{
John Allen3d52b592017-08-02 16:44:14 -05001868 struct ibmvnic_adapter *adapter = netdev_priv(dev);
1869
Thomas Falcon032c5e82015-12-21 11:26:06 -06001870 switch (sset) {
1871 case ETH_SS_STATS:
John Allen3d52b592017-08-02 16:44:14 -05001872 return ARRAY_SIZE(ibmvnic_stats) +
1873 adapter->req_tx_queues * NUM_TX_STATS +
1874 adapter->req_rx_queues * NUM_RX_STATS;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001875 default:
1876 return -EOPNOTSUPP;
1877 }
1878}
1879
1880static void ibmvnic_get_ethtool_stats(struct net_device *dev,
1881 struct ethtool_stats *stats, u64 *data)
1882{
1883 struct ibmvnic_adapter *adapter = netdev_priv(dev);
1884 union ibmvnic_crq crq;
John Allen3d52b592017-08-02 16:44:14 -05001885 int i, j;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001886
1887 memset(&crq, 0, sizeof(crq));
1888 crq.request_statistics.first = IBMVNIC_CRQ_CMD;
1889 crq.request_statistics.cmd = REQUEST_STATISTICS;
1890 crq.request_statistics.ioba = cpu_to_be32(adapter->stats_token);
1891 crq.request_statistics.len =
1892 cpu_to_be32(sizeof(struct ibmvnic_statistics));
Thomas Falcon032c5e82015-12-21 11:26:06 -06001893
1894 /* Wait for data to be written */
1895 init_completion(&adapter->stats_done);
Nathan Fontenotdb5d0b52017-02-10 13:45:05 -05001896 ibmvnic_send_crq(adapter, &crq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001897 wait_for_completion(&adapter->stats_done);
1898
1899 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++)
John Allen52da5c12017-08-02 16:45:28 -05001900 data[i] = be64_to_cpu(IBMVNIC_GET_STAT(adapter,
1901 ibmvnic_stats[i].offset));
John Allen3d52b592017-08-02 16:44:14 -05001902
1903 for (j = 0; j < adapter->req_tx_queues; j++) {
1904 data[i] = adapter->tx_stats_buffers[j].packets;
1905 i++;
1906 data[i] = adapter->tx_stats_buffers[j].bytes;
1907 i++;
1908 data[i] = adapter->tx_stats_buffers[j].dropped_packets;
1909 i++;
1910 }
1911
1912 for (j = 0; j < adapter->req_rx_queues; j++) {
1913 data[i] = adapter->rx_stats_buffers[j].packets;
1914 i++;
1915 data[i] = adapter->rx_stats_buffers[j].bytes;
1916 i++;
1917 data[i] = adapter->rx_stats_buffers[j].interrupts;
1918 i++;
1919 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001920}
1921
1922static const struct ethtool_ops ibmvnic_ethtool_ops = {
Thomas Falcon032c5e82015-12-21 11:26:06 -06001923 .get_drvinfo = ibmvnic_get_drvinfo,
1924 .get_msglevel = ibmvnic_get_msglevel,
1925 .set_msglevel = ibmvnic_set_msglevel,
1926 .get_link = ibmvnic_get_link,
1927 .get_ringparam = ibmvnic_get_ringparam,
John Allenc2dbeb62017-08-02 16:47:17 -05001928 .get_channels = ibmvnic_get_channels,
Thomas Falcon032c5e82015-12-21 11:26:06 -06001929 .get_strings = ibmvnic_get_strings,
1930 .get_sset_count = ibmvnic_get_sset_count,
1931 .get_ethtool_stats = ibmvnic_get_ethtool_stats,
Philippe Reynes8a433792017-01-07 22:37:29 +01001932 .get_link_ksettings = ibmvnic_get_link_ksettings,
Thomas Falcon032c5e82015-12-21 11:26:06 -06001933};
1934
1935/* Routines for managing CRQs/sCRQs */
1936
Nathan Fontenot57a49432017-05-26 10:31:12 -04001937static int reset_one_sub_crq_queue(struct ibmvnic_adapter *adapter,
1938 struct ibmvnic_sub_crq_queue *scrq)
1939{
1940 int rc;
1941
1942 if (scrq->irq) {
1943 free_irq(scrq->irq, scrq);
1944 irq_dispose_mapping(scrq->irq);
1945 scrq->irq = 0;
1946 }
1947
Thomas Falconc8b2ad02017-06-14 23:50:07 -05001948 memset(scrq->msgs, 0, 4 * PAGE_SIZE);
Nathan Fontenot57a49432017-05-26 10:31:12 -04001949 scrq->cur = 0;
1950
1951 rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
1952 4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
1953 return rc;
1954}
1955
1956static int reset_sub_crq_queues(struct ibmvnic_adapter *adapter)
1957{
1958 int i, rc;
1959
1960 for (i = 0; i < adapter->req_tx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001961 netdev_dbg(adapter->netdev, "Re-setting tx_scrq[%d]\n", i);
Nathan Fontenot57a49432017-05-26 10:31:12 -04001962 rc = reset_one_sub_crq_queue(adapter, adapter->tx_scrq[i]);
1963 if (rc)
1964 return rc;
1965 }
1966
1967 for (i = 0; i < adapter->req_rx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001968 netdev_dbg(adapter->netdev, "Re-setting rx_scrq[%d]\n", i);
Nathan Fontenot57a49432017-05-26 10:31:12 -04001969 rc = reset_one_sub_crq_queue(adapter, adapter->rx_scrq[i]);
1970 if (rc)
1971 return rc;
1972 }
1973
Nathan Fontenot57a49432017-05-26 10:31:12 -04001974 return rc;
1975}
1976
Thomas Falcon032c5e82015-12-21 11:26:06 -06001977static void release_sub_crq_queue(struct ibmvnic_adapter *adapter,
1978 struct ibmvnic_sub_crq_queue *scrq)
1979{
1980 struct device *dev = &adapter->vdev->dev;
1981 long rc;
1982
1983 netdev_dbg(adapter->netdev, "Releasing sub-CRQ\n");
1984
1985 /* Close the sub-crqs */
1986 do {
1987 rc = plpar_hcall_norets(H_FREE_SUB_CRQ,
1988 adapter->vdev->unit_address,
1989 scrq->crq_num);
1990 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
1991
Thomas Falconffa73852017-04-19 13:44:29 -04001992 if (rc) {
1993 netdev_err(adapter->netdev,
1994 "Failed to release sub-CRQ %16lx, rc = %ld\n",
1995 scrq->crq_num, rc);
1996 }
1997
Thomas Falcon032c5e82015-12-21 11:26:06 -06001998 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
1999 DMA_BIDIRECTIONAL);
2000 free_pages((unsigned long)scrq->msgs, 2);
2001 kfree(scrq);
2002}
2003
2004static struct ibmvnic_sub_crq_queue *init_sub_crq_queue(struct ibmvnic_adapter
2005 *adapter)
2006{
2007 struct device *dev = &adapter->vdev->dev;
2008 struct ibmvnic_sub_crq_queue *scrq;
2009 int rc;
2010
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04002011 scrq = kzalloc(sizeof(*scrq), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002012 if (!scrq)
2013 return NULL;
2014
Nathan Fontenot7f7adc52017-04-19 13:45:16 -04002015 scrq->msgs =
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04002016 (union sub_crq *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, 2);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002017 if (!scrq->msgs) {
2018 dev_warn(dev, "Couldn't allocate crq queue messages page\n");
2019 goto zero_page_failed;
2020 }
2021
2022 scrq->msg_token = dma_map_single(dev, scrq->msgs, 4 * PAGE_SIZE,
2023 DMA_BIDIRECTIONAL);
2024 if (dma_mapping_error(dev, scrq->msg_token)) {
2025 dev_warn(dev, "Couldn't map crq queue messages page\n");
2026 goto map_failed;
2027 }
2028
2029 rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
2030 4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
2031
2032 if (rc == H_RESOURCE)
2033 rc = ibmvnic_reset_crq(adapter);
2034
2035 if (rc == H_CLOSED) {
2036 dev_warn(dev, "Partner adapter not ready, waiting.\n");
2037 } else if (rc) {
2038 dev_warn(dev, "Error %d registering sub-crq\n", rc);
2039 goto reg_failed;
2040 }
2041
Thomas Falcon032c5e82015-12-21 11:26:06 -06002042 scrq->adapter = adapter;
2043 scrq->size = 4 * PAGE_SIZE / sizeof(*scrq->msgs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002044 spin_lock_init(&scrq->lock);
2045
2046 netdev_dbg(adapter->netdev,
2047 "sub-crq initialized, num %lx, hw_irq=%lx, irq=%x\n",
2048 scrq->crq_num, scrq->hw_irq, scrq->irq);
2049
2050 return scrq;
2051
Thomas Falcon032c5e82015-12-21 11:26:06 -06002052reg_failed:
2053 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
2054 DMA_BIDIRECTIONAL);
2055map_failed:
2056 free_pages((unsigned long)scrq->msgs, 2);
2057zero_page_failed:
2058 kfree(scrq);
2059
2060 return NULL;
2061}
2062
2063static void release_sub_crqs(struct ibmvnic_adapter *adapter)
2064{
2065 int i;
2066
2067 if (adapter->tx_scrq) {
Nathan Fontenotb5108882017-03-30 02:49:18 -04002068 for (i = 0; i < adapter->req_tx_queues; i++) {
2069 if (!adapter->tx_scrq[i])
2070 continue;
2071
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002072 netdev_dbg(adapter->netdev, "Releasing tx_scrq[%d]\n",
2073 i);
Nathan Fontenotb5108882017-03-30 02:49:18 -04002074 if (adapter->tx_scrq[i]->irq) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06002075 free_irq(adapter->tx_scrq[i]->irq,
2076 adapter->tx_scrq[i]);
Thomas Falcon88eb98a2016-07-06 15:35:16 -05002077 irq_dispose_mapping(adapter->tx_scrq[i]->irq);
Nathan Fontenotb5108882017-03-30 02:49:18 -04002078 adapter->tx_scrq[i]->irq = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002079 }
Nathan Fontenotb5108882017-03-30 02:49:18 -04002080
2081 release_sub_crq_queue(adapter, adapter->tx_scrq[i]);
2082 }
2083
Nathan Fontenot9501df32017-03-15 23:38:07 -04002084 kfree(adapter->tx_scrq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002085 adapter->tx_scrq = NULL;
2086 }
2087
2088 if (adapter->rx_scrq) {
Nathan Fontenotb5108882017-03-30 02:49:18 -04002089 for (i = 0; i < adapter->req_rx_queues; i++) {
2090 if (!adapter->rx_scrq[i])
2091 continue;
2092
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002093 netdev_dbg(adapter->netdev, "Releasing rx_scrq[%d]\n",
2094 i);
Nathan Fontenotb5108882017-03-30 02:49:18 -04002095 if (adapter->rx_scrq[i]->irq) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06002096 free_irq(adapter->rx_scrq[i]->irq,
2097 adapter->rx_scrq[i]);
Thomas Falcon88eb98a2016-07-06 15:35:16 -05002098 irq_dispose_mapping(adapter->rx_scrq[i]->irq);
Nathan Fontenotb5108882017-03-30 02:49:18 -04002099 adapter->rx_scrq[i]->irq = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002100 }
Nathan Fontenotb5108882017-03-30 02:49:18 -04002101
2102 release_sub_crq_queue(adapter, adapter->rx_scrq[i]);
2103 }
2104
Nathan Fontenot9501df32017-03-15 23:38:07 -04002105 kfree(adapter->rx_scrq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002106 adapter->rx_scrq = NULL;
2107 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002108}
2109
2110static int disable_scrq_irq(struct ibmvnic_adapter *adapter,
2111 struct ibmvnic_sub_crq_queue *scrq)
2112{
2113 struct device *dev = &adapter->vdev->dev;
2114 unsigned long rc;
2115
2116 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
2117 H_DISABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
2118 if (rc)
2119 dev_err(dev, "Couldn't disable scrq irq 0x%lx. rc=%ld\n",
2120 scrq->hw_irq, rc);
2121 return rc;
2122}
2123
2124static int enable_scrq_irq(struct ibmvnic_adapter *adapter,
2125 struct ibmvnic_sub_crq_queue *scrq)
2126{
2127 struct device *dev = &adapter->vdev->dev;
2128 unsigned long rc;
2129
2130 if (scrq->hw_irq > 0x100000000ULL) {
2131 dev_err(dev, "bad hw_irq = %lx\n", scrq->hw_irq);
2132 return 1;
2133 }
2134
2135 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
2136 H_ENABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
2137 if (rc)
2138 dev_err(dev, "Couldn't enable scrq irq 0x%lx. rc=%ld\n",
2139 scrq->hw_irq, rc);
2140 return rc;
2141}
2142
2143static int ibmvnic_complete_tx(struct ibmvnic_adapter *adapter,
2144 struct ibmvnic_sub_crq_queue *scrq)
2145{
2146 struct device *dev = &adapter->vdev->dev;
2147 struct ibmvnic_tx_buff *txbuff;
2148 union sub_crq *next;
2149 int index;
2150 int i, j;
Thomas Falconad7775d2016-04-01 17:20:34 -05002151 u8 first;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002152
2153restart_loop:
2154 while (pending_scrq(adapter, scrq)) {
2155 unsigned int pool = scrq->pool_index;
2156
2157 next = ibmvnic_next_scrq(adapter, scrq);
2158 for (i = 0; i < next->tx_comp.num_comps; i++) {
2159 if (next->tx_comp.rcs[i]) {
2160 dev_err(dev, "tx error %x\n",
2161 next->tx_comp.rcs[i]);
2162 continue;
2163 }
2164 index = be32_to_cpu(next->tx_comp.correlators[i]);
2165 txbuff = &adapter->tx_pool[pool].tx_buff[index];
2166
2167 for (j = 0; j < IBMVNIC_MAX_FRAGS_PER_CRQ; j++) {
2168 if (!txbuff->data_dma[j])
2169 continue;
2170
2171 txbuff->data_dma[j] = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002172 }
Thomas Falconad7775d2016-04-01 17:20:34 -05002173 /* if sub_crq was sent indirectly */
2174 first = txbuff->indir_arr[0].generic.first;
2175 if (first == IBMVNIC_CRQ_CMD) {
2176 dma_unmap_single(dev, txbuff->indir_dma,
2177 sizeof(txbuff->indir_arr),
2178 DMA_TO_DEVICE);
2179 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002180
Thomas Falcon142c0ac2017-03-05 12:18:41 -06002181 if (txbuff->last_frag) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06002182 dev_kfree_skb_any(txbuff->skb);
Nathan Fontenot7c3e7de2017-05-03 14:05:25 -04002183 txbuff->skb = NULL;
Thomas Falcon142c0ac2017-03-05 12:18:41 -06002184 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002185
2186 adapter->tx_pool[pool].free_map[adapter->tx_pool[pool].
2187 producer_index] = index;
2188 adapter->tx_pool[pool].producer_index =
2189 (adapter->tx_pool[pool].producer_index + 1) %
Thomas Falcon068d9f92017-03-05 12:18:42 -06002190 adapter->req_tx_entries_per_subcrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002191 }
2192 /* remove tx_comp scrq*/
2193 next->tx_comp.first = 0;
Nathan Fontenot7c3e7de2017-05-03 14:05:25 -04002194
2195 if (atomic_sub_return(next->tx_comp.num_comps, &scrq->used) <=
2196 (adapter->req_tx_entries_per_subcrq / 2) &&
2197 __netif_subqueue_stopped(adapter->netdev,
2198 scrq->pool_index)) {
2199 netif_wake_subqueue(adapter->netdev, scrq->pool_index);
2200 netdev_info(adapter->netdev, "Started queue %d\n",
2201 scrq->pool_index);
2202 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002203 }
2204
2205 enable_scrq_irq(adapter, scrq);
2206
2207 if (pending_scrq(adapter, scrq)) {
2208 disable_scrq_irq(adapter, scrq);
2209 goto restart_loop;
2210 }
2211
2212 return 0;
2213}
2214
2215static irqreturn_t ibmvnic_interrupt_tx(int irq, void *instance)
2216{
2217 struct ibmvnic_sub_crq_queue *scrq = instance;
2218 struct ibmvnic_adapter *adapter = scrq->adapter;
2219
2220 disable_scrq_irq(adapter, scrq);
2221 ibmvnic_complete_tx(adapter, scrq);
2222
2223 return IRQ_HANDLED;
2224}
2225
2226static irqreturn_t ibmvnic_interrupt_rx(int irq, void *instance)
2227{
2228 struct ibmvnic_sub_crq_queue *scrq = instance;
2229 struct ibmvnic_adapter *adapter = scrq->adapter;
2230
John Allen3d52b592017-08-02 16:44:14 -05002231 adapter->rx_stats_buffers[scrq->scrq_num].interrupts++;
2232
Thomas Falcon032c5e82015-12-21 11:26:06 -06002233 if (napi_schedule_prep(&adapter->napi[scrq->scrq_num])) {
2234 disable_scrq_irq(adapter, scrq);
2235 __napi_schedule(&adapter->napi[scrq->scrq_num]);
2236 }
2237
2238 return IRQ_HANDLED;
2239}
2240
Thomas Falconea22d512016-07-06 15:35:17 -05002241static int init_sub_crq_irqs(struct ibmvnic_adapter *adapter)
2242{
2243 struct device *dev = &adapter->vdev->dev;
2244 struct ibmvnic_sub_crq_queue *scrq;
2245 int i = 0, j = 0;
2246 int rc = 0;
2247
2248 for (i = 0; i < adapter->req_tx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002249 netdev_dbg(adapter->netdev, "Initializing tx_scrq[%d] irq\n",
2250 i);
Thomas Falconea22d512016-07-06 15:35:17 -05002251 scrq = adapter->tx_scrq[i];
2252 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
2253
Michael Ellerman99c17902016-09-10 19:59:05 +10002254 if (!scrq->irq) {
Thomas Falconea22d512016-07-06 15:35:17 -05002255 rc = -EINVAL;
2256 dev_err(dev, "Error mapping irq\n");
2257 goto req_tx_irq_failed;
2258 }
2259
2260 rc = request_irq(scrq->irq, ibmvnic_interrupt_tx,
2261 0, "ibmvnic_tx", scrq);
2262
2263 if (rc) {
2264 dev_err(dev, "Couldn't register tx irq 0x%x. rc=%d\n",
2265 scrq->irq, rc);
2266 irq_dispose_mapping(scrq->irq);
2267 goto req_rx_irq_failed;
2268 }
2269 }
2270
2271 for (i = 0; i < adapter->req_rx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002272 netdev_dbg(adapter->netdev, "Initializing rx_scrq[%d] irq\n",
2273 i);
Thomas Falconea22d512016-07-06 15:35:17 -05002274 scrq = adapter->rx_scrq[i];
2275 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
Michael Ellerman99c17902016-09-10 19:59:05 +10002276 if (!scrq->irq) {
Thomas Falconea22d512016-07-06 15:35:17 -05002277 rc = -EINVAL;
2278 dev_err(dev, "Error mapping irq\n");
2279 goto req_rx_irq_failed;
2280 }
2281 rc = request_irq(scrq->irq, ibmvnic_interrupt_rx,
2282 0, "ibmvnic_rx", scrq);
2283 if (rc) {
2284 dev_err(dev, "Couldn't register rx irq 0x%x. rc=%d\n",
2285 scrq->irq, rc);
2286 irq_dispose_mapping(scrq->irq);
2287 goto req_rx_irq_failed;
2288 }
2289 }
2290 return rc;
2291
2292req_rx_irq_failed:
Thomas Falcon8bf371e2016-10-27 12:28:52 -05002293 for (j = 0; j < i; j++) {
Thomas Falconea22d512016-07-06 15:35:17 -05002294 free_irq(adapter->rx_scrq[j]->irq, adapter->rx_scrq[j]);
2295 irq_dispose_mapping(adapter->rx_scrq[j]->irq);
Thomas Falcon8bf371e2016-10-27 12:28:52 -05002296 }
Thomas Falconea22d512016-07-06 15:35:17 -05002297 i = adapter->req_tx_queues;
2298req_tx_irq_failed:
Thomas Falcon8bf371e2016-10-27 12:28:52 -05002299 for (j = 0; j < i; j++) {
Thomas Falconea22d512016-07-06 15:35:17 -05002300 free_irq(adapter->tx_scrq[j]->irq, adapter->tx_scrq[j]);
2301 irq_dispose_mapping(adapter->rx_scrq[j]->irq);
Thomas Falcon8bf371e2016-10-27 12:28:52 -05002302 }
Nathan Fontenotb5108882017-03-30 02:49:18 -04002303 release_sub_crqs(adapter);
Thomas Falconea22d512016-07-06 15:35:17 -05002304 return rc;
2305}
2306
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002307static int init_sub_crqs(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002308{
2309 struct device *dev = &adapter->vdev->dev;
2310 struct ibmvnic_sub_crq_queue **allqueues;
2311 int registered_queues = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002312 int total_queues;
2313 int more = 0;
Thomas Falconea22d512016-07-06 15:35:17 -05002314 int i;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002315
Thomas Falcon032c5e82015-12-21 11:26:06 -06002316 total_queues = adapter->req_tx_queues + adapter->req_rx_queues;
2317
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04002318 allqueues = kcalloc(total_queues, sizeof(*allqueues), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002319 if (!allqueues)
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002320 return -1;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002321
2322 for (i = 0; i < total_queues; i++) {
2323 allqueues[i] = init_sub_crq_queue(adapter);
2324 if (!allqueues[i]) {
2325 dev_warn(dev, "Couldn't allocate all sub-crqs\n");
2326 break;
2327 }
2328 registered_queues++;
2329 }
2330
2331 /* Make sure we were able to register the minimum number of queues */
2332 if (registered_queues <
2333 adapter->min_tx_queues + adapter->min_rx_queues) {
2334 dev_err(dev, "Fatal: Couldn't init min number of sub-crqs\n");
2335 goto tx_failed;
2336 }
2337
2338 /* Distribute the failed allocated queues*/
2339 for (i = 0; i < total_queues - registered_queues + more ; i++) {
2340 netdev_dbg(adapter->netdev, "Reducing number of queues\n");
2341 switch (i % 3) {
2342 case 0:
2343 if (adapter->req_rx_queues > adapter->min_rx_queues)
2344 adapter->req_rx_queues--;
2345 else
2346 more++;
2347 break;
2348 case 1:
2349 if (adapter->req_tx_queues > adapter->min_tx_queues)
2350 adapter->req_tx_queues--;
2351 else
2352 more++;
2353 break;
2354 }
2355 }
2356
2357 adapter->tx_scrq = kcalloc(adapter->req_tx_queues,
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04002358 sizeof(*adapter->tx_scrq), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002359 if (!adapter->tx_scrq)
2360 goto tx_failed;
2361
2362 for (i = 0; i < adapter->req_tx_queues; i++) {
2363 adapter->tx_scrq[i] = allqueues[i];
2364 adapter->tx_scrq[i]->pool_index = i;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002365 }
2366
2367 adapter->rx_scrq = kcalloc(adapter->req_rx_queues,
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04002368 sizeof(*adapter->rx_scrq), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002369 if (!adapter->rx_scrq)
2370 goto rx_failed;
2371
2372 for (i = 0; i < adapter->req_rx_queues; i++) {
2373 adapter->rx_scrq[i] = allqueues[i + adapter->req_tx_queues];
2374 adapter->rx_scrq[i]->scrq_num = i;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002375 }
2376
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002377 kfree(allqueues);
2378 return 0;
2379
2380rx_failed:
2381 kfree(adapter->tx_scrq);
2382 adapter->tx_scrq = NULL;
2383tx_failed:
2384 for (i = 0; i < registered_queues; i++)
2385 release_sub_crq_queue(adapter, allqueues[i]);
2386 kfree(allqueues);
2387 return -1;
2388}
2389
2390static void ibmvnic_send_req_caps(struct ibmvnic_adapter *adapter, int retry)
2391{
2392 struct device *dev = &adapter->vdev->dev;
2393 union ibmvnic_crq crq;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002394
2395 if (!retry) {
2396 /* Sub-CRQ entries are 32 byte long */
2397 int entries_page = 4 * PAGE_SIZE / (sizeof(u64) * 4);
2398
2399 if (adapter->min_tx_entries_per_subcrq > entries_page ||
2400 adapter->min_rx_add_entries_per_subcrq > entries_page) {
2401 dev_err(dev, "Fatal, invalid entries per sub-crq\n");
2402 return;
2403 }
2404
2405 /* Get the minimum between the queried max and the entries
2406 * that fit in our PAGE_SIZE
2407 */
2408 adapter->req_tx_entries_per_subcrq =
2409 adapter->max_tx_entries_per_subcrq > entries_page ?
2410 entries_page : adapter->max_tx_entries_per_subcrq;
2411 adapter->req_rx_add_entries_per_subcrq =
2412 adapter->max_rx_add_entries_per_subcrq > entries_page ?
2413 entries_page : adapter->max_rx_add_entries_per_subcrq;
2414
2415 adapter->req_tx_queues = adapter->opt_tx_comp_sub_queues;
2416 adapter->req_rx_queues = adapter->opt_rx_comp_queues;
2417 adapter->req_rx_add_queues = adapter->max_rx_add_queues;
2418
2419 adapter->req_mtu = adapter->netdev->mtu + ETH_HLEN;
2420 }
2421
Thomas Falcon032c5e82015-12-21 11:26:06 -06002422 memset(&crq, 0, sizeof(crq));
2423 crq.request_capability.first = IBMVNIC_CRQ_CMD;
2424 crq.request_capability.cmd = REQUEST_CAPABILITY;
2425
2426 crq.request_capability.capability = cpu_to_be16(REQ_TX_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06002427 crq.request_capability.number = cpu_to_be64(adapter->req_tx_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06002428 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002429 ibmvnic_send_crq(adapter, &crq);
2430
2431 crq.request_capability.capability = cpu_to_be16(REQ_RX_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06002432 crq.request_capability.number = cpu_to_be64(adapter->req_rx_queues);
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_RX_ADD_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06002437 crq.request_capability.number = cpu_to_be64(adapter->req_rx_add_queues);
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 crq.request_capability.capability =
2442 cpu_to_be16(REQ_TX_ENTRIES_PER_SUBCRQ);
2443 crq.request_capability.number =
Thomas Falconde89e852016-03-01 10:20:09 -06002444 cpu_to_be64(adapter->req_tx_entries_per_subcrq);
Thomas Falcon901e0402017-02-15 12:17:59 -06002445 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002446 ibmvnic_send_crq(adapter, &crq);
2447
2448 crq.request_capability.capability =
2449 cpu_to_be16(REQ_RX_ADD_ENTRIES_PER_SUBCRQ);
2450 crq.request_capability.number =
Thomas Falconde89e852016-03-01 10:20:09 -06002451 cpu_to_be64(adapter->req_rx_add_entries_per_subcrq);
Thomas Falcon901e0402017-02-15 12:17:59 -06002452 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002453 ibmvnic_send_crq(adapter, &crq);
2454
2455 crq.request_capability.capability = cpu_to_be16(REQ_MTU);
Thomas Falconde89e852016-03-01 10:20:09 -06002456 crq.request_capability.number = cpu_to_be64(adapter->req_mtu);
Thomas Falcon901e0402017-02-15 12:17:59 -06002457 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002458 ibmvnic_send_crq(adapter, &crq);
2459
2460 if (adapter->netdev->flags & IFF_PROMISC) {
2461 if (adapter->promisc_supported) {
2462 crq.request_capability.capability =
2463 cpu_to_be16(PROMISC_REQUESTED);
Thomas Falconde89e852016-03-01 10:20:09 -06002464 crq.request_capability.number = cpu_to_be64(1);
Thomas Falcon901e0402017-02-15 12:17:59 -06002465 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002466 ibmvnic_send_crq(adapter, &crq);
2467 }
2468 } else {
2469 crq.request_capability.capability =
2470 cpu_to_be16(PROMISC_REQUESTED);
Thomas Falconde89e852016-03-01 10:20:09 -06002471 crq.request_capability.number = cpu_to_be64(0);
Thomas Falcon901e0402017-02-15 12:17:59 -06002472 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002473 ibmvnic_send_crq(adapter, &crq);
2474 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002475}
2476
2477static int pending_scrq(struct ibmvnic_adapter *adapter,
2478 struct ibmvnic_sub_crq_queue *scrq)
2479{
2480 union sub_crq *entry = &scrq->msgs[scrq->cur];
2481
Thomas Falcon1cf9cc72017-06-14 23:50:08 -05002482 if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002483 return 1;
2484 else
2485 return 0;
2486}
2487
2488static union sub_crq *ibmvnic_next_scrq(struct ibmvnic_adapter *adapter,
2489 struct ibmvnic_sub_crq_queue *scrq)
2490{
2491 union sub_crq *entry;
2492 unsigned long flags;
2493
2494 spin_lock_irqsave(&scrq->lock, flags);
2495 entry = &scrq->msgs[scrq->cur];
2496 if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP) {
2497 if (++scrq->cur == scrq->size)
2498 scrq->cur = 0;
2499 } else {
2500 entry = NULL;
2501 }
2502 spin_unlock_irqrestore(&scrq->lock, flags);
2503
2504 return entry;
2505}
2506
2507static union ibmvnic_crq *ibmvnic_next_crq(struct ibmvnic_adapter *adapter)
2508{
2509 struct ibmvnic_crq_queue *queue = &adapter->crq;
2510 union ibmvnic_crq *crq;
2511
2512 crq = &queue->msgs[queue->cur];
2513 if (crq->generic.first & IBMVNIC_CRQ_CMD_RSP) {
2514 if (++queue->cur == queue->size)
2515 queue->cur = 0;
2516 } else {
2517 crq = NULL;
2518 }
2519
2520 return crq;
2521}
2522
2523static int send_subcrq(struct ibmvnic_adapter *adapter, u64 remote_handle,
2524 union sub_crq *sub_crq)
2525{
2526 unsigned int ua = adapter->vdev->unit_address;
2527 struct device *dev = &adapter->vdev->dev;
2528 u64 *u64_crq = (u64 *)sub_crq;
2529 int rc;
2530
2531 netdev_dbg(adapter->netdev,
2532 "Sending sCRQ %016lx: %016lx %016lx %016lx %016lx\n",
2533 (unsigned long int)cpu_to_be64(remote_handle),
2534 (unsigned long int)cpu_to_be64(u64_crq[0]),
2535 (unsigned long int)cpu_to_be64(u64_crq[1]),
2536 (unsigned long int)cpu_to_be64(u64_crq[2]),
2537 (unsigned long int)cpu_to_be64(u64_crq[3]));
2538
2539 /* Make sure the hypervisor sees the complete request */
2540 mb();
2541
2542 rc = plpar_hcall_norets(H_SEND_SUB_CRQ, ua,
2543 cpu_to_be64(remote_handle),
2544 cpu_to_be64(u64_crq[0]),
2545 cpu_to_be64(u64_crq[1]),
2546 cpu_to_be64(u64_crq[2]),
2547 cpu_to_be64(u64_crq[3]));
2548
2549 if (rc) {
2550 if (rc == H_CLOSED)
2551 dev_warn(dev, "CRQ Queue closed\n");
2552 dev_err(dev, "Send error (rc=%d)\n", rc);
2553 }
2554
2555 return rc;
2556}
2557
Thomas Falconad7775d2016-04-01 17:20:34 -05002558static int send_subcrq_indirect(struct ibmvnic_adapter *adapter,
2559 u64 remote_handle, u64 ioba, u64 num_entries)
2560{
2561 unsigned int ua = adapter->vdev->unit_address;
2562 struct device *dev = &adapter->vdev->dev;
2563 int rc;
2564
2565 /* Make sure the hypervisor sees the complete request */
2566 mb();
2567 rc = plpar_hcall_norets(H_SEND_SUB_CRQ_INDIRECT, ua,
2568 cpu_to_be64(remote_handle),
2569 ioba, num_entries);
2570
2571 if (rc) {
2572 if (rc == H_CLOSED)
2573 dev_warn(dev, "CRQ Queue closed\n");
2574 dev_err(dev, "Send (indirect) error (rc=%d)\n", rc);
2575 }
2576
2577 return rc;
2578}
2579
Thomas Falcon032c5e82015-12-21 11:26:06 -06002580static int ibmvnic_send_crq(struct ibmvnic_adapter *adapter,
2581 union ibmvnic_crq *crq)
2582{
2583 unsigned int ua = adapter->vdev->unit_address;
2584 struct device *dev = &adapter->vdev->dev;
2585 u64 *u64_crq = (u64 *)crq;
2586 int rc;
2587
2588 netdev_dbg(adapter->netdev, "Sending CRQ: %016lx %016lx\n",
2589 (unsigned long int)cpu_to_be64(u64_crq[0]),
2590 (unsigned long int)cpu_to_be64(u64_crq[1]));
2591
2592 /* Make sure the hypervisor sees the complete request */
2593 mb();
2594
2595 rc = plpar_hcall_norets(H_SEND_CRQ, ua,
2596 cpu_to_be64(u64_crq[0]),
2597 cpu_to_be64(u64_crq[1]));
2598
2599 if (rc) {
2600 if (rc == H_CLOSED)
2601 dev_warn(dev, "CRQ Queue closed\n");
2602 dev_warn(dev, "Send error (rc=%d)\n", rc);
2603 }
2604
2605 return rc;
2606}
2607
2608static int ibmvnic_send_crq_init(struct ibmvnic_adapter *adapter)
2609{
2610 union ibmvnic_crq crq;
2611
2612 memset(&crq, 0, sizeof(crq));
2613 crq.generic.first = IBMVNIC_CRQ_INIT_CMD;
2614 crq.generic.cmd = IBMVNIC_CRQ_INIT;
2615 netdev_dbg(adapter->netdev, "Sending CRQ init\n");
2616
2617 return ibmvnic_send_crq(adapter, &crq);
2618}
2619
Thomas Falcon032c5e82015-12-21 11:26:06 -06002620static int send_version_xchg(struct ibmvnic_adapter *adapter)
2621{
2622 union ibmvnic_crq crq;
2623
2624 memset(&crq, 0, sizeof(crq));
2625 crq.version_exchange.first = IBMVNIC_CRQ_CMD;
2626 crq.version_exchange.cmd = VERSION_EXCHANGE;
2627 crq.version_exchange.version = cpu_to_be16(ibmvnic_version);
2628
2629 return ibmvnic_send_crq(adapter, &crq);
2630}
2631
2632static void send_login(struct ibmvnic_adapter *adapter)
2633{
2634 struct ibmvnic_login_rsp_buffer *login_rsp_buffer;
2635 struct ibmvnic_login_buffer *login_buffer;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002636 struct device *dev = &adapter->vdev->dev;
2637 dma_addr_t rsp_buffer_token;
2638 dma_addr_t buffer_token;
2639 size_t rsp_buffer_size;
2640 union ibmvnic_crq crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002641 size_t buffer_size;
2642 __be64 *tx_list_p;
2643 __be64 *rx_list_p;
2644 int i;
2645
2646 buffer_size =
2647 sizeof(struct ibmvnic_login_buffer) +
2648 sizeof(u64) * (adapter->req_tx_queues + adapter->req_rx_queues);
2649
2650 login_buffer = kmalloc(buffer_size, GFP_ATOMIC);
2651 if (!login_buffer)
2652 goto buf_alloc_failed;
2653
2654 buffer_token = dma_map_single(dev, login_buffer, buffer_size,
2655 DMA_TO_DEVICE);
2656 if (dma_mapping_error(dev, buffer_token)) {
2657 dev_err(dev, "Couldn't map login buffer\n");
2658 goto buf_map_failed;
2659 }
2660
John Allen498cd8e2016-04-06 11:49:55 -05002661 rsp_buffer_size = sizeof(struct ibmvnic_login_rsp_buffer) +
2662 sizeof(u64) * adapter->req_tx_queues +
2663 sizeof(u64) * adapter->req_rx_queues +
2664 sizeof(u64) * adapter->req_rx_queues +
2665 sizeof(u8) * IBMVNIC_TX_DESC_VERSIONS;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002666
2667 login_rsp_buffer = kmalloc(rsp_buffer_size, GFP_ATOMIC);
2668 if (!login_rsp_buffer)
2669 goto buf_rsp_alloc_failed;
2670
2671 rsp_buffer_token = dma_map_single(dev, login_rsp_buffer,
2672 rsp_buffer_size, DMA_FROM_DEVICE);
2673 if (dma_mapping_error(dev, rsp_buffer_token)) {
2674 dev_err(dev, "Couldn't map login rsp buffer\n");
2675 goto buf_rsp_map_failed;
2676 }
Nathan Fontenot661a2622017-04-19 13:44:58 -04002677
Thomas Falcon032c5e82015-12-21 11:26:06 -06002678 adapter->login_buf = login_buffer;
2679 adapter->login_buf_token = buffer_token;
2680 adapter->login_buf_sz = buffer_size;
2681 adapter->login_rsp_buf = login_rsp_buffer;
2682 adapter->login_rsp_buf_token = rsp_buffer_token;
2683 adapter->login_rsp_buf_sz = rsp_buffer_size;
2684
2685 login_buffer->len = cpu_to_be32(buffer_size);
2686 login_buffer->version = cpu_to_be32(INITIAL_VERSION_LB);
2687 login_buffer->num_txcomp_subcrqs = cpu_to_be32(adapter->req_tx_queues);
2688 login_buffer->off_txcomp_subcrqs =
2689 cpu_to_be32(sizeof(struct ibmvnic_login_buffer));
2690 login_buffer->num_rxcomp_subcrqs = cpu_to_be32(adapter->req_rx_queues);
2691 login_buffer->off_rxcomp_subcrqs =
2692 cpu_to_be32(sizeof(struct ibmvnic_login_buffer) +
2693 sizeof(u64) * adapter->req_tx_queues);
2694 login_buffer->login_rsp_ioba = cpu_to_be32(rsp_buffer_token);
2695 login_buffer->login_rsp_len = cpu_to_be32(rsp_buffer_size);
2696
2697 tx_list_p = (__be64 *)((char *)login_buffer +
2698 sizeof(struct ibmvnic_login_buffer));
2699 rx_list_p = (__be64 *)((char *)login_buffer +
2700 sizeof(struct ibmvnic_login_buffer) +
2701 sizeof(u64) * adapter->req_tx_queues);
2702
2703 for (i = 0; i < adapter->req_tx_queues; i++) {
2704 if (adapter->tx_scrq[i]) {
2705 tx_list_p[i] = cpu_to_be64(adapter->tx_scrq[i]->
2706 crq_num);
2707 }
2708 }
2709
2710 for (i = 0; i < adapter->req_rx_queues; i++) {
2711 if (adapter->rx_scrq[i]) {
2712 rx_list_p[i] = cpu_to_be64(adapter->rx_scrq[i]->
2713 crq_num);
2714 }
2715 }
2716
2717 netdev_dbg(adapter->netdev, "Login Buffer:\n");
2718 for (i = 0; i < (adapter->login_buf_sz - 1) / 8 + 1; i++) {
2719 netdev_dbg(adapter->netdev, "%016lx\n",
2720 ((unsigned long int *)(adapter->login_buf))[i]);
2721 }
2722
2723 memset(&crq, 0, sizeof(crq));
2724 crq.login.first = IBMVNIC_CRQ_CMD;
2725 crq.login.cmd = LOGIN;
2726 crq.login.ioba = cpu_to_be32(buffer_token);
2727 crq.login.len = cpu_to_be32(buffer_size);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002728 ibmvnic_send_crq(adapter, &crq);
2729
2730 return;
2731
Thomas Falcon032c5e82015-12-21 11:26:06 -06002732buf_rsp_map_failed:
2733 kfree(login_rsp_buffer);
2734buf_rsp_alloc_failed:
2735 dma_unmap_single(dev, buffer_token, buffer_size, DMA_TO_DEVICE);
2736buf_map_failed:
2737 kfree(login_buffer);
2738buf_alloc_failed:
2739 return;
2740}
2741
2742static void send_request_map(struct ibmvnic_adapter *adapter, dma_addr_t addr,
2743 u32 len, u8 map_id)
2744{
2745 union ibmvnic_crq crq;
2746
2747 memset(&crq, 0, sizeof(crq));
2748 crq.request_map.first = IBMVNIC_CRQ_CMD;
2749 crq.request_map.cmd = REQUEST_MAP;
2750 crq.request_map.map_id = map_id;
2751 crq.request_map.ioba = cpu_to_be32(addr);
2752 crq.request_map.len = cpu_to_be32(len);
2753 ibmvnic_send_crq(adapter, &crq);
2754}
2755
2756static void send_request_unmap(struct ibmvnic_adapter *adapter, u8 map_id)
2757{
2758 union ibmvnic_crq crq;
2759
2760 memset(&crq, 0, sizeof(crq));
2761 crq.request_unmap.first = IBMVNIC_CRQ_CMD;
2762 crq.request_unmap.cmd = REQUEST_UNMAP;
2763 crq.request_unmap.map_id = map_id;
2764 ibmvnic_send_crq(adapter, &crq);
2765}
2766
2767static void send_map_query(struct ibmvnic_adapter *adapter)
2768{
2769 union ibmvnic_crq crq;
2770
2771 memset(&crq, 0, sizeof(crq));
2772 crq.query_map.first = IBMVNIC_CRQ_CMD;
2773 crq.query_map.cmd = QUERY_MAP;
2774 ibmvnic_send_crq(adapter, &crq);
2775}
2776
2777/* Send a series of CRQs requesting various capabilities of the VNIC server */
2778static void send_cap_queries(struct ibmvnic_adapter *adapter)
2779{
2780 union ibmvnic_crq crq;
2781
Thomas Falcon901e0402017-02-15 12:17:59 -06002782 atomic_set(&adapter->running_cap_crqs, 0);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002783 memset(&crq, 0, sizeof(crq));
2784 crq.query_capability.first = IBMVNIC_CRQ_CMD;
2785 crq.query_capability.cmd = QUERY_CAPABILITY;
2786
2787 crq.query_capability.capability = cpu_to_be16(MIN_TX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002788 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002789 ibmvnic_send_crq(adapter, &crq);
2790
2791 crq.query_capability.capability = cpu_to_be16(MIN_RX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002792 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002793 ibmvnic_send_crq(adapter, &crq);
2794
2795 crq.query_capability.capability = cpu_to_be16(MIN_RX_ADD_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002796 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002797 ibmvnic_send_crq(adapter, &crq);
2798
2799 crq.query_capability.capability = cpu_to_be16(MAX_TX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002800 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002801 ibmvnic_send_crq(adapter, &crq);
2802
2803 crq.query_capability.capability = cpu_to_be16(MAX_RX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002804 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002805 ibmvnic_send_crq(adapter, &crq);
2806
2807 crq.query_capability.capability = cpu_to_be16(MAX_RX_ADD_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002808 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002809 ibmvnic_send_crq(adapter, &crq);
2810
2811 crq.query_capability.capability =
2812 cpu_to_be16(MIN_TX_ENTRIES_PER_SUBCRQ);
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 =
2817 cpu_to_be16(MIN_RX_ADD_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06002818 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002819 ibmvnic_send_crq(adapter, &crq);
2820
2821 crq.query_capability.capability =
2822 cpu_to_be16(MAX_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06002823 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002824 ibmvnic_send_crq(adapter, &crq);
2825
2826 crq.query_capability.capability =
2827 cpu_to_be16(MAX_RX_ADD_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06002828 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002829 ibmvnic_send_crq(adapter, &crq);
2830
2831 crq.query_capability.capability = cpu_to_be16(TCP_IP_OFFLOAD);
Thomas Falcon901e0402017-02-15 12:17:59 -06002832 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002833 ibmvnic_send_crq(adapter, &crq);
2834
2835 crq.query_capability.capability = cpu_to_be16(PROMISC_SUPPORTED);
Thomas Falcon901e0402017-02-15 12:17:59 -06002836 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002837 ibmvnic_send_crq(adapter, &crq);
2838
2839 crq.query_capability.capability = cpu_to_be16(MIN_MTU);
Thomas Falcon901e0402017-02-15 12:17:59 -06002840 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002841 ibmvnic_send_crq(adapter, &crq);
2842
2843 crq.query_capability.capability = cpu_to_be16(MAX_MTU);
Thomas Falcon901e0402017-02-15 12:17:59 -06002844 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002845 ibmvnic_send_crq(adapter, &crq);
2846
2847 crq.query_capability.capability = cpu_to_be16(MAX_MULTICAST_FILTERS);
Thomas Falcon901e0402017-02-15 12:17:59 -06002848 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002849 ibmvnic_send_crq(adapter, &crq);
2850
2851 crq.query_capability.capability = cpu_to_be16(VLAN_HEADER_INSERTION);
Thomas Falcon901e0402017-02-15 12:17:59 -06002852 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002853 ibmvnic_send_crq(adapter, &crq);
2854
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04002855 crq.query_capability.capability = cpu_to_be16(RX_VLAN_HEADER_INSERTION);
2856 atomic_inc(&adapter->running_cap_crqs);
2857 ibmvnic_send_crq(adapter, &crq);
2858
Thomas Falcon032c5e82015-12-21 11:26:06 -06002859 crq.query_capability.capability = cpu_to_be16(MAX_TX_SG_ENTRIES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002860 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002861 ibmvnic_send_crq(adapter, &crq);
2862
2863 crq.query_capability.capability = cpu_to_be16(RX_SG_SUPPORTED);
Thomas Falcon901e0402017-02-15 12:17:59 -06002864 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002865 ibmvnic_send_crq(adapter, &crq);
2866
2867 crq.query_capability.capability = cpu_to_be16(OPT_TX_COMP_SUB_QUEUES);
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(OPT_RX_COMP_QUEUES);
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 crq.query_capability.capability =
2876 cpu_to_be16(OPT_RX_BUFADD_Q_PER_RX_COMP_Q);
Thomas Falcon901e0402017-02-15 12:17:59 -06002877 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002878 ibmvnic_send_crq(adapter, &crq);
2879
2880 crq.query_capability.capability =
2881 cpu_to_be16(OPT_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06002882 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002883 ibmvnic_send_crq(adapter, &crq);
2884
2885 crq.query_capability.capability =
2886 cpu_to_be16(OPT_RXBA_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06002887 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002888 ibmvnic_send_crq(adapter, &crq);
2889
2890 crq.query_capability.capability = cpu_to_be16(TX_RX_DESC_REQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06002891 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002892 ibmvnic_send_crq(adapter, &crq);
2893}
2894
2895static void handle_query_ip_offload_rsp(struct ibmvnic_adapter *adapter)
2896{
2897 struct device *dev = &adapter->vdev->dev;
2898 struct ibmvnic_query_ip_offload_buffer *buf = &adapter->ip_offload_buf;
2899 union ibmvnic_crq crq;
2900 int i;
2901
2902 dma_unmap_single(dev, adapter->ip_offload_tok,
2903 sizeof(adapter->ip_offload_buf), DMA_FROM_DEVICE);
2904
2905 netdev_dbg(adapter->netdev, "Query IP Offload Buffer:\n");
2906 for (i = 0; i < (sizeof(adapter->ip_offload_buf) - 1) / 8 + 1; i++)
2907 netdev_dbg(adapter->netdev, "%016lx\n",
2908 ((unsigned long int *)(buf))[i]);
2909
2910 netdev_dbg(adapter->netdev, "ipv4_chksum = %d\n", buf->ipv4_chksum);
2911 netdev_dbg(adapter->netdev, "ipv6_chksum = %d\n", buf->ipv6_chksum);
2912 netdev_dbg(adapter->netdev, "tcp_ipv4_chksum = %d\n",
2913 buf->tcp_ipv4_chksum);
2914 netdev_dbg(adapter->netdev, "tcp_ipv6_chksum = %d\n",
2915 buf->tcp_ipv6_chksum);
2916 netdev_dbg(adapter->netdev, "udp_ipv4_chksum = %d\n",
2917 buf->udp_ipv4_chksum);
2918 netdev_dbg(adapter->netdev, "udp_ipv6_chksum = %d\n",
2919 buf->udp_ipv6_chksum);
2920 netdev_dbg(adapter->netdev, "large_tx_ipv4 = %d\n",
2921 buf->large_tx_ipv4);
2922 netdev_dbg(adapter->netdev, "large_tx_ipv6 = %d\n",
2923 buf->large_tx_ipv6);
2924 netdev_dbg(adapter->netdev, "large_rx_ipv4 = %d\n",
2925 buf->large_rx_ipv4);
2926 netdev_dbg(adapter->netdev, "large_rx_ipv6 = %d\n",
2927 buf->large_rx_ipv6);
2928 netdev_dbg(adapter->netdev, "max_ipv4_hdr_sz = %d\n",
2929 buf->max_ipv4_header_size);
2930 netdev_dbg(adapter->netdev, "max_ipv6_hdr_sz = %d\n",
2931 buf->max_ipv6_header_size);
2932 netdev_dbg(adapter->netdev, "max_tcp_hdr_size = %d\n",
2933 buf->max_tcp_header_size);
2934 netdev_dbg(adapter->netdev, "max_udp_hdr_size = %d\n",
2935 buf->max_udp_header_size);
2936 netdev_dbg(adapter->netdev, "max_large_tx_size = %d\n",
2937 buf->max_large_tx_size);
2938 netdev_dbg(adapter->netdev, "max_large_rx_size = %d\n",
2939 buf->max_large_rx_size);
2940 netdev_dbg(adapter->netdev, "ipv6_ext_hdr = %d\n",
2941 buf->ipv6_extension_header);
2942 netdev_dbg(adapter->netdev, "tcp_pseudosum_req = %d\n",
2943 buf->tcp_pseudosum_req);
2944 netdev_dbg(adapter->netdev, "num_ipv6_ext_hd = %d\n",
2945 buf->num_ipv6_ext_headers);
2946 netdev_dbg(adapter->netdev, "off_ipv6_ext_hd = %d\n",
2947 buf->off_ipv6_ext_headers);
2948
2949 adapter->ip_offload_ctrl_tok =
2950 dma_map_single(dev, &adapter->ip_offload_ctrl,
2951 sizeof(adapter->ip_offload_ctrl), DMA_TO_DEVICE);
2952
2953 if (dma_mapping_error(dev, adapter->ip_offload_ctrl_tok)) {
2954 dev_err(dev, "Couldn't map ip offload control buffer\n");
2955 return;
2956 }
2957
2958 adapter->ip_offload_ctrl.version = cpu_to_be32(INITIAL_VERSION_IOB);
2959 adapter->ip_offload_ctrl.tcp_ipv4_chksum = buf->tcp_ipv4_chksum;
2960 adapter->ip_offload_ctrl.udp_ipv4_chksum = buf->udp_ipv4_chksum;
2961 adapter->ip_offload_ctrl.tcp_ipv6_chksum = buf->tcp_ipv6_chksum;
2962 adapter->ip_offload_ctrl.udp_ipv6_chksum = buf->udp_ipv6_chksum;
2963
2964 /* large_tx/rx disabled for now, additional features needed */
2965 adapter->ip_offload_ctrl.large_tx_ipv4 = 0;
2966 adapter->ip_offload_ctrl.large_tx_ipv6 = 0;
2967 adapter->ip_offload_ctrl.large_rx_ipv4 = 0;
2968 adapter->ip_offload_ctrl.large_rx_ipv6 = 0;
2969
Thomas Falcon15482052017-10-17 12:36:54 -05002970 adapter->netdev->features = NETIF_F_SG | NETIF_F_GSO;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002971
2972 if (buf->tcp_ipv4_chksum || buf->udp_ipv4_chksum)
2973 adapter->netdev->features |= NETIF_F_IP_CSUM;
2974
2975 if (buf->tcp_ipv6_chksum || buf->udp_ipv6_chksum)
2976 adapter->netdev->features |= NETIF_F_IPV6_CSUM;
2977
Thomas Falcon9be02cd2016-04-01 17:20:35 -05002978 if ((adapter->netdev->features &
2979 (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)))
2980 adapter->netdev->features |= NETIF_F_RXCSUM;
2981
Thomas Falcon032c5e82015-12-21 11:26:06 -06002982 memset(&crq, 0, sizeof(crq));
2983 crq.control_ip_offload.first = IBMVNIC_CRQ_CMD;
2984 crq.control_ip_offload.cmd = CONTROL_IP_OFFLOAD;
2985 crq.control_ip_offload.len =
2986 cpu_to_be32(sizeof(adapter->ip_offload_ctrl));
2987 crq.control_ip_offload.ioba = cpu_to_be32(adapter->ip_offload_ctrl_tok);
2988 ibmvnic_send_crq(adapter, &crq);
2989}
2990
2991static void handle_error_info_rsp(union ibmvnic_crq *crq,
2992 struct ibmvnic_adapter *adapter)
2993{
2994 struct device *dev = &adapter->vdev->dev;
Wei Yongjun96183182016-06-27 20:48:53 +08002995 struct ibmvnic_error_buff *error_buff, *tmp;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002996 unsigned long flags;
2997 bool found = false;
2998 int i;
2999
3000 if (!crq->request_error_rsp.rc.code) {
3001 dev_info(dev, "Request Error Rsp returned with rc=%x\n",
3002 crq->request_error_rsp.rc.code);
3003 return;
3004 }
3005
3006 spin_lock_irqsave(&adapter->error_list_lock, flags);
Wei Yongjun96183182016-06-27 20:48:53 +08003007 list_for_each_entry_safe(error_buff, tmp, &adapter->errors, list)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003008 if (error_buff->error_id == crq->request_error_rsp.error_id) {
3009 found = true;
3010 list_del(&error_buff->list);
3011 break;
3012 }
3013 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
3014
3015 if (!found) {
3016 dev_err(dev, "Couldn't find error id %x\n",
Thomas Falcon75224c92017-02-15 10:33:33 -06003017 be32_to_cpu(crq->request_error_rsp.error_id));
Thomas Falcon032c5e82015-12-21 11:26:06 -06003018 return;
3019 }
3020
3021 dev_err(dev, "Detailed info for error id %x:",
Thomas Falcon75224c92017-02-15 10:33:33 -06003022 be32_to_cpu(crq->request_error_rsp.error_id));
Thomas Falcon032c5e82015-12-21 11:26:06 -06003023
3024 for (i = 0; i < error_buff->len; i++) {
3025 pr_cont("%02x", (int)error_buff->buff[i]);
3026 if (i % 8 == 7)
3027 pr_cont(" ");
3028 }
3029 pr_cont("\n");
3030
3031 dma_unmap_single(dev, error_buff->dma, error_buff->len,
3032 DMA_FROM_DEVICE);
3033 kfree(error_buff->buff);
3034 kfree(error_buff);
3035}
3036
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04003037static void request_error_information(struct ibmvnic_adapter *adapter,
3038 union ibmvnic_crq *err_crq)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003039{
Thomas Falcon032c5e82015-12-21 11:26:06 -06003040 struct device *dev = &adapter->vdev->dev;
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04003041 struct net_device *netdev = adapter->netdev;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003042 struct ibmvnic_error_buff *error_buff;
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04003043 unsigned long timeout = msecs_to_jiffies(30000);
3044 union ibmvnic_crq crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003045 unsigned long flags;
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04003046 int rc, detail_len;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003047
3048 error_buff = kmalloc(sizeof(*error_buff), GFP_ATOMIC);
3049 if (!error_buff)
3050 return;
3051
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04003052 detail_len = be32_to_cpu(err_crq->error_indication.detail_error_sz);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003053 error_buff->buff = kmalloc(detail_len, GFP_ATOMIC);
3054 if (!error_buff->buff) {
3055 kfree(error_buff);
3056 return;
3057 }
3058
3059 error_buff->dma = dma_map_single(dev, error_buff->buff, detail_len,
3060 DMA_FROM_DEVICE);
3061 if (dma_mapping_error(dev, error_buff->dma)) {
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04003062 netdev_err(netdev, "Couldn't map error buffer\n");
Thomas Falcon032c5e82015-12-21 11:26:06 -06003063 kfree(error_buff->buff);
3064 kfree(error_buff);
3065 return;
3066 }
3067
Thomas Falcon032c5e82015-12-21 11:26:06 -06003068 error_buff->len = detail_len;
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04003069 error_buff->error_id = err_crq->error_indication.error_id;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003070
3071 spin_lock_irqsave(&adapter->error_list_lock, flags);
3072 list_add_tail(&error_buff->list, &adapter->errors);
3073 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
3074
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04003075 memset(&crq, 0, sizeof(crq));
3076 crq.request_error_info.first = IBMVNIC_CRQ_CMD;
3077 crq.request_error_info.cmd = REQUEST_ERROR_INFO;
3078 crq.request_error_info.ioba = cpu_to_be32(error_buff->dma);
3079 crq.request_error_info.len = cpu_to_be32(detail_len);
3080 crq.request_error_info.error_id = err_crq->error_indication.error_id;
3081
3082 rc = ibmvnic_send_crq(adapter, &crq);
3083 if (rc) {
3084 netdev_err(netdev, "failed to request error information\n");
3085 goto err_info_fail;
3086 }
3087
3088 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
3089 netdev_err(netdev, "timeout waiting for error information\n");
3090 goto err_info_fail;
3091 }
3092
3093 return;
3094
3095err_info_fail:
3096 spin_lock_irqsave(&adapter->error_list_lock, flags);
3097 list_del(&error_buff->list);
3098 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
3099
3100 kfree(error_buff->buff);
3101 kfree(error_buff);
3102}
3103
3104static void handle_error_indication(union ibmvnic_crq *crq,
3105 struct ibmvnic_adapter *adapter)
3106{
3107 struct device *dev = &adapter->vdev->dev;
3108
3109 dev_err(dev, "Firmware reports %serror id %x, cause %d\n",
3110 crq->error_indication.flags
3111 & IBMVNIC_FATAL_ERROR ? "FATAL " : "",
3112 be32_to_cpu(crq->error_indication.error_id),
3113 be16_to_cpu(crq->error_indication.error_cause));
3114
3115 if (be32_to_cpu(crq->error_indication.error_id))
3116 request_error_information(adapter, crq);
Nathan Fontenoted651a12017-05-03 14:04:38 -04003117
3118 if (crq->error_indication.flags & IBMVNIC_FATAL_ERROR)
3119 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
John Allen8cb31cf2017-05-26 10:30:37 -04003120 else
3121 ibmvnic_reset(adapter, VNIC_RESET_NON_FATAL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003122}
3123
3124static void handle_change_mac_rsp(union ibmvnic_crq *crq,
3125 struct ibmvnic_adapter *adapter)
3126{
3127 struct net_device *netdev = adapter->netdev;
3128 struct device *dev = &adapter->vdev->dev;
3129 long rc;
3130
3131 rc = crq->change_mac_addr_rsp.rc.code;
3132 if (rc) {
3133 dev_err(dev, "Error %ld in CHANGE_MAC_ADDR_RSP\n", rc);
3134 return;
3135 }
3136 memcpy(netdev->dev_addr, &crq->change_mac_addr_rsp.mac_addr[0],
3137 ETH_ALEN);
3138}
3139
3140static void handle_request_cap_rsp(union ibmvnic_crq *crq,
3141 struct ibmvnic_adapter *adapter)
3142{
3143 struct device *dev = &adapter->vdev->dev;
3144 u64 *req_value;
3145 char *name;
3146
Thomas Falcon901e0402017-02-15 12:17:59 -06003147 atomic_dec(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003148 switch (be16_to_cpu(crq->request_capability_rsp.capability)) {
3149 case REQ_TX_QUEUES:
3150 req_value = &adapter->req_tx_queues;
3151 name = "tx";
3152 break;
3153 case REQ_RX_QUEUES:
3154 req_value = &adapter->req_rx_queues;
3155 name = "rx";
3156 break;
3157 case REQ_RX_ADD_QUEUES:
3158 req_value = &adapter->req_rx_add_queues;
3159 name = "rx_add";
3160 break;
3161 case REQ_TX_ENTRIES_PER_SUBCRQ:
3162 req_value = &adapter->req_tx_entries_per_subcrq;
3163 name = "tx_entries_per_subcrq";
3164 break;
3165 case REQ_RX_ADD_ENTRIES_PER_SUBCRQ:
3166 req_value = &adapter->req_rx_add_entries_per_subcrq;
3167 name = "rx_add_entries_per_subcrq";
3168 break;
3169 case REQ_MTU:
3170 req_value = &adapter->req_mtu;
3171 name = "mtu";
3172 break;
3173 case PROMISC_REQUESTED:
3174 req_value = &adapter->promisc;
3175 name = "promisc";
3176 break;
3177 default:
3178 dev_err(dev, "Got invalid cap request rsp %d\n",
3179 crq->request_capability.capability);
3180 return;
3181 }
3182
3183 switch (crq->request_capability_rsp.rc.code) {
3184 case SUCCESS:
3185 break;
3186 case PARTIALSUCCESS:
3187 dev_info(dev, "req=%lld, rsp=%ld in %s queue, retrying.\n",
3188 *req_value,
Thomas Falcon28f4d162017-02-15 10:32:11 -06003189 (long int)be64_to_cpu(crq->request_capability_rsp.
Thomas Falcon032c5e82015-12-21 11:26:06 -06003190 number), name);
Thomas Falcon28f4d162017-02-15 10:32:11 -06003191 *req_value = be64_to_cpu(crq->request_capability_rsp.number);
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003192 ibmvnic_send_req_caps(adapter, 1);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003193 return;
3194 default:
3195 dev_err(dev, "Error %d in request cap rsp\n",
3196 crq->request_capability_rsp.rc.code);
3197 return;
3198 }
3199
3200 /* Done receiving requested capabilities, query IP offload support */
Thomas Falcon901e0402017-02-15 12:17:59 -06003201 if (atomic_read(&adapter->running_cap_crqs) == 0) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06003202 union ibmvnic_crq newcrq;
3203 int buf_sz = sizeof(struct ibmvnic_query_ip_offload_buffer);
3204 struct ibmvnic_query_ip_offload_buffer *ip_offload_buf =
3205 &adapter->ip_offload_buf;
3206
Thomas Falcon249168a2017-02-15 12:18:00 -06003207 adapter->wait_capability = false;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003208 adapter->ip_offload_tok = dma_map_single(dev, ip_offload_buf,
3209 buf_sz,
3210 DMA_FROM_DEVICE);
3211
3212 if (dma_mapping_error(dev, adapter->ip_offload_tok)) {
3213 if (!firmware_has_feature(FW_FEATURE_CMO))
3214 dev_err(dev, "Couldn't map offload buffer\n");
3215 return;
3216 }
3217
3218 memset(&newcrq, 0, sizeof(newcrq));
3219 newcrq.query_ip_offload.first = IBMVNIC_CRQ_CMD;
3220 newcrq.query_ip_offload.cmd = QUERY_IP_OFFLOAD;
3221 newcrq.query_ip_offload.len = cpu_to_be32(buf_sz);
3222 newcrq.query_ip_offload.ioba =
3223 cpu_to_be32(adapter->ip_offload_tok);
3224
3225 ibmvnic_send_crq(adapter, &newcrq);
3226 }
3227}
3228
3229static int handle_login_rsp(union ibmvnic_crq *login_rsp_crq,
3230 struct ibmvnic_adapter *adapter)
3231{
3232 struct device *dev = &adapter->vdev->dev;
3233 struct ibmvnic_login_rsp_buffer *login_rsp = adapter->login_rsp_buf;
3234 struct ibmvnic_login_buffer *login = adapter->login_buf;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003235 int i;
3236
3237 dma_unmap_single(dev, adapter->login_buf_token, adapter->login_buf_sz,
3238 DMA_BIDIRECTIONAL);
3239 dma_unmap_single(dev, adapter->login_rsp_buf_token,
3240 adapter->login_rsp_buf_sz, DMA_BIDIRECTIONAL);
3241
John Allen498cd8e2016-04-06 11:49:55 -05003242 /* If the number of queues requested can't be allocated by the
3243 * server, the login response will return with code 1. We will need
3244 * to resend the login buffer with fewer queues requested.
3245 */
3246 if (login_rsp_crq->generic.rc.code) {
3247 adapter->renegotiate = true;
3248 complete(&adapter->init_done);
3249 return 0;
3250 }
3251
Thomas Falcon032c5e82015-12-21 11:26:06 -06003252 netdev_dbg(adapter->netdev, "Login Response Buffer:\n");
3253 for (i = 0; i < (adapter->login_rsp_buf_sz - 1) / 8 + 1; i++) {
3254 netdev_dbg(adapter->netdev, "%016lx\n",
3255 ((unsigned long int *)(adapter->login_rsp_buf))[i]);
3256 }
3257
3258 /* Sanity checks */
3259 if (login->num_txcomp_subcrqs != login_rsp->num_txsubm_subcrqs ||
3260 (be32_to_cpu(login->num_rxcomp_subcrqs) *
3261 adapter->req_rx_add_queues !=
3262 be32_to_cpu(login_rsp->num_rxadd_subcrqs))) {
3263 dev_err(dev, "FATAL: Inconsistent login and login rsp\n");
3264 ibmvnic_remove(adapter->vdev);
3265 return -EIO;
3266 }
3267 complete(&adapter->init_done);
3268
Thomas Falcon032c5e82015-12-21 11:26:06 -06003269 return 0;
3270}
3271
Thomas Falcon032c5e82015-12-21 11:26:06 -06003272static void handle_request_unmap_rsp(union ibmvnic_crq *crq,
3273 struct ibmvnic_adapter *adapter)
3274{
3275 struct device *dev = &adapter->vdev->dev;
3276 long rc;
3277
3278 rc = crq->request_unmap_rsp.rc.code;
3279 if (rc)
3280 dev_err(dev, "Error %ld in REQUEST_UNMAP_RSP\n", rc);
3281}
3282
3283static void handle_query_map_rsp(union ibmvnic_crq *crq,
3284 struct ibmvnic_adapter *adapter)
3285{
3286 struct net_device *netdev = adapter->netdev;
3287 struct device *dev = &adapter->vdev->dev;
3288 long rc;
3289
3290 rc = crq->query_map_rsp.rc.code;
3291 if (rc) {
3292 dev_err(dev, "Error %ld in QUERY_MAP_RSP\n", rc);
3293 return;
3294 }
3295 netdev_dbg(netdev, "page_size = %d\ntot_pages = %d\nfree_pages = %d\n",
3296 crq->query_map_rsp.page_size, crq->query_map_rsp.tot_pages,
3297 crq->query_map_rsp.free_pages);
3298}
3299
3300static void handle_query_cap_rsp(union ibmvnic_crq *crq,
3301 struct ibmvnic_adapter *adapter)
3302{
3303 struct net_device *netdev = adapter->netdev;
3304 struct device *dev = &adapter->vdev->dev;
3305 long rc;
3306
Thomas Falcon901e0402017-02-15 12:17:59 -06003307 atomic_dec(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003308 netdev_dbg(netdev, "Outstanding queries: %d\n",
Thomas Falcon901e0402017-02-15 12:17:59 -06003309 atomic_read(&adapter->running_cap_crqs));
Thomas Falcon032c5e82015-12-21 11:26:06 -06003310 rc = crq->query_capability.rc.code;
3311 if (rc) {
3312 dev_err(dev, "Error %ld in QUERY_CAP_RSP\n", rc);
3313 goto out;
3314 }
3315
3316 switch (be16_to_cpu(crq->query_capability.capability)) {
3317 case MIN_TX_QUEUES:
3318 adapter->min_tx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003319 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003320 netdev_dbg(netdev, "min_tx_queues = %lld\n",
3321 adapter->min_tx_queues);
3322 break;
3323 case MIN_RX_QUEUES:
3324 adapter->min_rx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003325 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003326 netdev_dbg(netdev, "min_rx_queues = %lld\n",
3327 adapter->min_rx_queues);
3328 break;
3329 case MIN_RX_ADD_QUEUES:
3330 adapter->min_rx_add_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003331 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003332 netdev_dbg(netdev, "min_rx_add_queues = %lld\n",
3333 adapter->min_rx_add_queues);
3334 break;
3335 case MAX_TX_QUEUES:
3336 adapter->max_tx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003337 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003338 netdev_dbg(netdev, "max_tx_queues = %lld\n",
3339 adapter->max_tx_queues);
3340 break;
3341 case MAX_RX_QUEUES:
3342 adapter->max_rx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003343 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003344 netdev_dbg(netdev, "max_rx_queues = %lld\n",
3345 adapter->max_rx_queues);
3346 break;
3347 case MAX_RX_ADD_QUEUES:
3348 adapter->max_rx_add_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003349 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003350 netdev_dbg(netdev, "max_rx_add_queues = %lld\n",
3351 adapter->max_rx_add_queues);
3352 break;
3353 case MIN_TX_ENTRIES_PER_SUBCRQ:
3354 adapter->min_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06003355 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003356 netdev_dbg(netdev, "min_tx_entries_per_subcrq = %lld\n",
3357 adapter->min_tx_entries_per_subcrq);
3358 break;
3359 case MIN_RX_ADD_ENTRIES_PER_SUBCRQ:
3360 adapter->min_rx_add_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06003361 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003362 netdev_dbg(netdev, "min_rx_add_entrs_per_subcrq = %lld\n",
3363 adapter->min_rx_add_entries_per_subcrq);
3364 break;
3365 case MAX_TX_ENTRIES_PER_SUBCRQ:
3366 adapter->max_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06003367 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003368 netdev_dbg(netdev, "max_tx_entries_per_subcrq = %lld\n",
3369 adapter->max_tx_entries_per_subcrq);
3370 break;
3371 case MAX_RX_ADD_ENTRIES_PER_SUBCRQ:
3372 adapter->max_rx_add_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06003373 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003374 netdev_dbg(netdev, "max_rx_add_entrs_per_subcrq = %lld\n",
3375 adapter->max_rx_add_entries_per_subcrq);
3376 break;
3377 case TCP_IP_OFFLOAD:
3378 adapter->tcp_ip_offload =
Thomas Falconde89e852016-03-01 10:20:09 -06003379 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003380 netdev_dbg(netdev, "tcp_ip_offload = %lld\n",
3381 adapter->tcp_ip_offload);
3382 break;
3383 case PROMISC_SUPPORTED:
3384 adapter->promisc_supported =
Thomas Falconde89e852016-03-01 10:20:09 -06003385 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003386 netdev_dbg(netdev, "promisc_supported = %lld\n",
3387 adapter->promisc_supported);
3388 break;
3389 case MIN_MTU:
Thomas Falconde89e852016-03-01 10:20:09 -06003390 adapter->min_mtu = be64_to_cpu(crq->query_capability.number);
Thomas Falconf39f0d12017-02-14 10:22:59 -06003391 netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003392 netdev_dbg(netdev, "min_mtu = %lld\n", adapter->min_mtu);
3393 break;
3394 case MAX_MTU:
Thomas Falconde89e852016-03-01 10:20:09 -06003395 adapter->max_mtu = be64_to_cpu(crq->query_capability.number);
Thomas Falconf39f0d12017-02-14 10:22:59 -06003396 netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003397 netdev_dbg(netdev, "max_mtu = %lld\n", adapter->max_mtu);
3398 break;
3399 case MAX_MULTICAST_FILTERS:
3400 adapter->max_multicast_filters =
Thomas Falconde89e852016-03-01 10:20:09 -06003401 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003402 netdev_dbg(netdev, "max_multicast_filters = %lld\n",
3403 adapter->max_multicast_filters);
3404 break;
3405 case VLAN_HEADER_INSERTION:
3406 adapter->vlan_header_insertion =
Thomas Falconde89e852016-03-01 10:20:09 -06003407 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003408 if (adapter->vlan_header_insertion)
3409 netdev->features |= NETIF_F_HW_VLAN_STAG_TX;
3410 netdev_dbg(netdev, "vlan_header_insertion = %lld\n",
3411 adapter->vlan_header_insertion);
3412 break;
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04003413 case RX_VLAN_HEADER_INSERTION:
3414 adapter->rx_vlan_header_insertion =
3415 be64_to_cpu(crq->query_capability.number);
3416 netdev_dbg(netdev, "rx_vlan_header_insertion = %lld\n",
3417 adapter->rx_vlan_header_insertion);
3418 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003419 case MAX_TX_SG_ENTRIES:
3420 adapter->max_tx_sg_entries =
Thomas Falconde89e852016-03-01 10:20:09 -06003421 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003422 netdev_dbg(netdev, "max_tx_sg_entries = %lld\n",
3423 adapter->max_tx_sg_entries);
3424 break;
3425 case RX_SG_SUPPORTED:
3426 adapter->rx_sg_supported =
Thomas Falconde89e852016-03-01 10:20:09 -06003427 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003428 netdev_dbg(netdev, "rx_sg_supported = %lld\n",
3429 adapter->rx_sg_supported);
3430 break;
3431 case OPT_TX_COMP_SUB_QUEUES:
3432 adapter->opt_tx_comp_sub_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003433 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003434 netdev_dbg(netdev, "opt_tx_comp_sub_queues = %lld\n",
3435 adapter->opt_tx_comp_sub_queues);
3436 break;
3437 case OPT_RX_COMP_QUEUES:
3438 adapter->opt_rx_comp_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003439 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003440 netdev_dbg(netdev, "opt_rx_comp_queues = %lld\n",
3441 adapter->opt_rx_comp_queues);
3442 break;
3443 case OPT_RX_BUFADD_Q_PER_RX_COMP_Q:
3444 adapter->opt_rx_bufadd_q_per_rx_comp_q =
Thomas Falconde89e852016-03-01 10:20:09 -06003445 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003446 netdev_dbg(netdev, "opt_rx_bufadd_q_per_rx_comp_q = %lld\n",
3447 adapter->opt_rx_bufadd_q_per_rx_comp_q);
3448 break;
3449 case OPT_TX_ENTRIES_PER_SUBCRQ:
3450 adapter->opt_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06003451 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003452 netdev_dbg(netdev, "opt_tx_entries_per_subcrq = %lld\n",
3453 adapter->opt_tx_entries_per_subcrq);
3454 break;
3455 case OPT_RXBA_ENTRIES_PER_SUBCRQ:
3456 adapter->opt_rxba_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06003457 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003458 netdev_dbg(netdev, "opt_rxba_entries_per_subcrq = %lld\n",
3459 adapter->opt_rxba_entries_per_subcrq);
3460 break;
3461 case TX_RX_DESC_REQ:
3462 adapter->tx_rx_desc_req = crq->query_capability.number;
3463 netdev_dbg(netdev, "tx_rx_desc_req = %llx\n",
3464 adapter->tx_rx_desc_req);
3465 break;
3466
3467 default:
3468 netdev_err(netdev, "Got invalid cap rsp %d\n",
3469 crq->query_capability.capability);
3470 }
3471
3472out:
Thomas Falcon249168a2017-02-15 12:18:00 -06003473 if (atomic_read(&adapter->running_cap_crqs) == 0) {
3474 adapter->wait_capability = false;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003475 ibmvnic_send_req_caps(adapter, 0);
Thomas Falcon249168a2017-02-15 12:18:00 -06003476 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06003477}
3478
Thomas Falcon032c5e82015-12-21 11:26:06 -06003479static void ibmvnic_handle_crq(union ibmvnic_crq *crq,
3480 struct ibmvnic_adapter *adapter)
3481{
3482 struct ibmvnic_generic_crq *gen_crq = &crq->generic;
3483 struct net_device *netdev = adapter->netdev;
3484 struct device *dev = &adapter->vdev->dev;
Murilo Fossa Vicentini993a82b2017-04-19 13:44:35 -04003485 u64 *u64_crq = (u64 *)crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003486 long rc;
3487
3488 netdev_dbg(netdev, "Handling CRQ: %016lx %016lx\n",
Murilo Fossa Vicentini993a82b2017-04-19 13:44:35 -04003489 (unsigned long int)cpu_to_be64(u64_crq[0]),
3490 (unsigned long int)cpu_to_be64(u64_crq[1]));
Thomas Falcon032c5e82015-12-21 11:26:06 -06003491 switch (gen_crq->first) {
3492 case IBMVNIC_CRQ_INIT_RSP:
3493 switch (gen_crq->cmd) {
3494 case IBMVNIC_CRQ_INIT:
3495 dev_info(dev, "Partner initialized\n");
John Allen017892c12017-05-26 10:30:19 -04003496 adapter->from_passive_init = true;
3497 complete(&adapter->init_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003498 break;
3499 case IBMVNIC_CRQ_INIT_COMPLETE:
3500 dev_info(dev, "Partner initialization complete\n");
3501 send_version_xchg(adapter);
3502 break;
3503 default:
3504 dev_err(dev, "Unknown crq cmd: %d\n", gen_crq->cmd);
3505 }
3506 return;
3507 case IBMVNIC_CRQ_XPORT_EVENT:
Nathan Fontenoted651a12017-05-03 14:04:38 -04003508 netif_carrier_off(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003509 if (gen_crq->cmd == IBMVNIC_PARTITION_MIGRATED) {
Nathan Fontenoted651a12017-05-03 14:04:38 -04003510 dev_info(dev, "Migrated, re-enabling adapter\n");
3511 ibmvnic_reset(adapter, VNIC_RESET_MOBILITY);
Thomas Falcondfad09a2016-08-18 11:37:51 -05003512 } else if (gen_crq->cmd == IBMVNIC_DEVICE_FAILOVER) {
3513 dev_info(dev, "Backing device failover detected\n");
Nathan Fontenoted651a12017-05-03 14:04:38 -04003514 ibmvnic_reset(adapter, VNIC_RESET_FAILOVER);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003515 } else {
3516 /* The adapter lost the connection */
3517 dev_err(dev, "Virtual Adapter failed (rc=%d)\n",
3518 gen_crq->cmd);
Nathan Fontenoted651a12017-05-03 14:04:38 -04003519 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003520 }
3521 return;
3522 case IBMVNIC_CRQ_CMD_RSP:
3523 break;
3524 default:
3525 dev_err(dev, "Got an invalid msg type 0x%02x\n",
3526 gen_crq->first);
3527 return;
3528 }
3529
3530 switch (gen_crq->cmd) {
3531 case VERSION_EXCHANGE_RSP:
3532 rc = crq->version_exchange_rsp.rc.code;
3533 if (rc) {
3534 dev_err(dev, "Error %ld in VERSION_EXCHG_RSP\n", rc);
3535 break;
3536 }
3537 dev_info(dev, "Partner protocol version is %d\n",
3538 crq->version_exchange_rsp.version);
3539 if (be16_to_cpu(crq->version_exchange_rsp.version) <
3540 ibmvnic_version)
3541 ibmvnic_version =
3542 be16_to_cpu(crq->version_exchange_rsp.version);
3543 send_cap_queries(adapter);
3544 break;
3545 case QUERY_CAPABILITY_RSP:
3546 handle_query_cap_rsp(crq, adapter);
3547 break;
3548 case QUERY_MAP_RSP:
3549 handle_query_map_rsp(crq, adapter);
3550 break;
3551 case REQUEST_MAP_RSP:
Thomas Falconf3be0cb2017-06-21 14:53:01 -05003552 adapter->fw_done_rc = crq->request_map_rsp.rc.code;
3553 complete(&adapter->fw_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003554 break;
3555 case REQUEST_UNMAP_RSP:
3556 handle_request_unmap_rsp(crq, adapter);
3557 break;
3558 case REQUEST_CAPABILITY_RSP:
3559 handle_request_cap_rsp(crq, adapter);
3560 break;
3561 case LOGIN_RSP:
3562 netdev_dbg(netdev, "Got Login Response\n");
3563 handle_login_rsp(crq, adapter);
3564 break;
3565 case LOGICAL_LINK_STATE_RSP:
Nathan Fontenot53da09e2017-04-21 15:39:04 -04003566 netdev_dbg(netdev,
3567 "Got Logical Link State Response, state: %d rc: %d\n",
3568 crq->logical_link_state_rsp.link_state,
3569 crq->logical_link_state_rsp.rc.code);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003570 adapter->logical_link_state =
3571 crq->logical_link_state_rsp.link_state;
Nathan Fontenot53da09e2017-04-21 15:39:04 -04003572 adapter->init_done_rc = crq->logical_link_state_rsp.rc.code;
3573 complete(&adapter->init_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003574 break;
3575 case LINK_STATE_INDICATION:
3576 netdev_dbg(netdev, "Got Logical Link State Indication\n");
3577 adapter->phys_link_state =
3578 crq->link_state_indication.phys_link_state;
3579 adapter->logical_link_state =
3580 crq->link_state_indication.logical_link_state;
3581 break;
3582 case CHANGE_MAC_ADDR_RSP:
3583 netdev_dbg(netdev, "Got MAC address change Response\n");
3584 handle_change_mac_rsp(crq, adapter);
3585 break;
3586 case ERROR_INDICATION:
3587 netdev_dbg(netdev, "Got Error Indication\n");
3588 handle_error_indication(crq, adapter);
3589 break;
3590 case REQUEST_ERROR_RSP:
3591 netdev_dbg(netdev, "Got Error Detail Response\n");
3592 handle_error_info_rsp(crq, adapter);
3593 break;
3594 case REQUEST_STATISTICS_RSP:
3595 netdev_dbg(netdev, "Got Statistics Response\n");
3596 complete(&adapter->stats_done);
3597 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003598 case QUERY_IP_OFFLOAD_RSP:
3599 netdev_dbg(netdev, "Got Query IP offload Response\n");
3600 handle_query_ip_offload_rsp(adapter);
3601 break;
3602 case MULTICAST_CTRL_RSP:
3603 netdev_dbg(netdev, "Got multicast control Response\n");
3604 break;
3605 case CONTROL_IP_OFFLOAD_RSP:
3606 netdev_dbg(netdev, "Got Control IP offload Response\n");
3607 dma_unmap_single(dev, adapter->ip_offload_ctrl_tok,
3608 sizeof(adapter->ip_offload_ctrl),
3609 DMA_TO_DEVICE);
John Allenbd0b6722017-03-17 17:13:40 -05003610 complete(&adapter->init_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003611 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003612 case COLLECT_FW_TRACE_RSP:
3613 netdev_dbg(netdev, "Got Collect firmware trace Response\n");
3614 complete(&adapter->fw_done);
3615 break;
3616 default:
3617 netdev_err(netdev, "Got an invalid cmd type 0x%02x\n",
3618 gen_crq->cmd);
3619 }
3620}
3621
3622static irqreturn_t ibmvnic_interrupt(int irq, void *instance)
3623{
3624 struct ibmvnic_adapter *adapter = instance;
Thomas Falcon6c267b32017-02-15 12:17:58 -06003625
Thomas Falcon6c267b32017-02-15 12:17:58 -06003626 tasklet_schedule(&adapter->tasklet);
Thomas Falcon6c267b32017-02-15 12:17:58 -06003627 return IRQ_HANDLED;
3628}
3629
3630static void ibmvnic_tasklet(void *data)
3631{
3632 struct ibmvnic_adapter *adapter = data;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003633 struct ibmvnic_crq_queue *queue = &adapter->crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003634 union ibmvnic_crq *crq;
3635 unsigned long flags;
3636 bool done = false;
3637
3638 spin_lock_irqsave(&queue->lock, flags);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003639 while (!done) {
3640 /* Pull all the valid messages off the CRQ */
3641 while ((crq = ibmvnic_next_crq(adapter)) != NULL) {
3642 ibmvnic_handle_crq(crq, adapter);
3643 crq->generic.first = 0;
3644 }
Brian Kinged7ecbf2017-04-19 13:44:53 -04003645
3646 /* remain in tasklet until all
3647 * capabilities responses are received
3648 */
3649 if (!adapter->wait_capability)
3650 done = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003651 }
Thomas Falcon249168a2017-02-15 12:18:00 -06003652 /* if capabilities CRQ's were sent in this tasklet, the following
3653 * tasklet must wait until all responses are received
3654 */
3655 if (atomic_read(&adapter->running_cap_crqs) != 0)
3656 adapter->wait_capability = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003657 spin_unlock_irqrestore(&queue->lock, flags);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003658}
3659
3660static int ibmvnic_reenable_crq_queue(struct ibmvnic_adapter *adapter)
3661{
3662 struct vio_dev *vdev = adapter->vdev;
3663 int rc;
3664
3665 do {
3666 rc = plpar_hcall_norets(H_ENABLE_CRQ, vdev->unit_address);
3667 } while (rc == H_IN_PROGRESS || rc == H_BUSY || H_IS_LONG_BUSY(rc));
3668
3669 if (rc)
3670 dev_err(&vdev->dev, "Error enabling adapter (rc=%d)\n", rc);
3671
3672 return rc;
3673}
3674
3675static int ibmvnic_reset_crq(struct ibmvnic_adapter *adapter)
3676{
3677 struct ibmvnic_crq_queue *crq = &adapter->crq;
3678 struct device *dev = &adapter->vdev->dev;
3679 struct vio_dev *vdev = adapter->vdev;
3680 int rc;
3681
3682 /* Close the CRQ */
3683 do {
3684 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
3685 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
3686
3687 /* Clean out the queue */
3688 memset(crq->msgs, 0, PAGE_SIZE);
3689 crq->cur = 0;
3690
3691 /* And re-open it again */
3692 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
3693 crq->msg_token, PAGE_SIZE);
3694
3695 if (rc == H_CLOSED)
3696 /* Adapter is good, but other end is not ready */
3697 dev_warn(dev, "Partner adapter not ready\n");
3698 else if (rc != 0)
3699 dev_warn(dev, "Couldn't register crq (rc=%d)\n", rc);
3700
3701 return rc;
3702}
3703
Nathan Fontenotf9928872017-03-30 02:48:54 -04003704static void release_crq_queue(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003705{
3706 struct ibmvnic_crq_queue *crq = &adapter->crq;
3707 struct vio_dev *vdev = adapter->vdev;
3708 long rc;
3709
Nathan Fontenotf9928872017-03-30 02:48:54 -04003710 if (!crq->msgs)
3711 return;
3712
Thomas Falcon032c5e82015-12-21 11:26:06 -06003713 netdev_dbg(adapter->netdev, "Releasing CRQ\n");
3714 free_irq(vdev->irq, adapter);
Thomas Falcon6c267b32017-02-15 12:17:58 -06003715 tasklet_kill(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003716 do {
3717 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
3718 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
3719
3720 dma_unmap_single(&vdev->dev, crq->msg_token, PAGE_SIZE,
3721 DMA_BIDIRECTIONAL);
3722 free_page((unsigned long)crq->msgs);
Nathan Fontenotf9928872017-03-30 02:48:54 -04003723 crq->msgs = NULL;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003724}
3725
Nathan Fontenotf9928872017-03-30 02:48:54 -04003726static int init_crq_queue(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003727{
3728 struct ibmvnic_crq_queue *crq = &adapter->crq;
3729 struct device *dev = &adapter->vdev->dev;
3730 struct vio_dev *vdev = adapter->vdev;
3731 int rc, retrc = -ENOMEM;
3732
Nathan Fontenotf9928872017-03-30 02:48:54 -04003733 if (crq->msgs)
3734 return 0;
3735
Thomas Falcon032c5e82015-12-21 11:26:06 -06003736 crq->msgs = (union ibmvnic_crq *)get_zeroed_page(GFP_KERNEL);
3737 /* Should we allocate more than one page? */
3738
3739 if (!crq->msgs)
3740 return -ENOMEM;
3741
3742 crq->size = PAGE_SIZE / sizeof(*crq->msgs);
3743 crq->msg_token = dma_map_single(dev, crq->msgs, PAGE_SIZE,
3744 DMA_BIDIRECTIONAL);
3745 if (dma_mapping_error(dev, crq->msg_token))
3746 goto map_failed;
3747
3748 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
3749 crq->msg_token, PAGE_SIZE);
3750
3751 if (rc == H_RESOURCE)
3752 /* maybe kexecing and resource is busy. try a reset */
3753 rc = ibmvnic_reset_crq(adapter);
3754 retrc = rc;
3755
3756 if (rc == H_CLOSED) {
3757 dev_warn(dev, "Partner adapter not ready\n");
3758 } else if (rc) {
3759 dev_warn(dev, "Error %d opening adapter\n", rc);
3760 goto reg_crq_failed;
3761 }
3762
3763 retrc = 0;
3764
Thomas Falcon6c267b32017-02-15 12:17:58 -06003765 tasklet_init(&adapter->tasklet, (void *)ibmvnic_tasklet,
3766 (unsigned long)adapter);
3767
Thomas Falcon032c5e82015-12-21 11:26:06 -06003768 netdev_dbg(adapter->netdev, "registering irq 0x%x\n", vdev->irq);
3769 rc = request_irq(vdev->irq, ibmvnic_interrupt, 0, IBMVNIC_NAME,
3770 adapter);
3771 if (rc) {
3772 dev_err(dev, "Couldn't register irq 0x%x. rc=%d\n",
3773 vdev->irq, rc);
3774 goto req_irq_failed;
3775 }
3776
3777 rc = vio_enable_interrupts(vdev);
3778 if (rc) {
3779 dev_err(dev, "Error %d enabling interrupts\n", rc);
3780 goto req_irq_failed;
3781 }
3782
3783 crq->cur = 0;
3784 spin_lock_init(&crq->lock);
3785
3786 return retrc;
3787
3788req_irq_failed:
Thomas Falcon6c267b32017-02-15 12:17:58 -06003789 tasklet_kill(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003790 do {
3791 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
3792 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
3793reg_crq_failed:
3794 dma_unmap_single(dev, crq->msg_token, PAGE_SIZE, DMA_BIDIRECTIONAL);
3795map_failed:
3796 free_page((unsigned long)crq->msgs);
Nathan Fontenotf9928872017-03-30 02:48:54 -04003797 crq->msgs = NULL;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003798 return retrc;
3799}
3800
John Allenf6ef6402017-03-17 17:13:42 -05003801static int ibmvnic_init(struct ibmvnic_adapter *adapter)
3802{
3803 struct device *dev = &adapter->vdev->dev;
3804 unsigned long timeout = msecs_to_jiffies(30000);
John Allenf6ef6402017-03-17 17:13:42 -05003805 int rc;
3806
Nathan Fontenot28cde752017-05-26 10:31:00 -04003807 if (adapter->resetting) {
3808 rc = ibmvnic_reset_crq(adapter);
3809 if (!rc)
3810 rc = vio_enable_interrupts(adapter->vdev);
3811 } else {
3812 rc = init_crq_queue(adapter);
3813 }
3814
John Allenf6ef6402017-03-17 17:13:42 -05003815 if (rc) {
3816 dev_err(dev, "Couldn't initialize crq. rc=%d\n", rc);
3817 return rc;
3818 }
3819
John Allen017892c12017-05-26 10:30:19 -04003820 adapter->from_passive_init = false;
3821
John Allenf6ef6402017-03-17 17:13:42 -05003822 init_completion(&adapter->init_done);
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04003823 adapter->init_done_rc = 0;
John Allenf6ef6402017-03-17 17:13:42 -05003824 ibmvnic_send_crq_init(adapter);
3825 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
3826 dev_err(dev, "Initialization sequence timed out\n");
John Allen017892c12017-05-26 10:30:19 -04003827 return -1;
3828 }
3829
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04003830 if (adapter->init_done_rc) {
3831 release_crq_queue(adapter);
3832 return adapter->init_done_rc;
3833 }
3834
John Allen017892c12017-05-26 10:30:19 -04003835 if (adapter->from_passive_init) {
3836 adapter->state = VNIC_OPEN;
3837 adapter->from_passive_init = false;
John Allenf6ef6402017-03-17 17:13:42 -05003838 return -1;
3839 }
3840
Nathan Fontenot57a49432017-05-26 10:31:12 -04003841 if (adapter->resetting)
3842 rc = reset_sub_crq_queues(adapter);
3843 else
3844 rc = init_sub_crqs(adapter);
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04003845 if (rc) {
3846 dev_err(dev, "Initialization of sub crqs failed\n");
3847 release_crq_queue(adapter);
Thomas Falcon5df969c2017-06-28 19:55:54 -05003848 return rc;
3849 }
3850
3851 rc = init_sub_crq_irqs(adapter);
3852 if (rc) {
3853 dev_err(dev, "Failed to initialize sub crq irqs\n");
3854 release_crq_queue(adapter);
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04003855 }
3856
3857 return rc;
John Allenf6ef6402017-03-17 17:13:42 -05003858}
3859
Thomas Falcon40c9db82017-06-12 12:35:04 -05003860static struct device_attribute dev_attr_failover;
3861
Thomas Falcon032c5e82015-12-21 11:26:06 -06003862static int ibmvnic_probe(struct vio_dev *dev, const struct vio_device_id *id)
3863{
3864 struct ibmvnic_adapter *adapter;
3865 struct net_device *netdev;
3866 unsigned char *mac_addr_p;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003867 int rc;
3868
3869 dev_dbg(&dev->dev, "entering ibmvnic_probe for UA 0x%x\n",
3870 dev->unit_address);
3871
3872 mac_addr_p = (unsigned char *)vio_get_attribute(dev,
3873 VETH_MAC_ADDR, NULL);
3874 if (!mac_addr_p) {
3875 dev_err(&dev->dev,
3876 "(%s:%3.3d) ERROR: Can't find MAC_ADDR attribute\n",
3877 __FILE__, __LINE__);
3878 return 0;
3879 }
3880
3881 netdev = alloc_etherdev_mq(sizeof(struct ibmvnic_adapter),
3882 IBMVNIC_MAX_TX_QUEUES);
3883 if (!netdev)
3884 return -ENOMEM;
3885
3886 adapter = netdev_priv(netdev);
Nathan Fontenot90c80142017-05-03 14:04:32 -04003887 adapter->state = VNIC_PROBING;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003888 dev_set_drvdata(&dev->dev, netdev);
3889 adapter->vdev = dev;
3890 adapter->netdev = netdev;
3891
3892 ether_addr_copy(adapter->mac_addr, mac_addr_p);
3893 ether_addr_copy(netdev->dev_addr, adapter->mac_addr);
3894 netdev->irq = dev->irq;
3895 netdev->netdev_ops = &ibmvnic_netdev_ops;
3896 netdev->ethtool_ops = &ibmvnic_ethtool_ops;
3897 SET_NETDEV_DEV(netdev, &dev->dev);
3898
3899 spin_lock_init(&adapter->stats_lock);
3900
Thomas Falcon032c5e82015-12-21 11:26:06 -06003901 INIT_LIST_HEAD(&adapter->errors);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003902 spin_lock_init(&adapter->error_list_lock);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003903
Nathan Fontenoted651a12017-05-03 14:04:38 -04003904 INIT_WORK(&adapter->ibmvnic_reset, __ibmvnic_reset);
3905 INIT_LIST_HEAD(&adapter->rwi_list);
3906 mutex_init(&adapter->reset_lock);
3907 mutex_init(&adapter->rwi_lock);
3908 adapter->resetting = false;
3909
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04003910 do {
3911 rc = ibmvnic_init(adapter);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05003912 if (rc && rc != EAGAIN)
3913 goto ibmvnic_init_fail;
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04003914 } while (rc == EAGAIN);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003915
Thomas Falconf39f0d12017-02-14 10:22:59 -06003916 netdev->mtu = adapter->req_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003917
Thomas Falcon40c9db82017-06-12 12:35:04 -05003918 rc = device_create_file(&dev->dev, &dev_attr_failover);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05003919 if (rc)
3920 goto ibmvnic_init_fail;
Thomas Falcon40c9db82017-06-12 12:35:04 -05003921
Mick Tarsele876a8a2017-09-28 13:53:18 -07003922 netif_carrier_off(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003923 rc = register_netdev(netdev);
3924 if (rc) {
3925 dev_err(&dev->dev, "failed to register netdev rc=%d\n", rc);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05003926 goto ibmvnic_register_fail;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003927 }
3928 dev_info(&dev->dev, "ibmvnic registered\n");
3929
Nathan Fontenot90c80142017-05-03 14:04:32 -04003930 adapter->state = VNIC_PROBED;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003931 return 0;
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05003932
3933ibmvnic_register_fail:
3934 device_remove_file(&dev->dev, &dev_attr_failover);
3935
3936ibmvnic_init_fail:
3937 release_sub_crqs(adapter);
3938 release_crq_queue(adapter);
3939 free_netdev(netdev);
3940
3941 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003942}
3943
3944static int ibmvnic_remove(struct vio_dev *dev)
3945{
3946 struct net_device *netdev = dev_get_drvdata(&dev->dev);
Nathan Fontenot37489052017-04-19 13:45:04 -04003947 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003948
Nathan Fontenot90c80142017-05-03 14:04:32 -04003949 adapter->state = VNIC_REMOVING;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003950 unregister_netdev(netdev);
Nathan Fontenoted651a12017-05-03 14:04:38 -04003951 mutex_lock(&adapter->reset_lock);
Nathan Fontenot37489052017-04-19 13:45:04 -04003952
3953 release_resources(adapter);
3954 release_sub_crqs(adapter);
3955 release_crq_queue(adapter);
3956
Nathan Fontenot90c80142017-05-03 14:04:32 -04003957 adapter->state = VNIC_REMOVED;
3958
Nathan Fontenoted651a12017-05-03 14:04:38 -04003959 mutex_unlock(&adapter->reset_lock);
Thomas Falcon40c9db82017-06-12 12:35:04 -05003960 device_remove_file(&dev->dev, &dev_attr_failover);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003961 free_netdev(netdev);
3962 dev_set_drvdata(&dev->dev, NULL);
3963
3964 return 0;
3965}
3966
Thomas Falcon40c9db82017-06-12 12:35:04 -05003967static ssize_t failover_store(struct device *dev, struct device_attribute *attr,
3968 const char *buf, size_t count)
3969{
3970 struct net_device *netdev = dev_get_drvdata(dev);
3971 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
3972 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
3973 __be64 session_token;
3974 long rc;
3975
3976 if (!sysfs_streq(buf, "1"))
3977 return -EINVAL;
3978
3979 rc = plpar_hcall(H_VIOCTL, retbuf, adapter->vdev->unit_address,
3980 H_GET_SESSION_TOKEN, 0, 0, 0);
3981 if (rc) {
3982 netdev_err(netdev, "Couldn't retrieve session token, rc %ld\n",
3983 rc);
3984 return -EINVAL;
3985 }
3986
3987 session_token = (__be64)retbuf[0];
3988 netdev_dbg(netdev, "Initiating client failover, session id %llx\n",
3989 be64_to_cpu(session_token));
3990 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
3991 H_SESSION_ERR_DETECTED, session_token, 0, 0);
3992 if (rc) {
3993 netdev_err(netdev, "Client initiated failover failed, rc %ld\n",
3994 rc);
3995 return -EINVAL;
3996 }
3997
3998 return count;
3999}
4000
4001static DEVICE_ATTR(failover, 0200, NULL, failover_store);
4002
Thomas Falcon032c5e82015-12-21 11:26:06 -06004003static unsigned long ibmvnic_get_desired_dma(struct vio_dev *vdev)
4004{
4005 struct net_device *netdev = dev_get_drvdata(&vdev->dev);
4006 struct ibmvnic_adapter *adapter;
4007 struct iommu_table *tbl;
4008 unsigned long ret = 0;
4009 int i;
4010
4011 tbl = get_iommu_table_base(&vdev->dev);
4012
4013 /* netdev inits at probe time along with the structures we need below*/
4014 if (!netdev)
4015 return IOMMU_PAGE_ALIGN(IBMVNIC_IO_ENTITLEMENT_DEFAULT, tbl);
4016
4017 adapter = netdev_priv(netdev);
4018
4019 ret += PAGE_SIZE; /* the crq message queue */
Thomas Falcon032c5e82015-12-21 11:26:06 -06004020 ret += IOMMU_PAGE_ALIGN(sizeof(struct ibmvnic_statistics), tbl);
4021
4022 for (i = 0; i < adapter->req_tx_queues + adapter->req_rx_queues; i++)
4023 ret += 4 * PAGE_SIZE; /* the scrq message queue */
4024
4025 for (i = 0; i < be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
4026 i++)
4027 ret += adapter->rx_pool[i].size *
4028 IOMMU_PAGE_ALIGN(adapter->rx_pool[i].buff_size, tbl);
4029
4030 return ret;
4031}
4032
4033static int ibmvnic_resume(struct device *dev)
4034{
4035 struct net_device *netdev = dev_get_drvdata(dev);
4036 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004037
John Allencb89ba22017-06-19 11:27:53 -05004038 if (adapter->state != VNIC_OPEN)
4039 return 0;
4040
John Allena2488782017-07-24 13:26:06 -05004041 tasklet_schedule(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004042
4043 return 0;
4044}
4045
Arvind Yadav8c37bc62017-08-17 18:52:54 +05304046static const struct vio_device_id ibmvnic_device_table[] = {
Thomas Falcon032c5e82015-12-21 11:26:06 -06004047 {"network", "IBM,vnic"},
4048 {"", "" }
4049};
4050MODULE_DEVICE_TABLE(vio, ibmvnic_device_table);
4051
4052static const struct dev_pm_ops ibmvnic_pm_ops = {
4053 .resume = ibmvnic_resume
4054};
4055
4056static struct vio_driver ibmvnic_driver = {
4057 .id_table = ibmvnic_device_table,
4058 .probe = ibmvnic_probe,
4059 .remove = ibmvnic_remove,
4060 .get_desired_dma = ibmvnic_get_desired_dma,
4061 .name = ibmvnic_driver_name,
4062 .pm = &ibmvnic_pm_ops,
4063};
4064
4065/* module functions */
4066static int __init ibmvnic_module_init(void)
4067{
4068 pr_info("%s: %s %s\n", ibmvnic_driver_name, ibmvnic_driver_string,
4069 IBMVNIC_DRIVER_VERSION);
4070
4071 return vio_register_driver(&ibmvnic_driver);
4072}
4073
4074static void __exit ibmvnic_module_exit(void)
4075{
4076 vio_unregister_driver(&ibmvnic_driver);
4077}
4078
4079module_init(ibmvnic_module_init);
4080module_exit(ibmvnic_module_exit);