blob: 3d0280196fdc5ce6cd327eb52a18f54bff9f10ce [file] [log] [blame]
Thomas Falcon032c5e82015-12-21 11:26:06 -06001/**************************************************************************/
2/* */
3/* IBM System i and System p Virtual NIC Device Driver */
4/* Copyright (C) 2014 IBM Corp. */
5/* Santiago Leon (santi_leon@yahoo.com) */
6/* Thomas Falcon (tlfalcon@linux.vnet.ibm.com) */
7/* John Allen (jallen@linux.vnet.ibm.com) */
8/* */
9/* This program is free software; you can redistribute it and/or modify */
10/* it under the terms of the GNU General Public License as published by */
11/* the Free Software Foundation; either version 2 of the License, or */
12/* (at your option) any later version. */
13/* */
14/* This program is distributed in the hope that it will be useful, */
15/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
16/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
17/* GNU General Public License for more details. */
18/* */
19/* You should have received a copy of the GNU General Public License */
20/* along with this program. */
21/* */
22/* This module contains the implementation of a virtual ethernet device */
23/* for use with IBM i/p Series LPAR Linux. It utilizes the logical LAN */
24/* option of the RS/6000 Platform Architecture to interface with virtual */
25/* ethernet NICs that are presented to the partition by the hypervisor. */
26/* */
27/* Messages are passed between the VNIC driver and the VNIC server using */
28/* Command/Response Queues (CRQs) and sub CRQs (sCRQs). CRQs are used to */
29/* issue and receive commands that initiate communication with the server */
30/* on driver initialization. Sub CRQs (sCRQs) are similar to CRQs, but */
31/* are used by the driver to notify the server that a packet is */
32/* ready for transmission or that a buffer has been added to receive a */
33/* packet. Subsequently, sCRQs are used by the server to notify the */
34/* driver that a packet transmission has been completed or that a packet */
35/* has been received and placed in a waiting buffer. */
36/* */
37/* In lieu of a more conventional "on-the-fly" DMA mapping strategy in */
38/* which skbs are DMA mapped and immediately unmapped when the transmit */
39/* or receive has been completed, the VNIC driver is required to use */
40/* "long term mapping". This entails that large, continuous DMA mapped */
41/* buffers are allocated on driver initialization and these buffers are */
42/* then continuously reused to pass skbs to and from the VNIC server. */
43/* */
44/**************************************************************************/
45
46#include <linux/module.h>
47#include <linux/moduleparam.h>
48#include <linux/types.h>
49#include <linux/errno.h>
50#include <linux/completion.h>
51#include <linux/ioport.h>
52#include <linux/dma-mapping.h>
53#include <linux/kernel.h>
54#include <linux/netdevice.h>
55#include <linux/etherdevice.h>
56#include <linux/skbuff.h>
57#include <linux/init.h>
58#include <linux/delay.h>
59#include <linux/mm.h>
60#include <linux/ethtool.h>
61#include <linux/proc_fs.h>
62#include <linux/in.h>
63#include <linux/ip.h>
Thomas Falconad7775d2016-04-01 17:20:34 -050064#include <linux/ipv6.h>
Thomas Falcon032c5e82015-12-21 11:26:06 -060065#include <linux/irq.h>
66#include <linux/kthread.h>
67#include <linux/seq_file.h>
Thomas Falcon032c5e82015-12-21 11:26:06 -060068#include <linux/interrupt.h>
69#include <net/net_namespace.h>
70#include <asm/hvcall.h>
71#include <linux/atomic.h>
72#include <asm/vio.h>
73#include <asm/iommu.h>
74#include <linux/uaccess.h>
75#include <asm/firmware.h>
Thomas Falcon65dc6892016-07-06 15:35:18 -050076#include <linux/workqueue.h>
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -040077#include <linux/if_vlan.h>
Thomas Falcon032c5e82015-12-21 11:26:06 -060078
79#include "ibmvnic.h"
80
81static const char ibmvnic_driver_name[] = "ibmvnic";
82static const char ibmvnic_driver_string[] = "IBM System i/p Virtual NIC Driver";
83
Thomas Falcon78b07ac2017-06-01 15:32:34 -050084MODULE_AUTHOR("Santiago Leon");
Thomas Falcon032c5e82015-12-21 11:26:06 -060085MODULE_DESCRIPTION("IBM System i/p Virtual NIC Driver");
86MODULE_LICENSE("GPL");
87MODULE_VERSION(IBMVNIC_DRIVER_VERSION);
88
89static int ibmvnic_version = IBMVNIC_INITIAL_VERSION;
90static int ibmvnic_remove(struct vio_dev *);
91static void release_sub_crqs(struct ibmvnic_adapter *);
92static int ibmvnic_reset_crq(struct ibmvnic_adapter *);
93static int ibmvnic_send_crq_init(struct ibmvnic_adapter *);
94static int ibmvnic_reenable_crq_queue(struct ibmvnic_adapter *);
95static int ibmvnic_send_crq(struct ibmvnic_adapter *, union ibmvnic_crq *);
96static int send_subcrq(struct ibmvnic_adapter *adapter, u64 remote_handle,
97 union sub_crq *sub_crq);
Thomas Falconad7775d2016-04-01 17:20:34 -050098static int send_subcrq_indirect(struct ibmvnic_adapter *, u64, u64, u64);
Thomas Falcon032c5e82015-12-21 11:26:06 -060099static irqreturn_t ibmvnic_interrupt_rx(int irq, void *instance);
100static int enable_scrq_irq(struct ibmvnic_adapter *,
101 struct ibmvnic_sub_crq_queue *);
102static int disable_scrq_irq(struct ibmvnic_adapter *,
103 struct ibmvnic_sub_crq_queue *);
104static int pending_scrq(struct ibmvnic_adapter *,
105 struct ibmvnic_sub_crq_queue *);
106static union sub_crq *ibmvnic_next_scrq(struct ibmvnic_adapter *,
107 struct ibmvnic_sub_crq_queue *);
108static int ibmvnic_poll(struct napi_struct *napi, int data);
109static void send_map_query(struct ibmvnic_adapter *adapter);
110static void send_request_map(struct ibmvnic_adapter *, dma_addr_t, __be32, u8);
111static void send_request_unmap(struct ibmvnic_adapter *, u8);
John Allenbd0b6722017-03-17 17:13:40 -0500112static void send_login(struct ibmvnic_adapter *adapter);
113static void send_cap_queries(struct ibmvnic_adapter *adapter);
Thomas Falcon4d96f122017-08-01 15:04:36 -0500114static int init_sub_crqs(struct ibmvnic_adapter *);
John Allenbd0b6722017-03-17 17:13:40 -0500115static int init_sub_crq_irqs(struct ibmvnic_adapter *adapter);
John Allenea5509f2017-03-17 17:13:43 -0500116static int ibmvnic_init(struct ibmvnic_adapter *);
Nathan Fontenotf9928872017-03-30 02:48:54 -0400117static void release_crq_queue(struct ibmvnic_adapter *);
John Allenc26eba02017-10-26 16:23:25 -0500118static int __ibmvnic_set_mac(struct net_device *netdev, struct sockaddr *p);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600119
120struct ibmvnic_stat {
121 char name[ETH_GSTRING_LEN];
122 int offset;
123};
124
125#define IBMVNIC_STAT_OFF(stat) (offsetof(struct ibmvnic_adapter, stats) + \
126 offsetof(struct ibmvnic_statistics, stat))
127#define IBMVNIC_GET_STAT(a, off) (*((u64 *)(((unsigned long)(a)) + off)))
128
129static const struct ibmvnic_stat ibmvnic_stats[] = {
130 {"rx_packets", IBMVNIC_STAT_OFF(rx_packets)},
131 {"rx_bytes", IBMVNIC_STAT_OFF(rx_bytes)},
132 {"tx_packets", IBMVNIC_STAT_OFF(tx_packets)},
133 {"tx_bytes", IBMVNIC_STAT_OFF(tx_bytes)},
134 {"ucast_tx_packets", IBMVNIC_STAT_OFF(ucast_tx_packets)},
135 {"ucast_rx_packets", IBMVNIC_STAT_OFF(ucast_rx_packets)},
136 {"mcast_tx_packets", IBMVNIC_STAT_OFF(mcast_tx_packets)},
137 {"mcast_rx_packets", IBMVNIC_STAT_OFF(mcast_rx_packets)},
138 {"bcast_tx_packets", IBMVNIC_STAT_OFF(bcast_tx_packets)},
139 {"bcast_rx_packets", IBMVNIC_STAT_OFF(bcast_rx_packets)},
140 {"align_errors", IBMVNIC_STAT_OFF(align_errors)},
141 {"fcs_errors", IBMVNIC_STAT_OFF(fcs_errors)},
142 {"single_collision_frames", IBMVNIC_STAT_OFF(single_collision_frames)},
143 {"multi_collision_frames", IBMVNIC_STAT_OFF(multi_collision_frames)},
144 {"sqe_test_errors", IBMVNIC_STAT_OFF(sqe_test_errors)},
145 {"deferred_tx", IBMVNIC_STAT_OFF(deferred_tx)},
146 {"late_collisions", IBMVNIC_STAT_OFF(late_collisions)},
147 {"excess_collisions", IBMVNIC_STAT_OFF(excess_collisions)},
148 {"internal_mac_tx_errors", IBMVNIC_STAT_OFF(internal_mac_tx_errors)},
149 {"carrier_sense", IBMVNIC_STAT_OFF(carrier_sense)},
150 {"too_long_frames", IBMVNIC_STAT_OFF(too_long_frames)},
151 {"internal_mac_rx_errors", IBMVNIC_STAT_OFF(internal_mac_rx_errors)},
152};
153
154static long h_reg_sub_crq(unsigned long unit_address, unsigned long token,
155 unsigned long length, unsigned long *number,
156 unsigned long *irq)
157{
158 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
159 long rc;
160
161 rc = plpar_hcall(H_REG_SUB_CRQ, retbuf, unit_address, token, length);
162 *number = retbuf[0];
163 *irq = retbuf[1];
164
165 return rc;
166}
167
Thomas Falcon032c5e82015-12-21 11:26:06 -0600168static int alloc_long_term_buff(struct ibmvnic_adapter *adapter,
169 struct ibmvnic_long_term_buff *ltb, int size)
170{
171 struct device *dev = &adapter->vdev->dev;
172
173 ltb->size = size;
174 ltb->buff = dma_alloc_coherent(dev, ltb->size, &ltb->addr,
175 GFP_KERNEL);
176
177 if (!ltb->buff) {
178 dev_err(dev, "Couldn't alloc long term buffer\n");
179 return -ENOMEM;
180 }
181 ltb->map_id = adapter->map_id;
182 adapter->map_id++;
Nathan Fontenotdb5d0b52017-02-10 13:45:05 -0500183
184 init_completion(&adapter->fw_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600185 send_request_map(adapter, ltb->addr,
186 ltb->size, ltb->map_id);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600187 wait_for_completion(&adapter->fw_done);
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500188
189 if (adapter->fw_done_rc) {
190 dev_err(dev, "Couldn't map long term buffer,rc = %d\n",
191 adapter->fw_done_rc);
192 return -1;
193 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600194 return 0;
195}
196
197static void free_long_term_buff(struct ibmvnic_adapter *adapter,
198 struct ibmvnic_long_term_buff *ltb)
199{
200 struct device *dev = &adapter->vdev->dev;
201
Nathan Fontenotc657e322017-03-30 02:49:06 -0400202 if (!ltb->buff)
203 return;
204
Nathan Fontenoted651a12017-05-03 14:04:38 -0400205 if (adapter->reset_reason != VNIC_RESET_FAILOVER &&
206 adapter->reset_reason != VNIC_RESET_MOBILITY)
Thomas Falcondfad09a2016-08-18 11:37:51 -0500207 send_request_unmap(adapter, ltb->map_id);
Brian King59af56c2017-04-19 13:44:41 -0400208 dma_free_coherent(dev, ltb->size, ltb->buff, ltb->addr);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600209}
210
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500211static int reset_long_term_buff(struct ibmvnic_adapter *adapter,
212 struct ibmvnic_long_term_buff *ltb)
213{
214 memset(ltb->buff, 0, ltb->size);
215
216 init_completion(&adapter->fw_done);
217 send_request_map(adapter, ltb->addr, ltb->size, ltb->map_id);
218 wait_for_completion(&adapter->fw_done);
219
220 if (adapter->fw_done_rc) {
221 dev_info(&adapter->vdev->dev,
222 "Reset failed, attempting to free and reallocate buffer\n");
223 free_long_term_buff(adapter, ltb);
224 return alloc_long_term_buff(adapter, ltb, ltb->size);
225 }
226 return 0;
227}
228
Thomas Falconf185a492017-05-26 10:30:48 -0400229static void deactivate_rx_pools(struct ibmvnic_adapter *adapter)
230{
231 int i;
232
233 for (i = 0; i < be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
234 i++)
235 adapter->rx_pool[i].active = 0;
236}
237
Thomas Falcon032c5e82015-12-21 11:26:06 -0600238static void replenish_rx_pool(struct ibmvnic_adapter *adapter,
239 struct ibmvnic_rx_pool *pool)
240{
241 int count = pool->size - atomic_read(&pool->available);
242 struct device *dev = &adapter->vdev->dev;
243 int buffers_added = 0;
244 unsigned long lpar_rc;
245 union sub_crq sub_crq;
246 struct sk_buff *skb;
247 unsigned int offset;
248 dma_addr_t dma_addr;
249 unsigned char *dst;
250 u64 *handle_array;
251 int shift = 0;
252 int index;
253 int i;
254
Thomas Falconf185a492017-05-26 10:30:48 -0400255 if (!pool->active)
256 return;
257
Thomas Falcon032c5e82015-12-21 11:26:06 -0600258 handle_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
259 be32_to_cpu(adapter->login_rsp_buf->
260 off_rxadd_subcrqs));
261
262 for (i = 0; i < count; ++i) {
263 skb = alloc_skb(pool->buff_size, GFP_ATOMIC);
264 if (!skb) {
265 dev_err(dev, "Couldn't replenish rx buff\n");
266 adapter->replenish_no_mem++;
267 break;
268 }
269
270 index = pool->free_map[pool->next_free];
271
272 if (pool->rx_buff[index].skb)
273 dev_err(dev, "Inconsistent free_map!\n");
274
275 /* Copy the skb to the long term mapped DMA buffer */
276 offset = index * pool->buff_size;
277 dst = pool->long_term_buff.buff + offset;
278 memset(dst, 0, pool->buff_size);
279 dma_addr = pool->long_term_buff.addr + offset;
280 pool->rx_buff[index].data = dst;
281
282 pool->free_map[pool->next_free] = IBMVNIC_INVALID_MAP;
283 pool->rx_buff[index].dma = dma_addr;
284 pool->rx_buff[index].skb = skb;
285 pool->rx_buff[index].pool_index = pool->index;
286 pool->rx_buff[index].size = pool->buff_size;
287
288 memset(&sub_crq, 0, sizeof(sub_crq));
289 sub_crq.rx_add.first = IBMVNIC_CRQ_CMD;
290 sub_crq.rx_add.correlator =
291 cpu_to_be64((u64)&pool->rx_buff[index]);
292 sub_crq.rx_add.ioba = cpu_to_be32(dma_addr);
293 sub_crq.rx_add.map_id = pool->long_term_buff.map_id;
294
295 /* The length field of the sCRQ is defined to be 24 bits so the
296 * buffer size needs to be left shifted by a byte before it is
297 * converted to big endian to prevent the last byte from being
298 * truncated.
299 */
300#ifdef __LITTLE_ENDIAN__
301 shift = 8;
302#endif
303 sub_crq.rx_add.len = cpu_to_be32(pool->buff_size << shift);
304
305 lpar_rc = send_subcrq(adapter, handle_array[pool->index],
306 &sub_crq);
307 if (lpar_rc != H_SUCCESS)
308 goto failure;
309
310 buffers_added++;
311 adapter->replenish_add_buff_success++;
312 pool->next_free = (pool->next_free + 1) % pool->size;
313 }
314 atomic_add(buffers_added, &pool->available);
315 return;
316
317failure:
318 dev_info(dev, "replenish pools failure\n");
319 pool->free_map[pool->next_free] = index;
320 pool->rx_buff[index].skb = NULL;
321 if (!dma_mapping_error(dev, dma_addr))
322 dma_unmap_single(dev, dma_addr, pool->buff_size,
323 DMA_FROM_DEVICE);
324
325 dev_kfree_skb_any(skb);
326 adapter->replenish_add_buff_failure++;
327 atomic_add(buffers_added, &pool->available);
Thomas Falconf185a492017-05-26 10:30:48 -0400328
329 if (lpar_rc == H_CLOSED) {
330 /* Disable buffer pool replenishment and report carrier off if
331 * queue is closed. Firmware guarantees that a signal will
332 * be sent to the driver, triggering a reset.
333 */
334 deactivate_rx_pools(adapter);
335 netif_carrier_off(adapter->netdev);
336 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600337}
338
339static void replenish_pools(struct ibmvnic_adapter *adapter)
340{
341 int i;
342
Thomas Falcon032c5e82015-12-21 11:26:06 -0600343 adapter->replenish_task_cycles++;
344 for (i = 0; i < be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
345 i++) {
346 if (adapter->rx_pool[i].active)
347 replenish_rx_pool(adapter, &adapter->rx_pool[i]);
348 }
349}
350
John Allen3d52b592017-08-02 16:44:14 -0500351static void release_stats_buffers(struct ibmvnic_adapter *adapter)
352{
353 kfree(adapter->tx_stats_buffers);
354 kfree(adapter->rx_stats_buffers);
355}
356
357static int init_stats_buffers(struct ibmvnic_adapter *adapter)
358{
359 adapter->tx_stats_buffers =
360 kcalloc(adapter->req_tx_queues,
361 sizeof(struct ibmvnic_tx_queue_stats),
362 GFP_KERNEL);
363 if (!adapter->tx_stats_buffers)
364 return -ENOMEM;
365
366 adapter->rx_stats_buffers =
367 kcalloc(adapter->req_rx_queues,
368 sizeof(struct ibmvnic_rx_queue_stats),
369 GFP_KERNEL);
370 if (!adapter->rx_stats_buffers)
371 return -ENOMEM;
372
373 return 0;
374}
375
Nathan Fontenot7bbc27a2017-03-30 02:49:23 -0400376static void release_stats_token(struct ibmvnic_adapter *adapter)
377{
378 struct device *dev = &adapter->vdev->dev;
379
380 if (!adapter->stats_token)
381 return;
382
383 dma_unmap_single(dev, adapter->stats_token,
384 sizeof(struct ibmvnic_statistics),
385 DMA_FROM_DEVICE);
386 adapter->stats_token = 0;
387}
388
389static int init_stats_token(struct ibmvnic_adapter *adapter)
390{
391 struct device *dev = &adapter->vdev->dev;
392 dma_addr_t stok;
393
394 stok = dma_map_single(dev, &adapter->stats,
395 sizeof(struct ibmvnic_statistics),
396 DMA_FROM_DEVICE);
397 if (dma_mapping_error(dev, stok)) {
398 dev_err(dev, "Couldn't map stats buffer\n");
399 return -1;
400 }
401
402 adapter->stats_token = stok;
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500403 netdev_dbg(adapter->netdev, "Stats token initialized (%llx)\n", stok);
Nathan Fontenot7bbc27a2017-03-30 02:49:23 -0400404 return 0;
405}
406
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400407static int reset_rx_pools(struct ibmvnic_adapter *adapter)
408{
409 struct ibmvnic_rx_pool *rx_pool;
410 int rx_scrqs;
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500411 int i, j, rc;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400412
413 rx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
414 for (i = 0; i < rx_scrqs; i++) {
415 rx_pool = &adapter->rx_pool[i];
416
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500417 netdev_dbg(adapter->netdev, "Re-setting rx_pool[%d]\n", i);
418
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500419 rc = reset_long_term_buff(adapter, &rx_pool->long_term_buff);
420 if (rc)
421 return rc;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400422
423 for (j = 0; j < rx_pool->size; j++)
424 rx_pool->free_map[j] = j;
425
426 memset(rx_pool->rx_buff, 0,
427 rx_pool->size * sizeof(struct ibmvnic_rx_buff));
428
429 atomic_set(&rx_pool->available, 0);
430 rx_pool->next_alloc = 0;
431 rx_pool->next_free = 0;
Thomas Falconc3e53b92017-06-14 23:50:05 -0500432 rx_pool->active = 1;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400433 }
434
435 return 0;
436}
437
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400438static void release_rx_pools(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -0600439{
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400440 struct ibmvnic_rx_pool *rx_pool;
441 int rx_scrqs;
442 int i, j;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600443
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400444 if (!adapter->rx_pool)
Thomas Falcon032c5e82015-12-21 11:26:06 -0600445 return;
446
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400447 rx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
448 for (i = 0; i < rx_scrqs; i++) {
449 rx_pool = &adapter->rx_pool[i];
450
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500451 netdev_dbg(adapter->netdev, "Releasing rx_pool[%d]\n", i);
452
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400453 kfree(rx_pool->free_map);
454 free_long_term_buff(adapter, &rx_pool->long_term_buff);
455
456 if (!rx_pool->rx_buff)
Nathan Fontenote0ebe9422017-05-03 14:04:50 -0400457 continue;
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400458
459 for (j = 0; j < rx_pool->size; j++) {
460 if (rx_pool->rx_buff[j].skb) {
461 dev_kfree_skb_any(rx_pool->rx_buff[i].skb);
462 rx_pool->rx_buff[i].skb = NULL;
463 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600464 }
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400465
466 kfree(rx_pool->rx_buff);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600467 }
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400468
469 kfree(adapter->rx_pool);
470 adapter->rx_pool = NULL;
471}
472
473static int init_rx_pools(struct net_device *netdev)
474{
475 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
476 struct device *dev = &adapter->vdev->dev;
477 struct ibmvnic_rx_pool *rx_pool;
478 int rxadd_subcrqs;
479 u64 *size_array;
480 int i, j;
481
482 rxadd_subcrqs =
483 be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
484 size_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
485 be32_to_cpu(adapter->login_rsp_buf->off_rxadd_buff_size));
486
487 adapter->rx_pool = kcalloc(rxadd_subcrqs,
488 sizeof(struct ibmvnic_rx_pool),
489 GFP_KERNEL);
490 if (!adapter->rx_pool) {
491 dev_err(dev, "Failed to allocate rx pools\n");
492 return -1;
493 }
494
495 for (i = 0; i < rxadd_subcrqs; i++) {
496 rx_pool = &adapter->rx_pool[i];
497
498 netdev_dbg(adapter->netdev,
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500499 "Initializing rx_pool[%d], %lld buffs, %lld bytes each\n",
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400500 i, adapter->req_rx_add_entries_per_subcrq,
501 be64_to_cpu(size_array[i]));
502
503 rx_pool->size = adapter->req_rx_add_entries_per_subcrq;
504 rx_pool->index = i;
505 rx_pool->buff_size = be64_to_cpu(size_array[i]);
506 rx_pool->active = 1;
507
508 rx_pool->free_map = kcalloc(rx_pool->size, sizeof(int),
509 GFP_KERNEL);
510 if (!rx_pool->free_map) {
511 release_rx_pools(adapter);
512 return -1;
513 }
514
515 rx_pool->rx_buff = kcalloc(rx_pool->size,
516 sizeof(struct ibmvnic_rx_buff),
517 GFP_KERNEL);
518 if (!rx_pool->rx_buff) {
519 dev_err(dev, "Couldn't alloc rx buffers\n");
520 release_rx_pools(adapter);
521 return -1;
522 }
523
524 if (alloc_long_term_buff(adapter, &rx_pool->long_term_buff,
525 rx_pool->size * rx_pool->buff_size)) {
526 release_rx_pools(adapter);
527 return -1;
528 }
529
530 for (j = 0; j < rx_pool->size; ++j)
531 rx_pool->free_map[j] = j;
532
533 atomic_set(&rx_pool->available, 0);
534 rx_pool->next_alloc = 0;
535 rx_pool->next_free = 0;
536 }
537
538 return 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600539}
540
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400541static int reset_tx_pools(struct ibmvnic_adapter *adapter)
542{
543 struct ibmvnic_tx_pool *tx_pool;
544 int tx_scrqs;
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500545 int i, j, rc;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400546
547 tx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
548 for (i = 0; i < tx_scrqs; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500549 netdev_dbg(adapter->netdev, "Re-setting tx_pool[%d]\n", i);
550
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400551 tx_pool = &adapter->tx_pool[i];
552
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500553 rc = reset_long_term_buff(adapter, &tx_pool->long_term_buff);
554 if (rc)
555 return rc;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400556
Thomas Falconfdb06102017-10-17 12:36:55 -0500557 rc = reset_long_term_buff(adapter, &tx_pool->tso_ltb);
558 if (rc)
559 return rc;
560
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400561 memset(tx_pool->tx_buff, 0,
562 adapter->req_tx_entries_per_subcrq *
563 sizeof(struct ibmvnic_tx_buff));
564
565 for (j = 0; j < adapter->req_tx_entries_per_subcrq; j++)
566 tx_pool->free_map[j] = j;
567
568 tx_pool->consumer_index = 0;
569 tx_pool->producer_index = 0;
Thomas Falconfdb06102017-10-17 12:36:55 -0500570 tx_pool->tso_index = 0;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400571 }
572
573 return 0;
574}
575
Nathan Fontenotc657e322017-03-30 02:49:06 -0400576static void release_tx_pools(struct ibmvnic_adapter *adapter)
577{
578 struct ibmvnic_tx_pool *tx_pool;
579 int i, tx_scrqs;
580
581 if (!adapter->tx_pool)
582 return;
583
584 tx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
585 for (i = 0; i < tx_scrqs; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500586 netdev_dbg(adapter->netdev, "Releasing tx_pool[%d]\n", i);
Nathan Fontenotc657e322017-03-30 02:49:06 -0400587 tx_pool = &adapter->tx_pool[i];
588 kfree(tx_pool->tx_buff);
589 free_long_term_buff(adapter, &tx_pool->long_term_buff);
Thomas Falconfdb06102017-10-17 12:36:55 -0500590 free_long_term_buff(adapter, &tx_pool->tso_ltb);
Nathan Fontenotc657e322017-03-30 02:49:06 -0400591 kfree(tx_pool->free_map);
592 }
593
594 kfree(adapter->tx_pool);
595 adapter->tx_pool = NULL;
596}
597
598static int init_tx_pools(struct net_device *netdev)
599{
600 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
601 struct device *dev = &adapter->vdev->dev;
602 struct ibmvnic_tx_pool *tx_pool;
603 int tx_subcrqs;
604 int i, j;
605
606 tx_subcrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
607 adapter->tx_pool = kcalloc(tx_subcrqs,
608 sizeof(struct ibmvnic_tx_pool), GFP_KERNEL);
609 if (!adapter->tx_pool)
610 return -1;
611
612 for (i = 0; i < tx_subcrqs; i++) {
613 tx_pool = &adapter->tx_pool[i];
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500614
615 netdev_dbg(adapter->netdev,
616 "Initializing tx_pool[%d], %lld buffs\n",
617 i, adapter->req_tx_entries_per_subcrq);
618
Nathan Fontenotc657e322017-03-30 02:49:06 -0400619 tx_pool->tx_buff = kcalloc(adapter->req_tx_entries_per_subcrq,
620 sizeof(struct ibmvnic_tx_buff),
621 GFP_KERNEL);
622 if (!tx_pool->tx_buff) {
623 dev_err(dev, "tx pool buffer allocation failed\n");
624 release_tx_pools(adapter);
625 return -1;
626 }
627
628 if (alloc_long_term_buff(adapter, &tx_pool->long_term_buff,
629 adapter->req_tx_entries_per_subcrq *
630 adapter->req_mtu)) {
631 release_tx_pools(adapter);
632 return -1;
633 }
634
Thomas Falconfdb06102017-10-17 12:36:55 -0500635 /* alloc TSO ltb */
636 if (alloc_long_term_buff(adapter, &tx_pool->tso_ltb,
637 IBMVNIC_TSO_BUFS *
638 IBMVNIC_TSO_BUF_SZ)) {
639 release_tx_pools(adapter);
640 return -1;
641 }
642
643 tx_pool->tso_index = 0;
644
Nathan Fontenotc657e322017-03-30 02:49:06 -0400645 tx_pool->free_map = kcalloc(adapter->req_tx_entries_per_subcrq,
646 sizeof(int), GFP_KERNEL);
647 if (!tx_pool->free_map) {
648 release_tx_pools(adapter);
649 return -1;
650 }
651
652 for (j = 0; j < adapter->req_tx_entries_per_subcrq; j++)
653 tx_pool->free_map[j] = j;
654
655 tx_pool->consumer_index = 0;
656 tx_pool->producer_index = 0;
657 }
658
659 return 0;
660}
661
Nathan Fontenot661a2622017-04-19 13:44:58 -0400662static void release_error_buffers(struct ibmvnic_adapter *adapter)
663{
664 struct device *dev = &adapter->vdev->dev;
665 struct ibmvnic_error_buff *error_buff, *tmp;
666 unsigned long flags;
667
668 spin_lock_irqsave(&adapter->error_list_lock, flags);
669 list_for_each_entry_safe(error_buff, tmp, &adapter->errors, list) {
670 list_del(&error_buff->list);
671 dma_unmap_single(dev, error_buff->dma, error_buff->len,
672 DMA_FROM_DEVICE);
673 kfree(error_buff->buff);
674 kfree(error_buff);
675 }
676 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
677}
678
John Allend944c3d62017-05-26 10:30:13 -0400679static void ibmvnic_napi_enable(struct ibmvnic_adapter *adapter)
680{
681 int i;
682
683 if (adapter->napi_enabled)
684 return;
685
686 for (i = 0; i < adapter->req_rx_queues; i++)
687 napi_enable(&adapter->napi[i]);
688
689 adapter->napi_enabled = true;
690}
691
692static void ibmvnic_napi_disable(struct ibmvnic_adapter *adapter)
693{
694 int i;
695
696 if (!adapter->napi_enabled)
697 return;
698
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500699 for (i = 0; i < adapter->req_rx_queues; i++) {
700 netdev_dbg(adapter->netdev, "Disabling napi[%d]\n", i);
John Allend944c3d62017-05-26 10:30:13 -0400701 napi_disable(&adapter->napi[i]);
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500702 }
John Allend944c3d62017-05-26 10:30:13 -0400703
704 adapter->napi_enabled = false;
705}
706
John Allena57a5d22017-03-17 17:13:41 -0500707static int ibmvnic_login(struct net_device *netdev)
Thomas Falcon032c5e82015-12-21 11:26:06 -0600708{
709 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
John Allenbd0b6722017-03-17 17:13:40 -0500710 unsigned long timeout = msecs_to_jiffies(30000);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600711 struct device *dev = &adapter->vdev->dev;
Thomas Falcon4d96f122017-08-01 15:04:36 -0500712 int rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600713
John Allenbd0b6722017-03-17 17:13:40 -0500714 do {
715 if (adapter->renegotiate) {
716 adapter->renegotiate = false;
Nathan Fontenotb5108882017-03-30 02:49:18 -0400717 release_sub_crqs(adapter);
John Allenbd0b6722017-03-17 17:13:40 -0500718
719 reinit_completion(&adapter->init_done);
720 send_cap_queries(adapter);
721 if (!wait_for_completion_timeout(&adapter->init_done,
722 timeout)) {
723 dev_err(dev, "Capabilities query timeout\n");
724 return -1;
725 }
Thomas Falcon4d96f122017-08-01 15:04:36 -0500726 rc = init_sub_crqs(adapter);
727 if (rc) {
728 dev_err(dev,
729 "Initialization of SCRQ's failed\n");
730 return -1;
731 }
732 rc = init_sub_crq_irqs(adapter);
733 if (rc) {
734 dev_err(dev,
735 "Initialization of SCRQ's irqs failed\n");
736 return -1;
737 }
John Allenbd0b6722017-03-17 17:13:40 -0500738 }
739
740 reinit_completion(&adapter->init_done);
741 send_login(adapter);
742 if (!wait_for_completion_timeout(&adapter->init_done,
743 timeout)) {
744 dev_err(dev, "Login timeout\n");
745 return -1;
746 }
747 } while (adapter->renegotiate);
748
John Allena57a5d22017-03-17 17:13:41 -0500749 return 0;
750}
751
Nathan Fontenot1b8955e2017-03-30 02:49:29 -0400752static void release_resources(struct ibmvnic_adapter *adapter)
753{
Nathan Fontenotc7bac002017-05-03 14:04:44 -0400754 int i;
755
Nathan Fontenot1b8955e2017-03-30 02:49:29 -0400756 release_tx_pools(adapter);
757 release_rx_pools(adapter);
758
Nathan Fontenot1b8955e2017-03-30 02:49:29 -0400759 release_stats_token(adapter);
John Allen3d52b592017-08-02 16:44:14 -0500760 release_stats_buffers(adapter);
Nathan Fontenot661a2622017-04-19 13:44:58 -0400761 release_error_buffers(adapter);
Nathan Fontenotc7bac002017-05-03 14:04:44 -0400762
763 if (adapter->napi) {
764 for (i = 0; i < adapter->req_rx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500765 if (&adapter->napi[i]) {
766 netdev_dbg(adapter->netdev,
767 "Releasing napi[%d]\n", i);
Nathan Fontenotc7bac002017-05-03 14:04:44 -0400768 netif_napi_del(&adapter->napi[i]);
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500769 }
Nathan Fontenotc7bac002017-05-03 14:04:44 -0400770 }
771 }
Nathan Fontenot1b8955e2017-03-30 02:49:29 -0400772}
773
Nathan Fontenot53da09e2017-04-21 15:39:04 -0400774static int set_link_state(struct ibmvnic_adapter *adapter, u8 link_state)
775{
776 struct net_device *netdev = adapter->netdev;
777 unsigned long timeout = msecs_to_jiffies(30000);
778 union ibmvnic_crq crq;
779 bool resend;
780 int rc;
781
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500782 netdev_dbg(netdev, "setting link state %d\n", link_state);
783
Nathan Fontenot53da09e2017-04-21 15:39:04 -0400784 memset(&crq, 0, sizeof(crq));
785 crq.logical_link_state.first = IBMVNIC_CRQ_CMD;
786 crq.logical_link_state.cmd = LOGICAL_LINK_STATE;
787 crq.logical_link_state.link_state = link_state;
788
789 do {
790 resend = false;
791
792 reinit_completion(&adapter->init_done);
793 rc = ibmvnic_send_crq(adapter, &crq);
794 if (rc) {
795 netdev_err(netdev, "Failed to set link state\n");
796 return rc;
797 }
798
799 if (!wait_for_completion_timeout(&adapter->init_done,
800 timeout)) {
801 netdev_err(netdev, "timeout setting link state\n");
802 return -1;
803 }
804
805 if (adapter->init_done_rc == 1) {
806 /* Partuial success, delay and re-send */
807 mdelay(1000);
808 resend = true;
809 }
810 } while (resend);
811
812 return 0;
813}
814
Thomas Falcon7f3c6e62017-04-21 15:38:40 -0400815static int set_real_num_queues(struct net_device *netdev)
816{
817 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
818 int rc;
819
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500820 netdev_dbg(netdev, "Setting real tx/rx queues (%llx/%llx)\n",
821 adapter->req_tx_queues, adapter->req_rx_queues);
822
Thomas Falcon7f3c6e62017-04-21 15:38:40 -0400823 rc = netif_set_real_num_tx_queues(netdev, adapter->req_tx_queues);
824 if (rc) {
825 netdev_err(netdev, "failed to set the number of tx queues\n");
826 return rc;
827 }
828
829 rc = netif_set_real_num_rx_queues(netdev, adapter->req_rx_queues);
830 if (rc)
831 netdev_err(netdev, "failed to set the number of rx queues\n");
832
833 return rc;
834}
835
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400836static int init_resources(struct ibmvnic_adapter *adapter)
John Allena57a5d22017-03-17 17:13:41 -0500837{
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400838 struct net_device *netdev = adapter->netdev;
839 int i, rc;
John Allena57a5d22017-03-17 17:13:41 -0500840
Thomas Falcon7f3c6e62017-04-21 15:38:40 -0400841 rc = set_real_num_queues(netdev);
842 if (rc)
843 return rc;
John Allenbd0b6722017-03-17 17:13:40 -0500844
John Allen3d52b592017-08-02 16:44:14 -0500845 rc = init_stats_buffers(adapter);
846 if (rc)
847 return rc;
848
Nathan Fontenot5d5e84e2017-04-21 15:38:58 -0400849 rc = init_stats_token(adapter);
850 if (rc)
851 return rc;
852
Thomas Falcon032c5e82015-12-21 11:26:06 -0600853 adapter->map_id = 1;
854 adapter->napi = kcalloc(adapter->req_rx_queues,
855 sizeof(struct napi_struct), GFP_KERNEL);
856 if (!adapter->napi)
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400857 return -ENOMEM;
858
Thomas Falcon032c5e82015-12-21 11:26:06 -0600859 for (i = 0; i < adapter->req_rx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500860 netdev_dbg(netdev, "Adding napi[%d]\n", i);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600861 netif_napi_add(netdev, &adapter->napi[i], ibmvnic_poll,
862 NAPI_POLL_WEIGHT);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600863 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600864
Thomas Falcon032c5e82015-12-21 11:26:06 -0600865 send_map_query(adapter);
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400866
867 rc = init_rx_pools(netdev);
868 if (rc)
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400869 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600870
Nathan Fontenotc657e322017-03-30 02:49:06 -0400871 rc = init_tx_pools(netdev);
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400872 return rc;
873}
874
Nathan Fontenoted651a12017-05-03 14:04:38 -0400875static int __ibmvnic_open(struct net_device *netdev)
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400876{
877 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Nathan Fontenoted651a12017-05-03 14:04:38 -0400878 enum vnic_state prev_state = adapter->state;
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400879 int i, rc;
880
Nathan Fontenot90c80142017-05-03 14:04:32 -0400881 adapter->state = VNIC_OPENING;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600882 replenish_pools(adapter);
John Allend944c3d62017-05-26 10:30:13 -0400883 ibmvnic_napi_enable(adapter);
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400884
Thomas Falcon032c5e82015-12-21 11:26:06 -0600885 /* We're ready to receive frames, enable the sub-crq interrupts and
886 * set the logical link state to up
887 */
Nathan Fontenoted651a12017-05-03 14:04:38 -0400888 for (i = 0; i < adapter->req_rx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500889 netdev_dbg(netdev, "Enabling rx_scrq[%d] irq\n", i);
Nathan Fontenoted651a12017-05-03 14:04:38 -0400890 if (prev_state == VNIC_CLOSED)
891 enable_irq(adapter->rx_scrq[i]->irq);
892 else
893 enable_scrq_irq(adapter, adapter->rx_scrq[i]);
894 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600895
Nathan Fontenoted651a12017-05-03 14:04:38 -0400896 for (i = 0; i < adapter->req_tx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500897 netdev_dbg(netdev, "Enabling tx_scrq[%d] irq\n", i);
Nathan Fontenoted651a12017-05-03 14:04:38 -0400898 if (prev_state == VNIC_CLOSED)
899 enable_irq(adapter->tx_scrq[i]->irq);
900 else
901 enable_scrq_irq(adapter, adapter->tx_scrq[i]);
902 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600903
Nathan Fontenot53da09e2017-04-21 15:39:04 -0400904 rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_UP);
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400905 if (rc) {
906 for (i = 0; i < adapter->req_rx_queues; i++)
907 napi_disable(&adapter->napi[i]);
908 release_resources(adapter);
Nathan Fontenoted651a12017-05-03 14:04:38 -0400909 return rc;
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400910 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600911
Nathan Fontenoted651a12017-05-03 14:04:38 -0400912 netif_tx_start_all_queues(netdev);
913
914 if (prev_state == VNIC_CLOSED) {
915 for (i = 0; i < adapter->req_rx_queues; i++)
916 napi_schedule(&adapter->napi[i]);
917 }
918
919 adapter->state = VNIC_OPEN;
920 return rc;
921}
922
923static int ibmvnic_open(struct net_device *netdev)
924{
925 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
926 int rc;
927
928 mutex_lock(&adapter->reset_lock);
929
John Allenc26eba02017-10-26 16:23:25 -0500930 if (adapter->mac_change_pending) {
931 __ibmvnic_set_mac(netdev, &adapter->desired.mac);
932 adapter->mac_change_pending = false;
933 }
934
Nathan Fontenoted651a12017-05-03 14:04:38 -0400935 if (adapter->state != VNIC_CLOSED) {
936 rc = ibmvnic_login(netdev);
937 if (rc) {
938 mutex_unlock(&adapter->reset_lock);
939 return rc;
940 }
941
942 rc = init_resources(adapter);
943 if (rc) {
944 netdev_err(netdev, "failed to initialize resources\n");
945 release_resources(adapter);
946 mutex_unlock(&adapter->reset_lock);
947 return rc;
948 }
949 }
950
951 rc = __ibmvnic_open(netdev);
Mick Tarsele876a8a2017-09-28 13:53:18 -0700952 netif_carrier_on(netdev);
Nathan Fontenoted651a12017-05-03 14:04:38 -0400953 mutex_unlock(&adapter->reset_lock);
954
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400955 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600956}
957
Nathan Fontenotb41b83e2017-05-03 14:04:56 -0400958static void clean_tx_pools(struct ibmvnic_adapter *adapter)
959{
960 struct ibmvnic_tx_pool *tx_pool;
961 u64 tx_entries;
962 int tx_scrqs;
963 int i, j;
964
965 if (!adapter->tx_pool)
966 return;
967
968 tx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
969 tx_entries = adapter->req_tx_entries_per_subcrq;
970
971 /* Free any remaining skbs in the tx buffer pools */
972 for (i = 0; i < tx_scrqs; i++) {
973 tx_pool = &adapter->tx_pool[i];
974 if (!tx_pool)
975 continue;
976
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500977 netdev_dbg(adapter->netdev, "Cleaning tx_pool[%d]\n", i);
Nathan Fontenotb41b83e2017-05-03 14:04:56 -0400978 for (j = 0; j < tx_entries; j++) {
979 if (tx_pool->tx_buff[j].skb) {
980 dev_kfree_skb_any(tx_pool->tx_buff[j].skb);
981 tx_pool->tx_buff[j].skb = NULL;
982 }
983 }
984 }
985}
986
Nathan Fontenoted651a12017-05-03 14:04:38 -0400987static int __ibmvnic_close(struct net_device *netdev)
John Allenea5509f2017-03-17 17:13:43 -0500988{
989 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Nathan Fontenot53da09e2017-04-21 15:39:04 -0400990 int rc = 0;
John Allenea5509f2017-03-17 17:13:43 -0500991 int i;
992
Nathan Fontenot90c80142017-05-03 14:04:32 -0400993 adapter->state = VNIC_CLOSING;
Thomas Falcon4c2687a2017-06-14 23:50:06 -0500994
995 /* ensure that transmissions are stopped if called by do_reset */
996 if (adapter->resetting)
997 netif_tx_disable(netdev);
998 else
999 netif_tx_stop_all_queues(netdev);
1000
John Allend944c3d62017-05-26 10:30:13 -04001001 ibmvnic_napi_disable(adapter);
Nathan Fontenot46293b92017-05-03 14:05:02 -04001002
1003 if (adapter->tx_scrq) {
1004 for (i = 0; i < adapter->req_tx_queues; i++)
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001005 if (adapter->tx_scrq[i]->irq) {
1006 netdev_dbg(adapter->netdev,
1007 "Disabling tx_scrq[%d] irq\n", i);
Nathan Fontenot46293b92017-05-03 14:05:02 -04001008 disable_irq(adapter->tx_scrq[i]->irq);
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001009 }
Nathan Fontenot46293b92017-05-03 14:05:02 -04001010 }
1011
Nathan Fontenot53da09e2017-04-21 15:39:04 -04001012 rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_DN);
Nathan Fontenot46293b92017-05-03 14:05:02 -04001013 if (rc)
1014 return rc;
1015
1016 if (adapter->rx_scrq) {
1017 for (i = 0; i < adapter->req_rx_queues; i++) {
1018 int retries = 10;
1019
1020 while (pending_scrq(adapter, adapter->rx_scrq[i])) {
1021 retries--;
1022 mdelay(100);
1023
1024 if (retries == 0)
1025 break;
1026 }
1027
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001028 if (adapter->rx_scrq[i]->irq) {
1029 netdev_dbg(adapter->netdev,
1030 "Disabling rx_scrq[%d] irq\n", i);
Nathan Fontenot46293b92017-05-03 14:05:02 -04001031 disable_irq(adapter->rx_scrq[i]->irq);
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001032 }
Nathan Fontenot46293b92017-05-03 14:05:02 -04001033 }
1034 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001035
Thomas Falcon10f76212017-05-26 10:30:31 -04001036 clean_tx_pools(adapter);
Nathan Fontenot90c80142017-05-03 14:04:32 -04001037 adapter->state = VNIC_CLOSED;
Nathan Fontenot53da09e2017-04-21 15:39:04 -04001038 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001039}
1040
Nathan Fontenoted651a12017-05-03 14:04:38 -04001041static int ibmvnic_close(struct net_device *netdev)
1042{
1043 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1044 int rc;
1045
1046 mutex_lock(&adapter->reset_lock);
1047 rc = __ibmvnic_close(netdev);
1048 mutex_unlock(&adapter->reset_lock);
1049
1050 return rc;
1051}
1052
Thomas Falconad7775d2016-04-01 17:20:34 -05001053/**
1054 * build_hdr_data - creates L2/L3/L4 header data buffer
1055 * @hdr_field - bitfield determining needed headers
1056 * @skb - socket buffer
1057 * @hdr_len - array of header lengths
1058 * @tot_len - total length of data
1059 *
1060 * Reads hdr_field to determine which headers are needed by firmware.
1061 * Builds a buffer containing these headers. Saves individual header
1062 * lengths and total buffer length to be used to build descriptors.
1063 */
1064static int build_hdr_data(u8 hdr_field, struct sk_buff *skb,
1065 int *hdr_len, u8 *hdr_data)
1066{
1067 int len = 0;
1068 u8 *hdr;
1069
1070 hdr_len[0] = sizeof(struct ethhdr);
1071
1072 if (skb->protocol == htons(ETH_P_IP)) {
1073 hdr_len[1] = ip_hdr(skb)->ihl * 4;
1074 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
1075 hdr_len[2] = tcp_hdrlen(skb);
1076 else if (ip_hdr(skb)->protocol == IPPROTO_UDP)
1077 hdr_len[2] = sizeof(struct udphdr);
1078 } else if (skb->protocol == htons(ETH_P_IPV6)) {
1079 hdr_len[1] = sizeof(struct ipv6hdr);
1080 if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
1081 hdr_len[2] = tcp_hdrlen(skb);
1082 else if (ipv6_hdr(skb)->nexthdr == IPPROTO_UDP)
1083 hdr_len[2] = sizeof(struct udphdr);
1084 }
1085
1086 memset(hdr_data, 0, 120);
1087 if ((hdr_field >> 6) & 1) {
1088 hdr = skb_mac_header(skb);
1089 memcpy(hdr_data, hdr, hdr_len[0]);
1090 len += hdr_len[0];
1091 }
1092
1093 if ((hdr_field >> 5) & 1) {
1094 hdr = skb_network_header(skb);
1095 memcpy(hdr_data + len, hdr, hdr_len[1]);
1096 len += hdr_len[1];
1097 }
1098
1099 if ((hdr_field >> 4) & 1) {
1100 hdr = skb_transport_header(skb);
1101 memcpy(hdr_data + len, hdr, hdr_len[2]);
1102 len += hdr_len[2];
1103 }
1104 return len;
1105}
1106
1107/**
1108 * create_hdr_descs - create header and header extension descriptors
1109 * @hdr_field - bitfield determining needed headers
1110 * @data - buffer containing header data
1111 * @len - length of data buffer
1112 * @hdr_len - array of individual header lengths
1113 * @scrq_arr - descriptor array
1114 *
1115 * Creates header and, if needed, header extension descriptors and
1116 * places them in a descriptor array, scrq_arr
1117 */
1118
Thomas Falcon2de09682017-10-16 10:02:11 -05001119static int create_hdr_descs(u8 hdr_field, u8 *hdr_data, int len, int *hdr_len,
1120 union sub_crq *scrq_arr)
Thomas Falconad7775d2016-04-01 17:20:34 -05001121{
1122 union sub_crq hdr_desc;
1123 int tmp_len = len;
Thomas Falcon2de09682017-10-16 10:02:11 -05001124 int num_descs = 0;
Thomas Falconad7775d2016-04-01 17:20:34 -05001125 u8 *data, *cur;
1126 int tmp;
1127
1128 while (tmp_len > 0) {
1129 cur = hdr_data + len - tmp_len;
1130
1131 memset(&hdr_desc, 0, sizeof(hdr_desc));
1132 if (cur != hdr_data) {
1133 data = hdr_desc.hdr_ext.data;
1134 tmp = tmp_len > 29 ? 29 : tmp_len;
1135 hdr_desc.hdr_ext.first = IBMVNIC_CRQ_CMD;
1136 hdr_desc.hdr_ext.type = IBMVNIC_HDR_EXT_DESC;
1137 hdr_desc.hdr_ext.len = tmp;
1138 } else {
1139 data = hdr_desc.hdr.data;
1140 tmp = tmp_len > 24 ? 24 : tmp_len;
1141 hdr_desc.hdr.first = IBMVNIC_CRQ_CMD;
1142 hdr_desc.hdr.type = IBMVNIC_HDR_DESC;
1143 hdr_desc.hdr.len = tmp;
1144 hdr_desc.hdr.l2_len = (u8)hdr_len[0];
1145 hdr_desc.hdr.l3_len = cpu_to_be16((u16)hdr_len[1]);
1146 hdr_desc.hdr.l4_len = (u8)hdr_len[2];
1147 hdr_desc.hdr.flag = hdr_field << 1;
1148 }
1149 memcpy(data, cur, tmp);
1150 tmp_len -= tmp;
1151 *scrq_arr = hdr_desc;
1152 scrq_arr++;
Thomas Falcon2de09682017-10-16 10:02:11 -05001153 num_descs++;
Thomas Falconad7775d2016-04-01 17:20:34 -05001154 }
Thomas Falcon2de09682017-10-16 10:02:11 -05001155
1156 return num_descs;
Thomas Falconad7775d2016-04-01 17:20:34 -05001157}
1158
1159/**
1160 * build_hdr_descs_arr - build a header descriptor array
1161 * @skb - socket buffer
1162 * @num_entries - number of descriptors to be sent
1163 * @subcrq - first TX descriptor
1164 * @hdr_field - bit field determining which headers will be sent
1165 *
1166 * This function will build a TX descriptor array with applicable
1167 * L2/L3/L4 packet header descriptors to be sent by send_subcrq_indirect.
1168 */
1169
1170static void build_hdr_descs_arr(struct ibmvnic_tx_buff *txbuff,
1171 int *num_entries, u8 hdr_field)
1172{
1173 int hdr_len[3] = {0, 0, 0};
Thomas Falcon2de09682017-10-16 10:02:11 -05001174 int tot_len;
Thomas Falconad7775d2016-04-01 17:20:34 -05001175 u8 *hdr_data = txbuff->hdr_data;
1176
1177 tot_len = build_hdr_data(hdr_field, txbuff->skb, hdr_len,
1178 txbuff->hdr_data);
Thomas Falcon2de09682017-10-16 10:02:11 -05001179 *num_entries += create_hdr_descs(hdr_field, hdr_data, tot_len, hdr_len,
Thomas Falconad7775d2016-04-01 17:20:34 -05001180 txbuff->indir_arr + 1);
1181}
1182
Thomas Falcon032c5e82015-12-21 11:26:06 -06001183static int ibmvnic_xmit(struct sk_buff *skb, struct net_device *netdev)
1184{
1185 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1186 int queue_num = skb_get_queue_mapping(skb);
Thomas Falconad7775d2016-04-01 17:20:34 -05001187 u8 *hdrs = (u8 *)&adapter->tx_rx_desc_req;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001188 struct device *dev = &adapter->vdev->dev;
1189 struct ibmvnic_tx_buff *tx_buff = NULL;
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001190 struct ibmvnic_sub_crq_queue *tx_scrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001191 struct ibmvnic_tx_pool *tx_pool;
1192 unsigned int tx_send_failed = 0;
1193 unsigned int tx_map_failed = 0;
1194 unsigned int tx_dropped = 0;
1195 unsigned int tx_packets = 0;
1196 unsigned int tx_bytes = 0;
1197 dma_addr_t data_dma_addr;
1198 struct netdev_queue *txq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001199 unsigned long lpar_rc;
1200 union sub_crq tx_crq;
1201 unsigned int offset;
Thomas Falconad7775d2016-04-01 17:20:34 -05001202 int num_entries = 1;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001203 unsigned char *dst;
1204 u64 *handle_array;
1205 int index = 0;
1206 int ret = 0;
1207
Nathan Fontenoted651a12017-05-03 14:04:38 -04001208 if (adapter->resetting) {
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001209 if (!netif_subqueue_stopped(netdev, skb))
1210 netif_stop_subqueue(netdev, queue_num);
1211 dev_kfree_skb_any(skb);
1212
Thomas Falcon032c5e82015-12-21 11:26:06 -06001213 tx_send_failed++;
1214 tx_dropped++;
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001215 ret = NETDEV_TX_OK;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001216 goto out;
1217 }
1218
Nathan Fontenot161b8a82017-05-03 14:05:08 -04001219 tx_pool = &adapter->tx_pool[queue_num];
1220 tx_scrq = adapter->tx_scrq[queue_num];
1221 txq = netdev_get_tx_queue(netdev, skb_get_queue_mapping(skb));
1222 handle_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
1223 be32_to_cpu(adapter->login_rsp_buf->off_txsubm_subcrqs));
1224
Thomas Falcon032c5e82015-12-21 11:26:06 -06001225 index = tx_pool->free_map[tx_pool->consumer_index];
Thomas Falconfdb06102017-10-17 12:36:55 -05001226
1227 if (skb_is_gso(skb)) {
1228 offset = tx_pool->tso_index * IBMVNIC_TSO_BUF_SZ;
1229 dst = tx_pool->tso_ltb.buff + offset;
1230 memset(dst, 0, IBMVNIC_TSO_BUF_SZ);
1231 data_dma_addr = tx_pool->tso_ltb.addr + offset;
1232 tx_pool->tso_index++;
1233 if (tx_pool->tso_index == IBMVNIC_TSO_BUFS)
1234 tx_pool->tso_index = 0;
1235 } else {
1236 offset = index * adapter->req_mtu;
1237 dst = tx_pool->long_term_buff.buff + offset;
1238 memset(dst, 0, adapter->req_mtu);
1239 data_dma_addr = tx_pool->long_term_buff.addr + offset;
1240 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001241
Thomas Falcon15482052017-10-17 12:36:54 -05001242 if (skb_shinfo(skb)->nr_frags) {
1243 int cur, i;
1244
1245 /* Copy the head */
1246 skb_copy_from_linear_data(skb, dst, skb_headlen(skb));
1247 cur = skb_headlen(skb);
1248
1249 /* Copy the frags */
1250 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1251 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1252
1253 memcpy(dst + cur,
1254 page_address(skb_frag_page(frag)) +
1255 frag->page_offset, skb_frag_size(frag));
1256 cur += skb_frag_size(frag);
1257 }
1258 } else {
1259 skb_copy_from_linear_data(skb, dst, skb->len);
1260 }
1261
Thomas Falcon032c5e82015-12-21 11:26:06 -06001262 tx_pool->consumer_index =
1263 (tx_pool->consumer_index + 1) %
Thomas Falcon068d9f92017-03-05 12:18:42 -06001264 adapter->req_tx_entries_per_subcrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001265
1266 tx_buff = &tx_pool->tx_buff[index];
1267 tx_buff->skb = skb;
1268 tx_buff->data_dma[0] = data_dma_addr;
1269 tx_buff->data_len[0] = skb->len;
1270 tx_buff->index = index;
1271 tx_buff->pool_index = queue_num;
1272 tx_buff->last_frag = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001273
1274 memset(&tx_crq, 0, sizeof(tx_crq));
1275 tx_crq.v1.first = IBMVNIC_CRQ_CMD;
1276 tx_crq.v1.type = IBMVNIC_TX_DESC;
1277 tx_crq.v1.n_crq_elem = 1;
1278 tx_crq.v1.n_sge = 1;
1279 tx_crq.v1.flags1 = IBMVNIC_TX_COMP_NEEDED;
1280 tx_crq.v1.correlator = cpu_to_be32(index);
Thomas Falconfdb06102017-10-17 12:36:55 -05001281 if (skb_is_gso(skb))
1282 tx_crq.v1.dma_reg = cpu_to_be16(tx_pool->tso_ltb.map_id);
1283 else
1284 tx_crq.v1.dma_reg = cpu_to_be16(tx_pool->long_term_buff.map_id);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001285 tx_crq.v1.sge_len = cpu_to_be32(skb->len);
1286 tx_crq.v1.ioba = cpu_to_be64(data_dma_addr);
1287
1288 if (adapter->vlan_header_insertion) {
1289 tx_crq.v1.flags2 |= IBMVNIC_TX_VLAN_INSERT;
1290 tx_crq.v1.vlan_id = cpu_to_be16(skb->vlan_tci);
1291 }
1292
1293 if (skb->protocol == htons(ETH_P_IP)) {
1294 if (ip_hdr(skb)->version == 4)
1295 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV4;
1296 else if (ip_hdr(skb)->version == 6)
1297 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV6;
1298
1299 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
1300 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_TCP;
1301 else if (ip_hdr(skb)->protocol != IPPROTO_TCP)
1302 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_UDP;
1303 }
1304
Thomas Falconad7775d2016-04-01 17:20:34 -05001305 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06001306 tx_crq.v1.flags1 |= IBMVNIC_TX_CHKSUM_OFFLOAD;
Thomas Falconad7775d2016-04-01 17:20:34 -05001307 hdrs += 2;
1308 }
Thomas Falconfdb06102017-10-17 12:36:55 -05001309 if (skb_is_gso(skb)) {
1310 tx_crq.v1.flags1 |= IBMVNIC_TX_LSO;
1311 tx_crq.v1.mss = cpu_to_be16(skb_shinfo(skb)->gso_size);
1312 hdrs += 2;
1313 }
Thomas Falconad7775d2016-04-01 17:20:34 -05001314 /* determine if l2/3/4 headers are sent to firmware */
1315 if ((*hdrs >> 7) & 1 &&
1316 (skb->protocol == htons(ETH_P_IP) ||
1317 skb->protocol == htons(ETH_P_IPV6))) {
1318 build_hdr_descs_arr(tx_buff, &num_entries, *hdrs);
1319 tx_crq.v1.n_crq_elem = num_entries;
1320 tx_buff->indir_arr[0] = tx_crq;
1321 tx_buff->indir_dma = dma_map_single(dev, tx_buff->indir_arr,
1322 sizeof(tx_buff->indir_arr),
1323 DMA_TO_DEVICE);
1324 if (dma_mapping_error(dev, tx_buff->indir_dma)) {
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001325 dev_kfree_skb_any(skb);
1326 tx_buff->skb = NULL;
Thomas Falconad7775d2016-04-01 17:20:34 -05001327 if (!firmware_has_feature(FW_FEATURE_CMO))
1328 dev_err(dev, "tx: unable to map descriptor array\n");
1329 tx_map_failed++;
1330 tx_dropped++;
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001331 ret = NETDEV_TX_OK;
Thomas Falconad7775d2016-04-01 17:20:34 -05001332 goto out;
1333 }
John Allen498cd8e2016-04-06 11:49:55 -05001334 lpar_rc = send_subcrq_indirect(adapter, handle_array[queue_num],
Thomas Falconad7775d2016-04-01 17:20:34 -05001335 (u64)tx_buff->indir_dma,
1336 (u64)num_entries);
1337 } else {
John Allen498cd8e2016-04-06 11:49:55 -05001338 lpar_rc = send_subcrq(adapter, handle_array[queue_num],
1339 &tx_crq);
Thomas Falconad7775d2016-04-01 17:20:34 -05001340 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001341 if (lpar_rc != H_SUCCESS) {
1342 dev_err(dev, "tx failed with code %ld\n", lpar_rc);
1343
1344 if (tx_pool->consumer_index == 0)
1345 tx_pool->consumer_index =
Thomas Falcon068d9f92017-03-05 12:18:42 -06001346 adapter->req_tx_entries_per_subcrq - 1;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001347 else
1348 tx_pool->consumer_index--;
1349
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001350 dev_kfree_skb_any(skb);
1351 tx_buff->skb = NULL;
1352
Thomas Falconb8c80b82017-05-26 10:30:42 -04001353 if (lpar_rc == H_CLOSED) {
1354 /* Disable TX and report carrier off if queue is closed.
1355 * Firmware guarantees that a signal will be sent to the
1356 * driver, triggering a reset or some other action.
1357 */
1358 netif_tx_stop_all_queues(netdev);
1359 netif_carrier_off(netdev);
1360 }
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001361
Thomas Falcon032c5e82015-12-21 11:26:06 -06001362 tx_send_failed++;
1363 tx_dropped++;
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001364 ret = NETDEV_TX_OK;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001365 goto out;
1366 }
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001367
Brian King58c8c0c2017-04-19 13:44:47 -04001368 if (atomic_inc_return(&tx_scrq->used)
1369 >= adapter->req_tx_entries_per_subcrq) {
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001370 netdev_info(netdev, "Stopping queue %d\n", queue_num);
1371 netif_stop_subqueue(netdev, queue_num);
1372 }
1373
Thomas Falcon032c5e82015-12-21 11:26:06 -06001374 tx_packets++;
1375 tx_bytes += skb->len;
1376 txq->trans_start = jiffies;
1377 ret = NETDEV_TX_OK;
1378
1379out:
1380 netdev->stats.tx_dropped += tx_dropped;
1381 netdev->stats.tx_bytes += tx_bytes;
1382 netdev->stats.tx_packets += tx_packets;
1383 adapter->tx_send_failed += tx_send_failed;
1384 adapter->tx_map_failed += tx_map_failed;
John Allen3d52b592017-08-02 16:44:14 -05001385 adapter->tx_stats_buffers[queue_num].packets += tx_packets;
1386 adapter->tx_stats_buffers[queue_num].bytes += tx_bytes;
1387 adapter->tx_stats_buffers[queue_num].dropped_packets += tx_dropped;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001388
1389 return ret;
1390}
1391
1392static void ibmvnic_set_multi(struct net_device *netdev)
1393{
1394 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1395 struct netdev_hw_addr *ha;
1396 union ibmvnic_crq crq;
1397
1398 memset(&crq, 0, sizeof(crq));
1399 crq.request_capability.first = IBMVNIC_CRQ_CMD;
1400 crq.request_capability.cmd = REQUEST_CAPABILITY;
1401
1402 if (netdev->flags & IFF_PROMISC) {
1403 if (!adapter->promisc_supported)
1404 return;
1405 } else {
1406 if (netdev->flags & IFF_ALLMULTI) {
1407 /* Accept all multicast */
1408 memset(&crq, 0, sizeof(crq));
1409 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1410 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1411 crq.multicast_ctrl.flags = IBMVNIC_ENABLE_ALL;
1412 ibmvnic_send_crq(adapter, &crq);
1413 } else if (netdev_mc_empty(netdev)) {
1414 /* Reject all multicast */
1415 memset(&crq, 0, sizeof(crq));
1416 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1417 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1418 crq.multicast_ctrl.flags = IBMVNIC_DISABLE_ALL;
1419 ibmvnic_send_crq(adapter, &crq);
1420 } else {
1421 /* Accept one or more multicast(s) */
1422 netdev_for_each_mc_addr(ha, netdev) {
1423 memset(&crq, 0, sizeof(crq));
1424 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1425 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1426 crq.multicast_ctrl.flags = IBMVNIC_ENABLE_MC;
1427 ether_addr_copy(&crq.multicast_ctrl.mac_addr[0],
1428 ha->addr);
1429 ibmvnic_send_crq(adapter, &crq);
1430 }
1431 }
1432 }
1433}
1434
John Allenc26eba02017-10-26 16:23:25 -05001435static int __ibmvnic_set_mac(struct net_device *netdev, struct sockaddr *p)
Thomas Falcon032c5e82015-12-21 11:26:06 -06001436{
1437 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1438 struct sockaddr *addr = p;
1439 union ibmvnic_crq crq;
1440
1441 if (!is_valid_ether_addr(addr->sa_data))
1442 return -EADDRNOTAVAIL;
1443
1444 memset(&crq, 0, sizeof(crq));
1445 crq.change_mac_addr.first = IBMVNIC_CRQ_CMD;
1446 crq.change_mac_addr.cmd = CHANGE_MAC_ADDR;
1447 ether_addr_copy(&crq.change_mac_addr.mac_addr[0], addr->sa_data);
1448 ibmvnic_send_crq(adapter, &crq);
1449 /* netdev->dev_addr is changed in handle_change_mac_rsp function */
1450 return 0;
1451}
1452
John Allenc26eba02017-10-26 16:23:25 -05001453static int ibmvnic_set_mac(struct net_device *netdev, void *p)
1454{
1455 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1456 struct sockaddr *addr = p;
1457
1458 if (adapter->state != VNIC_OPEN) {
1459 memcpy(&adapter->desired.mac, addr, sizeof(struct sockaddr));
1460 adapter->mac_change_pending = true;
1461 return 0;
1462 }
1463
1464 __ibmvnic_set_mac(netdev, addr);
1465
1466 return 0;
1467}
1468
Nathan Fontenoted651a12017-05-03 14:04:38 -04001469/**
1470 * do_reset returns zero if we are able to keep processing reset events, or
1471 * non-zero if we hit a fatal error and must halt.
1472 */
1473static int do_reset(struct ibmvnic_adapter *adapter,
1474 struct ibmvnic_rwi *rwi, u32 reset_state)
1475{
1476 struct net_device *netdev = adapter->netdev;
1477 int i, rc;
1478
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001479 netdev_dbg(adapter->netdev, "Re-setting driver (%d)\n",
1480 rwi->reset_reason);
1481
Nathan Fontenoted651a12017-05-03 14:04:38 -04001482 netif_carrier_off(netdev);
1483 adapter->reset_reason = rwi->reset_reason;
1484
1485 if (rwi->reset_reason == VNIC_RESET_MOBILITY) {
1486 rc = ibmvnic_reenable_crq_queue(adapter);
1487 if (rc)
1488 return 0;
1489 }
1490
1491 rc = __ibmvnic_close(netdev);
1492 if (rc)
1493 return rc;
1494
John Allenc26eba02017-10-26 16:23:25 -05001495 if (adapter->reset_reason == VNIC_RESET_CHANGE_PARAM ||
1496 adapter->wait_for_reset) {
1497 release_resources(adapter);
1498 release_sub_crqs(adapter);
1499 release_crq_queue(adapter);
1500 }
1501
John Allen8cb31cf2017-05-26 10:30:37 -04001502 if (adapter->reset_reason != VNIC_RESET_NON_FATAL) {
1503 /* remove the closed state so when we call open it appears
1504 * we are coming from the probed state.
1505 */
Nathan Fontenoted651a12017-05-03 14:04:38 -04001506 adapter->state = VNIC_PROBED;
John Allen8cb31cf2017-05-26 10:30:37 -04001507
John Allen8cb31cf2017-05-26 10:30:37 -04001508 rc = ibmvnic_init(adapter);
1509 if (rc)
1510 return 0;
1511
1512 /* If the adapter was in PROBE state prior to the reset,
1513 * exit here.
1514 */
1515 if (reset_state == VNIC_PROBED)
1516 return 0;
1517
1518 rc = ibmvnic_login(netdev);
1519 if (rc) {
1520 adapter->state = VNIC_PROBED;
1521 return 0;
1522 }
1523
John Allenc26eba02017-10-26 16:23:25 -05001524 if (adapter->reset_reason == VNIC_RESET_CHANGE_PARAM ||
1525 adapter->wait_for_reset) {
1526 rc = init_resources(adapter);
1527 if (rc)
1528 return rc;
1529 } else {
1530 rc = reset_tx_pools(adapter);
1531 if (rc)
1532 return rc;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -04001533
John Allenc26eba02017-10-26 16:23:25 -05001534 rc = reset_rx_pools(adapter);
1535 if (rc)
1536 return rc;
John Allen8cb31cf2017-05-26 10:30:37 -04001537
John Allenc26eba02017-10-26 16:23:25 -05001538 if (reset_state == VNIC_CLOSED)
1539 return 0;
1540 }
Nathan Fontenoted651a12017-05-03 14:04:38 -04001541 }
1542
Nathan Fontenoted651a12017-05-03 14:04:38 -04001543 rc = __ibmvnic_open(netdev);
1544 if (rc) {
1545 if (list_empty(&adapter->rwi_list))
1546 adapter->state = VNIC_CLOSED;
1547 else
1548 adapter->state = reset_state;
1549
1550 return 0;
1551 }
1552
1553 netif_carrier_on(netdev);
1554
1555 /* kick napi */
1556 for (i = 0; i < adapter->req_rx_queues; i++)
1557 napi_schedule(&adapter->napi[i]);
1558
Nathan Fontenot61d3e1d2017-06-12 20:47:45 -04001559 if (adapter->reset_reason != VNIC_RESET_FAILOVER)
1560 netdev_notify_peers(netdev);
1561
Nathan Fontenoted651a12017-05-03 14:04:38 -04001562 return 0;
1563}
1564
1565static struct ibmvnic_rwi *get_next_rwi(struct ibmvnic_adapter *adapter)
1566{
1567 struct ibmvnic_rwi *rwi;
1568
1569 mutex_lock(&adapter->rwi_lock);
1570
1571 if (!list_empty(&adapter->rwi_list)) {
1572 rwi = list_first_entry(&adapter->rwi_list, struct ibmvnic_rwi,
1573 list);
1574 list_del(&rwi->list);
1575 } else {
1576 rwi = NULL;
1577 }
1578
1579 mutex_unlock(&adapter->rwi_lock);
1580 return rwi;
1581}
1582
1583static void free_all_rwi(struct ibmvnic_adapter *adapter)
1584{
1585 struct ibmvnic_rwi *rwi;
1586
1587 rwi = get_next_rwi(adapter);
1588 while (rwi) {
1589 kfree(rwi);
1590 rwi = get_next_rwi(adapter);
1591 }
1592}
1593
1594static void __ibmvnic_reset(struct work_struct *work)
1595{
1596 struct ibmvnic_rwi *rwi;
1597 struct ibmvnic_adapter *adapter;
1598 struct net_device *netdev;
1599 u32 reset_state;
John Allenc26eba02017-10-26 16:23:25 -05001600 int rc = 0;
Nathan Fontenoted651a12017-05-03 14:04:38 -04001601
1602 adapter = container_of(work, struct ibmvnic_adapter, ibmvnic_reset);
1603 netdev = adapter->netdev;
1604
1605 mutex_lock(&adapter->reset_lock);
1606 adapter->resetting = true;
1607 reset_state = adapter->state;
1608
1609 rwi = get_next_rwi(adapter);
1610 while (rwi) {
1611 rc = do_reset(adapter, rwi, reset_state);
1612 kfree(rwi);
1613 if (rc)
1614 break;
1615
1616 rwi = get_next_rwi(adapter);
1617 }
1618
John Allenc26eba02017-10-26 16:23:25 -05001619 if (adapter->wait_for_reset) {
1620 adapter->wait_for_reset = false;
1621 adapter->reset_done_rc = rc;
1622 complete(&adapter->reset_done);
1623 }
1624
Nathan Fontenoted651a12017-05-03 14:04:38 -04001625 if (rc) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001626 netdev_dbg(adapter->netdev, "Reset failed\n");
Nathan Fontenoted651a12017-05-03 14:04:38 -04001627 free_all_rwi(adapter);
Wei Yongjun6d0af072017-05-18 15:24:52 +00001628 mutex_unlock(&adapter->reset_lock);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001629 return;
1630 }
1631
1632 adapter->resetting = false;
1633 mutex_unlock(&adapter->reset_lock);
1634}
1635
1636static void ibmvnic_reset(struct ibmvnic_adapter *adapter,
1637 enum ibmvnic_reset_reason reason)
1638{
1639 struct ibmvnic_rwi *rwi, *tmp;
1640 struct net_device *netdev = adapter->netdev;
1641 struct list_head *entry;
1642
1643 if (adapter->state == VNIC_REMOVING ||
1644 adapter->state == VNIC_REMOVED) {
1645 netdev_dbg(netdev, "Adapter removing, skipping reset\n");
1646 return;
1647 }
1648
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04001649 if (adapter->state == VNIC_PROBING) {
1650 netdev_warn(netdev, "Adapter reset during probe\n");
1651 adapter->init_done_rc = EAGAIN;
1652 return;
1653 }
1654
Nathan Fontenoted651a12017-05-03 14:04:38 -04001655 mutex_lock(&adapter->rwi_lock);
1656
1657 list_for_each(entry, &adapter->rwi_list) {
1658 tmp = list_entry(entry, struct ibmvnic_rwi, list);
1659 if (tmp->reset_reason == reason) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001660 netdev_dbg(netdev, "Skipping matching reset\n");
Nathan Fontenoted651a12017-05-03 14:04:38 -04001661 mutex_unlock(&adapter->rwi_lock);
1662 return;
1663 }
1664 }
1665
1666 rwi = kzalloc(sizeof(*rwi), GFP_KERNEL);
1667 if (!rwi) {
1668 mutex_unlock(&adapter->rwi_lock);
1669 ibmvnic_close(netdev);
1670 return;
1671 }
1672
1673 rwi->reset_reason = reason;
1674 list_add_tail(&rwi->list, &adapter->rwi_list);
1675 mutex_unlock(&adapter->rwi_lock);
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001676
1677 netdev_dbg(adapter->netdev, "Scheduling reset (reason %d)\n", reason);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001678 schedule_work(&adapter->ibmvnic_reset);
1679}
1680
Thomas Falcon032c5e82015-12-21 11:26:06 -06001681static void ibmvnic_tx_timeout(struct net_device *dev)
1682{
1683 struct ibmvnic_adapter *adapter = netdev_priv(dev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001684
Nathan Fontenoted651a12017-05-03 14:04:38 -04001685 ibmvnic_reset(adapter, VNIC_RESET_TIMEOUT);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001686}
1687
1688static void remove_buff_from_pool(struct ibmvnic_adapter *adapter,
1689 struct ibmvnic_rx_buff *rx_buff)
1690{
1691 struct ibmvnic_rx_pool *pool = &adapter->rx_pool[rx_buff->pool_index];
1692
1693 rx_buff->skb = NULL;
1694
1695 pool->free_map[pool->next_alloc] = (int)(rx_buff - pool->rx_buff);
1696 pool->next_alloc = (pool->next_alloc + 1) % pool->size;
1697
1698 atomic_dec(&pool->available);
1699}
1700
1701static int ibmvnic_poll(struct napi_struct *napi, int budget)
1702{
1703 struct net_device *netdev = napi->dev;
1704 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1705 int scrq_num = (int)(napi - adapter->napi);
1706 int frames_processed = 0;
Nathan Fontenot152ce472017-05-26 10:30:54 -04001707
Thomas Falcon032c5e82015-12-21 11:26:06 -06001708restart_poll:
1709 while (frames_processed < budget) {
1710 struct sk_buff *skb;
1711 struct ibmvnic_rx_buff *rx_buff;
1712 union sub_crq *next;
1713 u32 length;
1714 u16 offset;
1715 u8 flags = 0;
1716
Thomas Falcon21ecba62017-06-14 23:50:09 -05001717 if (unlikely(adapter->resetting)) {
1718 enable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
1719 napi_complete_done(napi, frames_processed);
1720 return frames_processed;
1721 }
1722
Thomas Falcon032c5e82015-12-21 11:26:06 -06001723 if (!pending_scrq(adapter, adapter->rx_scrq[scrq_num]))
1724 break;
1725 next = ibmvnic_next_scrq(adapter, adapter->rx_scrq[scrq_num]);
1726 rx_buff =
1727 (struct ibmvnic_rx_buff *)be64_to_cpu(next->
1728 rx_comp.correlator);
1729 /* do error checking */
1730 if (next->rx_comp.rc) {
John Allene1cea2e2017-08-07 15:42:30 -05001731 netdev_dbg(netdev, "rx buffer returned with rc %x\n",
1732 be16_to_cpu(next->rx_comp.rc));
Thomas Falcon032c5e82015-12-21 11:26:06 -06001733 /* free the entry */
1734 next->rx_comp.first = 0;
1735 remove_buff_from_pool(adapter, rx_buff);
Nathan Fontenotca05e312017-05-03 14:05:14 -04001736 continue;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001737 }
1738
1739 length = be32_to_cpu(next->rx_comp.len);
1740 offset = be16_to_cpu(next->rx_comp.off_frame_data);
1741 flags = next->rx_comp.flags;
1742 skb = rx_buff->skb;
1743 skb_copy_to_linear_data(skb, rx_buff->data + offset,
1744 length);
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04001745
1746 /* VLAN Header has been stripped by the system firmware and
1747 * needs to be inserted by the driver
1748 */
1749 if (adapter->rx_vlan_header_insertion &&
1750 (flags & IBMVNIC_VLAN_STRIPPED))
1751 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
1752 ntohs(next->rx_comp.vlan_tci));
1753
Thomas Falcon032c5e82015-12-21 11:26:06 -06001754 /* free the entry */
1755 next->rx_comp.first = 0;
1756 remove_buff_from_pool(adapter, rx_buff);
1757
1758 skb_put(skb, length);
1759 skb->protocol = eth_type_trans(skb, netdev);
Thomas Falcon94ca3052017-05-03 14:05:20 -04001760 skb_record_rx_queue(skb, scrq_num);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001761
1762 if (flags & IBMVNIC_IP_CHKSUM_GOOD &&
1763 flags & IBMVNIC_TCP_UDP_CHKSUM_GOOD) {
1764 skb->ip_summed = CHECKSUM_UNNECESSARY;
1765 }
1766
1767 length = skb->len;
1768 napi_gro_receive(napi, skb); /* send it up */
1769 netdev->stats.rx_packets++;
1770 netdev->stats.rx_bytes += length;
John Allen3d52b592017-08-02 16:44:14 -05001771 adapter->rx_stats_buffers[scrq_num].packets++;
1772 adapter->rx_stats_buffers[scrq_num].bytes += length;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001773 frames_processed++;
1774 }
Nathan Fontenot152ce472017-05-26 10:30:54 -04001775
1776 if (adapter->state != VNIC_CLOSING)
1777 replenish_rx_pool(adapter, &adapter->rx_pool[scrq_num]);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001778
1779 if (frames_processed < budget) {
1780 enable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
Eric Dumazet6ad20162017-01-30 08:22:01 -08001781 napi_complete_done(napi, frames_processed);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001782 if (pending_scrq(adapter, adapter->rx_scrq[scrq_num]) &&
1783 napi_reschedule(napi)) {
1784 disable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
1785 goto restart_poll;
1786 }
1787 }
1788 return frames_processed;
1789}
1790
1791#ifdef CONFIG_NET_POLL_CONTROLLER
1792static void ibmvnic_netpoll_controller(struct net_device *dev)
1793{
1794 struct ibmvnic_adapter *adapter = netdev_priv(dev);
1795 int i;
1796
1797 replenish_pools(netdev_priv(dev));
1798 for (i = 0; i < adapter->req_rx_queues; i++)
1799 ibmvnic_interrupt_rx(adapter->rx_scrq[i]->irq,
1800 adapter->rx_scrq[i]);
1801}
1802#endif
1803
John Allenc26eba02017-10-26 16:23:25 -05001804static int wait_for_reset(struct ibmvnic_adapter *adapter)
1805{
1806 adapter->fallback.mtu = adapter->req_mtu;
1807 adapter->fallback.rx_queues = adapter->req_rx_queues;
1808 adapter->fallback.tx_queues = adapter->req_tx_queues;
1809 adapter->fallback.rx_entries = adapter->req_rx_add_entries_per_subcrq;
1810 adapter->fallback.tx_entries = adapter->req_tx_entries_per_subcrq;
1811
1812 init_completion(&adapter->reset_done);
1813 ibmvnic_reset(adapter, VNIC_RESET_CHANGE_PARAM);
1814 adapter->wait_for_reset = true;
1815 wait_for_completion(&adapter->reset_done);
1816
1817 if (adapter->reset_done_rc) {
1818 adapter->desired.mtu = adapter->fallback.mtu;
1819 adapter->desired.rx_queues = adapter->fallback.rx_queues;
1820 adapter->desired.tx_queues = adapter->fallback.tx_queues;
1821 adapter->desired.rx_entries = adapter->fallback.rx_entries;
1822 adapter->desired.tx_entries = adapter->fallback.tx_entries;
1823
1824 init_completion(&adapter->reset_done);
1825 ibmvnic_reset(adapter, VNIC_RESET_CHANGE_PARAM);
1826 wait_for_completion(&adapter->reset_done);
1827 }
1828 adapter->wait_for_reset = false;
1829
1830 return adapter->reset_done_rc;
1831}
1832
John Allen3a807b72017-06-06 16:55:52 -05001833static int ibmvnic_change_mtu(struct net_device *netdev, int new_mtu)
1834{
John Allenc26eba02017-10-26 16:23:25 -05001835 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1836
1837 adapter->desired.mtu = new_mtu + ETH_HLEN;
1838
1839 return wait_for_reset(adapter);
John Allen3a807b72017-06-06 16:55:52 -05001840}
1841
Thomas Falcon032c5e82015-12-21 11:26:06 -06001842static const struct net_device_ops ibmvnic_netdev_ops = {
1843 .ndo_open = ibmvnic_open,
1844 .ndo_stop = ibmvnic_close,
1845 .ndo_start_xmit = ibmvnic_xmit,
1846 .ndo_set_rx_mode = ibmvnic_set_multi,
1847 .ndo_set_mac_address = ibmvnic_set_mac,
1848 .ndo_validate_addr = eth_validate_addr,
Thomas Falcon032c5e82015-12-21 11:26:06 -06001849 .ndo_tx_timeout = ibmvnic_tx_timeout,
1850#ifdef CONFIG_NET_POLL_CONTROLLER
1851 .ndo_poll_controller = ibmvnic_netpoll_controller,
1852#endif
John Allen3a807b72017-06-06 16:55:52 -05001853 .ndo_change_mtu = ibmvnic_change_mtu,
Thomas Falcon032c5e82015-12-21 11:26:06 -06001854};
1855
1856/* ethtool functions */
1857
Philippe Reynes8a433792017-01-07 22:37:29 +01001858static int ibmvnic_get_link_ksettings(struct net_device *netdev,
1859 struct ethtool_link_ksettings *cmd)
Thomas Falcon032c5e82015-12-21 11:26:06 -06001860{
Philippe Reynes8a433792017-01-07 22:37:29 +01001861 u32 supported, advertising;
1862
1863 supported = (SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg |
Thomas Falcon032c5e82015-12-21 11:26:06 -06001864 SUPPORTED_FIBRE);
Philippe Reynes8a433792017-01-07 22:37:29 +01001865 advertising = (ADVERTISED_1000baseT_Full | ADVERTISED_Autoneg |
Thomas Falcon032c5e82015-12-21 11:26:06 -06001866 ADVERTISED_FIBRE);
Philippe Reynes8a433792017-01-07 22:37:29 +01001867 cmd->base.speed = SPEED_1000;
1868 cmd->base.duplex = DUPLEX_FULL;
1869 cmd->base.port = PORT_FIBRE;
1870 cmd->base.phy_address = 0;
1871 cmd->base.autoneg = AUTONEG_ENABLE;
1872
1873 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
1874 supported);
1875 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
1876 advertising);
1877
Thomas Falcon032c5e82015-12-21 11:26:06 -06001878 return 0;
1879}
1880
1881static void ibmvnic_get_drvinfo(struct net_device *dev,
1882 struct ethtool_drvinfo *info)
1883{
1884 strlcpy(info->driver, ibmvnic_driver_name, sizeof(info->driver));
1885 strlcpy(info->version, IBMVNIC_DRIVER_VERSION, sizeof(info->version));
1886}
1887
1888static u32 ibmvnic_get_msglevel(struct net_device *netdev)
1889{
1890 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1891
1892 return adapter->msg_enable;
1893}
1894
1895static void ibmvnic_set_msglevel(struct net_device *netdev, u32 data)
1896{
1897 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1898
1899 adapter->msg_enable = data;
1900}
1901
1902static u32 ibmvnic_get_link(struct net_device *netdev)
1903{
1904 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1905
1906 /* Don't need to send a query because we request a logical link up at
1907 * init and then we wait for link state indications
1908 */
1909 return adapter->logical_link_state;
1910}
1911
1912static void ibmvnic_get_ringparam(struct net_device *netdev,
1913 struct ethtool_ringparam *ring)
1914{
John Allenbc131b32017-08-02 16:46:30 -05001915 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1916
1917 ring->rx_max_pending = adapter->max_rx_add_entries_per_subcrq;
1918 ring->tx_max_pending = adapter->max_tx_entries_per_subcrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001919 ring->rx_mini_max_pending = 0;
1920 ring->rx_jumbo_max_pending = 0;
John Allenbc131b32017-08-02 16:46:30 -05001921 ring->rx_pending = adapter->req_rx_add_entries_per_subcrq;
1922 ring->tx_pending = adapter->req_tx_entries_per_subcrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001923 ring->rx_mini_pending = 0;
1924 ring->rx_jumbo_pending = 0;
1925}
1926
John Allenc26eba02017-10-26 16:23:25 -05001927static int ibmvnic_set_ringparam(struct net_device *netdev,
1928 struct ethtool_ringparam *ring)
1929{
1930 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1931
1932 if (ring->rx_pending > adapter->max_rx_add_entries_per_subcrq ||
1933 ring->tx_pending > adapter->max_tx_entries_per_subcrq) {
1934 netdev_err(netdev, "Invalid request.\n");
1935 netdev_err(netdev, "Max tx buffers = %llu\n",
1936 adapter->max_rx_add_entries_per_subcrq);
1937 netdev_err(netdev, "Max rx buffers = %llu\n",
1938 adapter->max_tx_entries_per_subcrq);
1939 return -EINVAL;
1940 }
1941
1942 adapter->desired.rx_entries = ring->rx_pending;
1943 adapter->desired.tx_entries = ring->tx_pending;
1944
1945 return wait_for_reset(adapter);
1946}
1947
John Allenc2dbeb62017-08-02 16:47:17 -05001948static void ibmvnic_get_channels(struct net_device *netdev,
1949 struct ethtool_channels *channels)
1950{
1951 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1952
1953 channels->max_rx = adapter->max_rx_queues;
1954 channels->max_tx = adapter->max_tx_queues;
1955 channels->max_other = 0;
1956 channels->max_combined = 0;
1957 channels->rx_count = adapter->req_rx_queues;
1958 channels->tx_count = adapter->req_tx_queues;
1959 channels->other_count = 0;
1960 channels->combined_count = 0;
1961}
1962
John Allenc26eba02017-10-26 16:23:25 -05001963static int ibmvnic_set_channels(struct net_device *netdev,
1964 struct ethtool_channels *channels)
1965{
1966 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1967
1968 adapter->desired.rx_queues = channels->rx_count;
1969 adapter->desired.tx_queues = channels->tx_count;
1970
1971 return wait_for_reset(adapter);
1972}
1973
Thomas Falcon032c5e82015-12-21 11:26:06 -06001974static void ibmvnic_get_strings(struct net_device *dev, u32 stringset, u8 *data)
1975{
John Allen3d52b592017-08-02 16:44:14 -05001976 struct ibmvnic_adapter *adapter = netdev_priv(dev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001977 int i;
1978
1979 if (stringset != ETH_SS_STATS)
1980 return;
1981
1982 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++, data += ETH_GSTRING_LEN)
1983 memcpy(data, ibmvnic_stats[i].name, ETH_GSTRING_LEN);
John Allen3d52b592017-08-02 16:44:14 -05001984
1985 for (i = 0; i < adapter->req_tx_queues; i++) {
1986 snprintf(data, ETH_GSTRING_LEN, "tx%d_packets", i);
1987 data += ETH_GSTRING_LEN;
1988
1989 snprintf(data, ETH_GSTRING_LEN, "tx%d_bytes", i);
1990 data += ETH_GSTRING_LEN;
1991
1992 snprintf(data, ETH_GSTRING_LEN, "tx%d_dropped_packets", i);
1993 data += ETH_GSTRING_LEN;
1994 }
1995
1996 for (i = 0; i < adapter->req_rx_queues; i++) {
1997 snprintf(data, ETH_GSTRING_LEN, "rx%d_packets", i);
1998 data += ETH_GSTRING_LEN;
1999
2000 snprintf(data, ETH_GSTRING_LEN, "rx%d_bytes", i);
2001 data += ETH_GSTRING_LEN;
2002
2003 snprintf(data, ETH_GSTRING_LEN, "rx%d_interrupts", i);
2004 data += ETH_GSTRING_LEN;
2005 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002006}
2007
2008static int ibmvnic_get_sset_count(struct net_device *dev, int sset)
2009{
John Allen3d52b592017-08-02 16:44:14 -05002010 struct ibmvnic_adapter *adapter = netdev_priv(dev);
2011
Thomas Falcon032c5e82015-12-21 11:26:06 -06002012 switch (sset) {
2013 case ETH_SS_STATS:
John Allen3d52b592017-08-02 16:44:14 -05002014 return ARRAY_SIZE(ibmvnic_stats) +
2015 adapter->req_tx_queues * NUM_TX_STATS +
2016 adapter->req_rx_queues * NUM_RX_STATS;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002017 default:
2018 return -EOPNOTSUPP;
2019 }
2020}
2021
2022static void ibmvnic_get_ethtool_stats(struct net_device *dev,
2023 struct ethtool_stats *stats, u64 *data)
2024{
2025 struct ibmvnic_adapter *adapter = netdev_priv(dev);
2026 union ibmvnic_crq crq;
John Allen3d52b592017-08-02 16:44:14 -05002027 int i, j;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002028
2029 memset(&crq, 0, sizeof(crq));
2030 crq.request_statistics.first = IBMVNIC_CRQ_CMD;
2031 crq.request_statistics.cmd = REQUEST_STATISTICS;
2032 crq.request_statistics.ioba = cpu_to_be32(adapter->stats_token);
2033 crq.request_statistics.len =
2034 cpu_to_be32(sizeof(struct ibmvnic_statistics));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002035
2036 /* Wait for data to be written */
2037 init_completion(&adapter->stats_done);
Nathan Fontenotdb5d0b52017-02-10 13:45:05 -05002038 ibmvnic_send_crq(adapter, &crq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002039 wait_for_completion(&adapter->stats_done);
2040
2041 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++)
John Allen52da5c12017-08-02 16:45:28 -05002042 data[i] = be64_to_cpu(IBMVNIC_GET_STAT(adapter,
2043 ibmvnic_stats[i].offset));
John Allen3d52b592017-08-02 16:44:14 -05002044
2045 for (j = 0; j < adapter->req_tx_queues; j++) {
2046 data[i] = adapter->tx_stats_buffers[j].packets;
2047 i++;
2048 data[i] = adapter->tx_stats_buffers[j].bytes;
2049 i++;
2050 data[i] = adapter->tx_stats_buffers[j].dropped_packets;
2051 i++;
2052 }
2053
2054 for (j = 0; j < adapter->req_rx_queues; j++) {
2055 data[i] = adapter->rx_stats_buffers[j].packets;
2056 i++;
2057 data[i] = adapter->rx_stats_buffers[j].bytes;
2058 i++;
2059 data[i] = adapter->rx_stats_buffers[j].interrupts;
2060 i++;
2061 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002062}
2063
2064static const struct ethtool_ops ibmvnic_ethtool_ops = {
Thomas Falcon032c5e82015-12-21 11:26:06 -06002065 .get_drvinfo = ibmvnic_get_drvinfo,
2066 .get_msglevel = ibmvnic_get_msglevel,
2067 .set_msglevel = ibmvnic_set_msglevel,
2068 .get_link = ibmvnic_get_link,
2069 .get_ringparam = ibmvnic_get_ringparam,
John Allenc26eba02017-10-26 16:23:25 -05002070 .set_ringparam = ibmvnic_set_ringparam,
John Allenc2dbeb62017-08-02 16:47:17 -05002071 .get_channels = ibmvnic_get_channels,
John Allenc26eba02017-10-26 16:23:25 -05002072 .set_channels = ibmvnic_set_channels,
Thomas Falcon032c5e82015-12-21 11:26:06 -06002073 .get_strings = ibmvnic_get_strings,
2074 .get_sset_count = ibmvnic_get_sset_count,
2075 .get_ethtool_stats = ibmvnic_get_ethtool_stats,
Philippe Reynes8a433792017-01-07 22:37:29 +01002076 .get_link_ksettings = ibmvnic_get_link_ksettings,
Thomas Falcon032c5e82015-12-21 11:26:06 -06002077};
2078
2079/* Routines for managing CRQs/sCRQs */
2080
Nathan Fontenot57a49432017-05-26 10:31:12 -04002081static int reset_one_sub_crq_queue(struct ibmvnic_adapter *adapter,
2082 struct ibmvnic_sub_crq_queue *scrq)
2083{
2084 int rc;
2085
2086 if (scrq->irq) {
2087 free_irq(scrq->irq, scrq);
2088 irq_dispose_mapping(scrq->irq);
2089 scrq->irq = 0;
2090 }
2091
Thomas Falconc8b2ad02017-06-14 23:50:07 -05002092 memset(scrq->msgs, 0, 4 * PAGE_SIZE);
Nathan Fontenot57a49432017-05-26 10:31:12 -04002093 scrq->cur = 0;
2094
2095 rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
2096 4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
2097 return rc;
2098}
2099
2100static int reset_sub_crq_queues(struct ibmvnic_adapter *adapter)
2101{
2102 int i, rc;
2103
2104 for (i = 0; i < adapter->req_tx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002105 netdev_dbg(adapter->netdev, "Re-setting tx_scrq[%d]\n", i);
Nathan Fontenot57a49432017-05-26 10:31:12 -04002106 rc = reset_one_sub_crq_queue(adapter, adapter->tx_scrq[i]);
2107 if (rc)
2108 return rc;
2109 }
2110
2111 for (i = 0; i < adapter->req_rx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002112 netdev_dbg(adapter->netdev, "Re-setting rx_scrq[%d]\n", i);
Nathan Fontenot57a49432017-05-26 10:31:12 -04002113 rc = reset_one_sub_crq_queue(adapter, adapter->rx_scrq[i]);
2114 if (rc)
2115 return rc;
2116 }
2117
Nathan Fontenot57a49432017-05-26 10:31:12 -04002118 return rc;
2119}
2120
Thomas Falcon032c5e82015-12-21 11:26:06 -06002121static void release_sub_crq_queue(struct ibmvnic_adapter *adapter,
2122 struct ibmvnic_sub_crq_queue *scrq)
2123{
2124 struct device *dev = &adapter->vdev->dev;
2125 long rc;
2126
2127 netdev_dbg(adapter->netdev, "Releasing sub-CRQ\n");
2128
2129 /* Close the sub-crqs */
2130 do {
2131 rc = plpar_hcall_norets(H_FREE_SUB_CRQ,
2132 adapter->vdev->unit_address,
2133 scrq->crq_num);
2134 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
2135
Thomas Falconffa73852017-04-19 13:44:29 -04002136 if (rc) {
2137 netdev_err(adapter->netdev,
2138 "Failed to release sub-CRQ %16lx, rc = %ld\n",
2139 scrq->crq_num, rc);
2140 }
2141
Thomas Falcon032c5e82015-12-21 11:26:06 -06002142 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
2143 DMA_BIDIRECTIONAL);
2144 free_pages((unsigned long)scrq->msgs, 2);
2145 kfree(scrq);
2146}
2147
2148static struct ibmvnic_sub_crq_queue *init_sub_crq_queue(struct ibmvnic_adapter
2149 *adapter)
2150{
2151 struct device *dev = &adapter->vdev->dev;
2152 struct ibmvnic_sub_crq_queue *scrq;
2153 int rc;
2154
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04002155 scrq = kzalloc(sizeof(*scrq), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002156 if (!scrq)
2157 return NULL;
2158
Nathan Fontenot7f7adc52017-04-19 13:45:16 -04002159 scrq->msgs =
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04002160 (union sub_crq *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, 2);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002161 if (!scrq->msgs) {
2162 dev_warn(dev, "Couldn't allocate crq queue messages page\n");
2163 goto zero_page_failed;
2164 }
2165
2166 scrq->msg_token = dma_map_single(dev, scrq->msgs, 4 * PAGE_SIZE,
2167 DMA_BIDIRECTIONAL);
2168 if (dma_mapping_error(dev, scrq->msg_token)) {
2169 dev_warn(dev, "Couldn't map crq queue messages page\n");
2170 goto map_failed;
2171 }
2172
2173 rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
2174 4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
2175
2176 if (rc == H_RESOURCE)
2177 rc = ibmvnic_reset_crq(adapter);
2178
2179 if (rc == H_CLOSED) {
2180 dev_warn(dev, "Partner adapter not ready, waiting.\n");
2181 } else if (rc) {
2182 dev_warn(dev, "Error %d registering sub-crq\n", rc);
2183 goto reg_failed;
2184 }
2185
Thomas Falcon032c5e82015-12-21 11:26:06 -06002186 scrq->adapter = adapter;
2187 scrq->size = 4 * PAGE_SIZE / sizeof(*scrq->msgs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002188 spin_lock_init(&scrq->lock);
2189
2190 netdev_dbg(adapter->netdev,
2191 "sub-crq initialized, num %lx, hw_irq=%lx, irq=%x\n",
2192 scrq->crq_num, scrq->hw_irq, scrq->irq);
2193
2194 return scrq;
2195
Thomas Falcon032c5e82015-12-21 11:26:06 -06002196reg_failed:
2197 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
2198 DMA_BIDIRECTIONAL);
2199map_failed:
2200 free_pages((unsigned long)scrq->msgs, 2);
2201zero_page_failed:
2202 kfree(scrq);
2203
2204 return NULL;
2205}
2206
2207static void release_sub_crqs(struct ibmvnic_adapter *adapter)
2208{
2209 int i;
2210
2211 if (adapter->tx_scrq) {
Nathan Fontenotb5108882017-03-30 02:49:18 -04002212 for (i = 0; i < adapter->req_tx_queues; i++) {
2213 if (!adapter->tx_scrq[i])
2214 continue;
2215
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002216 netdev_dbg(adapter->netdev, "Releasing tx_scrq[%d]\n",
2217 i);
Nathan Fontenotb5108882017-03-30 02:49:18 -04002218 if (adapter->tx_scrq[i]->irq) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06002219 free_irq(adapter->tx_scrq[i]->irq,
2220 adapter->tx_scrq[i]);
Thomas Falcon88eb98a2016-07-06 15:35:16 -05002221 irq_dispose_mapping(adapter->tx_scrq[i]->irq);
Nathan Fontenotb5108882017-03-30 02:49:18 -04002222 adapter->tx_scrq[i]->irq = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002223 }
Nathan Fontenotb5108882017-03-30 02:49:18 -04002224
2225 release_sub_crq_queue(adapter, adapter->tx_scrq[i]);
2226 }
2227
Nathan Fontenot9501df32017-03-15 23:38:07 -04002228 kfree(adapter->tx_scrq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002229 adapter->tx_scrq = NULL;
2230 }
2231
2232 if (adapter->rx_scrq) {
Nathan Fontenotb5108882017-03-30 02:49:18 -04002233 for (i = 0; i < adapter->req_rx_queues; i++) {
2234 if (!adapter->rx_scrq[i])
2235 continue;
2236
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002237 netdev_dbg(adapter->netdev, "Releasing rx_scrq[%d]\n",
2238 i);
Nathan Fontenotb5108882017-03-30 02:49:18 -04002239 if (adapter->rx_scrq[i]->irq) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06002240 free_irq(adapter->rx_scrq[i]->irq,
2241 adapter->rx_scrq[i]);
Thomas Falcon88eb98a2016-07-06 15:35:16 -05002242 irq_dispose_mapping(adapter->rx_scrq[i]->irq);
Nathan Fontenotb5108882017-03-30 02:49:18 -04002243 adapter->rx_scrq[i]->irq = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002244 }
Nathan Fontenotb5108882017-03-30 02:49:18 -04002245
2246 release_sub_crq_queue(adapter, adapter->rx_scrq[i]);
2247 }
2248
Nathan Fontenot9501df32017-03-15 23:38:07 -04002249 kfree(adapter->rx_scrq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002250 adapter->rx_scrq = NULL;
2251 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002252}
2253
2254static int disable_scrq_irq(struct ibmvnic_adapter *adapter,
2255 struct ibmvnic_sub_crq_queue *scrq)
2256{
2257 struct device *dev = &adapter->vdev->dev;
2258 unsigned long rc;
2259
2260 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
2261 H_DISABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
2262 if (rc)
2263 dev_err(dev, "Couldn't disable scrq irq 0x%lx. rc=%ld\n",
2264 scrq->hw_irq, rc);
2265 return rc;
2266}
2267
2268static int enable_scrq_irq(struct ibmvnic_adapter *adapter,
2269 struct ibmvnic_sub_crq_queue *scrq)
2270{
2271 struct device *dev = &adapter->vdev->dev;
2272 unsigned long rc;
2273
2274 if (scrq->hw_irq > 0x100000000ULL) {
2275 dev_err(dev, "bad hw_irq = %lx\n", scrq->hw_irq);
2276 return 1;
2277 }
2278
2279 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
2280 H_ENABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
2281 if (rc)
2282 dev_err(dev, "Couldn't enable scrq irq 0x%lx. rc=%ld\n",
2283 scrq->hw_irq, rc);
2284 return rc;
2285}
2286
2287static int ibmvnic_complete_tx(struct ibmvnic_adapter *adapter,
2288 struct ibmvnic_sub_crq_queue *scrq)
2289{
2290 struct device *dev = &adapter->vdev->dev;
2291 struct ibmvnic_tx_buff *txbuff;
2292 union sub_crq *next;
2293 int index;
2294 int i, j;
Thomas Falconad7775d2016-04-01 17:20:34 -05002295 u8 first;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002296
2297restart_loop:
2298 while (pending_scrq(adapter, scrq)) {
2299 unsigned int pool = scrq->pool_index;
2300
2301 next = ibmvnic_next_scrq(adapter, scrq);
2302 for (i = 0; i < next->tx_comp.num_comps; i++) {
2303 if (next->tx_comp.rcs[i]) {
2304 dev_err(dev, "tx error %x\n",
2305 next->tx_comp.rcs[i]);
2306 continue;
2307 }
2308 index = be32_to_cpu(next->tx_comp.correlators[i]);
2309 txbuff = &adapter->tx_pool[pool].tx_buff[index];
2310
2311 for (j = 0; j < IBMVNIC_MAX_FRAGS_PER_CRQ; j++) {
2312 if (!txbuff->data_dma[j])
2313 continue;
2314
2315 txbuff->data_dma[j] = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002316 }
Thomas Falconad7775d2016-04-01 17:20:34 -05002317 /* if sub_crq was sent indirectly */
2318 first = txbuff->indir_arr[0].generic.first;
2319 if (first == IBMVNIC_CRQ_CMD) {
2320 dma_unmap_single(dev, txbuff->indir_dma,
2321 sizeof(txbuff->indir_arr),
2322 DMA_TO_DEVICE);
2323 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002324
Thomas Falcon142c0ac2017-03-05 12:18:41 -06002325 if (txbuff->last_frag) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06002326 dev_kfree_skb_any(txbuff->skb);
Nathan Fontenot7c3e7de2017-05-03 14:05:25 -04002327 txbuff->skb = NULL;
Thomas Falcon142c0ac2017-03-05 12:18:41 -06002328 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002329
2330 adapter->tx_pool[pool].free_map[adapter->tx_pool[pool].
2331 producer_index] = index;
2332 adapter->tx_pool[pool].producer_index =
2333 (adapter->tx_pool[pool].producer_index + 1) %
Thomas Falcon068d9f92017-03-05 12:18:42 -06002334 adapter->req_tx_entries_per_subcrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002335 }
2336 /* remove tx_comp scrq*/
2337 next->tx_comp.first = 0;
Nathan Fontenot7c3e7de2017-05-03 14:05:25 -04002338
2339 if (atomic_sub_return(next->tx_comp.num_comps, &scrq->used) <=
2340 (adapter->req_tx_entries_per_subcrq / 2) &&
2341 __netif_subqueue_stopped(adapter->netdev,
2342 scrq->pool_index)) {
2343 netif_wake_subqueue(adapter->netdev, scrq->pool_index);
2344 netdev_info(adapter->netdev, "Started queue %d\n",
2345 scrq->pool_index);
2346 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002347 }
2348
2349 enable_scrq_irq(adapter, scrq);
2350
2351 if (pending_scrq(adapter, scrq)) {
2352 disable_scrq_irq(adapter, scrq);
2353 goto restart_loop;
2354 }
2355
2356 return 0;
2357}
2358
2359static irqreturn_t ibmvnic_interrupt_tx(int irq, void *instance)
2360{
2361 struct ibmvnic_sub_crq_queue *scrq = instance;
2362 struct ibmvnic_adapter *adapter = scrq->adapter;
2363
2364 disable_scrq_irq(adapter, scrq);
2365 ibmvnic_complete_tx(adapter, scrq);
2366
2367 return IRQ_HANDLED;
2368}
2369
2370static irqreturn_t ibmvnic_interrupt_rx(int irq, void *instance)
2371{
2372 struct ibmvnic_sub_crq_queue *scrq = instance;
2373 struct ibmvnic_adapter *adapter = scrq->adapter;
2374
John Allen3d52b592017-08-02 16:44:14 -05002375 adapter->rx_stats_buffers[scrq->scrq_num].interrupts++;
2376
Thomas Falcon032c5e82015-12-21 11:26:06 -06002377 if (napi_schedule_prep(&adapter->napi[scrq->scrq_num])) {
2378 disable_scrq_irq(adapter, scrq);
2379 __napi_schedule(&adapter->napi[scrq->scrq_num]);
2380 }
2381
2382 return IRQ_HANDLED;
2383}
2384
Thomas Falconea22d512016-07-06 15:35:17 -05002385static int init_sub_crq_irqs(struct ibmvnic_adapter *adapter)
2386{
2387 struct device *dev = &adapter->vdev->dev;
2388 struct ibmvnic_sub_crq_queue *scrq;
2389 int i = 0, j = 0;
2390 int rc = 0;
2391
2392 for (i = 0; i < adapter->req_tx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002393 netdev_dbg(adapter->netdev, "Initializing tx_scrq[%d] irq\n",
2394 i);
Thomas Falconea22d512016-07-06 15:35:17 -05002395 scrq = adapter->tx_scrq[i];
2396 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
2397
Michael Ellerman99c17902016-09-10 19:59:05 +10002398 if (!scrq->irq) {
Thomas Falconea22d512016-07-06 15:35:17 -05002399 rc = -EINVAL;
2400 dev_err(dev, "Error mapping irq\n");
2401 goto req_tx_irq_failed;
2402 }
2403
2404 rc = request_irq(scrq->irq, ibmvnic_interrupt_tx,
2405 0, "ibmvnic_tx", scrq);
2406
2407 if (rc) {
2408 dev_err(dev, "Couldn't register tx irq 0x%x. rc=%d\n",
2409 scrq->irq, rc);
2410 irq_dispose_mapping(scrq->irq);
2411 goto req_rx_irq_failed;
2412 }
2413 }
2414
2415 for (i = 0; i < adapter->req_rx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002416 netdev_dbg(adapter->netdev, "Initializing rx_scrq[%d] irq\n",
2417 i);
Thomas Falconea22d512016-07-06 15:35:17 -05002418 scrq = adapter->rx_scrq[i];
2419 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
Michael Ellerman99c17902016-09-10 19:59:05 +10002420 if (!scrq->irq) {
Thomas Falconea22d512016-07-06 15:35:17 -05002421 rc = -EINVAL;
2422 dev_err(dev, "Error mapping irq\n");
2423 goto req_rx_irq_failed;
2424 }
2425 rc = request_irq(scrq->irq, ibmvnic_interrupt_rx,
2426 0, "ibmvnic_rx", scrq);
2427 if (rc) {
2428 dev_err(dev, "Couldn't register rx irq 0x%x. rc=%d\n",
2429 scrq->irq, rc);
2430 irq_dispose_mapping(scrq->irq);
2431 goto req_rx_irq_failed;
2432 }
2433 }
2434 return rc;
2435
2436req_rx_irq_failed:
Thomas Falcon8bf371e2016-10-27 12:28:52 -05002437 for (j = 0; j < i; j++) {
Thomas Falconea22d512016-07-06 15:35:17 -05002438 free_irq(adapter->rx_scrq[j]->irq, adapter->rx_scrq[j]);
2439 irq_dispose_mapping(adapter->rx_scrq[j]->irq);
Thomas Falcon8bf371e2016-10-27 12:28:52 -05002440 }
Thomas Falconea22d512016-07-06 15:35:17 -05002441 i = adapter->req_tx_queues;
2442req_tx_irq_failed:
Thomas Falcon8bf371e2016-10-27 12:28:52 -05002443 for (j = 0; j < i; j++) {
Thomas Falconea22d512016-07-06 15:35:17 -05002444 free_irq(adapter->tx_scrq[j]->irq, adapter->tx_scrq[j]);
2445 irq_dispose_mapping(adapter->rx_scrq[j]->irq);
Thomas Falcon8bf371e2016-10-27 12:28:52 -05002446 }
Nathan Fontenotb5108882017-03-30 02:49:18 -04002447 release_sub_crqs(adapter);
Thomas Falconea22d512016-07-06 15:35:17 -05002448 return rc;
2449}
2450
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002451static int init_sub_crqs(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002452{
2453 struct device *dev = &adapter->vdev->dev;
2454 struct ibmvnic_sub_crq_queue **allqueues;
2455 int registered_queues = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002456 int total_queues;
2457 int more = 0;
Thomas Falconea22d512016-07-06 15:35:17 -05002458 int i;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002459
Thomas Falcon032c5e82015-12-21 11:26:06 -06002460 total_queues = adapter->req_tx_queues + adapter->req_rx_queues;
2461
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04002462 allqueues = kcalloc(total_queues, sizeof(*allqueues), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002463 if (!allqueues)
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002464 return -1;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002465
2466 for (i = 0; i < total_queues; i++) {
2467 allqueues[i] = init_sub_crq_queue(adapter);
2468 if (!allqueues[i]) {
2469 dev_warn(dev, "Couldn't allocate all sub-crqs\n");
2470 break;
2471 }
2472 registered_queues++;
2473 }
2474
2475 /* Make sure we were able to register the minimum number of queues */
2476 if (registered_queues <
2477 adapter->min_tx_queues + adapter->min_rx_queues) {
2478 dev_err(dev, "Fatal: Couldn't init min number of sub-crqs\n");
2479 goto tx_failed;
2480 }
2481
2482 /* Distribute the failed allocated queues*/
2483 for (i = 0; i < total_queues - registered_queues + more ; i++) {
2484 netdev_dbg(adapter->netdev, "Reducing number of queues\n");
2485 switch (i % 3) {
2486 case 0:
2487 if (adapter->req_rx_queues > adapter->min_rx_queues)
2488 adapter->req_rx_queues--;
2489 else
2490 more++;
2491 break;
2492 case 1:
2493 if (adapter->req_tx_queues > adapter->min_tx_queues)
2494 adapter->req_tx_queues--;
2495 else
2496 more++;
2497 break;
2498 }
2499 }
2500
2501 adapter->tx_scrq = kcalloc(adapter->req_tx_queues,
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04002502 sizeof(*adapter->tx_scrq), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002503 if (!adapter->tx_scrq)
2504 goto tx_failed;
2505
2506 for (i = 0; i < adapter->req_tx_queues; i++) {
2507 adapter->tx_scrq[i] = allqueues[i];
2508 adapter->tx_scrq[i]->pool_index = i;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002509 }
2510
2511 adapter->rx_scrq = kcalloc(adapter->req_rx_queues,
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04002512 sizeof(*adapter->rx_scrq), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002513 if (!adapter->rx_scrq)
2514 goto rx_failed;
2515
2516 for (i = 0; i < adapter->req_rx_queues; i++) {
2517 adapter->rx_scrq[i] = allqueues[i + adapter->req_tx_queues];
2518 adapter->rx_scrq[i]->scrq_num = i;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002519 }
2520
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002521 kfree(allqueues);
2522 return 0;
2523
2524rx_failed:
2525 kfree(adapter->tx_scrq);
2526 adapter->tx_scrq = NULL;
2527tx_failed:
2528 for (i = 0; i < registered_queues; i++)
2529 release_sub_crq_queue(adapter, allqueues[i]);
2530 kfree(allqueues);
2531 return -1;
2532}
2533
2534static void ibmvnic_send_req_caps(struct ibmvnic_adapter *adapter, int retry)
2535{
2536 struct device *dev = &adapter->vdev->dev;
2537 union ibmvnic_crq crq;
John Allenc26eba02017-10-26 16:23:25 -05002538 int max_entries;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002539
2540 if (!retry) {
2541 /* Sub-CRQ entries are 32 byte long */
2542 int entries_page = 4 * PAGE_SIZE / (sizeof(u64) * 4);
2543
2544 if (adapter->min_tx_entries_per_subcrq > entries_page ||
2545 adapter->min_rx_add_entries_per_subcrq > entries_page) {
2546 dev_err(dev, "Fatal, invalid entries per sub-crq\n");
2547 return;
2548 }
2549
John Allenc26eba02017-10-26 16:23:25 -05002550 if (adapter->desired.mtu)
2551 adapter->req_mtu = adapter->desired.mtu;
2552 else
2553 adapter->req_mtu = adapter->netdev->mtu + ETH_HLEN;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002554
John Allenc26eba02017-10-26 16:23:25 -05002555 if (!adapter->desired.tx_entries)
2556 adapter->desired.tx_entries =
2557 adapter->max_tx_entries_per_subcrq;
2558 if (!adapter->desired.rx_entries)
2559 adapter->desired.rx_entries =
2560 adapter->max_rx_add_entries_per_subcrq;
2561
2562 max_entries = IBMVNIC_MAX_LTB_SIZE /
2563 (adapter->req_mtu + IBMVNIC_BUFFER_HLEN);
2564
2565 if ((adapter->req_mtu + IBMVNIC_BUFFER_HLEN) *
2566 adapter->desired.tx_entries > IBMVNIC_MAX_LTB_SIZE) {
2567 adapter->desired.tx_entries = max_entries;
2568 }
2569
2570 if ((adapter->req_mtu + IBMVNIC_BUFFER_HLEN) *
2571 adapter->desired.rx_entries > IBMVNIC_MAX_LTB_SIZE) {
2572 adapter->desired.rx_entries = max_entries;
2573 }
2574
2575 if (adapter->desired.tx_entries)
2576 adapter->req_tx_entries_per_subcrq =
2577 adapter->desired.tx_entries;
2578 else
2579 adapter->req_tx_entries_per_subcrq =
2580 adapter->max_tx_entries_per_subcrq;
2581
2582 if (adapter->desired.rx_entries)
2583 adapter->req_rx_add_entries_per_subcrq =
2584 adapter->desired.rx_entries;
2585 else
2586 adapter->req_rx_add_entries_per_subcrq =
2587 adapter->max_rx_add_entries_per_subcrq;
2588
2589 if (adapter->desired.tx_queues)
2590 adapter->req_tx_queues =
2591 adapter->desired.tx_queues;
2592 else
2593 adapter->req_tx_queues =
2594 adapter->opt_tx_comp_sub_queues;
2595
2596 if (adapter->desired.rx_queues)
2597 adapter->req_rx_queues =
2598 adapter->desired.rx_queues;
2599 else
2600 adapter->req_rx_queues =
2601 adapter->opt_rx_comp_queues;
2602
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002603 adapter->req_rx_add_queues = adapter->max_rx_add_queues;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002604 }
2605
Thomas Falcon032c5e82015-12-21 11:26:06 -06002606 memset(&crq, 0, sizeof(crq));
2607 crq.request_capability.first = IBMVNIC_CRQ_CMD;
2608 crq.request_capability.cmd = REQUEST_CAPABILITY;
2609
2610 crq.request_capability.capability = cpu_to_be16(REQ_TX_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06002611 crq.request_capability.number = cpu_to_be64(adapter->req_tx_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06002612 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002613 ibmvnic_send_crq(adapter, &crq);
2614
2615 crq.request_capability.capability = cpu_to_be16(REQ_RX_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06002616 crq.request_capability.number = cpu_to_be64(adapter->req_rx_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06002617 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002618 ibmvnic_send_crq(adapter, &crq);
2619
2620 crq.request_capability.capability = cpu_to_be16(REQ_RX_ADD_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06002621 crq.request_capability.number = cpu_to_be64(adapter->req_rx_add_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06002622 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002623 ibmvnic_send_crq(adapter, &crq);
2624
2625 crq.request_capability.capability =
2626 cpu_to_be16(REQ_TX_ENTRIES_PER_SUBCRQ);
2627 crq.request_capability.number =
Thomas Falconde89e852016-03-01 10:20:09 -06002628 cpu_to_be64(adapter->req_tx_entries_per_subcrq);
Thomas Falcon901e0402017-02-15 12:17:59 -06002629 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002630 ibmvnic_send_crq(adapter, &crq);
2631
2632 crq.request_capability.capability =
2633 cpu_to_be16(REQ_RX_ADD_ENTRIES_PER_SUBCRQ);
2634 crq.request_capability.number =
Thomas Falconde89e852016-03-01 10:20:09 -06002635 cpu_to_be64(adapter->req_rx_add_entries_per_subcrq);
Thomas Falcon901e0402017-02-15 12:17:59 -06002636 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002637 ibmvnic_send_crq(adapter, &crq);
2638
2639 crq.request_capability.capability = cpu_to_be16(REQ_MTU);
Thomas Falconde89e852016-03-01 10:20:09 -06002640 crq.request_capability.number = cpu_to_be64(adapter->req_mtu);
Thomas Falcon901e0402017-02-15 12:17:59 -06002641 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002642 ibmvnic_send_crq(adapter, &crq);
2643
2644 if (adapter->netdev->flags & IFF_PROMISC) {
2645 if (adapter->promisc_supported) {
2646 crq.request_capability.capability =
2647 cpu_to_be16(PROMISC_REQUESTED);
Thomas Falconde89e852016-03-01 10:20:09 -06002648 crq.request_capability.number = cpu_to_be64(1);
Thomas Falcon901e0402017-02-15 12:17:59 -06002649 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002650 ibmvnic_send_crq(adapter, &crq);
2651 }
2652 } else {
2653 crq.request_capability.capability =
2654 cpu_to_be16(PROMISC_REQUESTED);
Thomas Falconde89e852016-03-01 10:20:09 -06002655 crq.request_capability.number = cpu_to_be64(0);
Thomas Falcon901e0402017-02-15 12:17:59 -06002656 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002657 ibmvnic_send_crq(adapter, &crq);
2658 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002659}
2660
2661static int pending_scrq(struct ibmvnic_adapter *adapter,
2662 struct ibmvnic_sub_crq_queue *scrq)
2663{
2664 union sub_crq *entry = &scrq->msgs[scrq->cur];
2665
Thomas Falcon1cf9cc72017-06-14 23:50:08 -05002666 if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002667 return 1;
2668 else
2669 return 0;
2670}
2671
2672static union sub_crq *ibmvnic_next_scrq(struct ibmvnic_adapter *adapter,
2673 struct ibmvnic_sub_crq_queue *scrq)
2674{
2675 union sub_crq *entry;
2676 unsigned long flags;
2677
2678 spin_lock_irqsave(&scrq->lock, flags);
2679 entry = &scrq->msgs[scrq->cur];
2680 if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP) {
2681 if (++scrq->cur == scrq->size)
2682 scrq->cur = 0;
2683 } else {
2684 entry = NULL;
2685 }
2686 spin_unlock_irqrestore(&scrq->lock, flags);
2687
2688 return entry;
2689}
2690
2691static union ibmvnic_crq *ibmvnic_next_crq(struct ibmvnic_adapter *adapter)
2692{
2693 struct ibmvnic_crq_queue *queue = &adapter->crq;
2694 union ibmvnic_crq *crq;
2695
2696 crq = &queue->msgs[queue->cur];
2697 if (crq->generic.first & IBMVNIC_CRQ_CMD_RSP) {
2698 if (++queue->cur == queue->size)
2699 queue->cur = 0;
2700 } else {
2701 crq = NULL;
2702 }
2703
2704 return crq;
2705}
2706
2707static int send_subcrq(struct ibmvnic_adapter *adapter, u64 remote_handle,
2708 union sub_crq *sub_crq)
2709{
2710 unsigned int ua = adapter->vdev->unit_address;
2711 struct device *dev = &adapter->vdev->dev;
2712 u64 *u64_crq = (u64 *)sub_crq;
2713 int rc;
2714
2715 netdev_dbg(adapter->netdev,
2716 "Sending sCRQ %016lx: %016lx %016lx %016lx %016lx\n",
2717 (unsigned long int)cpu_to_be64(remote_handle),
2718 (unsigned long int)cpu_to_be64(u64_crq[0]),
2719 (unsigned long int)cpu_to_be64(u64_crq[1]),
2720 (unsigned long int)cpu_to_be64(u64_crq[2]),
2721 (unsigned long int)cpu_to_be64(u64_crq[3]));
2722
2723 /* Make sure the hypervisor sees the complete request */
2724 mb();
2725
2726 rc = plpar_hcall_norets(H_SEND_SUB_CRQ, ua,
2727 cpu_to_be64(remote_handle),
2728 cpu_to_be64(u64_crq[0]),
2729 cpu_to_be64(u64_crq[1]),
2730 cpu_to_be64(u64_crq[2]),
2731 cpu_to_be64(u64_crq[3]));
2732
2733 if (rc) {
2734 if (rc == H_CLOSED)
2735 dev_warn(dev, "CRQ Queue closed\n");
2736 dev_err(dev, "Send error (rc=%d)\n", rc);
2737 }
2738
2739 return rc;
2740}
2741
Thomas Falconad7775d2016-04-01 17:20:34 -05002742static int send_subcrq_indirect(struct ibmvnic_adapter *adapter,
2743 u64 remote_handle, u64 ioba, u64 num_entries)
2744{
2745 unsigned int ua = adapter->vdev->unit_address;
2746 struct device *dev = &adapter->vdev->dev;
2747 int rc;
2748
2749 /* Make sure the hypervisor sees the complete request */
2750 mb();
2751 rc = plpar_hcall_norets(H_SEND_SUB_CRQ_INDIRECT, ua,
2752 cpu_to_be64(remote_handle),
2753 ioba, num_entries);
2754
2755 if (rc) {
2756 if (rc == H_CLOSED)
2757 dev_warn(dev, "CRQ Queue closed\n");
2758 dev_err(dev, "Send (indirect) error (rc=%d)\n", rc);
2759 }
2760
2761 return rc;
2762}
2763
Thomas Falcon032c5e82015-12-21 11:26:06 -06002764static int ibmvnic_send_crq(struct ibmvnic_adapter *adapter,
2765 union ibmvnic_crq *crq)
2766{
2767 unsigned int ua = adapter->vdev->unit_address;
2768 struct device *dev = &adapter->vdev->dev;
2769 u64 *u64_crq = (u64 *)crq;
2770 int rc;
2771
2772 netdev_dbg(adapter->netdev, "Sending CRQ: %016lx %016lx\n",
2773 (unsigned long int)cpu_to_be64(u64_crq[0]),
2774 (unsigned long int)cpu_to_be64(u64_crq[1]));
2775
2776 /* Make sure the hypervisor sees the complete request */
2777 mb();
2778
2779 rc = plpar_hcall_norets(H_SEND_CRQ, ua,
2780 cpu_to_be64(u64_crq[0]),
2781 cpu_to_be64(u64_crq[1]));
2782
2783 if (rc) {
2784 if (rc == H_CLOSED)
2785 dev_warn(dev, "CRQ Queue closed\n");
2786 dev_warn(dev, "Send error (rc=%d)\n", rc);
2787 }
2788
2789 return rc;
2790}
2791
2792static int ibmvnic_send_crq_init(struct ibmvnic_adapter *adapter)
2793{
2794 union ibmvnic_crq crq;
2795
2796 memset(&crq, 0, sizeof(crq));
2797 crq.generic.first = IBMVNIC_CRQ_INIT_CMD;
2798 crq.generic.cmd = IBMVNIC_CRQ_INIT;
2799 netdev_dbg(adapter->netdev, "Sending CRQ init\n");
2800
2801 return ibmvnic_send_crq(adapter, &crq);
2802}
2803
Thomas Falcon032c5e82015-12-21 11:26:06 -06002804static int send_version_xchg(struct ibmvnic_adapter *adapter)
2805{
2806 union ibmvnic_crq crq;
2807
2808 memset(&crq, 0, sizeof(crq));
2809 crq.version_exchange.first = IBMVNIC_CRQ_CMD;
2810 crq.version_exchange.cmd = VERSION_EXCHANGE;
2811 crq.version_exchange.version = cpu_to_be16(ibmvnic_version);
2812
2813 return ibmvnic_send_crq(adapter, &crq);
2814}
2815
2816static void send_login(struct ibmvnic_adapter *adapter)
2817{
2818 struct ibmvnic_login_rsp_buffer *login_rsp_buffer;
2819 struct ibmvnic_login_buffer *login_buffer;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002820 struct device *dev = &adapter->vdev->dev;
2821 dma_addr_t rsp_buffer_token;
2822 dma_addr_t buffer_token;
2823 size_t rsp_buffer_size;
2824 union ibmvnic_crq crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002825 size_t buffer_size;
2826 __be64 *tx_list_p;
2827 __be64 *rx_list_p;
2828 int i;
2829
2830 buffer_size =
2831 sizeof(struct ibmvnic_login_buffer) +
2832 sizeof(u64) * (adapter->req_tx_queues + adapter->req_rx_queues);
2833
2834 login_buffer = kmalloc(buffer_size, GFP_ATOMIC);
2835 if (!login_buffer)
2836 goto buf_alloc_failed;
2837
2838 buffer_token = dma_map_single(dev, login_buffer, buffer_size,
2839 DMA_TO_DEVICE);
2840 if (dma_mapping_error(dev, buffer_token)) {
2841 dev_err(dev, "Couldn't map login buffer\n");
2842 goto buf_map_failed;
2843 }
2844
John Allen498cd8e2016-04-06 11:49:55 -05002845 rsp_buffer_size = sizeof(struct ibmvnic_login_rsp_buffer) +
2846 sizeof(u64) * adapter->req_tx_queues +
2847 sizeof(u64) * adapter->req_rx_queues +
2848 sizeof(u64) * adapter->req_rx_queues +
2849 sizeof(u8) * IBMVNIC_TX_DESC_VERSIONS;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002850
2851 login_rsp_buffer = kmalloc(rsp_buffer_size, GFP_ATOMIC);
2852 if (!login_rsp_buffer)
2853 goto buf_rsp_alloc_failed;
2854
2855 rsp_buffer_token = dma_map_single(dev, login_rsp_buffer,
2856 rsp_buffer_size, DMA_FROM_DEVICE);
2857 if (dma_mapping_error(dev, rsp_buffer_token)) {
2858 dev_err(dev, "Couldn't map login rsp buffer\n");
2859 goto buf_rsp_map_failed;
2860 }
Nathan Fontenot661a2622017-04-19 13:44:58 -04002861
Thomas Falcon032c5e82015-12-21 11:26:06 -06002862 adapter->login_buf = login_buffer;
2863 adapter->login_buf_token = buffer_token;
2864 adapter->login_buf_sz = buffer_size;
2865 adapter->login_rsp_buf = login_rsp_buffer;
2866 adapter->login_rsp_buf_token = rsp_buffer_token;
2867 adapter->login_rsp_buf_sz = rsp_buffer_size;
2868
2869 login_buffer->len = cpu_to_be32(buffer_size);
2870 login_buffer->version = cpu_to_be32(INITIAL_VERSION_LB);
2871 login_buffer->num_txcomp_subcrqs = cpu_to_be32(adapter->req_tx_queues);
2872 login_buffer->off_txcomp_subcrqs =
2873 cpu_to_be32(sizeof(struct ibmvnic_login_buffer));
2874 login_buffer->num_rxcomp_subcrqs = cpu_to_be32(adapter->req_rx_queues);
2875 login_buffer->off_rxcomp_subcrqs =
2876 cpu_to_be32(sizeof(struct ibmvnic_login_buffer) +
2877 sizeof(u64) * adapter->req_tx_queues);
2878 login_buffer->login_rsp_ioba = cpu_to_be32(rsp_buffer_token);
2879 login_buffer->login_rsp_len = cpu_to_be32(rsp_buffer_size);
2880
2881 tx_list_p = (__be64 *)((char *)login_buffer +
2882 sizeof(struct ibmvnic_login_buffer));
2883 rx_list_p = (__be64 *)((char *)login_buffer +
2884 sizeof(struct ibmvnic_login_buffer) +
2885 sizeof(u64) * adapter->req_tx_queues);
2886
2887 for (i = 0; i < adapter->req_tx_queues; i++) {
2888 if (adapter->tx_scrq[i]) {
2889 tx_list_p[i] = cpu_to_be64(adapter->tx_scrq[i]->
2890 crq_num);
2891 }
2892 }
2893
2894 for (i = 0; i < adapter->req_rx_queues; i++) {
2895 if (adapter->rx_scrq[i]) {
2896 rx_list_p[i] = cpu_to_be64(adapter->rx_scrq[i]->
2897 crq_num);
2898 }
2899 }
2900
2901 netdev_dbg(adapter->netdev, "Login Buffer:\n");
2902 for (i = 0; i < (adapter->login_buf_sz - 1) / 8 + 1; i++) {
2903 netdev_dbg(adapter->netdev, "%016lx\n",
2904 ((unsigned long int *)(adapter->login_buf))[i]);
2905 }
2906
2907 memset(&crq, 0, sizeof(crq));
2908 crq.login.first = IBMVNIC_CRQ_CMD;
2909 crq.login.cmd = LOGIN;
2910 crq.login.ioba = cpu_to_be32(buffer_token);
2911 crq.login.len = cpu_to_be32(buffer_size);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002912 ibmvnic_send_crq(adapter, &crq);
2913
2914 return;
2915
Thomas Falcon032c5e82015-12-21 11:26:06 -06002916buf_rsp_map_failed:
2917 kfree(login_rsp_buffer);
2918buf_rsp_alloc_failed:
2919 dma_unmap_single(dev, buffer_token, buffer_size, DMA_TO_DEVICE);
2920buf_map_failed:
2921 kfree(login_buffer);
2922buf_alloc_failed:
2923 return;
2924}
2925
2926static void send_request_map(struct ibmvnic_adapter *adapter, dma_addr_t addr,
2927 u32 len, u8 map_id)
2928{
2929 union ibmvnic_crq crq;
2930
2931 memset(&crq, 0, sizeof(crq));
2932 crq.request_map.first = IBMVNIC_CRQ_CMD;
2933 crq.request_map.cmd = REQUEST_MAP;
2934 crq.request_map.map_id = map_id;
2935 crq.request_map.ioba = cpu_to_be32(addr);
2936 crq.request_map.len = cpu_to_be32(len);
2937 ibmvnic_send_crq(adapter, &crq);
2938}
2939
2940static void send_request_unmap(struct ibmvnic_adapter *adapter, u8 map_id)
2941{
2942 union ibmvnic_crq crq;
2943
2944 memset(&crq, 0, sizeof(crq));
2945 crq.request_unmap.first = IBMVNIC_CRQ_CMD;
2946 crq.request_unmap.cmd = REQUEST_UNMAP;
2947 crq.request_unmap.map_id = map_id;
2948 ibmvnic_send_crq(adapter, &crq);
2949}
2950
2951static void send_map_query(struct ibmvnic_adapter *adapter)
2952{
2953 union ibmvnic_crq crq;
2954
2955 memset(&crq, 0, sizeof(crq));
2956 crq.query_map.first = IBMVNIC_CRQ_CMD;
2957 crq.query_map.cmd = QUERY_MAP;
2958 ibmvnic_send_crq(adapter, &crq);
2959}
2960
2961/* Send a series of CRQs requesting various capabilities of the VNIC server */
2962static void send_cap_queries(struct ibmvnic_adapter *adapter)
2963{
2964 union ibmvnic_crq crq;
2965
Thomas Falcon901e0402017-02-15 12:17:59 -06002966 atomic_set(&adapter->running_cap_crqs, 0);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002967 memset(&crq, 0, sizeof(crq));
2968 crq.query_capability.first = IBMVNIC_CRQ_CMD;
2969 crq.query_capability.cmd = QUERY_CAPABILITY;
2970
2971 crq.query_capability.capability = cpu_to_be16(MIN_TX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002972 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002973 ibmvnic_send_crq(adapter, &crq);
2974
2975 crq.query_capability.capability = cpu_to_be16(MIN_RX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002976 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002977 ibmvnic_send_crq(adapter, &crq);
2978
2979 crq.query_capability.capability = cpu_to_be16(MIN_RX_ADD_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002980 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002981 ibmvnic_send_crq(adapter, &crq);
2982
2983 crq.query_capability.capability = cpu_to_be16(MAX_TX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002984 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002985 ibmvnic_send_crq(adapter, &crq);
2986
2987 crq.query_capability.capability = cpu_to_be16(MAX_RX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002988 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002989 ibmvnic_send_crq(adapter, &crq);
2990
2991 crq.query_capability.capability = cpu_to_be16(MAX_RX_ADD_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002992 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002993 ibmvnic_send_crq(adapter, &crq);
2994
2995 crq.query_capability.capability =
2996 cpu_to_be16(MIN_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06002997 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002998 ibmvnic_send_crq(adapter, &crq);
2999
3000 crq.query_capability.capability =
3001 cpu_to_be16(MIN_RX_ADD_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06003002 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003003 ibmvnic_send_crq(adapter, &crq);
3004
3005 crq.query_capability.capability =
3006 cpu_to_be16(MAX_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06003007 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003008 ibmvnic_send_crq(adapter, &crq);
3009
3010 crq.query_capability.capability =
3011 cpu_to_be16(MAX_RX_ADD_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06003012 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003013 ibmvnic_send_crq(adapter, &crq);
3014
3015 crq.query_capability.capability = cpu_to_be16(TCP_IP_OFFLOAD);
Thomas Falcon901e0402017-02-15 12:17:59 -06003016 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003017 ibmvnic_send_crq(adapter, &crq);
3018
3019 crq.query_capability.capability = cpu_to_be16(PROMISC_SUPPORTED);
Thomas Falcon901e0402017-02-15 12:17:59 -06003020 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003021 ibmvnic_send_crq(adapter, &crq);
3022
3023 crq.query_capability.capability = cpu_to_be16(MIN_MTU);
Thomas Falcon901e0402017-02-15 12:17:59 -06003024 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003025 ibmvnic_send_crq(adapter, &crq);
3026
3027 crq.query_capability.capability = cpu_to_be16(MAX_MTU);
Thomas Falcon901e0402017-02-15 12:17:59 -06003028 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003029 ibmvnic_send_crq(adapter, &crq);
3030
3031 crq.query_capability.capability = cpu_to_be16(MAX_MULTICAST_FILTERS);
Thomas Falcon901e0402017-02-15 12:17:59 -06003032 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003033 ibmvnic_send_crq(adapter, &crq);
3034
3035 crq.query_capability.capability = cpu_to_be16(VLAN_HEADER_INSERTION);
Thomas Falcon901e0402017-02-15 12:17:59 -06003036 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003037 ibmvnic_send_crq(adapter, &crq);
3038
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04003039 crq.query_capability.capability = cpu_to_be16(RX_VLAN_HEADER_INSERTION);
3040 atomic_inc(&adapter->running_cap_crqs);
3041 ibmvnic_send_crq(adapter, &crq);
3042
Thomas Falcon032c5e82015-12-21 11:26:06 -06003043 crq.query_capability.capability = cpu_to_be16(MAX_TX_SG_ENTRIES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003044 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003045 ibmvnic_send_crq(adapter, &crq);
3046
3047 crq.query_capability.capability = cpu_to_be16(RX_SG_SUPPORTED);
Thomas Falcon901e0402017-02-15 12:17:59 -06003048 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003049 ibmvnic_send_crq(adapter, &crq);
3050
3051 crq.query_capability.capability = cpu_to_be16(OPT_TX_COMP_SUB_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003052 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003053 ibmvnic_send_crq(adapter, &crq);
3054
3055 crq.query_capability.capability = cpu_to_be16(OPT_RX_COMP_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003056 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003057 ibmvnic_send_crq(adapter, &crq);
3058
3059 crq.query_capability.capability =
3060 cpu_to_be16(OPT_RX_BUFADD_Q_PER_RX_COMP_Q);
Thomas Falcon901e0402017-02-15 12:17:59 -06003061 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003062 ibmvnic_send_crq(adapter, &crq);
3063
3064 crq.query_capability.capability =
3065 cpu_to_be16(OPT_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06003066 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003067 ibmvnic_send_crq(adapter, &crq);
3068
3069 crq.query_capability.capability =
3070 cpu_to_be16(OPT_RXBA_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06003071 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003072 ibmvnic_send_crq(adapter, &crq);
3073
3074 crq.query_capability.capability = cpu_to_be16(TX_RX_DESC_REQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06003075 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003076 ibmvnic_send_crq(adapter, &crq);
3077}
3078
3079static void handle_query_ip_offload_rsp(struct ibmvnic_adapter *adapter)
3080{
3081 struct device *dev = &adapter->vdev->dev;
3082 struct ibmvnic_query_ip_offload_buffer *buf = &adapter->ip_offload_buf;
3083 union ibmvnic_crq crq;
3084 int i;
3085
3086 dma_unmap_single(dev, adapter->ip_offload_tok,
3087 sizeof(adapter->ip_offload_buf), DMA_FROM_DEVICE);
3088
3089 netdev_dbg(adapter->netdev, "Query IP Offload Buffer:\n");
3090 for (i = 0; i < (sizeof(adapter->ip_offload_buf) - 1) / 8 + 1; i++)
3091 netdev_dbg(adapter->netdev, "%016lx\n",
3092 ((unsigned long int *)(buf))[i]);
3093
3094 netdev_dbg(adapter->netdev, "ipv4_chksum = %d\n", buf->ipv4_chksum);
3095 netdev_dbg(adapter->netdev, "ipv6_chksum = %d\n", buf->ipv6_chksum);
3096 netdev_dbg(adapter->netdev, "tcp_ipv4_chksum = %d\n",
3097 buf->tcp_ipv4_chksum);
3098 netdev_dbg(adapter->netdev, "tcp_ipv6_chksum = %d\n",
3099 buf->tcp_ipv6_chksum);
3100 netdev_dbg(adapter->netdev, "udp_ipv4_chksum = %d\n",
3101 buf->udp_ipv4_chksum);
3102 netdev_dbg(adapter->netdev, "udp_ipv6_chksum = %d\n",
3103 buf->udp_ipv6_chksum);
3104 netdev_dbg(adapter->netdev, "large_tx_ipv4 = %d\n",
3105 buf->large_tx_ipv4);
3106 netdev_dbg(adapter->netdev, "large_tx_ipv6 = %d\n",
3107 buf->large_tx_ipv6);
3108 netdev_dbg(adapter->netdev, "large_rx_ipv4 = %d\n",
3109 buf->large_rx_ipv4);
3110 netdev_dbg(adapter->netdev, "large_rx_ipv6 = %d\n",
3111 buf->large_rx_ipv6);
3112 netdev_dbg(adapter->netdev, "max_ipv4_hdr_sz = %d\n",
3113 buf->max_ipv4_header_size);
3114 netdev_dbg(adapter->netdev, "max_ipv6_hdr_sz = %d\n",
3115 buf->max_ipv6_header_size);
3116 netdev_dbg(adapter->netdev, "max_tcp_hdr_size = %d\n",
3117 buf->max_tcp_header_size);
3118 netdev_dbg(adapter->netdev, "max_udp_hdr_size = %d\n",
3119 buf->max_udp_header_size);
3120 netdev_dbg(adapter->netdev, "max_large_tx_size = %d\n",
3121 buf->max_large_tx_size);
3122 netdev_dbg(adapter->netdev, "max_large_rx_size = %d\n",
3123 buf->max_large_rx_size);
3124 netdev_dbg(adapter->netdev, "ipv6_ext_hdr = %d\n",
3125 buf->ipv6_extension_header);
3126 netdev_dbg(adapter->netdev, "tcp_pseudosum_req = %d\n",
3127 buf->tcp_pseudosum_req);
3128 netdev_dbg(adapter->netdev, "num_ipv6_ext_hd = %d\n",
3129 buf->num_ipv6_ext_headers);
3130 netdev_dbg(adapter->netdev, "off_ipv6_ext_hd = %d\n",
3131 buf->off_ipv6_ext_headers);
3132
3133 adapter->ip_offload_ctrl_tok =
3134 dma_map_single(dev, &adapter->ip_offload_ctrl,
3135 sizeof(adapter->ip_offload_ctrl), DMA_TO_DEVICE);
3136
3137 if (dma_mapping_error(dev, adapter->ip_offload_ctrl_tok)) {
3138 dev_err(dev, "Couldn't map ip offload control buffer\n");
3139 return;
3140 }
3141
3142 adapter->ip_offload_ctrl.version = cpu_to_be32(INITIAL_VERSION_IOB);
3143 adapter->ip_offload_ctrl.tcp_ipv4_chksum = buf->tcp_ipv4_chksum;
3144 adapter->ip_offload_ctrl.udp_ipv4_chksum = buf->udp_ipv4_chksum;
3145 adapter->ip_offload_ctrl.tcp_ipv6_chksum = buf->tcp_ipv6_chksum;
3146 adapter->ip_offload_ctrl.udp_ipv6_chksum = buf->udp_ipv6_chksum;
Thomas Falconfdb06102017-10-17 12:36:55 -05003147 adapter->ip_offload_ctrl.large_tx_ipv4 = buf->large_tx_ipv4;
3148 adapter->ip_offload_ctrl.large_tx_ipv6 = buf->large_tx_ipv6;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003149
Thomas Falconfdb06102017-10-17 12:36:55 -05003150 /* large_rx disabled for now, additional features needed */
Thomas Falcon032c5e82015-12-21 11:26:06 -06003151 adapter->ip_offload_ctrl.large_rx_ipv4 = 0;
3152 adapter->ip_offload_ctrl.large_rx_ipv6 = 0;
3153
Thomas Falcon15482052017-10-17 12:36:54 -05003154 adapter->netdev->features = NETIF_F_SG | NETIF_F_GSO;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003155
3156 if (buf->tcp_ipv4_chksum || buf->udp_ipv4_chksum)
3157 adapter->netdev->features |= NETIF_F_IP_CSUM;
3158
3159 if (buf->tcp_ipv6_chksum || buf->udp_ipv6_chksum)
3160 adapter->netdev->features |= NETIF_F_IPV6_CSUM;
3161
Thomas Falcon9be02cd2016-04-01 17:20:35 -05003162 if ((adapter->netdev->features &
3163 (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)))
3164 adapter->netdev->features |= NETIF_F_RXCSUM;
3165
Thomas Falconfdb06102017-10-17 12:36:55 -05003166 if (buf->large_tx_ipv4)
3167 adapter->netdev->features |= NETIF_F_TSO;
3168 if (buf->large_tx_ipv6)
3169 adapter->netdev->features |= NETIF_F_TSO6;
3170
Thomas Falconaa0bf852017-10-17 12:36:56 -05003171 adapter->netdev->hw_features |= adapter->netdev->features;
3172
Thomas Falcon032c5e82015-12-21 11:26:06 -06003173 memset(&crq, 0, sizeof(crq));
3174 crq.control_ip_offload.first = IBMVNIC_CRQ_CMD;
3175 crq.control_ip_offload.cmd = CONTROL_IP_OFFLOAD;
3176 crq.control_ip_offload.len =
3177 cpu_to_be32(sizeof(adapter->ip_offload_ctrl));
3178 crq.control_ip_offload.ioba = cpu_to_be32(adapter->ip_offload_ctrl_tok);
3179 ibmvnic_send_crq(adapter, &crq);
3180}
3181
3182static void handle_error_info_rsp(union ibmvnic_crq *crq,
3183 struct ibmvnic_adapter *adapter)
3184{
3185 struct device *dev = &adapter->vdev->dev;
Wei Yongjun96183182016-06-27 20:48:53 +08003186 struct ibmvnic_error_buff *error_buff, *tmp;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003187 unsigned long flags;
3188 bool found = false;
3189 int i;
3190
3191 if (!crq->request_error_rsp.rc.code) {
3192 dev_info(dev, "Request Error Rsp returned with rc=%x\n",
3193 crq->request_error_rsp.rc.code);
3194 return;
3195 }
3196
3197 spin_lock_irqsave(&adapter->error_list_lock, flags);
Wei Yongjun96183182016-06-27 20:48:53 +08003198 list_for_each_entry_safe(error_buff, tmp, &adapter->errors, list)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003199 if (error_buff->error_id == crq->request_error_rsp.error_id) {
3200 found = true;
3201 list_del(&error_buff->list);
3202 break;
3203 }
3204 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
3205
3206 if (!found) {
3207 dev_err(dev, "Couldn't find error id %x\n",
Thomas Falcon75224c92017-02-15 10:33:33 -06003208 be32_to_cpu(crq->request_error_rsp.error_id));
Thomas Falcon032c5e82015-12-21 11:26:06 -06003209 return;
3210 }
3211
3212 dev_err(dev, "Detailed info for error id %x:",
Thomas Falcon75224c92017-02-15 10:33:33 -06003213 be32_to_cpu(crq->request_error_rsp.error_id));
Thomas Falcon032c5e82015-12-21 11:26:06 -06003214
3215 for (i = 0; i < error_buff->len; i++) {
3216 pr_cont("%02x", (int)error_buff->buff[i]);
3217 if (i % 8 == 7)
3218 pr_cont(" ");
3219 }
3220 pr_cont("\n");
3221
3222 dma_unmap_single(dev, error_buff->dma, error_buff->len,
3223 DMA_FROM_DEVICE);
3224 kfree(error_buff->buff);
3225 kfree(error_buff);
3226}
3227
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04003228static void request_error_information(struct ibmvnic_adapter *adapter,
3229 union ibmvnic_crq *err_crq)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003230{
Thomas Falcon032c5e82015-12-21 11:26:06 -06003231 struct device *dev = &adapter->vdev->dev;
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04003232 struct net_device *netdev = adapter->netdev;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003233 struct ibmvnic_error_buff *error_buff;
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04003234 unsigned long timeout = msecs_to_jiffies(30000);
3235 union ibmvnic_crq crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003236 unsigned long flags;
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04003237 int rc, detail_len;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003238
3239 error_buff = kmalloc(sizeof(*error_buff), GFP_ATOMIC);
3240 if (!error_buff)
3241 return;
3242
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04003243 detail_len = be32_to_cpu(err_crq->error_indication.detail_error_sz);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003244 error_buff->buff = kmalloc(detail_len, GFP_ATOMIC);
3245 if (!error_buff->buff) {
3246 kfree(error_buff);
3247 return;
3248 }
3249
3250 error_buff->dma = dma_map_single(dev, error_buff->buff, detail_len,
3251 DMA_FROM_DEVICE);
3252 if (dma_mapping_error(dev, error_buff->dma)) {
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04003253 netdev_err(netdev, "Couldn't map error buffer\n");
Thomas Falcon032c5e82015-12-21 11:26:06 -06003254 kfree(error_buff->buff);
3255 kfree(error_buff);
3256 return;
3257 }
3258
Thomas Falcon032c5e82015-12-21 11:26:06 -06003259 error_buff->len = detail_len;
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04003260 error_buff->error_id = err_crq->error_indication.error_id;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003261
3262 spin_lock_irqsave(&adapter->error_list_lock, flags);
3263 list_add_tail(&error_buff->list, &adapter->errors);
3264 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
3265
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04003266 memset(&crq, 0, sizeof(crq));
3267 crq.request_error_info.first = IBMVNIC_CRQ_CMD;
3268 crq.request_error_info.cmd = REQUEST_ERROR_INFO;
3269 crq.request_error_info.ioba = cpu_to_be32(error_buff->dma);
3270 crq.request_error_info.len = cpu_to_be32(detail_len);
3271 crq.request_error_info.error_id = err_crq->error_indication.error_id;
3272
3273 rc = ibmvnic_send_crq(adapter, &crq);
3274 if (rc) {
3275 netdev_err(netdev, "failed to request error information\n");
3276 goto err_info_fail;
3277 }
3278
3279 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
3280 netdev_err(netdev, "timeout waiting for error information\n");
3281 goto err_info_fail;
3282 }
3283
3284 return;
3285
3286err_info_fail:
3287 spin_lock_irqsave(&adapter->error_list_lock, flags);
3288 list_del(&error_buff->list);
3289 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
3290
3291 kfree(error_buff->buff);
3292 kfree(error_buff);
3293}
3294
3295static void handle_error_indication(union ibmvnic_crq *crq,
3296 struct ibmvnic_adapter *adapter)
3297{
3298 struct device *dev = &adapter->vdev->dev;
3299
3300 dev_err(dev, "Firmware reports %serror id %x, cause %d\n",
3301 crq->error_indication.flags
3302 & IBMVNIC_FATAL_ERROR ? "FATAL " : "",
3303 be32_to_cpu(crq->error_indication.error_id),
3304 be16_to_cpu(crq->error_indication.error_cause));
3305
3306 if (be32_to_cpu(crq->error_indication.error_id))
3307 request_error_information(adapter, crq);
Nathan Fontenoted651a12017-05-03 14:04:38 -04003308
3309 if (crq->error_indication.flags & IBMVNIC_FATAL_ERROR)
3310 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
John Allen8cb31cf2017-05-26 10:30:37 -04003311 else
3312 ibmvnic_reset(adapter, VNIC_RESET_NON_FATAL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003313}
3314
3315static void handle_change_mac_rsp(union ibmvnic_crq *crq,
3316 struct ibmvnic_adapter *adapter)
3317{
3318 struct net_device *netdev = adapter->netdev;
3319 struct device *dev = &adapter->vdev->dev;
3320 long rc;
3321
3322 rc = crq->change_mac_addr_rsp.rc.code;
3323 if (rc) {
3324 dev_err(dev, "Error %ld in CHANGE_MAC_ADDR_RSP\n", rc);
3325 return;
3326 }
3327 memcpy(netdev->dev_addr, &crq->change_mac_addr_rsp.mac_addr[0],
3328 ETH_ALEN);
3329}
3330
3331static void handle_request_cap_rsp(union ibmvnic_crq *crq,
3332 struct ibmvnic_adapter *adapter)
3333{
3334 struct device *dev = &adapter->vdev->dev;
3335 u64 *req_value;
3336 char *name;
3337
Thomas Falcon901e0402017-02-15 12:17:59 -06003338 atomic_dec(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003339 switch (be16_to_cpu(crq->request_capability_rsp.capability)) {
3340 case REQ_TX_QUEUES:
3341 req_value = &adapter->req_tx_queues;
3342 name = "tx";
3343 break;
3344 case REQ_RX_QUEUES:
3345 req_value = &adapter->req_rx_queues;
3346 name = "rx";
3347 break;
3348 case REQ_RX_ADD_QUEUES:
3349 req_value = &adapter->req_rx_add_queues;
3350 name = "rx_add";
3351 break;
3352 case REQ_TX_ENTRIES_PER_SUBCRQ:
3353 req_value = &adapter->req_tx_entries_per_subcrq;
3354 name = "tx_entries_per_subcrq";
3355 break;
3356 case REQ_RX_ADD_ENTRIES_PER_SUBCRQ:
3357 req_value = &adapter->req_rx_add_entries_per_subcrq;
3358 name = "rx_add_entries_per_subcrq";
3359 break;
3360 case REQ_MTU:
3361 req_value = &adapter->req_mtu;
3362 name = "mtu";
3363 break;
3364 case PROMISC_REQUESTED:
3365 req_value = &adapter->promisc;
3366 name = "promisc";
3367 break;
3368 default:
3369 dev_err(dev, "Got invalid cap request rsp %d\n",
3370 crq->request_capability.capability);
3371 return;
3372 }
3373
3374 switch (crq->request_capability_rsp.rc.code) {
3375 case SUCCESS:
3376 break;
3377 case PARTIALSUCCESS:
3378 dev_info(dev, "req=%lld, rsp=%ld in %s queue, retrying.\n",
3379 *req_value,
Thomas Falcon28f4d162017-02-15 10:32:11 -06003380 (long int)be64_to_cpu(crq->request_capability_rsp.
Thomas Falcon032c5e82015-12-21 11:26:06 -06003381 number), name);
Thomas Falcon28f4d162017-02-15 10:32:11 -06003382 *req_value = be64_to_cpu(crq->request_capability_rsp.number);
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003383 ibmvnic_send_req_caps(adapter, 1);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003384 return;
3385 default:
3386 dev_err(dev, "Error %d in request cap rsp\n",
3387 crq->request_capability_rsp.rc.code);
3388 return;
3389 }
3390
3391 /* Done receiving requested capabilities, query IP offload support */
Thomas Falcon901e0402017-02-15 12:17:59 -06003392 if (atomic_read(&adapter->running_cap_crqs) == 0) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06003393 union ibmvnic_crq newcrq;
3394 int buf_sz = sizeof(struct ibmvnic_query_ip_offload_buffer);
3395 struct ibmvnic_query_ip_offload_buffer *ip_offload_buf =
3396 &adapter->ip_offload_buf;
3397
Thomas Falcon249168a2017-02-15 12:18:00 -06003398 adapter->wait_capability = false;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003399 adapter->ip_offload_tok = dma_map_single(dev, ip_offload_buf,
3400 buf_sz,
3401 DMA_FROM_DEVICE);
3402
3403 if (dma_mapping_error(dev, adapter->ip_offload_tok)) {
3404 if (!firmware_has_feature(FW_FEATURE_CMO))
3405 dev_err(dev, "Couldn't map offload buffer\n");
3406 return;
3407 }
3408
3409 memset(&newcrq, 0, sizeof(newcrq));
3410 newcrq.query_ip_offload.first = IBMVNIC_CRQ_CMD;
3411 newcrq.query_ip_offload.cmd = QUERY_IP_OFFLOAD;
3412 newcrq.query_ip_offload.len = cpu_to_be32(buf_sz);
3413 newcrq.query_ip_offload.ioba =
3414 cpu_to_be32(adapter->ip_offload_tok);
3415
3416 ibmvnic_send_crq(adapter, &newcrq);
3417 }
3418}
3419
3420static int handle_login_rsp(union ibmvnic_crq *login_rsp_crq,
3421 struct ibmvnic_adapter *adapter)
3422{
3423 struct device *dev = &adapter->vdev->dev;
John Allenc26eba02017-10-26 16:23:25 -05003424 struct net_device *netdev = adapter->netdev;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003425 struct ibmvnic_login_rsp_buffer *login_rsp = adapter->login_rsp_buf;
3426 struct ibmvnic_login_buffer *login = adapter->login_buf;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003427 int i;
3428
3429 dma_unmap_single(dev, adapter->login_buf_token, adapter->login_buf_sz,
3430 DMA_BIDIRECTIONAL);
3431 dma_unmap_single(dev, adapter->login_rsp_buf_token,
3432 adapter->login_rsp_buf_sz, DMA_BIDIRECTIONAL);
3433
John Allen498cd8e2016-04-06 11:49:55 -05003434 /* If the number of queues requested can't be allocated by the
3435 * server, the login response will return with code 1. We will need
3436 * to resend the login buffer with fewer queues requested.
3437 */
3438 if (login_rsp_crq->generic.rc.code) {
3439 adapter->renegotiate = true;
3440 complete(&adapter->init_done);
3441 return 0;
3442 }
3443
John Allenc26eba02017-10-26 16:23:25 -05003444 netdev->mtu = adapter->req_mtu - ETH_HLEN;
3445
Thomas Falcon032c5e82015-12-21 11:26:06 -06003446 netdev_dbg(adapter->netdev, "Login Response Buffer:\n");
3447 for (i = 0; i < (adapter->login_rsp_buf_sz - 1) / 8 + 1; i++) {
3448 netdev_dbg(adapter->netdev, "%016lx\n",
3449 ((unsigned long int *)(adapter->login_rsp_buf))[i]);
3450 }
3451
3452 /* Sanity checks */
3453 if (login->num_txcomp_subcrqs != login_rsp->num_txsubm_subcrqs ||
3454 (be32_to_cpu(login->num_rxcomp_subcrqs) *
3455 adapter->req_rx_add_queues !=
3456 be32_to_cpu(login_rsp->num_rxadd_subcrqs))) {
3457 dev_err(dev, "FATAL: Inconsistent login and login rsp\n");
3458 ibmvnic_remove(adapter->vdev);
3459 return -EIO;
3460 }
3461 complete(&adapter->init_done);
3462
Thomas Falcon032c5e82015-12-21 11:26:06 -06003463 return 0;
3464}
3465
Thomas Falcon032c5e82015-12-21 11:26:06 -06003466static void handle_request_unmap_rsp(union ibmvnic_crq *crq,
3467 struct ibmvnic_adapter *adapter)
3468{
3469 struct device *dev = &adapter->vdev->dev;
3470 long rc;
3471
3472 rc = crq->request_unmap_rsp.rc.code;
3473 if (rc)
3474 dev_err(dev, "Error %ld in REQUEST_UNMAP_RSP\n", rc);
3475}
3476
3477static void handle_query_map_rsp(union ibmvnic_crq *crq,
3478 struct ibmvnic_adapter *adapter)
3479{
3480 struct net_device *netdev = adapter->netdev;
3481 struct device *dev = &adapter->vdev->dev;
3482 long rc;
3483
3484 rc = crq->query_map_rsp.rc.code;
3485 if (rc) {
3486 dev_err(dev, "Error %ld in QUERY_MAP_RSP\n", rc);
3487 return;
3488 }
3489 netdev_dbg(netdev, "page_size = %d\ntot_pages = %d\nfree_pages = %d\n",
3490 crq->query_map_rsp.page_size, crq->query_map_rsp.tot_pages,
3491 crq->query_map_rsp.free_pages);
3492}
3493
3494static void handle_query_cap_rsp(union ibmvnic_crq *crq,
3495 struct ibmvnic_adapter *adapter)
3496{
3497 struct net_device *netdev = adapter->netdev;
3498 struct device *dev = &adapter->vdev->dev;
3499 long rc;
3500
Thomas Falcon901e0402017-02-15 12:17:59 -06003501 atomic_dec(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003502 netdev_dbg(netdev, "Outstanding queries: %d\n",
Thomas Falcon901e0402017-02-15 12:17:59 -06003503 atomic_read(&adapter->running_cap_crqs));
Thomas Falcon032c5e82015-12-21 11:26:06 -06003504 rc = crq->query_capability.rc.code;
3505 if (rc) {
3506 dev_err(dev, "Error %ld in QUERY_CAP_RSP\n", rc);
3507 goto out;
3508 }
3509
3510 switch (be16_to_cpu(crq->query_capability.capability)) {
3511 case MIN_TX_QUEUES:
3512 adapter->min_tx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003513 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003514 netdev_dbg(netdev, "min_tx_queues = %lld\n",
3515 adapter->min_tx_queues);
3516 break;
3517 case MIN_RX_QUEUES:
3518 adapter->min_rx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003519 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003520 netdev_dbg(netdev, "min_rx_queues = %lld\n",
3521 adapter->min_rx_queues);
3522 break;
3523 case MIN_RX_ADD_QUEUES:
3524 adapter->min_rx_add_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003525 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003526 netdev_dbg(netdev, "min_rx_add_queues = %lld\n",
3527 adapter->min_rx_add_queues);
3528 break;
3529 case MAX_TX_QUEUES:
3530 adapter->max_tx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003531 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003532 netdev_dbg(netdev, "max_tx_queues = %lld\n",
3533 adapter->max_tx_queues);
3534 break;
3535 case MAX_RX_QUEUES:
3536 adapter->max_rx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003537 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003538 netdev_dbg(netdev, "max_rx_queues = %lld\n",
3539 adapter->max_rx_queues);
3540 break;
3541 case MAX_RX_ADD_QUEUES:
3542 adapter->max_rx_add_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003543 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003544 netdev_dbg(netdev, "max_rx_add_queues = %lld\n",
3545 adapter->max_rx_add_queues);
3546 break;
3547 case MIN_TX_ENTRIES_PER_SUBCRQ:
3548 adapter->min_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06003549 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003550 netdev_dbg(netdev, "min_tx_entries_per_subcrq = %lld\n",
3551 adapter->min_tx_entries_per_subcrq);
3552 break;
3553 case MIN_RX_ADD_ENTRIES_PER_SUBCRQ:
3554 adapter->min_rx_add_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06003555 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003556 netdev_dbg(netdev, "min_rx_add_entrs_per_subcrq = %lld\n",
3557 adapter->min_rx_add_entries_per_subcrq);
3558 break;
3559 case MAX_TX_ENTRIES_PER_SUBCRQ:
3560 adapter->max_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06003561 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003562 netdev_dbg(netdev, "max_tx_entries_per_subcrq = %lld\n",
3563 adapter->max_tx_entries_per_subcrq);
3564 break;
3565 case MAX_RX_ADD_ENTRIES_PER_SUBCRQ:
3566 adapter->max_rx_add_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06003567 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003568 netdev_dbg(netdev, "max_rx_add_entrs_per_subcrq = %lld\n",
3569 adapter->max_rx_add_entries_per_subcrq);
3570 break;
3571 case TCP_IP_OFFLOAD:
3572 adapter->tcp_ip_offload =
Thomas Falconde89e852016-03-01 10:20:09 -06003573 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003574 netdev_dbg(netdev, "tcp_ip_offload = %lld\n",
3575 adapter->tcp_ip_offload);
3576 break;
3577 case PROMISC_SUPPORTED:
3578 adapter->promisc_supported =
Thomas Falconde89e852016-03-01 10:20:09 -06003579 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003580 netdev_dbg(netdev, "promisc_supported = %lld\n",
3581 adapter->promisc_supported);
3582 break;
3583 case MIN_MTU:
Thomas Falconde89e852016-03-01 10:20:09 -06003584 adapter->min_mtu = be64_to_cpu(crq->query_capability.number);
Thomas Falconf39f0d12017-02-14 10:22:59 -06003585 netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003586 netdev_dbg(netdev, "min_mtu = %lld\n", adapter->min_mtu);
3587 break;
3588 case MAX_MTU:
Thomas Falconde89e852016-03-01 10:20:09 -06003589 adapter->max_mtu = be64_to_cpu(crq->query_capability.number);
Thomas Falconf39f0d12017-02-14 10:22:59 -06003590 netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003591 netdev_dbg(netdev, "max_mtu = %lld\n", adapter->max_mtu);
3592 break;
3593 case MAX_MULTICAST_FILTERS:
3594 adapter->max_multicast_filters =
Thomas Falconde89e852016-03-01 10:20:09 -06003595 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003596 netdev_dbg(netdev, "max_multicast_filters = %lld\n",
3597 adapter->max_multicast_filters);
3598 break;
3599 case VLAN_HEADER_INSERTION:
3600 adapter->vlan_header_insertion =
Thomas Falconde89e852016-03-01 10:20:09 -06003601 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003602 if (adapter->vlan_header_insertion)
3603 netdev->features |= NETIF_F_HW_VLAN_STAG_TX;
3604 netdev_dbg(netdev, "vlan_header_insertion = %lld\n",
3605 adapter->vlan_header_insertion);
3606 break;
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04003607 case RX_VLAN_HEADER_INSERTION:
3608 adapter->rx_vlan_header_insertion =
3609 be64_to_cpu(crq->query_capability.number);
3610 netdev_dbg(netdev, "rx_vlan_header_insertion = %lld\n",
3611 adapter->rx_vlan_header_insertion);
3612 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003613 case MAX_TX_SG_ENTRIES:
3614 adapter->max_tx_sg_entries =
Thomas Falconde89e852016-03-01 10:20:09 -06003615 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003616 netdev_dbg(netdev, "max_tx_sg_entries = %lld\n",
3617 adapter->max_tx_sg_entries);
3618 break;
3619 case RX_SG_SUPPORTED:
3620 adapter->rx_sg_supported =
Thomas Falconde89e852016-03-01 10:20:09 -06003621 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003622 netdev_dbg(netdev, "rx_sg_supported = %lld\n",
3623 adapter->rx_sg_supported);
3624 break;
3625 case OPT_TX_COMP_SUB_QUEUES:
3626 adapter->opt_tx_comp_sub_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003627 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003628 netdev_dbg(netdev, "opt_tx_comp_sub_queues = %lld\n",
3629 adapter->opt_tx_comp_sub_queues);
3630 break;
3631 case OPT_RX_COMP_QUEUES:
3632 adapter->opt_rx_comp_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003633 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003634 netdev_dbg(netdev, "opt_rx_comp_queues = %lld\n",
3635 adapter->opt_rx_comp_queues);
3636 break;
3637 case OPT_RX_BUFADD_Q_PER_RX_COMP_Q:
3638 adapter->opt_rx_bufadd_q_per_rx_comp_q =
Thomas Falconde89e852016-03-01 10:20:09 -06003639 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003640 netdev_dbg(netdev, "opt_rx_bufadd_q_per_rx_comp_q = %lld\n",
3641 adapter->opt_rx_bufadd_q_per_rx_comp_q);
3642 break;
3643 case OPT_TX_ENTRIES_PER_SUBCRQ:
3644 adapter->opt_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06003645 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003646 netdev_dbg(netdev, "opt_tx_entries_per_subcrq = %lld\n",
3647 adapter->opt_tx_entries_per_subcrq);
3648 break;
3649 case OPT_RXBA_ENTRIES_PER_SUBCRQ:
3650 adapter->opt_rxba_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06003651 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003652 netdev_dbg(netdev, "opt_rxba_entries_per_subcrq = %lld\n",
3653 adapter->opt_rxba_entries_per_subcrq);
3654 break;
3655 case TX_RX_DESC_REQ:
3656 adapter->tx_rx_desc_req = crq->query_capability.number;
3657 netdev_dbg(netdev, "tx_rx_desc_req = %llx\n",
3658 adapter->tx_rx_desc_req);
3659 break;
3660
3661 default:
3662 netdev_err(netdev, "Got invalid cap rsp %d\n",
3663 crq->query_capability.capability);
3664 }
3665
3666out:
Thomas Falcon249168a2017-02-15 12:18:00 -06003667 if (atomic_read(&adapter->running_cap_crqs) == 0) {
3668 adapter->wait_capability = false;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003669 ibmvnic_send_req_caps(adapter, 0);
Thomas Falcon249168a2017-02-15 12:18:00 -06003670 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06003671}
3672
Thomas Falcon032c5e82015-12-21 11:26:06 -06003673static void ibmvnic_handle_crq(union ibmvnic_crq *crq,
3674 struct ibmvnic_adapter *adapter)
3675{
3676 struct ibmvnic_generic_crq *gen_crq = &crq->generic;
3677 struct net_device *netdev = adapter->netdev;
3678 struct device *dev = &adapter->vdev->dev;
Murilo Fossa Vicentini993a82b2017-04-19 13:44:35 -04003679 u64 *u64_crq = (u64 *)crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003680 long rc;
3681
3682 netdev_dbg(netdev, "Handling CRQ: %016lx %016lx\n",
Murilo Fossa Vicentini993a82b2017-04-19 13:44:35 -04003683 (unsigned long int)cpu_to_be64(u64_crq[0]),
3684 (unsigned long int)cpu_to_be64(u64_crq[1]));
Thomas Falcon032c5e82015-12-21 11:26:06 -06003685 switch (gen_crq->first) {
3686 case IBMVNIC_CRQ_INIT_RSP:
3687 switch (gen_crq->cmd) {
3688 case IBMVNIC_CRQ_INIT:
3689 dev_info(dev, "Partner initialized\n");
John Allen017892c12017-05-26 10:30:19 -04003690 adapter->from_passive_init = true;
3691 complete(&adapter->init_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003692 break;
3693 case IBMVNIC_CRQ_INIT_COMPLETE:
3694 dev_info(dev, "Partner initialization complete\n");
3695 send_version_xchg(adapter);
3696 break;
3697 default:
3698 dev_err(dev, "Unknown crq cmd: %d\n", gen_crq->cmd);
3699 }
3700 return;
3701 case IBMVNIC_CRQ_XPORT_EVENT:
Nathan Fontenoted651a12017-05-03 14:04:38 -04003702 netif_carrier_off(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003703 if (gen_crq->cmd == IBMVNIC_PARTITION_MIGRATED) {
Nathan Fontenoted651a12017-05-03 14:04:38 -04003704 dev_info(dev, "Migrated, re-enabling adapter\n");
3705 ibmvnic_reset(adapter, VNIC_RESET_MOBILITY);
Thomas Falcondfad09a2016-08-18 11:37:51 -05003706 } else if (gen_crq->cmd == IBMVNIC_DEVICE_FAILOVER) {
3707 dev_info(dev, "Backing device failover detected\n");
Nathan Fontenoted651a12017-05-03 14:04:38 -04003708 ibmvnic_reset(adapter, VNIC_RESET_FAILOVER);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003709 } else {
3710 /* The adapter lost the connection */
3711 dev_err(dev, "Virtual Adapter failed (rc=%d)\n",
3712 gen_crq->cmd);
Nathan Fontenoted651a12017-05-03 14:04:38 -04003713 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003714 }
3715 return;
3716 case IBMVNIC_CRQ_CMD_RSP:
3717 break;
3718 default:
3719 dev_err(dev, "Got an invalid msg type 0x%02x\n",
3720 gen_crq->first);
3721 return;
3722 }
3723
3724 switch (gen_crq->cmd) {
3725 case VERSION_EXCHANGE_RSP:
3726 rc = crq->version_exchange_rsp.rc.code;
3727 if (rc) {
3728 dev_err(dev, "Error %ld in VERSION_EXCHG_RSP\n", rc);
3729 break;
3730 }
3731 dev_info(dev, "Partner protocol version is %d\n",
3732 crq->version_exchange_rsp.version);
3733 if (be16_to_cpu(crq->version_exchange_rsp.version) <
3734 ibmvnic_version)
3735 ibmvnic_version =
3736 be16_to_cpu(crq->version_exchange_rsp.version);
3737 send_cap_queries(adapter);
3738 break;
3739 case QUERY_CAPABILITY_RSP:
3740 handle_query_cap_rsp(crq, adapter);
3741 break;
3742 case QUERY_MAP_RSP:
3743 handle_query_map_rsp(crq, adapter);
3744 break;
3745 case REQUEST_MAP_RSP:
Thomas Falconf3be0cb2017-06-21 14:53:01 -05003746 adapter->fw_done_rc = crq->request_map_rsp.rc.code;
3747 complete(&adapter->fw_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003748 break;
3749 case REQUEST_UNMAP_RSP:
3750 handle_request_unmap_rsp(crq, adapter);
3751 break;
3752 case REQUEST_CAPABILITY_RSP:
3753 handle_request_cap_rsp(crq, adapter);
3754 break;
3755 case LOGIN_RSP:
3756 netdev_dbg(netdev, "Got Login Response\n");
3757 handle_login_rsp(crq, adapter);
3758 break;
3759 case LOGICAL_LINK_STATE_RSP:
Nathan Fontenot53da09e2017-04-21 15:39:04 -04003760 netdev_dbg(netdev,
3761 "Got Logical Link State Response, state: %d rc: %d\n",
3762 crq->logical_link_state_rsp.link_state,
3763 crq->logical_link_state_rsp.rc.code);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003764 adapter->logical_link_state =
3765 crq->logical_link_state_rsp.link_state;
Nathan Fontenot53da09e2017-04-21 15:39:04 -04003766 adapter->init_done_rc = crq->logical_link_state_rsp.rc.code;
3767 complete(&adapter->init_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003768 break;
3769 case LINK_STATE_INDICATION:
3770 netdev_dbg(netdev, "Got Logical Link State Indication\n");
3771 adapter->phys_link_state =
3772 crq->link_state_indication.phys_link_state;
3773 adapter->logical_link_state =
3774 crq->link_state_indication.logical_link_state;
3775 break;
3776 case CHANGE_MAC_ADDR_RSP:
3777 netdev_dbg(netdev, "Got MAC address change Response\n");
3778 handle_change_mac_rsp(crq, adapter);
3779 break;
3780 case ERROR_INDICATION:
3781 netdev_dbg(netdev, "Got Error Indication\n");
3782 handle_error_indication(crq, adapter);
3783 break;
3784 case REQUEST_ERROR_RSP:
3785 netdev_dbg(netdev, "Got Error Detail Response\n");
3786 handle_error_info_rsp(crq, adapter);
3787 break;
3788 case REQUEST_STATISTICS_RSP:
3789 netdev_dbg(netdev, "Got Statistics Response\n");
3790 complete(&adapter->stats_done);
3791 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003792 case QUERY_IP_OFFLOAD_RSP:
3793 netdev_dbg(netdev, "Got Query IP offload Response\n");
3794 handle_query_ip_offload_rsp(adapter);
3795 break;
3796 case MULTICAST_CTRL_RSP:
3797 netdev_dbg(netdev, "Got multicast control Response\n");
3798 break;
3799 case CONTROL_IP_OFFLOAD_RSP:
3800 netdev_dbg(netdev, "Got Control IP offload Response\n");
3801 dma_unmap_single(dev, adapter->ip_offload_ctrl_tok,
3802 sizeof(adapter->ip_offload_ctrl),
3803 DMA_TO_DEVICE);
John Allenbd0b6722017-03-17 17:13:40 -05003804 complete(&adapter->init_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003805 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003806 case COLLECT_FW_TRACE_RSP:
3807 netdev_dbg(netdev, "Got Collect firmware trace Response\n");
3808 complete(&adapter->fw_done);
3809 break;
3810 default:
3811 netdev_err(netdev, "Got an invalid cmd type 0x%02x\n",
3812 gen_crq->cmd);
3813 }
3814}
3815
3816static irqreturn_t ibmvnic_interrupt(int irq, void *instance)
3817{
3818 struct ibmvnic_adapter *adapter = instance;
Thomas Falcon6c267b32017-02-15 12:17:58 -06003819
Thomas Falcon6c267b32017-02-15 12:17:58 -06003820 tasklet_schedule(&adapter->tasklet);
Thomas Falcon6c267b32017-02-15 12:17:58 -06003821 return IRQ_HANDLED;
3822}
3823
3824static void ibmvnic_tasklet(void *data)
3825{
3826 struct ibmvnic_adapter *adapter = data;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003827 struct ibmvnic_crq_queue *queue = &adapter->crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003828 union ibmvnic_crq *crq;
3829 unsigned long flags;
3830 bool done = false;
3831
3832 spin_lock_irqsave(&queue->lock, flags);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003833 while (!done) {
3834 /* Pull all the valid messages off the CRQ */
3835 while ((crq = ibmvnic_next_crq(adapter)) != NULL) {
3836 ibmvnic_handle_crq(crq, adapter);
3837 crq->generic.first = 0;
3838 }
Brian Kinged7ecbf2017-04-19 13:44:53 -04003839
3840 /* remain in tasklet until all
3841 * capabilities responses are received
3842 */
3843 if (!adapter->wait_capability)
3844 done = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003845 }
Thomas Falcon249168a2017-02-15 12:18:00 -06003846 /* if capabilities CRQ's were sent in this tasklet, the following
3847 * tasklet must wait until all responses are received
3848 */
3849 if (atomic_read(&adapter->running_cap_crqs) != 0)
3850 adapter->wait_capability = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003851 spin_unlock_irqrestore(&queue->lock, flags);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003852}
3853
3854static int ibmvnic_reenable_crq_queue(struct ibmvnic_adapter *adapter)
3855{
3856 struct vio_dev *vdev = adapter->vdev;
3857 int rc;
3858
3859 do {
3860 rc = plpar_hcall_norets(H_ENABLE_CRQ, vdev->unit_address);
3861 } while (rc == H_IN_PROGRESS || rc == H_BUSY || H_IS_LONG_BUSY(rc));
3862
3863 if (rc)
3864 dev_err(&vdev->dev, "Error enabling adapter (rc=%d)\n", rc);
3865
3866 return rc;
3867}
3868
3869static int ibmvnic_reset_crq(struct ibmvnic_adapter *adapter)
3870{
3871 struct ibmvnic_crq_queue *crq = &adapter->crq;
3872 struct device *dev = &adapter->vdev->dev;
3873 struct vio_dev *vdev = adapter->vdev;
3874 int rc;
3875
3876 /* Close the CRQ */
3877 do {
3878 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
3879 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
3880
3881 /* Clean out the queue */
3882 memset(crq->msgs, 0, PAGE_SIZE);
3883 crq->cur = 0;
3884
3885 /* And re-open it again */
3886 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
3887 crq->msg_token, PAGE_SIZE);
3888
3889 if (rc == H_CLOSED)
3890 /* Adapter is good, but other end is not ready */
3891 dev_warn(dev, "Partner adapter not ready\n");
3892 else if (rc != 0)
3893 dev_warn(dev, "Couldn't register crq (rc=%d)\n", rc);
3894
3895 return rc;
3896}
3897
Nathan Fontenotf9928872017-03-30 02:48:54 -04003898static void release_crq_queue(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003899{
3900 struct ibmvnic_crq_queue *crq = &adapter->crq;
3901 struct vio_dev *vdev = adapter->vdev;
3902 long rc;
3903
Nathan Fontenotf9928872017-03-30 02:48:54 -04003904 if (!crq->msgs)
3905 return;
3906
Thomas Falcon032c5e82015-12-21 11:26:06 -06003907 netdev_dbg(adapter->netdev, "Releasing CRQ\n");
3908 free_irq(vdev->irq, adapter);
Thomas Falcon6c267b32017-02-15 12:17:58 -06003909 tasklet_kill(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003910 do {
3911 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
3912 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
3913
3914 dma_unmap_single(&vdev->dev, crq->msg_token, PAGE_SIZE,
3915 DMA_BIDIRECTIONAL);
3916 free_page((unsigned long)crq->msgs);
Nathan Fontenotf9928872017-03-30 02:48:54 -04003917 crq->msgs = NULL;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003918}
3919
Nathan Fontenotf9928872017-03-30 02:48:54 -04003920static int init_crq_queue(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003921{
3922 struct ibmvnic_crq_queue *crq = &adapter->crq;
3923 struct device *dev = &adapter->vdev->dev;
3924 struct vio_dev *vdev = adapter->vdev;
3925 int rc, retrc = -ENOMEM;
3926
Nathan Fontenotf9928872017-03-30 02:48:54 -04003927 if (crq->msgs)
3928 return 0;
3929
Thomas Falcon032c5e82015-12-21 11:26:06 -06003930 crq->msgs = (union ibmvnic_crq *)get_zeroed_page(GFP_KERNEL);
3931 /* Should we allocate more than one page? */
3932
3933 if (!crq->msgs)
3934 return -ENOMEM;
3935
3936 crq->size = PAGE_SIZE / sizeof(*crq->msgs);
3937 crq->msg_token = dma_map_single(dev, crq->msgs, PAGE_SIZE,
3938 DMA_BIDIRECTIONAL);
3939 if (dma_mapping_error(dev, crq->msg_token))
3940 goto map_failed;
3941
3942 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
3943 crq->msg_token, PAGE_SIZE);
3944
3945 if (rc == H_RESOURCE)
3946 /* maybe kexecing and resource is busy. try a reset */
3947 rc = ibmvnic_reset_crq(adapter);
3948 retrc = rc;
3949
3950 if (rc == H_CLOSED) {
3951 dev_warn(dev, "Partner adapter not ready\n");
3952 } else if (rc) {
3953 dev_warn(dev, "Error %d opening adapter\n", rc);
3954 goto reg_crq_failed;
3955 }
3956
3957 retrc = 0;
3958
Thomas Falcon6c267b32017-02-15 12:17:58 -06003959 tasklet_init(&adapter->tasklet, (void *)ibmvnic_tasklet,
3960 (unsigned long)adapter);
3961
Thomas Falcon032c5e82015-12-21 11:26:06 -06003962 netdev_dbg(adapter->netdev, "registering irq 0x%x\n", vdev->irq);
3963 rc = request_irq(vdev->irq, ibmvnic_interrupt, 0, IBMVNIC_NAME,
3964 adapter);
3965 if (rc) {
3966 dev_err(dev, "Couldn't register irq 0x%x. rc=%d\n",
3967 vdev->irq, rc);
3968 goto req_irq_failed;
3969 }
3970
3971 rc = vio_enable_interrupts(vdev);
3972 if (rc) {
3973 dev_err(dev, "Error %d enabling interrupts\n", rc);
3974 goto req_irq_failed;
3975 }
3976
3977 crq->cur = 0;
3978 spin_lock_init(&crq->lock);
3979
3980 return retrc;
3981
3982req_irq_failed:
Thomas Falcon6c267b32017-02-15 12:17:58 -06003983 tasklet_kill(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003984 do {
3985 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
3986 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
3987reg_crq_failed:
3988 dma_unmap_single(dev, crq->msg_token, PAGE_SIZE, DMA_BIDIRECTIONAL);
3989map_failed:
3990 free_page((unsigned long)crq->msgs);
Nathan Fontenotf9928872017-03-30 02:48:54 -04003991 crq->msgs = NULL;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003992 return retrc;
3993}
3994
John Allenf6ef6402017-03-17 17:13:42 -05003995static int ibmvnic_init(struct ibmvnic_adapter *adapter)
3996{
3997 struct device *dev = &adapter->vdev->dev;
3998 unsigned long timeout = msecs_to_jiffies(30000);
John Allenf6ef6402017-03-17 17:13:42 -05003999 int rc;
4000
John Allenc26eba02017-10-26 16:23:25 -05004001 if (adapter->resetting && !adapter->wait_for_reset) {
Nathan Fontenot28cde752017-05-26 10:31:00 -04004002 rc = ibmvnic_reset_crq(adapter);
4003 if (!rc)
4004 rc = vio_enable_interrupts(adapter->vdev);
4005 } else {
4006 rc = init_crq_queue(adapter);
4007 }
4008
John Allenf6ef6402017-03-17 17:13:42 -05004009 if (rc) {
4010 dev_err(dev, "Couldn't initialize crq. rc=%d\n", rc);
4011 return rc;
4012 }
4013
John Allen017892c12017-05-26 10:30:19 -04004014 adapter->from_passive_init = false;
4015
John Allenf6ef6402017-03-17 17:13:42 -05004016 init_completion(&adapter->init_done);
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04004017 adapter->init_done_rc = 0;
John Allenf6ef6402017-03-17 17:13:42 -05004018 ibmvnic_send_crq_init(adapter);
4019 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
4020 dev_err(dev, "Initialization sequence timed out\n");
John Allen017892c12017-05-26 10:30:19 -04004021 return -1;
4022 }
4023
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04004024 if (adapter->init_done_rc) {
4025 release_crq_queue(adapter);
4026 return adapter->init_done_rc;
4027 }
4028
John Allen017892c12017-05-26 10:30:19 -04004029 if (adapter->from_passive_init) {
4030 adapter->state = VNIC_OPEN;
4031 adapter->from_passive_init = false;
John Allenf6ef6402017-03-17 17:13:42 -05004032 return -1;
4033 }
4034
John Allenc26eba02017-10-26 16:23:25 -05004035 if (adapter->resetting && !adapter->wait_for_reset)
Nathan Fontenot57a49432017-05-26 10:31:12 -04004036 rc = reset_sub_crq_queues(adapter);
4037 else
4038 rc = init_sub_crqs(adapter);
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04004039 if (rc) {
4040 dev_err(dev, "Initialization of sub crqs failed\n");
4041 release_crq_queue(adapter);
Thomas Falcon5df969c2017-06-28 19:55:54 -05004042 return rc;
4043 }
4044
4045 rc = init_sub_crq_irqs(adapter);
4046 if (rc) {
4047 dev_err(dev, "Failed to initialize sub crq irqs\n");
4048 release_crq_queue(adapter);
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04004049 }
4050
4051 return rc;
John Allenf6ef6402017-03-17 17:13:42 -05004052}
4053
Thomas Falcon40c9db82017-06-12 12:35:04 -05004054static struct device_attribute dev_attr_failover;
4055
Thomas Falcon032c5e82015-12-21 11:26:06 -06004056static int ibmvnic_probe(struct vio_dev *dev, const struct vio_device_id *id)
4057{
4058 struct ibmvnic_adapter *adapter;
4059 struct net_device *netdev;
4060 unsigned char *mac_addr_p;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004061 int rc;
4062
4063 dev_dbg(&dev->dev, "entering ibmvnic_probe for UA 0x%x\n",
4064 dev->unit_address);
4065
4066 mac_addr_p = (unsigned char *)vio_get_attribute(dev,
4067 VETH_MAC_ADDR, NULL);
4068 if (!mac_addr_p) {
4069 dev_err(&dev->dev,
4070 "(%s:%3.3d) ERROR: Can't find MAC_ADDR attribute\n",
4071 __FILE__, __LINE__);
4072 return 0;
4073 }
4074
4075 netdev = alloc_etherdev_mq(sizeof(struct ibmvnic_adapter),
4076 IBMVNIC_MAX_TX_QUEUES);
4077 if (!netdev)
4078 return -ENOMEM;
4079
4080 adapter = netdev_priv(netdev);
Nathan Fontenot90c80142017-05-03 14:04:32 -04004081 adapter->state = VNIC_PROBING;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004082 dev_set_drvdata(&dev->dev, netdev);
4083 adapter->vdev = dev;
4084 adapter->netdev = netdev;
4085
4086 ether_addr_copy(adapter->mac_addr, mac_addr_p);
4087 ether_addr_copy(netdev->dev_addr, adapter->mac_addr);
4088 netdev->irq = dev->irq;
4089 netdev->netdev_ops = &ibmvnic_netdev_ops;
4090 netdev->ethtool_ops = &ibmvnic_ethtool_ops;
4091 SET_NETDEV_DEV(netdev, &dev->dev);
4092
4093 spin_lock_init(&adapter->stats_lock);
4094
Thomas Falcon032c5e82015-12-21 11:26:06 -06004095 INIT_LIST_HEAD(&adapter->errors);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004096 spin_lock_init(&adapter->error_list_lock);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004097
Nathan Fontenoted651a12017-05-03 14:04:38 -04004098 INIT_WORK(&adapter->ibmvnic_reset, __ibmvnic_reset);
4099 INIT_LIST_HEAD(&adapter->rwi_list);
4100 mutex_init(&adapter->reset_lock);
4101 mutex_init(&adapter->rwi_lock);
4102 adapter->resetting = false;
4103
John Allenc26eba02017-10-26 16:23:25 -05004104 adapter->mac_change_pending = false;
4105
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04004106 do {
4107 rc = ibmvnic_init(adapter);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05004108 if (rc && rc != EAGAIN)
4109 goto ibmvnic_init_fail;
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04004110 } while (rc == EAGAIN);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004111
Thomas Falconf39f0d12017-02-14 10:22:59 -06004112 netdev->mtu = adapter->req_mtu - ETH_HLEN;
John Allenc26eba02017-10-26 16:23:25 -05004113 netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
4114 netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004115
Thomas Falcon40c9db82017-06-12 12:35:04 -05004116 rc = device_create_file(&dev->dev, &dev_attr_failover);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05004117 if (rc)
4118 goto ibmvnic_init_fail;
Thomas Falcon40c9db82017-06-12 12:35:04 -05004119
Mick Tarsele876a8a2017-09-28 13:53:18 -07004120 netif_carrier_off(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004121 rc = register_netdev(netdev);
4122 if (rc) {
4123 dev_err(&dev->dev, "failed to register netdev rc=%d\n", rc);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05004124 goto ibmvnic_register_fail;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004125 }
4126 dev_info(&dev->dev, "ibmvnic registered\n");
4127
Nathan Fontenot90c80142017-05-03 14:04:32 -04004128 adapter->state = VNIC_PROBED;
John Allenc26eba02017-10-26 16:23:25 -05004129
4130 adapter->wait_for_reset = false;
4131
Thomas Falcon032c5e82015-12-21 11:26:06 -06004132 return 0;
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05004133
4134ibmvnic_register_fail:
4135 device_remove_file(&dev->dev, &dev_attr_failover);
4136
4137ibmvnic_init_fail:
4138 release_sub_crqs(adapter);
4139 release_crq_queue(adapter);
4140 free_netdev(netdev);
4141
4142 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004143}
4144
4145static int ibmvnic_remove(struct vio_dev *dev)
4146{
4147 struct net_device *netdev = dev_get_drvdata(&dev->dev);
Nathan Fontenot37489052017-04-19 13:45:04 -04004148 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004149
Nathan Fontenot90c80142017-05-03 14:04:32 -04004150 adapter->state = VNIC_REMOVING;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004151 unregister_netdev(netdev);
Nathan Fontenoted651a12017-05-03 14:04:38 -04004152 mutex_lock(&adapter->reset_lock);
Nathan Fontenot37489052017-04-19 13:45:04 -04004153
4154 release_resources(adapter);
4155 release_sub_crqs(adapter);
4156 release_crq_queue(adapter);
4157
Nathan Fontenot90c80142017-05-03 14:04:32 -04004158 adapter->state = VNIC_REMOVED;
4159
Nathan Fontenoted651a12017-05-03 14:04:38 -04004160 mutex_unlock(&adapter->reset_lock);
Thomas Falcon40c9db82017-06-12 12:35:04 -05004161 device_remove_file(&dev->dev, &dev_attr_failover);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004162 free_netdev(netdev);
4163 dev_set_drvdata(&dev->dev, NULL);
4164
4165 return 0;
4166}
4167
Thomas Falcon40c9db82017-06-12 12:35:04 -05004168static ssize_t failover_store(struct device *dev, struct device_attribute *attr,
4169 const char *buf, size_t count)
4170{
4171 struct net_device *netdev = dev_get_drvdata(dev);
4172 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
4173 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
4174 __be64 session_token;
4175 long rc;
4176
4177 if (!sysfs_streq(buf, "1"))
4178 return -EINVAL;
4179
4180 rc = plpar_hcall(H_VIOCTL, retbuf, adapter->vdev->unit_address,
4181 H_GET_SESSION_TOKEN, 0, 0, 0);
4182 if (rc) {
4183 netdev_err(netdev, "Couldn't retrieve session token, rc %ld\n",
4184 rc);
4185 return -EINVAL;
4186 }
4187
4188 session_token = (__be64)retbuf[0];
4189 netdev_dbg(netdev, "Initiating client failover, session id %llx\n",
4190 be64_to_cpu(session_token));
4191 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
4192 H_SESSION_ERR_DETECTED, session_token, 0, 0);
4193 if (rc) {
4194 netdev_err(netdev, "Client initiated failover failed, rc %ld\n",
4195 rc);
4196 return -EINVAL;
4197 }
4198
4199 return count;
4200}
4201
4202static DEVICE_ATTR(failover, 0200, NULL, failover_store);
4203
Thomas Falcon032c5e82015-12-21 11:26:06 -06004204static unsigned long ibmvnic_get_desired_dma(struct vio_dev *vdev)
4205{
4206 struct net_device *netdev = dev_get_drvdata(&vdev->dev);
4207 struct ibmvnic_adapter *adapter;
4208 struct iommu_table *tbl;
4209 unsigned long ret = 0;
4210 int i;
4211
4212 tbl = get_iommu_table_base(&vdev->dev);
4213
4214 /* netdev inits at probe time along with the structures we need below*/
4215 if (!netdev)
4216 return IOMMU_PAGE_ALIGN(IBMVNIC_IO_ENTITLEMENT_DEFAULT, tbl);
4217
4218 adapter = netdev_priv(netdev);
4219
4220 ret += PAGE_SIZE; /* the crq message queue */
Thomas Falcon032c5e82015-12-21 11:26:06 -06004221 ret += IOMMU_PAGE_ALIGN(sizeof(struct ibmvnic_statistics), tbl);
4222
4223 for (i = 0; i < adapter->req_tx_queues + adapter->req_rx_queues; i++)
4224 ret += 4 * PAGE_SIZE; /* the scrq message queue */
4225
4226 for (i = 0; i < be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
4227 i++)
4228 ret += adapter->rx_pool[i].size *
4229 IOMMU_PAGE_ALIGN(adapter->rx_pool[i].buff_size, tbl);
4230
4231 return ret;
4232}
4233
4234static int ibmvnic_resume(struct device *dev)
4235{
4236 struct net_device *netdev = dev_get_drvdata(dev);
4237 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004238
John Allencb89ba22017-06-19 11:27:53 -05004239 if (adapter->state != VNIC_OPEN)
4240 return 0;
4241
John Allena2488782017-07-24 13:26:06 -05004242 tasklet_schedule(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004243
4244 return 0;
4245}
4246
Arvind Yadav8c37bc62017-08-17 18:52:54 +05304247static const struct vio_device_id ibmvnic_device_table[] = {
Thomas Falcon032c5e82015-12-21 11:26:06 -06004248 {"network", "IBM,vnic"},
4249 {"", "" }
4250};
4251MODULE_DEVICE_TABLE(vio, ibmvnic_device_table);
4252
4253static const struct dev_pm_ops ibmvnic_pm_ops = {
4254 .resume = ibmvnic_resume
4255};
4256
4257static struct vio_driver ibmvnic_driver = {
4258 .id_table = ibmvnic_device_table,
4259 .probe = ibmvnic_probe,
4260 .remove = ibmvnic_remove,
4261 .get_desired_dma = ibmvnic_get_desired_dma,
4262 .name = ibmvnic_driver_name,
4263 .pm = &ibmvnic_pm_ops,
4264};
4265
4266/* module functions */
4267static int __init ibmvnic_module_init(void)
4268{
4269 pr_info("%s: %s %s\n", ibmvnic_driver_name, ibmvnic_driver_string,
4270 IBMVNIC_DRIVER_VERSION);
4271
4272 return vio_register_driver(&ibmvnic_driver);
4273}
4274
4275static void __exit ibmvnic_module_exit(void)
4276{
4277 vio_unregister_driver(&ibmvnic_driver);
4278}
4279
4280module_init(ibmvnic_module_init);
4281module_exit(ibmvnic_module_exit);