blob: 55970f02d9da9fc09f83acb21bfd223516ffe622 [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 }
Dwip N. Banerjeec2af6222020-12-09 20:53:31 -0500401 adapter->replenish_add_buff_failure += ind_bufp->index;
402 atomic_add(buffers_added, &pool->available);
Thomas Falcon4f0b6812020-11-18 19:12:18 -0600403 ind_bufp->index = 0;
Thomas Falcon5a18e1e2018-04-06 18:37:05 -0500404 if (lpar_rc == H_CLOSED || adapter->failover_pending) {
Thomas Falconf185a492017-05-26 10:30:48 -0400405 /* Disable buffer pool replenishment and report carrier off if
Thomas Falcon5a18e1e2018-04-06 18:37:05 -0500406 * queue is closed or pending failover.
407 * Firmware guarantees that a signal will be sent to the
408 * driver, triggering a reset.
Thomas Falconf185a492017-05-26 10:30:48 -0400409 */
410 deactivate_rx_pools(adapter);
411 netif_carrier_off(adapter->netdev);
412 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600413}
414
415static void replenish_pools(struct ibmvnic_adapter *adapter)
416{
417 int i;
418
Thomas Falcon032c5e82015-12-21 11:26:06 -0600419 adapter->replenish_task_cycles++;
Thomas Falcon507ebe62020-08-21 13:39:01 -0500420 for (i = 0; i < adapter->num_active_rx_pools; i++) {
Thomas Falcon032c5e82015-12-21 11:26:06 -0600421 if (adapter->rx_pool[i].active)
422 replenish_rx_pool(adapter, &adapter->rx_pool[i]);
423 }
Sukadev Bhattiprolu38bd5ce2020-12-04 18:22:35 -0800424
425 netdev_dbg(adapter->netdev, "Replenished %d pools\n", i);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600426}
427
John Allen3d52b592017-08-02 16:44:14 -0500428static void release_stats_buffers(struct ibmvnic_adapter *adapter)
429{
430 kfree(adapter->tx_stats_buffers);
431 kfree(adapter->rx_stats_buffers);
Thomas Falconb0992ec2018-02-06 17:25:23 -0600432 adapter->tx_stats_buffers = NULL;
433 adapter->rx_stats_buffers = NULL;
John Allen3d52b592017-08-02 16:44:14 -0500434}
435
436static int init_stats_buffers(struct ibmvnic_adapter *adapter)
437{
438 adapter->tx_stats_buffers =
Nathan Fontenotabcae542018-02-19 13:30:47 -0600439 kcalloc(IBMVNIC_MAX_QUEUES,
John Allen3d52b592017-08-02 16:44:14 -0500440 sizeof(struct ibmvnic_tx_queue_stats),
441 GFP_KERNEL);
442 if (!adapter->tx_stats_buffers)
443 return -ENOMEM;
444
445 adapter->rx_stats_buffers =
Nathan Fontenotabcae542018-02-19 13:30:47 -0600446 kcalloc(IBMVNIC_MAX_QUEUES,
John Allen3d52b592017-08-02 16:44:14 -0500447 sizeof(struct ibmvnic_rx_queue_stats),
448 GFP_KERNEL);
449 if (!adapter->rx_stats_buffers)
450 return -ENOMEM;
451
452 return 0;
453}
454
Nathan Fontenot7bbc27a2017-03-30 02:49:23 -0400455static void release_stats_token(struct ibmvnic_adapter *adapter)
456{
457 struct device *dev = &adapter->vdev->dev;
458
459 if (!adapter->stats_token)
460 return;
461
462 dma_unmap_single(dev, adapter->stats_token,
463 sizeof(struct ibmvnic_statistics),
464 DMA_FROM_DEVICE);
465 adapter->stats_token = 0;
466}
467
468static int init_stats_token(struct ibmvnic_adapter *adapter)
469{
470 struct device *dev = &adapter->vdev->dev;
471 dma_addr_t stok;
472
473 stok = dma_map_single(dev, &adapter->stats,
474 sizeof(struct ibmvnic_statistics),
475 DMA_FROM_DEVICE);
476 if (dma_mapping_error(dev, stok)) {
477 dev_err(dev, "Couldn't map stats buffer\n");
478 return -1;
479 }
480
481 adapter->stats_token = stok;
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500482 netdev_dbg(adapter->netdev, "Stats token initialized (%llx)\n", stok);
Nathan Fontenot7bbc27a2017-03-30 02:49:23 -0400483 return 0;
484}
485
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400486static int reset_rx_pools(struct ibmvnic_adapter *adapter)
487{
488 struct ibmvnic_rx_pool *rx_pool;
Thomas Falcon507ebe62020-08-21 13:39:01 -0500489 u64 buff_size;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400490 int rx_scrqs;
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500491 int i, j, rc;
John Allen896d8692018-01-18 16:26:31 -0600492
Mingming Cao9f134572020-08-25 13:26:41 -0400493 if (!adapter->rx_pool)
494 return -1;
495
Thomas Falcon507ebe62020-08-21 13:39:01 -0500496 buff_size = adapter->cur_rx_buf_sz;
497 rx_scrqs = adapter->num_active_rx_pools;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400498 for (i = 0; i < rx_scrqs; i++) {
499 rx_pool = &adapter->rx_pool[i];
500
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500501 netdev_dbg(adapter->netdev, "Re-setting rx_pool[%d]\n", i);
502
Thomas Falcon507ebe62020-08-21 13:39:01 -0500503 if (rx_pool->buff_size != buff_size) {
John Allen896d8692018-01-18 16:26:31 -0600504 free_long_term_buff(adapter, &rx_pool->long_term_buff);
Dwip N. Banerjee9a87c3f2020-11-18 19:12:22 -0600505 rx_pool->buff_size = ALIGN(buff_size, L1_CACHE_BYTES);
Thomas Falcon7c940b12019-06-07 16:03:55 -0500506 rc = alloc_long_term_buff(adapter,
507 &rx_pool->long_term_buff,
508 rx_pool->size *
509 rx_pool->buff_size);
John Allen896d8692018-01-18 16:26:31 -0600510 } else {
511 rc = reset_long_term_buff(adapter,
512 &rx_pool->long_term_buff);
513 }
514
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500515 if (rc)
516 return rc;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400517
518 for (j = 0; j < rx_pool->size; j++)
519 rx_pool->free_map[j] = j;
520
521 memset(rx_pool->rx_buff, 0,
522 rx_pool->size * sizeof(struct ibmvnic_rx_buff));
523
524 atomic_set(&rx_pool->available, 0);
525 rx_pool->next_alloc = 0;
526 rx_pool->next_free = 0;
Thomas Falconc3e53b92017-06-14 23:50:05 -0500527 rx_pool->active = 1;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400528 }
529
530 return 0;
531}
532
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400533static void release_rx_pools(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -0600534{
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400535 struct ibmvnic_rx_pool *rx_pool;
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400536 int i, j;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600537
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400538 if (!adapter->rx_pool)
Thomas Falcon032c5e82015-12-21 11:26:06 -0600539 return;
540
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600541 for (i = 0; i < adapter->num_active_rx_pools; i++) {
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400542 rx_pool = &adapter->rx_pool[i];
543
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500544 netdev_dbg(adapter->netdev, "Releasing rx_pool[%d]\n", i);
545
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400546 kfree(rx_pool->free_map);
547 free_long_term_buff(adapter, &rx_pool->long_term_buff);
548
549 if (!rx_pool->rx_buff)
Nathan Fontenote0ebe9422017-05-03 14:04:50 -0400550 continue;
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400551
552 for (j = 0; j < rx_pool->size; j++) {
553 if (rx_pool->rx_buff[j].skb) {
Thomas Falconb7cdec32018-11-21 11:17:58 -0600554 dev_kfree_skb_any(rx_pool->rx_buff[j].skb);
555 rx_pool->rx_buff[j].skb = NULL;
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400556 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600557 }
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400558
559 kfree(rx_pool->rx_buff);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600560 }
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400561
562 kfree(adapter->rx_pool);
563 adapter->rx_pool = NULL;
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600564 adapter->num_active_rx_pools = 0;
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400565}
566
567static int init_rx_pools(struct net_device *netdev)
568{
569 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
570 struct device *dev = &adapter->vdev->dev;
571 struct ibmvnic_rx_pool *rx_pool;
572 int rxadd_subcrqs;
Thomas Falcon507ebe62020-08-21 13:39:01 -0500573 u64 buff_size;
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400574 int i, j;
575
Thomas Falcon507ebe62020-08-21 13:39:01 -0500576 rxadd_subcrqs = adapter->num_active_rx_scrqs;
577 buff_size = adapter->cur_rx_buf_sz;
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400578
579 adapter->rx_pool = kcalloc(rxadd_subcrqs,
580 sizeof(struct ibmvnic_rx_pool),
581 GFP_KERNEL);
582 if (!adapter->rx_pool) {
583 dev_err(dev, "Failed to allocate rx pools\n");
584 return -1;
585 }
586
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600587 adapter->num_active_rx_pools = rxadd_subcrqs;
588
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400589 for (i = 0; i < rxadd_subcrqs; i++) {
590 rx_pool = &adapter->rx_pool[i];
591
592 netdev_dbg(adapter->netdev,
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500593 "Initializing rx_pool[%d], %lld buffs, %lld bytes each\n",
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400594 i, adapter->req_rx_add_entries_per_subcrq,
Thomas Falcon507ebe62020-08-21 13:39:01 -0500595 buff_size);
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400596
597 rx_pool->size = adapter->req_rx_add_entries_per_subcrq;
598 rx_pool->index = i;
Dwip N. Banerjee9a87c3f2020-11-18 19:12:22 -0600599 rx_pool->buff_size = ALIGN(buff_size, L1_CACHE_BYTES);
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400600 rx_pool->active = 1;
601
602 rx_pool->free_map = kcalloc(rx_pool->size, sizeof(int),
603 GFP_KERNEL);
604 if (!rx_pool->free_map) {
605 release_rx_pools(adapter);
606 return -1;
607 }
608
609 rx_pool->rx_buff = kcalloc(rx_pool->size,
610 sizeof(struct ibmvnic_rx_buff),
611 GFP_KERNEL);
612 if (!rx_pool->rx_buff) {
613 dev_err(dev, "Couldn't alloc rx buffers\n");
614 release_rx_pools(adapter);
615 return -1;
616 }
617
618 if (alloc_long_term_buff(adapter, &rx_pool->long_term_buff,
619 rx_pool->size * rx_pool->buff_size)) {
620 release_rx_pools(adapter);
621 return -1;
622 }
623
624 for (j = 0; j < rx_pool->size; ++j)
625 rx_pool->free_map[j] = j;
626
627 atomic_set(&rx_pool->available, 0);
628 rx_pool->next_alloc = 0;
629 rx_pool->next_free = 0;
630 }
631
632 return 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600633}
634
Thomas Falcone26dc252018-03-16 20:00:25 -0500635static int reset_one_tx_pool(struct ibmvnic_adapter *adapter,
636 struct ibmvnic_tx_pool *tx_pool)
637{
638 int rc, i;
639
640 rc = reset_long_term_buff(adapter, &tx_pool->long_term_buff);
641 if (rc)
642 return rc;
643
644 memset(tx_pool->tx_buff, 0,
645 tx_pool->num_buffers *
646 sizeof(struct ibmvnic_tx_buff));
647
648 for (i = 0; i < tx_pool->num_buffers; i++)
649 tx_pool->free_map[i] = i;
650
651 tx_pool->consumer_index = 0;
652 tx_pool->producer_index = 0;
653
654 return 0;
655}
656
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400657static int reset_tx_pools(struct ibmvnic_adapter *adapter)
658{
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400659 int tx_scrqs;
Thomas Falcone26dc252018-03-16 20:00:25 -0500660 int i, rc;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400661
Mingming Cao9f134572020-08-25 13:26:41 -0400662 if (!adapter->tx_pool)
663 return -1;
664
Thomas Falcon507ebe62020-08-21 13:39:01 -0500665 tx_scrqs = adapter->num_active_tx_pools;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400666 for (i = 0; i < tx_scrqs; i++) {
Thomas Falcone26dc252018-03-16 20:00:25 -0500667 rc = reset_one_tx_pool(adapter, &adapter->tso_pool[i]);
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500668 if (rc)
669 return rc;
Thomas Falcone26dc252018-03-16 20:00:25 -0500670 rc = reset_one_tx_pool(adapter, &adapter->tx_pool[i]);
Thomas Falconfdb06102017-10-17 12:36:55 -0500671 if (rc)
672 return rc;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400673 }
674
675 return 0;
676}
677
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -0200678static void release_vpd_data(struct ibmvnic_adapter *adapter)
679{
680 if (!adapter->vpd)
681 return;
682
683 kfree(adapter->vpd->buff);
684 kfree(adapter->vpd);
Thomas Falconb0992ec2018-02-06 17:25:23 -0600685
686 adapter->vpd = NULL;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -0200687}
688
Thomas Falconfb794212018-03-16 20:00:26 -0500689static void release_one_tx_pool(struct ibmvnic_adapter *adapter,
690 struct ibmvnic_tx_pool *tx_pool)
691{
692 kfree(tx_pool->tx_buff);
693 kfree(tx_pool->free_map);
694 free_long_term_buff(adapter, &tx_pool->long_term_buff);
695}
696
Nathan Fontenotc657e322017-03-30 02:49:06 -0400697static void release_tx_pools(struct ibmvnic_adapter *adapter)
698{
John Allen896d8692018-01-18 16:26:31 -0600699 int i;
Nathan Fontenotc657e322017-03-30 02:49:06 -0400700
701 if (!adapter->tx_pool)
702 return;
703
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600704 for (i = 0; i < adapter->num_active_tx_pools; i++) {
Thomas Falconfb794212018-03-16 20:00:26 -0500705 release_one_tx_pool(adapter, &adapter->tx_pool[i]);
706 release_one_tx_pool(adapter, &adapter->tso_pool[i]);
Nathan Fontenotc657e322017-03-30 02:49:06 -0400707 }
708
709 kfree(adapter->tx_pool);
710 adapter->tx_pool = NULL;
Thomas Falconfb794212018-03-16 20:00:26 -0500711 kfree(adapter->tso_pool);
712 adapter->tso_pool = NULL;
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600713 adapter->num_active_tx_pools = 0;
Nathan Fontenotc657e322017-03-30 02:49:06 -0400714}
715
Thomas Falcon32053062018-03-16 20:00:27 -0500716static int init_one_tx_pool(struct net_device *netdev,
717 struct ibmvnic_tx_pool *tx_pool,
718 int num_entries, int buf_size)
719{
720 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
721 int i;
722
723 tx_pool->tx_buff = kcalloc(num_entries,
724 sizeof(struct ibmvnic_tx_buff),
725 GFP_KERNEL);
726 if (!tx_pool->tx_buff)
727 return -1;
728
729 if (alloc_long_term_buff(adapter, &tx_pool->long_term_buff,
730 num_entries * buf_size))
731 return -1;
732
733 tx_pool->free_map = kcalloc(num_entries, sizeof(int), GFP_KERNEL);
734 if (!tx_pool->free_map)
735 return -1;
736
737 for (i = 0; i < num_entries; i++)
738 tx_pool->free_map[i] = i;
739
740 tx_pool->consumer_index = 0;
741 tx_pool->producer_index = 0;
742 tx_pool->num_buffers = num_entries;
743 tx_pool->buf_size = buf_size;
744
745 return 0;
746}
747
Nathan Fontenotc657e322017-03-30 02:49:06 -0400748static int init_tx_pools(struct net_device *netdev)
749{
750 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Nathan Fontenotc657e322017-03-30 02:49:06 -0400751 int tx_subcrqs;
Dwip N. Banerjee9a87c3f2020-11-18 19:12:22 -0600752 u64 buff_size;
Thomas Falcon32053062018-03-16 20:00:27 -0500753 int i, rc;
Nathan Fontenotc657e322017-03-30 02:49:06 -0400754
Thomas Falcon507ebe62020-08-21 13:39:01 -0500755 tx_subcrqs = adapter->num_active_tx_scrqs;
Nathan Fontenotc657e322017-03-30 02:49:06 -0400756 adapter->tx_pool = kcalloc(tx_subcrqs,
757 sizeof(struct ibmvnic_tx_pool), GFP_KERNEL);
758 if (!adapter->tx_pool)
759 return -1;
760
Thomas Falcon32053062018-03-16 20:00:27 -0500761 adapter->tso_pool = kcalloc(tx_subcrqs,
762 sizeof(struct ibmvnic_tx_pool), GFP_KERNEL);
763 if (!adapter->tso_pool)
764 return -1;
765
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600766 adapter->num_active_tx_pools = tx_subcrqs;
767
Nathan Fontenotc657e322017-03-30 02:49:06 -0400768 for (i = 0; i < tx_subcrqs; i++) {
Dwip N. Banerjee9a87c3f2020-11-18 19:12:22 -0600769 buff_size = adapter->req_mtu + VLAN_HLEN;
770 buff_size = ALIGN(buff_size, L1_CACHE_BYTES);
Thomas Falcon32053062018-03-16 20:00:27 -0500771 rc = init_one_tx_pool(netdev, &adapter->tx_pool[i],
772 adapter->req_tx_entries_per_subcrq,
Dwip N. Banerjee9a87c3f2020-11-18 19:12:22 -0600773 buff_size);
Thomas Falcon32053062018-03-16 20:00:27 -0500774 if (rc) {
Nathan Fontenotc657e322017-03-30 02:49:06 -0400775 release_tx_pools(adapter);
Thomas Falcon32053062018-03-16 20:00:27 -0500776 return rc;
Nathan Fontenotc657e322017-03-30 02:49:06 -0400777 }
778
Thomas Falcon7c940b12019-06-07 16:03:55 -0500779 rc = init_one_tx_pool(netdev, &adapter->tso_pool[i],
780 IBMVNIC_TSO_BUFS,
781 IBMVNIC_TSO_BUF_SZ);
Thomas Falcon32053062018-03-16 20:00:27 -0500782 if (rc) {
Nathan Fontenotc657e322017-03-30 02:49:06 -0400783 release_tx_pools(adapter);
Thomas Falcon32053062018-03-16 20:00:27 -0500784 return rc;
Nathan Fontenotc657e322017-03-30 02:49:06 -0400785 }
Nathan Fontenotc657e322017-03-30 02:49:06 -0400786 }
787
788 return 0;
789}
790
John Allend944c3d62017-05-26 10:30:13 -0400791static void ibmvnic_napi_enable(struct ibmvnic_adapter *adapter)
792{
793 int i;
794
795 if (adapter->napi_enabled)
796 return;
797
798 for (i = 0; i < adapter->req_rx_queues; i++)
799 napi_enable(&adapter->napi[i]);
800
801 adapter->napi_enabled = true;
802}
803
804static void ibmvnic_napi_disable(struct ibmvnic_adapter *adapter)
805{
806 int i;
807
808 if (!adapter->napi_enabled)
809 return;
810
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500811 for (i = 0; i < adapter->req_rx_queues; i++) {
812 netdev_dbg(adapter->netdev, "Disabling napi[%d]\n", i);
John Allend944c3d62017-05-26 10:30:13 -0400813 napi_disable(&adapter->napi[i]);
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500814 }
John Allend944c3d62017-05-26 10:30:13 -0400815
816 adapter->napi_enabled = false;
817}
818
Nathan Fontenot86f669b2018-02-19 13:30:39 -0600819static int init_napi(struct ibmvnic_adapter *adapter)
820{
821 int i;
822
823 adapter->napi = kcalloc(adapter->req_rx_queues,
824 sizeof(struct napi_struct), GFP_KERNEL);
825 if (!adapter->napi)
826 return -ENOMEM;
827
828 for (i = 0; i < adapter->req_rx_queues; i++) {
829 netdev_dbg(adapter->netdev, "Adding napi[%d]\n", i);
830 netif_napi_add(adapter->netdev, &adapter->napi[i],
831 ibmvnic_poll, NAPI_POLL_WEIGHT);
832 }
833
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600834 adapter->num_active_rx_napi = adapter->req_rx_queues;
Nathan Fontenot86f669b2018-02-19 13:30:39 -0600835 return 0;
836}
837
838static void release_napi(struct ibmvnic_adapter *adapter)
839{
840 int i;
841
842 if (!adapter->napi)
843 return;
844
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600845 for (i = 0; i < adapter->num_active_rx_napi; i++) {
Wen Yang390de192018-12-11 12:20:46 +0800846 netdev_dbg(adapter->netdev, "Releasing napi[%d]\n", i);
847 netif_napi_del(&adapter->napi[i]);
Nathan Fontenot86f669b2018-02-19 13:30:39 -0600848 }
849
850 kfree(adapter->napi);
851 adapter->napi = NULL;
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600852 adapter->num_active_rx_napi = 0;
Thomas Falconc3f22412018-05-23 13:37:55 -0500853 adapter->napi_enabled = false;
Nathan Fontenot86f669b2018-02-19 13:30:39 -0600854}
855
John Allena57a5d22017-03-17 17:13:41 -0500856static int ibmvnic_login(struct net_device *netdev)
Thomas Falcon032c5e82015-12-21 11:26:06 -0600857{
858 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Dany Madden98c41f02020-11-25 18:04:32 -0600859 unsigned long timeout = msecs_to_jiffies(20000);
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500860 int retry_count = 0;
Thomas Falcondff515a32020-06-15 10:29:23 -0500861 int retries = 10;
Thomas Falconeb110412018-05-24 14:37:53 -0500862 bool retry;
Thomas Falcon4d96f122017-08-01 15:04:36 -0500863 int rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600864
John Allenbd0b6722017-03-17 17:13:40 -0500865 do {
Thomas Falconeb110412018-05-24 14:37:53 -0500866 retry = false;
Thomas Falcondff515a32020-06-15 10:29:23 -0500867 if (retry_count > retries) {
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500868 netdev_warn(netdev, "Login attempts exceeded\n");
869 return -1;
870 }
871
872 adapter->init_done_rc = 0;
873 reinit_completion(&adapter->init_done);
874 rc = send_login(adapter);
Dany Maddenc98d9cc2020-11-25 18:04:30 -0600875 if (rc)
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500876 return rc;
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500877
878 if (!wait_for_completion_timeout(&adapter->init_done,
879 timeout)) {
Thomas Falcondff515a32020-06-15 10:29:23 -0500880 netdev_warn(netdev, "Login timed out, retrying...\n");
881 retry = true;
882 adapter->init_done_rc = 0;
883 retry_count++;
884 continue;
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500885 }
886
Thomas Falcondff515a32020-06-15 10:29:23 -0500887 if (adapter->init_done_rc == ABORTED) {
888 netdev_warn(netdev, "Login aborted, retrying...\n");
889 retry = true;
890 adapter->init_done_rc = 0;
891 retry_count++;
892 /* FW or device may be busy, so
893 * wait a bit before retrying login
894 */
895 msleep(500);
896 } else if (adapter->init_done_rc == PARTIALSUCCESS) {
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500897 retry_count++;
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -0600898 release_sub_crqs(adapter, 1);
John Allenbd0b6722017-03-17 17:13:40 -0500899
Thomas Falconeb110412018-05-24 14:37:53 -0500900 retry = true;
901 netdev_dbg(netdev,
902 "Received partial success, retrying...\n");
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500903 adapter->init_done_rc = 0;
John Allenbd0b6722017-03-17 17:13:40 -0500904 reinit_completion(&adapter->init_done);
Lijun Pan491099a2020-09-27 20:13:26 -0500905 send_query_cap(adapter);
John Allenbd0b6722017-03-17 17:13:40 -0500906 if (!wait_for_completion_timeout(&adapter->init_done,
907 timeout)) {
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500908 netdev_warn(netdev,
909 "Capabilities query timed out\n");
John Allenbd0b6722017-03-17 17:13:40 -0500910 return -1;
911 }
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500912
Thomas Falcon4d96f122017-08-01 15:04:36 -0500913 rc = init_sub_crqs(adapter);
914 if (rc) {
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500915 netdev_warn(netdev,
916 "SCRQ initialization failed\n");
Thomas Falcon4d96f122017-08-01 15:04:36 -0500917 return -1;
918 }
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500919
Thomas Falcon4d96f122017-08-01 15:04:36 -0500920 rc = init_sub_crq_irqs(adapter);
921 if (rc) {
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500922 netdev_warn(netdev,
923 "SCRQ irq initialization failed\n");
Thomas Falcon4d96f122017-08-01 15:04:36 -0500924 return -1;
925 }
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500926 } else if (adapter->init_done_rc) {
927 netdev_warn(netdev, "Adapter login failed\n");
John Allenbd0b6722017-03-17 17:13:40 -0500928 return -1;
929 }
Thomas Falconeb110412018-05-24 14:37:53 -0500930 } while (retry);
John Allenbd0b6722017-03-17 17:13:40 -0500931
Thomas Falcon62740e92019-05-09 23:13:43 -0500932 __ibmvnic_set_mac(netdev, adapter->mac_addr);
Thomas Falcon3d166132018-01-10 19:39:52 -0600933
Sukadev Bhattiprolu38bd5ce2020-12-04 18:22:35 -0800934 netdev_dbg(netdev, "[S:%d] Login succeeded\n", adapter->state);
John Allena57a5d22017-03-17 17:13:41 -0500935 return 0;
936}
937
Thomas Falcon34f0f4e2018-02-13 18:23:40 -0600938static void release_login_buffer(struct ibmvnic_adapter *adapter)
939{
940 kfree(adapter->login_buf);
941 adapter->login_buf = NULL;
942}
943
944static void release_login_rsp_buffer(struct ibmvnic_adapter *adapter)
945{
946 kfree(adapter->login_rsp_buf);
947 adapter->login_rsp_buf = NULL;
948}
949
Nathan Fontenot1b8955e2017-03-30 02:49:29 -0400950static void release_resources(struct ibmvnic_adapter *adapter)
951{
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -0200952 release_vpd_data(adapter);
953
Nathan Fontenot1b8955e2017-03-30 02:49:29 -0400954 release_tx_pools(adapter);
955 release_rx_pools(adapter);
956
Nathan Fontenot86f669b2018-02-19 13:30:39 -0600957 release_napi(adapter);
Lijun Pana0c8be52020-12-19 15:39:19 -0600958 release_login_buffer(adapter);
Thomas Falcon34f0f4e2018-02-13 18:23:40 -0600959 release_login_rsp_buffer(adapter);
Nathan Fontenot1b8955e2017-03-30 02:49:29 -0400960}
961
Nathan Fontenot53da09e2017-04-21 15:39:04 -0400962static int set_link_state(struct ibmvnic_adapter *adapter, u8 link_state)
963{
964 struct net_device *netdev = adapter->netdev;
Dany Madden98c41f02020-11-25 18:04:32 -0600965 unsigned long timeout = msecs_to_jiffies(20000);
Nathan Fontenot53da09e2017-04-21 15:39:04 -0400966 union ibmvnic_crq crq;
967 bool resend;
968 int rc;
969
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500970 netdev_dbg(netdev, "setting link state %d\n", link_state);
971
Nathan Fontenot53da09e2017-04-21 15:39:04 -0400972 memset(&crq, 0, sizeof(crq));
973 crq.logical_link_state.first = IBMVNIC_CRQ_CMD;
974 crq.logical_link_state.cmd = LOGICAL_LINK_STATE;
975 crq.logical_link_state.link_state = link_state;
976
977 do {
978 resend = false;
979
980 reinit_completion(&adapter->init_done);
981 rc = ibmvnic_send_crq(adapter, &crq);
982 if (rc) {
983 netdev_err(netdev, "Failed to set link state\n");
984 return rc;
985 }
986
987 if (!wait_for_completion_timeout(&adapter->init_done,
988 timeout)) {
989 netdev_err(netdev, "timeout setting link state\n");
990 return -1;
991 }
992
Lijun Pan4c5f6af2020-08-19 17:52:23 -0500993 if (adapter->init_done_rc == PARTIALSUCCESS) {
Nathan Fontenot53da09e2017-04-21 15:39:04 -0400994 /* Partuial success, delay and re-send */
995 mdelay(1000);
996 resend = true;
Thomas Falconab5ec332018-05-23 13:37:59 -0500997 } else if (adapter->init_done_rc) {
998 netdev_warn(netdev, "Unable to set link state, rc=%d\n",
999 adapter->init_done_rc);
1000 return adapter->init_done_rc;
Nathan Fontenot53da09e2017-04-21 15:39:04 -04001001 }
1002 } while (resend);
1003
1004 return 0;
1005}
1006
Thomas Falcon7f3c6e62017-04-21 15:38:40 -04001007static int set_real_num_queues(struct net_device *netdev)
1008{
1009 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1010 int rc;
1011
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001012 netdev_dbg(netdev, "Setting real tx/rx queues (%llx/%llx)\n",
1013 adapter->req_tx_queues, adapter->req_rx_queues);
1014
Thomas Falcon7f3c6e62017-04-21 15:38:40 -04001015 rc = netif_set_real_num_tx_queues(netdev, adapter->req_tx_queues);
1016 if (rc) {
1017 netdev_err(netdev, "failed to set the number of tx queues\n");
1018 return rc;
1019 }
1020
1021 rc = netif_set_real_num_rx_queues(netdev, adapter->req_rx_queues);
1022 if (rc)
1023 netdev_err(netdev, "failed to set the number of rx queues\n");
1024
1025 return rc;
1026}
1027
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001028static int ibmvnic_get_vpd(struct ibmvnic_adapter *adapter)
1029{
1030 struct device *dev = &adapter->vdev->dev;
1031 union ibmvnic_crq crq;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001032 int len = 0;
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05001033 int rc;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001034
1035 if (adapter->vpd->buff)
1036 len = adapter->vpd->len;
1037
Thomas Falconff25dcb2019-11-25 17:12:56 -06001038 mutex_lock(&adapter->fw_lock);
1039 adapter->fw_done_rc = 0;
Thomas Falcon070eca92019-11-25 17:12:53 -06001040 reinit_completion(&adapter->fw_done);
Thomas Falconff25dcb2019-11-25 17:12:56 -06001041
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001042 crq.get_vpd_size.first = IBMVNIC_CRQ_CMD;
1043 crq.get_vpd_size.cmd = GET_VPD_SIZE;
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05001044 rc = ibmvnic_send_crq(adapter, &crq);
Thomas Falconff25dcb2019-11-25 17:12:56 -06001045 if (rc) {
1046 mutex_unlock(&adapter->fw_lock);
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05001047 return rc;
Thomas Falconff25dcb2019-11-25 17:12:56 -06001048 }
Thomas Falcon476d96c2019-11-25 17:12:55 -06001049
1050 rc = ibmvnic_wait_for_completion(adapter, &adapter->fw_done, 10000);
1051 if (rc) {
1052 dev_err(dev, "Could not retrieve VPD size, rc = %d\n", rc);
Thomas Falconff25dcb2019-11-25 17:12:56 -06001053 mutex_unlock(&adapter->fw_lock);
Thomas Falcon476d96c2019-11-25 17:12:55 -06001054 return rc;
1055 }
Thomas Falconff25dcb2019-11-25 17:12:56 -06001056 mutex_unlock(&adapter->fw_lock);
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001057
1058 if (!adapter->vpd->len)
1059 return -ENODATA;
1060
1061 if (!adapter->vpd->buff)
1062 adapter->vpd->buff = kzalloc(adapter->vpd->len, GFP_KERNEL);
1063 else if (adapter->vpd->len != len)
1064 adapter->vpd->buff =
1065 krealloc(adapter->vpd->buff,
1066 adapter->vpd->len, GFP_KERNEL);
1067
1068 if (!adapter->vpd->buff) {
1069 dev_err(dev, "Could allocate VPD buffer\n");
1070 return -ENOMEM;
1071 }
1072
1073 adapter->vpd->dma_addr =
1074 dma_map_single(dev, adapter->vpd->buff, adapter->vpd->len,
1075 DMA_FROM_DEVICE);
Desnes Augusto Nunes do Rosariof7431062017-11-17 09:09:04 -02001076 if (dma_mapping_error(dev, adapter->vpd->dma_addr)) {
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001077 dev_err(dev, "Could not map VPD buffer\n");
1078 kfree(adapter->vpd->buff);
Thomas Falconb0992ec2018-02-06 17:25:23 -06001079 adapter->vpd->buff = NULL;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001080 return -ENOMEM;
1081 }
1082
Thomas Falconff25dcb2019-11-25 17:12:56 -06001083 mutex_lock(&adapter->fw_lock);
1084 adapter->fw_done_rc = 0;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001085 reinit_completion(&adapter->fw_done);
Thomas Falconff25dcb2019-11-25 17:12:56 -06001086
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001087 crq.get_vpd.first = IBMVNIC_CRQ_CMD;
1088 crq.get_vpd.cmd = GET_VPD;
1089 crq.get_vpd.ioba = cpu_to_be32(adapter->vpd->dma_addr);
1090 crq.get_vpd.len = cpu_to_be32((u32)adapter->vpd->len);
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05001091 rc = ibmvnic_send_crq(adapter, &crq);
1092 if (rc) {
1093 kfree(adapter->vpd->buff);
1094 adapter->vpd->buff = NULL;
Thomas Falconff25dcb2019-11-25 17:12:56 -06001095 mutex_unlock(&adapter->fw_lock);
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05001096 return rc;
1097 }
Thomas Falcon476d96c2019-11-25 17:12:55 -06001098
1099 rc = ibmvnic_wait_for_completion(adapter, &adapter->fw_done, 10000);
1100 if (rc) {
1101 dev_err(dev, "Unable to retrieve VPD, rc = %d\n", rc);
1102 kfree(adapter->vpd->buff);
1103 adapter->vpd->buff = NULL;
Thomas Falconff25dcb2019-11-25 17:12:56 -06001104 mutex_unlock(&adapter->fw_lock);
Thomas Falcon476d96c2019-11-25 17:12:55 -06001105 return rc;
1106 }
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001107
Thomas Falconff25dcb2019-11-25 17:12:56 -06001108 mutex_unlock(&adapter->fw_lock);
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001109 return 0;
1110}
1111
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001112static int init_resources(struct ibmvnic_adapter *adapter)
John Allena57a5d22017-03-17 17:13:41 -05001113{
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001114 struct net_device *netdev = adapter->netdev;
Nathan Fontenot86f669b2018-02-19 13:30:39 -06001115 int rc;
John Allena57a5d22017-03-17 17:13:41 -05001116
Thomas Falcon7f3c6e62017-04-21 15:38:40 -04001117 rc = set_real_num_queues(netdev);
1118 if (rc)
1119 return rc;
John Allenbd0b6722017-03-17 17:13:40 -05001120
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001121 adapter->vpd = kzalloc(sizeof(*adapter->vpd), GFP_KERNEL);
1122 if (!adapter->vpd)
1123 return -ENOMEM;
1124
John Allen69d08dc2018-01-18 16:27:58 -06001125 /* Vital Product Data (VPD) */
1126 rc = ibmvnic_get_vpd(adapter);
1127 if (rc) {
1128 netdev_err(netdev, "failed to initialize Vital Product Data (VPD)\n");
1129 return rc;
1130 }
1131
Thomas Falcon032c5e82015-12-21 11:26:06 -06001132 adapter->map_id = 1;
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001133
Nathan Fontenot86f669b2018-02-19 13:30:39 -06001134 rc = init_napi(adapter);
1135 if (rc)
1136 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001137
Lijun Pan69980d02020-09-27 20:13:28 -05001138 send_query_map(adapter);
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -04001139
1140 rc = init_rx_pools(netdev);
1141 if (rc)
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001142 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001143
Nathan Fontenotc657e322017-03-30 02:49:06 -04001144 rc = init_tx_pools(netdev);
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001145 return rc;
1146}
1147
Nathan Fontenoted651a12017-05-03 14:04:38 -04001148static int __ibmvnic_open(struct net_device *netdev)
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001149{
1150 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001151 enum vnic_state prev_state = adapter->state;
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001152 int i, rc;
1153
Nathan Fontenot90c80142017-05-03 14:04:32 -04001154 adapter->state = VNIC_OPENING;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001155 replenish_pools(adapter);
John Allend944c3d62017-05-26 10:30:13 -04001156 ibmvnic_napi_enable(adapter);
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001157
Thomas Falcon032c5e82015-12-21 11:26:06 -06001158 /* We're ready to receive frames, enable the sub-crq interrupts and
1159 * set the logical link state to up
1160 */
Nathan Fontenoted651a12017-05-03 14:04:38 -04001161 for (i = 0; i < adapter->req_rx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001162 netdev_dbg(netdev, "Enabling rx_scrq[%d] irq\n", i);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001163 if (prev_state == VNIC_CLOSED)
1164 enable_irq(adapter->rx_scrq[i]->irq);
Thomas Falconf23e0642018-04-15 18:53:36 -05001165 enable_scrq_irq(adapter, adapter->rx_scrq[i]);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001166 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001167
Nathan Fontenoted651a12017-05-03 14:04:38 -04001168 for (i = 0; i < adapter->req_tx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001169 netdev_dbg(netdev, "Enabling tx_scrq[%d] irq\n", i);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001170 if (prev_state == VNIC_CLOSED)
1171 enable_irq(adapter->tx_scrq[i]->irq);
Thomas Falconf23e0642018-04-15 18:53:36 -05001172 enable_scrq_irq(adapter, adapter->tx_scrq[i]);
Thomas Falcon0d973382020-11-18 19:12:19 -06001173 netdev_tx_reset_queue(netdev_get_tx_queue(netdev, i));
Nathan Fontenoted651a12017-05-03 14:04:38 -04001174 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001175
Nathan Fontenot53da09e2017-04-21 15:39:04 -04001176 rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_UP);
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001177 if (rc) {
1178 for (i = 0; i < adapter->req_rx_queues; i++)
1179 napi_disable(&adapter->napi[i]);
1180 release_resources(adapter);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001181 return rc;
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001182 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001183
Nathan Fontenoted651a12017-05-03 14:04:38 -04001184 netif_tx_start_all_queues(netdev);
1185
1186 if (prev_state == VNIC_CLOSED) {
1187 for (i = 0; i < adapter->req_rx_queues; i++)
1188 napi_schedule(&adapter->napi[i]);
1189 }
1190
1191 adapter->state = VNIC_OPEN;
1192 return rc;
1193}
1194
1195static int ibmvnic_open(struct net_device *netdev)
1196{
1197 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
John Allen69d08dc2018-01-18 16:27:58 -06001198 int rc;
Nathan Fontenoted651a12017-05-03 14:04:38 -04001199
Thomas Falcon5a18e1e2018-04-06 18:37:05 -05001200 /* If device failover is pending, just set device state and return.
1201 * Device operation will be handled by reset routine.
1202 */
1203 if (adapter->failover_pending) {
1204 adapter->state = VNIC_OPEN;
1205 return 0;
1206 }
1207
Nathan Fontenoted651a12017-05-03 14:04:38 -04001208 if (adapter->state != VNIC_CLOSED) {
1209 rc = ibmvnic_login(netdev);
Juliet Kima5681e22018-11-19 15:59:22 -06001210 if (rc)
Sukadev Bhattiprolu1d850492020-10-30 10:07:11 -07001211 goto out;
Nathan Fontenoted651a12017-05-03 14:04:38 -04001212
1213 rc = init_resources(adapter);
1214 if (rc) {
1215 netdev_err(netdev, "failed to initialize resources\n");
1216 release_resources(adapter);
Sukadev Bhattiprolu1d850492020-10-30 10:07:11 -07001217 goto out;
Nathan Fontenoted651a12017-05-03 14:04:38 -04001218 }
1219 }
1220
1221 rc = __ibmvnic_open(netdev);
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001222
Sukadev Bhattiprolu1d850492020-10-30 10:07:11 -07001223out:
1224 /*
1225 * If open fails due to a pending failover, set device state and
1226 * return. Device operation will be handled by reset routine.
1227 */
1228 if (rc && adapter->failover_pending) {
1229 adapter->state = VNIC_OPEN;
1230 rc = 0;
1231 }
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001232 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001233}
1234
Thomas Falcond0869c02018-02-13 18:23:43 -06001235static void clean_rx_pools(struct ibmvnic_adapter *adapter)
1236{
1237 struct ibmvnic_rx_pool *rx_pool;
Thomas Falcon637f81d2018-02-26 18:10:57 -06001238 struct ibmvnic_rx_buff *rx_buff;
Thomas Falcond0869c02018-02-13 18:23:43 -06001239 u64 rx_entries;
1240 int rx_scrqs;
1241 int i, j;
1242
1243 if (!adapter->rx_pool)
1244 return;
1245
Thomas Falcon660e3092018-04-20 14:25:32 -05001246 rx_scrqs = adapter->num_active_rx_pools;
Thomas Falcond0869c02018-02-13 18:23:43 -06001247 rx_entries = adapter->req_rx_add_entries_per_subcrq;
1248
1249 /* Free any remaining skbs in the rx buffer pools */
1250 for (i = 0; i < rx_scrqs; i++) {
1251 rx_pool = &adapter->rx_pool[i];
Thomas Falcon637f81d2018-02-26 18:10:57 -06001252 if (!rx_pool || !rx_pool->rx_buff)
Thomas Falcond0869c02018-02-13 18:23:43 -06001253 continue;
1254
1255 netdev_dbg(adapter->netdev, "Cleaning rx_pool[%d]\n", i);
1256 for (j = 0; j < rx_entries; j++) {
Thomas Falcon637f81d2018-02-26 18:10:57 -06001257 rx_buff = &rx_pool->rx_buff[j];
1258 if (rx_buff && rx_buff->skb) {
1259 dev_kfree_skb_any(rx_buff->skb);
1260 rx_buff->skb = NULL;
Thomas Falcond0869c02018-02-13 18:23:43 -06001261 }
1262 }
1263 }
1264}
1265
Thomas Falcone9e1e972018-03-16 20:00:30 -05001266static void clean_one_tx_pool(struct ibmvnic_adapter *adapter,
1267 struct ibmvnic_tx_pool *tx_pool)
Nathan Fontenotb41b83e2017-05-03 14:04:56 -04001268{
Thomas Falcon637f81d2018-02-26 18:10:57 -06001269 struct ibmvnic_tx_buff *tx_buff;
Nathan Fontenotb41b83e2017-05-03 14:04:56 -04001270 u64 tx_entries;
Thomas Falcone9e1e972018-03-16 20:00:30 -05001271 int i;
Nathan Fontenotb41b83e2017-05-03 14:04:56 -04001272
Dan Carpenter050e85c2018-03-23 14:36:15 +03001273 if (!tx_pool || !tx_pool->tx_buff)
Thomas Falcone9e1e972018-03-16 20:00:30 -05001274 return;
1275
1276 tx_entries = tx_pool->num_buffers;
1277
1278 for (i = 0; i < tx_entries; i++) {
1279 tx_buff = &tx_pool->tx_buff[i];
1280 if (tx_buff && tx_buff->skb) {
1281 dev_kfree_skb_any(tx_buff->skb);
1282 tx_buff->skb = NULL;
1283 }
1284 }
1285}
1286
1287static void clean_tx_pools(struct ibmvnic_adapter *adapter)
1288{
1289 int tx_scrqs;
1290 int i;
1291
1292 if (!adapter->tx_pool || !adapter->tso_pool)
Nathan Fontenotb41b83e2017-05-03 14:04:56 -04001293 return;
1294
Thomas Falcon660e3092018-04-20 14:25:32 -05001295 tx_scrqs = adapter->num_active_tx_pools;
Nathan Fontenotb41b83e2017-05-03 14:04:56 -04001296
1297 /* Free any remaining skbs in the tx buffer pools */
1298 for (i = 0; i < tx_scrqs; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001299 netdev_dbg(adapter->netdev, "Cleaning tx_pool[%d]\n", i);
Thomas Falcone9e1e972018-03-16 20:00:30 -05001300 clean_one_tx_pool(adapter, &adapter->tx_pool[i]);
1301 clean_one_tx_pool(adapter, &adapter->tso_pool[i]);
Nathan Fontenotb41b83e2017-05-03 14:04:56 -04001302 }
1303}
1304
John Allen6095e592018-03-30 13:44:21 -05001305static void ibmvnic_disable_irqs(struct ibmvnic_adapter *adapter)
John Allenea5509f2017-03-17 17:13:43 -05001306{
John Allen6095e592018-03-30 13:44:21 -05001307 struct net_device *netdev = adapter->netdev;
John Allenea5509f2017-03-17 17:13:43 -05001308 int i;
1309
Nathan Fontenot46293b92017-05-03 14:05:02 -04001310 if (adapter->tx_scrq) {
1311 for (i = 0; i < adapter->req_tx_queues; i++)
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001312 if (adapter->tx_scrq[i]->irq) {
Thomas Falconf8738662018-03-07 17:51:45 -06001313 netdev_dbg(netdev,
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001314 "Disabling tx_scrq[%d] irq\n", i);
Thomas Falconf23e0642018-04-15 18:53:36 -05001315 disable_scrq_irq(adapter, adapter->tx_scrq[i]);
Nathan Fontenot46293b92017-05-03 14:05:02 -04001316 disable_irq(adapter->tx_scrq[i]->irq);
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001317 }
Nathan Fontenot46293b92017-05-03 14:05:02 -04001318 }
1319
Nathan Fontenot46293b92017-05-03 14:05:02 -04001320 if (adapter->rx_scrq) {
1321 for (i = 0; i < adapter->req_rx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001322 if (adapter->rx_scrq[i]->irq) {
Thomas Falconf8738662018-03-07 17:51:45 -06001323 netdev_dbg(netdev,
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001324 "Disabling rx_scrq[%d] irq\n", i);
Thomas Falconf23e0642018-04-15 18:53:36 -05001325 disable_scrq_irq(adapter, adapter->rx_scrq[i]);
Nathan Fontenot46293b92017-05-03 14:05:02 -04001326 disable_irq(adapter->rx_scrq[i]->irq);
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001327 }
Nathan Fontenot46293b92017-05-03 14:05:02 -04001328 }
1329 }
John Allen6095e592018-03-30 13:44:21 -05001330}
1331
1332static void ibmvnic_cleanup(struct net_device *netdev)
1333{
1334 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1335
1336 /* ensure that transmissions are stopped if called by do_reset */
Juliet Kim7ed5b312019-09-20 16:11:23 -04001337 if (test_bit(0, &adapter->resetting))
John Allen6095e592018-03-30 13:44:21 -05001338 netif_tx_disable(netdev);
1339 else
1340 netif_tx_stop_all_queues(netdev);
1341
1342 ibmvnic_napi_disable(adapter);
1343 ibmvnic_disable_irqs(adapter);
1344
Thomas Falcond0869c02018-02-13 18:23:43 -06001345 clean_rx_pools(adapter);
Thomas Falcon10f76212017-05-26 10:30:31 -04001346 clean_tx_pools(adapter);
Thomas Falcon01d9bd72018-03-07 17:51:46 -06001347}
1348
1349static int __ibmvnic_close(struct net_device *netdev)
1350{
1351 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1352 int rc = 0;
1353
1354 adapter->state = VNIC_CLOSING;
1355 rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_DN);
1356 if (rc)
1357 return rc;
Nathan Fontenot90c80142017-05-03 14:04:32 -04001358 adapter->state = VNIC_CLOSED;
Thomas Falcon01d9bd72018-03-07 17:51:46 -06001359 return 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001360}
1361
Nathan Fontenoted651a12017-05-03 14:04:38 -04001362static int ibmvnic_close(struct net_device *netdev)
1363{
1364 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1365 int rc;
1366
Sukadev Bhattiprolu38bd5ce2020-12-04 18:22:35 -08001367 netdev_dbg(netdev, "[S:%d FOP:%d FRR:%d] Closing\n",
1368 adapter->state, adapter->failover_pending,
1369 adapter->force_reset_recovery);
1370
Thomas Falcon5a18e1e2018-04-06 18:37:05 -05001371 /* If device failover is pending, just set device state and return.
1372 * Device operation will be handled by reset routine.
1373 */
1374 if (adapter->failover_pending) {
1375 adapter->state = VNIC_CLOSED;
1376 return 0;
1377 }
1378
Nathan Fontenoted651a12017-05-03 14:04:38 -04001379 rc = __ibmvnic_close(netdev);
Nathan Fontenot30f79622018-04-06 18:37:06 -05001380 ibmvnic_cleanup(netdev);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001381
1382 return rc;
1383}
1384
Thomas Falconad7775d2016-04-01 17:20:34 -05001385/**
1386 * build_hdr_data - creates L2/L3/L4 header data buffer
Lee Jones80708602021-01-15 20:09:03 +00001387 * @hdr_field: bitfield determining needed headers
1388 * @skb: socket buffer
1389 * @hdr_len: array of header lengths
1390 * @hdr_data: buffer to write the header to
Thomas Falconad7775d2016-04-01 17:20:34 -05001391 *
1392 * Reads hdr_field to determine which headers are needed by firmware.
1393 * Builds a buffer containing these headers. Saves individual header
1394 * lengths and total buffer length to be used to build descriptors.
1395 */
1396static int build_hdr_data(u8 hdr_field, struct sk_buff *skb,
1397 int *hdr_len, u8 *hdr_data)
1398{
1399 int len = 0;
1400 u8 *hdr;
1401
Thomas Falconda75e3b2018-03-12 11:51:02 -05001402 if (skb_vlan_tagged(skb) && !skb_vlan_tag_present(skb))
1403 hdr_len[0] = sizeof(struct vlan_ethhdr);
1404 else
1405 hdr_len[0] = sizeof(struct ethhdr);
Thomas Falconad7775d2016-04-01 17:20:34 -05001406
1407 if (skb->protocol == htons(ETH_P_IP)) {
1408 hdr_len[1] = ip_hdr(skb)->ihl * 4;
1409 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
1410 hdr_len[2] = tcp_hdrlen(skb);
1411 else if (ip_hdr(skb)->protocol == IPPROTO_UDP)
1412 hdr_len[2] = sizeof(struct udphdr);
1413 } else if (skb->protocol == htons(ETH_P_IPV6)) {
1414 hdr_len[1] = sizeof(struct ipv6hdr);
1415 if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
1416 hdr_len[2] = tcp_hdrlen(skb);
1417 else if (ipv6_hdr(skb)->nexthdr == IPPROTO_UDP)
1418 hdr_len[2] = sizeof(struct udphdr);
Thomas Falcon4eb50ce2017-12-18 12:52:40 -06001419 } else if (skb->protocol == htons(ETH_P_ARP)) {
1420 hdr_len[1] = arp_hdr_len(skb->dev);
1421 hdr_len[2] = 0;
Thomas Falconad7775d2016-04-01 17:20:34 -05001422 }
1423
1424 memset(hdr_data, 0, 120);
1425 if ((hdr_field >> 6) & 1) {
1426 hdr = skb_mac_header(skb);
1427 memcpy(hdr_data, hdr, hdr_len[0]);
1428 len += hdr_len[0];
1429 }
1430
1431 if ((hdr_field >> 5) & 1) {
1432 hdr = skb_network_header(skb);
1433 memcpy(hdr_data + len, hdr, hdr_len[1]);
1434 len += hdr_len[1];
1435 }
1436
1437 if ((hdr_field >> 4) & 1) {
1438 hdr = skb_transport_header(skb);
1439 memcpy(hdr_data + len, hdr, hdr_len[2]);
1440 len += hdr_len[2];
1441 }
1442 return len;
1443}
1444
1445/**
1446 * create_hdr_descs - create header and header extension descriptors
Lee Jones80708602021-01-15 20:09:03 +00001447 * @hdr_field: bitfield determining needed headers
1448 * @hdr_data: buffer containing header data
1449 * @len: length of data buffer
1450 * @hdr_len: array of individual header lengths
1451 * @scrq_arr: descriptor array
Thomas Falconad7775d2016-04-01 17:20:34 -05001452 *
1453 * Creates header and, if needed, header extension descriptors and
1454 * places them in a descriptor array, scrq_arr
1455 */
1456
Thomas Falcon2de09682017-10-16 10:02:11 -05001457static int create_hdr_descs(u8 hdr_field, u8 *hdr_data, int len, int *hdr_len,
1458 union sub_crq *scrq_arr)
Thomas Falconad7775d2016-04-01 17:20:34 -05001459{
1460 union sub_crq hdr_desc;
1461 int tmp_len = len;
Thomas Falcon2de09682017-10-16 10:02:11 -05001462 int num_descs = 0;
Thomas Falconad7775d2016-04-01 17:20:34 -05001463 u8 *data, *cur;
1464 int tmp;
1465
1466 while (tmp_len > 0) {
1467 cur = hdr_data + len - tmp_len;
1468
1469 memset(&hdr_desc, 0, sizeof(hdr_desc));
1470 if (cur != hdr_data) {
1471 data = hdr_desc.hdr_ext.data;
1472 tmp = tmp_len > 29 ? 29 : tmp_len;
1473 hdr_desc.hdr_ext.first = IBMVNIC_CRQ_CMD;
1474 hdr_desc.hdr_ext.type = IBMVNIC_HDR_EXT_DESC;
1475 hdr_desc.hdr_ext.len = tmp;
1476 } else {
1477 data = hdr_desc.hdr.data;
1478 tmp = tmp_len > 24 ? 24 : tmp_len;
1479 hdr_desc.hdr.first = IBMVNIC_CRQ_CMD;
1480 hdr_desc.hdr.type = IBMVNIC_HDR_DESC;
1481 hdr_desc.hdr.len = tmp;
1482 hdr_desc.hdr.l2_len = (u8)hdr_len[0];
1483 hdr_desc.hdr.l3_len = cpu_to_be16((u16)hdr_len[1]);
1484 hdr_desc.hdr.l4_len = (u8)hdr_len[2];
1485 hdr_desc.hdr.flag = hdr_field << 1;
1486 }
1487 memcpy(data, cur, tmp);
1488 tmp_len -= tmp;
1489 *scrq_arr = hdr_desc;
1490 scrq_arr++;
Thomas Falcon2de09682017-10-16 10:02:11 -05001491 num_descs++;
Thomas Falconad7775d2016-04-01 17:20:34 -05001492 }
Thomas Falcon2de09682017-10-16 10:02:11 -05001493
1494 return num_descs;
Thomas Falconad7775d2016-04-01 17:20:34 -05001495}
1496
1497/**
1498 * build_hdr_descs_arr - build a header descriptor array
Lee Jones80708602021-01-15 20:09:03 +00001499 * @txbuff: tx buffer
1500 * @num_entries: number of descriptors to be sent
1501 * @hdr_field: bit field determining which headers will be sent
Thomas Falconad7775d2016-04-01 17:20:34 -05001502 *
1503 * This function will build a TX descriptor array with applicable
1504 * L2/L3/L4 packet header descriptors to be sent by send_subcrq_indirect.
1505 */
1506
Thomas Falconc62aa372020-11-18 19:12:20 -06001507static void build_hdr_descs_arr(struct sk_buff *skb,
1508 union sub_crq *indir_arr,
Thomas Falconad7775d2016-04-01 17:20:34 -05001509 int *num_entries, u8 hdr_field)
1510{
1511 int hdr_len[3] = {0, 0, 0};
Thomas Falconc62aa372020-11-18 19:12:20 -06001512 u8 hdr_data[140] = {0};
Thomas Falcon2de09682017-10-16 10:02:11 -05001513 int tot_len;
Thomas Falconad7775d2016-04-01 17:20:34 -05001514
Thomas Falconc62aa372020-11-18 19:12:20 -06001515 tot_len = build_hdr_data(hdr_field, skb, hdr_len,
1516 hdr_data);
Thomas Falcon2de09682017-10-16 10:02:11 -05001517 *num_entries += create_hdr_descs(hdr_field, hdr_data, tot_len, hdr_len,
Thomas Falconc62aa372020-11-18 19:12:20 -06001518 indir_arr + 1);
Thomas Falconad7775d2016-04-01 17:20:34 -05001519}
1520
Thomas Falcon1f247a62018-03-12 11:51:04 -05001521static int ibmvnic_xmit_workarounds(struct sk_buff *skb,
1522 struct net_device *netdev)
1523{
1524 /* For some backing devices, mishandling of small packets
1525 * can result in a loss of connection or TX stall. Device
1526 * architects recommend that no packet should be smaller
1527 * than the minimum MTU value provided to the driver, so
1528 * pad any packets to that length
1529 */
1530 if (skb->len < netdev->min_mtu)
1531 return skb_put_padto(skb, netdev->min_mtu);
Thomas Falcon7083a452018-03-12 21:05:26 -05001532
1533 return 0;
Thomas Falcon1f247a62018-03-12 11:51:04 -05001534}
1535
Thomas Falcon0d973382020-11-18 19:12:19 -06001536static void ibmvnic_tx_scrq_clean_buffer(struct ibmvnic_adapter *adapter,
1537 struct ibmvnic_sub_crq_queue *tx_scrq)
1538{
1539 struct ibmvnic_ind_xmit_queue *ind_bufp;
1540 struct ibmvnic_tx_buff *tx_buff;
1541 struct ibmvnic_tx_pool *tx_pool;
1542 union sub_crq tx_scrq_entry;
1543 int queue_num;
1544 int entries;
1545 int index;
1546 int i;
1547
1548 ind_bufp = &tx_scrq->ind_buf;
1549 entries = (u64)ind_bufp->index;
1550 queue_num = tx_scrq->pool_index;
1551
1552 for (i = entries - 1; i >= 0; --i) {
1553 tx_scrq_entry = ind_bufp->indir_arr[i];
1554 if (tx_scrq_entry.v1.type != IBMVNIC_TX_DESC)
1555 continue;
1556 index = be32_to_cpu(tx_scrq_entry.v1.correlator);
1557 if (index & IBMVNIC_TSO_POOL_MASK) {
1558 tx_pool = &adapter->tso_pool[queue_num];
1559 index &= ~IBMVNIC_TSO_POOL_MASK;
1560 } else {
1561 tx_pool = &adapter->tx_pool[queue_num];
1562 }
1563 tx_pool->free_map[tx_pool->consumer_index] = index;
1564 tx_pool->consumer_index = tx_pool->consumer_index == 0 ?
1565 tx_pool->num_buffers - 1 :
1566 tx_pool->consumer_index - 1;
1567 tx_buff = &tx_pool->tx_buff[index];
1568 adapter->netdev->stats.tx_packets--;
1569 adapter->netdev->stats.tx_bytes -= tx_buff->skb->len;
1570 adapter->tx_stats_buffers[queue_num].packets--;
1571 adapter->tx_stats_buffers[queue_num].bytes -=
1572 tx_buff->skb->len;
1573 dev_kfree_skb_any(tx_buff->skb);
1574 tx_buff->skb = NULL;
1575 adapter->netdev->stats.tx_dropped++;
1576 }
1577 ind_bufp->index = 0;
1578 if (atomic_sub_return(entries, &tx_scrq->used) <=
1579 (adapter->req_tx_entries_per_subcrq / 2) &&
1580 __netif_subqueue_stopped(adapter->netdev, queue_num)) {
1581 netif_wake_subqueue(adapter->netdev, queue_num);
1582 netdev_dbg(adapter->netdev, "Started queue %d\n",
1583 queue_num);
1584 }
1585}
1586
1587static int ibmvnic_tx_scrq_flush(struct ibmvnic_adapter *adapter,
1588 struct ibmvnic_sub_crq_queue *tx_scrq)
1589{
1590 struct ibmvnic_ind_xmit_queue *ind_bufp;
1591 u64 dma_addr;
1592 u64 entries;
1593 u64 handle;
1594 int rc;
1595
1596 ind_bufp = &tx_scrq->ind_buf;
1597 dma_addr = (u64)ind_bufp->indir_dma;
1598 entries = (u64)ind_bufp->index;
1599 handle = tx_scrq->handle;
1600
1601 if (!entries)
1602 return 0;
1603 rc = send_subcrq_indirect(adapter, handle, dma_addr, entries);
1604 if (rc)
1605 ibmvnic_tx_scrq_clean_buffer(adapter, tx_scrq);
1606 else
1607 ind_bufp->index = 0;
1608 return 0;
1609}
1610
YueHaibing94b2bb22018-09-18 14:35:47 +08001611static netdev_tx_t ibmvnic_xmit(struct sk_buff *skb, struct net_device *netdev)
Thomas Falcon032c5e82015-12-21 11:26:06 -06001612{
1613 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1614 int queue_num = skb_get_queue_mapping(skb);
Thomas Falconad7775d2016-04-01 17:20:34 -05001615 u8 *hdrs = (u8 *)&adapter->tx_rx_desc_req;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001616 struct device *dev = &adapter->vdev->dev;
Thomas Falcon0d973382020-11-18 19:12:19 -06001617 struct ibmvnic_ind_xmit_queue *ind_bufp;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001618 struct ibmvnic_tx_buff *tx_buff = NULL;
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001619 struct ibmvnic_sub_crq_queue *tx_scrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001620 struct ibmvnic_tx_pool *tx_pool;
1621 unsigned int tx_send_failed = 0;
Thomas Falcon0d973382020-11-18 19:12:19 -06001622 netdev_tx_t ret = NETDEV_TX_OK;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001623 unsigned int tx_map_failed = 0;
Thomas Falconc62aa372020-11-18 19:12:20 -06001624 union sub_crq indir_arr[16];
Thomas Falcon032c5e82015-12-21 11:26:06 -06001625 unsigned int tx_dropped = 0;
1626 unsigned int tx_packets = 0;
1627 unsigned int tx_bytes = 0;
1628 dma_addr_t data_dma_addr;
1629 struct netdev_queue *txq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001630 unsigned long lpar_rc;
1631 union sub_crq tx_crq;
1632 unsigned int offset;
Thomas Falconad7775d2016-04-01 17:20:34 -05001633 int num_entries = 1;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001634 unsigned char *dst;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001635 int index = 0;
Thomas Falcona0dca102018-01-18 19:29:48 -06001636 u8 proto = 0;
Thomas Falcon0d973382020-11-18 19:12:19 -06001637
1638 tx_scrq = adapter->tx_scrq[queue_num];
1639 txq = netdev_get_tx_queue(netdev, queue_num);
1640 ind_bufp = &tx_scrq->ind_buf;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001641
Juliet Kim7ed5b312019-09-20 16:11:23 -04001642 if (test_bit(0, &adapter->resetting)) {
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001643 if (!netif_subqueue_stopped(netdev, skb))
1644 netif_stop_subqueue(netdev, queue_num);
1645 dev_kfree_skb_any(skb);
1646
Thomas Falcon032c5e82015-12-21 11:26:06 -06001647 tx_send_failed++;
1648 tx_dropped++;
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001649 ret = NETDEV_TX_OK;
Thomas Falcon0d973382020-11-18 19:12:19 -06001650 ibmvnic_tx_scrq_flush(adapter, tx_scrq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001651 goto out;
1652 }
1653
Thomas Falcon7083a452018-03-12 21:05:26 -05001654 if (ibmvnic_xmit_workarounds(skb, netdev)) {
Thomas Falcon1f247a62018-03-12 11:51:04 -05001655 tx_dropped++;
1656 tx_send_failed++;
1657 ret = NETDEV_TX_OK;
Thomas Falcon0d973382020-11-18 19:12:19 -06001658 ibmvnic_tx_scrq_flush(adapter, tx_scrq);
Thomas Falcon1f247a62018-03-12 11:51:04 -05001659 goto out;
1660 }
Thomas Falcon06b3e352018-03-16 20:00:28 -05001661 if (skb_is_gso(skb))
1662 tx_pool = &adapter->tso_pool[queue_num];
1663 else
1664 tx_pool = &adapter->tx_pool[queue_num];
Thomas Falcon1f247a62018-03-12 11:51:04 -05001665
Thomas Falcon032c5e82015-12-21 11:26:06 -06001666 index = tx_pool->free_map[tx_pool->consumer_index];
Thomas Falconfdb06102017-10-17 12:36:55 -05001667
Thomas Falcon86b61a52018-03-16 20:00:29 -05001668 if (index == IBMVNIC_INVALID_MAP) {
1669 dev_kfree_skb_any(skb);
1670 tx_send_failed++;
1671 tx_dropped++;
1672 ret = NETDEV_TX_OK;
Thomas Falcon0d973382020-11-18 19:12:19 -06001673 ibmvnic_tx_scrq_flush(adapter, tx_scrq);
Thomas Falcon86b61a52018-03-16 20:00:29 -05001674 goto out;
1675 }
1676
1677 tx_pool->free_map[tx_pool->consumer_index] = IBMVNIC_INVALID_MAP;
1678
Thomas Falcon06b3e352018-03-16 20:00:28 -05001679 offset = index * tx_pool->buf_size;
1680 dst = tx_pool->long_term_buff.buff + offset;
1681 memset(dst, 0, tx_pool->buf_size);
1682 data_dma_addr = tx_pool->long_term_buff.addr + offset;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001683
Thomas Falcon15482052017-10-17 12:36:54 -05001684 if (skb_shinfo(skb)->nr_frags) {
1685 int cur, i;
1686
1687 /* Copy the head */
1688 skb_copy_from_linear_data(skb, dst, skb_headlen(skb));
1689 cur = skb_headlen(skb);
1690
1691 /* Copy the frags */
1692 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1693 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1694
1695 memcpy(dst + cur,
1696 page_address(skb_frag_page(frag)) +
Jonathan Lemonb54c9d52019-07-30 07:40:33 -07001697 skb_frag_off(frag), skb_frag_size(frag));
Thomas Falcon15482052017-10-17 12:36:54 -05001698 cur += skb_frag_size(frag);
1699 }
1700 } else {
1701 skb_copy_from_linear_data(skb, dst, skb->len);
1702 }
1703
Thomas Falcon032c5e82015-12-21 11:26:06 -06001704 tx_pool->consumer_index =
Thomas Falcon06b3e352018-03-16 20:00:28 -05001705 (tx_pool->consumer_index + 1) % tx_pool->num_buffers;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001706
1707 tx_buff = &tx_pool->tx_buff[index];
1708 tx_buff->skb = skb;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001709 tx_buff->index = index;
1710 tx_buff->pool_index = queue_num;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001711
1712 memset(&tx_crq, 0, sizeof(tx_crq));
1713 tx_crq.v1.first = IBMVNIC_CRQ_CMD;
1714 tx_crq.v1.type = IBMVNIC_TX_DESC;
1715 tx_crq.v1.n_crq_elem = 1;
1716 tx_crq.v1.n_sge = 1;
1717 tx_crq.v1.flags1 = IBMVNIC_TX_COMP_NEEDED;
Thomas Falcon06b3e352018-03-16 20:00:28 -05001718
Thomas Falconfdb06102017-10-17 12:36:55 -05001719 if (skb_is_gso(skb))
Thomas Falcon06b3e352018-03-16 20:00:28 -05001720 tx_crq.v1.correlator =
1721 cpu_to_be32(index | IBMVNIC_TSO_POOL_MASK);
Thomas Falconfdb06102017-10-17 12:36:55 -05001722 else
Thomas Falcon06b3e352018-03-16 20:00:28 -05001723 tx_crq.v1.correlator = cpu_to_be32(index);
1724 tx_crq.v1.dma_reg = cpu_to_be16(tx_pool->long_term_buff.map_id);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001725 tx_crq.v1.sge_len = cpu_to_be32(skb->len);
1726 tx_crq.v1.ioba = cpu_to_be64(data_dma_addr);
1727
Michał Mirosławe84b4792018-11-07 17:50:52 +01001728 if (adapter->vlan_header_insertion && skb_vlan_tag_present(skb)) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06001729 tx_crq.v1.flags2 |= IBMVNIC_TX_VLAN_INSERT;
1730 tx_crq.v1.vlan_id = cpu_to_be16(skb->vlan_tci);
1731 }
1732
1733 if (skb->protocol == htons(ETH_P_IP)) {
Thomas Falcona0dca102018-01-18 19:29:48 -06001734 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV4;
1735 proto = ip_hdr(skb)->protocol;
1736 } else if (skb->protocol == htons(ETH_P_IPV6)) {
1737 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV6;
1738 proto = ipv6_hdr(skb)->nexthdr;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001739 }
1740
Thomas Falcona0dca102018-01-18 19:29:48 -06001741 if (proto == IPPROTO_TCP)
1742 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_TCP;
1743 else if (proto == IPPROTO_UDP)
1744 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_UDP;
1745
Thomas Falconad7775d2016-04-01 17:20:34 -05001746 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06001747 tx_crq.v1.flags1 |= IBMVNIC_TX_CHKSUM_OFFLOAD;
Thomas Falconad7775d2016-04-01 17:20:34 -05001748 hdrs += 2;
1749 }
Thomas Falconfdb06102017-10-17 12:36:55 -05001750 if (skb_is_gso(skb)) {
1751 tx_crq.v1.flags1 |= IBMVNIC_TX_LSO;
1752 tx_crq.v1.mss = cpu_to_be16(skb_shinfo(skb)->gso_size);
1753 hdrs += 2;
1754 }
Thomas Falcon0d973382020-11-18 19:12:19 -06001755
1756 if ((*hdrs >> 7) & 1)
Thomas Falconc62aa372020-11-18 19:12:20 -06001757 build_hdr_descs_arr(skb, indir_arr, &num_entries, *hdrs);
Thomas Falcon0d973382020-11-18 19:12:19 -06001758
1759 tx_crq.v1.n_crq_elem = num_entries;
1760 tx_buff->num_entries = num_entries;
1761 /* flush buffer if current entry can not fit */
1762 if (num_entries + ind_bufp->index > IBMVNIC_MAX_IND_DESCS) {
1763 lpar_rc = ibmvnic_tx_scrq_flush(adapter, tx_scrq);
1764 if (lpar_rc != H_SUCCESS)
1765 goto tx_flush_err;
Thomas Falconad7775d2016-04-01 17:20:34 -05001766 }
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001767
Thomas Falconc62aa372020-11-18 19:12:20 -06001768 indir_arr[0] = tx_crq;
1769 memcpy(&ind_bufp->indir_arr[ind_bufp->index], &indir_arr[0],
Thomas Falcon0d973382020-11-18 19:12:19 -06001770 num_entries * sizeof(struct ibmvnic_generic_scrq));
1771 ind_bufp->index += num_entries;
1772 if (__netdev_tx_sent_queue(txq, skb->len,
1773 netdev_xmit_more() &&
1774 ind_bufp->index < IBMVNIC_MAX_IND_DESCS)) {
1775 lpar_rc = ibmvnic_tx_scrq_flush(adapter, tx_scrq);
1776 if (lpar_rc != H_SUCCESS)
1777 goto tx_err;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001778 }
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001779
Thomas Falconffc385b2018-02-18 10:08:41 -06001780 if (atomic_add_return(num_entries, &tx_scrq->used)
Brian King58c8c0c2017-04-19 13:44:47 -04001781 >= adapter->req_tx_entries_per_subcrq) {
Thomas Falcon0aecb132018-02-26 18:10:58 -06001782 netdev_dbg(netdev, "Stopping queue %d\n", queue_num);
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001783 netif_stop_subqueue(netdev, queue_num);
1784 }
1785
Thomas Falcon032c5e82015-12-21 11:26:06 -06001786 tx_packets++;
1787 tx_bytes += skb->len;
1788 txq->trans_start = jiffies;
1789 ret = NETDEV_TX_OK;
Thomas Falcon86b61a52018-03-16 20:00:29 -05001790 goto out;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001791
Thomas Falcon0d973382020-11-18 19:12:19 -06001792tx_flush_err:
1793 dev_kfree_skb_any(skb);
1794 tx_buff->skb = NULL;
1795 tx_pool->consumer_index = tx_pool->consumer_index == 0 ?
1796 tx_pool->num_buffers - 1 :
1797 tx_pool->consumer_index - 1;
1798 tx_dropped++;
1799tx_err:
1800 if (lpar_rc != H_CLOSED && lpar_rc != H_PARAMETER)
1801 dev_err_ratelimited(dev, "tx: send failed\n");
1802
1803 if (lpar_rc == H_CLOSED || adapter->failover_pending) {
1804 /* Disable TX and report carrier off if queue is closed
1805 * or pending failover.
1806 * Firmware guarantees that a signal will be sent to the
1807 * driver, triggering a reset or some other action.
1808 */
1809 netif_tx_stop_all_queues(netdev);
1810 netif_carrier_off(netdev);
1811 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001812out:
1813 netdev->stats.tx_dropped += tx_dropped;
1814 netdev->stats.tx_bytes += tx_bytes;
1815 netdev->stats.tx_packets += tx_packets;
1816 adapter->tx_send_failed += tx_send_failed;
1817 adapter->tx_map_failed += tx_map_failed;
John Allen3d52b592017-08-02 16:44:14 -05001818 adapter->tx_stats_buffers[queue_num].packets += tx_packets;
1819 adapter->tx_stats_buffers[queue_num].bytes += tx_bytes;
1820 adapter->tx_stats_buffers[queue_num].dropped_packets += tx_dropped;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001821
1822 return ret;
1823}
1824
1825static void ibmvnic_set_multi(struct net_device *netdev)
1826{
1827 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1828 struct netdev_hw_addr *ha;
1829 union ibmvnic_crq crq;
1830
1831 memset(&crq, 0, sizeof(crq));
1832 crq.request_capability.first = IBMVNIC_CRQ_CMD;
1833 crq.request_capability.cmd = REQUEST_CAPABILITY;
1834
1835 if (netdev->flags & IFF_PROMISC) {
1836 if (!adapter->promisc_supported)
1837 return;
1838 } else {
1839 if (netdev->flags & IFF_ALLMULTI) {
1840 /* Accept all multicast */
1841 memset(&crq, 0, sizeof(crq));
1842 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1843 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1844 crq.multicast_ctrl.flags = IBMVNIC_ENABLE_ALL;
1845 ibmvnic_send_crq(adapter, &crq);
1846 } else if (netdev_mc_empty(netdev)) {
1847 /* Reject all multicast */
1848 memset(&crq, 0, sizeof(crq));
1849 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1850 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1851 crq.multicast_ctrl.flags = IBMVNIC_DISABLE_ALL;
1852 ibmvnic_send_crq(adapter, &crq);
1853 } else {
1854 /* Accept one or more multicast(s) */
1855 netdev_for_each_mc_addr(ha, netdev) {
1856 memset(&crq, 0, sizeof(crq));
1857 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1858 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1859 crq.multicast_ctrl.flags = IBMVNIC_ENABLE_MC;
1860 ether_addr_copy(&crq.multicast_ctrl.mac_addr[0],
1861 ha->addr);
1862 ibmvnic_send_crq(adapter, &crq);
1863 }
1864 }
1865 }
1866}
1867
Thomas Falcon62740e92019-05-09 23:13:43 -05001868static int __ibmvnic_set_mac(struct net_device *netdev, u8 *dev_addr)
Thomas Falcon032c5e82015-12-21 11:26:06 -06001869{
1870 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001871 union ibmvnic_crq crq;
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05001872 int rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001873
Thomas Falcon62740e92019-05-09 23:13:43 -05001874 if (!is_valid_ether_addr(dev_addr)) {
1875 rc = -EADDRNOTAVAIL;
1876 goto err;
1877 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001878
1879 memset(&crq, 0, sizeof(crq));
1880 crq.change_mac_addr.first = IBMVNIC_CRQ_CMD;
1881 crq.change_mac_addr.cmd = CHANGE_MAC_ADDR;
Thomas Falcon62740e92019-05-09 23:13:43 -05001882 ether_addr_copy(&crq.change_mac_addr.mac_addr[0], dev_addr);
Thomas Falconf8136142018-01-29 13:45:05 -06001883
Thomas Falconff25dcb2019-11-25 17:12:56 -06001884 mutex_lock(&adapter->fw_lock);
1885 adapter->fw_done_rc = 0;
Thomas Falcon070eca92019-11-25 17:12:53 -06001886 reinit_completion(&adapter->fw_done);
Thomas Falconff25dcb2019-11-25 17:12:56 -06001887
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05001888 rc = ibmvnic_send_crq(adapter, &crq);
Thomas Falcon62740e92019-05-09 23:13:43 -05001889 if (rc) {
1890 rc = -EIO;
Thomas Falconff25dcb2019-11-25 17:12:56 -06001891 mutex_unlock(&adapter->fw_lock);
Thomas Falcon62740e92019-05-09 23:13:43 -05001892 goto err;
1893 }
1894
Thomas Falcon476d96c2019-11-25 17:12:55 -06001895 rc = ibmvnic_wait_for_completion(adapter, &adapter->fw_done, 10000);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001896 /* netdev->dev_addr is changed in handle_change_mac_rsp function */
Thomas Falcon476d96c2019-11-25 17:12:55 -06001897 if (rc || adapter->fw_done_rc) {
Thomas Falcon62740e92019-05-09 23:13:43 -05001898 rc = -EIO;
Thomas Falconff25dcb2019-11-25 17:12:56 -06001899 mutex_unlock(&adapter->fw_lock);
Thomas Falcon62740e92019-05-09 23:13:43 -05001900 goto err;
1901 }
Thomas Falconff25dcb2019-11-25 17:12:56 -06001902 mutex_unlock(&adapter->fw_lock);
Thomas Falcon62740e92019-05-09 23:13:43 -05001903 return 0;
1904err:
1905 ether_addr_copy(adapter->mac_addr, netdev->dev_addr);
1906 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001907}
1908
John Allenc26eba02017-10-26 16:23:25 -05001909static int ibmvnic_set_mac(struct net_device *netdev, void *p)
1910{
1911 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1912 struct sockaddr *addr = p;
Thomas Falconf8136142018-01-29 13:45:05 -06001913 int rc;
John Allenc26eba02017-10-26 16:23:25 -05001914
Thomas Falcon62740e92019-05-09 23:13:43 -05001915 rc = 0;
Lijun Pan8fc36722020-10-27 17:04:56 -05001916 if (!is_valid_ether_addr(addr->sa_data))
1917 return -EADDRNOTAVAIL;
1918
1919 if (adapter->state != VNIC_PROBED) {
1920 ether_addr_copy(adapter->mac_addr, addr->sa_data);
Thomas Falcon62740e92019-05-09 23:13:43 -05001921 rc = __ibmvnic_set_mac(netdev, addr->sa_data);
Lijun Pan8fc36722020-10-27 17:04:56 -05001922 }
John Allenc26eba02017-10-26 16:23:25 -05001923
Thomas Falconf8136142018-01-29 13:45:05 -06001924 return rc;
John Allenc26eba02017-10-26 16:23:25 -05001925}
1926
Lee Jones80708602021-01-15 20:09:03 +00001927/*
Nathan Fontenoted651a12017-05-03 14:04:38 -04001928 * do_reset returns zero if we are able to keep processing reset events, or
1929 * non-zero if we hit a fatal error and must halt.
1930 */
1931static int do_reset(struct ibmvnic_adapter *adapter,
1932 struct ibmvnic_rwi *rwi, u32 reset_state)
1933{
John Allen896d8692018-01-18 16:26:31 -06001934 u64 old_num_rx_queues, old_num_tx_queues;
Thomas Falcon5bf032e2018-11-21 11:17:59 -06001935 u64 old_num_rx_slots, old_num_tx_slots;
Nathan Fontenoted651a12017-05-03 14:04:38 -04001936 struct net_device *netdev = adapter->netdev;
1937 int i, rc;
1938
Sukadev Bhattiprolu38bd5ce2020-12-04 18:22:35 -08001939 netdev_dbg(adapter->netdev,
1940 "[S:%d FOP:%d] Reset reason %d, reset_state %d\n",
1941 adapter->state, adapter->failover_pending,
1942 rwi->reset_reason, reset_state);
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001943
Lijun Pan3f5ec372021-01-06 15:35:14 -06001944 adapter->reset_reason = rwi->reset_reason;
1945 /* requestor of VNIC_RESET_CHANGE_PARAM already has the rtnl lock */
1946 if (!(adapter->reset_reason == VNIC_RESET_CHANGE_PARAM))
1947 rtnl_lock();
1948
Sukadev Bhattiprolu1d850492020-10-30 10:07:11 -07001949 /*
1950 * Now that we have the rtnl lock, clear any pending failover.
1951 * This will ensure ibmvnic_open() has either completed or will
1952 * block until failover is complete.
1953 */
1954 if (rwi->reset_reason == VNIC_RESET_FAILOVER)
1955 adapter->failover_pending = false;
Juliet Kimb27507b2019-09-20 16:11:22 -04001956
Nathan Fontenoted651a12017-05-03 14:04:38 -04001957 netif_carrier_off(netdev);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001958
John Allen896d8692018-01-18 16:26:31 -06001959 old_num_rx_queues = adapter->req_rx_queues;
1960 old_num_tx_queues = adapter->req_tx_queues;
Thomas Falcon5bf032e2018-11-21 11:17:59 -06001961 old_num_rx_slots = adapter->req_rx_add_entries_per_subcrq;
1962 old_num_tx_slots = adapter->req_tx_entries_per_subcrq;
John Allen896d8692018-01-18 16:26:31 -06001963
Nathan Fontenot30f79622018-04-06 18:37:06 -05001964 ibmvnic_cleanup(netdev);
1965
Thomas Falcon1f946082019-06-07 16:03:53 -05001966 if (reset_state == VNIC_OPEN &&
1967 adapter->reset_reason != VNIC_RESET_MOBILITY &&
Nathan Fontenot30f79622018-04-06 18:37:06 -05001968 adapter->reset_reason != VNIC_RESET_FAILOVER) {
Lijun Pan3f5ec372021-01-06 15:35:14 -06001969 if (adapter->reset_reason == VNIC_RESET_CHANGE_PARAM) {
1970 rc = __ibmvnic_close(netdev);
1971 if (rc)
1972 goto out;
1973 } else {
1974 adapter->state = VNIC_CLOSING;
Nathan Fontenoted651a12017-05-03 14:04:38 -04001975
Lijun Pan3f5ec372021-01-06 15:35:14 -06001976 /* Release the RTNL lock before link state change and
1977 * re-acquire after the link state change to allow
1978 * linkwatch_event to grab the RTNL lock and run during
1979 * a reset.
1980 */
1981 rtnl_unlock();
1982 rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_DN);
1983 rtnl_lock();
1984 if (rc)
1985 goto out;
Juliet Kimb27507b2019-09-20 16:11:22 -04001986
Lijun Pan3f5ec372021-01-06 15:35:14 -06001987 if (adapter->state != VNIC_CLOSING) {
1988 rc = -1;
1989 goto out;
1990 }
1991
1992 adapter->state = VNIC_CLOSED;
Juliet Kimb27507b2019-09-20 16:11:22 -04001993 }
Lijun Pan3f5ec372021-01-06 15:35:14 -06001994 }
Juliet Kimb27507b2019-09-20 16:11:22 -04001995
Lijun Pan3f5ec372021-01-06 15:35:14 -06001996 if (adapter->reset_reason == VNIC_RESET_CHANGE_PARAM) {
1997 release_resources(adapter);
1998 release_sub_crqs(adapter, 1);
1999 release_crq_queue(adapter);
John Allenc26eba02017-10-26 16:23:25 -05002000 }
2001
John Allen8cb31cf2017-05-26 10:30:37 -04002002 if (adapter->reset_reason != VNIC_RESET_NON_FATAL) {
2003 /* remove the closed state so when we call open it appears
2004 * we are coming from the probed state.
2005 */
Nathan Fontenoted651a12017-05-03 14:04:38 -04002006 adapter->state = VNIC_PROBED;
John Allen8cb31cf2017-05-26 10:30:37 -04002007
Lijun Pan3f5ec372021-01-06 15:35:14 -06002008 if (adapter->reset_reason == VNIC_RESET_CHANGE_PARAM) {
2009 rc = init_crq_queue(adapter);
2010 } else if (adapter->reset_reason == VNIC_RESET_MOBILITY) {
Nathan Fontenot30f79622018-04-06 18:37:06 -05002011 rc = ibmvnic_reenable_crq_queue(adapter);
2012 release_sub_crqs(adapter, 1);
2013 } else {
2014 rc = ibmvnic_reset_crq(adapter);
Dany Madden8b40eb732020-06-18 15:24:13 -04002015 if (rc == H_CLOSED || rc == H_SUCCESS) {
Nathan Fontenot30f79622018-04-06 18:37:06 -05002016 rc = vio_enable_interrupts(adapter->vdev);
Dany Madden8b40eb732020-06-18 15:24:13 -04002017 if (rc)
2018 netdev_err(adapter->netdev,
2019 "Reset failed to enable interrupts. rc=%d\n",
2020 rc);
2021 }
Nathan Fontenot30f79622018-04-06 18:37:06 -05002022 }
2023
2024 if (rc) {
2025 netdev_err(adapter->netdev,
Dany Madden8b40eb732020-06-18 15:24:13 -04002026 "Reset couldn't initialize crq. rc=%d\n", rc);
Juliet Kimb27507b2019-09-20 16:11:22 -04002027 goto out;
Nathan Fontenot30f79622018-04-06 18:37:06 -05002028 }
2029
Lijun Pan635e4422020-08-19 17:52:26 -05002030 rc = ibmvnic_reset_init(adapter, true);
Juliet Kimb27507b2019-09-20 16:11:22 -04002031 if (rc) {
2032 rc = IBMVNIC_INIT_FAILED;
2033 goto out;
2034 }
John Allen8cb31cf2017-05-26 10:30:37 -04002035
2036 /* If the adapter was in PROBE state prior to the reset,
2037 * exit here.
2038 */
Juliet Kimb27507b2019-09-20 16:11:22 -04002039 if (reset_state == VNIC_PROBED) {
2040 rc = 0;
2041 goto out;
2042 }
John Allen8cb31cf2017-05-26 10:30:37 -04002043
2044 rc = ibmvnic_login(netdev);
2045 if (rc) {
Juliet Kimb27507b2019-09-20 16:11:22 -04002046 goto out;
John Allen8cb31cf2017-05-26 10:30:37 -04002047 }
2048
Lijun Pan3f5ec372021-01-06 15:35:14 -06002049 if (adapter->reset_reason == VNIC_RESET_CHANGE_PARAM) {
2050 rc = init_resources(adapter);
2051 if (rc)
2052 goto out;
2053 } else if (adapter->req_rx_queues != old_num_rx_queues ||
Juliet Kimb27507b2019-09-20 16:11:22 -04002054 adapter->req_tx_queues != old_num_tx_queues ||
2055 adapter->req_rx_add_entries_per_subcrq !=
2056 old_num_rx_slots ||
2057 adapter->req_tx_entries_per_subcrq !=
Mingming Cao9f134572020-08-25 13:26:41 -04002058 old_num_tx_slots ||
2059 !adapter->rx_pool ||
2060 !adapter->tso_pool ||
2061 !adapter->tx_pool) {
John Allen896d8692018-01-18 16:26:31 -06002062 release_rx_pools(adapter);
2063 release_tx_pools(adapter);
Juliet Kima5681e22018-11-19 15:59:22 -06002064 release_napi(adapter);
2065 release_vpd_data(adapter);
2066
2067 rc = init_resources(adapter);
Thomas Falconf611a5b2018-08-30 13:19:53 -05002068 if (rc)
Juliet Kimb27507b2019-09-20 16:11:22 -04002069 goto out;
Nathan Fontenotd9043c12018-02-19 13:30:14 -06002070
John Allenc26eba02017-10-26 16:23:25 -05002071 } else {
2072 rc = reset_tx_pools(adapter);
Jakub Kicinski8ae4dff2020-09-04 21:07:49 -07002073 if (rc) {
Mingming Cao9f134572020-08-25 13:26:41 -04002074 netdev_dbg(adapter->netdev, "reset tx pools failed (%d)\n",
2075 rc);
Juliet Kimb27507b2019-09-20 16:11:22 -04002076 goto out;
Jakub Kicinski8ae4dff2020-09-04 21:07:49 -07002077 }
Nathan Fontenot8c0543a2017-05-26 10:31:06 -04002078
John Allenc26eba02017-10-26 16:23:25 -05002079 rc = reset_rx_pools(adapter);
Jakub Kicinski8ae4dff2020-09-04 21:07:49 -07002080 if (rc) {
Mingming Cao9f134572020-08-25 13:26:41 -04002081 netdev_dbg(adapter->netdev, "reset rx pools failed (%d)\n",
2082 rc);
Juliet Kimb27507b2019-09-20 16:11:22 -04002083 goto out;
Jakub Kicinski8ae4dff2020-09-04 21:07:49 -07002084 }
John Allenc26eba02017-10-26 16:23:25 -05002085 }
Thomas Falcon134bbe72018-05-16 15:49:04 -05002086 ibmvnic_disable_irqs(adapter);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002087 }
John Allene676d812018-03-14 10:41:29 -05002088 adapter->state = VNIC_CLOSED;
2089
Juliet Kimb27507b2019-09-20 16:11:22 -04002090 if (reset_state == VNIC_CLOSED) {
2091 rc = 0;
2092 goto out;
2093 }
John Allene676d812018-03-14 10:41:29 -05002094
Nathan Fontenoted651a12017-05-03 14:04:38 -04002095 rc = __ibmvnic_open(netdev);
2096 if (rc) {
Juliet Kimb27507b2019-09-20 16:11:22 -04002097 rc = IBMVNIC_OPEN_FAILED;
2098 goto out;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002099 }
2100
Thomas Falconbe32a242019-06-07 16:03:54 -05002101 /* refresh device's multicast list */
2102 ibmvnic_set_multi(netdev);
2103
Nathan Fontenoted651a12017-05-03 14:04:38 -04002104 /* kick napi */
2105 for (i = 0; i < adapter->req_rx_queues; i++)
2106 napi_schedule(&adapter->napi[i]);
2107
Lijun Pan98025bc2020-11-20 16:40:12 -06002108 if (adapter->reset_reason == VNIC_RESET_FAILOVER ||
Lijun Pan6be46662020-12-14 15:19:29 -06002109 adapter->reset_reason == VNIC_RESET_MOBILITY)
2110 __netdev_notify_peers(netdev);
Nathan Fontenot61d3e1d2017-06-12 20:47:45 -04002111
Juliet Kimb27507b2019-09-20 16:11:22 -04002112 rc = 0;
2113
2114out:
Dany Madden0cb4bc62020-11-25 18:04:27 -06002115 /* restore the adapter state if reset failed */
2116 if (rc)
2117 adapter->state = reset_state;
Lijun Pan3f5ec372021-01-06 15:35:14 -06002118 /* requestor of VNIC_RESET_CHANGE_PARAM should still hold the rtnl lock */
2119 if (!(adapter->reset_reason == VNIC_RESET_CHANGE_PARAM))
2120 rtnl_unlock();
Juliet Kimb27507b2019-09-20 16:11:22 -04002121
Sukadev Bhattiprolu38bd5ce2020-12-04 18:22:35 -08002122 netdev_dbg(adapter->netdev, "[S:%d FOP:%d] Reset done, rc %d\n",
2123 adapter->state, adapter->failover_pending, rc);
Juliet Kimb27507b2019-09-20 16:11:22 -04002124 return rc;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002125}
2126
Thomas Falcon2770a792018-05-23 13:38:02 -05002127static int do_hard_reset(struct ibmvnic_adapter *adapter,
2128 struct ibmvnic_rwi *rwi, u32 reset_state)
2129{
2130 struct net_device *netdev = adapter->netdev;
2131 int rc;
2132
2133 netdev_dbg(adapter->netdev, "Hard resetting driver (%d)\n",
2134 rwi->reset_reason);
2135
2136 netif_carrier_off(netdev);
2137 adapter->reset_reason = rwi->reset_reason;
2138
2139 ibmvnic_cleanup(netdev);
2140 release_resources(adapter);
2141 release_sub_crqs(adapter, 0);
2142 release_crq_queue(adapter);
2143
2144 /* remove the closed state so when we call open it appears
2145 * we are coming from the probed state.
2146 */
2147 adapter->state = VNIC_PROBED;
2148
Thomas Falconbbd669a2019-04-04 18:58:26 -05002149 reinit_completion(&adapter->init_done);
Thomas Falcon2770a792018-05-23 13:38:02 -05002150 rc = init_crq_queue(adapter);
2151 if (rc) {
2152 netdev_err(adapter->netdev,
2153 "Couldn't initialize crq. rc=%d\n", rc);
Dany Madden0cb4bc62020-11-25 18:04:27 -06002154 goto out;
Thomas Falcon2770a792018-05-23 13:38:02 -05002155 }
2156
Lijun Pan635e4422020-08-19 17:52:26 -05002157 rc = ibmvnic_reset_init(adapter, false);
Thomas Falcon2770a792018-05-23 13:38:02 -05002158 if (rc)
Dany Madden0cb4bc62020-11-25 18:04:27 -06002159 goto out;
Thomas Falcon2770a792018-05-23 13:38:02 -05002160
2161 /* If the adapter was in PROBE state prior to the reset,
2162 * exit here.
2163 */
2164 if (reset_state == VNIC_PROBED)
Dany Madden0cb4bc62020-11-25 18:04:27 -06002165 goto out;
Thomas Falcon2770a792018-05-23 13:38:02 -05002166
2167 rc = ibmvnic_login(netdev);
Dany Madden0cb4bc62020-11-25 18:04:27 -06002168 if (rc)
2169 goto out;
Juliet Kima5681e22018-11-19 15:59:22 -06002170
2171 rc = init_resources(adapter);
Thomas Falcon2770a792018-05-23 13:38:02 -05002172 if (rc)
Dany Madden0cb4bc62020-11-25 18:04:27 -06002173 goto out;
Thomas Falcon2770a792018-05-23 13:38:02 -05002174
2175 ibmvnic_disable_irqs(adapter);
2176 adapter->state = VNIC_CLOSED;
2177
2178 if (reset_state == VNIC_CLOSED)
Dany Madden0cb4bc62020-11-25 18:04:27 -06002179 goto out;
Thomas Falcon2770a792018-05-23 13:38:02 -05002180
2181 rc = __ibmvnic_open(netdev);
Dany Madden0cb4bc62020-11-25 18:04:27 -06002182 if (rc) {
2183 rc = IBMVNIC_OPEN_FAILED;
2184 goto out;
2185 }
Thomas Falcon2770a792018-05-23 13:38:02 -05002186
Lijun Pan6be46662020-12-14 15:19:29 -06002187 __netdev_notify_peers(netdev);
Dany Madden0cb4bc62020-11-25 18:04:27 -06002188out:
2189 /* restore adapter state if reset failed */
2190 if (rc)
2191 adapter->state = reset_state;
Sukadev Bhattiprolu38bd5ce2020-12-04 18:22:35 -08002192 netdev_dbg(adapter->netdev, "[S:%d FOP:%d] Hard reset done, rc %d\n",
2193 adapter->state, adapter->failover_pending, rc);
Dany Madden0cb4bc62020-11-25 18:04:27 -06002194 return rc;
Thomas Falcon2770a792018-05-23 13:38:02 -05002195}
2196
Nathan Fontenoted651a12017-05-03 14:04:38 -04002197static struct ibmvnic_rwi *get_next_rwi(struct ibmvnic_adapter *adapter)
2198{
2199 struct ibmvnic_rwi *rwi;
Thomas Falcon6c5c7482018-12-10 15:22:22 -06002200 unsigned long flags;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002201
Thomas Falcon6c5c7482018-12-10 15:22:22 -06002202 spin_lock_irqsave(&adapter->rwi_lock, flags);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002203
2204 if (!list_empty(&adapter->rwi_list)) {
2205 rwi = list_first_entry(&adapter->rwi_list, struct ibmvnic_rwi,
2206 list);
2207 list_del(&rwi->list);
2208 } else {
2209 rwi = NULL;
2210 }
2211
Thomas Falcon6c5c7482018-12-10 15:22:22 -06002212 spin_unlock_irqrestore(&adapter->rwi_lock, flags);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002213 return rwi;
2214}
2215
Nathan Fontenoted651a12017-05-03 14:04:38 -04002216static void __ibmvnic_reset(struct work_struct *work)
2217{
2218 struct ibmvnic_rwi *rwi;
2219 struct ibmvnic_adapter *adapter;
Juliet Kim7d7195a2020-03-10 09:23:58 -05002220 bool saved_state = false;
2221 unsigned long flags;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002222 u32 reset_state;
John Allenc26eba02017-10-26 16:23:25 -05002223 int rc = 0;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002224
2225 adapter = container_of(work, struct ibmvnic_adapter, ibmvnic_reset);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002226
Juliet Kim7ed5b312019-09-20 16:11:23 -04002227 if (test_and_set_bit_lock(0, &adapter->resetting)) {
2228 schedule_delayed_work(&adapter->ibmvnic_delayed_reset,
2229 IBMVNIC_RESET_DELAY);
2230 return;
2231 }
2232
Nathan Fontenoted651a12017-05-03 14:04:38 -04002233 rwi = get_next_rwi(adapter);
2234 while (rwi) {
Juliet Kim7d7195a2020-03-10 09:23:58 -05002235 spin_lock_irqsave(&adapter->state_lock, flags);
2236
Thomas Falcon36f10312019-08-27 11:10:04 -05002237 if (adapter->state == VNIC_REMOVING ||
Michal Suchanekc8dc5592019-09-09 22:44:51 +02002238 adapter->state == VNIC_REMOVED) {
Juliet Kim7d7195a2020-03-10 09:23:58 -05002239 spin_unlock_irqrestore(&adapter->state_lock, flags);
Juliet Kim1c2977c2019-09-05 17:30:01 -04002240 kfree(rwi);
2241 rc = EBUSY;
2242 break;
2243 }
Thomas Falcon36f10312019-08-27 11:10:04 -05002244
Juliet Kim7d7195a2020-03-10 09:23:58 -05002245 if (!saved_state) {
2246 reset_state = adapter->state;
Juliet Kim7d7195a2020-03-10 09:23:58 -05002247 saved_state = true;
2248 }
2249 spin_unlock_irqrestore(&adapter->state_lock, flags);
2250
Lijun Pan3f5ec372021-01-06 15:35:14 -06002251 if (adapter->force_reset_recovery) {
Sukadev Bhattiprolu1d850492020-10-30 10:07:11 -07002252 /*
2253 * Since we are doing a hard reset now, clear the
2254 * failover_pending flag so we don't ignore any
2255 * future MOBILITY or other resets.
2256 */
2257 adapter->failover_pending = false;
2258
Juliet Kimb27507b2019-09-20 16:11:22 -04002259 /* Transport event occurred during previous reset */
2260 if (adapter->wait_for_reset) {
2261 /* Previous was CHANGE_PARAM; caller locked */
2262 adapter->force_reset_recovery = false;
2263 rc = do_hard_reset(adapter, rwi, reset_state);
2264 } else {
2265 rtnl_lock();
2266 adapter->force_reset_recovery = false;
2267 rc = do_hard_reset(adapter, rwi, reset_state);
2268 rtnl_unlock();
2269 }
Sukadev Bhattiproluf15fde92020-11-25 18:04:28 -06002270 if (rc) {
2271 /* give backing device time to settle down */
2272 netdev_dbg(adapter->netdev,
2273 "[S:%d] Hard reset failed, waiting 60 secs\n",
2274 adapter->state);
2275 set_current_state(TASK_UNINTERRUPTIBLE);
2276 schedule_timeout(60 * HZ);
2277 }
Lijun Pan1f45dc22020-12-23 14:49:04 -06002278 } else {
Thomas Falcon2770a792018-05-23 13:38:02 -05002279 rc = do_reset(adapter, rwi, reset_state);
2280 }
Nathan Fontenoted651a12017-05-03 14:04:38 -04002281 kfree(rwi);
Dany Maddena86d5c62020-11-25 18:04:31 -06002282 adapter->last_reset_time = jiffies;
Dany Madden0cb4bc62020-11-25 18:04:27 -06002283
Dany Madden18f141b2020-11-25 18:04:25 -06002284 if (rc)
2285 netdev_dbg(adapter->netdev, "Reset failed, rc=%d\n", rc);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002286
2287 rwi = get_next_rwi(adapter);
Juliet Kim7ed5b312019-09-20 16:11:23 -04002288
2289 if (rwi && (rwi->reset_reason == VNIC_RESET_FAILOVER ||
2290 rwi->reset_reason == VNIC_RESET_MOBILITY))
2291 adapter->force_reset_recovery = true;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002292 }
2293
John Allenc26eba02017-10-26 16:23:25 -05002294 if (adapter->wait_for_reset) {
John Allenc26eba02017-10-26 16:23:25 -05002295 adapter->reset_done_rc = rc;
2296 complete(&adapter->reset_done);
2297 }
2298
Juliet Kim7ed5b312019-09-20 16:11:23 -04002299 clear_bit_unlock(0, &adapter->resetting);
Sukadev Bhattiprolu38bd5ce2020-12-04 18:22:35 -08002300
2301 netdev_dbg(adapter->netdev,
2302 "[S:%d FRR:%d WFR:%d] Done processing resets\n",
2303 adapter->state, adapter->force_reset_recovery,
2304 adapter->wait_for_reset);
Juliet Kim7ed5b312019-09-20 16:11:23 -04002305}
2306
2307static void __ibmvnic_delayed_reset(struct work_struct *work)
2308{
2309 struct ibmvnic_adapter *adapter;
2310
2311 adapter = container_of(work, struct ibmvnic_adapter,
2312 ibmvnic_delayed_reset.work);
2313 __ibmvnic_reset(&adapter->ibmvnic_reset);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002314}
2315
Thomas Falconaf894d22018-04-06 18:37:04 -05002316static int ibmvnic_reset(struct ibmvnic_adapter *adapter,
2317 enum ibmvnic_reset_reason reason)
Nathan Fontenoted651a12017-05-03 14:04:38 -04002318{
Thomas Falcon2770a792018-05-23 13:38:02 -05002319 struct list_head *entry, *tmp_entry;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002320 struct ibmvnic_rwi *rwi, *tmp;
2321 struct net_device *netdev = adapter->netdev;
Thomas Falcon6c5c7482018-12-10 15:22:22 -06002322 unsigned long flags;
Thomas Falconaf894d22018-04-06 18:37:04 -05002323 int ret;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002324
Sukadev Bhattiprolu1d850492020-10-30 10:07:11 -07002325 /*
2326 * If failover is pending don't schedule any other reset.
2327 * Instead let the failover complete. If there is already a
2328 * a failover reset scheduled, we will detect and drop the
2329 * duplicate reset when walking the ->rwi_list below.
2330 */
Nathan Fontenoted651a12017-05-03 14:04:38 -04002331 if (adapter->state == VNIC_REMOVING ||
Thomas Falcon5a18e1e2018-04-06 18:37:05 -05002332 adapter->state == VNIC_REMOVED ||
Sukadev Bhattiprolu1d850492020-10-30 10:07:11 -07002333 (adapter->failover_pending && reason != VNIC_RESET_FAILOVER)) {
Thomas Falconaf894d22018-04-06 18:37:04 -05002334 ret = EBUSY;
Thomas Falcon5a18e1e2018-04-06 18:37:05 -05002335 netdev_dbg(netdev, "Adapter removing or pending failover, skipping reset\n");
Thomas Falconaf894d22018-04-06 18:37:04 -05002336 goto err;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002337 }
2338
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04002339 if (adapter->state == VNIC_PROBING) {
2340 netdev_warn(netdev, "Adapter reset during probe\n");
Thomas Falconaf894d22018-04-06 18:37:04 -05002341 ret = adapter->init_done_rc = EAGAIN;
2342 goto err;
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04002343 }
2344
Thomas Falcon6c5c7482018-12-10 15:22:22 -06002345 spin_lock_irqsave(&adapter->rwi_lock, flags);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002346
2347 list_for_each(entry, &adapter->rwi_list) {
2348 tmp = list_entry(entry, struct ibmvnic_rwi, list);
2349 if (tmp->reset_reason == reason) {
Sukadev Bhattiprolu38bd5ce2020-12-04 18:22:35 -08002350 netdev_dbg(netdev, "Skipping matching reset, reason=%d\n",
2351 reason);
Thomas Falcon6c5c7482018-12-10 15:22:22 -06002352 spin_unlock_irqrestore(&adapter->rwi_lock, flags);
Thomas Falconaf894d22018-04-06 18:37:04 -05002353 ret = EBUSY;
2354 goto err;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002355 }
2356 }
2357
Thomas Falcon1d1bbc32018-12-10 15:22:23 -06002358 rwi = kzalloc(sizeof(*rwi), GFP_ATOMIC);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002359 if (!rwi) {
Thomas Falcon6c5c7482018-12-10 15:22:22 -06002360 spin_unlock_irqrestore(&adapter->rwi_lock, flags);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002361 ibmvnic_close(netdev);
Thomas Falconaf894d22018-04-06 18:37:04 -05002362 ret = ENOMEM;
2363 goto err;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002364 }
Thomas Falcon2770a792018-05-23 13:38:02 -05002365 /* if we just received a transport event,
2366 * flush reset queue and process this reset
2367 */
2368 if (adapter->force_reset_recovery && !list_empty(&adapter->rwi_list)) {
2369 list_for_each_safe(entry, tmp_entry, &adapter->rwi_list)
2370 list_del(entry);
2371 }
Nathan Fontenoted651a12017-05-03 14:04:38 -04002372 rwi->reset_reason = reason;
2373 list_add_tail(&rwi->list, &adapter->rwi_list);
Thomas Falcon6c5c7482018-12-10 15:22:22 -06002374 spin_unlock_irqrestore(&adapter->rwi_lock, flags);
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002375 netdev_dbg(adapter->netdev, "Scheduling reset (reason %d)\n", reason);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002376 schedule_work(&adapter->ibmvnic_reset);
Thomas Falconaf894d22018-04-06 18:37:04 -05002377
2378 return 0;
2379err:
Thomas Falconaf894d22018-04-06 18:37:04 -05002380 return -ret;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002381}
2382
Michael S. Tsirkin0290bd22019-12-10 09:23:51 -05002383static void ibmvnic_tx_timeout(struct net_device *dev, unsigned int txqueue)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002384{
2385 struct ibmvnic_adapter *adapter = netdev_priv(dev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002386
Lijun Pan855a6312020-11-20 16:40:13 -06002387 if (test_bit(0, &adapter->resetting)) {
2388 netdev_err(adapter->netdev,
2389 "Adapter is resetting, skip timeout reset\n");
2390 return;
2391 }
Dany Maddena86d5c62020-11-25 18:04:31 -06002392 /* No queuing up reset until at least 5 seconds (default watchdog val)
2393 * after last reset
2394 */
2395 if (time_before(jiffies, (adapter->last_reset_time + dev->watchdog_timeo))) {
2396 netdev_dbg(dev, "Not yet time to tx timeout.\n");
2397 return;
2398 }
Nathan Fontenoted651a12017-05-03 14:04:38 -04002399 ibmvnic_reset(adapter, VNIC_RESET_TIMEOUT);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002400}
2401
2402static void remove_buff_from_pool(struct ibmvnic_adapter *adapter,
2403 struct ibmvnic_rx_buff *rx_buff)
2404{
2405 struct ibmvnic_rx_pool *pool = &adapter->rx_pool[rx_buff->pool_index];
2406
2407 rx_buff->skb = NULL;
2408
2409 pool->free_map[pool->next_alloc] = (int)(rx_buff - pool->rx_buff);
2410 pool->next_alloc = (pool->next_alloc + 1) % pool->size;
2411
2412 atomic_dec(&pool->available);
2413}
2414
2415static int ibmvnic_poll(struct napi_struct *napi, int budget)
2416{
Dwip N. Banerjeeec20f362020-11-18 19:12:23 -06002417 struct ibmvnic_sub_crq_queue *rx_scrq;
2418 struct ibmvnic_adapter *adapter;
2419 struct net_device *netdev;
2420 int frames_processed;
2421 int scrq_num;
2422
2423 netdev = napi->dev;
2424 adapter = netdev_priv(netdev);
2425 scrq_num = (int)(napi - adapter->napi);
2426 frames_processed = 0;
2427 rx_scrq = adapter->rx_scrq[scrq_num];
Nathan Fontenot152ce472017-05-26 10:30:54 -04002428
Thomas Falcon032c5e82015-12-21 11:26:06 -06002429restart_poll:
2430 while (frames_processed < budget) {
2431 struct sk_buff *skb;
2432 struct ibmvnic_rx_buff *rx_buff;
2433 union sub_crq *next;
2434 u32 length;
2435 u16 offset;
2436 u8 flags = 0;
2437
Juliet Kim7ed5b312019-09-20 16:11:23 -04002438 if (unlikely(test_bit(0, &adapter->resetting) &&
John Allen34686562018-02-06 16:21:49 -06002439 adapter->reset_reason != VNIC_RESET_NON_FATAL)) {
Dwip N. Banerjeeec20f362020-11-18 19:12:23 -06002440 enable_scrq_irq(adapter, rx_scrq);
Thomas Falcon21ecba62017-06-14 23:50:09 -05002441 napi_complete_done(napi, frames_processed);
2442 return frames_processed;
2443 }
2444
Dwip N. Banerjeeec20f362020-11-18 19:12:23 -06002445 if (!pending_scrq(adapter, rx_scrq))
Thomas Falcon032c5e82015-12-21 11:26:06 -06002446 break;
Dwip N. Banerjeeec20f362020-11-18 19:12:23 -06002447 next = ibmvnic_next_scrq(adapter, rx_scrq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002448 rx_buff =
2449 (struct ibmvnic_rx_buff *)be64_to_cpu(next->
2450 rx_comp.correlator);
2451 /* do error checking */
2452 if (next->rx_comp.rc) {
John Allene1cea2e2017-08-07 15:42:30 -05002453 netdev_dbg(netdev, "rx buffer returned with rc %x\n",
2454 be16_to_cpu(next->rx_comp.rc));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002455 /* free the entry */
2456 next->rx_comp.first = 0;
Thomas Falcon4b9b0f02018-02-13 18:23:42 -06002457 dev_kfree_skb_any(rx_buff->skb);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002458 remove_buff_from_pool(adapter, rx_buff);
Nathan Fontenotca05e312017-05-03 14:05:14 -04002459 continue;
Thomas Falconabe27a82018-02-19 20:12:57 -06002460 } else if (!rx_buff->skb) {
2461 /* free the entry */
2462 next->rx_comp.first = 0;
2463 remove_buff_from_pool(adapter, rx_buff);
2464 continue;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002465 }
2466
2467 length = be32_to_cpu(next->rx_comp.len);
2468 offset = be16_to_cpu(next->rx_comp.off_frame_data);
2469 flags = next->rx_comp.flags;
2470 skb = rx_buff->skb;
2471 skb_copy_to_linear_data(skb, rx_buff->data + offset,
2472 length);
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04002473
2474 /* VLAN Header has been stripped by the system firmware and
2475 * needs to be inserted by the driver
2476 */
2477 if (adapter->rx_vlan_header_insertion &&
2478 (flags & IBMVNIC_VLAN_STRIPPED))
2479 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
2480 ntohs(next->rx_comp.vlan_tci));
2481
Thomas Falcon032c5e82015-12-21 11:26:06 -06002482 /* free the entry */
2483 next->rx_comp.first = 0;
2484 remove_buff_from_pool(adapter, rx_buff);
2485
2486 skb_put(skb, length);
2487 skb->protocol = eth_type_trans(skb, netdev);
Thomas Falcon94ca3052017-05-03 14:05:20 -04002488 skb_record_rx_queue(skb, scrq_num);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002489
2490 if (flags & IBMVNIC_IP_CHKSUM_GOOD &&
2491 flags & IBMVNIC_TCP_UDP_CHKSUM_GOOD) {
2492 skb->ip_summed = CHECKSUM_UNNECESSARY;
2493 }
2494
2495 length = skb->len;
2496 napi_gro_receive(napi, skb); /* send it up */
2497 netdev->stats.rx_packets++;
2498 netdev->stats.rx_bytes += length;
John Allen3d52b592017-08-02 16:44:14 -05002499 adapter->rx_stats_buffers[scrq_num].packets++;
2500 adapter->rx_stats_buffers[scrq_num].bytes += length;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002501 frames_processed++;
2502 }
Nathan Fontenot152ce472017-05-26 10:30:54 -04002503
Dwip N. Banerjee41ed0a02020-11-18 19:12:25 -06002504 if (adapter->state != VNIC_CLOSING &&
2505 ((atomic_read(&adapter->rx_pool[scrq_num].available) <
2506 adapter->req_rx_add_entries_per_subcrq / 2) ||
2507 frames_processed < budget))
Nathan Fontenot152ce472017-05-26 10:30:54 -04002508 replenish_rx_pool(adapter, &adapter->rx_pool[scrq_num]);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002509 if (frames_processed < budget) {
Dwip N. Banerjeeec20f362020-11-18 19:12:23 -06002510 if (napi_complete_done(napi, frames_processed)) {
2511 enable_scrq_irq(adapter, rx_scrq);
2512 if (pending_scrq(adapter, rx_scrq)) {
Dwip N. Banerjeeec20f362020-11-18 19:12:23 -06002513 if (napi_reschedule(napi)) {
2514 disable_scrq_irq(adapter, rx_scrq);
2515 goto restart_poll;
2516 }
2517 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002518 }
2519 }
2520 return frames_processed;
2521}
2522
John Allenc26eba02017-10-26 16:23:25 -05002523static int wait_for_reset(struct ibmvnic_adapter *adapter)
2524{
Thomas Falconaf894d22018-04-06 18:37:04 -05002525 int rc, ret;
2526
John Allenc26eba02017-10-26 16:23:25 -05002527 adapter->fallback.mtu = adapter->req_mtu;
2528 adapter->fallback.rx_queues = adapter->req_rx_queues;
2529 adapter->fallback.tx_queues = adapter->req_tx_queues;
2530 adapter->fallback.rx_entries = adapter->req_rx_add_entries_per_subcrq;
2531 adapter->fallback.tx_entries = adapter->req_tx_entries_per_subcrq;
2532
Thomas Falcon070eca92019-11-25 17:12:53 -06002533 reinit_completion(&adapter->reset_done);
John Allenc26eba02017-10-26 16:23:25 -05002534 adapter->wait_for_reset = true;
Thomas Falconaf894d22018-04-06 18:37:04 -05002535 rc = ibmvnic_reset(adapter, VNIC_RESET_CHANGE_PARAM);
Thomas Falcon476d96c2019-11-25 17:12:55 -06002536
2537 if (rc) {
2538 ret = rc;
2539 goto out;
2540 }
2541 rc = ibmvnic_wait_for_completion(adapter, &adapter->reset_done, 60000);
2542 if (rc) {
2543 ret = -ENODEV;
2544 goto out;
2545 }
John Allenc26eba02017-10-26 16:23:25 -05002546
Thomas Falconaf894d22018-04-06 18:37:04 -05002547 ret = 0;
John Allenc26eba02017-10-26 16:23:25 -05002548 if (adapter->reset_done_rc) {
Thomas Falconaf894d22018-04-06 18:37:04 -05002549 ret = -EIO;
John Allenc26eba02017-10-26 16:23:25 -05002550 adapter->desired.mtu = adapter->fallback.mtu;
2551 adapter->desired.rx_queues = adapter->fallback.rx_queues;
2552 adapter->desired.tx_queues = adapter->fallback.tx_queues;
2553 adapter->desired.rx_entries = adapter->fallback.rx_entries;
2554 adapter->desired.tx_entries = adapter->fallback.tx_entries;
2555
Thomas Falcon070eca92019-11-25 17:12:53 -06002556 reinit_completion(&adapter->reset_done);
Thomas Falconaf894d22018-04-06 18:37:04 -05002557 adapter->wait_for_reset = true;
2558 rc = ibmvnic_reset(adapter, VNIC_RESET_CHANGE_PARAM);
Thomas Falcon476d96c2019-11-25 17:12:55 -06002559 if (rc) {
2560 ret = rc;
2561 goto out;
2562 }
2563 rc = ibmvnic_wait_for_completion(adapter, &adapter->reset_done,
2564 60000);
2565 if (rc) {
2566 ret = -ENODEV;
2567 goto out;
2568 }
John Allenc26eba02017-10-26 16:23:25 -05002569 }
Thomas Falcon476d96c2019-11-25 17:12:55 -06002570out:
John Allenc26eba02017-10-26 16:23:25 -05002571 adapter->wait_for_reset = false;
2572
Thomas Falconaf894d22018-04-06 18:37:04 -05002573 return ret;
John Allenc26eba02017-10-26 16:23:25 -05002574}
2575
John Allen3a807b72017-06-06 16:55:52 -05002576static int ibmvnic_change_mtu(struct net_device *netdev, int new_mtu)
2577{
John Allenc26eba02017-10-26 16:23:25 -05002578 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2579
2580 adapter->desired.mtu = new_mtu + ETH_HLEN;
2581
2582 return wait_for_reset(adapter);
John Allen3a807b72017-06-06 16:55:52 -05002583}
2584
Thomas Falconf10b09e2018-03-12 11:51:05 -05002585static netdev_features_t ibmvnic_features_check(struct sk_buff *skb,
2586 struct net_device *dev,
2587 netdev_features_t features)
2588{
2589 /* Some backing hardware adapters can not
2590 * handle packets with a MSS less than 224
2591 * or with only one segment.
2592 */
2593 if (skb_is_gso(skb)) {
2594 if (skb_shinfo(skb)->gso_size < 224 ||
2595 skb_shinfo(skb)->gso_segs == 1)
2596 features &= ~NETIF_F_GSO_MASK;
2597 }
2598
2599 return features;
2600}
2601
Thomas Falcon032c5e82015-12-21 11:26:06 -06002602static const struct net_device_ops ibmvnic_netdev_ops = {
2603 .ndo_open = ibmvnic_open,
2604 .ndo_stop = ibmvnic_close,
2605 .ndo_start_xmit = ibmvnic_xmit,
2606 .ndo_set_rx_mode = ibmvnic_set_multi,
2607 .ndo_set_mac_address = ibmvnic_set_mac,
2608 .ndo_validate_addr = eth_validate_addr,
Thomas Falcon032c5e82015-12-21 11:26:06 -06002609 .ndo_tx_timeout = ibmvnic_tx_timeout,
John Allen3a807b72017-06-06 16:55:52 -05002610 .ndo_change_mtu = ibmvnic_change_mtu,
Thomas Falconf10b09e2018-03-12 11:51:05 -05002611 .ndo_features_check = ibmvnic_features_check,
Thomas Falcon032c5e82015-12-21 11:26:06 -06002612};
2613
2614/* ethtool functions */
2615
Philippe Reynes8a433792017-01-07 22:37:29 +01002616static int ibmvnic_get_link_ksettings(struct net_device *netdev,
2617 struct ethtool_link_ksettings *cmd)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002618{
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03002619 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2620 int rc;
Philippe Reynes8a433792017-01-07 22:37:29 +01002621
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03002622 rc = send_query_phys_parms(adapter);
2623 if (rc) {
2624 adapter->speed = SPEED_UNKNOWN;
2625 adapter->duplex = DUPLEX_UNKNOWN;
2626 }
2627 cmd->base.speed = adapter->speed;
2628 cmd->base.duplex = adapter->duplex;
Philippe Reynes8a433792017-01-07 22:37:29 +01002629 cmd->base.port = PORT_FIBRE;
2630 cmd->base.phy_address = 0;
2631 cmd->base.autoneg = AUTONEG_ENABLE;
2632
Thomas Falcon032c5e82015-12-21 11:26:06 -06002633 return 0;
2634}
2635
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02002636static void ibmvnic_get_drvinfo(struct net_device *netdev,
Thomas Falcon032c5e82015-12-21 11:26:06 -06002637 struct ethtool_drvinfo *info)
2638{
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02002639 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2640
Thomas Falcon032c5e82015-12-21 11:26:06 -06002641 strlcpy(info->driver, ibmvnic_driver_name, sizeof(info->driver));
2642 strlcpy(info->version, IBMVNIC_DRIVER_VERSION, sizeof(info->version));
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02002643 strlcpy(info->fw_version, adapter->fw_version,
2644 sizeof(info->fw_version));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002645}
2646
2647static u32 ibmvnic_get_msglevel(struct net_device *netdev)
2648{
2649 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2650
2651 return adapter->msg_enable;
2652}
2653
2654static void ibmvnic_set_msglevel(struct net_device *netdev, u32 data)
2655{
2656 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2657
2658 adapter->msg_enable = data;
2659}
2660
2661static u32 ibmvnic_get_link(struct net_device *netdev)
2662{
2663 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2664
2665 /* Don't need to send a query because we request a logical link up at
2666 * init and then we wait for link state indications
2667 */
2668 return adapter->logical_link_state;
2669}
2670
2671static void ibmvnic_get_ringparam(struct net_device *netdev,
2672 struct ethtool_ringparam *ring)
2673{
John Allenbc131b32017-08-02 16:46:30 -05002674 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2675
Thomas Falcon723ad912018-09-28 18:38:26 -05002676 if (adapter->priv_flags & IBMVNIC_USE_SERVER_MAXES) {
2677 ring->rx_max_pending = adapter->max_rx_add_entries_per_subcrq;
2678 ring->tx_max_pending = adapter->max_tx_entries_per_subcrq;
2679 } else {
2680 ring->rx_max_pending = IBMVNIC_MAX_QUEUE_SZ;
2681 ring->tx_max_pending = IBMVNIC_MAX_QUEUE_SZ;
2682 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002683 ring->rx_mini_max_pending = 0;
2684 ring->rx_jumbo_max_pending = 0;
John Allenbc131b32017-08-02 16:46:30 -05002685 ring->rx_pending = adapter->req_rx_add_entries_per_subcrq;
2686 ring->tx_pending = adapter->req_tx_entries_per_subcrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002687 ring->rx_mini_pending = 0;
2688 ring->rx_jumbo_pending = 0;
2689}
2690
John Allenc26eba02017-10-26 16:23:25 -05002691static int ibmvnic_set_ringparam(struct net_device *netdev,
2692 struct ethtool_ringparam *ring)
2693{
2694 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Thomas Falcon723ad912018-09-28 18:38:26 -05002695 int ret;
John Allenc26eba02017-10-26 16:23:25 -05002696
Thomas Falcon723ad912018-09-28 18:38:26 -05002697 ret = 0;
John Allenc26eba02017-10-26 16:23:25 -05002698 adapter->desired.rx_entries = ring->rx_pending;
2699 adapter->desired.tx_entries = ring->tx_pending;
2700
Thomas Falcon723ad912018-09-28 18:38:26 -05002701 ret = wait_for_reset(adapter);
2702
2703 if (!ret &&
2704 (adapter->req_rx_add_entries_per_subcrq != ring->rx_pending ||
2705 adapter->req_tx_entries_per_subcrq != ring->tx_pending))
2706 netdev_info(netdev,
2707 "Could not match full ringsize request. Requested: RX %d, TX %d; Allowed: RX %llu, TX %llu\n",
2708 ring->rx_pending, ring->tx_pending,
2709 adapter->req_rx_add_entries_per_subcrq,
2710 adapter->req_tx_entries_per_subcrq);
2711 return ret;
John Allenc26eba02017-10-26 16:23:25 -05002712}
2713
John Allenc2dbeb62017-08-02 16:47:17 -05002714static void ibmvnic_get_channels(struct net_device *netdev,
2715 struct ethtool_channels *channels)
2716{
2717 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2718
Thomas Falcon723ad912018-09-28 18:38:26 -05002719 if (adapter->priv_flags & IBMVNIC_USE_SERVER_MAXES) {
2720 channels->max_rx = adapter->max_rx_queues;
2721 channels->max_tx = adapter->max_tx_queues;
2722 } else {
2723 channels->max_rx = IBMVNIC_MAX_QUEUES;
2724 channels->max_tx = IBMVNIC_MAX_QUEUES;
2725 }
2726
John Allenc2dbeb62017-08-02 16:47:17 -05002727 channels->max_other = 0;
2728 channels->max_combined = 0;
2729 channels->rx_count = adapter->req_rx_queues;
2730 channels->tx_count = adapter->req_tx_queues;
2731 channels->other_count = 0;
2732 channels->combined_count = 0;
2733}
2734
John Allenc26eba02017-10-26 16:23:25 -05002735static int ibmvnic_set_channels(struct net_device *netdev,
2736 struct ethtool_channels *channels)
2737{
2738 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Thomas Falcon723ad912018-09-28 18:38:26 -05002739 int ret;
John Allenc26eba02017-10-26 16:23:25 -05002740
Thomas Falcon723ad912018-09-28 18:38:26 -05002741 ret = 0;
John Allenc26eba02017-10-26 16:23:25 -05002742 adapter->desired.rx_queues = channels->rx_count;
2743 adapter->desired.tx_queues = channels->tx_count;
2744
Thomas Falcon723ad912018-09-28 18:38:26 -05002745 ret = wait_for_reset(adapter);
2746
2747 if (!ret &&
2748 (adapter->req_rx_queues != channels->rx_count ||
2749 adapter->req_tx_queues != channels->tx_count))
2750 netdev_info(netdev,
2751 "Could not match full channels request. Requested: RX %d, TX %d; Allowed: RX %llu, TX %llu\n",
2752 channels->rx_count, channels->tx_count,
2753 adapter->req_rx_queues, adapter->req_tx_queues);
2754 return ret;
2755
John Allenc26eba02017-10-26 16:23:25 -05002756}
2757
Thomas Falcon032c5e82015-12-21 11:26:06 -06002758static void ibmvnic_get_strings(struct net_device *dev, u32 stringset, u8 *data)
2759{
John Allen3d52b592017-08-02 16:44:14 -05002760 struct ibmvnic_adapter *adapter = netdev_priv(dev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002761 int i;
2762
Thomas Falcon723ad912018-09-28 18:38:26 -05002763 switch (stringset) {
2764 case ETH_SS_STATS:
2765 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats);
2766 i++, data += ETH_GSTRING_LEN)
2767 memcpy(data, ibmvnic_stats[i].name, ETH_GSTRING_LEN);
2768
2769 for (i = 0; i < adapter->req_tx_queues; i++) {
2770 snprintf(data, ETH_GSTRING_LEN, "tx%d_packets", i);
2771 data += ETH_GSTRING_LEN;
2772
2773 snprintf(data, ETH_GSTRING_LEN, "tx%d_bytes", i);
2774 data += ETH_GSTRING_LEN;
2775
2776 snprintf(data, ETH_GSTRING_LEN,
2777 "tx%d_dropped_packets", i);
2778 data += ETH_GSTRING_LEN;
2779 }
2780
2781 for (i = 0; i < adapter->req_rx_queues; i++) {
2782 snprintf(data, ETH_GSTRING_LEN, "rx%d_packets", i);
2783 data += ETH_GSTRING_LEN;
2784
2785 snprintf(data, ETH_GSTRING_LEN, "rx%d_bytes", i);
2786 data += ETH_GSTRING_LEN;
2787
2788 snprintf(data, ETH_GSTRING_LEN, "rx%d_interrupts", i);
2789 data += ETH_GSTRING_LEN;
2790 }
2791 break;
2792
2793 case ETH_SS_PRIV_FLAGS:
2794 for (i = 0; i < ARRAY_SIZE(ibmvnic_priv_flags); i++)
2795 strcpy(data + i * ETH_GSTRING_LEN,
2796 ibmvnic_priv_flags[i]);
2797 break;
2798 default:
Thomas Falcon032c5e82015-12-21 11:26:06 -06002799 return;
John Allen3d52b592017-08-02 16:44:14 -05002800 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002801}
2802
2803static int ibmvnic_get_sset_count(struct net_device *dev, int sset)
2804{
John Allen3d52b592017-08-02 16:44:14 -05002805 struct ibmvnic_adapter *adapter = netdev_priv(dev);
2806
Thomas Falcon032c5e82015-12-21 11:26:06 -06002807 switch (sset) {
2808 case ETH_SS_STATS:
John Allen3d52b592017-08-02 16:44:14 -05002809 return ARRAY_SIZE(ibmvnic_stats) +
2810 adapter->req_tx_queues * NUM_TX_STATS +
2811 adapter->req_rx_queues * NUM_RX_STATS;
Thomas Falcon723ad912018-09-28 18:38:26 -05002812 case ETH_SS_PRIV_FLAGS:
2813 return ARRAY_SIZE(ibmvnic_priv_flags);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002814 default:
2815 return -EOPNOTSUPP;
2816 }
2817}
2818
2819static void ibmvnic_get_ethtool_stats(struct net_device *dev,
2820 struct ethtool_stats *stats, u64 *data)
2821{
2822 struct ibmvnic_adapter *adapter = netdev_priv(dev);
2823 union ibmvnic_crq crq;
John Allen3d52b592017-08-02 16:44:14 -05002824 int i, j;
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05002825 int rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002826
2827 memset(&crq, 0, sizeof(crq));
2828 crq.request_statistics.first = IBMVNIC_CRQ_CMD;
2829 crq.request_statistics.cmd = REQUEST_STATISTICS;
2830 crq.request_statistics.ioba = cpu_to_be32(adapter->stats_token);
2831 crq.request_statistics.len =
2832 cpu_to_be32(sizeof(struct ibmvnic_statistics));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002833
2834 /* Wait for data to be written */
Thomas Falcon070eca92019-11-25 17:12:53 -06002835 reinit_completion(&adapter->stats_done);
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05002836 rc = ibmvnic_send_crq(adapter, &crq);
2837 if (rc)
2838 return;
Thomas Falcon476d96c2019-11-25 17:12:55 -06002839 rc = ibmvnic_wait_for_completion(adapter, &adapter->stats_done, 10000);
2840 if (rc)
2841 return;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002842
2843 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++)
John Allen52da5c12017-08-02 16:45:28 -05002844 data[i] = be64_to_cpu(IBMVNIC_GET_STAT(adapter,
2845 ibmvnic_stats[i].offset));
John Allen3d52b592017-08-02 16:44:14 -05002846
2847 for (j = 0; j < adapter->req_tx_queues; j++) {
2848 data[i] = adapter->tx_stats_buffers[j].packets;
2849 i++;
2850 data[i] = adapter->tx_stats_buffers[j].bytes;
2851 i++;
2852 data[i] = adapter->tx_stats_buffers[j].dropped_packets;
2853 i++;
2854 }
2855
2856 for (j = 0; j < adapter->req_rx_queues; j++) {
2857 data[i] = adapter->rx_stats_buffers[j].packets;
2858 i++;
2859 data[i] = adapter->rx_stats_buffers[j].bytes;
2860 i++;
2861 data[i] = adapter->rx_stats_buffers[j].interrupts;
2862 i++;
2863 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002864}
2865
Thomas Falcon723ad912018-09-28 18:38:26 -05002866static u32 ibmvnic_get_priv_flags(struct net_device *netdev)
2867{
2868 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2869
2870 return adapter->priv_flags;
2871}
2872
2873static int ibmvnic_set_priv_flags(struct net_device *netdev, u32 flags)
2874{
2875 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2876 bool which_maxes = !!(flags & IBMVNIC_USE_SERVER_MAXES);
2877
2878 if (which_maxes)
2879 adapter->priv_flags |= IBMVNIC_USE_SERVER_MAXES;
2880 else
2881 adapter->priv_flags &= ~IBMVNIC_USE_SERVER_MAXES;
2882
2883 return 0;
2884}
Thomas Falcon032c5e82015-12-21 11:26:06 -06002885static const struct ethtool_ops ibmvnic_ethtool_ops = {
Thomas Falcon032c5e82015-12-21 11:26:06 -06002886 .get_drvinfo = ibmvnic_get_drvinfo,
2887 .get_msglevel = ibmvnic_get_msglevel,
2888 .set_msglevel = ibmvnic_set_msglevel,
2889 .get_link = ibmvnic_get_link,
2890 .get_ringparam = ibmvnic_get_ringparam,
John Allenc26eba02017-10-26 16:23:25 -05002891 .set_ringparam = ibmvnic_set_ringparam,
John Allenc2dbeb62017-08-02 16:47:17 -05002892 .get_channels = ibmvnic_get_channels,
John Allenc26eba02017-10-26 16:23:25 -05002893 .set_channels = ibmvnic_set_channels,
Thomas Falcon032c5e82015-12-21 11:26:06 -06002894 .get_strings = ibmvnic_get_strings,
2895 .get_sset_count = ibmvnic_get_sset_count,
2896 .get_ethtool_stats = ibmvnic_get_ethtool_stats,
Philippe Reynes8a433792017-01-07 22:37:29 +01002897 .get_link_ksettings = ibmvnic_get_link_ksettings,
Thomas Falcon723ad912018-09-28 18:38:26 -05002898 .get_priv_flags = ibmvnic_get_priv_flags,
2899 .set_priv_flags = ibmvnic_set_priv_flags,
Thomas Falcon032c5e82015-12-21 11:26:06 -06002900};
2901
2902/* Routines for managing CRQs/sCRQs */
2903
Nathan Fontenot57a49432017-05-26 10:31:12 -04002904static int reset_one_sub_crq_queue(struct ibmvnic_adapter *adapter,
2905 struct ibmvnic_sub_crq_queue *scrq)
2906{
2907 int rc;
2908
Dany Madden9281cf22020-11-25 18:04:26 -06002909 if (!scrq) {
YANG LI862aecb2020-12-30 15:23:14 +08002910 netdev_dbg(adapter->netdev, "Invalid scrq reset.\n");
Dany Madden9281cf22020-11-25 18:04:26 -06002911 return -EINVAL;
2912 }
2913
Nathan Fontenot57a49432017-05-26 10:31:12 -04002914 if (scrq->irq) {
2915 free_irq(scrq->irq, scrq);
2916 irq_dispose_mapping(scrq->irq);
2917 scrq->irq = 0;
2918 }
2919
Dany Madden9281cf22020-11-25 18:04:26 -06002920 if (scrq->msgs) {
2921 memset(scrq->msgs, 0, 4 * PAGE_SIZE);
2922 atomic_set(&scrq->used, 0);
2923 scrq->cur = 0;
Jakub Kicinski55fd59b2020-12-03 15:42:13 -08002924 scrq->ind_buf.index = 0;
Dany Madden9281cf22020-11-25 18:04:26 -06002925 } else {
2926 netdev_dbg(adapter->netdev, "Invalid scrq reset\n");
2927 return -EINVAL;
2928 }
Nathan Fontenot57a49432017-05-26 10:31:12 -04002929
2930 rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
2931 4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
2932 return rc;
2933}
2934
2935static int reset_sub_crq_queues(struct ibmvnic_adapter *adapter)
2936{
2937 int i, rc;
2938
Lijun Pana0faaa22020-11-23 13:35:45 -06002939 if (!adapter->tx_scrq || !adapter->rx_scrq)
2940 return -EINVAL;
2941
Nathan Fontenot57a49432017-05-26 10:31:12 -04002942 for (i = 0; i < adapter->req_tx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002943 netdev_dbg(adapter->netdev, "Re-setting tx_scrq[%d]\n", i);
Nathan Fontenot57a49432017-05-26 10:31:12 -04002944 rc = reset_one_sub_crq_queue(adapter, adapter->tx_scrq[i]);
2945 if (rc)
2946 return rc;
2947 }
2948
2949 for (i = 0; i < adapter->req_rx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002950 netdev_dbg(adapter->netdev, "Re-setting rx_scrq[%d]\n", i);
Nathan Fontenot57a49432017-05-26 10:31:12 -04002951 rc = reset_one_sub_crq_queue(adapter, adapter->rx_scrq[i]);
2952 if (rc)
2953 return rc;
2954 }
2955
Nathan Fontenot57a49432017-05-26 10:31:12 -04002956 return rc;
2957}
2958
Thomas Falcon032c5e82015-12-21 11:26:06 -06002959static void release_sub_crq_queue(struct ibmvnic_adapter *adapter,
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06002960 struct ibmvnic_sub_crq_queue *scrq,
2961 bool do_h_free)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002962{
2963 struct device *dev = &adapter->vdev->dev;
2964 long rc;
2965
2966 netdev_dbg(adapter->netdev, "Releasing sub-CRQ\n");
2967
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06002968 if (do_h_free) {
2969 /* Close the sub-crqs */
2970 do {
2971 rc = plpar_hcall_norets(H_FREE_SUB_CRQ,
2972 adapter->vdev->unit_address,
2973 scrq->crq_num);
2974 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002975
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06002976 if (rc) {
2977 netdev_err(adapter->netdev,
2978 "Failed to release sub-CRQ %16lx, rc = %ld\n",
2979 scrq->crq_num, rc);
2980 }
Thomas Falconffa73852017-04-19 13:44:29 -04002981 }
2982
Thomas Falconf019fb62020-11-18 19:12:17 -06002983 dma_free_coherent(dev,
2984 IBMVNIC_IND_ARR_SZ,
2985 scrq->ind_buf.indir_arr,
2986 scrq->ind_buf.indir_dma);
2987
Thomas Falcon032c5e82015-12-21 11:26:06 -06002988 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
2989 DMA_BIDIRECTIONAL);
2990 free_pages((unsigned long)scrq->msgs, 2);
2991 kfree(scrq);
2992}
2993
2994static struct ibmvnic_sub_crq_queue *init_sub_crq_queue(struct ibmvnic_adapter
2995 *adapter)
2996{
2997 struct device *dev = &adapter->vdev->dev;
2998 struct ibmvnic_sub_crq_queue *scrq;
2999 int rc;
3000
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04003001 scrq = kzalloc(sizeof(*scrq), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003002 if (!scrq)
3003 return NULL;
3004
Nathan Fontenot7f7adc52017-04-19 13:45:16 -04003005 scrq->msgs =
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04003006 (union sub_crq *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, 2);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003007 if (!scrq->msgs) {
3008 dev_warn(dev, "Couldn't allocate crq queue messages page\n");
3009 goto zero_page_failed;
3010 }
3011
3012 scrq->msg_token = dma_map_single(dev, scrq->msgs, 4 * PAGE_SIZE,
3013 DMA_BIDIRECTIONAL);
3014 if (dma_mapping_error(dev, scrq->msg_token)) {
3015 dev_warn(dev, "Couldn't map crq queue messages page\n");
3016 goto map_failed;
3017 }
3018
3019 rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
3020 4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
3021
3022 if (rc == H_RESOURCE)
3023 rc = ibmvnic_reset_crq(adapter);
3024
3025 if (rc == H_CLOSED) {
3026 dev_warn(dev, "Partner adapter not ready, waiting.\n");
3027 } else if (rc) {
3028 dev_warn(dev, "Error %d registering sub-crq\n", rc);
3029 goto reg_failed;
3030 }
3031
Thomas Falcon032c5e82015-12-21 11:26:06 -06003032 scrq->adapter = adapter;
3033 scrq->size = 4 * PAGE_SIZE / sizeof(*scrq->msgs);
Thomas Falconf019fb62020-11-18 19:12:17 -06003034 scrq->ind_buf.index = 0;
3035
3036 scrq->ind_buf.indir_arr =
3037 dma_alloc_coherent(dev,
3038 IBMVNIC_IND_ARR_SZ,
3039 &scrq->ind_buf.indir_dma,
3040 GFP_KERNEL);
3041
3042 if (!scrq->ind_buf.indir_arr)
3043 goto indir_failed;
3044
Thomas Falcon032c5e82015-12-21 11:26:06 -06003045 spin_lock_init(&scrq->lock);
3046
3047 netdev_dbg(adapter->netdev,
3048 "sub-crq initialized, num %lx, hw_irq=%lx, irq=%x\n",
3049 scrq->crq_num, scrq->hw_irq, scrq->irq);
3050
3051 return scrq;
3052
Thomas Falconf019fb62020-11-18 19:12:17 -06003053indir_failed:
3054 do {
3055 rc = plpar_hcall_norets(H_FREE_SUB_CRQ,
3056 adapter->vdev->unit_address,
3057 scrq->crq_num);
3058 } while (rc == H_BUSY || rc == H_IS_LONG_BUSY(rc));
Thomas Falcon032c5e82015-12-21 11:26:06 -06003059reg_failed:
3060 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
3061 DMA_BIDIRECTIONAL);
3062map_failed:
3063 free_pages((unsigned long)scrq->msgs, 2);
3064zero_page_failed:
3065 kfree(scrq);
3066
3067 return NULL;
3068}
3069
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06003070static void release_sub_crqs(struct ibmvnic_adapter *adapter, bool do_h_free)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003071{
3072 int i;
3073
3074 if (adapter->tx_scrq) {
Nathan Fontenot82e3be32018-02-21 21:33:56 -06003075 for (i = 0; i < adapter->num_active_tx_scrqs; i++) {
Nathan Fontenotb5108882017-03-30 02:49:18 -04003076 if (!adapter->tx_scrq[i])
3077 continue;
3078
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05003079 netdev_dbg(adapter->netdev, "Releasing tx_scrq[%d]\n",
3080 i);
Nathan Fontenotb5108882017-03-30 02:49:18 -04003081 if (adapter->tx_scrq[i]->irq) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06003082 free_irq(adapter->tx_scrq[i]->irq,
3083 adapter->tx_scrq[i]);
Thomas Falcon88eb98a2016-07-06 15:35:16 -05003084 irq_dispose_mapping(adapter->tx_scrq[i]->irq);
Nathan Fontenotb5108882017-03-30 02:49:18 -04003085 adapter->tx_scrq[i]->irq = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003086 }
Nathan Fontenotb5108882017-03-30 02:49:18 -04003087
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06003088 release_sub_crq_queue(adapter, adapter->tx_scrq[i],
3089 do_h_free);
Nathan Fontenotb5108882017-03-30 02:49:18 -04003090 }
3091
Nathan Fontenot9501df32017-03-15 23:38:07 -04003092 kfree(adapter->tx_scrq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003093 adapter->tx_scrq = NULL;
Nathan Fontenot82e3be32018-02-21 21:33:56 -06003094 adapter->num_active_tx_scrqs = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003095 }
3096
3097 if (adapter->rx_scrq) {
Nathan Fontenot82e3be32018-02-21 21:33:56 -06003098 for (i = 0; i < adapter->num_active_rx_scrqs; i++) {
Nathan Fontenotb5108882017-03-30 02:49:18 -04003099 if (!adapter->rx_scrq[i])
3100 continue;
3101
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05003102 netdev_dbg(adapter->netdev, "Releasing rx_scrq[%d]\n",
3103 i);
Nathan Fontenotb5108882017-03-30 02:49:18 -04003104 if (adapter->rx_scrq[i]->irq) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06003105 free_irq(adapter->rx_scrq[i]->irq,
3106 adapter->rx_scrq[i]);
Thomas Falcon88eb98a2016-07-06 15:35:16 -05003107 irq_dispose_mapping(adapter->rx_scrq[i]->irq);
Nathan Fontenotb5108882017-03-30 02:49:18 -04003108 adapter->rx_scrq[i]->irq = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003109 }
Nathan Fontenotb5108882017-03-30 02:49:18 -04003110
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06003111 release_sub_crq_queue(adapter, adapter->rx_scrq[i],
3112 do_h_free);
Nathan Fontenotb5108882017-03-30 02:49:18 -04003113 }
3114
Nathan Fontenot9501df32017-03-15 23:38:07 -04003115 kfree(adapter->rx_scrq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003116 adapter->rx_scrq = NULL;
Nathan Fontenot82e3be32018-02-21 21:33:56 -06003117 adapter->num_active_rx_scrqs = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003118 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06003119}
3120
3121static int disable_scrq_irq(struct ibmvnic_adapter *adapter,
3122 struct ibmvnic_sub_crq_queue *scrq)
3123{
3124 struct device *dev = &adapter->vdev->dev;
3125 unsigned long rc;
3126
3127 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
3128 H_DISABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
3129 if (rc)
3130 dev_err(dev, "Couldn't disable scrq irq 0x%lx. rc=%ld\n",
3131 scrq->hw_irq, rc);
3132 return rc;
3133}
3134
3135static int enable_scrq_irq(struct ibmvnic_adapter *adapter,
3136 struct ibmvnic_sub_crq_queue *scrq)
3137{
3138 struct device *dev = &adapter->vdev->dev;
3139 unsigned long rc;
3140
3141 if (scrq->hw_irq > 0x100000000ULL) {
3142 dev_err(dev, "bad hw_irq = %lx\n", scrq->hw_irq);
3143 return 1;
3144 }
3145
Juliet Kim7ed5b312019-09-20 16:11:23 -04003146 if (test_bit(0, &adapter->resetting) &&
Nathan Fontenot73f9d362018-05-22 11:21:10 -05003147 adapter->reset_reason == VNIC_RESET_MOBILITY) {
Juliet Kim284f87d2019-11-20 10:50:03 -05003148 u64 val = (0xff000000) | scrq->hw_irq;
Nathan Fontenot73f9d362018-05-22 11:21:10 -05003149
Juliet Kim284f87d2019-11-20 10:50:03 -05003150 rc = plpar_hcall_norets(H_EOI, val);
Juliet Kim2df5c602019-11-20 10:50:04 -05003151 /* H_EOI would fail with rc = H_FUNCTION when running
3152 * in XIVE mode which is expected, but not an error.
3153 */
3154 if (rc && (rc != H_FUNCTION))
Juliet Kim284f87d2019-11-20 10:50:03 -05003155 dev_err(dev, "H_EOI FAILED irq 0x%llx. rc=%ld\n",
3156 val, rc);
Nathan Fontenot73f9d362018-05-22 11:21:10 -05003157 }
Thomas Falconf23e0642018-04-15 18:53:36 -05003158
Thomas Falcon032c5e82015-12-21 11:26:06 -06003159 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
3160 H_ENABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
3161 if (rc)
3162 dev_err(dev, "Couldn't enable scrq irq 0x%lx. rc=%ld\n",
3163 scrq->hw_irq, rc);
3164 return rc;
3165}
3166
3167static int ibmvnic_complete_tx(struct ibmvnic_adapter *adapter,
3168 struct ibmvnic_sub_crq_queue *scrq)
3169{
3170 struct device *dev = &adapter->vdev->dev;
Thomas Falcon06b3e352018-03-16 20:00:28 -05003171 struct ibmvnic_tx_pool *tx_pool;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003172 struct ibmvnic_tx_buff *txbuff;
Thomas Falcon0d973382020-11-18 19:12:19 -06003173 struct netdev_queue *txq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003174 union sub_crq *next;
3175 int index;
Thomas Falconc62aa372020-11-18 19:12:20 -06003176 int i;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003177
3178restart_loop:
3179 while (pending_scrq(adapter, scrq)) {
3180 unsigned int pool = scrq->pool_index;
Thomas Falconffc385b2018-02-18 10:08:41 -06003181 int num_entries = 0;
Thomas Falcon0d973382020-11-18 19:12:19 -06003182 int total_bytes = 0;
3183 int num_packets = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003184
3185 next = ibmvnic_next_scrq(adapter, scrq);
3186 for (i = 0; i < next->tx_comp.num_comps; i++) {
Thomas Falconba246c12020-12-01 09:52:11 -06003187 if (next->tx_comp.rcs[i])
Thomas Falcon032c5e82015-12-21 11:26:06 -06003188 dev_err(dev, "tx error %x\n",
3189 next->tx_comp.rcs[i]);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003190 index = be32_to_cpu(next->tx_comp.correlators[i]);
Thomas Falcon06b3e352018-03-16 20:00:28 -05003191 if (index & IBMVNIC_TSO_POOL_MASK) {
3192 tx_pool = &adapter->tso_pool[pool];
3193 index &= ~IBMVNIC_TSO_POOL_MASK;
3194 } else {
3195 tx_pool = &adapter->tx_pool[pool];
3196 }
3197
3198 txbuff = &tx_pool->tx_buff[index];
Thomas Falcon0d973382020-11-18 19:12:19 -06003199 num_packets++;
Thomas Falconffc385b2018-02-18 10:08:41 -06003200 num_entries += txbuff->num_entries;
Thomas Falcon0d973382020-11-18 19:12:19 -06003201 if (txbuff->skb) {
3202 total_bytes += txbuff->skb->len;
3203 dev_consume_skb_irq(txbuff->skb);
3204 txbuff->skb = NULL;
3205 } else {
3206 netdev_warn(adapter->netdev,
3207 "TX completion received with NULL socket buffer\n");
3208 }
Thomas Falcon06b3e352018-03-16 20:00:28 -05003209 tx_pool->free_map[tx_pool->producer_index] = index;
3210 tx_pool->producer_index =
3211 (tx_pool->producer_index + 1) %
3212 tx_pool->num_buffers;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003213 }
3214 /* remove tx_comp scrq*/
3215 next->tx_comp.first = 0;
Nathan Fontenot7c3e7de2017-05-03 14:05:25 -04003216
Thomas Falcon0d973382020-11-18 19:12:19 -06003217 txq = netdev_get_tx_queue(adapter->netdev, scrq->pool_index);
3218 netdev_tx_completed_queue(txq, num_packets, total_bytes);
3219
Thomas Falconffc385b2018-02-18 10:08:41 -06003220 if (atomic_sub_return(num_entries, &scrq->used) <=
Nathan Fontenot7c3e7de2017-05-03 14:05:25 -04003221 (adapter->req_tx_entries_per_subcrq / 2) &&
3222 __netif_subqueue_stopped(adapter->netdev,
3223 scrq->pool_index)) {
3224 netif_wake_subqueue(adapter->netdev, scrq->pool_index);
Thomas Falcon0aecb132018-02-26 18:10:58 -06003225 netdev_dbg(adapter->netdev, "Started queue %d\n",
3226 scrq->pool_index);
Nathan Fontenot7c3e7de2017-05-03 14:05:25 -04003227 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06003228 }
3229
3230 enable_scrq_irq(adapter, scrq);
3231
3232 if (pending_scrq(adapter, scrq)) {
3233 disable_scrq_irq(adapter, scrq);
3234 goto restart_loop;
3235 }
3236
3237 return 0;
3238}
3239
3240static irqreturn_t ibmvnic_interrupt_tx(int irq, void *instance)
3241{
3242 struct ibmvnic_sub_crq_queue *scrq = instance;
3243 struct ibmvnic_adapter *adapter = scrq->adapter;
3244
3245 disable_scrq_irq(adapter, scrq);
3246 ibmvnic_complete_tx(adapter, scrq);
3247
3248 return IRQ_HANDLED;
3249}
3250
3251static irqreturn_t ibmvnic_interrupt_rx(int irq, void *instance)
3252{
3253 struct ibmvnic_sub_crq_queue *scrq = instance;
3254 struct ibmvnic_adapter *adapter = scrq->adapter;
3255
Nathan Fontenot09fb35e2018-01-10 10:40:09 -06003256 /* When booting a kdump kernel we can hit pending interrupts
3257 * prior to completing driver initialization.
3258 */
3259 if (unlikely(adapter->state != VNIC_OPEN))
3260 return IRQ_NONE;
3261
John Allen3d52b592017-08-02 16:44:14 -05003262 adapter->rx_stats_buffers[scrq->scrq_num].interrupts++;
3263
Thomas Falcon032c5e82015-12-21 11:26:06 -06003264 if (napi_schedule_prep(&adapter->napi[scrq->scrq_num])) {
3265 disable_scrq_irq(adapter, scrq);
3266 __napi_schedule(&adapter->napi[scrq->scrq_num]);
3267 }
3268
3269 return IRQ_HANDLED;
3270}
3271
Thomas Falconea22d512016-07-06 15:35:17 -05003272static int init_sub_crq_irqs(struct ibmvnic_adapter *adapter)
3273{
3274 struct device *dev = &adapter->vdev->dev;
3275 struct ibmvnic_sub_crq_queue *scrq;
3276 int i = 0, j = 0;
3277 int rc = 0;
3278
3279 for (i = 0; i < adapter->req_tx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05003280 netdev_dbg(adapter->netdev, "Initializing tx_scrq[%d] irq\n",
3281 i);
Thomas Falconea22d512016-07-06 15:35:17 -05003282 scrq = adapter->tx_scrq[i];
3283 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
3284
Michael Ellerman99c17902016-09-10 19:59:05 +10003285 if (!scrq->irq) {
Thomas Falconea22d512016-07-06 15:35:17 -05003286 rc = -EINVAL;
3287 dev_err(dev, "Error mapping irq\n");
3288 goto req_tx_irq_failed;
3289 }
3290
Murilo Fossa Vicentinie56e2512019-04-25 11:02:33 -03003291 snprintf(scrq->name, sizeof(scrq->name), "ibmvnic-%x-tx%d",
3292 adapter->vdev->unit_address, i);
Thomas Falconea22d512016-07-06 15:35:17 -05003293 rc = request_irq(scrq->irq, ibmvnic_interrupt_tx,
Murilo Fossa Vicentinie56e2512019-04-25 11:02:33 -03003294 0, scrq->name, scrq);
Thomas Falconea22d512016-07-06 15:35:17 -05003295
3296 if (rc) {
3297 dev_err(dev, "Couldn't register tx irq 0x%x. rc=%d\n",
3298 scrq->irq, rc);
3299 irq_dispose_mapping(scrq->irq);
Nathan Fontenotaf9090c2018-02-20 11:04:18 -06003300 goto req_tx_irq_failed;
Thomas Falconea22d512016-07-06 15:35:17 -05003301 }
3302 }
3303
3304 for (i = 0; i < adapter->req_rx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05003305 netdev_dbg(adapter->netdev, "Initializing rx_scrq[%d] irq\n",
3306 i);
Thomas Falconea22d512016-07-06 15:35:17 -05003307 scrq = adapter->rx_scrq[i];
3308 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
Michael Ellerman99c17902016-09-10 19:59:05 +10003309 if (!scrq->irq) {
Thomas Falconea22d512016-07-06 15:35:17 -05003310 rc = -EINVAL;
3311 dev_err(dev, "Error mapping irq\n");
3312 goto req_rx_irq_failed;
3313 }
Murilo Fossa Vicentinie56e2512019-04-25 11:02:33 -03003314 snprintf(scrq->name, sizeof(scrq->name), "ibmvnic-%x-rx%d",
3315 adapter->vdev->unit_address, i);
Thomas Falconea22d512016-07-06 15:35:17 -05003316 rc = request_irq(scrq->irq, ibmvnic_interrupt_rx,
Murilo Fossa Vicentinie56e2512019-04-25 11:02:33 -03003317 0, scrq->name, scrq);
Thomas Falconea22d512016-07-06 15:35:17 -05003318 if (rc) {
3319 dev_err(dev, "Couldn't register rx irq 0x%x. rc=%d\n",
3320 scrq->irq, rc);
3321 irq_dispose_mapping(scrq->irq);
3322 goto req_rx_irq_failed;
3323 }
3324 }
3325 return rc;
3326
3327req_rx_irq_failed:
Thomas Falcon8bf371e2016-10-27 12:28:52 -05003328 for (j = 0; j < i; j++) {
Thomas Falconea22d512016-07-06 15:35:17 -05003329 free_irq(adapter->rx_scrq[j]->irq, adapter->rx_scrq[j]);
3330 irq_dispose_mapping(adapter->rx_scrq[j]->irq);
Thomas Falcon8bf371e2016-10-27 12:28:52 -05003331 }
Thomas Falconea22d512016-07-06 15:35:17 -05003332 i = adapter->req_tx_queues;
3333req_tx_irq_failed:
Thomas Falcon8bf371e2016-10-27 12:28:52 -05003334 for (j = 0; j < i; j++) {
Thomas Falconea22d512016-07-06 15:35:17 -05003335 free_irq(adapter->tx_scrq[j]->irq, adapter->tx_scrq[j]);
Thomas Falcon27a21452020-07-29 16:36:32 -05003336 irq_dispose_mapping(adapter->tx_scrq[j]->irq);
Thomas Falcon8bf371e2016-10-27 12:28:52 -05003337 }
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06003338 release_sub_crqs(adapter, 1);
Thomas Falconea22d512016-07-06 15:35:17 -05003339 return rc;
3340}
3341
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003342static int init_sub_crqs(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003343{
3344 struct device *dev = &adapter->vdev->dev;
3345 struct ibmvnic_sub_crq_queue **allqueues;
3346 int registered_queues = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003347 int total_queues;
3348 int more = 0;
Thomas Falconea22d512016-07-06 15:35:17 -05003349 int i;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003350
Thomas Falcon032c5e82015-12-21 11:26:06 -06003351 total_queues = adapter->req_tx_queues + adapter->req_rx_queues;
3352
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04003353 allqueues = kcalloc(total_queues, sizeof(*allqueues), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003354 if (!allqueues)
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003355 return -1;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003356
3357 for (i = 0; i < total_queues; i++) {
3358 allqueues[i] = init_sub_crq_queue(adapter);
3359 if (!allqueues[i]) {
3360 dev_warn(dev, "Couldn't allocate all sub-crqs\n");
3361 break;
3362 }
3363 registered_queues++;
3364 }
3365
3366 /* Make sure we were able to register the minimum number of queues */
3367 if (registered_queues <
3368 adapter->min_tx_queues + adapter->min_rx_queues) {
3369 dev_err(dev, "Fatal: Couldn't init min number of sub-crqs\n");
3370 goto tx_failed;
3371 }
3372
3373 /* Distribute the failed allocated queues*/
3374 for (i = 0; i < total_queues - registered_queues + more ; i++) {
3375 netdev_dbg(adapter->netdev, "Reducing number of queues\n");
3376 switch (i % 3) {
3377 case 0:
3378 if (adapter->req_rx_queues > adapter->min_rx_queues)
3379 adapter->req_rx_queues--;
3380 else
3381 more++;
3382 break;
3383 case 1:
3384 if (adapter->req_tx_queues > adapter->min_tx_queues)
3385 adapter->req_tx_queues--;
3386 else
3387 more++;
3388 break;
3389 }
3390 }
3391
3392 adapter->tx_scrq = kcalloc(adapter->req_tx_queues,
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04003393 sizeof(*adapter->tx_scrq), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003394 if (!adapter->tx_scrq)
3395 goto tx_failed;
3396
3397 for (i = 0; i < adapter->req_tx_queues; i++) {
3398 adapter->tx_scrq[i] = allqueues[i];
3399 adapter->tx_scrq[i]->pool_index = i;
Nathan Fontenot82e3be32018-02-21 21:33:56 -06003400 adapter->num_active_tx_scrqs++;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003401 }
3402
3403 adapter->rx_scrq = kcalloc(adapter->req_rx_queues,
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04003404 sizeof(*adapter->rx_scrq), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003405 if (!adapter->rx_scrq)
3406 goto rx_failed;
3407
3408 for (i = 0; i < adapter->req_rx_queues; i++) {
3409 adapter->rx_scrq[i] = allqueues[i + adapter->req_tx_queues];
3410 adapter->rx_scrq[i]->scrq_num = i;
Nathan Fontenot82e3be32018-02-21 21:33:56 -06003411 adapter->num_active_rx_scrqs++;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003412 }
3413
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003414 kfree(allqueues);
3415 return 0;
3416
3417rx_failed:
3418 kfree(adapter->tx_scrq);
3419 adapter->tx_scrq = NULL;
3420tx_failed:
3421 for (i = 0; i < registered_queues; i++)
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06003422 release_sub_crq_queue(adapter, allqueues[i], 1);
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003423 kfree(allqueues);
3424 return -1;
3425}
3426
Lijun Pan09081b92020-09-27 20:13:27 -05003427static void send_request_cap(struct ibmvnic_adapter *adapter, int retry)
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003428{
3429 struct device *dev = &adapter->vdev->dev;
3430 union ibmvnic_crq crq;
John Allenc26eba02017-10-26 16:23:25 -05003431 int max_entries;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003432
3433 if (!retry) {
3434 /* Sub-CRQ entries are 32 byte long */
3435 int entries_page = 4 * PAGE_SIZE / (sizeof(u64) * 4);
3436
3437 if (adapter->min_tx_entries_per_subcrq > entries_page ||
3438 adapter->min_rx_add_entries_per_subcrq > entries_page) {
3439 dev_err(dev, "Fatal, invalid entries per sub-crq\n");
3440 return;
3441 }
3442
John Allenc26eba02017-10-26 16:23:25 -05003443 if (adapter->desired.mtu)
3444 adapter->req_mtu = adapter->desired.mtu;
3445 else
3446 adapter->req_mtu = adapter->netdev->mtu + ETH_HLEN;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003447
John Allenc26eba02017-10-26 16:23:25 -05003448 if (!adapter->desired.tx_entries)
3449 adapter->desired.tx_entries =
3450 adapter->max_tx_entries_per_subcrq;
3451 if (!adapter->desired.rx_entries)
3452 adapter->desired.rx_entries =
3453 adapter->max_rx_add_entries_per_subcrq;
3454
3455 max_entries = IBMVNIC_MAX_LTB_SIZE /
3456 (adapter->req_mtu + IBMVNIC_BUFFER_HLEN);
3457
3458 if ((adapter->req_mtu + IBMVNIC_BUFFER_HLEN) *
3459 adapter->desired.tx_entries > IBMVNIC_MAX_LTB_SIZE) {
3460 adapter->desired.tx_entries = max_entries;
3461 }
3462
3463 if ((adapter->req_mtu + IBMVNIC_BUFFER_HLEN) *
3464 adapter->desired.rx_entries > IBMVNIC_MAX_LTB_SIZE) {
3465 adapter->desired.rx_entries = max_entries;
3466 }
3467
3468 if (adapter->desired.tx_entries)
3469 adapter->req_tx_entries_per_subcrq =
3470 adapter->desired.tx_entries;
3471 else
3472 adapter->req_tx_entries_per_subcrq =
3473 adapter->max_tx_entries_per_subcrq;
3474
3475 if (adapter->desired.rx_entries)
3476 adapter->req_rx_add_entries_per_subcrq =
3477 adapter->desired.rx_entries;
3478 else
3479 adapter->req_rx_add_entries_per_subcrq =
3480 adapter->max_rx_add_entries_per_subcrq;
3481
3482 if (adapter->desired.tx_queues)
3483 adapter->req_tx_queues =
3484 adapter->desired.tx_queues;
3485 else
3486 adapter->req_tx_queues =
3487 adapter->opt_tx_comp_sub_queues;
3488
3489 if (adapter->desired.rx_queues)
3490 adapter->req_rx_queues =
3491 adapter->desired.rx_queues;
3492 else
3493 adapter->req_rx_queues =
3494 adapter->opt_rx_comp_queues;
3495
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003496 adapter->req_rx_add_queues = adapter->max_rx_add_queues;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003497 }
3498
Thomas Falcon032c5e82015-12-21 11:26:06 -06003499 memset(&crq, 0, sizeof(crq));
3500 crq.request_capability.first = IBMVNIC_CRQ_CMD;
3501 crq.request_capability.cmd = REQUEST_CAPABILITY;
3502
3503 crq.request_capability.capability = cpu_to_be16(REQ_TX_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06003504 crq.request_capability.number = cpu_to_be64(adapter->req_tx_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06003505 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003506 ibmvnic_send_crq(adapter, &crq);
3507
3508 crq.request_capability.capability = cpu_to_be16(REQ_RX_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06003509 crq.request_capability.number = cpu_to_be64(adapter->req_rx_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06003510 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003511 ibmvnic_send_crq(adapter, &crq);
3512
3513 crq.request_capability.capability = cpu_to_be16(REQ_RX_ADD_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06003514 crq.request_capability.number = cpu_to_be64(adapter->req_rx_add_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06003515 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003516 ibmvnic_send_crq(adapter, &crq);
3517
3518 crq.request_capability.capability =
3519 cpu_to_be16(REQ_TX_ENTRIES_PER_SUBCRQ);
3520 crq.request_capability.number =
Thomas Falconde89e852016-03-01 10:20:09 -06003521 cpu_to_be64(adapter->req_tx_entries_per_subcrq);
Thomas Falcon901e0402017-02-15 12:17:59 -06003522 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003523 ibmvnic_send_crq(adapter, &crq);
3524
3525 crq.request_capability.capability =
3526 cpu_to_be16(REQ_RX_ADD_ENTRIES_PER_SUBCRQ);
3527 crq.request_capability.number =
Thomas Falconde89e852016-03-01 10:20:09 -06003528 cpu_to_be64(adapter->req_rx_add_entries_per_subcrq);
Thomas Falcon901e0402017-02-15 12:17:59 -06003529 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003530 ibmvnic_send_crq(adapter, &crq);
3531
3532 crq.request_capability.capability = cpu_to_be16(REQ_MTU);
Thomas Falconde89e852016-03-01 10:20:09 -06003533 crq.request_capability.number = cpu_to_be64(adapter->req_mtu);
Thomas Falcon901e0402017-02-15 12:17:59 -06003534 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003535 ibmvnic_send_crq(adapter, &crq);
3536
3537 if (adapter->netdev->flags & IFF_PROMISC) {
3538 if (adapter->promisc_supported) {
3539 crq.request_capability.capability =
3540 cpu_to_be16(PROMISC_REQUESTED);
Thomas Falconde89e852016-03-01 10:20:09 -06003541 crq.request_capability.number = cpu_to_be64(1);
Thomas Falcon901e0402017-02-15 12:17:59 -06003542 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003543 ibmvnic_send_crq(adapter, &crq);
3544 }
3545 } else {
3546 crq.request_capability.capability =
3547 cpu_to_be16(PROMISC_REQUESTED);
Thomas Falconde89e852016-03-01 10:20:09 -06003548 crq.request_capability.number = cpu_to_be64(0);
Thomas Falcon901e0402017-02-15 12:17:59 -06003549 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003550 ibmvnic_send_crq(adapter, &crq);
3551 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06003552}
3553
3554static int pending_scrq(struct ibmvnic_adapter *adapter,
3555 struct ibmvnic_sub_crq_queue *scrq)
3556{
3557 union sub_crq *entry = &scrq->msgs[scrq->cur];
Lijun Pan665ab1e2021-01-29 19:19:04 -06003558 int rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003559
Lijun Pan665ab1e2021-01-29 19:19:04 -06003560 rc = !!(entry->generic.first & IBMVNIC_CRQ_CMD_RSP);
3561
3562 /* Ensure that the SCRQ valid flag is loaded prior to loading the
3563 * contents of the SCRQ descriptor
3564 */
3565 dma_rmb();
3566
3567 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003568}
3569
3570static union sub_crq *ibmvnic_next_scrq(struct ibmvnic_adapter *adapter,
3571 struct ibmvnic_sub_crq_queue *scrq)
3572{
3573 union sub_crq *entry;
3574 unsigned long flags;
3575
3576 spin_lock_irqsave(&scrq->lock, flags);
3577 entry = &scrq->msgs[scrq->cur];
3578 if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP) {
3579 if (++scrq->cur == scrq->size)
3580 scrq->cur = 0;
3581 } else {
3582 entry = NULL;
3583 }
3584 spin_unlock_irqrestore(&scrq->lock, flags);
3585
Lijun Pan665ab1e2021-01-29 19:19:04 -06003586 /* Ensure that the SCRQ valid flag is loaded prior to loading the
3587 * contents of the SCRQ descriptor
Thomas Falconb71ec952020-12-01 09:52:10 -06003588 */
3589 dma_rmb();
3590
Thomas Falcon032c5e82015-12-21 11:26:06 -06003591 return entry;
3592}
3593
3594static union ibmvnic_crq *ibmvnic_next_crq(struct ibmvnic_adapter *adapter)
3595{
3596 struct ibmvnic_crq_queue *queue = &adapter->crq;
3597 union ibmvnic_crq *crq;
3598
3599 crq = &queue->msgs[queue->cur];
3600 if (crq->generic.first & IBMVNIC_CRQ_CMD_RSP) {
3601 if (++queue->cur == queue->size)
3602 queue->cur = 0;
3603 } else {
3604 crq = NULL;
3605 }
3606
3607 return crq;
3608}
3609
Thomas Falcon2d14d372018-07-13 12:03:32 -05003610static void print_subcrq_error(struct device *dev, int rc, const char *func)
3611{
3612 switch (rc) {
3613 case H_PARAMETER:
3614 dev_warn_ratelimited(dev,
3615 "%s failed: Send request is malformed or adapter failover pending. (rc=%d)\n",
3616 func, rc);
3617 break;
3618 case H_CLOSED:
3619 dev_warn_ratelimited(dev,
3620 "%s failed: Backing queue closed. Adapter is down or failover pending. (rc=%d)\n",
3621 func, rc);
3622 break;
3623 default:
3624 dev_err_ratelimited(dev, "%s failed: (rc=%d)\n", func, rc);
3625 break;
3626 }
3627}
3628
Thomas Falconad7775d2016-04-01 17:20:34 -05003629static int send_subcrq_indirect(struct ibmvnic_adapter *adapter,
3630 u64 remote_handle, u64 ioba, u64 num_entries)
3631{
3632 unsigned int ua = adapter->vdev->unit_address;
3633 struct device *dev = &adapter->vdev->dev;
3634 int rc;
3635
3636 /* Make sure the hypervisor sees the complete request */
3637 mb();
3638 rc = plpar_hcall_norets(H_SEND_SUB_CRQ_INDIRECT, ua,
3639 cpu_to_be64(remote_handle),
3640 ioba, num_entries);
3641
Thomas Falcon2d14d372018-07-13 12:03:32 -05003642 if (rc)
3643 print_subcrq_error(dev, rc, __func__);
Thomas Falconad7775d2016-04-01 17:20:34 -05003644
3645 return rc;
3646}
3647
Thomas Falcon032c5e82015-12-21 11:26:06 -06003648static int ibmvnic_send_crq(struct ibmvnic_adapter *adapter,
3649 union ibmvnic_crq *crq)
3650{
3651 unsigned int ua = adapter->vdev->unit_address;
3652 struct device *dev = &adapter->vdev->dev;
3653 u64 *u64_crq = (u64 *)crq;
3654 int rc;
3655
3656 netdev_dbg(adapter->netdev, "Sending CRQ: %016lx %016lx\n",
Lijun Pan429aa362021-02-11 00:43:18 -06003657 (unsigned long)cpu_to_be64(u64_crq[0]),
3658 (unsigned long)cpu_to_be64(u64_crq[1]));
Thomas Falcon032c5e82015-12-21 11:26:06 -06003659
Thomas Falcon51536982018-05-23 13:37:56 -05003660 if (!adapter->crq.active &&
3661 crq->generic.first != IBMVNIC_CRQ_INIT_CMD) {
3662 dev_warn(dev, "Invalid request detected while CRQ is inactive, possible device state change during reset\n");
3663 return -EINVAL;
3664 }
3665
Thomas Falcon032c5e82015-12-21 11:26:06 -06003666 /* Make sure the hypervisor sees the complete request */
3667 mb();
3668
3669 rc = plpar_hcall_norets(H_SEND_CRQ, ua,
3670 cpu_to_be64(u64_crq[0]),
3671 cpu_to_be64(u64_crq[1]));
3672
3673 if (rc) {
Nathan Fontenotec95dff2018-02-07 13:00:24 -06003674 if (rc == H_CLOSED) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06003675 dev_warn(dev, "CRQ Queue closed\n");
Lijun Panfa68bfa2020-08-19 17:52:24 -05003676 /* do not reset, report the fail, wait for passive init from server */
Nathan Fontenotec95dff2018-02-07 13:00:24 -06003677 }
3678
Thomas Falcon032c5e82015-12-21 11:26:06 -06003679 dev_warn(dev, "Send error (rc=%d)\n", rc);
3680 }
3681
3682 return rc;
3683}
3684
3685static int ibmvnic_send_crq_init(struct ibmvnic_adapter *adapter)
3686{
Thomas Falcon36a782f2020-08-31 11:59:57 -05003687 struct device *dev = &adapter->vdev->dev;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003688 union ibmvnic_crq crq;
Thomas Falcon36a782f2020-08-31 11:59:57 -05003689 int retries = 100;
3690 int rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003691
3692 memset(&crq, 0, sizeof(crq));
3693 crq.generic.first = IBMVNIC_CRQ_INIT_CMD;
3694 crq.generic.cmd = IBMVNIC_CRQ_INIT;
3695 netdev_dbg(adapter->netdev, "Sending CRQ init\n");
3696
Thomas Falcon36a782f2020-08-31 11:59:57 -05003697 do {
3698 rc = ibmvnic_send_crq(adapter, &crq);
3699 if (rc != H_CLOSED)
3700 break;
3701 retries--;
3702 msleep(50);
3703
3704 } while (retries > 0);
3705
3706 if (rc) {
3707 dev_err(dev, "Failed to send init request, rc = %d\n", rc);
3708 return rc;
3709 }
3710
3711 return 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003712}
3713
Thomas Falcon032c5e82015-12-21 11:26:06 -06003714static int send_version_xchg(struct ibmvnic_adapter *adapter)
3715{
3716 union ibmvnic_crq crq;
3717
3718 memset(&crq, 0, sizeof(crq));
3719 crq.version_exchange.first = IBMVNIC_CRQ_CMD;
3720 crq.version_exchange.cmd = VERSION_EXCHANGE;
3721 crq.version_exchange.version = cpu_to_be16(ibmvnic_version);
3722
3723 return ibmvnic_send_crq(adapter, &crq);
3724}
3725
Nathan Fontenot37798d02017-11-08 11:23:56 -06003726struct vnic_login_client_data {
3727 u8 type;
3728 __be16 len;
Kees Cook08ea5562018-04-10 15:26:43 -07003729 char name[];
Nathan Fontenot37798d02017-11-08 11:23:56 -06003730} __packed;
3731
3732static int vnic_client_data_len(struct ibmvnic_adapter *adapter)
3733{
3734 int len;
3735
3736 /* Calculate the amount of buffer space needed for the
3737 * vnic client data in the login buffer. There are four entries,
3738 * OS name, LPAR name, device name, and a null last entry.
3739 */
3740 len = 4 * sizeof(struct vnic_login_client_data);
3741 len += 6; /* "Linux" plus NULL */
3742 len += strlen(utsname()->nodename) + 1;
3743 len += strlen(adapter->netdev->name) + 1;
3744
3745 return len;
3746}
3747
3748static void vnic_add_client_data(struct ibmvnic_adapter *adapter,
3749 struct vnic_login_client_data *vlcd)
3750{
3751 const char *os_name = "Linux";
3752 int len;
3753
3754 /* Type 1 - LPAR OS */
3755 vlcd->type = 1;
3756 len = strlen(os_name) + 1;
3757 vlcd->len = cpu_to_be16(len);
Kees Cook08ea5562018-04-10 15:26:43 -07003758 strncpy(vlcd->name, os_name, len);
3759 vlcd = (struct vnic_login_client_data *)(vlcd->name + len);
Nathan Fontenot37798d02017-11-08 11:23:56 -06003760
3761 /* Type 2 - LPAR name */
3762 vlcd->type = 2;
3763 len = strlen(utsname()->nodename) + 1;
3764 vlcd->len = cpu_to_be16(len);
Kees Cook08ea5562018-04-10 15:26:43 -07003765 strncpy(vlcd->name, utsname()->nodename, len);
3766 vlcd = (struct vnic_login_client_data *)(vlcd->name + len);
Nathan Fontenot37798d02017-11-08 11:23:56 -06003767
3768 /* Type 3 - device name */
3769 vlcd->type = 3;
3770 len = strlen(adapter->netdev->name) + 1;
3771 vlcd->len = cpu_to_be16(len);
Kees Cook08ea5562018-04-10 15:26:43 -07003772 strncpy(vlcd->name, adapter->netdev->name, len);
Nathan Fontenot37798d02017-11-08 11:23:56 -06003773}
3774
Thomas Falcon20a8ab72018-02-26 18:10:59 -06003775static int send_login(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003776{
3777 struct ibmvnic_login_rsp_buffer *login_rsp_buffer;
3778 struct ibmvnic_login_buffer *login_buffer;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003779 struct device *dev = &adapter->vdev->dev;
Dany Maddenc98d9cc2020-11-25 18:04:30 -06003780 struct vnic_login_client_data *vlcd;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003781 dma_addr_t rsp_buffer_token;
3782 dma_addr_t buffer_token;
3783 size_t rsp_buffer_size;
3784 union ibmvnic_crq crq;
Dany Maddenc98d9cc2020-11-25 18:04:30 -06003785 int client_data_len;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003786 size_t buffer_size;
3787 __be64 *tx_list_p;
3788 __be64 *rx_list_p;
Dany Maddenc98d9cc2020-11-25 18:04:30 -06003789 int rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003790 int i;
3791
Thomas Falcon20a8ab72018-02-26 18:10:59 -06003792 if (!adapter->tx_scrq || !adapter->rx_scrq) {
3793 netdev_err(adapter->netdev,
3794 "RX or TX queues are not allocated, device login failed\n");
3795 return -1;
3796 }
3797
Lijun Pana0c8be52020-12-19 15:39:19 -06003798 release_login_buffer(adapter);
Thomas Falcon34f0f4e2018-02-13 18:23:40 -06003799 release_login_rsp_buffer(adapter);
Lijun Pana0c8be52020-12-19 15:39:19 -06003800
Nathan Fontenot37798d02017-11-08 11:23:56 -06003801 client_data_len = vnic_client_data_len(adapter);
3802
Thomas Falcon032c5e82015-12-21 11:26:06 -06003803 buffer_size =
3804 sizeof(struct ibmvnic_login_buffer) +
Nathan Fontenot37798d02017-11-08 11:23:56 -06003805 sizeof(u64) * (adapter->req_tx_queues + adapter->req_rx_queues) +
3806 client_data_len;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003807
Nathan Fontenot37798d02017-11-08 11:23:56 -06003808 login_buffer = kzalloc(buffer_size, GFP_ATOMIC);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003809 if (!login_buffer)
3810 goto buf_alloc_failed;
3811
3812 buffer_token = dma_map_single(dev, login_buffer, buffer_size,
3813 DMA_TO_DEVICE);
3814 if (dma_mapping_error(dev, buffer_token)) {
3815 dev_err(dev, "Couldn't map login buffer\n");
3816 goto buf_map_failed;
3817 }
3818
John Allen498cd8e2016-04-06 11:49:55 -05003819 rsp_buffer_size = sizeof(struct ibmvnic_login_rsp_buffer) +
3820 sizeof(u64) * adapter->req_tx_queues +
3821 sizeof(u64) * adapter->req_rx_queues +
3822 sizeof(u64) * adapter->req_rx_queues +
3823 sizeof(u8) * IBMVNIC_TX_DESC_VERSIONS;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003824
3825 login_rsp_buffer = kmalloc(rsp_buffer_size, GFP_ATOMIC);
3826 if (!login_rsp_buffer)
3827 goto buf_rsp_alloc_failed;
3828
3829 rsp_buffer_token = dma_map_single(dev, login_rsp_buffer,
3830 rsp_buffer_size, DMA_FROM_DEVICE);
3831 if (dma_mapping_error(dev, rsp_buffer_token)) {
3832 dev_err(dev, "Couldn't map login rsp buffer\n");
3833 goto buf_rsp_map_failed;
3834 }
Nathan Fontenot661a2622017-04-19 13:44:58 -04003835
Thomas Falcon032c5e82015-12-21 11:26:06 -06003836 adapter->login_buf = login_buffer;
3837 adapter->login_buf_token = buffer_token;
3838 adapter->login_buf_sz = buffer_size;
3839 adapter->login_rsp_buf = login_rsp_buffer;
3840 adapter->login_rsp_buf_token = rsp_buffer_token;
3841 adapter->login_rsp_buf_sz = rsp_buffer_size;
3842
3843 login_buffer->len = cpu_to_be32(buffer_size);
3844 login_buffer->version = cpu_to_be32(INITIAL_VERSION_LB);
3845 login_buffer->num_txcomp_subcrqs = cpu_to_be32(adapter->req_tx_queues);
3846 login_buffer->off_txcomp_subcrqs =
3847 cpu_to_be32(sizeof(struct ibmvnic_login_buffer));
3848 login_buffer->num_rxcomp_subcrqs = cpu_to_be32(adapter->req_rx_queues);
3849 login_buffer->off_rxcomp_subcrqs =
3850 cpu_to_be32(sizeof(struct ibmvnic_login_buffer) +
3851 sizeof(u64) * adapter->req_tx_queues);
3852 login_buffer->login_rsp_ioba = cpu_to_be32(rsp_buffer_token);
3853 login_buffer->login_rsp_len = cpu_to_be32(rsp_buffer_size);
3854
3855 tx_list_p = (__be64 *)((char *)login_buffer +
3856 sizeof(struct ibmvnic_login_buffer));
3857 rx_list_p = (__be64 *)((char *)login_buffer +
3858 sizeof(struct ibmvnic_login_buffer) +
3859 sizeof(u64) * adapter->req_tx_queues);
3860
3861 for (i = 0; i < adapter->req_tx_queues; i++) {
3862 if (adapter->tx_scrq[i]) {
3863 tx_list_p[i] = cpu_to_be64(adapter->tx_scrq[i]->
3864 crq_num);
3865 }
3866 }
3867
3868 for (i = 0; i < adapter->req_rx_queues; i++) {
3869 if (adapter->rx_scrq[i]) {
3870 rx_list_p[i] = cpu_to_be64(adapter->rx_scrq[i]->
3871 crq_num);
3872 }
3873 }
3874
Nathan Fontenot37798d02017-11-08 11:23:56 -06003875 /* Insert vNIC login client data */
3876 vlcd = (struct vnic_login_client_data *)
3877 ((char *)rx_list_p + (sizeof(u64) * adapter->req_rx_queues));
3878 login_buffer->client_data_offset =
3879 cpu_to_be32((char *)vlcd - (char *)login_buffer);
3880 login_buffer->client_data_len = cpu_to_be32(client_data_len);
3881
3882 vnic_add_client_data(adapter, vlcd);
3883
Thomas Falcon032c5e82015-12-21 11:26:06 -06003884 netdev_dbg(adapter->netdev, "Login Buffer:\n");
3885 for (i = 0; i < (adapter->login_buf_sz - 1) / 8 + 1; i++) {
3886 netdev_dbg(adapter->netdev, "%016lx\n",
Lijun Pan429aa362021-02-11 00:43:18 -06003887 ((unsigned long *)(adapter->login_buf))[i]);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003888 }
3889
3890 memset(&crq, 0, sizeof(crq));
3891 crq.login.first = IBMVNIC_CRQ_CMD;
3892 crq.login.cmd = LOGIN;
3893 crq.login.ioba = cpu_to_be32(buffer_token);
3894 crq.login.len = cpu_to_be32(buffer_size);
Sukadev Bhattiprolu76cdc5c2020-11-25 18:04:29 -06003895
3896 adapter->login_pending = true;
Dany Maddenc98d9cc2020-11-25 18:04:30 -06003897 rc = ibmvnic_send_crq(adapter, &crq);
3898 if (rc) {
3899 adapter->login_pending = false;
3900 netdev_err(adapter->netdev, "Failed to send login, rc=%d\n", rc);
3901 goto buf_rsp_map_failed;
3902 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06003903
Thomas Falcon20a8ab72018-02-26 18:10:59 -06003904 return 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003905
Thomas Falcon032c5e82015-12-21 11:26:06 -06003906buf_rsp_map_failed:
3907 kfree(login_rsp_buffer);
Dany Maddenc98d9cc2020-11-25 18:04:30 -06003908 adapter->login_rsp_buf = NULL;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003909buf_rsp_alloc_failed:
3910 dma_unmap_single(dev, buffer_token, buffer_size, DMA_TO_DEVICE);
3911buf_map_failed:
3912 kfree(login_buffer);
Dany Maddenc98d9cc2020-11-25 18:04:30 -06003913 adapter->login_buf = NULL;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003914buf_alloc_failed:
Thomas Falcon20a8ab72018-02-26 18:10:59 -06003915 return -1;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003916}
3917
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05003918static int send_request_map(struct ibmvnic_adapter *adapter, dma_addr_t addr,
3919 u32 len, u8 map_id)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003920{
3921 union ibmvnic_crq crq;
3922
3923 memset(&crq, 0, sizeof(crq));
3924 crq.request_map.first = IBMVNIC_CRQ_CMD;
3925 crq.request_map.cmd = REQUEST_MAP;
3926 crq.request_map.map_id = map_id;
3927 crq.request_map.ioba = cpu_to_be32(addr);
3928 crq.request_map.len = cpu_to_be32(len);
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05003929 return ibmvnic_send_crq(adapter, &crq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003930}
3931
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05003932static int send_request_unmap(struct ibmvnic_adapter *adapter, u8 map_id)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003933{
3934 union ibmvnic_crq crq;
3935
3936 memset(&crq, 0, sizeof(crq));
3937 crq.request_unmap.first = IBMVNIC_CRQ_CMD;
3938 crq.request_unmap.cmd = REQUEST_UNMAP;
3939 crq.request_unmap.map_id = map_id;
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05003940 return ibmvnic_send_crq(adapter, &crq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003941}
3942
Lijun Pan69980d02020-09-27 20:13:28 -05003943static void send_query_map(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003944{
3945 union ibmvnic_crq crq;
3946
3947 memset(&crq, 0, sizeof(crq));
3948 crq.query_map.first = IBMVNIC_CRQ_CMD;
3949 crq.query_map.cmd = QUERY_MAP;
3950 ibmvnic_send_crq(adapter, &crq);
3951}
3952
3953/* Send a series of CRQs requesting various capabilities of the VNIC server */
Lijun Pan491099a2020-09-27 20:13:26 -05003954static void send_query_cap(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003955{
3956 union ibmvnic_crq crq;
3957
Thomas Falcon901e0402017-02-15 12:17:59 -06003958 atomic_set(&adapter->running_cap_crqs, 0);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003959 memset(&crq, 0, sizeof(crq));
3960 crq.query_capability.first = IBMVNIC_CRQ_CMD;
3961 crq.query_capability.cmd = QUERY_CAPABILITY;
3962
3963 crq.query_capability.capability = cpu_to_be16(MIN_TX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003964 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003965 ibmvnic_send_crq(adapter, &crq);
3966
3967 crq.query_capability.capability = cpu_to_be16(MIN_RX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003968 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003969 ibmvnic_send_crq(adapter, &crq);
3970
3971 crq.query_capability.capability = cpu_to_be16(MIN_RX_ADD_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003972 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003973 ibmvnic_send_crq(adapter, &crq);
3974
3975 crq.query_capability.capability = cpu_to_be16(MAX_TX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003976 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003977 ibmvnic_send_crq(adapter, &crq);
3978
3979 crq.query_capability.capability = cpu_to_be16(MAX_RX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003980 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003981 ibmvnic_send_crq(adapter, &crq);
3982
3983 crq.query_capability.capability = cpu_to_be16(MAX_RX_ADD_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003984 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003985 ibmvnic_send_crq(adapter, &crq);
3986
3987 crq.query_capability.capability =
3988 cpu_to_be16(MIN_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06003989 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003990 ibmvnic_send_crq(adapter, &crq);
3991
3992 crq.query_capability.capability =
3993 cpu_to_be16(MIN_RX_ADD_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06003994 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003995 ibmvnic_send_crq(adapter, &crq);
3996
3997 crq.query_capability.capability =
3998 cpu_to_be16(MAX_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06003999 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004000 ibmvnic_send_crq(adapter, &crq);
4001
4002 crq.query_capability.capability =
4003 cpu_to_be16(MAX_RX_ADD_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06004004 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004005 ibmvnic_send_crq(adapter, &crq);
4006
4007 crq.query_capability.capability = cpu_to_be16(TCP_IP_OFFLOAD);
Thomas Falcon901e0402017-02-15 12:17:59 -06004008 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004009 ibmvnic_send_crq(adapter, &crq);
4010
4011 crq.query_capability.capability = cpu_to_be16(PROMISC_SUPPORTED);
Thomas Falcon901e0402017-02-15 12:17:59 -06004012 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004013 ibmvnic_send_crq(adapter, &crq);
4014
4015 crq.query_capability.capability = cpu_to_be16(MIN_MTU);
Thomas Falcon901e0402017-02-15 12:17:59 -06004016 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004017 ibmvnic_send_crq(adapter, &crq);
4018
4019 crq.query_capability.capability = cpu_to_be16(MAX_MTU);
Thomas Falcon901e0402017-02-15 12:17:59 -06004020 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004021 ibmvnic_send_crq(adapter, &crq);
4022
4023 crq.query_capability.capability = cpu_to_be16(MAX_MULTICAST_FILTERS);
Thomas Falcon901e0402017-02-15 12:17:59 -06004024 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004025 ibmvnic_send_crq(adapter, &crq);
4026
4027 crq.query_capability.capability = cpu_to_be16(VLAN_HEADER_INSERTION);
Thomas Falcon901e0402017-02-15 12:17:59 -06004028 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004029 ibmvnic_send_crq(adapter, &crq);
4030
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04004031 crq.query_capability.capability = cpu_to_be16(RX_VLAN_HEADER_INSERTION);
4032 atomic_inc(&adapter->running_cap_crqs);
4033 ibmvnic_send_crq(adapter, &crq);
4034
Thomas Falcon032c5e82015-12-21 11:26:06 -06004035 crq.query_capability.capability = cpu_to_be16(MAX_TX_SG_ENTRIES);
Thomas Falcon901e0402017-02-15 12:17:59 -06004036 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004037 ibmvnic_send_crq(adapter, &crq);
4038
4039 crq.query_capability.capability = cpu_to_be16(RX_SG_SUPPORTED);
Thomas Falcon901e0402017-02-15 12:17:59 -06004040 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004041 ibmvnic_send_crq(adapter, &crq);
4042
4043 crq.query_capability.capability = cpu_to_be16(OPT_TX_COMP_SUB_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06004044 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004045 ibmvnic_send_crq(adapter, &crq);
4046
4047 crq.query_capability.capability = cpu_to_be16(OPT_RX_COMP_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06004048 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004049 ibmvnic_send_crq(adapter, &crq);
4050
4051 crq.query_capability.capability =
4052 cpu_to_be16(OPT_RX_BUFADD_Q_PER_RX_COMP_Q);
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 =
4057 cpu_to_be16(OPT_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06004058 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004059 ibmvnic_send_crq(adapter, &crq);
4060
4061 crq.query_capability.capability =
4062 cpu_to_be16(OPT_RXBA_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06004063 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004064 ibmvnic_send_crq(adapter, &crq);
4065
4066 crq.query_capability.capability = cpu_to_be16(TX_RX_DESC_REQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06004067 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004068 ibmvnic_send_crq(adapter, &crq);
4069}
4070
Lijun Pan16e811f2020-09-27 20:13:29 -05004071static void send_query_ip_offload(struct ibmvnic_adapter *adapter)
4072{
4073 int buf_sz = sizeof(struct ibmvnic_query_ip_offload_buffer);
4074 struct device *dev = &adapter->vdev->dev;
4075 union ibmvnic_crq crq;
4076
4077 adapter->ip_offload_tok =
4078 dma_map_single(dev,
4079 &adapter->ip_offload_buf,
4080 buf_sz,
4081 DMA_FROM_DEVICE);
4082
4083 if (dma_mapping_error(dev, adapter->ip_offload_tok)) {
4084 if (!firmware_has_feature(FW_FEATURE_CMO))
4085 dev_err(dev, "Couldn't map offload buffer\n");
4086 return;
4087 }
4088
4089 memset(&crq, 0, sizeof(crq));
4090 crq.query_ip_offload.first = IBMVNIC_CRQ_CMD;
4091 crq.query_ip_offload.cmd = QUERY_IP_OFFLOAD;
4092 crq.query_ip_offload.len = cpu_to_be32(buf_sz);
4093 crq.query_ip_offload.ioba =
4094 cpu_to_be32(adapter->ip_offload_tok);
4095
4096 ibmvnic_send_crq(adapter, &crq);
4097}
4098
Lijun Pan46899bd2020-09-27 20:13:30 -05004099static void send_control_ip_offload(struct ibmvnic_adapter *adapter)
4100{
4101 struct ibmvnic_control_ip_offload_buffer *ctrl_buf = &adapter->ip_offload_ctrl;
4102 struct ibmvnic_query_ip_offload_buffer *buf = &adapter->ip_offload_buf;
4103 struct device *dev = &adapter->vdev->dev;
4104 netdev_features_t old_hw_features = 0;
4105 union ibmvnic_crq crq;
4106
4107 adapter->ip_offload_ctrl_tok =
4108 dma_map_single(dev,
4109 ctrl_buf,
4110 sizeof(adapter->ip_offload_ctrl),
4111 DMA_TO_DEVICE);
4112
4113 if (dma_mapping_error(dev, adapter->ip_offload_ctrl_tok)) {
4114 dev_err(dev, "Couldn't map ip offload control buffer\n");
4115 return;
4116 }
4117
4118 ctrl_buf->len = cpu_to_be32(sizeof(adapter->ip_offload_ctrl));
4119 ctrl_buf->version = cpu_to_be32(INITIAL_VERSION_IOB);
4120 ctrl_buf->ipv4_chksum = buf->ipv4_chksum;
4121 ctrl_buf->ipv6_chksum = buf->ipv6_chksum;
4122 ctrl_buf->tcp_ipv4_chksum = buf->tcp_ipv4_chksum;
4123 ctrl_buf->udp_ipv4_chksum = buf->udp_ipv4_chksum;
4124 ctrl_buf->tcp_ipv6_chksum = buf->tcp_ipv6_chksum;
4125 ctrl_buf->udp_ipv6_chksum = buf->udp_ipv6_chksum;
4126 ctrl_buf->large_tx_ipv4 = buf->large_tx_ipv4;
4127 ctrl_buf->large_tx_ipv6 = buf->large_tx_ipv6;
4128
4129 /* large_rx disabled for now, additional features needed */
4130 ctrl_buf->large_rx_ipv4 = 0;
4131 ctrl_buf->large_rx_ipv6 = 0;
4132
4133 if (adapter->state != VNIC_PROBING) {
4134 old_hw_features = adapter->netdev->hw_features;
4135 adapter->netdev->hw_features = 0;
4136 }
4137
4138 adapter->netdev->hw_features = NETIF_F_SG | NETIF_F_GSO | NETIF_F_GRO;
4139
4140 if (buf->tcp_ipv4_chksum || buf->udp_ipv4_chksum)
4141 adapter->netdev->hw_features |= NETIF_F_IP_CSUM;
4142
4143 if (buf->tcp_ipv6_chksum || buf->udp_ipv6_chksum)
4144 adapter->netdev->hw_features |= NETIF_F_IPV6_CSUM;
4145
4146 if ((adapter->netdev->features &
4147 (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)))
4148 adapter->netdev->hw_features |= NETIF_F_RXCSUM;
4149
4150 if (buf->large_tx_ipv4)
4151 adapter->netdev->hw_features |= NETIF_F_TSO;
4152 if (buf->large_tx_ipv6)
4153 adapter->netdev->hw_features |= NETIF_F_TSO6;
4154
4155 if (adapter->state == VNIC_PROBING) {
4156 adapter->netdev->features |= adapter->netdev->hw_features;
4157 } else if (old_hw_features != adapter->netdev->hw_features) {
4158 netdev_features_t tmp = 0;
4159
4160 /* disable features no longer supported */
4161 adapter->netdev->features &= adapter->netdev->hw_features;
4162 /* turn on features now supported if previously enabled */
4163 tmp = (old_hw_features ^ adapter->netdev->hw_features) &
4164 adapter->netdev->hw_features;
4165 adapter->netdev->features |=
4166 tmp & adapter->netdev->wanted_features;
4167 }
4168
4169 memset(&crq, 0, sizeof(crq));
4170 crq.control_ip_offload.first = IBMVNIC_CRQ_CMD;
4171 crq.control_ip_offload.cmd = CONTROL_IP_OFFLOAD;
4172 crq.control_ip_offload.len =
4173 cpu_to_be32(sizeof(adapter->ip_offload_ctrl));
4174 crq.control_ip_offload.ioba = cpu_to_be32(adapter->ip_offload_ctrl_tok);
4175 ibmvnic_send_crq(adapter, &crq);
4176}
4177
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02004178static void handle_vpd_size_rsp(union ibmvnic_crq *crq,
4179 struct ibmvnic_adapter *adapter)
4180{
4181 struct device *dev = &adapter->vdev->dev;
4182
4183 if (crq->get_vpd_size_rsp.rc.code) {
4184 dev_err(dev, "Error retrieving VPD size, rc=%x\n",
4185 crq->get_vpd_size_rsp.rc.code);
4186 complete(&adapter->fw_done);
4187 return;
4188 }
4189
4190 adapter->vpd->len = be64_to_cpu(crq->get_vpd_size_rsp.len);
4191 complete(&adapter->fw_done);
4192}
4193
4194static void handle_vpd_rsp(union ibmvnic_crq *crq,
4195 struct ibmvnic_adapter *adapter)
4196{
4197 struct device *dev = &adapter->vdev->dev;
Desnes Augusto Nunes do Rosario21a25452018-02-05 14:33:55 -02004198 unsigned char *substr = NULL;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02004199 u8 fw_level_len = 0;
4200
4201 memset(adapter->fw_version, 0, 32);
4202
4203 dma_unmap_single(dev, adapter->vpd->dma_addr, adapter->vpd->len,
4204 DMA_FROM_DEVICE);
4205
4206 if (crq->get_vpd_rsp.rc.code) {
4207 dev_err(dev, "Error retrieving VPD from device, rc=%x\n",
4208 crq->get_vpd_rsp.rc.code);
4209 goto complete;
4210 }
4211
4212 /* get the position of the firmware version info
4213 * located after the ASCII 'RM' substring in the buffer
4214 */
4215 substr = strnstr(adapter->vpd->buff, "RM", adapter->vpd->len);
4216 if (!substr) {
Desnes Augusto Nunes do Rosarioa1073112018-02-01 16:04:30 -02004217 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 -02004218 goto complete;
4219 }
4220
4221 /* get length of firmware level ASCII substring */
4222 if ((substr + 2) < (adapter->vpd->buff + adapter->vpd->len)) {
4223 fw_level_len = *(substr + 2);
4224 } else {
4225 dev_info(dev, "Length of FW substr extrapolated VDP buff\n");
4226 goto complete;
4227 }
4228
4229 /* copy firmware version string from vpd into adapter */
4230 if ((substr + 3 + fw_level_len) <
4231 (adapter->vpd->buff + adapter->vpd->len)) {
Desnes Augusto Nunes do Rosario21a25452018-02-05 14:33:55 -02004232 strncpy((char *)adapter->fw_version, substr + 3, fw_level_len);
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02004233 } else {
4234 dev_info(dev, "FW substr extrapolated VPD buff\n");
4235 }
4236
4237complete:
Desnes Augusto Nunes do Rosario21a25452018-02-05 14:33:55 -02004238 if (adapter->fw_version[0] == '\0')
4239 strncpy((char *)adapter->fw_version, "N/A", 3 * sizeof(char));
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02004240 complete(&adapter->fw_done);
4241}
4242
Thomas Falcon032c5e82015-12-21 11:26:06 -06004243static void handle_query_ip_offload_rsp(struct ibmvnic_adapter *adapter)
4244{
4245 struct device *dev = &adapter->vdev->dev;
4246 struct ibmvnic_query_ip_offload_buffer *buf = &adapter->ip_offload_buf;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004247 int i;
4248
4249 dma_unmap_single(dev, adapter->ip_offload_tok,
4250 sizeof(adapter->ip_offload_buf), DMA_FROM_DEVICE);
4251
4252 netdev_dbg(adapter->netdev, "Query IP Offload Buffer:\n");
4253 for (i = 0; i < (sizeof(adapter->ip_offload_buf) - 1) / 8 + 1; i++)
4254 netdev_dbg(adapter->netdev, "%016lx\n",
Lijun Pan429aa362021-02-11 00:43:18 -06004255 ((unsigned long *)(buf))[i]);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004256
4257 netdev_dbg(adapter->netdev, "ipv4_chksum = %d\n", buf->ipv4_chksum);
4258 netdev_dbg(adapter->netdev, "ipv6_chksum = %d\n", buf->ipv6_chksum);
4259 netdev_dbg(adapter->netdev, "tcp_ipv4_chksum = %d\n",
4260 buf->tcp_ipv4_chksum);
4261 netdev_dbg(adapter->netdev, "tcp_ipv6_chksum = %d\n",
4262 buf->tcp_ipv6_chksum);
4263 netdev_dbg(adapter->netdev, "udp_ipv4_chksum = %d\n",
4264 buf->udp_ipv4_chksum);
4265 netdev_dbg(adapter->netdev, "udp_ipv6_chksum = %d\n",
4266 buf->udp_ipv6_chksum);
4267 netdev_dbg(adapter->netdev, "large_tx_ipv4 = %d\n",
4268 buf->large_tx_ipv4);
4269 netdev_dbg(adapter->netdev, "large_tx_ipv6 = %d\n",
4270 buf->large_tx_ipv6);
4271 netdev_dbg(adapter->netdev, "large_rx_ipv4 = %d\n",
4272 buf->large_rx_ipv4);
4273 netdev_dbg(adapter->netdev, "large_rx_ipv6 = %d\n",
4274 buf->large_rx_ipv6);
4275 netdev_dbg(adapter->netdev, "max_ipv4_hdr_sz = %d\n",
4276 buf->max_ipv4_header_size);
4277 netdev_dbg(adapter->netdev, "max_ipv6_hdr_sz = %d\n",
4278 buf->max_ipv6_header_size);
4279 netdev_dbg(adapter->netdev, "max_tcp_hdr_size = %d\n",
4280 buf->max_tcp_header_size);
4281 netdev_dbg(adapter->netdev, "max_udp_hdr_size = %d\n",
4282 buf->max_udp_header_size);
4283 netdev_dbg(adapter->netdev, "max_large_tx_size = %d\n",
4284 buf->max_large_tx_size);
4285 netdev_dbg(adapter->netdev, "max_large_rx_size = %d\n",
4286 buf->max_large_rx_size);
4287 netdev_dbg(adapter->netdev, "ipv6_ext_hdr = %d\n",
4288 buf->ipv6_extension_header);
4289 netdev_dbg(adapter->netdev, "tcp_pseudosum_req = %d\n",
4290 buf->tcp_pseudosum_req);
4291 netdev_dbg(adapter->netdev, "num_ipv6_ext_hd = %d\n",
4292 buf->num_ipv6_ext_headers);
4293 netdev_dbg(adapter->netdev, "off_ipv6_ext_hd = %d\n",
4294 buf->off_ipv6_ext_headers);
4295
Lijun Pan46899bd2020-09-27 20:13:30 -05004296 send_control_ip_offload(adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004297}
4298
Thomas Falconc9008d32018-08-06 21:39:59 -05004299static const char *ibmvnic_fw_err_cause(u16 cause)
4300{
4301 switch (cause) {
4302 case ADAPTER_PROBLEM:
4303 return "adapter problem";
4304 case BUS_PROBLEM:
4305 return "bus problem";
4306 case FW_PROBLEM:
4307 return "firmware problem";
4308 case DD_PROBLEM:
4309 return "device driver problem";
4310 case EEH_RECOVERY:
4311 return "EEH recovery";
4312 case FW_UPDATED:
4313 return "firmware updated";
4314 case LOW_MEMORY:
4315 return "low Memory";
4316 default:
4317 return "unknown";
4318 }
4319}
4320
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04004321static void handle_error_indication(union ibmvnic_crq *crq,
4322 struct ibmvnic_adapter *adapter)
4323{
4324 struct device *dev = &adapter->vdev->dev;
Thomas Falconc9008d32018-08-06 21:39:59 -05004325 u16 cause;
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04004326
Thomas Falconc9008d32018-08-06 21:39:59 -05004327 cause = be16_to_cpu(crq->error_indication.error_cause);
4328
4329 dev_warn_ratelimited(dev,
4330 "Firmware reports %serror, cause: %s. Starting recovery...\n",
4331 crq->error_indication.flags
4332 & IBMVNIC_FATAL_ERROR ? "FATAL " : "",
4333 ibmvnic_fw_err_cause(cause));
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04004334
Nathan Fontenoted651a12017-05-03 14:04:38 -04004335 if (crq->error_indication.flags & IBMVNIC_FATAL_ERROR)
4336 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
John Allen8cb31cf2017-05-26 10:30:37 -04004337 else
4338 ibmvnic_reset(adapter, VNIC_RESET_NON_FATAL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004339}
4340
Thomas Falconf8136142018-01-29 13:45:05 -06004341static int handle_change_mac_rsp(union ibmvnic_crq *crq,
4342 struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06004343{
4344 struct net_device *netdev = adapter->netdev;
4345 struct device *dev = &adapter->vdev->dev;
4346 long rc;
4347
4348 rc = crq->change_mac_addr_rsp.rc.code;
4349 if (rc) {
4350 dev_err(dev, "Error %ld in CHANGE_MAC_ADDR_RSP\n", rc);
Thomas Falconf8136142018-01-29 13:45:05 -06004351 goto out;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004352 }
Lijun Pand9b0e592020-10-20 17:39:19 -05004353 /* crq->change_mac_addr.mac_addr is the requested one
4354 * crq->change_mac_addr_rsp.mac_addr is the returned valid one.
4355 */
Thomas Falcon62740e92019-05-09 23:13:43 -05004356 ether_addr_copy(netdev->dev_addr,
4357 &crq->change_mac_addr_rsp.mac_addr[0]);
Lijun Pand9b0e592020-10-20 17:39:19 -05004358 ether_addr_copy(adapter->mac_addr,
4359 &crq->change_mac_addr_rsp.mac_addr[0]);
Thomas Falconf8136142018-01-29 13:45:05 -06004360out:
4361 complete(&adapter->fw_done);
4362 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004363}
4364
4365static void handle_request_cap_rsp(union ibmvnic_crq *crq,
4366 struct ibmvnic_adapter *adapter)
4367{
4368 struct device *dev = &adapter->vdev->dev;
4369 u64 *req_value;
4370 char *name;
4371
Thomas Falcon901e0402017-02-15 12:17:59 -06004372 atomic_dec(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004373 switch (be16_to_cpu(crq->request_capability_rsp.capability)) {
4374 case REQ_TX_QUEUES:
4375 req_value = &adapter->req_tx_queues;
4376 name = "tx";
4377 break;
4378 case REQ_RX_QUEUES:
4379 req_value = &adapter->req_rx_queues;
4380 name = "rx";
4381 break;
4382 case REQ_RX_ADD_QUEUES:
4383 req_value = &adapter->req_rx_add_queues;
4384 name = "rx_add";
4385 break;
4386 case REQ_TX_ENTRIES_PER_SUBCRQ:
4387 req_value = &adapter->req_tx_entries_per_subcrq;
4388 name = "tx_entries_per_subcrq";
4389 break;
4390 case REQ_RX_ADD_ENTRIES_PER_SUBCRQ:
4391 req_value = &adapter->req_rx_add_entries_per_subcrq;
4392 name = "rx_add_entries_per_subcrq";
4393 break;
4394 case REQ_MTU:
4395 req_value = &adapter->req_mtu;
4396 name = "mtu";
4397 break;
4398 case PROMISC_REQUESTED:
4399 req_value = &adapter->promisc;
4400 name = "promisc";
4401 break;
4402 default:
4403 dev_err(dev, "Got invalid cap request rsp %d\n",
4404 crq->request_capability.capability);
4405 return;
4406 }
4407
4408 switch (crq->request_capability_rsp.rc.code) {
4409 case SUCCESS:
4410 break;
4411 case PARTIALSUCCESS:
4412 dev_info(dev, "req=%lld, rsp=%ld in %s queue, retrying.\n",
4413 *req_value,
Lijun Pan429aa362021-02-11 00:43:18 -06004414 (long)be64_to_cpu(crq->request_capability_rsp.
Thomas Falcon032c5e82015-12-21 11:26:06 -06004415 number), name);
John Allene7913802018-01-18 16:27:12 -06004416
4417 if (be16_to_cpu(crq->request_capability_rsp.capability) ==
4418 REQ_MTU) {
4419 pr_err("mtu of %llu is not supported. Reverting.\n",
4420 *req_value);
4421 *req_value = adapter->fallback.mtu;
4422 } else {
4423 *req_value =
4424 be64_to_cpu(crq->request_capability_rsp.number);
4425 }
4426
Lijun Pan09081b92020-09-27 20:13:27 -05004427 send_request_cap(adapter, 1);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004428 return;
4429 default:
4430 dev_err(dev, "Error %d in request cap rsp\n",
4431 crq->request_capability_rsp.rc.code);
4432 return;
4433 }
4434
4435 /* Done receiving requested capabilities, query IP offload support */
Thomas Falcon901e0402017-02-15 12:17:59 -06004436 if (atomic_read(&adapter->running_cap_crqs) == 0) {
Thomas Falcon249168a2017-02-15 12:18:00 -06004437 adapter->wait_capability = false;
Lijun Pan16e811f2020-09-27 20:13:29 -05004438 send_query_ip_offload(adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004439 }
4440}
4441
4442static int handle_login_rsp(union ibmvnic_crq *login_rsp_crq,
4443 struct ibmvnic_adapter *adapter)
4444{
4445 struct device *dev = &adapter->vdev->dev;
John Allenc26eba02017-10-26 16:23:25 -05004446 struct net_device *netdev = adapter->netdev;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004447 struct ibmvnic_login_rsp_buffer *login_rsp = adapter->login_rsp_buf;
4448 struct ibmvnic_login_buffer *login = adapter->login_buf;
Cristobal Fornof3ae59c2020-08-19 13:16:23 -05004449 u64 *tx_handle_array;
4450 u64 *rx_handle_array;
4451 int num_tx_pools;
4452 int num_rx_pools;
Thomas Falcon507ebe62020-08-21 13:39:01 -05004453 u64 *size_array;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004454 int i;
4455
Sukadev Bhattiprolu76cdc5c2020-11-25 18:04:29 -06004456 /* CHECK: Test/set of login_pending does not need to be atomic
4457 * because only ibmvnic_tasklet tests/clears this.
4458 */
4459 if (!adapter->login_pending) {
4460 netdev_warn(netdev, "Ignoring unexpected login response\n");
4461 return 0;
4462 }
4463 adapter->login_pending = false;
4464
Thomas Falcon032c5e82015-12-21 11:26:06 -06004465 dma_unmap_single(dev, adapter->login_buf_token, adapter->login_buf_sz,
Thomas Falcon37e40fa2018-04-06 18:37:02 -05004466 DMA_TO_DEVICE);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004467 dma_unmap_single(dev, adapter->login_rsp_buf_token,
Thomas Falcon37e40fa2018-04-06 18:37:02 -05004468 adapter->login_rsp_buf_sz, DMA_FROM_DEVICE);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004469
John Allen498cd8e2016-04-06 11:49:55 -05004470 /* If the number of queues requested can't be allocated by the
4471 * server, the login response will return with code 1. We will need
4472 * to resend the login buffer with fewer queues requested.
4473 */
4474 if (login_rsp_crq->generic.rc.code) {
Nathan Fontenot64d92aa2018-04-11 10:09:32 -05004475 adapter->init_done_rc = login_rsp_crq->generic.rc.code;
John Allen498cd8e2016-04-06 11:49:55 -05004476 complete(&adapter->init_done);
4477 return 0;
4478 }
4479
John Allenc26eba02017-10-26 16:23:25 -05004480 netdev->mtu = adapter->req_mtu - ETH_HLEN;
4481
Thomas Falcon032c5e82015-12-21 11:26:06 -06004482 netdev_dbg(adapter->netdev, "Login Response Buffer:\n");
4483 for (i = 0; i < (adapter->login_rsp_buf_sz - 1) / 8 + 1; i++) {
4484 netdev_dbg(adapter->netdev, "%016lx\n",
Lijun Pan429aa362021-02-11 00:43:18 -06004485 ((unsigned long *)(adapter->login_rsp_buf))[i]);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004486 }
4487
4488 /* Sanity checks */
4489 if (login->num_txcomp_subcrqs != login_rsp->num_txsubm_subcrqs ||
4490 (be32_to_cpu(login->num_rxcomp_subcrqs) *
4491 adapter->req_rx_add_queues !=
4492 be32_to_cpu(login_rsp->num_rxadd_subcrqs))) {
4493 dev_err(dev, "FATAL: Inconsistent login and login rsp\n");
Dany Madden31d6b402020-11-25 18:04:24 -06004494 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004495 return -EIO;
4496 }
Thomas Falcon507ebe62020-08-21 13:39:01 -05004497 size_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
4498 be32_to_cpu(adapter->login_rsp_buf->off_rxadd_buff_size));
4499 /* variable buffer sizes are not supported, so just read the
4500 * first entry.
4501 */
4502 adapter->cur_rx_buf_sz = be64_to_cpu(size_array[0]);
Cristobal Fornof3ae59c2020-08-19 13:16:23 -05004503
4504 num_tx_pools = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
4505 num_rx_pools = be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
4506
4507 tx_handle_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
4508 be32_to_cpu(adapter->login_rsp_buf->off_txsubm_subcrqs));
4509 rx_handle_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
4510 be32_to_cpu(adapter->login_rsp_buf->off_rxadd_subcrqs));
4511
4512 for (i = 0; i < num_tx_pools; i++)
4513 adapter->tx_scrq[i]->handle = tx_handle_array[i];
4514
4515 for (i = 0; i < num_rx_pools; i++)
4516 adapter->rx_scrq[i]->handle = rx_handle_array[i];
4517
Thomas Falcon507ebe62020-08-21 13:39:01 -05004518 adapter->num_active_tx_scrqs = num_tx_pools;
4519 adapter->num_active_rx_scrqs = num_rx_pools;
Cristobal Fornof3ae59c2020-08-19 13:16:23 -05004520 release_login_rsp_buffer(adapter);
Thomas Falcona2c0f032018-02-21 18:18:30 -06004521 release_login_buffer(adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004522 complete(&adapter->init_done);
4523
Thomas Falcon032c5e82015-12-21 11:26:06 -06004524 return 0;
4525}
4526
Thomas Falcon032c5e82015-12-21 11:26:06 -06004527static void handle_request_unmap_rsp(union ibmvnic_crq *crq,
4528 struct ibmvnic_adapter *adapter)
4529{
4530 struct device *dev = &adapter->vdev->dev;
4531 long rc;
4532
4533 rc = crq->request_unmap_rsp.rc.code;
4534 if (rc)
4535 dev_err(dev, "Error %ld in REQUEST_UNMAP_RSP\n", rc);
4536}
4537
4538static void handle_query_map_rsp(union ibmvnic_crq *crq,
4539 struct ibmvnic_adapter *adapter)
4540{
4541 struct net_device *netdev = adapter->netdev;
4542 struct device *dev = &adapter->vdev->dev;
4543 long rc;
4544
4545 rc = crq->query_map_rsp.rc.code;
4546 if (rc) {
4547 dev_err(dev, "Error %ld in QUERY_MAP_RSP\n", rc);
4548 return;
4549 }
4550 netdev_dbg(netdev, "page_size = %d\ntot_pages = %d\nfree_pages = %d\n",
4551 crq->query_map_rsp.page_size, crq->query_map_rsp.tot_pages,
4552 crq->query_map_rsp.free_pages);
4553}
4554
4555static void handle_query_cap_rsp(union ibmvnic_crq *crq,
4556 struct ibmvnic_adapter *adapter)
4557{
4558 struct net_device *netdev = adapter->netdev;
4559 struct device *dev = &adapter->vdev->dev;
4560 long rc;
4561
Thomas Falcon901e0402017-02-15 12:17:59 -06004562 atomic_dec(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004563 netdev_dbg(netdev, "Outstanding queries: %d\n",
Thomas Falcon901e0402017-02-15 12:17:59 -06004564 atomic_read(&adapter->running_cap_crqs));
Thomas Falcon032c5e82015-12-21 11:26:06 -06004565 rc = crq->query_capability.rc.code;
4566 if (rc) {
4567 dev_err(dev, "Error %ld in QUERY_CAP_RSP\n", rc);
4568 goto out;
4569 }
4570
4571 switch (be16_to_cpu(crq->query_capability.capability)) {
4572 case MIN_TX_QUEUES:
4573 adapter->min_tx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004574 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004575 netdev_dbg(netdev, "min_tx_queues = %lld\n",
4576 adapter->min_tx_queues);
4577 break;
4578 case MIN_RX_QUEUES:
4579 adapter->min_rx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004580 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004581 netdev_dbg(netdev, "min_rx_queues = %lld\n",
4582 adapter->min_rx_queues);
4583 break;
4584 case MIN_RX_ADD_QUEUES:
4585 adapter->min_rx_add_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004586 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004587 netdev_dbg(netdev, "min_rx_add_queues = %lld\n",
4588 adapter->min_rx_add_queues);
4589 break;
4590 case MAX_TX_QUEUES:
4591 adapter->max_tx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004592 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004593 netdev_dbg(netdev, "max_tx_queues = %lld\n",
4594 adapter->max_tx_queues);
4595 break;
4596 case MAX_RX_QUEUES:
4597 adapter->max_rx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004598 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004599 netdev_dbg(netdev, "max_rx_queues = %lld\n",
4600 adapter->max_rx_queues);
4601 break;
4602 case MAX_RX_ADD_QUEUES:
4603 adapter->max_rx_add_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004604 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004605 netdev_dbg(netdev, "max_rx_add_queues = %lld\n",
4606 adapter->max_rx_add_queues);
4607 break;
4608 case MIN_TX_ENTRIES_PER_SUBCRQ:
4609 adapter->min_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06004610 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004611 netdev_dbg(netdev, "min_tx_entries_per_subcrq = %lld\n",
4612 adapter->min_tx_entries_per_subcrq);
4613 break;
4614 case MIN_RX_ADD_ENTRIES_PER_SUBCRQ:
4615 adapter->min_rx_add_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06004616 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004617 netdev_dbg(netdev, "min_rx_add_entrs_per_subcrq = %lld\n",
4618 adapter->min_rx_add_entries_per_subcrq);
4619 break;
4620 case MAX_TX_ENTRIES_PER_SUBCRQ:
4621 adapter->max_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06004622 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004623 netdev_dbg(netdev, "max_tx_entries_per_subcrq = %lld\n",
4624 adapter->max_tx_entries_per_subcrq);
4625 break;
4626 case MAX_RX_ADD_ENTRIES_PER_SUBCRQ:
4627 adapter->max_rx_add_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06004628 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004629 netdev_dbg(netdev, "max_rx_add_entrs_per_subcrq = %lld\n",
4630 adapter->max_rx_add_entries_per_subcrq);
4631 break;
4632 case TCP_IP_OFFLOAD:
4633 adapter->tcp_ip_offload =
Thomas Falconde89e852016-03-01 10:20:09 -06004634 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004635 netdev_dbg(netdev, "tcp_ip_offload = %lld\n",
4636 adapter->tcp_ip_offload);
4637 break;
4638 case PROMISC_SUPPORTED:
4639 adapter->promisc_supported =
Thomas Falconde89e852016-03-01 10:20:09 -06004640 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004641 netdev_dbg(netdev, "promisc_supported = %lld\n",
4642 adapter->promisc_supported);
4643 break;
4644 case MIN_MTU:
Thomas Falconde89e852016-03-01 10:20:09 -06004645 adapter->min_mtu = be64_to_cpu(crq->query_capability.number);
Thomas Falconf39f0d12017-02-14 10:22:59 -06004646 netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004647 netdev_dbg(netdev, "min_mtu = %lld\n", adapter->min_mtu);
4648 break;
4649 case MAX_MTU:
Thomas Falconde89e852016-03-01 10:20:09 -06004650 adapter->max_mtu = be64_to_cpu(crq->query_capability.number);
Thomas Falconf39f0d12017-02-14 10:22:59 -06004651 netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004652 netdev_dbg(netdev, "max_mtu = %lld\n", adapter->max_mtu);
4653 break;
4654 case MAX_MULTICAST_FILTERS:
4655 adapter->max_multicast_filters =
Thomas Falconde89e852016-03-01 10:20:09 -06004656 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004657 netdev_dbg(netdev, "max_multicast_filters = %lld\n",
4658 adapter->max_multicast_filters);
4659 break;
4660 case VLAN_HEADER_INSERTION:
4661 adapter->vlan_header_insertion =
Thomas Falconde89e852016-03-01 10:20:09 -06004662 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004663 if (adapter->vlan_header_insertion)
4664 netdev->features |= NETIF_F_HW_VLAN_STAG_TX;
4665 netdev_dbg(netdev, "vlan_header_insertion = %lld\n",
4666 adapter->vlan_header_insertion);
4667 break;
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04004668 case RX_VLAN_HEADER_INSERTION:
4669 adapter->rx_vlan_header_insertion =
4670 be64_to_cpu(crq->query_capability.number);
4671 netdev_dbg(netdev, "rx_vlan_header_insertion = %lld\n",
4672 adapter->rx_vlan_header_insertion);
4673 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004674 case MAX_TX_SG_ENTRIES:
4675 adapter->max_tx_sg_entries =
Thomas Falconde89e852016-03-01 10:20:09 -06004676 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004677 netdev_dbg(netdev, "max_tx_sg_entries = %lld\n",
4678 adapter->max_tx_sg_entries);
4679 break;
4680 case RX_SG_SUPPORTED:
4681 adapter->rx_sg_supported =
Thomas Falconde89e852016-03-01 10:20:09 -06004682 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004683 netdev_dbg(netdev, "rx_sg_supported = %lld\n",
4684 adapter->rx_sg_supported);
4685 break;
4686 case OPT_TX_COMP_SUB_QUEUES:
4687 adapter->opt_tx_comp_sub_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004688 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004689 netdev_dbg(netdev, "opt_tx_comp_sub_queues = %lld\n",
4690 adapter->opt_tx_comp_sub_queues);
4691 break;
4692 case OPT_RX_COMP_QUEUES:
4693 adapter->opt_rx_comp_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004694 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004695 netdev_dbg(netdev, "opt_rx_comp_queues = %lld\n",
4696 adapter->opt_rx_comp_queues);
4697 break;
4698 case OPT_RX_BUFADD_Q_PER_RX_COMP_Q:
4699 adapter->opt_rx_bufadd_q_per_rx_comp_q =
Thomas Falconde89e852016-03-01 10:20:09 -06004700 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004701 netdev_dbg(netdev, "opt_rx_bufadd_q_per_rx_comp_q = %lld\n",
4702 adapter->opt_rx_bufadd_q_per_rx_comp_q);
4703 break;
4704 case OPT_TX_ENTRIES_PER_SUBCRQ:
4705 adapter->opt_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06004706 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004707 netdev_dbg(netdev, "opt_tx_entries_per_subcrq = %lld\n",
4708 adapter->opt_tx_entries_per_subcrq);
4709 break;
4710 case OPT_RXBA_ENTRIES_PER_SUBCRQ:
4711 adapter->opt_rxba_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06004712 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004713 netdev_dbg(netdev, "opt_rxba_entries_per_subcrq = %lld\n",
4714 adapter->opt_rxba_entries_per_subcrq);
4715 break;
4716 case TX_RX_DESC_REQ:
4717 adapter->tx_rx_desc_req = crq->query_capability.number;
4718 netdev_dbg(netdev, "tx_rx_desc_req = %llx\n",
4719 adapter->tx_rx_desc_req);
4720 break;
4721
4722 default:
4723 netdev_err(netdev, "Got invalid cap rsp %d\n",
4724 crq->query_capability.capability);
4725 }
4726
4727out:
Thomas Falcon249168a2017-02-15 12:18:00 -06004728 if (atomic_read(&adapter->running_cap_crqs) == 0) {
4729 adapter->wait_capability = false;
Lijun Pan09081b92020-09-27 20:13:27 -05004730 send_request_cap(adapter, 0);
Thomas Falcon249168a2017-02-15 12:18:00 -06004731 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06004732}
4733
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03004734static int send_query_phys_parms(struct ibmvnic_adapter *adapter)
4735{
4736 union ibmvnic_crq crq;
4737 int rc;
4738
4739 memset(&crq, 0, sizeof(crq));
4740 crq.query_phys_parms.first = IBMVNIC_CRQ_CMD;
4741 crq.query_phys_parms.cmd = QUERY_PHYS_PARMS;
Thomas Falconff25dcb2019-11-25 17:12:56 -06004742
4743 mutex_lock(&adapter->fw_lock);
4744 adapter->fw_done_rc = 0;
Thomas Falcon070eca92019-11-25 17:12:53 -06004745 reinit_completion(&adapter->fw_done);
Thomas Falconff25dcb2019-11-25 17:12:56 -06004746
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03004747 rc = ibmvnic_send_crq(adapter, &crq);
Thomas Falconff25dcb2019-11-25 17:12:56 -06004748 if (rc) {
4749 mutex_unlock(&adapter->fw_lock);
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03004750 return rc;
Thomas Falconff25dcb2019-11-25 17:12:56 -06004751 }
Thomas Falcon476d96c2019-11-25 17:12:55 -06004752
4753 rc = ibmvnic_wait_for_completion(adapter, &adapter->fw_done, 10000);
Thomas Falconff25dcb2019-11-25 17:12:56 -06004754 if (rc) {
4755 mutex_unlock(&adapter->fw_lock);
Thomas Falcon476d96c2019-11-25 17:12:55 -06004756 return rc;
Thomas Falconff25dcb2019-11-25 17:12:56 -06004757 }
Thomas Falcon476d96c2019-11-25 17:12:55 -06004758
Thomas Falconff25dcb2019-11-25 17:12:56 -06004759 mutex_unlock(&adapter->fw_lock);
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03004760 return adapter->fw_done_rc ? -EIO : 0;
4761}
4762
4763static int handle_query_phys_parms_rsp(union ibmvnic_crq *crq,
4764 struct ibmvnic_adapter *adapter)
4765{
4766 struct net_device *netdev = adapter->netdev;
4767 int rc;
Murilo Fossa Vicentinidd0f9d82019-09-16 11:50:37 -03004768 __be32 rspeed = cpu_to_be32(crq->query_phys_parms_rsp.speed);
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03004769
4770 rc = crq->query_phys_parms_rsp.rc.code;
4771 if (rc) {
4772 netdev_err(netdev, "Error %d in QUERY_PHYS_PARMS\n", rc);
4773 return rc;
4774 }
Murilo Fossa Vicentinidd0f9d82019-09-16 11:50:37 -03004775 switch (rspeed) {
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03004776 case IBMVNIC_10MBPS:
4777 adapter->speed = SPEED_10;
4778 break;
4779 case IBMVNIC_100MBPS:
4780 adapter->speed = SPEED_100;
4781 break;
4782 case IBMVNIC_1GBPS:
4783 adapter->speed = SPEED_1000;
4784 break;
Lijun Panb9cd7952020-09-27 19:06:25 -05004785 case IBMVNIC_10GBPS:
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03004786 adapter->speed = SPEED_10000;
4787 break;
4788 case IBMVNIC_25GBPS:
4789 adapter->speed = SPEED_25000;
4790 break;
4791 case IBMVNIC_40GBPS:
4792 adapter->speed = SPEED_40000;
4793 break;
4794 case IBMVNIC_50GBPS:
4795 adapter->speed = SPEED_50000;
4796 break;
4797 case IBMVNIC_100GBPS:
4798 adapter->speed = SPEED_100000;
4799 break;
Lijun Panb9cd7952020-09-27 19:06:25 -05004800 case IBMVNIC_200GBPS:
4801 adapter->speed = SPEED_200000;
4802 break;
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03004803 default:
Murilo Fossa Vicentinidd0f9d82019-09-16 11:50:37 -03004804 if (netif_carrier_ok(netdev))
4805 netdev_warn(netdev, "Unknown speed 0x%08x\n", rspeed);
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03004806 adapter->speed = SPEED_UNKNOWN;
4807 }
4808 if (crq->query_phys_parms_rsp.flags1 & IBMVNIC_FULL_DUPLEX)
4809 adapter->duplex = DUPLEX_FULL;
4810 else if (crq->query_phys_parms_rsp.flags1 & IBMVNIC_HALF_DUPLEX)
4811 adapter->duplex = DUPLEX_HALF;
4812 else
4813 adapter->duplex = DUPLEX_UNKNOWN;
4814
4815 return rc;
4816}
4817
Thomas Falcon032c5e82015-12-21 11:26:06 -06004818static void ibmvnic_handle_crq(union ibmvnic_crq *crq,
4819 struct ibmvnic_adapter *adapter)
4820{
4821 struct ibmvnic_generic_crq *gen_crq = &crq->generic;
4822 struct net_device *netdev = adapter->netdev;
4823 struct device *dev = &adapter->vdev->dev;
Murilo Fossa Vicentini993a82b2017-04-19 13:44:35 -04004824 u64 *u64_crq = (u64 *)crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004825 long rc;
4826
4827 netdev_dbg(netdev, "Handling CRQ: %016lx %016lx\n",
Lijun Pan429aa362021-02-11 00:43:18 -06004828 (unsigned long)cpu_to_be64(u64_crq[0]),
4829 (unsigned long)cpu_to_be64(u64_crq[1]));
Thomas Falcon032c5e82015-12-21 11:26:06 -06004830 switch (gen_crq->first) {
4831 case IBMVNIC_CRQ_INIT_RSP:
4832 switch (gen_crq->cmd) {
4833 case IBMVNIC_CRQ_INIT:
4834 dev_info(dev, "Partner initialized\n");
John Allen017892c12017-05-26 10:30:19 -04004835 adapter->from_passive_init = true;
Sukadev Bhattiprolu76cdc5c2020-11-25 18:04:29 -06004836 /* Discard any stale login responses from prev reset.
4837 * CHECK: should we clear even on INIT_COMPLETE?
4838 */
4839 adapter->login_pending = false;
4840
Thomas Falcon17c87052018-05-23 13:37:58 -05004841 if (!completion_done(&adapter->init_done)) {
4842 complete(&adapter->init_done);
4843 adapter->init_done_rc = -EIO;
4844 }
Sukadev Bhattiproluef66a1e2021-02-02 21:08:02 -08004845 rc = ibmvnic_reset(adapter, VNIC_RESET_FAILOVER);
4846 if (rc && rc != -EBUSY) {
4847 /* We were unable to schedule the failover
4848 * reset either because the adapter was still
4849 * probing (eg: during kexec) or we could not
4850 * allocate memory. Clear the failover_pending
4851 * flag since no one else will. We ignore
4852 * EBUSY because it means either FAILOVER reset
4853 * is already scheduled or the adapter is
4854 * being removed.
4855 */
4856 netdev_err(netdev,
4857 "Error %ld scheduling failover reset\n",
4858 rc);
4859 adapter->failover_pending = false;
4860 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06004861 break;
4862 case IBMVNIC_CRQ_INIT_COMPLETE:
4863 dev_info(dev, "Partner initialization complete\n");
Thomas Falcon51536982018-05-23 13:37:56 -05004864 adapter->crq.active = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004865 send_version_xchg(adapter);
4866 break;
4867 default:
4868 dev_err(dev, "Unknown crq cmd: %d\n", gen_crq->cmd);
4869 }
4870 return;
4871 case IBMVNIC_CRQ_XPORT_EVENT:
Nathan Fontenoted651a12017-05-03 14:04:38 -04004872 netif_carrier_off(netdev);
Thomas Falcon51536982018-05-23 13:37:56 -05004873 adapter->crq.active = false;
Thomas Falcon2147e3d2019-11-25 17:12:54 -06004874 /* terminate any thread waiting for a response
4875 * from the device
4876 */
4877 if (!completion_done(&adapter->fw_done)) {
4878 adapter->fw_done_rc = -EIO;
4879 complete(&adapter->fw_done);
4880 }
4881 if (!completion_done(&adapter->stats_done))
4882 complete(&adapter->stats_done);
Juliet Kim7ed5b312019-09-20 16:11:23 -04004883 if (test_bit(0, &adapter->resetting))
Thomas Falcon2770a792018-05-23 13:38:02 -05004884 adapter->force_reset_recovery = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004885 if (gen_crq->cmd == IBMVNIC_PARTITION_MIGRATED) {
Nathan Fontenoted651a12017-05-03 14:04:38 -04004886 dev_info(dev, "Migrated, re-enabling adapter\n");
4887 ibmvnic_reset(adapter, VNIC_RESET_MOBILITY);
Thomas Falcondfad09a2016-08-18 11:37:51 -05004888 } else if (gen_crq->cmd == IBMVNIC_DEVICE_FAILOVER) {
4889 dev_info(dev, "Backing device failover detected\n");
Thomas Falcon5a18e1e2018-04-06 18:37:05 -05004890 adapter->failover_pending = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004891 } else {
4892 /* The adapter lost the connection */
4893 dev_err(dev, "Virtual Adapter failed (rc=%d)\n",
4894 gen_crq->cmd);
Nathan Fontenoted651a12017-05-03 14:04:38 -04004895 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004896 }
4897 return;
4898 case IBMVNIC_CRQ_CMD_RSP:
4899 break;
4900 default:
4901 dev_err(dev, "Got an invalid msg type 0x%02x\n",
4902 gen_crq->first);
4903 return;
4904 }
4905
4906 switch (gen_crq->cmd) {
4907 case VERSION_EXCHANGE_RSP:
4908 rc = crq->version_exchange_rsp.rc.code;
4909 if (rc) {
4910 dev_err(dev, "Error %ld in VERSION_EXCHG_RSP\n", rc);
4911 break;
4912 }
Thomas Falcon78468892020-05-28 11:19:17 -05004913 ibmvnic_version =
Thomas Falcon032c5e82015-12-21 11:26:06 -06004914 be16_to_cpu(crq->version_exchange_rsp.version);
Thomas Falcon78468892020-05-28 11:19:17 -05004915 dev_info(dev, "Partner protocol version is %d\n",
4916 ibmvnic_version);
Lijun Pan491099a2020-09-27 20:13:26 -05004917 send_query_cap(adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004918 break;
4919 case QUERY_CAPABILITY_RSP:
4920 handle_query_cap_rsp(crq, adapter);
4921 break;
4922 case QUERY_MAP_RSP:
4923 handle_query_map_rsp(crq, adapter);
4924 break;
4925 case REQUEST_MAP_RSP:
Thomas Falconf3be0cb2017-06-21 14:53:01 -05004926 adapter->fw_done_rc = crq->request_map_rsp.rc.code;
4927 complete(&adapter->fw_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004928 break;
4929 case REQUEST_UNMAP_RSP:
4930 handle_request_unmap_rsp(crq, adapter);
4931 break;
4932 case REQUEST_CAPABILITY_RSP:
4933 handle_request_cap_rsp(crq, adapter);
4934 break;
4935 case LOGIN_RSP:
4936 netdev_dbg(netdev, "Got Login Response\n");
4937 handle_login_rsp(crq, adapter);
4938 break;
4939 case LOGICAL_LINK_STATE_RSP:
Nathan Fontenot53da09e2017-04-21 15:39:04 -04004940 netdev_dbg(netdev,
4941 "Got Logical Link State Response, state: %d rc: %d\n",
4942 crq->logical_link_state_rsp.link_state,
4943 crq->logical_link_state_rsp.rc.code);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004944 adapter->logical_link_state =
4945 crq->logical_link_state_rsp.link_state;
Nathan Fontenot53da09e2017-04-21 15:39:04 -04004946 adapter->init_done_rc = crq->logical_link_state_rsp.rc.code;
4947 complete(&adapter->init_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004948 break;
4949 case LINK_STATE_INDICATION:
4950 netdev_dbg(netdev, "Got Logical Link State Indication\n");
4951 adapter->phys_link_state =
4952 crq->link_state_indication.phys_link_state;
4953 adapter->logical_link_state =
4954 crq->link_state_indication.logical_link_state;
Thomas Falcon0655f992019-05-09 23:13:44 -05004955 if (adapter->phys_link_state && adapter->logical_link_state)
4956 netif_carrier_on(netdev);
4957 else
4958 netif_carrier_off(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004959 break;
4960 case CHANGE_MAC_ADDR_RSP:
4961 netdev_dbg(netdev, "Got MAC address change Response\n");
Thomas Falconf8136142018-01-29 13:45:05 -06004962 adapter->fw_done_rc = handle_change_mac_rsp(crq, adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004963 break;
4964 case ERROR_INDICATION:
4965 netdev_dbg(netdev, "Got Error Indication\n");
4966 handle_error_indication(crq, adapter);
4967 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004968 case REQUEST_STATISTICS_RSP:
4969 netdev_dbg(netdev, "Got Statistics Response\n");
4970 complete(&adapter->stats_done);
4971 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004972 case QUERY_IP_OFFLOAD_RSP:
4973 netdev_dbg(netdev, "Got Query IP offload Response\n");
4974 handle_query_ip_offload_rsp(adapter);
4975 break;
4976 case MULTICAST_CTRL_RSP:
4977 netdev_dbg(netdev, "Got multicast control Response\n");
4978 break;
4979 case CONTROL_IP_OFFLOAD_RSP:
4980 netdev_dbg(netdev, "Got Control IP offload Response\n");
4981 dma_unmap_single(dev, adapter->ip_offload_ctrl_tok,
4982 sizeof(adapter->ip_offload_ctrl),
4983 DMA_TO_DEVICE);
John Allenbd0b6722017-03-17 17:13:40 -05004984 complete(&adapter->init_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004985 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004986 case COLLECT_FW_TRACE_RSP:
4987 netdev_dbg(netdev, "Got Collect firmware trace Response\n");
4988 complete(&adapter->fw_done);
4989 break;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02004990 case GET_VPD_SIZE_RSP:
4991 handle_vpd_size_rsp(crq, adapter);
4992 break;
4993 case GET_VPD_RSP:
4994 handle_vpd_rsp(crq, adapter);
4995 break;
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03004996 case QUERY_PHYS_PARMS_RSP:
4997 adapter->fw_done_rc = handle_query_phys_parms_rsp(crq, adapter);
4998 complete(&adapter->fw_done);
4999 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005000 default:
5001 netdev_err(netdev, "Got an invalid cmd type 0x%02x\n",
5002 gen_crq->cmd);
5003 }
5004}
5005
5006static irqreturn_t ibmvnic_interrupt(int irq, void *instance)
5007{
5008 struct ibmvnic_adapter *adapter = instance;
Thomas Falcon6c267b32017-02-15 12:17:58 -06005009
Thomas Falcon6c267b32017-02-15 12:17:58 -06005010 tasklet_schedule(&adapter->tasklet);
Thomas Falcon6c267b32017-02-15 12:17:58 -06005011 return IRQ_HANDLED;
5012}
5013
Allen Paisaa7c3fe2020-09-14 12:59:29 +05305014static void ibmvnic_tasklet(struct tasklet_struct *t)
Thomas Falcon6c267b32017-02-15 12:17:58 -06005015{
Allen Paisaa7c3fe2020-09-14 12:59:29 +05305016 struct ibmvnic_adapter *adapter = from_tasklet(adapter, t, tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005017 struct ibmvnic_crq_queue *queue = &adapter->crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005018 union ibmvnic_crq *crq;
5019 unsigned long flags;
5020 bool done = false;
5021
5022 spin_lock_irqsave(&queue->lock, flags);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005023 while (!done) {
5024 /* Pull all the valid messages off the CRQ */
5025 while ((crq = ibmvnic_next_crq(adapter)) != NULL) {
Lijun Pane41aec72021-01-27 19:34:42 -06005026 /* This barrier makes sure ibmvnic_next_crq()'s
5027 * crq->generic.first & IBMVNIC_CRQ_CMD_RSP is loaded
5028 * before ibmvnic_handle_crq()'s
5029 * switch(gen_crq->first) and switch(gen_crq->cmd).
5030 */
5031 dma_rmb();
Thomas Falcon032c5e82015-12-21 11:26:06 -06005032 ibmvnic_handle_crq(crq, adapter);
5033 crq->generic.first = 0;
5034 }
Brian Kinged7ecbf2017-04-19 13:44:53 -04005035
5036 /* remain in tasklet until all
5037 * capabilities responses are received
5038 */
5039 if (!adapter->wait_capability)
5040 done = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005041 }
Thomas Falcon249168a2017-02-15 12:18:00 -06005042 /* if capabilities CRQ's were sent in this tasklet, the following
5043 * tasklet must wait until all responses are received
5044 */
5045 if (atomic_read(&adapter->running_cap_crqs) != 0)
5046 adapter->wait_capability = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005047 spin_unlock_irqrestore(&queue->lock, flags);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005048}
5049
5050static int ibmvnic_reenable_crq_queue(struct ibmvnic_adapter *adapter)
5051{
5052 struct vio_dev *vdev = adapter->vdev;
5053 int rc;
5054
5055 do {
5056 rc = plpar_hcall_norets(H_ENABLE_CRQ, vdev->unit_address);
5057 } while (rc == H_IN_PROGRESS || rc == H_BUSY || H_IS_LONG_BUSY(rc));
5058
5059 if (rc)
5060 dev_err(&vdev->dev, "Error enabling adapter (rc=%d)\n", rc);
5061
5062 return rc;
5063}
5064
5065static int ibmvnic_reset_crq(struct ibmvnic_adapter *adapter)
5066{
5067 struct ibmvnic_crq_queue *crq = &adapter->crq;
5068 struct device *dev = &adapter->vdev->dev;
5069 struct vio_dev *vdev = adapter->vdev;
5070 int rc;
5071
5072 /* Close the CRQ */
5073 do {
5074 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
5075 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
5076
5077 /* Clean out the queue */
Lijun Pan0e435be2020-11-23 13:35:46 -06005078 if (!crq->msgs)
5079 return -EINVAL;
5080
Thomas Falcon032c5e82015-12-21 11:26:06 -06005081 memset(crq->msgs, 0, PAGE_SIZE);
5082 crq->cur = 0;
Thomas Falcon51536982018-05-23 13:37:56 -05005083 crq->active = false;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005084
5085 /* And re-open it again */
5086 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
5087 crq->msg_token, PAGE_SIZE);
5088
5089 if (rc == H_CLOSED)
5090 /* Adapter is good, but other end is not ready */
5091 dev_warn(dev, "Partner adapter not ready\n");
5092 else if (rc != 0)
5093 dev_warn(dev, "Couldn't register crq (rc=%d)\n", rc);
5094
5095 return rc;
5096}
5097
Nathan Fontenotf9928872017-03-30 02:48:54 -04005098static void release_crq_queue(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06005099{
5100 struct ibmvnic_crq_queue *crq = &adapter->crq;
5101 struct vio_dev *vdev = adapter->vdev;
5102 long rc;
5103
Nathan Fontenotf9928872017-03-30 02:48:54 -04005104 if (!crq->msgs)
5105 return;
5106
Thomas Falcon032c5e82015-12-21 11:26:06 -06005107 netdev_dbg(adapter->netdev, "Releasing CRQ\n");
5108 free_irq(vdev->irq, adapter);
Thomas Falcon6c267b32017-02-15 12:17:58 -06005109 tasklet_kill(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005110 do {
5111 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
5112 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
5113
5114 dma_unmap_single(&vdev->dev, crq->msg_token, PAGE_SIZE,
5115 DMA_BIDIRECTIONAL);
5116 free_page((unsigned long)crq->msgs);
Nathan Fontenotf9928872017-03-30 02:48:54 -04005117 crq->msgs = NULL;
Thomas Falcon51536982018-05-23 13:37:56 -05005118 crq->active = false;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005119}
5120
Nathan Fontenotf9928872017-03-30 02:48:54 -04005121static int init_crq_queue(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06005122{
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, retrc = -ENOMEM;
5127
Nathan Fontenotf9928872017-03-30 02:48:54 -04005128 if (crq->msgs)
5129 return 0;
5130
Thomas Falcon032c5e82015-12-21 11:26:06 -06005131 crq->msgs = (union ibmvnic_crq *)get_zeroed_page(GFP_KERNEL);
5132 /* Should we allocate more than one page? */
5133
5134 if (!crq->msgs)
5135 return -ENOMEM;
5136
5137 crq->size = PAGE_SIZE / sizeof(*crq->msgs);
5138 crq->msg_token = dma_map_single(dev, crq->msgs, PAGE_SIZE,
5139 DMA_BIDIRECTIONAL);
5140 if (dma_mapping_error(dev, crq->msg_token))
5141 goto map_failed;
5142
5143 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
5144 crq->msg_token, PAGE_SIZE);
5145
5146 if (rc == H_RESOURCE)
5147 /* maybe kexecing and resource is busy. try a reset */
5148 rc = ibmvnic_reset_crq(adapter);
5149 retrc = rc;
5150
5151 if (rc == H_CLOSED) {
5152 dev_warn(dev, "Partner adapter not ready\n");
5153 } else if (rc) {
5154 dev_warn(dev, "Error %d opening adapter\n", rc);
5155 goto reg_crq_failed;
5156 }
5157
5158 retrc = 0;
5159
Allen Paisaa7c3fe2020-09-14 12:59:29 +05305160 tasklet_setup(&adapter->tasklet, (void *)ibmvnic_tasklet);
Thomas Falcon6c267b32017-02-15 12:17:58 -06005161
Thomas Falcon032c5e82015-12-21 11:26:06 -06005162 netdev_dbg(adapter->netdev, "registering irq 0x%x\n", vdev->irq);
Murilo Fossa Vicentinie56e2512019-04-25 11:02:33 -03005163 snprintf(crq->name, sizeof(crq->name), "ibmvnic-%x",
5164 adapter->vdev->unit_address);
5165 rc = request_irq(vdev->irq, ibmvnic_interrupt, 0, crq->name, adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005166 if (rc) {
5167 dev_err(dev, "Couldn't register irq 0x%x. rc=%d\n",
5168 vdev->irq, rc);
5169 goto req_irq_failed;
5170 }
5171
5172 rc = vio_enable_interrupts(vdev);
5173 if (rc) {
5174 dev_err(dev, "Error %d enabling interrupts\n", rc);
5175 goto req_irq_failed;
5176 }
5177
5178 crq->cur = 0;
5179 spin_lock_init(&crq->lock);
5180
5181 return retrc;
5182
5183req_irq_failed:
Thomas Falcon6c267b32017-02-15 12:17:58 -06005184 tasklet_kill(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005185 do {
5186 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
5187 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
5188reg_crq_failed:
5189 dma_unmap_single(dev, crq->msg_token, PAGE_SIZE, DMA_BIDIRECTIONAL);
5190map_failed:
5191 free_page((unsigned long)crq->msgs);
Nathan Fontenotf9928872017-03-30 02:48:54 -04005192 crq->msgs = NULL;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005193 return retrc;
5194}
5195
Lijun Pan635e4422020-08-19 17:52:26 -05005196static int ibmvnic_reset_init(struct ibmvnic_adapter *adapter, bool reset)
John Allenf6ef6402017-03-17 17:13:42 -05005197{
5198 struct device *dev = &adapter->vdev->dev;
Dany Madden98c41f02020-11-25 18:04:32 -06005199 unsigned long timeout = msecs_to_jiffies(20000);
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06005200 u64 old_num_rx_queues, old_num_tx_queues;
John Allenf6ef6402017-03-17 17:13:42 -05005201 int rc;
5202
John Allen017892c12017-05-26 10:30:19 -04005203 adapter->from_passive_init = false;
5204
Lijun Pan635e4422020-08-19 17:52:26 -05005205 if (reset) {
5206 old_num_rx_queues = adapter->req_rx_queues;
5207 old_num_tx_queues = adapter->req_tx_queues;
5208 reinit_completion(&adapter->init_done);
5209 }
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06005210
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04005211 adapter->init_done_rc = 0;
Lijun Panfa68bfa2020-08-19 17:52:24 -05005212 rc = ibmvnic_send_crq_init(adapter);
5213 if (rc) {
5214 dev_err(dev, "Send crq init failed with error %d\n", rc);
5215 return rc;
5216 }
5217
John Allenf6ef6402017-03-17 17:13:42 -05005218 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
5219 dev_err(dev, "Initialization sequence timed out\n");
John Allen017892c12017-05-26 10:30:19 -04005220 return -1;
5221 }
5222
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04005223 if (adapter->init_done_rc) {
5224 release_crq_queue(adapter);
5225 return adapter->init_done_rc;
5226 }
5227
Lijun Pan785a2b12020-09-17 21:12:46 -05005228 if (adapter->from_passive_init) {
5229 adapter->state = VNIC_OPEN;
5230 adapter->from_passive_init = false;
5231 return -1;
5232 }
5233
Lijun Pan635e4422020-08-19 17:52:26 -05005234 if (reset &&
5235 test_bit(0, &adapter->resetting) && !adapter->wait_for_reset &&
Nathan Fontenot30f79622018-04-06 18:37:06 -05005236 adapter->reset_reason != VNIC_RESET_MOBILITY) {
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06005237 if (adapter->req_rx_queues != old_num_rx_queues ||
5238 adapter->req_tx_queues != old_num_tx_queues) {
5239 release_sub_crqs(adapter, 0);
5240 rc = init_sub_crqs(adapter);
5241 } else {
5242 rc = reset_sub_crq_queues(adapter);
5243 }
5244 } else {
Nathan Fontenot57a49432017-05-26 10:31:12 -04005245 rc = init_sub_crqs(adapter);
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06005246 }
5247
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04005248 if (rc) {
5249 dev_err(dev, "Initialization of sub crqs failed\n");
5250 release_crq_queue(adapter);
Thomas Falcon5df969c2017-06-28 19:55:54 -05005251 return rc;
5252 }
5253
5254 rc = init_sub_crq_irqs(adapter);
5255 if (rc) {
5256 dev_err(dev, "Failed to initialize sub crq irqs\n");
5257 release_crq_queue(adapter);
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04005258 }
5259
5260 return rc;
John Allenf6ef6402017-03-17 17:13:42 -05005261}
5262
Thomas Falcon40c9db82017-06-12 12:35:04 -05005263static struct device_attribute dev_attr_failover;
5264
Thomas Falcon032c5e82015-12-21 11:26:06 -06005265static int ibmvnic_probe(struct vio_dev *dev, const struct vio_device_id *id)
5266{
5267 struct ibmvnic_adapter *adapter;
5268 struct net_device *netdev;
5269 unsigned char *mac_addr_p;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005270 int rc;
5271
5272 dev_dbg(&dev->dev, "entering ibmvnic_probe for UA 0x%x\n",
5273 dev->unit_address);
5274
5275 mac_addr_p = (unsigned char *)vio_get_attribute(dev,
5276 VETH_MAC_ADDR, NULL);
5277 if (!mac_addr_p) {
5278 dev_err(&dev->dev,
5279 "(%s:%3.3d) ERROR: Can't find MAC_ADDR attribute\n",
5280 __FILE__, __LINE__);
5281 return 0;
5282 }
5283
5284 netdev = alloc_etherdev_mq(sizeof(struct ibmvnic_adapter),
Thomas Falcond45cc3a2017-12-18 12:52:11 -06005285 IBMVNIC_MAX_QUEUES);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005286 if (!netdev)
5287 return -ENOMEM;
5288
5289 adapter = netdev_priv(netdev);
Nathan Fontenot90c80142017-05-03 14:04:32 -04005290 adapter->state = VNIC_PROBING;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005291 dev_set_drvdata(&dev->dev, netdev);
5292 adapter->vdev = dev;
5293 adapter->netdev = netdev;
Sukadev Bhattiprolu76cdc5c2020-11-25 18:04:29 -06005294 adapter->login_pending = false;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005295
5296 ether_addr_copy(adapter->mac_addr, mac_addr_p);
5297 ether_addr_copy(netdev->dev_addr, adapter->mac_addr);
5298 netdev->irq = dev->irq;
5299 netdev->netdev_ops = &ibmvnic_netdev_ops;
5300 netdev->ethtool_ops = &ibmvnic_ethtool_ops;
5301 SET_NETDEV_DEV(netdev, &dev->dev);
5302
5303 spin_lock_init(&adapter->stats_lock);
5304
Nathan Fontenoted651a12017-05-03 14:04:38 -04005305 INIT_WORK(&adapter->ibmvnic_reset, __ibmvnic_reset);
Juliet Kim7ed5b312019-09-20 16:11:23 -04005306 INIT_DELAYED_WORK(&adapter->ibmvnic_delayed_reset,
5307 __ibmvnic_delayed_reset);
Nathan Fontenoted651a12017-05-03 14:04:38 -04005308 INIT_LIST_HEAD(&adapter->rwi_list);
Thomas Falcon6c5c7482018-12-10 15:22:22 -06005309 spin_lock_init(&adapter->rwi_lock);
Juliet Kim7d7195a2020-03-10 09:23:58 -05005310 spin_lock_init(&adapter->state_lock);
Thomas Falconff25dcb2019-11-25 17:12:56 -06005311 mutex_init(&adapter->fw_lock);
Thomas Falconbbd669a2019-04-04 18:58:26 -05005312 init_completion(&adapter->init_done);
Thomas Falcon070eca92019-11-25 17:12:53 -06005313 init_completion(&adapter->fw_done);
5314 init_completion(&adapter->reset_done);
5315 init_completion(&adapter->stats_done);
Juliet Kim7ed5b312019-09-20 16:11:23 -04005316 clear_bit(0, &adapter->resetting);
Nathan Fontenoted651a12017-05-03 14:04:38 -04005317
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04005318 do {
Nathan Fontenot30f79622018-04-06 18:37:06 -05005319 rc = init_crq_queue(adapter);
5320 if (rc) {
5321 dev_err(&dev->dev, "Couldn't initialize crq. rc=%d\n",
5322 rc);
5323 goto ibmvnic_init_fail;
5324 }
5325
Lijun Pan635e4422020-08-19 17:52:26 -05005326 rc = ibmvnic_reset_init(adapter, false);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05005327 if (rc && rc != EAGAIN)
5328 goto ibmvnic_init_fail;
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04005329 } while (rc == EAGAIN);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005330
Thomas Falcon07184212018-05-16 15:49:05 -05005331 rc = init_stats_buffers(adapter);
5332 if (rc)
5333 goto ibmvnic_init_fail;
5334
5335 rc = init_stats_token(adapter);
5336 if (rc)
5337 goto ibmvnic_stats_fail;
5338
Thomas Falconf39f0d12017-02-14 10:22:59 -06005339 netdev->mtu = adapter->req_mtu - ETH_HLEN;
John Allenc26eba02017-10-26 16:23:25 -05005340 netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
5341 netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005342
Thomas Falcon40c9db82017-06-12 12:35:04 -05005343 rc = device_create_file(&dev->dev, &dev_attr_failover);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05005344 if (rc)
Thomas Falcon07184212018-05-16 15:49:05 -05005345 goto ibmvnic_dev_file_err;
Thomas Falcon40c9db82017-06-12 12:35:04 -05005346
Mick Tarsele876a8a2017-09-28 13:53:18 -07005347 netif_carrier_off(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005348 rc = register_netdev(netdev);
5349 if (rc) {
5350 dev_err(&dev->dev, "failed to register netdev rc=%d\n", rc);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05005351 goto ibmvnic_register_fail;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005352 }
5353 dev_info(&dev->dev, "ibmvnic registered\n");
5354
Nathan Fontenot90c80142017-05-03 14:04:32 -04005355 adapter->state = VNIC_PROBED;
John Allenc26eba02017-10-26 16:23:25 -05005356
5357 adapter->wait_for_reset = false;
Dany Maddena86d5c62020-11-25 18:04:31 -06005358 adapter->last_reset_time = jiffies;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005359 return 0;
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05005360
5361ibmvnic_register_fail:
5362 device_remove_file(&dev->dev, &dev_attr_failover);
5363
Thomas Falcon07184212018-05-16 15:49:05 -05005364ibmvnic_dev_file_err:
5365 release_stats_token(adapter);
5366
5367ibmvnic_stats_fail:
5368 release_stats_buffers(adapter);
5369
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05005370ibmvnic_init_fail:
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06005371 release_sub_crqs(adapter, 1);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05005372 release_crq_queue(adapter);
Thomas Falconff25dcb2019-11-25 17:12:56 -06005373 mutex_destroy(&adapter->fw_lock);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05005374 free_netdev(netdev);
5375
5376 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005377}
5378
5379static int ibmvnic_remove(struct vio_dev *dev)
5380{
5381 struct net_device *netdev = dev_get_drvdata(&dev->dev);
Nathan Fontenot37489052017-04-19 13:45:04 -04005382 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Juliet Kim7d7195a2020-03-10 09:23:58 -05005383 unsigned long flags;
5384
5385 spin_lock_irqsave(&adapter->state_lock, flags);
Nathan Fontenot90c80142017-05-03 14:04:32 -04005386 adapter->state = VNIC_REMOVING;
Juliet Kim7d7195a2020-03-10 09:23:58 -05005387 spin_unlock_irqrestore(&adapter->state_lock, flags);
5388
Thomas Falcon6954a9e2020-06-12 13:34:41 -05005389 flush_work(&adapter->ibmvnic_reset);
5390 flush_delayed_work(&adapter->ibmvnic_delayed_reset);
5391
Juliet Kima5681e22018-11-19 15:59:22 -06005392 rtnl_lock();
5393 unregister_netdevice(netdev);
Nathan Fontenot37489052017-04-19 13:45:04 -04005394
5395 release_resources(adapter);
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06005396 release_sub_crqs(adapter, 1);
Nathan Fontenot37489052017-04-19 13:45:04 -04005397 release_crq_queue(adapter);
5398
Thomas Falcon53cc7722018-02-26 18:10:56 -06005399 release_stats_token(adapter);
5400 release_stats_buffers(adapter);
5401
Nathan Fontenot90c80142017-05-03 14:04:32 -04005402 adapter->state = VNIC_REMOVED;
5403
Juliet Kima5681e22018-11-19 15:59:22 -06005404 rtnl_unlock();
Thomas Falconff25dcb2019-11-25 17:12:56 -06005405 mutex_destroy(&adapter->fw_lock);
Thomas Falcon40c9db82017-06-12 12:35:04 -05005406 device_remove_file(&dev->dev, &dev_attr_failover);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005407 free_netdev(netdev);
5408 dev_set_drvdata(&dev->dev, NULL);
5409
5410 return 0;
5411}
5412
Thomas Falcon40c9db82017-06-12 12:35:04 -05005413static ssize_t failover_store(struct device *dev, struct device_attribute *attr,
5414 const char *buf, size_t count)
5415{
5416 struct net_device *netdev = dev_get_drvdata(dev);
5417 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
5418 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
5419 __be64 session_token;
5420 long rc;
5421
5422 if (!sysfs_streq(buf, "1"))
5423 return -EINVAL;
5424
5425 rc = plpar_hcall(H_VIOCTL, retbuf, adapter->vdev->unit_address,
5426 H_GET_SESSION_TOKEN, 0, 0, 0);
5427 if (rc) {
5428 netdev_err(netdev, "Couldn't retrieve session token, rc %ld\n",
5429 rc);
5430 return -EINVAL;
5431 }
5432
5433 session_token = (__be64)retbuf[0];
5434 netdev_dbg(netdev, "Initiating client failover, session id %llx\n",
5435 be64_to_cpu(session_token));
5436 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
5437 H_SESSION_ERR_DETECTED, session_token, 0, 0);
5438 if (rc) {
5439 netdev_err(netdev, "Client initiated failover failed, rc %ld\n",
5440 rc);
5441 return -EINVAL;
5442 }
5443
5444 return count;
5445}
5446
Joe Perches6cbaefb2017-12-19 10:15:09 -08005447static DEVICE_ATTR_WO(failover);
Thomas Falcon40c9db82017-06-12 12:35:04 -05005448
Thomas Falcon032c5e82015-12-21 11:26:06 -06005449static unsigned long ibmvnic_get_desired_dma(struct vio_dev *vdev)
5450{
5451 struct net_device *netdev = dev_get_drvdata(&vdev->dev);
5452 struct ibmvnic_adapter *adapter;
5453 struct iommu_table *tbl;
5454 unsigned long ret = 0;
5455 int i;
5456
5457 tbl = get_iommu_table_base(&vdev->dev);
5458
5459 /* netdev inits at probe time along with the structures we need below*/
5460 if (!netdev)
5461 return IOMMU_PAGE_ALIGN(IBMVNIC_IO_ENTITLEMENT_DEFAULT, tbl);
5462
5463 adapter = netdev_priv(netdev);
5464
5465 ret += PAGE_SIZE; /* the crq message queue */
Thomas Falcon032c5e82015-12-21 11:26:06 -06005466 ret += IOMMU_PAGE_ALIGN(sizeof(struct ibmvnic_statistics), tbl);
5467
5468 for (i = 0; i < adapter->req_tx_queues + adapter->req_rx_queues; i++)
5469 ret += 4 * PAGE_SIZE; /* the scrq message queue */
5470
Thomas Falcon507ebe62020-08-21 13:39:01 -05005471 for (i = 0; i < adapter->num_active_rx_pools; i++)
Thomas Falcon032c5e82015-12-21 11:26:06 -06005472 ret += adapter->rx_pool[i].size *
5473 IOMMU_PAGE_ALIGN(adapter->rx_pool[i].buff_size, tbl);
5474
5475 return ret;
5476}
5477
5478static int ibmvnic_resume(struct device *dev)
5479{
5480 struct net_device *netdev = dev_get_drvdata(dev);
5481 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005482
John Allencb89ba22017-06-19 11:27:53 -05005483 if (adapter->state != VNIC_OPEN)
5484 return 0;
5485
John Allena2488782017-07-24 13:26:06 -05005486 tasklet_schedule(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005487
5488 return 0;
5489}
5490
Arvind Yadav8c37bc62017-08-17 18:52:54 +05305491static const struct vio_device_id ibmvnic_device_table[] = {
Thomas Falcon032c5e82015-12-21 11:26:06 -06005492 {"network", "IBM,vnic"},
5493 {"", "" }
5494};
5495MODULE_DEVICE_TABLE(vio, ibmvnic_device_table);
5496
5497static const struct dev_pm_ops ibmvnic_pm_ops = {
5498 .resume = ibmvnic_resume
5499};
5500
5501static struct vio_driver ibmvnic_driver = {
5502 .id_table = ibmvnic_device_table,
5503 .probe = ibmvnic_probe,
5504 .remove = ibmvnic_remove,
5505 .get_desired_dma = ibmvnic_get_desired_dma,
5506 .name = ibmvnic_driver_name,
5507 .pm = &ibmvnic_pm_ops,
5508};
5509
5510/* module functions */
5511static int __init ibmvnic_module_init(void)
5512{
5513 pr_info("%s: %s %s\n", ibmvnic_driver_name, ibmvnic_driver_string,
5514 IBMVNIC_DRIVER_VERSION);
5515
5516 return vio_register_driver(&ibmvnic_driver);
5517}
5518
5519static void __exit ibmvnic_module_exit(void)
5520{
5521 vio_unregister_driver(&ibmvnic_driver);
5522}
5523
5524module_init(ibmvnic_module_init);
5525module_exit(ibmvnic_module_exit);