blob: 97041b319bebad53f8f60719b817b0a432ed404d [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;
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -060081static void release_sub_crqs(struct ibmvnic_adapter *, bool);
Thomas Falcon032c5e82015-12-21 11:26:06 -060082static int ibmvnic_reset_crq(struct ibmvnic_adapter *);
83static int ibmvnic_send_crq_init(struct ibmvnic_adapter *);
84static int ibmvnic_reenable_crq_queue(struct ibmvnic_adapter *);
85static int ibmvnic_send_crq(struct ibmvnic_adapter *, union ibmvnic_crq *);
Thomas Falconad7775d2016-04-01 17:20:34 -050086static int send_subcrq_indirect(struct ibmvnic_adapter *, u64, u64, u64);
Thomas Falcon032c5e82015-12-21 11:26:06 -060087static irqreturn_t ibmvnic_interrupt_rx(int irq, void *instance);
88static int enable_scrq_irq(struct ibmvnic_adapter *,
89 struct ibmvnic_sub_crq_queue *);
90static int disable_scrq_irq(struct ibmvnic_adapter *,
91 struct ibmvnic_sub_crq_queue *);
92static int pending_scrq(struct ibmvnic_adapter *,
93 struct ibmvnic_sub_crq_queue *);
94static union sub_crq *ibmvnic_next_scrq(struct ibmvnic_adapter *,
95 struct ibmvnic_sub_crq_queue *);
96static int ibmvnic_poll(struct napi_struct *napi, int data);
Lijun Pan69980d02020-09-27 20:13:28 -050097static void send_query_map(struct ibmvnic_adapter *adapter);
Lijun Pan673ead22021-06-14 00:20:45 -050098static int send_request_map(struct ibmvnic_adapter *, dma_addr_t, u32, u8);
Thomas Falcon9c4eaab2018-05-23 13:37:57 -050099static int send_request_unmap(struct ibmvnic_adapter *, u8);
Thomas Falcon20a8ab72018-02-26 18:10:59 -0600100static int send_login(struct ibmvnic_adapter *adapter);
Lijun Pan491099a2020-09-27 20:13:26 -0500101static void send_query_cap(struct ibmvnic_adapter *adapter);
Thomas Falcon4d96f122017-08-01 15:04:36 -0500102static int init_sub_crqs(struct ibmvnic_adapter *);
John Allenbd0b6722017-03-17 17:13:40 -0500103static int init_sub_crq_irqs(struct ibmvnic_adapter *adapter);
Lijun Pan635e4422020-08-19 17:52:26 -0500104static int ibmvnic_reset_init(struct ibmvnic_adapter *, bool reset);
Nathan Fontenotf9928872017-03-30 02:48:54 -0400105static void release_crq_queue(struct ibmvnic_adapter *);
Thomas Falcon62740e92019-05-09 23:13:43 -0500106static int __ibmvnic_set_mac(struct net_device *, u8 *);
Nathan Fontenot30f79622018-04-06 18:37:06 -0500107static int init_crq_queue(struct ibmvnic_adapter *adapter);
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -0300108static int send_query_phys_parms(struct ibmvnic_adapter *adapter);
Sukadev Bhattiprolu65d64702021-06-23 21:13:12 -0700109static void ibmvnic_tx_scrq_clean_buffer(struct ibmvnic_adapter *adapter,
110 struct ibmvnic_sub_crq_queue *tx_scrq);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600111
112struct ibmvnic_stat {
113 char name[ETH_GSTRING_LEN];
114 int offset;
115};
116
117#define IBMVNIC_STAT_OFF(stat) (offsetof(struct ibmvnic_adapter, stats) + \
118 offsetof(struct ibmvnic_statistics, stat))
Lijun Pan91dc5d22021-02-11 00:43:22 -0600119#define IBMVNIC_GET_STAT(a, off) (*((u64 *)(((unsigned long)(a)) + (off))))
Thomas Falcon032c5e82015-12-21 11:26:06 -0600120
121static const struct ibmvnic_stat ibmvnic_stats[] = {
122 {"rx_packets", IBMVNIC_STAT_OFF(rx_packets)},
123 {"rx_bytes", IBMVNIC_STAT_OFF(rx_bytes)},
124 {"tx_packets", IBMVNIC_STAT_OFF(tx_packets)},
125 {"tx_bytes", IBMVNIC_STAT_OFF(tx_bytes)},
126 {"ucast_tx_packets", IBMVNIC_STAT_OFF(ucast_tx_packets)},
127 {"ucast_rx_packets", IBMVNIC_STAT_OFF(ucast_rx_packets)},
128 {"mcast_tx_packets", IBMVNIC_STAT_OFF(mcast_tx_packets)},
129 {"mcast_rx_packets", IBMVNIC_STAT_OFF(mcast_rx_packets)},
130 {"bcast_tx_packets", IBMVNIC_STAT_OFF(bcast_tx_packets)},
131 {"bcast_rx_packets", IBMVNIC_STAT_OFF(bcast_rx_packets)},
132 {"align_errors", IBMVNIC_STAT_OFF(align_errors)},
133 {"fcs_errors", IBMVNIC_STAT_OFF(fcs_errors)},
134 {"single_collision_frames", IBMVNIC_STAT_OFF(single_collision_frames)},
135 {"multi_collision_frames", IBMVNIC_STAT_OFF(multi_collision_frames)},
136 {"sqe_test_errors", IBMVNIC_STAT_OFF(sqe_test_errors)},
137 {"deferred_tx", IBMVNIC_STAT_OFF(deferred_tx)},
138 {"late_collisions", IBMVNIC_STAT_OFF(late_collisions)},
139 {"excess_collisions", IBMVNIC_STAT_OFF(excess_collisions)},
140 {"internal_mac_tx_errors", IBMVNIC_STAT_OFF(internal_mac_tx_errors)},
141 {"carrier_sense", IBMVNIC_STAT_OFF(carrier_sense)},
142 {"too_long_frames", IBMVNIC_STAT_OFF(too_long_frames)},
143 {"internal_mac_rx_errors", IBMVNIC_STAT_OFF(internal_mac_rx_errors)},
144};
145
Cristobal Forno53f8b1b2021-06-10 11:08:35 -0600146static int send_crq_init_complete(struct ibmvnic_adapter *adapter)
147{
148 union ibmvnic_crq crq;
149
150 memset(&crq, 0, sizeof(crq));
151 crq.generic.first = IBMVNIC_CRQ_INIT_CMD;
152 crq.generic.cmd = IBMVNIC_CRQ_INIT_COMPLETE;
153
154 return ibmvnic_send_crq(adapter, &crq);
155}
156
157static int send_version_xchg(struct ibmvnic_adapter *adapter)
158{
159 union ibmvnic_crq crq;
160
161 memset(&crq, 0, sizeof(crq));
162 crq.version_exchange.first = IBMVNIC_CRQ_CMD;
163 crq.version_exchange.cmd = VERSION_EXCHANGE;
164 crq.version_exchange.version = cpu_to_be16(ibmvnic_version);
165
166 return ibmvnic_send_crq(adapter, &crq);
167}
168
Thomas Falcon032c5e82015-12-21 11:26:06 -0600169static long h_reg_sub_crq(unsigned long unit_address, unsigned long token,
170 unsigned long length, unsigned long *number,
171 unsigned long *irq)
172{
173 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
174 long rc;
175
176 rc = plpar_hcall(H_REG_SUB_CRQ, retbuf, unit_address, token, length);
177 *number = retbuf[0];
178 *irq = retbuf[1];
179
180 return rc;
181}
182
Thomas Falcon476d96c2019-11-25 17:12:55 -0600183/**
184 * ibmvnic_wait_for_completion - Check device state and wait for completion
185 * @adapter: private device data
186 * @comp_done: completion structure to wait for
187 * @timeout: time to wait in milliseconds
188 *
189 * Wait for a completion signal or until the timeout limit is reached
190 * while checking that the device is still active.
191 */
192static int ibmvnic_wait_for_completion(struct ibmvnic_adapter *adapter,
193 struct completion *comp_done,
194 unsigned long timeout)
195{
196 struct net_device *netdev;
197 unsigned long div_timeout;
198 u8 retry;
199
200 netdev = adapter->netdev;
201 retry = 5;
202 div_timeout = msecs_to_jiffies(timeout / retry);
203 while (true) {
204 if (!adapter->crq.active) {
205 netdev_err(netdev, "Device down!\n");
206 return -ENODEV;
207 }
Thomas Falcon8f9cc1e2019-12-11 09:38:39 -0600208 if (!retry--)
Thomas Falcon476d96c2019-11-25 17:12:55 -0600209 break;
210 if (wait_for_completion_timeout(comp_done, div_timeout))
211 return 0;
212 }
213 netdev_err(netdev, "Operation timed out.\n");
214 return -ETIMEDOUT;
215}
216
Thomas Falcon032c5e82015-12-21 11:26:06 -0600217static int alloc_long_term_buff(struct ibmvnic_adapter *adapter,
218 struct ibmvnic_long_term_buff *ltb, int size)
219{
220 struct device *dev = &adapter->vdev->dev;
Thomas Falcon9c4eaab2018-05-23 13:37:57 -0500221 int rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600222
223 ltb->size = size;
224 ltb->buff = dma_alloc_coherent(dev, ltb->size, &ltb->addr,
225 GFP_KERNEL);
226
227 if (!ltb->buff) {
228 dev_err(dev, "Couldn't alloc long term buffer\n");
229 return -ENOMEM;
230 }
231 ltb->map_id = adapter->map_id;
232 adapter->map_id++;
Nathan Fontenotdb5d0b52017-02-10 13:45:05 -0500233
Thomas Falconff25dcb2019-11-25 17:12:56 -0600234 mutex_lock(&adapter->fw_lock);
235 adapter->fw_done_rc = 0;
Thomas Falcon070eca92019-11-25 17:12:53 -0600236 reinit_completion(&adapter->fw_done);
Sukadev Bhattiprolu552a3372021-06-23 21:13:14 -0700237
238 rc = send_request_map(adapter, ltb->addr, ltb->size, ltb->map_id);
Thomas Falcon9c4eaab2018-05-23 13:37:57 -0500239 if (rc) {
Sukadev Bhattiprolu552a3372021-06-23 21:13:14 -0700240 dev_err(dev, "send_request_map failed, rc = %d\n", rc);
241 goto out;
Thomas Falcon9c4eaab2018-05-23 13:37:57 -0500242 }
Thomas Falcon476d96c2019-11-25 17:12:55 -0600243
244 rc = ibmvnic_wait_for_completion(adapter, &adapter->fw_done, 10000);
245 if (rc) {
Sukadev Bhattiprolu0f2bf312021-09-14 20:52:52 -0700246 dev_err(dev, "LTB map request aborted or timed out, rc = %d\n",
Thomas Falcon476d96c2019-11-25 17:12:55 -0600247 rc);
Sukadev Bhattiprolu552a3372021-06-23 21:13:14 -0700248 goto out;
Thomas Falcon476d96c2019-11-25 17:12:55 -0600249 }
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500250
251 if (adapter->fw_done_rc) {
Sukadev Bhattiprolu0f2bf312021-09-14 20:52:52 -0700252 dev_err(dev, "Couldn't map LTB, rc = %d\n",
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500253 adapter->fw_done_rc);
Sukadev Bhattiprolu552a3372021-06-23 21:13:14 -0700254 rc = -1;
255 goto out;
256 }
257 rc = 0;
258out:
259 if (rc) {
Thomas Falcon4cf2ddf32018-05-16 15:49:03 -0500260 dma_free_coherent(dev, ltb->size, ltb->buff, ltb->addr);
Sukadev Bhattiprolu552a3372021-06-23 21:13:14 -0700261 ltb->buff = NULL;
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500262 }
Thomas Falconff25dcb2019-11-25 17:12:56 -0600263 mutex_unlock(&adapter->fw_lock);
Sukadev Bhattiprolu552a3372021-06-23 21:13:14 -0700264 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600265}
266
267static void free_long_term_buff(struct ibmvnic_adapter *adapter,
268 struct ibmvnic_long_term_buff *ltb)
269{
270 struct device *dev = &adapter->vdev->dev;
271
Nathan Fontenotc657e322017-03-30 02:49:06 -0400272 if (!ltb->buff)
273 return;
274
Lijun Pan7d3a7b92021-02-12 20:49:00 -0600275 /* VIOS automatically unmaps the long term buffer at remote
276 * end for the following resets:
277 * FAILOVER, MOBILITY, TIMEOUT.
278 */
Nathan Fontenoted651a12017-05-03 14:04:38 -0400279 if (adapter->reset_reason != VNIC_RESET_FAILOVER &&
Lijun Pan7d3a7b92021-02-12 20:49:00 -0600280 adapter->reset_reason != VNIC_RESET_MOBILITY &&
281 adapter->reset_reason != VNIC_RESET_TIMEOUT)
Thomas Falcondfad09a2016-08-18 11:37:51 -0500282 send_request_unmap(adapter, ltb->map_id);
Sukadev Bhattiprolu0f2bf312021-09-14 20:52:52 -0700283
Brian King59af56c2017-04-19 13:44:41 -0400284 dma_free_coherent(dev, ltb->size, ltb->buff, ltb->addr);
Sukadev Bhattiprolu0f2bf312021-09-14 20:52:52 -0700285
Sukadev Bhattiprolu552a3372021-06-23 21:13:14 -0700286 ltb->buff = NULL;
287 ltb->map_id = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600288}
289
Sukadev Bhattiprolu0ec13af2021-06-23 21:13:10 -0700290static int reset_long_term_buff(struct ibmvnic_adapter *adapter,
291 struct ibmvnic_long_term_buff *ltb)
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500292{
Sukadev Bhattiprolu0ec13af2021-06-23 21:13:10 -0700293 struct device *dev = &adapter->vdev->dev;
294 int rc;
Thomas Falcon9c4eaab2018-05-23 13:37:57 -0500295
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500296 memset(ltb->buff, 0, ltb->size);
Sukadev Bhattiprolu0ec13af2021-06-23 21:13:10 -0700297
298 mutex_lock(&adapter->fw_lock);
299 adapter->fw_done_rc = 0;
300
301 reinit_completion(&adapter->fw_done);
302 rc = send_request_map(adapter, ltb->addr, ltb->size, ltb->map_id);
303 if (rc) {
304 mutex_unlock(&adapter->fw_lock);
305 return rc;
306 }
307
308 rc = ibmvnic_wait_for_completion(adapter, &adapter->fw_done, 10000);
309 if (rc) {
310 dev_info(dev,
311 "Reset failed, long term map request timed out or aborted\n");
312 mutex_unlock(&adapter->fw_lock);
313 return rc;
314 }
315
316 if (adapter->fw_done_rc) {
317 dev_info(dev,
318 "Reset failed, attempting to free and reallocate buffer\n");
319 free_long_term_buff(adapter, ltb);
320 mutex_unlock(&adapter->fw_lock);
321 return alloc_long_term_buff(adapter, ltb, ltb->size);
322 }
323 mutex_unlock(&adapter->fw_lock);
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500324 return 0;
325}
326
Thomas Falconf185a492017-05-26 10:30:48 -0400327static void deactivate_rx_pools(struct ibmvnic_adapter *adapter)
328{
329 int i;
330
Thomas Falcon507ebe62020-08-21 13:39:01 -0500331 for (i = 0; i < adapter->num_active_rx_pools; i++)
Thomas Falconf185a492017-05-26 10:30:48 -0400332 adapter->rx_pool[i].active = 0;
333}
334
Thomas Falcon032c5e82015-12-21 11:26:06 -0600335static void replenish_rx_pool(struct ibmvnic_adapter *adapter,
336 struct ibmvnic_rx_pool *pool)
337{
338 int count = pool->size - atomic_read(&pool->available);
Cristobal Fornof3ae59c2020-08-19 13:16:23 -0500339 u64 handle = adapter->rx_scrq[pool->index]->handle;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600340 struct device *dev = &adapter->vdev->dev;
Thomas Falcon4f0b6812020-11-18 19:12:18 -0600341 struct ibmvnic_ind_xmit_queue *ind_bufp;
342 struct ibmvnic_sub_crq_queue *rx_scrq;
343 union sub_crq *sub_crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600344 int buffers_added = 0;
345 unsigned long lpar_rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600346 struct sk_buff *skb;
347 unsigned int offset;
348 dma_addr_t dma_addr;
349 unsigned char *dst;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600350 int shift = 0;
351 int index;
352 int i;
353
Thomas Falconf185a492017-05-26 10:30:48 -0400354 if (!pool->active)
355 return;
356
Thomas Falcon4f0b6812020-11-18 19:12:18 -0600357 rx_scrq = adapter->rx_scrq[pool->index];
358 ind_bufp = &rx_scrq->ind_buf;
Sukadev Bhattiprolu72368f82021-06-23 21:13:13 -0700359
360 /* netdev_skb_alloc() could have failed after we saved a few skbs
361 * in the indir_buf and we would not have sent them to VIOS yet.
362 * To account for them, start the loop at ind_bufp->index rather
363 * than 0. If we pushed all the skbs to VIOS, ind_bufp->index will
364 * be 0.
365 */
366 for (i = ind_bufp->index; i < count; ++i) {
Dwip N. Banerjeee552aa32020-11-18 19:12:24 -0600367 skb = netdev_alloc_skb(adapter->netdev, pool->buff_size);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600368 if (!skb) {
369 dev_err(dev, "Couldn't replenish rx buff\n");
370 adapter->replenish_no_mem++;
371 break;
372 }
373
374 index = pool->free_map[pool->next_free];
Sukadev Bhattiprolu38106b22021-09-14 20:52:51 -0700375 pool->free_map[pool->next_free] = IBMVNIC_INVALID_MAP;
376 pool->next_free = (pool->next_free + 1) % pool->size;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600377
378 if (pool->rx_buff[index].skb)
379 dev_err(dev, "Inconsistent free_map!\n");
380
381 /* Copy the skb to the long term mapped DMA buffer */
382 offset = index * pool->buff_size;
383 dst = pool->long_term_buff.buff + offset;
384 memset(dst, 0, pool->buff_size);
385 dma_addr = pool->long_term_buff.addr + offset;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600386
Sukadev Bhattiprolu38106b22021-09-14 20:52:51 -0700387 /* add the skb to an rx_buff in the pool */
388 pool->rx_buff[index].data = dst;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600389 pool->rx_buff[index].dma = dma_addr;
390 pool->rx_buff[index].skb = skb;
391 pool->rx_buff[index].pool_index = pool->index;
392 pool->rx_buff[index].size = pool->buff_size;
393
Sukadev Bhattiprolu38106b22021-09-14 20:52:51 -0700394 /* queue the rx_buff for the next send_subcrq_indirect */
Thomas Falcon4f0b6812020-11-18 19:12:18 -0600395 sub_crq = &ind_bufp->indir_arr[ind_bufp->index++];
396 memset(sub_crq, 0, sizeof(*sub_crq));
397 sub_crq->rx_add.first = IBMVNIC_CRQ_CMD;
398 sub_crq->rx_add.correlator =
Thomas Falcon032c5e82015-12-21 11:26:06 -0600399 cpu_to_be64((u64)&pool->rx_buff[index]);
Thomas Falcon4f0b6812020-11-18 19:12:18 -0600400 sub_crq->rx_add.ioba = cpu_to_be32(dma_addr);
401 sub_crq->rx_add.map_id = pool->long_term_buff.map_id;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600402
403 /* The length field of the sCRQ is defined to be 24 bits so the
404 * buffer size needs to be left shifted by a byte before it is
405 * converted to big endian to prevent the last byte from being
406 * truncated.
407 */
408#ifdef __LITTLE_ENDIAN__
409 shift = 8;
410#endif
Thomas Falcon4f0b6812020-11-18 19:12:18 -0600411 sub_crq->rx_add.len = cpu_to_be32(pool->buff_size << shift);
Sukadev Bhattiprolu38106b22021-09-14 20:52:51 -0700412
413 /* if send_subcrq_indirect queue is full, flush to VIOS */
Thomas Falcon4f0b6812020-11-18 19:12:18 -0600414 if (ind_bufp->index == IBMVNIC_MAX_IND_DESCS ||
415 i == count - 1) {
416 lpar_rc =
417 send_subcrq_indirect(adapter, handle,
418 (u64)ind_bufp->indir_dma,
419 (u64)ind_bufp->index);
420 if (lpar_rc != H_SUCCESS)
421 goto failure;
422 buffers_added += ind_bufp->index;
423 adapter->replenish_add_buff_success += ind_bufp->index;
424 ind_bufp->index = 0;
425 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600426 }
427 atomic_add(buffers_added, &pool->available);
428 return;
429
430failure:
Thomas Falcon2d14d372018-07-13 12:03:32 -0500431 if (lpar_rc != H_PARAMETER && lpar_rc != H_CLOSED)
432 dev_err_ratelimited(dev, "rx: replenish packet buffer failed\n");
Thomas Falcon4f0b6812020-11-18 19:12:18 -0600433 for (i = ind_bufp->index - 1; i >= 0; --i) {
434 struct ibmvnic_rx_buff *rx_buff;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600435
Thomas Falcon4f0b6812020-11-18 19:12:18 -0600436 pool->next_free = pool->next_free == 0 ?
437 pool->size - 1 : pool->next_free - 1;
438 sub_crq = &ind_bufp->indir_arr[i];
439 rx_buff = (struct ibmvnic_rx_buff *)
440 be64_to_cpu(sub_crq->rx_add.correlator);
441 index = (int)(rx_buff - pool->rx_buff);
442 pool->free_map[pool->next_free] = index;
443 dev_kfree_skb_any(pool->rx_buff[index].skb);
444 pool->rx_buff[index].skb = NULL;
445 }
Dwip N. Banerjeec2af6222020-12-09 20:53:31 -0500446 adapter->replenish_add_buff_failure += ind_bufp->index;
447 atomic_add(buffers_added, &pool->available);
Thomas Falcon4f0b6812020-11-18 19:12:18 -0600448 ind_bufp->index = 0;
Thomas Falcon5a18e1e2018-04-06 18:37:05 -0500449 if (lpar_rc == H_CLOSED || adapter->failover_pending) {
Thomas Falconf185a492017-05-26 10:30:48 -0400450 /* Disable buffer pool replenishment and report carrier off if
Thomas Falcon5a18e1e2018-04-06 18:37:05 -0500451 * queue is closed or pending failover.
452 * Firmware guarantees that a signal will be sent to the
453 * driver, triggering a reset.
Thomas Falconf185a492017-05-26 10:30:48 -0400454 */
455 deactivate_rx_pools(adapter);
456 netif_carrier_off(adapter->netdev);
457 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600458}
459
460static void replenish_pools(struct ibmvnic_adapter *adapter)
461{
462 int i;
463
Thomas Falcon032c5e82015-12-21 11:26:06 -0600464 adapter->replenish_task_cycles++;
Thomas Falcon507ebe62020-08-21 13:39:01 -0500465 for (i = 0; i < adapter->num_active_rx_pools; i++) {
Thomas Falcon032c5e82015-12-21 11:26:06 -0600466 if (adapter->rx_pool[i].active)
467 replenish_rx_pool(adapter, &adapter->rx_pool[i]);
468 }
Sukadev Bhattiprolu38bd5ce2020-12-04 18:22:35 -0800469
470 netdev_dbg(adapter->netdev, "Replenished %d pools\n", i);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600471}
472
John Allen3d52b592017-08-02 16:44:14 -0500473static void release_stats_buffers(struct ibmvnic_adapter *adapter)
474{
475 kfree(adapter->tx_stats_buffers);
476 kfree(adapter->rx_stats_buffers);
Thomas Falconb0992ec2018-02-06 17:25:23 -0600477 adapter->tx_stats_buffers = NULL;
478 adapter->rx_stats_buffers = NULL;
John Allen3d52b592017-08-02 16:44:14 -0500479}
480
481static int init_stats_buffers(struct ibmvnic_adapter *adapter)
482{
483 adapter->tx_stats_buffers =
Nathan Fontenotabcae542018-02-19 13:30:47 -0600484 kcalloc(IBMVNIC_MAX_QUEUES,
John Allen3d52b592017-08-02 16:44:14 -0500485 sizeof(struct ibmvnic_tx_queue_stats),
486 GFP_KERNEL);
487 if (!adapter->tx_stats_buffers)
488 return -ENOMEM;
489
490 adapter->rx_stats_buffers =
Nathan Fontenotabcae542018-02-19 13:30:47 -0600491 kcalloc(IBMVNIC_MAX_QUEUES,
John Allen3d52b592017-08-02 16:44:14 -0500492 sizeof(struct ibmvnic_rx_queue_stats),
493 GFP_KERNEL);
494 if (!adapter->rx_stats_buffers)
495 return -ENOMEM;
496
497 return 0;
498}
499
Nathan Fontenot7bbc27a2017-03-30 02:49:23 -0400500static void release_stats_token(struct ibmvnic_adapter *adapter)
501{
502 struct device *dev = &adapter->vdev->dev;
503
504 if (!adapter->stats_token)
505 return;
506
507 dma_unmap_single(dev, adapter->stats_token,
508 sizeof(struct ibmvnic_statistics),
509 DMA_FROM_DEVICE);
510 adapter->stats_token = 0;
511}
512
513static int init_stats_token(struct ibmvnic_adapter *adapter)
514{
515 struct device *dev = &adapter->vdev->dev;
516 dma_addr_t stok;
517
518 stok = dma_map_single(dev, &adapter->stats,
519 sizeof(struct ibmvnic_statistics),
520 DMA_FROM_DEVICE);
521 if (dma_mapping_error(dev, stok)) {
522 dev_err(dev, "Couldn't map stats buffer\n");
523 return -1;
524 }
525
526 adapter->stats_token = stok;
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500527 netdev_dbg(adapter->netdev, "Stats token initialized (%llx)\n", stok);
Nathan Fontenot7bbc27a2017-03-30 02:49:23 -0400528 return 0;
529}
530
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400531static int reset_rx_pools(struct ibmvnic_adapter *adapter)
532{
533 struct ibmvnic_rx_pool *rx_pool;
Thomas Falcon507ebe62020-08-21 13:39:01 -0500534 u64 buff_size;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400535 int rx_scrqs;
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500536 int i, j, rc;
John Allen896d8692018-01-18 16:26:31 -0600537
Mingming Cao9f134572020-08-25 13:26:41 -0400538 if (!adapter->rx_pool)
539 return -1;
540
Thomas Falcon507ebe62020-08-21 13:39:01 -0500541 buff_size = adapter->cur_rx_buf_sz;
542 rx_scrqs = adapter->num_active_rx_pools;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400543 for (i = 0; i < rx_scrqs; i++) {
544 rx_pool = &adapter->rx_pool[i];
545
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500546 netdev_dbg(adapter->netdev, "Re-setting rx_pool[%d]\n", i);
547
Thomas Falcon507ebe62020-08-21 13:39:01 -0500548 if (rx_pool->buff_size != buff_size) {
John Allen896d8692018-01-18 16:26:31 -0600549 free_long_term_buff(adapter, &rx_pool->long_term_buff);
Dwip N. Banerjee9a87c3f2020-11-18 19:12:22 -0600550 rx_pool->buff_size = ALIGN(buff_size, L1_CACHE_BYTES);
Thomas Falcon7c940b12019-06-07 16:03:55 -0500551 rc = alloc_long_term_buff(adapter,
552 &rx_pool->long_term_buff,
553 rx_pool->size *
554 rx_pool->buff_size);
John Allen896d8692018-01-18 16:26:31 -0600555 } else {
Sukadev Bhattiprolu0ec13af2021-06-23 21:13:10 -0700556 rc = reset_long_term_buff(adapter,
557 &rx_pool->long_term_buff);
John Allen896d8692018-01-18 16:26:31 -0600558 }
559
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500560 if (rc)
561 return rc;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400562
563 for (j = 0; j < rx_pool->size; j++)
564 rx_pool->free_map[j] = j;
565
566 memset(rx_pool->rx_buff, 0,
567 rx_pool->size * sizeof(struct ibmvnic_rx_buff));
568
569 atomic_set(&rx_pool->available, 0);
570 rx_pool->next_alloc = 0;
571 rx_pool->next_free = 0;
Thomas Falconc3e53b92017-06-14 23:50:05 -0500572 rx_pool->active = 1;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400573 }
574
575 return 0;
576}
577
Sukadev Bhattiprolu0f2bf312021-09-14 20:52:52 -0700578/**
579 * release_rx_pools() - Release any rx pools attached to @adapter.
580 * @adapter: ibmvnic adapter
581 *
582 * Safe to call this multiple times - even if no pools are attached.
583 */
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400584static void release_rx_pools(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -0600585{
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400586 struct ibmvnic_rx_pool *rx_pool;
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400587 int i, j;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600588
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400589 if (!adapter->rx_pool)
Thomas Falcon032c5e82015-12-21 11:26:06 -0600590 return;
591
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600592 for (i = 0; i < adapter->num_active_rx_pools; i++) {
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400593 rx_pool = &adapter->rx_pool[i];
594
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500595 netdev_dbg(adapter->netdev, "Releasing rx_pool[%d]\n", i);
596
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400597 kfree(rx_pool->free_map);
598 free_long_term_buff(adapter, &rx_pool->long_term_buff);
599
600 if (!rx_pool->rx_buff)
Nathan Fontenote0ebe9422017-05-03 14:04:50 -0400601 continue;
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400602
603 for (j = 0; j < rx_pool->size; j++) {
604 if (rx_pool->rx_buff[j].skb) {
Thomas Falconb7cdec32018-11-21 11:17:58 -0600605 dev_kfree_skb_any(rx_pool->rx_buff[j].skb);
606 rx_pool->rx_buff[j].skb = NULL;
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400607 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600608 }
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400609
610 kfree(rx_pool->rx_buff);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600611 }
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400612
613 kfree(adapter->rx_pool);
614 adapter->rx_pool = NULL;
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600615 adapter->num_active_rx_pools = 0;
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400616}
617
618static int init_rx_pools(struct net_device *netdev)
619{
620 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
621 struct device *dev = &adapter->vdev->dev;
622 struct ibmvnic_rx_pool *rx_pool;
Sukadev Bhattiprolu0df7b9a2021-09-14 20:52:53 -0700623 u64 num_pools;
624 u64 pool_size; /* # of buffers in one pool */
Thomas Falcon507ebe62020-08-21 13:39:01 -0500625 u64 buff_size;
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400626 int i, j;
627
Sukadev Bhattiprolu0df7b9a2021-09-14 20:52:53 -0700628 num_pools = adapter->num_active_rx_scrqs;
629 pool_size = adapter->req_rx_add_entries_per_subcrq;
Thomas Falcon507ebe62020-08-21 13:39:01 -0500630 buff_size = adapter->cur_rx_buf_sz;
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400631
Sukadev Bhattiprolu0df7b9a2021-09-14 20:52:53 -0700632 adapter->rx_pool = kcalloc(num_pools,
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400633 sizeof(struct ibmvnic_rx_pool),
634 GFP_KERNEL);
635 if (!adapter->rx_pool) {
636 dev_err(dev, "Failed to allocate rx pools\n");
637 return -1;
638 }
639
Sukadev Bhattiprolu0f2bf312021-09-14 20:52:52 -0700640 /* Set num_active_rx_pools early. If we fail below after partial
641 * allocation, release_rx_pools() will know how many to look for.
642 */
Sukadev Bhattiprolu0df7b9a2021-09-14 20:52:53 -0700643 adapter->num_active_rx_pools = num_pools;
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600644
Sukadev Bhattiprolu0df7b9a2021-09-14 20:52:53 -0700645 for (i = 0; i < num_pools; i++) {
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400646 rx_pool = &adapter->rx_pool[i];
647
648 netdev_dbg(adapter->netdev,
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500649 "Initializing rx_pool[%d], %lld buffs, %lld bytes each\n",
Sukadev Bhattiprolu0df7b9a2021-09-14 20:52:53 -0700650 i, pool_size, buff_size);
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400651
Sukadev Bhattiprolu0df7b9a2021-09-14 20:52:53 -0700652 rx_pool->size = pool_size;
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400653 rx_pool->index = i;
Dwip N. Banerjee9a87c3f2020-11-18 19:12:22 -0600654 rx_pool->buff_size = ALIGN(buff_size, L1_CACHE_BYTES);
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400655 rx_pool->active = 1;
656
657 rx_pool->free_map = kcalloc(rx_pool->size, sizeof(int),
658 GFP_KERNEL);
659 if (!rx_pool->free_map) {
Sukadev Bhattiprolu0f2bf312021-09-14 20:52:52 -0700660 dev_err(dev, "Couldn't alloc free_map %d\n", i);
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400661 release_rx_pools(adapter);
662 return -1;
663 }
664
665 rx_pool->rx_buff = kcalloc(rx_pool->size,
666 sizeof(struct ibmvnic_rx_buff),
667 GFP_KERNEL);
668 if (!rx_pool->rx_buff) {
669 dev_err(dev, "Couldn't alloc rx buffers\n");
670 release_rx_pools(adapter);
671 return -1;
672 }
673
674 if (alloc_long_term_buff(adapter, &rx_pool->long_term_buff,
675 rx_pool->size * rx_pool->buff_size)) {
676 release_rx_pools(adapter);
677 return -1;
678 }
679
680 for (j = 0; j < rx_pool->size; ++j)
681 rx_pool->free_map[j] = j;
682
683 atomic_set(&rx_pool->available, 0);
684 rx_pool->next_alloc = 0;
685 rx_pool->next_free = 0;
686 }
687
688 return 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600689}
690
Sukadev Bhattiprolu0ec13af2021-06-23 21:13:10 -0700691static int reset_one_tx_pool(struct ibmvnic_adapter *adapter,
692 struct ibmvnic_tx_pool *tx_pool)
Thomas Falcone26dc252018-03-16 20:00:25 -0500693{
694 int rc, i;
695
Sukadev Bhattiprolu0ec13af2021-06-23 21:13:10 -0700696 rc = reset_long_term_buff(adapter, &tx_pool->long_term_buff);
Thomas Falcone26dc252018-03-16 20:00:25 -0500697 if (rc)
698 return rc;
699
700 memset(tx_pool->tx_buff, 0,
701 tx_pool->num_buffers *
702 sizeof(struct ibmvnic_tx_buff));
703
704 for (i = 0; i < tx_pool->num_buffers; i++)
705 tx_pool->free_map[i] = i;
706
707 tx_pool->consumer_index = 0;
708 tx_pool->producer_index = 0;
709
710 return 0;
711}
712
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400713static int reset_tx_pools(struct ibmvnic_adapter *adapter)
714{
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400715 int tx_scrqs;
Thomas Falcone26dc252018-03-16 20:00:25 -0500716 int i, rc;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400717
Mingming Cao9f134572020-08-25 13:26:41 -0400718 if (!adapter->tx_pool)
719 return -1;
720
Thomas Falcon507ebe62020-08-21 13:39:01 -0500721 tx_scrqs = adapter->num_active_tx_pools;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400722 for (i = 0; i < tx_scrqs; i++) {
Sukadev Bhattiprolu65d64702021-06-23 21:13:12 -0700723 ibmvnic_tx_scrq_clean_buffer(adapter, adapter->tx_scrq[i]);
Sukadev Bhattiprolu0ec13af2021-06-23 21:13:10 -0700724 rc = reset_one_tx_pool(adapter, &adapter->tso_pool[i]);
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500725 if (rc)
726 return rc;
Sukadev Bhattiprolu0ec13af2021-06-23 21:13:10 -0700727 rc = reset_one_tx_pool(adapter, &adapter->tx_pool[i]);
Thomas Falconfdb06102017-10-17 12:36:55 -0500728 if (rc)
729 return rc;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400730 }
731
732 return 0;
733}
734
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -0200735static void release_vpd_data(struct ibmvnic_adapter *adapter)
736{
737 if (!adapter->vpd)
738 return;
739
740 kfree(adapter->vpd->buff);
741 kfree(adapter->vpd);
Thomas Falconb0992ec2018-02-06 17:25:23 -0600742
743 adapter->vpd = NULL;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -0200744}
745
Thomas Falconfb794212018-03-16 20:00:26 -0500746static void release_one_tx_pool(struct ibmvnic_adapter *adapter,
747 struct ibmvnic_tx_pool *tx_pool)
748{
749 kfree(tx_pool->tx_buff);
750 kfree(tx_pool->free_map);
751 free_long_term_buff(adapter, &tx_pool->long_term_buff);
752}
753
Sukadev Bhattiprolu0f2bf312021-09-14 20:52:52 -0700754/**
755 * release_tx_pools() - Release any tx pools attached to @adapter.
756 * @adapter: ibmvnic adapter
757 *
758 * Safe to call this multiple times - even if no pools are attached.
759 */
Nathan Fontenotc657e322017-03-30 02:49:06 -0400760static void release_tx_pools(struct ibmvnic_adapter *adapter)
761{
John Allen896d8692018-01-18 16:26:31 -0600762 int i;
Nathan Fontenotc657e322017-03-30 02:49:06 -0400763
Sukadev Bhattiprolu0f2bf312021-09-14 20:52:52 -0700764 /* init_tx_pools() ensures that ->tx_pool and ->tso_pool are
765 * both NULL or both non-NULL. So we only need to check one.
766 */
Nathan Fontenotc657e322017-03-30 02:49:06 -0400767 if (!adapter->tx_pool)
768 return;
769
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600770 for (i = 0; i < adapter->num_active_tx_pools; i++) {
Thomas Falconfb794212018-03-16 20:00:26 -0500771 release_one_tx_pool(adapter, &adapter->tx_pool[i]);
772 release_one_tx_pool(adapter, &adapter->tso_pool[i]);
Nathan Fontenotc657e322017-03-30 02:49:06 -0400773 }
774
775 kfree(adapter->tx_pool);
776 adapter->tx_pool = NULL;
Thomas Falconfb794212018-03-16 20:00:26 -0500777 kfree(adapter->tso_pool);
778 adapter->tso_pool = NULL;
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600779 adapter->num_active_tx_pools = 0;
Nathan Fontenotc657e322017-03-30 02:49:06 -0400780}
781
Thomas Falcon32053062018-03-16 20:00:27 -0500782static int init_one_tx_pool(struct net_device *netdev,
783 struct ibmvnic_tx_pool *tx_pool,
Sukadev Bhattiprolu8243c7e2021-09-14 20:52:54 -0700784 int pool_size, int buf_size)
Thomas Falcon32053062018-03-16 20:00:27 -0500785{
786 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
787 int i;
788
Sukadev Bhattiprolu8243c7e2021-09-14 20:52:54 -0700789 tx_pool->tx_buff = kcalloc(pool_size,
Thomas Falcon32053062018-03-16 20:00:27 -0500790 sizeof(struct ibmvnic_tx_buff),
791 GFP_KERNEL);
792 if (!tx_pool->tx_buff)
793 return -1;
794
795 if (alloc_long_term_buff(adapter, &tx_pool->long_term_buff,
Sukadev Bhattiprolu8243c7e2021-09-14 20:52:54 -0700796 pool_size * buf_size))
Thomas Falcon32053062018-03-16 20:00:27 -0500797 return -1;
798
Sukadev Bhattiprolu8243c7e2021-09-14 20:52:54 -0700799 tx_pool->free_map = kcalloc(pool_size, sizeof(int), GFP_KERNEL);
Thomas Falcon32053062018-03-16 20:00:27 -0500800 if (!tx_pool->free_map)
801 return -1;
802
Sukadev Bhattiprolu8243c7e2021-09-14 20:52:54 -0700803 for (i = 0; i < pool_size; i++)
Thomas Falcon32053062018-03-16 20:00:27 -0500804 tx_pool->free_map[i] = i;
805
806 tx_pool->consumer_index = 0;
807 tx_pool->producer_index = 0;
Sukadev Bhattiprolu8243c7e2021-09-14 20:52:54 -0700808 tx_pool->num_buffers = pool_size;
Thomas Falcon32053062018-03-16 20:00:27 -0500809 tx_pool->buf_size = buf_size;
810
811 return 0;
812}
813
Nathan Fontenotc657e322017-03-30 02:49:06 -0400814static int init_tx_pools(struct net_device *netdev)
815{
816 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Sukadev Bhattiprolu0f2bf312021-09-14 20:52:52 -0700817 struct device *dev = &adapter->vdev->dev;
Sukadev Bhattiprolu8243c7e2021-09-14 20:52:54 -0700818 int num_pools;
819 u64 pool_size; /* # of buffers in pool */
Dwip N. Banerjee9a87c3f2020-11-18 19:12:22 -0600820 u64 buff_size;
Thomas Falcon32053062018-03-16 20:00:27 -0500821 int i, rc;
Nathan Fontenotc657e322017-03-30 02:49:06 -0400822
Sukadev Bhattiprolu8243c7e2021-09-14 20:52:54 -0700823 pool_size = adapter->req_tx_entries_per_subcrq;
824 num_pools = adapter->num_active_tx_scrqs;
825
826 adapter->tx_pool = kcalloc(num_pools,
Nathan Fontenotc657e322017-03-30 02:49:06 -0400827 sizeof(struct ibmvnic_tx_pool), GFP_KERNEL);
828 if (!adapter->tx_pool)
829 return -1;
830
Sukadev Bhattiprolu8243c7e2021-09-14 20:52:54 -0700831 adapter->tso_pool = kcalloc(num_pools,
Thomas Falcon32053062018-03-16 20:00:27 -0500832 sizeof(struct ibmvnic_tx_pool), GFP_KERNEL);
Sukadev Bhattiprolu0f2bf312021-09-14 20:52:52 -0700833 /* To simplify release_tx_pools() ensure that ->tx_pool and
834 * ->tso_pool are either both NULL or both non-NULL.
835 */
Sukadev Bhattiproluf6ebca82021-06-23 21:13:15 -0700836 if (!adapter->tso_pool) {
837 kfree(adapter->tx_pool);
838 adapter->tx_pool = NULL;
Thomas Falcon32053062018-03-16 20:00:27 -0500839 return -1;
Sukadev Bhattiproluf6ebca82021-06-23 21:13:15 -0700840 }
Thomas Falcon32053062018-03-16 20:00:27 -0500841
Sukadev Bhattiprolu0f2bf312021-09-14 20:52:52 -0700842 /* Set num_active_tx_pools early. If we fail below after partial
843 * allocation, release_tx_pools() will know how many to look for.
844 */
Sukadev Bhattiprolu8243c7e2021-09-14 20:52:54 -0700845 adapter->num_active_tx_pools = num_pools;
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600846
Sukadev Bhattiprolu8243c7e2021-09-14 20:52:54 -0700847 for (i = 0; i < num_pools; i++) {
Dwip N. Banerjee9a87c3f2020-11-18 19:12:22 -0600848 buff_size = adapter->req_mtu + VLAN_HLEN;
849 buff_size = ALIGN(buff_size, L1_CACHE_BYTES);
Sukadev Bhattiprolu0f2bf312021-09-14 20:52:52 -0700850
851 dev_dbg(dev, "Init tx pool %d [%llu, %llu]\n",
852 i, adapter->req_tx_entries_per_subcrq, buff_size);
853
Thomas Falcon32053062018-03-16 20:00:27 -0500854 rc = init_one_tx_pool(netdev, &adapter->tx_pool[i],
Sukadev Bhattiprolu8243c7e2021-09-14 20:52:54 -0700855 pool_size, buff_size);
Thomas Falcon32053062018-03-16 20:00:27 -0500856 if (rc) {
Nathan Fontenotc657e322017-03-30 02:49:06 -0400857 release_tx_pools(adapter);
Thomas Falcon32053062018-03-16 20:00:27 -0500858 return rc;
Nathan Fontenotc657e322017-03-30 02:49:06 -0400859 }
860
Thomas Falcon7c940b12019-06-07 16:03:55 -0500861 rc = init_one_tx_pool(netdev, &adapter->tso_pool[i],
862 IBMVNIC_TSO_BUFS,
863 IBMVNIC_TSO_BUF_SZ);
Thomas Falcon32053062018-03-16 20:00:27 -0500864 if (rc) {
Nathan Fontenotc657e322017-03-30 02:49:06 -0400865 release_tx_pools(adapter);
Thomas Falcon32053062018-03-16 20:00:27 -0500866 return rc;
Nathan Fontenotc657e322017-03-30 02:49:06 -0400867 }
Nathan Fontenotc657e322017-03-30 02:49:06 -0400868 }
869
870 return 0;
871}
872
John Allend944c3d62017-05-26 10:30:13 -0400873static void ibmvnic_napi_enable(struct ibmvnic_adapter *adapter)
874{
875 int i;
876
877 if (adapter->napi_enabled)
878 return;
879
880 for (i = 0; i < adapter->req_rx_queues; i++)
881 napi_enable(&adapter->napi[i]);
882
883 adapter->napi_enabled = true;
884}
885
886static void ibmvnic_napi_disable(struct ibmvnic_adapter *adapter)
887{
888 int i;
889
890 if (!adapter->napi_enabled)
891 return;
892
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500893 for (i = 0; i < adapter->req_rx_queues; i++) {
894 netdev_dbg(adapter->netdev, "Disabling napi[%d]\n", i);
John Allend944c3d62017-05-26 10:30:13 -0400895 napi_disable(&adapter->napi[i]);
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500896 }
John Allend944c3d62017-05-26 10:30:13 -0400897
898 adapter->napi_enabled = false;
899}
900
Nathan Fontenot86f669b2018-02-19 13:30:39 -0600901static int init_napi(struct ibmvnic_adapter *adapter)
902{
903 int i;
904
905 adapter->napi = kcalloc(adapter->req_rx_queues,
906 sizeof(struct napi_struct), GFP_KERNEL);
907 if (!adapter->napi)
908 return -ENOMEM;
909
910 for (i = 0; i < adapter->req_rx_queues; i++) {
911 netdev_dbg(adapter->netdev, "Adding napi[%d]\n", i);
912 netif_napi_add(adapter->netdev, &adapter->napi[i],
913 ibmvnic_poll, NAPI_POLL_WEIGHT);
914 }
915
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600916 adapter->num_active_rx_napi = adapter->req_rx_queues;
Nathan Fontenot86f669b2018-02-19 13:30:39 -0600917 return 0;
918}
919
920static void release_napi(struct ibmvnic_adapter *adapter)
921{
922 int i;
923
924 if (!adapter->napi)
925 return;
926
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600927 for (i = 0; i < adapter->num_active_rx_napi; i++) {
Wen Yang390de192018-12-11 12:20:46 +0800928 netdev_dbg(adapter->netdev, "Releasing napi[%d]\n", i);
929 netif_napi_del(&adapter->napi[i]);
Nathan Fontenot86f669b2018-02-19 13:30:39 -0600930 }
931
932 kfree(adapter->napi);
933 adapter->napi = NULL;
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600934 adapter->num_active_rx_napi = 0;
Thomas Falconc3f22412018-05-23 13:37:55 -0500935 adapter->napi_enabled = false;
Nathan Fontenot86f669b2018-02-19 13:30:39 -0600936}
937
Lijun Pan0666ef72021-04-12 02:41:28 -0500938static const char *adapter_state_to_string(enum vnic_state state)
939{
940 switch (state) {
941 case VNIC_PROBING:
942 return "PROBING";
943 case VNIC_PROBED:
944 return "PROBED";
945 case VNIC_OPENING:
946 return "OPENING";
947 case VNIC_OPEN:
948 return "OPEN";
949 case VNIC_CLOSING:
950 return "CLOSING";
951 case VNIC_CLOSED:
952 return "CLOSED";
953 case VNIC_REMOVING:
954 return "REMOVING";
955 case VNIC_REMOVED:
956 return "REMOVED";
Lijun Pan822ebc22021-06-11 10:35:37 -0500957 case VNIC_DOWN:
958 return "DOWN";
Lijun Pan0666ef72021-04-12 02:41:28 -0500959 }
Michal Suchanek07b5dc12021-05-20 08:50:34 +0200960 return "UNKNOWN";
Lijun Pan0666ef72021-04-12 02:41:28 -0500961}
962
John Allena57a5d22017-03-17 17:13:41 -0500963static int ibmvnic_login(struct net_device *netdev)
Thomas Falcon032c5e82015-12-21 11:26:06 -0600964{
965 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Dany Madden98c41f02020-11-25 18:04:32 -0600966 unsigned long timeout = msecs_to_jiffies(20000);
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500967 int retry_count = 0;
Thomas Falcondff515a32020-06-15 10:29:23 -0500968 int retries = 10;
Thomas Falconeb110412018-05-24 14:37:53 -0500969 bool retry;
Thomas Falcon4d96f122017-08-01 15:04:36 -0500970 int rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600971
John Allenbd0b6722017-03-17 17:13:40 -0500972 do {
Thomas Falconeb110412018-05-24 14:37:53 -0500973 retry = false;
Thomas Falcondff515a32020-06-15 10:29:23 -0500974 if (retry_count > retries) {
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500975 netdev_warn(netdev, "Login attempts exceeded\n");
976 return -1;
977 }
978
979 adapter->init_done_rc = 0;
980 reinit_completion(&adapter->init_done);
981 rc = send_login(adapter);
Dany Maddenc98d9cc2020-11-25 18:04:30 -0600982 if (rc)
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500983 return rc;
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500984
985 if (!wait_for_completion_timeout(&adapter->init_done,
986 timeout)) {
Thomas Falcondff515a32020-06-15 10:29:23 -0500987 netdev_warn(netdev, "Login timed out, retrying...\n");
988 retry = true;
989 adapter->init_done_rc = 0;
990 retry_count++;
991 continue;
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500992 }
993
Thomas Falcondff515a32020-06-15 10:29:23 -0500994 if (adapter->init_done_rc == ABORTED) {
995 netdev_warn(netdev, "Login aborted, retrying...\n");
996 retry = true;
997 adapter->init_done_rc = 0;
998 retry_count++;
999 /* FW or device may be busy, so
1000 * wait a bit before retrying login
1001 */
1002 msleep(500);
1003 } else if (adapter->init_done_rc == PARTIALSUCCESS) {
Nathan Fontenot64d92aa2018-04-11 10:09:32 -05001004 retry_count++;
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06001005 release_sub_crqs(adapter, 1);
John Allenbd0b6722017-03-17 17:13:40 -05001006
Thomas Falconeb110412018-05-24 14:37:53 -05001007 retry = true;
1008 netdev_dbg(netdev,
1009 "Received partial success, retrying...\n");
Nathan Fontenot64d92aa2018-04-11 10:09:32 -05001010 adapter->init_done_rc = 0;
John Allenbd0b6722017-03-17 17:13:40 -05001011 reinit_completion(&adapter->init_done);
Lijun Pan491099a2020-09-27 20:13:26 -05001012 send_query_cap(adapter);
John Allenbd0b6722017-03-17 17:13:40 -05001013 if (!wait_for_completion_timeout(&adapter->init_done,
1014 timeout)) {
Nathan Fontenot64d92aa2018-04-11 10:09:32 -05001015 netdev_warn(netdev,
1016 "Capabilities query timed out\n");
John Allenbd0b6722017-03-17 17:13:40 -05001017 return -1;
1018 }
Nathan Fontenot64d92aa2018-04-11 10:09:32 -05001019
Thomas Falcon4d96f122017-08-01 15:04:36 -05001020 rc = init_sub_crqs(adapter);
1021 if (rc) {
Nathan Fontenot64d92aa2018-04-11 10:09:32 -05001022 netdev_warn(netdev,
1023 "SCRQ initialization failed\n");
Thomas Falcon4d96f122017-08-01 15:04:36 -05001024 return -1;
1025 }
Nathan Fontenot64d92aa2018-04-11 10:09:32 -05001026
Thomas Falcon4d96f122017-08-01 15:04:36 -05001027 rc = init_sub_crq_irqs(adapter);
1028 if (rc) {
Nathan Fontenot64d92aa2018-04-11 10:09:32 -05001029 netdev_warn(netdev,
1030 "SCRQ irq initialization failed\n");
Thomas Falcon4d96f122017-08-01 15:04:36 -05001031 return -1;
1032 }
Nathan Fontenot64d92aa2018-04-11 10:09:32 -05001033 } else if (adapter->init_done_rc) {
1034 netdev_warn(netdev, "Adapter login failed\n");
John Allenbd0b6722017-03-17 17:13:40 -05001035 return -1;
1036 }
Thomas Falconeb110412018-05-24 14:37:53 -05001037 } while (retry);
John Allenbd0b6722017-03-17 17:13:40 -05001038
Thomas Falcon62740e92019-05-09 23:13:43 -05001039 __ibmvnic_set_mac(netdev, adapter->mac_addr);
Thomas Falcon3d166132018-01-10 19:39:52 -06001040
Lijun Pan0666ef72021-04-12 02:41:28 -05001041 netdev_dbg(netdev, "[S:%s] Login succeeded\n", adapter_state_to_string(adapter->state));
John Allena57a5d22017-03-17 17:13:41 -05001042 return 0;
1043}
1044
Thomas Falcon34f0f4e2018-02-13 18:23:40 -06001045static void release_login_buffer(struct ibmvnic_adapter *adapter)
1046{
1047 kfree(adapter->login_buf);
1048 adapter->login_buf = NULL;
1049}
1050
1051static void release_login_rsp_buffer(struct ibmvnic_adapter *adapter)
1052{
1053 kfree(adapter->login_rsp_buf);
1054 adapter->login_rsp_buf = NULL;
1055}
1056
Nathan Fontenot1b8955e2017-03-30 02:49:29 -04001057static void release_resources(struct ibmvnic_adapter *adapter)
1058{
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001059 release_vpd_data(adapter);
1060
Nathan Fontenot1b8955e2017-03-30 02:49:29 -04001061 release_tx_pools(adapter);
1062 release_rx_pools(adapter);
1063
Nathan Fontenot86f669b2018-02-19 13:30:39 -06001064 release_napi(adapter);
Lijun Pana0c8be52020-12-19 15:39:19 -06001065 release_login_buffer(adapter);
Thomas Falcon34f0f4e2018-02-13 18:23:40 -06001066 release_login_rsp_buffer(adapter);
Nathan Fontenot1b8955e2017-03-30 02:49:29 -04001067}
1068
Nathan Fontenot53da09e2017-04-21 15:39:04 -04001069static int set_link_state(struct ibmvnic_adapter *adapter, u8 link_state)
1070{
1071 struct net_device *netdev = adapter->netdev;
Dany Madden98c41f02020-11-25 18:04:32 -06001072 unsigned long timeout = msecs_to_jiffies(20000);
Nathan Fontenot53da09e2017-04-21 15:39:04 -04001073 union ibmvnic_crq crq;
1074 bool resend;
1075 int rc;
1076
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001077 netdev_dbg(netdev, "setting link state %d\n", link_state);
1078
Nathan Fontenot53da09e2017-04-21 15:39:04 -04001079 memset(&crq, 0, sizeof(crq));
1080 crq.logical_link_state.first = IBMVNIC_CRQ_CMD;
1081 crq.logical_link_state.cmd = LOGICAL_LINK_STATE;
1082 crq.logical_link_state.link_state = link_state;
1083
1084 do {
1085 resend = false;
1086
1087 reinit_completion(&adapter->init_done);
1088 rc = ibmvnic_send_crq(adapter, &crq);
1089 if (rc) {
1090 netdev_err(netdev, "Failed to set link state\n");
1091 return rc;
1092 }
1093
1094 if (!wait_for_completion_timeout(&adapter->init_done,
1095 timeout)) {
1096 netdev_err(netdev, "timeout setting link state\n");
1097 return -1;
1098 }
1099
Lijun Pan4c5f6af2020-08-19 17:52:23 -05001100 if (adapter->init_done_rc == PARTIALSUCCESS) {
Nathan Fontenot53da09e2017-04-21 15:39:04 -04001101 /* Partuial success, delay and re-send */
1102 mdelay(1000);
1103 resend = true;
Thomas Falconab5ec332018-05-23 13:37:59 -05001104 } else if (adapter->init_done_rc) {
1105 netdev_warn(netdev, "Unable to set link state, rc=%d\n",
1106 adapter->init_done_rc);
1107 return adapter->init_done_rc;
Nathan Fontenot53da09e2017-04-21 15:39:04 -04001108 }
1109 } while (resend);
1110
1111 return 0;
1112}
1113
Thomas Falcon7f3c6e62017-04-21 15:38:40 -04001114static int set_real_num_queues(struct net_device *netdev)
1115{
1116 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1117 int rc;
1118
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001119 netdev_dbg(netdev, "Setting real tx/rx queues (%llx/%llx)\n",
1120 adapter->req_tx_queues, adapter->req_rx_queues);
1121
Thomas Falcon7f3c6e62017-04-21 15:38:40 -04001122 rc = netif_set_real_num_tx_queues(netdev, adapter->req_tx_queues);
1123 if (rc) {
1124 netdev_err(netdev, "failed to set the number of tx queues\n");
1125 return rc;
1126 }
1127
1128 rc = netif_set_real_num_rx_queues(netdev, adapter->req_rx_queues);
1129 if (rc)
1130 netdev_err(netdev, "failed to set the number of rx queues\n");
1131
1132 return rc;
1133}
1134
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001135static int ibmvnic_get_vpd(struct ibmvnic_adapter *adapter)
1136{
1137 struct device *dev = &adapter->vdev->dev;
1138 union ibmvnic_crq crq;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001139 int len = 0;
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05001140 int rc;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001141
1142 if (adapter->vpd->buff)
1143 len = adapter->vpd->len;
1144
Thomas Falconff25dcb2019-11-25 17:12:56 -06001145 mutex_lock(&adapter->fw_lock);
1146 adapter->fw_done_rc = 0;
Thomas Falcon070eca92019-11-25 17:12:53 -06001147 reinit_completion(&adapter->fw_done);
Thomas Falconff25dcb2019-11-25 17:12:56 -06001148
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001149 crq.get_vpd_size.first = IBMVNIC_CRQ_CMD;
1150 crq.get_vpd_size.cmd = GET_VPD_SIZE;
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05001151 rc = ibmvnic_send_crq(adapter, &crq);
Thomas Falconff25dcb2019-11-25 17:12:56 -06001152 if (rc) {
1153 mutex_unlock(&adapter->fw_lock);
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05001154 return rc;
Thomas Falconff25dcb2019-11-25 17:12:56 -06001155 }
Thomas Falcon476d96c2019-11-25 17:12:55 -06001156
1157 rc = ibmvnic_wait_for_completion(adapter, &adapter->fw_done, 10000);
1158 if (rc) {
1159 dev_err(dev, "Could not retrieve VPD size, rc = %d\n", rc);
Thomas Falconff25dcb2019-11-25 17:12:56 -06001160 mutex_unlock(&adapter->fw_lock);
Thomas Falcon476d96c2019-11-25 17:12:55 -06001161 return rc;
1162 }
Thomas Falconff25dcb2019-11-25 17:12:56 -06001163 mutex_unlock(&adapter->fw_lock);
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001164
1165 if (!adapter->vpd->len)
1166 return -ENODATA;
1167
1168 if (!adapter->vpd->buff)
1169 adapter->vpd->buff = kzalloc(adapter->vpd->len, GFP_KERNEL);
1170 else if (adapter->vpd->len != len)
1171 adapter->vpd->buff =
1172 krealloc(adapter->vpd->buff,
1173 adapter->vpd->len, GFP_KERNEL);
1174
1175 if (!adapter->vpd->buff) {
1176 dev_err(dev, "Could allocate VPD buffer\n");
1177 return -ENOMEM;
1178 }
1179
1180 adapter->vpd->dma_addr =
1181 dma_map_single(dev, adapter->vpd->buff, adapter->vpd->len,
1182 DMA_FROM_DEVICE);
Desnes Augusto Nunes do Rosariof7431062017-11-17 09:09:04 -02001183 if (dma_mapping_error(dev, adapter->vpd->dma_addr)) {
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001184 dev_err(dev, "Could not map VPD buffer\n");
1185 kfree(adapter->vpd->buff);
Thomas Falconb0992ec2018-02-06 17:25:23 -06001186 adapter->vpd->buff = NULL;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001187 return -ENOMEM;
1188 }
1189
Thomas Falconff25dcb2019-11-25 17:12:56 -06001190 mutex_lock(&adapter->fw_lock);
1191 adapter->fw_done_rc = 0;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001192 reinit_completion(&adapter->fw_done);
Thomas Falconff25dcb2019-11-25 17:12:56 -06001193
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001194 crq.get_vpd.first = IBMVNIC_CRQ_CMD;
1195 crq.get_vpd.cmd = GET_VPD;
1196 crq.get_vpd.ioba = cpu_to_be32(adapter->vpd->dma_addr);
1197 crq.get_vpd.len = cpu_to_be32((u32)adapter->vpd->len);
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05001198 rc = ibmvnic_send_crq(adapter, &crq);
1199 if (rc) {
1200 kfree(adapter->vpd->buff);
1201 adapter->vpd->buff = NULL;
Thomas Falconff25dcb2019-11-25 17:12:56 -06001202 mutex_unlock(&adapter->fw_lock);
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05001203 return rc;
1204 }
Thomas Falcon476d96c2019-11-25 17:12:55 -06001205
1206 rc = ibmvnic_wait_for_completion(adapter, &adapter->fw_done, 10000);
1207 if (rc) {
1208 dev_err(dev, "Unable to retrieve VPD, rc = %d\n", rc);
1209 kfree(adapter->vpd->buff);
1210 adapter->vpd->buff = NULL;
Thomas Falconff25dcb2019-11-25 17:12:56 -06001211 mutex_unlock(&adapter->fw_lock);
Thomas Falcon476d96c2019-11-25 17:12:55 -06001212 return rc;
1213 }
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001214
Thomas Falconff25dcb2019-11-25 17:12:56 -06001215 mutex_unlock(&adapter->fw_lock);
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001216 return 0;
1217}
1218
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001219static int init_resources(struct ibmvnic_adapter *adapter)
John Allena57a5d22017-03-17 17:13:41 -05001220{
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001221 struct net_device *netdev = adapter->netdev;
Nathan Fontenot86f669b2018-02-19 13:30:39 -06001222 int rc;
John Allena57a5d22017-03-17 17:13:41 -05001223
Thomas Falcon7f3c6e62017-04-21 15:38:40 -04001224 rc = set_real_num_queues(netdev);
1225 if (rc)
1226 return rc;
John Allenbd0b6722017-03-17 17:13:40 -05001227
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001228 adapter->vpd = kzalloc(sizeof(*adapter->vpd), GFP_KERNEL);
1229 if (!adapter->vpd)
1230 return -ENOMEM;
1231
John Allen69d08dc2018-01-18 16:27:58 -06001232 /* Vital Product Data (VPD) */
1233 rc = ibmvnic_get_vpd(adapter);
1234 if (rc) {
1235 netdev_err(netdev, "failed to initialize Vital Product Data (VPD)\n");
1236 return rc;
1237 }
1238
Thomas Falcon032c5e82015-12-21 11:26:06 -06001239 adapter->map_id = 1;
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001240
Nathan Fontenot86f669b2018-02-19 13:30:39 -06001241 rc = init_napi(adapter);
1242 if (rc)
1243 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001244
Lijun Pan69980d02020-09-27 20:13:28 -05001245 send_query_map(adapter);
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -04001246
1247 rc = init_rx_pools(netdev);
1248 if (rc)
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001249 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001250
Nathan Fontenotc657e322017-03-30 02:49:06 -04001251 rc = init_tx_pools(netdev);
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001252 return rc;
1253}
1254
Nathan Fontenoted651a12017-05-03 14:04:38 -04001255static int __ibmvnic_open(struct net_device *netdev)
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001256{
1257 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001258 enum vnic_state prev_state = adapter->state;
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001259 int i, rc;
1260
Nathan Fontenot90c80142017-05-03 14:04:32 -04001261 adapter->state = VNIC_OPENING;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001262 replenish_pools(adapter);
John Allend944c3d62017-05-26 10:30:13 -04001263 ibmvnic_napi_enable(adapter);
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001264
Thomas Falcon032c5e82015-12-21 11:26:06 -06001265 /* We're ready to receive frames, enable the sub-crq interrupts and
1266 * set the logical link state to up
1267 */
Nathan Fontenoted651a12017-05-03 14:04:38 -04001268 for (i = 0; i < adapter->req_rx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001269 netdev_dbg(netdev, "Enabling rx_scrq[%d] irq\n", i);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001270 if (prev_state == VNIC_CLOSED)
1271 enable_irq(adapter->rx_scrq[i]->irq);
Thomas Falconf23e0642018-04-15 18:53:36 -05001272 enable_scrq_irq(adapter, adapter->rx_scrq[i]);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001273 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001274
Nathan Fontenoted651a12017-05-03 14:04:38 -04001275 for (i = 0; i < adapter->req_tx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001276 netdev_dbg(netdev, "Enabling tx_scrq[%d] irq\n", i);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001277 if (prev_state == VNIC_CLOSED)
1278 enable_irq(adapter->tx_scrq[i]->irq);
Thomas Falconf23e0642018-04-15 18:53:36 -05001279 enable_scrq_irq(adapter, adapter->tx_scrq[i]);
Thomas Falcon0d973382020-11-18 19:12:19 -06001280 netdev_tx_reset_queue(netdev_get_tx_queue(netdev, i));
Nathan Fontenoted651a12017-05-03 14:04:38 -04001281 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001282
Nathan Fontenot53da09e2017-04-21 15:39:04 -04001283 rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_UP);
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001284 if (rc) {
Lijun Pan0775ebc2021-04-14 02:46:14 -05001285 ibmvnic_napi_disable(adapter);
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001286 release_resources(adapter);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001287 return rc;
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001288 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001289
Nathan Fontenoted651a12017-05-03 14:04:38 -04001290 netif_tx_start_all_queues(netdev);
1291
Dany Madden2ca220f2021-06-23 21:13:11 -07001292 if (prev_state == VNIC_CLOSED) {
1293 for (i = 0; i < adapter->req_rx_queues; i++)
1294 napi_schedule(&adapter->napi[i]);
1295 }
1296
Nathan Fontenoted651a12017-05-03 14:04:38 -04001297 adapter->state = VNIC_OPEN;
1298 return rc;
1299}
1300
1301static int ibmvnic_open(struct net_device *netdev)
1302{
1303 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
John Allen69d08dc2018-01-18 16:27:58 -06001304 int rc;
Nathan Fontenoted651a12017-05-03 14:04:38 -04001305
Sukadev Bhattiprolu8f1c0fd2021-02-23 21:02:29 -08001306 ASSERT_RTNL();
1307
1308 /* If device failover is pending or we are about to reset, just set
1309 * device state and return. Device operation will be handled by reset
1310 * routine.
1311 *
1312 * It should be safe to overwrite the adapter->state here. Since
1313 * we hold the rtnl, either the reset has not actually started or
1314 * the rtnl got dropped during the set_link_state() in do_reset().
1315 * In the former case, no one else is changing the state (again we
1316 * have the rtnl) and in the latter case, do_reset() will detect and
1317 * honor our setting below.
Thomas Falcon5a18e1e2018-04-06 18:37:05 -05001318 */
Sukadev Bhattiprolu8f1c0fd2021-02-23 21:02:29 -08001319 if (adapter->failover_pending || (test_bit(0, &adapter->resetting))) {
Lijun Pan0666ef72021-04-12 02:41:28 -05001320 netdev_dbg(netdev, "[S:%s FOP:%d] Resetting, deferring open\n",
1321 adapter_state_to_string(adapter->state),
1322 adapter->failover_pending);
Thomas Falcon5a18e1e2018-04-06 18:37:05 -05001323 adapter->state = VNIC_OPEN;
Sukadev Bhattiprolu8f1c0fd2021-02-23 21:02:29 -08001324 rc = 0;
1325 goto out;
Thomas Falcon5a18e1e2018-04-06 18:37:05 -05001326 }
1327
Nathan Fontenoted651a12017-05-03 14:04:38 -04001328 if (adapter->state != VNIC_CLOSED) {
1329 rc = ibmvnic_login(netdev);
Juliet Kima5681e22018-11-19 15:59:22 -06001330 if (rc)
Sukadev Bhattiprolu1d850492020-10-30 10:07:11 -07001331 goto out;
Nathan Fontenoted651a12017-05-03 14:04:38 -04001332
1333 rc = init_resources(adapter);
1334 if (rc) {
1335 netdev_err(netdev, "failed to initialize resources\n");
1336 release_resources(adapter);
Sukadev Bhattiprolu1d850492020-10-30 10:07:11 -07001337 goto out;
Nathan Fontenoted651a12017-05-03 14:04:38 -04001338 }
1339 }
1340
1341 rc = __ibmvnic_open(netdev);
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001342
Sukadev Bhattiprolu1d850492020-10-30 10:07:11 -07001343out:
Sukadev Bhattiprolu8f1c0fd2021-02-23 21:02:29 -08001344 /* If open failed and there is a pending failover or in-progress reset,
1345 * set device state and return. Device operation will be handled by
1346 * reset routine. See also comments above regarding rtnl.
Sukadev Bhattiprolu1d850492020-10-30 10:07:11 -07001347 */
Sukadev Bhattiprolu8f1c0fd2021-02-23 21:02:29 -08001348 if (rc &&
1349 (adapter->failover_pending || (test_bit(0, &adapter->resetting)))) {
Sukadev Bhattiprolu1d850492020-10-30 10:07:11 -07001350 adapter->state = VNIC_OPEN;
1351 rc = 0;
1352 }
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001353 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001354}
1355
Thomas Falcond0869c02018-02-13 18:23:43 -06001356static void clean_rx_pools(struct ibmvnic_adapter *adapter)
1357{
1358 struct ibmvnic_rx_pool *rx_pool;
Thomas Falcon637f81d2018-02-26 18:10:57 -06001359 struct ibmvnic_rx_buff *rx_buff;
Thomas Falcond0869c02018-02-13 18:23:43 -06001360 u64 rx_entries;
1361 int rx_scrqs;
1362 int i, j;
1363
1364 if (!adapter->rx_pool)
1365 return;
1366
Thomas Falcon660e3092018-04-20 14:25:32 -05001367 rx_scrqs = adapter->num_active_rx_pools;
Thomas Falcond0869c02018-02-13 18:23:43 -06001368 rx_entries = adapter->req_rx_add_entries_per_subcrq;
1369
1370 /* Free any remaining skbs in the rx buffer pools */
1371 for (i = 0; i < rx_scrqs; i++) {
1372 rx_pool = &adapter->rx_pool[i];
Thomas Falcon637f81d2018-02-26 18:10:57 -06001373 if (!rx_pool || !rx_pool->rx_buff)
Thomas Falcond0869c02018-02-13 18:23:43 -06001374 continue;
1375
1376 netdev_dbg(adapter->netdev, "Cleaning rx_pool[%d]\n", i);
1377 for (j = 0; j < rx_entries; j++) {
Thomas Falcon637f81d2018-02-26 18:10:57 -06001378 rx_buff = &rx_pool->rx_buff[j];
1379 if (rx_buff && rx_buff->skb) {
1380 dev_kfree_skb_any(rx_buff->skb);
1381 rx_buff->skb = NULL;
Thomas Falcond0869c02018-02-13 18:23:43 -06001382 }
1383 }
1384 }
1385}
1386
Thomas Falcone9e1e972018-03-16 20:00:30 -05001387static void clean_one_tx_pool(struct ibmvnic_adapter *adapter,
1388 struct ibmvnic_tx_pool *tx_pool)
Nathan Fontenotb41b83e2017-05-03 14:04:56 -04001389{
Thomas Falcon637f81d2018-02-26 18:10:57 -06001390 struct ibmvnic_tx_buff *tx_buff;
Nathan Fontenotb41b83e2017-05-03 14:04:56 -04001391 u64 tx_entries;
Thomas Falcone9e1e972018-03-16 20:00:30 -05001392 int i;
Nathan Fontenotb41b83e2017-05-03 14:04:56 -04001393
Dan Carpenter050e85c2018-03-23 14:36:15 +03001394 if (!tx_pool || !tx_pool->tx_buff)
Thomas Falcone9e1e972018-03-16 20:00:30 -05001395 return;
1396
1397 tx_entries = tx_pool->num_buffers;
1398
1399 for (i = 0; i < tx_entries; i++) {
1400 tx_buff = &tx_pool->tx_buff[i];
1401 if (tx_buff && tx_buff->skb) {
1402 dev_kfree_skb_any(tx_buff->skb);
1403 tx_buff->skb = NULL;
1404 }
1405 }
1406}
1407
1408static void clean_tx_pools(struct ibmvnic_adapter *adapter)
1409{
1410 int tx_scrqs;
1411 int i;
1412
1413 if (!adapter->tx_pool || !adapter->tso_pool)
Nathan Fontenotb41b83e2017-05-03 14:04:56 -04001414 return;
1415
Thomas Falcon660e3092018-04-20 14:25:32 -05001416 tx_scrqs = adapter->num_active_tx_pools;
Nathan Fontenotb41b83e2017-05-03 14:04:56 -04001417
1418 /* Free any remaining skbs in the tx buffer pools */
1419 for (i = 0; i < tx_scrqs; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001420 netdev_dbg(adapter->netdev, "Cleaning tx_pool[%d]\n", i);
Thomas Falcone9e1e972018-03-16 20:00:30 -05001421 clean_one_tx_pool(adapter, &adapter->tx_pool[i]);
1422 clean_one_tx_pool(adapter, &adapter->tso_pool[i]);
Nathan Fontenotb41b83e2017-05-03 14:04:56 -04001423 }
1424}
1425
John Allen6095e592018-03-30 13:44:21 -05001426static void ibmvnic_disable_irqs(struct ibmvnic_adapter *adapter)
John Allenea5509f2017-03-17 17:13:43 -05001427{
John Allen6095e592018-03-30 13:44:21 -05001428 struct net_device *netdev = adapter->netdev;
John Allenea5509f2017-03-17 17:13:43 -05001429 int i;
1430
Nathan Fontenot46293b92017-05-03 14:05:02 -04001431 if (adapter->tx_scrq) {
1432 for (i = 0; i < adapter->req_tx_queues; i++)
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001433 if (adapter->tx_scrq[i]->irq) {
Thomas Falconf8738662018-03-07 17:51:45 -06001434 netdev_dbg(netdev,
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001435 "Disabling tx_scrq[%d] irq\n", i);
Thomas Falconf23e0642018-04-15 18:53:36 -05001436 disable_scrq_irq(adapter, adapter->tx_scrq[i]);
Nathan Fontenot46293b92017-05-03 14:05:02 -04001437 disable_irq(adapter->tx_scrq[i]->irq);
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001438 }
Nathan Fontenot46293b92017-05-03 14:05:02 -04001439 }
1440
Nathan Fontenot46293b92017-05-03 14:05:02 -04001441 if (adapter->rx_scrq) {
1442 for (i = 0; i < adapter->req_rx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001443 if (adapter->rx_scrq[i]->irq) {
Thomas Falconf8738662018-03-07 17:51:45 -06001444 netdev_dbg(netdev,
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001445 "Disabling rx_scrq[%d] irq\n", i);
Thomas Falconf23e0642018-04-15 18:53:36 -05001446 disable_scrq_irq(adapter, adapter->rx_scrq[i]);
Nathan Fontenot46293b92017-05-03 14:05:02 -04001447 disable_irq(adapter->rx_scrq[i]->irq);
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001448 }
Nathan Fontenot46293b92017-05-03 14:05:02 -04001449 }
1450 }
John Allen6095e592018-03-30 13:44:21 -05001451}
1452
1453static void ibmvnic_cleanup(struct net_device *netdev)
1454{
1455 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1456
1457 /* ensure that transmissions are stopped if called by do_reset */
Juliet Kim7ed5b312019-09-20 16:11:23 -04001458 if (test_bit(0, &adapter->resetting))
John Allen6095e592018-03-30 13:44:21 -05001459 netif_tx_disable(netdev);
1460 else
1461 netif_tx_stop_all_queues(netdev);
1462
1463 ibmvnic_napi_disable(adapter);
1464 ibmvnic_disable_irqs(adapter);
1465
Thomas Falcond0869c02018-02-13 18:23:43 -06001466 clean_rx_pools(adapter);
Thomas Falcon10f76212017-05-26 10:30:31 -04001467 clean_tx_pools(adapter);
Thomas Falcon01d9bd72018-03-07 17:51:46 -06001468}
1469
1470static int __ibmvnic_close(struct net_device *netdev)
1471{
1472 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1473 int rc = 0;
1474
1475 adapter->state = VNIC_CLOSING;
1476 rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_DN);
Nathan Fontenot90c80142017-05-03 14:04:32 -04001477 adapter->state = VNIC_CLOSED;
Sukadev Bhattiprolud4083d32021-02-10 17:41:43 -08001478 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001479}
1480
Nathan Fontenoted651a12017-05-03 14:04:38 -04001481static int ibmvnic_close(struct net_device *netdev)
1482{
1483 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1484 int rc;
1485
Lijun Pan0666ef72021-04-12 02:41:28 -05001486 netdev_dbg(netdev, "[S:%s FOP:%d FRR:%d] Closing\n",
1487 adapter_state_to_string(adapter->state),
1488 adapter->failover_pending,
Sukadev Bhattiprolu38bd5ce2020-12-04 18:22:35 -08001489 adapter->force_reset_recovery);
1490
Thomas Falcon5a18e1e2018-04-06 18:37:05 -05001491 /* If device failover is pending, just set device state and return.
1492 * Device operation will be handled by reset routine.
1493 */
1494 if (adapter->failover_pending) {
1495 adapter->state = VNIC_CLOSED;
1496 return 0;
1497 }
1498
Nathan Fontenoted651a12017-05-03 14:04:38 -04001499 rc = __ibmvnic_close(netdev);
Nathan Fontenot30f79622018-04-06 18:37:06 -05001500 ibmvnic_cleanup(netdev);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001501
1502 return rc;
1503}
1504
Thomas Falconad7775d2016-04-01 17:20:34 -05001505/**
1506 * build_hdr_data - creates L2/L3/L4 header data buffer
Lee Jones80708602021-01-15 20:09:03 +00001507 * @hdr_field: bitfield determining needed headers
1508 * @skb: socket buffer
1509 * @hdr_len: array of header lengths
1510 * @hdr_data: buffer to write the header to
Thomas Falconad7775d2016-04-01 17:20:34 -05001511 *
1512 * Reads hdr_field to determine which headers are needed by firmware.
1513 * Builds a buffer containing these headers. Saves individual header
1514 * lengths and total buffer length to be used to build descriptors.
1515 */
1516static int build_hdr_data(u8 hdr_field, struct sk_buff *skb,
1517 int *hdr_len, u8 *hdr_data)
1518{
1519 int len = 0;
1520 u8 *hdr;
1521
Thomas Falconda75e3b2018-03-12 11:51:02 -05001522 if (skb_vlan_tagged(skb) && !skb_vlan_tag_present(skb))
1523 hdr_len[0] = sizeof(struct vlan_ethhdr);
1524 else
1525 hdr_len[0] = sizeof(struct ethhdr);
Thomas Falconad7775d2016-04-01 17:20:34 -05001526
1527 if (skb->protocol == htons(ETH_P_IP)) {
1528 hdr_len[1] = ip_hdr(skb)->ihl * 4;
1529 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
1530 hdr_len[2] = tcp_hdrlen(skb);
1531 else if (ip_hdr(skb)->protocol == IPPROTO_UDP)
1532 hdr_len[2] = sizeof(struct udphdr);
1533 } else if (skb->protocol == htons(ETH_P_IPV6)) {
1534 hdr_len[1] = sizeof(struct ipv6hdr);
1535 if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
1536 hdr_len[2] = tcp_hdrlen(skb);
1537 else if (ipv6_hdr(skb)->nexthdr == IPPROTO_UDP)
1538 hdr_len[2] = sizeof(struct udphdr);
Thomas Falcon4eb50ce2017-12-18 12:52:40 -06001539 } else if (skb->protocol == htons(ETH_P_ARP)) {
1540 hdr_len[1] = arp_hdr_len(skb->dev);
1541 hdr_len[2] = 0;
Thomas Falconad7775d2016-04-01 17:20:34 -05001542 }
1543
1544 memset(hdr_data, 0, 120);
1545 if ((hdr_field >> 6) & 1) {
1546 hdr = skb_mac_header(skb);
1547 memcpy(hdr_data, hdr, hdr_len[0]);
1548 len += hdr_len[0];
1549 }
1550
1551 if ((hdr_field >> 5) & 1) {
1552 hdr = skb_network_header(skb);
1553 memcpy(hdr_data + len, hdr, hdr_len[1]);
1554 len += hdr_len[1];
1555 }
1556
1557 if ((hdr_field >> 4) & 1) {
1558 hdr = skb_transport_header(skb);
1559 memcpy(hdr_data + len, hdr, hdr_len[2]);
1560 len += hdr_len[2];
1561 }
1562 return len;
1563}
1564
1565/**
1566 * create_hdr_descs - create header and header extension descriptors
Lee Jones80708602021-01-15 20:09:03 +00001567 * @hdr_field: bitfield determining needed headers
1568 * @hdr_data: buffer containing header data
1569 * @len: length of data buffer
1570 * @hdr_len: array of individual header lengths
1571 * @scrq_arr: descriptor array
Thomas Falconad7775d2016-04-01 17:20:34 -05001572 *
1573 * Creates header and, if needed, header extension descriptors and
1574 * places them in a descriptor array, scrq_arr
1575 */
1576
Thomas Falcon2de09682017-10-16 10:02:11 -05001577static int create_hdr_descs(u8 hdr_field, u8 *hdr_data, int len, int *hdr_len,
1578 union sub_crq *scrq_arr)
Thomas Falconad7775d2016-04-01 17:20:34 -05001579{
1580 union sub_crq hdr_desc;
1581 int tmp_len = len;
Thomas Falcon2de09682017-10-16 10:02:11 -05001582 int num_descs = 0;
Thomas Falconad7775d2016-04-01 17:20:34 -05001583 u8 *data, *cur;
1584 int tmp;
1585
1586 while (tmp_len > 0) {
1587 cur = hdr_data + len - tmp_len;
1588
1589 memset(&hdr_desc, 0, sizeof(hdr_desc));
1590 if (cur != hdr_data) {
1591 data = hdr_desc.hdr_ext.data;
1592 tmp = tmp_len > 29 ? 29 : tmp_len;
1593 hdr_desc.hdr_ext.first = IBMVNIC_CRQ_CMD;
1594 hdr_desc.hdr_ext.type = IBMVNIC_HDR_EXT_DESC;
1595 hdr_desc.hdr_ext.len = tmp;
1596 } else {
1597 data = hdr_desc.hdr.data;
1598 tmp = tmp_len > 24 ? 24 : tmp_len;
1599 hdr_desc.hdr.first = IBMVNIC_CRQ_CMD;
1600 hdr_desc.hdr.type = IBMVNIC_HDR_DESC;
1601 hdr_desc.hdr.len = tmp;
1602 hdr_desc.hdr.l2_len = (u8)hdr_len[0];
1603 hdr_desc.hdr.l3_len = cpu_to_be16((u16)hdr_len[1]);
1604 hdr_desc.hdr.l4_len = (u8)hdr_len[2];
1605 hdr_desc.hdr.flag = hdr_field << 1;
1606 }
1607 memcpy(data, cur, tmp);
1608 tmp_len -= tmp;
1609 *scrq_arr = hdr_desc;
1610 scrq_arr++;
Thomas Falcon2de09682017-10-16 10:02:11 -05001611 num_descs++;
Thomas Falconad7775d2016-04-01 17:20:34 -05001612 }
Thomas Falcon2de09682017-10-16 10:02:11 -05001613
1614 return num_descs;
Thomas Falconad7775d2016-04-01 17:20:34 -05001615}
1616
1617/**
1618 * build_hdr_descs_arr - build a header descriptor array
Lijun Pan73214a62021-06-11 10:43:39 -05001619 * @skb: tx socket buffer
1620 * @indir_arr: indirect array
Lee Jones80708602021-01-15 20:09:03 +00001621 * @num_entries: number of descriptors to be sent
1622 * @hdr_field: bit field determining which headers will be sent
Thomas Falconad7775d2016-04-01 17:20:34 -05001623 *
1624 * This function will build a TX descriptor array with applicable
1625 * L2/L3/L4 packet header descriptors to be sent by send_subcrq_indirect.
1626 */
1627
Thomas Falconc62aa372020-11-18 19:12:20 -06001628static void build_hdr_descs_arr(struct sk_buff *skb,
1629 union sub_crq *indir_arr,
Thomas Falconad7775d2016-04-01 17:20:34 -05001630 int *num_entries, u8 hdr_field)
1631{
1632 int hdr_len[3] = {0, 0, 0};
Thomas Falconc62aa372020-11-18 19:12:20 -06001633 u8 hdr_data[140] = {0};
Thomas Falcon2de09682017-10-16 10:02:11 -05001634 int tot_len;
Thomas Falconad7775d2016-04-01 17:20:34 -05001635
Thomas Falconc62aa372020-11-18 19:12:20 -06001636 tot_len = build_hdr_data(hdr_field, skb, hdr_len,
1637 hdr_data);
Thomas Falcon2de09682017-10-16 10:02:11 -05001638 *num_entries += create_hdr_descs(hdr_field, hdr_data, tot_len, hdr_len,
Thomas Falconc62aa372020-11-18 19:12:20 -06001639 indir_arr + 1);
Thomas Falconad7775d2016-04-01 17:20:34 -05001640}
1641
Thomas Falcon1f247a62018-03-12 11:51:04 -05001642static int ibmvnic_xmit_workarounds(struct sk_buff *skb,
1643 struct net_device *netdev)
1644{
1645 /* For some backing devices, mishandling of small packets
1646 * can result in a loss of connection or TX stall. Device
1647 * architects recommend that no packet should be smaller
1648 * than the minimum MTU value provided to the driver, so
1649 * pad any packets to that length
1650 */
1651 if (skb->len < netdev->min_mtu)
1652 return skb_put_padto(skb, netdev->min_mtu);
Thomas Falcon7083a452018-03-12 21:05:26 -05001653
1654 return 0;
Thomas Falcon1f247a62018-03-12 11:51:04 -05001655}
1656
Thomas Falcon0d973382020-11-18 19:12:19 -06001657static void ibmvnic_tx_scrq_clean_buffer(struct ibmvnic_adapter *adapter,
1658 struct ibmvnic_sub_crq_queue *tx_scrq)
1659{
1660 struct ibmvnic_ind_xmit_queue *ind_bufp;
1661 struct ibmvnic_tx_buff *tx_buff;
1662 struct ibmvnic_tx_pool *tx_pool;
1663 union sub_crq tx_scrq_entry;
1664 int queue_num;
1665 int entries;
1666 int index;
1667 int i;
1668
1669 ind_bufp = &tx_scrq->ind_buf;
1670 entries = (u64)ind_bufp->index;
1671 queue_num = tx_scrq->pool_index;
1672
1673 for (i = entries - 1; i >= 0; --i) {
1674 tx_scrq_entry = ind_bufp->indir_arr[i];
1675 if (tx_scrq_entry.v1.type != IBMVNIC_TX_DESC)
1676 continue;
1677 index = be32_to_cpu(tx_scrq_entry.v1.correlator);
1678 if (index & IBMVNIC_TSO_POOL_MASK) {
1679 tx_pool = &adapter->tso_pool[queue_num];
1680 index &= ~IBMVNIC_TSO_POOL_MASK;
1681 } else {
1682 tx_pool = &adapter->tx_pool[queue_num];
1683 }
1684 tx_pool->free_map[tx_pool->consumer_index] = index;
1685 tx_pool->consumer_index = tx_pool->consumer_index == 0 ?
1686 tx_pool->num_buffers - 1 :
1687 tx_pool->consumer_index - 1;
1688 tx_buff = &tx_pool->tx_buff[index];
1689 adapter->netdev->stats.tx_packets--;
1690 adapter->netdev->stats.tx_bytes -= tx_buff->skb->len;
1691 adapter->tx_stats_buffers[queue_num].packets--;
1692 adapter->tx_stats_buffers[queue_num].bytes -=
1693 tx_buff->skb->len;
1694 dev_kfree_skb_any(tx_buff->skb);
1695 tx_buff->skb = NULL;
1696 adapter->netdev->stats.tx_dropped++;
1697 }
1698 ind_bufp->index = 0;
1699 if (atomic_sub_return(entries, &tx_scrq->used) <=
1700 (adapter->req_tx_entries_per_subcrq / 2) &&
Sukadev Bhattiprolu65d64702021-06-23 21:13:12 -07001701 __netif_subqueue_stopped(adapter->netdev, queue_num) &&
1702 !test_bit(0, &adapter->resetting)) {
Thomas Falcon0d973382020-11-18 19:12:19 -06001703 netif_wake_subqueue(adapter->netdev, queue_num);
1704 netdev_dbg(adapter->netdev, "Started queue %d\n",
1705 queue_num);
1706 }
1707}
1708
1709static int ibmvnic_tx_scrq_flush(struct ibmvnic_adapter *adapter,
1710 struct ibmvnic_sub_crq_queue *tx_scrq)
1711{
1712 struct ibmvnic_ind_xmit_queue *ind_bufp;
1713 u64 dma_addr;
1714 u64 entries;
1715 u64 handle;
1716 int rc;
1717
1718 ind_bufp = &tx_scrq->ind_buf;
1719 dma_addr = (u64)ind_bufp->indir_dma;
1720 entries = (u64)ind_bufp->index;
1721 handle = tx_scrq->handle;
1722
1723 if (!entries)
1724 return 0;
1725 rc = send_subcrq_indirect(adapter, handle, dma_addr, entries);
1726 if (rc)
1727 ibmvnic_tx_scrq_clean_buffer(adapter, tx_scrq);
1728 else
1729 ind_bufp->index = 0;
1730 return 0;
1731}
1732
YueHaibing94b2bb22018-09-18 14:35:47 +08001733static netdev_tx_t ibmvnic_xmit(struct sk_buff *skb, struct net_device *netdev)
Thomas Falcon032c5e82015-12-21 11:26:06 -06001734{
1735 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1736 int queue_num = skb_get_queue_mapping(skb);
Thomas Falconad7775d2016-04-01 17:20:34 -05001737 u8 *hdrs = (u8 *)&adapter->tx_rx_desc_req;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001738 struct device *dev = &adapter->vdev->dev;
Thomas Falcon0d973382020-11-18 19:12:19 -06001739 struct ibmvnic_ind_xmit_queue *ind_bufp;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001740 struct ibmvnic_tx_buff *tx_buff = NULL;
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001741 struct ibmvnic_sub_crq_queue *tx_scrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001742 struct ibmvnic_tx_pool *tx_pool;
1743 unsigned int tx_send_failed = 0;
Thomas Falcon0d973382020-11-18 19:12:19 -06001744 netdev_tx_t ret = NETDEV_TX_OK;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001745 unsigned int tx_map_failed = 0;
Thomas Falconc62aa372020-11-18 19:12:20 -06001746 union sub_crq indir_arr[16];
Thomas Falcon032c5e82015-12-21 11:26:06 -06001747 unsigned int tx_dropped = 0;
1748 unsigned int tx_packets = 0;
1749 unsigned int tx_bytes = 0;
1750 dma_addr_t data_dma_addr;
1751 struct netdev_queue *txq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001752 unsigned long lpar_rc;
1753 union sub_crq tx_crq;
1754 unsigned int offset;
Thomas Falconad7775d2016-04-01 17:20:34 -05001755 int num_entries = 1;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001756 unsigned char *dst;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001757 int index = 0;
Thomas Falcona0dca102018-01-18 19:29:48 -06001758 u8 proto = 0;
Thomas Falcon0d973382020-11-18 19:12:19 -06001759
1760 tx_scrq = adapter->tx_scrq[queue_num];
1761 txq = netdev_get_tx_queue(netdev, queue_num);
1762 ind_bufp = &tx_scrq->ind_buf;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001763
Juliet Kim7ed5b312019-09-20 16:11:23 -04001764 if (test_bit(0, &adapter->resetting)) {
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001765 if (!netif_subqueue_stopped(netdev, skb))
1766 netif_stop_subqueue(netdev, queue_num);
1767 dev_kfree_skb_any(skb);
1768
Thomas Falcon032c5e82015-12-21 11:26:06 -06001769 tx_send_failed++;
1770 tx_dropped++;
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001771 ret = NETDEV_TX_OK;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001772 goto out;
1773 }
1774
Thomas Falcon7083a452018-03-12 21:05:26 -05001775 if (ibmvnic_xmit_workarounds(skb, netdev)) {
Thomas Falcon1f247a62018-03-12 11:51:04 -05001776 tx_dropped++;
1777 tx_send_failed++;
1778 ret = NETDEV_TX_OK;
Thomas Falcon0d973382020-11-18 19:12:19 -06001779 ibmvnic_tx_scrq_flush(adapter, tx_scrq);
Thomas Falcon1f247a62018-03-12 11:51:04 -05001780 goto out;
1781 }
Thomas Falcon06b3e352018-03-16 20:00:28 -05001782 if (skb_is_gso(skb))
1783 tx_pool = &adapter->tso_pool[queue_num];
1784 else
1785 tx_pool = &adapter->tx_pool[queue_num];
Thomas Falcon1f247a62018-03-12 11:51:04 -05001786
Thomas Falcon032c5e82015-12-21 11:26:06 -06001787 index = tx_pool->free_map[tx_pool->consumer_index];
Thomas Falconfdb06102017-10-17 12:36:55 -05001788
Thomas Falcon86b61a52018-03-16 20:00:29 -05001789 if (index == IBMVNIC_INVALID_MAP) {
1790 dev_kfree_skb_any(skb);
1791 tx_send_failed++;
1792 tx_dropped++;
Sukadev Bhattiprolubb553622021-07-20 19:34:39 -07001793 ibmvnic_tx_scrq_flush(adapter, tx_scrq);
Thomas Falcon86b61a52018-03-16 20:00:29 -05001794 ret = NETDEV_TX_OK;
1795 goto out;
1796 }
1797
1798 tx_pool->free_map[tx_pool->consumer_index] = IBMVNIC_INVALID_MAP;
1799
Thomas Falcon06b3e352018-03-16 20:00:28 -05001800 offset = index * tx_pool->buf_size;
1801 dst = tx_pool->long_term_buff.buff + offset;
1802 memset(dst, 0, tx_pool->buf_size);
1803 data_dma_addr = tx_pool->long_term_buff.addr + offset;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001804
Thomas Falcon15482052017-10-17 12:36:54 -05001805 if (skb_shinfo(skb)->nr_frags) {
1806 int cur, i;
1807
1808 /* Copy the head */
1809 skb_copy_from_linear_data(skb, dst, skb_headlen(skb));
1810 cur = skb_headlen(skb);
1811
1812 /* Copy the frags */
1813 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1814 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1815
Christophe JAILLETc3105f82021-04-04 10:54:37 +02001816 memcpy(dst + cur, skb_frag_address(frag),
1817 skb_frag_size(frag));
Thomas Falcon15482052017-10-17 12:36:54 -05001818 cur += skb_frag_size(frag);
1819 }
1820 } else {
1821 skb_copy_from_linear_data(skb, dst, skb->len);
1822 }
1823
Lijun Pan42557da2021-02-12 20:48:40 -06001824 /* post changes to long_term_buff *dst before VIOS accessing it */
1825 dma_wmb();
1826
Thomas Falcon032c5e82015-12-21 11:26:06 -06001827 tx_pool->consumer_index =
Thomas Falcon06b3e352018-03-16 20:00:28 -05001828 (tx_pool->consumer_index + 1) % tx_pool->num_buffers;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001829
1830 tx_buff = &tx_pool->tx_buff[index];
1831 tx_buff->skb = skb;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001832 tx_buff->index = index;
1833 tx_buff->pool_index = queue_num;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001834
1835 memset(&tx_crq, 0, sizeof(tx_crq));
1836 tx_crq.v1.first = IBMVNIC_CRQ_CMD;
1837 tx_crq.v1.type = IBMVNIC_TX_DESC;
1838 tx_crq.v1.n_crq_elem = 1;
1839 tx_crq.v1.n_sge = 1;
1840 tx_crq.v1.flags1 = IBMVNIC_TX_COMP_NEEDED;
Thomas Falcon06b3e352018-03-16 20:00:28 -05001841
Thomas Falconfdb06102017-10-17 12:36:55 -05001842 if (skb_is_gso(skb))
Thomas Falcon06b3e352018-03-16 20:00:28 -05001843 tx_crq.v1.correlator =
1844 cpu_to_be32(index | IBMVNIC_TSO_POOL_MASK);
Thomas Falconfdb06102017-10-17 12:36:55 -05001845 else
Thomas Falcon06b3e352018-03-16 20:00:28 -05001846 tx_crq.v1.correlator = cpu_to_be32(index);
1847 tx_crq.v1.dma_reg = cpu_to_be16(tx_pool->long_term_buff.map_id);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001848 tx_crq.v1.sge_len = cpu_to_be32(skb->len);
1849 tx_crq.v1.ioba = cpu_to_be64(data_dma_addr);
1850
Michał Mirosławe84b4792018-11-07 17:50:52 +01001851 if (adapter->vlan_header_insertion && skb_vlan_tag_present(skb)) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06001852 tx_crq.v1.flags2 |= IBMVNIC_TX_VLAN_INSERT;
1853 tx_crq.v1.vlan_id = cpu_to_be16(skb->vlan_tci);
1854 }
1855
1856 if (skb->protocol == htons(ETH_P_IP)) {
Thomas Falcona0dca102018-01-18 19:29:48 -06001857 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV4;
1858 proto = ip_hdr(skb)->protocol;
1859 } else if (skb->protocol == htons(ETH_P_IPV6)) {
1860 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV6;
1861 proto = ipv6_hdr(skb)->nexthdr;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001862 }
1863
Thomas Falcona0dca102018-01-18 19:29:48 -06001864 if (proto == IPPROTO_TCP)
1865 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_TCP;
1866 else if (proto == IPPROTO_UDP)
1867 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_UDP;
1868
Thomas Falconad7775d2016-04-01 17:20:34 -05001869 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06001870 tx_crq.v1.flags1 |= IBMVNIC_TX_CHKSUM_OFFLOAD;
Thomas Falconad7775d2016-04-01 17:20:34 -05001871 hdrs += 2;
1872 }
Thomas Falconfdb06102017-10-17 12:36:55 -05001873 if (skb_is_gso(skb)) {
1874 tx_crq.v1.flags1 |= IBMVNIC_TX_LSO;
1875 tx_crq.v1.mss = cpu_to_be16(skb_shinfo(skb)->gso_size);
1876 hdrs += 2;
1877 }
Thomas Falcon0d973382020-11-18 19:12:19 -06001878
1879 if ((*hdrs >> 7) & 1)
Thomas Falconc62aa372020-11-18 19:12:20 -06001880 build_hdr_descs_arr(skb, indir_arr, &num_entries, *hdrs);
Thomas Falcon0d973382020-11-18 19:12:19 -06001881
1882 tx_crq.v1.n_crq_elem = num_entries;
1883 tx_buff->num_entries = num_entries;
1884 /* flush buffer if current entry can not fit */
1885 if (num_entries + ind_bufp->index > IBMVNIC_MAX_IND_DESCS) {
1886 lpar_rc = ibmvnic_tx_scrq_flush(adapter, tx_scrq);
1887 if (lpar_rc != H_SUCCESS)
1888 goto tx_flush_err;
Thomas Falconad7775d2016-04-01 17:20:34 -05001889 }
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001890
Thomas Falconc62aa372020-11-18 19:12:20 -06001891 indir_arr[0] = tx_crq;
1892 memcpy(&ind_bufp->indir_arr[ind_bufp->index], &indir_arr[0],
Thomas Falcon0d973382020-11-18 19:12:19 -06001893 num_entries * sizeof(struct ibmvnic_generic_scrq));
1894 ind_bufp->index += num_entries;
1895 if (__netdev_tx_sent_queue(txq, skb->len,
1896 netdev_xmit_more() &&
1897 ind_bufp->index < IBMVNIC_MAX_IND_DESCS)) {
1898 lpar_rc = ibmvnic_tx_scrq_flush(adapter, tx_scrq);
1899 if (lpar_rc != H_SUCCESS)
1900 goto tx_err;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001901 }
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001902
Thomas Falconffc385b2018-02-18 10:08:41 -06001903 if (atomic_add_return(num_entries, &tx_scrq->used)
Brian King58c8c0c2017-04-19 13:44:47 -04001904 >= adapter->req_tx_entries_per_subcrq) {
Thomas Falcon0aecb132018-02-26 18:10:58 -06001905 netdev_dbg(netdev, "Stopping queue %d\n", queue_num);
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001906 netif_stop_subqueue(netdev, queue_num);
1907 }
1908
Thomas Falcon032c5e82015-12-21 11:26:06 -06001909 tx_packets++;
1910 tx_bytes += skb->len;
1911 txq->trans_start = jiffies;
1912 ret = NETDEV_TX_OK;
Thomas Falcon86b61a52018-03-16 20:00:29 -05001913 goto out;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001914
Thomas Falcon0d973382020-11-18 19:12:19 -06001915tx_flush_err:
1916 dev_kfree_skb_any(skb);
1917 tx_buff->skb = NULL;
1918 tx_pool->consumer_index = tx_pool->consumer_index == 0 ?
1919 tx_pool->num_buffers - 1 :
1920 tx_pool->consumer_index - 1;
1921 tx_dropped++;
1922tx_err:
1923 if (lpar_rc != H_CLOSED && lpar_rc != H_PARAMETER)
1924 dev_err_ratelimited(dev, "tx: send failed\n");
1925
1926 if (lpar_rc == H_CLOSED || adapter->failover_pending) {
1927 /* Disable TX and report carrier off if queue is closed
1928 * or pending failover.
1929 * Firmware guarantees that a signal will be sent to the
1930 * driver, triggering a reset or some other action.
1931 */
1932 netif_tx_stop_all_queues(netdev);
1933 netif_carrier_off(netdev);
1934 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001935out:
1936 netdev->stats.tx_dropped += tx_dropped;
1937 netdev->stats.tx_bytes += tx_bytes;
1938 netdev->stats.tx_packets += tx_packets;
1939 adapter->tx_send_failed += tx_send_failed;
1940 adapter->tx_map_failed += tx_map_failed;
John Allen3d52b592017-08-02 16:44:14 -05001941 adapter->tx_stats_buffers[queue_num].packets += tx_packets;
1942 adapter->tx_stats_buffers[queue_num].bytes += tx_bytes;
1943 adapter->tx_stats_buffers[queue_num].dropped_packets += tx_dropped;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001944
1945 return ret;
1946}
1947
1948static void ibmvnic_set_multi(struct net_device *netdev)
1949{
1950 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1951 struct netdev_hw_addr *ha;
1952 union ibmvnic_crq crq;
1953
1954 memset(&crq, 0, sizeof(crq));
1955 crq.request_capability.first = IBMVNIC_CRQ_CMD;
1956 crq.request_capability.cmd = REQUEST_CAPABILITY;
1957
1958 if (netdev->flags & IFF_PROMISC) {
1959 if (!adapter->promisc_supported)
1960 return;
1961 } else {
1962 if (netdev->flags & IFF_ALLMULTI) {
1963 /* Accept all multicast */
1964 memset(&crq, 0, sizeof(crq));
1965 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1966 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1967 crq.multicast_ctrl.flags = IBMVNIC_ENABLE_ALL;
1968 ibmvnic_send_crq(adapter, &crq);
1969 } else if (netdev_mc_empty(netdev)) {
1970 /* Reject all multicast */
1971 memset(&crq, 0, sizeof(crq));
1972 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1973 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1974 crq.multicast_ctrl.flags = IBMVNIC_DISABLE_ALL;
1975 ibmvnic_send_crq(adapter, &crq);
1976 } else {
1977 /* Accept one or more multicast(s) */
1978 netdev_for_each_mc_addr(ha, netdev) {
1979 memset(&crq, 0, sizeof(crq));
1980 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1981 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1982 crq.multicast_ctrl.flags = IBMVNIC_ENABLE_MC;
1983 ether_addr_copy(&crq.multicast_ctrl.mac_addr[0],
1984 ha->addr);
1985 ibmvnic_send_crq(adapter, &crq);
1986 }
1987 }
1988 }
1989}
1990
Thomas Falcon62740e92019-05-09 23:13:43 -05001991static int __ibmvnic_set_mac(struct net_device *netdev, u8 *dev_addr)
Thomas Falcon032c5e82015-12-21 11:26:06 -06001992{
1993 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001994 union ibmvnic_crq crq;
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05001995 int rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001996
Thomas Falcon62740e92019-05-09 23:13:43 -05001997 if (!is_valid_ether_addr(dev_addr)) {
1998 rc = -EADDRNOTAVAIL;
1999 goto err;
2000 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002001
2002 memset(&crq, 0, sizeof(crq));
2003 crq.change_mac_addr.first = IBMVNIC_CRQ_CMD;
2004 crq.change_mac_addr.cmd = CHANGE_MAC_ADDR;
Thomas Falcon62740e92019-05-09 23:13:43 -05002005 ether_addr_copy(&crq.change_mac_addr.mac_addr[0], dev_addr);
Thomas Falconf8136142018-01-29 13:45:05 -06002006
Thomas Falconff25dcb2019-11-25 17:12:56 -06002007 mutex_lock(&adapter->fw_lock);
2008 adapter->fw_done_rc = 0;
Thomas Falcon070eca92019-11-25 17:12:53 -06002009 reinit_completion(&adapter->fw_done);
Thomas Falconff25dcb2019-11-25 17:12:56 -06002010
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05002011 rc = ibmvnic_send_crq(adapter, &crq);
Thomas Falcon62740e92019-05-09 23:13:43 -05002012 if (rc) {
2013 rc = -EIO;
Thomas Falconff25dcb2019-11-25 17:12:56 -06002014 mutex_unlock(&adapter->fw_lock);
Thomas Falcon62740e92019-05-09 23:13:43 -05002015 goto err;
2016 }
2017
Thomas Falcon476d96c2019-11-25 17:12:55 -06002018 rc = ibmvnic_wait_for_completion(adapter, &adapter->fw_done, 10000);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002019 /* netdev->dev_addr is changed in handle_change_mac_rsp function */
Thomas Falcon476d96c2019-11-25 17:12:55 -06002020 if (rc || adapter->fw_done_rc) {
Thomas Falcon62740e92019-05-09 23:13:43 -05002021 rc = -EIO;
Thomas Falconff25dcb2019-11-25 17:12:56 -06002022 mutex_unlock(&adapter->fw_lock);
Thomas Falcon62740e92019-05-09 23:13:43 -05002023 goto err;
2024 }
Thomas Falconff25dcb2019-11-25 17:12:56 -06002025 mutex_unlock(&adapter->fw_lock);
Thomas Falcon62740e92019-05-09 23:13:43 -05002026 return 0;
2027err:
2028 ether_addr_copy(adapter->mac_addr, netdev->dev_addr);
2029 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002030}
2031
John Allenc26eba02017-10-26 16:23:25 -05002032static int ibmvnic_set_mac(struct net_device *netdev, void *p)
2033{
2034 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2035 struct sockaddr *addr = p;
Thomas Falconf8136142018-01-29 13:45:05 -06002036 int rc;
John Allenc26eba02017-10-26 16:23:25 -05002037
Thomas Falcon62740e92019-05-09 23:13:43 -05002038 rc = 0;
Lijun Pan8fc36722020-10-27 17:04:56 -05002039 if (!is_valid_ether_addr(addr->sa_data))
2040 return -EADDRNOTAVAIL;
2041
Jiri Wiesner67eb2112021-03-04 17:18:28 +01002042 ether_addr_copy(adapter->mac_addr, addr->sa_data);
2043 if (adapter->state != VNIC_PROBED)
Thomas Falcon62740e92019-05-09 23:13:43 -05002044 rc = __ibmvnic_set_mac(netdev, addr->sa_data);
John Allenc26eba02017-10-26 16:23:25 -05002045
Thomas Falconf8136142018-01-29 13:45:05 -06002046 return rc;
John Allenc26eba02017-10-26 16:23:25 -05002047}
2048
Lijun Pancaee7bf2021-04-12 02:41:27 -05002049static const char *reset_reason_to_string(enum ibmvnic_reset_reason reason)
2050{
2051 switch (reason) {
2052 case VNIC_RESET_FAILOVER:
2053 return "FAILOVER";
2054 case VNIC_RESET_MOBILITY:
2055 return "MOBILITY";
2056 case VNIC_RESET_FATAL:
2057 return "FATAL";
2058 case VNIC_RESET_NON_FATAL:
2059 return "NON_FATAL";
2060 case VNIC_RESET_TIMEOUT:
2061 return "TIMEOUT";
2062 case VNIC_RESET_CHANGE_PARAM:
2063 return "CHANGE_PARAM";
Lijun Pan822ebc22021-06-11 10:35:37 -05002064 case VNIC_RESET_PASSIVE_INIT:
2065 return "PASSIVE_INIT";
Lijun Pancaee7bf2021-04-12 02:41:27 -05002066 }
Michal Suchanek07b5dc12021-05-20 08:50:34 +02002067 return "UNKNOWN";
Lijun Pancaee7bf2021-04-12 02:41:27 -05002068}
2069
Lee Jones80708602021-01-15 20:09:03 +00002070/*
Nathan Fontenoted651a12017-05-03 14:04:38 -04002071 * do_reset returns zero if we are able to keep processing reset events, or
2072 * non-zero if we hit a fatal error and must halt.
2073 */
2074static int do_reset(struct ibmvnic_adapter *adapter,
2075 struct ibmvnic_rwi *rwi, u32 reset_state)
2076{
John Allen896d8692018-01-18 16:26:31 -06002077 u64 old_num_rx_queues, old_num_tx_queues;
Thomas Falcon5bf032e2018-11-21 11:17:59 -06002078 u64 old_num_rx_slots, old_num_tx_slots;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002079 struct net_device *netdev = adapter->netdev;
Lijun Pand3a6abc2021-04-14 02:46:15 -05002080 int rc;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002081
Sukadev Bhattiprolu38bd5ce2020-12-04 18:22:35 -08002082 netdev_dbg(adapter->netdev,
Lijun Pan0666ef72021-04-12 02:41:28 -05002083 "[S:%s FOP:%d] Reset reason: %s, reset_state: %s\n",
2084 adapter_state_to_string(adapter->state),
2085 adapter->failover_pending,
2086 reset_reason_to_string(rwi->reset_reason),
2087 adapter_state_to_string(reset_state));
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002088
Lijun Pan3f5ec372021-01-06 15:35:14 -06002089 adapter->reset_reason = rwi->reset_reason;
2090 /* requestor of VNIC_RESET_CHANGE_PARAM already has the rtnl lock */
2091 if (!(adapter->reset_reason == VNIC_RESET_CHANGE_PARAM))
2092 rtnl_lock();
2093
Lijun Panbab08be2021-02-11 00:43:19 -06002094 /* Now that we have the rtnl lock, clear any pending failover.
Sukadev Bhattiprolu1d850492020-10-30 10:07:11 -07002095 * This will ensure ibmvnic_open() has either completed or will
2096 * block until failover is complete.
2097 */
2098 if (rwi->reset_reason == VNIC_RESET_FAILOVER)
2099 adapter->failover_pending = false;
Juliet Kimb27507b2019-09-20 16:11:22 -04002100
Sukadev Bhattiprolu8f1c0fd2021-02-23 21:02:29 -08002101 /* read the state and check (again) after getting rtnl */
2102 reset_state = adapter->state;
2103
2104 if (reset_state == VNIC_REMOVING || reset_state == VNIC_REMOVED) {
2105 rc = -EBUSY;
2106 goto out;
2107 }
2108
Nathan Fontenoted651a12017-05-03 14:04:38 -04002109 netif_carrier_off(netdev);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002110
John Allen896d8692018-01-18 16:26:31 -06002111 old_num_rx_queues = adapter->req_rx_queues;
2112 old_num_tx_queues = adapter->req_tx_queues;
Thomas Falcon5bf032e2018-11-21 11:17:59 -06002113 old_num_rx_slots = adapter->req_rx_add_entries_per_subcrq;
2114 old_num_tx_slots = adapter->req_tx_entries_per_subcrq;
John Allen896d8692018-01-18 16:26:31 -06002115
Nathan Fontenot30f79622018-04-06 18:37:06 -05002116 ibmvnic_cleanup(netdev);
2117
Thomas Falcon1f946082019-06-07 16:03:53 -05002118 if (reset_state == VNIC_OPEN &&
2119 adapter->reset_reason != VNIC_RESET_MOBILITY &&
Nathan Fontenot30f79622018-04-06 18:37:06 -05002120 adapter->reset_reason != VNIC_RESET_FAILOVER) {
Lijun Pan3f5ec372021-01-06 15:35:14 -06002121 if (adapter->reset_reason == VNIC_RESET_CHANGE_PARAM) {
2122 rc = __ibmvnic_close(netdev);
2123 if (rc)
2124 goto out;
2125 } else {
2126 adapter->state = VNIC_CLOSING;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002127
Lijun Pan3f5ec372021-01-06 15:35:14 -06002128 /* Release the RTNL lock before link state change and
2129 * re-acquire after the link state change to allow
2130 * linkwatch_event to grab the RTNL lock and run during
2131 * a reset.
2132 */
2133 rtnl_unlock();
2134 rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_DN);
2135 rtnl_lock();
2136 if (rc)
2137 goto out;
Juliet Kimb27507b2019-09-20 16:11:22 -04002138
Sukadev Bhattiprolu8f1c0fd2021-02-23 21:02:29 -08002139 if (adapter->state == VNIC_OPEN) {
2140 /* When we dropped rtnl, ibmvnic_open() got
2141 * it and noticed that we are resetting and
2142 * set the adapter state to OPEN. Update our
2143 * new "target" state, and resume the reset
2144 * from VNIC_CLOSING state.
2145 */
2146 netdev_dbg(netdev,
Lijun Pan0666ef72021-04-12 02:41:28 -05002147 "Open changed state from %s, updating.\n",
2148 adapter_state_to_string(reset_state));
Sukadev Bhattiprolu8f1c0fd2021-02-23 21:02:29 -08002149 reset_state = VNIC_OPEN;
2150 adapter->state = VNIC_CLOSING;
2151 }
2152
Lijun Pan3f5ec372021-01-06 15:35:14 -06002153 if (adapter->state != VNIC_CLOSING) {
Sukadev Bhattiprolu8f1c0fd2021-02-23 21:02:29 -08002154 /* If someone else changed the adapter state
2155 * when we dropped the rtnl, fail the reset
2156 */
Lijun Pan3f5ec372021-01-06 15:35:14 -06002157 rc = -1;
2158 goto out;
2159 }
Lijun Pan3f5ec372021-01-06 15:35:14 -06002160 adapter->state = VNIC_CLOSED;
Juliet Kimb27507b2019-09-20 16:11:22 -04002161 }
Lijun Pan3f5ec372021-01-06 15:35:14 -06002162 }
Juliet Kimb27507b2019-09-20 16:11:22 -04002163
Lijun Pan3f5ec372021-01-06 15:35:14 -06002164 if (adapter->reset_reason == VNIC_RESET_CHANGE_PARAM) {
2165 release_resources(adapter);
2166 release_sub_crqs(adapter, 1);
2167 release_crq_queue(adapter);
John Allenc26eba02017-10-26 16:23:25 -05002168 }
2169
John Allen8cb31cf2017-05-26 10:30:37 -04002170 if (adapter->reset_reason != VNIC_RESET_NON_FATAL) {
2171 /* remove the closed state so when we call open it appears
2172 * we are coming from the probed state.
2173 */
Nathan Fontenoted651a12017-05-03 14:04:38 -04002174 adapter->state = VNIC_PROBED;
John Allen8cb31cf2017-05-26 10:30:37 -04002175
Lijun Pan3f5ec372021-01-06 15:35:14 -06002176 if (adapter->reset_reason == VNIC_RESET_CHANGE_PARAM) {
2177 rc = init_crq_queue(adapter);
2178 } else if (adapter->reset_reason == VNIC_RESET_MOBILITY) {
Nathan Fontenot30f79622018-04-06 18:37:06 -05002179 rc = ibmvnic_reenable_crq_queue(adapter);
2180 release_sub_crqs(adapter, 1);
2181 } else {
2182 rc = ibmvnic_reset_crq(adapter);
Dany Madden8b40eb732020-06-18 15:24:13 -04002183 if (rc == H_CLOSED || rc == H_SUCCESS) {
Nathan Fontenot30f79622018-04-06 18:37:06 -05002184 rc = vio_enable_interrupts(adapter->vdev);
Dany Madden8b40eb732020-06-18 15:24:13 -04002185 if (rc)
2186 netdev_err(adapter->netdev,
2187 "Reset failed to enable interrupts. rc=%d\n",
2188 rc);
2189 }
Nathan Fontenot30f79622018-04-06 18:37:06 -05002190 }
2191
2192 if (rc) {
2193 netdev_err(adapter->netdev,
Dany Madden8b40eb732020-06-18 15:24:13 -04002194 "Reset couldn't initialize crq. rc=%d\n", rc);
Juliet Kimb27507b2019-09-20 16:11:22 -04002195 goto out;
Nathan Fontenot30f79622018-04-06 18:37:06 -05002196 }
2197
Lijun Pan635e4422020-08-19 17:52:26 -05002198 rc = ibmvnic_reset_init(adapter, true);
Juliet Kimb27507b2019-09-20 16:11:22 -04002199 if (rc) {
2200 rc = IBMVNIC_INIT_FAILED;
2201 goto out;
2202 }
John Allen8cb31cf2017-05-26 10:30:37 -04002203
Cristobal Forno53f8b1b2021-06-10 11:08:35 -06002204 /* If the adapter was in PROBE or DOWN state prior to the reset,
John Allen8cb31cf2017-05-26 10:30:37 -04002205 * exit here.
2206 */
Cristobal Forno53f8b1b2021-06-10 11:08:35 -06002207 if (reset_state == VNIC_PROBED || reset_state == VNIC_DOWN) {
Juliet Kimb27507b2019-09-20 16:11:22 -04002208 rc = 0;
2209 goto out;
2210 }
John Allen8cb31cf2017-05-26 10:30:37 -04002211
2212 rc = ibmvnic_login(netdev);
Lijun Panf78afaa2021-02-11 00:43:20 -06002213 if (rc)
Juliet Kimb27507b2019-09-20 16:11:22 -04002214 goto out;
John Allen8cb31cf2017-05-26 10:30:37 -04002215
Lijun Pan3f5ec372021-01-06 15:35:14 -06002216 if (adapter->reset_reason == VNIC_RESET_CHANGE_PARAM) {
2217 rc = init_resources(adapter);
2218 if (rc)
2219 goto out;
2220 } else if (adapter->req_rx_queues != old_num_rx_queues ||
Juliet Kimb27507b2019-09-20 16:11:22 -04002221 adapter->req_tx_queues != old_num_tx_queues ||
2222 adapter->req_rx_add_entries_per_subcrq !=
2223 old_num_rx_slots ||
2224 adapter->req_tx_entries_per_subcrq !=
Mingming Cao9f134572020-08-25 13:26:41 -04002225 old_num_tx_slots ||
2226 !adapter->rx_pool ||
2227 !adapter->tso_pool ||
2228 !adapter->tx_pool) {
John Allen896d8692018-01-18 16:26:31 -06002229 release_rx_pools(adapter);
2230 release_tx_pools(adapter);
Juliet Kima5681e22018-11-19 15:59:22 -06002231 release_napi(adapter);
2232 release_vpd_data(adapter);
2233
2234 rc = init_resources(adapter);
Thomas Falconf611a5b2018-08-30 13:19:53 -05002235 if (rc)
Juliet Kimb27507b2019-09-20 16:11:22 -04002236 goto out;
Nathan Fontenotd9043c12018-02-19 13:30:14 -06002237
John Allenc26eba02017-10-26 16:23:25 -05002238 } else {
2239 rc = reset_tx_pools(adapter);
Jakub Kicinski8ae4dff2020-09-04 21:07:49 -07002240 if (rc) {
Mingming Cao9f134572020-08-25 13:26:41 -04002241 netdev_dbg(adapter->netdev, "reset tx pools failed (%d)\n",
Lijun Pan91dc5d22021-02-11 00:43:22 -06002242 rc);
Juliet Kimb27507b2019-09-20 16:11:22 -04002243 goto out;
Jakub Kicinski8ae4dff2020-09-04 21:07:49 -07002244 }
Nathan Fontenot8c0543a2017-05-26 10:31:06 -04002245
John Allenc26eba02017-10-26 16:23:25 -05002246 rc = reset_rx_pools(adapter);
Jakub Kicinski8ae4dff2020-09-04 21:07:49 -07002247 if (rc) {
Mingming Cao9f134572020-08-25 13:26:41 -04002248 netdev_dbg(adapter->netdev, "reset rx pools failed (%d)\n",
Lijun Pan91dc5d22021-02-11 00:43:22 -06002249 rc);
Juliet Kimb27507b2019-09-20 16:11:22 -04002250 goto out;
Jakub Kicinski8ae4dff2020-09-04 21:07:49 -07002251 }
John Allenc26eba02017-10-26 16:23:25 -05002252 }
Thomas Falcon134bbe72018-05-16 15:49:04 -05002253 ibmvnic_disable_irqs(adapter);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002254 }
John Allene676d812018-03-14 10:41:29 -05002255 adapter->state = VNIC_CLOSED;
2256
Juliet Kimb27507b2019-09-20 16:11:22 -04002257 if (reset_state == VNIC_CLOSED) {
2258 rc = 0;
2259 goto out;
2260 }
John Allene676d812018-03-14 10:41:29 -05002261
Nathan Fontenoted651a12017-05-03 14:04:38 -04002262 rc = __ibmvnic_open(netdev);
2263 if (rc) {
Juliet Kimb27507b2019-09-20 16:11:22 -04002264 rc = IBMVNIC_OPEN_FAILED;
2265 goto out;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002266 }
2267
Thomas Falconbe32a242019-06-07 16:03:54 -05002268 /* refresh device's multicast list */
2269 ibmvnic_set_multi(netdev);
2270
Lijun Pan98025bc2020-11-20 16:40:12 -06002271 if (adapter->reset_reason == VNIC_RESET_FAILOVER ||
Lijun Pan6be46662020-12-14 15:19:29 -06002272 adapter->reset_reason == VNIC_RESET_MOBILITY)
2273 __netdev_notify_peers(netdev);
Nathan Fontenot61d3e1d2017-06-12 20:47:45 -04002274
Juliet Kimb27507b2019-09-20 16:11:22 -04002275 rc = 0;
2276
2277out:
Dany Madden0cb4bc62020-11-25 18:04:27 -06002278 /* restore the adapter state if reset failed */
2279 if (rc)
2280 adapter->state = reset_state;
Lijun Pan3f5ec372021-01-06 15:35:14 -06002281 /* requestor of VNIC_RESET_CHANGE_PARAM should still hold the rtnl lock */
2282 if (!(adapter->reset_reason == VNIC_RESET_CHANGE_PARAM))
2283 rtnl_unlock();
Juliet Kimb27507b2019-09-20 16:11:22 -04002284
Lijun Pan0666ef72021-04-12 02:41:28 -05002285 netdev_dbg(adapter->netdev, "[S:%s FOP:%d] Reset done, rc %d\n",
2286 adapter_state_to_string(adapter->state),
2287 adapter->failover_pending, rc);
Juliet Kimb27507b2019-09-20 16:11:22 -04002288 return rc;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002289}
2290
Thomas Falcon2770a792018-05-23 13:38:02 -05002291static int do_hard_reset(struct ibmvnic_adapter *adapter,
2292 struct ibmvnic_rwi *rwi, u32 reset_state)
2293{
2294 struct net_device *netdev = adapter->netdev;
2295 int rc;
2296
Lijun Pancaee7bf2021-04-12 02:41:27 -05002297 netdev_dbg(adapter->netdev, "Hard resetting driver (%s)\n",
2298 reset_reason_to_string(rwi->reset_reason));
Thomas Falcon2770a792018-05-23 13:38:02 -05002299
Sukadev Bhattiprolu8f1c0fd2021-02-23 21:02:29 -08002300 /* read the state and check (again) after getting rtnl */
2301 reset_state = adapter->state;
2302
2303 if (reset_state == VNIC_REMOVING || reset_state == VNIC_REMOVED) {
2304 rc = -EBUSY;
2305 goto out;
2306 }
2307
Thomas Falcon2770a792018-05-23 13:38:02 -05002308 netif_carrier_off(netdev);
2309 adapter->reset_reason = rwi->reset_reason;
2310
2311 ibmvnic_cleanup(netdev);
2312 release_resources(adapter);
2313 release_sub_crqs(adapter, 0);
2314 release_crq_queue(adapter);
2315
2316 /* remove the closed state so when we call open it appears
2317 * we are coming from the probed state.
2318 */
2319 adapter->state = VNIC_PROBED;
2320
Thomas Falconbbd669a2019-04-04 18:58:26 -05002321 reinit_completion(&adapter->init_done);
Thomas Falcon2770a792018-05-23 13:38:02 -05002322 rc = init_crq_queue(adapter);
2323 if (rc) {
2324 netdev_err(adapter->netdev,
2325 "Couldn't initialize crq. rc=%d\n", rc);
Dany Madden0cb4bc62020-11-25 18:04:27 -06002326 goto out;
Thomas Falcon2770a792018-05-23 13:38:02 -05002327 }
2328
Lijun Pan635e4422020-08-19 17:52:26 -05002329 rc = ibmvnic_reset_init(adapter, false);
Thomas Falcon2770a792018-05-23 13:38:02 -05002330 if (rc)
Dany Madden0cb4bc62020-11-25 18:04:27 -06002331 goto out;
Thomas Falcon2770a792018-05-23 13:38:02 -05002332
Cristobal Forno53f8b1b2021-06-10 11:08:35 -06002333 /* If the adapter was in PROBE or DOWN state prior to the reset,
Thomas Falcon2770a792018-05-23 13:38:02 -05002334 * exit here.
2335 */
Cristobal Forno53f8b1b2021-06-10 11:08:35 -06002336 if (reset_state == VNIC_PROBED || reset_state == VNIC_DOWN)
Dany Madden0cb4bc62020-11-25 18:04:27 -06002337 goto out;
Thomas Falcon2770a792018-05-23 13:38:02 -05002338
2339 rc = ibmvnic_login(netdev);
Dany Madden0cb4bc62020-11-25 18:04:27 -06002340 if (rc)
2341 goto out;
Juliet Kima5681e22018-11-19 15:59:22 -06002342
2343 rc = init_resources(adapter);
Thomas Falcon2770a792018-05-23 13:38:02 -05002344 if (rc)
Dany Madden0cb4bc62020-11-25 18:04:27 -06002345 goto out;
Thomas Falcon2770a792018-05-23 13:38:02 -05002346
2347 ibmvnic_disable_irqs(adapter);
2348 adapter->state = VNIC_CLOSED;
2349
2350 if (reset_state == VNIC_CLOSED)
Dany Madden0cb4bc62020-11-25 18:04:27 -06002351 goto out;
Thomas Falcon2770a792018-05-23 13:38:02 -05002352
2353 rc = __ibmvnic_open(netdev);
Dany Madden0cb4bc62020-11-25 18:04:27 -06002354 if (rc) {
2355 rc = IBMVNIC_OPEN_FAILED;
2356 goto out;
2357 }
Thomas Falcon2770a792018-05-23 13:38:02 -05002358
Lijun Pan6be46662020-12-14 15:19:29 -06002359 __netdev_notify_peers(netdev);
Dany Madden0cb4bc62020-11-25 18:04:27 -06002360out:
2361 /* restore adapter state if reset failed */
2362 if (rc)
2363 adapter->state = reset_state;
Lijun Pan0666ef72021-04-12 02:41:28 -05002364 netdev_dbg(adapter->netdev, "[S:%s FOP:%d] Hard reset done, rc %d\n",
2365 adapter_state_to_string(adapter->state),
2366 adapter->failover_pending, rc);
Dany Madden0cb4bc62020-11-25 18:04:27 -06002367 return rc;
Thomas Falcon2770a792018-05-23 13:38:02 -05002368}
2369
Nathan Fontenoted651a12017-05-03 14:04:38 -04002370static struct ibmvnic_rwi *get_next_rwi(struct ibmvnic_adapter *adapter)
2371{
2372 struct ibmvnic_rwi *rwi;
Thomas Falcon6c5c7482018-12-10 15:22:22 -06002373 unsigned long flags;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002374
Thomas Falcon6c5c7482018-12-10 15:22:22 -06002375 spin_lock_irqsave(&adapter->rwi_lock, flags);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002376
2377 if (!list_empty(&adapter->rwi_list)) {
2378 rwi = list_first_entry(&adapter->rwi_list, struct ibmvnic_rwi,
2379 list);
2380 list_del(&rwi->list);
2381 } else {
2382 rwi = NULL;
2383 }
2384
Thomas Falcon6c5c7482018-12-10 15:22:22 -06002385 spin_unlock_irqrestore(&adapter->rwi_lock, flags);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002386 return rwi;
2387}
2388
Cristobal Forno53f8b1b2021-06-10 11:08:35 -06002389/**
2390 * do_passive_init - complete probing when partner device is detected.
2391 * @adapter: ibmvnic_adapter struct
2392 *
2393 * If the ibmvnic device does not have a partner device to communicate with at boot
2394 * and that partner device comes online at a later time, this function is called
2395 * to complete the initialization process of ibmvnic device.
2396 * Caller is expected to hold rtnl_lock().
2397 *
2398 * Returns non-zero if sub-CRQs are not initialized properly leaving the device
2399 * in the down state.
2400 * Returns 0 upon success and the device is in PROBED state.
2401 */
2402
2403static int do_passive_init(struct ibmvnic_adapter *adapter)
2404{
2405 unsigned long timeout = msecs_to_jiffies(30000);
2406 struct net_device *netdev = adapter->netdev;
2407 struct device *dev = &adapter->vdev->dev;
2408 int rc;
2409
2410 netdev_dbg(netdev, "Partner device found, probing.\n");
2411
2412 adapter->state = VNIC_PROBING;
2413 reinit_completion(&adapter->init_done);
2414 adapter->init_done_rc = 0;
2415 adapter->crq.active = true;
2416
2417 rc = send_crq_init_complete(adapter);
2418 if (rc)
2419 goto out;
2420
2421 rc = send_version_xchg(adapter);
2422 if (rc)
2423 netdev_dbg(adapter->netdev, "send_version_xchg failed, rc=%d\n", rc);
2424
2425 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
2426 dev_err(dev, "Initialization sequence timed out\n");
2427 rc = -ETIMEDOUT;
2428 goto out;
2429 }
2430
2431 rc = init_sub_crqs(adapter);
2432 if (rc) {
2433 dev_err(dev, "Initialization of sub crqs failed, rc=%d\n", rc);
2434 goto out;
2435 }
2436
2437 rc = init_sub_crq_irqs(adapter);
2438 if (rc) {
2439 dev_err(dev, "Failed to initialize sub crq irqs\n, rc=%d", rc);
2440 goto init_failed;
2441 }
2442
2443 netdev->mtu = adapter->req_mtu - ETH_HLEN;
2444 netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
2445 netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
2446
2447 adapter->state = VNIC_PROBED;
2448 netdev_dbg(netdev, "Probed successfully. Waiting for signal from partner device.\n");
2449
2450 return 0;
2451
2452init_failed:
2453 release_sub_crqs(adapter, 1);
2454out:
2455 adapter->state = VNIC_DOWN;
2456 return rc;
2457}
2458
Nathan Fontenoted651a12017-05-03 14:04:38 -04002459static void __ibmvnic_reset(struct work_struct *work)
2460{
Nathan Fontenoted651a12017-05-03 14:04:38 -04002461 struct ibmvnic_adapter *adapter;
Juliet Kim7d7195a2020-03-10 09:23:58 -05002462 bool saved_state = false;
Sukadev Bhattiprolu4f408e12021-06-30 14:36:17 -04002463 struct ibmvnic_rwi *tmprwi;
2464 struct ibmvnic_rwi *rwi;
Juliet Kim7d7195a2020-03-10 09:23:58 -05002465 unsigned long flags;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002466 u32 reset_state;
John Allenc26eba02017-10-26 16:23:25 -05002467 int rc = 0;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002468
2469 adapter = container_of(work, struct ibmvnic_adapter, ibmvnic_reset);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002470
Juliet Kim7ed5b312019-09-20 16:11:23 -04002471 if (test_and_set_bit_lock(0, &adapter->resetting)) {
Lijun Pan870e04a2021-04-13 14:33:39 -05002472 queue_delayed_work(system_long_wq,
2473 &adapter->ibmvnic_delayed_reset,
2474 IBMVNIC_RESET_DELAY);
Juliet Kim7ed5b312019-09-20 16:11:23 -04002475 return;
2476 }
2477
Nathan Fontenoted651a12017-05-03 14:04:38 -04002478 rwi = get_next_rwi(adapter);
2479 while (rwi) {
Juliet Kim7d7195a2020-03-10 09:23:58 -05002480 spin_lock_irqsave(&adapter->state_lock, flags);
2481
Thomas Falcon36f10312019-08-27 11:10:04 -05002482 if (adapter->state == VNIC_REMOVING ||
Michal Suchanekc8dc5592019-09-09 22:44:51 +02002483 adapter->state == VNIC_REMOVED) {
Juliet Kim7d7195a2020-03-10 09:23:58 -05002484 spin_unlock_irqrestore(&adapter->state_lock, flags);
Juliet Kim1c2977c2019-09-05 17:30:01 -04002485 kfree(rwi);
2486 rc = EBUSY;
2487 break;
2488 }
Thomas Falcon36f10312019-08-27 11:10:04 -05002489
Juliet Kim7d7195a2020-03-10 09:23:58 -05002490 if (!saved_state) {
2491 reset_state = adapter->state;
Juliet Kim7d7195a2020-03-10 09:23:58 -05002492 saved_state = true;
2493 }
2494 spin_unlock_irqrestore(&adapter->state_lock, flags);
2495
Cristobal Forno53f8b1b2021-06-10 11:08:35 -06002496 if (rwi->reset_reason == VNIC_RESET_PASSIVE_INIT) {
2497 rtnl_lock();
2498 rc = do_passive_init(adapter);
2499 rtnl_unlock();
2500 if (!rc)
2501 netif_carrier_on(adapter->netdev);
2502 } else if (adapter->force_reset_recovery) {
Lijun Panbab08be2021-02-11 00:43:19 -06002503 /* Since we are doing a hard reset now, clear the
Sukadev Bhattiprolu1d850492020-10-30 10:07:11 -07002504 * failover_pending flag so we don't ignore any
2505 * future MOBILITY or other resets.
2506 */
2507 adapter->failover_pending = false;
2508
Juliet Kimb27507b2019-09-20 16:11:22 -04002509 /* Transport event occurred during previous reset */
2510 if (adapter->wait_for_reset) {
2511 /* Previous was CHANGE_PARAM; caller locked */
2512 adapter->force_reset_recovery = false;
2513 rc = do_hard_reset(adapter, rwi, reset_state);
2514 } else {
2515 rtnl_lock();
2516 adapter->force_reset_recovery = false;
2517 rc = do_hard_reset(adapter, rwi, reset_state);
2518 rtnl_unlock();
2519 }
Sukadev Bhattiproluf15fde92020-11-25 18:04:28 -06002520 if (rc) {
2521 /* give backing device time to settle down */
2522 netdev_dbg(adapter->netdev,
Lijun Pan0666ef72021-04-12 02:41:28 -05002523 "[S:%s] Hard reset failed, waiting 60 secs\n",
2524 adapter_state_to_string(adapter->state));
Sukadev Bhattiproluf15fde92020-11-25 18:04:28 -06002525 set_current_state(TASK_UNINTERRUPTIBLE);
2526 schedule_timeout(60 * HZ);
2527 }
Lijun Pan1f45dc22020-12-23 14:49:04 -06002528 } else {
Thomas Falcon2770a792018-05-23 13:38:02 -05002529 rc = do_reset(adapter, rwi, reset_state);
2530 }
Sukadev Bhattiprolu4f408e12021-06-30 14:36:17 -04002531 tmprwi = rwi;
Dany Maddena86d5c62020-11-25 18:04:31 -06002532 adapter->last_reset_time = jiffies;
Dany Madden0cb4bc62020-11-25 18:04:27 -06002533
Dany Madden18f141b2020-11-25 18:04:25 -06002534 if (rc)
2535 netdev_dbg(adapter->netdev, "Reset failed, rc=%d\n", rc);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002536
2537 rwi = get_next_rwi(adapter);
Juliet Kim7ed5b312019-09-20 16:11:23 -04002538
Sukadev Bhattiprolu4f408e12021-06-30 14:36:17 -04002539 /*
2540 * If there is another reset queued, free the previous rwi
2541 * and process the new reset even if previous reset failed
2542 * (the previous reset could have failed because of a fail
2543 * over for instance, so process the fail over).
2544 *
2545 * If there are no resets queued and the previous reset failed,
2546 * the adapter would be in an undefined state. So retry the
2547 * previous reset as a hard reset.
2548 */
2549 if (rwi)
2550 kfree(tmprwi);
2551 else if (rc)
2552 rwi = tmprwi;
2553
Juliet Kim7ed5b312019-09-20 16:11:23 -04002554 if (rwi && (rwi->reset_reason == VNIC_RESET_FAILOVER ||
Sukadev Bhattiprolu4f408e12021-06-30 14:36:17 -04002555 rwi->reset_reason == VNIC_RESET_MOBILITY || rc))
Juliet Kim7ed5b312019-09-20 16:11:23 -04002556 adapter->force_reset_recovery = true;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002557 }
2558
John Allenc26eba02017-10-26 16:23:25 -05002559 if (adapter->wait_for_reset) {
John Allenc26eba02017-10-26 16:23:25 -05002560 adapter->reset_done_rc = rc;
2561 complete(&adapter->reset_done);
2562 }
2563
Juliet Kim7ed5b312019-09-20 16:11:23 -04002564 clear_bit_unlock(0, &adapter->resetting);
Sukadev Bhattiprolu38bd5ce2020-12-04 18:22:35 -08002565
2566 netdev_dbg(adapter->netdev,
Lijun Pan0666ef72021-04-12 02:41:28 -05002567 "[S:%s FRR:%d WFR:%d] Done processing resets\n",
2568 adapter_state_to_string(adapter->state),
2569 adapter->force_reset_recovery,
Sukadev Bhattiprolu38bd5ce2020-12-04 18:22:35 -08002570 adapter->wait_for_reset);
Juliet Kim7ed5b312019-09-20 16:11:23 -04002571}
2572
2573static void __ibmvnic_delayed_reset(struct work_struct *work)
2574{
2575 struct ibmvnic_adapter *adapter;
2576
2577 adapter = container_of(work, struct ibmvnic_adapter,
2578 ibmvnic_delayed_reset.work);
2579 __ibmvnic_reset(&adapter->ibmvnic_reset);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002580}
2581
Thomas Falconaf894d22018-04-06 18:37:04 -05002582static int ibmvnic_reset(struct ibmvnic_adapter *adapter,
2583 enum ibmvnic_reset_reason reason)
Nathan Fontenoted651a12017-05-03 14:04:38 -04002584{
Thomas Falcon2770a792018-05-23 13:38:02 -05002585 struct list_head *entry, *tmp_entry;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002586 struct ibmvnic_rwi *rwi, *tmp;
2587 struct net_device *netdev = adapter->netdev;
Thomas Falcon6c5c7482018-12-10 15:22:22 -06002588 unsigned long flags;
Thomas Falconaf894d22018-04-06 18:37:04 -05002589 int ret;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002590
Jakub Kicinskib646acd52021-02-16 22:58:44 -08002591 spin_lock_irqsave(&adapter->rwi_lock, flags);
2592
2593 /* If failover is pending don't schedule any other reset.
Sukadev Bhattiprolu1d850492020-10-30 10:07:11 -07002594 * Instead let the failover complete. If there is already a
2595 * a failover reset scheduled, we will detect and drop the
2596 * duplicate reset when walking the ->rwi_list below.
2597 */
Nathan Fontenoted651a12017-05-03 14:04:38 -04002598 if (adapter->state == VNIC_REMOVING ||
Thomas Falcon5a18e1e2018-04-06 18:37:05 -05002599 adapter->state == VNIC_REMOVED ||
Sukadev Bhattiprolu1d850492020-10-30 10:07:11 -07002600 (adapter->failover_pending && reason != VNIC_RESET_FAILOVER)) {
Thomas Falconaf894d22018-04-06 18:37:04 -05002601 ret = EBUSY;
Thomas Falcon5a18e1e2018-04-06 18:37:05 -05002602 netdev_dbg(netdev, "Adapter removing or pending failover, skipping reset\n");
Thomas Falconaf894d22018-04-06 18:37:04 -05002603 goto err;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002604 }
2605
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04002606 if (adapter->state == VNIC_PROBING) {
2607 netdev_warn(netdev, "Adapter reset during probe\n");
Lijun Pan91dc5d22021-02-11 00:43:22 -06002608 adapter->init_done_rc = EAGAIN;
2609 ret = EAGAIN;
Thomas Falconaf894d22018-04-06 18:37:04 -05002610 goto err;
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04002611 }
2612
Wang Hai3e98ae02021-06-10 20:54:17 +08002613 list_for_each_entry(tmp, &adapter->rwi_list, list) {
Nathan Fontenoted651a12017-05-03 14:04:38 -04002614 if (tmp->reset_reason == reason) {
Lijun Pancaee7bf2021-04-12 02:41:27 -05002615 netdev_dbg(netdev, "Skipping matching reset, reason=%s\n",
2616 reset_reason_to_string(reason));
Thomas Falconaf894d22018-04-06 18:37:04 -05002617 ret = EBUSY;
2618 goto err;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002619 }
2620 }
2621
Thomas Falcon1d1bbc32018-12-10 15:22:23 -06002622 rwi = kzalloc(sizeof(*rwi), GFP_ATOMIC);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002623 if (!rwi) {
Thomas Falconaf894d22018-04-06 18:37:04 -05002624 ret = ENOMEM;
2625 goto err;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002626 }
Thomas Falcon2770a792018-05-23 13:38:02 -05002627 /* if we just received a transport event,
2628 * flush reset queue and process this reset
2629 */
2630 if (adapter->force_reset_recovery && !list_empty(&adapter->rwi_list)) {
2631 list_for_each_safe(entry, tmp_entry, &adapter->rwi_list)
2632 list_del(entry);
2633 }
Nathan Fontenoted651a12017-05-03 14:04:38 -04002634 rwi->reset_reason = reason;
2635 list_add_tail(&rwi->list, &adapter->rwi_list);
Lijun Pancaee7bf2021-04-12 02:41:27 -05002636 netdev_dbg(adapter->netdev, "Scheduling reset (reason %s)\n",
2637 reset_reason_to_string(reason));
Lijun Pan870e04a2021-04-13 14:33:39 -05002638 queue_work(system_long_wq, &adapter->ibmvnic_reset);
Thomas Falconaf894d22018-04-06 18:37:04 -05002639
Sukadev Bhattiprolu4a41c422021-02-12 20:42:50 -08002640 ret = 0;
Thomas Falconaf894d22018-04-06 18:37:04 -05002641err:
Sukadev Bhattiprolu4a41c422021-02-12 20:42:50 -08002642 /* ibmvnic_close() below can block, so drop the lock first */
2643 spin_unlock_irqrestore(&adapter->rwi_lock, flags);
2644
2645 if (ret == ENOMEM)
2646 ibmvnic_close(netdev);
2647
Thomas Falconaf894d22018-04-06 18:37:04 -05002648 return -ret;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002649}
2650
Michael S. Tsirkin0290bd22019-12-10 09:23:51 -05002651static void ibmvnic_tx_timeout(struct net_device *dev, unsigned int txqueue)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002652{
2653 struct ibmvnic_adapter *adapter = netdev_priv(dev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002654
Lijun Pan855a6312020-11-20 16:40:13 -06002655 if (test_bit(0, &adapter->resetting)) {
2656 netdev_err(adapter->netdev,
2657 "Adapter is resetting, skip timeout reset\n");
2658 return;
2659 }
Dany Maddena86d5c62020-11-25 18:04:31 -06002660 /* No queuing up reset until at least 5 seconds (default watchdog val)
2661 * after last reset
2662 */
2663 if (time_before(jiffies, (adapter->last_reset_time + dev->watchdog_timeo))) {
2664 netdev_dbg(dev, "Not yet time to tx timeout.\n");
2665 return;
2666 }
Nathan Fontenoted651a12017-05-03 14:04:38 -04002667 ibmvnic_reset(adapter, VNIC_RESET_TIMEOUT);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002668}
2669
2670static void remove_buff_from_pool(struct ibmvnic_adapter *adapter,
2671 struct ibmvnic_rx_buff *rx_buff)
2672{
2673 struct ibmvnic_rx_pool *pool = &adapter->rx_pool[rx_buff->pool_index];
2674
2675 rx_buff->skb = NULL;
2676
2677 pool->free_map[pool->next_alloc] = (int)(rx_buff - pool->rx_buff);
2678 pool->next_alloc = (pool->next_alloc + 1) % pool->size;
2679
2680 atomic_dec(&pool->available);
2681}
2682
2683static int ibmvnic_poll(struct napi_struct *napi, int budget)
2684{
Dwip N. Banerjeeec20f362020-11-18 19:12:23 -06002685 struct ibmvnic_sub_crq_queue *rx_scrq;
2686 struct ibmvnic_adapter *adapter;
2687 struct net_device *netdev;
2688 int frames_processed;
2689 int scrq_num;
2690
2691 netdev = napi->dev;
2692 adapter = netdev_priv(netdev);
2693 scrq_num = (int)(napi - adapter->napi);
2694 frames_processed = 0;
2695 rx_scrq = adapter->rx_scrq[scrq_num];
Nathan Fontenot152ce472017-05-26 10:30:54 -04002696
Thomas Falcon032c5e82015-12-21 11:26:06 -06002697restart_poll:
2698 while (frames_processed < budget) {
2699 struct sk_buff *skb;
2700 struct ibmvnic_rx_buff *rx_buff;
2701 union sub_crq *next;
2702 u32 length;
2703 u16 offset;
2704 u8 flags = 0;
2705
Juliet Kim7ed5b312019-09-20 16:11:23 -04002706 if (unlikely(test_bit(0, &adapter->resetting) &&
John Allen34686562018-02-06 16:21:49 -06002707 adapter->reset_reason != VNIC_RESET_NON_FATAL)) {
Dwip N. Banerjeeec20f362020-11-18 19:12:23 -06002708 enable_scrq_irq(adapter, rx_scrq);
Thomas Falcon21ecba62017-06-14 23:50:09 -05002709 napi_complete_done(napi, frames_processed);
2710 return frames_processed;
2711 }
2712
Dwip N. Banerjeeec20f362020-11-18 19:12:23 -06002713 if (!pending_scrq(adapter, rx_scrq))
Thomas Falcon032c5e82015-12-21 11:26:06 -06002714 break;
Dwip N. Banerjeeec20f362020-11-18 19:12:23 -06002715 next = ibmvnic_next_scrq(adapter, rx_scrq);
Lijun Pan914789a2021-02-11 00:43:21 -06002716 rx_buff = (struct ibmvnic_rx_buff *)
2717 be64_to_cpu(next->rx_comp.correlator);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002718 /* do error checking */
2719 if (next->rx_comp.rc) {
John Allene1cea2e2017-08-07 15:42:30 -05002720 netdev_dbg(netdev, "rx buffer returned with rc %x\n",
2721 be16_to_cpu(next->rx_comp.rc));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002722 /* free the entry */
2723 next->rx_comp.first = 0;
Thomas Falcon4b9b0f02018-02-13 18:23:42 -06002724 dev_kfree_skb_any(rx_buff->skb);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002725 remove_buff_from_pool(adapter, rx_buff);
Nathan Fontenotca05e312017-05-03 14:05:14 -04002726 continue;
Thomas Falconabe27a82018-02-19 20:12:57 -06002727 } else if (!rx_buff->skb) {
2728 /* free the entry */
2729 next->rx_comp.first = 0;
2730 remove_buff_from_pool(adapter, rx_buff);
2731 continue;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002732 }
2733
2734 length = be32_to_cpu(next->rx_comp.len);
2735 offset = be16_to_cpu(next->rx_comp.off_frame_data);
2736 flags = next->rx_comp.flags;
2737 skb = rx_buff->skb;
Lijun Pan42557da2021-02-12 20:48:40 -06002738 /* load long_term_buff before copying to skb */
2739 dma_rmb();
Thomas Falcon032c5e82015-12-21 11:26:06 -06002740 skb_copy_to_linear_data(skb, rx_buff->data + offset,
2741 length);
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04002742
2743 /* VLAN Header has been stripped by the system firmware and
2744 * needs to be inserted by the driver
2745 */
2746 if (adapter->rx_vlan_header_insertion &&
2747 (flags & IBMVNIC_VLAN_STRIPPED))
2748 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
2749 ntohs(next->rx_comp.vlan_tci));
2750
Thomas Falcon032c5e82015-12-21 11:26:06 -06002751 /* free the entry */
2752 next->rx_comp.first = 0;
2753 remove_buff_from_pool(adapter, rx_buff);
2754
2755 skb_put(skb, length);
2756 skb->protocol = eth_type_trans(skb, netdev);
Thomas Falcon94ca3052017-05-03 14:05:20 -04002757 skb_record_rx_queue(skb, scrq_num);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002758
2759 if (flags & IBMVNIC_IP_CHKSUM_GOOD &&
2760 flags & IBMVNIC_TCP_UDP_CHKSUM_GOOD) {
2761 skb->ip_summed = CHECKSUM_UNNECESSARY;
2762 }
2763
2764 length = skb->len;
2765 napi_gro_receive(napi, skb); /* send it up */
2766 netdev->stats.rx_packets++;
2767 netdev->stats.rx_bytes += length;
John Allen3d52b592017-08-02 16:44:14 -05002768 adapter->rx_stats_buffers[scrq_num].packets++;
2769 adapter->rx_stats_buffers[scrq_num].bytes += length;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002770 frames_processed++;
2771 }
Nathan Fontenot152ce472017-05-26 10:30:54 -04002772
Dwip N. Banerjee41ed0a02020-11-18 19:12:25 -06002773 if (adapter->state != VNIC_CLOSING &&
2774 ((atomic_read(&adapter->rx_pool[scrq_num].available) <
2775 adapter->req_rx_add_entries_per_subcrq / 2) ||
2776 frames_processed < budget))
Nathan Fontenot152ce472017-05-26 10:30:54 -04002777 replenish_rx_pool(adapter, &adapter->rx_pool[scrq_num]);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002778 if (frames_processed < budget) {
Dwip N. Banerjeeec20f362020-11-18 19:12:23 -06002779 if (napi_complete_done(napi, frames_processed)) {
2780 enable_scrq_irq(adapter, rx_scrq);
2781 if (pending_scrq(adapter, rx_scrq)) {
Dwip N. Banerjeeec20f362020-11-18 19:12:23 -06002782 if (napi_reschedule(napi)) {
2783 disable_scrq_irq(adapter, rx_scrq);
2784 goto restart_poll;
2785 }
2786 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002787 }
2788 }
2789 return frames_processed;
2790}
2791
John Allenc26eba02017-10-26 16:23:25 -05002792static int wait_for_reset(struct ibmvnic_adapter *adapter)
2793{
Thomas Falconaf894d22018-04-06 18:37:04 -05002794 int rc, ret;
2795
John Allenc26eba02017-10-26 16:23:25 -05002796 adapter->fallback.mtu = adapter->req_mtu;
2797 adapter->fallback.rx_queues = adapter->req_rx_queues;
2798 adapter->fallback.tx_queues = adapter->req_tx_queues;
2799 adapter->fallback.rx_entries = adapter->req_rx_add_entries_per_subcrq;
2800 adapter->fallback.tx_entries = adapter->req_tx_entries_per_subcrq;
2801
Thomas Falcon070eca92019-11-25 17:12:53 -06002802 reinit_completion(&adapter->reset_done);
John Allenc26eba02017-10-26 16:23:25 -05002803 adapter->wait_for_reset = true;
Thomas Falconaf894d22018-04-06 18:37:04 -05002804 rc = ibmvnic_reset(adapter, VNIC_RESET_CHANGE_PARAM);
Thomas Falcon476d96c2019-11-25 17:12:55 -06002805
2806 if (rc) {
2807 ret = rc;
2808 goto out;
2809 }
2810 rc = ibmvnic_wait_for_completion(adapter, &adapter->reset_done, 60000);
2811 if (rc) {
2812 ret = -ENODEV;
2813 goto out;
2814 }
John Allenc26eba02017-10-26 16:23:25 -05002815
Thomas Falconaf894d22018-04-06 18:37:04 -05002816 ret = 0;
John Allenc26eba02017-10-26 16:23:25 -05002817 if (adapter->reset_done_rc) {
Thomas Falconaf894d22018-04-06 18:37:04 -05002818 ret = -EIO;
John Allenc26eba02017-10-26 16:23:25 -05002819 adapter->desired.mtu = adapter->fallback.mtu;
2820 adapter->desired.rx_queues = adapter->fallback.rx_queues;
2821 adapter->desired.tx_queues = adapter->fallback.tx_queues;
2822 adapter->desired.rx_entries = adapter->fallback.rx_entries;
2823 adapter->desired.tx_entries = adapter->fallback.tx_entries;
2824
Thomas Falcon070eca92019-11-25 17:12:53 -06002825 reinit_completion(&adapter->reset_done);
Thomas Falconaf894d22018-04-06 18:37:04 -05002826 adapter->wait_for_reset = true;
2827 rc = ibmvnic_reset(adapter, VNIC_RESET_CHANGE_PARAM);
Thomas Falcon476d96c2019-11-25 17:12:55 -06002828 if (rc) {
2829 ret = rc;
2830 goto out;
2831 }
2832 rc = ibmvnic_wait_for_completion(adapter, &adapter->reset_done,
2833 60000);
2834 if (rc) {
2835 ret = -ENODEV;
2836 goto out;
2837 }
John Allenc26eba02017-10-26 16:23:25 -05002838 }
Thomas Falcon476d96c2019-11-25 17:12:55 -06002839out:
John Allenc26eba02017-10-26 16:23:25 -05002840 adapter->wait_for_reset = false;
2841
Thomas Falconaf894d22018-04-06 18:37:04 -05002842 return ret;
John Allenc26eba02017-10-26 16:23:25 -05002843}
2844
John Allen3a807b72017-06-06 16:55:52 -05002845static int ibmvnic_change_mtu(struct net_device *netdev, int new_mtu)
2846{
John Allenc26eba02017-10-26 16:23:25 -05002847 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2848
2849 adapter->desired.mtu = new_mtu + ETH_HLEN;
2850
2851 return wait_for_reset(adapter);
John Allen3a807b72017-06-06 16:55:52 -05002852}
2853
Thomas Falconf10b09e2018-03-12 11:51:05 -05002854static netdev_features_t ibmvnic_features_check(struct sk_buff *skb,
2855 struct net_device *dev,
2856 netdev_features_t features)
2857{
2858 /* Some backing hardware adapters can not
2859 * handle packets with a MSS less than 224
2860 * or with only one segment.
2861 */
2862 if (skb_is_gso(skb)) {
2863 if (skb_shinfo(skb)->gso_size < 224 ||
2864 skb_shinfo(skb)->gso_segs == 1)
2865 features &= ~NETIF_F_GSO_MASK;
2866 }
2867
2868 return features;
2869}
2870
Thomas Falcon032c5e82015-12-21 11:26:06 -06002871static const struct net_device_ops ibmvnic_netdev_ops = {
2872 .ndo_open = ibmvnic_open,
2873 .ndo_stop = ibmvnic_close,
2874 .ndo_start_xmit = ibmvnic_xmit,
2875 .ndo_set_rx_mode = ibmvnic_set_multi,
2876 .ndo_set_mac_address = ibmvnic_set_mac,
2877 .ndo_validate_addr = eth_validate_addr,
Thomas Falcon032c5e82015-12-21 11:26:06 -06002878 .ndo_tx_timeout = ibmvnic_tx_timeout,
John Allen3a807b72017-06-06 16:55:52 -05002879 .ndo_change_mtu = ibmvnic_change_mtu,
Thomas Falconf10b09e2018-03-12 11:51:05 -05002880 .ndo_features_check = ibmvnic_features_check,
Thomas Falcon032c5e82015-12-21 11:26:06 -06002881};
2882
2883/* ethtool functions */
2884
Philippe Reynes8a433792017-01-07 22:37:29 +01002885static int ibmvnic_get_link_ksettings(struct net_device *netdev,
2886 struct ethtool_link_ksettings *cmd)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002887{
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03002888 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2889 int rc;
Philippe Reynes8a433792017-01-07 22:37:29 +01002890
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03002891 rc = send_query_phys_parms(adapter);
2892 if (rc) {
2893 adapter->speed = SPEED_UNKNOWN;
2894 adapter->duplex = DUPLEX_UNKNOWN;
2895 }
2896 cmd->base.speed = adapter->speed;
2897 cmd->base.duplex = adapter->duplex;
Philippe Reynes8a433792017-01-07 22:37:29 +01002898 cmd->base.port = PORT_FIBRE;
2899 cmd->base.phy_address = 0;
2900 cmd->base.autoneg = AUTONEG_ENABLE;
2901
Thomas Falcon032c5e82015-12-21 11:26:06 -06002902 return 0;
2903}
2904
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02002905static void ibmvnic_get_drvinfo(struct net_device *netdev,
Thomas Falcon032c5e82015-12-21 11:26:06 -06002906 struct ethtool_drvinfo *info)
2907{
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02002908 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2909
Lijun Pan8a96c802021-02-11 00:43:25 -06002910 strscpy(info->driver, ibmvnic_driver_name, sizeof(info->driver));
2911 strscpy(info->version, IBMVNIC_DRIVER_VERSION, sizeof(info->version));
2912 strscpy(info->fw_version, adapter->fw_version,
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02002913 sizeof(info->fw_version));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002914}
2915
2916static u32 ibmvnic_get_msglevel(struct net_device *netdev)
2917{
2918 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2919
2920 return adapter->msg_enable;
2921}
2922
2923static void ibmvnic_set_msglevel(struct net_device *netdev, u32 data)
2924{
2925 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2926
2927 adapter->msg_enable = data;
2928}
2929
2930static u32 ibmvnic_get_link(struct net_device *netdev)
2931{
2932 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2933
2934 /* Don't need to send a query because we request a logical link up at
2935 * init and then we wait for link state indications
2936 */
2937 return adapter->logical_link_state;
2938}
2939
2940static void ibmvnic_get_ringparam(struct net_device *netdev,
2941 struct ethtool_ringparam *ring)
2942{
John Allenbc131b32017-08-02 16:46:30 -05002943 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2944
Thomas Falcon723ad912018-09-28 18:38:26 -05002945 if (adapter->priv_flags & IBMVNIC_USE_SERVER_MAXES) {
2946 ring->rx_max_pending = adapter->max_rx_add_entries_per_subcrq;
2947 ring->tx_max_pending = adapter->max_tx_entries_per_subcrq;
2948 } else {
2949 ring->rx_max_pending = IBMVNIC_MAX_QUEUE_SZ;
2950 ring->tx_max_pending = IBMVNIC_MAX_QUEUE_SZ;
2951 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002952 ring->rx_mini_max_pending = 0;
2953 ring->rx_jumbo_max_pending = 0;
John Allenbc131b32017-08-02 16:46:30 -05002954 ring->rx_pending = adapter->req_rx_add_entries_per_subcrq;
2955 ring->tx_pending = adapter->req_tx_entries_per_subcrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002956 ring->rx_mini_pending = 0;
2957 ring->rx_jumbo_pending = 0;
2958}
2959
John Allenc26eba02017-10-26 16:23:25 -05002960static int ibmvnic_set_ringparam(struct net_device *netdev,
2961 struct ethtool_ringparam *ring)
2962{
2963 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Thomas Falcon723ad912018-09-28 18:38:26 -05002964 int ret;
John Allenc26eba02017-10-26 16:23:25 -05002965
Thomas Falcon723ad912018-09-28 18:38:26 -05002966 ret = 0;
John Allenc26eba02017-10-26 16:23:25 -05002967 adapter->desired.rx_entries = ring->rx_pending;
2968 adapter->desired.tx_entries = ring->tx_pending;
2969
Thomas Falcon723ad912018-09-28 18:38:26 -05002970 ret = wait_for_reset(adapter);
2971
2972 if (!ret &&
2973 (adapter->req_rx_add_entries_per_subcrq != ring->rx_pending ||
2974 adapter->req_tx_entries_per_subcrq != ring->tx_pending))
2975 netdev_info(netdev,
2976 "Could not match full ringsize request. Requested: RX %d, TX %d; Allowed: RX %llu, TX %llu\n",
2977 ring->rx_pending, ring->tx_pending,
2978 adapter->req_rx_add_entries_per_subcrq,
2979 adapter->req_tx_entries_per_subcrq);
2980 return ret;
John Allenc26eba02017-10-26 16:23:25 -05002981}
2982
John Allenc2dbeb62017-08-02 16:47:17 -05002983static void ibmvnic_get_channels(struct net_device *netdev,
2984 struct ethtool_channels *channels)
2985{
2986 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2987
Thomas Falcon723ad912018-09-28 18:38:26 -05002988 if (adapter->priv_flags & IBMVNIC_USE_SERVER_MAXES) {
2989 channels->max_rx = adapter->max_rx_queues;
2990 channels->max_tx = adapter->max_tx_queues;
2991 } else {
2992 channels->max_rx = IBMVNIC_MAX_QUEUES;
2993 channels->max_tx = IBMVNIC_MAX_QUEUES;
2994 }
2995
John Allenc2dbeb62017-08-02 16:47:17 -05002996 channels->max_other = 0;
2997 channels->max_combined = 0;
2998 channels->rx_count = adapter->req_rx_queues;
2999 channels->tx_count = adapter->req_tx_queues;
3000 channels->other_count = 0;
3001 channels->combined_count = 0;
3002}
3003
John Allenc26eba02017-10-26 16:23:25 -05003004static int ibmvnic_set_channels(struct net_device *netdev,
3005 struct ethtool_channels *channels)
3006{
3007 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Thomas Falcon723ad912018-09-28 18:38:26 -05003008 int ret;
John Allenc26eba02017-10-26 16:23:25 -05003009
Thomas Falcon723ad912018-09-28 18:38:26 -05003010 ret = 0;
John Allenc26eba02017-10-26 16:23:25 -05003011 adapter->desired.rx_queues = channels->rx_count;
3012 adapter->desired.tx_queues = channels->tx_count;
3013
Thomas Falcon723ad912018-09-28 18:38:26 -05003014 ret = wait_for_reset(adapter);
3015
3016 if (!ret &&
3017 (adapter->req_rx_queues != channels->rx_count ||
3018 adapter->req_tx_queues != channels->tx_count))
3019 netdev_info(netdev,
3020 "Could not match full channels request. Requested: RX %d, TX %d; Allowed: RX %llu, TX %llu\n",
3021 channels->rx_count, channels->tx_count,
3022 adapter->req_rx_queues, adapter->req_tx_queues);
3023 return ret;
John Allenc26eba02017-10-26 16:23:25 -05003024}
3025
Thomas Falcon032c5e82015-12-21 11:26:06 -06003026static void ibmvnic_get_strings(struct net_device *dev, u32 stringset, u8 *data)
3027{
John Allen3d52b592017-08-02 16:44:14 -05003028 struct ibmvnic_adapter *adapter = netdev_priv(dev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003029 int i;
3030
Thomas Falcon723ad912018-09-28 18:38:26 -05003031 switch (stringset) {
3032 case ETH_SS_STATS:
3033 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats);
3034 i++, data += ETH_GSTRING_LEN)
3035 memcpy(data, ibmvnic_stats[i].name, ETH_GSTRING_LEN);
3036
3037 for (i = 0; i < adapter->req_tx_queues; i++) {
3038 snprintf(data, ETH_GSTRING_LEN, "tx%d_packets", i);
3039 data += ETH_GSTRING_LEN;
3040
3041 snprintf(data, ETH_GSTRING_LEN, "tx%d_bytes", i);
3042 data += ETH_GSTRING_LEN;
3043
3044 snprintf(data, ETH_GSTRING_LEN,
3045 "tx%d_dropped_packets", i);
3046 data += ETH_GSTRING_LEN;
3047 }
3048
3049 for (i = 0; i < adapter->req_rx_queues; i++) {
3050 snprintf(data, ETH_GSTRING_LEN, "rx%d_packets", i);
3051 data += ETH_GSTRING_LEN;
3052
3053 snprintf(data, ETH_GSTRING_LEN, "rx%d_bytes", i);
3054 data += ETH_GSTRING_LEN;
3055
3056 snprintf(data, ETH_GSTRING_LEN, "rx%d_interrupts", i);
3057 data += ETH_GSTRING_LEN;
3058 }
3059 break;
3060
3061 case ETH_SS_PRIV_FLAGS:
3062 for (i = 0; i < ARRAY_SIZE(ibmvnic_priv_flags); i++)
3063 strcpy(data + i * ETH_GSTRING_LEN,
3064 ibmvnic_priv_flags[i]);
3065 break;
3066 default:
Thomas Falcon032c5e82015-12-21 11:26:06 -06003067 return;
John Allen3d52b592017-08-02 16:44:14 -05003068 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06003069}
3070
3071static int ibmvnic_get_sset_count(struct net_device *dev, int sset)
3072{
John Allen3d52b592017-08-02 16:44:14 -05003073 struct ibmvnic_adapter *adapter = netdev_priv(dev);
3074
Thomas Falcon032c5e82015-12-21 11:26:06 -06003075 switch (sset) {
3076 case ETH_SS_STATS:
John Allen3d52b592017-08-02 16:44:14 -05003077 return ARRAY_SIZE(ibmvnic_stats) +
3078 adapter->req_tx_queues * NUM_TX_STATS +
3079 adapter->req_rx_queues * NUM_RX_STATS;
Thomas Falcon723ad912018-09-28 18:38:26 -05003080 case ETH_SS_PRIV_FLAGS:
3081 return ARRAY_SIZE(ibmvnic_priv_flags);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003082 default:
3083 return -EOPNOTSUPP;
3084 }
3085}
3086
3087static void ibmvnic_get_ethtool_stats(struct net_device *dev,
3088 struct ethtool_stats *stats, u64 *data)
3089{
3090 struct ibmvnic_adapter *adapter = netdev_priv(dev);
3091 union ibmvnic_crq crq;
John Allen3d52b592017-08-02 16:44:14 -05003092 int i, j;
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05003093 int rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003094
3095 memset(&crq, 0, sizeof(crq));
3096 crq.request_statistics.first = IBMVNIC_CRQ_CMD;
3097 crq.request_statistics.cmd = REQUEST_STATISTICS;
3098 crq.request_statistics.ioba = cpu_to_be32(adapter->stats_token);
3099 crq.request_statistics.len =
3100 cpu_to_be32(sizeof(struct ibmvnic_statistics));
Thomas Falcon032c5e82015-12-21 11:26:06 -06003101
3102 /* Wait for data to be written */
Thomas Falcon070eca92019-11-25 17:12:53 -06003103 reinit_completion(&adapter->stats_done);
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05003104 rc = ibmvnic_send_crq(adapter, &crq);
3105 if (rc)
3106 return;
Thomas Falcon476d96c2019-11-25 17:12:55 -06003107 rc = ibmvnic_wait_for_completion(adapter, &adapter->stats_done, 10000);
3108 if (rc)
3109 return;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003110
3111 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++)
Lijun Pan91dc5d22021-02-11 00:43:22 -06003112 data[i] = be64_to_cpu(IBMVNIC_GET_STAT
3113 (adapter, ibmvnic_stats[i].offset));
John Allen3d52b592017-08-02 16:44:14 -05003114
3115 for (j = 0; j < adapter->req_tx_queues; j++) {
3116 data[i] = adapter->tx_stats_buffers[j].packets;
3117 i++;
3118 data[i] = adapter->tx_stats_buffers[j].bytes;
3119 i++;
3120 data[i] = adapter->tx_stats_buffers[j].dropped_packets;
3121 i++;
3122 }
3123
3124 for (j = 0; j < adapter->req_rx_queues; j++) {
3125 data[i] = adapter->rx_stats_buffers[j].packets;
3126 i++;
3127 data[i] = adapter->rx_stats_buffers[j].bytes;
3128 i++;
3129 data[i] = adapter->rx_stats_buffers[j].interrupts;
3130 i++;
3131 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06003132}
3133
Thomas Falcon723ad912018-09-28 18:38:26 -05003134static u32 ibmvnic_get_priv_flags(struct net_device *netdev)
3135{
3136 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
3137
3138 return adapter->priv_flags;
3139}
3140
3141static int ibmvnic_set_priv_flags(struct net_device *netdev, u32 flags)
3142{
3143 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
3144 bool which_maxes = !!(flags & IBMVNIC_USE_SERVER_MAXES);
3145
3146 if (which_maxes)
3147 adapter->priv_flags |= IBMVNIC_USE_SERVER_MAXES;
3148 else
3149 adapter->priv_flags &= ~IBMVNIC_USE_SERVER_MAXES;
3150
3151 return 0;
3152}
Lijun Pan91dc5d22021-02-11 00:43:22 -06003153
Thomas Falcon032c5e82015-12-21 11:26:06 -06003154static const struct ethtool_ops ibmvnic_ethtool_ops = {
Thomas Falcon032c5e82015-12-21 11:26:06 -06003155 .get_drvinfo = ibmvnic_get_drvinfo,
3156 .get_msglevel = ibmvnic_get_msglevel,
3157 .set_msglevel = ibmvnic_set_msglevel,
3158 .get_link = ibmvnic_get_link,
3159 .get_ringparam = ibmvnic_get_ringparam,
John Allenc26eba02017-10-26 16:23:25 -05003160 .set_ringparam = ibmvnic_set_ringparam,
John Allenc2dbeb62017-08-02 16:47:17 -05003161 .get_channels = ibmvnic_get_channels,
John Allenc26eba02017-10-26 16:23:25 -05003162 .set_channels = ibmvnic_set_channels,
Thomas Falcon032c5e82015-12-21 11:26:06 -06003163 .get_strings = ibmvnic_get_strings,
3164 .get_sset_count = ibmvnic_get_sset_count,
3165 .get_ethtool_stats = ibmvnic_get_ethtool_stats,
Philippe Reynes8a433792017-01-07 22:37:29 +01003166 .get_link_ksettings = ibmvnic_get_link_ksettings,
Thomas Falcon723ad912018-09-28 18:38:26 -05003167 .get_priv_flags = ibmvnic_get_priv_flags,
3168 .set_priv_flags = ibmvnic_set_priv_flags,
Thomas Falcon032c5e82015-12-21 11:26:06 -06003169};
3170
3171/* Routines for managing CRQs/sCRQs */
3172
Nathan Fontenot57a49432017-05-26 10:31:12 -04003173static int reset_one_sub_crq_queue(struct ibmvnic_adapter *adapter,
3174 struct ibmvnic_sub_crq_queue *scrq)
3175{
3176 int rc;
3177
Dany Madden9281cf22020-11-25 18:04:26 -06003178 if (!scrq) {
YANG LI862aecb2020-12-30 15:23:14 +08003179 netdev_dbg(adapter->netdev, "Invalid scrq reset.\n");
Dany Madden9281cf22020-11-25 18:04:26 -06003180 return -EINVAL;
3181 }
3182
Nathan Fontenot57a49432017-05-26 10:31:12 -04003183 if (scrq->irq) {
3184 free_irq(scrq->irq, scrq);
3185 irq_dispose_mapping(scrq->irq);
3186 scrq->irq = 0;
3187 }
3188
Dany Madden9281cf22020-11-25 18:04:26 -06003189 if (scrq->msgs) {
3190 memset(scrq->msgs, 0, 4 * PAGE_SIZE);
3191 atomic_set(&scrq->used, 0);
3192 scrq->cur = 0;
Jakub Kicinski55fd59b2020-12-03 15:42:13 -08003193 scrq->ind_buf.index = 0;
Dany Madden9281cf22020-11-25 18:04:26 -06003194 } else {
3195 netdev_dbg(adapter->netdev, "Invalid scrq reset\n");
3196 return -EINVAL;
3197 }
Nathan Fontenot57a49432017-05-26 10:31:12 -04003198
3199 rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
3200 4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
3201 return rc;
3202}
3203
3204static int reset_sub_crq_queues(struct ibmvnic_adapter *adapter)
3205{
3206 int i, rc;
3207
Lijun Pana0faaa22020-11-23 13:35:45 -06003208 if (!adapter->tx_scrq || !adapter->rx_scrq)
3209 return -EINVAL;
3210
Nathan Fontenot57a49432017-05-26 10:31:12 -04003211 for (i = 0; i < adapter->req_tx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05003212 netdev_dbg(adapter->netdev, "Re-setting tx_scrq[%d]\n", i);
Nathan Fontenot57a49432017-05-26 10:31:12 -04003213 rc = reset_one_sub_crq_queue(adapter, adapter->tx_scrq[i]);
3214 if (rc)
3215 return rc;
3216 }
3217
3218 for (i = 0; i < adapter->req_rx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05003219 netdev_dbg(adapter->netdev, "Re-setting rx_scrq[%d]\n", i);
Nathan Fontenot57a49432017-05-26 10:31:12 -04003220 rc = reset_one_sub_crq_queue(adapter, adapter->rx_scrq[i]);
3221 if (rc)
3222 return rc;
3223 }
3224
Nathan Fontenot57a49432017-05-26 10:31:12 -04003225 return rc;
3226}
3227
Thomas Falcon032c5e82015-12-21 11:26:06 -06003228static void release_sub_crq_queue(struct ibmvnic_adapter *adapter,
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06003229 struct ibmvnic_sub_crq_queue *scrq,
3230 bool do_h_free)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003231{
3232 struct device *dev = &adapter->vdev->dev;
3233 long rc;
3234
3235 netdev_dbg(adapter->netdev, "Releasing sub-CRQ\n");
3236
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06003237 if (do_h_free) {
3238 /* Close the sub-crqs */
3239 do {
3240 rc = plpar_hcall_norets(H_FREE_SUB_CRQ,
3241 adapter->vdev->unit_address,
3242 scrq->crq_num);
3243 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
Thomas Falcon032c5e82015-12-21 11:26:06 -06003244
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06003245 if (rc) {
3246 netdev_err(adapter->netdev,
3247 "Failed to release sub-CRQ %16lx, rc = %ld\n",
3248 scrq->crq_num, rc);
3249 }
Thomas Falconffa73852017-04-19 13:44:29 -04003250 }
3251
Thomas Falconf019fb62020-11-18 19:12:17 -06003252 dma_free_coherent(dev,
3253 IBMVNIC_IND_ARR_SZ,
3254 scrq->ind_buf.indir_arr,
3255 scrq->ind_buf.indir_dma);
3256
Thomas Falcon032c5e82015-12-21 11:26:06 -06003257 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
3258 DMA_BIDIRECTIONAL);
3259 free_pages((unsigned long)scrq->msgs, 2);
3260 kfree(scrq);
3261}
3262
3263static struct ibmvnic_sub_crq_queue *init_sub_crq_queue(struct ibmvnic_adapter
3264 *adapter)
3265{
3266 struct device *dev = &adapter->vdev->dev;
3267 struct ibmvnic_sub_crq_queue *scrq;
3268 int rc;
3269
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04003270 scrq = kzalloc(sizeof(*scrq), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003271 if (!scrq)
3272 return NULL;
3273
Nathan Fontenot7f7adc52017-04-19 13:45:16 -04003274 scrq->msgs =
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04003275 (union sub_crq *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, 2);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003276 if (!scrq->msgs) {
3277 dev_warn(dev, "Couldn't allocate crq queue messages page\n");
3278 goto zero_page_failed;
3279 }
3280
3281 scrq->msg_token = dma_map_single(dev, scrq->msgs, 4 * PAGE_SIZE,
3282 DMA_BIDIRECTIONAL);
3283 if (dma_mapping_error(dev, scrq->msg_token)) {
3284 dev_warn(dev, "Couldn't map crq queue messages page\n");
3285 goto map_failed;
3286 }
3287
3288 rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
3289 4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
3290
3291 if (rc == H_RESOURCE)
3292 rc = ibmvnic_reset_crq(adapter);
3293
3294 if (rc == H_CLOSED) {
3295 dev_warn(dev, "Partner adapter not ready, waiting.\n");
3296 } else if (rc) {
3297 dev_warn(dev, "Error %d registering sub-crq\n", rc);
3298 goto reg_failed;
3299 }
3300
Thomas Falcon032c5e82015-12-21 11:26:06 -06003301 scrq->adapter = adapter;
3302 scrq->size = 4 * PAGE_SIZE / sizeof(*scrq->msgs);
Thomas Falconf019fb62020-11-18 19:12:17 -06003303 scrq->ind_buf.index = 0;
3304
3305 scrq->ind_buf.indir_arr =
3306 dma_alloc_coherent(dev,
3307 IBMVNIC_IND_ARR_SZ,
3308 &scrq->ind_buf.indir_dma,
3309 GFP_KERNEL);
3310
3311 if (!scrq->ind_buf.indir_arr)
3312 goto indir_failed;
3313
Thomas Falcon032c5e82015-12-21 11:26:06 -06003314 spin_lock_init(&scrq->lock);
3315
3316 netdev_dbg(adapter->netdev,
3317 "sub-crq initialized, num %lx, hw_irq=%lx, irq=%x\n",
3318 scrq->crq_num, scrq->hw_irq, scrq->irq);
3319
3320 return scrq;
3321
Thomas Falconf019fb62020-11-18 19:12:17 -06003322indir_failed:
3323 do {
3324 rc = plpar_hcall_norets(H_FREE_SUB_CRQ,
3325 adapter->vdev->unit_address,
3326 scrq->crq_num);
3327 } while (rc == H_BUSY || rc == H_IS_LONG_BUSY(rc));
Thomas Falcon032c5e82015-12-21 11:26:06 -06003328reg_failed:
3329 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
3330 DMA_BIDIRECTIONAL);
3331map_failed:
3332 free_pages((unsigned long)scrq->msgs, 2);
3333zero_page_failed:
3334 kfree(scrq);
3335
3336 return NULL;
3337}
3338
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06003339static void release_sub_crqs(struct ibmvnic_adapter *adapter, bool do_h_free)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003340{
3341 int i;
3342
3343 if (adapter->tx_scrq) {
Nathan Fontenot82e3be32018-02-21 21:33:56 -06003344 for (i = 0; i < adapter->num_active_tx_scrqs; i++) {
Nathan Fontenotb5108882017-03-30 02:49:18 -04003345 if (!adapter->tx_scrq[i])
3346 continue;
3347
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05003348 netdev_dbg(adapter->netdev, "Releasing tx_scrq[%d]\n",
3349 i);
Sukadev Bhattiprolu65d64702021-06-23 21:13:12 -07003350 ibmvnic_tx_scrq_clean_buffer(adapter, adapter->tx_scrq[i]);
Nathan Fontenotb5108882017-03-30 02:49:18 -04003351 if (adapter->tx_scrq[i]->irq) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06003352 free_irq(adapter->tx_scrq[i]->irq,
3353 adapter->tx_scrq[i]);
Thomas Falcon88eb98a2016-07-06 15:35:16 -05003354 irq_dispose_mapping(adapter->tx_scrq[i]->irq);
Nathan Fontenotb5108882017-03-30 02:49:18 -04003355 adapter->tx_scrq[i]->irq = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003356 }
Nathan Fontenotb5108882017-03-30 02:49:18 -04003357
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06003358 release_sub_crq_queue(adapter, adapter->tx_scrq[i],
3359 do_h_free);
Nathan Fontenotb5108882017-03-30 02:49:18 -04003360 }
3361
Nathan Fontenot9501df32017-03-15 23:38:07 -04003362 kfree(adapter->tx_scrq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003363 adapter->tx_scrq = NULL;
Nathan Fontenot82e3be32018-02-21 21:33:56 -06003364 adapter->num_active_tx_scrqs = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003365 }
3366
3367 if (adapter->rx_scrq) {
Nathan Fontenot82e3be32018-02-21 21:33:56 -06003368 for (i = 0; i < adapter->num_active_rx_scrqs; i++) {
Nathan Fontenotb5108882017-03-30 02:49:18 -04003369 if (!adapter->rx_scrq[i])
3370 continue;
3371
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05003372 netdev_dbg(adapter->netdev, "Releasing rx_scrq[%d]\n",
3373 i);
Nathan Fontenotb5108882017-03-30 02:49:18 -04003374 if (adapter->rx_scrq[i]->irq) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06003375 free_irq(adapter->rx_scrq[i]->irq,
3376 adapter->rx_scrq[i]);
Thomas Falcon88eb98a2016-07-06 15:35:16 -05003377 irq_dispose_mapping(adapter->rx_scrq[i]->irq);
Nathan Fontenotb5108882017-03-30 02:49:18 -04003378 adapter->rx_scrq[i]->irq = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003379 }
Nathan Fontenotb5108882017-03-30 02:49:18 -04003380
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06003381 release_sub_crq_queue(adapter, adapter->rx_scrq[i],
3382 do_h_free);
Nathan Fontenotb5108882017-03-30 02:49:18 -04003383 }
3384
Nathan Fontenot9501df32017-03-15 23:38:07 -04003385 kfree(adapter->rx_scrq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003386 adapter->rx_scrq = NULL;
Nathan Fontenot82e3be32018-02-21 21:33:56 -06003387 adapter->num_active_rx_scrqs = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003388 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06003389}
3390
3391static int disable_scrq_irq(struct ibmvnic_adapter *adapter,
3392 struct ibmvnic_sub_crq_queue *scrq)
3393{
3394 struct device *dev = &adapter->vdev->dev;
3395 unsigned long rc;
3396
3397 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
3398 H_DISABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
3399 if (rc)
3400 dev_err(dev, "Couldn't disable scrq irq 0x%lx. rc=%ld\n",
3401 scrq->hw_irq, rc);
3402 return rc;
3403}
3404
3405static int enable_scrq_irq(struct ibmvnic_adapter *adapter,
3406 struct ibmvnic_sub_crq_queue *scrq)
3407{
3408 struct device *dev = &adapter->vdev->dev;
3409 unsigned long rc;
3410
3411 if (scrq->hw_irq > 0x100000000ULL) {
3412 dev_err(dev, "bad hw_irq = %lx\n", scrq->hw_irq);
3413 return 1;
3414 }
3415
Juliet Kim7ed5b312019-09-20 16:11:23 -04003416 if (test_bit(0, &adapter->resetting) &&
Nathan Fontenot73f9d362018-05-22 11:21:10 -05003417 adapter->reset_reason == VNIC_RESET_MOBILITY) {
Juliet Kim284f87d2019-11-20 10:50:03 -05003418 u64 val = (0xff000000) | scrq->hw_irq;
Nathan Fontenot73f9d362018-05-22 11:21:10 -05003419
Juliet Kim284f87d2019-11-20 10:50:03 -05003420 rc = plpar_hcall_norets(H_EOI, val);
Juliet Kim2df5c602019-11-20 10:50:04 -05003421 /* H_EOI would fail with rc = H_FUNCTION when running
3422 * in XIVE mode which is expected, but not an error.
3423 */
Sukadev Bhattiprolu154b3b22021-06-23 21:13:16 -07003424 if (rc && (rc != H_FUNCTION))
Juliet Kim284f87d2019-11-20 10:50:03 -05003425 dev_err(dev, "H_EOI FAILED irq 0x%llx. rc=%ld\n",
3426 val, rc);
Nathan Fontenot73f9d362018-05-22 11:21:10 -05003427 }
Thomas Falconf23e0642018-04-15 18:53:36 -05003428
Thomas Falcon032c5e82015-12-21 11:26:06 -06003429 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
3430 H_ENABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
3431 if (rc)
3432 dev_err(dev, "Couldn't enable scrq irq 0x%lx. rc=%ld\n",
3433 scrq->hw_irq, rc);
3434 return rc;
3435}
3436
3437static int ibmvnic_complete_tx(struct ibmvnic_adapter *adapter,
3438 struct ibmvnic_sub_crq_queue *scrq)
3439{
3440 struct device *dev = &adapter->vdev->dev;
Thomas Falcon06b3e352018-03-16 20:00:28 -05003441 struct ibmvnic_tx_pool *tx_pool;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003442 struct ibmvnic_tx_buff *txbuff;
Thomas Falcon0d973382020-11-18 19:12:19 -06003443 struct netdev_queue *txq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003444 union sub_crq *next;
3445 int index;
Thomas Falconc62aa372020-11-18 19:12:20 -06003446 int i;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003447
3448restart_loop:
3449 while (pending_scrq(adapter, scrq)) {
3450 unsigned int pool = scrq->pool_index;
Thomas Falconffc385b2018-02-18 10:08:41 -06003451 int num_entries = 0;
Thomas Falcon0d973382020-11-18 19:12:19 -06003452 int total_bytes = 0;
3453 int num_packets = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003454
3455 next = ibmvnic_next_scrq(adapter, scrq);
3456 for (i = 0; i < next->tx_comp.num_comps; i++) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06003457 index = be32_to_cpu(next->tx_comp.correlators[i]);
Thomas Falcon06b3e352018-03-16 20:00:28 -05003458 if (index & IBMVNIC_TSO_POOL_MASK) {
3459 tx_pool = &adapter->tso_pool[pool];
3460 index &= ~IBMVNIC_TSO_POOL_MASK;
3461 } else {
3462 tx_pool = &adapter->tx_pool[pool];
3463 }
3464
3465 txbuff = &tx_pool->tx_buff[index];
Thomas Falcon0d973382020-11-18 19:12:19 -06003466 num_packets++;
Thomas Falconffc385b2018-02-18 10:08:41 -06003467 num_entries += txbuff->num_entries;
Thomas Falcon0d973382020-11-18 19:12:19 -06003468 if (txbuff->skb) {
3469 total_bytes += txbuff->skb->len;
Lijun Panca09bf72021-04-13 03:33:25 -05003470 if (next->tx_comp.rcs[i]) {
3471 dev_err(dev, "tx error %x\n",
3472 next->tx_comp.rcs[i]);
3473 dev_kfree_skb_irq(txbuff->skb);
3474 } else {
3475 dev_consume_skb_irq(txbuff->skb);
3476 }
Thomas Falcon0d973382020-11-18 19:12:19 -06003477 txbuff->skb = NULL;
3478 } else {
3479 netdev_warn(adapter->netdev,
3480 "TX completion received with NULL socket buffer\n");
3481 }
Thomas Falcon06b3e352018-03-16 20:00:28 -05003482 tx_pool->free_map[tx_pool->producer_index] = index;
3483 tx_pool->producer_index =
3484 (tx_pool->producer_index + 1) %
3485 tx_pool->num_buffers;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003486 }
3487 /* remove tx_comp scrq*/
3488 next->tx_comp.first = 0;
Nathan Fontenot7c3e7de2017-05-03 14:05:25 -04003489
Thomas Falcon0d973382020-11-18 19:12:19 -06003490 txq = netdev_get_tx_queue(adapter->netdev, scrq->pool_index);
3491 netdev_tx_completed_queue(txq, num_packets, total_bytes);
3492
Thomas Falconffc385b2018-02-18 10:08:41 -06003493 if (atomic_sub_return(num_entries, &scrq->used) <=
Nathan Fontenot7c3e7de2017-05-03 14:05:25 -04003494 (adapter->req_tx_entries_per_subcrq / 2) &&
3495 __netif_subqueue_stopped(adapter->netdev,
3496 scrq->pool_index)) {
3497 netif_wake_subqueue(adapter->netdev, scrq->pool_index);
Thomas Falcon0aecb132018-02-26 18:10:58 -06003498 netdev_dbg(adapter->netdev, "Started queue %d\n",
3499 scrq->pool_index);
Nathan Fontenot7c3e7de2017-05-03 14:05:25 -04003500 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06003501 }
3502
3503 enable_scrq_irq(adapter, scrq);
3504
3505 if (pending_scrq(adapter, scrq)) {
3506 disable_scrq_irq(adapter, scrq);
3507 goto restart_loop;
3508 }
3509
3510 return 0;
3511}
3512
3513static irqreturn_t ibmvnic_interrupt_tx(int irq, void *instance)
3514{
3515 struct ibmvnic_sub_crq_queue *scrq = instance;
3516 struct ibmvnic_adapter *adapter = scrq->adapter;
3517
3518 disable_scrq_irq(adapter, scrq);
3519 ibmvnic_complete_tx(adapter, scrq);
3520
3521 return IRQ_HANDLED;
3522}
3523
3524static irqreturn_t ibmvnic_interrupt_rx(int irq, void *instance)
3525{
3526 struct ibmvnic_sub_crq_queue *scrq = instance;
3527 struct ibmvnic_adapter *adapter = scrq->adapter;
3528
Nathan Fontenot09fb35e2018-01-10 10:40:09 -06003529 /* When booting a kdump kernel we can hit pending interrupts
3530 * prior to completing driver initialization.
3531 */
3532 if (unlikely(adapter->state != VNIC_OPEN))
3533 return IRQ_NONE;
3534
John Allen3d52b592017-08-02 16:44:14 -05003535 adapter->rx_stats_buffers[scrq->scrq_num].interrupts++;
3536
Thomas Falcon032c5e82015-12-21 11:26:06 -06003537 if (napi_schedule_prep(&adapter->napi[scrq->scrq_num])) {
3538 disable_scrq_irq(adapter, scrq);
3539 __napi_schedule(&adapter->napi[scrq->scrq_num]);
3540 }
3541
3542 return IRQ_HANDLED;
3543}
3544
Thomas Falconea22d512016-07-06 15:35:17 -05003545static int init_sub_crq_irqs(struct ibmvnic_adapter *adapter)
3546{
3547 struct device *dev = &adapter->vdev->dev;
3548 struct ibmvnic_sub_crq_queue *scrq;
3549 int i = 0, j = 0;
3550 int rc = 0;
3551
3552 for (i = 0; i < adapter->req_tx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05003553 netdev_dbg(adapter->netdev, "Initializing tx_scrq[%d] irq\n",
3554 i);
Thomas Falconea22d512016-07-06 15:35:17 -05003555 scrq = adapter->tx_scrq[i];
3556 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
3557
Michael Ellerman99c17902016-09-10 19:59:05 +10003558 if (!scrq->irq) {
Thomas Falconea22d512016-07-06 15:35:17 -05003559 rc = -EINVAL;
3560 dev_err(dev, "Error mapping irq\n");
3561 goto req_tx_irq_failed;
3562 }
3563
Murilo Fossa Vicentinie56e2512019-04-25 11:02:33 -03003564 snprintf(scrq->name, sizeof(scrq->name), "ibmvnic-%x-tx%d",
3565 adapter->vdev->unit_address, i);
Thomas Falconea22d512016-07-06 15:35:17 -05003566 rc = request_irq(scrq->irq, ibmvnic_interrupt_tx,
Murilo Fossa Vicentinie56e2512019-04-25 11:02:33 -03003567 0, scrq->name, scrq);
Thomas Falconea22d512016-07-06 15:35:17 -05003568
3569 if (rc) {
3570 dev_err(dev, "Couldn't register tx irq 0x%x. rc=%d\n",
3571 scrq->irq, rc);
3572 irq_dispose_mapping(scrq->irq);
Nathan Fontenotaf9090c2018-02-20 11:04:18 -06003573 goto req_tx_irq_failed;
Thomas Falconea22d512016-07-06 15:35:17 -05003574 }
3575 }
3576
3577 for (i = 0; i < adapter->req_rx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05003578 netdev_dbg(adapter->netdev, "Initializing rx_scrq[%d] irq\n",
3579 i);
Thomas Falconea22d512016-07-06 15:35:17 -05003580 scrq = adapter->rx_scrq[i];
3581 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
Michael Ellerman99c17902016-09-10 19:59:05 +10003582 if (!scrq->irq) {
Thomas Falconea22d512016-07-06 15:35:17 -05003583 rc = -EINVAL;
3584 dev_err(dev, "Error mapping irq\n");
3585 goto req_rx_irq_failed;
3586 }
Murilo Fossa Vicentinie56e2512019-04-25 11:02:33 -03003587 snprintf(scrq->name, sizeof(scrq->name), "ibmvnic-%x-rx%d",
3588 adapter->vdev->unit_address, i);
Thomas Falconea22d512016-07-06 15:35:17 -05003589 rc = request_irq(scrq->irq, ibmvnic_interrupt_rx,
Murilo Fossa Vicentinie56e2512019-04-25 11:02:33 -03003590 0, scrq->name, scrq);
Thomas Falconea22d512016-07-06 15:35:17 -05003591 if (rc) {
3592 dev_err(dev, "Couldn't register rx irq 0x%x. rc=%d\n",
3593 scrq->irq, rc);
3594 irq_dispose_mapping(scrq->irq);
3595 goto req_rx_irq_failed;
3596 }
3597 }
3598 return rc;
3599
3600req_rx_irq_failed:
Thomas Falcon8bf371e2016-10-27 12:28:52 -05003601 for (j = 0; j < i; j++) {
Thomas Falconea22d512016-07-06 15:35:17 -05003602 free_irq(adapter->rx_scrq[j]->irq, adapter->rx_scrq[j]);
3603 irq_dispose_mapping(adapter->rx_scrq[j]->irq);
Thomas Falcon8bf371e2016-10-27 12:28:52 -05003604 }
Thomas Falconea22d512016-07-06 15:35:17 -05003605 i = adapter->req_tx_queues;
3606req_tx_irq_failed:
Thomas Falcon8bf371e2016-10-27 12:28:52 -05003607 for (j = 0; j < i; j++) {
Thomas Falconea22d512016-07-06 15:35:17 -05003608 free_irq(adapter->tx_scrq[j]->irq, adapter->tx_scrq[j]);
Thomas Falcon27a21452020-07-29 16:36:32 -05003609 irq_dispose_mapping(adapter->tx_scrq[j]->irq);
Thomas Falcon8bf371e2016-10-27 12:28:52 -05003610 }
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06003611 release_sub_crqs(adapter, 1);
Thomas Falconea22d512016-07-06 15:35:17 -05003612 return rc;
3613}
3614
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003615static int init_sub_crqs(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003616{
3617 struct device *dev = &adapter->vdev->dev;
3618 struct ibmvnic_sub_crq_queue **allqueues;
3619 int registered_queues = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003620 int total_queues;
3621 int more = 0;
Thomas Falconea22d512016-07-06 15:35:17 -05003622 int i;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003623
Thomas Falcon032c5e82015-12-21 11:26:06 -06003624 total_queues = adapter->req_tx_queues + adapter->req_rx_queues;
3625
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04003626 allqueues = kcalloc(total_queues, sizeof(*allqueues), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003627 if (!allqueues)
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003628 return -1;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003629
3630 for (i = 0; i < total_queues; i++) {
3631 allqueues[i] = init_sub_crq_queue(adapter);
3632 if (!allqueues[i]) {
3633 dev_warn(dev, "Couldn't allocate all sub-crqs\n");
3634 break;
3635 }
3636 registered_queues++;
3637 }
3638
3639 /* Make sure we were able to register the minimum number of queues */
3640 if (registered_queues <
3641 adapter->min_tx_queues + adapter->min_rx_queues) {
3642 dev_err(dev, "Fatal: Couldn't init min number of sub-crqs\n");
3643 goto tx_failed;
3644 }
3645
3646 /* Distribute the failed allocated queues*/
3647 for (i = 0; i < total_queues - registered_queues + more ; i++) {
3648 netdev_dbg(adapter->netdev, "Reducing number of queues\n");
3649 switch (i % 3) {
3650 case 0:
3651 if (adapter->req_rx_queues > adapter->min_rx_queues)
3652 adapter->req_rx_queues--;
3653 else
3654 more++;
3655 break;
3656 case 1:
3657 if (adapter->req_tx_queues > adapter->min_tx_queues)
3658 adapter->req_tx_queues--;
3659 else
3660 more++;
3661 break;
3662 }
3663 }
3664
3665 adapter->tx_scrq = kcalloc(adapter->req_tx_queues,
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04003666 sizeof(*adapter->tx_scrq), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003667 if (!adapter->tx_scrq)
3668 goto tx_failed;
3669
3670 for (i = 0; i < adapter->req_tx_queues; i++) {
3671 adapter->tx_scrq[i] = allqueues[i];
3672 adapter->tx_scrq[i]->pool_index = i;
Nathan Fontenot82e3be32018-02-21 21:33:56 -06003673 adapter->num_active_tx_scrqs++;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003674 }
3675
3676 adapter->rx_scrq = kcalloc(adapter->req_rx_queues,
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04003677 sizeof(*adapter->rx_scrq), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003678 if (!adapter->rx_scrq)
3679 goto rx_failed;
3680
3681 for (i = 0; i < adapter->req_rx_queues; i++) {
3682 adapter->rx_scrq[i] = allqueues[i + adapter->req_tx_queues];
3683 adapter->rx_scrq[i]->scrq_num = i;
Nathan Fontenot82e3be32018-02-21 21:33:56 -06003684 adapter->num_active_rx_scrqs++;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003685 }
3686
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003687 kfree(allqueues);
3688 return 0;
3689
3690rx_failed:
3691 kfree(adapter->tx_scrq);
3692 adapter->tx_scrq = NULL;
3693tx_failed:
3694 for (i = 0; i < registered_queues; i++)
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06003695 release_sub_crq_queue(adapter, allqueues[i], 1);
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003696 kfree(allqueues);
3697 return -1;
3698}
3699
Lijun Pan09081b92020-09-27 20:13:27 -05003700static void send_request_cap(struct ibmvnic_adapter *adapter, int retry)
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003701{
3702 struct device *dev = &adapter->vdev->dev;
3703 union ibmvnic_crq crq;
John Allenc26eba02017-10-26 16:23:25 -05003704 int max_entries;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003705
3706 if (!retry) {
3707 /* Sub-CRQ entries are 32 byte long */
3708 int entries_page = 4 * PAGE_SIZE / (sizeof(u64) * 4);
3709
3710 if (adapter->min_tx_entries_per_subcrq > entries_page ||
3711 adapter->min_rx_add_entries_per_subcrq > entries_page) {
3712 dev_err(dev, "Fatal, invalid entries per sub-crq\n");
3713 return;
3714 }
3715
John Allenc26eba02017-10-26 16:23:25 -05003716 if (adapter->desired.mtu)
3717 adapter->req_mtu = adapter->desired.mtu;
3718 else
3719 adapter->req_mtu = adapter->netdev->mtu + ETH_HLEN;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003720
John Allenc26eba02017-10-26 16:23:25 -05003721 if (!adapter->desired.tx_entries)
3722 adapter->desired.tx_entries =
3723 adapter->max_tx_entries_per_subcrq;
3724 if (!adapter->desired.rx_entries)
3725 adapter->desired.rx_entries =
3726 adapter->max_rx_add_entries_per_subcrq;
3727
3728 max_entries = IBMVNIC_MAX_LTB_SIZE /
3729 (adapter->req_mtu + IBMVNIC_BUFFER_HLEN);
3730
3731 if ((adapter->req_mtu + IBMVNIC_BUFFER_HLEN) *
3732 adapter->desired.tx_entries > IBMVNIC_MAX_LTB_SIZE) {
3733 adapter->desired.tx_entries = max_entries;
3734 }
3735
3736 if ((adapter->req_mtu + IBMVNIC_BUFFER_HLEN) *
3737 adapter->desired.rx_entries > IBMVNIC_MAX_LTB_SIZE) {
3738 adapter->desired.rx_entries = max_entries;
3739 }
3740
3741 if (adapter->desired.tx_entries)
3742 adapter->req_tx_entries_per_subcrq =
3743 adapter->desired.tx_entries;
3744 else
3745 adapter->req_tx_entries_per_subcrq =
3746 adapter->max_tx_entries_per_subcrq;
3747
3748 if (adapter->desired.rx_entries)
3749 adapter->req_rx_add_entries_per_subcrq =
3750 adapter->desired.rx_entries;
3751 else
3752 adapter->req_rx_add_entries_per_subcrq =
3753 adapter->max_rx_add_entries_per_subcrq;
3754
3755 if (adapter->desired.tx_queues)
3756 adapter->req_tx_queues =
3757 adapter->desired.tx_queues;
3758 else
3759 adapter->req_tx_queues =
3760 adapter->opt_tx_comp_sub_queues;
3761
3762 if (adapter->desired.rx_queues)
3763 adapter->req_rx_queues =
3764 adapter->desired.rx_queues;
3765 else
3766 adapter->req_rx_queues =
3767 adapter->opt_rx_comp_queues;
3768
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003769 adapter->req_rx_add_queues = adapter->max_rx_add_queues;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003770 }
3771
Thomas Falcon032c5e82015-12-21 11:26:06 -06003772 memset(&crq, 0, sizeof(crq));
3773 crq.request_capability.first = IBMVNIC_CRQ_CMD;
3774 crq.request_capability.cmd = REQUEST_CAPABILITY;
3775
3776 crq.request_capability.capability = cpu_to_be16(REQ_TX_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06003777 crq.request_capability.number = cpu_to_be64(adapter->req_tx_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06003778 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003779 ibmvnic_send_crq(adapter, &crq);
3780
3781 crq.request_capability.capability = cpu_to_be16(REQ_RX_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06003782 crq.request_capability.number = cpu_to_be64(adapter->req_rx_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06003783 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003784 ibmvnic_send_crq(adapter, &crq);
3785
3786 crq.request_capability.capability = cpu_to_be16(REQ_RX_ADD_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06003787 crq.request_capability.number = cpu_to_be64(adapter->req_rx_add_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06003788 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003789 ibmvnic_send_crq(adapter, &crq);
3790
3791 crq.request_capability.capability =
3792 cpu_to_be16(REQ_TX_ENTRIES_PER_SUBCRQ);
3793 crq.request_capability.number =
Thomas Falconde89e852016-03-01 10:20:09 -06003794 cpu_to_be64(adapter->req_tx_entries_per_subcrq);
Thomas Falcon901e0402017-02-15 12:17:59 -06003795 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003796 ibmvnic_send_crq(adapter, &crq);
3797
3798 crq.request_capability.capability =
3799 cpu_to_be16(REQ_RX_ADD_ENTRIES_PER_SUBCRQ);
3800 crq.request_capability.number =
Thomas Falconde89e852016-03-01 10:20:09 -06003801 cpu_to_be64(adapter->req_rx_add_entries_per_subcrq);
Thomas Falcon901e0402017-02-15 12:17:59 -06003802 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003803 ibmvnic_send_crq(adapter, &crq);
3804
3805 crq.request_capability.capability = cpu_to_be16(REQ_MTU);
Thomas Falconde89e852016-03-01 10:20:09 -06003806 crq.request_capability.number = cpu_to_be64(adapter->req_mtu);
Thomas Falcon901e0402017-02-15 12:17:59 -06003807 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003808 ibmvnic_send_crq(adapter, &crq);
3809
3810 if (adapter->netdev->flags & IFF_PROMISC) {
3811 if (adapter->promisc_supported) {
3812 crq.request_capability.capability =
3813 cpu_to_be16(PROMISC_REQUESTED);
Thomas Falconde89e852016-03-01 10:20:09 -06003814 crq.request_capability.number = cpu_to_be64(1);
Thomas Falcon901e0402017-02-15 12:17:59 -06003815 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003816 ibmvnic_send_crq(adapter, &crq);
3817 }
3818 } else {
3819 crq.request_capability.capability =
3820 cpu_to_be16(PROMISC_REQUESTED);
Thomas Falconde89e852016-03-01 10:20:09 -06003821 crq.request_capability.number = cpu_to_be64(0);
Thomas Falcon901e0402017-02-15 12:17:59 -06003822 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003823 ibmvnic_send_crq(adapter, &crq);
3824 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06003825}
3826
3827static int pending_scrq(struct ibmvnic_adapter *adapter,
3828 struct ibmvnic_sub_crq_queue *scrq)
3829{
3830 union sub_crq *entry = &scrq->msgs[scrq->cur];
Lijun Pan665ab1e2021-01-29 19:19:04 -06003831 int rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003832
Lijun Pan665ab1e2021-01-29 19:19:04 -06003833 rc = !!(entry->generic.first & IBMVNIC_CRQ_CMD_RSP);
3834
3835 /* Ensure that the SCRQ valid flag is loaded prior to loading the
3836 * contents of the SCRQ descriptor
3837 */
3838 dma_rmb();
3839
3840 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003841}
3842
3843static union sub_crq *ibmvnic_next_scrq(struct ibmvnic_adapter *adapter,
3844 struct ibmvnic_sub_crq_queue *scrq)
3845{
3846 union sub_crq *entry;
3847 unsigned long flags;
3848
3849 spin_lock_irqsave(&scrq->lock, flags);
3850 entry = &scrq->msgs[scrq->cur];
3851 if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP) {
3852 if (++scrq->cur == scrq->size)
3853 scrq->cur = 0;
3854 } else {
3855 entry = NULL;
3856 }
3857 spin_unlock_irqrestore(&scrq->lock, flags);
3858
Lijun Pan665ab1e2021-01-29 19:19:04 -06003859 /* Ensure that the SCRQ valid flag is loaded prior to loading the
3860 * contents of the SCRQ descriptor
Thomas Falconb71ec952020-12-01 09:52:10 -06003861 */
3862 dma_rmb();
3863
Thomas Falcon032c5e82015-12-21 11:26:06 -06003864 return entry;
3865}
3866
3867static union ibmvnic_crq *ibmvnic_next_crq(struct ibmvnic_adapter *adapter)
3868{
3869 struct ibmvnic_crq_queue *queue = &adapter->crq;
3870 union ibmvnic_crq *crq;
3871
3872 crq = &queue->msgs[queue->cur];
3873 if (crq->generic.first & IBMVNIC_CRQ_CMD_RSP) {
3874 if (++queue->cur == queue->size)
3875 queue->cur = 0;
3876 } else {
3877 crq = NULL;
3878 }
3879
3880 return crq;
3881}
3882
Thomas Falcon2d14d372018-07-13 12:03:32 -05003883static void print_subcrq_error(struct device *dev, int rc, const char *func)
3884{
3885 switch (rc) {
3886 case H_PARAMETER:
3887 dev_warn_ratelimited(dev,
3888 "%s failed: Send request is malformed or adapter failover pending. (rc=%d)\n",
3889 func, rc);
3890 break;
3891 case H_CLOSED:
3892 dev_warn_ratelimited(dev,
3893 "%s failed: Backing queue closed. Adapter is down or failover pending. (rc=%d)\n",
3894 func, rc);
3895 break;
3896 default:
3897 dev_err_ratelimited(dev, "%s failed: (rc=%d)\n", func, rc);
3898 break;
3899 }
3900}
3901
Thomas Falconad7775d2016-04-01 17:20:34 -05003902static int send_subcrq_indirect(struct ibmvnic_adapter *adapter,
3903 u64 remote_handle, u64 ioba, u64 num_entries)
3904{
3905 unsigned int ua = adapter->vdev->unit_address;
3906 struct device *dev = &adapter->vdev->dev;
3907 int rc;
3908
3909 /* Make sure the hypervisor sees the complete request */
Lijun Pan1a421562021-02-12 20:36:46 -06003910 dma_wmb();
Thomas Falconad7775d2016-04-01 17:20:34 -05003911 rc = plpar_hcall_norets(H_SEND_SUB_CRQ_INDIRECT, ua,
3912 cpu_to_be64(remote_handle),
3913 ioba, num_entries);
3914
Thomas Falcon2d14d372018-07-13 12:03:32 -05003915 if (rc)
3916 print_subcrq_error(dev, rc, __func__);
Thomas Falconad7775d2016-04-01 17:20:34 -05003917
3918 return rc;
3919}
3920
Thomas Falcon032c5e82015-12-21 11:26:06 -06003921static int ibmvnic_send_crq(struct ibmvnic_adapter *adapter,
3922 union ibmvnic_crq *crq)
3923{
3924 unsigned int ua = adapter->vdev->unit_address;
3925 struct device *dev = &adapter->vdev->dev;
3926 u64 *u64_crq = (u64 *)crq;
3927 int rc;
3928
3929 netdev_dbg(adapter->netdev, "Sending CRQ: %016lx %016lx\n",
Lijun Pan429aa362021-02-11 00:43:18 -06003930 (unsigned long)cpu_to_be64(u64_crq[0]),
3931 (unsigned long)cpu_to_be64(u64_crq[1]));
Thomas Falcon032c5e82015-12-21 11:26:06 -06003932
Thomas Falcon51536982018-05-23 13:37:56 -05003933 if (!adapter->crq.active &&
3934 crq->generic.first != IBMVNIC_CRQ_INIT_CMD) {
3935 dev_warn(dev, "Invalid request detected while CRQ is inactive, possible device state change during reset\n");
3936 return -EINVAL;
3937 }
3938
Thomas Falcon032c5e82015-12-21 11:26:06 -06003939 /* Make sure the hypervisor sees the complete request */
Lijun Pan1a421562021-02-12 20:36:46 -06003940 dma_wmb();
Thomas Falcon032c5e82015-12-21 11:26:06 -06003941
3942 rc = plpar_hcall_norets(H_SEND_CRQ, ua,
3943 cpu_to_be64(u64_crq[0]),
3944 cpu_to_be64(u64_crq[1]));
3945
3946 if (rc) {
Nathan Fontenotec95dff2018-02-07 13:00:24 -06003947 if (rc == H_CLOSED) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06003948 dev_warn(dev, "CRQ Queue closed\n");
Lijun Panfa68bfa2020-08-19 17:52:24 -05003949 /* do not reset, report the fail, wait for passive init from server */
Nathan Fontenotec95dff2018-02-07 13:00:24 -06003950 }
3951
Thomas Falcon032c5e82015-12-21 11:26:06 -06003952 dev_warn(dev, "Send error (rc=%d)\n", rc);
3953 }
3954
3955 return rc;
3956}
3957
3958static int ibmvnic_send_crq_init(struct ibmvnic_adapter *adapter)
3959{
Thomas Falcon36a782f2020-08-31 11:59:57 -05003960 struct device *dev = &adapter->vdev->dev;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003961 union ibmvnic_crq crq;
Thomas Falcon36a782f2020-08-31 11:59:57 -05003962 int retries = 100;
3963 int rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003964
3965 memset(&crq, 0, sizeof(crq));
3966 crq.generic.first = IBMVNIC_CRQ_INIT_CMD;
3967 crq.generic.cmd = IBMVNIC_CRQ_INIT;
3968 netdev_dbg(adapter->netdev, "Sending CRQ init\n");
3969
Thomas Falcon36a782f2020-08-31 11:59:57 -05003970 do {
3971 rc = ibmvnic_send_crq(adapter, &crq);
3972 if (rc != H_CLOSED)
3973 break;
3974 retries--;
3975 msleep(50);
3976
3977 } while (retries > 0);
3978
3979 if (rc) {
3980 dev_err(dev, "Failed to send init request, rc = %d\n", rc);
3981 return rc;
3982 }
3983
3984 return 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003985}
3986
Nathan Fontenot37798d02017-11-08 11:23:56 -06003987struct vnic_login_client_data {
3988 u8 type;
3989 __be16 len;
Kees Cook08ea5562018-04-10 15:26:43 -07003990 char name[];
Nathan Fontenot37798d02017-11-08 11:23:56 -06003991} __packed;
3992
3993static int vnic_client_data_len(struct ibmvnic_adapter *adapter)
3994{
3995 int len;
3996
3997 /* Calculate the amount of buffer space needed for the
3998 * vnic client data in the login buffer. There are four entries,
3999 * OS name, LPAR name, device name, and a null last entry.
4000 */
4001 len = 4 * sizeof(struct vnic_login_client_data);
4002 len += 6; /* "Linux" plus NULL */
4003 len += strlen(utsname()->nodename) + 1;
4004 len += strlen(adapter->netdev->name) + 1;
4005
4006 return len;
4007}
4008
4009static void vnic_add_client_data(struct ibmvnic_adapter *adapter,
4010 struct vnic_login_client_data *vlcd)
4011{
4012 const char *os_name = "Linux";
4013 int len;
4014
4015 /* Type 1 - LPAR OS */
4016 vlcd->type = 1;
4017 len = strlen(os_name) + 1;
4018 vlcd->len = cpu_to_be16(len);
Kees Cookef2c3dd2021-06-21 14:35:09 -07004019 strscpy(vlcd->name, os_name, len);
Kees Cook08ea5562018-04-10 15:26:43 -07004020 vlcd = (struct vnic_login_client_data *)(vlcd->name + len);
Nathan Fontenot37798d02017-11-08 11:23:56 -06004021
4022 /* Type 2 - LPAR name */
4023 vlcd->type = 2;
4024 len = strlen(utsname()->nodename) + 1;
4025 vlcd->len = cpu_to_be16(len);
Kees Cookef2c3dd2021-06-21 14:35:09 -07004026 strscpy(vlcd->name, utsname()->nodename, len);
Kees Cook08ea5562018-04-10 15:26:43 -07004027 vlcd = (struct vnic_login_client_data *)(vlcd->name + len);
Nathan Fontenot37798d02017-11-08 11:23:56 -06004028
4029 /* Type 3 - device name */
4030 vlcd->type = 3;
4031 len = strlen(adapter->netdev->name) + 1;
4032 vlcd->len = cpu_to_be16(len);
Kees Cookef2c3dd2021-06-21 14:35:09 -07004033 strscpy(vlcd->name, adapter->netdev->name, len);
Nathan Fontenot37798d02017-11-08 11:23:56 -06004034}
4035
Thomas Falcon20a8ab72018-02-26 18:10:59 -06004036static int send_login(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06004037{
4038 struct ibmvnic_login_rsp_buffer *login_rsp_buffer;
4039 struct ibmvnic_login_buffer *login_buffer;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004040 struct device *dev = &adapter->vdev->dev;
Dany Maddenc98d9cc2020-11-25 18:04:30 -06004041 struct vnic_login_client_data *vlcd;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004042 dma_addr_t rsp_buffer_token;
4043 dma_addr_t buffer_token;
4044 size_t rsp_buffer_size;
4045 union ibmvnic_crq crq;
Dany Maddenc98d9cc2020-11-25 18:04:30 -06004046 int client_data_len;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004047 size_t buffer_size;
4048 __be64 *tx_list_p;
4049 __be64 *rx_list_p;
Dany Maddenc98d9cc2020-11-25 18:04:30 -06004050 int rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004051 int i;
4052
Thomas Falcon20a8ab72018-02-26 18:10:59 -06004053 if (!adapter->tx_scrq || !adapter->rx_scrq) {
4054 netdev_err(adapter->netdev,
4055 "RX or TX queues are not allocated, device login failed\n");
4056 return -1;
4057 }
4058
Lijun Pana0c8be52020-12-19 15:39:19 -06004059 release_login_buffer(adapter);
Thomas Falcon34f0f4e2018-02-13 18:23:40 -06004060 release_login_rsp_buffer(adapter);
Lijun Pana0c8be52020-12-19 15:39:19 -06004061
Nathan Fontenot37798d02017-11-08 11:23:56 -06004062 client_data_len = vnic_client_data_len(adapter);
4063
Thomas Falcon032c5e82015-12-21 11:26:06 -06004064 buffer_size =
4065 sizeof(struct ibmvnic_login_buffer) +
Nathan Fontenot37798d02017-11-08 11:23:56 -06004066 sizeof(u64) * (adapter->req_tx_queues + adapter->req_rx_queues) +
4067 client_data_len;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004068
Nathan Fontenot37798d02017-11-08 11:23:56 -06004069 login_buffer = kzalloc(buffer_size, GFP_ATOMIC);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004070 if (!login_buffer)
4071 goto buf_alloc_failed;
4072
4073 buffer_token = dma_map_single(dev, login_buffer, buffer_size,
4074 DMA_TO_DEVICE);
4075 if (dma_mapping_error(dev, buffer_token)) {
4076 dev_err(dev, "Couldn't map login buffer\n");
4077 goto buf_map_failed;
4078 }
4079
John Allen498cd8e2016-04-06 11:49:55 -05004080 rsp_buffer_size = sizeof(struct ibmvnic_login_rsp_buffer) +
4081 sizeof(u64) * adapter->req_tx_queues +
4082 sizeof(u64) * adapter->req_rx_queues +
4083 sizeof(u64) * adapter->req_rx_queues +
4084 sizeof(u8) * IBMVNIC_TX_DESC_VERSIONS;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004085
4086 login_rsp_buffer = kmalloc(rsp_buffer_size, GFP_ATOMIC);
4087 if (!login_rsp_buffer)
4088 goto buf_rsp_alloc_failed;
4089
4090 rsp_buffer_token = dma_map_single(dev, login_rsp_buffer,
4091 rsp_buffer_size, DMA_FROM_DEVICE);
4092 if (dma_mapping_error(dev, rsp_buffer_token)) {
4093 dev_err(dev, "Couldn't map login rsp buffer\n");
4094 goto buf_rsp_map_failed;
4095 }
Nathan Fontenot661a2622017-04-19 13:44:58 -04004096
Thomas Falcon032c5e82015-12-21 11:26:06 -06004097 adapter->login_buf = login_buffer;
4098 adapter->login_buf_token = buffer_token;
4099 adapter->login_buf_sz = buffer_size;
4100 adapter->login_rsp_buf = login_rsp_buffer;
4101 adapter->login_rsp_buf_token = rsp_buffer_token;
4102 adapter->login_rsp_buf_sz = rsp_buffer_size;
4103
4104 login_buffer->len = cpu_to_be32(buffer_size);
4105 login_buffer->version = cpu_to_be32(INITIAL_VERSION_LB);
4106 login_buffer->num_txcomp_subcrqs = cpu_to_be32(adapter->req_tx_queues);
4107 login_buffer->off_txcomp_subcrqs =
4108 cpu_to_be32(sizeof(struct ibmvnic_login_buffer));
4109 login_buffer->num_rxcomp_subcrqs = cpu_to_be32(adapter->req_rx_queues);
4110 login_buffer->off_rxcomp_subcrqs =
4111 cpu_to_be32(sizeof(struct ibmvnic_login_buffer) +
4112 sizeof(u64) * adapter->req_tx_queues);
4113 login_buffer->login_rsp_ioba = cpu_to_be32(rsp_buffer_token);
4114 login_buffer->login_rsp_len = cpu_to_be32(rsp_buffer_size);
4115
4116 tx_list_p = (__be64 *)((char *)login_buffer +
4117 sizeof(struct ibmvnic_login_buffer));
4118 rx_list_p = (__be64 *)((char *)login_buffer +
4119 sizeof(struct ibmvnic_login_buffer) +
4120 sizeof(u64) * adapter->req_tx_queues);
4121
4122 for (i = 0; i < adapter->req_tx_queues; i++) {
4123 if (adapter->tx_scrq[i]) {
Lijun Pan914789a2021-02-11 00:43:21 -06004124 tx_list_p[i] =
4125 cpu_to_be64(adapter->tx_scrq[i]->crq_num);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004126 }
4127 }
4128
4129 for (i = 0; i < adapter->req_rx_queues; i++) {
4130 if (adapter->rx_scrq[i]) {
Lijun Pan914789a2021-02-11 00:43:21 -06004131 rx_list_p[i] =
4132 cpu_to_be64(adapter->rx_scrq[i]->crq_num);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004133 }
4134 }
4135
Nathan Fontenot37798d02017-11-08 11:23:56 -06004136 /* Insert vNIC login client data */
4137 vlcd = (struct vnic_login_client_data *)
4138 ((char *)rx_list_p + (sizeof(u64) * adapter->req_rx_queues));
4139 login_buffer->client_data_offset =
4140 cpu_to_be32((char *)vlcd - (char *)login_buffer);
4141 login_buffer->client_data_len = cpu_to_be32(client_data_len);
4142
4143 vnic_add_client_data(adapter, vlcd);
4144
Thomas Falcon032c5e82015-12-21 11:26:06 -06004145 netdev_dbg(adapter->netdev, "Login Buffer:\n");
4146 for (i = 0; i < (adapter->login_buf_sz - 1) / 8 + 1; i++) {
4147 netdev_dbg(adapter->netdev, "%016lx\n",
Lijun Pan429aa362021-02-11 00:43:18 -06004148 ((unsigned long *)(adapter->login_buf))[i]);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004149 }
4150
4151 memset(&crq, 0, sizeof(crq));
4152 crq.login.first = IBMVNIC_CRQ_CMD;
4153 crq.login.cmd = LOGIN;
4154 crq.login.ioba = cpu_to_be32(buffer_token);
4155 crq.login.len = cpu_to_be32(buffer_size);
Sukadev Bhattiprolu76cdc5c2020-11-25 18:04:29 -06004156
4157 adapter->login_pending = true;
Dany Maddenc98d9cc2020-11-25 18:04:30 -06004158 rc = ibmvnic_send_crq(adapter, &crq);
4159 if (rc) {
4160 adapter->login_pending = false;
4161 netdev_err(adapter->netdev, "Failed to send login, rc=%d\n", rc);
4162 goto buf_rsp_map_failed;
4163 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06004164
Thomas Falcon20a8ab72018-02-26 18:10:59 -06004165 return 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004166
Thomas Falcon032c5e82015-12-21 11:26:06 -06004167buf_rsp_map_failed:
4168 kfree(login_rsp_buffer);
Dany Maddenc98d9cc2020-11-25 18:04:30 -06004169 adapter->login_rsp_buf = NULL;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004170buf_rsp_alloc_failed:
4171 dma_unmap_single(dev, buffer_token, buffer_size, DMA_TO_DEVICE);
4172buf_map_failed:
4173 kfree(login_buffer);
Dany Maddenc98d9cc2020-11-25 18:04:30 -06004174 adapter->login_buf = NULL;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004175buf_alloc_failed:
Thomas Falcon20a8ab72018-02-26 18:10:59 -06004176 return -1;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004177}
4178
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05004179static int send_request_map(struct ibmvnic_adapter *adapter, dma_addr_t addr,
4180 u32 len, u8 map_id)
Thomas Falcon032c5e82015-12-21 11:26:06 -06004181{
4182 union ibmvnic_crq crq;
4183
4184 memset(&crq, 0, sizeof(crq));
4185 crq.request_map.first = IBMVNIC_CRQ_CMD;
4186 crq.request_map.cmd = REQUEST_MAP;
4187 crq.request_map.map_id = map_id;
4188 crq.request_map.ioba = cpu_to_be32(addr);
4189 crq.request_map.len = cpu_to_be32(len);
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05004190 return ibmvnic_send_crq(adapter, &crq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004191}
4192
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05004193static int send_request_unmap(struct ibmvnic_adapter *adapter, u8 map_id)
Thomas Falcon032c5e82015-12-21 11:26:06 -06004194{
4195 union ibmvnic_crq crq;
4196
4197 memset(&crq, 0, sizeof(crq));
4198 crq.request_unmap.first = IBMVNIC_CRQ_CMD;
4199 crq.request_unmap.cmd = REQUEST_UNMAP;
4200 crq.request_unmap.map_id = map_id;
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05004201 return ibmvnic_send_crq(adapter, &crq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004202}
4203
Lijun Pan69980d02020-09-27 20:13:28 -05004204static void send_query_map(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06004205{
4206 union ibmvnic_crq crq;
4207
4208 memset(&crq, 0, sizeof(crq));
4209 crq.query_map.first = IBMVNIC_CRQ_CMD;
4210 crq.query_map.cmd = QUERY_MAP;
4211 ibmvnic_send_crq(adapter, &crq);
4212}
4213
4214/* Send a series of CRQs requesting various capabilities of the VNIC server */
Lijun Pan491099a2020-09-27 20:13:26 -05004215static void send_query_cap(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06004216{
4217 union ibmvnic_crq crq;
4218
Thomas Falcon901e0402017-02-15 12:17:59 -06004219 atomic_set(&adapter->running_cap_crqs, 0);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004220 memset(&crq, 0, sizeof(crq));
4221 crq.query_capability.first = IBMVNIC_CRQ_CMD;
4222 crq.query_capability.cmd = QUERY_CAPABILITY;
4223
4224 crq.query_capability.capability = cpu_to_be16(MIN_TX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06004225 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004226 ibmvnic_send_crq(adapter, &crq);
4227
4228 crq.query_capability.capability = cpu_to_be16(MIN_RX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06004229 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004230 ibmvnic_send_crq(adapter, &crq);
4231
4232 crq.query_capability.capability = cpu_to_be16(MIN_RX_ADD_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06004233 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004234 ibmvnic_send_crq(adapter, &crq);
4235
4236 crq.query_capability.capability = cpu_to_be16(MAX_TX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06004237 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004238 ibmvnic_send_crq(adapter, &crq);
4239
4240 crq.query_capability.capability = cpu_to_be16(MAX_RX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06004241 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004242 ibmvnic_send_crq(adapter, &crq);
4243
4244 crq.query_capability.capability = cpu_to_be16(MAX_RX_ADD_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06004245 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004246 ibmvnic_send_crq(adapter, &crq);
4247
4248 crq.query_capability.capability =
4249 cpu_to_be16(MIN_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06004250 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004251 ibmvnic_send_crq(adapter, &crq);
4252
4253 crq.query_capability.capability =
4254 cpu_to_be16(MIN_RX_ADD_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06004255 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004256 ibmvnic_send_crq(adapter, &crq);
4257
4258 crq.query_capability.capability =
4259 cpu_to_be16(MAX_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06004260 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004261 ibmvnic_send_crq(adapter, &crq);
4262
4263 crq.query_capability.capability =
4264 cpu_to_be16(MAX_RX_ADD_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06004265 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004266 ibmvnic_send_crq(adapter, &crq);
4267
4268 crq.query_capability.capability = cpu_to_be16(TCP_IP_OFFLOAD);
Thomas Falcon901e0402017-02-15 12:17:59 -06004269 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004270 ibmvnic_send_crq(adapter, &crq);
4271
4272 crq.query_capability.capability = cpu_to_be16(PROMISC_SUPPORTED);
Thomas Falcon901e0402017-02-15 12:17:59 -06004273 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004274 ibmvnic_send_crq(adapter, &crq);
4275
4276 crq.query_capability.capability = cpu_to_be16(MIN_MTU);
Thomas Falcon901e0402017-02-15 12:17:59 -06004277 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004278 ibmvnic_send_crq(adapter, &crq);
4279
4280 crq.query_capability.capability = cpu_to_be16(MAX_MTU);
Thomas Falcon901e0402017-02-15 12:17:59 -06004281 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004282 ibmvnic_send_crq(adapter, &crq);
4283
4284 crq.query_capability.capability = cpu_to_be16(MAX_MULTICAST_FILTERS);
Thomas Falcon901e0402017-02-15 12:17:59 -06004285 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004286 ibmvnic_send_crq(adapter, &crq);
4287
4288 crq.query_capability.capability = cpu_to_be16(VLAN_HEADER_INSERTION);
Thomas Falcon901e0402017-02-15 12:17:59 -06004289 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004290 ibmvnic_send_crq(adapter, &crq);
4291
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04004292 crq.query_capability.capability = cpu_to_be16(RX_VLAN_HEADER_INSERTION);
4293 atomic_inc(&adapter->running_cap_crqs);
4294 ibmvnic_send_crq(adapter, &crq);
4295
Thomas Falcon032c5e82015-12-21 11:26:06 -06004296 crq.query_capability.capability = cpu_to_be16(MAX_TX_SG_ENTRIES);
Thomas Falcon901e0402017-02-15 12:17:59 -06004297 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004298 ibmvnic_send_crq(adapter, &crq);
4299
4300 crq.query_capability.capability = cpu_to_be16(RX_SG_SUPPORTED);
Thomas Falcon901e0402017-02-15 12:17:59 -06004301 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004302 ibmvnic_send_crq(adapter, &crq);
4303
4304 crq.query_capability.capability = cpu_to_be16(OPT_TX_COMP_SUB_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06004305 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004306 ibmvnic_send_crq(adapter, &crq);
4307
4308 crq.query_capability.capability = cpu_to_be16(OPT_RX_COMP_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06004309 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004310 ibmvnic_send_crq(adapter, &crq);
4311
4312 crq.query_capability.capability =
4313 cpu_to_be16(OPT_RX_BUFADD_Q_PER_RX_COMP_Q);
Thomas Falcon901e0402017-02-15 12:17:59 -06004314 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004315 ibmvnic_send_crq(adapter, &crq);
4316
4317 crq.query_capability.capability =
4318 cpu_to_be16(OPT_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06004319 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004320 ibmvnic_send_crq(adapter, &crq);
4321
4322 crq.query_capability.capability =
4323 cpu_to_be16(OPT_RXBA_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06004324 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004325 ibmvnic_send_crq(adapter, &crq);
4326
4327 crq.query_capability.capability = cpu_to_be16(TX_RX_DESC_REQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06004328 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004329 ibmvnic_send_crq(adapter, &crq);
4330}
4331
Lijun Pan16e811f2020-09-27 20:13:29 -05004332static void send_query_ip_offload(struct ibmvnic_adapter *adapter)
4333{
4334 int buf_sz = sizeof(struct ibmvnic_query_ip_offload_buffer);
4335 struct device *dev = &adapter->vdev->dev;
4336 union ibmvnic_crq crq;
4337
4338 adapter->ip_offload_tok =
4339 dma_map_single(dev,
4340 &adapter->ip_offload_buf,
4341 buf_sz,
4342 DMA_FROM_DEVICE);
4343
4344 if (dma_mapping_error(dev, adapter->ip_offload_tok)) {
4345 if (!firmware_has_feature(FW_FEATURE_CMO))
4346 dev_err(dev, "Couldn't map offload buffer\n");
4347 return;
4348 }
4349
4350 memset(&crq, 0, sizeof(crq));
4351 crq.query_ip_offload.first = IBMVNIC_CRQ_CMD;
4352 crq.query_ip_offload.cmd = QUERY_IP_OFFLOAD;
4353 crq.query_ip_offload.len = cpu_to_be32(buf_sz);
4354 crq.query_ip_offload.ioba =
4355 cpu_to_be32(adapter->ip_offload_tok);
4356
4357 ibmvnic_send_crq(adapter, &crq);
4358}
4359
Lijun Pan46899bd2020-09-27 20:13:30 -05004360static void send_control_ip_offload(struct ibmvnic_adapter *adapter)
4361{
4362 struct ibmvnic_control_ip_offload_buffer *ctrl_buf = &adapter->ip_offload_ctrl;
4363 struct ibmvnic_query_ip_offload_buffer *buf = &adapter->ip_offload_buf;
4364 struct device *dev = &adapter->vdev->dev;
4365 netdev_features_t old_hw_features = 0;
4366 union ibmvnic_crq crq;
4367
4368 adapter->ip_offload_ctrl_tok =
4369 dma_map_single(dev,
4370 ctrl_buf,
4371 sizeof(adapter->ip_offload_ctrl),
4372 DMA_TO_DEVICE);
4373
4374 if (dma_mapping_error(dev, adapter->ip_offload_ctrl_tok)) {
4375 dev_err(dev, "Couldn't map ip offload control buffer\n");
4376 return;
4377 }
4378
4379 ctrl_buf->len = cpu_to_be32(sizeof(adapter->ip_offload_ctrl));
4380 ctrl_buf->version = cpu_to_be32(INITIAL_VERSION_IOB);
4381 ctrl_buf->ipv4_chksum = buf->ipv4_chksum;
4382 ctrl_buf->ipv6_chksum = buf->ipv6_chksum;
4383 ctrl_buf->tcp_ipv4_chksum = buf->tcp_ipv4_chksum;
4384 ctrl_buf->udp_ipv4_chksum = buf->udp_ipv4_chksum;
4385 ctrl_buf->tcp_ipv6_chksum = buf->tcp_ipv6_chksum;
4386 ctrl_buf->udp_ipv6_chksum = buf->udp_ipv6_chksum;
4387 ctrl_buf->large_tx_ipv4 = buf->large_tx_ipv4;
4388 ctrl_buf->large_tx_ipv6 = buf->large_tx_ipv6;
4389
4390 /* large_rx disabled for now, additional features needed */
4391 ctrl_buf->large_rx_ipv4 = 0;
4392 ctrl_buf->large_rx_ipv6 = 0;
4393
4394 if (adapter->state != VNIC_PROBING) {
4395 old_hw_features = adapter->netdev->hw_features;
4396 adapter->netdev->hw_features = 0;
4397 }
4398
4399 adapter->netdev->hw_features = NETIF_F_SG | NETIF_F_GSO | NETIF_F_GRO;
4400
4401 if (buf->tcp_ipv4_chksum || buf->udp_ipv4_chksum)
4402 adapter->netdev->hw_features |= NETIF_F_IP_CSUM;
4403
4404 if (buf->tcp_ipv6_chksum || buf->udp_ipv6_chksum)
4405 adapter->netdev->hw_features |= NETIF_F_IPV6_CSUM;
4406
4407 if ((adapter->netdev->features &
4408 (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)))
4409 adapter->netdev->hw_features |= NETIF_F_RXCSUM;
4410
4411 if (buf->large_tx_ipv4)
4412 adapter->netdev->hw_features |= NETIF_F_TSO;
4413 if (buf->large_tx_ipv6)
4414 adapter->netdev->hw_features |= NETIF_F_TSO6;
4415
4416 if (adapter->state == VNIC_PROBING) {
4417 adapter->netdev->features |= adapter->netdev->hw_features;
4418 } else if (old_hw_features != adapter->netdev->hw_features) {
4419 netdev_features_t tmp = 0;
4420
4421 /* disable features no longer supported */
4422 adapter->netdev->features &= adapter->netdev->hw_features;
4423 /* turn on features now supported if previously enabled */
4424 tmp = (old_hw_features ^ adapter->netdev->hw_features) &
4425 adapter->netdev->hw_features;
4426 adapter->netdev->features |=
4427 tmp & adapter->netdev->wanted_features;
4428 }
4429
4430 memset(&crq, 0, sizeof(crq));
4431 crq.control_ip_offload.first = IBMVNIC_CRQ_CMD;
4432 crq.control_ip_offload.cmd = CONTROL_IP_OFFLOAD;
4433 crq.control_ip_offload.len =
4434 cpu_to_be32(sizeof(adapter->ip_offload_ctrl));
4435 crq.control_ip_offload.ioba = cpu_to_be32(adapter->ip_offload_ctrl_tok);
4436 ibmvnic_send_crq(adapter, &crq);
4437}
4438
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02004439static void handle_vpd_size_rsp(union ibmvnic_crq *crq,
4440 struct ibmvnic_adapter *adapter)
4441{
4442 struct device *dev = &adapter->vdev->dev;
4443
4444 if (crq->get_vpd_size_rsp.rc.code) {
4445 dev_err(dev, "Error retrieving VPD size, rc=%x\n",
4446 crq->get_vpd_size_rsp.rc.code);
4447 complete(&adapter->fw_done);
4448 return;
4449 }
4450
4451 adapter->vpd->len = be64_to_cpu(crq->get_vpd_size_rsp.len);
4452 complete(&adapter->fw_done);
4453}
4454
4455static void handle_vpd_rsp(union ibmvnic_crq *crq,
4456 struct ibmvnic_adapter *adapter)
4457{
4458 struct device *dev = &adapter->vdev->dev;
Desnes Augusto Nunes do Rosario21a25452018-02-05 14:33:55 -02004459 unsigned char *substr = NULL;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02004460 u8 fw_level_len = 0;
4461
4462 memset(adapter->fw_version, 0, 32);
4463
4464 dma_unmap_single(dev, adapter->vpd->dma_addr, adapter->vpd->len,
4465 DMA_FROM_DEVICE);
4466
4467 if (crq->get_vpd_rsp.rc.code) {
4468 dev_err(dev, "Error retrieving VPD from device, rc=%x\n",
4469 crq->get_vpd_rsp.rc.code);
4470 goto complete;
4471 }
4472
4473 /* get the position of the firmware version info
4474 * located after the ASCII 'RM' substring in the buffer
4475 */
4476 substr = strnstr(adapter->vpd->buff, "RM", adapter->vpd->len);
4477 if (!substr) {
Desnes Augusto Nunes do Rosarioa1073112018-02-01 16:04:30 -02004478 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 -02004479 goto complete;
4480 }
4481
4482 /* get length of firmware level ASCII substring */
4483 if ((substr + 2) < (adapter->vpd->buff + adapter->vpd->len)) {
4484 fw_level_len = *(substr + 2);
4485 } else {
4486 dev_info(dev, "Length of FW substr extrapolated VDP buff\n");
4487 goto complete;
4488 }
4489
4490 /* copy firmware version string from vpd into adapter */
4491 if ((substr + 3 + fw_level_len) <
4492 (adapter->vpd->buff + adapter->vpd->len)) {
Desnes Augusto Nunes do Rosario21a25452018-02-05 14:33:55 -02004493 strncpy((char *)adapter->fw_version, substr + 3, fw_level_len);
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02004494 } else {
4495 dev_info(dev, "FW substr extrapolated VPD buff\n");
4496 }
4497
4498complete:
Desnes Augusto Nunes do Rosario21a25452018-02-05 14:33:55 -02004499 if (adapter->fw_version[0] == '\0')
Lijun Pan0b217d32021-06-11 13:33:53 -05004500 strscpy((char *)adapter->fw_version, "N/A", sizeof(adapter->fw_version));
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02004501 complete(&adapter->fw_done);
4502}
4503
Thomas Falcon032c5e82015-12-21 11:26:06 -06004504static void handle_query_ip_offload_rsp(struct ibmvnic_adapter *adapter)
4505{
4506 struct device *dev = &adapter->vdev->dev;
4507 struct ibmvnic_query_ip_offload_buffer *buf = &adapter->ip_offload_buf;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004508 int i;
4509
4510 dma_unmap_single(dev, adapter->ip_offload_tok,
4511 sizeof(adapter->ip_offload_buf), DMA_FROM_DEVICE);
4512
4513 netdev_dbg(adapter->netdev, "Query IP Offload Buffer:\n");
4514 for (i = 0; i < (sizeof(adapter->ip_offload_buf) - 1) / 8 + 1; i++)
4515 netdev_dbg(adapter->netdev, "%016lx\n",
Lijun Pan429aa362021-02-11 00:43:18 -06004516 ((unsigned long *)(buf))[i]);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004517
4518 netdev_dbg(adapter->netdev, "ipv4_chksum = %d\n", buf->ipv4_chksum);
4519 netdev_dbg(adapter->netdev, "ipv6_chksum = %d\n", buf->ipv6_chksum);
4520 netdev_dbg(adapter->netdev, "tcp_ipv4_chksum = %d\n",
4521 buf->tcp_ipv4_chksum);
4522 netdev_dbg(adapter->netdev, "tcp_ipv6_chksum = %d\n",
4523 buf->tcp_ipv6_chksum);
4524 netdev_dbg(adapter->netdev, "udp_ipv4_chksum = %d\n",
4525 buf->udp_ipv4_chksum);
4526 netdev_dbg(adapter->netdev, "udp_ipv6_chksum = %d\n",
4527 buf->udp_ipv6_chksum);
4528 netdev_dbg(adapter->netdev, "large_tx_ipv4 = %d\n",
4529 buf->large_tx_ipv4);
4530 netdev_dbg(adapter->netdev, "large_tx_ipv6 = %d\n",
4531 buf->large_tx_ipv6);
4532 netdev_dbg(adapter->netdev, "large_rx_ipv4 = %d\n",
4533 buf->large_rx_ipv4);
4534 netdev_dbg(adapter->netdev, "large_rx_ipv6 = %d\n",
4535 buf->large_rx_ipv6);
4536 netdev_dbg(adapter->netdev, "max_ipv4_hdr_sz = %d\n",
4537 buf->max_ipv4_header_size);
4538 netdev_dbg(adapter->netdev, "max_ipv6_hdr_sz = %d\n",
4539 buf->max_ipv6_header_size);
4540 netdev_dbg(adapter->netdev, "max_tcp_hdr_size = %d\n",
4541 buf->max_tcp_header_size);
4542 netdev_dbg(adapter->netdev, "max_udp_hdr_size = %d\n",
4543 buf->max_udp_header_size);
4544 netdev_dbg(adapter->netdev, "max_large_tx_size = %d\n",
4545 buf->max_large_tx_size);
4546 netdev_dbg(adapter->netdev, "max_large_rx_size = %d\n",
4547 buf->max_large_rx_size);
4548 netdev_dbg(adapter->netdev, "ipv6_ext_hdr = %d\n",
4549 buf->ipv6_extension_header);
4550 netdev_dbg(adapter->netdev, "tcp_pseudosum_req = %d\n",
4551 buf->tcp_pseudosum_req);
4552 netdev_dbg(adapter->netdev, "num_ipv6_ext_hd = %d\n",
4553 buf->num_ipv6_ext_headers);
4554 netdev_dbg(adapter->netdev, "off_ipv6_ext_hd = %d\n",
4555 buf->off_ipv6_ext_headers);
4556
Lijun Pan46899bd2020-09-27 20:13:30 -05004557 send_control_ip_offload(adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004558}
4559
Thomas Falconc9008d32018-08-06 21:39:59 -05004560static const char *ibmvnic_fw_err_cause(u16 cause)
4561{
4562 switch (cause) {
4563 case ADAPTER_PROBLEM:
4564 return "adapter problem";
4565 case BUS_PROBLEM:
4566 return "bus problem";
4567 case FW_PROBLEM:
4568 return "firmware problem";
4569 case DD_PROBLEM:
4570 return "device driver problem";
4571 case EEH_RECOVERY:
4572 return "EEH recovery";
4573 case FW_UPDATED:
4574 return "firmware updated";
4575 case LOW_MEMORY:
4576 return "low Memory";
4577 default:
4578 return "unknown";
4579 }
4580}
4581
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04004582static void handle_error_indication(union ibmvnic_crq *crq,
4583 struct ibmvnic_adapter *adapter)
4584{
4585 struct device *dev = &adapter->vdev->dev;
Thomas Falconc9008d32018-08-06 21:39:59 -05004586 u16 cause;
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04004587
Thomas Falconc9008d32018-08-06 21:39:59 -05004588 cause = be16_to_cpu(crq->error_indication.error_cause);
4589
4590 dev_warn_ratelimited(dev,
4591 "Firmware reports %serror, cause: %s. Starting recovery...\n",
4592 crq->error_indication.flags
4593 & IBMVNIC_FATAL_ERROR ? "FATAL " : "",
4594 ibmvnic_fw_err_cause(cause));
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04004595
Nathan Fontenoted651a12017-05-03 14:04:38 -04004596 if (crq->error_indication.flags & IBMVNIC_FATAL_ERROR)
4597 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
John Allen8cb31cf2017-05-26 10:30:37 -04004598 else
4599 ibmvnic_reset(adapter, VNIC_RESET_NON_FATAL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004600}
4601
Thomas Falconf8136142018-01-29 13:45:05 -06004602static int handle_change_mac_rsp(union ibmvnic_crq *crq,
4603 struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06004604{
4605 struct net_device *netdev = adapter->netdev;
4606 struct device *dev = &adapter->vdev->dev;
4607 long rc;
4608
4609 rc = crq->change_mac_addr_rsp.rc.code;
4610 if (rc) {
4611 dev_err(dev, "Error %ld in CHANGE_MAC_ADDR_RSP\n", rc);
Thomas Falconf8136142018-01-29 13:45:05 -06004612 goto out;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004613 }
Lijun Pand9b0e592020-10-20 17:39:19 -05004614 /* crq->change_mac_addr.mac_addr is the requested one
4615 * crq->change_mac_addr_rsp.mac_addr is the returned valid one.
4616 */
Thomas Falcon62740e92019-05-09 23:13:43 -05004617 ether_addr_copy(netdev->dev_addr,
4618 &crq->change_mac_addr_rsp.mac_addr[0]);
Lijun Pand9b0e592020-10-20 17:39:19 -05004619 ether_addr_copy(adapter->mac_addr,
4620 &crq->change_mac_addr_rsp.mac_addr[0]);
Thomas Falconf8136142018-01-29 13:45:05 -06004621out:
4622 complete(&adapter->fw_done);
4623 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004624}
4625
4626static void handle_request_cap_rsp(union ibmvnic_crq *crq,
4627 struct ibmvnic_adapter *adapter)
4628{
4629 struct device *dev = &adapter->vdev->dev;
4630 u64 *req_value;
4631 char *name;
4632
Thomas Falcon901e0402017-02-15 12:17:59 -06004633 atomic_dec(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004634 switch (be16_to_cpu(crq->request_capability_rsp.capability)) {
4635 case REQ_TX_QUEUES:
4636 req_value = &adapter->req_tx_queues;
4637 name = "tx";
4638 break;
4639 case REQ_RX_QUEUES:
4640 req_value = &adapter->req_rx_queues;
4641 name = "rx";
4642 break;
4643 case REQ_RX_ADD_QUEUES:
4644 req_value = &adapter->req_rx_add_queues;
4645 name = "rx_add";
4646 break;
4647 case REQ_TX_ENTRIES_PER_SUBCRQ:
4648 req_value = &adapter->req_tx_entries_per_subcrq;
4649 name = "tx_entries_per_subcrq";
4650 break;
4651 case REQ_RX_ADD_ENTRIES_PER_SUBCRQ:
4652 req_value = &adapter->req_rx_add_entries_per_subcrq;
4653 name = "rx_add_entries_per_subcrq";
4654 break;
4655 case REQ_MTU:
4656 req_value = &adapter->req_mtu;
4657 name = "mtu";
4658 break;
4659 case PROMISC_REQUESTED:
4660 req_value = &adapter->promisc;
4661 name = "promisc";
4662 break;
4663 default:
4664 dev_err(dev, "Got invalid cap request rsp %d\n",
4665 crq->request_capability.capability);
4666 return;
4667 }
4668
4669 switch (crq->request_capability_rsp.rc.code) {
4670 case SUCCESS:
4671 break;
4672 case PARTIALSUCCESS:
4673 dev_info(dev, "req=%lld, rsp=%ld in %s queue, retrying.\n",
4674 *req_value,
Lijun Pan914789a2021-02-11 00:43:21 -06004675 (long)be64_to_cpu(crq->request_capability_rsp.number),
4676 name);
John Allene7913802018-01-18 16:27:12 -06004677
4678 if (be16_to_cpu(crq->request_capability_rsp.capability) ==
4679 REQ_MTU) {
4680 pr_err("mtu of %llu is not supported. Reverting.\n",
4681 *req_value);
4682 *req_value = adapter->fallback.mtu;
4683 } else {
4684 *req_value =
4685 be64_to_cpu(crq->request_capability_rsp.number);
4686 }
4687
Lijun Pan09081b92020-09-27 20:13:27 -05004688 send_request_cap(adapter, 1);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004689 return;
4690 default:
4691 dev_err(dev, "Error %d in request cap rsp\n",
4692 crq->request_capability_rsp.rc.code);
4693 return;
4694 }
4695
4696 /* Done receiving requested capabilities, query IP offload support */
Thomas Falcon901e0402017-02-15 12:17:59 -06004697 if (atomic_read(&adapter->running_cap_crqs) == 0) {
Thomas Falcon249168a2017-02-15 12:18:00 -06004698 adapter->wait_capability = false;
Lijun Pan16e811f2020-09-27 20:13:29 -05004699 send_query_ip_offload(adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004700 }
4701}
4702
4703static int handle_login_rsp(union ibmvnic_crq *login_rsp_crq,
4704 struct ibmvnic_adapter *adapter)
4705{
4706 struct device *dev = &adapter->vdev->dev;
John Allenc26eba02017-10-26 16:23:25 -05004707 struct net_device *netdev = adapter->netdev;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004708 struct ibmvnic_login_rsp_buffer *login_rsp = adapter->login_rsp_buf;
4709 struct ibmvnic_login_buffer *login = adapter->login_buf;
Cristobal Fornof3ae59c2020-08-19 13:16:23 -05004710 u64 *tx_handle_array;
4711 u64 *rx_handle_array;
4712 int num_tx_pools;
4713 int num_rx_pools;
Thomas Falcon507ebe62020-08-21 13:39:01 -05004714 u64 *size_array;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004715 int i;
4716
Sukadev Bhattiprolu76cdc5c2020-11-25 18:04:29 -06004717 /* CHECK: Test/set of login_pending does not need to be atomic
4718 * because only ibmvnic_tasklet tests/clears this.
4719 */
4720 if (!adapter->login_pending) {
4721 netdev_warn(netdev, "Ignoring unexpected login response\n");
4722 return 0;
4723 }
4724 adapter->login_pending = false;
4725
Thomas Falcon032c5e82015-12-21 11:26:06 -06004726 dma_unmap_single(dev, adapter->login_buf_token, adapter->login_buf_sz,
Thomas Falcon37e40fa2018-04-06 18:37:02 -05004727 DMA_TO_DEVICE);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004728 dma_unmap_single(dev, adapter->login_rsp_buf_token,
Thomas Falcon37e40fa2018-04-06 18:37:02 -05004729 adapter->login_rsp_buf_sz, DMA_FROM_DEVICE);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004730
John Allen498cd8e2016-04-06 11:49:55 -05004731 /* If the number of queues requested can't be allocated by the
4732 * server, the login response will return with code 1. We will need
4733 * to resend the login buffer with fewer queues requested.
4734 */
4735 if (login_rsp_crq->generic.rc.code) {
Nathan Fontenot64d92aa2018-04-11 10:09:32 -05004736 adapter->init_done_rc = login_rsp_crq->generic.rc.code;
John Allen498cd8e2016-04-06 11:49:55 -05004737 complete(&adapter->init_done);
4738 return 0;
4739 }
4740
John Allenc26eba02017-10-26 16:23:25 -05004741 netdev->mtu = adapter->req_mtu - ETH_HLEN;
4742
Thomas Falcon032c5e82015-12-21 11:26:06 -06004743 netdev_dbg(adapter->netdev, "Login Response Buffer:\n");
4744 for (i = 0; i < (adapter->login_rsp_buf_sz - 1) / 8 + 1; i++) {
4745 netdev_dbg(adapter->netdev, "%016lx\n",
Lijun Pan429aa362021-02-11 00:43:18 -06004746 ((unsigned long *)(adapter->login_rsp_buf))[i]);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004747 }
4748
4749 /* Sanity checks */
4750 if (login->num_txcomp_subcrqs != login_rsp->num_txsubm_subcrqs ||
4751 (be32_to_cpu(login->num_rxcomp_subcrqs) *
4752 adapter->req_rx_add_queues !=
4753 be32_to_cpu(login_rsp->num_rxadd_subcrqs))) {
4754 dev_err(dev, "FATAL: Inconsistent login and login rsp\n");
Dany Madden31d6b402020-11-25 18:04:24 -06004755 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004756 return -EIO;
4757 }
Thomas Falcon507ebe62020-08-21 13:39:01 -05004758 size_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
4759 be32_to_cpu(adapter->login_rsp_buf->off_rxadd_buff_size));
4760 /* variable buffer sizes are not supported, so just read the
4761 * first entry.
4762 */
4763 adapter->cur_rx_buf_sz = be64_to_cpu(size_array[0]);
Cristobal Fornof3ae59c2020-08-19 13:16:23 -05004764
4765 num_tx_pools = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
4766 num_rx_pools = be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
4767
4768 tx_handle_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
4769 be32_to_cpu(adapter->login_rsp_buf->off_txsubm_subcrqs));
4770 rx_handle_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
4771 be32_to_cpu(adapter->login_rsp_buf->off_rxadd_subcrqs));
4772
4773 for (i = 0; i < num_tx_pools; i++)
4774 adapter->tx_scrq[i]->handle = tx_handle_array[i];
4775
4776 for (i = 0; i < num_rx_pools; i++)
4777 adapter->rx_scrq[i]->handle = rx_handle_array[i];
4778
Thomas Falcon507ebe62020-08-21 13:39:01 -05004779 adapter->num_active_tx_scrqs = num_tx_pools;
4780 adapter->num_active_rx_scrqs = num_rx_pools;
Cristobal Fornof3ae59c2020-08-19 13:16:23 -05004781 release_login_rsp_buffer(adapter);
Thomas Falcona2c0f032018-02-21 18:18:30 -06004782 release_login_buffer(adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004783 complete(&adapter->init_done);
4784
Thomas Falcon032c5e82015-12-21 11:26:06 -06004785 return 0;
4786}
4787
Thomas Falcon032c5e82015-12-21 11:26:06 -06004788static void handle_request_unmap_rsp(union ibmvnic_crq *crq,
4789 struct ibmvnic_adapter *adapter)
4790{
4791 struct device *dev = &adapter->vdev->dev;
4792 long rc;
4793
4794 rc = crq->request_unmap_rsp.rc.code;
4795 if (rc)
4796 dev_err(dev, "Error %ld in REQUEST_UNMAP_RSP\n", rc);
4797}
4798
4799static void handle_query_map_rsp(union ibmvnic_crq *crq,
4800 struct ibmvnic_adapter *adapter)
4801{
4802 struct net_device *netdev = adapter->netdev;
4803 struct device *dev = &adapter->vdev->dev;
4804 long rc;
4805
4806 rc = crq->query_map_rsp.rc.code;
4807 if (rc) {
4808 dev_err(dev, "Error %ld in QUERY_MAP_RSP\n", rc);
4809 return;
4810 }
Sukadev Bhattiprolu0f2bf312021-09-14 20:52:52 -07004811 netdev_dbg(netdev, "page_size = %d\ntot_pages = %u\nfree_pages = %u\n",
4812 crq->query_map_rsp.page_size,
4813 __be32_to_cpu(crq->query_map_rsp.tot_pages),
4814 __be32_to_cpu(crq->query_map_rsp.free_pages));
Thomas Falcon032c5e82015-12-21 11:26:06 -06004815}
4816
4817static void handle_query_cap_rsp(union ibmvnic_crq *crq,
4818 struct ibmvnic_adapter *adapter)
4819{
4820 struct net_device *netdev = adapter->netdev;
4821 struct device *dev = &adapter->vdev->dev;
4822 long rc;
4823
Thomas Falcon901e0402017-02-15 12:17:59 -06004824 atomic_dec(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004825 netdev_dbg(netdev, "Outstanding queries: %d\n",
Thomas Falcon901e0402017-02-15 12:17:59 -06004826 atomic_read(&adapter->running_cap_crqs));
Thomas Falcon032c5e82015-12-21 11:26:06 -06004827 rc = crq->query_capability.rc.code;
4828 if (rc) {
4829 dev_err(dev, "Error %ld in QUERY_CAP_RSP\n", rc);
4830 goto out;
4831 }
4832
4833 switch (be16_to_cpu(crq->query_capability.capability)) {
4834 case MIN_TX_QUEUES:
4835 adapter->min_tx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004836 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004837 netdev_dbg(netdev, "min_tx_queues = %lld\n",
4838 adapter->min_tx_queues);
4839 break;
4840 case MIN_RX_QUEUES:
4841 adapter->min_rx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004842 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004843 netdev_dbg(netdev, "min_rx_queues = %lld\n",
4844 adapter->min_rx_queues);
4845 break;
4846 case MIN_RX_ADD_QUEUES:
4847 adapter->min_rx_add_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004848 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004849 netdev_dbg(netdev, "min_rx_add_queues = %lld\n",
4850 adapter->min_rx_add_queues);
4851 break;
4852 case MAX_TX_QUEUES:
4853 adapter->max_tx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004854 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004855 netdev_dbg(netdev, "max_tx_queues = %lld\n",
4856 adapter->max_tx_queues);
4857 break;
4858 case MAX_RX_QUEUES:
4859 adapter->max_rx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004860 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004861 netdev_dbg(netdev, "max_rx_queues = %lld\n",
4862 adapter->max_rx_queues);
4863 break;
4864 case MAX_RX_ADD_QUEUES:
4865 adapter->max_rx_add_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004866 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004867 netdev_dbg(netdev, "max_rx_add_queues = %lld\n",
4868 adapter->max_rx_add_queues);
4869 break;
4870 case MIN_TX_ENTRIES_PER_SUBCRQ:
4871 adapter->min_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06004872 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004873 netdev_dbg(netdev, "min_tx_entries_per_subcrq = %lld\n",
4874 adapter->min_tx_entries_per_subcrq);
4875 break;
4876 case MIN_RX_ADD_ENTRIES_PER_SUBCRQ:
4877 adapter->min_rx_add_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06004878 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004879 netdev_dbg(netdev, "min_rx_add_entrs_per_subcrq = %lld\n",
4880 adapter->min_rx_add_entries_per_subcrq);
4881 break;
4882 case MAX_TX_ENTRIES_PER_SUBCRQ:
4883 adapter->max_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06004884 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004885 netdev_dbg(netdev, "max_tx_entries_per_subcrq = %lld\n",
4886 adapter->max_tx_entries_per_subcrq);
4887 break;
4888 case MAX_RX_ADD_ENTRIES_PER_SUBCRQ:
4889 adapter->max_rx_add_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06004890 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004891 netdev_dbg(netdev, "max_rx_add_entrs_per_subcrq = %lld\n",
4892 adapter->max_rx_add_entries_per_subcrq);
4893 break;
4894 case TCP_IP_OFFLOAD:
4895 adapter->tcp_ip_offload =
Thomas Falconde89e852016-03-01 10:20:09 -06004896 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004897 netdev_dbg(netdev, "tcp_ip_offload = %lld\n",
4898 adapter->tcp_ip_offload);
4899 break;
4900 case PROMISC_SUPPORTED:
4901 adapter->promisc_supported =
Thomas Falconde89e852016-03-01 10:20:09 -06004902 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004903 netdev_dbg(netdev, "promisc_supported = %lld\n",
4904 adapter->promisc_supported);
4905 break;
4906 case MIN_MTU:
Thomas Falconde89e852016-03-01 10:20:09 -06004907 adapter->min_mtu = be64_to_cpu(crq->query_capability.number);
Thomas Falconf39f0d12017-02-14 10:22:59 -06004908 netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004909 netdev_dbg(netdev, "min_mtu = %lld\n", adapter->min_mtu);
4910 break;
4911 case MAX_MTU:
Thomas Falconde89e852016-03-01 10:20:09 -06004912 adapter->max_mtu = be64_to_cpu(crq->query_capability.number);
Thomas Falconf39f0d12017-02-14 10:22:59 -06004913 netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004914 netdev_dbg(netdev, "max_mtu = %lld\n", adapter->max_mtu);
4915 break;
4916 case MAX_MULTICAST_FILTERS:
4917 adapter->max_multicast_filters =
Thomas Falconde89e852016-03-01 10:20:09 -06004918 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004919 netdev_dbg(netdev, "max_multicast_filters = %lld\n",
4920 adapter->max_multicast_filters);
4921 break;
4922 case VLAN_HEADER_INSERTION:
4923 adapter->vlan_header_insertion =
Thomas Falconde89e852016-03-01 10:20:09 -06004924 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004925 if (adapter->vlan_header_insertion)
4926 netdev->features |= NETIF_F_HW_VLAN_STAG_TX;
4927 netdev_dbg(netdev, "vlan_header_insertion = %lld\n",
4928 adapter->vlan_header_insertion);
4929 break;
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04004930 case RX_VLAN_HEADER_INSERTION:
4931 adapter->rx_vlan_header_insertion =
4932 be64_to_cpu(crq->query_capability.number);
4933 netdev_dbg(netdev, "rx_vlan_header_insertion = %lld\n",
4934 adapter->rx_vlan_header_insertion);
4935 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004936 case MAX_TX_SG_ENTRIES:
4937 adapter->max_tx_sg_entries =
Thomas Falconde89e852016-03-01 10:20:09 -06004938 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004939 netdev_dbg(netdev, "max_tx_sg_entries = %lld\n",
4940 adapter->max_tx_sg_entries);
4941 break;
4942 case RX_SG_SUPPORTED:
4943 adapter->rx_sg_supported =
Thomas Falconde89e852016-03-01 10:20:09 -06004944 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004945 netdev_dbg(netdev, "rx_sg_supported = %lld\n",
4946 adapter->rx_sg_supported);
4947 break;
4948 case OPT_TX_COMP_SUB_QUEUES:
4949 adapter->opt_tx_comp_sub_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004950 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004951 netdev_dbg(netdev, "opt_tx_comp_sub_queues = %lld\n",
4952 adapter->opt_tx_comp_sub_queues);
4953 break;
4954 case OPT_RX_COMP_QUEUES:
4955 adapter->opt_rx_comp_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004956 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004957 netdev_dbg(netdev, "opt_rx_comp_queues = %lld\n",
4958 adapter->opt_rx_comp_queues);
4959 break;
4960 case OPT_RX_BUFADD_Q_PER_RX_COMP_Q:
4961 adapter->opt_rx_bufadd_q_per_rx_comp_q =
Thomas Falconde89e852016-03-01 10:20:09 -06004962 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004963 netdev_dbg(netdev, "opt_rx_bufadd_q_per_rx_comp_q = %lld\n",
4964 adapter->opt_rx_bufadd_q_per_rx_comp_q);
4965 break;
4966 case OPT_TX_ENTRIES_PER_SUBCRQ:
4967 adapter->opt_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06004968 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004969 netdev_dbg(netdev, "opt_tx_entries_per_subcrq = %lld\n",
4970 adapter->opt_tx_entries_per_subcrq);
4971 break;
4972 case OPT_RXBA_ENTRIES_PER_SUBCRQ:
4973 adapter->opt_rxba_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06004974 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004975 netdev_dbg(netdev, "opt_rxba_entries_per_subcrq = %lld\n",
4976 adapter->opt_rxba_entries_per_subcrq);
4977 break;
4978 case TX_RX_DESC_REQ:
4979 adapter->tx_rx_desc_req = crq->query_capability.number;
4980 netdev_dbg(netdev, "tx_rx_desc_req = %llx\n",
4981 adapter->tx_rx_desc_req);
4982 break;
4983
4984 default:
4985 netdev_err(netdev, "Got invalid cap rsp %d\n",
4986 crq->query_capability.capability);
4987 }
4988
4989out:
Thomas Falcon249168a2017-02-15 12:18:00 -06004990 if (atomic_read(&adapter->running_cap_crqs) == 0) {
4991 adapter->wait_capability = false;
Lijun Pan09081b92020-09-27 20:13:27 -05004992 send_request_cap(adapter, 0);
Thomas Falcon249168a2017-02-15 12:18:00 -06004993 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06004994}
4995
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03004996static int send_query_phys_parms(struct ibmvnic_adapter *adapter)
4997{
4998 union ibmvnic_crq crq;
4999 int rc;
5000
5001 memset(&crq, 0, sizeof(crq));
5002 crq.query_phys_parms.first = IBMVNIC_CRQ_CMD;
5003 crq.query_phys_parms.cmd = QUERY_PHYS_PARMS;
Thomas Falconff25dcb2019-11-25 17:12:56 -06005004
5005 mutex_lock(&adapter->fw_lock);
5006 adapter->fw_done_rc = 0;
Thomas Falcon070eca92019-11-25 17:12:53 -06005007 reinit_completion(&adapter->fw_done);
Thomas Falconff25dcb2019-11-25 17:12:56 -06005008
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03005009 rc = ibmvnic_send_crq(adapter, &crq);
Thomas Falconff25dcb2019-11-25 17:12:56 -06005010 if (rc) {
5011 mutex_unlock(&adapter->fw_lock);
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03005012 return rc;
Thomas Falconff25dcb2019-11-25 17:12:56 -06005013 }
Thomas Falcon476d96c2019-11-25 17:12:55 -06005014
5015 rc = ibmvnic_wait_for_completion(adapter, &adapter->fw_done, 10000);
Thomas Falconff25dcb2019-11-25 17:12:56 -06005016 if (rc) {
5017 mutex_unlock(&adapter->fw_lock);
Thomas Falcon476d96c2019-11-25 17:12:55 -06005018 return rc;
Thomas Falconff25dcb2019-11-25 17:12:56 -06005019 }
Thomas Falcon476d96c2019-11-25 17:12:55 -06005020
Thomas Falconff25dcb2019-11-25 17:12:56 -06005021 mutex_unlock(&adapter->fw_lock);
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03005022 return adapter->fw_done_rc ? -EIO : 0;
5023}
5024
5025static int handle_query_phys_parms_rsp(union ibmvnic_crq *crq,
5026 struct ibmvnic_adapter *adapter)
5027{
5028 struct net_device *netdev = adapter->netdev;
5029 int rc;
Murilo Fossa Vicentinidd0f9d82019-09-16 11:50:37 -03005030 __be32 rspeed = cpu_to_be32(crq->query_phys_parms_rsp.speed);
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03005031
5032 rc = crq->query_phys_parms_rsp.rc.code;
5033 if (rc) {
5034 netdev_err(netdev, "Error %d in QUERY_PHYS_PARMS\n", rc);
5035 return rc;
5036 }
Murilo Fossa Vicentinidd0f9d82019-09-16 11:50:37 -03005037 switch (rspeed) {
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03005038 case IBMVNIC_10MBPS:
5039 adapter->speed = SPEED_10;
5040 break;
5041 case IBMVNIC_100MBPS:
5042 adapter->speed = SPEED_100;
5043 break;
5044 case IBMVNIC_1GBPS:
5045 adapter->speed = SPEED_1000;
5046 break;
Lijun Panb9cd7952020-09-27 19:06:25 -05005047 case IBMVNIC_10GBPS:
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03005048 adapter->speed = SPEED_10000;
5049 break;
5050 case IBMVNIC_25GBPS:
5051 adapter->speed = SPEED_25000;
5052 break;
5053 case IBMVNIC_40GBPS:
5054 adapter->speed = SPEED_40000;
5055 break;
5056 case IBMVNIC_50GBPS:
5057 adapter->speed = SPEED_50000;
5058 break;
5059 case IBMVNIC_100GBPS:
5060 adapter->speed = SPEED_100000;
5061 break;
Lijun Panb9cd7952020-09-27 19:06:25 -05005062 case IBMVNIC_200GBPS:
5063 adapter->speed = SPEED_200000;
5064 break;
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03005065 default:
Murilo Fossa Vicentinidd0f9d82019-09-16 11:50:37 -03005066 if (netif_carrier_ok(netdev))
5067 netdev_warn(netdev, "Unknown speed 0x%08x\n", rspeed);
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03005068 adapter->speed = SPEED_UNKNOWN;
5069 }
5070 if (crq->query_phys_parms_rsp.flags1 & IBMVNIC_FULL_DUPLEX)
5071 adapter->duplex = DUPLEX_FULL;
5072 else if (crq->query_phys_parms_rsp.flags1 & IBMVNIC_HALF_DUPLEX)
5073 adapter->duplex = DUPLEX_HALF;
5074 else
5075 adapter->duplex = DUPLEX_UNKNOWN;
5076
5077 return rc;
5078}
5079
Thomas Falcon032c5e82015-12-21 11:26:06 -06005080static void ibmvnic_handle_crq(union ibmvnic_crq *crq,
5081 struct ibmvnic_adapter *adapter)
5082{
5083 struct ibmvnic_generic_crq *gen_crq = &crq->generic;
5084 struct net_device *netdev = adapter->netdev;
5085 struct device *dev = &adapter->vdev->dev;
Murilo Fossa Vicentini993a82b2017-04-19 13:44:35 -04005086 u64 *u64_crq = (u64 *)crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005087 long rc;
5088
5089 netdev_dbg(netdev, "Handling CRQ: %016lx %016lx\n",
Lijun Pan429aa362021-02-11 00:43:18 -06005090 (unsigned long)cpu_to_be64(u64_crq[0]),
5091 (unsigned long)cpu_to_be64(u64_crq[1]));
Thomas Falcon032c5e82015-12-21 11:26:06 -06005092 switch (gen_crq->first) {
5093 case IBMVNIC_CRQ_INIT_RSP:
5094 switch (gen_crq->cmd) {
5095 case IBMVNIC_CRQ_INIT:
5096 dev_info(dev, "Partner initialized\n");
John Allen017892c12017-05-26 10:30:19 -04005097 adapter->from_passive_init = true;
Sukadev Bhattiprolu76cdc5c2020-11-25 18:04:29 -06005098 /* Discard any stale login responses from prev reset.
5099 * CHECK: should we clear even on INIT_COMPLETE?
5100 */
5101 adapter->login_pending = false;
5102
Thomas Falcon17c87052018-05-23 13:37:58 -05005103 if (!completion_done(&adapter->init_done)) {
5104 complete(&adapter->init_done);
5105 adapter->init_done_rc = -EIO;
5106 }
Cristobal Forno53f8b1b2021-06-10 11:08:35 -06005107
5108 if (adapter->state == VNIC_DOWN)
5109 rc = ibmvnic_reset(adapter, VNIC_RESET_PASSIVE_INIT);
5110 else
5111 rc = ibmvnic_reset(adapter, VNIC_RESET_FAILOVER);
5112
Sukadev Bhattiproluef66a1e2021-02-02 21:08:02 -08005113 if (rc && rc != -EBUSY) {
5114 /* We were unable to schedule the failover
5115 * reset either because the adapter was still
5116 * probing (eg: during kexec) or we could not
5117 * allocate memory. Clear the failover_pending
5118 * flag since no one else will. We ignore
5119 * EBUSY because it means either FAILOVER reset
5120 * is already scheduled or the adapter is
5121 * being removed.
5122 */
5123 netdev_err(netdev,
5124 "Error %ld scheduling failover reset\n",
5125 rc);
5126 adapter->failover_pending = false;
5127 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06005128 break;
5129 case IBMVNIC_CRQ_INIT_COMPLETE:
5130 dev_info(dev, "Partner initialization complete\n");
Thomas Falcon51536982018-05-23 13:37:56 -05005131 adapter->crq.active = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005132 send_version_xchg(adapter);
5133 break;
5134 default:
5135 dev_err(dev, "Unknown crq cmd: %d\n", gen_crq->cmd);
5136 }
5137 return;
5138 case IBMVNIC_CRQ_XPORT_EVENT:
Nathan Fontenoted651a12017-05-03 14:04:38 -04005139 netif_carrier_off(netdev);
Thomas Falcon51536982018-05-23 13:37:56 -05005140 adapter->crq.active = false;
Thomas Falcon2147e3d2019-11-25 17:12:54 -06005141 /* terminate any thread waiting for a response
5142 * from the device
5143 */
5144 if (!completion_done(&adapter->fw_done)) {
5145 adapter->fw_done_rc = -EIO;
5146 complete(&adapter->fw_done);
5147 }
5148 if (!completion_done(&adapter->stats_done))
5149 complete(&adapter->stats_done);
Juliet Kim7ed5b312019-09-20 16:11:23 -04005150 if (test_bit(0, &adapter->resetting))
Thomas Falcon2770a792018-05-23 13:38:02 -05005151 adapter->force_reset_recovery = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005152 if (gen_crq->cmd == IBMVNIC_PARTITION_MIGRATED) {
Nathan Fontenoted651a12017-05-03 14:04:38 -04005153 dev_info(dev, "Migrated, re-enabling adapter\n");
5154 ibmvnic_reset(adapter, VNIC_RESET_MOBILITY);
Thomas Falcondfad09a2016-08-18 11:37:51 -05005155 } else if (gen_crq->cmd == IBMVNIC_DEVICE_FAILOVER) {
5156 dev_info(dev, "Backing device failover detected\n");
Thomas Falcon5a18e1e2018-04-06 18:37:05 -05005157 adapter->failover_pending = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005158 } else {
5159 /* The adapter lost the connection */
5160 dev_err(dev, "Virtual Adapter failed (rc=%d)\n",
5161 gen_crq->cmd);
Nathan Fontenoted651a12017-05-03 14:04:38 -04005162 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005163 }
5164 return;
5165 case IBMVNIC_CRQ_CMD_RSP:
5166 break;
5167 default:
5168 dev_err(dev, "Got an invalid msg type 0x%02x\n",
5169 gen_crq->first);
5170 return;
5171 }
5172
5173 switch (gen_crq->cmd) {
5174 case VERSION_EXCHANGE_RSP:
5175 rc = crq->version_exchange_rsp.rc.code;
5176 if (rc) {
5177 dev_err(dev, "Error %ld in VERSION_EXCHG_RSP\n", rc);
5178 break;
5179 }
Thomas Falcon78468892020-05-28 11:19:17 -05005180 ibmvnic_version =
Thomas Falcon032c5e82015-12-21 11:26:06 -06005181 be16_to_cpu(crq->version_exchange_rsp.version);
Thomas Falcon78468892020-05-28 11:19:17 -05005182 dev_info(dev, "Partner protocol version is %d\n",
5183 ibmvnic_version);
Lijun Pan491099a2020-09-27 20:13:26 -05005184 send_query_cap(adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005185 break;
5186 case QUERY_CAPABILITY_RSP:
5187 handle_query_cap_rsp(crq, adapter);
5188 break;
5189 case QUERY_MAP_RSP:
5190 handle_query_map_rsp(crq, adapter);
5191 break;
5192 case REQUEST_MAP_RSP:
Thomas Falconf3be0cb2017-06-21 14:53:01 -05005193 adapter->fw_done_rc = crq->request_map_rsp.rc.code;
5194 complete(&adapter->fw_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005195 break;
5196 case REQUEST_UNMAP_RSP:
5197 handle_request_unmap_rsp(crq, adapter);
5198 break;
5199 case REQUEST_CAPABILITY_RSP:
5200 handle_request_cap_rsp(crq, adapter);
5201 break;
5202 case LOGIN_RSP:
5203 netdev_dbg(netdev, "Got Login Response\n");
5204 handle_login_rsp(crq, adapter);
5205 break;
5206 case LOGICAL_LINK_STATE_RSP:
Nathan Fontenot53da09e2017-04-21 15:39:04 -04005207 netdev_dbg(netdev,
5208 "Got Logical Link State Response, state: %d rc: %d\n",
5209 crq->logical_link_state_rsp.link_state,
5210 crq->logical_link_state_rsp.rc.code);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005211 adapter->logical_link_state =
5212 crq->logical_link_state_rsp.link_state;
Nathan Fontenot53da09e2017-04-21 15:39:04 -04005213 adapter->init_done_rc = crq->logical_link_state_rsp.rc.code;
5214 complete(&adapter->init_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005215 break;
5216 case LINK_STATE_INDICATION:
5217 netdev_dbg(netdev, "Got Logical Link State Indication\n");
5218 adapter->phys_link_state =
5219 crq->link_state_indication.phys_link_state;
5220 adapter->logical_link_state =
5221 crq->link_state_indication.logical_link_state;
Thomas Falcon0655f992019-05-09 23:13:44 -05005222 if (adapter->phys_link_state && adapter->logical_link_state)
5223 netif_carrier_on(netdev);
5224 else
5225 netif_carrier_off(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005226 break;
5227 case CHANGE_MAC_ADDR_RSP:
5228 netdev_dbg(netdev, "Got MAC address change Response\n");
Thomas Falconf8136142018-01-29 13:45:05 -06005229 adapter->fw_done_rc = handle_change_mac_rsp(crq, adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005230 break;
5231 case ERROR_INDICATION:
5232 netdev_dbg(netdev, "Got Error Indication\n");
5233 handle_error_indication(crq, adapter);
5234 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005235 case REQUEST_STATISTICS_RSP:
5236 netdev_dbg(netdev, "Got Statistics Response\n");
5237 complete(&adapter->stats_done);
5238 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005239 case QUERY_IP_OFFLOAD_RSP:
5240 netdev_dbg(netdev, "Got Query IP offload Response\n");
5241 handle_query_ip_offload_rsp(adapter);
5242 break;
5243 case MULTICAST_CTRL_RSP:
5244 netdev_dbg(netdev, "Got multicast control Response\n");
5245 break;
5246 case CONTROL_IP_OFFLOAD_RSP:
5247 netdev_dbg(netdev, "Got Control IP offload Response\n");
5248 dma_unmap_single(dev, adapter->ip_offload_ctrl_tok,
5249 sizeof(adapter->ip_offload_ctrl),
5250 DMA_TO_DEVICE);
John Allenbd0b6722017-03-17 17:13:40 -05005251 complete(&adapter->init_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005252 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005253 case COLLECT_FW_TRACE_RSP:
5254 netdev_dbg(netdev, "Got Collect firmware trace Response\n");
5255 complete(&adapter->fw_done);
5256 break;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02005257 case GET_VPD_SIZE_RSP:
5258 handle_vpd_size_rsp(crq, adapter);
5259 break;
5260 case GET_VPD_RSP:
5261 handle_vpd_rsp(crq, adapter);
5262 break;
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03005263 case QUERY_PHYS_PARMS_RSP:
5264 adapter->fw_done_rc = handle_query_phys_parms_rsp(crq, adapter);
5265 complete(&adapter->fw_done);
5266 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005267 default:
5268 netdev_err(netdev, "Got an invalid cmd type 0x%02x\n",
5269 gen_crq->cmd);
5270 }
5271}
5272
5273static irqreturn_t ibmvnic_interrupt(int irq, void *instance)
5274{
5275 struct ibmvnic_adapter *adapter = instance;
Thomas Falcon6c267b32017-02-15 12:17:58 -06005276
Thomas Falcon6c267b32017-02-15 12:17:58 -06005277 tasklet_schedule(&adapter->tasklet);
Thomas Falcon6c267b32017-02-15 12:17:58 -06005278 return IRQ_HANDLED;
5279}
5280
Allen Paisaa7c3fe2020-09-14 12:59:29 +05305281static void ibmvnic_tasklet(struct tasklet_struct *t)
Thomas Falcon6c267b32017-02-15 12:17:58 -06005282{
Allen Paisaa7c3fe2020-09-14 12:59:29 +05305283 struct ibmvnic_adapter *adapter = from_tasklet(adapter, t, tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005284 struct ibmvnic_crq_queue *queue = &adapter->crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005285 union ibmvnic_crq *crq;
5286 unsigned long flags;
5287 bool done = false;
5288
5289 spin_lock_irqsave(&queue->lock, flags);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005290 while (!done) {
5291 /* Pull all the valid messages off the CRQ */
5292 while ((crq = ibmvnic_next_crq(adapter)) != NULL) {
Lijun Pane41aec72021-01-27 19:34:42 -06005293 /* This barrier makes sure ibmvnic_next_crq()'s
5294 * crq->generic.first & IBMVNIC_CRQ_CMD_RSP is loaded
5295 * before ibmvnic_handle_crq()'s
5296 * switch(gen_crq->first) and switch(gen_crq->cmd).
5297 */
5298 dma_rmb();
Thomas Falcon032c5e82015-12-21 11:26:06 -06005299 ibmvnic_handle_crq(crq, adapter);
5300 crq->generic.first = 0;
5301 }
Brian Kinged7ecbf2017-04-19 13:44:53 -04005302
5303 /* remain in tasklet until all
5304 * capabilities responses are received
5305 */
5306 if (!adapter->wait_capability)
5307 done = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005308 }
Thomas Falcon249168a2017-02-15 12:18:00 -06005309 /* if capabilities CRQ's were sent in this tasklet, the following
5310 * tasklet must wait until all responses are received
5311 */
5312 if (atomic_read(&adapter->running_cap_crqs) != 0)
5313 adapter->wait_capability = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005314 spin_unlock_irqrestore(&queue->lock, flags);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005315}
5316
5317static int ibmvnic_reenable_crq_queue(struct ibmvnic_adapter *adapter)
5318{
5319 struct vio_dev *vdev = adapter->vdev;
5320 int rc;
5321
5322 do {
5323 rc = plpar_hcall_norets(H_ENABLE_CRQ, vdev->unit_address);
5324 } while (rc == H_IN_PROGRESS || rc == H_BUSY || H_IS_LONG_BUSY(rc));
5325
5326 if (rc)
5327 dev_err(&vdev->dev, "Error enabling adapter (rc=%d)\n", rc);
5328
5329 return rc;
5330}
5331
5332static int ibmvnic_reset_crq(struct ibmvnic_adapter *adapter)
5333{
5334 struct ibmvnic_crq_queue *crq = &adapter->crq;
5335 struct device *dev = &adapter->vdev->dev;
5336 struct vio_dev *vdev = adapter->vdev;
5337 int rc;
5338
5339 /* Close the CRQ */
5340 do {
5341 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
5342 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
5343
5344 /* Clean out the queue */
Lijun Pan0e435be2020-11-23 13:35:46 -06005345 if (!crq->msgs)
5346 return -EINVAL;
5347
Thomas Falcon032c5e82015-12-21 11:26:06 -06005348 memset(crq->msgs, 0, PAGE_SIZE);
5349 crq->cur = 0;
Thomas Falcon51536982018-05-23 13:37:56 -05005350 crq->active = false;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005351
5352 /* And re-open it again */
5353 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
5354 crq->msg_token, PAGE_SIZE);
5355
5356 if (rc == H_CLOSED)
5357 /* Adapter is good, but other end is not ready */
5358 dev_warn(dev, "Partner adapter not ready\n");
5359 else if (rc != 0)
5360 dev_warn(dev, "Couldn't register crq (rc=%d)\n", rc);
5361
5362 return rc;
5363}
5364
Nathan Fontenotf9928872017-03-30 02:48:54 -04005365static void release_crq_queue(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06005366{
5367 struct ibmvnic_crq_queue *crq = &adapter->crq;
5368 struct vio_dev *vdev = adapter->vdev;
5369 long rc;
5370
Nathan Fontenotf9928872017-03-30 02:48:54 -04005371 if (!crq->msgs)
5372 return;
5373
Thomas Falcon032c5e82015-12-21 11:26:06 -06005374 netdev_dbg(adapter->netdev, "Releasing CRQ\n");
5375 free_irq(vdev->irq, adapter);
Thomas Falcon6c267b32017-02-15 12:17:58 -06005376 tasklet_kill(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005377 do {
5378 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
5379 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
5380
5381 dma_unmap_single(&vdev->dev, crq->msg_token, PAGE_SIZE,
5382 DMA_BIDIRECTIONAL);
5383 free_page((unsigned long)crq->msgs);
Nathan Fontenotf9928872017-03-30 02:48:54 -04005384 crq->msgs = NULL;
Thomas Falcon51536982018-05-23 13:37:56 -05005385 crq->active = false;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005386}
5387
Nathan Fontenotf9928872017-03-30 02:48:54 -04005388static int init_crq_queue(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06005389{
5390 struct ibmvnic_crq_queue *crq = &adapter->crq;
5391 struct device *dev = &adapter->vdev->dev;
5392 struct vio_dev *vdev = adapter->vdev;
5393 int rc, retrc = -ENOMEM;
5394
Nathan Fontenotf9928872017-03-30 02:48:54 -04005395 if (crq->msgs)
5396 return 0;
5397
Thomas Falcon032c5e82015-12-21 11:26:06 -06005398 crq->msgs = (union ibmvnic_crq *)get_zeroed_page(GFP_KERNEL);
5399 /* Should we allocate more than one page? */
5400
5401 if (!crq->msgs)
5402 return -ENOMEM;
5403
5404 crq->size = PAGE_SIZE / sizeof(*crq->msgs);
5405 crq->msg_token = dma_map_single(dev, crq->msgs, PAGE_SIZE,
5406 DMA_BIDIRECTIONAL);
5407 if (dma_mapping_error(dev, crq->msg_token))
5408 goto map_failed;
5409
5410 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
5411 crq->msg_token, PAGE_SIZE);
5412
5413 if (rc == H_RESOURCE)
5414 /* maybe kexecing and resource is busy. try a reset */
5415 rc = ibmvnic_reset_crq(adapter);
5416 retrc = rc;
5417
5418 if (rc == H_CLOSED) {
5419 dev_warn(dev, "Partner adapter not ready\n");
5420 } else if (rc) {
5421 dev_warn(dev, "Error %d opening adapter\n", rc);
5422 goto reg_crq_failed;
5423 }
5424
5425 retrc = 0;
5426
Allen Paisaa7c3fe2020-09-14 12:59:29 +05305427 tasklet_setup(&adapter->tasklet, (void *)ibmvnic_tasklet);
Thomas Falcon6c267b32017-02-15 12:17:58 -06005428
Thomas Falcon032c5e82015-12-21 11:26:06 -06005429 netdev_dbg(adapter->netdev, "registering irq 0x%x\n", vdev->irq);
Murilo Fossa Vicentinie56e2512019-04-25 11:02:33 -03005430 snprintf(crq->name, sizeof(crq->name), "ibmvnic-%x",
5431 adapter->vdev->unit_address);
5432 rc = request_irq(vdev->irq, ibmvnic_interrupt, 0, crq->name, adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005433 if (rc) {
5434 dev_err(dev, "Couldn't register irq 0x%x. rc=%d\n",
5435 vdev->irq, rc);
5436 goto req_irq_failed;
5437 }
5438
5439 rc = vio_enable_interrupts(vdev);
5440 if (rc) {
5441 dev_err(dev, "Error %d enabling interrupts\n", rc);
5442 goto req_irq_failed;
5443 }
5444
5445 crq->cur = 0;
5446 spin_lock_init(&crq->lock);
5447
5448 return retrc;
5449
5450req_irq_failed:
Thomas Falcon6c267b32017-02-15 12:17:58 -06005451 tasklet_kill(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005452 do {
5453 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
5454 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
5455reg_crq_failed:
5456 dma_unmap_single(dev, crq->msg_token, PAGE_SIZE, DMA_BIDIRECTIONAL);
5457map_failed:
5458 free_page((unsigned long)crq->msgs);
Nathan Fontenotf9928872017-03-30 02:48:54 -04005459 crq->msgs = NULL;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005460 return retrc;
5461}
5462
Lijun Pan635e4422020-08-19 17:52:26 -05005463static int ibmvnic_reset_init(struct ibmvnic_adapter *adapter, bool reset)
John Allenf6ef6402017-03-17 17:13:42 -05005464{
5465 struct device *dev = &adapter->vdev->dev;
Dany Madden98c41f02020-11-25 18:04:32 -06005466 unsigned long timeout = msecs_to_jiffies(20000);
Michal Suchanek6881b072021-03-02 20:47:47 +01005467 u64 old_num_rx_queues = adapter->req_rx_queues;
5468 u64 old_num_tx_queues = adapter->req_tx_queues;
John Allenf6ef6402017-03-17 17:13:42 -05005469 int rc;
5470
John Allen017892c12017-05-26 10:30:19 -04005471 adapter->from_passive_init = false;
5472
Michal Suchanek6881b072021-03-02 20:47:47 +01005473 if (reset)
Lijun Pan635e4422020-08-19 17:52:26 -05005474 reinit_completion(&adapter->init_done);
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06005475
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04005476 adapter->init_done_rc = 0;
Lijun Panfa68bfa2020-08-19 17:52:24 -05005477 rc = ibmvnic_send_crq_init(adapter);
5478 if (rc) {
5479 dev_err(dev, "Send crq init failed with error %d\n", rc);
5480 return rc;
5481 }
5482
John Allenf6ef6402017-03-17 17:13:42 -05005483 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
5484 dev_err(dev, "Initialization sequence timed out\n");
John Allen017892c12017-05-26 10:30:19 -04005485 return -1;
5486 }
5487
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04005488 if (adapter->init_done_rc) {
5489 release_crq_queue(adapter);
5490 return adapter->init_done_rc;
5491 }
5492
Lijun Pan785a2b12020-09-17 21:12:46 -05005493 if (adapter->from_passive_init) {
5494 adapter->state = VNIC_OPEN;
5495 adapter->from_passive_init = false;
5496 return -1;
5497 }
5498
Lijun Pan635e4422020-08-19 17:52:26 -05005499 if (reset &&
5500 test_bit(0, &adapter->resetting) && !adapter->wait_for_reset &&
Nathan Fontenot30f79622018-04-06 18:37:06 -05005501 adapter->reset_reason != VNIC_RESET_MOBILITY) {
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06005502 if (adapter->req_rx_queues != old_num_rx_queues ||
5503 adapter->req_tx_queues != old_num_tx_queues) {
5504 release_sub_crqs(adapter, 0);
5505 rc = init_sub_crqs(adapter);
5506 } else {
5507 rc = reset_sub_crq_queues(adapter);
5508 }
5509 } else {
Nathan Fontenot57a49432017-05-26 10:31:12 -04005510 rc = init_sub_crqs(adapter);
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06005511 }
5512
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04005513 if (rc) {
5514 dev_err(dev, "Initialization of sub crqs failed\n");
5515 release_crq_queue(adapter);
Thomas Falcon5df969c2017-06-28 19:55:54 -05005516 return rc;
5517 }
5518
5519 rc = init_sub_crq_irqs(adapter);
5520 if (rc) {
5521 dev_err(dev, "Failed to initialize sub crq irqs\n");
5522 release_crq_queue(adapter);
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04005523 }
5524
5525 return rc;
John Allenf6ef6402017-03-17 17:13:42 -05005526}
5527
Thomas Falcon40c9db82017-06-12 12:35:04 -05005528static struct device_attribute dev_attr_failover;
5529
Thomas Falcon032c5e82015-12-21 11:26:06 -06005530static int ibmvnic_probe(struct vio_dev *dev, const struct vio_device_id *id)
5531{
5532 struct ibmvnic_adapter *adapter;
5533 struct net_device *netdev;
5534 unsigned char *mac_addr_p;
Cristobal Forno53f8b1b2021-06-10 11:08:35 -06005535 bool init_success;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005536 int rc;
5537
5538 dev_dbg(&dev->dev, "entering ibmvnic_probe for UA 0x%x\n",
5539 dev->unit_address);
5540
5541 mac_addr_p = (unsigned char *)vio_get_attribute(dev,
5542 VETH_MAC_ADDR, NULL);
5543 if (!mac_addr_p) {
5544 dev_err(&dev->dev,
5545 "(%s:%3.3d) ERROR: Can't find MAC_ADDR attribute\n",
5546 __FILE__, __LINE__);
5547 return 0;
5548 }
5549
5550 netdev = alloc_etherdev_mq(sizeof(struct ibmvnic_adapter),
Thomas Falcond45cc3a2017-12-18 12:52:11 -06005551 IBMVNIC_MAX_QUEUES);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005552 if (!netdev)
5553 return -ENOMEM;
5554
5555 adapter = netdev_priv(netdev);
Nathan Fontenot90c80142017-05-03 14:04:32 -04005556 adapter->state = VNIC_PROBING;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005557 dev_set_drvdata(&dev->dev, netdev);
5558 adapter->vdev = dev;
5559 adapter->netdev = netdev;
Sukadev Bhattiprolu76cdc5c2020-11-25 18:04:29 -06005560 adapter->login_pending = false;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005561
5562 ether_addr_copy(adapter->mac_addr, mac_addr_p);
5563 ether_addr_copy(netdev->dev_addr, adapter->mac_addr);
5564 netdev->irq = dev->irq;
5565 netdev->netdev_ops = &ibmvnic_netdev_ops;
5566 netdev->ethtool_ops = &ibmvnic_ethtool_ops;
5567 SET_NETDEV_DEV(netdev, &dev->dev);
5568
Nathan Fontenoted651a12017-05-03 14:04:38 -04005569 INIT_WORK(&adapter->ibmvnic_reset, __ibmvnic_reset);
Juliet Kim7ed5b312019-09-20 16:11:23 -04005570 INIT_DELAYED_WORK(&adapter->ibmvnic_delayed_reset,
5571 __ibmvnic_delayed_reset);
Nathan Fontenoted651a12017-05-03 14:04:38 -04005572 INIT_LIST_HEAD(&adapter->rwi_list);
Thomas Falcon6c5c7482018-12-10 15:22:22 -06005573 spin_lock_init(&adapter->rwi_lock);
Juliet Kim7d7195a2020-03-10 09:23:58 -05005574 spin_lock_init(&adapter->state_lock);
Thomas Falconff25dcb2019-11-25 17:12:56 -06005575 mutex_init(&adapter->fw_lock);
Thomas Falconbbd669a2019-04-04 18:58:26 -05005576 init_completion(&adapter->init_done);
Thomas Falcon070eca92019-11-25 17:12:53 -06005577 init_completion(&adapter->fw_done);
5578 init_completion(&adapter->reset_done);
5579 init_completion(&adapter->stats_done);
Juliet Kim7ed5b312019-09-20 16:11:23 -04005580 clear_bit(0, &adapter->resetting);
Nathan Fontenoted651a12017-05-03 14:04:38 -04005581
Cristobal Forno53f8b1b2021-06-10 11:08:35 -06005582 init_success = false;
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04005583 do {
Nathan Fontenot30f79622018-04-06 18:37:06 -05005584 rc = init_crq_queue(adapter);
5585 if (rc) {
5586 dev_err(&dev->dev, "Couldn't initialize crq. rc=%d\n",
5587 rc);
5588 goto ibmvnic_init_fail;
5589 }
5590
Lijun Pan635e4422020-08-19 17:52:26 -05005591 rc = ibmvnic_reset_init(adapter, false);
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04005592 } while (rc == EAGAIN);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005593
Cristobal Forno53f8b1b2021-06-10 11:08:35 -06005594 /* We are ignoring the error from ibmvnic_reset_init() assuming that the
5595 * partner is not ready. CRQ is not active. When the partner becomes
5596 * ready, we will do the passive init reset.
5597 */
5598
5599 if (!rc)
5600 init_success = true;
5601
Thomas Falcon07184212018-05-16 15:49:05 -05005602 rc = init_stats_buffers(adapter);
5603 if (rc)
5604 goto ibmvnic_init_fail;
5605
5606 rc = init_stats_token(adapter);
5607 if (rc)
5608 goto ibmvnic_stats_fail;
5609
Thomas Falcon40c9db82017-06-12 12:35:04 -05005610 rc = device_create_file(&dev->dev, &dev_attr_failover);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05005611 if (rc)
Thomas Falcon07184212018-05-16 15:49:05 -05005612 goto ibmvnic_dev_file_err;
Thomas Falcon40c9db82017-06-12 12:35:04 -05005613
Mick Tarsele876a8a2017-09-28 13:53:18 -07005614 netif_carrier_off(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005615 rc = register_netdev(netdev);
5616 if (rc) {
5617 dev_err(&dev->dev, "failed to register netdev rc=%d\n", rc);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05005618 goto ibmvnic_register_fail;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005619 }
5620 dev_info(&dev->dev, "ibmvnic registered\n");
5621
Cristobal Forno53f8b1b2021-06-10 11:08:35 -06005622 if (init_success) {
5623 adapter->state = VNIC_PROBED;
5624 netdev->mtu = adapter->req_mtu - ETH_HLEN;
5625 netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
5626 netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
5627 } else {
5628 adapter->state = VNIC_DOWN;
5629 }
John Allenc26eba02017-10-26 16:23:25 -05005630
5631 adapter->wait_for_reset = false;
Dany Maddena86d5c62020-11-25 18:04:31 -06005632 adapter->last_reset_time = jiffies;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005633 return 0;
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05005634
5635ibmvnic_register_fail:
5636 device_remove_file(&dev->dev, &dev_attr_failover);
5637
Thomas Falcon07184212018-05-16 15:49:05 -05005638ibmvnic_dev_file_err:
5639 release_stats_token(adapter);
5640
5641ibmvnic_stats_fail:
5642 release_stats_buffers(adapter);
5643
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05005644ibmvnic_init_fail:
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06005645 release_sub_crqs(adapter, 1);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05005646 release_crq_queue(adapter);
Thomas Falconff25dcb2019-11-25 17:12:56 -06005647 mutex_destroy(&adapter->fw_lock);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05005648 free_netdev(netdev);
5649
5650 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005651}
5652
Uwe Kleine-König386a9662021-02-25 23:18:34 +01005653static void ibmvnic_remove(struct vio_dev *dev)
Thomas Falcon032c5e82015-12-21 11:26:06 -06005654{
5655 struct net_device *netdev = dev_get_drvdata(&dev->dev);
Nathan Fontenot37489052017-04-19 13:45:04 -04005656 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Juliet Kim7d7195a2020-03-10 09:23:58 -05005657 unsigned long flags;
5658
5659 spin_lock_irqsave(&adapter->state_lock, flags);
Sukadev Bhattiprolu4a41c422021-02-12 20:42:50 -08005660
5661 /* If ibmvnic_reset() is scheduling a reset, wait for it to
5662 * finish. Then, set the state to REMOVING to prevent it from
5663 * scheduling any more work and to have reset functions ignore
5664 * any resets that have already been scheduled. Drop the lock
5665 * after setting state, so __ibmvnic_reset() which is called
5666 * from the flush_work() below, can make progress.
5667 */
Junlin Yang69cdb792021-03-05 16:48:39 +08005668 spin_lock(&adapter->rwi_lock);
Nathan Fontenot90c80142017-05-03 14:04:32 -04005669 adapter->state = VNIC_REMOVING;
Junlin Yang69cdb792021-03-05 16:48:39 +08005670 spin_unlock(&adapter->rwi_lock);
Sukadev Bhattiprolu4a41c422021-02-12 20:42:50 -08005671
Juliet Kim7d7195a2020-03-10 09:23:58 -05005672 spin_unlock_irqrestore(&adapter->state_lock, flags);
5673
Thomas Falcon6954a9e2020-06-12 13:34:41 -05005674 flush_work(&adapter->ibmvnic_reset);
5675 flush_delayed_work(&adapter->ibmvnic_delayed_reset);
5676
Juliet Kima5681e22018-11-19 15:59:22 -06005677 rtnl_lock();
5678 unregister_netdevice(netdev);
Nathan Fontenot37489052017-04-19 13:45:04 -04005679
5680 release_resources(adapter);
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06005681 release_sub_crqs(adapter, 1);
Nathan Fontenot37489052017-04-19 13:45:04 -04005682 release_crq_queue(adapter);
5683
Thomas Falcon53cc7722018-02-26 18:10:56 -06005684 release_stats_token(adapter);
5685 release_stats_buffers(adapter);
5686
Nathan Fontenot90c80142017-05-03 14:04:32 -04005687 adapter->state = VNIC_REMOVED;
5688
Juliet Kima5681e22018-11-19 15:59:22 -06005689 rtnl_unlock();
Thomas Falconff25dcb2019-11-25 17:12:56 -06005690 mutex_destroy(&adapter->fw_lock);
Thomas Falcon40c9db82017-06-12 12:35:04 -05005691 device_remove_file(&dev->dev, &dev_attr_failover);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005692 free_netdev(netdev);
5693 dev_set_drvdata(&dev->dev, NULL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005694}
5695
Thomas Falcon40c9db82017-06-12 12:35:04 -05005696static ssize_t failover_store(struct device *dev, struct device_attribute *attr,
5697 const char *buf, size_t count)
5698{
5699 struct net_device *netdev = dev_get_drvdata(dev);
5700 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
5701 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
5702 __be64 session_token;
5703 long rc;
5704
5705 if (!sysfs_streq(buf, "1"))
5706 return -EINVAL;
5707
5708 rc = plpar_hcall(H_VIOCTL, retbuf, adapter->vdev->unit_address,
5709 H_GET_SESSION_TOKEN, 0, 0, 0);
5710 if (rc) {
5711 netdev_err(netdev, "Couldn't retrieve session token, rc %ld\n",
5712 rc);
Lijun Pan334c4242021-04-13 03:31:44 -05005713 goto last_resort;
Thomas Falcon40c9db82017-06-12 12:35:04 -05005714 }
5715
5716 session_token = (__be64)retbuf[0];
5717 netdev_dbg(netdev, "Initiating client failover, session id %llx\n",
5718 be64_to_cpu(session_token));
5719 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
5720 H_SESSION_ERR_DETECTED, session_token, 0, 0);
Lijun Pan334c4242021-04-13 03:31:44 -05005721 if (rc)
5722 netdev_err(netdev,
5723 "H_VIOCTL initiated failover failed, rc %ld\n",
Thomas Falcon40c9db82017-06-12 12:35:04 -05005724 rc);
Lijun Pan334c4242021-04-13 03:31:44 -05005725
5726last_resort:
5727 netdev_dbg(netdev, "Trying to send CRQ_CMD, the last resort\n");
5728 ibmvnic_reset(adapter, VNIC_RESET_FAILOVER);
Thomas Falcon40c9db82017-06-12 12:35:04 -05005729
5730 return count;
5731}
Joe Perches6cbaefb2017-12-19 10:15:09 -08005732static DEVICE_ATTR_WO(failover);
Thomas Falcon40c9db82017-06-12 12:35:04 -05005733
Thomas Falcon032c5e82015-12-21 11:26:06 -06005734static unsigned long ibmvnic_get_desired_dma(struct vio_dev *vdev)
5735{
5736 struct net_device *netdev = dev_get_drvdata(&vdev->dev);
5737 struct ibmvnic_adapter *adapter;
5738 struct iommu_table *tbl;
5739 unsigned long ret = 0;
5740 int i;
5741
5742 tbl = get_iommu_table_base(&vdev->dev);
5743
5744 /* netdev inits at probe time along with the structures we need below*/
5745 if (!netdev)
5746 return IOMMU_PAGE_ALIGN(IBMVNIC_IO_ENTITLEMENT_DEFAULT, tbl);
5747
5748 adapter = netdev_priv(netdev);
5749
5750 ret += PAGE_SIZE; /* the crq message queue */
Thomas Falcon032c5e82015-12-21 11:26:06 -06005751 ret += IOMMU_PAGE_ALIGN(sizeof(struct ibmvnic_statistics), tbl);
5752
5753 for (i = 0; i < adapter->req_tx_queues + adapter->req_rx_queues; i++)
5754 ret += 4 * PAGE_SIZE; /* the scrq message queue */
5755
Thomas Falcon507ebe62020-08-21 13:39:01 -05005756 for (i = 0; i < adapter->num_active_rx_pools; i++)
Thomas Falcon032c5e82015-12-21 11:26:06 -06005757 ret += adapter->rx_pool[i].size *
5758 IOMMU_PAGE_ALIGN(adapter->rx_pool[i].buff_size, tbl);
5759
5760 return ret;
5761}
5762
5763static int ibmvnic_resume(struct device *dev)
5764{
5765 struct net_device *netdev = dev_get_drvdata(dev);
5766 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005767
John Allencb89ba22017-06-19 11:27:53 -05005768 if (adapter->state != VNIC_OPEN)
5769 return 0;
5770
John Allena2488782017-07-24 13:26:06 -05005771 tasklet_schedule(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005772
5773 return 0;
5774}
5775
Arvind Yadav8c37bc62017-08-17 18:52:54 +05305776static const struct vio_device_id ibmvnic_device_table[] = {
Thomas Falcon032c5e82015-12-21 11:26:06 -06005777 {"network", "IBM,vnic"},
5778 {"", "" }
5779};
5780MODULE_DEVICE_TABLE(vio, ibmvnic_device_table);
5781
5782static const struct dev_pm_ops ibmvnic_pm_ops = {
5783 .resume = ibmvnic_resume
5784};
5785
5786static struct vio_driver ibmvnic_driver = {
5787 .id_table = ibmvnic_device_table,
5788 .probe = ibmvnic_probe,
5789 .remove = ibmvnic_remove,
5790 .get_desired_dma = ibmvnic_get_desired_dma,
5791 .name = ibmvnic_driver_name,
5792 .pm = &ibmvnic_pm_ops,
5793};
5794
5795/* module functions */
5796static int __init ibmvnic_module_init(void)
5797{
5798 pr_info("%s: %s %s\n", ibmvnic_driver_name, ibmvnic_driver_string,
5799 IBMVNIC_DRIVER_VERSION);
5800
5801 return vio_register_driver(&ibmvnic_driver);
5802}
5803
5804static void __exit ibmvnic_module_exit(void)
5805{
5806 vio_unregister_driver(&ibmvnic_driver);
5807}
5808
5809module_init(ibmvnic_module_init);
5810module_exit(ibmvnic_module_exit);