blob: cb701a6c07120a5d9f7bf4e2a55c8c8d25ba2e7c [file] [log] [blame]
Thomas Gleixnerd5bb9942019-05-23 11:14:51 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Thomas Falcon032c5e82015-12-21 11:26:06 -06002/**************************************************************************/
3/* */
4/* IBM System i and System p Virtual NIC Device Driver */
5/* Copyright (C) 2014 IBM Corp. */
6/* Santiago Leon (santi_leon@yahoo.com) */
7/* Thomas Falcon (tlfalcon@linux.vnet.ibm.com) */
8/* John Allen (jallen@linux.vnet.ibm.com) */
9/* */
Thomas Falcon032c5e82015-12-21 11:26:06 -060010/* */
11/* This module contains the implementation of a virtual ethernet device */
12/* for use with IBM i/p Series LPAR Linux. It utilizes the logical LAN */
13/* option of the RS/6000 Platform Architecture to interface with virtual */
14/* ethernet NICs that are presented to the partition by the hypervisor. */
15/* */
16/* Messages are passed between the VNIC driver and the VNIC server using */
17/* Command/Response Queues (CRQs) and sub CRQs (sCRQs). CRQs are used to */
18/* issue and receive commands that initiate communication with the server */
19/* on driver initialization. Sub CRQs (sCRQs) are similar to CRQs, but */
20/* are used by the driver to notify the server that a packet is */
21/* ready for transmission or that a buffer has been added to receive a */
22/* packet. Subsequently, sCRQs are used by the server to notify the */
23/* driver that a packet transmission has been completed or that a packet */
24/* has been received and placed in a waiting buffer. */
25/* */
26/* In lieu of a more conventional "on-the-fly" DMA mapping strategy in */
27/* which skbs are DMA mapped and immediately unmapped when the transmit */
28/* or receive has been completed, the VNIC driver is required to use */
29/* "long term mapping". This entails that large, continuous DMA mapped */
30/* buffers are allocated on driver initialization and these buffers are */
31/* then continuously reused to pass skbs to and from the VNIC server. */
32/* */
33/**************************************************************************/
34
35#include <linux/module.h>
36#include <linux/moduleparam.h>
37#include <linux/types.h>
38#include <linux/errno.h>
39#include <linux/completion.h>
40#include <linux/ioport.h>
41#include <linux/dma-mapping.h>
42#include <linux/kernel.h>
43#include <linux/netdevice.h>
44#include <linux/etherdevice.h>
45#include <linux/skbuff.h>
46#include <linux/init.h>
47#include <linux/delay.h>
48#include <linux/mm.h>
49#include <linux/ethtool.h>
50#include <linux/proc_fs.h>
Thomas Falcon4eb50ce2017-12-18 12:52:40 -060051#include <linux/if_arp.h>
Thomas Falcon032c5e82015-12-21 11:26:06 -060052#include <linux/in.h>
53#include <linux/ip.h>
Thomas Falconad7775d2016-04-01 17:20:34 -050054#include <linux/ipv6.h>
Thomas Falcon032c5e82015-12-21 11:26:06 -060055#include <linux/irq.h>
56#include <linux/kthread.h>
57#include <linux/seq_file.h>
Thomas Falcon032c5e82015-12-21 11:26:06 -060058#include <linux/interrupt.h>
59#include <net/net_namespace.h>
60#include <asm/hvcall.h>
61#include <linux/atomic.h>
62#include <asm/vio.h>
63#include <asm/iommu.h>
64#include <linux/uaccess.h>
65#include <asm/firmware.h>
Thomas Falcon65dc6892016-07-06 15:35:18 -050066#include <linux/workqueue.h>
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -040067#include <linux/if_vlan.h>
Nathan Fontenot37798d02017-11-08 11:23:56 -060068#include <linux/utsname.h>
Thomas Falcon032c5e82015-12-21 11:26:06 -060069
70#include "ibmvnic.h"
71
72static const char ibmvnic_driver_name[] = "ibmvnic";
73static const char ibmvnic_driver_string[] = "IBM System i/p Virtual NIC Driver";
74
Thomas Falcon78b07ac2017-06-01 15:32:34 -050075MODULE_AUTHOR("Santiago Leon");
Thomas Falcon032c5e82015-12-21 11:26:06 -060076MODULE_DESCRIPTION("IBM System i/p Virtual NIC Driver");
77MODULE_LICENSE("GPL");
78MODULE_VERSION(IBMVNIC_DRIVER_VERSION);
79
80static int ibmvnic_version = IBMVNIC_INITIAL_VERSION;
81static int ibmvnic_remove(struct vio_dev *);
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -060082static void release_sub_crqs(struct ibmvnic_adapter *, bool);
Thomas Falcon032c5e82015-12-21 11:26:06 -060083static int ibmvnic_reset_crq(struct ibmvnic_adapter *);
84static int ibmvnic_send_crq_init(struct ibmvnic_adapter *);
85static int ibmvnic_reenable_crq_queue(struct ibmvnic_adapter *);
86static int ibmvnic_send_crq(struct ibmvnic_adapter *, union ibmvnic_crq *);
Thomas Falconad7775d2016-04-01 17:20:34 -050087static int send_subcrq_indirect(struct ibmvnic_adapter *, u64, u64, u64);
Thomas Falcon032c5e82015-12-21 11:26:06 -060088static irqreturn_t ibmvnic_interrupt_rx(int irq, void *instance);
89static int enable_scrq_irq(struct ibmvnic_adapter *,
90 struct ibmvnic_sub_crq_queue *);
91static int disable_scrq_irq(struct ibmvnic_adapter *,
92 struct ibmvnic_sub_crq_queue *);
93static int pending_scrq(struct ibmvnic_adapter *,
94 struct ibmvnic_sub_crq_queue *);
95static union sub_crq *ibmvnic_next_scrq(struct ibmvnic_adapter *,
96 struct ibmvnic_sub_crq_queue *);
97static int ibmvnic_poll(struct napi_struct *napi, int data);
Lijun Pan69980d02020-09-27 20:13:28 -050098static void send_query_map(struct ibmvnic_adapter *adapter);
Thomas Falcon9c4eaab2018-05-23 13:37:57 -050099static int send_request_map(struct ibmvnic_adapter *, dma_addr_t, __be32, u8);
100static int send_request_unmap(struct ibmvnic_adapter *, u8);
Thomas Falcon20a8ab72018-02-26 18:10:59 -0600101static int send_login(struct ibmvnic_adapter *adapter);
Lijun Pan491099a2020-09-27 20:13:26 -0500102static void send_query_cap(struct ibmvnic_adapter *adapter);
Thomas Falcon4d96f122017-08-01 15:04:36 -0500103static int init_sub_crqs(struct ibmvnic_adapter *);
John Allenbd0b6722017-03-17 17:13:40 -0500104static int init_sub_crq_irqs(struct ibmvnic_adapter *adapter);
Lijun Pan635e4422020-08-19 17:52:26 -0500105static int ibmvnic_reset_init(struct ibmvnic_adapter *, bool reset);
Nathan Fontenotf9928872017-03-30 02:48:54 -0400106static void release_crq_queue(struct ibmvnic_adapter *);
Thomas Falcon62740e92019-05-09 23:13:43 -0500107static int __ibmvnic_set_mac(struct net_device *, u8 *);
Nathan Fontenot30f79622018-04-06 18:37:06 -0500108static int init_crq_queue(struct ibmvnic_adapter *adapter);
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -0300109static int send_query_phys_parms(struct ibmvnic_adapter *adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600110
111struct ibmvnic_stat {
112 char name[ETH_GSTRING_LEN];
113 int offset;
114};
115
116#define IBMVNIC_STAT_OFF(stat) (offsetof(struct ibmvnic_adapter, stats) + \
117 offsetof(struct ibmvnic_statistics, stat))
118#define IBMVNIC_GET_STAT(a, off) (*((u64 *)(((unsigned long)(a)) + off)))
119
120static const struct ibmvnic_stat ibmvnic_stats[] = {
121 {"rx_packets", IBMVNIC_STAT_OFF(rx_packets)},
122 {"rx_bytes", IBMVNIC_STAT_OFF(rx_bytes)},
123 {"tx_packets", IBMVNIC_STAT_OFF(tx_packets)},
124 {"tx_bytes", IBMVNIC_STAT_OFF(tx_bytes)},
125 {"ucast_tx_packets", IBMVNIC_STAT_OFF(ucast_tx_packets)},
126 {"ucast_rx_packets", IBMVNIC_STAT_OFF(ucast_rx_packets)},
127 {"mcast_tx_packets", IBMVNIC_STAT_OFF(mcast_tx_packets)},
128 {"mcast_rx_packets", IBMVNIC_STAT_OFF(mcast_rx_packets)},
129 {"bcast_tx_packets", IBMVNIC_STAT_OFF(bcast_tx_packets)},
130 {"bcast_rx_packets", IBMVNIC_STAT_OFF(bcast_rx_packets)},
131 {"align_errors", IBMVNIC_STAT_OFF(align_errors)},
132 {"fcs_errors", IBMVNIC_STAT_OFF(fcs_errors)},
133 {"single_collision_frames", IBMVNIC_STAT_OFF(single_collision_frames)},
134 {"multi_collision_frames", IBMVNIC_STAT_OFF(multi_collision_frames)},
135 {"sqe_test_errors", IBMVNIC_STAT_OFF(sqe_test_errors)},
136 {"deferred_tx", IBMVNIC_STAT_OFF(deferred_tx)},
137 {"late_collisions", IBMVNIC_STAT_OFF(late_collisions)},
138 {"excess_collisions", IBMVNIC_STAT_OFF(excess_collisions)},
139 {"internal_mac_tx_errors", IBMVNIC_STAT_OFF(internal_mac_tx_errors)},
140 {"carrier_sense", IBMVNIC_STAT_OFF(carrier_sense)},
141 {"too_long_frames", IBMVNIC_STAT_OFF(too_long_frames)},
142 {"internal_mac_rx_errors", IBMVNIC_STAT_OFF(internal_mac_rx_errors)},
143};
144
145static long h_reg_sub_crq(unsigned long unit_address, unsigned long token,
146 unsigned long length, unsigned long *number,
147 unsigned long *irq)
148{
149 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
150 long rc;
151
152 rc = plpar_hcall(H_REG_SUB_CRQ, retbuf, unit_address, token, length);
153 *number = retbuf[0];
154 *irq = retbuf[1];
155
156 return rc;
157}
158
Thomas Falcon476d96c2019-11-25 17:12:55 -0600159/**
160 * ibmvnic_wait_for_completion - Check device state and wait for completion
161 * @adapter: private device data
162 * @comp_done: completion structure to wait for
163 * @timeout: time to wait in milliseconds
164 *
165 * Wait for a completion signal or until the timeout limit is reached
166 * while checking that the device is still active.
167 */
168static int ibmvnic_wait_for_completion(struct ibmvnic_adapter *adapter,
169 struct completion *comp_done,
170 unsigned long timeout)
171{
172 struct net_device *netdev;
173 unsigned long div_timeout;
174 u8 retry;
175
176 netdev = adapter->netdev;
177 retry = 5;
178 div_timeout = msecs_to_jiffies(timeout / retry);
179 while (true) {
180 if (!adapter->crq.active) {
181 netdev_err(netdev, "Device down!\n");
182 return -ENODEV;
183 }
Thomas Falcon8f9cc1e2019-12-11 09:38:39 -0600184 if (!retry--)
Thomas Falcon476d96c2019-11-25 17:12:55 -0600185 break;
186 if (wait_for_completion_timeout(comp_done, div_timeout))
187 return 0;
188 }
189 netdev_err(netdev, "Operation timed out.\n");
190 return -ETIMEDOUT;
191}
192
Thomas Falcon032c5e82015-12-21 11:26:06 -0600193static int alloc_long_term_buff(struct ibmvnic_adapter *adapter,
194 struct ibmvnic_long_term_buff *ltb, int size)
195{
196 struct device *dev = &adapter->vdev->dev;
Thomas Falcon9c4eaab2018-05-23 13:37:57 -0500197 int rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600198
199 ltb->size = size;
200 ltb->buff = dma_alloc_coherent(dev, ltb->size, &ltb->addr,
201 GFP_KERNEL);
202
203 if (!ltb->buff) {
204 dev_err(dev, "Couldn't alloc long term buffer\n");
205 return -ENOMEM;
206 }
207 ltb->map_id = adapter->map_id;
208 adapter->map_id++;
Nathan Fontenotdb5d0b52017-02-10 13:45:05 -0500209
Thomas Falconff25dcb2019-11-25 17:12:56 -0600210 mutex_lock(&adapter->fw_lock);
211 adapter->fw_done_rc = 0;
Thomas Falcon070eca92019-11-25 17:12:53 -0600212 reinit_completion(&adapter->fw_done);
Thomas Falcon9c4eaab2018-05-23 13:37:57 -0500213 rc = send_request_map(adapter, ltb->addr,
214 ltb->size, ltb->map_id);
215 if (rc) {
216 dma_free_coherent(dev, ltb->size, ltb->buff, ltb->addr);
Thomas Falconff25dcb2019-11-25 17:12:56 -0600217 mutex_unlock(&adapter->fw_lock);
Thomas Falcon9c4eaab2018-05-23 13:37:57 -0500218 return rc;
219 }
Thomas Falcon476d96c2019-11-25 17:12:55 -0600220
221 rc = ibmvnic_wait_for_completion(adapter, &adapter->fw_done, 10000);
222 if (rc) {
223 dev_err(dev,
224 "Long term map request aborted or timed out,rc = %d\n",
225 rc);
226 dma_free_coherent(dev, ltb->size, ltb->buff, ltb->addr);
Thomas Falconff25dcb2019-11-25 17:12:56 -0600227 mutex_unlock(&adapter->fw_lock);
Thomas Falcon476d96c2019-11-25 17:12:55 -0600228 return rc;
229 }
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500230
231 if (adapter->fw_done_rc) {
232 dev_err(dev, "Couldn't map long term buffer,rc = %d\n",
233 adapter->fw_done_rc);
Thomas Falcon4cf2ddf32018-05-16 15:49:03 -0500234 dma_free_coherent(dev, ltb->size, ltb->buff, ltb->addr);
Thomas Falconff25dcb2019-11-25 17:12:56 -0600235 mutex_unlock(&adapter->fw_lock);
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500236 return -1;
237 }
Thomas Falconff25dcb2019-11-25 17:12:56 -0600238 mutex_unlock(&adapter->fw_lock);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600239 return 0;
240}
241
242static void free_long_term_buff(struct ibmvnic_adapter *adapter,
243 struct ibmvnic_long_term_buff *ltb)
244{
245 struct device *dev = &adapter->vdev->dev;
246
Nathan Fontenotc657e322017-03-30 02:49:06 -0400247 if (!ltb->buff)
248 return;
249
Nathan Fontenoted651a12017-05-03 14:04:38 -0400250 if (adapter->reset_reason != VNIC_RESET_FAILOVER &&
251 adapter->reset_reason != VNIC_RESET_MOBILITY)
Thomas Falcondfad09a2016-08-18 11:37:51 -0500252 send_request_unmap(adapter, ltb->map_id);
Brian King59af56c2017-04-19 13:44:41 -0400253 dma_free_coherent(dev, ltb->size, ltb->buff, ltb->addr);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600254}
255
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500256static int reset_long_term_buff(struct ibmvnic_adapter *adapter,
257 struct ibmvnic_long_term_buff *ltb)
258{
Thomas Falcon476d96c2019-11-25 17:12:55 -0600259 struct device *dev = &adapter->vdev->dev;
Thomas Falcon9c4eaab2018-05-23 13:37:57 -0500260 int rc;
261
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500262 memset(ltb->buff, 0, ltb->size);
263
Thomas Falconff25dcb2019-11-25 17:12:56 -0600264 mutex_lock(&adapter->fw_lock);
265 adapter->fw_done_rc = 0;
266
Thomas Falcon070eca92019-11-25 17:12:53 -0600267 reinit_completion(&adapter->fw_done);
Thomas Falcon9c4eaab2018-05-23 13:37:57 -0500268 rc = send_request_map(adapter, ltb->addr, ltb->size, ltb->map_id);
Thomas Falconff25dcb2019-11-25 17:12:56 -0600269 if (rc) {
270 mutex_unlock(&adapter->fw_lock);
Thomas Falcon9c4eaab2018-05-23 13:37:57 -0500271 return rc;
Thomas Falconff25dcb2019-11-25 17:12:56 -0600272 }
Thomas Falcon476d96c2019-11-25 17:12:55 -0600273
274 rc = ibmvnic_wait_for_completion(adapter, &adapter->fw_done, 10000);
275 if (rc) {
276 dev_info(dev,
277 "Reset failed, long term map request timed out or aborted\n");
Thomas Falconff25dcb2019-11-25 17:12:56 -0600278 mutex_unlock(&adapter->fw_lock);
Thomas Falcon476d96c2019-11-25 17:12:55 -0600279 return rc;
280 }
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500281
282 if (adapter->fw_done_rc) {
Thomas Falcon476d96c2019-11-25 17:12:55 -0600283 dev_info(dev,
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500284 "Reset failed, attempting to free and reallocate buffer\n");
285 free_long_term_buff(adapter, ltb);
Thomas Falconff25dcb2019-11-25 17:12:56 -0600286 mutex_unlock(&adapter->fw_lock);
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500287 return alloc_long_term_buff(adapter, ltb, ltb->size);
288 }
Thomas Falconff25dcb2019-11-25 17:12:56 -0600289 mutex_unlock(&adapter->fw_lock);
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500290 return 0;
291}
292
Thomas Falconf185a492017-05-26 10:30:48 -0400293static void deactivate_rx_pools(struct ibmvnic_adapter *adapter)
294{
295 int i;
296
Thomas Falcon507ebe62020-08-21 13:39:01 -0500297 for (i = 0; i < adapter->num_active_rx_pools; i++)
Thomas Falconf185a492017-05-26 10:30:48 -0400298 adapter->rx_pool[i].active = 0;
299}
300
Thomas Falcon032c5e82015-12-21 11:26:06 -0600301static void replenish_rx_pool(struct ibmvnic_adapter *adapter,
302 struct ibmvnic_rx_pool *pool)
303{
304 int count = pool->size - atomic_read(&pool->available);
Cristobal Fornof3ae59c2020-08-19 13:16:23 -0500305 u64 handle = adapter->rx_scrq[pool->index]->handle;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600306 struct device *dev = &adapter->vdev->dev;
Thomas Falcon4f0b6812020-11-18 19:12:18 -0600307 struct ibmvnic_ind_xmit_queue *ind_bufp;
308 struct ibmvnic_sub_crq_queue *rx_scrq;
309 union sub_crq *sub_crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600310 int buffers_added = 0;
311 unsigned long lpar_rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600312 struct sk_buff *skb;
313 unsigned int offset;
314 dma_addr_t dma_addr;
315 unsigned char *dst;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600316 int shift = 0;
317 int index;
318 int i;
319
Thomas Falconf185a492017-05-26 10:30:48 -0400320 if (!pool->active)
321 return;
322
Thomas Falcon4f0b6812020-11-18 19:12:18 -0600323 rx_scrq = adapter->rx_scrq[pool->index];
324 ind_bufp = &rx_scrq->ind_buf;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600325 for (i = 0; i < count; ++i) {
Dwip N. Banerjeee552aa32020-11-18 19:12:24 -0600326 skb = netdev_alloc_skb(adapter->netdev, pool->buff_size);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600327 if (!skb) {
328 dev_err(dev, "Couldn't replenish rx buff\n");
329 adapter->replenish_no_mem++;
330 break;
331 }
332
333 index = pool->free_map[pool->next_free];
334
335 if (pool->rx_buff[index].skb)
336 dev_err(dev, "Inconsistent free_map!\n");
337
338 /* Copy the skb to the long term mapped DMA buffer */
339 offset = index * pool->buff_size;
340 dst = pool->long_term_buff.buff + offset;
341 memset(dst, 0, pool->buff_size);
342 dma_addr = pool->long_term_buff.addr + offset;
343 pool->rx_buff[index].data = dst;
344
345 pool->free_map[pool->next_free] = IBMVNIC_INVALID_MAP;
346 pool->rx_buff[index].dma = dma_addr;
347 pool->rx_buff[index].skb = skb;
348 pool->rx_buff[index].pool_index = pool->index;
349 pool->rx_buff[index].size = pool->buff_size;
350
Thomas Falcon4f0b6812020-11-18 19:12:18 -0600351 sub_crq = &ind_bufp->indir_arr[ind_bufp->index++];
352 memset(sub_crq, 0, sizeof(*sub_crq));
353 sub_crq->rx_add.first = IBMVNIC_CRQ_CMD;
354 sub_crq->rx_add.correlator =
Thomas Falcon032c5e82015-12-21 11:26:06 -0600355 cpu_to_be64((u64)&pool->rx_buff[index]);
Thomas Falcon4f0b6812020-11-18 19:12:18 -0600356 sub_crq->rx_add.ioba = cpu_to_be32(dma_addr);
357 sub_crq->rx_add.map_id = pool->long_term_buff.map_id;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600358
359 /* The length field of the sCRQ is defined to be 24 bits so the
360 * buffer size needs to be left shifted by a byte before it is
361 * converted to big endian to prevent the last byte from being
362 * truncated.
363 */
364#ifdef __LITTLE_ENDIAN__
365 shift = 8;
366#endif
Thomas Falcon4f0b6812020-11-18 19:12:18 -0600367 sub_crq->rx_add.len = cpu_to_be32(pool->buff_size << shift);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600368 pool->next_free = (pool->next_free + 1) % pool->size;
Thomas Falcon4f0b6812020-11-18 19:12:18 -0600369 if (ind_bufp->index == IBMVNIC_MAX_IND_DESCS ||
370 i == count - 1) {
371 lpar_rc =
372 send_subcrq_indirect(adapter, handle,
373 (u64)ind_bufp->indir_dma,
374 (u64)ind_bufp->index);
375 if (lpar_rc != H_SUCCESS)
376 goto failure;
377 buffers_added += ind_bufp->index;
378 adapter->replenish_add_buff_success += ind_bufp->index;
379 ind_bufp->index = 0;
380 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600381 }
382 atomic_add(buffers_added, &pool->available);
383 return;
384
385failure:
Thomas Falcon2d14d372018-07-13 12:03:32 -0500386 if (lpar_rc != H_PARAMETER && lpar_rc != H_CLOSED)
387 dev_err_ratelimited(dev, "rx: replenish packet buffer failed\n");
Thomas Falcon4f0b6812020-11-18 19:12:18 -0600388 for (i = ind_bufp->index - 1; i >= 0; --i) {
389 struct ibmvnic_rx_buff *rx_buff;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600390
Thomas Falcon4f0b6812020-11-18 19:12:18 -0600391 pool->next_free = pool->next_free == 0 ?
392 pool->size - 1 : pool->next_free - 1;
393 sub_crq = &ind_bufp->indir_arr[i];
394 rx_buff = (struct ibmvnic_rx_buff *)
395 be64_to_cpu(sub_crq->rx_add.correlator);
396 index = (int)(rx_buff - pool->rx_buff);
397 pool->free_map[pool->next_free] = index;
398 dev_kfree_skb_any(pool->rx_buff[index].skb);
399 pool->rx_buff[index].skb = NULL;
400 }
401 ind_bufp->index = 0;
Thomas Falcon5a18e1e2018-04-06 18:37:05 -0500402 if (lpar_rc == H_CLOSED || adapter->failover_pending) {
Thomas Falconf185a492017-05-26 10:30:48 -0400403 /* Disable buffer pool replenishment and report carrier off if
Thomas Falcon5a18e1e2018-04-06 18:37:05 -0500404 * queue is closed or pending failover.
405 * Firmware guarantees that a signal will be sent to the
406 * driver, triggering a reset.
Thomas Falconf185a492017-05-26 10:30:48 -0400407 */
408 deactivate_rx_pools(adapter);
409 netif_carrier_off(adapter->netdev);
410 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600411}
412
413static void replenish_pools(struct ibmvnic_adapter *adapter)
414{
415 int i;
416
Thomas Falcon032c5e82015-12-21 11:26:06 -0600417 adapter->replenish_task_cycles++;
Thomas Falcon507ebe62020-08-21 13:39:01 -0500418 for (i = 0; i < adapter->num_active_rx_pools; i++) {
Thomas Falcon032c5e82015-12-21 11:26:06 -0600419 if (adapter->rx_pool[i].active)
420 replenish_rx_pool(adapter, &adapter->rx_pool[i]);
421 }
Sukadev Bhattiprolu38bd5ce2020-12-04 18:22:35 -0800422
423 netdev_dbg(adapter->netdev, "Replenished %d pools\n", i);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600424}
425
John Allen3d52b592017-08-02 16:44:14 -0500426static void release_stats_buffers(struct ibmvnic_adapter *adapter)
427{
428 kfree(adapter->tx_stats_buffers);
429 kfree(adapter->rx_stats_buffers);
Thomas Falconb0992ec2018-02-06 17:25:23 -0600430 adapter->tx_stats_buffers = NULL;
431 adapter->rx_stats_buffers = NULL;
John Allen3d52b592017-08-02 16:44:14 -0500432}
433
434static int init_stats_buffers(struct ibmvnic_adapter *adapter)
435{
436 adapter->tx_stats_buffers =
Nathan Fontenotabcae542018-02-19 13:30:47 -0600437 kcalloc(IBMVNIC_MAX_QUEUES,
John Allen3d52b592017-08-02 16:44:14 -0500438 sizeof(struct ibmvnic_tx_queue_stats),
439 GFP_KERNEL);
440 if (!adapter->tx_stats_buffers)
441 return -ENOMEM;
442
443 adapter->rx_stats_buffers =
Nathan Fontenotabcae542018-02-19 13:30:47 -0600444 kcalloc(IBMVNIC_MAX_QUEUES,
John Allen3d52b592017-08-02 16:44:14 -0500445 sizeof(struct ibmvnic_rx_queue_stats),
446 GFP_KERNEL);
447 if (!adapter->rx_stats_buffers)
448 return -ENOMEM;
449
450 return 0;
451}
452
Nathan Fontenot7bbc27a2017-03-30 02:49:23 -0400453static void release_stats_token(struct ibmvnic_adapter *adapter)
454{
455 struct device *dev = &adapter->vdev->dev;
456
457 if (!adapter->stats_token)
458 return;
459
460 dma_unmap_single(dev, adapter->stats_token,
461 sizeof(struct ibmvnic_statistics),
462 DMA_FROM_DEVICE);
463 adapter->stats_token = 0;
464}
465
466static int init_stats_token(struct ibmvnic_adapter *adapter)
467{
468 struct device *dev = &adapter->vdev->dev;
469 dma_addr_t stok;
470
471 stok = dma_map_single(dev, &adapter->stats,
472 sizeof(struct ibmvnic_statistics),
473 DMA_FROM_DEVICE);
474 if (dma_mapping_error(dev, stok)) {
475 dev_err(dev, "Couldn't map stats buffer\n");
476 return -1;
477 }
478
479 adapter->stats_token = stok;
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500480 netdev_dbg(adapter->netdev, "Stats token initialized (%llx)\n", stok);
Nathan Fontenot7bbc27a2017-03-30 02:49:23 -0400481 return 0;
482}
483
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400484static int reset_rx_pools(struct ibmvnic_adapter *adapter)
485{
486 struct ibmvnic_rx_pool *rx_pool;
Thomas Falcon507ebe62020-08-21 13:39:01 -0500487 u64 buff_size;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400488 int rx_scrqs;
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500489 int i, j, rc;
John Allen896d8692018-01-18 16:26:31 -0600490
Mingming Cao9f134572020-08-25 13:26:41 -0400491 if (!adapter->rx_pool)
492 return -1;
493
Thomas Falcon507ebe62020-08-21 13:39:01 -0500494 buff_size = adapter->cur_rx_buf_sz;
495 rx_scrqs = adapter->num_active_rx_pools;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400496 for (i = 0; i < rx_scrqs; i++) {
497 rx_pool = &adapter->rx_pool[i];
498
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500499 netdev_dbg(adapter->netdev, "Re-setting rx_pool[%d]\n", i);
500
Thomas Falcon507ebe62020-08-21 13:39:01 -0500501 if (rx_pool->buff_size != buff_size) {
John Allen896d8692018-01-18 16:26:31 -0600502 free_long_term_buff(adapter, &rx_pool->long_term_buff);
Dwip N. Banerjee9a87c3f2020-11-18 19:12:22 -0600503 rx_pool->buff_size = ALIGN(buff_size, L1_CACHE_BYTES);
Thomas Falcon7c940b12019-06-07 16:03:55 -0500504 rc = alloc_long_term_buff(adapter,
505 &rx_pool->long_term_buff,
506 rx_pool->size *
507 rx_pool->buff_size);
John Allen896d8692018-01-18 16:26:31 -0600508 } else {
509 rc = reset_long_term_buff(adapter,
510 &rx_pool->long_term_buff);
511 }
512
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500513 if (rc)
514 return rc;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400515
516 for (j = 0; j < rx_pool->size; j++)
517 rx_pool->free_map[j] = j;
518
519 memset(rx_pool->rx_buff, 0,
520 rx_pool->size * sizeof(struct ibmvnic_rx_buff));
521
522 atomic_set(&rx_pool->available, 0);
523 rx_pool->next_alloc = 0;
524 rx_pool->next_free = 0;
Thomas Falconc3e53b92017-06-14 23:50:05 -0500525 rx_pool->active = 1;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400526 }
527
528 return 0;
529}
530
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400531static void release_rx_pools(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -0600532{
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400533 struct ibmvnic_rx_pool *rx_pool;
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400534 int i, j;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600535
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400536 if (!adapter->rx_pool)
Thomas Falcon032c5e82015-12-21 11:26:06 -0600537 return;
538
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600539 for (i = 0; i < adapter->num_active_rx_pools; i++) {
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400540 rx_pool = &adapter->rx_pool[i];
541
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500542 netdev_dbg(adapter->netdev, "Releasing rx_pool[%d]\n", i);
543
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400544 kfree(rx_pool->free_map);
545 free_long_term_buff(adapter, &rx_pool->long_term_buff);
546
547 if (!rx_pool->rx_buff)
Nathan Fontenote0ebe9422017-05-03 14:04:50 -0400548 continue;
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400549
550 for (j = 0; j < rx_pool->size; j++) {
551 if (rx_pool->rx_buff[j].skb) {
Thomas Falconb7cdec32018-11-21 11:17:58 -0600552 dev_kfree_skb_any(rx_pool->rx_buff[j].skb);
553 rx_pool->rx_buff[j].skb = NULL;
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400554 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600555 }
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400556
557 kfree(rx_pool->rx_buff);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600558 }
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400559
560 kfree(adapter->rx_pool);
561 adapter->rx_pool = NULL;
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600562 adapter->num_active_rx_pools = 0;
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400563}
564
565static int init_rx_pools(struct net_device *netdev)
566{
567 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
568 struct device *dev = &adapter->vdev->dev;
569 struct ibmvnic_rx_pool *rx_pool;
570 int rxadd_subcrqs;
Thomas Falcon507ebe62020-08-21 13:39:01 -0500571 u64 buff_size;
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400572 int i, j;
573
Thomas Falcon507ebe62020-08-21 13:39:01 -0500574 rxadd_subcrqs = adapter->num_active_rx_scrqs;
575 buff_size = adapter->cur_rx_buf_sz;
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400576
577 adapter->rx_pool = kcalloc(rxadd_subcrqs,
578 sizeof(struct ibmvnic_rx_pool),
579 GFP_KERNEL);
580 if (!adapter->rx_pool) {
581 dev_err(dev, "Failed to allocate rx pools\n");
582 return -1;
583 }
584
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600585 adapter->num_active_rx_pools = rxadd_subcrqs;
586
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400587 for (i = 0; i < rxadd_subcrqs; i++) {
588 rx_pool = &adapter->rx_pool[i];
589
590 netdev_dbg(adapter->netdev,
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500591 "Initializing rx_pool[%d], %lld buffs, %lld bytes each\n",
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400592 i, adapter->req_rx_add_entries_per_subcrq,
Thomas Falcon507ebe62020-08-21 13:39:01 -0500593 buff_size);
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400594
595 rx_pool->size = adapter->req_rx_add_entries_per_subcrq;
596 rx_pool->index = i;
Dwip N. Banerjee9a87c3f2020-11-18 19:12:22 -0600597 rx_pool->buff_size = ALIGN(buff_size, L1_CACHE_BYTES);
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400598 rx_pool->active = 1;
599
600 rx_pool->free_map = kcalloc(rx_pool->size, sizeof(int),
601 GFP_KERNEL);
602 if (!rx_pool->free_map) {
603 release_rx_pools(adapter);
604 return -1;
605 }
606
607 rx_pool->rx_buff = kcalloc(rx_pool->size,
608 sizeof(struct ibmvnic_rx_buff),
609 GFP_KERNEL);
610 if (!rx_pool->rx_buff) {
611 dev_err(dev, "Couldn't alloc rx buffers\n");
612 release_rx_pools(adapter);
613 return -1;
614 }
615
616 if (alloc_long_term_buff(adapter, &rx_pool->long_term_buff,
617 rx_pool->size * rx_pool->buff_size)) {
618 release_rx_pools(adapter);
619 return -1;
620 }
621
622 for (j = 0; j < rx_pool->size; ++j)
623 rx_pool->free_map[j] = j;
624
625 atomic_set(&rx_pool->available, 0);
626 rx_pool->next_alloc = 0;
627 rx_pool->next_free = 0;
628 }
629
630 return 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600631}
632
Thomas Falcone26dc252018-03-16 20:00:25 -0500633static int reset_one_tx_pool(struct ibmvnic_adapter *adapter,
634 struct ibmvnic_tx_pool *tx_pool)
635{
636 int rc, i;
637
638 rc = reset_long_term_buff(adapter, &tx_pool->long_term_buff);
639 if (rc)
640 return rc;
641
642 memset(tx_pool->tx_buff, 0,
643 tx_pool->num_buffers *
644 sizeof(struct ibmvnic_tx_buff));
645
646 for (i = 0; i < tx_pool->num_buffers; i++)
647 tx_pool->free_map[i] = i;
648
649 tx_pool->consumer_index = 0;
650 tx_pool->producer_index = 0;
651
652 return 0;
653}
654
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400655static int reset_tx_pools(struct ibmvnic_adapter *adapter)
656{
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400657 int tx_scrqs;
Thomas Falcone26dc252018-03-16 20:00:25 -0500658 int i, rc;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400659
Mingming Cao9f134572020-08-25 13:26:41 -0400660 if (!adapter->tx_pool)
661 return -1;
662
Thomas Falcon507ebe62020-08-21 13:39:01 -0500663 tx_scrqs = adapter->num_active_tx_pools;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400664 for (i = 0; i < tx_scrqs; i++) {
Thomas Falcone26dc252018-03-16 20:00:25 -0500665 rc = reset_one_tx_pool(adapter, &adapter->tso_pool[i]);
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500666 if (rc)
667 return rc;
Thomas Falcone26dc252018-03-16 20:00:25 -0500668 rc = reset_one_tx_pool(adapter, &adapter->tx_pool[i]);
Thomas Falconfdb06102017-10-17 12:36:55 -0500669 if (rc)
670 return rc;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400671 }
672
673 return 0;
674}
675
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -0200676static void release_vpd_data(struct ibmvnic_adapter *adapter)
677{
678 if (!adapter->vpd)
679 return;
680
681 kfree(adapter->vpd->buff);
682 kfree(adapter->vpd);
Thomas Falconb0992ec2018-02-06 17:25:23 -0600683
684 adapter->vpd = NULL;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -0200685}
686
Thomas Falconfb794212018-03-16 20:00:26 -0500687static void release_one_tx_pool(struct ibmvnic_adapter *adapter,
688 struct ibmvnic_tx_pool *tx_pool)
689{
690 kfree(tx_pool->tx_buff);
691 kfree(tx_pool->free_map);
692 free_long_term_buff(adapter, &tx_pool->long_term_buff);
693}
694
Nathan Fontenotc657e322017-03-30 02:49:06 -0400695static void release_tx_pools(struct ibmvnic_adapter *adapter)
696{
John Allen896d8692018-01-18 16:26:31 -0600697 int i;
Nathan Fontenotc657e322017-03-30 02:49:06 -0400698
699 if (!adapter->tx_pool)
700 return;
701
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600702 for (i = 0; i < adapter->num_active_tx_pools; i++) {
Thomas Falconfb794212018-03-16 20:00:26 -0500703 release_one_tx_pool(adapter, &adapter->tx_pool[i]);
704 release_one_tx_pool(adapter, &adapter->tso_pool[i]);
Nathan Fontenotc657e322017-03-30 02:49:06 -0400705 }
706
707 kfree(adapter->tx_pool);
708 adapter->tx_pool = NULL;
Thomas Falconfb794212018-03-16 20:00:26 -0500709 kfree(adapter->tso_pool);
710 adapter->tso_pool = NULL;
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600711 adapter->num_active_tx_pools = 0;
Nathan Fontenotc657e322017-03-30 02:49:06 -0400712}
713
Thomas Falcon32053062018-03-16 20:00:27 -0500714static int init_one_tx_pool(struct net_device *netdev,
715 struct ibmvnic_tx_pool *tx_pool,
716 int num_entries, int buf_size)
717{
718 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
719 int i;
720
721 tx_pool->tx_buff = kcalloc(num_entries,
722 sizeof(struct ibmvnic_tx_buff),
723 GFP_KERNEL);
724 if (!tx_pool->tx_buff)
725 return -1;
726
727 if (alloc_long_term_buff(adapter, &tx_pool->long_term_buff,
728 num_entries * buf_size))
729 return -1;
730
731 tx_pool->free_map = kcalloc(num_entries, sizeof(int), GFP_KERNEL);
732 if (!tx_pool->free_map)
733 return -1;
734
735 for (i = 0; i < num_entries; i++)
736 tx_pool->free_map[i] = i;
737
738 tx_pool->consumer_index = 0;
739 tx_pool->producer_index = 0;
740 tx_pool->num_buffers = num_entries;
741 tx_pool->buf_size = buf_size;
742
743 return 0;
744}
745
Nathan Fontenotc657e322017-03-30 02:49:06 -0400746static int init_tx_pools(struct net_device *netdev)
747{
748 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Nathan Fontenotc657e322017-03-30 02:49:06 -0400749 int tx_subcrqs;
Dwip N. Banerjee9a87c3f2020-11-18 19:12:22 -0600750 u64 buff_size;
Thomas Falcon32053062018-03-16 20:00:27 -0500751 int i, rc;
Nathan Fontenotc657e322017-03-30 02:49:06 -0400752
Thomas Falcon507ebe62020-08-21 13:39:01 -0500753 tx_subcrqs = adapter->num_active_tx_scrqs;
Nathan Fontenotc657e322017-03-30 02:49:06 -0400754 adapter->tx_pool = kcalloc(tx_subcrqs,
755 sizeof(struct ibmvnic_tx_pool), GFP_KERNEL);
756 if (!adapter->tx_pool)
757 return -1;
758
Thomas Falcon32053062018-03-16 20:00:27 -0500759 adapter->tso_pool = kcalloc(tx_subcrqs,
760 sizeof(struct ibmvnic_tx_pool), GFP_KERNEL);
761 if (!adapter->tso_pool)
762 return -1;
763
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600764 adapter->num_active_tx_pools = tx_subcrqs;
765
Nathan Fontenotc657e322017-03-30 02:49:06 -0400766 for (i = 0; i < tx_subcrqs; i++) {
Dwip N. Banerjee9a87c3f2020-11-18 19:12:22 -0600767 buff_size = adapter->req_mtu + VLAN_HLEN;
768 buff_size = ALIGN(buff_size, L1_CACHE_BYTES);
Thomas Falcon32053062018-03-16 20:00:27 -0500769 rc = init_one_tx_pool(netdev, &adapter->tx_pool[i],
770 adapter->req_tx_entries_per_subcrq,
Dwip N. Banerjee9a87c3f2020-11-18 19:12:22 -0600771 buff_size);
Thomas Falcon32053062018-03-16 20:00:27 -0500772 if (rc) {
Nathan Fontenotc657e322017-03-30 02:49:06 -0400773 release_tx_pools(adapter);
Thomas Falcon32053062018-03-16 20:00:27 -0500774 return rc;
Nathan Fontenotc657e322017-03-30 02:49:06 -0400775 }
776
Thomas Falcon7c940b12019-06-07 16:03:55 -0500777 rc = init_one_tx_pool(netdev, &adapter->tso_pool[i],
778 IBMVNIC_TSO_BUFS,
779 IBMVNIC_TSO_BUF_SZ);
Thomas Falcon32053062018-03-16 20:00:27 -0500780 if (rc) {
Nathan Fontenotc657e322017-03-30 02:49:06 -0400781 release_tx_pools(adapter);
Thomas Falcon32053062018-03-16 20:00:27 -0500782 return rc;
Nathan Fontenotc657e322017-03-30 02:49:06 -0400783 }
Nathan Fontenotc657e322017-03-30 02:49:06 -0400784 }
785
786 return 0;
787}
788
John Allend944c3d62017-05-26 10:30:13 -0400789static void ibmvnic_napi_enable(struct ibmvnic_adapter *adapter)
790{
791 int i;
792
793 if (adapter->napi_enabled)
794 return;
795
796 for (i = 0; i < adapter->req_rx_queues; i++)
797 napi_enable(&adapter->napi[i]);
798
799 adapter->napi_enabled = true;
800}
801
802static void ibmvnic_napi_disable(struct ibmvnic_adapter *adapter)
803{
804 int i;
805
806 if (!adapter->napi_enabled)
807 return;
808
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500809 for (i = 0; i < adapter->req_rx_queues; i++) {
810 netdev_dbg(adapter->netdev, "Disabling napi[%d]\n", i);
John Allend944c3d62017-05-26 10:30:13 -0400811 napi_disable(&adapter->napi[i]);
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500812 }
John Allend944c3d62017-05-26 10:30:13 -0400813
814 adapter->napi_enabled = false;
815}
816
Nathan Fontenot86f669b2018-02-19 13:30:39 -0600817static int init_napi(struct ibmvnic_adapter *adapter)
818{
819 int i;
820
821 adapter->napi = kcalloc(adapter->req_rx_queues,
822 sizeof(struct napi_struct), GFP_KERNEL);
823 if (!adapter->napi)
824 return -ENOMEM;
825
826 for (i = 0; i < adapter->req_rx_queues; i++) {
827 netdev_dbg(adapter->netdev, "Adding napi[%d]\n", i);
828 netif_napi_add(adapter->netdev, &adapter->napi[i],
829 ibmvnic_poll, NAPI_POLL_WEIGHT);
830 }
831
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600832 adapter->num_active_rx_napi = adapter->req_rx_queues;
Nathan Fontenot86f669b2018-02-19 13:30:39 -0600833 return 0;
834}
835
836static void release_napi(struct ibmvnic_adapter *adapter)
837{
838 int i;
839
840 if (!adapter->napi)
841 return;
842
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600843 for (i = 0; i < adapter->num_active_rx_napi; i++) {
Wen Yang390de192018-12-11 12:20:46 +0800844 netdev_dbg(adapter->netdev, "Releasing napi[%d]\n", i);
845 netif_napi_del(&adapter->napi[i]);
Nathan Fontenot86f669b2018-02-19 13:30:39 -0600846 }
847
848 kfree(adapter->napi);
849 adapter->napi = NULL;
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600850 adapter->num_active_rx_napi = 0;
Thomas Falconc3f22412018-05-23 13:37:55 -0500851 adapter->napi_enabled = false;
Nathan Fontenot86f669b2018-02-19 13:30:39 -0600852}
853
John Allena57a5d22017-03-17 17:13:41 -0500854static int ibmvnic_login(struct net_device *netdev)
Thomas Falcon032c5e82015-12-21 11:26:06 -0600855{
856 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Dany Madden98c41f02020-11-25 18:04:32 -0600857 unsigned long timeout = msecs_to_jiffies(20000);
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500858 int retry_count = 0;
Thomas Falcondff515a32020-06-15 10:29:23 -0500859 int retries = 10;
Thomas Falconeb110412018-05-24 14:37:53 -0500860 bool retry;
Thomas Falcon4d96f122017-08-01 15:04:36 -0500861 int rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600862
John Allenbd0b6722017-03-17 17:13:40 -0500863 do {
Thomas Falconeb110412018-05-24 14:37:53 -0500864 retry = false;
Thomas Falcondff515a32020-06-15 10:29:23 -0500865 if (retry_count > retries) {
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500866 netdev_warn(netdev, "Login attempts exceeded\n");
867 return -1;
868 }
869
870 adapter->init_done_rc = 0;
871 reinit_completion(&adapter->init_done);
872 rc = send_login(adapter);
Dany Maddenc98d9cc2020-11-25 18:04:30 -0600873 if (rc)
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500874 return rc;
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500875
876 if (!wait_for_completion_timeout(&adapter->init_done,
877 timeout)) {
Thomas Falcondff515a32020-06-15 10:29:23 -0500878 netdev_warn(netdev, "Login timed out, retrying...\n");
879 retry = true;
880 adapter->init_done_rc = 0;
881 retry_count++;
882 continue;
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500883 }
884
Thomas Falcondff515a32020-06-15 10:29:23 -0500885 if (adapter->init_done_rc == ABORTED) {
886 netdev_warn(netdev, "Login aborted, retrying...\n");
887 retry = true;
888 adapter->init_done_rc = 0;
889 retry_count++;
890 /* FW or device may be busy, so
891 * wait a bit before retrying login
892 */
893 msleep(500);
894 } else if (adapter->init_done_rc == PARTIALSUCCESS) {
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500895 retry_count++;
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -0600896 release_sub_crqs(adapter, 1);
John Allenbd0b6722017-03-17 17:13:40 -0500897
Thomas Falconeb110412018-05-24 14:37:53 -0500898 retry = true;
899 netdev_dbg(netdev,
900 "Received partial success, retrying...\n");
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500901 adapter->init_done_rc = 0;
John Allenbd0b6722017-03-17 17:13:40 -0500902 reinit_completion(&adapter->init_done);
Lijun Pan491099a2020-09-27 20:13:26 -0500903 send_query_cap(adapter);
John Allenbd0b6722017-03-17 17:13:40 -0500904 if (!wait_for_completion_timeout(&adapter->init_done,
905 timeout)) {
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500906 netdev_warn(netdev,
907 "Capabilities query timed out\n");
John Allenbd0b6722017-03-17 17:13:40 -0500908 return -1;
909 }
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500910
Thomas Falcon4d96f122017-08-01 15:04:36 -0500911 rc = init_sub_crqs(adapter);
912 if (rc) {
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500913 netdev_warn(netdev,
914 "SCRQ initialization failed\n");
Thomas Falcon4d96f122017-08-01 15:04:36 -0500915 return -1;
916 }
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500917
Thomas Falcon4d96f122017-08-01 15:04:36 -0500918 rc = init_sub_crq_irqs(adapter);
919 if (rc) {
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500920 netdev_warn(netdev,
921 "SCRQ irq initialization failed\n");
Thomas Falcon4d96f122017-08-01 15:04:36 -0500922 return -1;
923 }
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500924 } else if (adapter->init_done_rc) {
925 netdev_warn(netdev, "Adapter login failed\n");
John Allenbd0b6722017-03-17 17:13:40 -0500926 return -1;
927 }
Thomas Falconeb110412018-05-24 14:37:53 -0500928 } while (retry);
John Allenbd0b6722017-03-17 17:13:40 -0500929
Thomas Falcon62740e92019-05-09 23:13:43 -0500930 __ibmvnic_set_mac(netdev, adapter->mac_addr);
Thomas Falcon3d166132018-01-10 19:39:52 -0600931
Sukadev Bhattiprolu38bd5ce2020-12-04 18:22:35 -0800932 netdev_dbg(netdev, "[S:%d] Login succeeded\n", adapter->state);
John Allena57a5d22017-03-17 17:13:41 -0500933 return 0;
934}
935
Thomas Falcon34f0f4e2018-02-13 18:23:40 -0600936static void release_login_buffer(struct ibmvnic_adapter *adapter)
937{
938 kfree(adapter->login_buf);
939 adapter->login_buf = NULL;
940}
941
942static void release_login_rsp_buffer(struct ibmvnic_adapter *adapter)
943{
944 kfree(adapter->login_rsp_buf);
945 adapter->login_rsp_buf = NULL;
946}
947
Nathan Fontenot1b8955e2017-03-30 02:49:29 -0400948static void release_resources(struct ibmvnic_adapter *adapter)
949{
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -0200950 release_vpd_data(adapter);
951
Nathan Fontenot1b8955e2017-03-30 02:49:29 -0400952 release_tx_pools(adapter);
953 release_rx_pools(adapter);
954
Nathan Fontenot86f669b2018-02-19 13:30:39 -0600955 release_napi(adapter);
Thomas Falcon34f0f4e2018-02-13 18:23:40 -0600956 release_login_rsp_buffer(adapter);
Nathan Fontenot1b8955e2017-03-30 02:49:29 -0400957}
958
Nathan Fontenot53da09e2017-04-21 15:39:04 -0400959static int set_link_state(struct ibmvnic_adapter *adapter, u8 link_state)
960{
961 struct net_device *netdev = adapter->netdev;
Dany Madden98c41f02020-11-25 18:04:32 -0600962 unsigned long timeout = msecs_to_jiffies(20000);
Nathan Fontenot53da09e2017-04-21 15:39:04 -0400963 union ibmvnic_crq crq;
964 bool resend;
965 int rc;
966
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500967 netdev_dbg(netdev, "setting link state %d\n", link_state);
968
Nathan Fontenot53da09e2017-04-21 15:39:04 -0400969 memset(&crq, 0, sizeof(crq));
970 crq.logical_link_state.first = IBMVNIC_CRQ_CMD;
971 crq.logical_link_state.cmd = LOGICAL_LINK_STATE;
972 crq.logical_link_state.link_state = link_state;
973
974 do {
975 resend = false;
976
977 reinit_completion(&adapter->init_done);
978 rc = ibmvnic_send_crq(adapter, &crq);
979 if (rc) {
980 netdev_err(netdev, "Failed to set link state\n");
981 return rc;
982 }
983
984 if (!wait_for_completion_timeout(&adapter->init_done,
985 timeout)) {
986 netdev_err(netdev, "timeout setting link state\n");
987 return -1;
988 }
989
Lijun Pan4c5f6af2020-08-19 17:52:23 -0500990 if (adapter->init_done_rc == PARTIALSUCCESS) {
Nathan Fontenot53da09e2017-04-21 15:39:04 -0400991 /* Partuial success, delay and re-send */
992 mdelay(1000);
993 resend = true;
Thomas Falconab5ec332018-05-23 13:37:59 -0500994 } else if (adapter->init_done_rc) {
995 netdev_warn(netdev, "Unable to set link state, rc=%d\n",
996 adapter->init_done_rc);
997 return adapter->init_done_rc;
Nathan Fontenot53da09e2017-04-21 15:39:04 -0400998 }
999 } while (resend);
1000
1001 return 0;
1002}
1003
Thomas Falcon7f3c6e62017-04-21 15:38:40 -04001004static int set_real_num_queues(struct net_device *netdev)
1005{
1006 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1007 int rc;
1008
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001009 netdev_dbg(netdev, "Setting real tx/rx queues (%llx/%llx)\n",
1010 adapter->req_tx_queues, adapter->req_rx_queues);
1011
Thomas Falcon7f3c6e62017-04-21 15:38:40 -04001012 rc = netif_set_real_num_tx_queues(netdev, adapter->req_tx_queues);
1013 if (rc) {
1014 netdev_err(netdev, "failed to set the number of tx queues\n");
1015 return rc;
1016 }
1017
1018 rc = netif_set_real_num_rx_queues(netdev, adapter->req_rx_queues);
1019 if (rc)
1020 netdev_err(netdev, "failed to set the number of rx queues\n");
1021
1022 return rc;
1023}
1024
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001025static int ibmvnic_get_vpd(struct ibmvnic_adapter *adapter)
1026{
1027 struct device *dev = &adapter->vdev->dev;
1028 union ibmvnic_crq crq;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001029 int len = 0;
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05001030 int rc;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001031
1032 if (adapter->vpd->buff)
1033 len = adapter->vpd->len;
1034
Thomas Falconff25dcb2019-11-25 17:12:56 -06001035 mutex_lock(&adapter->fw_lock);
1036 adapter->fw_done_rc = 0;
Thomas Falcon070eca92019-11-25 17:12:53 -06001037 reinit_completion(&adapter->fw_done);
Thomas Falconff25dcb2019-11-25 17:12:56 -06001038
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001039 crq.get_vpd_size.first = IBMVNIC_CRQ_CMD;
1040 crq.get_vpd_size.cmd = GET_VPD_SIZE;
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05001041 rc = ibmvnic_send_crq(adapter, &crq);
Thomas Falconff25dcb2019-11-25 17:12:56 -06001042 if (rc) {
1043 mutex_unlock(&adapter->fw_lock);
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05001044 return rc;
Thomas Falconff25dcb2019-11-25 17:12:56 -06001045 }
Thomas Falcon476d96c2019-11-25 17:12:55 -06001046
1047 rc = ibmvnic_wait_for_completion(adapter, &adapter->fw_done, 10000);
1048 if (rc) {
1049 dev_err(dev, "Could not retrieve VPD size, rc = %d\n", rc);
Thomas Falconff25dcb2019-11-25 17:12:56 -06001050 mutex_unlock(&adapter->fw_lock);
Thomas Falcon476d96c2019-11-25 17:12:55 -06001051 return rc;
1052 }
Thomas Falconff25dcb2019-11-25 17:12:56 -06001053 mutex_unlock(&adapter->fw_lock);
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001054
1055 if (!adapter->vpd->len)
1056 return -ENODATA;
1057
1058 if (!adapter->vpd->buff)
1059 adapter->vpd->buff = kzalloc(adapter->vpd->len, GFP_KERNEL);
1060 else if (adapter->vpd->len != len)
1061 adapter->vpd->buff =
1062 krealloc(adapter->vpd->buff,
1063 adapter->vpd->len, GFP_KERNEL);
1064
1065 if (!adapter->vpd->buff) {
1066 dev_err(dev, "Could allocate VPD buffer\n");
1067 return -ENOMEM;
1068 }
1069
1070 adapter->vpd->dma_addr =
1071 dma_map_single(dev, adapter->vpd->buff, adapter->vpd->len,
1072 DMA_FROM_DEVICE);
Desnes Augusto Nunes do Rosariof7431062017-11-17 09:09:04 -02001073 if (dma_mapping_error(dev, adapter->vpd->dma_addr)) {
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001074 dev_err(dev, "Could not map VPD buffer\n");
1075 kfree(adapter->vpd->buff);
Thomas Falconb0992ec2018-02-06 17:25:23 -06001076 adapter->vpd->buff = NULL;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001077 return -ENOMEM;
1078 }
1079
Thomas Falconff25dcb2019-11-25 17:12:56 -06001080 mutex_lock(&adapter->fw_lock);
1081 adapter->fw_done_rc = 0;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001082 reinit_completion(&adapter->fw_done);
Thomas Falconff25dcb2019-11-25 17:12:56 -06001083
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001084 crq.get_vpd.first = IBMVNIC_CRQ_CMD;
1085 crq.get_vpd.cmd = GET_VPD;
1086 crq.get_vpd.ioba = cpu_to_be32(adapter->vpd->dma_addr);
1087 crq.get_vpd.len = cpu_to_be32((u32)adapter->vpd->len);
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05001088 rc = ibmvnic_send_crq(adapter, &crq);
1089 if (rc) {
1090 kfree(adapter->vpd->buff);
1091 adapter->vpd->buff = NULL;
Thomas Falconff25dcb2019-11-25 17:12:56 -06001092 mutex_unlock(&adapter->fw_lock);
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05001093 return rc;
1094 }
Thomas Falcon476d96c2019-11-25 17:12:55 -06001095
1096 rc = ibmvnic_wait_for_completion(adapter, &adapter->fw_done, 10000);
1097 if (rc) {
1098 dev_err(dev, "Unable to retrieve VPD, rc = %d\n", rc);
1099 kfree(adapter->vpd->buff);
1100 adapter->vpd->buff = NULL;
Thomas Falconff25dcb2019-11-25 17:12:56 -06001101 mutex_unlock(&adapter->fw_lock);
Thomas Falcon476d96c2019-11-25 17:12:55 -06001102 return rc;
1103 }
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001104
Thomas Falconff25dcb2019-11-25 17:12:56 -06001105 mutex_unlock(&adapter->fw_lock);
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001106 return 0;
1107}
1108
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001109static int init_resources(struct ibmvnic_adapter *adapter)
John Allena57a5d22017-03-17 17:13:41 -05001110{
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001111 struct net_device *netdev = adapter->netdev;
Nathan Fontenot86f669b2018-02-19 13:30:39 -06001112 int rc;
John Allena57a5d22017-03-17 17:13:41 -05001113
Thomas Falcon7f3c6e62017-04-21 15:38:40 -04001114 rc = set_real_num_queues(netdev);
1115 if (rc)
1116 return rc;
John Allenbd0b6722017-03-17 17:13:40 -05001117
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001118 adapter->vpd = kzalloc(sizeof(*adapter->vpd), GFP_KERNEL);
1119 if (!adapter->vpd)
1120 return -ENOMEM;
1121
John Allen69d08dc2018-01-18 16:27:58 -06001122 /* Vital Product Data (VPD) */
1123 rc = ibmvnic_get_vpd(adapter);
1124 if (rc) {
1125 netdev_err(netdev, "failed to initialize Vital Product Data (VPD)\n");
1126 return rc;
1127 }
1128
Thomas Falcon032c5e82015-12-21 11:26:06 -06001129 adapter->map_id = 1;
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001130
Nathan Fontenot86f669b2018-02-19 13:30:39 -06001131 rc = init_napi(adapter);
1132 if (rc)
1133 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001134
Lijun Pan69980d02020-09-27 20:13:28 -05001135 send_query_map(adapter);
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -04001136
1137 rc = init_rx_pools(netdev);
1138 if (rc)
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001139 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001140
Nathan Fontenotc657e322017-03-30 02:49:06 -04001141 rc = init_tx_pools(netdev);
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001142 return rc;
1143}
1144
Nathan Fontenoted651a12017-05-03 14:04:38 -04001145static int __ibmvnic_open(struct net_device *netdev)
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001146{
1147 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001148 enum vnic_state prev_state = adapter->state;
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001149 int i, rc;
1150
Nathan Fontenot90c80142017-05-03 14:04:32 -04001151 adapter->state = VNIC_OPENING;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001152 replenish_pools(adapter);
John Allend944c3d62017-05-26 10:30:13 -04001153 ibmvnic_napi_enable(adapter);
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001154
Thomas Falcon032c5e82015-12-21 11:26:06 -06001155 /* We're ready to receive frames, enable the sub-crq interrupts and
1156 * set the logical link state to up
1157 */
Nathan Fontenoted651a12017-05-03 14:04:38 -04001158 for (i = 0; i < adapter->req_rx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001159 netdev_dbg(netdev, "Enabling rx_scrq[%d] irq\n", i);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001160 if (prev_state == VNIC_CLOSED)
1161 enable_irq(adapter->rx_scrq[i]->irq);
Thomas Falconf23e0642018-04-15 18:53:36 -05001162 enable_scrq_irq(adapter, adapter->rx_scrq[i]);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001163 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001164
Nathan Fontenoted651a12017-05-03 14:04:38 -04001165 for (i = 0; i < adapter->req_tx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001166 netdev_dbg(netdev, "Enabling tx_scrq[%d] irq\n", i);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001167 if (prev_state == VNIC_CLOSED)
1168 enable_irq(adapter->tx_scrq[i]->irq);
Thomas Falconf23e0642018-04-15 18:53:36 -05001169 enable_scrq_irq(adapter, adapter->tx_scrq[i]);
Thomas Falcon0d973382020-11-18 19:12:19 -06001170 netdev_tx_reset_queue(netdev_get_tx_queue(netdev, i));
Nathan Fontenoted651a12017-05-03 14:04:38 -04001171 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001172
Nathan Fontenot53da09e2017-04-21 15:39:04 -04001173 rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_UP);
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001174 if (rc) {
1175 for (i = 0; i < adapter->req_rx_queues; i++)
1176 napi_disable(&adapter->napi[i]);
1177 release_resources(adapter);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001178 return rc;
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001179 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001180
Nathan Fontenoted651a12017-05-03 14:04:38 -04001181 netif_tx_start_all_queues(netdev);
1182
1183 if (prev_state == VNIC_CLOSED) {
1184 for (i = 0; i < adapter->req_rx_queues; i++)
1185 napi_schedule(&adapter->napi[i]);
1186 }
1187
1188 adapter->state = VNIC_OPEN;
1189 return rc;
1190}
1191
1192static int ibmvnic_open(struct net_device *netdev)
1193{
1194 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
John Allen69d08dc2018-01-18 16:27:58 -06001195 int rc;
Nathan Fontenoted651a12017-05-03 14:04:38 -04001196
Thomas Falcon5a18e1e2018-04-06 18:37:05 -05001197 /* If device failover is pending, just set device state and return.
1198 * Device operation will be handled by reset routine.
1199 */
1200 if (adapter->failover_pending) {
1201 adapter->state = VNIC_OPEN;
1202 return 0;
1203 }
1204
Nathan Fontenoted651a12017-05-03 14:04:38 -04001205 if (adapter->state != VNIC_CLOSED) {
1206 rc = ibmvnic_login(netdev);
Juliet Kima5681e22018-11-19 15:59:22 -06001207 if (rc)
Sukadev Bhattiprolu1d850492020-10-30 10:07:11 -07001208 goto out;
Nathan Fontenoted651a12017-05-03 14:04:38 -04001209
1210 rc = init_resources(adapter);
1211 if (rc) {
1212 netdev_err(netdev, "failed to initialize resources\n");
1213 release_resources(adapter);
Sukadev Bhattiprolu1d850492020-10-30 10:07:11 -07001214 goto out;
Nathan Fontenoted651a12017-05-03 14:04:38 -04001215 }
1216 }
1217
1218 rc = __ibmvnic_open(netdev);
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001219
Sukadev Bhattiprolu1d850492020-10-30 10:07:11 -07001220out:
1221 /*
1222 * If open fails due to a pending failover, set device state and
1223 * return. Device operation will be handled by reset routine.
1224 */
1225 if (rc && adapter->failover_pending) {
1226 adapter->state = VNIC_OPEN;
1227 rc = 0;
1228 }
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001229 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001230}
1231
Thomas Falcond0869c02018-02-13 18:23:43 -06001232static void clean_rx_pools(struct ibmvnic_adapter *adapter)
1233{
1234 struct ibmvnic_rx_pool *rx_pool;
Thomas Falcon637f81d2018-02-26 18:10:57 -06001235 struct ibmvnic_rx_buff *rx_buff;
Thomas Falcond0869c02018-02-13 18:23:43 -06001236 u64 rx_entries;
1237 int rx_scrqs;
1238 int i, j;
1239
1240 if (!adapter->rx_pool)
1241 return;
1242
Thomas Falcon660e3092018-04-20 14:25:32 -05001243 rx_scrqs = adapter->num_active_rx_pools;
Thomas Falcond0869c02018-02-13 18:23:43 -06001244 rx_entries = adapter->req_rx_add_entries_per_subcrq;
1245
1246 /* Free any remaining skbs in the rx buffer pools */
1247 for (i = 0; i < rx_scrqs; i++) {
1248 rx_pool = &adapter->rx_pool[i];
Thomas Falcon637f81d2018-02-26 18:10:57 -06001249 if (!rx_pool || !rx_pool->rx_buff)
Thomas Falcond0869c02018-02-13 18:23:43 -06001250 continue;
1251
1252 netdev_dbg(adapter->netdev, "Cleaning rx_pool[%d]\n", i);
1253 for (j = 0; j < rx_entries; j++) {
Thomas Falcon637f81d2018-02-26 18:10:57 -06001254 rx_buff = &rx_pool->rx_buff[j];
1255 if (rx_buff && rx_buff->skb) {
1256 dev_kfree_skb_any(rx_buff->skb);
1257 rx_buff->skb = NULL;
Thomas Falcond0869c02018-02-13 18:23:43 -06001258 }
1259 }
1260 }
1261}
1262
Thomas Falcone9e1e972018-03-16 20:00:30 -05001263static void clean_one_tx_pool(struct ibmvnic_adapter *adapter,
1264 struct ibmvnic_tx_pool *tx_pool)
Nathan Fontenotb41b83e2017-05-03 14:04:56 -04001265{
Thomas Falcon637f81d2018-02-26 18:10:57 -06001266 struct ibmvnic_tx_buff *tx_buff;
Nathan Fontenotb41b83e2017-05-03 14:04:56 -04001267 u64 tx_entries;
Thomas Falcone9e1e972018-03-16 20:00:30 -05001268 int i;
Nathan Fontenotb41b83e2017-05-03 14:04:56 -04001269
Dan Carpenter050e85c2018-03-23 14:36:15 +03001270 if (!tx_pool || !tx_pool->tx_buff)
Thomas Falcone9e1e972018-03-16 20:00:30 -05001271 return;
1272
1273 tx_entries = tx_pool->num_buffers;
1274
1275 for (i = 0; i < tx_entries; i++) {
1276 tx_buff = &tx_pool->tx_buff[i];
1277 if (tx_buff && tx_buff->skb) {
1278 dev_kfree_skb_any(tx_buff->skb);
1279 tx_buff->skb = NULL;
1280 }
1281 }
1282}
1283
1284static void clean_tx_pools(struct ibmvnic_adapter *adapter)
1285{
1286 int tx_scrqs;
1287 int i;
1288
1289 if (!adapter->tx_pool || !adapter->tso_pool)
Nathan Fontenotb41b83e2017-05-03 14:04:56 -04001290 return;
1291
Thomas Falcon660e3092018-04-20 14:25:32 -05001292 tx_scrqs = adapter->num_active_tx_pools;
Nathan Fontenotb41b83e2017-05-03 14:04:56 -04001293
1294 /* Free any remaining skbs in the tx buffer pools */
1295 for (i = 0; i < tx_scrqs; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001296 netdev_dbg(adapter->netdev, "Cleaning tx_pool[%d]\n", i);
Thomas Falcone9e1e972018-03-16 20:00:30 -05001297 clean_one_tx_pool(adapter, &adapter->tx_pool[i]);
1298 clean_one_tx_pool(adapter, &adapter->tso_pool[i]);
Nathan Fontenotb41b83e2017-05-03 14:04:56 -04001299 }
1300}
1301
John Allen6095e592018-03-30 13:44:21 -05001302static void ibmvnic_disable_irqs(struct ibmvnic_adapter *adapter)
John Allenea5509f2017-03-17 17:13:43 -05001303{
John Allen6095e592018-03-30 13:44:21 -05001304 struct net_device *netdev = adapter->netdev;
John Allenea5509f2017-03-17 17:13:43 -05001305 int i;
1306
Nathan Fontenot46293b92017-05-03 14:05:02 -04001307 if (adapter->tx_scrq) {
1308 for (i = 0; i < adapter->req_tx_queues; i++)
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001309 if (adapter->tx_scrq[i]->irq) {
Thomas Falconf8738662018-03-07 17:51:45 -06001310 netdev_dbg(netdev,
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001311 "Disabling tx_scrq[%d] irq\n", i);
Thomas Falconf23e0642018-04-15 18:53:36 -05001312 disable_scrq_irq(adapter, adapter->tx_scrq[i]);
Nathan Fontenot46293b92017-05-03 14:05:02 -04001313 disable_irq(adapter->tx_scrq[i]->irq);
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001314 }
Nathan Fontenot46293b92017-05-03 14:05:02 -04001315 }
1316
Nathan Fontenot46293b92017-05-03 14:05:02 -04001317 if (adapter->rx_scrq) {
1318 for (i = 0; i < adapter->req_rx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001319 if (adapter->rx_scrq[i]->irq) {
Thomas Falconf8738662018-03-07 17:51:45 -06001320 netdev_dbg(netdev,
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001321 "Disabling rx_scrq[%d] irq\n", i);
Thomas Falconf23e0642018-04-15 18:53:36 -05001322 disable_scrq_irq(adapter, adapter->rx_scrq[i]);
Nathan Fontenot46293b92017-05-03 14:05:02 -04001323 disable_irq(adapter->rx_scrq[i]->irq);
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001324 }
Nathan Fontenot46293b92017-05-03 14:05:02 -04001325 }
1326 }
John Allen6095e592018-03-30 13:44:21 -05001327}
1328
1329static void ibmvnic_cleanup(struct net_device *netdev)
1330{
1331 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1332
1333 /* ensure that transmissions are stopped if called by do_reset */
Juliet Kim7ed5b312019-09-20 16:11:23 -04001334 if (test_bit(0, &adapter->resetting))
John Allen6095e592018-03-30 13:44:21 -05001335 netif_tx_disable(netdev);
1336 else
1337 netif_tx_stop_all_queues(netdev);
1338
1339 ibmvnic_napi_disable(adapter);
1340 ibmvnic_disable_irqs(adapter);
1341
Thomas Falcond0869c02018-02-13 18:23:43 -06001342 clean_rx_pools(adapter);
Thomas Falcon10f76212017-05-26 10:30:31 -04001343 clean_tx_pools(adapter);
Thomas Falcon01d9bd72018-03-07 17:51:46 -06001344}
1345
1346static int __ibmvnic_close(struct net_device *netdev)
1347{
1348 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1349 int rc = 0;
1350
1351 adapter->state = VNIC_CLOSING;
1352 rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_DN);
1353 if (rc)
1354 return rc;
Nathan Fontenot90c80142017-05-03 14:04:32 -04001355 adapter->state = VNIC_CLOSED;
Thomas Falcon01d9bd72018-03-07 17:51:46 -06001356 return 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001357}
1358
Nathan Fontenoted651a12017-05-03 14:04:38 -04001359static int ibmvnic_close(struct net_device *netdev)
1360{
1361 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1362 int rc;
1363
Sukadev Bhattiprolu38bd5ce2020-12-04 18:22:35 -08001364 netdev_dbg(netdev, "[S:%d FOP:%d FRR:%d] Closing\n",
1365 adapter->state, adapter->failover_pending,
1366 adapter->force_reset_recovery);
1367
Thomas Falcon5a18e1e2018-04-06 18:37:05 -05001368 /* If device failover is pending, just set device state and return.
1369 * Device operation will be handled by reset routine.
1370 */
1371 if (adapter->failover_pending) {
1372 adapter->state = VNIC_CLOSED;
1373 return 0;
1374 }
1375
Nathan Fontenoted651a12017-05-03 14:04:38 -04001376 rc = __ibmvnic_close(netdev);
Nathan Fontenot30f79622018-04-06 18:37:06 -05001377 ibmvnic_cleanup(netdev);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001378
1379 return rc;
1380}
1381
Thomas Falconad7775d2016-04-01 17:20:34 -05001382/**
1383 * build_hdr_data - creates L2/L3/L4 header data buffer
1384 * @hdr_field - bitfield determining needed headers
1385 * @skb - socket buffer
1386 * @hdr_len - array of header lengths
1387 * @tot_len - total length of data
1388 *
1389 * Reads hdr_field to determine which headers are needed by firmware.
1390 * Builds a buffer containing these headers. Saves individual header
1391 * lengths and total buffer length to be used to build descriptors.
1392 */
1393static int build_hdr_data(u8 hdr_field, struct sk_buff *skb,
1394 int *hdr_len, u8 *hdr_data)
1395{
1396 int len = 0;
1397 u8 *hdr;
1398
Thomas Falconda75e3b2018-03-12 11:51:02 -05001399 if (skb_vlan_tagged(skb) && !skb_vlan_tag_present(skb))
1400 hdr_len[0] = sizeof(struct vlan_ethhdr);
1401 else
1402 hdr_len[0] = sizeof(struct ethhdr);
Thomas Falconad7775d2016-04-01 17:20:34 -05001403
1404 if (skb->protocol == htons(ETH_P_IP)) {
1405 hdr_len[1] = ip_hdr(skb)->ihl * 4;
1406 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
1407 hdr_len[2] = tcp_hdrlen(skb);
1408 else if (ip_hdr(skb)->protocol == IPPROTO_UDP)
1409 hdr_len[2] = sizeof(struct udphdr);
1410 } else if (skb->protocol == htons(ETH_P_IPV6)) {
1411 hdr_len[1] = sizeof(struct ipv6hdr);
1412 if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
1413 hdr_len[2] = tcp_hdrlen(skb);
1414 else if (ipv6_hdr(skb)->nexthdr == IPPROTO_UDP)
1415 hdr_len[2] = sizeof(struct udphdr);
Thomas Falcon4eb50ce2017-12-18 12:52:40 -06001416 } else if (skb->protocol == htons(ETH_P_ARP)) {
1417 hdr_len[1] = arp_hdr_len(skb->dev);
1418 hdr_len[2] = 0;
Thomas Falconad7775d2016-04-01 17:20:34 -05001419 }
1420
1421 memset(hdr_data, 0, 120);
1422 if ((hdr_field >> 6) & 1) {
1423 hdr = skb_mac_header(skb);
1424 memcpy(hdr_data, hdr, hdr_len[0]);
1425 len += hdr_len[0];
1426 }
1427
1428 if ((hdr_field >> 5) & 1) {
1429 hdr = skb_network_header(skb);
1430 memcpy(hdr_data + len, hdr, hdr_len[1]);
1431 len += hdr_len[1];
1432 }
1433
1434 if ((hdr_field >> 4) & 1) {
1435 hdr = skb_transport_header(skb);
1436 memcpy(hdr_data + len, hdr, hdr_len[2]);
1437 len += hdr_len[2];
1438 }
1439 return len;
1440}
1441
1442/**
1443 * create_hdr_descs - create header and header extension descriptors
1444 * @hdr_field - bitfield determining needed headers
1445 * @data - buffer containing header data
1446 * @len - length of data buffer
1447 * @hdr_len - array of individual header lengths
1448 * @scrq_arr - descriptor array
1449 *
1450 * Creates header and, if needed, header extension descriptors and
1451 * places them in a descriptor array, scrq_arr
1452 */
1453
Thomas Falcon2de09682017-10-16 10:02:11 -05001454static int create_hdr_descs(u8 hdr_field, u8 *hdr_data, int len, int *hdr_len,
1455 union sub_crq *scrq_arr)
Thomas Falconad7775d2016-04-01 17:20:34 -05001456{
1457 union sub_crq hdr_desc;
1458 int tmp_len = len;
Thomas Falcon2de09682017-10-16 10:02:11 -05001459 int num_descs = 0;
Thomas Falconad7775d2016-04-01 17:20:34 -05001460 u8 *data, *cur;
1461 int tmp;
1462
1463 while (tmp_len > 0) {
1464 cur = hdr_data + len - tmp_len;
1465
1466 memset(&hdr_desc, 0, sizeof(hdr_desc));
1467 if (cur != hdr_data) {
1468 data = hdr_desc.hdr_ext.data;
1469 tmp = tmp_len > 29 ? 29 : tmp_len;
1470 hdr_desc.hdr_ext.first = IBMVNIC_CRQ_CMD;
1471 hdr_desc.hdr_ext.type = IBMVNIC_HDR_EXT_DESC;
1472 hdr_desc.hdr_ext.len = tmp;
1473 } else {
1474 data = hdr_desc.hdr.data;
1475 tmp = tmp_len > 24 ? 24 : tmp_len;
1476 hdr_desc.hdr.first = IBMVNIC_CRQ_CMD;
1477 hdr_desc.hdr.type = IBMVNIC_HDR_DESC;
1478 hdr_desc.hdr.len = tmp;
1479 hdr_desc.hdr.l2_len = (u8)hdr_len[0];
1480 hdr_desc.hdr.l3_len = cpu_to_be16((u16)hdr_len[1]);
1481 hdr_desc.hdr.l4_len = (u8)hdr_len[2];
1482 hdr_desc.hdr.flag = hdr_field << 1;
1483 }
1484 memcpy(data, cur, tmp);
1485 tmp_len -= tmp;
1486 *scrq_arr = hdr_desc;
1487 scrq_arr++;
Thomas Falcon2de09682017-10-16 10:02:11 -05001488 num_descs++;
Thomas Falconad7775d2016-04-01 17:20:34 -05001489 }
Thomas Falcon2de09682017-10-16 10:02:11 -05001490
1491 return num_descs;
Thomas Falconad7775d2016-04-01 17:20:34 -05001492}
1493
1494/**
1495 * build_hdr_descs_arr - build a header descriptor array
1496 * @skb - socket buffer
1497 * @num_entries - number of descriptors to be sent
1498 * @subcrq - first TX descriptor
1499 * @hdr_field - bit field determining which headers will be sent
1500 *
1501 * This function will build a TX descriptor array with applicable
1502 * L2/L3/L4 packet header descriptors to be sent by send_subcrq_indirect.
1503 */
1504
Thomas Falconc62aa372020-11-18 19:12:20 -06001505static void build_hdr_descs_arr(struct sk_buff *skb,
1506 union sub_crq *indir_arr,
Thomas Falconad7775d2016-04-01 17:20:34 -05001507 int *num_entries, u8 hdr_field)
1508{
1509 int hdr_len[3] = {0, 0, 0};
Thomas Falconc62aa372020-11-18 19:12:20 -06001510 u8 hdr_data[140] = {0};
Thomas Falcon2de09682017-10-16 10:02:11 -05001511 int tot_len;
Thomas Falconad7775d2016-04-01 17:20:34 -05001512
Thomas Falconc62aa372020-11-18 19:12:20 -06001513 tot_len = build_hdr_data(hdr_field, skb, hdr_len,
1514 hdr_data);
Thomas Falcon2de09682017-10-16 10:02:11 -05001515 *num_entries += create_hdr_descs(hdr_field, hdr_data, tot_len, hdr_len,
Thomas Falconc62aa372020-11-18 19:12:20 -06001516 indir_arr + 1);
Thomas Falconad7775d2016-04-01 17:20:34 -05001517}
1518
Thomas Falcon1f247a62018-03-12 11:51:04 -05001519static int ibmvnic_xmit_workarounds(struct sk_buff *skb,
1520 struct net_device *netdev)
1521{
1522 /* For some backing devices, mishandling of small packets
1523 * can result in a loss of connection or TX stall. Device
1524 * architects recommend that no packet should be smaller
1525 * than the minimum MTU value provided to the driver, so
1526 * pad any packets to that length
1527 */
1528 if (skb->len < netdev->min_mtu)
1529 return skb_put_padto(skb, netdev->min_mtu);
Thomas Falcon7083a452018-03-12 21:05:26 -05001530
1531 return 0;
Thomas Falcon1f247a62018-03-12 11:51:04 -05001532}
1533
Thomas Falcon0d973382020-11-18 19:12:19 -06001534static void ibmvnic_tx_scrq_clean_buffer(struct ibmvnic_adapter *adapter,
1535 struct ibmvnic_sub_crq_queue *tx_scrq)
1536{
1537 struct ibmvnic_ind_xmit_queue *ind_bufp;
1538 struct ibmvnic_tx_buff *tx_buff;
1539 struct ibmvnic_tx_pool *tx_pool;
1540 union sub_crq tx_scrq_entry;
1541 int queue_num;
1542 int entries;
1543 int index;
1544 int i;
1545
1546 ind_bufp = &tx_scrq->ind_buf;
1547 entries = (u64)ind_bufp->index;
1548 queue_num = tx_scrq->pool_index;
1549
1550 for (i = entries - 1; i >= 0; --i) {
1551 tx_scrq_entry = ind_bufp->indir_arr[i];
1552 if (tx_scrq_entry.v1.type != IBMVNIC_TX_DESC)
1553 continue;
1554 index = be32_to_cpu(tx_scrq_entry.v1.correlator);
1555 if (index & IBMVNIC_TSO_POOL_MASK) {
1556 tx_pool = &adapter->tso_pool[queue_num];
1557 index &= ~IBMVNIC_TSO_POOL_MASK;
1558 } else {
1559 tx_pool = &adapter->tx_pool[queue_num];
1560 }
1561 tx_pool->free_map[tx_pool->consumer_index] = index;
1562 tx_pool->consumer_index = tx_pool->consumer_index == 0 ?
1563 tx_pool->num_buffers - 1 :
1564 tx_pool->consumer_index - 1;
1565 tx_buff = &tx_pool->tx_buff[index];
1566 adapter->netdev->stats.tx_packets--;
1567 adapter->netdev->stats.tx_bytes -= tx_buff->skb->len;
1568 adapter->tx_stats_buffers[queue_num].packets--;
1569 adapter->tx_stats_buffers[queue_num].bytes -=
1570 tx_buff->skb->len;
1571 dev_kfree_skb_any(tx_buff->skb);
1572 tx_buff->skb = NULL;
1573 adapter->netdev->stats.tx_dropped++;
1574 }
1575 ind_bufp->index = 0;
1576 if (atomic_sub_return(entries, &tx_scrq->used) <=
1577 (adapter->req_tx_entries_per_subcrq / 2) &&
1578 __netif_subqueue_stopped(adapter->netdev, queue_num)) {
1579 netif_wake_subqueue(adapter->netdev, queue_num);
1580 netdev_dbg(adapter->netdev, "Started queue %d\n",
1581 queue_num);
1582 }
1583}
1584
1585static int ibmvnic_tx_scrq_flush(struct ibmvnic_adapter *adapter,
1586 struct ibmvnic_sub_crq_queue *tx_scrq)
1587{
1588 struct ibmvnic_ind_xmit_queue *ind_bufp;
1589 u64 dma_addr;
1590 u64 entries;
1591 u64 handle;
1592 int rc;
1593
1594 ind_bufp = &tx_scrq->ind_buf;
1595 dma_addr = (u64)ind_bufp->indir_dma;
1596 entries = (u64)ind_bufp->index;
1597 handle = tx_scrq->handle;
1598
1599 if (!entries)
1600 return 0;
1601 rc = send_subcrq_indirect(adapter, handle, dma_addr, entries);
1602 if (rc)
1603 ibmvnic_tx_scrq_clean_buffer(adapter, tx_scrq);
1604 else
1605 ind_bufp->index = 0;
1606 return 0;
1607}
1608
YueHaibing94b2bb22018-09-18 14:35:47 +08001609static netdev_tx_t ibmvnic_xmit(struct sk_buff *skb, struct net_device *netdev)
Thomas Falcon032c5e82015-12-21 11:26:06 -06001610{
1611 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1612 int queue_num = skb_get_queue_mapping(skb);
Thomas Falconad7775d2016-04-01 17:20:34 -05001613 u8 *hdrs = (u8 *)&adapter->tx_rx_desc_req;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001614 struct device *dev = &adapter->vdev->dev;
Thomas Falcon0d973382020-11-18 19:12:19 -06001615 struct ibmvnic_ind_xmit_queue *ind_bufp;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001616 struct ibmvnic_tx_buff *tx_buff = NULL;
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001617 struct ibmvnic_sub_crq_queue *tx_scrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001618 struct ibmvnic_tx_pool *tx_pool;
1619 unsigned int tx_send_failed = 0;
Thomas Falcon0d973382020-11-18 19:12:19 -06001620 netdev_tx_t ret = NETDEV_TX_OK;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001621 unsigned int tx_map_failed = 0;
Thomas Falconc62aa372020-11-18 19:12:20 -06001622 union sub_crq indir_arr[16];
Thomas Falcon032c5e82015-12-21 11:26:06 -06001623 unsigned int tx_dropped = 0;
1624 unsigned int tx_packets = 0;
1625 unsigned int tx_bytes = 0;
1626 dma_addr_t data_dma_addr;
1627 struct netdev_queue *txq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001628 unsigned long lpar_rc;
1629 union sub_crq tx_crq;
1630 unsigned int offset;
Thomas Falconad7775d2016-04-01 17:20:34 -05001631 int num_entries = 1;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001632 unsigned char *dst;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001633 int index = 0;
Thomas Falcona0dca102018-01-18 19:29:48 -06001634 u8 proto = 0;
Thomas Falcon0d973382020-11-18 19:12:19 -06001635
1636 tx_scrq = adapter->tx_scrq[queue_num];
1637 txq = netdev_get_tx_queue(netdev, queue_num);
1638 ind_bufp = &tx_scrq->ind_buf;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001639
Juliet Kim7ed5b312019-09-20 16:11:23 -04001640 if (test_bit(0, &adapter->resetting)) {
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001641 if (!netif_subqueue_stopped(netdev, skb))
1642 netif_stop_subqueue(netdev, queue_num);
1643 dev_kfree_skb_any(skb);
1644
Thomas Falcon032c5e82015-12-21 11:26:06 -06001645 tx_send_failed++;
1646 tx_dropped++;
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001647 ret = NETDEV_TX_OK;
Thomas Falcon0d973382020-11-18 19:12:19 -06001648 ibmvnic_tx_scrq_flush(adapter, tx_scrq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001649 goto out;
1650 }
1651
Thomas Falcon7083a452018-03-12 21:05:26 -05001652 if (ibmvnic_xmit_workarounds(skb, netdev)) {
Thomas Falcon1f247a62018-03-12 11:51:04 -05001653 tx_dropped++;
1654 tx_send_failed++;
1655 ret = NETDEV_TX_OK;
Thomas Falcon0d973382020-11-18 19:12:19 -06001656 ibmvnic_tx_scrq_flush(adapter, tx_scrq);
Thomas Falcon1f247a62018-03-12 11:51:04 -05001657 goto out;
1658 }
Thomas Falcon06b3e352018-03-16 20:00:28 -05001659 if (skb_is_gso(skb))
1660 tx_pool = &adapter->tso_pool[queue_num];
1661 else
1662 tx_pool = &adapter->tx_pool[queue_num];
Thomas Falcon1f247a62018-03-12 11:51:04 -05001663
Thomas Falcon032c5e82015-12-21 11:26:06 -06001664 index = tx_pool->free_map[tx_pool->consumer_index];
Thomas Falconfdb06102017-10-17 12:36:55 -05001665
Thomas Falcon86b61a52018-03-16 20:00:29 -05001666 if (index == IBMVNIC_INVALID_MAP) {
1667 dev_kfree_skb_any(skb);
1668 tx_send_failed++;
1669 tx_dropped++;
1670 ret = NETDEV_TX_OK;
Thomas Falcon0d973382020-11-18 19:12:19 -06001671 ibmvnic_tx_scrq_flush(adapter, tx_scrq);
Thomas Falcon86b61a52018-03-16 20:00:29 -05001672 goto out;
1673 }
1674
1675 tx_pool->free_map[tx_pool->consumer_index] = IBMVNIC_INVALID_MAP;
1676
Thomas Falcon06b3e352018-03-16 20:00:28 -05001677 offset = index * tx_pool->buf_size;
1678 dst = tx_pool->long_term_buff.buff + offset;
1679 memset(dst, 0, tx_pool->buf_size);
1680 data_dma_addr = tx_pool->long_term_buff.addr + offset;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001681
Thomas Falcon15482052017-10-17 12:36:54 -05001682 if (skb_shinfo(skb)->nr_frags) {
1683 int cur, i;
1684
1685 /* Copy the head */
1686 skb_copy_from_linear_data(skb, dst, skb_headlen(skb));
1687 cur = skb_headlen(skb);
1688
1689 /* Copy the frags */
1690 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1691 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1692
1693 memcpy(dst + cur,
1694 page_address(skb_frag_page(frag)) +
Jonathan Lemonb54c9d52019-07-30 07:40:33 -07001695 skb_frag_off(frag), skb_frag_size(frag));
Thomas Falcon15482052017-10-17 12:36:54 -05001696 cur += skb_frag_size(frag);
1697 }
1698 } else {
1699 skb_copy_from_linear_data(skb, dst, skb->len);
1700 }
1701
Thomas Falcon032c5e82015-12-21 11:26:06 -06001702 tx_pool->consumer_index =
Thomas Falcon06b3e352018-03-16 20:00:28 -05001703 (tx_pool->consumer_index + 1) % tx_pool->num_buffers;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001704
1705 tx_buff = &tx_pool->tx_buff[index];
1706 tx_buff->skb = skb;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001707 tx_buff->index = index;
1708 tx_buff->pool_index = queue_num;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001709
1710 memset(&tx_crq, 0, sizeof(tx_crq));
1711 tx_crq.v1.first = IBMVNIC_CRQ_CMD;
1712 tx_crq.v1.type = IBMVNIC_TX_DESC;
1713 tx_crq.v1.n_crq_elem = 1;
1714 tx_crq.v1.n_sge = 1;
1715 tx_crq.v1.flags1 = IBMVNIC_TX_COMP_NEEDED;
Thomas Falcon06b3e352018-03-16 20:00:28 -05001716
Thomas Falconfdb06102017-10-17 12:36:55 -05001717 if (skb_is_gso(skb))
Thomas Falcon06b3e352018-03-16 20:00:28 -05001718 tx_crq.v1.correlator =
1719 cpu_to_be32(index | IBMVNIC_TSO_POOL_MASK);
Thomas Falconfdb06102017-10-17 12:36:55 -05001720 else
Thomas Falcon06b3e352018-03-16 20:00:28 -05001721 tx_crq.v1.correlator = cpu_to_be32(index);
1722 tx_crq.v1.dma_reg = cpu_to_be16(tx_pool->long_term_buff.map_id);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001723 tx_crq.v1.sge_len = cpu_to_be32(skb->len);
1724 tx_crq.v1.ioba = cpu_to_be64(data_dma_addr);
1725
Michał Mirosławe84b4792018-11-07 17:50:52 +01001726 if (adapter->vlan_header_insertion && skb_vlan_tag_present(skb)) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06001727 tx_crq.v1.flags2 |= IBMVNIC_TX_VLAN_INSERT;
1728 tx_crq.v1.vlan_id = cpu_to_be16(skb->vlan_tci);
1729 }
1730
1731 if (skb->protocol == htons(ETH_P_IP)) {
Thomas Falcona0dca102018-01-18 19:29:48 -06001732 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV4;
1733 proto = ip_hdr(skb)->protocol;
1734 } else if (skb->protocol == htons(ETH_P_IPV6)) {
1735 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV6;
1736 proto = ipv6_hdr(skb)->nexthdr;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001737 }
1738
Thomas Falcona0dca102018-01-18 19:29:48 -06001739 if (proto == IPPROTO_TCP)
1740 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_TCP;
1741 else if (proto == IPPROTO_UDP)
1742 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_UDP;
1743
Thomas Falconad7775d2016-04-01 17:20:34 -05001744 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06001745 tx_crq.v1.flags1 |= IBMVNIC_TX_CHKSUM_OFFLOAD;
Thomas Falconad7775d2016-04-01 17:20:34 -05001746 hdrs += 2;
1747 }
Thomas Falconfdb06102017-10-17 12:36:55 -05001748 if (skb_is_gso(skb)) {
1749 tx_crq.v1.flags1 |= IBMVNIC_TX_LSO;
1750 tx_crq.v1.mss = cpu_to_be16(skb_shinfo(skb)->gso_size);
1751 hdrs += 2;
1752 }
Thomas Falcon0d973382020-11-18 19:12:19 -06001753
1754 if ((*hdrs >> 7) & 1)
Thomas Falconc62aa372020-11-18 19:12:20 -06001755 build_hdr_descs_arr(skb, indir_arr, &num_entries, *hdrs);
Thomas Falcon0d973382020-11-18 19:12:19 -06001756
1757 tx_crq.v1.n_crq_elem = num_entries;
1758 tx_buff->num_entries = num_entries;
1759 /* flush buffer if current entry can not fit */
1760 if (num_entries + ind_bufp->index > IBMVNIC_MAX_IND_DESCS) {
1761 lpar_rc = ibmvnic_tx_scrq_flush(adapter, tx_scrq);
1762 if (lpar_rc != H_SUCCESS)
1763 goto tx_flush_err;
Thomas Falconad7775d2016-04-01 17:20:34 -05001764 }
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001765
Thomas Falconc62aa372020-11-18 19:12:20 -06001766 indir_arr[0] = tx_crq;
1767 memcpy(&ind_bufp->indir_arr[ind_bufp->index], &indir_arr[0],
Thomas Falcon0d973382020-11-18 19:12:19 -06001768 num_entries * sizeof(struct ibmvnic_generic_scrq));
1769 ind_bufp->index += num_entries;
1770 if (__netdev_tx_sent_queue(txq, skb->len,
1771 netdev_xmit_more() &&
1772 ind_bufp->index < IBMVNIC_MAX_IND_DESCS)) {
1773 lpar_rc = ibmvnic_tx_scrq_flush(adapter, tx_scrq);
1774 if (lpar_rc != H_SUCCESS)
1775 goto tx_err;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001776 }
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001777
Thomas Falconffc385b2018-02-18 10:08:41 -06001778 if (atomic_add_return(num_entries, &tx_scrq->used)
Brian King58c8c0c2017-04-19 13:44:47 -04001779 >= adapter->req_tx_entries_per_subcrq) {
Thomas Falcon0aecb132018-02-26 18:10:58 -06001780 netdev_dbg(netdev, "Stopping queue %d\n", queue_num);
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001781 netif_stop_subqueue(netdev, queue_num);
1782 }
1783
Thomas Falcon032c5e82015-12-21 11:26:06 -06001784 tx_packets++;
1785 tx_bytes += skb->len;
1786 txq->trans_start = jiffies;
1787 ret = NETDEV_TX_OK;
Thomas Falcon86b61a52018-03-16 20:00:29 -05001788 goto out;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001789
Thomas Falcon0d973382020-11-18 19:12:19 -06001790tx_flush_err:
1791 dev_kfree_skb_any(skb);
1792 tx_buff->skb = NULL;
1793 tx_pool->consumer_index = tx_pool->consumer_index == 0 ?
1794 tx_pool->num_buffers - 1 :
1795 tx_pool->consumer_index - 1;
1796 tx_dropped++;
1797tx_err:
1798 if (lpar_rc != H_CLOSED && lpar_rc != H_PARAMETER)
1799 dev_err_ratelimited(dev, "tx: send failed\n");
1800
1801 if (lpar_rc == H_CLOSED || adapter->failover_pending) {
1802 /* Disable TX and report carrier off if queue is closed
1803 * or pending failover.
1804 * Firmware guarantees that a signal will be sent to the
1805 * driver, triggering a reset or some other action.
1806 */
1807 netif_tx_stop_all_queues(netdev);
1808 netif_carrier_off(netdev);
1809 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001810out:
1811 netdev->stats.tx_dropped += tx_dropped;
1812 netdev->stats.tx_bytes += tx_bytes;
1813 netdev->stats.tx_packets += tx_packets;
1814 adapter->tx_send_failed += tx_send_failed;
1815 adapter->tx_map_failed += tx_map_failed;
John Allen3d52b592017-08-02 16:44:14 -05001816 adapter->tx_stats_buffers[queue_num].packets += tx_packets;
1817 adapter->tx_stats_buffers[queue_num].bytes += tx_bytes;
1818 adapter->tx_stats_buffers[queue_num].dropped_packets += tx_dropped;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001819
1820 return ret;
1821}
1822
1823static void ibmvnic_set_multi(struct net_device *netdev)
1824{
1825 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1826 struct netdev_hw_addr *ha;
1827 union ibmvnic_crq crq;
1828
1829 memset(&crq, 0, sizeof(crq));
1830 crq.request_capability.first = IBMVNIC_CRQ_CMD;
1831 crq.request_capability.cmd = REQUEST_CAPABILITY;
1832
1833 if (netdev->flags & IFF_PROMISC) {
1834 if (!adapter->promisc_supported)
1835 return;
1836 } else {
1837 if (netdev->flags & IFF_ALLMULTI) {
1838 /* Accept all multicast */
1839 memset(&crq, 0, sizeof(crq));
1840 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1841 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1842 crq.multicast_ctrl.flags = IBMVNIC_ENABLE_ALL;
1843 ibmvnic_send_crq(adapter, &crq);
1844 } else if (netdev_mc_empty(netdev)) {
1845 /* Reject all multicast */
1846 memset(&crq, 0, sizeof(crq));
1847 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1848 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1849 crq.multicast_ctrl.flags = IBMVNIC_DISABLE_ALL;
1850 ibmvnic_send_crq(adapter, &crq);
1851 } else {
1852 /* Accept one or more multicast(s) */
1853 netdev_for_each_mc_addr(ha, netdev) {
1854 memset(&crq, 0, sizeof(crq));
1855 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1856 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1857 crq.multicast_ctrl.flags = IBMVNIC_ENABLE_MC;
1858 ether_addr_copy(&crq.multicast_ctrl.mac_addr[0],
1859 ha->addr);
1860 ibmvnic_send_crq(adapter, &crq);
1861 }
1862 }
1863 }
1864}
1865
Thomas Falcon62740e92019-05-09 23:13:43 -05001866static int __ibmvnic_set_mac(struct net_device *netdev, u8 *dev_addr)
Thomas Falcon032c5e82015-12-21 11:26:06 -06001867{
1868 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001869 union ibmvnic_crq crq;
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05001870 int rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001871
Thomas Falcon62740e92019-05-09 23:13:43 -05001872 if (!is_valid_ether_addr(dev_addr)) {
1873 rc = -EADDRNOTAVAIL;
1874 goto err;
1875 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001876
1877 memset(&crq, 0, sizeof(crq));
1878 crq.change_mac_addr.first = IBMVNIC_CRQ_CMD;
1879 crq.change_mac_addr.cmd = CHANGE_MAC_ADDR;
Thomas Falcon62740e92019-05-09 23:13:43 -05001880 ether_addr_copy(&crq.change_mac_addr.mac_addr[0], dev_addr);
Thomas Falconf8136142018-01-29 13:45:05 -06001881
Thomas Falconff25dcb2019-11-25 17:12:56 -06001882 mutex_lock(&adapter->fw_lock);
1883 adapter->fw_done_rc = 0;
Thomas Falcon070eca92019-11-25 17:12:53 -06001884 reinit_completion(&adapter->fw_done);
Thomas Falconff25dcb2019-11-25 17:12:56 -06001885
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05001886 rc = ibmvnic_send_crq(adapter, &crq);
Thomas Falcon62740e92019-05-09 23:13:43 -05001887 if (rc) {
1888 rc = -EIO;
Thomas Falconff25dcb2019-11-25 17:12:56 -06001889 mutex_unlock(&adapter->fw_lock);
Thomas Falcon62740e92019-05-09 23:13:43 -05001890 goto err;
1891 }
1892
Thomas Falcon476d96c2019-11-25 17:12:55 -06001893 rc = ibmvnic_wait_for_completion(adapter, &adapter->fw_done, 10000);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001894 /* netdev->dev_addr is changed in handle_change_mac_rsp function */
Thomas Falcon476d96c2019-11-25 17:12:55 -06001895 if (rc || adapter->fw_done_rc) {
Thomas Falcon62740e92019-05-09 23:13:43 -05001896 rc = -EIO;
Thomas Falconff25dcb2019-11-25 17:12:56 -06001897 mutex_unlock(&adapter->fw_lock);
Thomas Falcon62740e92019-05-09 23:13:43 -05001898 goto err;
1899 }
Thomas Falconff25dcb2019-11-25 17:12:56 -06001900 mutex_unlock(&adapter->fw_lock);
Thomas Falcon62740e92019-05-09 23:13:43 -05001901 return 0;
1902err:
1903 ether_addr_copy(adapter->mac_addr, netdev->dev_addr);
1904 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001905}
1906
John Allenc26eba02017-10-26 16:23:25 -05001907static int ibmvnic_set_mac(struct net_device *netdev, void *p)
1908{
1909 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1910 struct sockaddr *addr = p;
Thomas Falconf8136142018-01-29 13:45:05 -06001911 int rc;
John Allenc26eba02017-10-26 16:23:25 -05001912
Thomas Falcon62740e92019-05-09 23:13:43 -05001913 rc = 0;
Lijun Pan8fc36722020-10-27 17:04:56 -05001914 if (!is_valid_ether_addr(addr->sa_data))
1915 return -EADDRNOTAVAIL;
1916
1917 if (adapter->state != VNIC_PROBED) {
1918 ether_addr_copy(adapter->mac_addr, addr->sa_data);
Thomas Falcon62740e92019-05-09 23:13:43 -05001919 rc = __ibmvnic_set_mac(netdev, addr->sa_data);
Lijun Pan8fc36722020-10-27 17:04:56 -05001920 }
John Allenc26eba02017-10-26 16:23:25 -05001921
Thomas Falconf8136142018-01-29 13:45:05 -06001922 return rc;
John Allenc26eba02017-10-26 16:23:25 -05001923}
1924
Nathan Fontenoted651a12017-05-03 14:04:38 -04001925/**
Juliet Kimb27507b2019-09-20 16:11:22 -04001926 * do_change_param_reset returns zero if we are able to keep processing reset
1927 * events, or non-zero if we hit a fatal error and must halt.
1928 */
1929static int do_change_param_reset(struct ibmvnic_adapter *adapter,
1930 struct ibmvnic_rwi *rwi,
1931 u32 reset_state)
1932{
1933 struct net_device *netdev = adapter->netdev;
1934 int i, rc;
1935
1936 netdev_dbg(adapter->netdev, "Change param resetting driver (%d)\n",
1937 rwi->reset_reason);
1938
1939 netif_carrier_off(netdev);
1940 adapter->reset_reason = rwi->reset_reason;
1941
1942 ibmvnic_cleanup(netdev);
1943
1944 if (reset_state == VNIC_OPEN) {
1945 rc = __ibmvnic_close(netdev);
1946 if (rc)
Dany Madden0cb4bc62020-11-25 18:04:27 -06001947 goto out;
Juliet Kimb27507b2019-09-20 16:11:22 -04001948 }
1949
1950 release_resources(adapter);
1951 release_sub_crqs(adapter, 1);
1952 release_crq_queue(adapter);
1953
1954 adapter->state = VNIC_PROBED;
1955
1956 rc = init_crq_queue(adapter);
1957
1958 if (rc) {
1959 netdev_err(adapter->netdev,
1960 "Couldn't initialize crq. rc=%d\n", rc);
1961 return rc;
1962 }
1963
Lijun Pan635e4422020-08-19 17:52:26 -05001964 rc = ibmvnic_reset_init(adapter, true);
Dany Madden0cb4bc62020-11-25 18:04:27 -06001965 if (rc) {
1966 rc = IBMVNIC_INIT_FAILED;
1967 goto out;
1968 }
Juliet Kimb27507b2019-09-20 16:11:22 -04001969
1970 /* If the adapter was in PROBE state prior to the reset,
1971 * exit here.
1972 */
1973 if (reset_state == VNIC_PROBED)
Dany Madden0cb4bc62020-11-25 18:04:27 -06001974 goto out;
Juliet Kimb27507b2019-09-20 16:11:22 -04001975
1976 rc = ibmvnic_login(netdev);
1977 if (rc) {
Dany Madden0cb4bc62020-11-25 18:04:27 -06001978 goto out;
Juliet Kimb27507b2019-09-20 16:11:22 -04001979 }
1980
1981 rc = init_resources(adapter);
1982 if (rc)
Dany Madden0cb4bc62020-11-25 18:04:27 -06001983 goto out;
Juliet Kimb27507b2019-09-20 16:11:22 -04001984
1985 ibmvnic_disable_irqs(adapter);
1986
1987 adapter->state = VNIC_CLOSED;
1988
1989 if (reset_state == VNIC_CLOSED)
1990 return 0;
1991
1992 rc = __ibmvnic_open(netdev);
Dany Madden0cb4bc62020-11-25 18:04:27 -06001993 if (rc) {
1994 rc = IBMVNIC_OPEN_FAILED;
1995 goto out;
1996 }
Juliet Kimb27507b2019-09-20 16:11:22 -04001997
1998 /* refresh device's multicast list */
1999 ibmvnic_set_multi(netdev);
2000
2001 /* kick napi */
2002 for (i = 0; i < adapter->req_rx_queues; i++)
2003 napi_schedule(&adapter->napi[i]);
2004
Dany Madden0cb4bc62020-11-25 18:04:27 -06002005out:
2006 if (rc)
2007 adapter->state = reset_state;
2008 return rc;
Juliet Kimb27507b2019-09-20 16:11:22 -04002009}
2010
2011/**
Nathan Fontenoted651a12017-05-03 14:04:38 -04002012 * do_reset returns zero if we are able to keep processing reset events, or
2013 * non-zero if we hit a fatal error and must halt.
2014 */
2015static int do_reset(struct ibmvnic_adapter *adapter,
2016 struct ibmvnic_rwi *rwi, u32 reset_state)
2017{
John Allen896d8692018-01-18 16:26:31 -06002018 u64 old_num_rx_queues, old_num_tx_queues;
Thomas Falcon5bf032e2018-11-21 11:17:59 -06002019 u64 old_num_rx_slots, old_num_tx_slots;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002020 struct net_device *netdev = adapter->netdev;
2021 int i, rc;
2022
Sukadev Bhattiprolu38bd5ce2020-12-04 18:22:35 -08002023 netdev_dbg(adapter->netdev,
2024 "[S:%d FOP:%d] Reset reason %d, reset_state %d\n",
2025 adapter->state, adapter->failover_pending,
2026 rwi->reset_reason, reset_state);
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002027
Juliet Kimb27507b2019-09-20 16:11:22 -04002028 rtnl_lock();
Sukadev Bhattiprolu1d850492020-10-30 10:07:11 -07002029 /*
2030 * Now that we have the rtnl lock, clear any pending failover.
2031 * This will ensure ibmvnic_open() has either completed or will
2032 * block until failover is complete.
2033 */
2034 if (rwi->reset_reason == VNIC_RESET_FAILOVER)
2035 adapter->failover_pending = false;
Juliet Kimb27507b2019-09-20 16:11:22 -04002036
Nathan Fontenoted651a12017-05-03 14:04:38 -04002037 netif_carrier_off(netdev);
2038 adapter->reset_reason = rwi->reset_reason;
2039
John Allen896d8692018-01-18 16:26:31 -06002040 old_num_rx_queues = adapter->req_rx_queues;
2041 old_num_tx_queues = adapter->req_tx_queues;
Thomas Falcon5bf032e2018-11-21 11:17:59 -06002042 old_num_rx_slots = adapter->req_rx_add_entries_per_subcrq;
2043 old_num_tx_slots = adapter->req_tx_entries_per_subcrq;
John Allen896d8692018-01-18 16:26:31 -06002044
Nathan Fontenot30f79622018-04-06 18:37:06 -05002045 ibmvnic_cleanup(netdev);
2046
Thomas Falcon1f946082019-06-07 16:03:53 -05002047 if (reset_state == VNIC_OPEN &&
2048 adapter->reset_reason != VNIC_RESET_MOBILITY &&
Nathan Fontenot30f79622018-04-06 18:37:06 -05002049 adapter->reset_reason != VNIC_RESET_FAILOVER) {
Juliet Kimb27507b2019-09-20 16:11:22 -04002050 adapter->state = VNIC_CLOSING;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002051
Juliet Kimb27507b2019-09-20 16:11:22 -04002052 /* Release the RTNL lock before link state change and
2053 * re-acquire after the link state change to allow
2054 * linkwatch_event to grab the RTNL lock and run during
2055 * a reset.
2056 */
2057 rtnl_unlock();
2058 rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_DN);
2059 rtnl_lock();
2060 if (rc)
2061 goto out;
2062
2063 if (adapter->state != VNIC_CLOSING) {
2064 rc = -1;
2065 goto out;
2066 }
2067
2068 adapter->state = VNIC_CLOSED;
John Allenc26eba02017-10-26 16:23:25 -05002069 }
2070
John Allen8cb31cf2017-05-26 10:30:37 -04002071 if (adapter->reset_reason != VNIC_RESET_NON_FATAL) {
2072 /* remove the closed state so when we call open it appears
2073 * we are coming from the probed state.
2074 */
Nathan Fontenoted651a12017-05-03 14:04:38 -04002075 adapter->state = VNIC_PROBED;
John Allen8cb31cf2017-05-26 10:30:37 -04002076
Juliet Kimb27507b2019-09-20 16:11:22 -04002077 if (adapter->reset_reason == VNIC_RESET_MOBILITY) {
Nathan Fontenot30f79622018-04-06 18:37:06 -05002078 rc = ibmvnic_reenable_crq_queue(adapter);
2079 release_sub_crqs(adapter, 1);
2080 } else {
2081 rc = ibmvnic_reset_crq(adapter);
Dany Madden8b40eb732020-06-18 15:24:13 -04002082 if (rc == H_CLOSED || rc == H_SUCCESS) {
Nathan Fontenot30f79622018-04-06 18:37:06 -05002083 rc = vio_enable_interrupts(adapter->vdev);
Dany Madden8b40eb732020-06-18 15:24:13 -04002084 if (rc)
2085 netdev_err(adapter->netdev,
2086 "Reset failed to enable interrupts. rc=%d\n",
2087 rc);
2088 }
Nathan Fontenot30f79622018-04-06 18:37:06 -05002089 }
2090
2091 if (rc) {
2092 netdev_err(adapter->netdev,
Dany Madden8b40eb732020-06-18 15:24:13 -04002093 "Reset couldn't initialize crq. rc=%d\n", rc);
Juliet Kimb27507b2019-09-20 16:11:22 -04002094 goto out;
Nathan Fontenot30f79622018-04-06 18:37:06 -05002095 }
2096
Lijun Pan635e4422020-08-19 17:52:26 -05002097 rc = ibmvnic_reset_init(adapter, true);
Juliet Kimb27507b2019-09-20 16:11:22 -04002098 if (rc) {
2099 rc = IBMVNIC_INIT_FAILED;
2100 goto out;
2101 }
John Allen8cb31cf2017-05-26 10:30:37 -04002102
2103 /* If the adapter was in PROBE state prior to the reset,
2104 * exit here.
2105 */
Juliet Kimb27507b2019-09-20 16:11:22 -04002106 if (reset_state == VNIC_PROBED) {
2107 rc = 0;
2108 goto out;
2109 }
John Allen8cb31cf2017-05-26 10:30:37 -04002110
2111 rc = ibmvnic_login(netdev);
2112 if (rc) {
Juliet Kimb27507b2019-09-20 16:11:22 -04002113 goto out;
John Allen8cb31cf2017-05-26 10:30:37 -04002114 }
2115
Juliet Kimb27507b2019-09-20 16:11:22 -04002116 if (adapter->req_rx_queues != old_num_rx_queues ||
2117 adapter->req_tx_queues != old_num_tx_queues ||
2118 adapter->req_rx_add_entries_per_subcrq !=
2119 old_num_rx_slots ||
2120 adapter->req_tx_entries_per_subcrq !=
Mingming Cao9f134572020-08-25 13:26:41 -04002121 old_num_tx_slots ||
2122 !adapter->rx_pool ||
2123 !adapter->tso_pool ||
2124 !adapter->tx_pool) {
John Allen896d8692018-01-18 16:26:31 -06002125 release_rx_pools(adapter);
2126 release_tx_pools(adapter);
Juliet Kima5681e22018-11-19 15:59:22 -06002127 release_napi(adapter);
2128 release_vpd_data(adapter);
2129
2130 rc = init_resources(adapter);
Thomas Falconf611a5b2018-08-30 13:19:53 -05002131 if (rc)
Juliet Kimb27507b2019-09-20 16:11:22 -04002132 goto out;
Nathan Fontenotd9043c12018-02-19 13:30:14 -06002133
John Allenc26eba02017-10-26 16:23:25 -05002134 } else {
2135 rc = reset_tx_pools(adapter);
Jakub Kicinski8ae4dff2020-09-04 21:07:49 -07002136 if (rc) {
Mingming Cao9f134572020-08-25 13:26:41 -04002137 netdev_dbg(adapter->netdev, "reset tx pools failed (%d)\n",
2138 rc);
Juliet Kimb27507b2019-09-20 16:11:22 -04002139 goto out;
Jakub Kicinski8ae4dff2020-09-04 21:07:49 -07002140 }
Nathan Fontenot8c0543a2017-05-26 10:31:06 -04002141
John Allenc26eba02017-10-26 16:23:25 -05002142 rc = reset_rx_pools(adapter);
Jakub Kicinski8ae4dff2020-09-04 21:07:49 -07002143 if (rc) {
Mingming Cao9f134572020-08-25 13:26:41 -04002144 netdev_dbg(adapter->netdev, "reset rx pools failed (%d)\n",
2145 rc);
Juliet Kimb27507b2019-09-20 16:11:22 -04002146 goto out;
Jakub Kicinski8ae4dff2020-09-04 21:07:49 -07002147 }
John Allenc26eba02017-10-26 16:23:25 -05002148 }
Thomas Falcon134bbe72018-05-16 15:49:04 -05002149 ibmvnic_disable_irqs(adapter);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002150 }
John Allene676d812018-03-14 10:41:29 -05002151 adapter->state = VNIC_CLOSED;
2152
Juliet Kimb27507b2019-09-20 16:11:22 -04002153 if (reset_state == VNIC_CLOSED) {
2154 rc = 0;
2155 goto out;
2156 }
John Allene676d812018-03-14 10:41:29 -05002157
Nathan Fontenoted651a12017-05-03 14:04:38 -04002158 rc = __ibmvnic_open(netdev);
2159 if (rc) {
Juliet Kimb27507b2019-09-20 16:11:22 -04002160 rc = IBMVNIC_OPEN_FAILED;
2161 goto out;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002162 }
2163
Thomas Falconbe32a242019-06-07 16:03:54 -05002164 /* refresh device's multicast list */
2165 ibmvnic_set_multi(netdev);
2166
Nathan Fontenoted651a12017-05-03 14:04:38 -04002167 /* kick napi */
2168 for (i = 0; i < adapter->req_rx_queues; i++)
2169 napi_schedule(&adapter->napi[i]);
2170
Lijun Pan98025bc2020-11-20 16:40:12 -06002171 if (adapter->reset_reason == VNIC_RESET_FAILOVER ||
2172 adapter->reset_reason == VNIC_RESET_MOBILITY) {
Thomas Falcon986103e2018-11-30 10:59:08 -06002173 call_netdevice_notifiers(NETDEV_NOTIFY_PEERS, netdev);
Lijun Pan83935972020-11-20 16:40:11 -06002174 call_netdevice_notifiers(NETDEV_RESEND_IGMP, netdev);
2175 }
Nathan Fontenot61d3e1d2017-06-12 20:47:45 -04002176
Juliet Kimb27507b2019-09-20 16:11:22 -04002177 rc = 0;
2178
2179out:
Dany Madden0cb4bc62020-11-25 18:04:27 -06002180 /* restore the adapter state if reset failed */
2181 if (rc)
2182 adapter->state = reset_state;
Juliet Kimb27507b2019-09-20 16:11:22 -04002183 rtnl_unlock();
2184
Sukadev Bhattiprolu38bd5ce2020-12-04 18:22:35 -08002185 netdev_dbg(adapter->netdev, "[S:%d FOP:%d] Reset done, rc %d\n",
2186 adapter->state, adapter->failover_pending, rc);
Juliet Kimb27507b2019-09-20 16:11:22 -04002187 return rc;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002188}
2189
Thomas Falcon2770a792018-05-23 13:38:02 -05002190static int do_hard_reset(struct ibmvnic_adapter *adapter,
2191 struct ibmvnic_rwi *rwi, u32 reset_state)
2192{
2193 struct net_device *netdev = adapter->netdev;
2194 int rc;
2195
2196 netdev_dbg(adapter->netdev, "Hard resetting driver (%d)\n",
2197 rwi->reset_reason);
2198
2199 netif_carrier_off(netdev);
2200 adapter->reset_reason = rwi->reset_reason;
2201
2202 ibmvnic_cleanup(netdev);
2203 release_resources(adapter);
2204 release_sub_crqs(adapter, 0);
2205 release_crq_queue(adapter);
2206
2207 /* remove the closed state so when we call open it appears
2208 * we are coming from the probed state.
2209 */
2210 adapter->state = VNIC_PROBED;
2211
Thomas Falconbbd669a2019-04-04 18:58:26 -05002212 reinit_completion(&adapter->init_done);
Thomas Falcon2770a792018-05-23 13:38:02 -05002213 rc = init_crq_queue(adapter);
2214 if (rc) {
2215 netdev_err(adapter->netdev,
2216 "Couldn't initialize crq. rc=%d\n", rc);
Dany Madden0cb4bc62020-11-25 18:04:27 -06002217 goto out;
Thomas Falcon2770a792018-05-23 13:38:02 -05002218 }
2219
Lijun Pan635e4422020-08-19 17:52:26 -05002220 rc = ibmvnic_reset_init(adapter, false);
Thomas Falcon2770a792018-05-23 13:38:02 -05002221 if (rc)
Dany Madden0cb4bc62020-11-25 18:04:27 -06002222 goto out;
Thomas Falcon2770a792018-05-23 13:38:02 -05002223
2224 /* If the adapter was in PROBE state prior to the reset,
2225 * exit here.
2226 */
2227 if (reset_state == VNIC_PROBED)
Dany Madden0cb4bc62020-11-25 18:04:27 -06002228 goto out;
Thomas Falcon2770a792018-05-23 13:38:02 -05002229
2230 rc = ibmvnic_login(netdev);
Dany Madden0cb4bc62020-11-25 18:04:27 -06002231 if (rc)
2232 goto out;
Juliet Kima5681e22018-11-19 15:59:22 -06002233
2234 rc = init_resources(adapter);
Thomas Falcon2770a792018-05-23 13:38:02 -05002235 if (rc)
Dany Madden0cb4bc62020-11-25 18:04:27 -06002236 goto out;
Thomas Falcon2770a792018-05-23 13:38:02 -05002237
2238 ibmvnic_disable_irqs(adapter);
2239 adapter->state = VNIC_CLOSED;
2240
2241 if (reset_state == VNIC_CLOSED)
Dany Madden0cb4bc62020-11-25 18:04:27 -06002242 goto out;
Thomas Falcon2770a792018-05-23 13:38:02 -05002243
2244 rc = __ibmvnic_open(netdev);
Dany Madden0cb4bc62020-11-25 18:04:27 -06002245 if (rc) {
2246 rc = IBMVNIC_OPEN_FAILED;
2247 goto out;
2248 }
Thomas Falcon2770a792018-05-23 13:38:02 -05002249
Lijun Pan98025bc2020-11-20 16:40:12 -06002250 call_netdevice_notifiers(NETDEV_NOTIFY_PEERS, netdev);
2251 call_netdevice_notifiers(NETDEV_RESEND_IGMP, netdev);
Dany Madden0cb4bc62020-11-25 18:04:27 -06002252out:
2253 /* restore adapter state if reset failed */
2254 if (rc)
2255 adapter->state = reset_state;
Sukadev Bhattiprolu38bd5ce2020-12-04 18:22:35 -08002256 netdev_dbg(adapter->netdev, "[S:%d FOP:%d] Hard reset done, rc %d\n",
2257 adapter->state, adapter->failover_pending, rc);
Dany Madden0cb4bc62020-11-25 18:04:27 -06002258 return rc;
Thomas Falcon2770a792018-05-23 13:38:02 -05002259}
2260
Nathan Fontenoted651a12017-05-03 14:04:38 -04002261static struct ibmvnic_rwi *get_next_rwi(struct ibmvnic_adapter *adapter)
2262{
2263 struct ibmvnic_rwi *rwi;
Thomas Falcon6c5c7482018-12-10 15:22:22 -06002264 unsigned long flags;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002265
Thomas Falcon6c5c7482018-12-10 15:22:22 -06002266 spin_lock_irqsave(&adapter->rwi_lock, flags);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002267
2268 if (!list_empty(&adapter->rwi_list)) {
2269 rwi = list_first_entry(&adapter->rwi_list, struct ibmvnic_rwi,
2270 list);
2271 list_del(&rwi->list);
2272 } else {
2273 rwi = NULL;
2274 }
2275
Thomas Falcon6c5c7482018-12-10 15:22:22 -06002276 spin_unlock_irqrestore(&adapter->rwi_lock, flags);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002277 return rwi;
2278}
2279
Nathan Fontenoted651a12017-05-03 14:04:38 -04002280static void __ibmvnic_reset(struct work_struct *work)
2281{
2282 struct ibmvnic_rwi *rwi;
2283 struct ibmvnic_adapter *adapter;
Juliet Kim7d7195a2020-03-10 09:23:58 -05002284 bool saved_state = false;
2285 unsigned long flags;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002286 u32 reset_state;
John Allenc26eba02017-10-26 16:23:25 -05002287 int rc = 0;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002288
2289 adapter = container_of(work, struct ibmvnic_adapter, ibmvnic_reset);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002290
Juliet Kim7ed5b312019-09-20 16:11:23 -04002291 if (test_and_set_bit_lock(0, &adapter->resetting)) {
2292 schedule_delayed_work(&adapter->ibmvnic_delayed_reset,
2293 IBMVNIC_RESET_DELAY);
2294 return;
2295 }
2296
Nathan Fontenoted651a12017-05-03 14:04:38 -04002297 rwi = get_next_rwi(adapter);
2298 while (rwi) {
Juliet Kim7d7195a2020-03-10 09:23:58 -05002299 spin_lock_irqsave(&adapter->state_lock, flags);
2300
Thomas Falcon36f10312019-08-27 11:10:04 -05002301 if (adapter->state == VNIC_REMOVING ||
Michal Suchanekc8dc5592019-09-09 22:44:51 +02002302 adapter->state == VNIC_REMOVED) {
Juliet Kim7d7195a2020-03-10 09:23:58 -05002303 spin_unlock_irqrestore(&adapter->state_lock, flags);
Juliet Kim1c2977c2019-09-05 17:30:01 -04002304 kfree(rwi);
2305 rc = EBUSY;
2306 break;
2307 }
Thomas Falcon36f10312019-08-27 11:10:04 -05002308
Juliet Kim7d7195a2020-03-10 09:23:58 -05002309 if (!saved_state) {
2310 reset_state = adapter->state;
Juliet Kim7d7195a2020-03-10 09:23:58 -05002311 saved_state = true;
2312 }
2313 spin_unlock_irqrestore(&adapter->state_lock, flags);
2314
Juliet Kimb27507b2019-09-20 16:11:22 -04002315 if (rwi->reset_reason == VNIC_RESET_CHANGE_PARAM) {
2316 /* CHANGE_PARAM requestor holds rtnl_lock */
2317 rc = do_change_param_reset(adapter, rwi, reset_state);
2318 } else if (adapter->force_reset_recovery) {
Sukadev Bhattiprolu1d850492020-10-30 10:07:11 -07002319 /*
2320 * Since we are doing a hard reset now, clear the
2321 * failover_pending flag so we don't ignore any
2322 * future MOBILITY or other resets.
2323 */
2324 adapter->failover_pending = false;
2325
Juliet Kimb27507b2019-09-20 16:11:22 -04002326 /* Transport event occurred during previous reset */
2327 if (adapter->wait_for_reset) {
2328 /* Previous was CHANGE_PARAM; caller locked */
2329 adapter->force_reset_recovery = false;
2330 rc = do_hard_reset(adapter, rwi, reset_state);
2331 } else {
2332 rtnl_lock();
2333 adapter->force_reset_recovery = false;
2334 rc = do_hard_reset(adapter, rwi, reset_state);
2335 rtnl_unlock();
2336 }
Sukadev Bhattiproluf15fde92020-11-25 18:04:28 -06002337 if (rc) {
2338 /* give backing device time to settle down */
2339 netdev_dbg(adapter->netdev,
2340 "[S:%d] Hard reset failed, waiting 60 secs\n",
2341 adapter->state);
2342 set_current_state(TASK_UNINTERRUPTIBLE);
2343 schedule_timeout(60 * HZ);
2344 }
Juliet Kimf9c6cea2020-04-30 13:22:11 -05002345 } else if (!(rwi->reset_reason == VNIC_RESET_FATAL &&
2346 adapter->from_passive_init)) {
Thomas Falcon2770a792018-05-23 13:38:02 -05002347 rc = do_reset(adapter, rwi, reset_state);
2348 }
Nathan Fontenoted651a12017-05-03 14:04:38 -04002349 kfree(rwi);
Dany Maddena86d5c62020-11-25 18:04:31 -06002350 adapter->last_reset_time = jiffies;
Dany Madden0cb4bc62020-11-25 18:04:27 -06002351
Dany Madden18f141b2020-11-25 18:04:25 -06002352 if (rc)
2353 netdev_dbg(adapter->netdev, "Reset failed, rc=%d\n", rc);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002354
2355 rwi = get_next_rwi(adapter);
Juliet Kim7ed5b312019-09-20 16:11:23 -04002356
2357 if (rwi && (rwi->reset_reason == VNIC_RESET_FAILOVER ||
2358 rwi->reset_reason == VNIC_RESET_MOBILITY))
2359 adapter->force_reset_recovery = true;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002360 }
2361
John Allenc26eba02017-10-26 16:23:25 -05002362 if (adapter->wait_for_reset) {
John Allenc26eba02017-10-26 16:23:25 -05002363 adapter->reset_done_rc = rc;
2364 complete(&adapter->reset_done);
2365 }
2366
Juliet Kim7ed5b312019-09-20 16:11:23 -04002367 clear_bit_unlock(0, &adapter->resetting);
Sukadev Bhattiprolu38bd5ce2020-12-04 18:22:35 -08002368
2369 netdev_dbg(adapter->netdev,
2370 "[S:%d FRR:%d WFR:%d] Done processing resets\n",
2371 adapter->state, adapter->force_reset_recovery,
2372 adapter->wait_for_reset);
Juliet Kim7ed5b312019-09-20 16:11:23 -04002373}
2374
2375static void __ibmvnic_delayed_reset(struct work_struct *work)
2376{
2377 struct ibmvnic_adapter *adapter;
2378
2379 adapter = container_of(work, struct ibmvnic_adapter,
2380 ibmvnic_delayed_reset.work);
2381 __ibmvnic_reset(&adapter->ibmvnic_reset);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002382}
2383
Thomas Falconaf894d22018-04-06 18:37:04 -05002384static int ibmvnic_reset(struct ibmvnic_adapter *adapter,
2385 enum ibmvnic_reset_reason reason)
Nathan Fontenoted651a12017-05-03 14:04:38 -04002386{
Thomas Falcon2770a792018-05-23 13:38:02 -05002387 struct list_head *entry, *tmp_entry;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002388 struct ibmvnic_rwi *rwi, *tmp;
2389 struct net_device *netdev = adapter->netdev;
Thomas Falcon6c5c7482018-12-10 15:22:22 -06002390 unsigned long flags;
Thomas Falconaf894d22018-04-06 18:37:04 -05002391 int ret;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002392
Sukadev Bhattiprolu1d850492020-10-30 10:07:11 -07002393 /*
2394 * If failover is pending don't schedule any other reset.
2395 * Instead let the failover complete. If there is already a
2396 * a failover reset scheduled, we will detect and drop the
2397 * duplicate reset when walking the ->rwi_list below.
2398 */
Nathan Fontenoted651a12017-05-03 14:04:38 -04002399 if (adapter->state == VNIC_REMOVING ||
Thomas Falcon5a18e1e2018-04-06 18:37:05 -05002400 adapter->state == VNIC_REMOVED ||
Sukadev Bhattiprolu1d850492020-10-30 10:07:11 -07002401 (adapter->failover_pending && reason != VNIC_RESET_FAILOVER)) {
Thomas Falconaf894d22018-04-06 18:37:04 -05002402 ret = EBUSY;
Thomas Falcon5a18e1e2018-04-06 18:37:05 -05002403 netdev_dbg(netdev, "Adapter removing or pending failover, skipping reset\n");
Thomas Falconaf894d22018-04-06 18:37:04 -05002404 goto err;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002405 }
2406
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04002407 if (adapter->state == VNIC_PROBING) {
2408 netdev_warn(netdev, "Adapter reset during probe\n");
Thomas Falconaf894d22018-04-06 18:37:04 -05002409 ret = adapter->init_done_rc = EAGAIN;
2410 goto err;
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04002411 }
2412
Thomas Falcon6c5c7482018-12-10 15:22:22 -06002413 spin_lock_irqsave(&adapter->rwi_lock, flags);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002414
2415 list_for_each(entry, &adapter->rwi_list) {
2416 tmp = list_entry(entry, struct ibmvnic_rwi, list);
2417 if (tmp->reset_reason == reason) {
Sukadev Bhattiprolu38bd5ce2020-12-04 18:22:35 -08002418 netdev_dbg(netdev, "Skipping matching reset, reason=%d\n",
2419 reason);
Thomas Falcon6c5c7482018-12-10 15:22:22 -06002420 spin_unlock_irqrestore(&adapter->rwi_lock, flags);
Thomas Falconaf894d22018-04-06 18:37:04 -05002421 ret = EBUSY;
2422 goto err;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002423 }
2424 }
2425
Thomas Falcon1d1bbc32018-12-10 15:22:23 -06002426 rwi = kzalloc(sizeof(*rwi), GFP_ATOMIC);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002427 if (!rwi) {
Thomas Falcon6c5c7482018-12-10 15:22:22 -06002428 spin_unlock_irqrestore(&adapter->rwi_lock, flags);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002429 ibmvnic_close(netdev);
Thomas Falconaf894d22018-04-06 18:37:04 -05002430 ret = ENOMEM;
2431 goto err;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002432 }
Thomas Falcon2770a792018-05-23 13:38:02 -05002433 /* if we just received a transport event,
2434 * flush reset queue and process this reset
2435 */
2436 if (adapter->force_reset_recovery && !list_empty(&adapter->rwi_list)) {
2437 list_for_each_safe(entry, tmp_entry, &adapter->rwi_list)
2438 list_del(entry);
2439 }
Nathan Fontenoted651a12017-05-03 14:04:38 -04002440 rwi->reset_reason = reason;
2441 list_add_tail(&rwi->list, &adapter->rwi_list);
Thomas Falcon6c5c7482018-12-10 15:22:22 -06002442 spin_unlock_irqrestore(&adapter->rwi_lock, flags);
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002443 netdev_dbg(adapter->netdev, "Scheduling reset (reason %d)\n", reason);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002444 schedule_work(&adapter->ibmvnic_reset);
Thomas Falconaf894d22018-04-06 18:37:04 -05002445
2446 return 0;
2447err:
Thomas Falconaf894d22018-04-06 18:37:04 -05002448 return -ret;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002449}
2450
Michael S. Tsirkin0290bd22019-12-10 09:23:51 -05002451static void ibmvnic_tx_timeout(struct net_device *dev, unsigned int txqueue)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002452{
2453 struct ibmvnic_adapter *adapter = netdev_priv(dev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002454
Lijun Pan855a6312020-11-20 16:40:13 -06002455 if (test_bit(0, &adapter->resetting)) {
2456 netdev_err(adapter->netdev,
2457 "Adapter is resetting, skip timeout reset\n");
2458 return;
2459 }
Dany Maddena86d5c62020-11-25 18:04:31 -06002460 /* No queuing up reset until at least 5 seconds (default watchdog val)
2461 * after last reset
2462 */
2463 if (time_before(jiffies, (adapter->last_reset_time + dev->watchdog_timeo))) {
2464 netdev_dbg(dev, "Not yet time to tx timeout.\n");
2465 return;
2466 }
Nathan Fontenoted651a12017-05-03 14:04:38 -04002467 ibmvnic_reset(adapter, VNIC_RESET_TIMEOUT);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002468}
2469
2470static void remove_buff_from_pool(struct ibmvnic_adapter *adapter,
2471 struct ibmvnic_rx_buff *rx_buff)
2472{
2473 struct ibmvnic_rx_pool *pool = &adapter->rx_pool[rx_buff->pool_index];
2474
2475 rx_buff->skb = NULL;
2476
2477 pool->free_map[pool->next_alloc] = (int)(rx_buff - pool->rx_buff);
2478 pool->next_alloc = (pool->next_alloc + 1) % pool->size;
2479
2480 atomic_dec(&pool->available);
2481}
2482
2483static int ibmvnic_poll(struct napi_struct *napi, int budget)
2484{
Dwip N. Banerjeeec20f362020-11-18 19:12:23 -06002485 struct ibmvnic_sub_crq_queue *rx_scrq;
2486 struct ibmvnic_adapter *adapter;
2487 struct net_device *netdev;
2488 int frames_processed;
2489 int scrq_num;
2490
2491 netdev = napi->dev;
2492 adapter = netdev_priv(netdev);
2493 scrq_num = (int)(napi - adapter->napi);
2494 frames_processed = 0;
2495 rx_scrq = adapter->rx_scrq[scrq_num];
Nathan Fontenot152ce472017-05-26 10:30:54 -04002496
Thomas Falcon032c5e82015-12-21 11:26:06 -06002497restart_poll:
2498 while (frames_processed < budget) {
2499 struct sk_buff *skb;
2500 struct ibmvnic_rx_buff *rx_buff;
2501 union sub_crq *next;
2502 u32 length;
2503 u16 offset;
2504 u8 flags = 0;
2505
Juliet Kim7ed5b312019-09-20 16:11:23 -04002506 if (unlikely(test_bit(0, &adapter->resetting) &&
John Allen34686562018-02-06 16:21:49 -06002507 adapter->reset_reason != VNIC_RESET_NON_FATAL)) {
Dwip N. Banerjeeec20f362020-11-18 19:12:23 -06002508 enable_scrq_irq(adapter, rx_scrq);
Thomas Falcon21ecba62017-06-14 23:50:09 -05002509 napi_complete_done(napi, frames_processed);
2510 return frames_processed;
2511 }
2512
Dwip N. Banerjeeec20f362020-11-18 19:12:23 -06002513 if (!pending_scrq(adapter, rx_scrq))
Thomas Falcon032c5e82015-12-21 11:26:06 -06002514 break;
Thomas Falconb71ec952020-12-01 09:52:10 -06002515 /* The queue entry at the current index is peeked at above
2516 * to determine that there is a valid descriptor awaiting
2517 * processing. We want to be sure that the current slot
2518 * holds a valid descriptor before reading its contents.
2519 */
2520 dma_rmb();
Dwip N. Banerjeeec20f362020-11-18 19:12:23 -06002521 next = ibmvnic_next_scrq(adapter, rx_scrq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002522 rx_buff =
2523 (struct ibmvnic_rx_buff *)be64_to_cpu(next->
2524 rx_comp.correlator);
2525 /* do error checking */
2526 if (next->rx_comp.rc) {
John Allene1cea2e2017-08-07 15:42:30 -05002527 netdev_dbg(netdev, "rx buffer returned with rc %x\n",
2528 be16_to_cpu(next->rx_comp.rc));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002529 /* free the entry */
2530 next->rx_comp.first = 0;
Thomas Falcon4b9b0f02018-02-13 18:23:42 -06002531 dev_kfree_skb_any(rx_buff->skb);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002532 remove_buff_from_pool(adapter, rx_buff);
Nathan Fontenotca05e312017-05-03 14:05:14 -04002533 continue;
Thomas Falconabe27a82018-02-19 20:12:57 -06002534 } else if (!rx_buff->skb) {
2535 /* free the entry */
2536 next->rx_comp.first = 0;
2537 remove_buff_from_pool(adapter, rx_buff);
2538 continue;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002539 }
2540
2541 length = be32_to_cpu(next->rx_comp.len);
2542 offset = be16_to_cpu(next->rx_comp.off_frame_data);
2543 flags = next->rx_comp.flags;
2544 skb = rx_buff->skb;
2545 skb_copy_to_linear_data(skb, rx_buff->data + offset,
2546 length);
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04002547
2548 /* VLAN Header has been stripped by the system firmware and
2549 * needs to be inserted by the driver
2550 */
2551 if (adapter->rx_vlan_header_insertion &&
2552 (flags & IBMVNIC_VLAN_STRIPPED))
2553 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
2554 ntohs(next->rx_comp.vlan_tci));
2555
Thomas Falcon032c5e82015-12-21 11:26:06 -06002556 /* free the entry */
2557 next->rx_comp.first = 0;
2558 remove_buff_from_pool(adapter, rx_buff);
2559
2560 skb_put(skb, length);
2561 skb->protocol = eth_type_trans(skb, netdev);
Thomas Falcon94ca3052017-05-03 14:05:20 -04002562 skb_record_rx_queue(skb, scrq_num);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002563
2564 if (flags & IBMVNIC_IP_CHKSUM_GOOD &&
2565 flags & IBMVNIC_TCP_UDP_CHKSUM_GOOD) {
2566 skb->ip_summed = CHECKSUM_UNNECESSARY;
2567 }
2568
2569 length = skb->len;
2570 napi_gro_receive(napi, skb); /* send it up */
2571 netdev->stats.rx_packets++;
2572 netdev->stats.rx_bytes += length;
John Allen3d52b592017-08-02 16:44:14 -05002573 adapter->rx_stats_buffers[scrq_num].packets++;
2574 adapter->rx_stats_buffers[scrq_num].bytes += length;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002575 frames_processed++;
2576 }
Nathan Fontenot152ce472017-05-26 10:30:54 -04002577
Dwip N. Banerjee41ed0a02020-11-18 19:12:25 -06002578 if (adapter->state != VNIC_CLOSING &&
2579 ((atomic_read(&adapter->rx_pool[scrq_num].available) <
2580 adapter->req_rx_add_entries_per_subcrq / 2) ||
2581 frames_processed < budget))
Nathan Fontenot152ce472017-05-26 10:30:54 -04002582 replenish_rx_pool(adapter, &adapter->rx_pool[scrq_num]);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002583 if (frames_processed < budget) {
Dwip N. Banerjeeec20f362020-11-18 19:12:23 -06002584 if (napi_complete_done(napi, frames_processed)) {
2585 enable_scrq_irq(adapter, rx_scrq);
2586 if (pending_scrq(adapter, rx_scrq)) {
2587 rmb();
2588 if (napi_reschedule(napi)) {
2589 disable_scrq_irq(adapter, rx_scrq);
2590 goto restart_poll;
2591 }
2592 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002593 }
2594 }
2595 return frames_processed;
2596}
2597
John Allenc26eba02017-10-26 16:23:25 -05002598static int wait_for_reset(struct ibmvnic_adapter *adapter)
2599{
Thomas Falconaf894d22018-04-06 18:37:04 -05002600 int rc, ret;
2601
John Allenc26eba02017-10-26 16:23:25 -05002602 adapter->fallback.mtu = adapter->req_mtu;
2603 adapter->fallback.rx_queues = adapter->req_rx_queues;
2604 adapter->fallback.tx_queues = adapter->req_tx_queues;
2605 adapter->fallback.rx_entries = adapter->req_rx_add_entries_per_subcrq;
2606 adapter->fallback.tx_entries = adapter->req_tx_entries_per_subcrq;
2607
Thomas Falcon070eca92019-11-25 17:12:53 -06002608 reinit_completion(&adapter->reset_done);
John Allenc26eba02017-10-26 16:23:25 -05002609 adapter->wait_for_reset = true;
Thomas Falconaf894d22018-04-06 18:37:04 -05002610 rc = ibmvnic_reset(adapter, VNIC_RESET_CHANGE_PARAM);
Thomas Falcon476d96c2019-11-25 17:12:55 -06002611
2612 if (rc) {
2613 ret = rc;
2614 goto out;
2615 }
2616 rc = ibmvnic_wait_for_completion(adapter, &adapter->reset_done, 60000);
2617 if (rc) {
2618 ret = -ENODEV;
2619 goto out;
2620 }
John Allenc26eba02017-10-26 16:23:25 -05002621
Thomas Falconaf894d22018-04-06 18:37:04 -05002622 ret = 0;
John Allenc26eba02017-10-26 16:23:25 -05002623 if (adapter->reset_done_rc) {
Thomas Falconaf894d22018-04-06 18:37:04 -05002624 ret = -EIO;
John Allenc26eba02017-10-26 16:23:25 -05002625 adapter->desired.mtu = adapter->fallback.mtu;
2626 adapter->desired.rx_queues = adapter->fallback.rx_queues;
2627 adapter->desired.tx_queues = adapter->fallback.tx_queues;
2628 adapter->desired.rx_entries = adapter->fallback.rx_entries;
2629 adapter->desired.tx_entries = adapter->fallback.tx_entries;
2630
Thomas Falcon070eca92019-11-25 17:12:53 -06002631 reinit_completion(&adapter->reset_done);
Thomas Falconaf894d22018-04-06 18:37:04 -05002632 adapter->wait_for_reset = true;
2633 rc = ibmvnic_reset(adapter, VNIC_RESET_CHANGE_PARAM);
Thomas Falcon476d96c2019-11-25 17:12:55 -06002634 if (rc) {
2635 ret = rc;
2636 goto out;
2637 }
2638 rc = ibmvnic_wait_for_completion(adapter, &adapter->reset_done,
2639 60000);
2640 if (rc) {
2641 ret = -ENODEV;
2642 goto out;
2643 }
John Allenc26eba02017-10-26 16:23:25 -05002644 }
Thomas Falcon476d96c2019-11-25 17:12:55 -06002645out:
John Allenc26eba02017-10-26 16:23:25 -05002646 adapter->wait_for_reset = false;
2647
Thomas Falconaf894d22018-04-06 18:37:04 -05002648 return ret;
John Allenc26eba02017-10-26 16:23:25 -05002649}
2650
John Allen3a807b72017-06-06 16:55:52 -05002651static int ibmvnic_change_mtu(struct net_device *netdev, int new_mtu)
2652{
John Allenc26eba02017-10-26 16:23:25 -05002653 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2654
2655 adapter->desired.mtu = new_mtu + ETH_HLEN;
2656
2657 return wait_for_reset(adapter);
John Allen3a807b72017-06-06 16:55:52 -05002658}
2659
Thomas Falconf10b09e2018-03-12 11:51:05 -05002660static netdev_features_t ibmvnic_features_check(struct sk_buff *skb,
2661 struct net_device *dev,
2662 netdev_features_t features)
2663{
2664 /* Some backing hardware adapters can not
2665 * handle packets with a MSS less than 224
2666 * or with only one segment.
2667 */
2668 if (skb_is_gso(skb)) {
2669 if (skb_shinfo(skb)->gso_size < 224 ||
2670 skb_shinfo(skb)->gso_segs == 1)
2671 features &= ~NETIF_F_GSO_MASK;
2672 }
2673
2674 return features;
2675}
2676
Thomas Falcon032c5e82015-12-21 11:26:06 -06002677static const struct net_device_ops ibmvnic_netdev_ops = {
2678 .ndo_open = ibmvnic_open,
2679 .ndo_stop = ibmvnic_close,
2680 .ndo_start_xmit = ibmvnic_xmit,
2681 .ndo_set_rx_mode = ibmvnic_set_multi,
2682 .ndo_set_mac_address = ibmvnic_set_mac,
2683 .ndo_validate_addr = eth_validate_addr,
Thomas Falcon032c5e82015-12-21 11:26:06 -06002684 .ndo_tx_timeout = ibmvnic_tx_timeout,
John Allen3a807b72017-06-06 16:55:52 -05002685 .ndo_change_mtu = ibmvnic_change_mtu,
Thomas Falconf10b09e2018-03-12 11:51:05 -05002686 .ndo_features_check = ibmvnic_features_check,
Thomas Falcon032c5e82015-12-21 11:26:06 -06002687};
2688
2689/* ethtool functions */
2690
Philippe Reynes8a433792017-01-07 22:37:29 +01002691static int ibmvnic_get_link_ksettings(struct net_device *netdev,
2692 struct ethtool_link_ksettings *cmd)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002693{
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03002694 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2695 int rc;
Philippe Reynes8a433792017-01-07 22:37:29 +01002696
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03002697 rc = send_query_phys_parms(adapter);
2698 if (rc) {
2699 adapter->speed = SPEED_UNKNOWN;
2700 adapter->duplex = DUPLEX_UNKNOWN;
2701 }
2702 cmd->base.speed = adapter->speed;
2703 cmd->base.duplex = adapter->duplex;
Philippe Reynes8a433792017-01-07 22:37:29 +01002704 cmd->base.port = PORT_FIBRE;
2705 cmd->base.phy_address = 0;
2706 cmd->base.autoneg = AUTONEG_ENABLE;
2707
Thomas Falcon032c5e82015-12-21 11:26:06 -06002708 return 0;
2709}
2710
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02002711static void ibmvnic_get_drvinfo(struct net_device *netdev,
Thomas Falcon032c5e82015-12-21 11:26:06 -06002712 struct ethtool_drvinfo *info)
2713{
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02002714 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2715
Thomas Falcon032c5e82015-12-21 11:26:06 -06002716 strlcpy(info->driver, ibmvnic_driver_name, sizeof(info->driver));
2717 strlcpy(info->version, IBMVNIC_DRIVER_VERSION, sizeof(info->version));
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02002718 strlcpy(info->fw_version, adapter->fw_version,
2719 sizeof(info->fw_version));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002720}
2721
2722static u32 ibmvnic_get_msglevel(struct net_device *netdev)
2723{
2724 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2725
2726 return adapter->msg_enable;
2727}
2728
2729static void ibmvnic_set_msglevel(struct net_device *netdev, u32 data)
2730{
2731 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2732
2733 adapter->msg_enable = data;
2734}
2735
2736static u32 ibmvnic_get_link(struct net_device *netdev)
2737{
2738 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2739
2740 /* Don't need to send a query because we request a logical link up at
2741 * init and then we wait for link state indications
2742 */
2743 return adapter->logical_link_state;
2744}
2745
2746static void ibmvnic_get_ringparam(struct net_device *netdev,
2747 struct ethtool_ringparam *ring)
2748{
John Allenbc131b32017-08-02 16:46:30 -05002749 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2750
Thomas Falcon723ad912018-09-28 18:38:26 -05002751 if (adapter->priv_flags & IBMVNIC_USE_SERVER_MAXES) {
2752 ring->rx_max_pending = adapter->max_rx_add_entries_per_subcrq;
2753 ring->tx_max_pending = adapter->max_tx_entries_per_subcrq;
2754 } else {
2755 ring->rx_max_pending = IBMVNIC_MAX_QUEUE_SZ;
2756 ring->tx_max_pending = IBMVNIC_MAX_QUEUE_SZ;
2757 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002758 ring->rx_mini_max_pending = 0;
2759 ring->rx_jumbo_max_pending = 0;
John Allenbc131b32017-08-02 16:46:30 -05002760 ring->rx_pending = adapter->req_rx_add_entries_per_subcrq;
2761 ring->tx_pending = adapter->req_tx_entries_per_subcrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002762 ring->rx_mini_pending = 0;
2763 ring->rx_jumbo_pending = 0;
2764}
2765
John Allenc26eba02017-10-26 16:23:25 -05002766static int ibmvnic_set_ringparam(struct net_device *netdev,
2767 struct ethtool_ringparam *ring)
2768{
2769 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Thomas Falcon723ad912018-09-28 18:38:26 -05002770 int ret;
John Allenc26eba02017-10-26 16:23:25 -05002771
Thomas Falcon723ad912018-09-28 18:38:26 -05002772 ret = 0;
John Allenc26eba02017-10-26 16:23:25 -05002773 adapter->desired.rx_entries = ring->rx_pending;
2774 adapter->desired.tx_entries = ring->tx_pending;
2775
Thomas Falcon723ad912018-09-28 18:38:26 -05002776 ret = wait_for_reset(adapter);
2777
2778 if (!ret &&
2779 (adapter->req_rx_add_entries_per_subcrq != ring->rx_pending ||
2780 adapter->req_tx_entries_per_subcrq != ring->tx_pending))
2781 netdev_info(netdev,
2782 "Could not match full ringsize request. Requested: RX %d, TX %d; Allowed: RX %llu, TX %llu\n",
2783 ring->rx_pending, ring->tx_pending,
2784 adapter->req_rx_add_entries_per_subcrq,
2785 adapter->req_tx_entries_per_subcrq);
2786 return ret;
John Allenc26eba02017-10-26 16:23:25 -05002787}
2788
John Allenc2dbeb62017-08-02 16:47:17 -05002789static void ibmvnic_get_channels(struct net_device *netdev,
2790 struct ethtool_channels *channels)
2791{
2792 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2793
Thomas Falcon723ad912018-09-28 18:38:26 -05002794 if (adapter->priv_flags & IBMVNIC_USE_SERVER_MAXES) {
2795 channels->max_rx = adapter->max_rx_queues;
2796 channels->max_tx = adapter->max_tx_queues;
2797 } else {
2798 channels->max_rx = IBMVNIC_MAX_QUEUES;
2799 channels->max_tx = IBMVNIC_MAX_QUEUES;
2800 }
2801
John Allenc2dbeb62017-08-02 16:47:17 -05002802 channels->max_other = 0;
2803 channels->max_combined = 0;
2804 channels->rx_count = adapter->req_rx_queues;
2805 channels->tx_count = adapter->req_tx_queues;
2806 channels->other_count = 0;
2807 channels->combined_count = 0;
2808}
2809
John Allenc26eba02017-10-26 16:23:25 -05002810static int ibmvnic_set_channels(struct net_device *netdev,
2811 struct ethtool_channels *channels)
2812{
2813 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Thomas Falcon723ad912018-09-28 18:38:26 -05002814 int ret;
John Allenc26eba02017-10-26 16:23:25 -05002815
Thomas Falcon723ad912018-09-28 18:38:26 -05002816 ret = 0;
John Allenc26eba02017-10-26 16:23:25 -05002817 adapter->desired.rx_queues = channels->rx_count;
2818 adapter->desired.tx_queues = channels->tx_count;
2819
Thomas Falcon723ad912018-09-28 18:38:26 -05002820 ret = wait_for_reset(adapter);
2821
2822 if (!ret &&
2823 (adapter->req_rx_queues != channels->rx_count ||
2824 adapter->req_tx_queues != channels->tx_count))
2825 netdev_info(netdev,
2826 "Could not match full channels request. Requested: RX %d, TX %d; Allowed: RX %llu, TX %llu\n",
2827 channels->rx_count, channels->tx_count,
2828 adapter->req_rx_queues, adapter->req_tx_queues);
2829 return ret;
2830
John Allenc26eba02017-10-26 16:23:25 -05002831}
2832
Thomas Falcon032c5e82015-12-21 11:26:06 -06002833static void ibmvnic_get_strings(struct net_device *dev, u32 stringset, u8 *data)
2834{
John Allen3d52b592017-08-02 16:44:14 -05002835 struct ibmvnic_adapter *adapter = netdev_priv(dev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002836 int i;
2837
Thomas Falcon723ad912018-09-28 18:38:26 -05002838 switch (stringset) {
2839 case ETH_SS_STATS:
2840 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats);
2841 i++, data += ETH_GSTRING_LEN)
2842 memcpy(data, ibmvnic_stats[i].name, ETH_GSTRING_LEN);
2843
2844 for (i = 0; i < adapter->req_tx_queues; i++) {
2845 snprintf(data, ETH_GSTRING_LEN, "tx%d_packets", i);
2846 data += ETH_GSTRING_LEN;
2847
2848 snprintf(data, ETH_GSTRING_LEN, "tx%d_bytes", i);
2849 data += ETH_GSTRING_LEN;
2850
2851 snprintf(data, ETH_GSTRING_LEN,
2852 "tx%d_dropped_packets", i);
2853 data += ETH_GSTRING_LEN;
2854 }
2855
2856 for (i = 0; i < adapter->req_rx_queues; i++) {
2857 snprintf(data, ETH_GSTRING_LEN, "rx%d_packets", i);
2858 data += ETH_GSTRING_LEN;
2859
2860 snprintf(data, ETH_GSTRING_LEN, "rx%d_bytes", i);
2861 data += ETH_GSTRING_LEN;
2862
2863 snprintf(data, ETH_GSTRING_LEN, "rx%d_interrupts", i);
2864 data += ETH_GSTRING_LEN;
2865 }
2866 break;
2867
2868 case ETH_SS_PRIV_FLAGS:
2869 for (i = 0; i < ARRAY_SIZE(ibmvnic_priv_flags); i++)
2870 strcpy(data + i * ETH_GSTRING_LEN,
2871 ibmvnic_priv_flags[i]);
2872 break;
2873 default:
Thomas Falcon032c5e82015-12-21 11:26:06 -06002874 return;
John Allen3d52b592017-08-02 16:44:14 -05002875 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002876}
2877
2878static int ibmvnic_get_sset_count(struct net_device *dev, int sset)
2879{
John Allen3d52b592017-08-02 16:44:14 -05002880 struct ibmvnic_adapter *adapter = netdev_priv(dev);
2881
Thomas Falcon032c5e82015-12-21 11:26:06 -06002882 switch (sset) {
2883 case ETH_SS_STATS:
John Allen3d52b592017-08-02 16:44:14 -05002884 return ARRAY_SIZE(ibmvnic_stats) +
2885 adapter->req_tx_queues * NUM_TX_STATS +
2886 adapter->req_rx_queues * NUM_RX_STATS;
Thomas Falcon723ad912018-09-28 18:38:26 -05002887 case ETH_SS_PRIV_FLAGS:
2888 return ARRAY_SIZE(ibmvnic_priv_flags);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002889 default:
2890 return -EOPNOTSUPP;
2891 }
2892}
2893
2894static void ibmvnic_get_ethtool_stats(struct net_device *dev,
2895 struct ethtool_stats *stats, u64 *data)
2896{
2897 struct ibmvnic_adapter *adapter = netdev_priv(dev);
2898 union ibmvnic_crq crq;
John Allen3d52b592017-08-02 16:44:14 -05002899 int i, j;
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05002900 int rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002901
2902 memset(&crq, 0, sizeof(crq));
2903 crq.request_statistics.first = IBMVNIC_CRQ_CMD;
2904 crq.request_statistics.cmd = REQUEST_STATISTICS;
2905 crq.request_statistics.ioba = cpu_to_be32(adapter->stats_token);
2906 crq.request_statistics.len =
2907 cpu_to_be32(sizeof(struct ibmvnic_statistics));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002908
2909 /* Wait for data to be written */
Thomas Falcon070eca92019-11-25 17:12:53 -06002910 reinit_completion(&adapter->stats_done);
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05002911 rc = ibmvnic_send_crq(adapter, &crq);
2912 if (rc)
2913 return;
Thomas Falcon476d96c2019-11-25 17:12:55 -06002914 rc = ibmvnic_wait_for_completion(adapter, &adapter->stats_done, 10000);
2915 if (rc)
2916 return;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002917
2918 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++)
John Allen52da5c12017-08-02 16:45:28 -05002919 data[i] = be64_to_cpu(IBMVNIC_GET_STAT(adapter,
2920 ibmvnic_stats[i].offset));
John Allen3d52b592017-08-02 16:44:14 -05002921
2922 for (j = 0; j < adapter->req_tx_queues; j++) {
2923 data[i] = adapter->tx_stats_buffers[j].packets;
2924 i++;
2925 data[i] = adapter->tx_stats_buffers[j].bytes;
2926 i++;
2927 data[i] = adapter->tx_stats_buffers[j].dropped_packets;
2928 i++;
2929 }
2930
2931 for (j = 0; j < adapter->req_rx_queues; j++) {
2932 data[i] = adapter->rx_stats_buffers[j].packets;
2933 i++;
2934 data[i] = adapter->rx_stats_buffers[j].bytes;
2935 i++;
2936 data[i] = adapter->rx_stats_buffers[j].interrupts;
2937 i++;
2938 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002939}
2940
Thomas Falcon723ad912018-09-28 18:38:26 -05002941static u32 ibmvnic_get_priv_flags(struct net_device *netdev)
2942{
2943 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2944
2945 return adapter->priv_flags;
2946}
2947
2948static int ibmvnic_set_priv_flags(struct net_device *netdev, u32 flags)
2949{
2950 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2951 bool which_maxes = !!(flags & IBMVNIC_USE_SERVER_MAXES);
2952
2953 if (which_maxes)
2954 adapter->priv_flags |= IBMVNIC_USE_SERVER_MAXES;
2955 else
2956 adapter->priv_flags &= ~IBMVNIC_USE_SERVER_MAXES;
2957
2958 return 0;
2959}
Thomas Falcon032c5e82015-12-21 11:26:06 -06002960static const struct ethtool_ops ibmvnic_ethtool_ops = {
Thomas Falcon032c5e82015-12-21 11:26:06 -06002961 .get_drvinfo = ibmvnic_get_drvinfo,
2962 .get_msglevel = ibmvnic_get_msglevel,
2963 .set_msglevel = ibmvnic_set_msglevel,
2964 .get_link = ibmvnic_get_link,
2965 .get_ringparam = ibmvnic_get_ringparam,
John Allenc26eba02017-10-26 16:23:25 -05002966 .set_ringparam = ibmvnic_set_ringparam,
John Allenc2dbeb62017-08-02 16:47:17 -05002967 .get_channels = ibmvnic_get_channels,
John Allenc26eba02017-10-26 16:23:25 -05002968 .set_channels = ibmvnic_set_channels,
Thomas Falcon032c5e82015-12-21 11:26:06 -06002969 .get_strings = ibmvnic_get_strings,
2970 .get_sset_count = ibmvnic_get_sset_count,
2971 .get_ethtool_stats = ibmvnic_get_ethtool_stats,
Philippe Reynes8a433792017-01-07 22:37:29 +01002972 .get_link_ksettings = ibmvnic_get_link_ksettings,
Thomas Falcon723ad912018-09-28 18:38:26 -05002973 .get_priv_flags = ibmvnic_get_priv_flags,
2974 .set_priv_flags = ibmvnic_set_priv_flags,
Thomas Falcon032c5e82015-12-21 11:26:06 -06002975};
2976
2977/* Routines for managing CRQs/sCRQs */
2978
Nathan Fontenot57a49432017-05-26 10:31:12 -04002979static int reset_one_sub_crq_queue(struct ibmvnic_adapter *adapter,
2980 struct ibmvnic_sub_crq_queue *scrq)
2981{
2982 int rc;
2983
Dany Madden9281cf22020-11-25 18:04:26 -06002984 if (!scrq) {
2985 netdev_dbg(adapter->netdev,
2986 "Invalid scrq reset. irq (%d) or msgs (%p).\n",
2987 scrq->irq, scrq->msgs);
2988 return -EINVAL;
2989 }
2990
Nathan Fontenot57a49432017-05-26 10:31:12 -04002991 if (scrq->irq) {
2992 free_irq(scrq->irq, scrq);
2993 irq_dispose_mapping(scrq->irq);
2994 scrq->irq = 0;
2995 }
2996
Dany Madden9281cf22020-11-25 18:04:26 -06002997 if (scrq->msgs) {
2998 memset(scrq->msgs, 0, 4 * PAGE_SIZE);
2999 atomic_set(&scrq->used, 0);
3000 scrq->cur = 0;
Jakub Kicinski55fd59b2020-12-03 15:42:13 -08003001 scrq->ind_buf.index = 0;
Dany Madden9281cf22020-11-25 18:04:26 -06003002 } else {
3003 netdev_dbg(adapter->netdev, "Invalid scrq reset\n");
3004 return -EINVAL;
3005 }
Nathan Fontenot57a49432017-05-26 10:31:12 -04003006
3007 rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
3008 4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
3009 return rc;
3010}
3011
3012static int reset_sub_crq_queues(struct ibmvnic_adapter *adapter)
3013{
3014 int i, rc;
3015
Lijun Pana0faaa22020-11-23 13:35:45 -06003016 if (!adapter->tx_scrq || !adapter->rx_scrq)
3017 return -EINVAL;
3018
Nathan Fontenot57a49432017-05-26 10:31:12 -04003019 for (i = 0; i < adapter->req_tx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05003020 netdev_dbg(adapter->netdev, "Re-setting tx_scrq[%d]\n", i);
Nathan Fontenot57a49432017-05-26 10:31:12 -04003021 rc = reset_one_sub_crq_queue(adapter, adapter->tx_scrq[i]);
3022 if (rc)
3023 return rc;
3024 }
3025
3026 for (i = 0; i < adapter->req_rx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05003027 netdev_dbg(adapter->netdev, "Re-setting rx_scrq[%d]\n", i);
Nathan Fontenot57a49432017-05-26 10:31:12 -04003028 rc = reset_one_sub_crq_queue(adapter, adapter->rx_scrq[i]);
3029 if (rc)
3030 return rc;
3031 }
3032
Nathan Fontenot57a49432017-05-26 10:31:12 -04003033 return rc;
3034}
3035
Thomas Falcon032c5e82015-12-21 11:26:06 -06003036static void release_sub_crq_queue(struct ibmvnic_adapter *adapter,
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06003037 struct ibmvnic_sub_crq_queue *scrq,
3038 bool do_h_free)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003039{
3040 struct device *dev = &adapter->vdev->dev;
3041 long rc;
3042
3043 netdev_dbg(adapter->netdev, "Releasing sub-CRQ\n");
3044
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06003045 if (do_h_free) {
3046 /* Close the sub-crqs */
3047 do {
3048 rc = plpar_hcall_norets(H_FREE_SUB_CRQ,
3049 adapter->vdev->unit_address,
3050 scrq->crq_num);
3051 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
Thomas Falcon032c5e82015-12-21 11:26:06 -06003052
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06003053 if (rc) {
3054 netdev_err(adapter->netdev,
3055 "Failed to release sub-CRQ %16lx, rc = %ld\n",
3056 scrq->crq_num, rc);
3057 }
Thomas Falconffa73852017-04-19 13:44:29 -04003058 }
3059
Thomas Falconf019fb62020-11-18 19:12:17 -06003060 dma_free_coherent(dev,
3061 IBMVNIC_IND_ARR_SZ,
3062 scrq->ind_buf.indir_arr,
3063 scrq->ind_buf.indir_dma);
3064
Thomas Falcon032c5e82015-12-21 11:26:06 -06003065 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
3066 DMA_BIDIRECTIONAL);
3067 free_pages((unsigned long)scrq->msgs, 2);
3068 kfree(scrq);
3069}
3070
3071static struct ibmvnic_sub_crq_queue *init_sub_crq_queue(struct ibmvnic_adapter
3072 *adapter)
3073{
3074 struct device *dev = &adapter->vdev->dev;
3075 struct ibmvnic_sub_crq_queue *scrq;
3076 int rc;
3077
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04003078 scrq = kzalloc(sizeof(*scrq), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003079 if (!scrq)
3080 return NULL;
3081
Nathan Fontenot7f7adc52017-04-19 13:45:16 -04003082 scrq->msgs =
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04003083 (union sub_crq *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, 2);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003084 if (!scrq->msgs) {
3085 dev_warn(dev, "Couldn't allocate crq queue messages page\n");
3086 goto zero_page_failed;
3087 }
3088
3089 scrq->msg_token = dma_map_single(dev, scrq->msgs, 4 * PAGE_SIZE,
3090 DMA_BIDIRECTIONAL);
3091 if (dma_mapping_error(dev, scrq->msg_token)) {
3092 dev_warn(dev, "Couldn't map crq queue messages page\n");
3093 goto map_failed;
3094 }
3095
3096 rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
3097 4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
3098
3099 if (rc == H_RESOURCE)
3100 rc = ibmvnic_reset_crq(adapter);
3101
3102 if (rc == H_CLOSED) {
3103 dev_warn(dev, "Partner adapter not ready, waiting.\n");
3104 } else if (rc) {
3105 dev_warn(dev, "Error %d registering sub-crq\n", rc);
3106 goto reg_failed;
3107 }
3108
Thomas Falcon032c5e82015-12-21 11:26:06 -06003109 scrq->adapter = adapter;
3110 scrq->size = 4 * PAGE_SIZE / sizeof(*scrq->msgs);
Thomas Falconf019fb62020-11-18 19:12:17 -06003111 scrq->ind_buf.index = 0;
3112
3113 scrq->ind_buf.indir_arr =
3114 dma_alloc_coherent(dev,
3115 IBMVNIC_IND_ARR_SZ,
3116 &scrq->ind_buf.indir_dma,
3117 GFP_KERNEL);
3118
3119 if (!scrq->ind_buf.indir_arr)
3120 goto indir_failed;
3121
Thomas Falcon032c5e82015-12-21 11:26:06 -06003122 spin_lock_init(&scrq->lock);
3123
3124 netdev_dbg(adapter->netdev,
3125 "sub-crq initialized, num %lx, hw_irq=%lx, irq=%x\n",
3126 scrq->crq_num, scrq->hw_irq, scrq->irq);
3127
3128 return scrq;
3129
Thomas Falconf019fb62020-11-18 19:12:17 -06003130indir_failed:
3131 do {
3132 rc = plpar_hcall_norets(H_FREE_SUB_CRQ,
3133 adapter->vdev->unit_address,
3134 scrq->crq_num);
3135 } while (rc == H_BUSY || rc == H_IS_LONG_BUSY(rc));
Thomas Falcon032c5e82015-12-21 11:26:06 -06003136reg_failed:
3137 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
3138 DMA_BIDIRECTIONAL);
3139map_failed:
3140 free_pages((unsigned long)scrq->msgs, 2);
3141zero_page_failed:
3142 kfree(scrq);
3143
3144 return NULL;
3145}
3146
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06003147static void release_sub_crqs(struct ibmvnic_adapter *adapter, bool do_h_free)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003148{
3149 int i;
3150
3151 if (adapter->tx_scrq) {
Nathan Fontenot82e3be32018-02-21 21:33:56 -06003152 for (i = 0; i < adapter->num_active_tx_scrqs; i++) {
Nathan Fontenotb5108882017-03-30 02:49:18 -04003153 if (!adapter->tx_scrq[i])
3154 continue;
3155
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05003156 netdev_dbg(adapter->netdev, "Releasing tx_scrq[%d]\n",
3157 i);
Nathan Fontenotb5108882017-03-30 02:49:18 -04003158 if (adapter->tx_scrq[i]->irq) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06003159 free_irq(adapter->tx_scrq[i]->irq,
3160 adapter->tx_scrq[i]);
Thomas Falcon88eb98a2016-07-06 15:35:16 -05003161 irq_dispose_mapping(adapter->tx_scrq[i]->irq);
Nathan Fontenotb5108882017-03-30 02:49:18 -04003162 adapter->tx_scrq[i]->irq = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003163 }
Nathan Fontenotb5108882017-03-30 02:49:18 -04003164
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06003165 release_sub_crq_queue(adapter, adapter->tx_scrq[i],
3166 do_h_free);
Nathan Fontenotb5108882017-03-30 02:49:18 -04003167 }
3168
Nathan Fontenot9501df32017-03-15 23:38:07 -04003169 kfree(adapter->tx_scrq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003170 adapter->tx_scrq = NULL;
Nathan Fontenot82e3be32018-02-21 21:33:56 -06003171 adapter->num_active_tx_scrqs = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003172 }
3173
3174 if (adapter->rx_scrq) {
Nathan Fontenot82e3be32018-02-21 21:33:56 -06003175 for (i = 0; i < adapter->num_active_rx_scrqs; i++) {
Nathan Fontenotb5108882017-03-30 02:49:18 -04003176 if (!adapter->rx_scrq[i])
3177 continue;
3178
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05003179 netdev_dbg(adapter->netdev, "Releasing rx_scrq[%d]\n",
3180 i);
Nathan Fontenotb5108882017-03-30 02:49:18 -04003181 if (adapter->rx_scrq[i]->irq) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06003182 free_irq(adapter->rx_scrq[i]->irq,
3183 adapter->rx_scrq[i]);
Thomas Falcon88eb98a2016-07-06 15:35:16 -05003184 irq_dispose_mapping(adapter->rx_scrq[i]->irq);
Nathan Fontenotb5108882017-03-30 02:49:18 -04003185 adapter->rx_scrq[i]->irq = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003186 }
Nathan Fontenotb5108882017-03-30 02:49:18 -04003187
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06003188 release_sub_crq_queue(adapter, adapter->rx_scrq[i],
3189 do_h_free);
Nathan Fontenotb5108882017-03-30 02:49:18 -04003190 }
3191
Nathan Fontenot9501df32017-03-15 23:38:07 -04003192 kfree(adapter->rx_scrq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003193 adapter->rx_scrq = NULL;
Nathan Fontenot82e3be32018-02-21 21:33:56 -06003194 adapter->num_active_rx_scrqs = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003195 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06003196}
3197
3198static int disable_scrq_irq(struct ibmvnic_adapter *adapter,
3199 struct ibmvnic_sub_crq_queue *scrq)
3200{
3201 struct device *dev = &adapter->vdev->dev;
3202 unsigned long rc;
3203
3204 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
3205 H_DISABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
3206 if (rc)
3207 dev_err(dev, "Couldn't disable scrq irq 0x%lx. rc=%ld\n",
3208 scrq->hw_irq, rc);
3209 return rc;
3210}
3211
3212static int enable_scrq_irq(struct ibmvnic_adapter *adapter,
3213 struct ibmvnic_sub_crq_queue *scrq)
3214{
3215 struct device *dev = &adapter->vdev->dev;
3216 unsigned long rc;
3217
3218 if (scrq->hw_irq > 0x100000000ULL) {
3219 dev_err(dev, "bad hw_irq = %lx\n", scrq->hw_irq);
3220 return 1;
3221 }
3222
Juliet Kim7ed5b312019-09-20 16:11:23 -04003223 if (test_bit(0, &adapter->resetting) &&
Nathan Fontenot73f9d362018-05-22 11:21:10 -05003224 adapter->reset_reason == VNIC_RESET_MOBILITY) {
Juliet Kim284f87d2019-11-20 10:50:03 -05003225 u64 val = (0xff000000) | scrq->hw_irq;
Nathan Fontenot73f9d362018-05-22 11:21:10 -05003226
Juliet Kim284f87d2019-11-20 10:50:03 -05003227 rc = plpar_hcall_norets(H_EOI, val);
Juliet Kim2df5c602019-11-20 10:50:04 -05003228 /* H_EOI would fail with rc = H_FUNCTION when running
3229 * in XIVE mode which is expected, but not an error.
3230 */
3231 if (rc && (rc != H_FUNCTION))
Juliet Kim284f87d2019-11-20 10:50:03 -05003232 dev_err(dev, "H_EOI FAILED irq 0x%llx. rc=%ld\n",
3233 val, rc);
Nathan Fontenot73f9d362018-05-22 11:21:10 -05003234 }
Thomas Falconf23e0642018-04-15 18:53:36 -05003235
Thomas Falcon032c5e82015-12-21 11:26:06 -06003236 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
3237 H_ENABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
3238 if (rc)
3239 dev_err(dev, "Couldn't enable scrq irq 0x%lx. rc=%ld\n",
3240 scrq->hw_irq, rc);
3241 return rc;
3242}
3243
3244static int ibmvnic_complete_tx(struct ibmvnic_adapter *adapter,
3245 struct ibmvnic_sub_crq_queue *scrq)
3246{
3247 struct device *dev = &adapter->vdev->dev;
Thomas Falcon06b3e352018-03-16 20:00:28 -05003248 struct ibmvnic_tx_pool *tx_pool;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003249 struct ibmvnic_tx_buff *txbuff;
Thomas Falcon0d973382020-11-18 19:12:19 -06003250 struct netdev_queue *txq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003251 union sub_crq *next;
3252 int index;
Thomas Falconc62aa372020-11-18 19:12:20 -06003253 int i;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003254
3255restart_loop:
3256 while (pending_scrq(adapter, scrq)) {
3257 unsigned int pool = scrq->pool_index;
Thomas Falconffc385b2018-02-18 10:08:41 -06003258 int num_entries = 0;
Thomas Falcon0d973382020-11-18 19:12:19 -06003259 int total_bytes = 0;
3260 int num_packets = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003261
Thomas Falconb71ec952020-12-01 09:52:10 -06003262 /* The queue entry at the current index is peeked at above
3263 * to determine that there is a valid descriptor awaiting
3264 * processing. We want to be sure that the current slot
3265 * holds a valid descriptor before reading its contents.
3266 */
3267 dma_rmb();
3268
Thomas Falcon032c5e82015-12-21 11:26:06 -06003269 next = ibmvnic_next_scrq(adapter, scrq);
3270 for (i = 0; i < next->tx_comp.num_comps; i++) {
Thomas Falconba246c12020-12-01 09:52:11 -06003271 if (next->tx_comp.rcs[i])
Thomas Falcon032c5e82015-12-21 11:26:06 -06003272 dev_err(dev, "tx error %x\n",
3273 next->tx_comp.rcs[i]);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003274 index = be32_to_cpu(next->tx_comp.correlators[i]);
Thomas Falcon06b3e352018-03-16 20:00:28 -05003275 if (index & IBMVNIC_TSO_POOL_MASK) {
3276 tx_pool = &adapter->tso_pool[pool];
3277 index &= ~IBMVNIC_TSO_POOL_MASK;
3278 } else {
3279 tx_pool = &adapter->tx_pool[pool];
3280 }
3281
3282 txbuff = &tx_pool->tx_buff[index];
Thomas Falcon0d973382020-11-18 19:12:19 -06003283 num_packets++;
Thomas Falconffc385b2018-02-18 10:08:41 -06003284 num_entries += txbuff->num_entries;
Thomas Falcon0d973382020-11-18 19:12:19 -06003285 if (txbuff->skb) {
3286 total_bytes += txbuff->skb->len;
3287 dev_consume_skb_irq(txbuff->skb);
3288 txbuff->skb = NULL;
3289 } else {
3290 netdev_warn(adapter->netdev,
3291 "TX completion received with NULL socket buffer\n");
3292 }
Thomas Falcon06b3e352018-03-16 20:00:28 -05003293 tx_pool->free_map[tx_pool->producer_index] = index;
3294 tx_pool->producer_index =
3295 (tx_pool->producer_index + 1) %
3296 tx_pool->num_buffers;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003297 }
3298 /* remove tx_comp scrq*/
3299 next->tx_comp.first = 0;
Nathan Fontenot7c3e7de2017-05-03 14:05:25 -04003300
Thomas Falcon0d973382020-11-18 19:12:19 -06003301 txq = netdev_get_tx_queue(adapter->netdev, scrq->pool_index);
3302 netdev_tx_completed_queue(txq, num_packets, total_bytes);
3303
Thomas Falconffc385b2018-02-18 10:08:41 -06003304 if (atomic_sub_return(num_entries, &scrq->used) <=
Nathan Fontenot7c3e7de2017-05-03 14:05:25 -04003305 (adapter->req_tx_entries_per_subcrq / 2) &&
3306 __netif_subqueue_stopped(adapter->netdev,
3307 scrq->pool_index)) {
3308 netif_wake_subqueue(adapter->netdev, scrq->pool_index);
Thomas Falcon0aecb132018-02-26 18:10:58 -06003309 netdev_dbg(adapter->netdev, "Started queue %d\n",
3310 scrq->pool_index);
Nathan Fontenot7c3e7de2017-05-03 14:05:25 -04003311 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06003312 }
3313
3314 enable_scrq_irq(adapter, scrq);
3315
3316 if (pending_scrq(adapter, scrq)) {
3317 disable_scrq_irq(adapter, scrq);
3318 goto restart_loop;
3319 }
3320
3321 return 0;
3322}
3323
3324static irqreturn_t ibmvnic_interrupt_tx(int irq, void *instance)
3325{
3326 struct ibmvnic_sub_crq_queue *scrq = instance;
3327 struct ibmvnic_adapter *adapter = scrq->adapter;
3328
3329 disable_scrq_irq(adapter, scrq);
3330 ibmvnic_complete_tx(adapter, scrq);
3331
3332 return IRQ_HANDLED;
3333}
3334
3335static irqreturn_t ibmvnic_interrupt_rx(int irq, void *instance)
3336{
3337 struct ibmvnic_sub_crq_queue *scrq = instance;
3338 struct ibmvnic_adapter *adapter = scrq->adapter;
3339
Nathan Fontenot09fb35e2018-01-10 10:40:09 -06003340 /* When booting a kdump kernel we can hit pending interrupts
3341 * prior to completing driver initialization.
3342 */
3343 if (unlikely(adapter->state != VNIC_OPEN))
3344 return IRQ_NONE;
3345
John Allen3d52b592017-08-02 16:44:14 -05003346 adapter->rx_stats_buffers[scrq->scrq_num].interrupts++;
3347
Thomas Falcon032c5e82015-12-21 11:26:06 -06003348 if (napi_schedule_prep(&adapter->napi[scrq->scrq_num])) {
3349 disable_scrq_irq(adapter, scrq);
3350 __napi_schedule(&adapter->napi[scrq->scrq_num]);
3351 }
3352
3353 return IRQ_HANDLED;
3354}
3355
Thomas Falconea22d512016-07-06 15:35:17 -05003356static int init_sub_crq_irqs(struct ibmvnic_adapter *adapter)
3357{
3358 struct device *dev = &adapter->vdev->dev;
3359 struct ibmvnic_sub_crq_queue *scrq;
3360 int i = 0, j = 0;
3361 int rc = 0;
3362
3363 for (i = 0; i < adapter->req_tx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05003364 netdev_dbg(adapter->netdev, "Initializing tx_scrq[%d] irq\n",
3365 i);
Thomas Falconea22d512016-07-06 15:35:17 -05003366 scrq = adapter->tx_scrq[i];
3367 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
3368
Michael Ellerman99c17902016-09-10 19:59:05 +10003369 if (!scrq->irq) {
Thomas Falconea22d512016-07-06 15:35:17 -05003370 rc = -EINVAL;
3371 dev_err(dev, "Error mapping irq\n");
3372 goto req_tx_irq_failed;
3373 }
3374
Murilo Fossa Vicentinie56e2512019-04-25 11:02:33 -03003375 snprintf(scrq->name, sizeof(scrq->name), "ibmvnic-%x-tx%d",
3376 adapter->vdev->unit_address, i);
Thomas Falconea22d512016-07-06 15:35:17 -05003377 rc = request_irq(scrq->irq, ibmvnic_interrupt_tx,
Murilo Fossa Vicentinie56e2512019-04-25 11:02:33 -03003378 0, scrq->name, scrq);
Thomas Falconea22d512016-07-06 15:35:17 -05003379
3380 if (rc) {
3381 dev_err(dev, "Couldn't register tx irq 0x%x. rc=%d\n",
3382 scrq->irq, rc);
3383 irq_dispose_mapping(scrq->irq);
Nathan Fontenotaf9090c2018-02-20 11:04:18 -06003384 goto req_tx_irq_failed;
Thomas Falconea22d512016-07-06 15:35:17 -05003385 }
3386 }
3387
3388 for (i = 0; i < adapter->req_rx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05003389 netdev_dbg(adapter->netdev, "Initializing rx_scrq[%d] irq\n",
3390 i);
Thomas Falconea22d512016-07-06 15:35:17 -05003391 scrq = adapter->rx_scrq[i];
3392 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
Michael Ellerman99c17902016-09-10 19:59:05 +10003393 if (!scrq->irq) {
Thomas Falconea22d512016-07-06 15:35:17 -05003394 rc = -EINVAL;
3395 dev_err(dev, "Error mapping irq\n");
3396 goto req_rx_irq_failed;
3397 }
Murilo Fossa Vicentinie56e2512019-04-25 11:02:33 -03003398 snprintf(scrq->name, sizeof(scrq->name), "ibmvnic-%x-rx%d",
3399 adapter->vdev->unit_address, i);
Thomas Falconea22d512016-07-06 15:35:17 -05003400 rc = request_irq(scrq->irq, ibmvnic_interrupt_rx,
Murilo Fossa Vicentinie56e2512019-04-25 11:02:33 -03003401 0, scrq->name, scrq);
Thomas Falconea22d512016-07-06 15:35:17 -05003402 if (rc) {
3403 dev_err(dev, "Couldn't register rx irq 0x%x. rc=%d\n",
3404 scrq->irq, rc);
3405 irq_dispose_mapping(scrq->irq);
3406 goto req_rx_irq_failed;
3407 }
3408 }
3409 return rc;
3410
3411req_rx_irq_failed:
Thomas Falcon8bf371e2016-10-27 12:28:52 -05003412 for (j = 0; j < i; j++) {
Thomas Falconea22d512016-07-06 15:35:17 -05003413 free_irq(adapter->rx_scrq[j]->irq, adapter->rx_scrq[j]);
3414 irq_dispose_mapping(adapter->rx_scrq[j]->irq);
Thomas Falcon8bf371e2016-10-27 12:28:52 -05003415 }
Thomas Falconea22d512016-07-06 15:35:17 -05003416 i = adapter->req_tx_queues;
3417req_tx_irq_failed:
Thomas Falcon8bf371e2016-10-27 12:28:52 -05003418 for (j = 0; j < i; j++) {
Thomas Falconea22d512016-07-06 15:35:17 -05003419 free_irq(adapter->tx_scrq[j]->irq, adapter->tx_scrq[j]);
Thomas Falcon27a21452020-07-29 16:36:32 -05003420 irq_dispose_mapping(adapter->tx_scrq[j]->irq);
Thomas Falcon8bf371e2016-10-27 12:28:52 -05003421 }
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06003422 release_sub_crqs(adapter, 1);
Thomas Falconea22d512016-07-06 15:35:17 -05003423 return rc;
3424}
3425
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003426static int init_sub_crqs(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003427{
3428 struct device *dev = &adapter->vdev->dev;
3429 struct ibmvnic_sub_crq_queue **allqueues;
3430 int registered_queues = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003431 int total_queues;
3432 int more = 0;
Thomas Falconea22d512016-07-06 15:35:17 -05003433 int i;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003434
Thomas Falcon032c5e82015-12-21 11:26:06 -06003435 total_queues = adapter->req_tx_queues + adapter->req_rx_queues;
3436
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04003437 allqueues = kcalloc(total_queues, sizeof(*allqueues), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003438 if (!allqueues)
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003439 return -1;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003440
3441 for (i = 0; i < total_queues; i++) {
3442 allqueues[i] = init_sub_crq_queue(adapter);
3443 if (!allqueues[i]) {
3444 dev_warn(dev, "Couldn't allocate all sub-crqs\n");
3445 break;
3446 }
3447 registered_queues++;
3448 }
3449
3450 /* Make sure we were able to register the minimum number of queues */
3451 if (registered_queues <
3452 adapter->min_tx_queues + adapter->min_rx_queues) {
3453 dev_err(dev, "Fatal: Couldn't init min number of sub-crqs\n");
3454 goto tx_failed;
3455 }
3456
3457 /* Distribute the failed allocated queues*/
3458 for (i = 0; i < total_queues - registered_queues + more ; i++) {
3459 netdev_dbg(adapter->netdev, "Reducing number of queues\n");
3460 switch (i % 3) {
3461 case 0:
3462 if (adapter->req_rx_queues > adapter->min_rx_queues)
3463 adapter->req_rx_queues--;
3464 else
3465 more++;
3466 break;
3467 case 1:
3468 if (adapter->req_tx_queues > adapter->min_tx_queues)
3469 adapter->req_tx_queues--;
3470 else
3471 more++;
3472 break;
3473 }
3474 }
3475
3476 adapter->tx_scrq = kcalloc(adapter->req_tx_queues,
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04003477 sizeof(*adapter->tx_scrq), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003478 if (!adapter->tx_scrq)
3479 goto tx_failed;
3480
3481 for (i = 0; i < adapter->req_tx_queues; i++) {
3482 adapter->tx_scrq[i] = allqueues[i];
3483 adapter->tx_scrq[i]->pool_index = i;
Nathan Fontenot82e3be32018-02-21 21:33:56 -06003484 adapter->num_active_tx_scrqs++;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003485 }
3486
3487 adapter->rx_scrq = kcalloc(adapter->req_rx_queues,
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04003488 sizeof(*adapter->rx_scrq), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003489 if (!adapter->rx_scrq)
3490 goto rx_failed;
3491
3492 for (i = 0; i < adapter->req_rx_queues; i++) {
3493 adapter->rx_scrq[i] = allqueues[i + adapter->req_tx_queues];
3494 adapter->rx_scrq[i]->scrq_num = i;
Nathan Fontenot82e3be32018-02-21 21:33:56 -06003495 adapter->num_active_rx_scrqs++;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003496 }
3497
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003498 kfree(allqueues);
3499 return 0;
3500
3501rx_failed:
3502 kfree(adapter->tx_scrq);
3503 adapter->tx_scrq = NULL;
3504tx_failed:
3505 for (i = 0; i < registered_queues; i++)
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06003506 release_sub_crq_queue(adapter, allqueues[i], 1);
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003507 kfree(allqueues);
3508 return -1;
3509}
3510
Lijun Pan09081b92020-09-27 20:13:27 -05003511static void send_request_cap(struct ibmvnic_adapter *adapter, int retry)
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003512{
3513 struct device *dev = &adapter->vdev->dev;
3514 union ibmvnic_crq crq;
John Allenc26eba02017-10-26 16:23:25 -05003515 int max_entries;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003516
3517 if (!retry) {
3518 /* Sub-CRQ entries are 32 byte long */
3519 int entries_page = 4 * PAGE_SIZE / (sizeof(u64) * 4);
3520
3521 if (adapter->min_tx_entries_per_subcrq > entries_page ||
3522 adapter->min_rx_add_entries_per_subcrq > entries_page) {
3523 dev_err(dev, "Fatal, invalid entries per sub-crq\n");
3524 return;
3525 }
3526
John Allenc26eba02017-10-26 16:23:25 -05003527 if (adapter->desired.mtu)
3528 adapter->req_mtu = adapter->desired.mtu;
3529 else
3530 adapter->req_mtu = adapter->netdev->mtu + ETH_HLEN;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003531
John Allenc26eba02017-10-26 16:23:25 -05003532 if (!adapter->desired.tx_entries)
3533 adapter->desired.tx_entries =
3534 adapter->max_tx_entries_per_subcrq;
3535 if (!adapter->desired.rx_entries)
3536 adapter->desired.rx_entries =
3537 adapter->max_rx_add_entries_per_subcrq;
3538
3539 max_entries = IBMVNIC_MAX_LTB_SIZE /
3540 (adapter->req_mtu + IBMVNIC_BUFFER_HLEN);
3541
3542 if ((adapter->req_mtu + IBMVNIC_BUFFER_HLEN) *
3543 adapter->desired.tx_entries > IBMVNIC_MAX_LTB_SIZE) {
3544 adapter->desired.tx_entries = max_entries;
3545 }
3546
3547 if ((adapter->req_mtu + IBMVNIC_BUFFER_HLEN) *
3548 adapter->desired.rx_entries > IBMVNIC_MAX_LTB_SIZE) {
3549 adapter->desired.rx_entries = max_entries;
3550 }
3551
3552 if (adapter->desired.tx_entries)
3553 adapter->req_tx_entries_per_subcrq =
3554 adapter->desired.tx_entries;
3555 else
3556 adapter->req_tx_entries_per_subcrq =
3557 adapter->max_tx_entries_per_subcrq;
3558
3559 if (adapter->desired.rx_entries)
3560 adapter->req_rx_add_entries_per_subcrq =
3561 adapter->desired.rx_entries;
3562 else
3563 adapter->req_rx_add_entries_per_subcrq =
3564 adapter->max_rx_add_entries_per_subcrq;
3565
3566 if (adapter->desired.tx_queues)
3567 adapter->req_tx_queues =
3568 adapter->desired.tx_queues;
3569 else
3570 adapter->req_tx_queues =
3571 adapter->opt_tx_comp_sub_queues;
3572
3573 if (adapter->desired.rx_queues)
3574 adapter->req_rx_queues =
3575 adapter->desired.rx_queues;
3576 else
3577 adapter->req_rx_queues =
3578 adapter->opt_rx_comp_queues;
3579
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003580 adapter->req_rx_add_queues = adapter->max_rx_add_queues;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003581 }
3582
Thomas Falcon032c5e82015-12-21 11:26:06 -06003583 memset(&crq, 0, sizeof(crq));
3584 crq.request_capability.first = IBMVNIC_CRQ_CMD;
3585 crq.request_capability.cmd = REQUEST_CAPABILITY;
3586
3587 crq.request_capability.capability = cpu_to_be16(REQ_TX_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06003588 crq.request_capability.number = cpu_to_be64(adapter->req_tx_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06003589 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003590 ibmvnic_send_crq(adapter, &crq);
3591
3592 crq.request_capability.capability = cpu_to_be16(REQ_RX_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06003593 crq.request_capability.number = cpu_to_be64(adapter->req_rx_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06003594 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003595 ibmvnic_send_crq(adapter, &crq);
3596
3597 crq.request_capability.capability = cpu_to_be16(REQ_RX_ADD_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06003598 crq.request_capability.number = cpu_to_be64(adapter->req_rx_add_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06003599 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003600 ibmvnic_send_crq(adapter, &crq);
3601
3602 crq.request_capability.capability =
3603 cpu_to_be16(REQ_TX_ENTRIES_PER_SUBCRQ);
3604 crq.request_capability.number =
Thomas Falconde89e852016-03-01 10:20:09 -06003605 cpu_to_be64(adapter->req_tx_entries_per_subcrq);
Thomas Falcon901e0402017-02-15 12:17:59 -06003606 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003607 ibmvnic_send_crq(adapter, &crq);
3608
3609 crq.request_capability.capability =
3610 cpu_to_be16(REQ_RX_ADD_ENTRIES_PER_SUBCRQ);
3611 crq.request_capability.number =
Thomas Falconde89e852016-03-01 10:20:09 -06003612 cpu_to_be64(adapter->req_rx_add_entries_per_subcrq);
Thomas Falcon901e0402017-02-15 12:17:59 -06003613 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003614 ibmvnic_send_crq(adapter, &crq);
3615
3616 crq.request_capability.capability = cpu_to_be16(REQ_MTU);
Thomas Falconde89e852016-03-01 10:20:09 -06003617 crq.request_capability.number = cpu_to_be64(adapter->req_mtu);
Thomas Falcon901e0402017-02-15 12:17:59 -06003618 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003619 ibmvnic_send_crq(adapter, &crq);
3620
3621 if (adapter->netdev->flags & IFF_PROMISC) {
3622 if (adapter->promisc_supported) {
3623 crq.request_capability.capability =
3624 cpu_to_be16(PROMISC_REQUESTED);
Thomas Falconde89e852016-03-01 10:20:09 -06003625 crq.request_capability.number = cpu_to_be64(1);
Thomas Falcon901e0402017-02-15 12:17:59 -06003626 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003627 ibmvnic_send_crq(adapter, &crq);
3628 }
3629 } else {
3630 crq.request_capability.capability =
3631 cpu_to_be16(PROMISC_REQUESTED);
Thomas Falconde89e852016-03-01 10:20:09 -06003632 crq.request_capability.number = cpu_to_be64(0);
Thomas Falcon901e0402017-02-15 12:17:59 -06003633 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003634 ibmvnic_send_crq(adapter, &crq);
3635 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06003636}
3637
3638static int pending_scrq(struct ibmvnic_adapter *adapter,
3639 struct ibmvnic_sub_crq_queue *scrq)
3640{
3641 union sub_crq *entry = &scrq->msgs[scrq->cur];
3642
Thomas Falcon1cf9cc72017-06-14 23:50:08 -05003643 if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003644 return 1;
3645 else
3646 return 0;
3647}
3648
3649static union sub_crq *ibmvnic_next_scrq(struct ibmvnic_adapter *adapter,
3650 struct ibmvnic_sub_crq_queue *scrq)
3651{
3652 union sub_crq *entry;
3653 unsigned long flags;
3654
3655 spin_lock_irqsave(&scrq->lock, flags);
3656 entry = &scrq->msgs[scrq->cur];
3657 if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP) {
3658 if (++scrq->cur == scrq->size)
3659 scrq->cur = 0;
3660 } else {
3661 entry = NULL;
3662 }
3663 spin_unlock_irqrestore(&scrq->lock, flags);
3664
Thomas Falconb71ec952020-12-01 09:52:10 -06003665 /* Ensure that the entire buffer descriptor has been
3666 * loaded before reading its contents
3667 */
3668 dma_rmb();
3669
Thomas Falcon032c5e82015-12-21 11:26:06 -06003670 return entry;
3671}
3672
3673static union ibmvnic_crq *ibmvnic_next_crq(struct ibmvnic_adapter *adapter)
3674{
3675 struct ibmvnic_crq_queue *queue = &adapter->crq;
3676 union ibmvnic_crq *crq;
3677
3678 crq = &queue->msgs[queue->cur];
3679 if (crq->generic.first & IBMVNIC_CRQ_CMD_RSP) {
3680 if (++queue->cur == queue->size)
3681 queue->cur = 0;
3682 } else {
3683 crq = NULL;
3684 }
3685
3686 return crq;
3687}
3688
Thomas Falcon2d14d372018-07-13 12:03:32 -05003689static void print_subcrq_error(struct device *dev, int rc, const char *func)
3690{
3691 switch (rc) {
3692 case H_PARAMETER:
3693 dev_warn_ratelimited(dev,
3694 "%s failed: Send request is malformed or adapter failover pending. (rc=%d)\n",
3695 func, rc);
3696 break;
3697 case H_CLOSED:
3698 dev_warn_ratelimited(dev,
3699 "%s failed: Backing queue closed. Adapter is down or failover pending. (rc=%d)\n",
3700 func, rc);
3701 break;
3702 default:
3703 dev_err_ratelimited(dev, "%s failed: (rc=%d)\n", func, rc);
3704 break;
3705 }
3706}
3707
Thomas Falconad7775d2016-04-01 17:20:34 -05003708static int send_subcrq_indirect(struct ibmvnic_adapter *adapter,
3709 u64 remote_handle, u64 ioba, u64 num_entries)
3710{
3711 unsigned int ua = adapter->vdev->unit_address;
3712 struct device *dev = &adapter->vdev->dev;
3713 int rc;
3714
3715 /* Make sure the hypervisor sees the complete request */
3716 mb();
3717 rc = plpar_hcall_norets(H_SEND_SUB_CRQ_INDIRECT, ua,
3718 cpu_to_be64(remote_handle),
3719 ioba, num_entries);
3720
Thomas Falcon2d14d372018-07-13 12:03:32 -05003721 if (rc)
3722 print_subcrq_error(dev, rc, __func__);
Thomas Falconad7775d2016-04-01 17:20:34 -05003723
3724 return rc;
3725}
3726
Thomas Falcon032c5e82015-12-21 11:26:06 -06003727static int ibmvnic_send_crq(struct ibmvnic_adapter *adapter,
3728 union ibmvnic_crq *crq)
3729{
3730 unsigned int ua = adapter->vdev->unit_address;
3731 struct device *dev = &adapter->vdev->dev;
3732 u64 *u64_crq = (u64 *)crq;
3733 int rc;
3734
3735 netdev_dbg(adapter->netdev, "Sending CRQ: %016lx %016lx\n",
3736 (unsigned long int)cpu_to_be64(u64_crq[0]),
3737 (unsigned long int)cpu_to_be64(u64_crq[1]));
3738
Thomas Falcon51536982018-05-23 13:37:56 -05003739 if (!adapter->crq.active &&
3740 crq->generic.first != IBMVNIC_CRQ_INIT_CMD) {
3741 dev_warn(dev, "Invalid request detected while CRQ is inactive, possible device state change during reset\n");
3742 return -EINVAL;
3743 }
3744
Thomas Falcon032c5e82015-12-21 11:26:06 -06003745 /* Make sure the hypervisor sees the complete request */
3746 mb();
3747
3748 rc = plpar_hcall_norets(H_SEND_CRQ, ua,
3749 cpu_to_be64(u64_crq[0]),
3750 cpu_to_be64(u64_crq[1]));
3751
3752 if (rc) {
Nathan Fontenotec95dff2018-02-07 13:00:24 -06003753 if (rc == H_CLOSED) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06003754 dev_warn(dev, "CRQ Queue closed\n");
Lijun Panfa68bfa2020-08-19 17:52:24 -05003755 /* do not reset, report the fail, wait for passive init from server */
Nathan Fontenotec95dff2018-02-07 13:00:24 -06003756 }
3757
Thomas Falcon032c5e82015-12-21 11:26:06 -06003758 dev_warn(dev, "Send error (rc=%d)\n", rc);
3759 }
3760
3761 return rc;
3762}
3763
3764static int ibmvnic_send_crq_init(struct ibmvnic_adapter *adapter)
3765{
Thomas Falcon36a782f2020-08-31 11:59:57 -05003766 struct device *dev = &adapter->vdev->dev;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003767 union ibmvnic_crq crq;
Thomas Falcon36a782f2020-08-31 11:59:57 -05003768 int retries = 100;
3769 int rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003770
3771 memset(&crq, 0, sizeof(crq));
3772 crq.generic.first = IBMVNIC_CRQ_INIT_CMD;
3773 crq.generic.cmd = IBMVNIC_CRQ_INIT;
3774 netdev_dbg(adapter->netdev, "Sending CRQ init\n");
3775
Thomas Falcon36a782f2020-08-31 11:59:57 -05003776 do {
3777 rc = ibmvnic_send_crq(adapter, &crq);
3778 if (rc != H_CLOSED)
3779 break;
3780 retries--;
3781 msleep(50);
3782
3783 } while (retries > 0);
3784
3785 if (rc) {
3786 dev_err(dev, "Failed to send init request, rc = %d\n", rc);
3787 return rc;
3788 }
3789
3790 return 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003791}
3792
Thomas Falcon032c5e82015-12-21 11:26:06 -06003793static int send_version_xchg(struct ibmvnic_adapter *adapter)
3794{
3795 union ibmvnic_crq crq;
3796
3797 memset(&crq, 0, sizeof(crq));
3798 crq.version_exchange.first = IBMVNIC_CRQ_CMD;
3799 crq.version_exchange.cmd = VERSION_EXCHANGE;
3800 crq.version_exchange.version = cpu_to_be16(ibmvnic_version);
3801
3802 return ibmvnic_send_crq(adapter, &crq);
3803}
3804
Nathan Fontenot37798d02017-11-08 11:23:56 -06003805struct vnic_login_client_data {
3806 u8 type;
3807 __be16 len;
Kees Cook08ea5562018-04-10 15:26:43 -07003808 char name[];
Nathan Fontenot37798d02017-11-08 11:23:56 -06003809} __packed;
3810
3811static int vnic_client_data_len(struct ibmvnic_adapter *adapter)
3812{
3813 int len;
3814
3815 /* Calculate the amount of buffer space needed for the
3816 * vnic client data in the login buffer. There are four entries,
3817 * OS name, LPAR name, device name, and a null last entry.
3818 */
3819 len = 4 * sizeof(struct vnic_login_client_data);
3820 len += 6; /* "Linux" plus NULL */
3821 len += strlen(utsname()->nodename) + 1;
3822 len += strlen(adapter->netdev->name) + 1;
3823
3824 return len;
3825}
3826
3827static void vnic_add_client_data(struct ibmvnic_adapter *adapter,
3828 struct vnic_login_client_data *vlcd)
3829{
3830 const char *os_name = "Linux";
3831 int len;
3832
3833 /* Type 1 - LPAR OS */
3834 vlcd->type = 1;
3835 len = strlen(os_name) + 1;
3836 vlcd->len = cpu_to_be16(len);
Kees Cook08ea5562018-04-10 15:26:43 -07003837 strncpy(vlcd->name, os_name, len);
3838 vlcd = (struct vnic_login_client_data *)(vlcd->name + len);
Nathan Fontenot37798d02017-11-08 11:23:56 -06003839
3840 /* Type 2 - LPAR name */
3841 vlcd->type = 2;
3842 len = strlen(utsname()->nodename) + 1;
3843 vlcd->len = cpu_to_be16(len);
Kees Cook08ea5562018-04-10 15:26:43 -07003844 strncpy(vlcd->name, utsname()->nodename, len);
3845 vlcd = (struct vnic_login_client_data *)(vlcd->name + len);
Nathan Fontenot37798d02017-11-08 11:23:56 -06003846
3847 /* Type 3 - device name */
3848 vlcd->type = 3;
3849 len = strlen(adapter->netdev->name) + 1;
3850 vlcd->len = cpu_to_be16(len);
Kees Cook08ea5562018-04-10 15:26:43 -07003851 strncpy(vlcd->name, adapter->netdev->name, len);
Nathan Fontenot37798d02017-11-08 11:23:56 -06003852}
3853
Thomas Falcon20a8ab72018-02-26 18:10:59 -06003854static int send_login(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003855{
3856 struct ibmvnic_login_rsp_buffer *login_rsp_buffer;
3857 struct ibmvnic_login_buffer *login_buffer;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003858 struct device *dev = &adapter->vdev->dev;
Dany Maddenc98d9cc2020-11-25 18:04:30 -06003859 struct vnic_login_client_data *vlcd;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003860 dma_addr_t rsp_buffer_token;
3861 dma_addr_t buffer_token;
3862 size_t rsp_buffer_size;
3863 union ibmvnic_crq crq;
Dany Maddenc98d9cc2020-11-25 18:04:30 -06003864 int client_data_len;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003865 size_t buffer_size;
3866 __be64 *tx_list_p;
3867 __be64 *rx_list_p;
Dany Maddenc98d9cc2020-11-25 18:04:30 -06003868 int rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003869 int i;
3870
Thomas Falcon20a8ab72018-02-26 18:10:59 -06003871 if (!adapter->tx_scrq || !adapter->rx_scrq) {
3872 netdev_err(adapter->netdev,
3873 "RX or TX queues are not allocated, device login failed\n");
3874 return -1;
3875 }
3876
Thomas Falcon34f0f4e2018-02-13 18:23:40 -06003877 release_login_rsp_buffer(adapter);
Nathan Fontenot37798d02017-11-08 11:23:56 -06003878 client_data_len = vnic_client_data_len(adapter);
3879
Thomas Falcon032c5e82015-12-21 11:26:06 -06003880 buffer_size =
3881 sizeof(struct ibmvnic_login_buffer) +
Nathan Fontenot37798d02017-11-08 11:23:56 -06003882 sizeof(u64) * (adapter->req_tx_queues + adapter->req_rx_queues) +
3883 client_data_len;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003884
Nathan Fontenot37798d02017-11-08 11:23:56 -06003885 login_buffer = kzalloc(buffer_size, GFP_ATOMIC);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003886 if (!login_buffer)
3887 goto buf_alloc_failed;
3888
3889 buffer_token = dma_map_single(dev, login_buffer, buffer_size,
3890 DMA_TO_DEVICE);
3891 if (dma_mapping_error(dev, buffer_token)) {
3892 dev_err(dev, "Couldn't map login buffer\n");
3893 goto buf_map_failed;
3894 }
3895
John Allen498cd8e2016-04-06 11:49:55 -05003896 rsp_buffer_size = sizeof(struct ibmvnic_login_rsp_buffer) +
3897 sizeof(u64) * adapter->req_tx_queues +
3898 sizeof(u64) * adapter->req_rx_queues +
3899 sizeof(u64) * adapter->req_rx_queues +
3900 sizeof(u8) * IBMVNIC_TX_DESC_VERSIONS;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003901
3902 login_rsp_buffer = kmalloc(rsp_buffer_size, GFP_ATOMIC);
3903 if (!login_rsp_buffer)
3904 goto buf_rsp_alloc_failed;
3905
3906 rsp_buffer_token = dma_map_single(dev, login_rsp_buffer,
3907 rsp_buffer_size, DMA_FROM_DEVICE);
3908 if (dma_mapping_error(dev, rsp_buffer_token)) {
3909 dev_err(dev, "Couldn't map login rsp buffer\n");
3910 goto buf_rsp_map_failed;
3911 }
Nathan Fontenot661a2622017-04-19 13:44:58 -04003912
Thomas Falcon032c5e82015-12-21 11:26:06 -06003913 adapter->login_buf = login_buffer;
3914 adapter->login_buf_token = buffer_token;
3915 adapter->login_buf_sz = buffer_size;
3916 adapter->login_rsp_buf = login_rsp_buffer;
3917 adapter->login_rsp_buf_token = rsp_buffer_token;
3918 adapter->login_rsp_buf_sz = rsp_buffer_size;
3919
3920 login_buffer->len = cpu_to_be32(buffer_size);
3921 login_buffer->version = cpu_to_be32(INITIAL_VERSION_LB);
3922 login_buffer->num_txcomp_subcrqs = cpu_to_be32(adapter->req_tx_queues);
3923 login_buffer->off_txcomp_subcrqs =
3924 cpu_to_be32(sizeof(struct ibmvnic_login_buffer));
3925 login_buffer->num_rxcomp_subcrqs = cpu_to_be32(adapter->req_rx_queues);
3926 login_buffer->off_rxcomp_subcrqs =
3927 cpu_to_be32(sizeof(struct ibmvnic_login_buffer) +
3928 sizeof(u64) * adapter->req_tx_queues);
3929 login_buffer->login_rsp_ioba = cpu_to_be32(rsp_buffer_token);
3930 login_buffer->login_rsp_len = cpu_to_be32(rsp_buffer_size);
3931
3932 tx_list_p = (__be64 *)((char *)login_buffer +
3933 sizeof(struct ibmvnic_login_buffer));
3934 rx_list_p = (__be64 *)((char *)login_buffer +
3935 sizeof(struct ibmvnic_login_buffer) +
3936 sizeof(u64) * adapter->req_tx_queues);
3937
3938 for (i = 0; i < adapter->req_tx_queues; i++) {
3939 if (adapter->tx_scrq[i]) {
3940 tx_list_p[i] = cpu_to_be64(adapter->tx_scrq[i]->
3941 crq_num);
3942 }
3943 }
3944
3945 for (i = 0; i < adapter->req_rx_queues; i++) {
3946 if (adapter->rx_scrq[i]) {
3947 rx_list_p[i] = cpu_to_be64(adapter->rx_scrq[i]->
3948 crq_num);
3949 }
3950 }
3951
Nathan Fontenot37798d02017-11-08 11:23:56 -06003952 /* Insert vNIC login client data */
3953 vlcd = (struct vnic_login_client_data *)
3954 ((char *)rx_list_p + (sizeof(u64) * adapter->req_rx_queues));
3955 login_buffer->client_data_offset =
3956 cpu_to_be32((char *)vlcd - (char *)login_buffer);
3957 login_buffer->client_data_len = cpu_to_be32(client_data_len);
3958
3959 vnic_add_client_data(adapter, vlcd);
3960
Thomas Falcon032c5e82015-12-21 11:26:06 -06003961 netdev_dbg(adapter->netdev, "Login Buffer:\n");
3962 for (i = 0; i < (adapter->login_buf_sz - 1) / 8 + 1; i++) {
3963 netdev_dbg(adapter->netdev, "%016lx\n",
3964 ((unsigned long int *)(adapter->login_buf))[i]);
3965 }
3966
3967 memset(&crq, 0, sizeof(crq));
3968 crq.login.first = IBMVNIC_CRQ_CMD;
3969 crq.login.cmd = LOGIN;
3970 crq.login.ioba = cpu_to_be32(buffer_token);
3971 crq.login.len = cpu_to_be32(buffer_size);
Sukadev Bhattiprolu76cdc5c2020-11-25 18:04:29 -06003972
3973 adapter->login_pending = true;
Dany Maddenc98d9cc2020-11-25 18:04:30 -06003974 rc = ibmvnic_send_crq(adapter, &crq);
3975 if (rc) {
3976 adapter->login_pending = false;
3977 netdev_err(adapter->netdev, "Failed to send login, rc=%d\n", rc);
3978 goto buf_rsp_map_failed;
3979 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06003980
Thomas Falcon20a8ab72018-02-26 18:10:59 -06003981 return 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003982
Thomas Falcon032c5e82015-12-21 11:26:06 -06003983buf_rsp_map_failed:
3984 kfree(login_rsp_buffer);
Dany Maddenc98d9cc2020-11-25 18:04:30 -06003985 adapter->login_rsp_buf = NULL;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003986buf_rsp_alloc_failed:
3987 dma_unmap_single(dev, buffer_token, buffer_size, DMA_TO_DEVICE);
3988buf_map_failed:
3989 kfree(login_buffer);
Dany Maddenc98d9cc2020-11-25 18:04:30 -06003990 adapter->login_buf = NULL;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003991buf_alloc_failed:
Thomas Falcon20a8ab72018-02-26 18:10:59 -06003992 return -1;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003993}
3994
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05003995static int send_request_map(struct ibmvnic_adapter *adapter, dma_addr_t addr,
3996 u32 len, u8 map_id)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003997{
3998 union ibmvnic_crq crq;
3999
4000 memset(&crq, 0, sizeof(crq));
4001 crq.request_map.first = IBMVNIC_CRQ_CMD;
4002 crq.request_map.cmd = REQUEST_MAP;
4003 crq.request_map.map_id = map_id;
4004 crq.request_map.ioba = cpu_to_be32(addr);
4005 crq.request_map.len = cpu_to_be32(len);
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05004006 return ibmvnic_send_crq(adapter, &crq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004007}
4008
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05004009static int send_request_unmap(struct ibmvnic_adapter *adapter, u8 map_id)
Thomas Falcon032c5e82015-12-21 11:26:06 -06004010{
4011 union ibmvnic_crq crq;
4012
4013 memset(&crq, 0, sizeof(crq));
4014 crq.request_unmap.first = IBMVNIC_CRQ_CMD;
4015 crq.request_unmap.cmd = REQUEST_UNMAP;
4016 crq.request_unmap.map_id = map_id;
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05004017 return ibmvnic_send_crq(adapter, &crq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004018}
4019
Lijun Pan69980d02020-09-27 20:13:28 -05004020static void send_query_map(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06004021{
4022 union ibmvnic_crq crq;
4023
4024 memset(&crq, 0, sizeof(crq));
4025 crq.query_map.first = IBMVNIC_CRQ_CMD;
4026 crq.query_map.cmd = QUERY_MAP;
4027 ibmvnic_send_crq(adapter, &crq);
4028}
4029
4030/* Send a series of CRQs requesting various capabilities of the VNIC server */
Lijun Pan491099a2020-09-27 20:13:26 -05004031static void send_query_cap(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06004032{
4033 union ibmvnic_crq crq;
4034
Thomas Falcon901e0402017-02-15 12:17:59 -06004035 atomic_set(&adapter->running_cap_crqs, 0);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004036 memset(&crq, 0, sizeof(crq));
4037 crq.query_capability.first = IBMVNIC_CRQ_CMD;
4038 crq.query_capability.cmd = QUERY_CAPABILITY;
4039
4040 crq.query_capability.capability = cpu_to_be16(MIN_TX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06004041 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004042 ibmvnic_send_crq(adapter, &crq);
4043
4044 crq.query_capability.capability = cpu_to_be16(MIN_RX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06004045 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004046 ibmvnic_send_crq(adapter, &crq);
4047
4048 crq.query_capability.capability = cpu_to_be16(MIN_RX_ADD_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06004049 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004050 ibmvnic_send_crq(adapter, &crq);
4051
4052 crq.query_capability.capability = cpu_to_be16(MAX_TX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06004053 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004054 ibmvnic_send_crq(adapter, &crq);
4055
4056 crq.query_capability.capability = cpu_to_be16(MAX_RX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06004057 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004058 ibmvnic_send_crq(adapter, &crq);
4059
4060 crq.query_capability.capability = cpu_to_be16(MAX_RX_ADD_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06004061 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004062 ibmvnic_send_crq(adapter, &crq);
4063
4064 crq.query_capability.capability =
4065 cpu_to_be16(MIN_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06004066 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004067 ibmvnic_send_crq(adapter, &crq);
4068
4069 crq.query_capability.capability =
4070 cpu_to_be16(MIN_RX_ADD_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06004071 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004072 ibmvnic_send_crq(adapter, &crq);
4073
4074 crq.query_capability.capability =
4075 cpu_to_be16(MAX_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06004076 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004077 ibmvnic_send_crq(adapter, &crq);
4078
4079 crq.query_capability.capability =
4080 cpu_to_be16(MAX_RX_ADD_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06004081 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004082 ibmvnic_send_crq(adapter, &crq);
4083
4084 crq.query_capability.capability = cpu_to_be16(TCP_IP_OFFLOAD);
Thomas Falcon901e0402017-02-15 12:17:59 -06004085 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004086 ibmvnic_send_crq(adapter, &crq);
4087
4088 crq.query_capability.capability = cpu_to_be16(PROMISC_SUPPORTED);
Thomas Falcon901e0402017-02-15 12:17:59 -06004089 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004090 ibmvnic_send_crq(adapter, &crq);
4091
4092 crq.query_capability.capability = cpu_to_be16(MIN_MTU);
Thomas Falcon901e0402017-02-15 12:17:59 -06004093 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004094 ibmvnic_send_crq(adapter, &crq);
4095
4096 crq.query_capability.capability = cpu_to_be16(MAX_MTU);
Thomas Falcon901e0402017-02-15 12:17:59 -06004097 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004098 ibmvnic_send_crq(adapter, &crq);
4099
4100 crq.query_capability.capability = cpu_to_be16(MAX_MULTICAST_FILTERS);
Thomas Falcon901e0402017-02-15 12:17:59 -06004101 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004102 ibmvnic_send_crq(adapter, &crq);
4103
4104 crq.query_capability.capability = cpu_to_be16(VLAN_HEADER_INSERTION);
Thomas Falcon901e0402017-02-15 12:17:59 -06004105 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004106 ibmvnic_send_crq(adapter, &crq);
4107
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04004108 crq.query_capability.capability = cpu_to_be16(RX_VLAN_HEADER_INSERTION);
4109 atomic_inc(&adapter->running_cap_crqs);
4110 ibmvnic_send_crq(adapter, &crq);
4111
Thomas Falcon032c5e82015-12-21 11:26:06 -06004112 crq.query_capability.capability = cpu_to_be16(MAX_TX_SG_ENTRIES);
Thomas Falcon901e0402017-02-15 12:17:59 -06004113 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004114 ibmvnic_send_crq(adapter, &crq);
4115
4116 crq.query_capability.capability = cpu_to_be16(RX_SG_SUPPORTED);
Thomas Falcon901e0402017-02-15 12:17:59 -06004117 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004118 ibmvnic_send_crq(adapter, &crq);
4119
4120 crq.query_capability.capability = cpu_to_be16(OPT_TX_COMP_SUB_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06004121 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004122 ibmvnic_send_crq(adapter, &crq);
4123
4124 crq.query_capability.capability = cpu_to_be16(OPT_RX_COMP_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06004125 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004126 ibmvnic_send_crq(adapter, &crq);
4127
4128 crq.query_capability.capability =
4129 cpu_to_be16(OPT_RX_BUFADD_Q_PER_RX_COMP_Q);
Thomas Falcon901e0402017-02-15 12:17:59 -06004130 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004131 ibmvnic_send_crq(adapter, &crq);
4132
4133 crq.query_capability.capability =
4134 cpu_to_be16(OPT_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06004135 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004136 ibmvnic_send_crq(adapter, &crq);
4137
4138 crq.query_capability.capability =
4139 cpu_to_be16(OPT_RXBA_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06004140 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004141 ibmvnic_send_crq(adapter, &crq);
4142
4143 crq.query_capability.capability = cpu_to_be16(TX_RX_DESC_REQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06004144 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004145 ibmvnic_send_crq(adapter, &crq);
4146}
4147
Lijun Pan16e811f2020-09-27 20:13:29 -05004148static void send_query_ip_offload(struct ibmvnic_adapter *adapter)
4149{
4150 int buf_sz = sizeof(struct ibmvnic_query_ip_offload_buffer);
4151 struct device *dev = &adapter->vdev->dev;
4152 union ibmvnic_crq crq;
4153
4154 adapter->ip_offload_tok =
4155 dma_map_single(dev,
4156 &adapter->ip_offload_buf,
4157 buf_sz,
4158 DMA_FROM_DEVICE);
4159
4160 if (dma_mapping_error(dev, adapter->ip_offload_tok)) {
4161 if (!firmware_has_feature(FW_FEATURE_CMO))
4162 dev_err(dev, "Couldn't map offload buffer\n");
4163 return;
4164 }
4165
4166 memset(&crq, 0, sizeof(crq));
4167 crq.query_ip_offload.first = IBMVNIC_CRQ_CMD;
4168 crq.query_ip_offload.cmd = QUERY_IP_OFFLOAD;
4169 crq.query_ip_offload.len = cpu_to_be32(buf_sz);
4170 crq.query_ip_offload.ioba =
4171 cpu_to_be32(adapter->ip_offload_tok);
4172
4173 ibmvnic_send_crq(adapter, &crq);
4174}
4175
Lijun Pan46899bd2020-09-27 20:13:30 -05004176static void send_control_ip_offload(struct ibmvnic_adapter *adapter)
4177{
4178 struct ibmvnic_control_ip_offload_buffer *ctrl_buf = &adapter->ip_offload_ctrl;
4179 struct ibmvnic_query_ip_offload_buffer *buf = &adapter->ip_offload_buf;
4180 struct device *dev = &adapter->vdev->dev;
4181 netdev_features_t old_hw_features = 0;
4182 union ibmvnic_crq crq;
4183
4184 adapter->ip_offload_ctrl_tok =
4185 dma_map_single(dev,
4186 ctrl_buf,
4187 sizeof(adapter->ip_offload_ctrl),
4188 DMA_TO_DEVICE);
4189
4190 if (dma_mapping_error(dev, adapter->ip_offload_ctrl_tok)) {
4191 dev_err(dev, "Couldn't map ip offload control buffer\n");
4192 return;
4193 }
4194
4195 ctrl_buf->len = cpu_to_be32(sizeof(adapter->ip_offload_ctrl));
4196 ctrl_buf->version = cpu_to_be32(INITIAL_VERSION_IOB);
4197 ctrl_buf->ipv4_chksum = buf->ipv4_chksum;
4198 ctrl_buf->ipv6_chksum = buf->ipv6_chksum;
4199 ctrl_buf->tcp_ipv4_chksum = buf->tcp_ipv4_chksum;
4200 ctrl_buf->udp_ipv4_chksum = buf->udp_ipv4_chksum;
4201 ctrl_buf->tcp_ipv6_chksum = buf->tcp_ipv6_chksum;
4202 ctrl_buf->udp_ipv6_chksum = buf->udp_ipv6_chksum;
4203 ctrl_buf->large_tx_ipv4 = buf->large_tx_ipv4;
4204 ctrl_buf->large_tx_ipv6 = buf->large_tx_ipv6;
4205
4206 /* large_rx disabled for now, additional features needed */
4207 ctrl_buf->large_rx_ipv4 = 0;
4208 ctrl_buf->large_rx_ipv6 = 0;
4209
4210 if (adapter->state != VNIC_PROBING) {
4211 old_hw_features = adapter->netdev->hw_features;
4212 adapter->netdev->hw_features = 0;
4213 }
4214
4215 adapter->netdev->hw_features = NETIF_F_SG | NETIF_F_GSO | NETIF_F_GRO;
4216
4217 if (buf->tcp_ipv4_chksum || buf->udp_ipv4_chksum)
4218 adapter->netdev->hw_features |= NETIF_F_IP_CSUM;
4219
4220 if (buf->tcp_ipv6_chksum || buf->udp_ipv6_chksum)
4221 adapter->netdev->hw_features |= NETIF_F_IPV6_CSUM;
4222
4223 if ((adapter->netdev->features &
4224 (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)))
4225 adapter->netdev->hw_features |= NETIF_F_RXCSUM;
4226
4227 if (buf->large_tx_ipv4)
4228 adapter->netdev->hw_features |= NETIF_F_TSO;
4229 if (buf->large_tx_ipv6)
4230 adapter->netdev->hw_features |= NETIF_F_TSO6;
4231
4232 if (adapter->state == VNIC_PROBING) {
4233 adapter->netdev->features |= adapter->netdev->hw_features;
4234 } else if (old_hw_features != adapter->netdev->hw_features) {
4235 netdev_features_t tmp = 0;
4236
4237 /* disable features no longer supported */
4238 adapter->netdev->features &= adapter->netdev->hw_features;
4239 /* turn on features now supported if previously enabled */
4240 tmp = (old_hw_features ^ adapter->netdev->hw_features) &
4241 adapter->netdev->hw_features;
4242 adapter->netdev->features |=
4243 tmp & adapter->netdev->wanted_features;
4244 }
4245
4246 memset(&crq, 0, sizeof(crq));
4247 crq.control_ip_offload.first = IBMVNIC_CRQ_CMD;
4248 crq.control_ip_offload.cmd = CONTROL_IP_OFFLOAD;
4249 crq.control_ip_offload.len =
4250 cpu_to_be32(sizeof(adapter->ip_offload_ctrl));
4251 crq.control_ip_offload.ioba = cpu_to_be32(adapter->ip_offload_ctrl_tok);
4252 ibmvnic_send_crq(adapter, &crq);
4253}
4254
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02004255static void handle_vpd_size_rsp(union ibmvnic_crq *crq,
4256 struct ibmvnic_adapter *adapter)
4257{
4258 struct device *dev = &adapter->vdev->dev;
4259
4260 if (crq->get_vpd_size_rsp.rc.code) {
4261 dev_err(dev, "Error retrieving VPD size, rc=%x\n",
4262 crq->get_vpd_size_rsp.rc.code);
4263 complete(&adapter->fw_done);
4264 return;
4265 }
4266
4267 adapter->vpd->len = be64_to_cpu(crq->get_vpd_size_rsp.len);
4268 complete(&adapter->fw_done);
4269}
4270
4271static void handle_vpd_rsp(union ibmvnic_crq *crq,
4272 struct ibmvnic_adapter *adapter)
4273{
4274 struct device *dev = &adapter->vdev->dev;
Desnes Augusto Nunes do Rosario21a25452018-02-05 14:33:55 -02004275 unsigned char *substr = NULL;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02004276 u8 fw_level_len = 0;
4277
4278 memset(adapter->fw_version, 0, 32);
4279
4280 dma_unmap_single(dev, adapter->vpd->dma_addr, adapter->vpd->len,
4281 DMA_FROM_DEVICE);
4282
4283 if (crq->get_vpd_rsp.rc.code) {
4284 dev_err(dev, "Error retrieving VPD from device, rc=%x\n",
4285 crq->get_vpd_rsp.rc.code);
4286 goto complete;
4287 }
4288
4289 /* get the position of the firmware version info
4290 * located after the ASCII 'RM' substring in the buffer
4291 */
4292 substr = strnstr(adapter->vpd->buff, "RM", adapter->vpd->len);
4293 if (!substr) {
Desnes Augusto Nunes do Rosarioa1073112018-02-01 16:04:30 -02004294 dev_info(dev, "Warning - No FW level has been provided in the VPD buffer by the VIOS Server\n");
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02004295 goto complete;
4296 }
4297
4298 /* get length of firmware level ASCII substring */
4299 if ((substr + 2) < (adapter->vpd->buff + adapter->vpd->len)) {
4300 fw_level_len = *(substr + 2);
4301 } else {
4302 dev_info(dev, "Length of FW substr extrapolated VDP buff\n");
4303 goto complete;
4304 }
4305
4306 /* copy firmware version string from vpd into adapter */
4307 if ((substr + 3 + fw_level_len) <
4308 (adapter->vpd->buff + adapter->vpd->len)) {
Desnes Augusto Nunes do Rosario21a25452018-02-05 14:33:55 -02004309 strncpy((char *)adapter->fw_version, substr + 3, fw_level_len);
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02004310 } else {
4311 dev_info(dev, "FW substr extrapolated VPD buff\n");
4312 }
4313
4314complete:
Desnes Augusto Nunes do Rosario21a25452018-02-05 14:33:55 -02004315 if (adapter->fw_version[0] == '\0')
4316 strncpy((char *)adapter->fw_version, "N/A", 3 * sizeof(char));
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02004317 complete(&adapter->fw_done);
4318}
4319
Thomas Falcon032c5e82015-12-21 11:26:06 -06004320static void handle_query_ip_offload_rsp(struct ibmvnic_adapter *adapter)
4321{
4322 struct device *dev = &adapter->vdev->dev;
4323 struct ibmvnic_query_ip_offload_buffer *buf = &adapter->ip_offload_buf;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004324 int i;
4325
4326 dma_unmap_single(dev, adapter->ip_offload_tok,
4327 sizeof(adapter->ip_offload_buf), DMA_FROM_DEVICE);
4328
4329 netdev_dbg(adapter->netdev, "Query IP Offload Buffer:\n");
4330 for (i = 0; i < (sizeof(adapter->ip_offload_buf) - 1) / 8 + 1; i++)
4331 netdev_dbg(adapter->netdev, "%016lx\n",
4332 ((unsigned long int *)(buf))[i]);
4333
4334 netdev_dbg(adapter->netdev, "ipv4_chksum = %d\n", buf->ipv4_chksum);
4335 netdev_dbg(adapter->netdev, "ipv6_chksum = %d\n", buf->ipv6_chksum);
4336 netdev_dbg(adapter->netdev, "tcp_ipv4_chksum = %d\n",
4337 buf->tcp_ipv4_chksum);
4338 netdev_dbg(adapter->netdev, "tcp_ipv6_chksum = %d\n",
4339 buf->tcp_ipv6_chksum);
4340 netdev_dbg(adapter->netdev, "udp_ipv4_chksum = %d\n",
4341 buf->udp_ipv4_chksum);
4342 netdev_dbg(adapter->netdev, "udp_ipv6_chksum = %d\n",
4343 buf->udp_ipv6_chksum);
4344 netdev_dbg(adapter->netdev, "large_tx_ipv4 = %d\n",
4345 buf->large_tx_ipv4);
4346 netdev_dbg(adapter->netdev, "large_tx_ipv6 = %d\n",
4347 buf->large_tx_ipv6);
4348 netdev_dbg(adapter->netdev, "large_rx_ipv4 = %d\n",
4349 buf->large_rx_ipv4);
4350 netdev_dbg(adapter->netdev, "large_rx_ipv6 = %d\n",
4351 buf->large_rx_ipv6);
4352 netdev_dbg(adapter->netdev, "max_ipv4_hdr_sz = %d\n",
4353 buf->max_ipv4_header_size);
4354 netdev_dbg(adapter->netdev, "max_ipv6_hdr_sz = %d\n",
4355 buf->max_ipv6_header_size);
4356 netdev_dbg(adapter->netdev, "max_tcp_hdr_size = %d\n",
4357 buf->max_tcp_header_size);
4358 netdev_dbg(adapter->netdev, "max_udp_hdr_size = %d\n",
4359 buf->max_udp_header_size);
4360 netdev_dbg(adapter->netdev, "max_large_tx_size = %d\n",
4361 buf->max_large_tx_size);
4362 netdev_dbg(adapter->netdev, "max_large_rx_size = %d\n",
4363 buf->max_large_rx_size);
4364 netdev_dbg(adapter->netdev, "ipv6_ext_hdr = %d\n",
4365 buf->ipv6_extension_header);
4366 netdev_dbg(adapter->netdev, "tcp_pseudosum_req = %d\n",
4367 buf->tcp_pseudosum_req);
4368 netdev_dbg(adapter->netdev, "num_ipv6_ext_hd = %d\n",
4369 buf->num_ipv6_ext_headers);
4370 netdev_dbg(adapter->netdev, "off_ipv6_ext_hd = %d\n",
4371 buf->off_ipv6_ext_headers);
4372
Lijun Pan46899bd2020-09-27 20:13:30 -05004373 send_control_ip_offload(adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004374}
4375
Thomas Falconc9008d32018-08-06 21:39:59 -05004376static const char *ibmvnic_fw_err_cause(u16 cause)
4377{
4378 switch (cause) {
4379 case ADAPTER_PROBLEM:
4380 return "adapter problem";
4381 case BUS_PROBLEM:
4382 return "bus problem";
4383 case FW_PROBLEM:
4384 return "firmware problem";
4385 case DD_PROBLEM:
4386 return "device driver problem";
4387 case EEH_RECOVERY:
4388 return "EEH recovery";
4389 case FW_UPDATED:
4390 return "firmware updated";
4391 case LOW_MEMORY:
4392 return "low Memory";
4393 default:
4394 return "unknown";
4395 }
4396}
4397
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04004398static void handle_error_indication(union ibmvnic_crq *crq,
4399 struct ibmvnic_adapter *adapter)
4400{
4401 struct device *dev = &adapter->vdev->dev;
Thomas Falconc9008d32018-08-06 21:39:59 -05004402 u16 cause;
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04004403
Thomas Falconc9008d32018-08-06 21:39:59 -05004404 cause = be16_to_cpu(crq->error_indication.error_cause);
4405
4406 dev_warn_ratelimited(dev,
4407 "Firmware reports %serror, cause: %s. Starting recovery...\n",
4408 crq->error_indication.flags
4409 & IBMVNIC_FATAL_ERROR ? "FATAL " : "",
4410 ibmvnic_fw_err_cause(cause));
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04004411
Nathan Fontenoted651a12017-05-03 14:04:38 -04004412 if (crq->error_indication.flags & IBMVNIC_FATAL_ERROR)
4413 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
John Allen8cb31cf2017-05-26 10:30:37 -04004414 else
4415 ibmvnic_reset(adapter, VNIC_RESET_NON_FATAL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004416}
4417
Thomas Falconf8136142018-01-29 13:45:05 -06004418static int handle_change_mac_rsp(union ibmvnic_crq *crq,
4419 struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06004420{
4421 struct net_device *netdev = adapter->netdev;
4422 struct device *dev = &adapter->vdev->dev;
4423 long rc;
4424
4425 rc = crq->change_mac_addr_rsp.rc.code;
4426 if (rc) {
4427 dev_err(dev, "Error %ld in CHANGE_MAC_ADDR_RSP\n", rc);
Thomas Falconf8136142018-01-29 13:45:05 -06004428 goto out;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004429 }
Lijun Pand9b0e592020-10-20 17:39:19 -05004430 /* crq->change_mac_addr.mac_addr is the requested one
4431 * crq->change_mac_addr_rsp.mac_addr is the returned valid one.
4432 */
Thomas Falcon62740e92019-05-09 23:13:43 -05004433 ether_addr_copy(netdev->dev_addr,
4434 &crq->change_mac_addr_rsp.mac_addr[0]);
Lijun Pand9b0e592020-10-20 17:39:19 -05004435 ether_addr_copy(adapter->mac_addr,
4436 &crq->change_mac_addr_rsp.mac_addr[0]);
Thomas Falconf8136142018-01-29 13:45:05 -06004437out:
4438 complete(&adapter->fw_done);
4439 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004440}
4441
4442static void handle_request_cap_rsp(union ibmvnic_crq *crq,
4443 struct ibmvnic_adapter *adapter)
4444{
4445 struct device *dev = &adapter->vdev->dev;
4446 u64 *req_value;
4447 char *name;
4448
Thomas Falcon901e0402017-02-15 12:17:59 -06004449 atomic_dec(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004450 switch (be16_to_cpu(crq->request_capability_rsp.capability)) {
4451 case REQ_TX_QUEUES:
4452 req_value = &adapter->req_tx_queues;
4453 name = "tx";
4454 break;
4455 case REQ_RX_QUEUES:
4456 req_value = &adapter->req_rx_queues;
4457 name = "rx";
4458 break;
4459 case REQ_RX_ADD_QUEUES:
4460 req_value = &adapter->req_rx_add_queues;
4461 name = "rx_add";
4462 break;
4463 case REQ_TX_ENTRIES_PER_SUBCRQ:
4464 req_value = &adapter->req_tx_entries_per_subcrq;
4465 name = "tx_entries_per_subcrq";
4466 break;
4467 case REQ_RX_ADD_ENTRIES_PER_SUBCRQ:
4468 req_value = &adapter->req_rx_add_entries_per_subcrq;
4469 name = "rx_add_entries_per_subcrq";
4470 break;
4471 case REQ_MTU:
4472 req_value = &adapter->req_mtu;
4473 name = "mtu";
4474 break;
4475 case PROMISC_REQUESTED:
4476 req_value = &adapter->promisc;
4477 name = "promisc";
4478 break;
4479 default:
4480 dev_err(dev, "Got invalid cap request rsp %d\n",
4481 crq->request_capability.capability);
4482 return;
4483 }
4484
4485 switch (crq->request_capability_rsp.rc.code) {
4486 case SUCCESS:
4487 break;
4488 case PARTIALSUCCESS:
4489 dev_info(dev, "req=%lld, rsp=%ld in %s queue, retrying.\n",
4490 *req_value,
Thomas Falcon28f4d162017-02-15 10:32:11 -06004491 (long int)be64_to_cpu(crq->request_capability_rsp.
Thomas Falcon032c5e82015-12-21 11:26:06 -06004492 number), name);
John Allene7913802018-01-18 16:27:12 -06004493
4494 if (be16_to_cpu(crq->request_capability_rsp.capability) ==
4495 REQ_MTU) {
4496 pr_err("mtu of %llu is not supported. Reverting.\n",
4497 *req_value);
4498 *req_value = adapter->fallback.mtu;
4499 } else {
4500 *req_value =
4501 be64_to_cpu(crq->request_capability_rsp.number);
4502 }
4503
Lijun Pan09081b92020-09-27 20:13:27 -05004504 send_request_cap(adapter, 1);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004505 return;
4506 default:
4507 dev_err(dev, "Error %d in request cap rsp\n",
4508 crq->request_capability_rsp.rc.code);
4509 return;
4510 }
4511
4512 /* Done receiving requested capabilities, query IP offload support */
Thomas Falcon901e0402017-02-15 12:17:59 -06004513 if (atomic_read(&adapter->running_cap_crqs) == 0) {
Thomas Falcon249168a2017-02-15 12:18:00 -06004514 adapter->wait_capability = false;
Lijun Pan16e811f2020-09-27 20:13:29 -05004515 send_query_ip_offload(adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004516 }
4517}
4518
4519static int handle_login_rsp(union ibmvnic_crq *login_rsp_crq,
4520 struct ibmvnic_adapter *adapter)
4521{
4522 struct device *dev = &adapter->vdev->dev;
John Allenc26eba02017-10-26 16:23:25 -05004523 struct net_device *netdev = adapter->netdev;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004524 struct ibmvnic_login_rsp_buffer *login_rsp = adapter->login_rsp_buf;
4525 struct ibmvnic_login_buffer *login = adapter->login_buf;
Cristobal Fornof3ae59c2020-08-19 13:16:23 -05004526 u64 *tx_handle_array;
4527 u64 *rx_handle_array;
4528 int num_tx_pools;
4529 int num_rx_pools;
Thomas Falcon507ebe62020-08-21 13:39:01 -05004530 u64 *size_array;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004531 int i;
4532
Sukadev Bhattiprolu76cdc5c2020-11-25 18:04:29 -06004533 /* CHECK: Test/set of login_pending does not need to be atomic
4534 * because only ibmvnic_tasklet tests/clears this.
4535 */
4536 if (!adapter->login_pending) {
4537 netdev_warn(netdev, "Ignoring unexpected login response\n");
4538 return 0;
4539 }
4540 adapter->login_pending = false;
4541
Thomas Falcon032c5e82015-12-21 11:26:06 -06004542 dma_unmap_single(dev, adapter->login_buf_token, adapter->login_buf_sz,
Thomas Falcon37e40fa2018-04-06 18:37:02 -05004543 DMA_TO_DEVICE);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004544 dma_unmap_single(dev, adapter->login_rsp_buf_token,
Thomas Falcon37e40fa2018-04-06 18:37:02 -05004545 adapter->login_rsp_buf_sz, DMA_FROM_DEVICE);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004546
John Allen498cd8e2016-04-06 11:49:55 -05004547 /* If the number of queues requested can't be allocated by the
4548 * server, the login response will return with code 1. We will need
4549 * to resend the login buffer with fewer queues requested.
4550 */
4551 if (login_rsp_crq->generic.rc.code) {
Nathan Fontenot64d92aa2018-04-11 10:09:32 -05004552 adapter->init_done_rc = login_rsp_crq->generic.rc.code;
John Allen498cd8e2016-04-06 11:49:55 -05004553 complete(&adapter->init_done);
4554 return 0;
4555 }
4556
John Allenc26eba02017-10-26 16:23:25 -05004557 netdev->mtu = adapter->req_mtu - ETH_HLEN;
4558
Thomas Falcon032c5e82015-12-21 11:26:06 -06004559 netdev_dbg(adapter->netdev, "Login Response Buffer:\n");
4560 for (i = 0; i < (adapter->login_rsp_buf_sz - 1) / 8 + 1; i++) {
4561 netdev_dbg(adapter->netdev, "%016lx\n",
4562 ((unsigned long int *)(adapter->login_rsp_buf))[i]);
4563 }
4564
4565 /* Sanity checks */
4566 if (login->num_txcomp_subcrqs != login_rsp->num_txsubm_subcrqs ||
4567 (be32_to_cpu(login->num_rxcomp_subcrqs) *
4568 adapter->req_rx_add_queues !=
4569 be32_to_cpu(login_rsp->num_rxadd_subcrqs))) {
4570 dev_err(dev, "FATAL: Inconsistent login and login rsp\n");
Dany Madden31d6b402020-11-25 18:04:24 -06004571 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004572 return -EIO;
4573 }
Thomas Falcon507ebe62020-08-21 13:39:01 -05004574 size_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
4575 be32_to_cpu(adapter->login_rsp_buf->off_rxadd_buff_size));
4576 /* variable buffer sizes are not supported, so just read the
4577 * first entry.
4578 */
4579 adapter->cur_rx_buf_sz = be64_to_cpu(size_array[0]);
Cristobal Fornof3ae59c2020-08-19 13:16:23 -05004580
4581 num_tx_pools = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
4582 num_rx_pools = be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
4583
4584 tx_handle_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
4585 be32_to_cpu(adapter->login_rsp_buf->off_txsubm_subcrqs));
4586 rx_handle_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
4587 be32_to_cpu(adapter->login_rsp_buf->off_rxadd_subcrqs));
4588
4589 for (i = 0; i < num_tx_pools; i++)
4590 adapter->tx_scrq[i]->handle = tx_handle_array[i];
4591
4592 for (i = 0; i < num_rx_pools; i++)
4593 adapter->rx_scrq[i]->handle = rx_handle_array[i];
4594
Thomas Falcon507ebe62020-08-21 13:39:01 -05004595 adapter->num_active_tx_scrqs = num_tx_pools;
4596 adapter->num_active_rx_scrqs = num_rx_pools;
Cristobal Fornof3ae59c2020-08-19 13:16:23 -05004597 release_login_rsp_buffer(adapter);
Thomas Falcona2c0f032018-02-21 18:18:30 -06004598 release_login_buffer(adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004599 complete(&adapter->init_done);
4600
Thomas Falcon032c5e82015-12-21 11:26:06 -06004601 return 0;
4602}
4603
Thomas Falcon032c5e82015-12-21 11:26:06 -06004604static void handle_request_unmap_rsp(union ibmvnic_crq *crq,
4605 struct ibmvnic_adapter *adapter)
4606{
4607 struct device *dev = &adapter->vdev->dev;
4608 long rc;
4609
4610 rc = crq->request_unmap_rsp.rc.code;
4611 if (rc)
4612 dev_err(dev, "Error %ld in REQUEST_UNMAP_RSP\n", rc);
4613}
4614
4615static void handle_query_map_rsp(union ibmvnic_crq *crq,
4616 struct ibmvnic_adapter *adapter)
4617{
4618 struct net_device *netdev = adapter->netdev;
4619 struct device *dev = &adapter->vdev->dev;
4620 long rc;
4621
4622 rc = crq->query_map_rsp.rc.code;
4623 if (rc) {
4624 dev_err(dev, "Error %ld in QUERY_MAP_RSP\n", rc);
4625 return;
4626 }
4627 netdev_dbg(netdev, "page_size = %d\ntot_pages = %d\nfree_pages = %d\n",
4628 crq->query_map_rsp.page_size, crq->query_map_rsp.tot_pages,
4629 crq->query_map_rsp.free_pages);
4630}
4631
4632static void handle_query_cap_rsp(union ibmvnic_crq *crq,
4633 struct ibmvnic_adapter *adapter)
4634{
4635 struct net_device *netdev = adapter->netdev;
4636 struct device *dev = &adapter->vdev->dev;
4637 long rc;
4638
Thomas Falcon901e0402017-02-15 12:17:59 -06004639 atomic_dec(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004640 netdev_dbg(netdev, "Outstanding queries: %d\n",
Thomas Falcon901e0402017-02-15 12:17:59 -06004641 atomic_read(&adapter->running_cap_crqs));
Thomas Falcon032c5e82015-12-21 11:26:06 -06004642 rc = crq->query_capability.rc.code;
4643 if (rc) {
4644 dev_err(dev, "Error %ld in QUERY_CAP_RSP\n", rc);
4645 goto out;
4646 }
4647
4648 switch (be16_to_cpu(crq->query_capability.capability)) {
4649 case MIN_TX_QUEUES:
4650 adapter->min_tx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004651 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004652 netdev_dbg(netdev, "min_tx_queues = %lld\n",
4653 adapter->min_tx_queues);
4654 break;
4655 case MIN_RX_QUEUES:
4656 adapter->min_rx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004657 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004658 netdev_dbg(netdev, "min_rx_queues = %lld\n",
4659 adapter->min_rx_queues);
4660 break;
4661 case MIN_RX_ADD_QUEUES:
4662 adapter->min_rx_add_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004663 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004664 netdev_dbg(netdev, "min_rx_add_queues = %lld\n",
4665 adapter->min_rx_add_queues);
4666 break;
4667 case MAX_TX_QUEUES:
4668 adapter->max_tx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004669 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004670 netdev_dbg(netdev, "max_tx_queues = %lld\n",
4671 adapter->max_tx_queues);
4672 break;
4673 case MAX_RX_QUEUES:
4674 adapter->max_rx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004675 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004676 netdev_dbg(netdev, "max_rx_queues = %lld\n",
4677 adapter->max_rx_queues);
4678 break;
4679 case MAX_RX_ADD_QUEUES:
4680 adapter->max_rx_add_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004681 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004682 netdev_dbg(netdev, "max_rx_add_queues = %lld\n",
4683 adapter->max_rx_add_queues);
4684 break;
4685 case MIN_TX_ENTRIES_PER_SUBCRQ:
4686 adapter->min_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06004687 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004688 netdev_dbg(netdev, "min_tx_entries_per_subcrq = %lld\n",
4689 adapter->min_tx_entries_per_subcrq);
4690 break;
4691 case MIN_RX_ADD_ENTRIES_PER_SUBCRQ:
4692 adapter->min_rx_add_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06004693 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004694 netdev_dbg(netdev, "min_rx_add_entrs_per_subcrq = %lld\n",
4695 adapter->min_rx_add_entries_per_subcrq);
4696 break;
4697 case MAX_TX_ENTRIES_PER_SUBCRQ:
4698 adapter->max_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06004699 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004700 netdev_dbg(netdev, "max_tx_entries_per_subcrq = %lld\n",
4701 adapter->max_tx_entries_per_subcrq);
4702 break;
4703 case MAX_RX_ADD_ENTRIES_PER_SUBCRQ:
4704 adapter->max_rx_add_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06004705 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004706 netdev_dbg(netdev, "max_rx_add_entrs_per_subcrq = %lld\n",
4707 adapter->max_rx_add_entries_per_subcrq);
4708 break;
4709 case TCP_IP_OFFLOAD:
4710 adapter->tcp_ip_offload =
Thomas Falconde89e852016-03-01 10:20:09 -06004711 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004712 netdev_dbg(netdev, "tcp_ip_offload = %lld\n",
4713 adapter->tcp_ip_offload);
4714 break;
4715 case PROMISC_SUPPORTED:
4716 adapter->promisc_supported =
Thomas Falconde89e852016-03-01 10:20:09 -06004717 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004718 netdev_dbg(netdev, "promisc_supported = %lld\n",
4719 adapter->promisc_supported);
4720 break;
4721 case MIN_MTU:
Thomas Falconde89e852016-03-01 10:20:09 -06004722 adapter->min_mtu = be64_to_cpu(crq->query_capability.number);
Thomas Falconf39f0d12017-02-14 10:22:59 -06004723 netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004724 netdev_dbg(netdev, "min_mtu = %lld\n", adapter->min_mtu);
4725 break;
4726 case MAX_MTU:
Thomas Falconde89e852016-03-01 10:20:09 -06004727 adapter->max_mtu = be64_to_cpu(crq->query_capability.number);
Thomas Falconf39f0d12017-02-14 10:22:59 -06004728 netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004729 netdev_dbg(netdev, "max_mtu = %lld\n", adapter->max_mtu);
4730 break;
4731 case MAX_MULTICAST_FILTERS:
4732 adapter->max_multicast_filters =
Thomas Falconde89e852016-03-01 10:20:09 -06004733 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004734 netdev_dbg(netdev, "max_multicast_filters = %lld\n",
4735 adapter->max_multicast_filters);
4736 break;
4737 case VLAN_HEADER_INSERTION:
4738 adapter->vlan_header_insertion =
Thomas Falconde89e852016-03-01 10:20:09 -06004739 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004740 if (adapter->vlan_header_insertion)
4741 netdev->features |= NETIF_F_HW_VLAN_STAG_TX;
4742 netdev_dbg(netdev, "vlan_header_insertion = %lld\n",
4743 adapter->vlan_header_insertion);
4744 break;
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04004745 case RX_VLAN_HEADER_INSERTION:
4746 adapter->rx_vlan_header_insertion =
4747 be64_to_cpu(crq->query_capability.number);
4748 netdev_dbg(netdev, "rx_vlan_header_insertion = %lld\n",
4749 adapter->rx_vlan_header_insertion);
4750 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004751 case MAX_TX_SG_ENTRIES:
4752 adapter->max_tx_sg_entries =
Thomas Falconde89e852016-03-01 10:20:09 -06004753 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004754 netdev_dbg(netdev, "max_tx_sg_entries = %lld\n",
4755 adapter->max_tx_sg_entries);
4756 break;
4757 case RX_SG_SUPPORTED:
4758 adapter->rx_sg_supported =
Thomas Falconde89e852016-03-01 10:20:09 -06004759 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004760 netdev_dbg(netdev, "rx_sg_supported = %lld\n",
4761 adapter->rx_sg_supported);
4762 break;
4763 case OPT_TX_COMP_SUB_QUEUES:
4764 adapter->opt_tx_comp_sub_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004765 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004766 netdev_dbg(netdev, "opt_tx_comp_sub_queues = %lld\n",
4767 adapter->opt_tx_comp_sub_queues);
4768 break;
4769 case OPT_RX_COMP_QUEUES:
4770 adapter->opt_rx_comp_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004771 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004772 netdev_dbg(netdev, "opt_rx_comp_queues = %lld\n",
4773 adapter->opt_rx_comp_queues);
4774 break;
4775 case OPT_RX_BUFADD_Q_PER_RX_COMP_Q:
4776 adapter->opt_rx_bufadd_q_per_rx_comp_q =
Thomas Falconde89e852016-03-01 10:20:09 -06004777 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004778 netdev_dbg(netdev, "opt_rx_bufadd_q_per_rx_comp_q = %lld\n",
4779 adapter->opt_rx_bufadd_q_per_rx_comp_q);
4780 break;
4781 case OPT_TX_ENTRIES_PER_SUBCRQ:
4782 adapter->opt_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06004783 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004784 netdev_dbg(netdev, "opt_tx_entries_per_subcrq = %lld\n",
4785 adapter->opt_tx_entries_per_subcrq);
4786 break;
4787 case OPT_RXBA_ENTRIES_PER_SUBCRQ:
4788 adapter->opt_rxba_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06004789 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004790 netdev_dbg(netdev, "opt_rxba_entries_per_subcrq = %lld\n",
4791 adapter->opt_rxba_entries_per_subcrq);
4792 break;
4793 case TX_RX_DESC_REQ:
4794 adapter->tx_rx_desc_req = crq->query_capability.number;
4795 netdev_dbg(netdev, "tx_rx_desc_req = %llx\n",
4796 adapter->tx_rx_desc_req);
4797 break;
4798
4799 default:
4800 netdev_err(netdev, "Got invalid cap rsp %d\n",
4801 crq->query_capability.capability);
4802 }
4803
4804out:
Thomas Falcon249168a2017-02-15 12:18:00 -06004805 if (atomic_read(&adapter->running_cap_crqs) == 0) {
4806 adapter->wait_capability = false;
Lijun Pan09081b92020-09-27 20:13:27 -05004807 send_request_cap(adapter, 0);
Thomas Falcon249168a2017-02-15 12:18:00 -06004808 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06004809}
4810
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03004811static int send_query_phys_parms(struct ibmvnic_adapter *adapter)
4812{
4813 union ibmvnic_crq crq;
4814 int rc;
4815
4816 memset(&crq, 0, sizeof(crq));
4817 crq.query_phys_parms.first = IBMVNIC_CRQ_CMD;
4818 crq.query_phys_parms.cmd = QUERY_PHYS_PARMS;
Thomas Falconff25dcb2019-11-25 17:12:56 -06004819
4820 mutex_lock(&adapter->fw_lock);
4821 adapter->fw_done_rc = 0;
Thomas Falcon070eca92019-11-25 17:12:53 -06004822 reinit_completion(&adapter->fw_done);
Thomas Falconff25dcb2019-11-25 17:12:56 -06004823
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03004824 rc = ibmvnic_send_crq(adapter, &crq);
Thomas Falconff25dcb2019-11-25 17:12:56 -06004825 if (rc) {
4826 mutex_unlock(&adapter->fw_lock);
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03004827 return rc;
Thomas Falconff25dcb2019-11-25 17:12:56 -06004828 }
Thomas Falcon476d96c2019-11-25 17:12:55 -06004829
4830 rc = ibmvnic_wait_for_completion(adapter, &adapter->fw_done, 10000);
Thomas Falconff25dcb2019-11-25 17:12:56 -06004831 if (rc) {
4832 mutex_unlock(&adapter->fw_lock);
Thomas Falcon476d96c2019-11-25 17:12:55 -06004833 return rc;
Thomas Falconff25dcb2019-11-25 17:12:56 -06004834 }
Thomas Falcon476d96c2019-11-25 17:12:55 -06004835
Thomas Falconff25dcb2019-11-25 17:12:56 -06004836 mutex_unlock(&adapter->fw_lock);
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03004837 return adapter->fw_done_rc ? -EIO : 0;
4838}
4839
4840static int handle_query_phys_parms_rsp(union ibmvnic_crq *crq,
4841 struct ibmvnic_adapter *adapter)
4842{
4843 struct net_device *netdev = adapter->netdev;
4844 int rc;
Murilo Fossa Vicentinidd0f9d82019-09-16 11:50:37 -03004845 __be32 rspeed = cpu_to_be32(crq->query_phys_parms_rsp.speed);
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03004846
4847 rc = crq->query_phys_parms_rsp.rc.code;
4848 if (rc) {
4849 netdev_err(netdev, "Error %d in QUERY_PHYS_PARMS\n", rc);
4850 return rc;
4851 }
Murilo Fossa Vicentinidd0f9d82019-09-16 11:50:37 -03004852 switch (rspeed) {
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03004853 case IBMVNIC_10MBPS:
4854 adapter->speed = SPEED_10;
4855 break;
4856 case IBMVNIC_100MBPS:
4857 adapter->speed = SPEED_100;
4858 break;
4859 case IBMVNIC_1GBPS:
4860 adapter->speed = SPEED_1000;
4861 break;
Lijun Panb9cd7952020-09-27 19:06:25 -05004862 case IBMVNIC_10GBPS:
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03004863 adapter->speed = SPEED_10000;
4864 break;
4865 case IBMVNIC_25GBPS:
4866 adapter->speed = SPEED_25000;
4867 break;
4868 case IBMVNIC_40GBPS:
4869 adapter->speed = SPEED_40000;
4870 break;
4871 case IBMVNIC_50GBPS:
4872 adapter->speed = SPEED_50000;
4873 break;
4874 case IBMVNIC_100GBPS:
4875 adapter->speed = SPEED_100000;
4876 break;
Lijun Panb9cd7952020-09-27 19:06:25 -05004877 case IBMVNIC_200GBPS:
4878 adapter->speed = SPEED_200000;
4879 break;
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03004880 default:
Murilo Fossa Vicentinidd0f9d82019-09-16 11:50:37 -03004881 if (netif_carrier_ok(netdev))
4882 netdev_warn(netdev, "Unknown speed 0x%08x\n", rspeed);
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03004883 adapter->speed = SPEED_UNKNOWN;
4884 }
4885 if (crq->query_phys_parms_rsp.flags1 & IBMVNIC_FULL_DUPLEX)
4886 adapter->duplex = DUPLEX_FULL;
4887 else if (crq->query_phys_parms_rsp.flags1 & IBMVNIC_HALF_DUPLEX)
4888 adapter->duplex = DUPLEX_HALF;
4889 else
4890 adapter->duplex = DUPLEX_UNKNOWN;
4891
4892 return rc;
4893}
4894
Thomas Falcon032c5e82015-12-21 11:26:06 -06004895static void ibmvnic_handle_crq(union ibmvnic_crq *crq,
4896 struct ibmvnic_adapter *adapter)
4897{
4898 struct ibmvnic_generic_crq *gen_crq = &crq->generic;
4899 struct net_device *netdev = adapter->netdev;
4900 struct device *dev = &adapter->vdev->dev;
Murilo Fossa Vicentini993a82b2017-04-19 13:44:35 -04004901 u64 *u64_crq = (u64 *)crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004902 long rc;
4903
4904 netdev_dbg(netdev, "Handling CRQ: %016lx %016lx\n",
Murilo Fossa Vicentini993a82b2017-04-19 13:44:35 -04004905 (unsigned long int)cpu_to_be64(u64_crq[0]),
4906 (unsigned long int)cpu_to_be64(u64_crq[1]));
Thomas Falcon032c5e82015-12-21 11:26:06 -06004907 switch (gen_crq->first) {
4908 case IBMVNIC_CRQ_INIT_RSP:
4909 switch (gen_crq->cmd) {
4910 case IBMVNIC_CRQ_INIT:
4911 dev_info(dev, "Partner initialized\n");
John Allen017892c12017-05-26 10:30:19 -04004912 adapter->from_passive_init = true;
Sukadev Bhattiprolu76cdc5c2020-11-25 18:04:29 -06004913 /* Discard any stale login responses from prev reset.
4914 * CHECK: should we clear even on INIT_COMPLETE?
4915 */
4916 adapter->login_pending = false;
4917
Thomas Falcon17c87052018-05-23 13:37:58 -05004918 if (!completion_done(&adapter->init_done)) {
4919 complete(&adapter->init_done);
4920 adapter->init_done_rc = -EIO;
4921 }
Thomas Falcon5a18e1e2018-04-06 18:37:05 -05004922 ibmvnic_reset(adapter, VNIC_RESET_FAILOVER);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004923 break;
4924 case IBMVNIC_CRQ_INIT_COMPLETE:
4925 dev_info(dev, "Partner initialization complete\n");
Thomas Falcon51536982018-05-23 13:37:56 -05004926 adapter->crq.active = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004927 send_version_xchg(adapter);
4928 break;
4929 default:
4930 dev_err(dev, "Unknown crq cmd: %d\n", gen_crq->cmd);
4931 }
4932 return;
4933 case IBMVNIC_CRQ_XPORT_EVENT:
Nathan Fontenoted651a12017-05-03 14:04:38 -04004934 netif_carrier_off(netdev);
Thomas Falcon51536982018-05-23 13:37:56 -05004935 adapter->crq.active = false;
Thomas Falcon2147e3d2019-11-25 17:12:54 -06004936 /* terminate any thread waiting for a response
4937 * from the device
4938 */
4939 if (!completion_done(&adapter->fw_done)) {
4940 adapter->fw_done_rc = -EIO;
4941 complete(&adapter->fw_done);
4942 }
4943 if (!completion_done(&adapter->stats_done))
4944 complete(&adapter->stats_done);
Juliet Kim7ed5b312019-09-20 16:11:23 -04004945 if (test_bit(0, &adapter->resetting))
Thomas Falcon2770a792018-05-23 13:38:02 -05004946 adapter->force_reset_recovery = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004947 if (gen_crq->cmd == IBMVNIC_PARTITION_MIGRATED) {
Nathan Fontenoted651a12017-05-03 14:04:38 -04004948 dev_info(dev, "Migrated, re-enabling adapter\n");
4949 ibmvnic_reset(adapter, VNIC_RESET_MOBILITY);
Thomas Falcondfad09a2016-08-18 11:37:51 -05004950 } else if (gen_crq->cmd == IBMVNIC_DEVICE_FAILOVER) {
4951 dev_info(dev, "Backing device failover detected\n");
Thomas Falcon5a18e1e2018-04-06 18:37:05 -05004952 adapter->failover_pending = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004953 } else {
4954 /* The adapter lost the connection */
4955 dev_err(dev, "Virtual Adapter failed (rc=%d)\n",
4956 gen_crq->cmd);
Nathan Fontenoted651a12017-05-03 14:04:38 -04004957 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004958 }
4959 return;
4960 case IBMVNIC_CRQ_CMD_RSP:
4961 break;
4962 default:
4963 dev_err(dev, "Got an invalid msg type 0x%02x\n",
4964 gen_crq->first);
4965 return;
4966 }
4967
4968 switch (gen_crq->cmd) {
4969 case VERSION_EXCHANGE_RSP:
4970 rc = crq->version_exchange_rsp.rc.code;
4971 if (rc) {
4972 dev_err(dev, "Error %ld in VERSION_EXCHG_RSP\n", rc);
4973 break;
4974 }
Thomas Falcon78468892020-05-28 11:19:17 -05004975 ibmvnic_version =
Thomas Falcon032c5e82015-12-21 11:26:06 -06004976 be16_to_cpu(crq->version_exchange_rsp.version);
Thomas Falcon78468892020-05-28 11:19:17 -05004977 dev_info(dev, "Partner protocol version is %d\n",
4978 ibmvnic_version);
Lijun Pan491099a2020-09-27 20:13:26 -05004979 send_query_cap(adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004980 break;
4981 case QUERY_CAPABILITY_RSP:
4982 handle_query_cap_rsp(crq, adapter);
4983 break;
4984 case QUERY_MAP_RSP:
4985 handle_query_map_rsp(crq, adapter);
4986 break;
4987 case REQUEST_MAP_RSP:
Thomas Falconf3be0cb2017-06-21 14:53:01 -05004988 adapter->fw_done_rc = crq->request_map_rsp.rc.code;
4989 complete(&adapter->fw_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004990 break;
4991 case REQUEST_UNMAP_RSP:
4992 handle_request_unmap_rsp(crq, adapter);
4993 break;
4994 case REQUEST_CAPABILITY_RSP:
4995 handle_request_cap_rsp(crq, adapter);
4996 break;
4997 case LOGIN_RSP:
4998 netdev_dbg(netdev, "Got Login Response\n");
4999 handle_login_rsp(crq, adapter);
5000 break;
5001 case LOGICAL_LINK_STATE_RSP:
Nathan Fontenot53da09e2017-04-21 15:39:04 -04005002 netdev_dbg(netdev,
5003 "Got Logical Link State Response, state: %d rc: %d\n",
5004 crq->logical_link_state_rsp.link_state,
5005 crq->logical_link_state_rsp.rc.code);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005006 adapter->logical_link_state =
5007 crq->logical_link_state_rsp.link_state;
Nathan Fontenot53da09e2017-04-21 15:39:04 -04005008 adapter->init_done_rc = crq->logical_link_state_rsp.rc.code;
5009 complete(&adapter->init_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005010 break;
5011 case LINK_STATE_INDICATION:
5012 netdev_dbg(netdev, "Got Logical Link State Indication\n");
5013 adapter->phys_link_state =
5014 crq->link_state_indication.phys_link_state;
5015 adapter->logical_link_state =
5016 crq->link_state_indication.logical_link_state;
Thomas Falcon0655f992019-05-09 23:13:44 -05005017 if (adapter->phys_link_state && adapter->logical_link_state)
5018 netif_carrier_on(netdev);
5019 else
5020 netif_carrier_off(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005021 break;
5022 case CHANGE_MAC_ADDR_RSP:
5023 netdev_dbg(netdev, "Got MAC address change Response\n");
Thomas Falconf8136142018-01-29 13:45:05 -06005024 adapter->fw_done_rc = handle_change_mac_rsp(crq, adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005025 break;
5026 case ERROR_INDICATION:
5027 netdev_dbg(netdev, "Got Error Indication\n");
5028 handle_error_indication(crq, adapter);
5029 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005030 case REQUEST_STATISTICS_RSP:
5031 netdev_dbg(netdev, "Got Statistics Response\n");
5032 complete(&adapter->stats_done);
5033 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005034 case QUERY_IP_OFFLOAD_RSP:
5035 netdev_dbg(netdev, "Got Query IP offload Response\n");
5036 handle_query_ip_offload_rsp(adapter);
5037 break;
5038 case MULTICAST_CTRL_RSP:
5039 netdev_dbg(netdev, "Got multicast control Response\n");
5040 break;
5041 case CONTROL_IP_OFFLOAD_RSP:
5042 netdev_dbg(netdev, "Got Control IP offload Response\n");
5043 dma_unmap_single(dev, adapter->ip_offload_ctrl_tok,
5044 sizeof(adapter->ip_offload_ctrl),
5045 DMA_TO_DEVICE);
John Allenbd0b6722017-03-17 17:13:40 -05005046 complete(&adapter->init_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005047 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005048 case COLLECT_FW_TRACE_RSP:
5049 netdev_dbg(netdev, "Got Collect firmware trace Response\n");
5050 complete(&adapter->fw_done);
5051 break;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02005052 case GET_VPD_SIZE_RSP:
5053 handle_vpd_size_rsp(crq, adapter);
5054 break;
5055 case GET_VPD_RSP:
5056 handle_vpd_rsp(crq, adapter);
5057 break;
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03005058 case QUERY_PHYS_PARMS_RSP:
5059 adapter->fw_done_rc = handle_query_phys_parms_rsp(crq, adapter);
5060 complete(&adapter->fw_done);
5061 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005062 default:
5063 netdev_err(netdev, "Got an invalid cmd type 0x%02x\n",
5064 gen_crq->cmd);
5065 }
5066}
5067
5068static irqreturn_t ibmvnic_interrupt(int irq, void *instance)
5069{
5070 struct ibmvnic_adapter *adapter = instance;
Thomas Falcon6c267b32017-02-15 12:17:58 -06005071
Thomas Falcon6c267b32017-02-15 12:17:58 -06005072 tasklet_schedule(&adapter->tasklet);
Thomas Falcon6c267b32017-02-15 12:17:58 -06005073 return IRQ_HANDLED;
5074}
5075
Allen Paisaa7c3fe2020-09-14 12:59:29 +05305076static void ibmvnic_tasklet(struct tasklet_struct *t)
Thomas Falcon6c267b32017-02-15 12:17:58 -06005077{
Allen Paisaa7c3fe2020-09-14 12:59:29 +05305078 struct ibmvnic_adapter *adapter = from_tasklet(adapter, t, tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005079 struct ibmvnic_crq_queue *queue = &adapter->crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005080 union ibmvnic_crq *crq;
5081 unsigned long flags;
5082 bool done = false;
5083
5084 spin_lock_irqsave(&queue->lock, flags);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005085 while (!done) {
5086 /* Pull all the valid messages off the CRQ */
5087 while ((crq = ibmvnic_next_crq(adapter)) != NULL) {
5088 ibmvnic_handle_crq(crq, adapter);
5089 crq->generic.first = 0;
5090 }
Brian Kinged7ecbf2017-04-19 13:44:53 -04005091
5092 /* remain in tasklet until all
5093 * capabilities responses are received
5094 */
5095 if (!adapter->wait_capability)
5096 done = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005097 }
Thomas Falcon249168a2017-02-15 12:18:00 -06005098 /* if capabilities CRQ's were sent in this tasklet, the following
5099 * tasklet must wait until all responses are received
5100 */
5101 if (atomic_read(&adapter->running_cap_crqs) != 0)
5102 adapter->wait_capability = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005103 spin_unlock_irqrestore(&queue->lock, flags);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005104}
5105
5106static int ibmvnic_reenable_crq_queue(struct ibmvnic_adapter *adapter)
5107{
5108 struct vio_dev *vdev = adapter->vdev;
5109 int rc;
5110
5111 do {
5112 rc = plpar_hcall_norets(H_ENABLE_CRQ, vdev->unit_address);
5113 } while (rc == H_IN_PROGRESS || rc == H_BUSY || H_IS_LONG_BUSY(rc));
5114
5115 if (rc)
5116 dev_err(&vdev->dev, "Error enabling adapter (rc=%d)\n", rc);
5117
5118 return rc;
5119}
5120
5121static int ibmvnic_reset_crq(struct ibmvnic_adapter *adapter)
5122{
5123 struct ibmvnic_crq_queue *crq = &adapter->crq;
5124 struct device *dev = &adapter->vdev->dev;
5125 struct vio_dev *vdev = adapter->vdev;
5126 int rc;
5127
5128 /* Close the CRQ */
5129 do {
5130 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
5131 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
5132
5133 /* Clean out the queue */
Lijun Pan0e435be2020-11-23 13:35:46 -06005134 if (!crq->msgs)
5135 return -EINVAL;
5136
Thomas Falcon032c5e82015-12-21 11:26:06 -06005137 memset(crq->msgs, 0, PAGE_SIZE);
5138 crq->cur = 0;
Thomas Falcon51536982018-05-23 13:37:56 -05005139 crq->active = false;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005140
5141 /* And re-open it again */
5142 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
5143 crq->msg_token, PAGE_SIZE);
5144
5145 if (rc == H_CLOSED)
5146 /* Adapter is good, but other end is not ready */
5147 dev_warn(dev, "Partner adapter not ready\n");
5148 else if (rc != 0)
5149 dev_warn(dev, "Couldn't register crq (rc=%d)\n", rc);
5150
5151 return rc;
5152}
5153
Nathan Fontenotf9928872017-03-30 02:48:54 -04005154static void release_crq_queue(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06005155{
5156 struct ibmvnic_crq_queue *crq = &adapter->crq;
5157 struct vio_dev *vdev = adapter->vdev;
5158 long rc;
5159
Nathan Fontenotf9928872017-03-30 02:48:54 -04005160 if (!crq->msgs)
5161 return;
5162
Thomas Falcon032c5e82015-12-21 11:26:06 -06005163 netdev_dbg(adapter->netdev, "Releasing CRQ\n");
5164 free_irq(vdev->irq, adapter);
Thomas Falcon6c267b32017-02-15 12:17:58 -06005165 tasklet_kill(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005166 do {
5167 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
5168 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
5169
5170 dma_unmap_single(&vdev->dev, crq->msg_token, PAGE_SIZE,
5171 DMA_BIDIRECTIONAL);
5172 free_page((unsigned long)crq->msgs);
Nathan Fontenotf9928872017-03-30 02:48:54 -04005173 crq->msgs = NULL;
Thomas Falcon51536982018-05-23 13:37:56 -05005174 crq->active = false;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005175}
5176
Nathan Fontenotf9928872017-03-30 02:48:54 -04005177static int init_crq_queue(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06005178{
5179 struct ibmvnic_crq_queue *crq = &adapter->crq;
5180 struct device *dev = &adapter->vdev->dev;
5181 struct vio_dev *vdev = adapter->vdev;
5182 int rc, retrc = -ENOMEM;
5183
Nathan Fontenotf9928872017-03-30 02:48:54 -04005184 if (crq->msgs)
5185 return 0;
5186
Thomas Falcon032c5e82015-12-21 11:26:06 -06005187 crq->msgs = (union ibmvnic_crq *)get_zeroed_page(GFP_KERNEL);
5188 /* Should we allocate more than one page? */
5189
5190 if (!crq->msgs)
5191 return -ENOMEM;
5192
5193 crq->size = PAGE_SIZE / sizeof(*crq->msgs);
5194 crq->msg_token = dma_map_single(dev, crq->msgs, PAGE_SIZE,
5195 DMA_BIDIRECTIONAL);
5196 if (dma_mapping_error(dev, crq->msg_token))
5197 goto map_failed;
5198
5199 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
5200 crq->msg_token, PAGE_SIZE);
5201
5202 if (rc == H_RESOURCE)
5203 /* maybe kexecing and resource is busy. try a reset */
5204 rc = ibmvnic_reset_crq(adapter);
5205 retrc = rc;
5206
5207 if (rc == H_CLOSED) {
5208 dev_warn(dev, "Partner adapter not ready\n");
5209 } else if (rc) {
5210 dev_warn(dev, "Error %d opening adapter\n", rc);
5211 goto reg_crq_failed;
5212 }
5213
5214 retrc = 0;
5215
Allen Paisaa7c3fe2020-09-14 12:59:29 +05305216 tasklet_setup(&adapter->tasklet, (void *)ibmvnic_tasklet);
Thomas Falcon6c267b32017-02-15 12:17:58 -06005217
Thomas Falcon032c5e82015-12-21 11:26:06 -06005218 netdev_dbg(adapter->netdev, "registering irq 0x%x\n", vdev->irq);
Murilo Fossa Vicentinie56e2512019-04-25 11:02:33 -03005219 snprintf(crq->name, sizeof(crq->name), "ibmvnic-%x",
5220 adapter->vdev->unit_address);
5221 rc = request_irq(vdev->irq, ibmvnic_interrupt, 0, crq->name, adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005222 if (rc) {
5223 dev_err(dev, "Couldn't register irq 0x%x. rc=%d\n",
5224 vdev->irq, rc);
5225 goto req_irq_failed;
5226 }
5227
5228 rc = vio_enable_interrupts(vdev);
5229 if (rc) {
5230 dev_err(dev, "Error %d enabling interrupts\n", rc);
5231 goto req_irq_failed;
5232 }
5233
5234 crq->cur = 0;
5235 spin_lock_init(&crq->lock);
5236
5237 return retrc;
5238
5239req_irq_failed:
Thomas Falcon6c267b32017-02-15 12:17:58 -06005240 tasklet_kill(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005241 do {
5242 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
5243 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
5244reg_crq_failed:
5245 dma_unmap_single(dev, crq->msg_token, PAGE_SIZE, DMA_BIDIRECTIONAL);
5246map_failed:
5247 free_page((unsigned long)crq->msgs);
Nathan Fontenotf9928872017-03-30 02:48:54 -04005248 crq->msgs = NULL;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005249 return retrc;
5250}
5251
Lijun Pan635e4422020-08-19 17:52:26 -05005252static int ibmvnic_reset_init(struct ibmvnic_adapter *adapter, bool reset)
John Allenf6ef6402017-03-17 17:13:42 -05005253{
5254 struct device *dev = &adapter->vdev->dev;
Dany Madden98c41f02020-11-25 18:04:32 -06005255 unsigned long timeout = msecs_to_jiffies(20000);
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06005256 u64 old_num_rx_queues, old_num_tx_queues;
John Allenf6ef6402017-03-17 17:13:42 -05005257 int rc;
5258
John Allen017892c12017-05-26 10:30:19 -04005259 adapter->from_passive_init = false;
5260
Lijun Pan635e4422020-08-19 17:52:26 -05005261 if (reset) {
5262 old_num_rx_queues = adapter->req_rx_queues;
5263 old_num_tx_queues = adapter->req_tx_queues;
5264 reinit_completion(&adapter->init_done);
5265 }
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06005266
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04005267 adapter->init_done_rc = 0;
Lijun Panfa68bfa2020-08-19 17:52:24 -05005268 rc = ibmvnic_send_crq_init(adapter);
5269 if (rc) {
5270 dev_err(dev, "Send crq init failed with error %d\n", rc);
5271 return rc;
5272 }
5273
John Allenf6ef6402017-03-17 17:13:42 -05005274 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
5275 dev_err(dev, "Initialization sequence timed out\n");
John Allen017892c12017-05-26 10:30:19 -04005276 return -1;
5277 }
5278
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04005279 if (adapter->init_done_rc) {
5280 release_crq_queue(adapter);
5281 return adapter->init_done_rc;
5282 }
5283
Lijun Pan785a2b12020-09-17 21:12:46 -05005284 if (adapter->from_passive_init) {
5285 adapter->state = VNIC_OPEN;
5286 adapter->from_passive_init = false;
5287 return -1;
5288 }
5289
Lijun Pan635e4422020-08-19 17:52:26 -05005290 if (reset &&
5291 test_bit(0, &adapter->resetting) && !adapter->wait_for_reset &&
Nathan Fontenot30f79622018-04-06 18:37:06 -05005292 adapter->reset_reason != VNIC_RESET_MOBILITY) {
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06005293 if (adapter->req_rx_queues != old_num_rx_queues ||
5294 adapter->req_tx_queues != old_num_tx_queues) {
5295 release_sub_crqs(adapter, 0);
5296 rc = init_sub_crqs(adapter);
5297 } else {
5298 rc = reset_sub_crq_queues(adapter);
5299 }
5300 } else {
Nathan Fontenot57a49432017-05-26 10:31:12 -04005301 rc = init_sub_crqs(adapter);
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06005302 }
5303
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04005304 if (rc) {
5305 dev_err(dev, "Initialization of sub crqs failed\n");
5306 release_crq_queue(adapter);
Thomas Falcon5df969c2017-06-28 19:55:54 -05005307 return rc;
5308 }
5309
5310 rc = init_sub_crq_irqs(adapter);
5311 if (rc) {
5312 dev_err(dev, "Failed to initialize sub crq irqs\n");
5313 release_crq_queue(adapter);
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04005314 }
5315
5316 return rc;
John Allenf6ef6402017-03-17 17:13:42 -05005317}
5318
Thomas Falcon40c9db82017-06-12 12:35:04 -05005319static struct device_attribute dev_attr_failover;
5320
Thomas Falcon032c5e82015-12-21 11:26:06 -06005321static int ibmvnic_probe(struct vio_dev *dev, const struct vio_device_id *id)
5322{
5323 struct ibmvnic_adapter *adapter;
5324 struct net_device *netdev;
5325 unsigned char *mac_addr_p;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005326 int rc;
5327
5328 dev_dbg(&dev->dev, "entering ibmvnic_probe for UA 0x%x\n",
5329 dev->unit_address);
5330
5331 mac_addr_p = (unsigned char *)vio_get_attribute(dev,
5332 VETH_MAC_ADDR, NULL);
5333 if (!mac_addr_p) {
5334 dev_err(&dev->dev,
5335 "(%s:%3.3d) ERROR: Can't find MAC_ADDR attribute\n",
5336 __FILE__, __LINE__);
5337 return 0;
5338 }
5339
5340 netdev = alloc_etherdev_mq(sizeof(struct ibmvnic_adapter),
Thomas Falcond45cc3a2017-12-18 12:52:11 -06005341 IBMVNIC_MAX_QUEUES);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005342 if (!netdev)
5343 return -ENOMEM;
5344
5345 adapter = netdev_priv(netdev);
Nathan Fontenot90c80142017-05-03 14:04:32 -04005346 adapter->state = VNIC_PROBING;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005347 dev_set_drvdata(&dev->dev, netdev);
5348 adapter->vdev = dev;
5349 adapter->netdev = netdev;
Sukadev Bhattiprolu76cdc5c2020-11-25 18:04:29 -06005350 adapter->login_pending = false;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005351
5352 ether_addr_copy(adapter->mac_addr, mac_addr_p);
5353 ether_addr_copy(netdev->dev_addr, adapter->mac_addr);
5354 netdev->irq = dev->irq;
5355 netdev->netdev_ops = &ibmvnic_netdev_ops;
5356 netdev->ethtool_ops = &ibmvnic_ethtool_ops;
5357 SET_NETDEV_DEV(netdev, &dev->dev);
5358
5359 spin_lock_init(&adapter->stats_lock);
5360
Nathan Fontenoted651a12017-05-03 14:04:38 -04005361 INIT_WORK(&adapter->ibmvnic_reset, __ibmvnic_reset);
Juliet Kim7ed5b312019-09-20 16:11:23 -04005362 INIT_DELAYED_WORK(&adapter->ibmvnic_delayed_reset,
5363 __ibmvnic_delayed_reset);
Nathan Fontenoted651a12017-05-03 14:04:38 -04005364 INIT_LIST_HEAD(&adapter->rwi_list);
Thomas Falcon6c5c7482018-12-10 15:22:22 -06005365 spin_lock_init(&adapter->rwi_lock);
Juliet Kim7d7195a2020-03-10 09:23:58 -05005366 spin_lock_init(&adapter->state_lock);
Thomas Falconff25dcb2019-11-25 17:12:56 -06005367 mutex_init(&adapter->fw_lock);
Thomas Falconbbd669a2019-04-04 18:58:26 -05005368 init_completion(&adapter->init_done);
Thomas Falcon070eca92019-11-25 17:12:53 -06005369 init_completion(&adapter->fw_done);
5370 init_completion(&adapter->reset_done);
5371 init_completion(&adapter->stats_done);
Juliet Kim7ed5b312019-09-20 16:11:23 -04005372 clear_bit(0, &adapter->resetting);
Nathan Fontenoted651a12017-05-03 14:04:38 -04005373
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04005374 do {
Nathan Fontenot30f79622018-04-06 18:37:06 -05005375 rc = init_crq_queue(adapter);
5376 if (rc) {
5377 dev_err(&dev->dev, "Couldn't initialize crq. rc=%d\n",
5378 rc);
5379 goto ibmvnic_init_fail;
5380 }
5381
Lijun Pan635e4422020-08-19 17:52:26 -05005382 rc = ibmvnic_reset_init(adapter, false);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05005383 if (rc && rc != EAGAIN)
5384 goto ibmvnic_init_fail;
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04005385 } while (rc == EAGAIN);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005386
Thomas Falcon07184212018-05-16 15:49:05 -05005387 rc = init_stats_buffers(adapter);
5388 if (rc)
5389 goto ibmvnic_init_fail;
5390
5391 rc = init_stats_token(adapter);
5392 if (rc)
5393 goto ibmvnic_stats_fail;
5394
Thomas Falconf39f0d12017-02-14 10:22:59 -06005395 netdev->mtu = adapter->req_mtu - ETH_HLEN;
John Allenc26eba02017-10-26 16:23:25 -05005396 netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
5397 netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005398
Thomas Falcon40c9db82017-06-12 12:35:04 -05005399 rc = device_create_file(&dev->dev, &dev_attr_failover);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05005400 if (rc)
Thomas Falcon07184212018-05-16 15:49:05 -05005401 goto ibmvnic_dev_file_err;
Thomas Falcon40c9db82017-06-12 12:35:04 -05005402
Mick Tarsele876a8a2017-09-28 13:53:18 -07005403 netif_carrier_off(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005404 rc = register_netdev(netdev);
5405 if (rc) {
5406 dev_err(&dev->dev, "failed to register netdev rc=%d\n", rc);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05005407 goto ibmvnic_register_fail;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005408 }
5409 dev_info(&dev->dev, "ibmvnic registered\n");
5410
Nathan Fontenot90c80142017-05-03 14:04:32 -04005411 adapter->state = VNIC_PROBED;
John Allenc26eba02017-10-26 16:23:25 -05005412
5413 adapter->wait_for_reset = false;
Dany Maddena86d5c62020-11-25 18:04:31 -06005414 adapter->last_reset_time = jiffies;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005415 return 0;
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05005416
5417ibmvnic_register_fail:
5418 device_remove_file(&dev->dev, &dev_attr_failover);
5419
Thomas Falcon07184212018-05-16 15:49:05 -05005420ibmvnic_dev_file_err:
5421 release_stats_token(adapter);
5422
5423ibmvnic_stats_fail:
5424 release_stats_buffers(adapter);
5425
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05005426ibmvnic_init_fail:
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06005427 release_sub_crqs(adapter, 1);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05005428 release_crq_queue(adapter);
Thomas Falconff25dcb2019-11-25 17:12:56 -06005429 mutex_destroy(&adapter->fw_lock);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05005430 free_netdev(netdev);
5431
5432 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005433}
5434
5435static int ibmvnic_remove(struct vio_dev *dev)
5436{
5437 struct net_device *netdev = dev_get_drvdata(&dev->dev);
Nathan Fontenot37489052017-04-19 13:45:04 -04005438 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Juliet Kim7d7195a2020-03-10 09:23:58 -05005439 unsigned long flags;
5440
5441 spin_lock_irqsave(&adapter->state_lock, flags);
Lijun Pan3ada2882020-11-23 13:35:47 -06005442 if (test_bit(0, &adapter->resetting)) {
Juliet Kim7d7195a2020-03-10 09:23:58 -05005443 spin_unlock_irqrestore(&adapter->state_lock, flags);
5444 return -EBUSY;
5445 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06005446
Nathan Fontenot90c80142017-05-03 14:04:32 -04005447 adapter->state = VNIC_REMOVING;
Juliet Kim7d7195a2020-03-10 09:23:58 -05005448 spin_unlock_irqrestore(&adapter->state_lock, flags);
5449
Thomas Falcon6954a9e2020-06-12 13:34:41 -05005450 flush_work(&adapter->ibmvnic_reset);
5451 flush_delayed_work(&adapter->ibmvnic_delayed_reset);
5452
Juliet Kima5681e22018-11-19 15:59:22 -06005453 rtnl_lock();
5454 unregister_netdevice(netdev);
Nathan Fontenot37489052017-04-19 13:45:04 -04005455
5456 release_resources(adapter);
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06005457 release_sub_crqs(adapter, 1);
Nathan Fontenot37489052017-04-19 13:45:04 -04005458 release_crq_queue(adapter);
5459
Thomas Falcon53cc7722018-02-26 18:10:56 -06005460 release_stats_token(adapter);
5461 release_stats_buffers(adapter);
5462
Nathan Fontenot90c80142017-05-03 14:04:32 -04005463 adapter->state = VNIC_REMOVED;
5464
Juliet Kima5681e22018-11-19 15:59:22 -06005465 rtnl_unlock();
Thomas Falconff25dcb2019-11-25 17:12:56 -06005466 mutex_destroy(&adapter->fw_lock);
Thomas Falcon40c9db82017-06-12 12:35:04 -05005467 device_remove_file(&dev->dev, &dev_attr_failover);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005468 free_netdev(netdev);
5469 dev_set_drvdata(&dev->dev, NULL);
5470
5471 return 0;
5472}
5473
Thomas Falcon40c9db82017-06-12 12:35:04 -05005474static ssize_t failover_store(struct device *dev, struct device_attribute *attr,
5475 const char *buf, size_t count)
5476{
5477 struct net_device *netdev = dev_get_drvdata(dev);
5478 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
5479 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
5480 __be64 session_token;
5481 long rc;
5482
5483 if (!sysfs_streq(buf, "1"))
5484 return -EINVAL;
5485
5486 rc = plpar_hcall(H_VIOCTL, retbuf, adapter->vdev->unit_address,
5487 H_GET_SESSION_TOKEN, 0, 0, 0);
5488 if (rc) {
5489 netdev_err(netdev, "Couldn't retrieve session token, rc %ld\n",
5490 rc);
5491 return -EINVAL;
5492 }
5493
5494 session_token = (__be64)retbuf[0];
5495 netdev_dbg(netdev, "Initiating client failover, session id %llx\n",
5496 be64_to_cpu(session_token));
5497 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
5498 H_SESSION_ERR_DETECTED, session_token, 0, 0);
5499 if (rc) {
5500 netdev_err(netdev, "Client initiated failover failed, rc %ld\n",
5501 rc);
5502 return -EINVAL;
5503 }
5504
5505 return count;
5506}
5507
Joe Perches6cbaefb2017-12-19 10:15:09 -08005508static DEVICE_ATTR_WO(failover);
Thomas Falcon40c9db82017-06-12 12:35:04 -05005509
Thomas Falcon032c5e82015-12-21 11:26:06 -06005510static unsigned long ibmvnic_get_desired_dma(struct vio_dev *vdev)
5511{
5512 struct net_device *netdev = dev_get_drvdata(&vdev->dev);
5513 struct ibmvnic_adapter *adapter;
5514 struct iommu_table *tbl;
5515 unsigned long ret = 0;
5516 int i;
5517
5518 tbl = get_iommu_table_base(&vdev->dev);
5519
5520 /* netdev inits at probe time along with the structures we need below*/
5521 if (!netdev)
5522 return IOMMU_PAGE_ALIGN(IBMVNIC_IO_ENTITLEMENT_DEFAULT, tbl);
5523
5524 adapter = netdev_priv(netdev);
5525
5526 ret += PAGE_SIZE; /* the crq message queue */
Thomas Falcon032c5e82015-12-21 11:26:06 -06005527 ret += IOMMU_PAGE_ALIGN(sizeof(struct ibmvnic_statistics), tbl);
5528
5529 for (i = 0; i < adapter->req_tx_queues + adapter->req_rx_queues; i++)
5530 ret += 4 * PAGE_SIZE; /* the scrq message queue */
5531
Thomas Falcon507ebe62020-08-21 13:39:01 -05005532 for (i = 0; i < adapter->num_active_rx_pools; i++)
Thomas Falcon032c5e82015-12-21 11:26:06 -06005533 ret += adapter->rx_pool[i].size *
5534 IOMMU_PAGE_ALIGN(adapter->rx_pool[i].buff_size, tbl);
5535
5536 return ret;
5537}
5538
5539static int ibmvnic_resume(struct device *dev)
5540{
5541 struct net_device *netdev = dev_get_drvdata(dev);
5542 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005543
John Allencb89ba22017-06-19 11:27:53 -05005544 if (adapter->state != VNIC_OPEN)
5545 return 0;
5546
John Allena2488782017-07-24 13:26:06 -05005547 tasklet_schedule(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005548
5549 return 0;
5550}
5551
Arvind Yadav8c37bc62017-08-17 18:52:54 +05305552static const struct vio_device_id ibmvnic_device_table[] = {
Thomas Falcon032c5e82015-12-21 11:26:06 -06005553 {"network", "IBM,vnic"},
5554 {"", "" }
5555};
5556MODULE_DEVICE_TABLE(vio, ibmvnic_device_table);
5557
5558static const struct dev_pm_ops ibmvnic_pm_ops = {
5559 .resume = ibmvnic_resume
5560};
5561
5562static struct vio_driver ibmvnic_driver = {
5563 .id_table = ibmvnic_device_table,
5564 .probe = ibmvnic_probe,
5565 .remove = ibmvnic_remove,
5566 .get_desired_dma = ibmvnic_get_desired_dma,
5567 .name = ibmvnic_driver_name,
5568 .pm = &ibmvnic_pm_ops,
5569};
5570
5571/* module functions */
5572static int __init ibmvnic_module_init(void)
5573{
5574 pr_info("%s: %s %s\n", ibmvnic_driver_name, ibmvnic_driver_string,
5575 IBMVNIC_DRIVER_VERSION);
5576
5577 return vio_register_driver(&ibmvnic_driver);
5578}
5579
5580static void __exit ibmvnic_module_exit(void)
5581{
5582 vio_unregister_driver(&ibmvnic_driver);
5583}
5584
5585module_init(ibmvnic_module_init);
5586module_exit(ibmvnic_module_exit);