blob: 1b6268ce7b0d5f89100c9e446096493ffacbb805 [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
84MODULE_AUTHOR("Santiago Leon <santi_leon@yahoo.com>");
85MODULE_DESCRIPTION("IBM System i/p Virtual NIC Driver");
86MODULE_LICENSE("GPL");
87MODULE_VERSION(IBMVNIC_DRIVER_VERSION);
88
89static int ibmvnic_version = IBMVNIC_INITIAL_VERSION;
90static int ibmvnic_remove(struct vio_dev *);
91static void release_sub_crqs(struct ibmvnic_adapter *);
92static int ibmvnic_reset_crq(struct ibmvnic_adapter *);
93static int ibmvnic_send_crq_init(struct ibmvnic_adapter *);
94static int ibmvnic_reenable_crq_queue(struct ibmvnic_adapter *);
95static int ibmvnic_send_crq(struct ibmvnic_adapter *, union ibmvnic_crq *);
96static int send_subcrq(struct ibmvnic_adapter *adapter, u64 remote_handle,
97 union sub_crq *sub_crq);
Thomas Falconad7775d2016-04-01 17:20:34 -050098static int send_subcrq_indirect(struct ibmvnic_adapter *, u64, u64, u64);
Thomas Falcon032c5e82015-12-21 11:26:06 -060099static irqreturn_t ibmvnic_interrupt_rx(int irq, void *instance);
100static int enable_scrq_irq(struct ibmvnic_adapter *,
101 struct ibmvnic_sub_crq_queue *);
102static int disable_scrq_irq(struct ibmvnic_adapter *,
103 struct ibmvnic_sub_crq_queue *);
104static int pending_scrq(struct ibmvnic_adapter *,
105 struct ibmvnic_sub_crq_queue *);
106static union sub_crq *ibmvnic_next_scrq(struct ibmvnic_adapter *,
107 struct ibmvnic_sub_crq_queue *);
108static int ibmvnic_poll(struct napi_struct *napi, int data);
109static void send_map_query(struct ibmvnic_adapter *adapter);
110static void send_request_map(struct ibmvnic_adapter *, dma_addr_t, __be32, u8);
111static void send_request_unmap(struct ibmvnic_adapter *, u8);
John Allenbd0b6722017-03-17 17:13:40 -0500112static void send_login(struct ibmvnic_adapter *adapter);
113static void send_cap_queries(struct ibmvnic_adapter *adapter);
114static int init_sub_crq_irqs(struct ibmvnic_adapter *adapter);
John Allenea5509f2017-03-17 17:13:43 -0500115static int ibmvnic_init(struct ibmvnic_adapter *);
Nathan Fontenotf9928872017-03-30 02:48:54 -0400116static void release_crq_queue(struct ibmvnic_adapter *);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600117
118struct ibmvnic_stat {
119 char name[ETH_GSTRING_LEN];
120 int offset;
121};
122
123#define IBMVNIC_STAT_OFF(stat) (offsetof(struct ibmvnic_adapter, stats) + \
124 offsetof(struct ibmvnic_statistics, stat))
125#define IBMVNIC_GET_STAT(a, off) (*((u64 *)(((unsigned long)(a)) + off)))
126
127static const struct ibmvnic_stat ibmvnic_stats[] = {
128 {"rx_packets", IBMVNIC_STAT_OFF(rx_packets)},
129 {"rx_bytes", IBMVNIC_STAT_OFF(rx_bytes)},
130 {"tx_packets", IBMVNIC_STAT_OFF(tx_packets)},
131 {"tx_bytes", IBMVNIC_STAT_OFF(tx_bytes)},
132 {"ucast_tx_packets", IBMVNIC_STAT_OFF(ucast_tx_packets)},
133 {"ucast_rx_packets", IBMVNIC_STAT_OFF(ucast_rx_packets)},
134 {"mcast_tx_packets", IBMVNIC_STAT_OFF(mcast_tx_packets)},
135 {"mcast_rx_packets", IBMVNIC_STAT_OFF(mcast_rx_packets)},
136 {"bcast_tx_packets", IBMVNIC_STAT_OFF(bcast_tx_packets)},
137 {"bcast_rx_packets", IBMVNIC_STAT_OFF(bcast_rx_packets)},
138 {"align_errors", IBMVNIC_STAT_OFF(align_errors)},
139 {"fcs_errors", IBMVNIC_STAT_OFF(fcs_errors)},
140 {"single_collision_frames", IBMVNIC_STAT_OFF(single_collision_frames)},
141 {"multi_collision_frames", IBMVNIC_STAT_OFF(multi_collision_frames)},
142 {"sqe_test_errors", IBMVNIC_STAT_OFF(sqe_test_errors)},
143 {"deferred_tx", IBMVNIC_STAT_OFF(deferred_tx)},
144 {"late_collisions", IBMVNIC_STAT_OFF(late_collisions)},
145 {"excess_collisions", IBMVNIC_STAT_OFF(excess_collisions)},
146 {"internal_mac_tx_errors", IBMVNIC_STAT_OFF(internal_mac_tx_errors)},
147 {"carrier_sense", IBMVNIC_STAT_OFF(carrier_sense)},
148 {"too_long_frames", IBMVNIC_STAT_OFF(too_long_frames)},
149 {"internal_mac_rx_errors", IBMVNIC_STAT_OFF(internal_mac_rx_errors)},
150};
151
152static long h_reg_sub_crq(unsigned long unit_address, unsigned long token,
153 unsigned long length, unsigned long *number,
154 unsigned long *irq)
155{
156 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
157 long rc;
158
159 rc = plpar_hcall(H_REG_SUB_CRQ, retbuf, unit_address, token, length);
160 *number = retbuf[0];
161 *irq = retbuf[1];
162
163 return rc;
164}
165
Thomas Falcon032c5e82015-12-21 11:26:06 -0600166static int alloc_long_term_buff(struct ibmvnic_adapter *adapter,
167 struct ibmvnic_long_term_buff *ltb, int size)
168{
169 struct device *dev = &adapter->vdev->dev;
170
171 ltb->size = size;
172 ltb->buff = dma_alloc_coherent(dev, ltb->size, &ltb->addr,
173 GFP_KERNEL);
174
175 if (!ltb->buff) {
176 dev_err(dev, "Couldn't alloc long term buffer\n");
177 return -ENOMEM;
178 }
179 ltb->map_id = adapter->map_id;
180 adapter->map_id++;
Nathan Fontenotdb5d0b52017-02-10 13:45:05 -0500181
182 init_completion(&adapter->fw_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600183 send_request_map(adapter, ltb->addr,
184 ltb->size, ltb->map_id);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600185 wait_for_completion(&adapter->fw_done);
186 return 0;
187}
188
189static void free_long_term_buff(struct ibmvnic_adapter *adapter,
190 struct ibmvnic_long_term_buff *ltb)
191{
192 struct device *dev = &adapter->vdev->dev;
193
Nathan Fontenotc657e322017-03-30 02:49:06 -0400194 if (!ltb->buff)
195 return;
196
Nathan Fontenoted651a12017-05-03 14:04:38 -0400197 if (adapter->reset_reason != VNIC_RESET_FAILOVER &&
198 adapter->reset_reason != VNIC_RESET_MOBILITY)
Thomas Falcondfad09a2016-08-18 11:37:51 -0500199 send_request_unmap(adapter, ltb->map_id);
Brian King59af56c2017-04-19 13:44:41 -0400200 dma_free_coherent(dev, ltb->size, ltb->buff, ltb->addr);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600201}
202
Thomas Falcon032c5e82015-12-21 11:26:06 -0600203static void replenish_rx_pool(struct ibmvnic_adapter *adapter,
204 struct ibmvnic_rx_pool *pool)
205{
206 int count = pool->size - atomic_read(&pool->available);
207 struct device *dev = &adapter->vdev->dev;
208 int buffers_added = 0;
209 unsigned long lpar_rc;
210 union sub_crq sub_crq;
211 struct sk_buff *skb;
212 unsigned int offset;
213 dma_addr_t dma_addr;
214 unsigned char *dst;
215 u64 *handle_array;
216 int shift = 0;
217 int index;
218 int i;
219
220 handle_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
221 be32_to_cpu(adapter->login_rsp_buf->
222 off_rxadd_subcrqs));
223
224 for (i = 0; i < count; ++i) {
225 skb = alloc_skb(pool->buff_size, GFP_ATOMIC);
226 if (!skb) {
227 dev_err(dev, "Couldn't replenish rx buff\n");
228 adapter->replenish_no_mem++;
229 break;
230 }
231
232 index = pool->free_map[pool->next_free];
233
234 if (pool->rx_buff[index].skb)
235 dev_err(dev, "Inconsistent free_map!\n");
236
237 /* Copy the skb to the long term mapped DMA buffer */
238 offset = index * pool->buff_size;
239 dst = pool->long_term_buff.buff + offset;
240 memset(dst, 0, pool->buff_size);
241 dma_addr = pool->long_term_buff.addr + offset;
242 pool->rx_buff[index].data = dst;
243
244 pool->free_map[pool->next_free] = IBMVNIC_INVALID_MAP;
245 pool->rx_buff[index].dma = dma_addr;
246 pool->rx_buff[index].skb = skb;
247 pool->rx_buff[index].pool_index = pool->index;
248 pool->rx_buff[index].size = pool->buff_size;
249
250 memset(&sub_crq, 0, sizeof(sub_crq));
251 sub_crq.rx_add.first = IBMVNIC_CRQ_CMD;
252 sub_crq.rx_add.correlator =
253 cpu_to_be64((u64)&pool->rx_buff[index]);
254 sub_crq.rx_add.ioba = cpu_to_be32(dma_addr);
255 sub_crq.rx_add.map_id = pool->long_term_buff.map_id;
256
257 /* The length field of the sCRQ is defined to be 24 bits so the
258 * buffer size needs to be left shifted by a byte before it is
259 * converted to big endian to prevent the last byte from being
260 * truncated.
261 */
262#ifdef __LITTLE_ENDIAN__
263 shift = 8;
264#endif
265 sub_crq.rx_add.len = cpu_to_be32(pool->buff_size << shift);
266
267 lpar_rc = send_subcrq(adapter, handle_array[pool->index],
268 &sub_crq);
269 if (lpar_rc != H_SUCCESS)
270 goto failure;
271
272 buffers_added++;
273 adapter->replenish_add_buff_success++;
274 pool->next_free = (pool->next_free + 1) % pool->size;
275 }
276 atomic_add(buffers_added, &pool->available);
277 return;
278
279failure:
280 dev_info(dev, "replenish pools failure\n");
281 pool->free_map[pool->next_free] = index;
282 pool->rx_buff[index].skb = NULL;
283 if (!dma_mapping_error(dev, dma_addr))
284 dma_unmap_single(dev, dma_addr, pool->buff_size,
285 DMA_FROM_DEVICE);
286
287 dev_kfree_skb_any(skb);
288 adapter->replenish_add_buff_failure++;
289 atomic_add(buffers_added, &pool->available);
290}
291
292static void replenish_pools(struct ibmvnic_adapter *adapter)
293{
294 int i;
295
Thomas Falcon032c5e82015-12-21 11:26:06 -0600296 adapter->replenish_task_cycles++;
297 for (i = 0; i < be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
298 i++) {
299 if (adapter->rx_pool[i].active)
300 replenish_rx_pool(adapter, &adapter->rx_pool[i]);
301 }
302}
303
Nathan Fontenot7bbc27a2017-03-30 02:49:23 -0400304static void release_stats_token(struct ibmvnic_adapter *adapter)
305{
306 struct device *dev = &adapter->vdev->dev;
307
308 if (!adapter->stats_token)
309 return;
310
311 dma_unmap_single(dev, adapter->stats_token,
312 sizeof(struct ibmvnic_statistics),
313 DMA_FROM_DEVICE);
314 adapter->stats_token = 0;
315}
316
317static int init_stats_token(struct ibmvnic_adapter *adapter)
318{
319 struct device *dev = &adapter->vdev->dev;
320 dma_addr_t stok;
321
322 stok = dma_map_single(dev, &adapter->stats,
323 sizeof(struct ibmvnic_statistics),
324 DMA_FROM_DEVICE);
325 if (dma_mapping_error(dev, stok)) {
326 dev_err(dev, "Couldn't map stats buffer\n");
327 return -1;
328 }
329
330 adapter->stats_token = stok;
331 return 0;
332}
333
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400334static void release_rx_pools(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -0600335{
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400336 struct ibmvnic_rx_pool *rx_pool;
337 int rx_scrqs;
338 int i, j;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600339
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400340 if (!adapter->rx_pool)
Thomas Falcon032c5e82015-12-21 11:26:06 -0600341 return;
342
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400343 rx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
344 for (i = 0; i < rx_scrqs; i++) {
345 rx_pool = &adapter->rx_pool[i];
346
347 kfree(rx_pool->free_map);
348 free_long_term_buff(adapter, &rx_pool->long_term_buff);
349
350 if (!rx_pool->rx_buff)
Nathan Fontenote0ebe9422017-05-03 14:04:50 -0400351 continue;
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400352
353 for (j = 0; j < rx_pool->size; j++) {
354 if (rx_pool->rx_buff[j].skb) {
355 dev_kfree_skb_any(rx_pool->rx_buff[i].skb);
356 rx_pool->rx_buff[i].skb = NULL;
357 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600358 }
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400359
360 kfree(rx_pool->rx_buff);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600361 }
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400362
363 kfree(adapter->rx_pool);
364 adapter->rx_pool = NULL;
365}
366
367static int init_rx_pools(struct net_device *netdev)
368{
369 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
370 struct device *dev = &adapter->vdev->dev;
371 struct ibmvnic_rx_pool *rx_pool;
372 int rxadd_subcrqs;
373 u64 *size_array;
374 int i, j;
375
376 rxadd_subcrqs =
377 be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
378 size_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
379 be32_to_cpu(adapter->login_rsp_buf->off_rxadd_buff_size));
380
381 adapter->rx_pool = kcalloc(rxadd_subcrqs,
382 sizeof(struct ibmvnic_rx_pool),
383 GFP_KERNEL);
384 if (!adapter->rx_pool) {
385 dev_err(dev, "Failed to allocate rx pools\n");
386 return -1;
387 }
388
389 for (i = 0; i < rxadd_subcrqs; i++) {
390 rx_pool = &adapter->rx_pool[i];
391
392 netdev_dbg(adapter->netdev,
393 "Initializing rx_pool %d, %lld buffs, %lld bytes each\n",
394 i, adapter->req_rx_add_entries_per_subcrq,
395 be64_to_cpu(size_array[i]));
396
397 rx_pool->size = adapter->req_rx_add_entries_per_subcrq;
398 rx_pool->index = i;
399 rx_pool->buff_size = be64_to_cpu(size_array[i]);
400 rx_pool->active = 1;
401
402 rx_pool->free_map = kcalloc(rx_pool->size, sizeof(int),
403 GFP_KERNEL);
404 if (!rx_pool->free_map) {
405 release_rx_pools(adapter);
406 return -1;
407 }
408
409 rx_pool->rx_buff = kcalloc(rx_pool->size,
410 sizeof(struct ibmvnic_rx_buff),
411 GFP_KERNEL);
412 if (!rx_pool->rx_buff) {
413 dev_err(dev, "Couldn't alloc rx buffers\n");
414 release_rx_pools(adapter);
415 return -1;
416 }
417
418 if (alloc_long_term_buff(adapter, &rx_pool->long_term_buff,
419 rx_pool->size * rx_pool->buff_size)) {
420 release_rx_pools(adapter);
421 return -1;
422 }
423
424 for (j = 0; j < rx_pool->size; ++j)
425 rx_pool->free_map[j] = j;
426
427 atomic_set(&rx_pool->available, 0);
428 rx_pool->next_alloc = 0;
429 rx_pool->next_free = 0;
430 }
431
432 return 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600433}
434
Nathan Fontenotc657e322017-03-30 02:49:06 -0400435static void release_tx_pools(struct ibmvnic_adapter *adapter)
436{
437 struct ibmvnic_tx_pool *tx_pool;
438 int i, tx_scrqs;
439
440 if (!adapter->tx_pool)
441 return;
442
443 tx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
444 for (i = 0; i < tx_scrqs; i++) {
445 tx_pool = &adapter->tx_pool[i];
446 kfree(tx_pool->tx_buff);
447 free_long_term_buff(adapter, &tx_pool->long_term_buff);
448 kfree(tx_pool->free_map);
449 }
450
451 kfree(adapter->tx_pool);
452 adapter->tx_pool = NULL;
453}
454
455static int init_tx_pools(struct net_device *netdev)
456{
457 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
458 struct device *dev = &adapter->vdev->dev;
459 struct ibmvnic_tx_pool *tx_pool;
460 int tx_subcrqs;
461 int i, j;
462
463 tx_subcrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
464 adapter->tx_pool = kcalloc(tx_subcrqs,
465 sizeof(struct ibmvnic_tx_pool), GFP_KERNEL);
466 if (!adapter->tx_pool)
467 return -1;
468
469 for (i = 0; i < tx_subcrqs; i++) {
470 tx_pool = &adapter->tx_pool[i];
471 tx_pool->tx_buff = kcalloc(adapter->req_tx_entries_per_subcrq,
472 sizeof(struct ibmvnic_tx_buff),
473 GFP_KERNEL);
474 if (!tx_pool->tx_buff) {
475 dev_err(dev, "tx pool buffer allocation failed\n");
476 release_tx_pools(adapter);
477 return -1;
478 }
479
480 if (alloc_long_term_buff(adapter, &tx_pool->long_term_buff,
481 adapter->req_tx_entries_per_subcrq *
482 adapter->req_mtu)) {
483 release_tx_pools(adapter);
484 return -1;
485 }
486
487 tx_pool->free_map = kcalloc(adapter->req_tx_entries_per_subcrq,
488 sizeof(int), GFP_KERNEL);
489 if (!tx_pool->free_map) {
490 release_tx_pools(adapter);
491 return -1;
492 }
493
494 for (j = 0; j < adapter->req_tx_entries_per_subcrq; j++)
495 tx_pool->free_map[j] = j;
496
497 tx_pool->consumer_index = 0;
498 tx_pool->producer_index = 0;
499 }
500
501 return 0;
502}
503
Nathan Fontenot661a2622017-04-19 13:44:58 -0400504static void release_error_buffers(struct ibmvnic_adapter *adapter)
505{
506 struct device *dev = &adapter->vdev->dev;
507 struct ibmvnic_error_buff *error_buff, *tmp;
508 unsigned long flags;
509
510 spin_lock_irqsave(&adapter->error_list_lock, flags);
511 list_for_each_entry_safe(error_buff, tmp, &adapter->errors, list) {
512 list_del(&error_buff->list);
513 dma_unmap_single(dev, error_buff->dma, error_buff->len,
514 DMA_FROM_DEVICE);
515 kfree(error_buff->buff);
516 kfree(error_buff);
517 }
518 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
519}
520
John Allena57a5d22017-03-17 17:13:41 -0500521static int ibmvnic_login(struct net_device *netdev)
Thomas Falcon032c5e82015-12-21 11:26:06 -0600522{
523 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
John Allenbd0b6722017-03-17 17:13:40 -0500524 unsigned long timeout = msecs_to_jiffies(30000);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600525 struct device *dev = &adapter->vdev->dev;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600526
John Allenbd0b6722017-03-17 17:13:40 -0500527 do {
528 if (adapter->renegotiate) {
529 adapter->renegotiate = false;
Nathan Fontenotb5108882017-03-30 02:49:18 -0400530 release_sub_crqs(adapter);
John Allenbd0b6722017-03-17 17:13:40 -0500531
532 reinit_completion(&adapter->init_done);
533 send_cap_queries(adapter);
534 if (!wait_for_completion_timeout(&adapter->init_done,
535 timeout)) {
536 dev_err(dev, "Capabilities query timeout\n");
537 return -1;
538 }
539 }
540
541 reinit_completion(&adapter->init_done);
542 send_login(adapter);
543 if (!wait_for_completion_timeout(&adapter->init_done,
544 timeout)) {
545 dev_err(dev, "Login timeout\n");
546 return -1;
547 }
548 } while (adapter->renegotiate);
549
John Allena57a5d22017-03-17 17:13:41 -0500550 return 0;
551}
552
Nathan Fontenot1b8955e2017-03-30 02:49:29 -0400553static void release_resources(struct ibmvnic_adapter *adapter)
554{
Nathan Fontenotc7bac002017-05-03 14:04:44 -0400555 int i;
556
Nathan Fontenot1b8955e2017-03-30 02:49:29 -0400557 release_tx_pools(adapter);
558 release_rx_pools(adapter);
559
Nathan Fontenot1b8955e2017-03-30 02:49:29 -0400560 release_stats_token(adapter);
Nathan Fontenot661a2622017-04-19 13:44:58 -0400561 release_error_buffers(adapter);
Nathan Fontenotc7bac002017-05-03 14:04:44 -0400562
563 if (adapter->napi) {
564 for (i = 0; i < adapter->req_rx_queues; i++) {
565 if (&adapter->napi[i])
566 netif_napi_del(&adapter->napi[i]);
567 }
568 }
Nathan Fontenot1b8955e2017-03-30 02:49:29 -0400569}
570
Nathan Fontenot53da09e2017-04-21 15:39:04 -0400571static int set_link_state(struct ibmvnic_adapter *adapter, u8 link_state)
572{
573 struct net_device *netdev = adapter->netdev;
574 unsigned long timeout = msecs_to_jiffies(30000);
575 union ibmvnic_crq crq;
576 bool resend;
577 int rc;
578
Nathan Fontenot53da09e2017-04-21 15:39:04 -0400579 netdev_err(netdev, "setting link state %d\n", link_state);
580 memset(&crq, 0, sizeof(crq));
581 crq.logical_link_state.first = IBMVNIC_CRQ_CMD;
582 crq.logical_link_state.cmd = LOGICAL_LINK_STATE;
583 crq.logical_link_state.link_state = link_state;
584
585 do {
586 resend = false;
587
588 reinit_completion(&adapter->init_done);
589 rc = ibmvnic_send_crq(adapter, &crq);
590 if (rc) {
591 netdev_err(netdev, "Failed to set link state\n");
592 return rc;
593 }
594
595 if (!wait_for_completion_timeout(&adapter->init_done,
596 timeout)) {
597 netdev_err(netdev, "timeout setting link state\n");
598 return -1;
599 }
600
601 if (adapter->init_done_rc == 1) {
602 /* Partuial success, delay and re-send */
603 mdelay(1000);
604 resend = true;
605 }
606 } while (resend);
607
608 return 0;
609}
610
Thomas Falcon7f3c6e62017-04-21 15:38:40 -0400611static int set_real_num_queues(struct net_device *netdev)
612{
613 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
614 int rc;
615
616 rc = netif_set_real_num_tx_queues(netdev, adapter->req_tx_queues);
617 if (rc) {
618 netdev_err(netdev, "failed to set the number of tx queues\n");
619 return rc;
620 }
621
622 rc = netif_set_real_num_rx_queues(netdev, adapter->req_rx_queues);
623 if (rc)
624 netdev_err(netdev, "failed to set the number of rx queues\n");
625
626 return rc;
627}
628
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400629static int init_resources(struct ibmvnic_adapter *adapter)
John Allena57a5d22017-03-17 17:13:41 -0500630{
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400631 struct net_device *netdev = adapter->netdev;
632 int i, rc;
John Allena57a5d22017-03-17 17:13:41 -0500633
Thomas Falcon7f3c6e62017-04-21 15:38:40 -0400634 rc = set_real_num_queues(netdev);
635 if (rc)
636 return rc;
John Allenbd0b6722017-03-17 17:13:40 -0500637
638 rc = init_sub_crq_irqs(adapter);
639 if (rc) {
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400640 netdev_err(netdev, "failed to initialize sub crq irqs\n");
John Allenbd0b6722017-03-17 17:13:40 -0500641 return -1;
642 }
643
Nathan Fontenot5d5e84e2017-04-21 15:38:58 -0400644 rc = init_stats_token(adapter);
645 if (rc)
646 return rc;
647
Thomas Falcon032c5e82015-12-21 11:26:06 -0600648 adapter->map_id = 1;
649 adapter->napi = kcalloc(adapter->req_rx_queues,
650 sizeof(struct napi_struct), GFP_KERNEL);
651 if (!adapter->napi)
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400652 return -ENOMEM;
653
Thomas Falcon032c5e82015-12-21 11:26:06 -0600654 for (i = 0; i < adapter->req_rx_queues; i++) {
655 netif_napi_add(netdev, &adapter->napi[i], ibmvnic_poll,
656 NAPI_POLL_WEIGHT);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600657 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600658
Thomas Falcon032c5e82015-12-21 11:26:06 -0600659 send_map_query(adapter);
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400660
661 rc = init_rx_pools(netdev);
662 if (rc)
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400663 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600664
Nathan Fontenotc657e322017-03-30 02:49:06 -0400665 rc = init_tx_pools(netdev);
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400666 return rc;
667}
668
Nathan Fontenoted651a12017-05-03 14:04:38 -0400669static int __ibmvnic_open(struct net_device *netdev)
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400670{
671 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Nathan Fontenoted651a12017-05-03 14:04:38 -0400672 enum vnic_state prev_state = adapter->state;
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400673 int i, rc;
674
Nathan Fontenot90c80142017-05-03 14:04:32 -0400675 adapter->state = VNIC_OPENING;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600676 replenish_pools(adapter);
677
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400678 for (i = 0; i < adapter->req_rx_queues; i++)
679 napi_enable(&adapter->napi[i]);
680
Thomas Falcon032c5e82015-12-21 11:26:06 -0600681 /* We're ready to receive frames, enable the sub-crq interrupts and
682 * set the logical link state to up
683 */
Nathan Fontenoted651a12017-05-03 14:04:38 -0400684 for (i = 0; i < adapter->req_rx_queues; i++) {
685 if (prev_state == VNIC_CLOSED)
686 enable_irq(adapter->rx_scrq[i]->irq);
687 else
688 enable_scrq_irq(adapter, adapter->rx_scrq[i]);
689 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600690
Nathan Fontenoted651a12017-05-03 14:04:38 -0400691 for (i = 0; i < adapter->req_tx_queues; i++) {
692 if (prev_state == VNIC_CLOSED)
693 enable_irq(adapter->tx_scrq[i]->irq);
694 else
695 enable_scrq_irq(adapter, adapter->tx_scrq[i]);
696 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600697
Nathan Fontenot53da09e2017-04-21 15:39:04 -0400698 rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_UP);
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400699 if (rc) {
700 for (i = 0; i < adapter->req_rx_queues; i++)
701 napi_disable(&adapter->napi[i]);
702 release_resources(adapter);
Nathan Fontenoted651a12017-05-03 14:04:38 -0400703 return rc;
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400704 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600705
Nathan Fontenoted651a12017-05-03 14:04:38 -0400706 netif_tx_start_all_queues(netdev);
707
708 if (prev_state == VNIC_CLOSED) {
709 for (i = 0; i < adapter->req_rx_queues; i++)
710 napi_schedule(&adapter->napi[i]);
711 }
712
713 adapter->state = VNIC_OPEN;
714 return rc;
715}
716
717static int ibmvnic_open(struct net_device *netdev)
718{
719 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
720 int rc;
721
722 mutex_lock(&adapter->reset_lock);
723
724 if (adapter->state != VNIC_CLOSED) {
725 rc = ibmvnic_login(netdev);
726 if (rc) {
727 mutex_unlock(&adapter->reset_lock);
728 return rc;
729 }
730
731 rc = init_resources(adapter);
732 if (rc) {
733 netdev_err(netdev, "failed to initialize resources\n");
734 release_resources(adapter);
735 mutex_unlock(&adapter->reset_lock);
736 return rc;
737 }
738 }
739
740 rc = __ibmvnic_open(netdev);
741 mutex_unlock(&adapter->reset_lock);
742
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400743 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600744}
745
Nathan Fontenotb41b83e2017-05-03 14:04:56 -0400746static void clean_tx_pools(struct ibmvnic_adapter *adapter)
747{
748 struct ibmvnic_tx_pool *tx_pool;
749 u64 tx_entries;
750 int tx_scrqs;
751 int i, j;
752
753 if (!adapter->tx_pool)
754 return;
755
756 tx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
757 tx_entries = adapter->req_tx_entries_per_subcrq;
758
759 /* Free any remaining skbs in the tx buffer pools */
760 for (i = 0; i < tx_scrqs; i++) {
761 tx_pool = &adapter->tx_pool[i];
762 if (!tx_pool)
763 continue;
764
765 for (j = 0; j < tx_entries; j++) {
766 if (tx_pool->tx_buff[j].skb) {
767 dev_kfree_skb_any(tx_pool->tx_buff[j].skb);
768 tx_pool->tx_buff[j].skb = NULL;
769 }
770 }
771 }
772}
773
Nathan Fontenoted651a12017-05-03 14:04:38 -0400774static int __ibmvnic_close(struct net_device *netdev)
John Allenea5509f2017-03-17 17:13:43 -0500775{
776 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Nathan Fontenot53da09e2017-04-21 15:39:04 -0400777 int rc = 0;
John Allenea5509f2017-03-17 17:13:43 -0500778 int i;
779
Nathan Fontenot90c80142017-05-03 14:04:32 -0400780 adapter->state = VNIC_CLOSING;
Nathan Fontenoted651a12017-05-03 14:04:38 -0400781 netif_tx_stop_all_queues(netdev);
Nathan Fontenotb41b83e2017-05-03 14:04:56 -0400782
Nathan Fontenot3ca19932017-04-21 15:39:10 -0400783 if (adapter->napi) {
784 for (i = 0; i < adapter->req_rx_queues; i++)
785 napi_disable(&adapter->napi[i]);
786 }
John Allenea5509f2017-03-17 17:13:43 -0500787
Nathan Fontenot46293b92017-05-03 14:05:02 -0400788 clean_tx_pools(adapter);
789
790 if (adapter->tx_scrq) {
791 for (i = 0; i < adapter->req_tx_queues; i++)
792 if (adapter->tx_scrq[i]->irq)
793 disable_irq(adapter->tx_scrq[i]->irq);
794 }
795
Nathan Fontenot53da09e2017-04-21 15:39:04 -0400796 rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_DN);
Nathan Fontenot46293b92017-05-03 14:05:02 -0400797 if (rc)
798 return rc;
799
800 if (adapter->rx_scrq) {
801 for (i = 0; i < adapter->req_rx_queues; i++) {
802 int retries = 10;
803
804 while (pending_scrq(adapter, adapter->rx_scrq[i])) {
805 retries--;
806 mdelay(100);
807
808 if (retries == 0)
809 break;
810 }
811
812 if (adapter->rx_scrq[i]->irq)
813 disable_irq(adapter->rx_scrq[i]->irq);
814 }
815 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600816
Nathan Fontenot90c80142017-05-03 14:04:32 -0400817 adapter->state = VNIC_CLOSED;
Nathan Fontenot53da09e2017-04-21 15:39:04 -0400818 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600819}
820
Nathan Fontenoted651a12017-05-03 14:04:38 -0400821static int ibmvnic_close(struct net_device *netdev)
822{
823 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
824 int rc;
825
826 mutex_lock(&adapter->reset_lock);
827 rc = __ibmvnic_close(netdev);
828 mutex_unlock(&adapter->reset_lock);
829
830 return rc;
831}
832
Thomas Falconad7775d2016-04-01 17:20:34 -0500833/**
834 * build_hdr_data - creates L2/L3/L4 header data buffer
835 * @hdr_field - bitfield determining needed headers
836 * @skb - socket buffer
837 * @hdr_len - array of header lengths
838 * @tot_len - total length of data
839 *
840 * Reads hdr_field to determine which headers are needed by firmware.
841 * Builds a buffer containing these headers. Saves individual header
842 * lengths and total buffer length to be used to build descriptors.
843 */
844static int build_hdr_data(u8 hdr_field, struct sk_buff *skb,
845 int *hdr_len, u8 *hdr_data)
846{
847 int len = 0;
848 u8 *hdr;
849
850 hdr_len[0] = sizeof(struct ethhdr);
851
852 if (skb->protocol == htons(ETH_P_IP)) {
853 hdr_len[1] = ip_hdr(skb)->ihl * 4;
854 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
855 hdr_len[2] = tcp_hdrlen(skb);
856 else if (ip_hdr(skb)->protocol == IPPROTO_UDP)
857 hdr_len[2] = sizeof(struct udphdr);
858 } else if (skb->protocol == htons(ETH_P_IPV6)) {
859 hdr_len[1] = sizeof(struct ipv6hdr);
860 if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
861 hdr_len[2] = tcp_hdrlen(skb);
862 else if (ipv6_hdr(skb)->nexthdr == IPPROTO_UDP)
863 hdr_len[2] = sizeof(struct udphdr);
864 }
865
866 memset(hdr_data, 0, 120);
867 if ((hdr_field >> 6) & 1) {
868 hdr = skb_mac_header(skb);
869 memcpy(hdr_data, hdr, hdr_len[0]);
870 len += hdr_len[0];
871 }
872
873 if ((hdr_field >> 5) & 1) {
874 hdr = skb_network_header(skb);
875 memcpy(hdr_data + len, hdr, hdr_len[1]);
876 len += hdr_len[1];
877 }
878
879 if ((hdr_field >> 4) & 1) {
880 hdr = skb_transport_header(skb);
881 memcpy(hdr_data + len, hdr, hdr_len[2]);
882 len += hdr_len[2];
883 }
884 return len;
885}
886
887/**
888 * create_hdr_descs - create header and header extension descriptors
889 * @hdr_field - bitfield determining needed headers
890 * @data - buffer containing header data
891 * @len - length of data buffer
892 * @hdr_len - array of individual header lengths
893 * @scrq_arr - descriptor array
894 *
895 * Creates header and, if needed, header extension descriptors and
896 * places them in a descriptor array, scrq_arr
897 */
898
899static void create_hdr_descs(u8 hdr_field, u8 *hdr_data, int len, int *hdr_len,
900 union sub_crq *scrq_arr)
901{
902 union sub_crq hdr_desc;
903 int tmp_len = len;
904 u8 *data, *cur;
905 int tmp;
906
907 while (tmp_len > 0) {
908 cur = hdr_data + len - tmp_len;
909
910 memset(&hdr_desc, 0, sizeof(hdr_desc));
911 if (cur != hdr_data) {
912 data = hdr_desc.hdr_ext.data;
913 tmp = tmp_len > 29 ? 29 : tmp_len;
914 hdr_desc.hdr_ext.first = IBMVNIC_CRQ_CMD;
915 hdr_desc.hdr_ext.type = IBMVNIC_HDR_EXT_DESC;
916 hdr_desc.hdr_ext.len = tmp;
917 } else {
918 data = hdr_desc.hdr.data;
919 tmp = tmp_len > 24 ? 24 : tmp_len;
920 hdr_desc.hdr.first = IBMVNIC_CRQ_CMD;
921 hdr_desc.hdr.type = IBMVNIC_HDR_DESC;
922 hdr_desc.hdr.len = tmp;
923 hdr_desc.hdr.l2_len = (u8)hdr_len[0];
924 hdr_desc.hdr.l3_len = cpu_to_be16((u16)hdr_len[1]);
925 hdr_desc.hdr.l4_len = (u8)hdr_len[2];
926 hdr_desc.hdr.flag = hdr_field << 1;
927 }
928 memcpy(data, cur, tmp);
929 tmp_len -= tmp;
930 *scrq_arr = hdr_desc;
931 scrq_arr++;
932 }
933}
934
935/**
936 * build_hdr_descs_arr - build a header descriptor array
937 * @skb - socket buffer
938 * @num_entries - number of descriptors to be sent
939 * @subcrq - first TX descriptor
940 * @hdr_field - bit field determining which headers will be sent
941 *
942 * This function will build a TX descriptor array with applicable
943 * L2/L3/L4 packet header descriptors to be sent by send_subcrq_indirect.
944 */
945
946static void build_hdr_descs_arr(struct ibmvnic_tx_buff *txbuff,
947 int *num_entries, u8 hdr_field)
948{
949 int hdr_len[3] = {0, 0, 0};
950 int tot_len, len;
951 u8 *hdr_data = txbuff->hdr_data;
952
953 tot_len = build_hdr_data(hdr_field, txbuff->skb, hdr_len,
954 txbuff->hdr_data);
955 len = tot_len;
956 len -= 24;
957 if (len > 0)
958 num_entries += len % 29 ? len / 29 + 1 : len / 29;
959 create_hdr_descs(hdr_field, hdr_data, tot_len, hdr_len,
960 txbuff->indir_arr + 1);
961}
962
Thomas Falcon032c5e82015-12-21 11:26:06 -0600963static int ibmvnic_xmit(struct sk_buff *skb, struct net_device *netdev)
964{
965 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
966 int queue_num = skb_get_queue_mapping(skb);
Thomas Falconad7775d2016-04-01 17:20:34 -0500967 u8 *hdrs = (u8 *)&adapter->tx_rx_desc_req;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600968 struct device *dev = &adapter->vdev->dev;
969 struct ibmvnic_tx_buff *tx_buff = NULL;
Thomas Falcon142c0ac2017-03-05 12:18:41 -0600970 struct ibmvnic_sub_crq_queue *tx_scrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600971 struct ibmvnic_tx_pool *tx_pool;
972 unsigned int tx_send_failed = 0;
973 unsigned int tx_map_failed = 0;
974 unsigned int tx_dropped = 0;
975 unsigned int tx_packets = 0;
976 unsigned int tx_bytes = 0;
977 dma_addr_t data_dma_addr;
978 struct netdev_queue *txq;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600979 unsigned long lpar_rc;
980 union sub_crq tx_crq;
981 unsigned int offset;
Thomas Falconad7775d2016-04-01 17:20:34 -0500982 int num_entries = 1;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600983 unsigned char *dst;
984 u64 *handle_array;
985 int index = 0;
986 int ret = 0;
987
Nathan Fontenoted651a12017-05-03 14:04:38 -0400988 if (adapter->resetting) {
Thomas Falcon7f5b0302017-04-21 15:39:16 -0400989 if (!netif_subqueue_stopped(netdev, skb))
990 netif_stop_subqueue(netdev, queue_num);
991 dev_kfree_skb_any(skb);
992
Thomas Falcon032c5e82015-12-21 11:26:06 -0600993 tx_send_failed++;
994 tx_dropped++;
Thomas Falcon7f5b0302017-04-21 15:39:16 -0400995 ret = NETDEV_TX_OK;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600996 goto out;
997 }
998
Nathan Fontenot161b8a82017-05-03 14:05:08 -0400999 tx_pool = &adapter->tx_pool[queue_num];
1000 tx_scrq = adapter->tx_scrq[queue_num];
1001 txq = netdev_get_tx_queue(netdev, skb_get_queue_mapping(skb));
1002 handle_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
1003 be32_to_cpu(adapter->login_rsp_buf->off_txsubm_subcrqs));
1004
Thomas Falcon032c5e82015-12-21 11:26:06 -06001005 index = tx_pool->free_map[tx_pool->consumer_index];
1006 offset = index * adapter->req_mtu;
1007 dst = tx_pool->long_term_buff.buff + offset;
1008 memset(dst, 0, adapter->req_mtu);
1009 skb_copy_from_linear_data(skb, dst, skb->len);
1010 data_dma_addr = tx_pool->long_term_buff.addr + offset;
1011
1012 tx_pool->consumer_index =
1013 (tx_pool->consumer_index + 1) %
Thomas Falcon068d9f92017-03-05 12:18:42 -06001014 adapter->req_tx_entries_per_subcrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001015
1016 tx_buff = &tx_pool->tx_buff[index];
1017 tx_buff->skb = skb;
1018 tx_buff->data_dma[0] = data_dma_addr;
1019 tx_buff->data_len[0] = skb->len;
1020 tx_buff->index = index;
1021 tx_buff->pool_index = queue_num;
1022 tx_buff->last_frag = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001023
1024 memset(&tx_crq, 0, sizeof(tx_crq));
1025 tx_crq.v1.first = IBMVNIC_CRQ_CMD;
1026 tx_crq.v1.type = IBMVNIC_TX_DESC;
1027 tx_crq.v1.n_crq_elem = 1;
1028 tx_crq.v1.n_sge = 1;
1029 tx_crq.v1.flags1 = IBMVNIC_TX_COMP_NEEDED;
1030 tx_crq.v1.correlator = cpu_to_be32(index);
1031 tx_crq.v1.dma_reg = cpu_to_be16(tx_pool->long_term_buff.map_id);
1032 tx_crq.v1.sge_len = cpu_to_be32(skb->len);
1033 tx_crq.v1.ioba = cpu_to_be64(data_dma_addr);
1034
1035 if (adapter->vlan_header_insertion) {
1036 tx_crq.v1.flags2 |= IBMVNIC_TX_VLAN_INSERT;
1037 tx_crq.v1.vlan_id = cpu_to_be16(skb->vlan_tci);
1038 }
1039
1040 if (skb->protocol == htons(ETH_P_IP)) {
1041 if (ip_hdr(skb)->version == 4)
1042 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV4;
1043 else if (ip_hdr(skb)->version == 6)
1044 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV6;
1045
1046 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
1047 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_TCP;
1048 else if (ip_hdr(skb)->protocol != IPPROTO_TCP)
1049 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_UDP;
1050 }
1051
Thomas Falconad7775d2016-04-01 17:20:34 -05001052 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06001053 tx_crq.v1.flags1 |= IBMVNIC_TX_CHKSUM_OFFLOAD;
Thomas Falconad7775d2016-04-01 17:20:34 -05001054 hdrs += 2;
1055 }
1056 /* determine if l2/3/4 headers are sent to firmware */
1057 if ((*hdrs >> 7) & 1 &&
1058 (skb->protocol == htons(ETH_P_IP) ||
1059 skb->protocol == htons(ETH_P_IPV6))) {
1060 build_hdr_descs_arr(tx_buff, &num_entries, *hdrs);
1061 tx_crq.v1.n_crq_elem = num_entries;
1062 tx_buff->indir_arr[0] = tx_crq;
1063 tx_buff->indir_dma = dma_map_single(dev, tx_buff->indir_arr,
1064 sizeof(tx_buff->indir_arr),
1065 DMA_TO_DEVICE);
1066 if (dma_mapping_error(dev, tx_buff->indir_dma)) {
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001067 dev_kfree_skb_any(skb);
1068 tx_buff->skb = NULL;
Thomas Falconad7775d2016-04-01 17:20:34 -05001069 if (!firmware_has_feature(FW_FEATURE_CMO))
1070 dev_err(dev, "tx: unable to map descriptor array\n");
1071 tx_map_failed++;
1072 tx_dropped++;
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001073 ret = NETDEV_TX_OK;
Thomas Falconad7775d2016-04-01 17:20:34 -05001074 goto out;
1075 }
John Allen498cd8e2016-04-06 11:49:55 -05001076 lpar_rc = send_subcrq_indirect(adapter, handle_array[queue_num],
Thomas Falconad7775d2016-04-01 17:20:34 -05001077 (u64)tx_buff->indir_dma,
1078 (u64)num_entries);
1079 } else {
John Allen498cd8e2016-04-06 11:49:55 -05001080 lpar_rc = send_subcrq(adapter, handle_array[queue_num],
1081 &tx_crq);
Thomas Falconad7775d2016-04-01 17:20:34 -05001082 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001083 if (lpar_rc != H_SUCCESS) {
1084 dev_err(dev, "tx failed with code %ld\n", lpar_rc);
1085
1086 if (tx_pool->consumer_index == 0)
1087 tx_pool->consumer_index =
Thomas Falcon068d9f92017-03-05 12:18:42 -06001088 adapter->req_tx_entries_per_subcrq - 1;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001089 else
1090 tx_pool->consumer_index--;
1091
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001092 dev_kfree_skb_any(skb);
1093 tx_buff->skb = NULL;
1094
1095 if (lpar_rc == H_CLOSED)
1096 netif_stop_subqueue(netdev, queue_num);
1097
Thomas Falcon032c5e82015-12-21 11:26:06 -06001098 tx_send_failed++;
1099 tx_dropped++;
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001100 ret = NETDEV_TX_OK;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001101 goto out;
1102 }
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001103
Brian King58c8c0c2017-04-19 13:44:47 -04001104 if (atomic_inc_return(&tx_scrq->used)
1105 >= adapter->req_tx_entries_per_subcrq) {
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001106 netdev_info(netdev, "Stopping queue %d\n", queue_num);
1107 netif_stop_subqueue(netdev, queue_num);
1108 }
1109
Thomas Falcon032c5e82015-12-21 11:26:06 -06001110 tx_packets++;
1111 tx_bytes += skb->len;
1112 txq->trans_start = jiffies;
1113 ret = NETDEV_TX_OK;
1114
1115out:
1116 netdev->stats.tx_dropped += tx_dropped;
1117 netdev->stats.tx_bytes += tx_bytes;
1118 netdev->stats.tx_packets += tx_packets;
1119 adapter->tx_send_failed += tx_send_failed;
1120 adapter->tx_map_failed += tx_map_failed;
1121
1122 return ret;
1123}
1124
1125static void ibmvnic_set_multi(struct net_device *netdev)
1126{
1127 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1128 struct netdev_hw_addr *ha;
1129 union ibmvnic_crq crq;
1130
1131 memset(&crq, 0, sizeof(crq));
1132 crq.request_capability.first = IBMVNIC_CRQ_CMD;
1133 crq.request_capability.cmd = REQUEST_CAPABILITY;
1134
1135 if (netdev->flags & IFF_PROMISC) {
1136 if (!adapter->promisc_supported)
1137 return;
1138 } else {
1139 if (netdev->flags & IFF_ALLMULTI) {
1140 /* Accept all multicast */
1141 memset(&crq, 0, sizeof(crq));
1142 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1143 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1144 crq.multicast_ctrl.flags = IBMVNIC_ENABLE_ALL;
1145 ibmvnic_send_crq(adapter, &crq);
1146 } else if (netdev_mc_empty(netdev)) {
1147 /* Reject all multicast */
1148 memset(&crq, 0, sizeof(crq));
1149 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1150 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1151 crq.multicast_ctrl.flags = IBMVNIC_DISABLE_ALL;
1152 ibmvnic_send_crq(adapter, &crq);
1153 } else {
1154 /* Accept one or more multicast(s) */
1155 netdev_for_each_mc_addr(ha, netdev) {
1156 memset(&crq, 0, sizeof(crq));
1157 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1158 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1159 crq.multicast_ctrl.flags = IBMVNIC_ENABLE_MC;
1160 ether_addr_copy(&crq.multicast_ctrl.mac_addr[0],
1161 ha->addr);
1162 ibmvnic_send_crq(adapter, &crq);
1163 }
1164 }
1165 }
1166}
1167
1168static int ibmvnic_set_mac(struct net_device *netdev, void *p)
1169{
1170 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1171 struct sockaddr *addr = p;
1172 union ibmvnic_crq crq;
1173
1174 if (!is_valid_ether_addr(addr->sa_data))
1175 return -EADDRNOTAVAIL;
1176
1177 memset(&crq, 0, sizeof(crq));
1178 crq.change_mac_addr.first = IBMVNIC_CRQ_CMD;
1179 crq.change_mac_addr.cmd = CHANGE_MAC_ADDR;
1180 ether_addr_copy(&crq.change_mac_addr.mac_addr[0], addr->sa_data);
1181 ibmvnic_send_crq(adapter, &crq);
1182 /* netdev->dev_addr is changed in handle_change_mac_rsp function */
1183 return 0;
1184}
1185
Nathan Fontenoted651a12017-05-03 14:04:38 -04001186/**
1187 * do_reset returns zero if we are able to keep processing reset events, or
1188 * non-zero if we hit a fatal error and must halt.
1189 */
1190static int do_reset(struct ibmvnic_adapter *adapter,
1191 struct ibmvnic_rwi *rwi, u32 reset_state)
1192{
1193 struct net_device *netdev = adapter->netdev;
1194 int i, rc;
1195
1196 netif_carrier_off(netdev);
1197 adapter->reset_reason = rwi->reset_reason;
1198
1199 if (rwi->reset_reason == VNIC_RESET_MOBILITY) {
1200 rc = ibmvnic_reenable_crq_queue(adapter);
1201 if (rc)
1202 return 0;
1203 }
1204
1205 rc = __ibmvnic_close(netdev);
1206 if (rc)
1207 return rc;
1208
1209 /* remove the closed state so when we call open it appears
1210 * we are coming from the probed state.
1211 */
1212 adapter->state = VNIC_PROBED;
1213
1214 release_resources(adapter);
1215 release_sub_crqs(adapter);
1216 release_crq_queue(adapter);
1217
1218 rc = ibmvnic_init(adapter);
1219 if (rc)
1220 return 0;
1221
1222 /* If the adapter was in PROBE state prior to the reset, exit here. */
1223 if (reset_state == VNIC_PROBED)
1224 return 0;
1225
1226 rc = ibmvnic_login(netdev);
1227 if (rc) {
1228 adapter->state = VNIC_PROBED;
1229 return 0;
1230 }
1231
1232 rtnl_lock();
1233 rc = init_resources(adapter);
1234 rtnl_unlock();
1235 if (rc)
1236 return rc;
1237
1238 if (reset_state == VNIC_CLOSED)
1239 return 0;
1240
1241 rc = __ibmvnic_open(netdev);
1242 if (rc) {
1243 if (list_empty(&adapter->rwi_list))
1244 adapter->state = VNIC_CLOSED;
1245 else
1246 adapter->state = reset_state;
1247
1248 return 0;
1249 }
1250
1251 netif_carrier_on(netdev);
1252
1253 /* kick napi */
1254 for (i = 0; i < adapter->req_rx_queues; i++)
1255 napi_schedule(&adapter->napi[i]);
1256
1257 return 0;
1258}
1259
1260static struct ibmvnic_rwi *get_next_rwi(struct ibmvnic_adapter *adapter)
1261{
1262 struct ibmvnic_rwi *rwi;
1263
1264 mutex_lock(&adapter->rwi_lock);
1265
1266 if (!list_empty(&adapter->rwi_list)) {
1267 rwi = list_first_entry(&adapter->rwi_list, struct ibmvnic_rwi,
1268 list);
1269 list_del(&rwi->list);
1270 } else {
1271 rwi = NULL;
1272 }
1273
1274 mutex_unlock(&adapter->rwi_lock);
1275 return rwi;
1276}
1277
1278static void free_all_rwi(struct ibmvnic_adapter *adapter)
1279{
1280 struct ibmvnic_rwi *rwi;
1281
1282 rwi = get_next_rwi(adapter);
1283 while (rwi) {
1284 kfree(rwi);
1285 rwi = get_next_rwi(adapter);
1286 }
1287}
1288
1289static void __ibmvnic_reset(struct work_struct *work)
1290{
1291 struct ibmvnic_rwi *rwi;
1292 struct ibmvnic_adapter *adapter;
1293 struct net_device *netdev;
1294 u32 reset_state;
1295 int rc;
1296
1297 adapter = container_of(work, struct ibmvnic_adapter, ibmvnic_reset);
1298 netdev = adapter->netdev;
1299
1300 mutex_lock(&adapter->reset_lock);
1301 adapter->resetting = true;
1302 reset_state = adapter->state;
1303
1304 rwi = get_next_rwi(adapter);
1305 while (rwi) {
1306 rc = do_reset(adapter, rwi, reset_state);
1307 kfree(rwi);
1308 if (rc)
1309 break;
1310
1311 rwi = get_next_rwi(adapter);
1312 }
1313
1314 if (rc) {
1315 free_all_rwi(adapter);
1316 return;
1317 }
1318
1319 adapter->resetting = false;
1320 mutex_unlock(&adapter->reset_lock);
1321}
1322
1323static void ibmvnic_reset(struct ibmvnic_adapter *adapter,
1324 enum ibmvnic_reset_reason reason)
1325{
1326 struct ibmvnic_rwi *rwi, *tmp;
1327 struct net_device *netdev = adapter->netdev;
1328 struct list_head *entry;
1329
1330 if (adapter->state == VNIC_REMOVING ||
1331 adapter->state == VNIC_REMOVED) {
1332 netdev_dbg(netdev, "Adapter removing, skipping reset\n");
1333 return;
1334 }
1335
1336 mutex_lock(&adapter->rwi_lock);
1337
1338 list_for_each(entry, &adapter->rwi_list) {
1339 tmp = list_entry(entry, struct ibmvnic_rwi, list);
1340 if (tmp->reset_reason == reason) {
1341 netdev_err(netdev, "Matching reset found, skipping\n");
1342 mutex_unlock(&adapter->rwi_lock);
1343 return;
1344 }
1345 }
1346
1347 rwi = kzalloc(sizeof(*rwi), GFP_KERNEL);
1348 if (!rwi) {
1349 mutex_unlock(&adapter->rwi_lock);
1350 ibmvnic_close(netdev);
1351 return;
1352 }
1353
1354 rwi->reset_reason = reason;
1355 list_add_tail(&rwi->list, &adapter->rwi_list);
1356 mutex_unlock(&adapter->rwi_lock);
1357 schedule_work(&adapter->ibmvnic_reset);
1358}
1359
Thomas Falcon032c5e82015-12-21 11:26:06 -06001360static void ibmvnic_tx_timeout(struct net_device *dev)
1361{
1362 struct ibmvnic_adapter *adapter = netdev_priv(dev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001363
Nathan Fontenoted651a12017-05-03 14:04:38 -04001364 ibmvnic_reset(adapter, VNIC_RESET_TIMEOUT);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001365}
1366
1367static void remove_buff_from_pool(struct ibmvnic_adapter *adapter,
1368 struct ibmvnic_rx_buff *rx_buff)
1369{
1370 struct ibmvnic_rx_pool *pool = &adapter->rx_pool[rx_buff->pool_index];
1371
1372 rx_buff->skb = NULL;
1373
1374 pool->free_map[pool->next_alloc] = (int)(rx_buff - pool->rx_buff);
1375 pool->next_alloc = (pool->next_alloc + 1) % pool->size;
1376
1377 atomic_dec(&pool->available);
1378}
1379
1380static int ibmvnic_poll(struct napi_struct *napi, int budget)
1381{
1382 struct net_device *netdev = napi->dev;
1383 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1384 int scrq_num = (int)(napi - adapter->napi);
1385 int frames_processed = 0;
1386restart_poll:
1387 while (frames_processed < budget) {
1388 struct sk_buff *skb;
1389 struct ibmvnic_rx_buff *rx_buff;
1390 union sub_crq *next;
1391 u32 length;
1392 u16 offset;
1393 u8 flags = 0;
1394
1395 if (!pending_scrq(adapter, adapter->rx_scrq[scrq_num]))
1396 break;
1397 next = ibmvnic_next_scrq(adapter, adapter->rx_scrq[scrq_num]);
1398 rx_buff =
1399 (struct ibmvnic_rx_buff *)be64_to_cpu(next->
1400 rx_comp.correlator);
1401 /* do error checking */
1402 if (next->rx_comp.rc) {
1403 netdev_err(netdev, "rx error %x\n", next->rx_comp.rc);
1404 /* free the entry */
1405 next->rx_comp.first = 0;
1406 remove_buff_from_pool(adapter, rx_buff);
Nathan Fontenotca05e312017-05-03 14:05:14 -04001407 continue;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001408 }
1409
1410 length = be32_to_cpu(next->rx_comp.len);
1411 offset = be16_to_cpu(next->rx_comp.off_frame_data);
1412 flags = next->rx_comp.flags;
1413 skb = rx_buff->skb;
1414 skb_copy_to_linear_data(skb, rx_buff->data + offset,
1415 length);
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04001416
1417 /* VLAN Header has been stripped by the system firmware and
1418 * needs to be inserted by the driver
1419 */
1420 if (adapter->rx_vlan_header_insertion &&
1421 (flags & IBMVNIC_VLAN_STRIPPED))
1422 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
1423 ntohs(next->rx_comp.vlan_tci));
1424
Thomas Falcon032c5e82015-12-21 11:26:06 -06001425 /* free the entry */
1426 next->rx_comp.first = 0;
1427 remove_buff_from_pool(adapter, rx_buff);
1428
1429 skb_put(skb, length);
1430 skb->protocol = eth_type_trans(skb, netdev);
Thomas Falcon94ca3052017-05-03 14:05:20 -04001431 skb_record_rx_queue(skb, scrq_num);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001432
1433 if (flags & IBMVNIC_IP_CHKSUM_GOOD &&
1434 flags & IBMVNIC_TCP_UDP_CHKSUM_GOOD) {
1435 skb->ip_summed = CHECKSUM_UNNECESSARY;
1436 }
1437
1438 length = skb->len;
1439 napi_gro_receive(napi, skb); /* send it up */
1440 netdev->stats.rx_packets++;
1441 netdev->stats.rx_bytes += length;
1442 frames_processed++;
1443 }
John Allen498cd8e2016-04-06 11:49:55 -05001444 replenish_rx_pool(adapter, &adapter->rx_pool[scrq_num]);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001445
1446 if (frames_processed < budget) {
1447 enable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
Eric Dumazet6ad20162017-01-30 08:22:01 -08001448 napi_complete_done(napi, frames_processed);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001449 if (pending_scrq(adapter, adapter->rx_scrq[scrq_num]) &&
1450 napi_reschedule(napi)) {
1451 disable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
1452 goto restart_poll;
1453 }
1454 }
1455 return frames_processed;
1456}
1457
1458#ifdef CONFIG_NET_POLL_CONTROLLER
1459static void ibmvnic_netpoll_controller(struct net_device *dev)
1460{
1461 struct ibmvnic_adapter *adapter = netdev_priv(dev);
1462 int i;
1463
1464 replenish_pools(netdev_priv(dev));
1465 for (i = 0; i < adapter->req_rx_queues; i++)
1466 ibmvnic_interrupt_rx(adapter->rx_scrq[i]->irq,
1467 adapter->rx_scrq[i]);
1468}
1469#endif
1470
1471static const struct net_device_ops ibmvnic_netdev_ops = {
1472 .ndo_open = ibmvnic_open,
1473 .ndo_stop = ibmvnic_close,
1474 .ndo_start_xmit = ibmvnic_xmit,
1475 .ndo_set_rx_mode = ibmvnic_set_multi,
1476 .ndo_set_mac_address = ibmvnic_set_mac,
1477 .ndo_validate_addr = eth_validate_addr,
Thomas Falcon032c5e82015-12-21 11:26:06 -06001478 .ndo_tx_timeout = ibmvnic_tx_timeout,
1479#ifdef CONFIG_NET_POLL_CONTROLLER
1480 .ndo_poll_controller = ibmvnic_netpoll_controller,
1481#endif
1482};
1483
1484/* ethtool functions */
1485
Philippe Reynes8a433792017-01-07 22:37:29 +01001486static int ibmvnic_get_link_ksettings(struct net_device *netdev,
1487 struct ethtool_link_ksettings *cmd)
Thomas Falcon032c5e82015-12-21 11:26:06 -06001488{
Philippe Reynes8a433792017-01-07 22:37:29 +01001489 u32 supported, advertising;
1490
1491 supported = (SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg |
Thomas Falcon032c5e82015-12-21 11:26:06 -06001492 SUPPORTED_FIBRE);
Philippe Reynes8a433792017-01-07 22:37:29 +01001493 advertising = (ADVERTISED_1000baseT_Full | ADVERTISED_Autoneg |
Thomas Falcon032c5e82015-12-21 11:26:06 -06001494 ADVERTISED_FIBRE);
Philippe Reynes8a433792017-01-07 22:37:29 +01001495 cmd->base.speed = SPEED_1000;
1496 cmd->base.duplex = DUPLEX_FULL;
1497 cmd->base.port = PORT_FIBRE;
1498 cmd->base.phy_address = 0;
1499 cmd->base.autoneg = AUTONEG_ENABLE;
1500
1501 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
1502 supported);
1503 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
1504 advertising);
1505
Thomas Falcon032c5e82015-12-21 11:26:06 -06001506 return 0;
1507}
1508
1509static void ibmvnic_get_drvinfo(struct net_device *dev,
1510 struct ethtool_drvinfo *info)
1511{
1512 strlcpy(info->driver, ibmvnic_driver_name, sizeof(info->driver));
1513 strlcpy(info->version, IBMVNIC_DRIVER_VERSION, sizeof(info->version));
1514}
1515
1516static u32 ibmvnic_get_msglevel(struct net_device *netdev)
1517{
1518 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1519
1520 return adapter->msg_enable;
1521}
1522
1523static void ibmvnic_set_msglevel(struct net_device *netdev, u32 data)
1524{
1525 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1526
1527 adapter->msg_enable = data;
1528}
1529
1530static u32 ibmvnic_get_link(struct net_device *netdev)
1531{
1532 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1533
1534 /* Don't need to send a query because we request a logical link up at
1535 * init and then we wait for link state indications
1536 */
1537 return adapter->logical_link_state;
1538}
1539
1540static void ibmvnic_get_ringparam(struct net_device *netdev,
1541 struct ethtool_ringparam *ring)
1542{
1543 ring->rx_max_pending = 0;
1544 ring->tx_max_pending = 0;
1545 ring->rx_mini_max_pending = 0;
1546 ring->rx_jumbo_max_pending = 0;
1547 ring->rx_pending = 0;
1548 ring->tx_pending = 0;
1549 ring->rx_mini_pending = 0;
1550 ring->rx_jumbo_pending = 0;
1551}
1552
1553static void ibmvnic_get_strings(struct net_device *dev, u32 stringset, u8 *data)
1554{
1555 int i;
1556
1557 if (stringset != ETH_SS_STATS)
1558 return;
1559
1560 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++, data += ETH_GSTRING_LEN)
1561 memcpy(data, ibmvnic_stats[i].name, ETH_GSTRING_LEN);
1562}
1563
1564static int ibmvnic_get_sset_count(struct net_device *dev, int sset)
1565{
1566 switch (sset) {
1567 case ETH_SS_STATS:
1568 return ARRAY_SIZE(ibmvnic_stats);
1569 default:
1570 return -EOPNOTSUPP;
1571 }
1572}
1573
1574static void ibmvnic_get_ethtool_stats(struct net_device *dev,
1575 struct ethtool_stats *stats, u64 *data)
1576{
1577 struct ibmvnic_adapter *adapter = netdev_priv(dev);
1578 union ibmvnic_crq crq;
1579 int i;
1580
1581 memset(&crq, 0, sizeof(crq));
1582 crq.request_statistics.first = IBMVNIC_CRQ_CMD;
1583 crq.request_statistics.cmd = REQUEST_STATISTICS;
1584 crq.request_statistics.ioba = cpu_to_be32(adapter->stats_token);
1585 crq.request_statistics.len =
1586 cpu_to_be32(sizeof(struct ibmvnic_statistics));
Thomas Falcon032c5e82015-12-21 11:26:06 -06001587
1588 /* Wait for data to be written */
1589 init_completion(&adapter->stats_done);
Nathan Fontenotdb5d0b52017-02-10 13:45:05 -05001590 ibmvnic_send_crq(adapter, &crq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001591 wait_for_completion(&adapter->stats_done);
1592
1593 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++)
1594 data[i] = IBMVNIC_GET_STAT(adapter, ibmvnic_stats[i].offset);
1595}
1596
1597static const struct ethtool_ops ibmvnic_ethtool_ops = {
Thomas Falcon032c5e82015-12-21 11:26:06 -06001598 .get_drvinfo = ibmvnic_get_drvinfo,
1599 .get_msglevel = ibmvnic_get_msglevel,
1600 .set_msglevel = ibmvnic_set_msglevel,
1601 .get_link = ibmvnic_get_link,
1602 .get_ringparam = ibmvnic_get_ringparam,
1603 .get_strings = ibmvnic_get_strings,
1604 .get_sset_count = ibmvnic_get_sset_count,
1605 .get_ethtool_stats = ibmvnic_get_ethtool_stats,
Philippe Reynes8a433792017-01-07 22:37:29 +01001606 .get_link_ksettings = ibmvnic_get_link_ksettings,
Thomas Falcon032c5e82015-12-21 11:26:06 -06001607};
1608
1609/* Routines for managing CRQs/sCRQs */
1610
1611static void release_sub_crq_queue(struct ibmvnic_adapter *adapter,
1612 struct ibmvnic_sub_crq_queue *scrq)
1613{
1614 struct device *dev = &adapter->vdev->dev;
1615 long rc;
1616
1617 netdev_dbg(adapter->netdev, "Releasing sub-CRQ\n");
1618
1619 /* Close the sub-crqs */
1620 do {
1621 rc = plpar_hcall_norets(H_FREE_SUB_CRQ,
1622 adapter->vdev->unit_address,
1623 scrq->crq_num);
1624 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
1625
Thomas Falconffa73852017-04-19 13:44:29 -04001626 if (rc) {
1627 netdev_err(adapter->netdev,
1628 "Failed to release sub-CRQ %16lx, rc = %ld\n",
1629 scrq->crq_num, rc);
1630 }
1631
Thomas Falcon032c5e82015-12-21 11:26:06 -06001632 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
1633 DMA_BIDIRECTIONAL);
1634 free_pages((unsigned long)scrq->msgs, 2);
1635 kfree(scrq);
1636}
1637
1638static struct ibmvnic_sub_crq_queue *init_sub_crq_queue(struct ibmvnic_adapter
1639 *adapter)
1640{
1641 struct device *dev = &adapter->vdev->dev;
1642 struct ibmvnic_sub_crq_queue *scrq;
1643 int rc;
1644
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04001645 scrq = kzalloc(sizeof(*scrq), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001646 if (!scrq)
1647 return NULL;
1648
Nathan Fontenot7f7adc52017-04-19 13:45:16 -04001649 scrq->msgs =
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04001650 (union sub_crq *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, 2);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001651 if (!scrq->msgs) {
1652 dev_warn(dev, "Couldn't allocate crq queue messages page\n");
1653 goto zero_page_failed;
1654 }
1655
1656 scrq->msg_token = dma_map_single(dev, scrq->msgs, 4 * PAGE_SIZE,
1657 DMA_BIDIRECTIONAL);
1658 if (dma_mapping_error(dev, scrq->msg_token)) {
1659 dev_warn(dev, "Couldn't map crq queue messages page\n");
1660 goto map_failed;
1661 }
1662
1663 rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
1664 4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
1665
1666 if (rc == H_RESOURCE)
1667 rc = ibmvnic_reset_crq(adapter);
1668
1669 if (rc == H_CLOSED) {
1670 dev_warn(dev, "Partner adapter not ready, waiting.\n");
1671 } else if (rc) {
1672 dev_warn(dev, "Error %d registering sub-crq\n", rc);
1673 goto reg_failed;
1674 }
1675
Thomas Falcon032c5e82015-12-21 11:26:06 -06001676 scrq->adapter = adapter;
1677 scrq->size = 4 * PAGE_SIZE / sizeof(*scrq->msgs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001678 spin_lock_init(&scrq->lock);
1679
1680 netdev_dbg(adapter->netdev,
1681 "sub-crq initialized, num %lx, hw_irq=%lx, irq=%x\n",
1682 scrq->crq_num, scrq->hw_irq, scrq->irq);
1683
1684 return scrq;
1685
Thomas Falcon032c5e82015-12-21 11:26:06 -06001686reg_failed:
1687 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
1688 DMA_BIDIRECTIONAL);
1689map_failed:
1690 free_pages((unsigned long)scrq->msgs, 2);
1691zero_page_failed:
1692 kfree(scrq);
1693
1694 return NULL;
1695}
1696
1697static void release_sub_crqs(struct ibmvnic_adapter *adapter)
1698{
1699 int i;
1700
1701 if (adapter->tx_scrq) {
Nathan Fontenotb5108882017-03-30 02:49:18 -04001702 for (i = 0; i < adapter->req_tx_queues; i++) {
1703 if (!adapter->tx_scrq[i])
1704 continue;
1705
1706 if (adapter->tx_scrq[i]->irq) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06001707 free_irq(adapter->tx_scrq[i]->irq,
1708 adapter->tx_scrq[i]);
Thomas Falcon88eb98a2016-07-06 15:35:16 -05001709 irq_dispose_mapping(adapter->tx_scrq[i]->irq);
Nathan Fontenotb5108882017-03-30 02:49:18 -04001710 adapter->tx_scrq[i]->irq = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001711 }
Nathan Fontenotb5108882017-03-30 02:49:18 -04001712
1713 release_sub_crq_queue(adapter, adapter->tx_scrq[i]);
1714 }
1715
Nathan Fontenot9501df32017-03-15 23:38:07 -04001716 kfree(adapter->tx_scrq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001717 adapter->tx_scrq = NULL;
1718 }
1719
1720 if (adapter->rx_scrq) {
Nathan Fontenotb5108882017-03-30 02:49:18 -04001721 for (i = 0; i < adapter->req_rx_queues; i++) {
1722 if (!adapter->rx_scrq[i])
1723 continue;
1724
1725 if (adapter->rx_scrq[i]->irq) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06001726 free_irq(adapter->rx_scrq[i]->irq,
1727 adapter->rx_scrq[i]);
Thomas Falcon88eb98a2016-07-06 15:35:16 -05001728 irq_dispose_mapping(adapter->rx_scrq[i]->irq);
Nathan Fontenotb5108882017-03-30 02:49:18 -04001729 adapter->rx_scrq[i]->irq = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001730 }
Nathan Fontenotb5108882017-03-30 02:49:18 -04001731
1732 release_sub_crq_queue(adapter, adapter->rx_scrq[i]);
1733 }
1734
Nathan Fontenot9501df32017-03-15 23:38:07 -04001735 kfree(adapter->rx_scrq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001736 adapter->rx_scrq = NULL;
1737 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001738}
1739
1740static int disable_scrq_irq(struct ibmvnic_adapter *adapter,
1741 struct ibmvnic_sub_crq_queue *scrq)
1742{
1743 struct device *dev = &adapter->vdev->dev;
1744 unsigned long rc;
1745
1746 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
1747 H_DISABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
1748 if (rc)
1749 dev_err(dev, "Couldn't disable scrq irq 0x%lx. rc=%ld\n",
1750 scrq->hw_irq, rc);
1751 return rc;
1752}
1753
1754static int enable_scrq_irq(struct ibmvnic_adapter *adapter,
1755 struct ibmvnic_sub_crq_queue *scrq)
1756{
1757 struct device *dev = &adapter->vdev->dev;
1758 unsigned long rc;
1759
1760 if (scrq->hw_irq > 0x100000000ULL) {
1761 dev_err(dev, "bad hw_irq = %lx\n", scrq->hw_irq);
1762 return 1;
1763 }
1764
1765 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
1766 H_ENABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
1767 if (rc)
1768 dev_err(dev, "Couldn't enable scrq irq 0x%lx. rc=%ld\n",
1769 scrq->hw_irq, rc);
1770 return rc;
1771}
1772
1773static int ibmvnic_complete_tx(struct ibmvnic_adapter *adapter,
1774 struct ibmvnic_sub_crq_queue *scrq)
1775{
1776 struct device *dev = &adapter->vdev->dev;
1777 struct ibmvnic_tx_buff *txbuff;
1778 union sub_crq *next;
1779 int index;
1780 int i, j;
Thomas Falconad7775d2016-04-01 17:20:34 -05001781 u8 first;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001782
1783restart_loop:
1784 while (pending_scrq(adapter, scrq)) {
1785 unsigned int pool = scrq->pool_index;
1786
1787 next = ibmvnic_next_scrq(adapter, scrq);
1788 for (i = 0; i < next->tx_comp.num_comps; i++) {
1789 if (next->tx_comp.rcs[i]) {
1790 dev_err(dev, "tx error %x\n",
1791 next->tx_comp.rcs[i]);
1792 continue;
1793 }
1794 index = be32_to_cpu(next->tx_comp.correlators[i]);
1795 txbuff = &adapter->tx_pool[pool].tx_buff[index];
1796
1797 for (j = 0; j < IBMVNIC_MAX_FRAGS_PER_CRQ; j++) {
1798 if (!txbuff->data_dma[j])
1799 continue;
1800
1801 txbuff->data_dma[j] = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001802 }
Thomas Falconad7775d2016-04-01 17:20:34 -05001803 /* if sub_crq was sent indirectly */
1804 first = txbuff->indir_arr[0].generic.first;
1805 if (first == IBMVNIC_CRQ_CMD) {
1806 dma_unmap_single(dev, txbuff->indir_dma,
1807 sizeof(txbuff->indir_arr),
1808 DMA_TO_DEVICE);
1809 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001810
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001811 if (txbuff->last_frag) {
Brian King58c8c0c2017-04-19 13:44:47 -04001812 if (atomic_sub_return(next->tx_comp.num_comps,
1813 &scrq->used) <=
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001814 (adapter->req_tx_entries_per_subcrq / 2) &&
1815 netif_subqueue_stopped(adapter->netdev,
1816 txbuff->skb)) {
1817 netif_wake_subqueue(adapter->netdev,
1818 scrq->pool_index);
1819 netdev_dbg(adapter->netdev,
1820 "Started queue %d\n",
1821 scrq->pool_index);
1822 }
1823
Thomas Falcon032c5e82015-12-21 11:26:06 -06001824 dev_kfree_skb_any(txbuff->skb);
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001825 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001826
1827 adapter->tx_pool[pool].free_map[adapter->tx_pool[pool].
1828 producer_index] = index;
1829 adapter->tx_pool[pool].producer_index =
1830 (adapter->tx_pool[pool].producer_index + 1) %
Thomas Falcon068d9f92017-03-05 12:18:42 -06001831 adapter->req_tx_entries_per_subcrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001832 }
1833 /* remove tx_comp scrq*/
1834 next->tx_comp.first = 0;
1835 }
1836
1837 enable_scrq_irq(adapter, scrq);
1838
1839 if (pending_scrq(adapter, scrq)) {
1840 disable_scrq_irq(adapter, scrq);
1841 goto restart_loop;
1842 }
1843
1844 return 0;
1845}
1846
1847static irqreturn_t ibmvnic_interrupt_tx(int irq, void *instance)
1848{
1849 struct ibmvnic_sub_crq_queue *scrq = instance;
1850 struct ibmvnic_adapter *adapter = scrq->adapter;
1851
1852 disable_scrq_irq(adapter, scrq);
1853 ibmvnic_complete_tx(adapter, scrq);
1854
1855 return IRQ_HANDLED;
1856}
1857
1858static irqreturn_t ibmvnic_interrupt_rx(int irq, void *instance)
1859{
1860 struct ibmvnic_sub_crq_queue *scrq = instance;
1861 struct ibmvnic_adapter *adapter = scrq->adapter;
1862
1863 if (napi_schedule_prep(&adapter->napi[scrq->scrq_num])) {
1864 disable_scrq_irq(adapter, scrq);
1865 __napi_schedule(&adapter->napi[scrq->scrq_num]);
1866 }
1867
1868 return IRQ_HANDLED;
1869}
1870
Thomas Falconea22d512016-07-06 15:35:17 -05001871static int init_sub_crq_irqs(struct ibmvnic_adapter *adapter)
1872{
1873 struct device *dev = &adapter->vdev->dev;
1874 struct ibmvnic_sub_crq_queue *scrq;
1875 int i = 0, j = 0;
1876 int rc = 0;
1877
1878 for (i = 0; i < adapter->req_tx_queues; i++) {
1879 scrq = adapter->tx_scrq[i];
1880 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
1881
Michael Ellerman99c17902016-09-10 19:59:05 +10001882 if (!scrq->irq) {
Thomas Falconea22d512016-07-06 15:35:17 -05001883 rc = -EINVAL;
1884 dev_err(dev, "Error mapping irq\n");
1885 goto req_tx_irq_failed;
1886 }
1887
1888 rc = request_irq(scrq->irq, ibmvnic_interrupt_tx,
1889 0, "ibmvnic_tx", scrq);
1890
1891 if (rc) {
1892 dev_err(dev, "Couldn't register tx irq 0x%x. rc=%d\n",
1893 scrq->irq, rc);
1894 irq_dispose_mapping(scrq->irq);
1895 goto req_rx_irq_failed;
1896 }
1897 }
1898
1899 for (i = 0; i < adapter->req_rx_queues; i++) {
1900 scrq = adapter->rx_scrq[i];
1901 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
Michael Ellerman99c17902016-09-10 19:59:05 +10001902 if (!scrq->irq) {
Thomas Falconea22d512016-07-06 15:35:17 -05001903 rc = -EINVAL;
1904 dev_err(dev, "Error mapping irq\n");
1905 goto req_rx_irq_failed;
1906 }
1907 rc = request_irq(scrq->irq, ibmvnic_interrupt_rx,
1908 0, "ibmvnic_rx", scrq);
1909 if (rc) {
1910 dev_err(dev, "Couldn't register rx irq 0x%x. rc=%d\n",
1911 scrq->irq, rc);
1912 irq_dispose_mapping(scrq->irq);
1913 goto req_rx_irq_failed;
1914 }
1915 }
1916 return rc;
1917
1918req_rx_irq_failed:
Thomas Falcon8bf371e2016-10-27 12:28:52 -05001919 for (j = 0; j < i; j++) {
Thomas Falconea22d512016-07-06 15:35:17 -05001920 free_irq(adapter->rx_scrq[j]->irq, adapter->rx_scrq[j]);
1921 irq_dispose_mapping(adapter->rx_scrq[j]->irq);
Thomas Falcon8bf371e2016-10-27 12:28:52 -05001922 }
Thomas Falconea22d512016-07-06 15:35:17 -05001923 i = adapter->req_tx_queues;
1924req_tx_irq_failed:
Thomas Falcon8bf371e2016-10-27 12:28:52 -05001925 for (j = 0; j < i; j++) {
Thomas Falconea22d512016-07-06 15:35:17 -05001926 free_irq(adapter->tx_scrq[j]->irq, adapter->tx_scrq[j]);
1927 irq_dispose_mapping(adapter->rx_scrq[j]->irq);
Thomas Falcon8bf371e2016-10-27 12:28:52 -05001928 }
Nathan Fontenotb5108882017-03-30 02:49:18 -04001929 release_sub_crqs(adapter);
Thomas Falconea22d512016-07-06 15:35:17 -05001930 return rc;
1931}
1932
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04001933static int init_sub_crqs(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06001934{
1935 struct device *dev = &adapter->vdev->dev;
1936 struct ibmvnic_sub_crq_queue **allqueues;
1937 int registered_queues = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001938 int total_queues;
1939 int more = 0;
Thomas Falconea22d512016-07-06 15:35:17 -05001940 int i;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001941
Thomas Falcon032c5e82015-12-21 11:26:06 -06001942 total_queues = adapter->req_tx_queues + adapter->req_rx_queues;
1943
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04001944 allqueues = kcalloc(total_queues, sizeof(*allqueues), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001945 if (!allqueues)
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04001946 return -1;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001947
1948 for (i = 0; i < total_queues; i++) {
1949 allqueues[i] = init_sub_crq_queue(adapter);
1950 if (!allqueues[i]) {
1951 dev_warn(dev, "Couldn't allocate all sub-crqs\n");
1952 break;
1953 }
1954 registered_queues++;
1955 }
1956
1957 /* Make sure we were able to register the minimum number of queues */
1958 if (registered_queues <
1959 adapter->min_tx_queues + adapter->min_rx_queues) {
1960 dev_err(dev, "Fatal: Couldn't init min number of sub-crqs\n");
1961 goto tx_failed;
1962 }
1963
1964 /* Distribute the failed allocated queues*/
1965 for (i = 0; i < total_queues - registered_queues + more ; i++) {
1966 netdev_dbg(adapter->netdev, "Reducing number of queues\n");
1967 switch (i % 3) {
1968 case 0:
1969 if (adapter->req_rx_queues > adapter->min_rx_queues)
1970 adapter->req_rx_queues--;
1971 else
1972 more++;
1973 break;
1974 case 1:
1975 if (adapter->req_tx_queues > adapter->min_tx_queues)
1976 adapter->req_tx_queues--;
1977 else
1978 more++;
1979 break;
1980 }
1981 }
1982
1983 adapter->tx_scrq = kcalloc(adapter->req_tx_queues,
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04001984 sizeof(*adapter->tx_scrq), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001985 if (!adapter->tx_scrq)
1986 goto tx_failed;
1987
1988 for (i = 0; i < adapter->req_tx_queues; i++) {
1989 adapter->tx_scrq[i] = allqueues[i];
1990 adapter->tx_scrq[i]->pool_index = i;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001991 }
1992
1993 adapter->rx_scrq = kcalloc(adapter->req_rx_queues,
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04001994 sizeof(*adapter->rx_scrq), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001995 if (!adapter->rx_scrq)
1996 goto rx_failed;
1997
1998 for (i = 0; i < adapter->req_rx_queues; i++) {
1999 adapter->rx_scrq[i] = allqueues[i + adapter->req_tx_queues];
2000 adapter->rx_scrq[i]->scrq_num = i;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002001 }
2002
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002003 kfree(allqueues);
2004 return 0;
2005
2006rx_failed:
2007 kfree(adapter->tx_scrq);
2008 adapter->tx_scrq = NULL;
2009tx_failed:
2010 for (i = 0; i < registered_queues; i++)
2011 release_sub_crq_queue(adapter, allqueues[i]);
2012 kfree(allqueues);
2013 return -1;
2014}
2015
2016static void ibmvnic_send_req_caps(struct ibmvnic_adapter *adapter, int retry)
2017{
2018 struct device *dev = &adapter->vdev->dev;
2019 union ibmvnic_crq crq;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002020
2021 if (!retry) {
2022 /* Sub-CRQ entries are 32 byte long */
2023 int entries_page = 4 * PAGE_SIZE / (sizeof(u64) * 4);
2024
2025 if (adapter->min_tx_entries_per_subcrq > entries_page ||
2026 adapter->min_rx_add_entries_per_subcrq > entries_page) {
2027 dev_err(dev, "Fatal, invalid entries per sub-crq\n");
2028 return;
2029 }
2030
2031 /* Get the minimum between the queried max and the entries
2032 * that fit in our PAGE_SIZE
2033 */
2034 adapter->req_tx_entries_per_subcrq =
2035 adapter->max_tx_entries_per_subcrq > entries_page ?
2036 entries_page : adapter->max_tx_entries_per_subcrq;
2037 adapter->req_rx_add_entries_per_subcrq =
2038 adapter->max_rx_add_entries_per_subcrq > entries_page ?
2039 entries_page : adapter->max_rx_add_entries_per_subcrq;
2040
2041 adapter->req_tx_queues = adapter->opt_tx_comp_sub_queues;
2042 adapter->req_rx_queues = adapter->opt_rx_comp_queues;
2043 adapter->req_rx_add_queues = adapter->max_rx_add_queues;
2044
2045 adapter->req_mtu = adapter->netdev->mtu + ETH_HLEN;
2046 }
2047
Thomas Falcon032c5e82015-12-21 11:26:06 -06002048 memset(&crq, 0, sizeof(crq));
2049 crq.request_capability.first = IBMVNIC_CRQ_CMD;
2050 crq.request_capability.cmd = REQUEST_CAPABILITY;
2051
2052 crq.request_capability.capability = cpu_to_be16(REQ_TX_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06002053 crq.request_capability.number = cpu_to_be64(adapter->req_tx_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06002054 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002055 ibmvnic_send_crq(adapter, &crq);
2056
2057 crq.request_capability.capability = cpu_to_be16(REQ_RX_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06002058 crq.request_capability.number = cpu_to_be64(adapter->req_rx_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06002059 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002060 ibmvnic_send_crq(adapter, &crq);
2061
2062 crq.request_capability.capability = cpu_to_be16(REQ_RX_ADD_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06002063 crq.request_capability.number = cpu_to_be64(adapter->req_rx_add_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06002064 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002065 ibmvnic_send_crq(adapter, &crq);
2066
2067 crq.request_capability.capability =
2068 cpu_to_be16(REQ_TX_ENTRIES_PER_SUBCRQ);
2069 crq.request_capability.number =
Thomas Falconde89e852016-03-01 10:20:09 -06002070 cpu_to_be64(adapter->req_tx_entries_per_subcrq);
Thomas Falcon901e0402017-02-15 12:17:59 -06002071 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002072 ibmvnic_send_crq(adapter, &crq);
2073
2074 crq.request_capability.capability =
2075 cpu_to_be16(REQ_RX_ADD_ENTRIES_PER_SUBCRQ);
2076 crq.request_capability.number =
Thomas Falconde89e852016-03-01 10:20:09 -06002077 cpu_to_be64(adapter->req_rx_add_entries_per_subcrq);
Thomas Falcon901e0402017-02-15 12:17:59 -06002078 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002079 ibmvnic_send_crq(adapter, &crq);
2080
2081 crq.request_capability.capability = cpu_to_be16(REQ_MTU);
Thomas Falconde89e852016-03-01 10:20:09 -06002082 crq.request_capability.number = cpu_to_be64(adapter->req_mtu);
Thomas Falcon901e0402017-02-15 12:17:59 -06002083 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002084 ibmvnic_send_crq(adapter, &crq);
2085
2086 if (adapter->netdev->flags & IFF_PROMISC) {
2087 if (adapter->promisc_supported) {
2088 crq.request_capability.capability =
2089 cpu_to_be16(PROMISC_REQUESTED);
Thomas Falconde89e852016-03-01 10:20:09 -06002090 crq.request_capability.number = cpu_to_be64(1);
Thomas Falcon901e0402017-02-15 12:17:59 -06002091 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002092 ibmvnic_send_crq(adapter, &crq);
2093 }
2094 } else {
2095 crq.request_capability.capability =
2096 cpu_to_be16(PROMISC_REQUESTED);
Thomas Falconde89e852016-03-01 10:20:09 -06002097 crq.request_capability.number = cpu_to_be64(0);
Thomas Falcon901e0402017-02-15 12:17:59 -06002098 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002099 ibmvnic_send_crq(adapter, &crq);
2100 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002101}
2102
2103static int pending_scrq(struct ibmvnic_adapter *adapter,
2104 struct ibmvnic_sub_crq_queue *scrq)
2105{
2106 union sub_crq *entry = &scrq->msgs[scrq->cur];
2107
Nathan Fontenot90c80142017-05-03 14:04:32 -04002108 if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP ||
2109 adapter->state == VNIC_CLOSING)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002110 return 1;
2111 else
2112 return 0;
2113}
2114
2115static union sub_crq *ibmvnic_next_scrq(struct ibmvnic_adapter *adapter,
2116 struct ibmvnic_sub_crq_queue *scrq)
2117{
2118 union sub_crq *entry;
2119 unsigned long flags;
2120
2121 spin_lock_irqsave(&scrq->lock, flags);
2122 entry = &scrq->msgs[scrq->cur];
2123 if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP) {
2124 if (++scrq->cur == scrq->size)
2125 scrq->cur = 0;
2126 } else {
2127 entry = NULL;
2128 }
2129 spin_unlock_irqrestore(&scrq->lock, flags);
2130
2131 return entry;
2132}
2133
2134static union ibmvnic_crq *ibmvnic_next_crq(struct ibmvnic_adapter *adapter)
2135{
2136 struct ibmvnic_crq_queue *queue = &adapter->crq;
2137 union ibmvnic_crq *crq;
2138
2139 crq = &queue->msgs[queue->cur];
2140 if (crq->generic.first & IBMVNIC_CRQ_CMD_RSP) {
2141 if (++queue->cur == queue->size)
2142 queue->cur = 0;
2143 } else {
2144 crq = NULL;
2145 }
2146
2147 return crq;
2148}
2149
2150static int send_subcrq(struct ibmvnic_adapter *adapter, u64 remote_handle,
2151 union sub_crq *sub_crq)
2152{
2153 unsigned int ua = adapter->vdev->unit_address;
2154 struct device *dev = &adapter->vdev->dev;
2155 u64 *u64_crq = (u64 *)sub_crq;
2156 int rc;
2157
2158 netdev_dbg(adapter->netdev,
2159 "Sending sCRQ %016lx: %016lx %016lx %016lx %016lx\n",
2160 (unsigned long int)cpu_to_be64(remote_handle),
2161 (unsigned long int)cpu_to_be64(u64_crq[0]),
2162 (unsigned long int)cpu_to_be64(u64_crq[1]),
2163 (unsigned long int)cpu_to_be64(u64_crq[2]),
2164 (unsigned long int)cpu_to_be64(u64_crq[3]));
2165
2166 /* Make sure the hypervisor sees the complete request */
2167 mb();
2168
2169 rc = plpar_hcall_norets(H_SEND_SUB_CRQ, ua,
2170 cpu_to_be64(remote_handle),
2171 cpu_to_be64(u64_crq[0]),
2172 cpu_to_be64(u64_crq[1]),
2173 cpu_to_be64(u64_crq[2]),
2174 cpu_to_be64(u64_crq[3]));
2175
2176 if (rc) {
2177 if (rc == H_CLOSED)
2178 dev_warn(dev, "CRQ Queue closed\n");
2179 dev_err(dev, "Send error (rc=%d)\n", rc);
2180 }
2181
2182 return rc;
2183}
2184
Thomas Falconad7775d2016-04-01 17:20:34 -05002185static int send_subcrq_indirect(struct ibmvnic_adapter *adapter,
2186 u64 remote_handle, u64 ioba, u64 num_entries)
2187{
2188 unsigned int ua = adapter->vdev->unit_address;
2189 struct device *dev = &adapter->vdev->dev;
2190 int rc;
2191
2192 /* Make sure the hypervisor sees the complete request */
2193 mb();
2194 rc = plpar_hcall_norets(H_SEND_SUB_CRQ_INDIRECT, ua,
2195 cpu_to_be64(remote_handle),
2196 ioba, num_entries);
2197
2198 if (rc) {
2199 if (rc == H_CLOSED)
2200 dev_warn(dev, "CRQ Queue closed\n");
2201 dev_err(dev, "Send (indirect) error (rc=%d)\n", rc);
2202 }
2203
2204 return rc;
2205}
2206
Thomas Falcon032c5e82015-12-21 11:26:06 -06002207static int ibmvnic_send_crq(struct ibmvnic_adapter *adapter,
2208 union ibmvnic_crq *crq)
2209{
2210 unsigned int ua = adapter->vdev->unit_address;
2211 struct device *dev = &adapter->vdev->dev;
2212 u64 *u64_crq = (u64 *)crq;
2213 int rc;
2214
2215 netdev_dbg(adapter->netdev, "Sending CRQ: %016lx %016lx\n",
2216 (unsigned long int)cpu_to_be64(u64_crq[0]),
2217 (unsigned long int)cpu_to_be64(u64_crq[1]));
2218
2219 /* Make sure the hypervisor sees the complete request */
2220 mb();
2221
2222 rc = plpar_hcall_norets(H_SEND_CRQ, ua,
2223 cpu_to_be64(u64_crq[0]),
2224 cpu_to_be64(u64_crq[1]));
2225
2226 if (rc) {
2227 if (rc == H_CLOSED)
2228 dev_warn(dev, "CRQ Queue closed\n");
2229 dev_warn(dev, "Send error (rc=%d)\n", rc);
2230 }
2231
2232 return rc;
2233}
2234
2235static int ibmvnic_send_crq_init(struct ibmvnic_adapter *adapter)
2236{
2237 union ibmvnic_crq crq;
2238
2239 memset(&crq, 0, sizeof(crq));
2240 crq.generic.first = IBMVNIC_CRQ_INIT_CMD;
2241 crq.generic.cmd = IBMVNIC_CRQ_INIT;
2242 netdev_dbg(adapter->netdev, "Sending CRQ init\n");
2243
2244 return ibmvnic_send_crq(adapter, &crq);
2245}
2246
Thomas Falcon032c5e82015-12-21 11:26:06 -06002247static int send_version_xchg(struct ibmvnic_adapter *adapter)
2248{
2249 union ibmvnic_crq crq;
2250
2251 memset(&crq, 0, sizeof(crq));
2252 crq.version_exchange.first = IBMVNIC_CRQ_CMD;
2253 crq.version_exchange.cmd = VERSION_EXCHANGE;
2254 crq.version_exchange.version = cpu_to_be16(ibmvnic_version);
2255
2256 return ibmvnic_send_crq(adapter, &crq);
2257}
2258
2259static void send_login(struct ibmvnic_adapter *adapter)
2260{
2261 struct ibmvnic_login_rsp_buffer *login_rsp_buffer;
2262 struct ibmvnic_login_buffer *login_buffer;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002263 struct device *dev = &adapter->vdev->dev;
2264 dma_addr_t rsp_buffer_token;
2265 dma_addr_t buffer_token;
2266 size_t rsp_buffer_size;
2267 union ibmvnic_crq crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002268 size_t buffer_size;
2269 __be64 *tx_list_p;
2270 __be64 *rx_list_p;
2271 int i;
2272
2273 buffer_size =
2274 sizeof(struct ibmvnic_login_buffer) +
2275 sizeof(u64) * (adapter->req_tx_queues + adapter->req_rx_queues);
2276
2277 login_buffer = kmalloc(buffer_size, GFP_ATOMIC);
2278 if (!login_buffer)
2279 goto buf_alloc_failed;
2280
2281 buffer_token = dma_map_single(dev, login_buffer, buffer_size,
2282 DMA_TO_DEVICE);
2283 if (dma_mapping_error(dev, buffer_token)) {
2284 dev_err(dev, "Couldn't map login buffer\n");
2285 goto buf_map_failed;
2286 }
2287
John Allen498cd8e2016-04-06 11:49:55 -05002288 rsp_buffer_size = sizeof(struct ibmvnic_login_rsp_buffer) +
2289 sizeof(u64) * adapter->req_tx_queues +
2290 sizeof(u64) * adapter->req_rx_queues +
2291 sizeof(u64) * adapter->req_rx_queues +
2292 sizeof(u8) * IBMVNIC_TX_DESC_VERSIONS;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002293
2294 login_rsp_buffer = kmalloc(rsp_buffer_size, GFP_ATOMIC);
2295 if (!login_rsp_buffer)
2296 goto buf_rsp_alloc_failed;
2297
2298 rsp_buffer_token = dma_map_single(dev, login_rsp_buffer,
2299 rsp_buffer_size, DMA_FROM_DEVICE);
2300 if (dma_mapping_error(dev, rsp_buffer_token)) {
2301 dev_err(dev, "Couldn't map login rsp buffer\n");
2302 goto buf_rsp_map_failed;
2303 }
Nathan Fontenot661a2622017-04-19 13:44:58 -04002304
Thomas Falcon032c5e82015-12-21 11:26:06 -06002305 adapter->login_buf = login_buffer;
2306 adapter->login_buf_token = buffer_token;
2307 adapter->login_buf_sz = buffer_size;
2308 adapter->login_rsp_buf = login_rsp_buffer;
2309 adapter->login_rsp_buf_token = rsp_buffer_token;
2310 adapter->login_rsp_buf_sz = rsp_buffer_size;
2311
2312 login_buffer->len = cpu_to_be32(buffer_size);
2313 login_buffer->version = cpu_to_be32(INITIAL_VERSION_LB);
2314 login_buffer->num_txcomp_subcrqs = cpu_to_be32(adapter->req_tx_queues);
2315 login_buffer->off_txcomp_subcrqs =
2316 cpu_to_be32(sizeof(struct ibmvnic_login_buffer));
2317 login_buffer->num_rxcomp_subcrqs = cpu_to_be32(adapter->req_rx_queues);
2318 login_buffer->off_rxcomp_subcrqs =
2319 cpu_to_be32(sizeof(struct ibmvnic_login_buffer) +
2320 sizeof(u64) * adapter->req_tx_queues);
2321 login_buffer->login_rsp_ioba = cpu_to_be32(rsp_buffer_token);
2322 login_buffer->login_rsp_len = cpu_to_be32(rsp_buffer_size);
2323
2324 tx_list_p = (__be64 *)((char *)login_buffer +
2325 sizeof(struct ibmvnic_login_buffer));
2326 rx_list_p = (__be64 *)((char *)login_buffer +
2327 sizeof(struct ibmvnic_login_buffer) +
2328 sizeof(u64) * adapter->req_tx_queues);
2329
2330 for (i = 0; i < adapter->req_tx_queues; i++) {
2331 if (adapter->tx_scrq[i]) {
2332 tx_list_p[i] = cpu_to_be64(adapter->tx_scrq[i]->
2333 crq_num);
2334 }
2335 }
2336
2337 for (i = 0; i < adapter->req_rx_queues; i++) {
2338 if (adapter->rx_scrq[i]) {
2339 rx_list_p[i] = cpu_to_be64(adapter->rx_scrq[i]->
2340 crq_num);
2341 }
2342 }
2343
2344 netdev_dbg(adapter->netdev, "Login Buffer:\n");
2345 for (i = 0; i < (adapter->login_buf_sz - 1) / 8 + 1; i++) {
2346 netdev_dbg(adapter->netdev, "%016lx\n",
2347 ((unsigned long int *)(adapter->login_buf))[i]);
2348 }
2349
2350 memset(&crq, 0, sizeof(crq));
2351 crq.login.first = IBMVNIC_CRQ_CMD;
2352 crq.login.cmd = LOGIN;
2353 crq.login.ioba = cpu_to_be32(buffer_token);
2354 crq.login.len = cpu_to_be32(buffer_size);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002355 ibmvnic_send_crq(adapter, &crq);
2356
2357 return;
2358
Thomas Falcon032c5e82015-12-21 11:26:06 -06002359buf_rsp_map_failed:
2360 kfree(login_rsp_buffer);
2361buf_rsp_alloc_failed:
2362 dma_unmap_single(dev, buffer_token, buffer_size, DMA_TO_DEVICE);
2363buf_map_failed:
2364 kfree(login_buffer);
2365buf_alloc_failed:
2366 return;
2367}
2368
2369static void send_request_map(struct ibmvnic_adapter *adapter, dma_addr_t addr,
2370 u32 len, u8 map_id)
2371{
2372 union ibmvnic_crq crq;
2373
2374 memset(&crq, 0, sizeof(crq));
2375 crq.request_map.first = IBMVNIC_CRQ_CMD;
2376 crq.request_map.cmd = REQUEST_MAP;
2377 crq.request_map.map_id = map_id;
2378 crq.request_map.ioba = cpu_to_be32(addr);
2379 crq.request_map.len = cpu_to_be32(len);
2380 ibmvnic_send_crq(adapter, &crq);
2381}
2382
2383static void send_request_unmap(struct ibmvnic_adapter *adapter, u8 map_id)
2384{
2385 union ibmvnic_crq crq;
2386
2387 memset(&crq, 0, sizeof(crq));
2388 crq.request_unmap.first = IBMVNIC_CRQ_CMD;
2389 crq.request_unmap.cmd = REQUEST_UNMAP;
2390 crq.request_unmap.map_id = map_id;
2391 ibmvnic_send_crq(adapter, &crq);
2392}
2393
2394static void send_map_query(struct ibmvnic_adapter *adapter)
2395{
2396 union ibmvnic_crq crq;
2397
2398 memset(&crq, 0, sizeof(crq));
2399 crq.query_map.first = IBMVNIC_CRQ_CMD;
2400 crq.query_map.cmd = QUERY_MAP;
2401 ibmvnic_send_crq(adapter, &crq);
2402}
2403
2404/* Send a series of CRQs requesting various capabilities of the VNIC server */
2405static void send_cap_queries(struct ibmvnic_adapter *adapter)
2406{
2407 union ibmvnic_crq crq;
2408
Thomas Falcon901e0402017-02-15 12:17:59 -06002409 atomic_set(&adapter->running_cap_crqs, 0);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002410 memset(&crq, 0, sizeof(crq));
2411 crq.query_capability.first = IBMVNIC_CRQ_CMD;
2412 crq.query_capability.cmd = QUERY_CAPABILITY;
2413
2414 crq.query_capability.capability = cpu_to_be16(MIN_TX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002415 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002416 ibmvnic_send_crq(adapter, &crq);
2417
2418 crq.query_capability.capability = cpu_to_be16(MIN_RX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002419 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002420 ibmvnic_send_crq(adapter, &crq);
2421
2422 crq.query_capability.capability = cpu_to_be16(MIN_RX_ADD_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002423 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002424 ibmvnic_send_crq(adapter, &crq);
2425
2426 crq.query_capability.capability = cpu_to_be16(MAX_TX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002427 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002428 ibmvnic_send_crq(adapter, &crq);
2429
2430 crq.query_capability.capability = cpu_to_be16(MAX_RX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002431 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002432 ibmvnic_send_crq(adapter, &crq);
2433
2434 crq.query_capability.capability = cpu_to_be16(MAX_RX_ADD_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002435 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002436 ibmvnic_send_crq(adapter, &crq);
2437
2438 crq.query_capability.capability =
2439 cpu_to_be16(MIN_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06002440 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002441 ibmvnic_send_crq(adapter, &crq);
2442
2443 crq.query_capability.capability =
2444 cpu_to_be16(MIN_RX_ADD_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06002445 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002446 ibmvnic_send_crq(adapter, &crq);
2447
2448 crq.query_capability.capability =
2449 cpu_to_be16(MAX_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06002450 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002451 ibmvnic_send_crq(adapter, &crq);
2452
2453 crq.query_capability.capability =
2454 cpu_to_be16(MAX_RX_ADD_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06002455 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002456 ibmvnic_send_crq(adapter, &crq);
2457
2458 crq.query_capability.capability = cpu_to_be16(TCP_IP_OFFLOAD);
Thomas Falcon901e0402017-02-15 12:17:59 -06002459 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002460 ibmvnic_send_crq(adapter, &crq);
2461
2462 crq.query_capability.capability = cpu_to_be16(PROMISC_SUPPORTED);
Thomas Falcon901e0402017-02-15 12:17:59 -06002463 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002464 ibmvnic_send_crq(adapter, &crq);
2465
2466 crq.query_capability.capability = cpu_to_be16(MIN_MTU);
Thomas Falcon901e0402017-02-15 12:17:59 -06002467 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002468 ibmvnic_send_crq(adapter, &crq);
2469
2470 crq.query_capability.capability = cpu_to_be16(MAX_MTU);
Thomas Falcon901e0402017-02-15 12:17:59 -06002471 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002472 ibmvnic_send_crq(adapter, &crq);
2473
2474 crq.query_capability.capability = cpu_to_be16(MAX_MULTICAST_FILTERS);
Thomas Falcon901e0402017-02-15 12:17:59 -06002475 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002476 ibmvnic_send_crq(adapter, &crq);
2477
2478 crq.query_capability.capability = cpu_to_be16(VLAN_HEADER_INSERTION);
Thomas Falcon901e0402017-02-15 12:17:59 -06002479 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002480 ibmvnic_send_crq(adapter, &crq);
2481
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04002482 crq.query_capability.capability = cpu_to_be16(RX_VLAN_HEADER_INSERTION);
2483 atomic_inc(&adapter->running_cap_crqs);
2484 ibmvnic_send_crq(adapter, &crq);
2485
Thomas Falcon032c5e82015-12-21 11:26:06 -06002486 crq.query_capability.capability = cpu_to_be16(MAX_TX_SG_ENTRIES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002487 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002488 ibmvnic_send_crq(adapter, &crq);
2489
2490 crq.query_capability.capability = cpu_to_be16(RX_SG_SUPPORTED);
Thomas Falcon901e0402017-02-15 12:17:59 -06002491 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002492 ibmvnic_send_crq(adapter, &crq);
2493
2494 crq.query_capability.capability = cpu_to_be16(OPT_TX_COMP_SUB_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002495 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002496 ibmvnic_send_crq(adapter, &crq);
2497
2498 crq.query_capability.capability = cpu_to_be16(OPT_RX_COMP_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002499 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002500 ibmvnic_send_crq(adapter, &crq);
2501
2502 crq.query_capability.capability =
2503 cpu_to_be16(OPT_RX_BUFADD_Q_PER_RX_COMP_Q);
Thomas Falcon901e0402017-02-15 12:17:59 -06002504 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002505 ibmvnic_send_crq(adapter, &crq);
2506
2507 crq.query_capability.capability =
2508 cpu_to_be16(OPT_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06002509 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002510 ibmvnic_send_crq(adapter, &crq);
2511
2512 crq.query_capability.capability =
2513 cpu_to_be16(OPT_RXBA_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06002514 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002515 ibmvnic_send_crq(adapter, &crq);
2516
2517 crq.query_capability.capability = cpu_to_be16(TX_RX_DESC_REQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06002518 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002519 ibmvnic_send_crq(adapter, &crq);
2520}
2521
2522static void handle_query_ip_offload_rsp(struct ibmvnic_adapter *adapter)
2523{
2524 struct device *dev = &adapter->vdev->dev;
2525 struct ibmvnic_query_ip_offload_buffer *buf = &adapter->ip_offload_buf;
2526 union ibmvnic_crq crq;
2527 int i;
2528
2529 dma_unmap_single(dev, adapter->ip_offload_tok,
2530 sizeof(adapter->ip_offload_buf), DMA_FROM_DEVICE);
2531
2532 netdev_dbg(adapter->netdev, "Query IP Offload Buffer:\n");
2533 for (i = 0; i < (sizeof(adapter->ip_offload_buf) - 1) / 8 + 1; i++)
2534 netdev_dbg(adapter->netdev, "%016lx\n",
2535 ((unsigned long int *)(buf))[i]);
2536
2537 netdev_dbg(adapter->netdev, "ipv4_chksum = %d\n", buf->ipv4_chksum);
2538 netdev_dbg(adapter->netdev, "ipv6_chksum = %d\n", buf->ipv6_chksum);
2539 netdev_dbg(adapter->netdev, "tcp_ipv4_chksum = %d\n",
2540 buf->tcp_ipv4_chksum);
2541 netdev_dbg(adapter->netdev, "tcp_ipv6_chksum = %d\n",
2542 buf->tcp_ipv6_chksum);
2543 netdev_dbg(adapter->netdev, "udp_ipv4_chksum = %d\n",
2544 buf->udp_ipv4_chksum);
2545 netdev_dbg(adapter->netdev, "udp_ipv6_chksum = %d\n",
2546 buf->udp_ipv6_chksum);
2547 netdev_dbg(adapter->netdev, "large_tx_ipv4 = %d\n",
2548 buf->large_tx_ipv4);
2549 netdev_dbg(adapter->netdev, "large_tx_ipv6 = %d\n",
2550 buf->large_tx_ipv6);
2551 netdev_dbg(adapter->netdev, "large_rx_ipv4 = %d\n",
2552 buf->large_rx_ipv4);
2553 netdev_dbg(adapter->netdev, "large_rx_ipv6 = %d\n",
2554 buf->large_rx_ipv6);
2555 netdev_dbg(adapter->netdev, "max_ipv4_hdr_sz = %d\n",
2556 buf->max_ipv4_header_size);
2557 netdev_dbg(adapter->netdev, "max_ipv6_hdr_sz = %d\n",
2558 buf->max_ipv6_header_size);
2559 netdev_dbg(adapter->netdev, "max_tcp_hdr_size = %d\n",
2560 buf->max_tcp_header_size);
2561 netdev_dbg(adapter->netdev, "max_udp_hdr_size = %d\n",
2562 buf->max_udp_header_size);
2563 netdev_dbg(adapter->netdev, "max_large_tx_size = %d\n",
2564 buf->max_large_tx_size);
2565 netdev_dbg(adapter->netdev, "max_large_rx_size = %d\n",
2566 buf->max_large_rx_size);
2567 netdev_dbg(adapter->netdev, "ipv6_ext_hdr = %d\n",
2568 buf->ipv6_extension_header);
2569 netdev_dbg(adapter->netdev, "tcp_pseudosum_req = %d\n",
2570 buf->tcp_pseudosum_req);
2571 netdev_dbg(adapter->netdev, "num_ipv6_ext_hd = %d\n",
2572 buf->num_ipv6_ext_headers);
2573 netdev_dbg(adapter->netdev, "off_ipv6_ext_hd = %d\n",
2574 buf->off_ipv6_ext_headers);
2575
2576 adapter->ip_offload_ctrl_tok =
2577 dma_map_single(dev, &adapter->ip_offload_ctrl,
2578 sizeof(adapter->ip_offload_ctrl), DMA_TO_DEVICE);
2579
2580 if (dma_mapping_error(dev, adapter->ip_offload_ctrl_tok)) {
2581 dev_err(dev, "Couldn't map ip offload control buffer\n");
2582 return;
2583 }
2584
2585 adapter->ip_offload_ctrl.version = cpu_to_be32(INITIAL_VERSION_IOB);
2586 adapter->ip_offload_ctrl.tcp_ipv4_chksum = buf->tcp_ipv4_chksum;
2587 adapter->ip_offload_ctrl.udp_ipv4_chksum = buf->udp_ipv4_chksum;
2588 adapter->ip_offload_ctrl.tcp_ipv6_chksum = buf->tcp_ipv6_chksum;
2589 adapter->ip_offload_ctrl.udp_ipv6_chksum = buf->udp_ipv6_chksum;
2590
2591 /* large_tx/rx disabled for now, additional features needed */
2592 adapter->ip_offload_ctrl.large_tx_ipv4 = 0;
2593 adapter->ip_offload_ctrl.large_tx_ipv6 = 0;
2594 adapter->ip_offload_ctrl.large_rx_ipv4 = 0;
2595 adapter->ip_offload_ctrl.large_rx_ipv6 = 0;
2596
2597 adapter->netdev->features = NETIF_F_GSO;
2598
2599 if (buf->tcp_ipv4_chksum || buf->udp_ipv4_chksum)
2600 adapter->netdev->features |= NETIF_F_IP_CSUM;
2601
2602 if (buf->tcp_ipv6_chksum || buf->udp_ipv6_chksum)
2603 adapter->netdev->features |= NETIF_F_IPV6_CSUM;
2604
Thomas Falcon9be02cd2016-04-01 17:20:35 -05002605 if ((adapter->netdev->features &
2606 (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)))
2607 adapter->netdev->features |= NETIF_F_RXCSUM;
2608
Thomas Falcon032c5e82015-12-21 11:26:06 -06002609 memset(&crq, 0, sizeof(crq));
2610 crq.control_ip_offload.first = IBMVNIC_CRQ_CMD;
2611 crq.control_ip_offload.cmd = CONTROL_IP_OFFLOAD;
2612 crq.control_ip_offload.len =
2613 cpu_to_be32(sizeof(adapter->ip_offload_ctrl));
2614 crq.control_ip_offload.ioba = cpu_to_be32(adapter->ip_offload_ctrl_tok);
2615 ibmvnic_send_crq(adapter, &crq);
2616}
2617
2618static void handle_error_info_rsp(union ibmvnic_crq *crq,
2619 struct ibmvnic_adapter *adapter)
2620{
2621 struct device *dev = &adapter->vdev->dev;
Wei Yongjun96183182016-06-27 20:48:53 +08002622 struct ibmvnic_error_buff *error_buff, *tmp;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002623 unsigned long flags;
2624 bool found = false;
2625 int i;
2626
2627 if (!crq->request_error_rsp.rc.code) {
2628 dev_info(dev, "Request Error Rsp returned with rc=%x\n",
2629 crq->request_error_rsp.rc.code);
2630 return;
2631 }
2632
2633 spin_lock_irqsave(&adapter->error_list_lock, flags);
Wei Yongjun96183182016-06-27 20:48:53 +08002634 list_for_each_entry_safe(error_buff, tmp, &adapter->errors, list)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002635 if (error_buff->error_id == crq->request_error_rsp.error_id) {
2636 found = true;
2637 list_del(&error_buff->list);
2638 break;
2639 }
2640 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
2641
2642 if (!found) {
2643 dev_err(dev, "Couldn't find error id %x\n",
Thomas Falcon75224c92017-02-15 10:33:33 -06002644 be32_to_cpu(crq->request_error_rsp.error_id));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002645 return;
2646 }
2647
2648 dev_err(dev, "Detailed info for error id %x:",
Thomas Falcon75224c92017-02-15 10:33:33 -06002649 be32_to_cpu(crq->request_error_rsp.error_id));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002650
2651 for (i = 0; i < error_buff->len; i++) {
2652 pr_cont("%02x", (int)error_buff->buff[i]);
2653 if (i % 8 == 7)
2654 pr_cont(" ");
2655 }
2656 pr_cont("\n");
2657
2658 dma_unmap_single(dev, error_buff->dma, error_buff->len,
2659 DMA_FROM_DEVICE);
2660 kfree(error_buff->buff);
2661 kfree(error_buff);
2662}
2663
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04002664static void request_error_information(struct ibmvnic_adapter *adapter,
2665 union ibmvnic_crq *err_crq)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002666{
Thomas Falcon032c5e82015-12-21 11:26:06 -06002667 struct device *dev = &adapter->vdev->dev;
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04002668 struct net_device *netdev = adapter->netdev;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002669 struct ibmvnic_error_buff *error_buff;
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04002670 unsigned long timeout = msecs_to_jiffies(30000);
2671 union ibmvnic_crq crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002672 unsigned long flags;
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04002673 int rc, detail_len;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002674
2675 error_buff = kmalloc(sizeof(*error_buff), GFP_ATOMIC);
2676 if (!error_buff)
2677 return;
2678
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04002679 detail_len = be32_to_cpu(err_crq->error_indication.detail_error_sz);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002680 error_buff->buff = kmalloc(detail_len, GFP_ATOMIC);
2681 if (!error_buff->buff) {
2682 kfree(error_buff);
2683 return;
2684 }
2685
2686 error_buff->dma = dma_map_single(dev, error_buff->buff, detail_len,
2687 DMA_FROM_DEVICE);
2688 if (dma_mapping_error(dev, error_buff->dma)) {
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04002689 netdev_err(netdev, "Couldn't map error buffer\n");
Thomas Falcon032c5e82015-12-21 11:26:06 -06002690 kfree(error_buff->buff);
2691 kfree(error_buff);
2692 return;
2693 }
2694
Thomas Falcon032c5e82015-12-21 11:26:06 -06002695 error_buff->len = detail_len;
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04002696 error_buff->error_id = err_crq->error_indication.error_id;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002697
2698 spin_lock_irqsave(&adapter->error_list_lock, flags);
2699 list_add_tail(&error_buff->list, &adapter->errors);
2700 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
2701
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04002702 memset(&crq, 0, sizeof(crq));
2703 crq.request_error_info.first = IBMVNIC_CRQ_CMD;
2704 crq.request_error_info.cmd = REQUEST_ERROR_INFO;
2705 crq.request_error_info.ioba = cpu_to_be32(error_buff->dma);
2706 crq.request_error_info.len = cpu_to_be32(detail_len);
2707 crq.request_error_info.error_id = err_crq->error_indication.error_id;
2708
2709 rc = ibmvnic_send_crq(adapter, &crq);
2710 if (rc) {
2711 netdev_err(netdev, "failed to request error information\n");
2712 goto err_info_fail;
2713 }
2714
2715 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
2716 netdev_err(netdev, "timeout waiting for error information\n");
2717 goto err_info_fail;
2718 }
2719
2720 return;
2721
2722err_info_fail:
2723 spin_lock_irqsave(&adapter->error_list_lock, flags);
2724 list_del(&error_buff->list);
2725 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
2726
2727 kfree(error_buff->buff);
2728 kfree(error_buff);
2729}
2730
2731static void handle_error_indication(union ibmvnic_crq *crq,
2732 struct ibmvnic_adapter *adapter)
2733{
2734 struct device *dev = &adapter->vdev->dev;
2735
2736 dev_err(dev, "Firmware reports %serror id %x, cause %d\n",
2737 crq->error_indication.flags
2738 & IBMVNIC_FATAL_ERROR ? "FATAL " : "",
2739 be32_to_cpu(crq->error_indication.error_id),
2740 be16_to_cpu(crq->error_indication.error_cause));
2741
2742 if (be32_to_cpu(crq->error_indication.error_id))
2743 request_error_information(adapter, crq);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002744
2745 if (crq->error_indication.flags & IBMVNIC_FATAL_ERROR)
2746 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002747}
2748
2749static void handle_change_mac_rsp(union ibmvnic_crq *crq,
2750 struct ibmvnic_adapter *adapter)
2751{
2752 struct net_device *netdev = adapter->netdev;
2753 struct device *dev = &adapter->vdev->dev;
2754 long rc;
2755
2756 rc = crq->change_mac_addr_rsp.rc.code;
2757 if (rc) {
2758 dev_err(dev, "Error %ld in CHANGE_MAC_ADDR_RSP\n", rc);
2759 return;
2760 }
2761 memcpy(netdev->dev_addr, &crq->change_mac_addr_rsp.mac_addr[0],
2762 ETH_ALEN);
2763}
2764
2765static void handle_request_cap_rsp(union ibmvnic_crq *crq,
2766 struct ibmvnic_adapter *adapter)
2767{
2768 struct device *dev = &adapter->vdev->dev;
2769 u64 *req_value;
2770 char *name;
2771
Thomas Falcon901e0402017-02-15 12:17:59 -06002772 atomic_dec(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002773 switch (be16_to_cpu(crq->request_capability_rsp.capability)) {
2774 case REQ_TX_QUEUES:
2775 req_value = &adapter->req_tx_queues;
2776 name = "tx";
2777 break;
2778 case REQ_RX_QUEUES:
2779 req_value = &adapter->req_rx_queues;
2780 name = "rx";
2781 break;
2782 case REQ_RX_ADD_QUEUES:
2783 req_value = &adapter->req_rx_add_queues;
2784 name = "rx_add";
2785 break;
2786 case REQ_TX_ENTRIES_PER_SUBCRQ:
2787 req_value = &adapter->req_tx_entries_per_subcrq;
2788 name = "tx_entries_per_subcrq";
2789 break;
2790 case REQ_RX_ADD_ENTRIES_PER_SUBCRQ:
2791 req_value = &adapter->req_rx_add_entries_per_subcrq;
2792 name = "rx_add_entries_per_subcrq";
2793 break;
2794 case REQ_MTU:
2795 req_value = &adapter->req_mtu;
2796 name = "mtu";
2797 break;
2798 case PROMISC_REQUESTED:
2799 req_value = &adapter->promisc;
2800 name = "promisc";
2801 break;
2802 default:
2803 dev_err(dev, "Got invalid cap request rsp %d\n",
2804 crq->request_capability.capability);
2805 return;
2806 }
2807
2808 switch (crq->request_capability_rsp.rc.code) {
2809 case SUCCESS:
2810 break;
2811 case PARTIALSUCCESS:
2812 dev_info(dev, "req=%lld, rsp=%ld in %s queue, retrying.\n",
2813 *req_value,
Thomas Falcon28f4d162017-02-15 10:32:11 -06002814 (long int)be64_to_cpu(crq->request_capability_rsp.
Thomas Falcon032c5e82015-12-21 11:26:06 -06002815 number), name);
Nathan Fontenotb5108882017-03-30 02:49:18 -04002816 release_sub_crqs(adapter);
Thomas Falcon28f4d162017-02-15 10:32:11 -06002817 *req_value = be64_to_cpu(crq->request_capability_rsp.number);
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002818 ibmvnic_send_req_caps(adapter, 1);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002819 return;
2820 default:
2821 dev_err(dev, "Error %d in request cap rsp\n",
2822 crq->request_capability_rsp.rc.code);
2823 return;
2824 }
2825
2826 /* Done receiving requested capabilities, query IP offload support */
Thomas Falcon901e0402017-02-15 12:17:59 -06002827 if (atomic_read(&adapter->running_cap_crqs) == 0) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06002828 union ibmvnic_crq newcrq;
2829 int buf_sz = sizeof(struct ibmvnic_query_ip_offload_buffer);
2830 struct ibmvnic_query_ip_offload_buffer *ip_offload_buf =
2831 &adapter->ip_offload_buf;
2832
Thomas Falcon249168a2017-02-15 12:18:00 -06002833 adapter->wait_capability = false;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002834 adapter->ip_offload_tok = dma_map_single(dev, ip_offload_buf,
2835 buf_sz,
2836 DMA_FROM_DEVICE);
2837
2838 if (dma_mapping_error(dev, adapter->ip_offload_tok)) {
2839 if (!firmware_has_feature(FW_FEATURE_CMO))
2840 dev_err(dev, "Couldn't map offload buffer\n");
2841 return;
2842 }
2843
2844 memset(&newcrq, 0, sizeof(newcrq));
2845 newcrq.query_ip_offload.first = IBMVNIC_CRQ_CMD;
2846 newcrq.query_ip_offload.cmd = QUERY_IP_OFFLOAD;
2847 newcrq.query_ip_offload.len = cpu_to_be32(buf_sz);
2848 newcrq.query_ip_offload.ioba =
2849 cpu_to_be32(adapter->ip_offload_tok);
2850
2851 ibmvnic_send_crq(adapter, &newcrq);
2852 }
2853}
2854
2855static int handle_login_rsp(union ibmvnic_crq *login_rsp_crq,
2856 struct ibmvnic_adapter *adapter)
2857{
2858 struct device *dev = &adapter->vdev->dev;
2859 struct ibmvnic_login_rsp_buffer *login_rsp = adapter->login_rsp_buf;
2860 struct ibmvnic_login_buffer *login = adapter->login_buf;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002861 int i;
2862
2863 dma_unmap_single(dev, adapter->login_buf_token, adapter->login_buf_sz,
2864 DMA_BIDIRECTIONAL);
2865 dma_unmap_single(dev, adapter->login_rsp_buf_token,
2866 adapter->login_rsp_buf_sz, DMA_BIDIRECTIONAL);
2867
John Allen498cd8e2016-04-06 11:49:55 -05002868 /* If the number of queues requested can't be allocated by the
2869 * server, the login response will return with code 1. We will need
2870 * to resend the login buffer with fewer queues requested.
2871 */
2872 if (login_rsp_crq->generic.rc.code) {
2873 adapter->renegotiate = true;
2874 complete(&adapter->init_done);
2875 return 0;
2876 }
2877
Thomas Falcon032c5e82015-12-21 11:26:06 -06002878 netdev_dbg(adapter->netdev, "Login Response Buffer:\n");
2879 for (i = 0; i < (adapter->login_rsp_buf_sz - 1) / 8 + 1; i++) {
2880 netdev_dbg(adapter->netdev, "%016lx\n",
2881 ((unsigned long int *)(adapter->login_rsp_buf))[i]);
2882 }
2883
2884 /* Sanity checks */
2885 if (login->num_txcomp_subcrqs != login_rsp->num_txsubm_subcrqs ||
2886 (be32_to_cpu(login->num_rxcomp_subcrqs) *
2887 adapter->req_rx_add_queues !=
2888 be32_to_cpu(login_rsp->num_rxadd_subcrqs))) {
2889 dev_err(dev, "FATAL: Inconsistent login and login rsp\n");
2890 ibmvnic_remove(adapter->vdev);
2891 return -EIO;
2892 }
2893 complete(&adapter->init_done);
2894
Thomas Falcon032c5e82015-12-21 11:26:06 -06002895 return 0;
2896}
2897
2898static void handle_request_map_rsp(union ibmvnic_crq *crq,
2899 struct ibmvnic_adapter *adapter)
2900{
2901 struct device *dev = &adapter->vdev->dev;
2902 u8 map_id = crq->request_map_rsp.map_id;
2903 int tx_subcrqs;
2904 int rx_subcrqs;
2905 long rc;
2906 int i;
2907
2908 tx_subcrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
2909 rx_subcrqs = be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
2910
2911 rc = crq->request_map_rsp.rc.code;
2912 if (rc) {
2913 dev_err(dev, "Error %ld in REQUEST_MAP_RSP\n", rc);
2914 adapter->map_id--;
2915 /* need to find and zero tx/rx_pool map_id */
2916 for (i = 0; i < tx_subcrqs; i++) {
2917 if (adapter->tx_pool[i].long_term_buff.map_id == map_id)
2918 adapter->tx_pool[i].long_term_buff.map_id = 0;
2919 }
2920 for (i = 0; i < rx_subcrqs; i++) {
2921 if (adapter->rx_pool[i].long_term_buff.map_id == map_id)
2922 adapter->rx_pool[i].long_term_buff.map_id = 0;
2923 }
2924 }
2925 complete(&adapter->fw_done);
2926}
2927
2928static void handle_request_unmap_rsp(union ibmvnic_crq *crq,
2929 struct ibmvnic_adapter *adapter)
2930{
2931 struct device *dev = &adapter->vdev->dev;
2932 long rc;
2933
2934 rc = crq->request_unmap_rsp.rc.code;
2935 if (rc)
2936 dev_err(dev, "Error %ld in REQUEST_UNMAP_RSP\n", rc);
2937}
2938
2939static void handle_query_map_rsp(union ibmvnic_crq *crq,
2940 struct ibmvnic_adapter *adapter)
2941{
2942 struct net_device *netdev = adapter->netdev;
2943 struct device *dev = &adapter->vdev->dev;
2944 long rc;
2945
2946 rc = crq->query_map_rsp.rc.code;
2947 if (rc) {
2948 dev_err(dev, "Error %ld in QUERY_MAP_RSP\n", rc);
2949 return;
2950 }
2951 netdev_dbg(netdev, "page_size = %d\ntot_pages = %d\nfree_pages = %d\n",
2952 crq->query_map_rsp.page_size, crq->query_map_rsp.tot_pages,
2953 crq->query_map_rsp.free_pages);
2954}
2955
2956static void handle_query_cap_rsp(union ibmvnic_crq *crq,
2957 struct ibmvnic_adapter *adapter)
2958{
2959 struct net_device *netdev = adapter->netdev;
2960 struct device *dev = &adapter->vdev->dev;
2961 long rc;
2962
Thomas Falcon901e0402017-02-15 12:17:59 -06002963 atomic_dec(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002964 netdev_dbg(netdev, "Outstanding queries: %d\n",
Thomas Falcon901e0402017-02-15 12:17:59 -06002965 atomic_read(&adapter->running_cap_crqs));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002966 rc = crq->query_capability.rc.code;
2967 if (rc) {
2968 dev_err(dev, "Error %ld in QUERY_CAP_RSP\n", rc);
2969 goto out;
2970 }
2971
2972 switch (be16_to_cpu(crq->query_capability.capability)) {
2973 case MIN_TX_QUEUES:
2974 adapter->min_tx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06002975 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002976 netdev_dbg(netdev, "min_tx_queues = %lld\n",
2977 adapter->min_tx_queues);
2978 break;
2979 case MIN_RX_QUEUES:
2980 adapter->min_rx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06002981 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002982 netdev_dbg(netdev, "min_rx_queues = %lld\n",
2983 adapter->min_rx_queues);
2984 break;
2985 case MIN_RX_ADD_QUEUES:
2986 adapter->min_rx_add_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06002987 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002988 netdev_dbg(netdev, "min_rx_add_queues = %lld\n",
2989 adapter->min_rx_add_queues);
2990 break;
2991 case MAX_TX_QUEUES:
2992 adapter->max_tx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06002993 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002994 netdev_dbg(netdev, "max_tx_queues = %lld\n",
2995 adapter->max_tx_queues);
2996 break;
2997 case MAX_RX_QUEUES:
2998 adapter->max_rx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06002999 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003000 netdev_dbg(netdev, "max_rx_queues = %lld\n",
3001 adapter->max_rx_queues);
3002 break;
3003 case MAX_RX_ADD_QUEUES:
3004 adapter->max_rx_add_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003005 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003006 netdev_dbg(netdev, "max_rx_add_queues = %lld\n",
3007 adapter->max_rx_add_queues);
3008 break;
3009 case MIN_TX_ENTRIES_PER_SUBCRQ:
3010 adapter->min_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06003011 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003012 netdev_dbg(netdev, "min_tx_entries_per_subcrq = %lld\n",
3013 adapter->min_tx_entries_per_subcrq);
3014 break;
3015 case MIN_RX_ADD_ENTRIES_PER_SUBCRQ:
3016 adapter->min_rx_add_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06003017 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003018 netdev_dbg(netdev, "min_rx_add_entrs_per_subcrq = %lld\n",
3019 adapter->min_rx_add_entries_per_subcrq);
3020 break;
3021 case MAX_TX_ENTRIES_PER_SUBCRQ:
3022 adapter->max_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06003023 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003024 netdev_dbg(netdev, "max_tx_entries_per_subcrq = %lld\n",
3025 adapter->max_tx_entries_per_subcrq);
3026 break;
3027 case MAX_RX_ADD_ENTRIES_PER_SUBCRQ:
3028 adapter->max_rx_add_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06003029 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003030 netdev_dbg(netdev, "max_rx_add_entrs_per_subcrq = %lld\n",
3031 adapter->max_rx_add_entries_per_subcrq);
3032 break;
3033 case TCP_IP_OFFLOAD:
3034 adapter->tcp_ip_offload =
Thomas Falconde89e852016-03-01 10:20:09 -06003035 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003036 netdev_dbg(netdev, "tcp_ip_offload = %lld\n",
3037 adapter->tcp_ip_offload);
3038 break;
3039 case PROMISC_SUPPORTED:
3040 adapter->promisc_supported =
Thomas Falconde89e852016-03-01 10:20:09 -06003041 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003042 netdev_dbg(netdev, "promisc_supported = %lld\n",
3043 adapter->promisc_supported);
3044 break;
3045 case MIN_MTU:
Thomas Falconde89e852016-03-01 10:20:09 -06003046 adapter->min_mtu = be64_to_cpu(crq->query_capability.number);
Thomas Falconf39f0d12017-02-14 10:22:59 -06003047 netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003048 netdev_dbg(netdev, "min_mtu = %lld\n", adapter->min_mtu);
3049 break;
3050 case MAX_MTU:
Thomas Falconde89e852016-03-01 10:20:09 -06003051 adapter->max_mtu = be64_to_cpu(crq->query_capability.number);
Thomas Falconf39f0d12017-02-14 10:22:59 -06003052 netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003053 netdev_dbg(netdev, "max_mtu = %lld\n", adapter->max_mtu);
3054 break;
3055 case MAX_MULTICAST_FILTERS:
3056 adapter->max_multicast_filters =
Thomas Falconde89e852016-03-01 10:20:09 -06003057 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003058 netdev_dbg(netdev, "max_multicast_filters = %lld\n",
3059 adapter->max_multicast_filters);
3060 break;
3061 case VLAN_HEADER_INSERTION:
3062 adapter->vlan_header_insertion =
Thomas Falconde89e852016-03-01 10:20:09 -06003063 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003064 if (adapter->vlan_header_insertion)
3065 netdev->features |= NETIF_F_HW_VLAN_STAG_TX;
3066 netdev_dbg(netdev, "vlan_header_insertion = %lld\n",
3067 adapter->vlan_header_insertion);
3068 break;
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04003069 case RX_VLAN_HEADER_INSERTION:
3070 adapter->rx_vlan_header_insertion =
3071 be64_to_cpu(crq->query_capability.number);
3072 netdev_dbg(netdev, "rx_vlan_header_insertion = %lld\n",
3073 adapter->rx_vlan_header_insertion);
3074 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003075 case MAX_TX_SG_ENTRIES:
3076 adapter->max_tx_sg_entries =
Thomas Falconde89e852016-03-01 10:20:09 -06003077 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003078 netdev_dbg(netdev, "max_tx_sg_entries = %lld\n",
3079 adapter->max_tx_sg_entries);
3080 break;
3081 case RX_SG_SUPPORTED:
3082 adapter->rx_sg_supported =
Thomas Falconde89e852016-03-01 10:20:09 -06003083 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003084 netdev_dbg(netdev, "rx_sg_supported = %lld\n",
3085 adapter->rx_sg_supported);
3086 break;
3087 case OPT_TX_COMP_SUB_QUEUES:
3088 adapter->opt_tx_comp_sub_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003089 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003090 netdev_dbg(netdev, "opt_tx_comp_sub_queues = %lld\n",
3091 adapter->opt_tx_comp_sub_queues);
3092 break;
3093 case OPT_RX_COMP_QUEUES:
3094 adapter->opt_rx_comp_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003095 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003096 netdev_dbg(netdev, "opt_rx_comp_queues = %lld\n",
3097 adapter->opt_rx_comp_queues);
3098 break;
3099 case OPT_RX_BUFADD_Q_PER_RX_COMP_Q:
3100 adapter->opt_rx_bufadd_q_per_rx_comp_q =
Thomas Falconde89e852016-03-01 10:20:09 -06003101 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003102 netdev_dbg(netdev, "opt_rx_bufadd_q_per_rx_comp_q = %lld\n",
3103 adapter->opt_rx_bufadd_q_per_rx_comp_q);
3104 break;
3105 case OPT_TX_ENTRIES_PER_SUBCRQ:
3106 adapter->opt_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06003107 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003108 netdev_dbg(netdev, "opt_tx_entries_per_subcrq = %lld\n",
3109 adapter->opt_tx_entries_per_subcrq);
3110 break;
3111 case OPT_RXBA_ENTRIES_PER_SUBCRQ:
3112 adapter->opt_rxba_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06003113 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003114 netdev_dbg(netdev, "opt_rxba_entries_per_subcrq = %lld\n",
3115 adapter->opt_rxba_entries_per_subcrq);
3116 break;
3117 case TX_RX_DESC_REQ:
3118 adapter->tx_rx_desc_req = crq->query_capability.number;
3119 netdev_dbg(netdev, "tx_rx_desc_req = %llx\n",
3120 adapter->tx_rx_desc_req);
3121 break;
3122
3123 default:
3124 netdev_err(netdev, "Got invalid cap rsp %d\n",
3125 crq->query_capability.capability);
3126 }
3127
3128out:
Thomas Falcon249168a2017-02-15 12:18:00 -06003129 if (atomic_read(&adapter->running_cap_crqs) == 0) {
3130 adapter->wait_capability = false;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003131 ibmvnic_send_req_caps(adapter, 0);
Thomas Falcon249168a2017-02-15 12:18:00 -06003132 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06003133}
3134
Thomas Falcon032c5e82015-12-21 11:26:06 -06003135static void ibmvnic_handle_crq(union ibmvnic_crq *crq,
3136 struct ibmvnic_adapter *adapter)
3137{
3138 struct ibmvnic_generic_crq *gen_crq = &crq->generic;
3139 struct net_device *netdev = adapter->netdev;
3140 struct device *dev = &adapter->vdev->dev;
Murilo Fossa Vicentini993a82b2017-04-19 13:44:35 -04003141 u64 *u64_crq = (u64 *)crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003142 long rc;
3143
3144 netdev_dbg(netdev, "Handling CRQ: %016lx %016lx\n",
Murilo Fossa Vicentini993a82b2017-04-19 13:44:35 -04003145 (unsigned long int)cpu_to_be64(u64_crq[0]),
3146 (unsigned long int)cpu_to_be64(u64_crq[1]));
Thomas Falcon032c5e82015-12-21 11:26:06 -06003147 switch (gen_crq->first) {
3148 case IBMVNIC_CRQ_INIT_RSP:
3149 switch (gen_crq->cmd) {
3150 case IBMVNIC_CRQ_INIT:
3151 dev_info(dev, "Partner initialized\n");
Thomas Falcon032c5e82015-12-21 11:26:06 -06003152 break;
3153 case IBMVNIC_CRQ_INIT_COMPLETE:
3154 dev_info(dev, "Partner initialization complete\n");
3155 send_version_xchg(adapter);
3156 break;
3157 default:
3158 dev_err(dev, "Unknown crq cmd: %d\n", gen_crq->cmd);
3159 }
3160 return;
3161 case IBMVNIC_CRQ_XPORT_EVENT:
Nathan Fontenoted651a12017-05-03 14:04:38 -04003162 netif_carrier_off(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003163 if (gen_crq->cmd == IBMVNIC_PARTITION_MIGRATED) {
Nathan Fontenoted651a12017-05-03 14:04:38 -04003164 dev_info(dev, "Migrated, re-enabling adapter\n");
3165 ibmvnic_reset(adapter, VNIC_RESET_MOBILITY);
Thomas Falcondfad09a2016-08-18 11:37:51 -05003166 } else if (gen_crq->cmd == IBMVNIC_DEVICE_FAILOVER) {
3167 dev_info(dev, "Backing device failover detected\n");
Nathan Fontenoted651a12017-05-03 14:04:38 -04003168 ibmvnic_reset(adapter, VNIC_RESET_FAILOVER);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003169 } else {
3170 /* The adapter lost the connection */
3171 dev_err(dev, "Virtual Adapter failed (rc=%d)\n",
3172 gen_crq->cmd);
Nathan Fontenoted651a12017-05-03 14:04:38 -04003173 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003174 }
3175 return;
3176 case IBMVNIC_CRQ_CMD_RSP:
3177 break;
3178 default:
3179 dev_err(dev, "Got an invalid msg type 0x%02x\n",
3180 gen_crq->first);
3181 return;
3182 }
3183
3184 switch (gen_crq->cmd) {
3185 case VERSION_EXCHANGE_RSP:
3186 rc = crq->version_exchange_rsp.rc.code;
3187 if (rc) {
3188 dev_err(dev, "Error %ld in VERSION_EXCHG_RSP\n", rc);
3189 break;
3190 }
3191 dev_info(dev, "Partner protocol version is %d\n",
3192 crq->version_exchange_rsp.version);
3193 if (be16_to_cpu(crq->version_exchange_rsp.version) <
3194 ibmvnic_version)
3195 ibmvnic_version =
3196 be16_to_cpu(crq->version_exchange_rsp.version);
3197 send_cap_queries(adapter);
3198 break;
3199 case QUERY_CAPABILITY_RSP:
3200 handle_query_cap_rsp(crq, adapter);
3201 break;
3202 case QUERY_MAP_RSP:
3203 handle_query_map_rsp(crq, adapter);
3204 break;
3205 case REQUEST_MAP_RSP:
3206 handle_request_map_rsp(crq, adapter);
3207 break;
3208 case REQUEST_UNMAP_RSP:
3209 handle_request_unmap_rsp(crq, adapter);
3210 break;
3211 case REQUEST_CAPABILITY_RSP:
3212 handle_request_cap_rsp(crq, adapter);
3213 break;
3214 case LOGIN_RSP:
3215 netdev_dbg(netdev, "Got Login Response\n");
3216 handle_login_rsp(crq, adapter);
3217 break;
3218 case LOGICAL_LINK_STATE_RSP:
Nathan Fontenot53da09e2017-04-21 15:39:04 -04003219 netdev_dbg(netdev,
3220 "Got Logical Link State Response, state: %d rc: %d\n",
3221 crq->logical_link_state_rsp.link_state,
3222 crq->logical_link_state_rsp.rc.code);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003223 adapter->logical_link_state =
3224 crq->logical_link_state_rsp.link_state;
Nathan Fontenot53da09e2017-04-21 15:39:04 -04003225 adapter->init_done_rc = crq->logical_link_state_rsp.rc.code;
3226 complete(&adapter->init_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003227 break;
3228 case LINK_STATE_INDICATION:
3229 netdev_dbg(netdev, "Got Logical Link State Indication\n");
3230 adapter->phys_link_state =
3231 crq->link_state_indication.phys_link_state;
3232 adapter->logical_link_state =
3233 crq->link_state_indication.logical_link_state;
3234 break;
3235 case CHANGE_MAC_ADDR_RSP:
3236 netdev_dbg(netdev, "Got MAC address change Response\n");
3237 handle_change_mac_rsp(crq, adapter);
3238 break;
3239 case ERROR_INDICATION:
3240 netdev_dbg(netdev, "Got Error Indication\n");
3241 handle_error_indication(crq, adapter);
3242 break;
3243 case REQUEST_ERROR_RSP:
3244 netdev_dbg(netdev, "Got Error Detail Response\n");
3245 handle_error_info_rsp(crq, adapter);
3246 break;
3247 case REQUEST_STATISTICS_RSP:
3248 netdev_dbg(netdev, "Got Statistics Response\n");
3249 complete(&adapter->stats_done);
3250 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003251 case QUERY_IP_OFFLOAD_RSP:
3252 netdev_dbg(netdev, "Got Query IP offload Response\n");
3253 handle_query_ip_offload_rsp(adapter);
3254 break;
3255 case MULTICAST_CTRL_RSP:
3256 netdev_dbg(netdev, "Got multicast control Response\n");
3257 break;
3258 case CONTROL_IP_OFFLOAD_RSP:
3259 netdev_dbg(netdev, "Got Control IP offload Response\n");
3260 dma_unmap_single(dev, adapter->ip_offload_ctrl_tok,
3261 sizeof(adapter->ip_offload_ctrl),
3262 DMA_TO_DEVICE);
John Allenbd0b6722017-03-17 17:13:40 -05003263 complete(&adapter->init_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003264 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003265 case COLLECT_FW_TRACE_RSP:
3266 netdev_dbg(netdev, "Got Collect firmware trace Response\n");
3267 complete(&adapter->fw_done);
3268 break;
3269 default:
3270 netdev_err(netdev, "Got an invalid cmd type 0x%02x\n",
3271 gen_crq->cmd);
3272 }
3273}
3274
3275static irqreturn_t ibmvnic_interrupt(int irq, void *instance)
3276{
3277 struct ibmvnic_adapter *adapter = instance;
Thomas Falcon6c267b32017-02-15 12:17:58 -06003278
Thomas Falcon6c267b32017-02-15 12:17:58 -06003279 tasklet_schedule(&adapter->tasklet);
Thomas Falcon6c267b32017-02-15 12:17:58 -06003280 return IRQ_HANDLED;
3281}
3282
3283static void ibmvnic_tasklet(void *data)
3284{
3285 struct ibmvnic_adapter *adapter = data;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003286 struct ibmvnic_crq_queue *queue = &adapter->crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003287 union ibmvnic_crq *crq;
3288 unsigned long flags;
3289 bool done = false;
3290
3291 spin_lock_irqsave(&queue->lock, flags);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003292 while (!done) {
3293 /* Pull all the valid messages off the CRQ */
3294 while ((crq = ibmvnic_next_crq(adapter)) != NULL) {
3295 ibmvnic_handle_crq(crq, adapter);
3296 crq->generic.first = 0;
3297 }
Brian Kinged7ecbf2017-04-19 13:44:53 -04003298
3299 /* remain in tasklet until all
3300 * capabilities responses are received
3301 */
3302 if (!adapter->wait_capability)
3303 done = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003304 }
Thomas Falcon249168a2017-02-15 12:18:00 -06003305 /* if capabilities CRQ's were sent in this tasklet, the following
3306 * tasklet must wait until all responses are received
3307 */
3308 if (atomic_read(&adapter->running_cap_crqs) != 0)
3309 adapter->wait_capability = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003310 spin_unlock_irqrestore(&queue->lock, flags);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003311}
3312
3313static int ibmvnic_reenable_crq_queue(struct ibmvnic_adapter *adapter)
3314{
3315 struct vio_dev *vdev = adapter->vdev;
3316 int rc;
3317
3318 do {
3319 rc = plpar_hcall_norets(H_ENABLE_CRQ, vdev->unit_address);
3320 } while (rc == H_IN_PROGRESS || rc == H_BUSY || H_IS_LONG_BUSY(rc));
3321
3322 if (rc)
3323 dev_err(&vdev->dev, "Error enabling adapter (rc=%d)\n", rc);
3324
3325 return rc;
3326}
3327
3328static int ibmvnic_reset_crq(struct ibmvnic_adapter *adapter)
3329{
3330 struct ibmvnic_crq_queue *crq = &adapter->crq;
3331 struct device *dev = &adapter->vdev->dev;
3332 struct vio_dev *vdev = adapter->vdev;
3333 int rc;
3334
3335 /* Close the CRQ */
3336 do {
3337 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
3338 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
3339
3340 /* Clean out the queue */
3341 memset(crq->msgs, 0, PAGE_SIZE);
3342 crq->cur = 0;
3343
3344 /* And re-open it again */
3345 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
3346 crq->msg_token, PAGE_SIZE);
3347
3348 if (rc == H_CLOSED)
3349 /* Adapter is good, but other end is not ready */
3350 dev_warn(dev, "Partner adapter not ready\n");
3351 else if (rc != 0)
3352 dev_warn(dev, "Couldn't register crq (rc=%d)\n", rc);
3353
3354 return rc;
3355}
3356
Nathan Fontenotf9928872017-03-30 02:48:54 -04003357static void release_crq_queue(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003358{
3359 struct ibmvnic_crq_queue *crq = &adapter->crq;
3360 struct vio_dev *vdev = adapter->vdev;
3361 long rc;
3362
Nathan Fontenotf9928872017-03-30 02:48:54 -04003363 if (!crq->msgs)
3364 return;
3365
Thomas Falcon032c5e82015-12-21 11:26:06 -06003366 netdev_dbg(adapter->netdev, "Releasing CRQ\n");
3367 free_irq(vdev->irq, adapter);
Thomas Falcon6c267b32017-02-15 12:17:58 -06003368 tasklet_kill(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003369 do {
3370 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
3371 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
3372
3373 dma_unmap_single(&vdev->dev, crq->msg_token, PAGE_SIZE,
3374 DMA_BIDIRECTIONAL);
3375 free_page((unsigned long)crq->msgs);
Nathan Fontenotf9928872017-03-30 02:48:54 -04003376 crq->msgs = NULL;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003377}
3378
Nathan Fontenotf9928872017-03-30 02:48:54 -04003379static int init_crq_queue(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003380{
3381 struct ibmvnic_crq_queue *crq = &adapter->crq;
3382 struct device *dev = &adapter->vdev->dev;
3383 struct vio_dev *vdev = adapter->vdev;
3384 int rc, retrc = -ENOMEM;
3385
Nathan Fontenotf9928872017-03-30 02:48:54 -04003386 if (crq->msgs)
3387 return 0;
3388
Thomas Falcon032c5e82015-12-21 11:26:06 -06003389 crq->msgs = (union ibmvnic_crq *)get_zeroed_page(GFP_KERNEL);
3390 /* Should we allocate more than one page? */
3391
3392 if (!crq->msgs)
3393 return -ENOMEM;
3394
3395 crq->size = PAGE_SIZE / sizeof(*crq->msgs);
3396 crq->msg_token = dma_map_single(dev, crq->msgs, PAGE_SIZE,
3397 DMA_BIDIRECTIONAL);
3398 if (dma_mapping_error(dev, crq->msg_token))
3399 goto map_failed;
3400
3401 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
3402 crq->msg_token, PAGE_SIZE);
3403
3404 if (rc == H_RESOURCE)
3405 /* maybe kexecing and resource is busy. try a reset */
3406 rc = ibmvnic_reset_crq(adapter);
3407 retrc = rc;
3408
3409 if (rc == H_CLOSED) {
3410 dev_warn(dev, "Partner adapter not ready\n");
3411 } else if (rc) {
3412 dev_warn(dev, "Error %d opening adapter\n", rc);
3413 goto reg_crq_failed;
3414 }
3415
3416 retrc = 0;
3417
Thomas Falcon6c267b32017-02-15 12:17:58 -06003418 tasklet_init(&adapter->tasklet, (void *)ibmvnic_tasklet,
3419 (unsigned long)adapter);
3420
Thomas Falcon032c5e82015-12-21 11:26:06 -06003421 netdev_dbg(adapter->netdev, "registering irq 0x%x\n", vdev->irq);
3422 rc = request_irq(vdev->irq, ibmvnic_interrupt, 0, IBMVNIC_NAME,
3423 adapter);
3424 if (rc) {
3425 dev_err(dev, "Couldn't register irq 0x%x. rc=%d\n",
3426 vdev->irq, rc);
3427 goto req_irq_failed;
3428 }
3429
3430 rc = vio_enable_interrupts(vdev);
3431 if (rc) {
3432 dev_err(dev, "Error %d enabling interrupts\n", rc);
3433 goto req_irq_failed;
3434 }
3435
3436 crq->cur = 0;
3437 spin_lock_init(&crq->lock);
3438
3439 return retrc;
3440
3441req_irq_failed:
Thomas Falcon6c267b32017-02-15 12:17:58 -06003442 tasklet_kill(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003443 do {
3444 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
3445 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
3446reg_crq_failed:
3447 dma_unmap_single(dev, crq->msg_token, PAGE_SIZE, DMA_BIDIRECTIONAL);
3448map_failed:
3449 free_page((unsigned long)crq->msgs);
Nathan Fontenotf9928872017-03-30 02:48:54 -04003450 crq->msgs = NULL;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003451 return retrc;
3452}
3453
John Allenf6ef6402017-03-17 17:13:42 -05003454static int ibmvnic_init(struct ibmvnic_adapter *adapter)
3455{
3456 struct device *dev = &adapter->vdev->dev;
3457 unsigned long timeout = msecs_to_jiffies(30000);
John Allenf6ef6402017-03-17 17:13:42 -05003458 int rc;
3459
Nathan Fontenotf9928872017-03-30 02:48:54 -04003460 rc = init_crq_queue(adapter);
John Allenf6ef6402017-03-17 17:13:42 -05003461 if (rc) {
3462 dev_err(dev, "Couldn't initialize crq. rc=%d\n", rc);
3463 return rc;
3464 }
3465
John Allenf6ef6402017-03-17 17:13:42 -05003466 init_completion(&adapter->init_done);
3467 ibmvnic_send_crq_init(adapter);
3468 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
3469 dev_err(dev, "Initialization sequence timed out\n");
Nathan Fontenotf9928872017-03-30 02:48:54 -04003470 release_crq_queue(adapter);
John Allenf6ef6402017-03-17 17:13:42 -05003471 return -1;
3472 }
3473
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04003474 rc = init_sub_crqs(adapter);
3475 if (rc) {
3476 dev_err(dev, "Initialization of sub crqs failed\n");
3477 release_crq_queue(adapter);
3478 }
3479
3480 return rc;
John Allenf6ef6402017-03-17 17:13:42 -05003481}
3482
Thomas Falcon032c5e82015-12-21 11:26:06 -06003483static int ibmvnic_probe(struct vio_dev *dev, const struct vio_device_id *id)
3484{
3485 struct ibmvnic_adapter *adapter;
3486 struct net_device *netdev;
3487 unsigned char *mac_addr_p;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003488 int rc;
3489
3490 dev_dbg(&dev->dev, "entering ibmvnic_probe for UA 0x%x\n",
3491 dev->unit_address);
3492
3493 mac_addr_p = (unsigned char *)vio_get_attribute(dev,
3494 VETH_MAC_ADDR, NULL);
3495 if (!mac_addr_p) {
3496 dev_err(&dev->dev,
3497 "(%s:%3.3d) ERROR: Can't find MAC_ADDR attribute\n",
3498 __FILE__, __LINE__);
3499 return 0;
3500 }
3501
3502 netdev = alloc_etherdev_mq(sizeof(struct ibmvnic_adapter),
3503 IBMVNIC_MAX_TX_QUEUES);
3504 if (!netdev)
3505 return -ENOMEM;
3506
3507 adapter = netdev_priv(netdev);
Nathan Fontenot90c80142017-05-03 14:04:32 -04003508 adapter->state = VNIC_PROBING;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003509 dev_set_drvdata(&dev->dev, netdev);
3510 adapter->vdev = dev;
3511 adapter->netdev = netdev;
3512
3513 ether_addr_copy(adapter->mac_addr, mac_addr_p);
3514 ether_addr_copy(netdev->dev_addr, adapter->mac_addr);
3515 netdev->irq = dev->irq;
3516 netdev->netdev_ops = &ibmvnic_netdev_ops;
3517 netdev->ethtool_ops = &ibmvnic_ethtool_ops;
3518 SET_NETDEV_DEV(netdev, &dev->dev);
3519
3520 spin_lock_init(&adapter->stats_lock);
3521
Thomas Falcon032c5e82015-12-21 11:26:06 -06003522 INIT_LIST_HEAD(&adapter->errors);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003523 spin_lock_init(&adapter->error_list_lock);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003524
Nathan Fontenoted651a12017-05-03 14:04:38 -04003525 INIT_WORK(&adapter->ibmvnic_reset, __ibmvnic_reset);
3526 INIT_LIST_HEAD(&adapter->rwi_list);
3527 mutex_init(&adapter->reset_lock);
3528 mutex_init(&adapter->rwi_lock);
3529 adapter->resetting = false;
3530
John Allenf6ef6402017-03-17 17:13:42 -05003531 rc = ibmvnic_init(adapter);
3532 if (rc) {
3533 free_netdev(netdev);
3534 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003535 }
3536
Thomas Falconf39f0d12017-02-14 10:22:59 -06003537 netdev->mtu = adapter->req_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003538
3539 rc = register_netdev(netdev);
3540 if (rc) {
3541 dev_err(&dev->dev, "failed to register netdev rc=%d\n", rc);
John Allenf6ef6402017-03-17 17:13:42 -05003542 free_netdev(netdev);
3543 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003544 }
3545 dev_info(&dev->dev, "ibmvnic registered\n");
3546
Nathan Fontenot90c80142017-05-03 14:04:32 -04003547 adapter->state = VNIC_PROBED;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003548 return 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003549}
3550
3551static int ibmvnic_remove(struct vio_dev *dev)
3552{
3553 struct net_device *netdev = dev_get_drvdata(&dev->dev);
Nathan Fontenot37489052017-04-19 13:45:04 -04003554 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003555
Nathan Fontenot90c80142017-05-03 14:04:32 -04003556 adapter->state = VNIC_REMOVING;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003557 unregister_netdev(netdev);
Nathan Fontenoted651a12017-05-03 14:04:38 -04003558 mutex_lock(&adapter->reset_lock);
Nathan Fontenot37489052017-04-19 13:45:04 -04003559
3560 release_resources(adapter);
3561 release_sub_crqs(adapter);
3562 release_crq_queue(adapter);
3563
Nathan Fontenot90c80142017-05-03 14:04:32 -04003564 adapter->state = VNIC_REMOVED;
3565
Nathan Fontenoted651a12017-05-03 14:04:38 -04003566 mutex_unlock(&adapter->reset_lock);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003567 free_netdev(netdev);
3568 dev_set_drvdata(&dev->dev, NULL);
3569
3570 return 0;
3571}
3572
3573static unsigned long ibmvnic_get_desired_dma(struct vio_dev *vdev)
3574{
3575 struct net_device *netdev = dev_get_drvdata(&vdev->dev);
3576 struct ibmvnic_adapter *adapter;
3577 struct iommu_table *tbl;
3578 unsigned long ret = 0;
3579 int i;
3580
3581 tbl = get_iommu_table_base(&vdev->dev);
3582
3583 /* netdev inits at probe time along with the structures we need below*/
3584 if (!netdev)
3585 return IOMMU_PAGE_ALIGN(IBMVNIC_IO_ENTITLEMENT_DEFAULT, tbl);
3586
3587 adapter = netdev_priv(netdev);
3588
3589 ret += PAGE_SIZE; /* the crq message queue */
Thomas Falcon032c5e82015-12-21 11:26:06 -06003590 ret += IOMMU_PAGE_ALIGN(sizeof(struct ibmvnic_statistics), tbl);
3591
3592 for (i = 0; i < adapter->req_tx_queues + adapter->req_rx_queues; i++)
3593 ret += 4 * PAGE_SIZE; /* the scrq message queue */
3594
3595 for (i = 0; i < be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
3596 i++)
3597 ret += adapter->rx_pool[i].size *
3598 IOMMU_PAGE_ALIGN(adapter->rx_pool[i].buff_size, tbl);
3599
3600 return ret;
3601}
3602
3603static int ibmvnic_resume(struct device *dev)
3604{
3605 struct net_device *netdev = dev_get_drvdata(dev);
3606 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
3607 int i;
3608
3609 /* kick the interrupt handlers just in case we lost an interrupt */
3610 for (i = 0; i < adapter->req_rx_queues; i++)
3611 ibmvnic_interrupt_rx(adapter->rx_scrq[i]->irq,
3612 adapter->rx_scrq[i]);
3613
3614 return 0;
3615}
3616
3617static struct vio_device_id ibmvnic_device_table[] = {
3618 {"network", "IBM,vnic"},
3619 {"", "" }
3620};
3621MODULE_DEVICE_TABLE(vio, ibmvnic_device_table);
3622
3623static const struct dev_pm_ops ibmvnic_pm_ops = {
3624 .resume = ibmvnic_resume
3625};
3626
3627static struct vio_driver ibmvnic_driver = {
3628 .id_table = ibmvnic_device_table,
3629 .probe = ibmvnic_probe,
3630 .remove = ibmvnic_remove,
3631 .get_desired_dma = ibmvnic_get_desired_dma,
3632 .name = ibmvnic_driver_name,
3633 .pm = &ibmvnic_pm_ops,
3634};
3635
3636/* module functions */
3637static int __init ibmvnic_module_init(void)
3638{
3639 pr_info("%s: %s %s\n", ibmvnic_driver_name, ibmvnic_driver_string,
3640 IBMVNIC_DRIVER_VERSION);
3641
3642 return vio_register_driver(&ibmvnic_driver);
3643}
3644
3645static void __exit ibmvnic_module_exit(void)
3646{
3647 vio_unregister_driver(&ibmvnic_driver);
3648}
3649
3650module_init(ibmvnic_module_init);
3651module_exit(ibmvnic_module_exit);