blob: cb8182f4fdfa1725f8d4024741e00219376d7326 [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);
930 mutex_unlock(&adapter->reset_lock);
931
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400932 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600933}
934
Nathan Fontenotb41b83e2017-05-03 14:04:56 -0400935static void clean_tx_pools(struct ibmvnic_adapter *adapter)
936{
937 struct ibmvnic_tx_pool *tx_pool;
938 u64 tx_entries;
939 int tx_scrqs;
940 int i, j;
941
942 if (!adapter->tx_pool)
943 return;
944
945 tx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
946 tx_entries = adapter->req_tx_entries_per_subcrq;
947
948 /* Free any remaining skbs in the tx buffer pools */
949 for (i = 0; i < tx_scrqs; i++) {
950 tx_pool = &adapter->tx_pool[i];
951 if (!tx_pool)
952 continue;
953
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500954 netdev_dbg(adapter->netdev, "Cleaning tx_pool[%d]\n", i);
Nathan Fontenotb41b83e2017-05-03 14:04:56 -0400955 for (j = 0; j < tx_entries; j++) {
956 if (tx_pool->tx_buff[j].skb) {
957 dev_kfree_skb_any(tx_pool->tx_buff[j].skb);
958 tx_pool->tx_buff[j].skb = NULL;
959 }
960 }
961 }
962}
963
Nathan Fontenoted651a12017-05-03 14:04:38 -0400964static int __ibmvnic_close(struct net_device *netdev)
John Allenea5509f2017-03-17 17:13:43 -0500965{
966 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Nathan Fontenot53da09e2017-04-21 15:39:04 -0400967 int rc = 0;
John Allenea5509f2017-03-17 17:13:43 -0500968 int i;
969
Nathan Fontenot90c80142017-05-03 14:04:32 -0400970 adapter->state = VNIC_CLOSING;
Thomas Falcon4c2687a2017-06-14 23:50:06 -0500971
972 /* ensure that transmissions are stopped if called by do_reset */
973 if (adapter->resetting)
974 netif_tx_disable(netdev);
975 else
976 netif_tx_stop_all_queues(netdev);
977
John Allend944c3d62017-05-26 10:30:13 -0400978 ibmvnic_napi_disable(adapter);
Nathan Fontenot46293b92017-05-03 14:05:02 -0400979
980 if (adapter->tx_scrq) {
981 for (i = 0; i < adapter->req_tx_queues; i++)
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500982 if (adapter->tx_scrq[i]->irq) {
983 netdev_dbg(adapter->netdev,
984 "Disabling tx_scrq[%d] irq\n", i);
Nathan Fontenot46293b92017-05-03 14:05:02 -0400985 disable_irq(adapter->tx_scrq[i]->irq);
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500986 }
Nathan Fontenot46293b92017-05-03 14:05:02 -0400987 }
988
Nathan Fontenot53da09e2017-04-21 15:39:04 -0400989 rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_DN);
Nathan Fontenot46293b92017-05-03 14:05:02 -0400990 if (rc)
991 return rc;
992
993 if (adapter->rx_scrq) {
994 for (i = 0; i < adapter->req_rx_queues; i++) {
995 int retries = 10;
996
997 while (pending_scrq(adapter, adapter->rx_scrq[i])) {
998 retries--;
999 mdelay(100);
1000
1001 if (retries == 0)
1002 break;
1003 }
1004
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001005 if (adapter->rx_scrq[i]->irq) {
1006 netdev_dbg(adapter->netdev,
1007 "Disabling rx_scrq[%d] irq\n", i);
Nathan Fontenot46293b92017-05-03 14:05:02 -04001008 disable_irq(adapter->rx_scrq[i]->irq);
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001009 }
Nathan Fontenot46293b92017-05-03 14:05:02 -04001010 }
1011 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001012
Thomas Falcon10f76212017-05-26 10:30:31 -04001013 clean_tx_pools(adapter);
Nathan Fontenot90c80142017-05-03 14:04:32 -04001014 adapter->state = VNIC_CLOSED;
Nathan Fontenot53da09e2017-04-21 15:39:04 -04001015 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001016}
1017
Nathan Fontenoted651a12017-05-03 14:04:38 -04001018static int ibmvnic_close(struct net_device *netdev)
1019{
1020 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1021 int rc;
1022
1023 mutex_lock(&adapter->reset_lock);
1024 rc = __ibmvnic_close(netdev);
1025 mutex_unlock(&adapter->reset_lock);
1026
1027 return rc;
1028}
1029
Thomas Falconad7775d2016-04-01 17:20:34 -05001030/**
1031 * build_hdr_data - creates L2/L3/L4 header data buffer
1032 * @hdr_field - bitfield determining needed headers
1033 * @skb - socket buffer
1034 * @hdr_len - array of header lengths
1035 * @tot_len - total length of data
1036 *
1037 * Reads hdr_field to determine which headers are needed by firmware.
1038 * Builds a buffer containing these headers. Saves individual header
1039 * lengths and total buffer length to be used to build descriptors.
1040 */
1041static int build_hdr_data(u8 hdr_field, struct sk_buff *skb,
1042 int *hdr_len, u8 *hdr_data)
1043{
1044 int len = 0;
1045 u8 *hdr;
1046
1047 hdr_len[0] = sizeof(struct ethhdr);
1048
1049 if (skb->protocol == htons(ETH_P_IP)) {
1050 hdr_len[1] = ip_hdr(skb)->ihl * 4;
1051 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
1052 hdr_len[2] = tcp_hdrlen(skb);
1053 else if (ip_hdr(skb)->protocol == IPPROTO_UDP)
1054 hdr_len[2] = sizeof(struct udphdr);
1055 } else if (skb->protocol == htons(ETH_P_IPV6)) {
1056 hdr_len[1] = sizeof(struct ipv6hdr);
1057 if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
1058 hdr_len[2] = tcp_hdrlen(skb);
1059 else if (ipv6_hdr(skb)->nexthdr == IPPROTO_UDP)
1060 hdr_len[2] = sizeof(struct udphdr);
1061 }
1062
1063 memset(hdr_data, 0, 120);
1064 if ((hdr_field >> 6) & 1) {
1065 hdr = skb_mac_header(skb);
1066 memcpy(hdr_data, hdr, hdr_len[0]);
1067 len += hdr_len[0];
1068 }
1069
1070 if ((hdr_field >> 5) & 1) {
1071 hdr = skb_network_header(skb);
1072 memcpy(hdr_data + len, hdr, hdr_len[1]);
1073 len += hdr_len[1];
1074 }
1075
1076 if ((hdr_field >> 4) & 1) {
1077 hdr = skb_transport_header(skb);
1078 memcpy(hdr_data + len, hdr, hdr_len[2]);
1079 len += hdr_len[2];
1080 }
1081 return len;
1082}
1083
1084/**
1085 * create_hdr_descs - create header and header extension descriptors
1086 * @hdr_field - bitfield determining needed headers
1087 * @data - buffer containing header data
1088 * @len - length of data buffer
1089 * @hdr_len - array of individual header lengths
1090 * @scrq_arr - descriptor array
1091 *
1092 * Creates header and, if needed, header extension descriptors and
1093 * places them in a descriptor array, scrq_arr
1094 */
1095
1096static void create_hdr_descs(u8 hdr_field, u8 *hdr_data, int len, int *hdr_len,
1097 union sub_crq *scrq_arr)
1098{
1099 union sub_crq hdr_desc;
1100 int tmp_len = len;
1101 u8 *data, *cur;
1102 int tmp;
1103
1104 while (tmp_len > 0) {
1105 cur = hdr_data + len - tmp_len;
1106
1107 memset(&hdr_desc, 0, sizeof(hdr_desc));
1108 if (cur != hdr_data) {
1109 data = hdr_desc.hdr_ext.data;
1110 tmp = tmp_len > 29 ? 29 : tmp_len;
1111 hdr_desc.hdr_ext.first = IBMVNIC_CRQ_CMD;
1112 hdr_desc.hdr_ext.type = IBMVNIC_HDR_EXT_DESC;
1113 hdr_desc.hdr_ext.len = tmp;
1114 } else {
1115 data = hdr_desc.hdr.data;
1116 tmp = tmp_len > 24 ? 24 : tmp_len;
1117 hdr_desc.hdr.first = IBMVNIC_CRQ_CMD;
1118 hdr_desc.hdr.type = IBMVNIC_HDR_DESC;
1119 hdr_desc.hdr.len = tmp;
1120 hdr_desc.hdr.l2_len = (u8)hdr_len[0];
1121 hdr_desc.hdr.l3_len = cpu_to_be16((u16)hdr_len[1]);
1122 hdr_desc.hdr.l4_len = (u8)hdr_len[2];
1123 hdr_desc.hdr.flag = hdr_field << 1;
1124 }
1125 memcpy(data, cur, tmp);
1126 tmp_len -= tmp;
1127 *scrq_arr = hdr_desc;
1128 scrq_arr++;
1129 }
1130}
1131
1132/**
1133 * build_hdr_descs_arr - build a header descriptor array
1134 * @skb - socket buffer
1135 * @num_entries - number of descriptors to be sent
1136 * @subcrq - first TX descriptor
1137 * @hdr_field - bit field determining which headers will be sent
1138 *
1139 * This function will build a TX descriptor array with applicable
1140 * L2/L3/L4 packet header descriptors to be sent by send_subcrq_indirect.
1141 */
1142
1143static void build_hdr_descs_arr(struct ibmvnic_tx_buff *txbuff,
1144 int *num_entries, u8 hdr_field)
1145{
1146 int hdr_len[3] = {0, 0, 0};
1147 int tot_len, len;
1148 u8 *hdr_data = txbuff->hdr_data;
1149
1150 tot_len = build_hdr_data(hdr_field, txbuff->skb, hdr_len,
1151 txbuff->hdr_data);
1152 len = tot_len;
1153 len -= 24;
1154 if (len > 0)
1155 num_entries += len % 29 ? len / 29 + 1 : len / 29;
1156 create_hdr_descs(hdr_field, hdr_data, tot_len, hdr_len,
1157 txbuff->indir_arr + 1);
1158}
1159
Thomas Falcon032c5e82015-12-21 11:26:06 -06001160static int ibmvnic_xmit(struct sk_buff *skb, struct net_device *netdev)
1161{
1162 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1163 int queue_num = skb_get_queue_mapping(skb);
Thomas Falconad7775d2016-04-01 17:20:34 -05001164 u8 *hdrs = (u8 *)&adapter->tx_rx_desc_req;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001165 struct device *dev = &adapter->vdev->dev;
1166 struct ibmvnic_tx_buff *tx_buff = NULL;
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001167 struct ibmvnic_sub_crq_queue *tx_scrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001168 struct ibmvnic_tx_pool *tx_pool;
1169 unsigned int tx_send_failed = 0;
1170 unsigned int tx_map_failed = 0;
1171 unsigned int tx_dropped = 0;
1172 unsigned int tx_packets = 0;
1173 unsigned int tx_bytes = 0;
1174 dma_addr_t data_dma_addr;
1175 struct netdev_queue *txq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001176 unsigned long lpar_rc;
1177 union sub_crq tx_crq;
1178 unsigned int offset;
Thomas Falconad7775d2016-04-01 17:20:34 -05001179 int num_entries = 1;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001180 unsigned char *dst;
1181 u64 *handle_array;
1182 int index = 0;
1183 int ret = 0;
1184
Nathan Fontenoted651a12017-05-03 14:04:38 -04001185 if (adapter->resetting) {
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001186 if (!netif_subqueue_stopped(netdev, skb))
1187 netif_stop_subqueue(netdev, queue_num);
1188 dev_kfree_skb_any(skb);
1189
Thomas Falcon032c5e82015-12-21 11:26:06 -06001190 tx_send_failed++;
1191 tx_dropped++;
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001192 ret = NETDEV_TX_OK;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001193 goto out;
1194 }
1195
Nathan Fontenot161b8a82017-05-03 14:05:08 -04001196 tx_pool = &adapter->tx_pool[queue_num];
1197 tx_scrq = adapter->tx_scrq[queue_num];
1198 txq = netdev_get_tx_queue(netdev, skb_get_queue_mapping(skb));
1199 handle_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
1200 be32_to_cpu(adapter->login_rsp_buf->off_txsubm_subcrqs));
1201
Thomas Falcon032c5e82015-12-21 11:26:06 -06001202 index = tx_pool->free_map[tx_pool->consumer_index];
1203 offset = index * adapter->req_mtu;
1204 dst = tx_pool->long_term_buff.buff + offset;
1205 memset(dst, 0, adapter->req_mtu);
1206 skb_copy_from_linear_data(skb, dst, skb->len);
1207 data_dma_addr = tx_pool->long_term_buff.addr + offset;
1208
1209 tx_pool->consumer_index =
1210 (tx_pool->consumer_index + 1) %
Thomas Falcon068d9f92017-03-05 12:18:42 -06001211 adapter->req_tx_entries_per_subcrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001212
1213 tx_buff = &tx_pool->tx_buff[index];
1214 tx_buff->skb = skb;
1215 tx_buff->data_dma[0] = data_dma_addr;
1216 tx_buff->data_len[0] = skb->len;
1217 tx_buff->index = index;
1218 tx_buff->pool_index = queue_num;
1219 tx_buff->last_frag = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001220
1221 memset(&tx_crq, 0, sizeof(tx_crq));
1222 tx_crq.v1.first = IBMVNIC_CRQ_CMD;
1223 tx_crq.v1.type = IBMVNIC_TX_DESC;
1224 tx_crq.v1.n_crq_elem = 1;
1225 tx_crq.v1.n_sge = 1;
1226 tx_crq.v1.flags1 = IBMVNIC_TX_COMP_NEEDED;
1227 tx_crq.v1.correlator = cpu_to_be32(index);
1228 tx_crq.v1.dma_reg = cpu_to_be16(tx_pool->long_term_buff.map_id);
1229 tx_crq.v1.sge_len = cpu_to_be32(skb->len);
1230 tx_crq.v1.ioba = cpu_to_be64(data_dma_addr);
1231
1232 if (adapter->vlan_header_insertion) {
1233 tx_crq.v1.flags2 |= IBMVNIC_TX_VLAN_INSERT;
1234 tx_crq.v1.vlan_id = cpu_to_be16(skb->vlan_tci);
1235 }
1236
1237 if (skb->protocol == htons(ETH_P_IP)) {
1238 if (ip_hdr(skb)->version == 4)
1239 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV4;
1240 else if (ip_hdr(skb)->version == 6)
1241 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV6;
1242
1243 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
1244 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_TCP;
1245 else if (ip_hdr(skb)->protocol != IPPROTO_TCP)
1246 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_UDP;
1247 }
1248
Thomas Falconad7775d2016-04-01 17:20:34 -05001249 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06001250 tx_crq.v1.flags1 |= IBMVNIC_TX_CHKSUM_OFFLOAD;
Thomas Falconad7775d2016-04-01 17:20:34 -05001251 hdrs += 2;
1252 }
1253 /* determine if l2/3/4 headers are sent to firmware */
1254 if ((*hdrs >> 7) & 1 &&
1255 (skb->protocol == htons(ETH_P_IP) ||
1256 skb->protocol == htons(ETH_P_IPV6))) {
1257 build_hdr_descs_arr(tx_buff, &num_entries, *hdrs);
1258 tx_crq.v1.n_crq_elem = num_entries;
1259 tx_buff->indir_arr[0] = tx_crq;
1260 tx_buff->indir_dma = dma_map_single(dev, tx_buff->indir_arr,
1261 sizeof(tx_buff->indir_arr),
1262 DMA_TO_DEVICE);
1263 if (dma_mapping_error(dev, tx_buff->indir_dma)) {
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001264 dev_kfree_skb_any(skb);
1265 tx_buff->skb = NULL;
Thomas Falconad7775d2016-04-01 17:20:34 -05001266 if (!firmware_has_feature(FW_FEATURE_CMO))
1267 dev_err(dev, "tx: unable to map descriptor array\n");
1268 tx_map_failed++;
1269 tx_dropped++;
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001270 ret = NETDEV_TX_OK;
Thomas Falconad7775d2016-04-01 17:20:34 -05001271 goto out;
1272 }
John Allen498cd8e2016-04-06 11:49:55 -05001273 lpar_rc = send_subcrq_indirect(adapter, handle_array[queue_num],
Thomas Falconad7775d2016-04-01 17:20:34 -05001274 (u64)tx_buff->indir_dma,
1275 (u64)num_entries);
1276 } else {
John Allen498cd8e2016-04-06 11:49:55 -05001277 lpar_rc = send_subcrq(adapter, handle_array[queue_num],
1278 &tx_crq);
Thomas Falconad7775d2016-04-01 17:20:34 -05001279 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001280 if (lpar_rc != H_SUCCESS) {
1281 dev_err(dev, "tx failed with code %ld\n", lpar_rc);
1282
1283 if (tx_pool->consumer_index == 0)
1284 tx_pool->consumer_index =
Thomas Falcon068d9f92017-03-05 12:18:42 -06001285 adapter->req_tx_entries_per_subcrq - 1;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001286 else
1287 tx_pool->consumer_index--;
1288
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001289 dev_kfree_skb_any(skb);
1290 tx_buff->skb = NULL;
1291
Thomas Falconb8c80b82017-05-26 10:30:42 -04001292 if (lpar_rc == H_CLOSED) {
1293 /* Disable TX and report carrier off if queue is closed.
1294 * Firmware guarantees that a signal will be sent to the
1295 * driver, triggering a reset or some other action.
1296 */
1297 netif_tx_stop_all_queues(netdev);
1298 netif_carrier_off(netdev);
1299 }
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001300
Thomas Falcon032c5e82015-12-21 11:26:06 -06001301 tx_send_failed++;
1302 tx_dropped++;
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001303 ret = NETDEV_TX_OK;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001304 goto out;
1305 }
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001306
Brian King58c8c0c2017-04-19 13:44:47 -04001307 if (atomic_inc_return(&tx_scrq->used)
1308 >= adapter->req_tx_entries_per_subcrq) {
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001309 netdev_info(netdev, "Stopping queue %d\n", queue_num);
1310 netif_stop_subqueue(netdev, queue_num);
1311 }
1312
Thomas Falcon032c5e82015-12-21 11:26:06 -06001313 tx_packets++;
1314 tx_bytes += skb->len;
1315 txq->trans_start = jiffies;
1316 ret = NETDEV_TX_OK;
1317
1318out:
1319 netdev->stats.tx_dropped += tx_dropped;
1320 netdev->stats.tx_bytes += tx_bytes;
1321 netdev->stats.tx_packets += tx_packets;
1322 adapter->tx_send_failed += tx_send_failed;
1323 adapter->tx_map_failed += tx_map_failed;
John Allen3d52b592017-08-02 16:44:14 -05001324 adapter->tx_stats_buffers[queue_num].packets += tx_packets;
1325 adapter->tx_stats_buffers[queue_num].bytes += tx_bytes;
1326 adapter->tx_stats_buffers[queue_num].dropped_packets += tx_dropped;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001327
1328 return ret;
1329}
1330
1331static void ibmvnic_set_multi(struct net_device *netdev)
1332{
1333 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1334 struct netdev_hw_addr *ha;
1335 union ibmvnic_crq crq;
1336
1337 memset(&crq, 0, sizeof(crq));
1338 crq.request_capability.first = IBMVNIC_CRQ_CMD;
1339 crq.request_capability.cmd = REQUEST_CAPABILITY;
1340
1341 if (netdev->flags & IFF_PROMISC) {
1342 if (!adapter->promisc_supported)
1343 return;
1344 } else {
1345 if (netdev->flags & IFF_ALLMULTI) {
1346 /* Accept all multicast */
1347 memset(&crq, 0, sizeof(crq));
1348 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1349 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1350 crq.multicast_ctrl.flags = IBMVNIC_ENABLE_ALL;
1351 ibmvnic_send_crq(adapter, &crq);
1352 } else if (netdev_mc_empty(netdev)) {
1353 /* Reject all multicast */
1354 memset(&crq, 0, sizeof(crq));
1355 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1356 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1357 crq.multicast_ctrl.flags = IBMVNIC_DISABLE_ALL;
1358 ibmvnic_send_crq(adapter, &crq);
1359 } else {
1360 /* Accept one or more multicast(s) */
1361 netdev_for_each_mc_addr(ha, netdev) {
1362 memset(&crq, 0, sizeof(crq));
1363 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1364 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1365 crq.multicast_ctrl.flags = IBMVNIC_ENABLE_MC;
1366 ether_addr_copy(&crq.multicast_ctrl.mac_addr[0],
1367 ha->addr);
1368 ibmvnic_send_crq(adapter, &crq);
1369 }
1370 }
1371 }
1372}
1373
1374static int ibmvnic_set_mac(struct net_device *netdev, void *p)
1375{
1376 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1377 struct sockaddr *addr = p;
1378 union ibmvnic_crq crq;
1379
1380 if (!is_valid_ether_addr(addr->sa_data))
1381 return -EADDRNOTAVAIL;
1382
1383 memset(&crq, 0, sizeof(crq));
1384 crq.change_mac_addr.first = IBMVNIC_CRQ_CMD;
1385 crq.change_mac_addr.cmd = CHANGE_MAC_ADDR;
1386 ether_addr_copy(&crq.change_mac_addr.mac_addr[0], addr->sa_data);
1387 ibmvnic_send_crq(adapter, &crq);
1388 /* netdev->dev_addr is changed in handle_change_mac_rsp function */
1389 return 0;
1390}
1391
Nathan Fontenoted651a12017-05-03 14:04:38 -04001392/**
1393 * do_reset returns zero if we are able to keep processing reset events, or
1394 * non-zero if we hit a fatal error and must halt.
1395 */
1396static int do_reset(struct ibmvnic_adapter *adapter,
1397 struct ibmvnic_rwi *rwi, u32 reset_state)
1398{
1399 struct net_device *netdev = adapter->netdev;
1400 int i, rc;
1401
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001402 netdev_dbg(adapter->netdev, "Re-setting driver (%d)\n",
1403 rwi->reset_reason);
1404
Nathan Fontenoted651a12017-05-03 14:04:38 -04001405 netif_carrier_off(netdev);
1406 adapter->reset_reason = rwi->reset_reason;
1407
1408 if (rwi->reset_reason == VNIC_RESET_MOBILITY) {
1409 rc = ibmvnic_reenable_crq_queue(adapter);
1410 if (rc)
1411 return 0;
1412 }
1413
1414 rc = __ibmvnic_close(netdev);
1415 if (rc)
1416 return rc;
1417
John Allen8cb31cf2017-05-26 10:30:37 -04001418 if (adapter->reset_reason != VNIC_RESET_NON_FATAL) {
1419 /* remove the closed state so when we call open it appears
1420 * we are coming from the probed state.
1421 */
Nathan Fontenoted651a12017-05-03 14:04:38 -04001422 adapter->state = VNIC_PROBED;
John Allen8cb31cf2017-05-26 10:30:37 -04001423
John Allen8cb31cf2017-05-26 10:30:37 -04001424 rc = ibmvnic_init(adapter);
1425 if (rc)
1426 return 0;
1427
1428 /* If the adapter was in PROBE state prior to the reset,
1429 * exit here.
1430 */
1431 if (reset_state == VNIC_PROBED)
1432 return 0;
1433
1434 rc = ibmvnic_login(netdev);
1435 if (rc) {
1436 adapter->state = VNIC_PROBED;
1437 return 0;
1438 }
1439
Nathan Fontenot8c0543a2017-05-26 10:31:06 -04001440 rc = reset_tx_pools(adapter);
1441 if (rc)
1442 return rc;
1443
1444 rc = reset_rx_pools(adapter);
John Allen8cb31cf2017-05-26 10:30:37 -04001445 if (rc)
1446 return rc;
1447
1448 if (reset_state == VNIC_CLOSED)
1449 return 0;
Nathan Fontenoted651a12017-05-03 14:04:38 -04001450 }
1451
Nathan Fontenoted651a12017-05-03 14:04:38 -04001452 rc = __ibmvnic_open(netdev);
1453 if (rc) {
1454 if (list_empty(&adapter->rwi_list))
1455 adapter->state = VNIC_CLOSED;
1456 else
1457 adapter->state = reset_state;
1458
1459 return 0;
1460 }
1461
1462 netif_carrier_on(netdev);
1463
1464 /* kick napi */
1465 for (i = 0; i < adapter->req_rx_queues; i++)
1466 napi_schedule(&adapter->napi[i]);
1467
Nathan Fontenot61d3e1d2017-06-12 20:47:45 -04001468 if (adapter->reset_reason != VNIC_RESET_FAILOVER)
1469 netdev_notify_peers(netdev);
1470
Nathan Fontenoted651a12017-05-03 14:04:38 -04001471 return 0;
1472}
1473
1474static struct ibmvnic_rwi *get_next_rwi(struct ibmvnic_adapter *adapter)
1475{
1476 struct ibmvnic_rwi *rwi;
1477
1478 mutex_lock(&adapter->rwi_lock);
1479
1480 if (!list_empty(&adapter->rwi_list)) {
1481 rwi = list_first_entry(&adapter->rwi_list, struct ibmvnic_rwi,
1482 list);
1483 list_del(&rwi->list);
1484 } else {
1485 rwi = NULL;
1486 }
1487
1488 mutex_unlock(&adapter->rwi_lock);
1489 return rwi;
1490}
1491
1492static void free_all_rwi(struct ibmvnic_adapter *adapter)
1493{
1494 struct ibmvnic_rwi *rwi;
1495
1496 rwi = get_next_rwi(adapter);
1497 while (rwi) {
1498 kfree(rwi);
1499 rwi = get_next_rwi(adapter);
1500 }
1501}
1502
1503static void __ibmvnic_reset(struct work_struct *work)
1504{
1505 struct ibmvnic_rwi *rwi;
1506 struct ibmvnic_adapter *adapter;
1507 struct net_device *netdev;
1508 u32 reset_state;
1509 int rc;
1510
1511 adapter = container_of(work, struct ibmvnic_adapter, ibmvnic_reset);
1512 netdev = adapter->netdev;
1513
1514 mutex_lock(&adapter->reset_lock);
1515 adapter->resetting = true;
1516 reset_state = adapter->state;
1517
1518 rwi = get_next_rwi(adapter);
1519 while (rwi) {
1520 rc = do_reset(adapter, rwi, reset_state);
1521 kfree(rwi);
1522 if (rc)
1523 break;
1524
1525 rwi = get_next_rwi(adapter);
1526 }
1527
1528 if (rc) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001529 netdev_dbg(adapter->netdev, "Reset failed\n");
Nathan Fontenoted651a12017-05-03 14:04:38 -04001530 free_all_rwi(adapter);
Wei Yongjun6d0af072017-05-18 15:24:52 +00001531 mutex_unlock(&adapter->reset_lock);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001532 return;
1533 }
1534
1535 adapter->resetting = false;
1536 mutex_unlock(&adapter->reset_lock);
1537}
1538
1539static void ibmvnic_reset(struct ibmvnic_adapter *adapter,
1540 enum ibmvnic_reset_reason reason)
1541{
1542 struct ibmvnic_rwi *rwi, *tmp;
1543 struct net_device *netdev = adapter->netdev;
1544 struct list_head *entry;
1545
1546 if (adapter->state == VNIC_REMOVING ||
1547 adapter->state == VNIC_REMOVED) {
1548 netdev_dbg(netdev, "Adapter removing, skipping reset\n");
1549 return;
1550 }
1551
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04001552 if (adapter->state == VNIC_PROBING) {
1553 netdev_warn(netdev, "Adapter reset during probe\n");
1554 adapter->init_done_rc = EAGAIN;
1555 return;
1556 }
1557
Nathan Fontenoted651a12017-05-03 14:04:38 -04001558 mutex_lock(&adapter->rwi_lock);
1559
1560 list_for_each(entry, &adapter->rwi_list) {
1561 tmp = list_entry(entry, struct ibmvnic_rwi, list);
1562 if (tmp->reset_reason == reason) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001563 netdev_dbg(netdev, "Skipping matching reset\n");
Nathan Fontenoted651a12017-05-03 14:04:38 -04001564 mutex_unlock(&adapter->rwi_lock);
1565 return;
1566 }
1567 }
1568
1569 rwi = kzalloc(sizeof(*rwi), GFP_KERNEL);
1570 if (!rwi) {
1571 mutex_unlock(&adapter->rwi_lock);
1572 ibmvnic_close(netdev);
1573 return;
1574 }
1575
1576 rwi->reset_reason = reason;
1577 list_add_tail(&rwi->list, &adapter->rwi_list);
1578 mutex_unlock(&adapter->rwi_lock);
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001579
1580 netdev_dbg(adapter->netdev, "Scheduling reset (reason %d)\n", reason);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001581 schedule_work(&adapter->ibmvnic_reset);
1582}
1583
Thomas Falcon032c5e82015-12-21 11:26:06 -06001584static void ibmvnic_tx_timeout(struct net_device *dev)
1585{
1586 struct ibmvnic_adapter *adapter = netdev_priv(dev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001587
Nathan Fontenoted651a12017-05-03 14:04:38 -04001588 ibmvnic_reset(adapter, VNIC_RESET_TIMEOUT);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001589}
1590
1591static void remove_buff_from_pool(struct ibmvnic_adapter *adapter,
1592 struct ibmvnic_rx_buff *rx_buff)
1593{
1594 struct ibmvnic_rx_pool *pool = &adapter->rx_pool[rx_buff->pool_index];
1595
1596 rx_buff->skb = NULL;
1597
1598 pool->free_map[pool->next_alloc] = (int)(rx_buff - pool->rx_buff);
1599 pool->next_alloc = (pool->next_alloc + 1) % pool->size;
1600
1601 atomic_dec(&pool->available);
1602}
1603
1604static int ibmvnic_poll(struct napi_struct *napi, int budget)
1605{
1606 struct net_device *netdev = napi->dev;
1607 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1608 int scrq_num = (int)(napi - adapter->napi);
1609 int frames_processed = 0;
Nathan Fontenot152ce472017-05-26 10:30:54 -04001610
Thomas Falcon032c5e82015-12-21 11:26:06 -06001611restart_poll:
1612 while (frames_processed < budget) {
1613 struct sk_buff *skb;
1614 struct ibmvnic_rx_buff *rx_buff;
1615 union sub_crq *next;
1616 u32 length;
1617 u16 offset;
1618 u8 flags = 0;
1619
Thomas Falcon21ecba62017-06-14 23:50:09 -05001620 if (unlikely(adapter->resetting)) {
1621 enable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
1622 napi_complete_done(napi, frames_processed);
1623 return frames_processed;
1624 }
1625
Thomas Falcon032c5e82015-12-21 11:26:06 -06001626 if (!pending_scrq(adapter, adapter->rx_scrq[scrq_num]))
1627 break;
1628 next = ibmvnic_next_scrq(adapter, adapter->rx_scrq[scrq_num]);
1629 rx_buff =
1630 (struct ibmvnic_rx_buff *)be64_to_cpu(next->
1631 rx_comp.correlator);
1632 /* do error checking */
1633 if (next->rx_comp.rc) {
John Allene1cea2e2017-08-07 15:42:30 -05001634 netdev_dbg(netdev, "rx buffer returned with rc %x\n",
1635 be16_to_cpu(next->rx_comp.rc));
Thomas Falcon032c5e82015-12-21 11:26:06 -06001636 /* free the entry */
1637 next->rx_comp.first = 0;
1638 remove_buff_from_pool(adapter, rx_buff);
Nathan Fontenotca05e312017-05-03 14:05:14 -04001639 continue;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001640 }
1641
1642 length = be32_to_cpu(next->rx_comp.len);
1643 offset = be16_to_cpu(next->rx_comp.off_frame_data);
1644 flags = next->rx_comp.flags;
1645 skb = rx_buff->skb;
1646 skb_copy_to_linear_data(skb, rx_buff->data + offset,
1647 length);
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04001648
1649 /* VLAN Header has been stripped by the system firmware and
1650 * needs to be inserted by the driver
1651 */
1652 if (adapter->rx_vlan_header_insertion &&
1653 (flags & IBMVNIC_VLAN_STRIPPED))
1654 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
1655 ntohs(next->rx_comp.vlan_tci));
1656
Thomas Falcon032c5e82015-12-21 11:26:06 -06001657 /* free the entry */
1658 next->rx_comp.first = 0;
1659 remove_buff_from_pool(adapter, rx_buff);
1660
1661 skb_put(skb, length);
1662 skb->protocol = eth_type_trans(skb, netdev);
Thomas Falcon94ca3052017-05-03 14:05:20 -04001663 skb_record_rx_queue(skb, scrq_num);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001664
1665 if (flags & IBMVNIC_IP_CHKSUM_GOOD &&
1666 flags & IBMVNIC_TCP_UDP_CHKSUM_GOOD) {
1667 skb->ip_summed = CHECKSUM_UNNECESSARY;
1668 }
1669
1670 length = skb->len;
1671 napi_gro_receive(napi, skb); /* send it up */
1672 netdev->stats.rx_packets++;
1673 netdev->stats.rx_bytes += length;
John Allen3d52b592017-08-02 16:44:14 -05001674 adapter->rx_stats_buffers[scrq_num].packets++;
1675 adapter->rx_stats_buffers[scrq_num].bytes += length;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001676 frames_processed++;
1677 }
Nathan Fontenot152ce472017-05-26 10:30:54 -04001678
1679 if (adapter->state != VNIC_CLOSING)
1680 replenish_rx_pool(adapter, &adapter->rx_pool[scrq_num]);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001681
1682 if (frames_processed < budget) {
1683 enable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
Eric Dumazet6ad20162017-01-30 08:22:01 -08001684 napi_complete_done(napi, frames_processed);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001685 if (pending_scrq(adapter, adapter->rx_scrq[scrq_num]) &&
1686 napi_reschedule(napi)) {
1687 disable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
1688 goto restart_poll;
1689 }
1690 }
1691 return frames_processed;
1692}
1693
1694#ifdef CONFIG_NET_POLL_CONTROLLER
1695static void ibmvnic_netpoll_controller(struct net_device *dev)
1696{
1697 struct ibmvnic_adapter *adapter = netdev_priv(dev);
1698 int i;
1699
1700 replenish_pools(netdev_priv(dev));
1701 for (i = 0; i < adapter->req_rx_queues; i++)
1702 ibmvnic_interrupt_rx(adapter->rx_scrq[i]->irq,
1703 adapter->rx_scrq[i]);
1704}
1705#endif
1706
John Allen3a807b72017-06-06 16:55:52 -05001707static int ibmvnic_change_mtu(struct net_device *netdev, int new_mtu)
1708{
1709 return -EOPNOTSUPP;
1710}
1711
Thomas Falcon032c5e82015-12-21 11:26:06 -06001712static const struct net_device_ops ibmvnic_netdev_ops = {
1713 .ndo_open = ibmvnic_open,
1714 .ndo_stop = ibmvnic_close,
1715 .ndo_start_xmit = ibmvnic_xmit,
1716 .ndo_set_rx_mode = ibmvnic_set_multi,
1717 .ndo_set_mac_address = ibmvnic_set_mac,
1718 .ndo_validate_addr = eth_validate_addr,
Thomas Falcon032c5e82015-12-21 11:26:06 -06001719 .ndo_tx_timeout = ibmvnic_tx_timeout,
1720#ifdef CONFIG_NET_POLL_CONTROLLER
1721 .ndo_poll_controller = ibmvnic_netpoll_controller,
1722#endif
John Allen3a807b72017-06-06 16:55:52 -05001723 .ndo_change_mtu = ibmvnic_change_mtu,
Thomas Falcon032c5e82015-12-21 11:26:06 -06001724};
1725
1726/* ethtool functions */
1727
Philippe Reynes8a433792017-01-07 22:37:29 +01001728static int ibmvnic_get_link_ksettings(struct net_device *netdev,
1729 struct ethtool_link_ksettings *cmd)
Thomas Falcon032c5e82015-12-21 11:26:06 -06001730{
Philippe Reynes8a433792017-01-07 22:37:29 +01001731 u32 supported, advertising;
1732
1733 supported = (SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg |
Thomas Falcon032c5e82015-12-21 11:26:06 -06001734 SUPPORTED_FIBRE);
Philippe Reynes8a433792017-01-07 22:37:29 +01001735 advertising = (ADVERTISED_1000baseT_Full | ADVERTISED_Autoneg |
Thomas Falcon032c5e82015-12-21 11:26:06 -06001736 ADVERTISED_FIBRE);
Philippe Reynes8a433792017-01-07 22:37:29 +01001737 cmd->base.speed = SPEED_1000;
1738 cmd->base.duplex = DUPLEX_FULL;
1739 cmd->base.port = PORT_FIBRE;
1740 cmd->base.phy_address = 0;
1741 cmd->base.autoneg = AUTONEG_ENABLE;
1742
1743 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
1744 supported);
1745 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
1746 advertising);
1747
Thomas Falcon032c5e82015-12-21 11:26:06 -06001748 return 0;
1749}
1750
1751static void ibmvnic_get_drvinfo(struct net_device *dev,
1752 struct ethtool_drvinfo *info)
1753{
1754 strlcpy(info->driver, ibmvnic_driver_name, sizeof(info->driver));
1755 strlcpy(info->version, IBMVNIC_DRIVER_VERSION, sizeof(info->version));
1756}
1757
1758static u32 ibmvnic_get_msglevel(struct net_device *netdev)
1759{
1760 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1761
1762 return adapter->msg_enable;
1763}
1764
1765static void ibmvnic_set_msglevel(struct net_device *netdev, u32 data)
1766{
1767 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1768
1769 adapter->msg_enable = data;
1770}
1771
1772static u32 ibmvnic_get_link(struct net_device *netdev)
1773{
1774 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1775
1776 /* Don't need to send a query because we request a logical link up at
1777 * init and then we wait for link state indications
1778 */
1779 return adapter->logical_link_state;
1780}
1781
1782static void ibmvnic_get_ringparam(struct net_device *netdev,
1783 struct ethtool_ringparam *ring)
1784{
John Allenbc131b32017-08-02 16:46:30 -05001785 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1786
1787 ring->rx_max_pending = adapter->max_rx_add_entries_per_subcrq;
1788 ring->tx_max_pending = adapter->max_tx_entries_per_subcrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001789 ring->rx_mini_max_pending = 0;
1790 ring->rx_jumbo_max_pending = 0;
John Allenbc131b32017-08-02 16:46:30 -05001791 ring->rx_pending = adapter->req_rx_add_entries_per_subcrq;
1792 ring->tx_pending = adapter->req_tx_entries_per_subcrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001793 ring->rx_mini_pending = 0;
1794 ring->rx_jumbo_pending = 0;
1795}
1796
John Allenc2dbeb62017-08-02 16:47:17 -05001797static void ibmvnic_get_channels(struct net_device *netdev,
1798 struct ethtool_channels *channels)
1799{
1800 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1801
1802 channels->max_rx = adapter->max_rx_queues;
1803 channels->max_tx = adapter->max_tx_queues;
1804 channels->max_other = 0;
1805 channels->max_combined = 0;
1806 channels->rx_count = adapter->req_rx_queues;
1807 channels->tx_count = adapter->req_tx_queues;
1808 channels->other_count = 0;
1809 channels->combined_count = 0;
1810}
1811
Thomas Falcon032c5e82015-12-21 11:26:06 -06001812static void ibmvnic_get_strings(struct net_device *dev, u32 stringset, u8 *data)
1813{
John Allen3d52b592017-08-02 16:44:14 -05001814 struct ibmvnic_adapter *adapter = netdev_priv(dev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001815 int i;
1816
1817 if (stringset != ETH_SS_STATS)
1818 return;
1819
1820 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++, data += ETH_GSTRING_LEN)
1821 memcpy(data, ibmvnic_stats[i].name, ETH_GSTRING_LEN);
John Allen3d52b592017-08-02 16:44:14 -05001822
1823 for (i = 0; i < adapter->req_tx_queues; i++) {
1824 snprintf(data, ETH_GSTRING_LEN, "tx%d_packets", i);
1825 data += ETH_GSTRING_LEN;
1826
1827 snprintf(data, ETH_GSTRING_LEN, "tx%d_bytes", i);
1828 data += ETH_GSTRING_LEN;
1829
1830 snprintf(data, ETH_GSTRING_LEN, "tx%d_dropped_packets", i);
1831 data += ETH_GSTRING_LEN;
1832 }
1833
1834 for (i = 0; i < adapter->req_rx_queues; i++) {
1835 snprintf(data, ETH_GSTRING_LEN, "rx%d_packets", i);
1836 data += ETH_GSTRING_LEN;
1837
1838 snprintf(data, ETH_GSTRING_LEN, "rx%d_bytes", i);
1839 data += ETH_GSTRING_LEN;
1840
1841 snprintf(data, ETH_GSTRING_LEN, "rx%d_interrupts", i);
1842 data += ETH_GSTRING_LEN;
1843 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001844}
1845
1846static int ibmvnic_get_sset_count(struct net_device *dev, int sset)
1847{
John Allen3d52b592017-08-02 16:44:14 -05001848 struct ibmvnic_adapter *adapter = netdev_priv(dev);
1849
Thomas Falcon032c5e82015-12-21 11:26:06 -06001850 switch (sset) {
1851 case ETH_SS_STATS:
John Allen3d52b592017-08-02 16:44:14 -05001852 return ARRAY_SIZE(ibmvnic_stats) +
1853 adapter->req_tx_queues * NUM_TX_STATS +
1854 adapter->req_rx_queues * NUM_RX_STATS;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001855 default:
1856 return -EOPNOTSUPP;
1857 }
1858}
1859
1860static void ibmvnic_get_ethtool_stats(struct net_device *dev,
1861 struct ethtool_stats *stats, u64 *data)
1862{
1863 struct ibmvnic_adapter *adapter = netdev_priv(dev);
1864 union ibmvnic_crq crq;
John Allen3d52b592017-08-02 16:44:14 -05001865 int i, j;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001866
1867 memset(&crq, 0, sizeof(crq));
1868 crq.request_statistics.first = IBMVNIC_CRQ_CMD;
1869 crq.request_statistics.cmd = REQUEST_STATISTICS;
1870 crq.request_statistics.ioba = cpu_to_be32(adapter->stats_token);
1871 crq.request_statistics.len =
1872 cpu_to_be32(sizeof(struct ibmvnic_statistics));
Thomas Falcon032c5e82015-12-21 11:26:06 -06001873
1874 /* Wait for data to be written */
1875 init_completion(&adapter->stats_done);
Nathan Fontenotdb5d0b52017-02-10 13:45:05 -05001876 ibmvnic_send_crq(adapter, &crq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001877 wait_for_completion(&adapter->stats_done);
1878
1879 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++)
John Allen52da5c12017-08-02 16:45:28 -05001880 data[i] = be64_to_cpu(IBMVNIC_GET_STAT(adapter,
1881 ibmvnic_stats[i].offset));
John Allen3d52b592017-08-02 16:44:14 -05001882
1883 for (j = 0; j < adapter->req_tx_queues; j++) {
1884 data[i] = adapter->tx_stats_buffers[j].packets;
1885 i++;
1886 data[i] = adapter->tx_stats_buffers[j].bytes;
1887 i++;
1888 data[i] = adapter->tx_stats_buffers[j].dropped_packets;
1889 i++;
1890 }
1891
1892 for (j = 0; j < adapter->req_rx_queues; j++) {
1893 data[i] = adapter->rx_stats_buffers[j].packets;
1894 i++;
1895 data[i] = adapter->rx_stats_buffers[j].bytes;
1896 i++;
1897 data[i] = adapter->rx_stats_buffers[j].interrupts;
1898 i++;
1899 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001900}
1901
1902static const struct ethtool_ops ibmvnic_ethtool_ops = {
Thomas Falcon032c5e82015-12-21 11:26:06 -06001903 .get_drvinfo = ibmvnic_get_drvinfo,
1904 .get_msglevel = ibmvnic_get_msglevel,
1905 .set_msglevel = ibmvnic_set_msglevel,
1906 .get_link = ibmvnic_get_link,
1907 .get_ringparam = ibmvnic_get_ringparam,
John Allenc2dbeb62017-08-02 16:47:17 -05001908 .get_channels = ibmvnic_get_channels,
Thomas Falcon032c5e82015-12-21 11:26:06 -06001909 .get_strings = ibmvnic_get_strings,
1910 .get_sset_count = ibmvnic_get_sset_count,
1911 .get_ethtool_stats = ibmvnic_get_ethtool_stats,
Philippe Reynes8a433792017-01-07 22:37:29 +01001912 .get_link_ksettings = ibmvnic_get_link_ksettings,
Thomas Falcon032c5e82015-12-21 11:26:06 -06001913};
1914
1915/* Routines for managing CRQs/sCRQs */
1916
Nathan Fontenot57a49432017-05-26 10:31:12 -04001917static int reset_one_sub_crq_queue(struct ibmvnic_adapter *adapter,
1918 struct ibmvnic_sub_crq_queue *scrq)
1919{
1920 int rc;
1921
1922 if (scrq->irq) {
1923 free_irq(scrq->irq, scrq);
1924 irq_dispose_mapping(scrq->irq);
1925 scrq->irq = 0;
1926 }
1927
Thomas Falconc8b2ad02017-06-14 23:50:07 -05001928 memset(scrq->msgs, 0, 4 * PAGE_SIZE);
Nathan Fontenot57a49432017-05-26 10:31:12 -04001929 scrq->cur = 0;
1930
1931 rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
1932 4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
1933 return rc;
1934}
1935
1936static int reset_sub_crq_queues(struct ibmvnic_adapter *adapter)
1937{
1938 int i, rc;
1939
1940 for (i = 0; i < adapter->req_tx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001941 netdev_dbg(adapter->netdev, "Re-setting tx_scrq[%d]\n", i);
Nathan Fontenot57a49432017-05-26 10:31:12 -04001942 rc = reset_one_sub_crq_queue(adapter, adapter->tx_scrq[i]);
1943 if (rc)
1944 return rc;
1945 }
1946
1947 for (i = 0; i < adapter->req_rx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001948 netdev_dbg(adapter->netdev, "Re-setting rx_scrq[%d]\n", i);
Nathan Fontenot57a49432017-05-26 10:31:12 -04001949 rc = reset_one_sub_crq_queue(adapter, adapter->rx_scrq[i]);
1950 if (rc)
1951 return rc;
1952 }
1953
Nathan Fontenot57a49432017-05-26 10:31:12 -04001954 return rc;
1955}
1956
Thomas Falcon032c5e82015-12-21 11:26:06 -06001957static void release_sub_crq_queue(struct ibmvnic_adapter *adapter,
1958 struct ibmvnic_sub_crq_queue *scrq)
1959{
1960 struct device *dev = &adapter->vdev->dev;
1961 long rc;
1962
1963 netdev_dbg(adapter->netdev, "Releasing sub-CRQ\n");
1964
1965 /* Close the sub-crqs */
1966 do {
1967 rc = plpar_hcall_norets(H_FREE_SUB_CRQ,
1968 adapter->vdev->unit_address,
1969 scrq->crq_num);
1970 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
1971
Thomas Falconffa73852017-04-19 13:44:29 -04001972 if (rc) {
1973 netdev_err(adapter->netdev,
1974 "Failed to release sub-CRQ %16lx, rc = %ld\n",
1975 scrq->crq_num, rc);
1976 }
1977
Thomas Falcon032c5e82015-12-21 11:26:06 -06001978 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
1979 DMA_BIDIRECTIONAL);
1980 free_pages((unsigned long)scrq->msgs, 2);
1981 kfree(scrq);
1982}
1983
1984static struct ibmvnic_sub_crq_queue *init_sub_crq_queue(struct ibmvnic_adapter
1985 *adapter)
1986{
1987 struct device *dev = &adapter->vdev->dev;
1988 struct ibmvnic_sub_crq_queue *scrq;
1989 int rc;
1990
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04001991 scrq = kzalloc(sizeof(*scrq), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001992 if (!scrq)
1993 return NULL;
1994
Nathan Fontenot7f7adc52017-04-19 13:45:16 -04001995 scrq->msgs =
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04001996 (union sub_crq *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, 2);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001997 if (!scrq->msgs) {
1998 dev_warn(dev, "Couldn't allocate crq queue messages page\n");
1999 goto zero_page_failed;
2000 }
2001
2002 scrq->msg_token = dma_map_single(dev, scrq->msgs, 4 * PAGE_SIZE,
2003 DMA_BIDIRECTIONAL);
2004 if (dma_mapping_error(dev, scrq->msg_token)) {
2005 dev_warn(dev, "Couldn't map crq queue messages page\n");
2006 goto map_failed;
2007 }
2008
2009 rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
2010 4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
2011
2012 if (rc == H_RESOURCE)
2013 rc = ibmvnic_reset_crq(adapter);
2014
2015 if (rc == H_CLOSED) {
2016 dev_warn(dev, "Partner adapter not ready, waiting.\n");
2017 } else if (rc) {
2018 dev_warn(dev, "Error %d registering sub-crq\n", rc);
2019 goto reg_failed;
2020 }
2021
Thomas Falcon032c5e82015-12-21 11:26:06 -06002022 scrq->adapter = adapter;
2023 scrq->size = 4 * PAGE_SIZE / sizeof(*scrq->msgs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002024 spin_lock_init(&scrq->lock);
2025
2026 netdev_dbg(adapter->netdev,
2027 "sub-crq initialized, num %lx, hw_irq=%lx, irq=%x\n",
2028 scrq->crq_num, scrq->hw_irq, scrq->irq);
2029
2030 return scrq;
2031
Thomas Falcon032c5e82015-12-21 11:26:06 -06002032reg_failed:
2033 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
2034 DMA_BIDIRECTIONAL);
2035map_failed:
2036 free_pages((unsigned long)scrq->msgs, 2);
2037zero_page_failed:
2038 kfree(scrq);
2039
2040 return NULL;
2041}
2042
2043static void release_sub_crqs(struct ibmvnic_adapter *adapter)
2044{
2045 int i;
2046
2047 if (adapter->tx_scrq) {
Nathan Fontenotb5108882017-03-30 02:49:18 -04002048 for (i = 0; i < adapter->req_tx_queues; i++) {
2049 if (!adapter->tx_scrq[i])
2050 continue;
2051
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002052 netdev_dbg(adapter->netdev, "Releasing tx_scrq[%d]\n",
2053 i);
Nathan Fontenotb5108882017-03-30 02:49:18 -04002054 if (adapter->tx_scrq[i]->irq) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06002055 free_irq(adapter->tx_scrq[i]->irq,
2056 adapter->tx_scrq[i]);
Thomas Falcon88eb98a2016-07-06 15:35:16 -05002057 irq_dispose_mapping(adapter->tx_scrq[i]->irq);
Nathan Fontenotb5108882017-03-30 02:49:18 -04002058 adapter->tx_scrq[i]->irq = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002059 }
Nathan Fontenotb5108882017-03-30 02:49:18 -04002060
2061 release_sub_crq_queue(adapter, adapter->tx_scrq[i]);
2062 }
2063
Nathan Fontenot9501df32017-03-15 23:38:07 -04002064 kfree(adapter->tx_scrq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002065 adapter->tx_scrq = NULL;
2066 }
2067
2068 if (adapter->rx_scrq) {
Nathan Fontenotb5108882017-03-30 02:49:18 -04002069 for (i = 0; i < adapter->req_rx_queues; i++) {
2070 if (!adapter->rx_scrq[i])
2071 continue;
2072
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002073 netdev_dbg(adapter->netdev, "Releasing rx_scrq[%d]\n",
2074 i);
Nathan Fontenotb5108882017-03-30 02:49:18 -04002075 if (adapter->rx_scrq[i]->irq) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06002076 free_irq(adapter->rx_scrq[i]->irq,
2077 adapter->rx_scrq[i]);
Thomas Falcon88eb98a2016-07-06 15:35:16 -05002078 irq_dispose_mapping(adapter->rx_scrq[i]->irq);
Nathan Fontenotb5108882017-03-30 02:49:18 -04002079 adapter->rx_scrq[i]->irq = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002080 }
Nathan Fontenotb5108882017-03-30 02:49:18 -04002081
2082 release_sub_crq_queue(adapter, adapter->rx_scrq[i]);
2083 }
2084
Nathan Fontenot9501df32017-03-15 23:38:07 -04002085 kfree(adapter->rx_scrq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002086 adapter->rx_scrq = NULL;
2087 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002088}
2089
2090static int disable_scrq_irq(struct ibmvnic_adapter *adapter,
2091 struct ibmvnic_sub_crq_queue *scrq)
2092{
2093 struct device *dev = &adapter->vdev->dev;
2094 unsigned long rc;
2095
2096 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
2097 H_DISABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
2098 if (rc)
2099 dev_err(dev, "Couldn't disable scrq irq 0x%lx. rc=%ld\n",
2100 scrq->hw_irq, rc);
2101 return rc;
2102}
2103
2104static int enable_scrq_irq(struct ibmvnic_adapter *adapter,
2105 struct ibmvnic_sub_crq_queue *scrq)
2106{
2107 struct device *dev = &adapter->vdev->dev;
2108 unsigned long rc;
2109
2110 if (scrq->hw_irq > 0x100000000ULL) {
2111 dev_err(dev, "bad hw_irq = %lx\n", scrq->hw_irq);
2112 return 1;
2113 }
2114
2115 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
2116 H_ENABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
2117 if (rc)
2118 dev_err(dev, "Couldn't enable scrq irq 0x%lx. rc=%ld\n",
2119 scrq->hw_irq, rc);
2120 return rc;
2121}
2122
2123static int ibmvnic_complete_tx(struct ibmvnic_adapter *adapter,
2124 struct ibmvnic_sub_crq_queue *scrq)
2125{
2126 struct device *dev = &adapter->vdev->dev;
2127 struct ibmvnic_tx_buff *txbuff;
2128 union sub_crq *next;
2129 int index;
2130 int i, j;
Thomas Falconad7775d2016-04-01 17:20:34 -05002131 u8 first;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002132
2133restart_loop:
2134 while (pending_scrq(adapter, scrq)) {
2135 unsigned int pool = scrq->pool_index;
2136
2137 next = ibmvnic_next_scrq(adapter, scrq);
2138 for (i = 0; i < next->tx_comp.num_comps; i++) {
2139 if (next->tx_comp.rcs[i]) {
2140 dev_err(dev, "tx error %x\n",
2141 next->tx_comp.rcs[i]);
2142 continue;
2143 }
2144 index = be32_to_cpu(next->tx_comp.correlators[i]);
2145 txbuff = &adapter->tx_pool[pool].tx_buff[index];
2146
2147 for (j = 0; j < IBMVNIC_MAX_FRAGS_PER_CRQ; j++) {
2148 if (!txbuff->data_dma[j])
2149 continue;
2150
2151 txbuff->data_dma[j] = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002152 }
Thomas Falconad7775d2016-04-01 17:20:34 -05002153 /* if sub_crq was sent indirectly */
2154 first = txbuff->indir_arr[0].generic.first;
2155 if (first == IBMVNIC_CRQ_CMD) {
2156 dma_unmap_single(dev, txbuff->indir_dma,
2157 sizeof(txbuff->indir_arr),
2158 DMA_TO_DEVICE);
2159 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002160
Thomas Falcon142c0ac2017-03-05 12:18:41 -06002161 if (txbuff->last_frag) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06002162 dev_kfree_skb_any(txbuff->skb);
Nathan Fontenot7c3e7de2017-05-03 14:05:25 -04002163 txbuff->skb = NULL;
Thomas Falcon142c0ac2017-03-05 12:18:41 -06002164 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002165
2166 adapter->tx_pool[pool].free_map[adapter->tx_pool[pool].
2167 producer_index] = index;
2168 adapter->tx_pool[pool].producer_index =
2169 (adapter->tx_pool[pool].producer_index + 1) %
Thomas Falcon068d9f92017-03-05 12:18:42 -06002170 adapter->req_tx_entries_per_subcrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002171 }
2172 /* remove tx_comp scrq*/
2173 next->tx_comp.first = 0;
Nathan Fontenot7c3e7de2017-05-03 14:05:25 -04002174
2175 if (atomic_sub_return(next->tx_comp.num_comps, &scrq->used) <=
2176 (adapter->req_tx_entries_per_subcrq / 2) &&
2177 __netif_subqueue_stopped(adapter->netdev,
2178 scrq->pool_index)) {
2179 netif_wake_subqueue(adapter->netdev, scrq->pool_index);
2180 netdev_info(adapter->netdev, "Started queue %d\n",
2181 scrq->pool_index);
2182 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002183 }
2184
2185 enable_scrq_irq(adapter, scrq);
2186
2187 if (pending_scrq(adapter, scrq)) {
2188 disable_scrq_irq(adapter, scrq);
2189 goto restart_loop;
2190 }
2191
2192 return 0;
2193}
2194
2195static irqreturn_t ibmvnic_interrupt_tx(int irq, void *instance)
2196{
2197 struct ibmvnic_sub_crq_queue *scrq = instance;
2198 struct ibmvnic_adapter *adapter = scrq->adapter;
2199
2200 disable_scrq_irq(adapter, scrq);
2201 ibmvnic_complete_tx(adapter, scrq);
2202
2203 return IRQ_HANDLED;
2204}
2205
2206static irqreturn_t ibmvnic_interrupt_rx(int irq, void *instance)
2207{
2208 struct ibmvnic_sub_crq_queue *scrq = instance;
2209 struct ibmvnic_adapter *adapter = scrq->adapter;
2210
John Allen3d52b592017-08-02 16:44:14 -05002211 adapter->rx_stats_buffers[scrq->scrq_num].interrupts++;
2212
Thomas Falcon032c5e82015-12-21 11:26:06 -06002213 if (napi_schedule_prep(&adapter->napi[scrq->scrq_num])) {
2214 disable_scrq_irq(adapter, scrq);
2215 __napi_schedule(&adapter->napi[scrq->scrq_num]);
2216 }
2217
2218 return IRQ_HANDLED;
2219}
2220
Thomas Falconea22d512016-07-06 15:35:17 -05002221static int init_sub_crq_irqs(struct ibmvnic_adapter *adapter)
2222{
2223 struct device *dev = &adapter->vdev->dev;
2224 struct ibmvnic_sub_crq_queue *scrq;
2225 int i = 0, j = 0;
2226 int rc = 0;
2227
2228 for (i = 0; i < adapter->req_tx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002229 netdev_dbg(adapter->netdev, "Initializing tx_scrq[%d] irq\n",
2230 i);
Thomas Falconea22d512016-07-06 15:35:17 -05002231 scrq = adapter->tx_scrq[i];
2232 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
2233
Michael Ellerman99c17902016-09-10 19:59:05 +10002234 if (!scrq->irq) {
Thomas Falconea22d512016-07-06 15:35:17 -05002235 rc = -EINVAL;
2236 dev_err(dev, "Error mapping irq\n");
2237 goto req_tx_irq_failed;
2238 }
2239
2240 rc = request_irq(scrq->irq, ibmvnic_interrupt_tx,
2241 0, "ibmvnic_tx", scrq);
2242
2243 if (rc) {
2244 dev_err(dev, "Couldn't register tx irq 0x%x. rc=%d\n",
2245 scrq->irq, rc);
2246 irq_dispose_mapping(scrq->irq);
2247 goto req_rx_irq_failed;
2248 }
2249 }
2250
2251 for (i = 0; i < adapter->req_rx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002252 netdev_dbg(adapter->netdev, "Initializing rx_scrq[%d] irq\n",
2253 i);
Thomas Falconea22d512016-07-06 15:35:17 -05002254 scrq = adapter->rx_scrq[i];
2255 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
Michael Ellerman99c17902016-09-10 19:59:05 +10002256 if (!scrq->irq) {
Thomas Falconea22d512016-07-06 15:35:17 -05002257 rc = -EINVAL;
2258 dev_err(dev, "Error mapping irq\n");
2259 goto req_rx_irq_failed;
2260 }
2261 rc = request_irq(scrq->irq, ibmvnic_interrupt_rx,
2262 0, "ibmvnic_rx", scrq);
2263 if (rc) {
2264 dev_err(dev, "Couldn't register rx 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 return rc;
2271
2272req_rx_irq_failed:
Thomas Falcon8bf371e2016-10-27 12:28:52 -05002273 for (j = 0; j < i; j++) {
Thomas Falconea22d512016-07-06 15:35:17 -05002274 free_irq(adapter->rx_scrq[j]->irq, adapter->rx_scrq[j]);
2275 irq_dispose_mapping(adapter->rx_scrq[j]->irq);
Thomas Falcon8bf371e2016-10-27 12:28:52 -05002276 }
Thomas Falconea22d512016-07-06 15:35:17 -05002277 i = adapter->req_tx_queues;
2278req_tx_irq_failed:
Thomas Falcon8bf371e2016-10-27 12:28:52 -05002279 for (j = 0; j < i; j++) {
Thomas Falconea22d512016-07-06 15:35:17 -05002280 free_irq(adapter->tx_scrq[j]->irq, adapter->tx_scrq[j]);
2281 irq_dispose_mapping(adapter->rx_scrq[j]->irq);
Thomas Falcon8bf371e2016-10-27 12:28:52 -05002282 }
Nathan Fontenotb5108882017-03-30 02:49:18 -04002283 release_sub_crqs(adapter);
Thomas Falconea22d512016-07-06 15:35:17 -05002284 return rc;
2285}
2286
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002287static int init_sub_crqs(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002288{
2289 struct device *dev = &adapter->vdev->dev;
2290 struct ibmvnic_sub_crq_queue **allqueues;
2291 int registered_queues = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002292 int total_queues;
2293 int more = 0;
Thomas Falconea22d512016-07-06 15:35:17 -05002294 int i;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002295
Thomas Falcon032c5e82015-12-21 11:26:06 -06002296 total_queues = adapter->req_tx_queues + adapter->req_rx_queues;
2297
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04002298 allqueues = kcalloc(total_queues, sizeof(*allqueues), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002299 if (!allqueues)
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002300 return -1;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002301
2302 for (i = 0; i < total_queues; i++) {
2303 allqueues[i] = init_sub_crq_queue(adapter);
2304 if (!allqueues[i]) {
2305 dev_warn(dev, "Couldn't allocate all sub-crqs\n");
2306 break;
2307 }
2308 registered_queues++;
2309 }
2310
2311 /* Make sure we were able to register the minimum number of queues */
2312 if (registered_queues <
2313 adapter->min_tx_queues + adapter->min_rx_queues) {
2314 dev_err(dev, "Fatal: Couldn't init min number of sub-crqs\n");
2315 goto tx_failed;
2316 }
2317
2318 /* Distribute the failed allocated queues*/
2319 for (i = 0; i < total_queues - registered_queues + more ; i++) {
2320 netdev_dbg(adapter->netdev, "Reducing number of queues\n");
2321 switch (i % 3) {
2322 case 0:
2323 if (adapter->req_rx_queues > adapter->min_rx_queues)
2324 adapter->req_rx_queues--;
2325 else
2326 more++;
2327 break;
2328 case 1:
2329 if (adapter->req_tx_queues > adapter->min_tx_queues)
2330 adapter->req_tx_queues--;
2331 else
2332 more++;
2333 break;
2334 }
2335 }
2336
2337 adapter->tx_scrq = kcalloc(adapter->req_tx_queues,
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04002338 sizeof(*adapter->tx_scrq), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002339 if (!adapter->tx_scrq)
2340 goto tx_failed;
2341
2342 for (i = 0; i < adapter->req_tx_queues; i++) {
2343 adapter->tx_scrq[i] = allqueues[i];
2344 adapter->tx_scrq[i]->pool_index = i;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002345 }
2346
2347 adapter->rx_scrq = kcalloc(adapter->req_rx_queues,
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04002348 sizeof(*adapter->rx_scrq), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002349 if (!adapter->rx_scrq)
2350 goto rx_failed;
2351
2352 for (i = 0; i < adapter->req_rx_queues; i++) {
2353 adapter->rx_scrq[i] = allqueues[i + adapter->req_tx_queues];
2354 adapter->rx_scrq[i]->scrq_num = i;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002355 }
2356
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002357 kfree(allqueues);
2358 return 0;
2359
2360rx_failed:
2361 kfree(adapter->tx_scrq);
2362 adapter->tx_scrq = NULL;
2363tx_failed:
2364 for (i = 0; i < registered_queues; i++)
2365 release_sub_crq_queue(adapter, allqueues[i]);
2366 kfree(allqueues);
2367 return -1;
2368}
2369
2370static void ibmvnic_send_req_caps(struct ibmvnic_adapter *adapter, int retry)
2371{
2372 struct device *dev = &adapter->vdev->dev;
2373 union ibmvnic_crq crq;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002374
2375 if (!retry) {
2376 /* Sub-CRQ entries are 32 byte long */
2377 int entries_page = 4 * PAGE_SIZE / (sizeof(u64) * 4);
2378
2379 if (adapter->min_tx_entries_per_subcrq > entries_page ||
2380 adapter->min_rx_add_entries_per_subcrq > entries_page) {
2381 dev_err(dev, "Fatal, invalid entries per sub-crq\n");
2382 return;
2383 }
2384
2385 /* Get the minimum between the queried max and the entries
2386 * that fit in our PAGE_SIZE
2387 */
2388 adapter->req_tx_entries_per_subcrq =
2389 adapter->max_tx_entries_per_subcrq > entries_page ?
2390 entries_page : adapter->max_tx_entries_per_subcrq;
2391 adapter->req_rx_add_entries_per_subcrq =
2392 adapter->max_rx_add_entries_per_subcrq > entries_page ?
2393 entries_page : adapter->max_rx_add_entries_per_subcrq;
2394
2395 adapter->req_tx_queues = adapter->opt_tx_comp_sub_queues;
2396 adapter->req_rx_queues = adapter->opt_rx_comp_queues;
2397 adapter->req_rx_add_queues = adapter->max_rx_add_queues;
2398
2399 adapter->req_mtu = adapter->netdev->mtu + ETH_HLEN;
2400 }
2401
Thomas Falcon032c5e82015-12-21 11:26:06 -06002402 memset(&crq, 0, sizeof(crq));
2403 crq.request_capability.first = IBMVNIC_CRQ_CMD;
2404 crq.request_capability.cmd = REQUEST_CAPABILITY;
2405
2406 crq.request_capability.capability = cpu_to_be16(REQ_TX_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06002407 crq.request_capability.number = cpu_to_be64(adapter->req_tx_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06002408 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002409 ibmvnic_send_crq(adapter, &crq);
2410
2411 crq.request_capability.capability = cpu_to_be16(REQ_RX_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06002412 crq.request_capability.number = cpu_to_be64(adapter->req_rx_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06002413 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002414 ibmvnic_send_crq(adapter, &crq);
2415
2416 crq.request_capability.capability = cpu_to_be16(REQ_RX_ADD_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06002417 crq.request_capability.number = cpu_to_be64(adapter->req_rx_add_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06002418 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002419 ibmvnic_send_crq(adapter, &crq);
2420
2421 crq.request_capability.capability =
2422 cpu_to_be16(REQ_TX_ENTRIES_PER_SUBCRQ);
2423 crq.request_capability.number =
Thomas Falconde89e852016-03-01 10:20:09 -06002424 cpu_to_be64(adapter->req_tx_entries_per_subcrq);
Thomas Falcon901e0402017-02-15 12:17:59 -06002425 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002426 ibmvnic_send_crq(adapter, &crq);
2427
2428 crq.request_capability.capability =
2429 cpu_to_be16(REQ_RX_ADD_ENTRIES_PER_SUBCRQ);
2430 crq.request_capability.number =
Thomas Falconde89e852016-03-01 10:20:09 -06002431 cpu_to_be64(adapter->req_rx_add_entries_per_subcrq);
Thomas Falcon901e0402017-02-15 12:17:59 -06002432 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002433 ibmvnic_send_crq(adapter, &crq);
2434
2435 crq.request_capability.capability = cpu_to_be16(REQ_MTU);
Thomas Falconde89e852016-03-01 10:20:09 -06002436 crq.request_capability.number = cpu_to_be64(adapter->req_mtu);
Thomas Falcon901e0402017-02-15 12:17:59 -06002437 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002438 ibmvnic_send_crq(adapter, &crq);
2439
2440 if (adapter->netdev->flags & IFF_PROMISC) {
2441 if (adapter->promisc_supported) {
2442 crq.request_capability.capability =
2443 cpu_to_be16(PROMISC_REQUESTED);
Thomas Falconde89e852016-03-01 10:20:09 -06002444 crq.request_capability.number = cpu_to_be64(1);
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 } else {
2449 crq.request_capability.capability =
2450 cpu_to_be16(PROMISC_REQUESTED);
Thomas Falconde89e852016-03-01 10:20:09 -06002451 crq.request_capability.number = cpu_to_be64(0);
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 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002455}
2456
2457static int pending_scrq(struct ibmvnic_adapter *adapter,
2458 struct ibmvnic_sub_crq_queue *scrq)
2459{
2460 union sub_crq *entry = &scrq->msgs[scrq->cur];
2461
Thomas Falcon1cf9cc72017-06-14 23:50:08 -05002462 if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002463 return 1;
2464 else
2465 return 0;
2466}
2467
2468static union sub_crq *ibmvnic_next_scrq(struct ibmvnic_adapter *adapter,
2469 struct ibmvnic_sub_crq_queue *scrq)
2470{
2471 union sub_crq *entry;
2472 unsigned long flags;
2473
2474 spin_lock_irqsave(&scrq->lock, flags);
2475 entry = &scrq->msgs[scrq->cur];
2476 if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP) {
2477 if (++scrq->cur == scrq->size)
2478 scrq->cur = 0;
2479 } else {
2480 entry = NULL;
2481 }
2482 spin_unlock_irqrestore(&scrq->lock, flags);
2483
2484 return entry;
2485}
2486
2487static union ibmvnic_crq *ibmvnic_next_crq(struct ibmvnic_adapter *adapter)
2488{
2489 struct ibmvnic_crq_queue *queue = &adapter->crq;
2490 union ibmvnic_crq *crq;
2491
2492 crq = &queue->msgs[queue->cur];
2493 if (crq->generic.first & IBMVNIC_CRQ_CMD_RSP) {
2494 if (++queue->cur == queue->size)
2495 queue->cur = 0;
2496 } else {
2497 crq = NULL;
2498 }
2499
2500 return crq;
2501}
2502
2503static int send_subcrq(struct ibmvnic_adapter *adapter, u64 remote_handle,
2504 union sub_crq *sub_crq)
2505{
2506 unsigned int ua = adapter->vdev->unit_address;
2507 struct device *dev = &adapter->vdev->dev;
2508 u64 *u64_crq = (u64 *)sub_crq;
2509 int rc;
2510
2511 netdev_dbg(adapter->netdev,
2512 "Sending sCRQ %016lx: %016lx %016lx %016lx %016lx\n",
2513 (unsigned long int)cpu_to_be64(remote_handle),
2514 (unsigned long int)cpu_to_be64(u64_crq[0]),
2515 (unsigned long int)cpu_to_be64(u64_crq[1]),
2516 (unsigned long int)cpu_to_be64(u64_crq[2]),
2517 (unsigned long int)cpu_to_be64(u64_crq[3]));
2518
2519 /* Make sure the hypervisor sees the complete request */
2520 mb();
2521
2522 rc = plpar_hcall_norets(H_SEND_SUB_CRQ, ua,
2523 cpu_to_be64(remote_handle),
2524 cpu_to_be64(u64_crq[0]),
2525 cpu_to_be64(u64_crq[1]),
2526 cpu_to_be64(u64_crq[2]),
2527 cpu_to_be64(u64_crq[3]));
2528
2529 if (rc) {
2530 if (rc == H_CLOSED)
2531 dev_warn(dev, "CRQ Queue closed\n");
2532 dev_err(dev, "Send error (rc=%d)\n", rc);
2533 }
2534
2535 return rc;
2536}
2537
Thomas Falconad7775d2016-04-01 17:20:34 -05002538static int send_subcrq_indirect(struct ibmvnic_adapter *adapter,
2539 u64 remote_handle, u64 ioba, u64 num_entries)
2540{
2541 unsigned int ua = adapter->vdev->unit_address;
2542 struct device *dev = &adapter->vdev->dev;
2543 int rc;
2544
2545 /* Make sure the hypervisor sees the complete request */
2546 mb();
2547 rc = plpar_hcall_norets(H_SEND_SUB_CRQ_INDIRECT, ua,
2548 cpu_to_be64(remote_handle),
2549 ioba, num_entries);
2550
2551 if (rc) {
2552 if (rc == H_CLOSED)
2553 dev_warn(dev, "CRQ Queue closed\n");
2554 dev_err(dev, "Send (indirect) error (rc=%d)\n", rc);
2555 }
2556
2557 return rc;
2558}
2559
Thomas Falcon032c5e82015-12-21 11:26:06 -06002560static int ibmvnic_send_crq(struct ibmvnic_adapter *adapter,
2561 union ibmvnic_crq *crq)
2562{
2563 unsigned int ua = adapter->vdev->unit_address;
2564 struct device *dev = &adapter->vdev->dev;
2565 u64 *u64_crq = (u64 *)crq;
2566 int rc;
2567
2568 netdev_dbg(adapter->netdev, "Sending CRQ: %016lx %016lx\n",
2569 (unsigned long int)cpu_to_be64(u64_crq[0]),
2570 (unsigned long int)cpu_to_be64(u64_crq[1]));
2571
2572 /* Make sure the hypervisor sees the complete request */
2573 mb();
2574
2575 rc = plpar_hcall_norets(H_SEND_CRQ, ua,
2576 cpu_to_be64(u64_crq[0]),
2577 cpu_to_be64(u64_crq[1]));
2578
2579 if (rc) {
2580 if (rc == H_CLOSED)
2581 dev_warn(dev, "CRQ Queue closed\n");
2582 dev_warn(dev, "Send error (rc=%d)\n", rc);
2583 }
2584
2585 return rc;
2586}
2587
2588static int ibmvnic_send_crq_init(struct ibmvnic_adapter *adapter)
2589{
2590 union ibmvnic_crq crq;
2591
2592 memset(&crq, 0, sizeof(crq));
2593 crq.generic.first = IBMVNIC_CRQ_INIT_CMD;
2594 crq.generic.cmd = IBMVNIC_CRQ_INIT;
2595 netdev_dbg(adapter->netdev, "Sending CRQ init\n");
2596
2597 return ibmvnic_send_crq(adapter, &crq);
2598}
2599
Thomas Falcon032c5e82015-12-21 11:26:06 -06002600static int send_version_xchg(struct ibmvnic_adapter *adapter)
2601{
2602 union ibmvnic_crq crq;
2603
2604 memset(&crq, 0, sizeof(crq));
2605 crq.version_exchange.first = IBMVNIC_CRQ_CMD;
2606 crq.version_exchange.cmd = VERSION_EXCHANGE;
2607 crq.version_exchange.version = cpu_to_be16(ibmvnic_version);
2608
2609 return ibmvnic_send_crq(adapter, &crq);
2610}
2611
2612static void send_login(struct ibmvnic_adapter *adapter)
2613{
2614 struct ibmvnic_login_rsp_buffer *login_rsp_buffer;
2615 struct ibmvnic_login_buffer *login_buffer;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002616 struct device *dev = &adapter->vdev->dev;
2617 dma_addr_t rsp_buffer_token;
2618 dma_addr_t buffer_token;
2619 size_t rsp_buffer_size;
2620 union ibmvnic_crq crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002621 size_t buffer_size;
2622 __be64 *tx_list_p;
2623 __be64 *rx_list_p;
2624 int i;
2625
2626 buffer_size =
2627 sizeof(struct ibmvnic_login_buffer) +
2628 sizeof(u64) * (adapter->req_tx_queues + adapter->req_rx_queues);
2629
2630 login_buffer = kmalloc(buffer_size, GFP_ATOMIC);
2631 if (!login_buffer)
2632 goto buf_alloc_failed;
2633
2634 buffer_token = dma_map_single(dev, login_buffer, buffer_size,
2635 DMA_TO_DEVICE);
2636 if (dma_mapping_error(dev, buffer_token)) {
2637 dev_err(dev, "Couldn't map login buffer\n");
2638 goto buf_map_failed;
2639 }
2640
John Allen498cd8e2016-04-06 11:49:55 -05002641 rsp_buffer_size = sizeof(struct ibmvnic_login_rsp_buffer) +
2642 sizeof(u64) * adapter->req_tx_queues +
2643 sizeof(u64) * adapter->req_rx_queues +
2644 sizeof(u64) * adapter->req_rx_queues +
2645 sizeof(u8) * IBMVNIC_TX_DESC_VERSIONS;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002646
2647 login_rsp_buffer = kmalloc(rsp_buffer_size, GFP_ATOMIC);
2648 if (!login_rsp_buffer)
2649 goto buf_rsp_alloc_failed;
2650
2651 rsp_buffer_token = dma_map_single(dev, login_rsp_buffer,
2652 rsp_buffer_size, DMA_FROM_DEVICE);
2653 if (dma_mapping_error(dev, rsp_buffer_token)) {
2654 dev_err(dev, "Couldn't map login rsp buffer\n");
2655 goto buf_rsp_map_failed;
2656 }
Nathan Fontenot661a2622017-04-19 13:44:58 -04002657
Thomas Falcon032c5e82015-12-21 11:26:06 -06002658 adapter->login_buf = login_buffer;
2659 adapter->login_buf_token = buffer_token;
2660 adapter->login_buf_sz = buffer_size;
2661 adapter->login_rsp_buf = login_rsp_buffer;
2662 adapter->login_rsp_buf_token = rsp_buffer_token;
2663 adapter->login_rsp_buf_sz = rsp_buffer_size;
2664
2665 login_buffer->len = cpu_to_be32(buffer_size);
2666 login_buffer->version = cpu_to_be32(INITIAL_VERSION_LB);
2667 login_buffer->num_txcomp_subcrqs = cpu_to_be32(adapter->req_tx_queues);
2668 login_buffer->off_txcomp_subcrqs =
2669 cpu_to_be32(sizeof(struct ibmvnic_login_buffer));
2670 login_buffer->num_rxcomp_subcrqs = cpu_to_be32(adapter->req_rx_queues);
2671 login_buffer->off_rxcomp_subcrqs =
2672 cpu_to_be32(sizeof(struct ibmvnic_login_buffer) +
2673 sizeof(u64) * adapter->req_tx_queues);
2674 login_buffer->login_rsp_ioba = cpu_to_be32(rsp_buffer_token);
2675 login_buffer->login_rsp_len = cpu_to_be32(rsp_buffer_size);
2676
2677 tx_list_p = (__be64 *)((char *)login_buffer +
2678 sizeof(struct ibmvnic_login_buffer));
2679 rx_list_p = (__be64 *)((char *)login_buffer +
2680 sizeof(struct ibmvnic_login_buffer) +
2681 sizeof(u64) * adapter->req_tx_queues);
2682
2683 for (i = 0; i < adapter->req_tx_queues; i++) {
2684 if (adapter->tx_scrq[i]) {
2685 tx_list_p[i] = cpu_to_be64(adapter->tx_scrq[i]->
2686 crq_num);
2687 }
2688 }
2689
2690 for (i = 0; i < adapter->req_rx_queues; i++) {
2691 if (adapter->rx_scrq[i]) {
2692 rx_list_p[i] = cpu_to_be64(adapter->rx_scrq[i]->
2693 crq_num);
2694 }
2695 }
2696
2697 netdev_dbg(adapter->netdev, "Login Buffer:\n");
2698 for (i = 0; i < (adapter->login_buf_sz - 1) / 8 + 1; i++) {
2699 netdev_dbg(adapter->netdev, "%016lx\n",
2700 ((unsigned long int *)(adapter->login_buf))[i]);
2701 }
2702
2703 memset(&crq, 0, sizeof(crq));
2704 crq.login.first = IBMVNIC_CRQ_CMD;
2705 crq.login.cmd = LOGIN;
2706 crq.login.ioba = cpu_to_be32(buffer_token);
2707 crq.login.len = cpu_to_be32(buffer_size);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002708 ibmvnic_send_crq(adapter, &crq);
2709
2710 return;
2711
Thomas Falcon032c5e82015-12-21 11:26:06 -06002712buf_rsp_map_failed:
2713 kfree(login_rsp_buffer);
2714buf_rsp_alloc_failed:
2715 dma_unmap_single(dev, buffer_token, buffer_size, DMA_TO_DEVICE);
2716buf_map_failed:
2717 kfree(login_buffer);
2718buf_alloc_failed:
2719 return;
2720}
2721
2722static void send_request_map(struct ibmvnic_adapter *adapter, dma_addr_t addr,
2723 u32 len, u8 map_id)
2724{
2725 union ibmvnic_crq crq;
2726
2727 memset(&crq, 0, sizeof(crq));
2728 crq.request_map.first = IBMVNIC_CRQ_CMD;
2729 crq.request_map.cmd = REQUEST_MAP;
2730 crq.request_map.map_id = map_id;
2731 crq.request_map.ioba = cpu_to_be32(addr);
2732 crq.request_map.len = cpu_to_be32(len);
2733 ibmvnic_send_crq(adapter, &crq);
2734}
2735
2736static void send_request_unmap(struct ibmvnic_adapter *adapter, u8 map_id)
2737{
2738 union ibmvnic_crq crq;
2739
2740 memset(&crq, 0, sizeof(crq));
2741 crq.request_unmap.first = IBMVNIC_CRQ_CMD;
2742 crq.request_unmap.cmd = REQUEST_UNMAP;
2743 crq.request_unmap.map_id = map_id;
2744 ibmvnic_send_crq(adapter, &crq);
2745}
2746
2747static void send_map_query(struct ibmvnic_adapter *adapter)
2748{
2749 union ibmvnic_crq crq;
2750
2751 memset(&crq, 0, sizeof(crq));
2752 crq.query_map.first = IBMVNIC_CRQ_CMD;
2753 crq.query_map.cmd = QUERY_MAP;
2754 ibmvnic_send_crq(adapter, &crq);
2755}
2756
2757/* Send a series of CRQs requesting various capabilities of the VNIC server */
2758static void send_cap_queries(struct ibmvnic_adapter *adapter)
2759{
2760 union ibmvnic_crq crq;
2761
Thomas Falcon901e0402017-02-15 12:17:59 -06002762 atomic_set(&adapter->running_cap_crqs, 0);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002763 memset(&crq, 0, sizeof(crq));
2764 crq.query_capability.first = IBMVNIC_CRQ_CMD;
2765 crq.query_capability.cmd = QUERY_CAPABILITY;
2766
2767 crq.query_capability.capability = cpu_to_be16(MIN_TX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002768 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002769 ibmvnic_send_crq(adapter, &crq);
2770
2771 crq.query_capability.capability = cpu_to_be16(MIN_RX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002772 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002773 ibmvnic_send_crq(adapter, &crq);
2774
2775 crq.query_capability.capability = cpu_to_be16(MIN_RX_ADD_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002776 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002777 ibmvnic_send_crq(adapter, &crq);
2778
2779 crq.query_capability.capability = cpu_to_be16(MAX_TX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002780 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002781 ibmvnic_send_crq(adapter, &crq);
2782
2783 crq.query_capability.capability = cpu_to_be16(MAX_RX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002784 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002785 ibmvnic_send_crq(adapter, &crq);
2786
2787 crq.query_capability.capability = cpu_to_be16(MAX_RX_ADD_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 =
2792 cpu_to_be16(MIN_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06002793 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002794 ibmvnic_send_crq(adapter, &crq);
2795
2796 crq.query_capability.capability =
2797 cpu_to_be16(MIN_RX_ADD_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06002798 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002799 ibmvnic_send_crq(adapter, &crq);
2800
2801 crq.query_capability.capability =
2802 cpu_to_be16(MAX_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06002803 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002804 ibmvnic_send_crq(adapter, &crq);
2805
2806 crq.query_capability.capability =
2807 cpu_to_be16(MAX_RX_ADD_ENTRIES_PER_SUBCRQ);
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 = cpu_to_be16(TCP_IP_OFFLOAD);
Thomas Falcon901e0402017-02-15 12:17:59 -06002812 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002813 ibmvnic_send_crq(adapter, &crq);
2814
2815 crq.query_capability.capability = cpu_to_be16(PROMISC_SUPPORTED);
Thomas Falcon901e0402017-02-15 12:17:59 -06002816 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002817 ibmvnic_send_crq(adapter, &crq);
2818
2819 crq.query_capability.capability = cpu_to_be16(MIN_MTU);
Thomas Falcon901e0402017-02-15 12:17:59 -06002820 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002821 ibmvnic_send_crq(adapter, &crq);
2822
2823 crq.query_capability.capability = cpu_to_be16(MAX_MTU);
Thomas Falcon901e0402017-02-15 12:17:59 -06002824 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002825 ibmvnic_send_crq(adapter, &crq);
2826
2827 crq.query_capability.capability = cpu_to_be16(MAX_MULTICAST_FILTERS);
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(VLAN_HEADER_INSERTION);
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
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04002835 crq.query_capability.capability = cpu_to_be16(RX_VLAN_HEADER_INSERTION);
2836 atomic_inc(&adapter->running_cap_crqs);
2837 ibmvnic_send_crq(adapter, &crq);
2838
Thomas Falcon032c5e82015-12-21 11:26:06 -06002839 crq.query_capability.capability = cpu_to_be16(MAX_TX_SG_ENTRIES);
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(RX_SG_SUPPORTED);
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(OPT_TX_COMP_SUB_QUEUES);
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(OPT_RX_COMP_QUEUES);
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
2855 crq.query_capability.capability =
2856 cpu_to_be16(OPT_RX_BUFADD_Q_PER_RX_COMP_Q);
Thomas Falcon901e0402017-02-15 12:17:59 -06002857 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002858 ibmvnic_send_crq(adapter, &crq);
2859
2860 crq.query_capability.capability =
2861 cpu_to_be16(OPT_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06002862 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002863 ibmvnic_send_crq(adapter, &crq);
2864
2865 crq.query_capability.capability =
2866 cpu_to_be16(OPT_RXBA_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06002867 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002868 ibmvnic_send_crq(adapter, &crq);
2869
2870 crq.query_capability.capability = cpu_to_be16(TX_RX_DESC_REQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06002871 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002872 ibmvnic_send_crq(adapter, &crq);
2873}
2874
2875static void handle_query_ip_offload_rsp(struct ibmvnic_adapter *adapter)
2876{
2877 struct device *dev = &adapter->vdev->dev;
2878 struct ibmvnic_query_ip_offload_buffer *buf = &adapter->ip_offload_buf;
2879 union ibmvnic_crq crq;
2880 int i;
2881
2882 dma_unmap_single(dev, adapter->ip_offload_tok,
2883 sizeof(adapter->ip_offload_buf), DMA_FROM_DEVICE);
2884
2885 netdev_dbg(adapter->netdev, "Query IP Offload Buffer:\n");
2886 for (i = 0; i < (sizeof(adapter->ip_offload_buf) - 1) / 8 + 1; i++)
2887 netdev_dbg(adapter->netdev, "%016lx\n",
2888 ((unsigned long int *)(buf))[i]);
2889
2890 netdev_dbg(adapter->netdev, "ipv4_chksum = %d\n", buf->ipv4_chksum);
2891 netdev_dbg(adapter->netdev, "ipv6_chksum = %d\n", buf->ipv6_chksum);
2892 netdev_dbg(adapter->netdev, "tcp_ipv4_chksum = %d\n",
2893 buf->tcp_ipv4_chksum);
2894 netdev_dbg(adapter->netdev, "tcp_ipv6_chksum = %d\n",
2895 buf->tcp_ipv6_chksum);
2896 netdev_dbg(adapter->netdev, "udp_ipv4_chksum = %d\n",
2897 buf->udp_ipv4_chksum);
2898 netdev_dbg(adapter->netdev, "udp_ipv6_chksum = %d\n",
2899 buf->udp_ipv6_chksum);
2900 netdev_dbg(adapter->netdev, "large_tx_ipv4 = %d\n",
2901 buf->large_tx_ipv4);
2902 netdev_dbg(adapter->netdev, "large_tx_ipv6 = %d\n",
2903 buf->large_tx_ipv6);
2904 netdev_dbg(adapter->netdev, "large_rx_ipv4 = %d\n",
2905 buf->large_rx_ipv4);
2906 netdev_dbg(adapter->netdev, "large_rx_ipv6 = %d\n",
2907 buf->large_rx_ipv6);
2908 netdev_dbg(adapter->netdev, "max_ipv4_hdr_sz = %d\n",
2909 buf->max_ipv4_header_size);
2910 netdev_dbg(adapter->netdev, "max_ipv6_hdr_sz = %d\n",
2911 buf->max_ipv6_header_size);
2912 netdev_dbg(adapter->netdev, "max_tcp_hdr_size = %d\n",
2913 buf->max_tcp_header_size);
2914 netdev_dbg(adapter->netdev, "max_udp_hdr_size = %d\n",
2915 buf->max_udp_header_size);
2916 netdev_dbg(adapter->netdev, "max_large_tx_size = %d\n",
2917 buf->max_large_tx_size);
2918 netdev_dbg(adapter->netdev, "max_large_rx_size = %d\n",
2919 buf->max_large_rx_size);
2920 netdev_dbg(adapter->netdev, "ipv6_ext_hdr = %d\n",
2921 buf->ipv6_extension_header);
2922 netdev_dbg(adapter->netdev, "tcp_pseudosum_req = %d\n",
2923 buf->tcp_pseudosum_req);
2924 netdev_dbg(adapter->netdev, "num_ipv6_ext_hd = %d\n",
2925 buf->num_ipv6_ext_headers);
2926 netdev_dbg(adapter->netdev, "off_ipv6_ext_hd = %d\n",
2927 buf->off_ipv6_ext_headers);
2928
2929 adapter->ip_offload_ctrl_tok =
2930 dma_map_single(dev, &adapter->ip_offload_ctrl,
2931 sizeof(adapter->ip_offload_ctrl), DMA_TO_DEVICE);
2932
2933 if (dma_mapping_error(dev, adapter->ip_offload_ctrl_tok)) {
2934 dev_err(dev, "Couldn't map ip offload control buffer\n");
2935 return;
2936 }
2937
2938 adapter->ip_offload_ctrl.version = cpu_to_be32(INITIAL_VERSION_IOB);
2939 adapter->ip_offload_ctrl.tcp_ipv4_chksum = buf->tcp_ipv4_chksum;
2940 adapter->ip_offload_ctrl.udp_ipv4_chksum = buf->udp_ipv4_chksum;
2941 adapter->ip_offload_ctrl.tcp_ipv6_chksum = buf->tcp_ipv6_chksum;
2942 adapter->ip_offload_ctrl.udp_ipv6_chksum = buf->udp_ipv6_chksum;
2943
2944 /* large_tx/rx disabled for now, additional features needed */
2945 adapter->ip_offload_ctrl.large_tx_ipv4 = 0;
2946 adapter->ip_offload_ctrl.large_tx_ipv6 = 0;
2947 adapter->ip_offload_ctrl.large_rx_ipv4 = 0;
2948 adapter->ip_offload_ctrl.large_rx_ipv6 = 0;
2949
2950 adapter->netdev->features = NETIF_F_GSO;
2951
2952 if (buf->tcp_ipv4_chksum || buf->udp_ipv4_chksum)
2953 adapter->netdev->features |= NETIF_F_IP_CSUM;
2954
2955 if (buf->tcp_ipv6_chksum || buf->udp_ipv6_chksum)
2956 adapter->netdev->features |= NETIF_F_IPV6_CSUM;
2957
Thomas Falcon9be02cd2016-04-01 17:20:35 -05002958 if ((adapter->netdev->features &
2959 (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)))
2960 adapter->netdev->features |= NETIF_F_RXCSUM;
2961
Thomas Falcon032c5e82015-12-21 11:26:06 -06002962 memset(&crq, 0, sizeof(crq));
2963 crq.control_ip_offload.first = IBMVNIC_CRQ_CMD;
2964 crq.control_ip_offload.cmd = CONTROL_IP_OFFLOAD;
2965 crq.control_ip_offload.len =
2966 cpu_to_be32(sizeof(adapter->ip_offload_ctrl));
2967 crq.control_ip_offload.ioba = cpu_to_be32(adapter->ip_offload_ctrl_tok);
2968 ibmvnic_send_crq(adapter, &crq);
2969}
2970
2971static void handle_error_info_rsp(union ibmvnic_crq *crq,
2972 struct ibmvnic_adapter *adapter)
2973{
2974 struct device *dev = &adapter->vdev->dev;
Wei Yongjun96183182016-06-27 20:48:53 +08002975 struct ibmvnic_error_buff *error_buff, *tmp;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002976 unsigned long flags;
2977 bool found = false;
2978 int i;
2979
2980 if (!crq->request_error_rsp.rc.code) {
2981 dev_info(dev, "Request Error Rsp returned with rc=%x\n",
2982 crq->request_error_rsp.rc.code);
2983 return;
2984 }
2985
2986 spin_lock_irqsave(&adapter->error_list_lock, flags);
Wei Yongjun96183182016-06-27 20:48:53 +08002987 list_for_each_entry_safe(error_buff, tmp, &adapter->errors, list)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002988 if (error_buff->error_id == crq->request_error_rsp.error_id) {
2989 found = true;
2990 list_del(&error_buff->list);
2991 break;
2992 }
2993 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
2994
2995 if (!found) {
2996 dev_err(dev, "Couldn't find error id %x\n",
Thomas Falcon75224c92017-02-15 10:33:33 -06002997 be32_to_cpu(crq->request_error_rsp.error_id));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002998 return;
2999 }
3000
3001 dev_err(dev, "Detailed info for error id %x:",
Thomas Falcon75224c92017-02-15 10:33:33 -06003002 be32_to_cpu(crq->request_error_rsp.error_id));
Thomas Falcon032c5e82015-12-21 11:26:06 -06003003
3004 for (i = 0; i < error_buff->len; i++) {
3005 pr_cont("%02x", (int)error_buff->buff[i]);
3006 if (i % 8 == 7)
3007 pr_cont(" ");
3008 }
3009 pr_cont("\n");
3010
3011 dma_unmap_single(dev, error_buff->dma, error_buff->len,
3012 DMA_FROM_DEVICE);
3013 kfree(error_buff->buff);
3014 kfree(error_buff);
3015}
3016
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04003017static void request_error_information(struct ibmvnic_adapter *adapter,
3018 union ibmvnic_crq *err_crq)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003019{
Thomas Falcon032c5e82015-12-21 11:26:06 -06003020 struct device *dev = &adapter->vdev->dev;
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04003021 struct net_device *netdev = adapter->netdev;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003022 struct ibmvnic_error_buff *error_buff;
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04003023 unsigned long timeout = msecs_to_jiffies(30000);
3024 union ibmvnic_crq crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003025 unsigned long flags;
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04003026 int rc, detail_len;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003027
3028 error_buff = kmalloc(sizeof(*error_buff), GFP_ATOMIC);
3029 if (!error_buff)
3030 return;
3031
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04003032 detail_len = be32_to_cpu(err_crq->error_indication.detail_error_sz);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003033 error_buff->buff = kmalloc(detail_len, GFP_ATOMIC);
3034 if (!error_buff->buff) {
3035 kfree(error_buff);
3036 return;
3037 }
3038
3039 error_buff->dma = dma_map_single(dev, error_buff->buff, detail_len,
3040 DMA_FROM_DEVICE);
3041 if (dma_mapping_error(dev, error_buff->dma)) {
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04003042 netdev_err(netdev, "Couldn't map error buffer\n");
Thomas Falcon032c5e82015-12-21 11:26:06 -06003043 kfree(error_buff->buff);
3044 kfree(error_buff);
3045 return;
3046 }
3047
Thomas Falcon032c5e82015-12-21 11:26:06 -06003048 error_buff->len = detail_len;
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04003049 error_buff->error_id = err_crq->error_indication.error_id;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003050
3051 spin_lock_irqsave(&adapter->error_list_lock, flags);
3052 list_add_tail(&error_buff->list, &adapter->errors);
3053 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
3054
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04003055 memset(&crq, 0, sizeof(crq));
3056 crq.request_error_info.first = IBMVNIC_CRQ_CMD;
3057 crq.request_error_info.cmd = REQUEST_ERROR_INFO;
3058 crq.request_error_info.ioba = cpu_to_be32(error_buff->dma);
3059 crq.request_error_info.len = cpu_to_be32(detail_len);
3060 crq.request_error_info.error_id = err_crq->error_indication.error_id;
3061
3062 rc = ibmvnic_send_crq(adapter, &crq);
3063 if (rc) {
3064 netdev_err(netdev, "failed to request error information\n");
3065 goto err_info_fail;
3066 }
3067
3068 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
3069 netdev_err(netdev, "timeout waiting for error information\n");
3070 goto err_info_fail;
3071 }
3072
3073 return;
3074
3075err_info_fail:
3076 spin_lock_irqsave(&adapter->error_list_lock, flags);
3077 list_del(&error_buff->list);
3078 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
3079
3080 kfree(error_buff->buff);
3081 kfree(error_buff);
3082}
3083
3084static void handle_error_indication(union ibmvnic_crq *crq,
3085 struct ibmvnic_adapter *adapter)
3086{
3087 struct device *dev = &adapter->vdev->dev;
3088
3089 dev_err(dev, "Firmware reports %serror id %x, cause %d\n",
3090 crq->error_indication.flags
3091 & IBMVNIC_FATAL_ERROR ? "FATAL " : "",
3092 be32_to_cpu(crq->error_indication.error_id),
3093 be16_to_cpu(crq->error_indication.error_cause));
3094
3095 if (be32_to_cpu(crq->error_indication.error_id))
3096 request_error_information(adapter, crq);
Nathan Fontenoted651a12017-05-03 14:04:38 -04003097
3098 if (crq->error_indication.flags & IBMVNIC_FATAL_ERROR)
3099 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
John Allen8cb31cf2017-05-26 10:30:37 -04003100 else
3101 ibmvnic_reset(adapter, VNIC_RESET_NON_FATAL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003102}
3103
3104static void handle_change_mac_rsp(union ibmvnic_crq *crq,
3105 struct ibmvnic_adapter *adapter)
3106{
3107 struct net_device *netdev = adapter->netdev;
3108 struct device *dev = &adapter->vdev->dev;
3109 long rc;
3110
3111 rc = crq->change_mac_addr_rsp.rc.code;
3112 if (rc) {
3113 dev_err(dev, "Error %ld in CHANGE_MAC_ADDR_RSP\n", rc);
3114 return;
3115 }
3116 memcpy(netdev->dev_addr, &crq->change_mac_addr_rsp.mac_addr[0],
3117 ETH_ALEN);
3118}
3119
3120static void handle_request_cap_rsp(union ibmvnic_crq *crq,
3121 struct ibmvnic_adapter *adapter)
3122{
3123 struct device *dev = &adapter->vdev->dev;
3124 u64 *req_value;
3125 char *name;
3126
Thomas Falcon901e0402017-02-15 12:17:59 -06003127 atomic_dec(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003128 switch (be16_to_cpu(crq->request_capability_rsp.capability)) {
3129 case REQ_TX_QUEUES:
3130 req_value = &adapter->req_tx_queues;
3131 name = "tx";
3132 break;
3133 case REQ_RX_QUEUES:
3134 req_value = &adapter->req_rx_queues;
3135 name = "rx";
3136 break;
3137 case REQ_RX_ADD_QUEUES:
3138 req_value = &adapter->req_rx_add_queues;
3139 name = "rx_add";
3140 break;
3141 case REQ_TX_ENTRIES_PER_SUBCRQ:
3142 req_value = &adapter->req_tx_entries_per_subcrq;
3143 name = "tx_entries_per_subcrq";
3144 break;
3145 case REQ_RX_ADD_ENTRIES_PER_SUBCRQ:
3146 req_value = &adapter->req_rx_add_entries_per_subcrq;
3147 name = "rx_add_entries_per_subcrq";
3148 break;
3149 case REQ_MTU:
3150 req_value = &adapter->req_mtu;
3151 name = "mtu";
3152 break;
3153 case PROMISC_REQUESTED:
3154 req_value = &adapter->promisc;
3155 name = "promisc";
3156 break;
3157 default:
3158 dev_err(dev, "Got invalid cap request rsp %d\n",
3159 crq->request_capability.capability);
3160 return;
3161 }
3162
3163 switch (crq->request_capability_rsp.rc.code) {
3164 case SUCCESS:
3165 break;
3166 case PARTIALSUCCESS:
3167 dev_info(dev, "req=%lld, rsp=%ld in %s queue, retrying.\n",
3168 *req_value,
Thomas Falcon28f4d162017-02-15 10:32:11 -06003169 (long int)be64_to_cpu(crq->request_capability_rsp.
Thomas Falcon032c5e82015-12-21 11:26:06 -06003170 number), name);
Thomas Falcon28f4d162017-02-15 10:32:11 -06003171 *req_value = be64_to_cpu(crq->request_capability_rsp.number);
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003172 ibmvnic_send_req_caps(adapter, 1);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003173 return;
3174 default:
3175 dev_err(dev, "Error %d in request cap rsp\n",
3176 crq->request_capability_rsp.rc.code);
3177 return;
3178 }
3179
3180 /* Done receiving requested capabilities, query IP offload support */
Thomas Falcon901e0402017-02-15 12:17:59 -06003181 if (atomic_read(&adapter->running_cap_crqs) == 0) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06003182 union ibmvnic_crq newcrq;
3183 int buf_sz = sizeof(struct ibmvnic_query_ip_offload_buffer);
3184 struct ibmvnic_query_ip_offload_buffer *ip_offload_buf =
3185 &adapter->ip_offload_buf;
3186
Thomas Falcon249168a2017-02-15 12:18:00 -06003187 adapter->wait_capability = false;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003188 adapter->ip_offload_tok = dma_map_single(dev, ip_offload_buf,
3189 buf_sz,
3190 DMA_FROM_DEVICE);
3191
3192 if (dma_mapping_error(dev, adapter->ip_offload_tok)) {
3193 if (!firmware_has_feature(FW_FEATURE_CMO))
3194 dev_err(dev, "Couldn't map offload buffer\n");
3195 return;
3196 }
3197
3198 memset(&newcrq, 0, sizeof(newcrq));
3199 newcrq.query_ip_offload.first = IBMVNIC_CRQ_CMD;
3200 newcrq.query_ip_offload.cmd = QUERY_IP_OFFLOAD;
3201 newcrq.query_ip_offload.len = cpu_to_be32(buf_sz);
3202 newcrq.query_ip_offload.ioba =
3203 cpu_to_be32(adapter->ip_offload_tok);
3204
3205 ibmvnic_send_crq(adapter, &newcrq);
3206 }
3207}
3208
3209static int handle_login_rsp(union ibmvnic_crq *login_rsp_crq,
3210 struct ibmvnic_adapter *adapter)
3211{
3212 struct device *dev = &adapter->vdev->dev;
3213 struct ibmvnic_login_rsp_buffer *login_rsp = adapter->login_rsp_buf;
3214 struct ibmvnic_login_buffer *login = adapter->login_buf;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003215 int i;
3216
3217 dma_unmap_single(dev, adapter->login_buf_token, adapter->login_buf_sz,
3218 DMA_BIDIRECTIONAL);
3219 dma_unmap_single(dev, adapter->login_rsp_buf_token,
3220 adapter->login_rsp_buf_sz, DMA_BIDIRECTIONAL);
3221
John Allen498cd8e2016-04-06 11:49:55 -05003222 /* If the number of queues requested can't be allocated by the
3223 * server, the login response will return with code 1. We will need
3224 * to resend the login buffer with fewer queues requested.
3225 */
3226 if (login_rsp_crq->generic.rc.code) {
3227 adapter->renegotiate = true;
3228 complete(&adapter->init_done);
3229 return 0;
3230 }
3231
Thomas Falcon032c5e82015-12-21 11:26:06 -06003232 netdev_dbg(adapter->netdev, "Login Response Buffer:\n");
3233 for (i = 0; i < (adapter->login_rsp_buf_sz - 1) / 8 + 1; i++) {
3234 netdev_dbg(adapter->netdev, "%016lx\n",
3235 ((unsigned long int *)(adapter->login_rsp_buf))[i]);
3236 }
3237
3238 /* Sanity checks */
3239 if (login->num_txcomp_subcrqs != login_rsp->num_txsubm_subcrqs ||
3240 (be32_to_cpu(login->num_rxcomp_subcrqs) *
3241 adapter->req_rx_add_queues !=
3242 be32_to_cpu(login_rsp->num_rxadd_subcrqs))) {
3243 dev_err(dev, "FATAL: Inconsistent login and login rsp\n");
3244 ibmvnic_remove(adapter->vdev);
3245 return -EIO;
3246 }
3247 complete(&adapter->init_done);
3248
Thomas Falcon032c5e82015-12-21 11:26:06 -06003249 return 0;
3250}
3251
Thomas Falcon032c5e82015-12-21 11:26:06 -06003252static void handle_request_unmap_rsp(union ibmvnic_crq *crq,
3253 struct ibmvnic_adapter *adapter)
3254{
3255 struct device *dev = &adapter->vdev->dev;
3256 long rc;
3257
3258 rc = crq->request_unmap_rsp.rc.code;
3259 if (rc)
3260 dev_err(dev, "Error %ld in REQUEST_UNMAP_RSP\n", rc);
3261}
3262
3263static void handle_query_map_rsp(union ibmvnic_crq *crq,
3264 struct ibmvnic_adapter *adapter)
3265{
3266 struct net_device *netdev = adapter->netdev;
3267 struct device *dev = &adapter->vdev->dev;
3268 long rc;
3269
3270 rc = crq->query_map_rsp.rc.code;
3271 if (rc) {
3272 dev_err(dev, "Error %ld in QUERY_MAP_RSP\n", rc);
3273 return;
3274 }
3275 netdev_dbg(netdev, "page_size = %d\ntot_pages = %d\nfree_pages = %d\n",
3276 crq->query_map_rsp.page_size, crq->query_map_rsp.tot_pages,
3277 crq->query_map_rsp.free_pages);
3278}
3279
3280static void handle_query_cap_rsp(union ibmvnic_crq *crq,
3281 struct ibmvnic_adapter *adapter)
3282{
3283 struct net_device *netdev = adapter->netdev;
3284 struct device *dev = &adapter->vdev->dev;
3285 long rc;
3286
Thomas Falcon901e0402017-02-15 12:17:59 -06003287 atomic_dec(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003288 netdev_dbg(netdev, "Outstanding queries: %d\n",
Thomas Falcon901e0402017-02-15 12:17:59 -06003289 atomic_read(&adapter->running_cap_crqs));
Thomas Falcon032c5e82015-12-21 11:26:06 -06003290 rc = crq->query_capability.rc.code;
3291 if (rc) {
3292 dev_err(dev, "Error %ld in QUERY_CAP_RSP\n", rc);
3293 goto out;
3294 }
3295
3296 switch (be16_to_cpu(crq->query_capability.capability)) {
3297 case MIN_TX_QUEUES:
3298 adapter->min_tx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003299 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003300 netdev_dbg(netdev, "min_tx_queues = %lld\n",
3301 adapter->min_tx_queues);
3302 break;
3303 case MIN_RX_QUEUES:
3304 adapter->min_rx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003305 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003306 netdev_dbg(netdev, "min_rx_queues = %lld\n",
3307 adapter->min_rx_queues);
3308 break;
3309 case MIN_RX_ADD_QUEUES:
3310 adapter->min_rx_add_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003311 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003312 netdev_dbg(netdev, "min_rx_add_queues = %lld\n",
3313 adapter->min_rx_add_queues);
3314 break;
3315 case MAX_TX_QUEUES:
3316 adapter->max_tx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003317 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003318 netdev_dbg(netdev, "max_tx_queues = %lld\n",
3319 adapter->max_tx_queues);
3320 break;
3321 case MAX_RX_QUEUES:
3322 adapter->max_rx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003323 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003324 netdev_dbg(netdev, "max_rx_queues = %lld\n",
3325 adapter->max_rx_queues);
3326 break;
3327 case MAX_RX_ADD_QUEUES:
3328 adapter->max_rx_add_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003329 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003330 netdev_dbg(netdev, "max_rx_add_queues = %lld\n",
3331 adapter->max_rx_add_queues);
3332 break;
3333 case MIN_TX_ENTRIES_PER_SUBCRQ:
3334 adapter->min_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06003335 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003336 netdev_dbg(netdev, "min_tx_entries_per_subcrq = %lld\n",
3337 adapter->min_tx_entries_per_subcrq);
3338 break;
3339 case MIN_RX_ADD_ENTRIES_PER_SUBCRQ:
3340 adapter->min_rx_add_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06003341 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003342 netdev_dbg(netdev, "min_rx_add_entrs_per_subcrq = %lld\n",
3343 adapter->min_rx_add_entries_per_subcrq);
3344 break;
3345 case MAX_TX_ENTRIES_PER_SUBCRQ:
3346 adapter->max_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06003347 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003348 netdev_dbg(netdev, "max_tx_entries_per_subcrq = %lld\n",
3349 adapter->max_tx_entries_per_subcrq);
3350 break;
3351 case MAX_RX_ADD_ENTRIES_PER_SUBCRQ:
3352 adapter->max_rx_add_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06003353 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003354 netdev_dbg(netdev, "max_rx_add_entrs_per_subcrq = %lld\n",
3355 adapter->max_rx_add_entries_per_subcrq);
3356 break;
3357 case TCP_IP_OFFLOAD:
3358 adapter->tcp_ip_offload =
Thomas Falconde89e852016-03-01 10:20:09 -06003359 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003360 netdev_dbg(netdev, "tcp_ip_offload = %lld\n",
3361 adapter->tcp_ip_offload);
3362 break;
3363 case PROMISC_SUPPORTED:
3364 adapter->promisc_supported =
Thomas Falconde89e852016-03-01 10:20:09 -06003365 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003366 netdev_dbg(netdev, "promisc_supported = %lld\n",
3367 adapter->promisc_supported);
3368 break;
3369 case MIN_MTU:
Thomas Falconde89e852016-03-01 10:20:09 -06003370 adapter->min_mtu = be64_to_cpu(crq->query_capability.number);
Thomas Falconf39f0d12017-02-14 10:22:59 -06003371 netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003372 netdev_dbg(netdev, "min_mtu = %lld\n", adapter->min_mtu);
3373 break;
3374 case MAX_MTU:
Thomas Falconde89e852016-03-01 10:20:09 -06003375 adapter->max_mtu = be64_to_cpu(crq->query_capability.number);
Thomas Falconf39f0d12017-02-14 10:22:59 -06003376 netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003377 netdev_dbg(netdev, "max_mtu = %lld\n", adapter->max_mtu);
3378 break;
3379 case MAX_MULTICAST_FILTERS:
3380 adapter->max_multicast_filters =
Thomas Falconde89e852016-03-01 10:20:09 -06003381 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003382 netdev_dbg(netdev, "max_multicast_filters = %lld\n",
3383 adapter->max_multicast_filters);
3384 break;
3385 case VLAN_HEADER_INSERTION:
3386 adapter->vlan_header_insertion =
Thomas Falconde89e852016-03-01 10:20:09 -06003387 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003388 if (adapter->vlan_header_insertion)
3389 netdev->features |= NETIF_F_HW_VLAN_STAG_TX;
3390 netdev_dbg(netdev, "vlan_header_insertion = %lld\n",
3391 adapter->vlan_header_insertion);
3392 break;
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04003393 case RX_VLAN_HEADER_INSERTION:
3394 adapter->rx_vlan_header_insertion =
3395 be64_to_cpu(crq->query_capability.number);
3396 netdev_dbg(netdev, "rx_vlan_header_insertion = %lld\n",
3397 adapter->rx_vlan_header_insertion);
3398 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003399 case MAX_TX_SG_ENTRIES:
3400 adapter->max_tx_sg_entries =
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_tx_sg_entries = %lld\n",
3403 adapter->max_tx_sg_entries);
3404 break;
3405 case RX_SG_SUPPORTED:
3406 adapter->rx_sg_supported =
Thomas Falconde89e852016-03-01 10:20:09 -06003407 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003408 netdev_dbg(netdev, "rx_sg_supported = %lld\n",
3409 adapter->rx_sg_supported);
3410 break;
3411 case OPT_TX_COMP_SUB_QUEUES:
3412 adapter->opt_tx_comp_sub_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003413 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003414 netdev_dbg(netdev, "opt_tx_comp_sub_queues = %lld\n",
3415 adapter->opt_tx_comp_sub_queues);
3416 break;
3417 case OPT_RX_COMP_QUEUES:
3418 adapter->opt_rx_comp_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003419 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003420 netdev_dbg(netdev, "opt_rx_comp_queues = %lld\n",
3421 adapter->opt_rx_comp_queues);
3422 break;
3423 case OPT_RX_BUFADD_Q_PER_RX_COMP_Q:
3424 adapter->opt_rx_bufadd_q_per_rx_comp_q =
Thomas Falconde89e852016-03-01 10:20:09 -06003425 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003426 netdev_dbg(netdev, "opt_rx_bufadd_q_per_rx_comp_q = %lld\n",
3427 adapter->opt_rx_bufadd_q_per_rx_comp_q);
3428 break;
3429 case OPT_TX_ENTRIES_PER_SUBCRQ:
3430 adapter->opt_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06003431 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003432 netdev_dbg(netdev, "opt_tx_entries_per_subcrq = %lld\n",
3433 adapter->opt_tx_entries_per_subcrq);
3434 break;
3435 case OPT_RXBA_ENTRIES_PER_SUBCRQ:
3436 adapter->opt_rxba_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06003437 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003438 netdev_dbg(netdev, "opt_rxba_entries_per_subcrq = %lld\n",
3439 adapter->opt_rxba_entries_per_subcrq);
3440 break;
3441 case TX_RX_DESC_REQ:
3442 adapter->tx_rx_desc_req = crq->query_capability.number;
3443 netdev_dbg(netdev, "tx_rx_desc_req = %llx\n",
3444 adapter->tx_rx_desc_req);
3445 break;
3446
3447 default:
3448 netdev_err(netdev, "Got invalid cap rsp %d\n",
3449 crq->query_capability.capability);
3450 }
3451
3452out:
Thomas Falcon249168a2017-02-15 12:18:00 -06003453 if (atomic_read(&adapter->running_cap_crqs) == 0) {
3454 adapter->wait_capability = false;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003455 ibmvnic_send_req_caps(adapter, 0);
Thomas Falcon249168a2017-02-15 12:18:00 -06003456 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06003457}
3458
Thomas Falcon032c5e82015-12-21 11:26:06 -06003459static void ibmvnic_handle_crq(union ibmvnic_crq *crq,
3460 struct ibmvnic_adapter *adapter)
3461{
3462 struct ibmvnic_generic_crq *gen_crq = &crq->generic;
3463 struct net_device *netdev = adapter->netdev;
3464 struct device *dev = &adapter->vdev->dev;
Murilo Fossa Vicentini993a82b2017-04-19 13:44:35 -04003465 u64 *u64_crq = (u64 *)crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003466 long rc;
3467
3468 netdev_dbg(netdev, "Handling CRQ: %016lx %016lx\n",
Murilo Fossa Vicentini993a82b2017-04-19 13:44:35 -04003469 (unsigned long int)cpu_to_be64(u64_crq[0]),
3470 (unsigned long int)cpu_to_be64(u64_crq[1]));
Thomas Falcon032c5e82015-12-21 11:26:06 -06003471 switch (gen_crq->first) {
3472 case IBMVNIC_CRQ_INIT_RSP:
3473 switch (gen_crq->cmd) {
3474 case IBMVNIC_CRQ_INIT:
3475 dev_info(dev, "Partner initialized\n");
John Allen017892c12017-05-26 10:30:19 -04003476 adapter->from_passive_init = true;
3477 complete(&adapter->init_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003478 break;
3479 case IBMVNIC_CRQ_INIT_COMPLETE:
3480 dev_info(dev, "Partner initialization complete\n");
3481 send_version_xchg(adapter);
3482 break;
3483 default:
3484 dev_err(dev, "Unknown crq cmd: %d\n", gen_crq->cmd);
3485 }
3486 return;
3487 case IBMVNIC_CRQ_XPORT_EVENT:
Nathan Fontenoted651a12017-05-03 14:04:38 -04003488 netif_carrier_off(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003489 if (gen_crq->cmd == IBMVNIC_PARTITION_MIGRATED) {
Nathan Fontenoted651a12017-05-03 14:04:38 -04003490 dev_info(dev, "Migrated, re-enabling adapter\n");
3491 ibmvnic_reset(adapter, VNIC_RESET_MOBILITY);
Thomas Falcondfad09a2016-08-18 11:37:51 -05003492 } else if (gen_crq->cmd == IBMVNIC_DEVICE_FAILOVER) {
3493 dev_info(dev, "Backing device failover detected\n");
Nathan Fontenoted651a12017-05-03 14:04:38 -04003494 ibmvnic_reset(adapter, VNIC_RESET_FAILOVER);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003495 } else {
3496 /* The adapter lost the connection */
3497 dev_err(dev, "Virtual Adapter failed (rc=%d)\n",
3498 gen_crq->cmd);
Nathan Fontenoted651a12017-05-03 14:04:38 -04003499 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003500 }
3501 return;
3502 case IBMVNIC_CRQ_CMD_RSP:
3503 break;
3504 default:
3505 dev_err(dev, "Got an invalid msg type 0x%02x\n",
3506 gen_crq->first);
3507 return;
3508 }
3509
3510 switch (gen_crq->cmd) {
3511 case VERSION_EXCHANGE_RSP:
3512 rc = crq->version_exchange_rsp.rc.code;
3513 if (rc) {
3514 dev_err(dev, "Error %ld in VERSION_EXCHG_RSP\n", rc);
3515 break;
3516 }
3517 dev_info(dev, "Partner protocol version is %d\n",
3518 crq->version_exchange_rsp.version);
3519 if (be16_to_cpu(crq->version_exchange_rsp.version) <
3520 ibmvnic_version)
3521 ibmvnic_version =
3522 be16_to_cpu(crq->version_exchange_rsp.version);
3523 send_cap_queries(adapter);
3524 break;
3525 case QUERY_CAPABILITY_RSP:
3526 handle_query_cap_rsp(crq, adapter);
3527 break;
3528 case QUERY_MAP_RSP:
3529 handle_query_map_rsp(crq, adapter);
3530 break;
3531 case REQUEST_MAP_RSP:
Thomas Falconf3be0cb2017-06-21 14:53:01 -05003532 adapter->fw_done_rc = crq->request_map_rsp.rc.code;
3533 complete(&adapter->fw_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003534 break;
3535 case REQUEST_UNMAP_RSP:
3536 handle_request_unmap_rsp(crq, adapter);
3537 break;
3538 case REQUEST_CAPABILITY_RSP:
3539 handle_request_cap_rsp(crq, adapter);
3540 break;
3541 case LOGIN_RSP:
3542 netdev_dbg(netdev, "Got Login Response\n");
3543 handle_login_rsp(crq, adapter);
3544 break;
3545 case LOGICAL_LINK_STATE_RSP:
Nathan Fontenot53da09e2017-04-21 15:39:04 -04003546 netdev_dbg(netdev,
3547 "Got Logical Link State Response, state: %d rc: %d\n",
3548 crq->logical_link_state_rsp.link_state,
3549 crq->logical_link_state_rsp.rc.code);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003550 adapter->logical_link_state =
3551 crq->logical_link_state_rsp.link_state;
Nathan Fontenot53da09e2017-04-21 15:39:04 -04003552 adapter->init_done_rc = crq->logical_link_state_rsp.rc.code;
3553 complete(&adapter->init_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003554 break;
3555 case LINK_STATE_INDICATION:
3556 netdev_dbg(netdev, "Got Logical Link State Indication\n");
3557 adapter->phys_link_state =
3558 crq->link_state_indication.phys_link_state;
3559 adapter->logical_link_state =
3560 crq->link_state_indication.logical_link_state;
3561 break;
3562 case CHANGE_MAC_ADDR_RSP:
3563 netdev_dbg(netdev, "Got MAC address change Response\n");
3564 handle_change_mac_rsp(crq, adapter);
3565 break;
3566 case ERROR_INDICATION:
3567 netdev_dbg(netdev, "Got Error Indication\n");
3568 handle_error_indication(crq, adapter);
3569 break;
3570 case REQUEST_ERROR_RSP:
3571 netdev_dbg(netdev, "Got Error Detail Response\n");
3572 handle_error_info_rsp(crq, adapter);
3573 break;
3574 case REQUEST_STATISTICS_RSP:
3575 netdev_dbg(netdev, "Got Statistics Response\n");
3576 complete(&adapter->stats_done);
3577 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003578 case QUERY_IP_OFFLOAD_RSP:
3579 netdev_dbg(netdev, "Got Query IP offload Response\n");
3580 handle_query_ip_offload_rsp(adapter);
3581 break;
3582 case MULTICAST_CTRL_RSP:
3583 netdev_dbg(netdev, "Got multicast control Response\n");
3584 break;
3585 case CONTROL_IP_OFFLOAD_RSP:
3586 netdev_dbg(netdev, "Got Control IP offload Response\n");
3587 dma_unmap_single(dev, adapter->ip_offload_ctrl_tok,
3588 sizeof(adapter->ip_offload_ctrl),
3589 DMA_TO_DEVICE);
John Allenbd0b6722017-03-17 17:13:40 -05003590 complete(&adapter->init_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003591 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003592 case COLLECT_FW_TRACE_RSP:
3593 netdev_dbg(netdev, "Got Collect firmware trace Response\n");
3594 complete(&adapter->fw_done);
3595 break;
3596 default:
3597 netdev_err(netdev, "Got an invalid cmd type 0x%02x\n",
3598 gen_crq->cmd);
3599 }
3600}
3601
3602static irqreturn_t ibmvnic_interrupt(int irq, void *instance)
3603{
3604 struct ibmvnic_adapter *adapter = instance;
Thomas Falcon6c267b32017-02-15 12:17:58 -06003605
Thomas Falcon6c267b32017-02-15 12:17:58 -06003606 tasklet_schedule(&adapter->tasklet);
Thomas Falcon6c267b32017-02-15 12:17:58 -06003607 return IRQ_HANDLED;
3608}
3609
3610static void ibmvnic_tasklet(void *data)
3611{
3612 struct ibmvnic_adapter *adapter = data;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003613 struct ibmvnic_crq_queue *queue = &adapter->crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003614 union ibmvnic_crq *crq;
3615 unsigned long flags;
3616 bool done = false;
3617
3618 spin_lock_irqsave(&queue->lock, flags);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003619 while (!done) {
3620 /* Pull all the valid messages off the CRQ */
3621 while ((crq = ibmvnic_next_crq(adapter)) != NULL) {
3622 ibmvnic_handle_crq(crq, adapter);
3623 crq->generic.first = 0;
3624 }
Brian Kinged7ecbf2017-04-19 13:44:53 -04003625
3626 /* remain in tasklet until all
3627 * capabilities responses are received
3628 */
3629 if (!adapter->wait_capability)
3630 done = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003631 }
Thomas Falcon249168a2017-02-15 12:18:00 -06003632 /* if capabilities CRQ's were sent in this tasklet, the following
3633 * tasklet must wait until all responses are received
3634 */
3635 if (atomic_read(&adapter->running_cap_crqs) != 0)
3636 adapter->wait_capability = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003637 spin_unlock_irqrestore(&queue->lock, flags);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003638}
3639
3640static int ibmvnic_reenable_crq_queue(struct ibmvnic_adapter *adapter)
3641{
3642 struct vio_dev *vdev = adapter->vdev;
3643 int rc;
3644
3645 do {
3646 rc = plpar_hcall_norets(H_ENABLE_CRQ, vdev->unit_address);
3647 } while (rc == H_IN_PROGRESS || rc == H_BUSY || H_IS_LONG_BUSY(rc));
3648
3649 if (rc)
3650 dev_err(&vdev->dev, "Error enabling adapter (rc=%d)\n", rc);
3651
3652 return rc;
3653}
3654
3655static int ibmvnic_reset_crq(struct ibmvnic_adapter *adapter)
3656{
3657 struct ibmvnic_crq_queue *crq = &adapter->crq;
3658 struct device *dev = &adapter->vdev->dev;
3659 struct vio_dev *vdev = adapter->vdev;
3660 int rc;
3661
3662 /* Close the CRQ */
3663 do {
3664 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
3665 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
3666
3667 /* Clean out the queue */
3668 memset(crq->msgs, 0, PAGE_SIZE);
3669 crq->cur = 0;
3670
3671 /* And re-open it again */
3672 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
3673 crq->msg_token, PAGE_SIZE);
3674
3675 if (rc == H_CLOSED)
3676 /* Adapter is good, but other end is not ready */
3677 dev_warn(dev, "Partner adapter not ready\n");
3678 else if (rc != 0)
3679 dev_warn(dev, "Couldn't register crq (rc=%d)\n", rc);
3680
3681 return rc;
3682}
3683
Nathan Fontenotf9928872017-03-30 02:48:54 -04003684static void release_crq_queue(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003685{
3686 struct ibmvnic_crq_queue *crq = &adapter->crq;
3687 struct vio_dev *vdev = adapter->vdev;
3688 long rc;
3689
Nathan Fontenotf9928872017-03-30 02:48:54 -04003690 if (!crq->msgs)
3691 return;
3692
Thomas Falcon032c5e82015-12-21 11:26:06 -06003693 netdev_dbg(adapter->netdev, "Releasing CRQ\n");
3694 free_irq(vdev->irq, adapter);
Thomas Falcon6c267b32017-02-15 12:17:58 -06003695 tasklet_kill(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003696 do {
3697 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
3698 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
3699
3700 dma_unmap_single(&vdev->dev, crq->msg_token, PAGE_SIZE,
3701 DMA_BIDIRECTIONAL);
3702 free_page((unsigned long)crq->msgs);
Nathan Fontenotf9928872017-03-30 02:48:54 -04003703 crq->msgs = NULL;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003704}
3705
Nathan Fontenotf9928872017-03-30 02:48:54 -04003706static int init_crq_queue(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003707{
3708 struct ibmvnic_crq_queue *crq = &adapter->crq;
3709 struct device *dev = &adapter->vdev->dev;
3710 struct vio_dev *vdev = adapter->vdev;
3711 int rc, retrc = -ENOMEM;
3712
Nathan Fontenotf9928872017-03-30 02:48:54 -04003713 if (crq->msgs)
3714 return 0;
3715
Thomas Falcon032c5e82015-12-21 11:26:06 -06003716 crq->msgs = (union ibmvnic_crq *)get_zeroed_page(GFP_KERNEL);
3717 /* Should we allocate more than one page? */
3718
3719 if (!crq->msgs)
3720 return -ENOMEM;
3721
3722 crq->size = PAGE_SIZE / sizeof(*crq->msgs);
3723 crq->msg_token = dma_map_single(dev, crq->msgs, PAGE_SIZE,
3724 DMA_BIDIRECTIONAL);
3725 if (dma_mapping_error(dev, crq->msg_token))
3726 goto map_failed;
3727
3728 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
3729 crq->msg_token, PAGE_SIZE);
3730
3731 if (rc == H_RESOURCE)
3732 /* maybe kexecing and resource is busy. try a reset */
3733 rc = ibmvnic_reset_crq(adapter);
3734 retrc = rc;
3735
3736 if (rc == H_CLOSED) {
3737 dev_warn(dev, "Partner adapter not ready\n");
3738 } else if (rc) {
3739 dev_warn(dev, "Error %d opening adapter\n", rc);
3740 goto reg_crq_failed;
3741 }
3742
3743 retrc = 0;
3744
Thomas Falcon6c267b32017-02-15 12:17:58 -06003745 tasklet_init(&adapter->tasklet, (void *)ibmvnic_tasklet,
3746 (unsigned long)adapter);
3747
Thomas Falcon032c5e82015-12-21 11:26:06 -06003748 netdev_dbg(adapter->netdev, "registering irq 0x%x\n", vdev->irq);
3749 rc = request_irq(vdev->irq, ibmvnic_interrupt, 0, IBMVNIC_NAME,
3750 adapter);
3751 if (rc) {
3752 dev_err(dev, "Couldn't register irq 0x%x. rc=%d\n",
3753 vdev->irq, rc);
3754 goto req_irq_failed;
3755 }
3756
3757 rc = vio_enable_interrupts(vdev);
3758 if (rc) {
3759 dev_err(dev, "Error %d enabling interrupts\n", rc);
3760 goto req_irq_failed;
3761 }
3762
3763 crq->cur = 0;
3764 spin_lock_init(&crq->lock);
3765
3766 return retrc;
3767
3768req_irq_failed:
Thomas Falcon6c267b32017-02-15 12:17:58 -06003769 tasklet_kill(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003770 do {
3771 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
3772 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
3773reg_crq_failed:
3774 dma_unmap_single(dev, crq->msg_token, PAGE_SIZE, DMA_BIDIRECTIONAL);
3775map_failed:
3776 free_page((unsigned long)crq->msgs);
Nathan Fontenotf9928872017-03-30 02:48:54 -04003777 crq->msgs = NULL;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003778 return retrc;
3779}
3780
John Allenf6ef6402017-03-17 17:13:42 -05003781static int ibmvnic_init(struct ibmvnic_adapter *adapter)
3782{
3783 struct device *dev = &adapter->vdev->dev;
3784 unsigned long timeout = msecs_to_jiffies(30000);
John Allenf6ef6402017-03-17 17:13:42 -05003785 int rc;
3786
Nathan Fontenot28cde752017-05-26 10:31:00 -04003787 if (adapter->resetting) {
3788 rc = ibmvnic_reset_crq(adapter);
3789 if (!rc)
3790 rc = vio_enable_interrupts(adapter->vdev);
3791 } else {
3792 rc = init_crq_queue(adapter);
3793 }
3794
John Allenf6ef6402017-03-17 17:13:42 -05003795 if (rc) {
3796 dev_err(dev, "Couldn't initialize crq. rc=%d\n", rc);
3797 return rc;
3798 }
3799
John Allen017892c12017-05-26 10:30:19 -04003800 adapter->from_passive_init = false;
3801
John Allenf6ef6402017-03-17 17:13:42 -05003802 init_completion(&adapter->init_done);
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04003803 adapter->init_done_rc = 0;
John Allenf6ef6402017-03-17 17:13:42 -05003804 ibmvnic_send_crq_init(adapter);
3805 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
3806 dev_err(dev, "Initialization sequence timed out\n");
John Allen017892c12017-05-26 10:30:19 -04003807 return -1;
3808 }
3809
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04003810 if (adapter->init_done_rc) {
3811 release_crq_queue(adapter);
3812 return adapter->init_done_rc;
3813 }
3814
John Allen017892c12017-05-26 10:30:19 -04003815 if (adapter->from_passive_init) {
3816 adapter->state = VNIC_OPEN;
3817 adapter->from_passive_init = false;
John Allenf6ef6402017-03-17 17:13:42 -05003818 return -1;
3819 }
3820
Nathan Fontenot57a49432017-05-26 10:31:12 -04003821 if (adapter->resetting)
3822 rc = reset_sub_crq_queues(adapter);
3823 else
3824 rc = init_sub_crqs(adapter);
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04003825 if (rc) {
3826 dev_err(dev, "Initialization of sub crqs failed\n");
3827 release_crq_queue(adapter);
Thomas Falcon5df969c2017-06-28 19:55:54 -05003828 return rc;
3829 }
3830
3831 rc = init_sub_crq_irqs(adapter);
3832 if (rc) {
3833 dev_err(dev, "Failed to initialize sub crq irqs\n");
3834 release_crq_queue(adapter);
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04003835 }
3836
3837 return rc;
John Allenf6ef6402017-03-17 17:13:42 -05003838}
3839
Thomas Falcon40c9db82017-06-12 12:35:04 -05003840static struct device_attribute dev_attr_failover;
3841
Thomas Falcon032c5e82015-12-21 11:26:06 -06003842static int ibmvnic_probe(struct vio_dev *dev, const struct vio_device_id *id)
3843{
3844 struct ibmvnic_adapter *adapter;
3845 struct net_device *netdev;
3846 unsigned char *mac_addr_p;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003847 int rc;
3848
3849 dev_dbg(&dev->dev, "entering ibmvnic_probe for UA 0x%x\n",
3850 dev->unit_address);
3851
3852 mac_addr_p = (unsigned char *)vio_get_attribute(dev,
3853 VETH_MAC_ADDR, NULL);
3854 if (!mac_addr_p) {
3855 dev_err(&dev->dev,
3856 "(%s:%3.3d) ERROR: Can't find MAC_ADDR attribute\n",
3857 __FILE__, __LINE__);
3858 return 0;
3859 }
3860
3861 netdev = alloc_etherdev_mq(sizeof(struct ibmvnic_adapter),
3862 IBMVNIC_MAX_TX_QUEUES);
3863 if (!netdev)
3864 return -ENOMEM;
3865
3866 adapter = netdev_priv(netdev);
Nathan Fontenot90c80142017-05-03 14:04:32 -04003867 adapter->state = VNIC_PROBING;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003868 dev_set_drvdata(&dev->dev, netdev);
3869 adapter->vdev = dev;
3870 adapter->netdev = netdev;
3871
3872 ether_addr_copy(adapter->mac_addr, mac_addr_p);
3873 ether_addr_copy(netdev->dev_addr, adapter->mac_addr);
3874 netdev->irq = dev->irq;
3875 netdev->netdev_ops = &ibmvnic_netdev_ops;
3876 netdev->ethtool_ops = &ibmvnic_ethtool_ops;
3877 SET_NETDEV_DEV(netdev, &dev->dev);
3878
3879 spin_lock_init(&adapter->stats_lock);
3880
Thomas Falcon032c5e82015-12-21 11:26:06 -06003881 INIT_LIST_HEAD(&adapter->errors);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003882 spin_lock_init(&adapter->error_list_lock);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003883
Nathan Fontenoted651a12017-05-03 14:04:38 -04003884 INIT_WORK(&adapter->ibmvnic_reset, __ibmvnic_reset);
3885 INIT_LIST_HEAD(&adapter->rwi_list);
3886 mutex_init(&adapter->reset_lock);
3887 mutex_init(&adapter->rwi_lock);
3888 adapter->resetting = false;
3889
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04003890 do {
3891 rc = ibmvnic_init(adapter);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05003892 if (rc && rc != EAGAIN)
3893 goto ibmvnic_init_fail;
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04003894 } while (rc == EAGAIN);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003895
Thomas Falconf39f0d12017-02-14 10:22:59 -06003896 netdev->mtu = adapter->req_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003897
Thomas Falcon40c9db82017-06-12 12:35:04 -05003898 rc = device_create_file(&dev->dev, &dev_attr_failover);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05003899 if (rc)
3900 goto ibmvnic_init_fail;
Thomas Falcon40c9db82017-06-12 12:35:04 -05003901
Thomas Falcon032c5e82015-12-21 11:26:06 -06003902 rc = register_netdev(netdev);
3903 if (rc) {
3904 dev_err(&dev->dev, "failed to register netdev rc=%d\n", rc);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05003905 goto ibmvnic_register_fail;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003906 }
3907 dev_info(&dev->dev, "ibmvnic registered\n");
3908
Nathan Fontenot90c80142017-05-03 14:04:32 -04003909 adapter->state = VNIC_PROBED;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003910 return 0;
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05003911
3912ibmvnic_register_fail:
3913 device_remove_file(&dev->dev, &dev_attr_failover);
3914
3915ibmvnic_init_fail:
3916 release_sub_crqs(adapter);
3917 release_crq_queue(adapter);
3918 free_netdev(netdev);
3919
3920 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003921}
3922
3923static int ibmvnic_remove(struct vio_dev *dev)
3924{
3925 struct net_device *netdev = dev_get_drvdata(&dev->dev);
Nathan Fontenot37489052017-04-19 13:45:04 -04003926 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003927
Nathan Fontenot90c80142017-05-03 14:04:32 -04003928 adapter->state = VNIC_REMOVING;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003929 unregister_netdev(netdev);
Nathan Fontenoted651a12017-05-03 14:04:38 -04003930 mutex_lock(&adapter->reset_lock);
Nathan Fontenot37489052017-04-19 13:45:04 -04003931
3932 release_resources(adapter);
3933 release_sub_crqs(adapter);
3934 release_crq_queue(adapter);
3935
Nathan Fontenot90c80142017-05-03 14:04:32 -04003936 adapter->state = VNIC_REMOVED;
3937
Nathan Fontenoted651a12017-05-03 14:04:38 -04003938 mutex_unlock(&adapter->reset_lock);
Thomas Falcon40c9db82017-06-12 12:35:04 -05003939 device_remove_file(&dev->dev, &dev_attr_failover);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003940 free_netdev(netdev);
3941 dev_set_drvdata(&dev->dev, NULL);
3942
3943 return 0;
3944}
3945
Thomas Falcon40c9db82017-06-12 12:35:04 -05003946static ssize_t failover_store(struct device *dev, struct device_attribute *attr,
3947 const char *buf, size_t count)
3948{
3949 struct net_device *netdev = dev_get_drvdata(dev);
3950 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
3951 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
3952 __be64 session_token;
3953 long rc;
3954
3955 if (!sysfs_streq(buf, "1"))
3956 return -EINVAL;
3957
3958 rc = plpar_hcall(H_VIOCTL, retbuf, adapter->vdev->unit_address,
3959 H_GET_SESSION_TOKEN, 0, 0, 0);
3960 if (rc) {
3961 netdev_err(netdev, "Couldn't retrieve session token, rc %ld\n",
3962 rc);
3963 return -EINVAL;
3964 }
3965
3966 session_token = (__be64)retbuf[0];
3967 netdev_dbg(netdev, "Initiating client failover, session id %llx\n",
3968 be64_to_cpu(session_token));
3969 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
3970 H_SESSION_ERR_DETECTED, session_token, 0, 0);
3971 if (rc) {
3972 netdev_err(netdev, "Client initiated failover failed, rc %ld\n",
3973 rc);
3974 return -EINVAL;
3975 }
3976
3977 return count;
3978}
3979
3980static DEVICE_ATTR(failover, 0200, NULL, failover_store);
3981
Thomas Falcon032c5e82015-12-21 11:26:06 -06003982static unsigned long ibmvnic_get_desired_dma(struct vio_dev *vdev)
3983{
3984 struct net_device *netdev = dev_get_drvdata(&vdev->dev);
3985 struct ibmvnic_adapter *adapter;
3986 struct iommu_table *tbl;
3987 unsigned long ret = 0;
3988 int i;
3989
3990 tbl = get_iommu_table_base(&vdev->dev);
3991
3992 /* netdev inits at probe time along with the structures we need below*/
3993 if (!netdev)
3994 return IOMMU_PAGE_ALIGN(IBMVNIC_IO_ENTITLEMENT_DEFAULT, tbl);
3995
3996 adapter = netdev_priv(netdev);
3997
3998 ret += PAGE_SIZE; /* the crq message queue */
Thomas Falcon032c5e82015-12-21 11:26:06 -06003999 ret += IOMMU_PAGE_ALIGN(sizeof(struct ibmvnic_statistics), tbl);
4000
4001 for (i = 0; i < adapter->req_tx_queues + adapter->req_rx_queues; i++)
4002 ret += 4 * PAGE_SIZE; /* the scrq message queue */
4003
4004 for (i = 0; i < be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
4005 i++)
4006 ret += adapter->rx_pool[i].size *
4007 IOMMU_PAGE_ALIGN(adapter->rx_pool[i].buff_size, tbl);
4008
4009 return ret;
4010}
4011
4012static int ibmvnic_resume(struct device *dev)
4013{
4014 struct net_device *netdev = dev_get_drvdata(dev);
4015 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004016
John Allencb89ba22017-06-19 11:27:53 -05004017 if (adapter->state != VNIC_OPEN)
4018 return 0;
4019
John Allena2488782017-07-24 13:26:06 -05004020 tasklet_schedule(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004021
4022 return 0;
4023}
4024
Arvind Yadav8c37bc62017-08-17 18:52:54 +05304025static const struct vio_device_id ibmvnic_device_table[] = {
Thomas Falcon032c5e82015-12-21 11:26:06 -06004026 {"network", "IBM,vnic"},
4027 {"", "" }
4028};
4029MODULE_DEVICE_TABLE(vio, ibmvnic_device_table);
4030
4031static const struct dev_pm_ops ibmvnic_pm_ops = {
4032 .resume = ibmvnic_resume
4033};
4034
4035static struct vio_driver ibmvnic_driver = {
4036 .id_table = ibmvnic_device_table,
4037 .probe = ibmvnic_probe,
4038 .remove = ibmvnic_remove,
4039 .get_desired_dma = ibmvnic_get_desired_dma,
4040 .name = ibmvnic_driver_name,
4041 .pm = &ibmvnic_pm_ops,
4042};
4043
4044/* module functions */
4045static int __init ibmvnic_module_init(void)
4046{
4047 pr_info("%s: %s %s\n", ibmvnic_driver_name, ibmvnic_driver_string,
4048 IBMVNIC_DRIVER_VERSION);
4049
4050 return vio_register_driver(&ibmvnic_driver);
4051}
4052
4053static void __exit ibmvnic_module_exit(void)
4054{
4055 vio_unregister_driver(&ibmvnic_driver);
4056}
4057
4058module_init(ibmvnic_module_init);
4059module_exit(ibmvnic_module_exit);