blob: 68848947579164eedd7b64be07b3675e7b7db1bf [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,
784 int num_entries, int buf_size)
785{
786 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
787 int i;
788
789 tx_pool->tx_buff = kcalloc(num_entries,
790 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,
796 num_entries * buf_size))
797 return -1;
798
799 tx_pool->free_map = kcalloc(num_entries, sizeof(int), GFP_KERNEL);
800 if (!tx_pool->free_map)
801 return -1;
802
803 for (i = 0; i < num_entries; i++)
804 tx_pool->free_map[i] = i;
805
806 tx_pool->consumer_index = 0;
807 tx_pool->producer_index = 0;
808 tx_pool->num_buffers = num_entries;
809 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;
Nathan Fontenotc657e322017-03-30 02:49:06 -0400818 int tx_subcrqs;
Dwip N. Banerjee9a87c3f2020-11-18 19:12:22 -0600819 u64 buff_size;
Thomas Falcon32053062018-03-16 20:00:27 -0500820 int i, rc;
Nathan Fontenotc657e322017-03-30 02:49:06 -0400821
Thomas Falcon507ebe62020-08-21 13:39:01 -0500822 tx_subcrqs = adapter->num_active_tx_scrqs;
Nathan Fontenotc657e322017-03-30 02:49:06 -0400823 adapter->tx_pool = kcalloc(tx_subcrqs,
824 sizeof(struct ibmvnic_tx_pool), GFP_KERNEL);
825 if (!adapter->tx_pool)
826 return -1;
827
Thomas Falcon32053062018-03-16 20:00:27 -0500828 adapter->tso_pool = kcalloc(tx_subcrqs,
829 sizeof(struct ibmvnic_tx_pool), GFP_KERNEL);
Sukadev Bhattiprolu0f2bf312021-09-14 20:52:52 -0700830 /* To simplify release_tx_pools() ensure that ->tx_pool and
831 * ->tso_pool are either both NULL or both non-NULL.
832 */
Sukadev Bhattiproluf6ebca82021-06-23 21:13:15 -0700833 if (!adapter->tso_pool) {
834 kfree(adapter->tx_pool);
835 adapter->tx_pool = NULL;
Thomas Falcon32053062018-03-16 20:00:27 -0500836 return -1;
Sukadev Bhattiproluf6ebca82021-06-23 21:13:15 -0700837 }
Thomas Falcon32053062018-03-16 20:00:27 -0500838
Sukadev Bhattiprolu0f2bf312021-09-14 20:52:52 -0700839 /* Set num_active_tx_pools early. If we fail below after partial
840 * allocation, release_tx_pools() will know how many to look for.
841 */
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600842 adapter->num_active_tx_pools = tx_subcrqs;
843
Nathan Fontenotc657e322017-03-30 02:49:06 -0400844 for (i = 0; i < tx_subcrqs; i++) {
Dwip N. Banerjee9a87c3f2020-11-18 19:12:22 -0600845 buff_size = adapter->req_mtu + VLAN_HLEN;
846 buff_size = ALIGN(buff_size, L1_CACHE_BYTES);
Sukadev Bhattiprolu0f2bf312021-09-14 20:52:52 -0700847
848 dev_dbg(dev, "Init tx pool %d [%llu, %llu]\n",
849 i, adapter->req_tx_entries_per_subcrq, buff_size);
850
Thomas Falcon32053062018-03-16 20:00:27 -0500851 rc = init_one_tx_pool(netdev, &adapter->tx_pool[i],
852 adapter->req_tx_entries_per_subcrq,
Dwip N. Banerjee9a87c3f2020-11-18 19:12:22 -0600853 buff_size);
Thomas Falcon32053062018-03-16 20:00:27 -0500854 if (rc) {
Nathan Fontenotc657e322017-03-30 02:49:06 -0400855 release_tx_pools(adapter);
Thomas Falcon32053062018-03-16 20:00:27 -0500856 return rc;
Nathan Fontenotc657e322017-03-30 02:49:06 -0400857 }
858
Thomas Falcon7c940b12019-06-07 16:03:55 -0500859 rc = init_one_tx_pool(netdev, &adapter->tso_pool[i],
860 IBMVNIC_TSO_BUFS,
861 IBMVNIC_TSO_BUF_SZ);
Thomas Falcon32053062018-03-16 20:00:27 -0500862 if (rc) {
Nathan Fontenotc657e322017-03-30 02:49:06 -0400863 release_tx_pools(adapter);
Thomas Falcon32053062018-03-16 20:00:27 -0500864 return rc;
Nathan Fontenotc657e322017-03-30 02:49:06 -0400865 }
Nathan Fontenotc657e322017-03-30 02:49:06 -0400866 }
867
868 return 0;
869}
870
John Allend944c3d62017-05-26 10:30:13 -0400871static void ibmvnic_napi_enable(struct ibmvnic_adapter *adapter)
872{
873 int i;
874
875 if (adapter->napi_enabled)
876 return;
877
878 for (i = 0; i < adapter->req_rx_queues; i++)
879 napi_enable(&adapter->napi[i]);
880
881 adapter->napi_enabled = true;
882}
883
884static void ibmvnic_napi_disable(struct ibmvnic_adapter *adapter)
885{
886 int i;
887
888 if (!adapter->napi_enabled)
889 return;
890
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500891 for (i = 0; i < adapter->req_rx_queues; i++) {
892 netdev_dbg(adapter->netdev, "Disabling napi[%d]\n", i);
John Allend944c3d62017-05-26 10:30:13 -0400893 napi_disable(&adapter->napi[i]);
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500894 }
John Allend944c3d62017-05-26 10:30:13 -0400895
896 adapter->napi_enabled = false;
897}
898
Nathan Fontenot86f669b2018-02-19 13:30:39 -0600899static int init_napi(struct ibmvnic_adapter *adapter)
900{
901 int i;
902
903 adapter->napi = kcalloc(adapter->req_rx_queues,
904 sizeof(struct napi_struct), GFP_KERNEL);
905 if (!adapter->napi)
906 return -ENOMEM;
907
908 for (i = 0; i < adapter->req_rx_queues; i++) {
909 netdev_dbg(adapter->netdev, "Adding napi[%d]\n", i);
910 netif_napi_add(adapter->netdev, &adapter->napi[i],
911 ibmvnic_poll, NAPI_POLL_WEIGHT);
912 }
913
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600914 adapter->num_active_rx_napi = adapter->req_rx_queues;
Nathan Fontenot86f669b2018-02-19 13:30:39 -0600915 return 0;
916}
917
918static void release_napi(struct ibmvnic_adapter *adapter)
919{
920 int i;
921
922 if (!adapter->napi)
923 return;
924
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600925 for (i = 0; i < adapter->num_active_rx_napi; i++) {
Wen Yang390de192018-12-11 12:20:46 +0800926 netdev_dbg(adapter->netdev, "Releasing napi[%d]\n", i);
927 netif_napi_del(&adapter->napi[i]);
Nathan Fontenot86f669b2018-02-19 13:30:39 -0600928 }
929
930 kfree(adapter->napi);
931 adapter->napi = NULL;
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600932 adapter->num_active_rx_napi = 0;
Thomas Falconc3f22412018-05-23 13:37:55 -0500933 adapter->napi_enabled = false;
Nathan Fontenot86f669b2018-02-19 13:30:39 -0600934}
935
Lijun Pan0666ef72021-04-12 02:41:28 -0500936static const char *adapter_state_to_string(enum vnic_state state)
937{
938 switch (state) {
939 case VNIC_PROBING:
940 return "PROBING";
941 case VNIC_PROBED:
942 return "PROBED";
943 case VNIC_OPENING:
944 return "OPENING";
945 case VNIC_OPEN:
946 return "OPEN";
947 case VNIC_CLOSING:
948 return "CLOSING";
949 case VNIC_CLOSED:
950 return "CLOSED";
951 case VNIC_REMOVING:
952 return "REMOVING";
953 case VNIC_REMOVED:
954 return "REMOVED";
Lijun Pan822ebc22021-06-11 10:35:37 -0500955 case VNIC_DOWN:
956 return "DOWN";
Lijun Pan0666ef72021-04-12 02:41:28 -0500957 }
Michal Suchanek07b5dc12021-05-20 08:50:34 +0200958 return "UNKNOWN";
Lijun Pan0666ef72021-04-12 02:41:28 -0500959}
960
John Allena57a5d22017-03-17 17:13:41 -0500961static int ibmvnic_login(struct net_device *netdev)
Thomas Falcon032c5e82015-12-21 11:26:06 -0600962{
963 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Dany Madden98c41f02020-11-25 18:04:32 -0600964 unsigned long timeout = msecs_to_jiffies(20000);
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500965 int retry_count = 0;
Thomas Falcondff515a32020-06-15 10:29:23 -0500966 int retries = 10;
Thomas Falconeb110412018-05-24 14:37:53 -0500967 bool retry;
Thomas Falcon4d96f122017-08-01 15:04:36 -0500968 int rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600969
John Allenbd0b6722017-03-17 17:13:40 -0500970 do {
Thomas Falconeb110412018-05-24 14:37:53 -0500971 retry = false;
Thomas Falcondff515a32020-06-15 10:29:23 -0500972 if (retry_count > retries) {
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500973 netdev_warn(netdev, "Login attempts exceeded\n");
974 return -1;
975 }
976
977 adapter->init_done_rc = 0;
978 reinit_completion(&adapter->init_done);
979 rc = send_login(adapter);
Dany Maddenc98d9cc2020-11-25 18:04:30 -0600980 if (rc)
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500981 return rc;
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500982
983 if (!wait_for_completion_timeout(&adapter->init_done,
984 timeout)) {
Thomas Falcondff515a32020-06-15 10:29:23 -0500985 netdev_warn(netdev, "Login timed out, retrying...\n");
986 retry = true;
987 adapter->init_done_rc = 0;
988 retry_count++;
989 continue;
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500990 }
991
Thomas Falcondff515a32020-06-15 10:29:23 -0500992 if (adapter->init_done_rc == ABORTED) {
993 netdev_warn(netdev, "Login aborted, retrying...\n");
994 retry = true;
995 adapter->init_done_rc = 0;
996 retry_count++;
997 /* FW or device may be busy, so
998 * wait a bit before retrying login
999 */
1000 msleep(500);
1001 } else if (adapter->init_done_rc == PARTIALSUCCESS) {
Nathan Fontenot64d92aa2018-04-11 10:09:32 -05001002 retry_count++;
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06001003 release_sub_crqs(adapter, 1);
John Allenbd0b6722017-03-17 17:13:40 -05001004
Thomas Falconeb110412018-05-24 14:37:53 -05001005 retry = true;
1006 netdev_dbg(netdev,
1007 "Received partial success, retrying...\n");
Nathan Fontenot64d92aa2018-04-11 10:09:32 -05001008 adapter->init_done_rc = 0;
John Allenbd0b6722017-03-17 17:13:40 -05001009 reinit_completion(&adapter->init_done);
Lijun Pan491099a2020-09-27 20:13:26 -05001010 send_query_cap(adapter);
John Allenbd0b6722017-03-17 17:13:40 -05001011 if (!wait_for_completion_timeout(&adapter->init_done,
1012 timeout)) {
Nathan Fontenot64d92aa2018-04-11 10:09:32 -05001013 netdev_warn(netdev,
1014 "Capabilities query timed out\n");
John Allenbd0b6722017-03-17 17:13:40 -05001015 return -1;
1016 }
Nathan Fontenot64d92aa2018-04-11 10:09:32 -05001017
Thomas Falcon4d96f122017-08-01 15:04:36 -05001018 rc = init_sub_crqs(adapter);
1019 if (rc) {
Nathan Fontenot64d92aa2018-04-11 10:09:32 -05001020 netdev_warn(netdev,
1021 "SCRQ initialization failed\n");
Thomas Falcon4d96f122017-08-01 15:04:36 -05001022 return -1;
1023 }
Nathan Fontenot64d92aa2018-04-11 10:09:32 -05001024
Thomas Falcon4d96f122017-08-01 15:04:36 -05001025 rc = init_sub_crq_irqs(adapter);
1026 if (rc) {
Nathan Fontenot64d92aa2018-04-11 10:09:32 -05001027 netdev_warn(netdev,
1028 "SCRQ irq initialization failed\n");
Thomas Falcon4d96f122017-08-01 15:04:36 -05001029 return -1;
1030 }
Nathan Fontenot64d92aa2018-04-11 10:09:32 -05001031 } else if (adapter->init_done_rc) {
1032 netdev_warn(netdev, "Adapter login failed\n");
John Allenbd0b6722017-03-17 17:13:40 -05001033 return -1;
1034 }
Thomas Falconeb110412018-05-24 14:37:53 -05001035 } while (retry);
John Allenbd0b6722017-03-17 17:13:40 -05001036
Thomas Falcon62740e92019-05-09 23:13:43 -05001037 __ibmvnic_set_mac(netdev, adapter->mac_addr);
Thomas Falcon3d166132018-01-10 19:39:52 -06001038
Lijun Pan0666ef72021-04-12 02:41:28 -05001039 netdev_dbg(netdev, "[S:%s] Login succeeded\n", adapter_state_to_string(adapter->state));
John Allena57a5d22017-03-17 17:13:41 -05001040 return 0;
1041}
1042
Thomas Falcon34f0f4e2018-02-13 18:23:40 -06001043static void release_login_buffer(struct ibmvnic_adapter *adapter)
1044{
1045 kfree(adapter->login_buf);
1046 adapter->login_buf = NULL;
1047}
1048
1049static void release_login_rsp_buffer(struct ibmvnic_adapter *adapter)
1050{
1051 kfree(adapter->login_rsp_buf);
1052 adapter->login_rsp_buf = NULL;
1053}
1054
Nathan Fontenot1b8955e2017-03-30 02:49:29 -04001055static void release_resources(struct ibmvnic_adapter *adapter)
1056{
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001057 release_vpd_data(adapter);
1058
Nathan Fontenot1b8955e2017-03-30 02:49:29 -04001059 release_tx_pools(adapter);
1060 release_rx_pools(adapter);
1061
Nathan Fontenot86f669b2018-02-19 13:30:39 -06001062 release_napi(adapter);
Lijun Pana0c8be52020-12-19 15:39:19 -06001063 release_login_buffer(adapter);
Thomas Falcon34f0f4e2018-02-13 18:23:40 -06001064 release_login_rsp_buffer(adapter);
Nathan Fontenot1b8955e2017-03-30 02:49:29 -04001065}
1066
Nathan Fontenot53da09e2017-04-21 15:39:04 -04001067static int set_link_state(struct ibmvnic_adapter *adapter, u8 link_state)
1068{
1069 struct net_device *netdev = adapter->netdev;
Dany Madden98c41f02020-11-25 18:04:32 -06001070 unsigned long timeout = msecs_to_jiffies(20000);
Nathan Fontenot53da09e2017-04-21 15:39:04 -04001071 union ibmvnic_crq crq;
1072 bool resend;
1073 int rc;
1074
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001075 netdev_dbg(netdev, "setting link state %d\n", link_state);
1076
Nathan Fontenot53da09e2017-04-21 15:39:04 -04001077 memset(&crq, 0, sizeof(crq));
1078 crq.logical_link_state.first = IBMVNIC_CRQ_CMD;
1079 crq.logical_link_state.cmd = LOGICAL_LINK_STATE;
1080 crq.logical_link_state.link_state = link_state;
1081
1082 do {
1083 resend = false;
1084
1085 reinit_completion(&adapter->init_done);
1086 rc = ibmvnic_send_crq(adapter, &crq);
1087 if (rc) {
1088 netdev_err(netdev, "Failed to set link state\n");
1089 return rc;
1090 }
1091
1092 if (!wait_for_completion_timeout(&adapter->init_done,
1093 timeout)) {
1094 netdev_err(netdev, "timeout setting link state\n");
1095 return -1;
1096 }
1097
Lijun Pan4c5f6af2020-08-19 17:52:23 -05001098 if (adapter->init_done_rc == PARTIALSUCCESS) {
Nathan Fontenot53da09e2017-04-21 15:39:04 -04001099 /* Partuial success, delay and re-send */
1100 mdelay(1000);
1101 resend = true;
Thomas Falconab5ec332018-05-23 13:37:59 -05001102 } else if (adapter->init_done_rc) {
1103 netdev_warn(netdev, "Unable to set link state, rc=%d\n",
1104 adapter->init_done_rc);
1105 return adapter->init_done_rc;
Nathan Fontenot53da09e2017-04-21 15:39:04 -04001106 }
1107 } while (resend);
1108
1109 return 0;
1110}
1111
Thomas Falcon7f3c6e62017-04-21 15:38:40 -04001112static int set_real_num_queues(struct net_device *netdev)
1113{
1114 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1115 int rc;
1116
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001117 netdev_dbg(netdev, "Setting real tx/rx queues (%llx/%llx)\n",
1118 adapter->req_tx_queues, adapter->req_rx_queues);
1119
Thomas Falcon7f3c6e62017-04-21 15:38:40 -04001120 rc = netif_set_real_num_tx_queues(netdev, adapter->req_tx_queues);
1121 if (rc) {
1122 netdev_err(netdev, "failed to set the number of tx queues\n");
1123 return rc;
1124 }
1125
1126 rc = netif_set_real_num_rx_queues(netdev, adapter->req_rx_queues);
1127 if (rc)
1128 netdev_err(netdev, "failed to set the number of rx queues\n");
1129
1130 return rc;
1131}
1132
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001133static int ibmvnic_get_vpd(struct ibmvnic_adapter *adapter)
1134{
1135 struct device *dev = &adapter->vdev->dev;
1136 union ibmvnic_crq crq;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001137 int len = 0;
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05001138 int rc;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001139
1140 if (adapter->vpd->buff)
1141 len = adapter->vpd->len;
1142
Thomas Falconff25dcb2019-11-25 17:12:56 -06001143 mutex_lock(&adapter->fw_lock);
1144 adapter->fw_done_rc = 0;
Thomas Falcon070eca92019-11-25 17:12:53 -06001145 reinit_completion(&adapter->fw_done);
Thomas Falconff25dcb2019-11-25 17:12:56 -06001146
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001147 crq.get_vpd_size.first = IBMVNIC_CRQ_CMD;
1148 crq.get_vpd_size.cmd = GET_VPD_SIZE;
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05001149 rc = ibmvnic_send_crq(adapter, &crq);
Thomas Falconff25dcb2019-11-25 17:12:56 -06001150 if (rc) {
1151 mutex_unlock(&adapter->fw_lock);
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05001152 return rc;
Thomas Falconff25dcb2019-11-25 17:12:56 -06001153 }
Thomas Falcon476d96c2019-11-25 17:12:55 -06001154
1155 rc = ibmvnic_wait_for_completion(adapter, &adapter->fw_done, 10000);
1156 if (rc) {
1157 dev_err(dev, "Could not retrieve VPD size, rc = %d\n", rc);
Thomas Falconff25dcb2019-11-25 17:12:56 -06001158 mutex_unlock(&adapter->fw_lock);
Thomas Falcon476d96c2019-11-25 17:12:55 -06001159 return rc;
1160 }
Thomas Falconff25dcb2019-11-25 17:12:56 -06001161 mutex_unlock(&adapter->fw_lock);
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001162
1163 if (!adapter->vpd->len)
1164 return -ENODATA;
1165
1166 if (!adapter->vpd->buff)
1167 adapter->vpd->buff = kzalloc(adapter->vpd->len, GFP_KERNEL);
1168 else if (adapter->vpd->len != len)
1169 adapter->vpd->buff =
1170 krealloc(adapter->vpd->buff,
1171 adapter->vpd->len, GFP_KERNEL);
1172
1173 if (!adapter->vpd->buff) {
1174 dev_err(dev, "Could allocate VPD buffer\n");
1175 return -ENOMEM;
1176 }
1177
1178 adapter->vpd->dma_addr =
1179 dma_map_single(dev, adapter->vpd->buff, adapter->vpd->len,
1180 DMA_FROM_DEVICE);
Desnes Augusto Nunes do Rosariof7431062017-11-17 09:09:04 -02001181 if (dma_mapping_error(dev, adapter->vpd->dma_addr)) {
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001182 dev_err(dev, "Could not map VPD buffer\n");
1183 kfree(adapter->vpd->buff);
Thomas Falconb0992ec2018-02-06 17:25:23 -06001184 adapter->vpd->buff = NULL;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001185 return -ENOMEM;
1186 }
1187
Thomas Falconff25dcb2019-11-25 17:12:56 -06001188 mutex_lock(&adapter->fw_lock);
1189 adapter->fw_done_rc = 0;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001190 reinit_completion(&adapter->fw_done);
Thomas Falconff25dcb2019-11-25 17:12:56 -06001191
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001192 crq.get_vpd.first = IBMVNIC_CRQ_CMD;
1193 crq.get_vpd.cmd = GET_VPD;
1194 crq.get_vpd.ioba = cpu_to_be32(adapter->vpd->dma_addr);
1195 crq.get_vpd.len = cpu_to_be32((u32)adapter->vpd->len);
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05001196 rc = ibmvnic_send_crq(adapter, &crq);
1197 if (rc) {
1198 kfree(adapter->vpd->buff);
1199 adapter->vpd->buff = NULL;
Thomas Falconff25dcb2019-11-25 17:12:56 -06001200 mutex_unlock(&adapter->fw_lock);
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05001201 return rc;
1202 }
Thomas Falcon476d96c2019-11-25 17:12:55 -06001203
1204 rc = ibmvnic_wait_for_completion(adapter, &adapter->fw_done, 10000);
1205 if (rc) {
1206 dev_err(dev, "Unable to retrieve VPD, rc = %d\n", rc);
1207 kfree(adapter->vpd->buff);
1208 adapter->vpd->buff = NULL;
Thomas Falconff25dcb2019-11-25 17:12:56 -06001209 mutex_unlock(&adapter->fw_lock);
Thomas Falcon476d96c2019-11-25 17:12:55 -06001210 return rc;
1211 }
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001212
Thomas Falconff25dcb2019-11-25 17:12:56 -06001213 mutex_unlock(&adapter->fw_lock);
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001214 return 0;
1215}
1216
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001217static int init_resources(struct ibmvnic_adapter *adapter)
John Allena57a5d22017-03-17 17:13:41 -05001218{
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001219 struct net_device *netdev = adapter->netdev;
Nathan Fontenot86f669b2018-02-19 13:30:39 -06001220 int rc;
John Allena57a5d22017-03-17 17:13:41 -05001221
Thomas Falcon7f3c6e62017-04-21 15:38:40 -04001222 rc = set_real_num_queues(netdev);
1223 if (rc)
1224 return rc;
John Allenbd0b6722017-03-17 17:13:40 -05001225
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001226 adapter->vpd = kzalloc(sizeof(*adapter->vpd), GFP_KERNEL);
1227 if (!adapter->vpd)
1228 return -ENOMEM;
1229
John Allen69d08dc2018-01-18 16:27:58 -06001230 /* Vital Product Data (VPD) */
1231 rc = ibmvnic_get_vpd(adapter);
1232 if (rc) {
1233 netdev_err(netdev, "failed to initialize Vital Product Data (VPD)\n");
1234 return rc;
1235 }
1236
Thomas Falcon032c5e82015-12-21 11:26:06 -06001237 adapter->map_id = 1;
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001238
Nathan Fontenot86f669b2018-02-19 13:30:39 -06001239 rc = init_napi(adapter);
1240 if (rc)
1241 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001242
Lijun Pan69980d02020-09-27 20:13:28 -05001243 send_query_map(adapter);
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -04001244
1245 rc = init_rx_pools(netdev);
1246 if (rc)
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001247 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001248
Nathan Fontenotc657e322017-03-30 02:49:06 -04001249 rc = init_tx_pools(netdev);
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001250 return rc;
1251}
1252
Nathan Fontenoted651a12017-05-03 14:04:38 -04001253static int __ibmvnic_open(struct net_device *netdev)
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001254{
1255 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001256 enum vnic_state prev_state = adapter->state;
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001257 int i, rc;
1258
Nathan Fontenot90c80142017-05-03 14:04:32 -04001259 adapter->state = VNIC_OPENING;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001260 replenish_pools(adapter);
John Allend944c3d62017-05-26 10:30:13 -04001261 ibmvnic_napi_enable(adapter);
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001262
Thomas Falcon032c5e82015-12-21 11:26:06 -06001263 /* We're ready to receive frames, enable the sub-crq interrupts and
1264 * set the logical link state to up
1265 */
Nathan Fontenoted651a12017-05-03 14:04:38 -04001266 for (i = 0; i < adapter->req_rx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001267 netdev_dbg(netdev, "Enabling rx_scrq[%d] irq\n", i);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001268 if (prev_state == VNIC_CLOSED)
1269 enable_irq(adapter->rx_scrq[i]->irq);
Thomas Falconf23e0642018-04-15 18:53:36 -05001270 enable_scrq_irq(adapter, adapter->rx_scrq[i]);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001271 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001272
Nathan Fontenoted651a12017-05-03 14:04:38 -04001273 for (i = 0; i < adapter->req_tx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001274 netdev_dbg(netdev, "Enabling tx_scrq[%d] irq\n", i);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001275 if (prev_state == VNIC_CLOSED)
1276 enable_irq(adapter->tx_scrq[i]->irq);
Thomas Falconf23e0642018-04-15 18:53:36 -05001277 enable_scrq_irq(adapter, adapter->tx_scrq[i]);
Thomas Falcon0d973382020-11-18 19:12:19 -06001278 netdev_tx_reset_queue(netdev_get_tx_queue(netdev, i));
Nathan Fontenoted651a12017-05-03 14:04:38 -04001279 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001280
Nathan Fontenot53da09e2017-04-21 15:39:04 -04001281 rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_UP);
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001282 if (rc) {
Lijun Pan0775ebc2021-04-14 02:46:14 -05001283 ibmvnic_napi_disable(adapter);
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001284 release_resources(adapter);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001285 return rc;
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001286 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001287
Nathan Fontenoted651a12017-05-03 14:04:38 -04001288 netif_tx_start_all_queues(netdev);
1289
Dany Madden2ca220f2021-06-23 21:13:11 -07001290 if (prev_state == VNIC_CLOSED) {
1291 for (i = 0; i < adapter->req_rx_queues; i++)
1292 napi_schedule(&adapter->napi[i]);
1293 }
1294
Nathan Fontenoted651a12017-05-03 14:04:38 -04001295 adapter->state = VNIC_OPEN;
1296 return rc;
1297}
1298
1299static int ibmvnic_open(struct net_device *netdev)
1300{
1301 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
John Allen69d08dc2018-01-18 16:27:58 -06001302 int rc;
Nathan Fontenoted651a12017-05-03 14:04:38 -04001303
Sukadev Bhattiprolu8f1c0fd2021-02-23 21:02:29 -08001304 ASSERT_RTNL();
1305
1306 /* If device failover is pending or we are about to reset, just set
1307 * device state and return. Device operation will be handled by reset
1308 * routine.
1309 *
1310 * It should be safe to overwrite the adapter->state here. Since
1311 * we hold the rtnl, either the reset has not actually started or
1312 * the rtnl got dropped during the set_link_state() in do_reset().
1313 * In the former case, no one else is changing the state (again we
1314 * have the rtnl) and in the latter case, do_reset() will detect and
1315 * honor our setting below.
Thomas Falcon5a18e1e2018-04-06 18:37:05 -05001316 */
Sukadev Bhattiprolu8f1c0fd2021-02-23 21:02:29 -08001317 if (adapter->failover_pending || (test_bit(0, &adapter->resetting))) {
Lijun Pan0666ef72021-04-12 02:41:28 -05001318 netdev_dbg(netdev, "[S:%s FOP:%d] Resetting, deferring open\n",
1319 adapter_state_to_string(adapter->state),
1320 adapter->failover_pending);
Thomas Falcon5a18e1e2018-04-06 18:37:05 -05001321 adapter->state = VNIC_OPEN;
Sukadev Bhattiprolu8f1c0fd2021-02-23 21:02:29 -08001322 rc = 0;
1323 goto out;
Thomas Falcon5a18e1e2018-04-06 18:37:05 -05001324 }
1325
Nathan Fontenoted651a12017-05-03 14:04:38 -04001326 if (adapter->state != VNIC_CLOSED) {
1327 rc = ibmvnic_login(netdev);
Juliet Kima5681e22018-11-19 15:59:22 -06001328 if (rc)
Sukadev Bhattiprolu1d850492020-10-30 10:07:11 -07001329 goto out;
Nathan Fontenoted651a12017-05-03 14:04:38 -04001330
1331 rc = init_resources(adapter);
1332 if (rc) {
1333 netdev_err(netdev, "failed to initialize resources\n");
1334 release_resources(adapter);
Sukadev Bhattiprolu1d850492020-10-30 10:07:11 -07001335 goto out;
Nathan Fontenoted651a12017-05-03 14:04:38 -04001336 }
1337 }
1338
1339 rc = __ibmvnic_open(netdev);
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001340
Sukadev Bhattiprolu1d850492020-10-30 10:07:11 -07001341out:
Sukadev Bhattiprolu8f1c0fd2021-02-23 21:02:29 -08001342 /* If open failed and there is a pending failover or in-progress reset,
1343 * set device state and return. Device operation will be handled by
1344 * reset routine. See also comments above regarding rtnl.
Sukadev Bhattiprolu1d850492020-10-30 10:07:11 -07001345 */
Sukadev Bhattiprolu8f1c0fd2021-02-23 21:02:29 -08001346 if (rc &&
1347 (adapter->failover_pending || (test_bit(0, &adapter->resetting)))) {
Sukadev Bhattiprolu1d850492020-10-30 10:07:11 -07001348 adapter->state = VNIC_OPEN;
1349 rc = 0;
1350 }
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001351 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001352}
1353
Thomas Falcond0869c02018-02-13 18:23:43 -06001354static void clean_rx_pools(struct ibmvnic_adapter *adapter)
1355{
1356 struct ibmvnic_rx_pool *rx_pool;
Thomas Falcon637f81d2018-02-26 18:10:57 -06001357 struct ibmvnic_rx_buff *rx_buff;
Thomas Falcond0869c02018-02-13 18:23:43 -06001358 u64 rx_entries;
1359 int rx_scrqs;
1360 int i, j;
1361
1362 if (!adapter->rx_pool)
1363 return;
1364
Thomas Falcon660e3092018-04-20 14:25:32 -05001365 rx_scrqs = adapter->num_active_rx_pools;
Thomas Falcond0869c02018-02-13 18:23:43 -06001366 rx_entries = adapter->req_rx_add_entries_per_subcrq;
1367
1368 /* Free any remaining skbs in the rx buffer pools */
1369 for (i = 0; i < rx_scrqs; i++) {
1370 rx_pool = &adapter->rx_pool[i];
Thomas Falcon637f81d2018-02-26 18:10:57 -06001371 if (!rx_pool || !rx_pool->rx_buff)
Thomas Falcond0869c02018-02-13 18:23:43 -06001372 continue;
1373
1374 netdev_dbg(adapter->netdev, "Cleaning rx_pool[%d]\n", i);
1375 for (j = 0; j < rx_entries; j++) {
Thomas Falcon637f81d2018-02-26 18:10:57 -06001376 rx_buff = &rx_pool->rx_buff[j];
1377 if (rx_buff && rx_buff->skb) {
1378 dev_kfree_skb_any(rx_buff->skb);
1379 rx_buff->skb = NULL;
Thomas Falcond0869c02018-02-13 18:23:43 -06001380 }
1381 }
1382 }
1383}
1384
Thomas Falcone9e1e972018-03-16 20:00:30 -05001385static void clean_one_tx_pool(struct ibmvnic_adapter *adapter,
1386 struct ibmvnic_tx_pool *tx_pool)
Nathan Fontenotb41b83e2017-05-03 14:04:56 -04001387{
Thomas Falcon637f81d2018-02-26 18:10:57 -06001388 struct ibmvnic_tx_buff *tx_buff;
Nathan Fontenotb41b83e2017-05-03 14:04:56 -04001389 u64 tx_entries;
Thomas Falcone9e1e972018-03-16 20:00:30 -05001390 int i;
Nathan Fontenotb41b83e2017-05-03 14:04:56 -04001391
Dan Carpenter050e85c2018-03-23 14:36:15 +03001392 if (!tx_pool || !tx_pool->tx_buff)
Thomas Falcone9e1e972018-03-16 20:00:30 -05001393 return;
1394
1395 tx_entries = tx_pool->num_buffers;
1396
1397 for (i = 0; i < tx_entries; i++) {
1398 tx_buff = &tx_pool->tx_buff[i];
1399 if (tx_buff && tx_buff->skb) {
1400 dev_kfree_skb_any(tx_buff->skb);
1401 tx_buff->skb = NULL;
1402 }
1403 }
1404}
1405
1406static void clean_tx_pools(struct ibmvnic_adapter *adapter)
1407{
1408 int tx_scrqs;
1409 int i;
1410
1411 if (!adapter->tx_pool || !adapter->tso_pool)
Nathan Fontenotb41b83e2017-05-03 14:04:56 -04001412 return;
1413
Thomas Falcon660e3092018-04-20 14:25:32 -05001414 tx_scrqs = adapter->num_active_tx_pools;
Nathan Fontenotb41b83e2017-05-03 14:04:56 -04001415
1416 /* Free any remaining skbs in the tx buffer pools */
1417 for (i = 0; i < tx_scrqs; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001418 netdev_dbg(adapter->netdev, "Cleaning tx_pool[%d]\n", i);
Thomas Falcone9e1e972018-03-16 20:00:30 -05001419 clean_one_tx_pool(adapter, &adapter->tx_pool[i]);
1420 clean_one_tx_pool(adapter, &adapter->tso_pool[i]);
Nathan Fontenotb41b83e2017-05-03 14:04:56 -04001421 }
1422}
1423
John Allen6095e592018-03-30 13:44:21 -05001424static void ibmvnic_disable_irqs(struct ibmvnic_adapter *adapter)
John Allenea5509f2017-03-17 17:13:43 -05001425{
John Allen6095e592018-03-30 13:44:21 -05001426 struct net_device *netdev = adapter->netdev;
John Allenea5509f2017-03-17 17:13:43 -05001427 int i;
1428
Nathan Fontenot46293b92017-05-03 14:05:02 -04001429 if (adapter->tx_scrq) {
1430 for (i = 0; i < adapter->req_tx_queues; i++)
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001431 if (adapter->tx_scrq[i]->irq) {
Thomas Falconf8738662018-03-07 17:51:45 -06001432 netdev_dbg(netdev,
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001433 "Disabling tx_scrq[%d] irq\n", i);
Thomas Falconf23e0642018-04-15 18:53:36 -05001434 disable_scrq_irq(adapter, adapter->tx_scrq[i]);
Nathan Fontenot46293b92017-05-03 14:05:02 -04001435 disable_irq(adapter->tx_scrq[i]->irq);
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001436 }
Nathan Fontenot46293b92017-05-03 14:05:02 -04001437 }
1438
Nathan Fontenot46293b92017-05-03 14:05:02 -04001439 if (adapter->rx_scrq) {
1440 for (i = 0; i < adapter->req_rx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001441 if (adapter->rx_scrq[i]->irq) {
Thomas Falconf8738662018-03-07 17:51:45 -06001442 netdev_dbg(netdev,
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001443 "Disabling rx_scrq[%d] irq\n", i);
Thomas Falconf23e0642018-04-15 18:53:36 -05001444 disable_scrq_irq(adapter, adapter->rx_scrq[i]);
Nathan Fontenot46293b92017-05-03 14:05:02 -04001445 disable_irq(adapter->rx_scrq[i]->irq);
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001446 }
Nathan Fontenot46293b92017-05-03 14:05:02 -04001447 }
1448 }
John Allen6095e592018-03-30 13:44:21 -05001449}
1450
1451static void ibmvnic_cleanup(struct net_device *netdev)
1452{
1453 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1454
1455 /* ensure that transmissions are stopped if called by do_reset */
Juliet Kim7ed5b312019-09-20 16:11:23 -04001456 if (test_bit(0, &adapter->resetting))
John Allen6095e592018-03-30 13:44:21 -05001457 netif_tx_disable(netdev);
1458 else
1459 netif_tx_stop_all_queues(netdev);
1460
1461 ibmvnic_napi_disable(adapter);
1462 ibmvnic_disable_irqs(adapter);
1463
Thomas Falcond0869c02018-02-13 18:23:43 -06001464 clean_rx_pools(adapter);
Thomas Falcon10f76212017-05-26 10:30:31 -04001465 clean_tx_pools(adapter);
Thomas Falcon01d9bd72018-03-07 17:51:46 -06001466}
1467
1468static int __ibmvnic_close(struct net_device *netdev)
1469{
1470 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1471 int rc = 0;
1472
1473 adapter->state = VNIC_CLOSING;
1474 rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_DN);
Nathan Fontenot90c80142017-05-03 14:04:32 -04001475 adapter->state = VNIC_CLOSED;
Sukadev Bhattiprolud4083d32021-02-10 17:41:43 -08001476 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001477}
1478
Nathan Fontenoted651a12017-05-03 14:04:38 -04001479static int ibmvnic_close(struct net_device *netdev)
1480{
1481 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1482 int rc;
1483
Lijun Pan0666ef72021-04-12 02:41:28 -05001484 netdev_dbg(netdev, "[S:%s FOP:%d FRR:%d] Closing\n",
1485 adapter_state_to_string(adapter->state),
1486 adapter->failover_pending,
Sukadev Bhattiprolu38bd5ce2020-12-04 18:22:35 -08001487 adapter->force_reset_recovery);
1488
Thomas Falcon5a18e1e2018-04-06 18:37:05 -05001489 /* If device failover is pending, just set device state and return.
1490 * Device operation will be handled by reset routine.
1491 */
1492 if (adapter->failover_pending) {
1493 adapter->state = VNIC_CLOSED;
1494 return 0;
1495 }
1496
Nathan Fontenoted651a12017-05-03 14:04:38 -04001497 rc = __ibmvnic_close(netdev);
Nathan Fontenot30f79622018-04-06 18:37:06 -05001498 ibmvnic_cleanup(netdev);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001499
1500 return rc;
1501}
1502
Thomas Falconad7775d2016-04-01 17:20:34 -05001503/**
1504 * build_hdr_data - creates L2/L3/L4 header data buffer
Lee Jones80708602021-01-15 20:09:03 +00001505 * @hdr_field: bitfield determining needed headers
1506 * @skb: socket buffer
1507 * @hdr_len: array of header lengths
1508 * @hdr_data: buffer to write the header to
Thomas Falconad7775d2016-04-01 17:20:34 -05001509 *
1510 * Reads hdr_field to determine which headers are needed by firmware.
1511 * Builds a buffer containing these headers. Saves individual header
1512 * lengths and total buffer length to be used to build descriptors.
1513 */
1514static int build_hdr_data(u8 hdr_field, struct sk_buff *skb,
1515 int *hdr_len, u8 *hdr_data)
1516{
1517 int len = 0;
1518 u8 *hdr;
1519
Thomas Falconda75e3b2018-03-12 11:51:02 -05001520 if (skb_vlan_tagged(skb) && !skb_vlan_tag_present(skb))
1521 hdr_len[0] = sizeof(struct vlan_ethhdr);
1522 else
1523 hdr_len[0] = sizeof(struct ethhdr);
Thomas Falconad7775d2016-04-01 17:20:34 -05001524
1525 if (skb->protocol == htons(ETH_P_IP)) {
1526 hdr_len[1] = ip_hdr(skb)->ihl * 4;
1527 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
1528 hdr_len[2] = tcp_hdrlen(skb);
1529 else if (ip_hdr(skb)->protocol == IPPROTO_UDP)
1530 hdr_len[2] = sizeof(struct udphdr);
1531 } else if (skb->protocol == htons(ETH_P_IPV6)) {
1532 hdr_len[1] = sizeof(struct ipv6hdr);
1533 if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
1534 hdr_len[2] = tcp_hdrlen(skb);
1535 else if (ipv6_hdr(skb)->nexthdr == IPPROTO_UDP)
1536 hdr_len[2] = sizeof(struct udphdr);
Thomas Falcon4eb50ce2017-12-18 12:52:40 -06001537 } else if (skb->protocol == htons(ETH_P_ARP)) {
1538 hdr_len[1] = arp_hdr_len(skb->dev);
1539 hdr_len[2] = 0;
Thomas Falconad7775d2016-04-01 17:20:34 -05001540 }
1541
1542 memset(hdr_data, 0, 120);
1543 if ((hdr_field >> 6) & 1) {
1544 hdr = skb_mac_header(skb);
1545 memcpy(hdr_data, hdr, hdr_len[0]);
1546 len += hdr_len[0];
1547 }
1548
1549 if ((hdr_field >> 5) & 1) {
1550 hdr = skb_network_header(skb);
1551 memcpy(hdr_data + len, hdr, hdr_len[1]);
1552 len += hdr_len[1];
1553 }
1554
1555 if ((hdr_field >> 4) & 1) {
1556 hdr = skb_transport_header(skb);
1557 memcpy(hdr_data + len, hdr, hdr_len[2]);
1558 len += hdr_len[2];
1559 }
1560 return len;
1561}
1562
1563/**
1564 * create_hdr_descs - create header and header extension descriptors
Lee Jones80708602021-01-15 20:09:03 +00001565 * @hdr_field: bitfield determining needed headers
1566 * @hdr_data: buffer containing header data
1567 * @len: length of data buffer
1568 * @hdr_len: array of individual header lengths
1569 * @scrq_arr: descriptor array
Thomas Falconad7775d2016-04-01 17:20:34 -05001570 *
1571 * Creates header and, if needed, header extension descriptors and
1572 * places them in a descriptor array, scrq_arr
1573 */
1574
Thomas Falcon2de09682017-10-16 10:02:11 -05001575static int create_hdr_descs(u8 hdr_field, u8 *hdr_data, int len, int *hdr_len,
1576 union sub_crq *scrq_arr)
Thomas Falconad7775d2016-04-01 17:20:34 -05001577{
1578 union sub_crq hdr_desc;
1579 int tmp_len = len;
Thomas Falcon2de09682017-10-16 10:02:11 -05001580 int num_descs = 0;
Thomas Falconad7775d2016-04-01 17:20:34 -05001581 u8 *data, *cur;
1582 int tmp;
1583
1584 while (tmp_len > 0) {
1585 cur = hdr_data + len - tmp_len;
1586
1587 memset(&hdr_desc, 0, sizeof(hdr_desc));
1588 if (cur != hdr_data) {
1589 data = hdr_desc.hdr_ext.data;
1590 tmp = tmp_len > 29 ? 29 : tmp_len;
1591 hdr_desc.hdr_ext.first = IBMVNIC_CRQ_CMD;
1592 hdr_desc.hdr_ext.type = IBMVNIC_HDR_EXT_DESC;
1593 hdr_desc.hdr_ext.len = tmp;
1594 } else {
1595 data = hdr_desc.hdr.data;
1596 tmp = tmp_len > 24 ? 24 : tmp_len;
1597 hdr_desc.hdr.first = IBMVNIC_CRQ_CMD;
1598 hdr_desc.hdr.type = IBMVNIC_HDR_DESC;
1599 hdr_desc.hdr.len = tmp;
1600 hdr_desc.hdr.l2_len = (u8)hdr_len[0];
1601 hdr_desc.hdr.l3_len = cpu_to_be16((u16)hdr_len[1]);
1602 hdr_desc.hdr.l4_len = (u8)hdr_len[2];
1603 hdr_desc.hdr.flag = hdr_field << 1;
1604 }
1605 memcpy(data, cur, tmp);
1606 tmp_len -= tmp;
1607 *scrq_arr = hdr_desc;
1608 scrq_arr++;
Thomas Falcon2de09682017-10-16 10:02:11 -05001609 num_descs++;
Thomas Falconad7775d2016-04-01 17:20:34 -05001610 }
Thomas Falcon2de09682017-10-16 10:02:11 -05001611
1612 return num_descs;
Thomas Falconad7775d2016-04-01 17:20:34 -05001613}
1614
1615/**
1616 * build_hdr_descs_arr - build a header descriptor array
Lijun Pan73214a62021-06-11 10:43:39 -05001617 * @skb: tx socket buffer
1618 * @indir_arr: indirect array
Lee Jones80708602021-01-15 20:09:03 +00001619 * @num_entries: number of descriptors to be sent
1620 * @hdr_field: bit field determining which headers will be sent
Thomas Falconad7775d2016-04-01 17:20:34 -05001621 *
1622 * This function will build a TX descriptor array with applicable
1623 * L2/L3/L4 packet header descriptors to be sent by send_subcrq_indirect.
1624 */
1625
Thomas Falconc62aa372020-11-18 19:12:20 -06001626static void build_hdr_descs_arr(struct sk_buff *skb,
1627 union sub_crq *indir_arr,
Thomas Falconad7775d2016-04-01 17:20:34 -05001628 int *num_entries, u8 hdr_field)
1629{
1630 int hdr_len[3] = {0, 0, 0};
Thomas Falconc62aa372020-11-18 19:12:20 -06001631 u8 hdr_data[140] = {0};
Thomas Falcon2de09682017-10-16 10:02:11 -05001632 int tot_len;
Thomas Falconad7775d2016-04-01 17:20:34 -05001633
Thomas Falconc62aa372020-11-18 19:12:20 -06001634 tot_len = build_hdr_data(hdr_field, skb, hdr_len,
1635 hdr_data);
Thomas Falcon2de09682017-10-16 10:02:11 -05001636 *num_entries += create_hdr_descs(hdr_field, hdr_data, tot_len, hdr_len,
Thomas Falconc62aa372020-11-18 19:12:20 -06001637 indir_arr + 1);
Thomas Falconad7775d2016-04-01 17:20:34 -05001638}
1639
Thomas Falcon1f247a62018-03-12 11:51:04 -05001640static int ibmvnic_xmit_workarounds(struct sk_buff *skb,
1641 struct net_device *netdev)
1642{
1643 /* For some backing devices, mishandling of small packets
1644 * can result in a loss of connection or TX stall. Device
1645 * architects recommend that no packet should be smaller
1646 * than the minimum MTU value provided to the driver, so
1647 * pad any packets to that length
1648 */
1649 if (skb->len < netdev->min_mtu)
1650 return skb_put_padto(skb, netdev->min_mtu);
Thomas Falcon7083a452018-03-12 21:05:26 -05001651
1652 return 0;
Thomas Falcon1f247a62018-03-12 11:51:04 -05001653}
1654
Thomas Falcon0d973382020-11-18 19:12:19 -06001655static void ibmvnic_tx_scrq_clean_buffer(struct ibmvnic_adapter *adapter,
1656 struct ibmvnic_sub_crq_queue *tx_scrq)
1657{
1658 struct ibmvnic_ind_xmit_queue *ind_bufp;
1659 struct ibmvnic_tx_buff *tx_buff;
1660 struct ibmvnic_tx_pool *tx_pool;
1661 union sub_crq tx_scrq_entry;
1662 int queue_num;
1663 int entries;
1664 int index;
1665 int i;
1666
1667 ind_bufp = &tx_scrq->ind_buf;
1668 entries = (u64)ind_bufp->index;
1669 queue_num = tx_scrq->pool_index;
1670
1671 for (i = entries - 1; i >= 0; --i) {
1672 tx_scrq_entry = ind_bufp->indir_arr[i];
1673 if (tx_scrq_entry.v1.type != IBMVNIC_TX_DESC)
1674 continue;
1675 index = be32_to_cpu(tx_scrq_entry.v1.correlator);
1676 if (index & IBMVNIC_TSO_POOL_MASK) {
1677 tx_pool = &adapter->tso_pool[queue_num];
1678 index &= ~IBMVNIC_TSO_POOL_MASK;
1679 } else {
1680 tx_pool = &adapter->tx_pool[queue_num];
1681 }
1682 tx_pool->free_map[tx_pool->consumer_index] = index;
1683 tx_pool->consumer_index = tx_pool->consumer_index == 0 ?
1684 tx_pool->num_buffers - 1 :
1685 tx_pool->consumer_index - 1;
1686 tx_buff = &tx_pool->tx_buff[index];
1687 adapter->netdev->stats.tx_packets--;
1688 adapter->netdev->stats.tx_bytes -= tx_buff->skb->len;
1689 adapter->tx_stats_buffers[queue_num].packets--;
1690 adapter->tx_stats_buffers[queue_num].bytes -=
1691 tx_buff->skb->len;
1692 dev_kfree_skb_any(tx_buff->skb);
1693 tx_buff->skb = NULL;
1694 adapter->netdev->stats.tx_dropped++;
1695 }
1696 ind_bufp->index = 0;
1697 if (atomic_sub_return(entries, &tx_scrq->used) <=
1698 (adapter->req_tx_entries_per_subcrq / 2) &&
Sukadev Bhattiprolu65d64702021-06-23 21:13:12 -07001699 __netif_subqueue_stopped(adapter->netdev, queue_num) &&
1700 !test_bit(0, &adapter->resetting)) {
Thomas Falcon0d973382020-11-18 19:12:19 -06001701 netif_wake_subqueue(adapter->netdev, queue_num);
1702 netdev_dbg(adapter->netdev, "Started queue %d\n",
1703 queue_num);
1704 }
1705}
1706
1707static int ibmvnic_tx_scrq_flush(struct ibmvnic_adapter *adapter,
1708 struct ibmvnic_sub_crq_queue *tx_scrq)
1709{
1710 struct ibmvnic_ind_xmit_queue *ind_bufp;
1711 u64 dma_addr;
1712 u64 entries;
1713 u64 handle;
1714 int rc;
1715
1716 ind_bufp = &tx_scrq->ind_buf;
1717 dma_addr = (u64)ind_bufp->indir_dma;
1718 entries = (u64)ind_bufp->index;
1719 handle = tx_scrq->handle;
1720
1721 if (!entries)
1722 return 0;
1723 rc = send_subcrq_indirect(adapter, handle, dma_addr, entries);
1724 if (rc)
1725 ibmvnic_tx_scrq_clean_buffer(adapter, tx_scrq);
1726 else
1727 ind_bufp->index = 0;
1728 return 0;
1729}
1730
YueHaibing94b2bb22018-09-18 14:35:47 +08001731static netdev_tx_t ibmvnic_xmit(struct sk_buff *skb, struct net_device *netdev)
Thomas Falcon032c5e82015-12-21 11:26:06 -06001732{
1733 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1734 int queue_num = skb_get_queue_mapping(skb);
Thomas Falconad7775d2016-04-01 17:20:34 -05001735 u8 *hdrs = (u8 *)&adapter->tx_rx_desc_req;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001736 struct device *dev = &adapter->vdev->dev;
Thomas Falcon0d973382020-11-18 19:12:19 -06001737 struct ibmvnic_ind_xmit_queue *ind_bufp;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001738 struct ibmvnic_tx_buff *tx_buff = NULL;
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001739 struct ibmvnic_sub_crq_queue *tx_scrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001740 struct ibmvnic_tx_pool *tx_pool;
1741 unsigned int tx_send_failed = 0;
Thomas Falcon0d973382020-11-18 19:12:19 -06001742 netdev_tx_t ret = NETDEV_TX_OK;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001743 unsigned int tx_map_failed = 0;
Thomas Falconc62aa372020-11-18 19:12:20 -06001744 union sub_crq indir_arr[16];
Thomas Falcon032c5e82015-12-21 11:26:06 -06001745 unsigned int tx_dropped = 0;
1746 unsigned int tx_packets = 0;
1747 unsigned int tx_bytes = 0;
1748 dma_addr_t data_dma_addr;
1749 struct netdev_queue *txq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001750 unsigned long lpar_rc;
1751 union sub_crq tx_crq;
1752 unsigned int offset;
Thomas Falconad7775d2016-04-01 17:20:34 -05001753 int num_entries = 1;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001754 unsigned char *dst;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001755 int index = 0;
Thomas Falcona0dca102018-01-18 19:29:48 -06001756 u8 proto = 0;
Thomas Falcon0d973382020-11-18 19:12:19 -06001757
1758 tx_scrq = adapter->tx_scrq[queue_num];
1759 txq = netdev_get_tx_queue(netdev, queue_num);
1760 ind_bufp = &tx_scrq->ind_buf;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001761
Juliet Kim7ed5b312019-09-20 16:11:23 -04001762 if (test_bit(0, &adapter->resetting)) {
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001763 if (!netif_subqueue_stopped(netdev, skb))
1764 netif_stop_subqueue(netdev, queue_num);
1765 dev_kfree_skb_any(skb);
1766
Thomas Falcon032c5e82015-12-21 11:26:06 -06001767 tx_send_failed++;
1768 tx_dropped++;
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001769 ret = NETDEV_TX_OK;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001770 goto out;
1771 }
1772
Thomas Falcon7083a452018-03-12 21:05:26 -05001773 if (ibmvnic_xmit_workarounds(skb, netdev)) {
Thomas Falcon1f247a62018-03-12 11:51:04 -05001774 tx_dropped++;
1775 tx_send_failed++;
1776 ret = NETDEV_TX_OK;
Thomas Falcon0d973382020-11-18 19:12:19 -06001777 ibmvnic_tx_scrq_flush(adapter, tx_scrq);
Thomas Falcon1f247a62018-03-12 11:51:04 -05001778 goto out;
1779 }
Thomas Falcon06b3e352018-03-16 20:00:28 -05001780 if (skb_is_gso(skb))
1781 tx_pool = &adapter->tso_pool[queue_num];
1782 else
1783 tx_pool = &adapter->tx_pool[queue_num];
Thomas Falcon1f247a62018-03-12 11:51:04 -05001784
Thomas Falcon032c5e82015-12-21 11:26:06 -06001785 index = tx_pool->free_map[tx_pool->consumer_index];
Thomas Falconfdb06102017-10-17 12:36:55 -05001786
Thomas Falcon86b61a52018-03-16 20:00:29 -05001787 if (index == IBMVNIC_INVALID_MAP) {
1788 dev_kfree_skb_any(skb);
1789 tx_send_failed++;
1790 tx_dropped++;
Sukadev Bhattiprolubb553622021-07-20 19:34:39 -07001791 ibmvnic_tx_scrq_flush(adapter, tx_scrq);
Thomas Falcon86b61a52018-03-16 20:00:29 -05001792 ret = NETDEV_TX_OK;
1793 goto out;
1794 }
1795
1796 tx_pool->free_map[tx_pool->consumer_index] = IBMVNIC_INVALID_MAP;
1797
Thomas Falcon06b3e352018-03-16 20:00:28 -05001798 offset = index * tx_pool->buf_size;
1799 dst = tx_pool->long_term_buff.buff + offset;
1800 memset(dst, 0, tx_pool->buf_size);
1801 data_dma_addr = tx_pool->long_term_buff.addr + offset;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001802
Thomas Falcon15482052017-10-17 12:36:54 -05001803 if (skb_shinfo(skb)->nr_frags) {
1804 int cur, i;
1805
1806 /* Copy the head */
1807 skb_copy_from_linear_data(skb, dst, skb_headlen(skb));
1808 cur = skb_headlen(skb);
1809
1810 /* Copy the frags */
1811 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1812 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1813
Christophe JAILLETc3105f82021-04-04 10:54:37 +02001814 memcpy(dst + cur, skb_frag_address(frag),
1815 skb_frag_size(frag));
Thomas Falcon15482052017-10-17 12:36:54 -05001816 cur += skb_frag_size(frag);
1817 }
1818 } else {
1819 skb_copy_from_linear_data(skb, dst, skb->len);
1820 }
1821
Lijun Pan42557da2021-02-12 20:48:40 -06001822 /* post changes to long_term_buff *dst before VIOS accessing it */
1823 dma_wmb();
1824
Thomas Falcon032c5e82015-12-21 11:26:06 -06001825 tx_pool->consumer_index =
Thomas Falcon06b3e352018-03-16 20:00:28 -05001826 (tx_pool->consumer_index + 1) % tx_pool->num_buffers;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001827
1828 tx_buff = &tx_pool->tx_buff[index];
1829 tx_buff->skb = skb;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001830 tx_buff->index = index;
1831 tx_buff->pool_index = queue_num;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001832
1833 memset(&tx_crq, 0, sizeof(tx_crq));
1834 tx_crq.v1.first = IBMVNIC_CRQ_CMD;
1835 tx_crq.v1.type = IBMVNIC_TX_DESC;
1836 tx_crq.v1.n_crq_elem = 1;
1837 tx_crq.v1.n_sge = 1;
1838 tx_crq.v1.flags1 = IBMVNIC_TX_COMP_NEEDED;
Thomas Falcon06b3e352018-03-16 20:00:28 -05001839
Thomas Falconfdb06102017-10-17 12:36:55 -05001840 if (skb_is_gso(skb))
Thomas Falcon06b3e352018-03-16 20:00:28 -05001841 tx_crq.v1.correlator =
1842 cpu_to_be32(index | IBMVNIC_TSO_POOL_MASK);
Thomas Falconfdb06102017-10-17 12:36:55 -05001843 else
Thomas Falcon06b3e352018-03-16 20:00:28 -05001844 tx_crq.v1.correlator = cpu_to_be32(index);
1845 tx_crq.v1.dma_reg = cpu_to_be16(tx_pool->long_term_buff.map_id);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001846 tx_crq.v1.sge_len = cpu_to_be32(skb->len);
1847 tx_crq.v1.ioba = cpu_to_be64(data_dma_addr);
1848
Michał Mirosławe84b4792018-11-07 17:50:52 +01001849 if (adapter->vlan_header_insertion && skb_vlan_tag_present(skb)) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06001850 tx_crq.v1.flags2 |= IBMVNIC_TX_VLAN_INSERT;
1851 tx_crq.v1.vlan_id = cpu_to_be16(skb->vlan_tci);
1852 }
1853
1854 if (skb->protocol == htons(ETH_P_IP)) {
Thomas Falcona0dca102018-01-18 19:29:48 -06001855 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV4;
1856 proto = ip_hdr(skb)->protocol;
1857 } else if (skb->protocol == htons(ETH_P_IPV6)) {
1858 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV6;
1859 proto = ipv6_hdr(skb)->nexthdr;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001860 }
1861
Thomas Falcona0dca102018-01-18 19:29:48 -06001862 if (proto == IPPROTO_TCP)
1863 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_TCP;
1864 else if (proto == IPPROTO_UDP)
1865 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_UDP;
1866
Thomas Falconad7775d2016-04-01 17:20:34 -05001867 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06001868 tx_crq.v1.flags1 |= IBMVNIC_TX_CHKSUM_OFFLOAD;
Thomas Falconad7775d2016-04-01 17:20:34 -05001869 hdrs += 2;
1870 }
Thomas Falconfdb06102017-10-17 12:36:55 -05001871 if (skb_is_gso(skb)) {
1872 tx_crq.v1.flags1 |= IBMVNIC_TX_LSO;
1873 tx_crq.v1.mss = cpu_to_be16(skb_shinfo(skb)->gso_size);
1874 hdrs += 2;
1875 }
Thomas Falcon0d973382020-11-18 19:12:19 -06001876
1877 if ((*hdrs >> 7) & 1)
Thomas Falconc62aa372020-11-18 19:12:20 -06001878 build_hdr_descs_arr(skb, indir_arr, &num_entries, *hdrs);
Thomas Falcon0d973382020-11-18 19:12:19 -06001879
1880 tx_crq.v1.n_crq_elem = num_entries;
1881 tx_buff->num_entries = num_entries;
1882 /* flush buffer if current entry can not fit */
1883 if (num_entries + ind_bufp->index > IBMVNIC_MAX_IND_DESCS) {
1884 lpar_rc = ibmvnic_tx_scrq_flush(adapter, tx_scrq);
1885 if (lpar_rc != H_SUCCESS)
1886 goto tx_flush_err;
Thomas Falconad7775d2016-04-01 17:20:34 -05001887 }
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001888
Thomas Falconc62aa372020-11-18 19:12:20 -06001889 indir_arr[0] = tx_crq;
1890 memcpy(&ind_bufp->indir_arr[ind_bufp->index], &indir_arr[0],
Thomas Falcon0d973382020-11-18 19:12:19 -06001891 num_entries * sizeof(struct ibmvnic_generic_scrq));
1892 ind_bufp->index += num_entries;
1893 if (__netdev_tx_sent_queue(txq, skb->len,
1894 netdev_xmit_more() &&
1895 ind_bufp->index < IBMVNIC_MAX_IND_DESCS)) {
1896 lpar_rc = ibmvnic_tx_scrq_flush(adapter, tx_scrq);
1897 if (lpar_rc != H_SUCCESS)
1898 goto tx_err;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001899 }
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001900
Thomas Falconffc385b2018-02-18 10:08:41 -06001901 if (atomic_add_return(num_entries, &tx_scrq->used)
Brian King58c8c0c2017-04-19 13:44:47 -04001902 >= adapter->req_tx_entries_per_subcrq) {
Thomas Falcon0aecb132018-02-26 18:10:58 -06001903 netdev_dbg(netdev, "Stopping queue %d\n", queue_num);
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001904 netif_stop_subqueue(netdev, queue_num);
1905 }
1906
Thomas Falcon032c5e82015-12-21 11:26:06 -06001907 tx_packets++;
1908 tx_bytes += skb->len;
1909 txq->trans_start = jiffies;
1910 ret = NETDEV_TX_OK;
Thomas Falcon86b61a52018-03-16 20:00:29 -05001911 goto out;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001912
Thomas Falcon0d973382020-11-18 19:12:19 -06001913tx_flush_err:
1914 dev_kfree_skb_any(skb);
1915 tx_buff->skb = NULL;
1916 tx_pool->consumer_index = tx_pool->consumer_index == 0 ?
1917 tx_pool->num_buffers - 1 :
1918 tx_pool->consumer_index - 1;
1919 tx_dropped++;
1920tx_err:
1921 if (lpar_rc != H_CLOSED && lpar_rc != H_PARAMETER)
1922 dev_err_ratelimited(dev, "tx: send failed\n");
1923
1924 if (lpar_rc == H_CLOSED || adapter->failover_pending) {
1925 /* Disable TX and report carrier off if queue is closed
1926 * or pending failover.
1927 * Firmware guarantees that a signal will be sent to the
1928 * driver, triggering a reset or some other action.
1929 */
1930 netif_tx_stop_all_queues(netdev);
1931 netif_carrier_off(netdev);
1932 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001933out:
1934 netdev->stats.tx_dropped += tx_dropped;
1935 netdev->stats.tx_bytes += tx_bytes;
1936 netdev->stats.tx_packets += tx_packets;
1937 adapter->tx_send_failed += tx_send_failed;
1938 adapter->tx_map_failed += tx_map_failed;
John Allen3d52b592017-08-02 16:44:14 -05001939 adapter->tx_stats_buffers[queue_num].packets += tx_packets;
1940 adapter->tx_stats_buffers[queue_num].bytes += tx_bytes;
1941 adapter->tx_stats_buffers[queue_num].dropped_packets += tx_dropped;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001942
1943 return ret;
1944}
1945
1946static void ibmvnic_set_multi(struct net_device *netdev)
1947{
1948 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1949 struct netdev_hw_addr *ha;
1950 union ibmvnic_crq crq;
1951
1952 memset(&crq, 0, sizeof(crq));
1953 crq.request_capability.first = IBMVNIC_CRQ_CMD;
1954 crq.request_capability.cmd = REQUEST_CAPABILITY;
1955
1956 if (netdev->flags & IFF_PROMISC) {
1957 if (!adapter->promisc_supported)
1958 return;
1959 } else {
1960 if (netdev->flags & IFF_ALLMULTI) {
1961 /* Accept all multicast */
1962 memset(&crq, 0, sizeof(crq));
1963 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1964 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1965 crq.multicast_ctrl.flags = IBMVNIC_ENABLE_ALL;
1966 ibmvnic_send_crq(adapter, &crq);
1967 } else if (netdev_mc_empty(netdev)) {
1968 /* Reject all multicast */
1969 memset(&crq, 0, sizeof(crq));
1970 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1971 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1972 crq.multicast_ctrl.flags = IBMVNIC_DISABLE_ALL;
1973 ibmvnic_send_crq(adapter, &crq);
1974 } else {
1975 /* Accept one or more multicast(s) */
1976 netdev_for_each_mc_addr(ha, netdev) {
1977 memset(&crq, 0, sizeof(crq));
1978 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1979 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1980 crq.multicast_ctrl.flags = IBMVNIC_ENABLE_MC;
1981 ether_addr_copy(&crq.multicast_ctrl.mac_addr[0],
1982 ha->addr);
1983 ibmvnic_send_crq(adapter, &crq);
1984 }
1985 }
1986 }
1987}
1988
Thomas Falcon62740e92019-05-09 23:13:43 -05001989static int __ibmvnic_set_mac(struct net_device *netdev, u8 *dev_addr)
Thomas Falcon032c5e82015-12-21 11:26:06 -06001990{
1991 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001992 union ibmvnic_crq crq;
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05001993 int rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001994
Thomas Falcon62740e92019-05-09 23:13:43 -05001995 if (!is_valid_ether_addr(dev_addr)) {
1996 rc = -EADDRNOTAVAIL;
1997 goto err;
1998 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001999
2000 memset(&crq, 0, sizeof(crq));
2001 crq.change_mac_addr.first = IBMVNIC_CRQ_CMD;
2002 crq.change_mac_addr.cmd = CHANGE_MAC_ADDR;
Thomas Falcon62740e92019-05-09 23:13:43 -05002003 ether_addr_copy(&crq.change_mac_addr.mac_addr[0], dev_addr);
Thomas Falconf8136142018-01-29 13:45:05 -06002004
Thomas Falconff25dcb2019-11-25 17:12:56 -06002005 mutex_lock(&adapter->fw_lock);
2006 adapter->fw_done_rc = 0;
Thomas Falcon070eca92019-11-25 17:12:53 -06002007 reinit_completion(&adapter->fw_done);
Thomas Falconff25dcb2019-11-25 17:12:56 -06002008
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05002009 rc = ibmvnic_send_crq(adapter, &crq);
Thomas Falcon62740e92019-05-09 23:13:43 -05002010 if (rc) {
2011 rc = -EIO;
Thomas Falconff25dcb2019-11-25 17:12:56 -06002012 mutex_unlock(&adapter->fw_lock);
Thomas Falcon62740e92019-05-09 23:13:43 -05002013 goto err;
2014 }
2015
Thomas Falcon476d96c2019-11-25 17:12:55 -06002016 rc = ibmvnic_wait_for_completion(adapter, &adapter->fw_done, 10000);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002017 /* netdev->dev_addr is changed in handle_change_mac_rsp function */
Thomas Falcon476d96c2019-11-25 17:12:55 -06002018 if (rc || adapter->fw_done_rc) {
Thomas Falcon62740e92019-05-09 23:13:43 -05002019 rc = -EIO;
Thomas Falconff25dcb2019-11-25 17:12:56 -06002020 mutex_unlock(&adapter->fw_lock);
Thomas Falcon62740e92019-05-09 23:13:43 -05002021 goto err;
2022 }
Thomas Falconff25dcb2019-11-25 17:12:56 -06002023 mutex_unlock(&adapter->fw_lock);
Thomas Falcon62740e92019-05-09 23:13:43 -05002024 return 0;
2025err:
2026 ether_addr_copy(adapter->mac_addr, netdev->dev_addr);
2027 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002028}
2029
John Allenc26eba02017-10-26 16:23:25 -05002030static int ibmvnic_set_mac(struct net_device *netdev, void *p)
2031{
2032 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2033 struct sockaddr *addr = p;
Thomas Falconf8136142018-01-29 13:45:05 -06002034 int rc;
John Allenc26eba02017-10-26 16:23:25 -05002035
Thomas Falcon62740e92019-05-09 23:13:43 -05002036 rc = 0;
Lijun Pan8fc36722020-10-27 17:04:56 -05002037 if (!is_valid_ether_addr(addr->sa_data))
2038 return -EADDRNOTAVAIL;
2039
Jiri Wiesner67eb2112021-03-04 17:18:28 +01002040 ether_addr_copy(adapter->mac_addr, addr->sa_data);
2041 if (adapter->state != VNIC_PROBED)
Thomas Falcon62740e92019-05-09 23:13:43 -05002042 rc = __ibmvnic_set_mac(netdev, addr->sa_data);
John Allenc26eba02017-10-26 16:23:25 -05002043
Thomas Falconf8136142018-01-29 13:45:05 -06002044 return rc;
John Allenc26eba02017-10-26 16:23:25 -05002045}
2046
Lijun Pancaee7bf2021-04-12 02:41:27 -05002047static const char *reset_reason_to_string(enum ibmvnic_reset_reason reason)
2048{
2049 switch (reason) {
2050 case VNIC_RESET_FAILOVER:
2051 return "FAILOVER";
2052 case VNIC_RESET_MOBILITY:
2053 return "MOBILITY";
2054 case VNIC_RESET_FATAL:
2055 return "FATAL";
2056 case VNIC_RESET_NON_FATAL:
2057 return "NON_FATAL";
2058 case VNIC_RESET_TIMEOUT:
2059 return "TIMEOUT";
2060 case VNIC_RESET_CHANGE_PARAM:
2061 return "CHANGE_PARAM";
Lijun Pan822ebc22021-06-11 10:35:37 -05002062 case VNIC_RESET_PASSIVE_INIT:
2063 return "PASSIVE_INIT";
Lijun Pancaee7bf2021-04-12 02:41:27 -05002064 }
Michal Suchanek07b5dc12021-05-20 08:50:34 +02002065 return "UNKNOWN";
Lijun Pancaee7bf2021-04-12 02:41:27 -05002066}
2067
Lee Jones80708602021-01-15 20:09:03 +00002068/*
Nathan Fontenoted651a12017-05-03 14:04:38 -04002069 * do_reset returns zero if we are able to keep processing reset events, or
2070 * non-zero if we hit a fatal error and must halt.
2071 */
2072static int do_reset(struct ibmvnic_adapter *adapter,
2073 struct ibmvnic_rwi *rwi, u32 reset_state)
2074{
John Allen896d8692018-01-18 16:26:31 -06002075 u64 old_num_rx_queues, old_num_tx_queues;
Thomas Falcon5bf032e2018-11-21 11:17:59 -06002076 u64 old_num_rx_slots, old_num_tx_slots;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002077 struct net_device *netdev = adapter->netdev;
Lijun Pand3a6abc2021-04-14 02:46:15 -05002078 int rc;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002079
Sukadev Bhattiprolu38bd5ce2020-12-04 18:22:35 -08002080 netdev_dbg(adapter->netdev,
Lijun Pan0666ef72021-04-12 02:41:28 -05002081 "[S:%s FOP:%d] Reset reason: %s, reset_state: %s\n",
2082 adapter_state_to_string(adapter->state),
2083 adapter->failover_pending,
2084 reset_reason_to_string(rwi->reset_reason),
2085 adapter_state_to_string(reset_state));
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002086
Lijun Pan3f5ec372021-01-06 15:35:14 -06002087 adapter->reset_reason = rwi->reset_reason;
2088 /* requestor of VNIC_RESET_CHANGE_PARAM already has the rtnl lock */
2089 if (!(adapter->reset_reason == VNIC_RESET_CHANGE_PARAM))
2090 rtnl_lock();
2091
Lijun Panbab08be2021-02-11 00:43:19 -06002092 /* Now that we have the rtnl lock, clear any pending failover.
Sukadev Bhattiprolu1d850492020-10-30 10:07:11 -07002093 * This will ensure ibmvnic_open() has either completed or will
2094 * block until failover is complete.
2095 */
2096 if (rwi->reset_reason == VNIC_RESET_FAILOVER)
2097 adapter->failover_pending = false;
Juliet Kimb27507b2019-09-20 16:11:22 -04002098
Sukadev Bhattiprolu8f1c0fd2021-02-23 21:02:29 -08002099 /* read the state and check (again) after getting rtnl */
2100 reset_state = adapter->state;
2101
2102 if (reset_state == VNIC_REMOVING || reset_state == VNIC_REMOVED) {
2103 rc = -EBUSY;
2104 goto out;
2105 }
2106
Nathan Fontenoted651a12017-05-03 14:04:38 -04002107 netif_carrier_off(netdev);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002108
John Allen896d8692018-01-18 16:26:31 -06002109 old_num_rx_queues = adapter->req_rx_queues;
2110 old_num_tx_queues = adapter->req_tx_queues;
Thomas Falcon5bf032e2018-11-21 11:17:59 -06002111 old_num_rx_slots = adapter->req_rx_add_entries_per_subcrq;
2112 old_num_tx_slots = adapter->req_tx_entries_per_subcrq;
John Allen896d8692018-01-18 16:26:31 -06002113
Nathan Fontenot30f79622018-04-06 18:37:06 -05002114 ibmvnic_cleanup(netdev);
2115
Thomas Falcon1f946082019-06-07 16:03:53 -05002116 if (reset_state == VNIC_OPEN &&
2117 adapter->reset_reason != VNIC_RESET_MOBILITY &&
Nathan Fontenot30f79622018-04-06 18:37:06 -05002118 adapter->reset_reason != VNIC_RESET_FAILOVER) {
Lijun Pan3f5ec372021-01-06 15:35:14 -06002119 if (adapter->reset_reason == VNIC_RESET_CHANGE_PARAM) {
2120 rc = __ibmvnic_close(netdev);
2121 if (rc)
2122 goto out;
2123 } else {
2124 adapter->state = VNIC_CLOSING;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002125
Lijun Pan3f5ec372021-01-06 15:35:14 -06002126 /* Release the RTNL lock before link state change and
2127 * re-acquire after the link state change to allow
2128 * linkwatch_event to grab the RTNL lock and run during
2129 * a reset.
2130 */
2131 rtnl_unlock();
2132 rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_DN);
2133 rtnl_lock();
2134 if (rc)
2135 goto out;
Juliet Kimb27507b2019-09-20 16:11:22 -04002136
Sukadev Bhattiprolu8f1c0fd2021-02-23 21:02:29 -08002137 if (adapter->state == VNIC_OPEN) {
2138 /* When we dropped rtnl, ibmvnic_open() got
2139 * it and noticed that we are resetting and
2140 * set the adapter state to OPEN. Update our
2141 * new "target" state, and resume the reset
2142 * from VNIC_CLOSING state.
2143 */
2144 netdev_dbg(netdev,
Lijun Pan0666ef72021-04-12 02:41:28 -05002145 "Open changed state from %s, updating.\n",
2146 adapter_state_to_string(reset_state));
Sukadev Bhattiprolu8f1c0fd2021-02-23 21:02:29 -08002147 reset_state = VNIC_OPEN;
2148 adapter->state = VNIC_CLOSING;
2149 }
2150
Lijun Pan3f5ec372021-01-06 15:35:14 -06002151 if (adapter->state != VNIC_CLOSING) {
Sukadev Bhattiprolu8f1c0fd2021-02-23 21:02:29 -08002152 /* If someone else changed the adapter state
2153 * when we dropped the rtnl, fail the reset
2154 */
Lijun Pan3f5ec372021-01-06 15:35:14 -06002155 rc = -1;
2156 goto out;
2157 }
Lijun Pan3f5ec372021-01-06 15:35:14 -06002158 adapter->state = VNIC_CLOSED;
Juliet Kimb27507b2019-09-20 16:11:22 -04002159 }
Lijun Pan3f5ec372021-01-06 15:35:14 -06002160 }
Juliet Kimb27507b2019-09-20 16:11:22 -04002161
Lijun Pan3f5ec372021-01-06 15:35:14 -06002162 if (adapter->reset_reason == VNIC_RESET_CHANGE_PARAM) {
2163 release_resources(adapter);
2164 release_sub_crqs(adapter, 1);
2165 release_crq_queue(adapter);
John Allenc26eba02017-10-26 16:23:25 -05002166 }
2167
John Allen8cb31cf2017-05-26 10:30:37 -04002168 if (adapter->reset_reason != VNIC_RESET_NON_FATAL) {
2169 /* remove the closed state so when we call open it appears
2170 * we are coming from the probed state.
2171 */
Nathan Fontenoted651a12017-05-03 14:04:38 -04002172 adapter->state = VNIC_PROBED;
John Allen8cb31cf2017-05-26 10:30:37 -04002173
Lijun Pan3f5ec372021-01-06 15:35:14 -06002174 if (adapter->reset_reason == VNIC_RESET_CHANGE_PARAM) {
2175 rc = init_crq_queue(adapter);
2176 } else if (adapter->reset_reason == VNIC_RESET_MOBILITY) {
Nathan Fontenot30f79622018-04-06 18:37:06 -05002177 rc = ibmvnic_reenable_crq_queue(adapter);
2178 release_sub_crqs(adapter, 1);
2179 } else {
2180 rc = ibmvnic_reset_crq(adapter);
Dany Madden8b40eb732020-06-18 15:24:13 -04002181 if (rc == H_CLOSED || rc == H_SUCCESS) {
Nathan Fontenot30f79622018-04-06 18:37:06 -05002182 rc = vio_enable_interrupts(adapter->vdev);
Dany Madden8b40eb732020-06-18 15:24:13 -04002183 if (rc)
2184 netdev_err(adapter->netdev,
2185 "Reset failed to enable interrupts. rc=%d\n",
2186 rc);
2187 }
Nathan Fontenot30f79622018-04-06 18:37:06 -05002188 }
2189
2190 if (rc) {
2191 netdev_err(adapter->netdev,
Dany Madden8b40eb732020-06-18 15:24:13 -04002192 "Reset couldn't initialize crq. rc=%d\n", rc);
Juliet Kimb27507b2019-09-20 16:11:22 -04002193 goto out;
Nathan Fontenot30f79622018-04-06 18:37:06 -05002194 }
2195
Lijun Pan635e4422020-08-19 17:52:26 -05002196 rc = ibmvnic_reset_init(adapter, true);
Juliet Kimb27507b2019-09-20 16:11:22 -04002197 if (rc) {
2198 rc = IBMVNIC_INIT_FAILED;
2199 goto out;
2200 }
John Allen8cb31cf2017-05-26 10:30:37 -04002201
Cristobal Forno53f8b1b2021-06-10 11:08:35 -06002202 /* If the adapter was in PROBE or DOWN state prior to the reset,
John Allen8cb31cf2017-05-26 10:30:37 -04002203 * exit here.
2204 */
Cristobal Forno53f8b1b2021-06-10 11:08:35 -06002205 if (reset_state == VNIC_PROBED || reset_state == VNIC_DOWN) {
Juliet Kimb27507b2019-09-20 16:11:22 -04002206 rc = 0;
2207 goto out;
2208 }
John Allen8cb31cf2017-05-26 10:30:37 -04002209
2210 rc = ibmvnic_login(netdev);
Lijun Panf78afaa2021-02-11 00:43:20 -06002211 if (rc)
Juliet Kimb27507b2019-09-20 16:11:22 -04002212 goto out;
John Allen8cb31cf2017-05-26 10:30:37 -04002213
Lijun Pan3f5ec372021-01-06 15:35:14 -06002214 if (adapter->reset_reason == VNIC_RESET_CHANGE_PARAM) {
2215 rc = init_resources(adapter);
2216 if (rc)
2217 goto out;
2218 } else if (adapter->req_rx_queues != old_num_rx_queues ||
Juliet Kimb27507b2019-09-20 16:11:22 -04002219 adapter->req_tx_queues != old_num_tx_queues ||
2220 adapter->req_rx_add_entries_per_subcrq !=
2221 old_num_rx_slots ||
2222 adapter->req_tx_entries_per_subcrq !=
Mingming Cao9f134572020-08-25 13:26:41 -04002223 old_num_tx_slots ||
2224 !adapter->rx_pool ||
2225 !adapter->tso_pool ||
2226 !adapter->tx_pool) {
John Allen896d8692018-01-18 16:26:31 -06002227 release_rx_pools(adapter);
2228 release_tx_pools(adapter);
Juliet Kima5681e22018-11-19 15:59:22 -06002229 release_napi(adapter);
2230 release_vpd_data(adapter);
2231
2232 rc = init_resources(adapter);
Thomas Falconf611a5b2018-08-30 13:19:53 -05002233 if (rc)
Juliet Kimb27507b2019-09-20 16:11:22 -04002234 goto out;
Nathan Fontenotd9043c12018-02-19 13:30:14 -06002235
John Allenc26eba02017-10-26 16:23:25 -05002236 } else {
2237 rc = reset_tx_pools(adapter);
Jakub Kicinski8ae4dff2020-09-04 21:07:49 -07002238 if (rc) {
Mingming Cao9f134572020-08-25 13:26:41 -04002239 netdev_dbg(adapter->netdev, "reset tx pools failed (%d)\n",
Lijun Pan91dc5d22021-02-11 00:43:22 -06002240 rc);
Juliet Kimb27507b2019-09-20 16:11:22 -04002241 goto out;
Jakub Kicinski8ae4dff2020-09-04 21:07:49 -07002242 }
Nathan Fontenot8c0543a2017-05-26 10:31:06 -04002243
John Allenc26eba02017-10-26 16:23:25 -05002244 rc = reset_rx_pools(adapter);
Jakub Kicinski8ae4dff2020-09-04 21:07:49 -07002245 if (rc) {
Mingming Cao9f134572020-08-25 13:26:41 -04002246 netdev_dbg(adapter->netdev, "reset rx pools failed (%d)\n",
Lijun Pan91dc5d22021-02-11 00:43:22 -06002247 rc);
Juliet Kimb27507b2019-09-20 16:11:22 -04002248 goto out;
Jakub Kicinski8ae4dff2020-09-04 21:07:49 -07002249 }
John Allenc26eba02017-10-26 16:23:25 -05002250 }
Thomas Falcon134bbe72018-05-16 15:49:04 -05002251 ibmvnic_disable_irqs(adapter);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002252 }
John Allene676d812018-03-14 10:41:29 -05002253 adapter->state = VNIC_CLOSED;
2254
Juliet Kimb27507b2019-09-20 16:11:22 -04002255 if (reset_state == VNIC_CLOSED) {
2256 rc = 0;
2257 goto out;
2258 }
John Allene676d812018-03-14 10:41:29 -05002259
Nathan Fontenoted651a12017-05-03 14:04:38 -04002260 rc = __ibmvnic_open(netdev);
2261 if (rc) {
Juliet Kimb27507b2019-09-20 16:11:22 -04002262 rc = IBMVNIC_OPEN_FAILED;
2263 goto out;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002264 }
2265
Thomas Falconbe32a242019-06-07 16:03:54 -05002266 /* refresh device's multicast list */
2267 ibmvnic_set_multi(netdev);
2268
Lijun Pan98025bc2020-11-20 16:40:12 -06002269 if (adapter->reset_reason == VNIC_RESET_FAILOVER ||
Lijun Pan6be46662020-12-14 15:19:29 -06002270 adapter->reset_reason == VNIC_RESET_MOBILITY)
2271 __netdev_notify_peers(netdev);
Nathan Fontenot61d3e1d2017-06-12 20:47:45 -04002272
Juliet Kimb27507b2019-09-20 16:11:22 -04002273 rc = 0;
2274
2275out:
Dany Madden0cb4bc62020-11-25 18:04:27 -06002276 /* restore the adapter state if reset failed */
2277 if (rc)
2278 adapter->state = reset_state;
Lijun Pan3f5ec372021-01-06 15:35:14 -06002279 /* requestor of VNIC_RESET_CHANGE_PARAM should still hold the rtnl lock */
2280 if (!(adapter->reset_reason == VNIC_RESET_CHANGE_PARAM))
2281 rtnl_unlock();
Juliet Kimb27507b2019-09-20 16:11:22 -04002282
Lijun Pan0666ef72021-04-12 02:41:28 -05002283 netdev_dbg(adapter->netdev, "[S:%s FOP:%d] Reset done, rc %d\n",
2284 adapter_state_to_string(adapter->state),
2285 adapter->failover_pending, rc);
Juliet Kimb27507b2019-09-20 16:11:22 -04002286 return rc;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002287}
2288
Thomas Falcon2770a792018-05-23 13:38:02 -05002289static int do_hard_reset(struct ibmvnic_adapter *adapter,
2290 struct ibmvnic_rwi *rwi, u32 reset_state)
2291{
2292 struct net_device *netdev = adapter->netdev;
2293 int rc;
2294
Lijun Pancaee7bf2021-04-12 02:41:27 -05002295 netdev_dbg(adapter->netdev, "Hard resetting driver (%s)\n",
2296 reset_reason_to_string(rwi->reset_reason));
Thomas Falcon2770a792018-05-23 13:38:02 -05002297
Sukadev Bhattiprolu8f1c0fd2021-02-23 21:02:29 -08002298 /* read the state and check (again) after getting rtnl */
2299 reset_state = adapter->state;
2300
2301 if (reset_state == VNIC_REMOVING || reset_state == VNIC_REMOVED) {
2302 rc = -EBUSY;
2303 goto out;
2304 }
2305
Thomas Falcon2770a792018-05-23 13:38:02 -05002306 netif_carrier_off(netdev);
2307 adapter->reset_reason = rwi->reset_reason;
2308
2309 ibmvnic_cleanup(netdev);
2310 release_resources(adapter);
2311 release_sub_crqs(adapter, 0);
2312 release_crq_queue(adapter);
2313
2314 /* remove the closed state so when we call open it appears
2315 * we are coming from the probed state.
2316 */
2317 adapter->state = VNIC_PROBED;
2318
Thomas Falconbbd669a2019-04-04 18:58:26 -05002319 reinit_completion(&adapter->init_done);
Thomas Falcon2770a792018-05-23 13:38:02 -05002320 rc = init_crq_queue(adapter);
2321 if (rc) {
2322 netdev_err(adapter->netdev,
2323 "Couldn't initialize crq. rc=%d\n", rc);
Dany Madden0cb4bc62020-11-25 18:04:27 -06002324 goto out;
Thomas Falcon2770a792018-05-23 13:38:02 -05002325 }
2326
Lijun Pan635e4422020-08-19 17:52:26 -05002327 rc = ibmvnic_reset_init(adapter, false);
Thomas Falcon2770a792018-05-23 13:38:02 -05002328 if (rc)
Dany Madden0cb4bc62020-11-25 18:04:27 -06002329 goto out;
Thomas Falcon2770a792018-05-23 13:38:02 -05002330
Cristobal Forno53f8b1b2021-06-10 11:08:35 -06002331 /* If the adapter was in PROBE or DOWN state prior to the reset,
Thomas Falcon2770a792018-05-23 13:38:02 -05002332 * exit here.
2333 */
Cristobal Forno53f8b1b2021-06-10 11:08:35 -06002334 if (reset_state == VNIC_PROBED || reset_state == VNIC_DOWN)
Dany Madden0cb4bc62020-11-25 18:04:27 -06002335 goto out;
Thomas Falcon2770a792018-05-23 13:38:02 -05002336
2337 rc = ibmvnic_login(netdev);
Dany Madden0cb4bc62020-11-25 18:04:27 -06002338 if (rc)
2339 goto out;
Juliet Kima5681e22018-11-19 15:59:22 -06002340
2341 rc = init_resources(adapter);
Thomas Falcon2770a792018-05-23 13:38:02 -05002342 if (rc)
Dany Madden0cb4bc62020-11-25 18:04:27 -06002343 goto out;
Thomas Falcon2770a792018-05-23 13:38:02 -05002344
2345 ibmvnic_disable_irqs(adapter);
2346 adapter->state = VNIC_CLOSED;
2347
2348 if (reset_state == VNIC_CLOSED)
Dany Madden0cb4bc62020-11-25 18:04:27 -06002349 goto out;
Thomas Falcon2770a792018-05-23 13:38:02 -05002350
2351 rc = __ibmvnic_open(netdev);
Dany Madden0cb4bc62020-11-25 18:04:27 -06002352 if (rc) {
2353 rc = IBMVNIC_OPEN_FAILED;
2354 goto out;
2355 }
Thomas Falcon2770a792018-05-23 13:38:02 -05002356
Lijun Pan6be46662020-12-14 15:19:29 -06002357 __netdev_notify_peers(netdev);
Dany Madden0cb4bc62020-11-25 18:04:27 -06002358out:
2359 /* restore adapter state if reset failed */
2360 if (rc)
2361 adapter->state = reset_state;
Lijun Pan0666ef72021-04-12 02:41:28 -05002362 netdev_dbg(adapter->netdev, "[S:%s FOP:%d] Hard reset done, rc %d\n",
2363 adapter_state_to_string(adapter->state),
2364 adapter->failover_pending, rc);
Dany Madden0cb4bc62020-11-25 18:04:27 -06002365 return rc;
Thomas Falcon2770a792018-05-23 13:38:02 -05002366}
2367
Nathan Fontenoted651a12017-05-03 14:04:38 -04002368static struct ibmvnic_rwi *get_next_rwi(struct ibmvnic_adapter *adapter)
2369{
2370 struct ibmvnic_rwi *rwi;
Thomas Falcon6c5c7482018-12-10 15:22:22 -06002371 unsigned long flags;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002372
Thomas Falcon6c5c7482018-12-10 15:22:22 -06002373 spin_lock_irqsave(&adapter->rwi_lock, flags);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002374
2375 if (!list_empty(&adapter->rwi_list)) {
2376 rwi = list_first_entry(&adapter->rwi_list, struct ibmvnic_rwi,
2377 list);
2378 list_del(&rwi->list);
2379 } else {
2380 rwi = NULL;
2381 }
2382
Thomas Falcon6c5c7482018-12-10 15:22:22 -06002383 spin_unlock_irqrestore(&adapter->rwi_lock, flags);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002384 return rwi;
2385}
2386
Cristobal Forno53f8b1b2021-06-10 11:08:35 -06002387/**
2388 * do_passive_init - complete probing when partner device is detected.
2389 * @adapter: ibmvnic_adapter struct
2390 *
2391 * If the ibmvnic device does not have a partner device to communicate with at boot
2392 * and that partner device comes online at a later time, this function is called
2393 * to complete the initialization process of ibmvnic device.
2394 * Caller is expected to hold rtnl_lock().
2395 *
2396 * Returns non-zero if sub-CRQs are not initialized properly leaving the device
2397 * in the down state.
2398 * Returns 0 upon success and the device is in PROBED state.
2399 */
2400
2401static int do_passive_init(struct ibmvnic_adapter *adapter)
2402{
2403 unsigned long timeout = msecs_to_jiffies(30000);
2404 struct net_device *netdev = adapter->netdev;
2405 struct device *dev = &adapter->vdev->dev;
2406 int rc;
2407
2408 netdev_dbg(netdev, "Partner device found, probing.\n");
2409
2410 adapter->state = VNIC_PROBING;
2411 reinit_completion(&adapter->init_done);
2412 adapter->init_done_rc = 0;
2413 adapter->crq.active = true;
2414
2415 rc = send_crq_init_complete(adapter);
2416 if (rc)
2417 goto out;
2418
2419 rc = send_version_xchg(adapter);
2420 if (rc)
2421 netdev_dbg(adapter->netdev, "send_version_xchg failed, rc=%d\n", rc);
2422
2423 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
2424 dev_err(dev, "Initialization sequence timed out\n");
2425 rc = -ETIMEDOUT;
2426 goto out;
2427 }
2428
2429 rc = init_sub_crqs(adapter);
2430 if (rc) {
2431 dev_err(dev, "Initialization of sub crqs failed, rc=%d\n", rc);
2432 goto out;
2433 }
2434
2435 rc = init_sub_crq_irqs(adapter);
2436 if (rc) {
2437 dev_err(dev, "Failed to initialize sub crq irqs\n, rc=%d", rc);
2438 goto init_failed;
2439 }
2440
2441 netdev->mtu = adapter->req_mtu - ETH_HLEN;
2442 netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
2443 netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
2444
2445 adapter->state = VNIC_PROBED;
2446 netdev_dbg(netdev, "Probed successfully. Waiting for signal from partner device.\n");
2447
2448 return 0;
2449
2450init_failed:
2451 release_sub_crqs(adapter, 1);
2452out:
2453 adapter->state = VNIC_DOWN;
2454 return rc;
2455}
2456
Nathan Fontenoted651a12017-05-03 14:04:38 -04002457static void __ibmvnic_reset(struct work_struct *work)
2458{
Nathan Fontenoted651a12017-05-03 14:04:38 -04002459 struct ibmvnic_adapter *adapter;
Juliet Kim7d7195a2020-03-10 09:23:58 -05002460 bool saved_state = false;
Sukadev Bhattiprolu4f408e12021-06-30 14:36:17 -04002461 struct ibmvnic_rwi *tmprwi;
2462 struct ibmvnic_rwi *rwi;
Juliet Kim7d7195a2020-03-10 09:23:58 -05002463 unsigned long flags;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002464 u32 reset_state;
John Allenc26eba02017-10-26 16:23:25 -05002465 int rc = 0;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002466
2467 adapter = container_of(work, struct ibmvnic_adapter, ibmvnic_reset);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002468
Juliet Kim7ed5b312019-09-20 16:11:23 -04002469 if (test_and_set_bit_lock(0, &adapter->resetting)) {
Lijun Pan870e04a2021-04-13 14:33:39 -05002470 queue_delayed_work(system_long_wq,
2471 &adapter->ibmvnic_delayed_reset,
2472 IBMVNIC_RESET_DELAY);
Juliet Kim7ed5b312019-09-20 16:11:23 -04002473 return;
2474 }
2475
Nathan Fontenoted651a12017-05-03 14:04:38 -04002476 rwi = get_next_rwi(adapter);
2477 while (rwi) {
Juliet Kim7d7195a2020-03-10 09:23:58 -05002478 spin_lock_irqsave(&adapter->state_lock, flags);
2479
Thomas Falcon36f10312019-08-27 11:10:04 -05002480 if (adapter->state == VNIC_REMOVING ||
Michal Suchanekc8dc5592019-09-09 22:44:51 +02002481 adapter->state == VNIC_REMOVED) {
Juliet Kim7d7195a2020-03-10 09:23:58 -05002482 spin_unlock_irqrestore(&adapter->state_lock, flags);
Juliet Kim1c2977c2019-09-05 17:30:01 -04002483 kfree(rwi);
2484 rc = EBUSY;
2485 break;
2486 }
Thomas Falcon36f10312019-08-27 11:10:04 -05002487
Juliet Kim7d7195a2020-03-10 09:23:58 -05002488 if (!saved_state) {
2489 reset_state = adapter->state;
Juliet Kim7d7195a2020-03-10 09:23:58 -05002490 saved_state = true;
2491 }
2492 spin_unlock_irqrestore(&adapter->state_lock, flags);
2493
Cristobal Forno53f8b1b2021-06-10 11:08:35 -06002494 if (rwi->reset_reason == VNIC_RESET_PASSIVE_INIT) {
2495 rtnl_lock();
2496 rc = do_passive_init(adapter);
2497 rtnl_unlock();
2498 if (!rc)
2499 netif_carrier_on(adapter->netdev);
2500 } else if (adapter->force_reset_recovery) {
Lijun Panbab08be2021-02-11 00:43:19 -06002501 /* Since we are doing a hard reset now, clear the
Sukadev Bhattiprolu1d850492020-10-30 10:07:11 -07002502 * failover_pending flag so we don't ignore any
2503 * future MOBILITY or other resets.
2504 */
2505 adapter->failover_pending = false;
2506
Juliet Kimb27507b2019-09-20 16:11:22 -04002507 /* Transport event occurred during previous reset */
2508 if (adapter->wait_for_reset) {
2509 /* Previous was CHANGE_PARAM; caller locked */
2510 adapter->force_reset_recovery = false;
2511 rc = do_hard_reset(adapter, rwi, reset_state);
2512 } else {
2513 rtnl_lock();
2514 adapter->force_reset_recovery = false;
2515 rc = do_hard_reset(adapter, rwi, reset_state);
2516 rtnl_unlock();
2517 }
Sukadev Bhattiproluf15fde92020-11-25 18:04:28 -06002518 if (rc) {
2519 /* give backing device time to settle down */
2520 netdev_dbg(adapter->netdev,
Lijun Pan0666ef72021-04-12 02:41:28 -05002521 "[S:%s] Hard reset failed, waiting 60 secs\n",
2522 adapter_state_to_string(adapter->state));
Sukadev Bhattiproluf15fde92020-11-25 18:04:28 -06002523 set_current_state(TASK_UNINTERRUPTIBLE);
2524 schedule_timeout(60 * HZ);
2525 }
Lijun Pan1f45dc22020-12-23 14:49:04 -06002526 } else {
Thomas Falcon2770a792018-05-23 13:38:02 -05002527 rc = do_reset(adapter, rwi, reset_state);
2528 }
Sukadev Bhattiprolu4f408e12021-06-30 14:36:17 -04002529 tmprwi = rwi;
Dany Maddena86d5c62020-11-25 18:04:31 -06002530 adapter->last_reset_time = jiffies;
Dany Madden0cb4bc62020-11-25 18:04:27 -06002531
Dany Madden18f141b2020-11-25 18:04:25 -06002532 if (rc)
2533 netdev_dbg(adapter->netdev, "Reset failed, rc=%d\n", rc);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002534
2535 rwi = get_next_rwi(adapter);
Juliet Kim7ed5b312019-09-20 16:11:23 -04002536
Sukadev Bhattiprolu4f408e12021-06-30 14:36:17 -04002537 /*
2538 * If there is another reset queued, free the previous rwi
2539 * and process the new reset even if previous reset failed
2540 * (the previous reset could have failed because of a fail
2541 * over for instance, so process the fail over).
2542 *
2543 * If there are no resets queued and the previous reset failed,
2544 * the adapter would be in an undefined state. So retry the
2545 * previous reset as a hard reset.
2546 */
2547 if (rwi)
2548 kfree(tmprwi);
2549 else if (rc)
2550 rwi = tmprwi;
2551
Juliet Kim7ed5b312019-09-20 16:11:23 -04002552 if (rwi && (rwi->reset_reason == VNIC_RESET_FAILOVER ||
Sukadev Bhattiprolu4f408e12021-06-30 14:36:17 -04002553 rwi->reset_reason == VNIC_RESET_MOBILITY || rc))
Juliet Kim7ed5b312019-09-20 16:11:23 -04002554 adapter->force_reset_recovery = true;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002555 }
2556
John Allenc26eba02017-10-26 16:23:25 -05002557 if (adapter->wait_for_reset) {
John Allenc26eba02017-10-26 16:23:25 -05002558 adapter->reset_done_rc = rc;
2559 complete(&adapter->reset_done);
2560 }
2561
Juliet Kim7ed5b312019-09-20 16:11:23 -04002562 clear_bit_unlock(0, &adapter->resetting);
Sukadev Bhattiprolu38bd5ce2020-12-04 18:22:35 -08002563
2564 netdev_dbg(adapter->netdev,
Lijun Pan0666ef72021-04-12 02:41:28 -05002565 "[S:%s FRR:%d WFR:%d] Done processing resets\n",
2566 adapter_state_to_string(adapter->state),
2567 adapter->force_reset_recovery,
Sukadev Bhattiprolu38bd5ce2020-12-04 18:22:35 -08002568 adapter->wait_for_reset);
Juliet Kim7ed5b312019-09-20 16:11:23 -04002569}
2570
2571static void __ibmvnic_delayed_reset(struct work_struct *work)
2572{
2573 struct ibmvnic_adapter *adapter;
2574
2575 adapter = container_of(work, struct ibmvnic_adapter,
2576 ibmvnic_delayed_reset.work);
2577 __ibmvnic_reset(&adapter->ibmvnic_reset);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002578}
2579
Thomas Falconaf894d22018-04-06 18:37:04 -05002580static int ibmvnic_reset(struct ibmvnic_adapter *adapter,
2581 enum ibmvnic_reset_reason reason)
Nathan Fontenoted651a12017-05-03 14:04:38 -04002582{
Thomas Falcon2770a792018-05-23 13:38:02 -05002583 struct list_head *entry, *tmp_entry;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002584 struct ibmvnic_rwi *rwi, *tmp;
2585 struct net_device *netdev = adapter->netdev;
Thomas Falcon6c5c7482018-12-10 15:22:22 -06002586 unsigned long flags;
Thomas Falconaf894d22018-04-06 18:37:04 -05002587 int ret;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002588
Jakub Kicinskib646acd52021-02-16 22:58:44 -08002589 spin_lock_irqsave(&adapter->rwi_lock, flags);
2590
2591 /* If failover is pending don't schedule any other reset.
Sukadev Bhattiprolu1d850492020-10-30 10:07:11 -07002592 * Instead let the failover complete. If there is already a
2593 * a failover reset scheduled, we will detect and drop the
2594 * duplicate reset when walking the ->rwi_list below.
2595 */
Nathan Fontenoted651a12017-05-03 14:04:38 -04002596 if (adapter->state == VNIC_REMOVING ||
Thomas Falcon5a18e1e2018-04-06 18:37:05 -05002597 adapter->state == VNIC_REMOVED ||
Sukadev Bhattiprolu1d850492020-10-30 10:07:11 -07002598 (adapter->failover_pending && reason != VNIC_RESET_FAILOVER)) {
Thomas Falconaf894d22018-04-06 18:37:04 -05002599 ret = EBUSY;
Thomas Falcon5a18e1e2018-04-06 18:37:05 -05002600 netdev_dbg(netdev, "Adapter removing or pending failover, skipping reset\n");
Thomas Falconaf894d22018-04-06 18:37:04 -05002601 goto err;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002602 }
2603
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04002604 if (adapter->state == VNIC_PROBING) {
2605 netdev_warn(netdev, "Adapter reset during probe\n");
Lijun Pan91dc5d22021-02-11 00:43:22 -06002606 adapter->init_done_rc = EAGAIN;
2607 ret = EAGAIN;
Thomas Falconaf894d22018-04-06 18:37:04 -05002608 goto err;
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04002609 }
2610
Wang Hai3e98ae02021-06-10 20:54:17 +08002611 list_for_each_entry(tmp, &adapter->rwi_list, list) {
Nathan Fontenoted651a12017-05-03 14:04:38 -04002612 if (tmp->reset_reason == reason) {
Lijun Pancaee7bf2021-04-12 02:41:27 -05002613 netdev_dbg(netdev, "Skipping matching reset, reason=%s\n",
2614 reset_reason_to_string(reason));
Thomas Falconaf894d22018-04-06 18:37:04 -05002615 ret = EBUSY;
2616 goto err;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002617 }
2618 }
2619
Thomas Falcon1d1bbc32018-12-10 15:22:23 -06002620 rwi = kzalloc(sizeof(*rwi), GFP_ATOMIC);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002621 if (!rwi) {
Thomas Falconaf894d22018-04-06 18:37:04 -05002622 ret = ENOMEM;
2623 goto err;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002624 }
Thomas Falcon2770a792018-05-23 13:38:02 -05002625 /* if we just received a transport event,
2626 * flush reset queue and process this reset
2627 */
2628 if (adapter->force_reset_recovery && !list_empty(&adapter->rwi_list)) {
2629 list_for_each_safe(entry, tmp_entry, &adapter->rwi_list)
2630 list_del(entry);
2631 }
Nathan Fontenoted651a12017-05-03 14:04:38 -04002632 rwi->reset_reason = reason;
2633 list_add_tail(&rwi->list, &adapter->rwi_list);
Lijun Pancaee7bf2021-04-12 02:41:27 -05002634 netdev_dbg(adapter->netdev, "Scheduling reset (reason %s)\n",
2635 reset_reason_to_string(reason));
Lijun Pan870e04a2021-04-13 14:33:39 -05002636 queue_work(system_long_wq, &adapter->ibmvnic_reset);
Thomas Falconaf894d22018-04-06 18:37:04 -05002637
Sukadev Bhattiprolu4a41c422021-02-12 20:42:50 -08002638 ret = 0;
Thomas Falconaf894d22018-04-06 18:37:04 -05002639err:
Sukadev Bhattiprolu4a41c422021-02-12 20:42:50 -08002640 /* ibmvnic_close() below can block, so drop the lock first */
2641 spin_unlock_irqrestore(&adapter->rwi_lock, flags);
2642
2643 if (ret == ENOMEM)
2644 ibmvnic_close(netdev);
2645
Thomas Falconaf894d22018-04-06 18:37:04 -05002646 return -ret;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002647}
2648
Michael S. Tsirkin0290bd22019-12-10 09:23:51 -05002649static void ibmvnic_tx_timeout(struct net_device *dev, unsigned int txqueue)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002650{
2651 struct ibmvnic_adapter *adapter = netdev_priv(dev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002652
Lijun Pan855a6312020-11-20 16:40:13 -06002653 if (test_bit(0, &adapter->resetting)) {
2654 netdev_err(adapter->netdev,
2655 "Adapter is resetting, skip timeout reset\n");
2656 return;
2657 }
Dany Maddena86d5c62020-11-25 18:04:31 -06002658 /* No queuing up reset until at least 5 seconds (default watchdog val)
2659 * after last reset
2660 */
2661 if (time_before(jiffies, (adapter->last_reset_time + dev->watchdog_timeo))) {
2662 netdev_dbg(dev, "Not yet time to tx timeout.\n");
2663 return;
2664 }
Nathan Fontenoted651a12017-05-03 14:04:38 -04002665 ibmvnic_reset(adapter, VNIC_RESET_TIMEOUT);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002666}
2667
2668static void remove_buff_from_pool(struct ibmvnic_adapter *adapter,
2669 struct ibmvnic_rx_buff *rx_buff)
2670{
2671 struct ibmvnic_rx_pool *pool = &adapter->rx_pool[rx_buff->pool_index];
2672
2673 rx_buff->skb = NULL;
2674
2675 pool->free_map[pool->next_alloc] = (int)(rx_buff - pool->rx_buff);
2676 pool->next_alloc = (pool->next_alloc + 1) % pool->size;
2677
2678 atomic_dec(&pool->available);
2679}
2680
2681static int ibmvnic_poll(struct napi_struct *napi, int budget)
2682{
Dwip N. Banerjeeec20f362020-11-18 19:12:23 -06002683 struct ibmvnic_sub_crq_queue *rx_scrq;
2684 struct ibmvnic_adapter *adapter;
2685 struct net_device *netdev;
2686 int frames_processed;
2687 int scrq_num;
2688
2689 netdev = napi->dev;
2690 adapter = netdev_priv(netdev);
2691 scrq_num = (int)(napi - adapter->napi);
2692 frames_processed = 0;
2693 rx_scrq = adapter->rx_scrq[scrq_num];
Nathan Fontenot152ce472017-05-26 10:30:54 -04002694
Thomas Falcon032c5e82015-12-21 11:26:06 -06002695restart_poll:
2696 while (frames_processed < budget) {
2697 struct sk_buff *skb;
2698 struct ibmvnic_rx_buff *rx_buff;
2699 union sub_crq *next;
2700 u32 length;
2701 u16 offset;
2702 u8 flags = 0;
2703
Juliet Kim7ed5b312019-09-20 16:11:23 -04002704 if (unlikely(test_bit(0, &adapter->resetting) &&
John Allen34686562018-02-06 16:21:49 -06002705 adapter->reset_reason != VNIC_RESET_NON_FATAL)) {
Dwip N. Banerjeeec20f362020-11-18 19:12:23 -06002706 enable_scrq_irq(adapter, rx_scrq);
Thomas Falcon21ecba62017-06-14 23:50:09 -05002707 napi_complete_done(napi, frames_processed);
2708 return frames_processed;
2709 }
2710
Dwip N. Banerjeeec20f362020-11-18 19:12:23 -06002711 if (!pending_scrq(adapter, rx_scrq))
Thomas Falcon032c5e82015-12-21 11:26:06 -06002712 break;
Dwip N. Banerjeeec20f362020-11-18 19:12:23 -06002713 next = ibmvnic_next_scrq(adapter, rx_scrq);
Lijun Pan914789a2021-02-11 00:43:21 -06002714 rx_buff = (struct ibmvnic_rx_buff *)
2715 be64_to_cpu(next->rx_comp.correlator);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002716 /* do error checking */
2717 if (next->rx_comp.rc) {
John Allene1cea2e2017-08-07 15:42:30 -05002718 netdev_dbg(netdev, "rx buffer returned with rc %x\n",
2719 be16_to_cpu(next->rx_comp.rc));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002720 /* free the entry */
2721 next->rx_comp.first = 0;
Thomas Falcon4b9b0f02018-02-13 18:23:42 -06002722 dev_kfree_skb_any(rx_buff->skb);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002723 remove_buff_from_pool(adapter, rx_buff);
Nathan Fontenotca05e312017-05-03 14:05:14 -04002724 continue;
Thomas Falconabe27a82018-02-19 20:12:57 -06002725 } else if (!rx_buff->skb) {
2726 /* free the entry */
2727 next->rx_comp.first = 0;
2728 remove_buff_from_pool(adapter, rx_buff);
2729 continue;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002730 }
2731
2732 length = be32_to_cpu(next->rx_comp.len);
2733 offset = be16_to_cpu(next->rx_comp.off_frame_data);
2734 flags = next->rx_comp.flags;
2735 skb = rx_buff->skb;
Lijun Pan42557da2021-02-12 20:48:40 -06002736 /* load long_term_buff before copying to skb */
2737 dma_rmb();
Thomas Falcon032c5e82015-12-21 11:26:06 -06002738 skb_copy_to_linear_data(skb, rx_buff->data + offset,
2739 length);
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04002740
2741 /* VLAN Header has been stripped by the system firmware and
2742 * needs to be inserted by the driver
2743 */
2744 if (adapter->rx_vlan_header_insertion &&
2745 (flags & IBMVNIC_VLAN_STRIPPED))
2746 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
2747 ntohs(next->rx_comp.vlan_tci));
2748
Thomas Falcon032c5e82015-12-21 11:26:06 -06002749 /* free the entry */
2750 next->rx_comp.first = 0;
2751 remove_buff_from_pool(adapter, rx_buff);
2752
2753 skb_put(skb, length);
2754 skb->protocol = eth_type_trans(skb, netdev);
Thomas Falcon94ca3052017-05-03 14:05:20 -04002755 skb_record_rx_queue(skb, scrq_num);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002756
2757 if (flags & IBMVNIC_IP_CHKSUM_GOOD &&
2758 flags & IBMVNIC_TCP_UDP_CHKSUM_GOOD) {
2759 skb->ip_summed = CHECKSUM_UNNECESSARY;
2760 }
2761
2762 length = skb->len;
2763 napi_gro_receive(napi, skb); /* send it up */
2764 netdev->stats.rx_packets++;
2765 netdev->stats.rx_bytes += length;
John Allen3d52b592017-08-02 16:44:14 -05002766 adapter->rx_stats_buffers[scrq_num].packets++;
2767 adapter->rx_stats_buffers[scrq_num].bytes += length;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002768 frames_processed++;
2769 }
Nathan Fontenot152ce472017-05-26 10:30:54 -04002770
Dwip N. Banerjee41ed0a02020-11-18 19:12:25 -06002771 if (adapter->state != VNIC_CLOSING &&
2772 ((atomic_read(&adapter->rx_pool[scrq_num].available) <
2773 adapter->req_rx_add_entries_per_subcrq / 2) ||
2774 frames_processed < budget))
Nathan Fontenot152ce472017-05-26 10:30:54 -04002775 replenish_rx_pool(adapter, &adapter->rx_pool[scrq_num]);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002776 if (frames_processed < budget) {
Dwip N. Banerjeeec20f362020-11-18 19:12:23 -06002777 if (napi_complete_done(napi, frames_processed)) {
2778 enable_scrq_irq(adapter, rx_scrq);
2779 if (pending_scrq(adapter, rx_scrq)) {
Dwip N. Banerjeeec20f362020-11-18 19:12:23 -06002780 if (napi_reschedule(napi)) {
2781 disable_scrq_irq(adapter, rx_scrq);
2782 goto restart_poll;
2783 }
2784 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002785 }
2786 }
2787 return frames_processed;
2788}
2789
John Allenc26eba02017-10-26 16:23:25 -05002790static int wait_for_reset(struct ibmvnic_adapter *adapter)
2791{
Thomas Falconaf894d22018-04-06 18:37:04 -05002792 int rc, ret;
2793
John Allenc26eba02017-10-26 16:23:25 -05002794 adapter->fallback.mtu = adapter->req_mtu;
2795 adapter->fallback.rx_queues = adapter->req_rx_queues;
2796 adapter->fallback.tx_queues = adapter->req_tx_queues;
2797 adapter->fallback.rx_entries = adapter->req_rx_add_entries_per_subcrq;
2798 adapter->fallback.tx_entries = adapter->req_tx_entries_per_subcrq;
2799
Thomas Falcon070eca92019-11-25 17:12:53 -06002800 reinit_completion(&adapter->reset_done);
John Allenc26eba02017-10-26 16:23:25 -05002801 adapter->wait_for_reset = true;
Thomas Falconaf894d22018-04-06 18:37:04 -05002802 rc = ibmvnic_reset(adapter, VNIC_RESET_CHANGE_PARAM);
Thomas Falcon476d96c2019-11-25 17:12:55 -06002803
2804 if (rc) {
2805 ret = rc;
2806 goto out;
2807 }
2808 rc = ibmvnic_wait_for_completion(adapter, &adapter->reset_done, 60000);
2809 if (rc) {
2810 ret = -ENODEV;
2811 goto out;
2812 }
John Allenc26eba02017-10-26 16:23:25 -05002813
Thomas Falconaf894d22018-04-06 18:37:04 -05002814 ret = 0;
John Allenc26eba02017-10-26 16:23:25 -05002815 if (adapter->reset_done_rc) {
Thomas Falconaf894d22018-04-06 18:37:04 -05002816 ret = -EIO;
John Allenc26eba02017-10-26 16:23:25 -05002817 adapter->desired.mtu = adapter->fallback.mtu;
2818 adapter->desired.rx_queues = adapter->fallback.rx_queues;
2819 adapter->desired.tx_queues = adapter->fallback.tx_queues;
2820 adapter->desired.rx_entries = adapter->fallback.rx_entries;
2821 adapter->desired.tx_entries = adapter->fallback.tx_entries;
2822
Thomas Falcon070eca92019-11-25 17:12:53 -06002823 reinit_completion(&adapter->reset_done);
Thomas Falconaf894d22018-04-06 18:37:04 -05002824 adapter->wait_for_reset = true;
2825 rc = ibmvnic_reset(adapter, VNIC_RESET_CHANGE_PARAM);
Thomas Falcon476d96c2019-11-25 17:12:55 -06002826 if (rc) {
2827 ret = rc;
2828 goto out;
2829 }
2830 rc = ibmvnic_wait_for_completion(adapter, &adapter->reset_done,
2831 60000);
2832 if (rc) {
2833 ret = -ENODEV;
2834 goto out;
2835 }
John Allenc26eba02017-10-26 16:23:25 -05002836 }
Thomas Falcon476d96c2019-11-25 17:12:55 -06002837out:
John Allenc26eba02017-10-26 16:23:25 -05002838 adapter->wait_for_reset = false;
2839
Thomas Falconaf894d22018-04-06 18:37:04 -05002840 return ret;
John Allenc26eba02017-10-26 16:23:25 -05002841}
2842
John Allen3a807b72017-06-06 16:55:52 -05002843static int ibmvnic_change_mtu(struct net_device *netdev, int new_mtu)
2844{
John Allenc26eba02017-10-26 16:23:25 -05002845 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2846
2847 adapter->desired.mtu = new_mtu + ETH_HLEN;
2848
2849 return wait_for_reset(adapter);
John Allen3a807b72017-06-06 16:55:52 -05002850}
2851
Thomas Falconf10b09e2018-03-12 11:51:05 -05002852static netdev_features_t ibmvnic_features_check(struct sk_buff *skb,
2853 struct net_device *dev,
2854 netdev_features_t features)
2855{
2856 /* Some backing hardware adapters can not
2857 * handle packets with a MSS less than 224
2858 * or with only one segment.
2859 */
2860 if (skb_is_gso(skb)) {
2861 if (skb_shinfo(skb)->gso_size < 224 ||
2862 skb_shinfo(skb)->gso_segs == 1)
2863 features &= ~NETIF_F_GSO_MASK;
2864 }
2865
2866 return features;
2867}
2868
Thomas Falcon032c5e82015-12-21 11:26:06 -06002869static const struct net_device_ops ibmvnic_netdev_ops = {
2870 .ndo_open = ibmvnic_open,
2871 .ndo_stop = ibmvnic_close,
2872 .ndo_start_xmit = ibmvnic_xmit,
2873 .ndo_set_rx_mode = ibmvnic_set_multi,
2874 .ndo_set_mac_address = ibmvnic_set_mac,
2875 .ndo_validate_addr = eth_validate_addr,
Thomas Falcon032c5e82015-12-21 11:26:06 -06002876 .ndo_tx_timeout = ibmvnic_tx_timeout,
John Allen3a807b72017-06-06 16:55:52 -05002877 .ndo_change_mtu = ibmvnic_change_mtu,
Thomas Falconf10b09e2018-03-12 11:51:05 -05002878 .ndo_features_check = ibmvnic_features_check,
Thomas Falcon032c5e82015-12-21 11:26:06 -06002879};
2880
2881/* ethtool functions */
2882
Philippe Reynes8a433792017-01-07 22:37:29 +01002883static int ibmvnic_get_link_ksettings(struct net_device *netdev,
2884 struct ethtool_link_ksettings *cmd)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002885{
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03002886 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2887 int rc;
Philippe Reynes8a433792017-01-07 22:37:29 +01002888
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03002889 rc = send_query_phys_parms(adapter);
2890 if (rc) {
2891 adapter->speed = SPEED_UNKNOWN;
2892 adapter->duplex = DUPLEX_UNKNOWN;
2893 }
2894 cmd->base.speed = adapter->speed;
2895 cmd->base.duplex = adapter->duplex;
Philippe Reynes8a433792017-01-07 22:37:29 +01002896 cmd->base.port = PORT_FIBRE;
2897 cmd->base.phy_address = 0;
2898 cmd->base.autoneg = AUTONEG_ENABLE;
2899
Thomas Falcon032c5e82015-12-21 11:26:06 -06002900 return 0;
2901}
2902
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02002903static void ibmvnic_get_drvinfo(struct net_device *netdev,
Thomas Falcon032c5e82015-12-21 11:26:06 -06002904 struct ethtool_drvinfo *info)
2905{
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02002906 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2907
Lijun Pan8a96c802021-02-11 00:43:25 -06002908 strscpy(info->driver, ibmvnic_driver_name, sizeof(info->driver));
2909 strscpy(info->version, IBMVNIC_DRIVER_VERSION, sizeof(info->version));
2910 strscpy(info->fw_version, adapter->fw_version,
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02002911 sizeof(info->fw_version));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002912}
2913
2914static u32 ibmvnic_get_msglevel(struct net_device *netdev)
2915{
2916 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2917
2918 return adapter->msg_enable;
2919}
2920
2921static void ibmvnic_set_msglevel(struct net_device *netdev, u32 data)
2922{
2923 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2924
2925 adapter->msg_enable = data;
2926}
2927
2928static u32 ibmvnic_get_link(struct net_device *netdev)
2929{
2930 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2931
2932 /* Don't need to send a query because we request a logical link up at
2933 * init and then we wait for link state indications
2934 */
2935 return adapter->logical_link_state;
2936}
2937
2938static void ibmvnic_get_ringparam(struct net_device *netdev,
2939 struct ethtool_ringparam *ring)
2940{
John Allenbc131b32017-08-02 16:46:30 -05002941 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2942
Thomas Falcon723ad912018-09-28 18:38:26 -05002943 if (adapter->priv_flags & IBMVNIC_USE_SERVER_MAXES) {
2944 ring->rx_max_pending = adapter->max_rx_add_entries_per_subcrq;
2945 ring->tx_max_pending = adapter->max_tx_entries_per_subcrq;
2946 } else {
2947 ring->rx_max_pending = IBMVNIC_MAX_QUEUE_SZ;
2948 ring->tx_max_pending = IBMVNIC_MAX_QUEUE_SZ;
2949 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002950 ring->rx_mini_max_pending = 0;
2951 ring->rx_jumbo_max_pending = 0;
John Allenbc131b32017-08-02 16:46:30 -05002952 ring->rx_pending = adapter->req_rx_add_entries_per_subcrq;
2953 ring->tx_pending = adapter->req_tx_entries_per_subcrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002954 ring->rx_mini_pending = 0;
2955 ring->rx_jumbo_pending = 0;
2956}
2957
John Allenc26eba02017-10-26 16:23:25 -05002958static int ibmvnic_set_ringparam(struct net_device *netdev,
2959 struct ethtool_ringparam *ring)
2960{
2961 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Thomas Falcon723ad912018-09-28 18:38:26 -05002962 int ret;
John Allenc26eba02017-10-26 16:23:25 -05002963
Thomas Falcon723ad912018-09-28 18:38:26 -05002964 ret = 0;
John Allenc26eba02017-10-26 16:23:25 -05002965 adapter->desired.rx_entries = ring->rx_pending;
2966 adapter->desired.tx_entries = ring->tx_pending;
2967
Thomas Falcon723ad912018-09-28 18:38:26 -05002968 ret = wait_for_reset(adapter);
2969
2970 if (!ret &&
2971 (adapter->req_rx_add_entries_per_subcrq != ring->rx_pending ||
2972 adapter->req_tx_entries_per_subcrq != ring->tx_pending))
2973 netdev_info(netdev,
2974 "Could not match full ringsize request. Requested: RX %d, TX %d; Allowed: RX %llu, TX %llu\n",
2975 ring->rx_pending, ring->tx_pending,
2976 adapter->req_rx_add_entries_per_subcrq,
2977 adapter->req_tx_entries_per_subcrq);
2978 return ret;
John Allenc26eba02017-10-26 16:23:25 -05002979}
2980
John Allenc2dbeb62017-08-02 16:47:17 -05002981static void ibmvnic_get_channels(struct net_device *netdev,
2982 struct ethtool_channels *channels)
2983{
2984 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2985
Thomas Falcon723ad912018-09-28 18:38:26 -05002986 if (adapter->priv_flags & IBMVNIC_USE_SERVER_MAXES) {
2987 channels->max_rx = adapter->max_rx_queues;
2988 channels->max_tx = adapter->max_tx_queues;
2989 } else {
2990 channels->max_rx = IBMVNIC_MAX_QUEUES;
2991 channels->max_tx = IBMVNIC_MAX_QUEUES;
2992 }
2993
John Allenc2dbeb62017-08-02 16:47:17 -05002994 channels->max_other = 0;
2995 channels->max_combined = 0;
2996 channels->rx_count = adapter->req_rx_queues;
2997 channels->tx_count = adapter->req_tx_queues;
2998 channels->other_count = 0;
2999 channels->combined_count = 0;
3000}
3001
John Allenc26eba02017-10-26 16:23:25 -05003002static int ibmvnic_set_channels(struct net_device *netdev,
3003 struct ethtool_channels *channels)
3004{
3005 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Thomas Falcon723ad912018-09-28 18:38:26 -05003006 int ret;
John Allenc26eba02017-10-26 16:23:25 -05003007
Thomas Falcon723ad912018-09-28 18:38:26 -05003008 ret = 0;
John Allenc26eba02017-10-26 16:23:25 -05003009 adapter->desired.rx_queues = channels->rx_count;
3010 adapter->desired.tx_queues = channels->tx_count;
3011
Thomas Falcon723ad912018-09-28 18:38:26 -05003012 ret = wait_for_reset(adapter);
3013
3014 if (!ret &&
3015 (adapter->req_rx_queues != channels->rx_count ||
3016 adapter->req_tx_queues != channels->tx_count))
3017 netdev_info(netdev,
3018 "Could not match full channels request. Requested: RX %d, TX %d; Allowed: RX %llu, TX %llu\n",
3019 channels->rx_count, channels->tx_count,
3020 adapter->req_rx_queues, adapter->req_tx_queues);
3021 return ret;
John Allenc26eba02017-10-26 16:23:25 -05003022}
3023
Thomas Falcon032c5e82015-12-21 11:26:06 -06003024static void ibmvnic_get_strings(struct net_device *dev, u32 stringset, u8 *data)
3025{
John Allen3d52b592017-08-02 16:44:14 -05003026 struct ibmvnic_adapter *adapter = netdev_priv(dev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003027 int i;
3028
Thomas Falcon723ad912018-09-28 18:38:26 -05003029 switch (stringset) {
3030 case ETH_SS_STATS:
3031 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats);
3032 i++, data += ETH_GSTRING_LEN)
3033 memcpy(data, ibmvnic_stats[i].name, ETH_GSTRING_LEN);
3034
3035 for (i = 0; i < adapter->req_tx_queues; i++) {
3036 snprintf(data, ETH_GSTRING_LEN, "tx%d_packets", i);
3037 data += ETH_GSTRING_LEN;
3038
3039 snprintf(data, ETH_GSTRING_LEN, "tx%d_bytes", i);
3040 data += ETH_GSTRING_LEN;
3041
3042 snprintf(data, ETH_GSTRING_LEN,
3043 "tx%d_dropped_packets", i);
3044 data += ETH_GSTRING_LEN;
3045 }
3046
3047 for (i = 0; i < adapter->req_rx_queues; i++) {
3048 snprintf(data, ETH_GSTRING_LEN, "rx%d_packets", i);
3049 data += ETH_GSTRING_LEN;
3050
3051 snprintf(data, ETH_GSTRING_LEN, "rx%d_bytes", i);
3052 data += ETH_GSTRING_LEN;
3053
3054 snprintf(data, ETH_GSTRING_LEN, "rx%d_interrupts", i);
3055 data += ETH_GSTRING_LEN;
3056 }
3057 break;
3058
3059 case ETH_SS_PRIV_FLAGS:
3060 for (i = 0; i < ARRAY_SIZE(ibmvnic_priv_flags); i++)
3061 strcpy(data + i * ETH_GSTRING_LEN,
3062 ibmvnic_priv_flags[i]);
3063 break;
3064 default:
Thomas Falcon032c5e82015-12-21 11:26:06 -06003065 return;
John Allen3d52b592017-08-02 16:44:14 -05003066 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06003067}
3068
3069static int ibmvnic_get_sset_count(struct net_device *dev, int sset)
3070{
John Allen3d52b592017-08-02 16:44:14 -05003071 struct ibmvnic_adapter *adapter = netdev_priv(dev);
3072
Thomas Falcon032c5e82015-12-21 11:26:06 -06003073 switch (sset) {
3074 case ETH_SS_STATS:
John Allen3d52b592017-08-02 16:44:14 -05003075 return ARRAY_SIZE(ibmvnic_stats) +
3076 adapter->req_tx_queues * NUM_TX_STATS +
3077 adapter->req_rx_queues * NUM_RX_STATS;
Thomas Falcon723ad912018-09-28 18:38:26 -05003078 case ETH_SS_PRIV_FLAGS:
3079 return ARRAY_SIZE(ibmvnic_priv_flags);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003080 default:
3081 return -EOPNOTSUPP;
3082 }
3083}
3084
3085static void ibmvnic_get_ethtool_stats(struct net_device *dev,
3086 struct ethtool_stats *stats, u64 *data)
3087{
3088 struct ibmvnic_adapter *adapter = netdev_priv(dev);
3089 union ibmvnic_crq crq;
John Allen3d52b592017-08-02 16:44:14 -05003090 int i, j;
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05003091 int rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003092
3093 memset(&crq, 0, sizeof(crq));
3094 crq.request_statistics.first = IBMVNIC_CRQ_CMD;
3095 crq.request_statistics.cmd = REQUEST_STATISTICS;
3096 crq.request_statistics.ioba = cpu_to_be32(adapter->stats_token);
3097 crq.request_statistics.len =
3098 cpu_to_be32(sizeof(struct ibmvnic_statistics));
Thomas Falcon032c5e82015-12-21 11:26:06 -06003099
3100 /* Wait for data to be written */
Thomas Falcon070eca92019-11-25 17:12:53 -06003101 reinit_completion(&adapter->stats_done);
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05003102 rc = ibmvnic_send_crq(adapter, &crq);
3103 if (rc)
3104 return;
Thomas Falcon476d96c2019-11-25 17:12:55 -06003105 rc = ibmvnic_wait_for_completion(adapter, &adapter->stats_done, 10000);
3106 if (rc)
3107 return;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003108
3109 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++)
Lijun Pan91dc5d22021-02-11 00:43:22 -06003110 data[i] = be64_to_cpu(IBMVNIC_GET_STAT
3111 (adapter, ibmvnic_stats[i].offset));
John Allen3d52b592017-08-02 16:44:14 -05003112
3113 for (j = 0; j < adapter->req_tx_queues; j++) {
3114 data[i] = adapter->tx_stats_buffers[j].packets;
3115 i++;
3116 data[i] = adapter->tx_stats_buffers[j].bytes;
3117 i++;
3118 data[i] = adapter->tx_stats_buffers[j].dropped_packets;
3119 i++;
3120 }
3121
3122 for (j = 0; j < adapter->req_rx_queues; j++) {
3123 data[i] = adapter->rx_stats_buffers[j].packets;
3124 i++;
3125 data[i] = adapter->rx_stats_buffers[j].bytes;
3126 i++;
3127 data[i] = adapter->rx_stats_buffers[j].interrupts;
3128 i++;
3129 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06003130}
3131
Thomas Falcon723ad912018-09-28 18:38:26 -05003132static u32 ibmvnic_get_priv_flags(struct net_device *netdev)
3133{
3134 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
3135
3136 return adapter->priv_flags;
3137}
3138
3139static int ibmvnic_set_priv_flags(struct net_device *netdev, u32 flags)
3140{
3141 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
3142 bool which_maxes = !!(flags & IBMVNIC_USE_SERVER_MAXES);
3143
3144 if (which_maxes)
3145 adapter->priv_flags |= IBMVNIC_USE_SERVER_MAXES;
3146 else
3147 adapter->priv_flags &= ~IBMVNIC_USE_SERVER_MAXES;
3148
3149 return 0;
3150}
Lijun Pan91dc5d22021-02-11 00:43:22 -06003151
Thomas Falcon032c5e82015-12-21 11:26:06 -06003152static const struct ethtool_ops ibmvnic_ethtool_ops = {
Thomas Falcon032c5e82015-12-21 11:26:06 -06003153 .get_drvinfo = ibmvnic_get_drvinfo,
3154 .get_msglevel = ibmvnic_get_msglevel,
3155 .set_msglevel = ibmvnic_set_msglevel,
3156 .get_link = ibmvnic_get_link,
3157 .get_ringparam = ibmvnic_get_ringparam,
John Allenc26eba02017-10-26 16:23:25 -05003158 .set_ringparam = ibmvnic_set_ringparam,
John Allenc2dbeb62017-08-02 16:47:17 -05003159 .get_channels = ibmvnic_get_channels,
John Allenc26eba02017-10-26 16:23:25 -05003160 .set_channels = ibmvnic_set_channels,
Thomas Falcon032c5e82015-12-21 11:26:06 -06003161 .get_strings = ibmvnic_get_strings,
3162 .get_sset_count = ibmvnic_get_sset_count,
3163 .get_ethtool_stats = ibmvnic_get_ethtool_stats,
Philippe Reynes8a433792017-01-07 22:37:29 +01003164 .get_link_ksettings = ibmvnic_get_link_ksettings,
Thomas Falcon723ad912018-09-28 18:38:26 -05003165 .get_priv_flags = ibmvnic_get_priv_flags,
3166 .set_priv_flags = ibmvnic_set_priv_flags,
Thomas Falcon032c5e82015-12-21 11:26:06 -06003167};
3168
3169/* Routines for managing CRQs/sCRQs */
3170
Nathan Fontenot57a49432017-05-26 10:31:12 -04003171static int reset_one_sub_crq_queue(struct ibmvnic_adapter *adapter,
3172 struct ibmvnic_sub_crq_queue *scrq)
3173{
3174 int rc;
3175
Dany Madden9281cf22020-11-25 18:04:26 -06003176 if (!scrq) {
YANG LI862aecb2020-12-30 15:23:14 +08003177 netdev_dbg(adapter->netdev, "Invalid scrq reset.\n");
Dany Madden9281cf22020-11-25 18:04:26 -06003178 return -EINVAL;
3179 }
3180
Nathan Fontenot57a49432017-05-26 10:31:12 -04003181 if (scrq->irq) {
3182 free_irq(scrq->irq, scrq);
3183 irq_dispose_mapping(scrq->irq);
3184 scrq->irq = 0;
3185 }
3186
Dany Madden9281cf22020-11-25 18:04:26 -06003187 if (scrq->msgs) {
3188 memset(scrq->msgs, 0, 4 * PAGE_SIZE);
3189 atomic_set(&scrq->used, 0);
3190 scrq->cur = 0;
Jakub Kicinski55fd59b2020-12-03 15:42:13 -08003191 scrq->ind_buf.index = 0;
Dany Madden9281cf22020-11-25 18:04:26 -06003192 } else {
3193 netdev_dbg(adapter->netdev, "Invalid scrq reset\n");
3194 return -EINVAL;
3195 }
Nathan Fontenot57a49432017-05-26 10:31:12 -04003196
3197 rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
3198 4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
3199 return rc;
3200}
3201
3202static int reset_sub_crq_queues(struct ibmvnic_adapter *adapter)
3203{
3204 int i, rc;
3205
Lijun Pana0faaa22020-11-23 13:35:45 -06003206 if (!adapter->tx_scrq || !adapter->rx_scrq)
3207 return -EINVAL;
3208
Nathan Fontenot57a49432017-05-26 10:31:12 -04003209 for (i = 0; i < adapter->req_tx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05003210 netdev_dbg(adapter->netdev, "Re-setting tx_scrq[%d]\n", i);
Nathan Fontenot57a49432017-05-26 10:31:12 -04003211 rc = reset_one_sub_crq_queue(adapter, adapter->tx_scrq[i]);
3212 if (rc)
3213 return rc;
3214 }
3215
3216 for (i = 0; i < adapter->req_rx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05003217 netdev_dbg(adapter->netdev, "Re-setting rx_scrq[%d]\n", i);
Nathan Fontenot57a49432017-05-26 10:31:12 -04003218 rc = reset_one_sub_crq_queue(adapter, adapter->rx_scrq[i]);
3219 if (rc)
3220 return rc;
3221 }
3222
Nathan Fontenot57a49432017-05-26 10:31:12 -04003223 return rc;
3224}
3225
Thomas Falcon032c5e82015-12-21 11:26:06 -06003226static void release_sub_crq_queue(struct ibmvnic_adapter *adapter,
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06003227 struct ibmvnic_sub_crq_queue *scrq,
3228 bool do_h_free)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003229{
3230 struct device *dev = &adapter->vdev->dev;
3231 long rc;
3232
3233 netdev_dbg(adapter->netdev, "Releasing sub-CRQ\n");
3234
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06003235 if (do_h_free) {
3236 /* Close the sub-crqs */
3237 do {
3238 rc = plpar_hcall_norets(H_FREE_SUB_CRQ,
3239 adapter->vdev->unit_address,
3240 scrq->crq_num);
3241 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
Thomas Falcon032c5e82015-12-21 11:26:06 -06003242
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06003243 if (rc) {
3244 netdev_err(adapter->netdev,
3245 "Failed to release sub-CRQ %16lx, rc = %ld\n",
3246 scrq->crq_num, rc);
3247 }
Thomas Falconffa73852017-04-19 13:44:29 -04003248 }
3249
Thomas Falconf019fb62020-11-18 19:12:17 -06003250 dma_free_coherent(dev,
3251 IBMVNIC_IND_ARR_SZ,
3252 scrq->ind_buf.indir_arr,
3253 scrq->ind_buf.indir_dma);
3254
Thomas Falcon032c5e82015-12-21 11:26:06 -06003255 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
3256 DMA_BIDIRECTIONAL);
3257 free_pages((unsigned long)scrq->msgs, 2);
3258 kfree(scrq);
3259}
3260
3261static struct ibmvnic_sub_crq_queue *init_sub_crq_queue(struct ibmvnic_adapter
3262 *adapter)
3263{
3264 struct device *dev = &adapter->vdev->dev;
3265 struct ibmvnic_sub_crq_queue *scrq;
3266 int rc;
3267
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04003268 scrq = kzalloc(sizeof(*scrq), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003269 if (!scrq)
3270 return NULL;
3271
Nathan Fontenot7f7adc52017-04-19 13:45:16 -04003272 scrq->msgs =
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04003273 (union sub_crq *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, 2);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003274 if (!scrq->msgs) {
3275 dev_warn(dev, "Couldn't allocate crq queue messages page\n");
3276 goto zero_page_failed;
3277 }
3278
3279 scrq->msg_token = dma_map_single(dev, scrq->msgs, 4 * PAGE_SIZE,
3280 DMA_BIDIRECTIONAL);
3281 if (dma_mapping_error(dev, scrq->msg_token)) {
3282 dev_warn(dev, "Couldn't map crq queue messages page\n");
3283 goto map_failed;
3284 }
3285
3286 rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
3287 4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
3288
3289 if (rc == H_RESOURCE)
3290 rc = ibmvnic_reset_crq(adapter);
3291
3292 if (rc == H_CLOSED) {
3293 dev_warn(dev, "Partner adapter not ready, waiting.\n");
3294 } else if (rc) {
3295 dev_warn(dev, "Error %d registering sub-crq\n", rc);
3296 goto reg_failed;
3297 }
3298
Thomas Falcon032c5e82015-12-21 11:26:06 -06003299 scrq->adapter = adapter;
3300 scrq->size = 4 * PAGE_SIZE / sizeof(*scrq->msgs);
Thomas Falconf019fb62020-11-18 19:12:17 -06003301 scrq->ind_buf.index = 0;
3302
3303 scrq->ind_buf.indir_arr =
3304 dma_alloc_coherent(dev,
3305 IBMVNIC_IND_ARR_SZ,
3306 &scrq->ind_buf.indir_dma,
3307 GFP_KERNEL);
3308
3309 if (!scrq->ind_buf.indir_arr)
3310 goto indir_failed;
3311
Thomas Falcon032c5e82015-12-21 11:26:06 -06003312 spin_lock_init(&scrq->lock);
3313
3314 netdev_dbg(adapter->netdev,
3315 "sub-crq initialized, num %lx, hw_irq=%lx, irq=%x\n",
3316 scrq->crq_num, scrq->hw_irq, scrq->irq);
3317
3318 return scrq;
3319
Thomas Falconf019fb62020-11-18 19:12:17 -06003320indir_failed:
3321 do {
3322 rc = plpar_hcall_norets(H_FREE_SUB_CRQ,
3323 adapter->vdev->unit_address,
3324 scrq->crq_num);
3325 } while (rc == H_BUSY || rc == H_IS_LONG_BUSY(rc));
Thomas Falcon032c5e82015-12-21 11:26:06 -06003326reg_failed:
3327 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
3328 DMA_BIDIRECTIONAL);
3329map_failed:
3330 free_pages((unsigned long)scrq->msgs, 2);
3331zero_page_failed:
3332 kfree(scrq);
3333
3334 return NULL;
3335}
3336
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06003337static void release_sub_crqs(struct ibmvnic_adapter *adapter, bool do_h_free)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003338{
3339 int i;
3340
3341 if (adapter->tx_scrq) {
Nathan Fontenot82e3be32018-02-21 21:33:56 -06003342 for (i = 0; i < adapter->num_active_tx_scrqs; i++) {
Nathan Fontenotb5108882017-03-30 02:49:18 -04003343 if (!adapter->tx_scrq[i])
3344 continue;
3345
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05003346 netdev_dbg(adapter->netdev, "Releasing tx_scrq[%d]\n",
3347 i);
Sukadev Bhattiprolu65d64702021-06-23 21:13:12 -07003348 ibmvnic_tx_scrq_clean_buffer(adapter, adapter->tx_scrq[i]);
Nathan Fontenotb5108882017-03-30 02:49:18 -04003349 if (adapter->tx_scrq[i]->irq) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06003350 free_irq(adapter->tx_scrq[i]->irq,
3351 adapter->tx_scrq[i]);
Thomas Falcon88eb98a2016-07-06 15:35:16 -05003352 irq_dispose_mapping(adapter->tx_scrq[i]->irq);
Nathan Fontenotb5108882017-03-30 02:49:18 -04003353 adapter->tx_scrq[i]->irq = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003354 }
Nathan Fontenotb5108882017-03-30 02:49:18 -04003355
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06003356 release_sub_crq_queue(adapter, adapter->tx_scrq[i],
3357 do_h_free);
Nathan Fontenotb5108882017-03-30 02:49:18 -04003358 }
3359
Nathan Fontenot9501df32017-03-15 23:38:07 -04003360 kfree(adapter->tx_scrq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003361 adapter->tx_scrq = NULL;
Nathan Fontenot82e3be32018-02-21 21:33:56 -06003362 adapter->num_active_tx_scrqs = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003363 }
3364
3365 if (adapter->rx_scrq) {
Nathan Fontenot82e3be32018-02-21 21:33:56 -06003366 for (i = 0; i < adapter->num_active_rx_scrqs; i++) {
Nathan Fontenotb5108882017-03-30 02:49:18 -04003367 if (!adapter->rx_scrq[i])
3368 continue;
3369
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05003370 netdev_dbg(adapter->netdev, "Releasing rx_scrq[%d]\n",
3371 i);
Nathan Fontenotb5108882017-03-30 02:49:18 -04003372 if (adapter->rx_scrq[i]->irq) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06003373 free_irq(adapter->rx_scrq[i]->irq,
3374 adapter->rx_scrq[i]);
Thomas Falcon88eb98a2016-07-06 15:35:16 -05003375 irq_dispose_mapping(adapter->rx_scrq[i]->irq);
Nathan Fontenotb5108882017-03-30 02:49:18 -04003376 adapter->rx_scrq[i]->irq = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003377 }
Nathan Fontenotb5108882017-03-30 02:49:18 -04003378
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06003379 release_sub_crq_queue(adapter, adapter->rx_scrq[i],
3380 do_h_free);
Nathan Fontenotb5108882017-03-30 02:49:18 -04003381 }
3382
Nathan Fontenot9501df32017-03-15 23:38:07 -04003383 kfree(adapter->rx_scrq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003384 adapter->rx_scrq = NULL;
Nathan Fontenot82e3be32018-02-21 21:33:56 -06003385 adapter->num_active_rx_scrqs = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003386 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06003387}
3388
3389static int disable_scrq_irq(struct ibmvnic_adapter *adapter,
3390 struct ibmvnic_sub_crq_queue *scrq)
3391{
3392 struct device *dev = &adapter->vdev->dev;
3393 unsigned long rc;
3394
3395 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
3396 H_DISABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
3397 if (rc)
3398 dev_err(dev, "Couldn't disable scrq irq 0x%lx. rc=%ld\n",
3399 scrq->hw_irq, rc);
3400 return rc;
3401}
3402
3403static int enable_scrq_irq(struct ibmvnic_adapter *adapter,
3404 struct ibmvnic_sub_crq_queue *scrq)
3405{
3406 struct device *dev = &adapter->vdev->dev;
3407 unsigned long rc;
3408
3409 if (scrq->hw_irq > 0x100000000ULL) {
3410 dev_err(dev, "bad hw_irq = %lx\n", scrq->hw_irq);
3411 return 1;
3412 }
3413
Juliet Kim7ed5b312019-09-20 16:11:23 -04003414 if (test_bit(0, &adapter->resetting) &&
Nathan Fontenot73f9d362018-05-22 11:21:10 -05003415 adapter->reset_reason == VNIC_RESET_MOBILITY) {
Juliet Kim284f87d2019-11-20 10:50:03 -05003416 u64 val = (0xff000000) | scrq->hw_irq;
Nathan Fontenot73f9d362018-05-22 11:21:10 -05003417
Juliet Kim284f87d2019-11-20 10:50:03 -05003418 rc = plpar_hcall_norets(H_EOI, val);
Juliet Kim2df5c602019-11-20 10:50:04 -05003419 /* H_EOI would fail with rc = H_FUNCTION when running
3420 * in XIVE mode which is expected, but not an error.
3421 */
Sukadev Bhattiprolu154b3b22021-06-23 21:13:16 -07003422 if (rc && (rc != H_FUNCTION))
Juliet Kim284f87d2019-11-20 10:50:03 -05003423 dev_err(dev, "H_EOI FAILED irq 0x%llx. rc=%ld\n",
3424 val, rc);
Nathan Fontenot73f9d362018-05-22 11:21:10 -05003425 }
Thomas Falconf23e0642018-04-15 18:53:36 -05003426
Thomas Falcon032c5e82015-12-21 11:26:06 -06003427 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
3428 H_ENABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
3429 if (rc)
3430 dev_err(dev, "Couldn't enable scrq irq 0x%lx. rc=%ld\n",
3431 scrq->hw_irq, rc);
3432 return rc;
3433}
3434
3435static int ibmvnic_complete_tx(struct ibmvnic_adapter *adapter,
3436 struct ibmvnic_sub_crq_queue *scrq)
3437{
3438 struct device *dev = &adapter->vdev->dev;
Thomas Falcon06b3e352018-03-16 20:00:28 -05003439 struct ibmvnic_tx_pool *tx_pool;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003440 struct ibmvnic_tx_buff *txbuff;
Thomas Falcon0d973382020-11-18 19:12:19 -06003441 struct netdev_queue *txq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003442 union sub_crq *next;
3443 int index;
Thomas Falconc62aa372020-11-18 19:12:20 -06003444 int i;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003445
3446restart_loop:
3447 while (pending_scrq(adapter, scrq)) {
3448 unsigned int pool = scrq->pool_index;
Thomas Falconffc385b2018-02-18 10:08:41 -06003449 int num_entries = 0;
Thomas Falcon0d973382020-11-18 19:12:19 -06003450 int total_bytes = 0;
3451 int num_packets = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003452
3453 next = ibmvnic_next_scrq(adapter, scrq);
3454 for (i = 0; i < next->tx_comp.num_comps; i++) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06003455 index = be32_to_cpu(next->tx_comp.correlators[i]);
Thomas Falcon06b3e352018-03-16 20:00:28 -05003456 if (index & IBMVNIC_TSO_POOL_MASK) {
3457 tx_pool = &adapter->tso_pool[pool];
3458 index &= ~IBMVNIC_TSO_POOL_MASK;
3459 } else {
3460 tx_pool = &adapter->tx_pool[pool];
3461 }
3462
3463 txbuff = &tx_pool->tx_buff[index];
Thomas Falcon0d973382020-11-18 19:12:19 -06003464 num_packets++;
Thomas Falconffc385b2018-02-18 10:08:41 -06003465 num_entries += txbuff->num_entries;
Thomas Falcon0d973382020-11-18 19:12:19 -06003466 if (txbuff->skb) {
3467 total_bytes += txbuff->skb->len;
Lijun Panca09bf72021-04-13 03:33:25 -05003468 if (next->tx_comp.rcs[i]) {
3469 dev_err(dev, "tx error %x\n",
3470 next->tx_comp.rcs[i]);
3471 dev_kfree_skb_irq(txbuff->skb);
3472 } else {
3473 dev_consume_skb_irq(txbuff->skb);
3474 }
Thomas Falcon0d973382020-11-18 19:12:19 -06003475 txbuff->skb = NULL;
3476 } else {
3477 netdev_warn(adapter->netdev,
3478 "TX completion received with NULL socket buffer\n");
3479 }
Thomas Falcon06b3e352018-03-16 20:00:28 -05003480 tx_pool->free_map[tx_pool->producer_index] = index;
3481 tx_pool->producer_index =
3482 (tx_pool->producer_index + 1) %
3483 tx_pool->num_buffers;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003484 }
3485 /* remove tx_comp scrq*/
3486 next->tx_comp.first = 0;
Nathan Fontenot7c3e7de2017-05-03 14:05:25 -04003487
Thomas Falcon0d973382020-11-18 19:12:19 -06003488 txq = netdev_get_tx_queue(adapter->netdev, scrq->pool_index);
3489 netdev_tx_completed_queue(txq, num_packets, total_bytes);
3490
Thomas Falconffc385b2018-02-18 10:08:41 -06003491 if (atomic_sub_return(num_entries, &scrq->used) <=
Nathan Fontenot7c3e7de2017-05-03 14:05:25 -04003492 (adapter->req_tx_entries_per_subcrq / 2) &&
3493 __netif_subqueue_stopped(adapter->netdev,
3494 scrq->pool_index)) {
3495 netif_wake_subqueue(adapter->netdev, scrq->pool_index);
Thomas Falcon0aecb132018-02-26 18:10:58 -06003496 netdev_dbg(adapter->netdev, "Started queue %d\n",
3497 scrq->pool_index);
Nathan Fontenot7c3e7de2017-05-03 14:05:25 -04003498 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06003499 }
3500
3501 enable_scrq_irq(adapter, scrq);
3502
3503 if (pending_scrq(adapter, scrq)) {
3504 disable_scrq_irq(adapter, scrq);
3505 goto restart_loop;
3506 }
3507
3508 return 0;
3509}
3510
3511static irqreturn_t ibmvnic_interrupt_tx(int irq, void *instance)
3512{
3513 struct ibmvnic_sub_crq_queue *scrq = instance;
3514 struct ibmvnic_adapter *adapter = scrq->adapter;
3515
3516 disable_scrq_irq(adapter, scrq);
3517 ibmvnic_complete_tx(adapter, scrq);
3518
3519 return IRQ_HANDLED;
3520}
3521
3522static irqreturn_t ibmvnic_interrupt_rx(int irq, void *instance)
3523{
3524 struct ibmvnic_sub_crq_queue *scrq = instance;
3525 struct ibmvnic_adapter *adapter = scrq->adapter;
3526
Nathan Fontenot09fb35e2018-01-10 10:40:09 -06003527 /* When booting a kdump kernel we can hit pending interrupts
3528 * prior to completing driver initialization.
3529 */
3530 if (unlikely(adapter->state != VNIC_OPEN))
3531 return IRQ_NONE;
3532
John Allen3d52b592017-08-02 16:44:14 -05003533 adapter->rx_stats_buffers[scrq->scrq_num].interrupts++;
3534
Thomas Falcon032c5e82015-12-21 11:26:06 -06003535 if (napi_schedule_prep(&adapter->napi[scrq->scrq_num])) {
3536 disable_scrq_irq(adapter, scrq);
3537 __napi_schedule(&adapter->napi[scrq->scrq_num]);
3538 }
3539
3540 return IRQ_HANDLED;
3541}
3542
Thomas Falconea22d512016-07-06 15:35:17 -05003543static int init_sub_crq_irqs(struct ibmvnic_adapter *adapter)
3544{
3545 struct device *dev = &adapter->vdev->dev;
3546 struct ibmvnic_sub_crq_queue *scrq;
3547 int i = 0, j = 0;
3548 int rc = 0;
3549
3550 for (i = 0; i < adapter->req_tx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05003551 netdev_dbg(adapter->netdev, "Initializing tx_scrq[%d] irq\n",
3552 i);
Thomas Falconea22d512016-07-06 15:35:17 -05003553 scrq = adapter->tx_scrq[i];
3554 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
3555
Michael Ellerman99c17902016-09-10 19:59:05 +10003556 if (!scrq->irq) {
Thomas Falconea22d512016-07-06 15:35:17 -05003557 rc = -EINVAL;
3558 dev_err(dev, "Error mapping irq\n");
3559 goto req_tx_irq_failed;
3560 }
3561
Murilo Fossa Vicentinie56e2512019-04-25 11:02:33 -03003562 snprintf(scrq->name, sizeof(scrq->name), "ibmvnic-%x-tx%d",
3563 adapter->vdev->unit_address, i);
Thomas Falconea22d512016-07-06 15:35:17 -05003564 rc = request_irq(scrq->irq, ibmvnic_interrupt_tx,
Murilo Fossa Vicentinie56e2512019-04-25 11:02:33 -03003565 0, scrq->name, scrq);
Thomas Falconea22d512016-07-06 15:35:17 -05003566
3567 if (rc) {
3568 dev_err(dev, "Couldn't register tx irq 0x%x. rc=%d\n",
3569 scrq->irq, rc);
3570 irq_dispose_mapping(scrq->irq);
Nathan Fontenotaf9090c2018-02-20 11:04:18 -06003571 goto req_tx_irq_failed;
Thomas Falconea22d512016-07-06 15:35:17 -05003572 }
3573 }
3574
3575 for (i = 0; i < adapter->req_rx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05003576 netdev_dbg(adapter->netdev, "Initializing rx_scrq[%d] irq\n",
3577 i);
Thomas Falconea22d512016-07-06 15:35:17 -05003578 scrq = adapter->rx_scrq[i];
3579 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
Michael Ellerman99c17902016-09-10 19:59:05 +10003580 if (!scrq->irq) {
Thomas Falconea22d512016-07-06 15:35:17 -05003581 rc = -EINVAL;
3582 dev_err(dev, "Error mapping irq\n");
3583 goto req_rx_irq_failed;
3584 }
Murilo Fossa Vicentinie56e2512019-04-25 11:02:33 -03003585 snprintf(scrq->name, sizeof(scrq->name), "ibmvnic-%x-rx%d",
3586 adapter->vdev->unit_address, i);
Thomas Falconea22d512016-07-06 15:35:17 -05003587 rc = request_irq(scrq->irq, ibmvnic_interrupt_rx,
Murilo Fossa Vicentinie56e2512019-04-25 11:02:33 -03003588 0, scrq->name, scrq);
Thomas Falconea22d512016-07-06 15:35:17 -05003589 if (rc) {
3590 dev_err(dev, "Couldn't register rx irq 0x%x. rc=%d\n",
3591 scrq->irq, rc);
3592 irq_dispose_mapping(scrq->irq);
3593 goto req_rx_irq_failed;
3594 }
3595 }
3596 return rc;
3597
3598req_rx_irq_failed:
Thomas Falcon8bf371e2016-10-27 12:28:52 -05003599 for (j = 0; j < i; j++) {
Thomas Falconea22d512016-07-06 15:35:17 -05003600 free_irq(adapter->rx_scrq[j]->irq, adapter->rx_scrq[j]);
3601 irq_dispose_mapping(adapter->rx_scrq[j]->irq);
Thomas Falcon8bf371e2016-10-27 12:28:52 -05003602 }
Thomas Falconea22d512016-07-06 15:35:17 -05003603 i = adapter->req_tx_queues;
3604req_tx_irq_failed:
Thomas Falcon8bf371e2016-10-27 12:28:52 -05003605 for (j = 0; j < i; j++) {
Thomas Falconea22d512016-07-06 15:35:17 -05003606 free_irq(adapter->tx_scrq[j]->irq, adapter->tx_scrq[j]);
Thomas Falcon27a21452020-07-29 16:36:32 -05003607 irq_dispose_mapping(adapter->tx_scrq[j]->irq);
Thomas Falcon8bf371e2016-10-27 12:28:52 -05003608 }
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06003609 release_sub_crqs(adapter, 1);
Thomas Falconea22d512016-07-06 15:35:17 -05003610 return rc;
3611}
3612
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003613static int init_sub_crqs(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003614{
3615 struct device *dev = &adapter->vdev->dev;
3616 struct ibmvnic_sub_crq_queue **allqueues;
3617 int registered_queues = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003618 int total_queues;
3619 int more = 0;
Thomas Falconea22d512016-07-06 15:35:17 -05003620 int i;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003621
Thomas Falcon032c5e82015-12-21 11:26:06 -06003622 total_queues = adapter->req_tx_queues + adapter->req_rx_queues;
3623
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04003624 allqueues = kcalloc(total_queues, sizeof(*allqueues), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003625 if (!allqueues)
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003626 return -1;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003627
3628 for (i = 0; i < total_queues; i++) {
3629 allqueues[i] = init_sub_crq_queue(adapter);
3630 if (!allqueues[i]) {
3631 dev_warn(dev, "Couldn't allocate all sub-crqs\n");
3632 break;
3633 }
3634 registered_queues++;
3635 }
3636
3637 /* Make sure we were able to register the minimum number of queues */
3638 if (registered_queues <
3639 adapter->min_tx_queues + adapter->min_rx_queues) {
3640 dev_err(dev, "Fatal: Couldn't init min number of sub-crqs\n");
3641 goto tx_failed;
3642 }
3643
3644 /* Distribute the failed allocated queues*/
3645 for (i = 0; i < total_queues - registered_queues + more ; i++) {
3646 netdev_dbg(adapter->netdev, "Reducing number of queues\n");
3647 switch (i % 3) {
3648 case 0:
3649 if (adapter->req_rx_queues > adapter->min_rx_queues)
3650 adapter->req_rx_queues--;
3651 else
3652 more++;
3653 break;
3654 case 1:
3655 if (adapter->req_tx_queues > adapter->min_tx_queues)
3656 adapter->req_tx_queues--;
3657 else
3658 more++;
3659 break;
3660 }
3661 }
3662
3663 adapter->tx_scrq = kcalloc(adapter->req_tx_queues,
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04003664 sizeof(*adapter->tx_scrq), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003665 if (!adapter->tx_scrq)
3666 goto tx_failed;
3667
3668 for (i = 0; i < adapter->req_tx_queues; i++) {
3669 adapter->tx_scrq[i] = allqueues[i];
3670 adapter->tx_scrq[i]->pool_index = i;
Nathan Fontenot82e3be32018-02-21 21:33:56 -06003671 adapter->num_active_tx_scrqs++;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003672 }
3673
3674 adapter->rx_scrq = kcalloc(adapter->req_rx_queues,
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04003675 sizeof(*adapter->rx_scrq), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003676 if (!adapter->rx_scrq)
3677 goto rx_failed;
3678
3679 for (i = 0; i < adapter->req_rx_queues; i++) {
3680 adapter->rx_scrq[i] = allqueues[i + adapter->req_tx_queues];
3681 adapter->rx_scrq[i]->scrq_num = i;
Nathan Fontenot82e3be32018-02-21 21:33:56 -06003682 adapter->num_active_rx_scrqs++;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003683 }
3684
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003685 kfree(allqueues);
3686 return 0;
3687
3688rx_failed:
3689 kfree(adapter->tx_scrq);
3690 adapter->tx_scrq = NULL;
3691tx_failed:
3692 for (i = 0; i < registered_queues; i++)
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06003693 release_sub_crq_queue(adapter, allqueues[i], 1);
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003694 kfree(allqueues);
3695 return -1;
3696}
3697
Lijun Pan09081b92020-09-27 20:13:27 -05003698static void send_request_cap(struct ibmvnic_adapter *adapter, int retry)
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003699{
3700 struct device *dev = &adapter->vdev->dev;
3701 union ibmvnic_crq crq;
John Allenc26eba02017-10-26 16:23:25 -05003702 int max_entries;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003703
3704 if (!retry) {
3705 /* Sub-CRQ entries are 32 byte long */
3706 int entries_page = 4 * PAGE_SIZE / (sizeof(u64) * 4);
3707
3708 if (adapter->min_tx_entries_per_subcrq > entries_page ||
3709 adapter->min_rx_add_entries_per_subcrq > entries_page) {
3710 dev_err(dev, "Fatal, invalid entries per sub-crq\n");
3711 return;
3712 }
3713
John Allenc26eba02017-10-26 16:23:25 -05003714 if (adapter->desired.mtu)
3715 adapter->req_mtu = adapter->desired.mtu;
3716 else
3717 adapter->req_mtu = adapter->netdev->mtu + ETH_HLEN;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003718
John Allenc26eba02017-10-26 16:23:25 -05003719 if (!adapter->desired.tx_entries)
3720 adapter->desired.tx_entries =
3721 adapter->max_tx_entries_per_subcrq;
3722 if (!adapter->desired.rx_entries)
3723 adapter->desired.rx_entries =
3724 adapter->max_rx_add_entries_per_subcrq;
3725
3726 max_entries = IBMVNIC_MAX_LTB_SIZE /
3727 (adapter->req_mtu + IBMVNIC_BUFFER_HLEN);
3728
3729 if ((adapter->req_mtu + IBMVNIC_BUFFER_HLEN) *
3730 adapter->desired.tx_entries > IBMVNIC_MAX_LTB_SIZE) {
3731 adapter->desired.tx_entries = max_entries;
3732 }
3733
3734 if ((adapter->req_mtu + IBMVNIC_BUFFER_HLEN) *
3735 adapter->desired.rx_entries > IBMVNIC_MAX_LTB_SIZE) {
3736 adapter->desired.rx_entries = max_entries;
3737 }
3738
3739 if (adapter->desired.tx_entries)
3740 adapter->req_tx_entries_per_subcrq =
3741 adapter->desired.tx_entries;
3742 else
3743 adapter->req_tx_entries_per_subcrq =
3744 adapter->max_tx_entries_per_subcrq;
3745
3746 if (adapter->desired.rx_entries)
3747 adapter->req_rx_add_entries_per_subcrq =
3748 adapter->desired.rx_entries;
3749 else
3750 adapter->req_rx_add_entries_per_subcrq =
3751 adapter->max_rx_add_entries_per_subcrq;
3752
3753 if (adapter->desired.tx_queues)
3754 adapter->req_tx_queues =
3755 adapter->desired.tx_queues;
3756 else
3757 adapter->req_tx_queues =
3758 adapter->opt_tx_comp_sub_queues;
3759
3760 if (adapter->desired.rx_queues)
3761 adapter->req_rx_queues =
3762 adapter->desired.rx_queues;
3763 else
3764 adapter->req_rx_queues =
3765 adapter->opt_rx_comp_queues;
3766
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003767 adapter->req_rx_add_queues = adapter->max_rx_add_queues;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003768 }
3769
Thomas Falcon032c5e82015-12-21 11:26:06 -06003770 memset(&crq, 0, sizeof(crq));
3771 crq.request_capability.first = IBMVNIC_CRQ_CMD;
3772 crq.request_capability.cmd = REQUEST_CAPABILITY;
3773
3774 crq.request_capability.capability = cpu_to_be16(REQ_TX_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06003775 crq.request_capability.number = cpu_to_be64(adapter->req_tx_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06003776 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003777 ibmvnic_send_crq(adapter, &crq);
3778
3779 crq.request_capability.capability = cpu_to_be16(REQ_RX_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06003780 crq.request_capability.number = cpu_to_be64(adapter->req_rx_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06003781 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003782 ibmvnic_send_crq(adapter, &crq);
3783
3784 crq.request_capability.capability = cpu_to_be16(REQ_RX_ADD_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06003785 crq.request_capability.number = cpu_to_be64(adapter->req_rx_add_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06003786 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003787 ibmvnic_send_crq(adapter, &crq);
3788
3789 crq.request_capability.capability =
3790 cpu_to_be16(REQ_TX_ENTRIES_PER_SUBCRQ);
3791 crq.request_capability.number =
Thomas Falconde89e852016-03-01 10:20:09 -06003792 cpu_to_be64(adapter->req_tx_entries_per_subcrq);
Thomas Falcon901e0402017-02-15 12:17:59 -06003793 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003794 ibmvnic_send_crq(adapter, &crq);
3795
3796 crq.request_capability.capability =
3797 cpu_to_be16(REQ_RX_ADD_ENTRIES_PER_SUBCRQ);
3798 crq.request_capability.number =
Thomas Falconde89e852016-03-01 10:20:09 -06003799 cpu_to_be64(adapter->req_rx_add_entries_per_subcrq);
Thomas Falcon901e0402017-02-15 12:17:59 -06003800 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003801 ibmvnic_send_crq(adapter, &crq);
3802
3803 crq.request_capability.capability = cpu_to_be16(REQ_MTU);
Thomas Falconde89e852016-03-01 10:20:09 -06003804 crq.request_capability.number = cpu_to_be64(adapter->req_mtu);
Thomas Falcon901e0402017-02-15 12:17:59 -06003805 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003806 ibmvnic_send_crq(adapter, &crq);
3807
3808 if (adapter->netdev->flags & IFF_PROMISC) {
3809 if (adapter->promisc_supported) {
3810 crq.request_capability.capability =
3811 cpu_to_be16(PROMISC_REQUESTED);
Thomas Falconde89e852016-03-01 10:20:09 -06003812 crq.request_capability.number = cpu_to_be64(1);
Thomas Falcon901e0402017-02-15 12:17:59 -06003813 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003814 ibmvnic_send_crq(adapter, &crq);
3815 }
3816 } else {
3817 crq.request_capability.capability =
3818 cpu_to_be16(PROMISC_REQUESTED);
Thomas Falconde89e852016-03-01 10:20:09 -06003819 crq.request_capability.number = cpu_to_be64(0);
Thomas Falcon901e0402017-02-15 12:17:59 -06003820 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003821 ibmvnic_send_crq(adapter, &crq);
3822 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06003823}
3824
3825static int pending_scrq(struct ibmvnic_adapter *adapter,
3826 struct ibmvnic_sub_crq_queue *scrq)
3827{
3828 union sub_crq *entry = &scrq->msgs[scrq->cur];
Lijun Pan665ab1e2021-01-29 19:19:04 -06003829 int rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003830
Lijun Pan665ab1e2021-01-29 19:19:04 -06003831 rc = !!(entry->generic.first & IBMVNIC_CRQ_CMD_RSP);
3832
3833 /* Ensure that the SCRQ valid flag is loaded prior to loading the
3834 * contents of the SCRQ descriptor
3835 */
3836 dma_rmb();
3837
3838 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003839}
3840
3841static union sub_crq *ibmvnic_next_scrq(struct ibmvnic_adapter *adapter,
3842 struct ibmvnic_sub_crq_queue *scrq)
3843{
3844 union sub_crq *entry;
3845 unsigned long flags;
3846
3847 spin_lock_irqsave(&scrq->lock, flags);
3848 entry = &scrq->msgs[scrq->cur];
3849 if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP) {
3850 if (++scrq->cur == scrq->size)
3851 scrq->cur = 0;
3852 } else {
3853 entry = NULL;
3854 }
3855 spin_unlock_irqrestore(&scrq->lock, flags);
3856
Lijun Pan665ab1e2021-01-29 19:19:04 -06003857 /* Ensure that the SCRQ valid flag is loaded prior to loading the
3858 * contents of the SCRQ descriptor
Thomas Falconb71ec952020-12-01 09:52:10 -06003859 */
3860 dma_rmb();
3861
Thomas Falcon032c5e82015-12-21 11:26:06 -06003862 return entry;
3863}
3864
3865static union ibmvnic_crq *ibmvnic_next_crq(struct ibmvnic_adapter *adapter)
3866{
3867 struct ibmvnic_crq_queue *queue = &adapter->crq;
3868 union ibmvnic_crq *crq;
3869
3870 crq = &queue->msgs[queue->cur];
3871 if (crq->generic.first & IBMVNIC_CRQ_CMD_RSP) {
3872 if (++queue->cur == queue->size)
3873 queue->cur = 0;
3874 } else {
3875 crq = NULL;
3876 }
3877
3878 return crq;
3879}
3880
Thomas Falcon2d14d372018-07-13 12:03:32 -05003881static void print_subcrq_error(struct device *dev, int rc, const char *func)
3882{
3883 switch (rc) {
3884 case H_PARAMETER:
3885 dev_warn_ratelimited(dev,
3886 "%s failed: Send request is malformed or adapter failover pending. (rc=%d)\n",
3887 func, rc);
3888 break;
3889 case H_CLOSED:
3890 dev_warn_ratelimited(dev,
3891 "%s failed: Backing queue closed. Adapter is down or failover pending. (rc=%d)\n",
3892 func, rc);
3893 break;
3894 default:
3895 dev_err_ratelimited(dev, "%s failed: (rc=%d)\n", func, rc);
3896 break;
3897 }
3898}
3899
Thomas Falconad7775d2016-04-01 17:20:34 -05003900static int send_subcrq_indirect(struct ibmvnic_adapter *adapter,
3901 u64 remote_handle, u64 ioba, u64 num_entries)
3902{
3903 unsigned int ua = adapter->vdev->unit_address;
3904 struct device *dev = &adapter->vdev->dev;
3905 int rc;
3906
3907 /* Make sure the hypervisor sees the complete request */
Lijun Pan1a421562021-02-12 20:36:46 -06003908 dma_wmb();
Thomas Falconad7775d2016-04-01 17:20:34 -05003909 rc = plpar_hcall_norets(H_SEND_SUB_CRQ_INDIRECT, ua,
3910 cpu_to_be64(remote_handle),
3911 ioba, num_entries);
3912
Thomas Falcon2d14d372018-07-13 12:03:32 -05003913 if (rc)
3914 print_subcrq_error(dev, rc, __func__);
Thomas Falconad7775d2016-04-01 17:20:34 -05003915
3916 return rc;
3917}
3918
Thomas Falcon032c5e82015-12-21 11:26:06 -06003919static int ibmvnic_send_crq(struct ibmvnic_adapter *adapter,
3920 union ibmvnic_crq *crq)
3921{
3922 unsigned int ua = adapter->vdev->unit_address;
3923 struct device *dev = &adapter->vdev->dev;
3924 u64 *u64_crq = (u64 *)crq;
3925 int rc;
3926
3927 netdev_dbg(adapter->netdev, "Sending CRQ: %016lx %016lx\n",
Lijun Pan429aa362021-02-11 00:43:18 -06003928 (unsigned long)cpu_to_be64(u64_crq[0]),
3929 (unsigned long)cpu_to_be64(u64_crq[1]));
Thomas Falcon032c5e82015-12-21 11:26:06 -06003930
Thomas Falcon51536982018-05-23 13:37:56 -05003931 if (!adapter->crq.active &&
3932 crq->generic.first != IBMVNIC_CRQ_INIT_CMD) {
3933 dev_warn(dev, "Invalid request detected while CRQ is inactive, possible device state change during reset\n");
3934 return -EINVAL;
3935 }
3936
Thomas Falcon032c5e82015-12-21 11:26:06 -06003937 /* Make sure the hypervisor sees the complete request */
Lijun Pan1a421562021-02-12 20:36:46 -06003938 dma_wmb();
Thomas Falcon032c5e82015-12-21 11:26:06 -06003939
3940 rc = plpar_hcall_norets(H_SEND_CRQ, ua,
3941 cpu_to_be64(u64_crq[0]),
3942 cpu_to_be64(u64_crq[1]));
3943
3944 if (rc) {
Nathan Fontenotec95dff2018-02-07 13:00:24 -06003945 if (rc == H_CLOSED) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06003946 dev_warn(dev, "CRQ Queue closed\n");
Lijun Panfa68bfa2020-08-19 17:52:24 -05003947 /* do not reset, report the fail, wait for passive init from server */
Nathan Fontenotec95dff2018-02-07 13:00:24 -06003948 }
3949
Thomas Falcon032c5e82015-12-21 11:26:06 -06003950 dev_warn(dev, "Send error (rc=%d)\n", rc);
3951 }
3952
3953 return rc;
3954}
3955
3956static int ibmvnic_send_crq_init(struct ibmvnic_adapter *adapter)
3957{
Thomas Falcon36a782f2020-08-31 11:59:57 -05003958 struct device *dev = &adapter->vdev->dev;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003959 union ibmvnic_crq crq;
Thomas Falcon36a782f2020-08-31 11:59:57 -05003960 int retries = 100;
3961 int rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003962
3963 memset(&crq, 0, sizeof(crq));
3964 crq.generic.first = IBMVNIC_CRQ_INIT_CMD;
3965 crq.generic.cmd = IBMVNIC_CRQ_INIT;
3966 netdev_dbg(adapter->netdev, "Sending CRQ init\n");
3967
Thomas Falcon36a782f2020-08-31 11:59:57 -05003968 do {
3969 rc = ibmvnic_send_crq(adapter, &crq);
3970 if (rc != H_CLOSED)
3971 break;
3972 retries--;
3973 msleep(50);
3974
3975 } while (retries > 0);
3976
3977 if (rc) {
3978 dev_err(dev, "Failed to send init request, rc = %d\n", rc);
3979 return rc;
3980 }
3981
3982 return 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003983}
3984
Nathan Fontenot37798d02017-11-08 11:23:56 -06003985struct vnic_login_client_data {
3986 u8 type;
3987 __be16 len;
Kees Cook08ea5562018-04-10 15:26:43 -07003988 char name[];
Nathan Fontenot37798d02017-11-08 11:23:56 -06003989} __packed;
3990
3991static int vnic_client_data_len(struct ibmvnic_adapter *adapter)
3992{
3993 int len;
3994
3995 /* Calculate the amount of buffer space needed for the
3996 * vnic client data in the login buffer. There are four entries,
3997 * OS name, LPAR name, device name, and a null last entry.
3998 */
3999 len = 4 * sizeof(struct vnic_login_client_data);
4000 len += 6; /* "Linux" plus NULL */
4001 len += strlen(utsname()->nodename) + 1;
4002 len += strlen(adapter->netdev->name) + 1;
4003
4004 return len;
4005}
4006
4007static void vnic_add_client_data(struct ibmvnic_adapter *adapter,
4008 struct vnic_login_client_data *vlcd)
4009{
4010 const char *os_name = "Linux";
4011 int len;
4012
4013 /* Type 1 - LPAR OS */
4014 vlcd->type = 1;
4015 len = strlen(os_name) + 1;
4016 vlcd->len = cpu_to_be16(len);
Kees Cookef2c3dd2021-06-21 14:35:09 -07004017 strscpy(vlcd->name, os_name, len);
Kees Cook08ea5562018-04-10 15:26:43 -07004018 vlcd = (struct vnic_login_client_data *)(vlcd->name + len);
Nathan Fontenot37798d02017-11-08 11:23:56 -06004019
4020 /* Type 2 - LPAR name */
4021 vlcd->type = 2;
4022 len = strlen(utsname()->nodename) + 1;
4023 vlcd->len = cpu_to_be16(len);
Kees Cookef2c3dd2021-06-21 14:35:09 -07004024 strscpy(vlcd->name, utsname()->nodename, len);
Kees Cook08ea5562018-04-10 15:26:43 -07004025 vlcd = (struct vnic_login_client_data *)(vlcd->name + len);
Nathan Fontenot37798d02017-11-08 11:23:56 -06004026
4027 /* Type 3 - device name */
4028 vlcd->type = 3;
4029 len = strlen(adapter->netdev->name) + 1;
4030 vlcd->len = cpu_to_be16(len);
Kees Cookef2c3dd2021-06-21 14:35:09 -07004031 strscpy(vlcd->name, adapter->netdev->name, len);
Nathan Fontenot37798d02017-11-08 11:23:56 -06004032}
4033
Thomas Falcon20a8ab72018-02-26 18:10:59 -06004034static int send_login(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06004035{
4036 struct ibmvnic_login_rsp_buffer *login_rsp_buffer;
4037 struct ibmvnic_login_buffer *login_buffer;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004038 struct device *dev = &adapter->vdev->dev;
Dany Maddenc98d9cc2020-11-25 18:04:30 -06004039 struct vnic_login_client_data *vlcd;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004040 dma_addr_t rsp_buffer_token;
4041 dma_addr_t buffer_token;
4042 size_t rsp_buffer_size;
4043 union ibmvnic_crq crq;
Dany Maddenc98d9cc2020-11-25 18:04:30 -06004044 int client_data_len;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004045 size_t buffer_size;
4046 __be64 *tx_list_p;
4047 __be64 *rx_list_p;
Dany Maddenc98d9cc2020-11-25 18:04:30 -06004048 int rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004049 int i;
4050
Thomas Falcon20a8ab72018-02-26 18:10:59 -06004051 if (!adapter->tx_scrq || !adapter->rx_scrq) {
4052 netdev_err(adapter->netdev,
4053 "RX or TX queues are not allocated, device login failed\n");
4054 return -1;
4055 }
4056
Lijun Pana0c8be52020-12-19 15:39:19 -06004057 release_login_buffer(adapter);
Thomas Falcon34f0f4e2018-02-13 18:23:40 -06004058 release_login_rsp_buffer(adapter);
Lijun Pana0c8be52020-12-19 15:39:19 -06004059
Nathan Fontenot37798d02017-11-08 11:23:56 -06004060 client_data_len = vnic_client_data_len(adapter);
4061
Thomas Falcon032c5e82015-12-21 11:26:06 -06004062 buffer_size =
4063 sizeof(struct ibmvnic_login_buffer) +
Nathan Fontenot37798d02017-11-08 11:23:56 -06004064 sizeof(u64) * (adapter->req_tx_queues + adapter->req_rx_queues) +
4065 client_data_len;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004066
Nathan Fontenot37798d02017-11-08 11:23:56 -06004067 login_buffer = kzalloc(buffer_size, GFP_ATOMIC);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004068 if (!login_buffer)
4069 goto buf_alloc_failed;
4070
4071 buffer_token = dma_map_single(dev, login_buffer, buffer_size,
4072 DMA_TO_DEVICE);
4073 if (dma_mapping_error(dev, buffer_token)) {
4074 dev_err(dev, "Couldn't map login buffer\n");
4075 goto buf_map_failed;
4076 }
4077
John Allen498cd8e2016-04-06 11:49:55 -05004078 rsp_buffer_size = sizeof(struct ibmvnic_login_rsp_buffer) +
4079 sizeof(u64) * adapter->req_tx_queues +
4080 sizeof(u64) * adapter->req_rx_queues +
4081 sizeof(u64) * adapter->req_rx_queues +
4082 sizeof(u8) * IBMVNIC_TX_DESC_VERSIONS;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004083
4084 login_rsp_buffer = kmalloc(rsp_buffer_size, GFP_ATOMIC);
4085 if (!login_rsp_buffer)
4086 goto buf_rsp_alloc_failed;
4087
4088 rsp_buffer_token = dma_map_single(dev, login_rsp_buffer,
4089 rsp_buffer_size, DMA_FROM_DEVICE);
4090 if (dma_mapping_error(dev, rsp_buffer_token)) {
4091 dev_err(dev, "Couldn't map login rsp buffer\n");
4092 goto buf_rsp_map_failed;
4093 }
Nathan Fontenot661a2622017-04-19 13:44:58 -04004094
Thomas Falcon032c5e82015-12-21 11:26:06 -06004095 adapter->login_buf = login_buffer;
4096 adapter->login_buf_token = buffer_token;
4097 adapter->login_buf_sz = buffer_size;
4098 adapter->login_rsp_buf = login_rsp_buffer;
4099 adapter->login_rsp_buf_token = rsp_buffer_token;
4100 adapter->login_rsp_buf_sz = rsp_buffer_size;
4101
4102 login_buffer->len = cpu_to_be32(buffer_size);
4103 login_buffer->version = cpu_to_be32(INITIAL_VERSION_LB);
4104 login_buffer->num_txcomp_subcrqs = cpu_to_be32(adapter->req_tx_queues);
4105 login_buffer->off_txcomp_subcrqs =
4106 cpu_to_be32(sizeof(struct ibmvnic_login_buffer));
4107 login_buffer->num_rxcomp_subcrqs = cpu_to_be32(adapter->req_rx_queues);
4108 login_buffer->off_rxcomp_subcrqs =
4109 cpu_to_be32(sizeof(struct ibmvnic_login_buffer) +
4110 sizeof(u64) * adapter->req_tx_queues);
4111 login_buffer->login_rsp_ioba = cpu_to_be32(rsp_buffer_token);
4112 login_buffer->login_rsp_len = cpu_to_be32(rsp_buffer_size);
4113
4114 tx_list_p = (__be64 *)((char *)login_buffer +
4115 sizeof(struct ibmvnic_login_buffer));
4116 rx_list_p = (__be64 *)((char *)login_buffer +
4117 sizeof(struct ibmvnic_login_buffer) +
4118 sizeof(u64) * adapter->req_tx_queues);
4119
4120 for (i = 0; i < adapter->req_tx_queues; i++) {
4121 if (adapter->tx_scrq[i]) {
Lijun Pan914789a2021-02-11 00:43:21 -06004122 tx_list_p[i] =
4123 cpu_to_be64(adapter->tx_scrq[i]->crq_num);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004124 }
4125 }
4126
4127 for (i = 0; i < adapter->req_rx_queues; i++) {
4128 if (adapter->rx_scrq[i]) {
Lijun Pan914789a2021-02-11 00:43:21 -06004129 rx_list_p[i] =
4130 cpu_to_be64(adapter->rx_scrq[i]->crq_num);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004131 }
4132 }
4133
Nathan Fontenot37798d02017-11-08 11:23:56 -06004134 /* Insert vNIC login client data */
4135 vlcd = (struct vnic_login_client_data *)
4136 ((char *)rx_list_p + (sizeof(u64) * adapter->req_rx_queues));
4137 login_buffer->client_data_offset =
4138 cpu_to_be32((char *)vlcd - (char *)login_buffer);
4139 login_buffer->client_data_len = cpu_to_be32(client_data_len);
4140
4141 vnic_add_client_data(adapter, vlcd);
4142
Thomas Falcon032c5e82015-12-21 11:26:06 -06004143 netdev_dbg(adapter->netdev, "Login Buffer:\n");
4144 for (i = 0; i < (adapter->login_buf_sz - 1) / 8 + 1; i++) {
4145 netdev_dbg(adapter->netdev, "%016lx\n",
Lijun Pan429aa362021-02-11 00:43:18 -06004146 ((unsigned long *)(adapter->login_buf))[i]);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004147 }
4148
4149 memset(&crq, 0, sizeof(crq));
4150 crq.login.first = IBMVNIC_CRQ_CMD;
4151 crq.login.cmd = LOGIN;
4152 crq.login.ioba = cpu_to_be32(buffer_token);
4153 crq.login.len = cpu_to_be32(buffer_size);
Sukadev Bhattiprolu76cdc5c2020-11-25 18:04:29 -06004154
4155 adapter->login_pending = true;
Dany Maddenc98d9cc2020-11-25 18:04:30 -06004156 rc = ibmvnic_send_crq(adapter, &crq);
4157 if (rc) {
4158 adapter->login_pending = false;
4159 netdev_err(adapter->netdev, "Failed to send login, rc=%d\n", rc);
4160 goto buf_rsp_map_failed;
4161 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06004162
Thomas Falcon20a8ab72018-02-26 18:10:59 -06004163 return 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004164
Thomas Falcon032c5e82015-12-21 11:26:06 -06004165buf_rsp_map_failed:
4166 kfree(login_rsp_buffer);
Dany Maddenc98d9cc2020-11-25 18:04:30 -06004167 adapter->login_rsp_buf = NULL;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004168buf_rsp_alloc_failed:
4169 dma_unmap_single(dev, buffer_token, buffer_size, DMA_TO_DEVICE);
4170buf_map_failed:
4171 kfree(login_buffer);
Dany Maddenc98d9cc2020-11-25 18:04:30 -06004172 adapter->login_buf = NULL;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004173buf_alloc_failed:
Thomas Falcon20a8ab72018-02-26 18:10:59 -06004174 return -1;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004175}
4176
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05004177static int send_request_map(struct ibmvnic_adapter *adapter, dma_addr_t addr,
4178 u32 len, u8 map_id)
Thomas Falcon032c5e82015-12-21 11:26:06 -06004179{
4180 union ibmvnic_crq crq;
4181
4182 memset(&crq, 0, sizeof(crq));
4183 crq.request_map.first = IBMVNIC_CRQ_CMD;
4184 crq.request_map.cmd = REQUEST_MAP;
4185 crq.request_map.map_id = map_id;
4186 crq.request_map.ioba = cpu_to_be32(addr);
4187 crq.request_map.len = cpu_to_be32(len);
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05004188 return ibmvnic_send_crq(adapter, &crq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004189}
4190
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05004191static int send_request_unmap(struct ibmvnic_adapter *adapter, u8 map_id)
Thomas Falcon032c5e82015-12-21 11:26:06 -06004192{
4193 union ibmvnic_crq crq;
4194
4195 memset(&crq, 0, sizeof(crq));
4196 crq.request_unmap.first = IBMVNIC_CRQ_CMD;
4197 crq.request_unmap.cmd = REQUEST_UNMAP;
4198 crq.request_unmap.map_id = map_id;
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05004199 return ibmvnic_send_crq(adapter, &crq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004200}
4201
Lijun Pan69980d02020-09-27 20:13:28 -05004202static void send_query_map(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06004203{
4204 union ibmvnic_crq crq;
4205
4206 memset(&crq, 0, sizeof(crq));
4207 crq.query_map.first = IBMVNIC_CRQ_CMD;
4208 crq.query_map.cmd = QUERY_MAP;
4209 ibmvnic_send_crq(adapter, &crq);
4210}
4211
4212/* Send a series of CRQs requesting various capabilities of the VNIC server */
Lijun Pan491099a2020-09-27 20:13:26 -05004213static void send_query_cap(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06004214{
4215 union ibmvnic_crq crq;
4216
Thomas Falcon901e0402017-02-15 12:17:59 -06004217 atomic_set(&adapter->running_cap_crqs, 0);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004218 memset(&crq, 0, sizeof(crq));
4219 crq.query_capability.first = IBMVNIC_CRQ_CMD;
4220 crq.query_capability.cmd = QUERY_CAPABILITY;
4221
4222 crq.query_capability.capability = cpu_to_be16(MIN_TX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06004223 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004224 ibmvnic_send_crq(adapter, &crq);
4225
4226 crq.query_capability.capability = cpu_to_be16(MIN_RX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06004227 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004228 ibmvnic_send_crq(adapter, &crq);
4229
4230 crq.query_capability.capability = cpu_to_be16(MIN_RX_ADD_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06004231 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004232 ibmvnic_send_crq(adapter, &crq);
4233
4234 crq.query_capability.capability = cpu_to_be16(MAX_TX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06004235 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004236 ibmvnic_send_crq(adapter, &crq);
4237
4238 crq.query_capability.capability = cpu_to_be16(MAX_RX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06004239 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004240 ibmvnic_send_crq(adapter, &crq);
4241
4242 crq.query_capability.capability = cpu_to_be16(MAX_RX_ADD_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06004243 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004244 ibmvnic_send_crq(adapter, &crq);
4245
4246 crq.query_capability.capability =
4247 cpu_to_be16(MIN_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06004248 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004249 ibmvnic_send_crq(adapter, &crq);
4250
4251 crq.query_capability.capability =
4252 cpu_to_be16(MIN_RX_ADD_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06004253 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004254 ibmvnic_send_crq(adapter, &crq);
4255
4256 crq.query_capability.capability =
4257 cpu_to_be16(MAX_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06004258 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004259 ibmvnic_send_crq(adapter, &crq);
4260
4261 crq.query_capability.capability =
4262 cpu_to_be16(MAX_RX_ADD_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06004263 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004264 ibmvnic_send_crq(adapter, &crq);
4265
4266 crq.query_capability.capability = cpu_to_be16(TCP_IP_OFFLOAD);
Thomas Falcon901e0402017-02-15 12:17:59 -06004267 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004268 ibmvnic_send_crq(adapter, &crq);
4269
4270 crq.query_capability.capability = cpu_to_be16(PROMISC_SUPPORTED);
Thomas Falcon901e0402017-02-15 12:17:59 -06004271 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004272 ibmvnic_send_crq(adapter, &crq);
4273
4274 crq.query_capability.capability = cpu_to_be16(MIN_MTU);
Thomas Falcon901e0402017-02-15 12:17:59 -06004275 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004276 ibmvnic_send_crq(adapter, &crq);
4277
4278 crq.query_capability.capability = cpu_to_be16(MAX_MTU);
Thomas Falcon901e0402017-02-15 12:17:59 -06004279 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004280 ibmvnic_send_crq(adapter, &crq);
4281
4282 crq.query_capability.capability = cpu_to_be16(MAX_MULTICAST_FILTERS);
Thomas Falcon901e0402017-02-15 12:17:59 -06004283 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004284 ibmvnic_send_crq(adapter, &crq);
4285
4286 crq.query_capability.capability = cpu_to_be16(VLAN_HEADER_INSERTION);
Thomas Falcon901e0402017-02-15 12:17:59 -06004287 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004288 ibmvnic_send_crq(adapter, &crq);
4289
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04004290 crq.query_capability.capability = cpu_to_be16(RX_VLAN_HEADER_INSERTION);
4291 atomic_inc(&adapter->running_cap_crqs);
4292 ibmvnic_send_crq(adapter, &crq);
4293
Thomas Falcon032c5e82015-12-21 11:26:06 -06004294 crq.query_capability.capability = cpu_to_be16(MAX_TX_SG_ENTRIES);
Thomas Falcon901e0402017-02-15 12:17:59 -06004295 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004296 ibmvnic_send_crq(adapter, &crq);
4297
4298 crq.query_capability.capability = cpu_to_be16(RX_SG_SUPPORTED);
Thomas Falcon901e0402017-02-15 12:17:59 -06004299 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004300 ibmvnic_send_crq(adapter, &crq);
4301
4302 crq.query_capability.capability = cpu_to_be16(OPT_TX_COMP_SUB_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06004303 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004304 ibmvnic_send_crq(adapter, &crq);
4305
4306 crq.query_capability.capability = cpu_to_be16(OPT_RX_COMP_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06004307 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004308 ibmvnic_send_crq(adapter, &crq);
4309
4310 crq.query_capability.capability =
4311 cpu_to_be16(OPT_RX_BUFADD_Q_PER_RX_COMP_Q);
Thomas Falcon901e0402017-02-15 12:17:59 -06004312 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004313 ibmvnic_send_crq(adapter, &crq);
4314
4315 crq.query_capability.capability =
4316 cpu_to_be16(OPT_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06004317 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004318 ibmvnic_send_crq(adapter, &crq);
4319
4320 crq.query_capability.capability =
4321 cpu_to_be16(OPT_RXBA_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06004322 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004323 ibmvnic_send_crq(adapter, &crq);
4324
4325 crq.query_capability.capability = cpu_to_be16(TX_RX_DESC_REQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06004326 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004327 ibmvnic_send_crq(adapter, &crq);
4328}
4329
Lijun Pan16e811f2020-09-27 20:13:29 -05004330static void send_query_ip_offload(struct ibmvnic_adapter *adapter)
4331{
4332 int buf_sz = sizeof(struct ibmvnic_query_ip_offload_buffer);
4333 struct device *dev = &adapter->vdev->dev;
4334 union ibmvnic_crq crq;
4335
4336 adapter->ip_offload_tok =
4337 dma_map_single(dev,
4338 &adapter->ip_offload_buf,
4339 buf_sz,
4340 DMA_FROM_DEVICE);
4341
4342 if (dma_mapping_error(dev, adapter->ip_offload_tok)) {
4343 if (!firmware_has_feature(FW_FEATURE_CMO))
4344 dev_err(dev, "Couldn't map offload buffer\n");
4345 return;
4346 }
4347
4348 memset(&crq, 0, sizeof(crq));
4349 crq.query_ip_offload.first = IBMVNIC_CRQ_CMD;
4350 crq.query_ip_offload.cmd = QUERY_IP_OFFLOAD;
4351 crq.query_ip_offload.len = cpu_to_be32(buf_sz);
4352 crq.query_ip_offload.ioba =
4353 cpu_to_be32(adapter->ip_offload_tok);
4354
4355 ibmvnic_send_crq(adapter, &crq);
4356}
4357
Lijun Pan46899bd2020-09-27 20:13:30 -05004358static void send_control_ip_offload(struct ibmvnic_adapter *adapter)
4359{
4360 struct ibmvnic_control_ip_offload_buffer *ctrl_buf = &adapter->ip_offload_ctrl;
4361 struct ibmvnic_query_ip_offload_buffer *buf = &adapter->ip_offload_buf;
4362 struct device *dev = &adapter->vdev->dev;
4363 netdev_features_t old_hw_features = 0;
4364 union ibmvnic_crq crq;
4365
4366 adapter->ip_offload_ctrl_tok =
4367 dma_map_single(dev,
4368 ctrl_buf,
4369 sizeof(adapter->ip_offload_ctrl),
4370 DMA_TO_DEVICE);
4371
4372 if (dma_mapping_error(dev, adapter->ip_offload_ctrl_tok)) {
4373 dev_err(dev, "Couldn't map ip offload control buffer\n");
4374 return;
4375 }
4376
4377 ctrl_buf->len = cpu_to_be32(sizeof(adapter->ip_offload_ctrl));
4378 ctrl_buf->version = cpu_to_be32(INITIAL_VERSION_IOB);
4379 ctrl_buf->ipv4_chksum = buf->ipv4_chksum;
4380 ctrl_buf->ipv6_chksum = buf->ipv6_chksum;
4381 ctrl_buf->tcp_ipv4_chksum = buf->tcp_ipv4_chksum;
4382 ctrl_buf->udp_ipv4_chksum = buf->udp_ipv4_chksum;
4383 ctrl_buf->tcp_ipv6_chksum = buf->tcp_ipv6_chksum;
4384 ctrl_buf->udp_ipv6_chksum = buf->udp_ipv6_chksum;
4385 ctrl_buf->large_tx_ipv4 = buf->large_tx_ipv4;
4386 ctrl_buf->large_tx_ipv6 = buf->large_tx_ipv6;
4387
4388 /* large_rx disabled for now, additional features needed */
4389 ctrl_buf->large_rx_ipv4 = 0;
4390 ctrl_buf->large_rx_ipv6 = 0;
4391
4392 if (adapter->state != VNIC_PROBING) {
4393 old_hw_features = adapter->netdev->hw_features;
4394 adapter->netdev->hw_features = 0;
4395 }
4396
4397 adapter->netdev->hw_features = NETIF_F_SG | NETIF_F_GSO | NETIF_F_GRO;
4398
4399 if (buf->tcp_ipv4_chksum || buf->udp_ipv4_chksum)
4400 adapter->netdev->hw_features |= NETIF_F_IP_CSUM;
4401
4402 if (buf->tcp_ipv6_chksum || buf->udp_ipv6_chksum)
4403 adapter->netdev->hw_features |= NETIF_F_IPV6_CSUM;
4404
4405 if ((adapter->netdev->features &
4406 (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)))
4407 adapter->netdev->hw_features |= NETIF_F_RXCSUM;
4408
4409 if (buf->large_tx_ipv4)
4410 adapter->netdev->hw_features |= NETIF_F_TSO;
4411 if (buf->large_tx_ipv6)
4412 adapter->netdev->hw_features |= NETIF_F_TSO6;
4413
4414 if (adapter->state == VNIC_PROBING) {
4415 adapter->netdev->features |= adapter->netdev->hw_features;
4416 } else if (old_hw_features != adapter->netdev->hw_features) {
4417 netdev_features_t tmp = 0;
4418
4419 /* disable features no longer supported */
4420 adapter->netdev->features &= adapter->netdev->hw_features;
4421 /* turn on features now supported if previously enabled */
4422 tmp = (old_hw_features ^ adapter->netdev->hw_features) &
4423 adapter->netdev->hw_features;
4424 adapter->netdev->features |=
4425 tmp & adapter->netdev->wanted_features;
4426 }
4427
4428 memset(&crq, 0, sizeof(crq));
4429 crq.control_ip_offload.first = IBMVNIC_CRQ_CMD;
4430 crq.control_ip_offload.cmd = CONTROL_IP_OFFLOAD;
4431 crq.control_ip_offload.len =
4432 cpu_to_be32(sizeof(adapter->ip_offload_ctrl));
4433 crq.control_ip_offload.ioba = cpu_to_be32(adapter->ip_offload_ctrl_tok);
4434 ibmvnic_send_crq(adapter, &crq);
4435}
4436
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02004437static void handle_vpd_size_rsp(union ibmvnic_crq *crq,
4438 struct ibmvnic_adapter *adapter)
4439{
4440 struct device *dev = &adapter->vdev->dev;
4441
4442 if (crq->get_vpd_size_rsp.rc.code) {
4443 dev_err(dev, "Error retrieving VPD size, rc=%x\n",
4444 crq->get_vpd_size_rsp.rc.code);
4445 complete(&adapter->fw_done);
4446 return;
4447 }
4448
4449 adapter->vpd->len = be64_to_cpu(crq->get_vpd_size_rsp.len);
4450 complete(&adapter->fw_done);
4451}
4452
4453static void handle_vpd_rsp(union ibmvnic_crq *crq,
4454 struct ibmvnic_adapter *adapter)
4455{
4456 struct device *dev = &adapter->vdev->dev;
Desnes Augusto Nunes do Rosario21a25452018-02-05 14:33:55 -02004457 unsigned char *substr = NULL;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02004458 u8 fw_level_len = 0;
4459
4460 memset(adapter->fw_version, 0, 32);
4461
4462 dma_unmap_single(dev, adapter->vpd->dma_addr, adapter->vpd->len,
4463 DMA_FROM_DEVICE);
4464
4465 if (crq->get_vpd_rsp.rc.code) {
4466 dev_err(dev, "Error retrieving VPD from device, rc=%x\n",
4467 crq->get_vpd_rsp.rc.code);
4468 goto complete;
4469 }
4470
4471 /* get the position of the firmware version info
4472 * located after the ASCII 'RM' substring in the buffer
4473 */
4474 substr = strnstr(adapter->vpd->buff, "RM", adapter->vpd->len);
4475 if (!substr) {
Desnes Augusto Nunes do Rosarioa1073112018-02-01 16:04:30 -02004476 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 -02004477 goto complete;
4478 }
4479
4480 /* get length of firmware level ASCII substring */
4481 if ((substr + 2) < (adapter->vpd->buff + adapter->vpd->len)) {
4482 fw_level_len = *(substr + 2);
4483 } else {
4484 dev_info(dev, "Length of FW substr extrapolated VDP buff\n");
4485 goto complete;
4486 }
4487
4488 /* copy firmware version string from vpd into adapter */
4489 if ((substr + 3 + fw_level_len) <
4490 (adapter->vpd->buff + adapter->vpd->len)) {
Desnes Augusto Nunes do Rosario21a25452018-02-05 14:33:55 -02004491 strncpy((char *)adapter->fw_version, substr + 3, fw_level_len);
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02004492 } else {
4493 dev_info(dev, "FW substr extrapolated VPD buff\n");
4494 }
4495
4496complete:
Desnes Augusto Nunes do Rosario21a25452018-02-05 14:33:55 -02004497 if (adapter->fw_version[0] == '\0')
Lijun Pan0b217d32021-06-11 13:33:53 -05004498 strscpy((char *)adapter->fw_version, "N/A", sizeof(adapter->fw_version));
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02004499 complete(&adapter->fw_done);
4500}
4501
Thomas Falcon032c5e82015-12-21 11:26:06 -06004502static void handle_query_ip_offload_rsp(struct ibmvnic_adapter *adapter)
4503{
4504 struct device *dev = &adapter->vdev->dev;
4505 struct ibmvnic_query_ip_offload_buffer *buf = &adapter->ip_offload_buf;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004506 int i;
4507
4508 dma_unmap_single(dev, adapter->ip_offload_tok,
4509 sizeof(adapter->ip_offload_buf), DMA_FROM_DEVICE);
4510
4511 netdev_dbg(adapter->netdev, "Query IP Offload Buffer:\n");
4512 for (i = 0; i < (sizeof(adapter->ip_offload_buf) - 1) / 8 + 1; i++)
4513 netdev_dbg(adapter->netdev, "%016lx\n",
Lijun Pan429aa362021-02-11 00:43:18 -06004514 ((unsigned long *)(buf))[i]);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004515
4516 netdev_dbg(adapter->netdev, "ipv4_chksum = %d\n", buf->ipv4_chksum);
4517 netdev_dbg(adapter->netdev, "ipv6_chksum = %d\n", buf->ipv6_chksum);
4518 netdev_dbg(adapter->netdev, "tcp_ipv4_chksum = %d\n",
4519 buf->tcp_ipv4_chksum);
4520 netdev_dbg(adapter->netdev, "tcp_ipv6_chksum = %d\n",
4521 buf->tcp_ipv6_chksum);
4522 netdev_dbg(adapter->netdev, "udp_ipv4_chksum = %d\n",
4523 buf->udp_ipv4_chksum);
4524 netdev_dbg(adapter->netdev, "udp_ipv6_chksum = %d\n",
4525 buf->udp_ipv6_chksum);
4526 netdev_dbg(adapter->netdev, "large_tx_ipv4 = %d\n",
4527 buf->large_tx_ipv4);
4528 netdev_dbg(adapter->netdev, "large_tx_ipv6 = %d\n",
4529 buf->large_tx_ipv6);
4530 netdev_dbg(adapter->netdev, "large_rx_ipv4 = %d\n",
4531 buf->large_rx_ipv4);
4532 netdev_dbg(adapter->netdev, "large_rx_ipv6 = %d\n",
4533 buf->large_rx_ipv6);
4534 netdev_dbg(adapter->netdev, "max_ipv4_hdr_sz = %d\n",
4535 buf->max_ipv4_header_size);
4536 netdev_dbg(adapter->netdev, "max_ipv6_hdr_sz = %d\n",
4537 buf->max_ipv6_header_size);
4538 netdev_dbg(adapter->netdev, "max_tcp_hdr_size = %d\n",
4539 buf->max_tcp_header_size);
4540 netdev_dbg(adapter->netdev, "max_udp_hdr_size = %d\n",
4541 buf->max_udp_header_size);
4542 netdev_dbg(adapter->netdev, "max_large_tx_size = %d\n",
4543 buf->max_large_tx_size);
4544 netdev_dbg(adapter->netdev, "max_large_rx_size = %d\n",
4545 buf->max_large_rx_size);
4546 netdev_dbg(adapter->netdev, "ipv6_ext_hdr = %d\n",
4547 buf->ipv6_extension_header);
4548 netdev_dbg(adapter->netdev, "tcp_pseudosum_req = %d\n",
4549 buf->tcp_pseudosum_req);
4550 netdev_dbg(adapter->netdev, "num_ipv6_ext_hd = %d\n",
4551 buf->num_ipv6_ext_headers);
4552 netdev_dbg(adapter->netdev, "off_ipv6_ext_hd = %d\n",
4553 buf->off_ipv6_ext_headers);
4554
Lijun Pan46899bd2020-09-27 20:13:30 -05004555 send_control_ip_offload(adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004556}
4557
Thomas Falconc9008d32018-08-06 21:39:59 -05004558static const char *ibmvnic_fw_err_cause(u16 cause)
4559{
4560 switch (cause) {
4561 case ADAPTER_PROBLEM:
4562 return "adapter problem";
4563 case BUS_PROBLEM:
4564 return "bus problem";
4565 case FW_PROBLEM:
4566 return "firmware problem";
4567 case DD_PROBLEM:
4568 return "device driver problem";
4569 case EEH_RECOVERY:
4570 return "EEH recovery";
4571 case FW_UPDATED:
4572 return "firmware updated";
4573 case LOW_MEMORY:
4574 return "low Memory";
4575 default:
4576 return "unknown";
4577 }
4578}
4579
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04004580static void handle_error_indication(union ibmvnic_crq *crq,
4581 struct ibmvnic_adapter *adapter)
4582{
4583 struct device *dev = &adapter->vdev->dev;
Thomas Falconc9008d32018-08-06 21:39:59 -05004584 u16 cause;
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04004585
Thomas Falconc9008d32018-08-06 21:39:59 -05004586 cause = be16_to_cpu(crq->error_indication.error_cause);
4587
4588 dev_warn_ratelimited(dev,
4589 "Firmware reports %serror, cause: %s. Starting recovery...\n",
4590 crq->error_indication.flags
4591 & IBMVNIC_FATAL_ERROR ? "FATAL " : "",
4592 ibmvnic_fw_err_cause(cause));
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04004593
Nathan Fontenoted651a12017-05-03 14:04:38 -04004594 if (crq->error_indication.flags & IBMVNIC_FATAL_ERROR)
4595 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
John Allen8cb31cf2017-05-26 10:30:37 -04004596 else
4597 ibmvnic_reset(adapter, VNIC_RESET_NON_FATAL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004598}
4599
Thomas Falconf8136142018-01-29 13:45:05 -06004600static int handle_change_mac_rsp(union ibmvnic_crq *crq,
4601 struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06004602{
4603 struct net_device *netdev = adapter->netdev;
4604 struct device *dev = &adapter->vdev->dev;
4605 long rc;
4606
4607 rc = crq->change_mac_addr_rsp.rc.code;
4608 if (rc) {
4609 dev_err(dev, "Error %ld in CHANGE_MAC_ADDR_RSP\n", rc);
Thomas Falconf8136142018-01-29 13:45:05 -06004610 goto out;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004611 }
Lijun Pand9b0e592020-10-20 17:39:19 -05004612 /* crq->change_mac_addr.mac_addr is the requested one
4613 * crq->change_mac_addr_rsp.mac_addr is the returned valid one.
4614 */
Thomas Falcon62740e92019-05-09 23:13:43 -05004615 ether_addr_copy(netdev->dev_addr,
4616 &crq->change_mac_addr_rsp.mac_addr[0]);
Lijun Pand9b0e592020-10-20 17:39:19 -05004617 ether_addr_copy(adapter->mac_addr,
4618 &crq->change_mac_addr_rsp.mac_addr[0]);
Thomas Falconf8136142018-01-29 13:45:05 -06004619out:
4620 complete(&adapter->fw_done);
4621 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004622}
4623
4624static void handle_request_cap_rsp(union ibmvnic_crq *crq,
4625 struct ibmvnic_adapter *adapter)
4626{
4627 struct device *dev = &adapter->vdev->dev;
4628 u64 *req_value;
4629 char *name;
4630
Thomas Falcon901e0402017-02-15 12:17:59 -06004631 atomic_dec(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004632 switch (be16_to_cpu(crq->request_capability_rsp.capability)) {
4633 case REQ_TX_QUEUES:
4634 req_value = &adapter->req_tx_queues;
4635 name = "tx";
4636 break;
4637 case REQ_RX_QUEUES:
4638 req_value = &adapter->req_rx_queues;
4639 name = "rx";
4640 break;
4641 case REQ_RX_ADD_QUEUES:
4642 req_value = &adapter->req_rx_add_queues;
4643 name = "rx_add";
4644 break;
4645 case REQ_TX_ENTRIES_PER_SUBCRQ:
4646 req_value = &adapter->req_tx_entries_per_subcrq;
4647 name = "tx_entries_per_subcrq";
4648 break;
4649 case REQ_RX_ADD_ENTRIES_PER_SUBCRQ:
4650 req_value = &adapter->req_rx_add_entries_per_subcrq;
4651 name = "rx_add_entries_per_subcrq";
4652 break;
4653 case REQ_MTU:
4654 req_value = &adapter->req_mtu;
4655 name = "mtu";
4656 break;
4657 case PROMISC_REQUESTED:
4658 req_value = &adapter->promisc;
4659 name = "promisc";
4660 break;
4661 default:
4662 dev_err(dev, "Got invalid cap request rsp %d\n",
4663 crq->request_capability.capability);
4664 return;
4665 }
4666
4667 switch (crq->request_capability_rsp.rc.code) {
4668 case SUCCESS:
4669 break;
4670 case PARTIALSUCCESS:
4671 dev_info(dev, "req=%lld, rsp=%ld in %s queue, retrying.\n",
4672 *req_value,
Lijun Pan914789a2021-02-11 00:43:21 -06004673 (long)be64_to_cpu(crq->request_capability_rsp.number),
4674 name);
John Allene7913802018-01-18 16:27:12 -06004675
4676 if (be16_to_cpu(crq->request_capability_rsp.capability) ==
4677 REQ_MTU) {
4678 pr_err("mtu of %llu is not supported. Reverting.\n",
4679 *req_value);
4680 *req_value = adapter->fallback.mtu;
4681 } else {
4682 *req_value =
4683 be64_to_cpu(crq->request_capability_rsp.number);
4684 }
4685
Lijun Pan09081b92020-09-27 20:13:27 -05004686 send_request_cap(adapter, 1);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004687 return;
4688 default:
4689 dev_err(dev, "Error %d in request cap rsp\n",
4690 crq->request_capability_rsp.rc.code);
4691 return;
4692 }
4693
4694 /* Done receiving requested capabilities, query IP offload support */
Thomas Falcon901e0402017-02-15 12:17:59 -06004695 if (atomic_read(&adapter->running_cap_crqs) == 0) {
Thomas Falcon249168a2017-02-15 12:18:00 -06004696 adapter->wait_capability = false;
Lijun Pan16e811f2020-09-27 20:13:29 -05004697 send_query_ip_offload(adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004698 }
4699}
4700
4701static int handle_login_rsp(union ibmvnic_crq *login_rsp_crq,
4702 struct ibmvnic_adapter *adapter)
4703{
4704 struct device *dev = &adapter->vdev->dev;
John Allenc26eba02017-10-26 16:23:25 -05004705 struct net_device *netdev = adapter->netdev;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004706 struct ibmvnic_login_rsp_buffer *login_rsp = adapter->login_rsp_buf;
4707 struct ibmvnic_login_buffer *login = adapter->login_buf;
Cristobal Fornof3ae59c2020-08-19 13:16:23 -05004708 u64 *tx_handle_array;
4709 u64 *rx_handle_array;
4710 int num_tx_pools;
4711 int num_rx_pools;
Thomas Falcon507ebe62020-08-21 13:39:01 -05004712 u64 *size_array;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004713 int i;
4714
Sukadev Bhattiprolu76cdc5c2020-11-25 18:04:29 -06004715 /* CHECK: Test/set of login_pending does not need to be atomic
4716 * because only ibmvnic_tasklet tests/clears this.
4717 */
4718 if (!adapter->login_pending) {
4719 netdev_warn(netdev, "Ignoring unexpected login response\n");
4720 return 0;
4721 }
4722 adapter->login_pending = false;
4723
Thomas Falcon032c5e82015-12-21 11:26:06 -06004724 dma_unmap_single(dev, adapter->login_buf_token, adapter->login_buf_sz,
Thomas Falcon37e40fa2018-04-06 18:37:02 -05004725 DMA_TO_DEVICE);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004726 dma_unmap_single(dev, adapter->login_rsp_buf_token,
Thomas Falcon37e40fa2018-04-06 18:37:02 -05004727 adapter->login_rsp_buf_sz, DMA_FROM_DEVICE);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004728
John Allen498cd8e2016-04-06 11:49:55 -05004729 /* If the number of queues requested can't be allocated by the
4730 * server, the login response will return with code 1. We will need
4731 * to resend the login buffer with fewer queues requested.
4732 */
4733 if (login_rsp_crq->generic.rc.code) {
Nathan Fontenot64d92aa2018-04-11 10:09:32 -05004734 adapter->init_done_rc = login_rsp_crq->generic.rc.code;
John Allen498cd8e2016-04-06 11:49:55 -05004735 complete(&adapter->init_done);
4736 return 0;
4737 }
4738
John Allenc26eba02017-10-26 16:23:25 -05004739 netdev->mtu = adapter->req_mtu - ETH_HLEN;
4740
Thomas Falcon032c5e82015-12-21 11:26:06 -06004741 netdev_dbg(adapter->netdev, "Login Response Buffer:\n");
4742 for (i = 0; i < (adapter->login_rsp_buf_sz - 1) / 8 + 1; i++) {
4743 netdev_dbg(adapter->netdev, "%016lx\n",
Lijun Pan429aa362021-02-11 00:43:18 -06004744 ((unsigned long *)(adapter->login_rsp_buf))[i]);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004745 }
4746
4747 /* Sanity checks */
4748 if (login->num_txcomp_subcrqs != login_rsp->num_txsubm_subcrqs ||
4749 (be32_to_cpu(login->num_rxcomp_subcrqs) *
4750 adapter->req_rx_add_queues !=
4751 be32_to_cpu(login_rsp->num_rxadd_subcrqs))) {
4752 dev_err(dev, "FATAL: Inconsistent login and login rsp\n");
Dany Madden31d6b402020-11-25 18:04:24 -06004753 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004754 return -EIO;
4755 }
Thomas Falcon507ebe62020-08-21 13:39:01 -05004756 size_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
4757 be32_to_cpu(adapter->login_rsp_buf->off_rxadd_buff_size));
4758 /* variable buffer sizes are not supported, so just read the
4759 * first entry.
4760 */
4761 adapter->cur_rx_buf_sz = be64_to_cpu(size_array[0]);
Cristobal Fornof3ae59c2020-08-19 13:16:23 -05004762
4763 num_tx_pools = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
4764 num_rx_pools = be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
4765
4766 tx_handle_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
4767 be32_to_cpu(adapter->login_rsp_buf->off_txsubm_subcrqs));
4768 rx_handle_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
4769 be32_to_cpu(adapter->login_rsp_buf->off_rxadd_subcrqs));
4770
4771 for (i = 0; i < num_tx_pools; i++)
4772 adapter->tx_scrq[i]->handle = tx_handle_array[i];
4773
4774 for (i = 0; i < num_rx_pools; i++)
4775 adapter->rx_scrq[i]->handle = rx_handle_array[i];
4776
Thomas Falcon507ebe62020-08-21 13:39:01 -05004777 adapter->num_active_tx_scrqs = num_tx_pools;
4778 adapter->num_active_rx_scrqs = num_rx_pools;
Cristobal Fornof3ae59c2020-08-19 13:16:23 -05004779 release_login_rsp_buffer(adapter);
Thomas Falcona2c0f032018-02-21 18:18:30 -06004780 release_login_buffer(adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004781 complete(&adapter->init_done);
4782
Thomas Falcon032c5e82015-12-21 11:26:06 -06004783 return 0;
4784}
4785
Thomas Falcon032c5e82015-12-21 11:26:06 -06004786static void handle_request_unmap_rsp(union ibmvnic_crq *crq,
4787 struct ibmvnic_adapter *adapter)
4788{
4789 struct device *dev = &adapter->vdev->dev;
4790 long rc;
4791
4792 rc = crq->request_unmap_rsp.rc.code;
4793 if (rc)
4794 dev_err(dev, "Error %ld in REQUEST_UNMAP_RSP\n", rc);
4795}
4796
4797static void handle_query_map_rsp(union ibmvnic_crq *crq,
4798 struct ibmvnic_adapter *adapter)
4799{
4800 struct net_device *netdev = adapter->netdev;
4801 struct device *dev = &adapter->vdev->dev;
4802 long rc;
4803
4804 rc = crq->query_map_rsp.rc.code;
4805 if (rc) {
4806 dev_err(dev, "Error %ld in QUERY_MAP_RSP\n", rc);
4807 return;
4808 }
Sukadev Bhattiprolu0f2bf312021-09-14 20:52:52 -07004809 netdev_dbg(netdev, "page_size = %d\ntot_pages = %u\nfree_pages = %u\n",
4810 crq->query_map_rsp.page_size,
4811 __be32_to_cpu(crq->query_map_rsp.tot_pages),
4812 __be32_to_cpu(crq->query_map_rsp.free_pages));
Thomas Falcon032c5e82015-12-21 11:26:06 -06004813}
4814
4815static void handle_query_cap_rsp(union ibmvnic_crq *crq,
4816 struct ibmvnic_adapter *adapter)
4817{
4818 struct net_device *netdev = adapter->netdev;
4819 struct device *dev = &adapter->vdev->dev;
4820 long rc;
4821
Thomas Falcon901e0402017-02-15 12:17:59 -06004822 atomic_dec(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004823 netdev_dbg(netdev, "Outstanding queries: %d\n",
Thomas Falcon901e0402017-02-15 12:17:59 -06004824 atomic_read(&adapter->running_cap_crqs));
Thomas Falcon032c5e82015-12-21 11:26:06 -06004825 rc = crq->query_capability.rc.code;
4826 if (rc) {
4827 dev_err(dev, "Error %ld in QUERY_CAP_RSP\n", rc);
4828 goto out;
4829 }
4830
4831 switch (be16_to_cpu(crq->query_capability.capability)) {
4832 case MIN_TX_QUEUES:
4833 adapter->min_tx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004834 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004835 netdev_dbg(netdev, "min_tx_queues = %lld\n",
4836 adapter->min_tx_queues);
4837 break;
4838 case MIN_RX_QUEUES:
4839 adapter->min_rx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004840 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004841 netdev_dbg(netdev, "min_rx_queues = %lld\n",
4842 adapter->min_rx_queues);
4843 break;
4844 case MIN_RX_ADD_QUEUES:
4845 adapter->min_rx_add_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004846 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004847 netdev_dbg(netdev, "min_rx_add_queues = %lld\n",
4848 adapter->min_rx_add_queues);
4849 break;
4850 case MAX_TX_QUEUES:
4851 adapter->max_tx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004852 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004853 netdev_dbg(netdev, "max_tx_queues = %lld\n",
4854 adapter->max_tx_queues);
4855 break;
4856 case MAX_RX_QUEUES:
4857 adapter->max_rx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004858 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004859 netdev_dbg(netdev, "max_rx_queues = %lld\n",
4860 adapter->max_rx_queues);
4861 break;
4862 case MAX_RX_ADD_QUEUES:
4863 adapter->max_rx_add_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004864 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004865 netdev_dbg(netdev, "max_rx_add_queues = %lld\n",
4866 adapter->max_rx_add_queues);
4867 break;
4868 case MIN_TX_ENTRIES_PER_SUBCRQ:
4869 adapter->min_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06004870 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004871 netdev_dbg(netdev, "min_tx_entries_per_subcrq = %lld\n",
4872 adapter->min_tx_entries_per_subcrq);
4873 break;
4874 case MIN_RX_ADD_ENTRIES_PER_SUBCRQ:
4875 adapter->min_rx_add_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06004876 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004877 netdev_dbg(netdev, "min_rx_add_entrs_per_subcrq = %lld\n",
4878 adapter->min_rx_add_entries_per_subcrq);
4879 break;
4880 case MAX_TX_ENTRIES_PER_SUBCRQ:
4881 adapter->max_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06004882 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004883 netdev_dbg(netdev, "max_tx_entries_per_subcrq = %lld\n",
4884 adapter->max_tx_entries_per_subcrq);
4885 break;
4886 case MAX_RX_ADD_ENTRIES_PER_SUBCRQ:
4887 adapter->max_rx_add_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06004888 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004889 netdev_dbg(netdev, "max_rx_add_entrs_per_subcrq = %lld\n",
4890 adapter->max_rx_add_entries_per_subcrq);
4891 break;
4892 case TCP_IP_OFFLOAD:
4893 adapter->tcp_ip_offload =
Thomas Falconde89e852016-03-01 10:20:09 -06004894 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004895 netdev_dbg(netdev, "tcp_ip_offload = %lld\n",
4896 adapter->tcp_ip_offload);
4897 break;
4898 case PROMISC_SUPPORTED:
4899 adapter->promisc_supported =
Thomas Falconde89e852016-03-01 10:20:09 -06004900 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004901 netdev_dbg(netdev, "promisc_supported = %lld\n",
4902 adapter->promisc_supported);
4903 break;
4904 case MIN_MTU:
Thomas Falconde89e852016-03-01 10:20:09 -06004905 adapter->min_mtu = be64_to_cpu(crq->query_capability.number);
Thomas Falconf39f0d12017-02-14 10:22:59 -06004906 netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004907 netdev_dbg(netdev, "min_mtu = %lld\n", adapter->min_mtu);
4908 break;
4909 case MAX_MTU:
Thomas Falconde89e852016-03-01 10:20:09 -06004910 adapter->max_mtu = be64_to_cpu(crq->query_capability.number);
Thomas Falconf39f0d12017-02-14 10:22:59 -06004911 netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004912 netdev_dbg(netdev, "max_mtu = %lld\n", adapter->max_mtu);
4913 break;
4914 case MAX_MULTICAST_FILTERS:
4915 adapter->max_multicast_filters =
Thomas Falconde89e852016-03-01 10:20:09 -06004916 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004917 netdev_dbg(netdev, "max_multicast_filters = %lld\n",
4918 adapter->max_multicast_filters);
4919 break;
4920 case VLAN_HEADER_INSERTION:
4921 adapter->vlan_header_insertion =
Thomas Falconde89e852016-03-01 10:20:09 -06004922 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004923 if (adapter->vlan_header_insertion)
4924 netdev->features |= NETIF_F_HW_VLAN_STAG_TX;
4925 netdev_dbg(netdev, "vlan_header_insertion = %lld\n",
4926 adapter->vlan_header_insertion);
4927 break;
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04004928 case RX_VLAN_HEADER_INSERTION:
4929 adapter->rx_vlan_header_insertion =
4930 be64_to_cpu(crq->query_capability.number);
4931 netdev_dbg(netdev, "rx_vlan_header_insertion = %lld\n",
4932 adapter->rx_vlan_header_insertion);
4933 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004934 case MAX_TX_SG_ENTRIES:
4935 adapter->max_tx_sg_entries =
Thomas Falconde89e852016-03-01 10:20:09 -06004936 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004937 netdev_dbg(netdev, "max_tx_sg_entries = %lld\n",
4938 adapter->max_tx_sg_entries);
4939 break;
4940 case RX_SG_SUPPORTED:
4941 adapter->rx_sg_supported =
Thomas Falconde89e852016-03-01 10:20:09 -06004942 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004943 netdev_dbg(netdev, "rx_sg_supported = %lld\n",
4944 adapter->rx_sg_supported);
4945 break;
4946 case OPT_TX_COMP_SUB_QUEUES:
4947 adapter->opt_tx_comp_sub_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004948 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004949 netdev_dbg(netdev, "opt_tx_comp_sub_queues = %lld\n",
4950 adapter->opt_tx_comp_sub_queues);
4951 break;
4952 case OPT_RX_COMP_QUEUES:
4953 adapter->opt_rx_comp_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004954 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004955 netdev_dbg(netdev, "opt_rx_comp_queues = %lld\n",
4956 adapter->opt_rx_comp_queues);
4957 break;
4958 case OPT_RX_BUFADD_Q_PER_RX_COMP_Q:
4959 adapter->opt_rx_bufadd_q_per_rx_comp_q =
Thomas Falconde89e852016-03-01 10:20:09 -06004960 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004961 netdev_dbg(netdev, "opt_rx_bufadd_q_per_rx_comp_q = %lld\n",
4962 adapter->opt_rx_bufadd_q_per_rx_comp_q);
4963 break;
4964 case OPT_TX_ENTRIES_PER_SUBCRQ:
4965 adapter->opt_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06004966 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004967 netdev_dbg(netdev, "opt_tx_entries_per_subcrq = %lld\n",
4968 adapter->opt_tx_entries_per_subcrq);
4969 break;
4970 case OPT_RXBA_ENTRIES_PER_SUBCRQ:
4971 adapter->opt_rxba_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06004972 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004973 netdev_dbg(netdev, "opt_rxba_entries_per_subcrq = %lld\n",
4974 adapter->opt_rxba_entries_per_subcrq);
4975 break;
4976 case TX_RX_DESC_REQ:
4977 adapter->tx_rx_desc_req = crq->query_capability.number;
4978 netdev_dbg(netdev, "tx_rx_desc_req = %llx\n",
4979 adapter->tx_rx_desc_req);
4980 break;
4981
4982 default:
4983 netdev_err(netdev, "Got invalid cap rsp %d\n",
4984 crq->query_capability.capability);
4985 }
4986
4987out:
Thomas Falcon249168a2017-02-15 12:18:00 -06004988 if (atomic_read(&adapter->running_cap_crqs) == 0) {
4989 adapter->wait_capability = false;
Lijun Pan09081b92020-09-27 20:13:27 -05004990 send_request_cap(adapter, 0);
Thomas Falcon249168a2017-02-15 12:18:00 -06004991 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06004992}
4993
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03004994static int send_query_phys_parms(struct ibmvnic_adapter *adapter)
4995{
4996 union ibmvnic_crq crq;
4997 int rc;
4998
4999 memset(&crq, 0, sizeof(crq));
5000 crq.query_phys_parms.first = IBMVNIC_CRQ_CMD;
5001 crq.query_phys_parms.cmd = QUERY_PHYS_PARMS;
Thomas Falconff25dcb2019-11-25 17:12:56 -06005002
5003 mutex_lock(&adapter->fw_lock);
5004 adapter->fw_done_rc = 0;
Thomas Falcon070eca92019-11-25 17:12:53 -06005005 reinit_completion(&adapter->fw_done);
Thomas Falconff25dcb2019-11-25 17:12:56 -06005006
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03005007 rc = ibmvnic_send_crq(adapter, &crq);
Thomas Falconff25dcb2019-11-25 17:12:56 -06005008 if (rc) {
5009 mutex_unlock(&adapter->fw_lock);
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03005010 return rc;
Thomas Falconff25dcb2019-11-25 17:12:56 -06005011 }
Thomas Falcon476d96c2019-11-25 17:12:55 -06005012
5013 rc = ibmvnic_wait_for_completion(adapter, &adapter->fw_done, 10000);
Thomas Falconff25dcb2019-11-25 17:12:56 -06005014 if (rc) {
5015 mutex_unlock(&adapter->fw_lock);
Thomas Falcon476d96c2019-11-25 17:12:55 -06005016 return rc;
Thomas Falconff25dcb2019-11-25 17:12:56 -06005017 }
Thomas Falcon476d96c2019-11-25 17:12:55 -06005018
Thomas Falconff25dcb2019-11-25 17:12:56 -06005019 mutex_unlock(&adapter->fw_lock);
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03005020 return adapter->fw_done_rc ? -EIO : 0;
5021}
5022
5023static int handle_query_phys_parms_rsp(union ibmvnic_crq *crq,
5024 struct ibmvnic_adapter *adapter)
5025{
5026 struct net_device *netdev = adapter->netdev;
5027 int rc;
Murilo Fossa Vicentinidd0f9d82019-09-16 11:50:37 -03005028 __be32 rspeed = cpu_to_be32(crq->query_phys_parms_rsp.speed);
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03005029
5030 rc = crq->query_phys_parms_rsp.rc.code;
5031 if (rc) {
5032 netdev_err(netdev, "Error %d in QUERY_PHYS_PARMS\n", rc);
5033 return rc;
5034 }
Murilo Fossa Vicentinidd0f9d82019-09-16 11:50:37 -03005035 switch (rspeed) {
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03005036 case IBMVNIC_10MBPS:
5037 adapter->speed = SPEED_10;
5038 break;
5039 case IBMVNIC_100MBPS:
5040 adapter->speed = SPEED_100;
5041 break;
5042 case IBMVNIC_1GBPS:
5043 adapter->speed = SPEED_1000;
5044 break;
Lijun Panb9cd7952020-09-27 19:06:25 -05005045 case IBMVNIC_10GBPS:
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03005046 adapter->speed = SPEED_10000;
5047 break;
5048 case IBMVNIC_25GBPS:
5049 adapter->speed = SPEED_25000;
5050 break;
5051 case IBMVNIC_40GBPS:
5052 adapter->speed = SPEED_40000;
5053 break;
5054 case IBMVNIC_50GBPS:
5055 adapter->speed = SPEED_50000;
5056 break;
5057 case IBMVNIC_100GBPS:
5058 adapter->speed = SPEED_100000;
5059 break;
Lijun Panb9cd7952020-09-27 19:06:25 -05005060 case IBMVNIC_200GBPS:
5061 adapter->speed = SPEED_200000;
5062 break;
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03005063 default:
Murilo Fossa Vicentinidd0f9d82019-09-16 11:50:37 -03005064 if (netif_carrier_ok(netdev))
5065 netdev_warn(netdev, "Unknown speed 0x%08x\n", rspeed);
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03005066 adapter->speed = SPEED_UNKNOWN;
5067 }
5068 if (crq->query_phys_parms_rsp.flags1 & IBMVNIC_FULL_DUPLEX)
5069 adapter->duplex = DUPLEX_FULL;
5070 else if (crq->query_phys_parms_rsp.flags1 & IBMVNIC_HALF_DUPLEX)
5071 adapter->duplex = DUPLEX_HALF;
5072 else
5073 adapter->duplex = DUPLEX_UNKNOWN;
5074
5075 return rc;
5076}
5077
Thomas Falcon032c5e82015-12-21 11:26:06 -06005078static void ibmvnic_handle_crq(union ibmvnic_crq *crq,
5079 struct ibmvnic_adapter *adapter)
5080{
5081 struct ibmvnic_generic_crq *gen_crq = &crq->generic;
5082 struct net_device *netdev = adapter->netdev;
5083 struct device *dev = &adapter->vdev->dev;
Murilo Fossa Vicentini993a82b2017-04-19 13:44:35 -04005084 u64 *u64_crq = (u64 *)crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005085 long rc;
5086
5087 netdev_dbg(netdev, "Handling CRQ: %016lx %016lx\n",
Lijun Pan429aa362021-02-11 00:43:18 -06005088 (unsigned long)cpu_to_be64(u64_crq[0]),
5089 (unsigned long)cpu_to_be64(u64_crq[1]));
Thomas Falcon032c5e82015-12-21 11:26:06 -06005090 switch (gen_crq->first) {
5091 case IBMVNIC_CRQ_INIT_RSP:
5092 switch (gen_crq->cmd) {
5093 case IBMVNIC_CRQ_INIT:
5094 dev_info(dev, "Partner initialized\n");
John Allen017892c12017-05-26 10:30:19 -04005095 adapter->from_passive_init = true;
Sukadev Bhattiprolu76cdc5c2020-11-25 18:04:29 -06005096 /* Discard any stale login responses from prev reset.
5097 * CHECK: should we clear even on INIT_COMPLETE?
5098 */
5099 adapter->login_pending = false;
5100
Thomas Falcon17c87052018-05-23 13:37:58 -05005101 if (!completion_done(&adapter->init_done)) {
5102 complete(&adapter->init_done);
5103 adapter->init_done_rc = -EIO;
5104 }
Cristobal Forno53f8b1b2021-06-10 11:08:35 -06005105
5106 if (adapter->state == VNIC_DOWN)
5107 rc = ibmvnic_reset(adapter, VNIC_RESET_PASSIVE_INIT);
5108 else
5109 rc = ibmvnic_reset(adapter, VNIC_RESET_FAILOVER);
5110
Sukadev Bhattiproluef66a1e2021-02-02 21:08:02 -08005111 if (rc && rc != -EBUSY) {
5112 /* We were unable to schedule the failover
5113 * reset either because the adapter was still
5114 * probing (eg: during kexec) or we could not
5115 * allocate memory. Clear the failover_pending
5116 * flag since no one else will. We ignore
5117 * EBUSY because it means either FAILOVER reset
5118 * is already scheduled or the adapter is
5119 * being removed.
5120 */
5121 netdev_err(netdev,
5122 "Error %ld scheduling failover reset\n",
5123 rc);
5124 adapter->failover_pending = false;
5125 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06005126 break;
5127 case IBMVNIC_CRQ_INIT_COMPLETE:
5128 dev_info(dev, "Partner initialization complete\n");
Thomas Falcon51536982018-05-23 13:37:56 -05005129 adapter->crq.active = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005130 send_version_xchg(adapter);
5131 break;
5132 default:
5133 dev_err(dev, "Unknown crq cmd: %d\n", gen_crq->cmd);
5134 }
5135 return;
5136 case IBMVNIC_CRQ_XPORT_EVENT:
Nathan Fontenoted651a12017-05-03 14:04:38 -04005137 netif_carrier_off(netdev);
Thomas Falcon51536982018-05-23 13:37:56 -05005138 adapter->crq.active = false;
Thomas Falcon2147e3d2019-11-25 17:12:54 -06005139 /* terminate any thread waiting for a response
5140 * from the device
5141 */
5142 if (!completion_done(&adapter->fw_done)) {
5143 adapter->fw_done_rc = -EIO;
5144 complete(&adapter->fw_done);
5145 }
5146 if (!completion_done(&adapter->stats_done))
5147 complete(&adapter->stats_done);
Juliet Kim7ed5b312019-09-20 16:11:23 -04005148 if (test_bit(0, &adapter->resetting))
Thomas Falcon2770a792018-05-23 13:38:02 -05005149 adapter->force_reset_recovery = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005150 if (gen_crq->cmd == IBMVNIC_PARTITION_MIGRATED) {
Nathan Fontenoted651a12017-05-03 14:04:38 -04005151 dev_info(dev, "Migrated, re-enabling adapter\n");
5152 ibmvnic_reset(adapter, VNIC_RESET_MOBILITY);
Thomas Falcondfad09a2016-08-18 11:37:51 -05005153 } else if (gen_crq->cmd == IBMVNIC_DEVICE_FAILOVER) {
5154 dev_info(dev, "Backing device failover detected\n");
Thomas Falcon5a18e1e2018-04-06 18:37:05 -05005155 adapter->failover_pending = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005156 } else {
5157 /* The adapter lost the connection */
5158 dev_err(dev, "Virtual Adapter failed (rc=%d)\n",
5159 gen_crq->cmd);
Nathan Fontenoted651a12017-05-03 14:04:38 -04005160 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005161 }
5162 return;
5163 case IBMVNIC_CRQ_CMD_RSP:
5164 break;
5165 default:
5166 dev_err(dev, "Got an invalid msg type 0x%02x\n",
5167 gen_crq->first);
5168 return;
5169 }
5170
5171 switch (gen_crq->cmd) {
5172 case VERSION_EXCHANGE_RSP:
5173 rc = crq->version_exchange_rsp.rc.code;
5174 if (rc) {
5175 dev_err(dev, "Error %ld in VERSION_EXCHG_RSP\n", rc);
5176 break;
5177 }
Thomas Falcon78468892020-05-28 11:19:17 -05005178 ibmvnic_version =
Thomas Falcon032c5e82015-12-21 11:26:06 -06005179 be16_to_cpu(crq->version_exchange_rsp.version);
Thomas Falcon78468892020-05-28 11:19:17 -05005180 dev_info(dev, "Partner protocol version is %d\n",
5181 ibmvnic_version);
Lijun Pan491099a2020-09-27 20:13:26 -05005182 send_query_cap(adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005183 break;
5184 case QUERY_CAPABILITY_RSP:
5185 handle_query_cap_rsp(crq, adapter);
5186 break;
5187 case QUERY_MAP_RSP:
5188 handle_query_map_rsp(crq, adapter);
5189 break;
5190 case REQUEST_MAP_RSP:
Thomas Falconf3be0cb2017-06-21 14:53:01 -05005191 adapter->fw_done_rc = crq->request_map_rsp.rc.code;
5192 complete(&adapter->fw_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005193 break;
5194 case REQUEST_UNMAP_RSP:
5195 handle_request_unmap_rsp(crq, adapter);
5196 break;
5197 case REQUEST_CAPABILITY_RSP:
5198 handle_request_cap_rsp(crq, adapter);
5199 break;
5200 case LOGIN_RSP:
5201 netdev_dbg(netdev, "Got Login Response\n");
5202 handle_login_rsp(crq, adapter);
5203 break;
5204 case LOGICAL_LINK_STATE_RSP:
Nathan Fontenot53da09e2017-04-21 15:39:04 -04005205 netdev_dbg(netdev,
5206 "Got Logical Link State Response, state: %d rc: %d\n",
5207 crq->logical_link_state_rsp.link_state,
5208 crq->logical_link_state_rsp.rc.code);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005209 adapter->logical_link_state =
5210 crq->logical_link_state_rsp.link_state;
Nathan Fontenot53da09e2017-04-21 15:39:04 -04005211 adapter->init_done_rc = crq->logical_link_state_rsp.rc.code;
5212 complete(&adapter->init_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005213 break;
5214 case LINK_STATE_INDICATION:
5215 netdev_dbg(netdev, "Got Logical Link State Indication\n");
5216 adapter->phys_link_state =
5217 crq->link_state_indication.phys_link_state;
5218 adapter->logical_link_state =
5219 crq->link_state_indication.logical_link_state;
Thomas Falcon0655f992019-05-09 23:13:44 -05005220 if (adapter->phys_link_state && adapter->logical_link_state)
5221 netif_carrier_on(netdev);
5222 else
5223 netif_carrier_off(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005224 break;
5225 case CHANGE_MAC_ADDR_RSP:
5226 netdev_dbg(netdev, "Got MAC address change Response\n");
Thomas Falconf8136142018-01-29 13:45:05 -06005227 adapter->fw_done_rc = handle_change_mac_rsp(crq, adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005228 break;
5229 case ERROR_INDICATION:
5230 netdev_dbg(netdev, "Got Error Indication\n");
5231 handle_error_indication(crq, adapter);
5232 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005233 case REQUEST_STATISTICS_RSP:
5234 netdev_dbg(netdev, "Got Statistics Response\n");
5235 complete(&adapter->stats_done);
5236 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005237 case QUERY_IP_OFFLOAD_RSP:
5238 netdev_dbg(netdev, "Got Query IP offload Response\n");
5239 handle_query_ip_offload_rsp(adapter);
5240 break;
5241 case MULTICAST_CTRL_RSP:
5242 netdev_dbg(netdev, "Got multicast control Response\n");
5243 break;
5244 case CONTROL_IP_OFFLOAD_RSP:
5245 netdev_dbg(netdev, "Got Control IP offload Response\n");
5246 dma_unmap_single(dev, adapter->ip_offload_ctrl_tok,
5247 sizeof(adapter->ip_offload_ctrl),
5248 DMA_TO_DEVICE);
John Allenbd0b6722017-03-17 17:13:40 -05005249 complete(&adapter->init_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005250 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005251 case COLLECT_FW_TRACE_RSP:
5252 netdev_dbg(netdev, "Got Collect firmware trace Response\n");
5253 complete(&adapter->fw_done);
5254 break;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02005255 case GET_VPD_SIZE_RSP:
5256 handle_vpd_size_rsp(crq, adapter);
5257 break;
5258 case GET_VPD_RSP:
5259 handle_vpd_rsp(crq, adapter);
5260 break;
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03005261 case QUERY_PHYS_PARMS_RSP:
5262 adapter->fw_done_rc = handle_query_phys_parms_rsp(crq, adapter);
5263 complete(&adapter->fw_done);
5264 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005265 default:
5266 netdev_err(netdev, "Got an invalid cmd type 0x%02x\n",
5267 gen_crq->cmd);
5268 }
5269}
5270
5271static irqreturn_t ibmvnic_interrupt(int irq, void *instance)
5272{
5273 struct ibmvnic_adapter *adapter = instance;
Thomas Falcon6c267b32017-02-15 12:17:58 -06005274
Thomas Falcon6c267b32017-02-15 12:17:58 -06005275 tasklet_schedule(&adapter->tasklet);
Thomas Falcon6c267b32017-02-15 12:17:58 -06005276 return IRQ_HANDLED;
5277}
5278
Allen Paisaa7c3fe2020-09-14 12:59:29 +05305279static void ibmvnic_tasklet(struct tasklet_struct *t)
Thomas Falcon6c267b32017-02-15 12:17:58 -06005280{
Allen Paisaa7c3fe2020-09-14 12:59:29 +05305281 struct ibmvnic_adapter *adapter = from_tasklet(adapter, t, tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005282 struct ibmvnic_crq_queue *queue = &adapter->crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005283 union ibmvnic_crq *crq;
5284 unsigned long flags;
5285 bool done = false;
5286
5287 spin_lock_irqsave(&queue->lock, flags);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005288 while (!done) {
5289 /* Pull all the valid messages off the CRQ */
5290 while ((crq = ibmvnic_next_crq(adapter)) != NULL) {
Lijun Pane41aec72021-01-27 19:34:42 -06005291 /* This barrier makes sure ibmvnic_next_crq()'s
5292 * crq->generic.first & IBMVNIC_CRQ_CMD_RSP is loaded
5293 * before ibmvnic_handle_crq()'s
5294 * switch(gen_crq->first) and switch(gen_crq->cmd).
5295 */
5296 dma_rmb();
Thomas Falcon032c5e82015-12-21 11:26:06 -06005297 ibmvnic_handle_crq(crq, adapter);
5298 crq->generic.first = 0;
5299 }
Brian Kinged7ecbf2017-04-19 13:44:53 -04005300
5301 /* remain in tasklet until all
5302 * capabilities responses are received
5303 */
5304 if (!adapter->wait_capability)
5305 done = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005306 }
Thomas Falcon249168a2017-02-15 12:18:00 -06005307 /* if capabilities CRQ's were sent in this tasklet, the following
5308 * tasklet must wait until all responses are received
5309 */
5310 if (atomic_read(&adapter->running_cap_crqs) != 0)
5311 adapter->wait_capability = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005312 spin_unlock_irqrestore(&queue->lock, flags);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005313}
5314
5315static int ibmvnic_reenable_crq_queue(struct ibmvnic_adapter *adapter)
5316{
5317 struct vio_dev *vdev = adapter->vdev;
5318 int rc;
5319
5320 do {
5321 rc = plpar_hcall_norets(H_ENABLE_CRQ, vdev->unit_address);
5322 } while (rc == H_IN_PROGRESS || rc == H_BUSY || H_IS_LONG_BUSY(rc));
5323
5324 if (rc)
5325 dev_err(&vdev->dev, "Error enabling adapter (rc=%d)\n", rc);
5326
5327 return rc;
5328}
5329
5330static int ibmvnic_reset_crq(struct ibmvnic_adapter *adapter)
5331{
5332 struct ibmvnic_crq_queue *crq = &adapter->crq;
5333 struct device *dev = &adapter->vdev->dev;
5334 struct vio_dev *vdev = adapter->vdev;
5335 int rc;
5336
5337 /* Close the CRQ */
5338 do {
5339 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
5340 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
5341
5342 /* Clean out the queue */
Lijun Pan0e435be2020-11-23 13:35:46 -06005343 if (!crq->msgs)
5344 return -EINVAL;
5345
Thomas Falcon032c5e82015-12-21 11:26:06 -06005346 memset(crq->msgs, 0, PAGE_SIZE);
5347 crq->cur = 0;
Thomas Falcon51536982018-05-23 13:37:56 -05005348 crq->active = false;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005349
5350 /* And re-open it again */
5351 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
5352 crq->msg_token, PAGE_SIZE);
5353
5354 if (rc == H_CLOSED)
5355 /* Adapter is good, but other end is not ready */
5356 dev_warn(dev, "Partner adapter not ready\n");
5357 else if (rc != 0)
5358 dev_warn(dev, "Couldn't register crq (rc=%d)\n", rc);
5359
5360 return rc;
5361}
5362
Nathan Fontenotf9928872017-03-30 02:48:54 -04005363static void release_crq_queue(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06005364{
5365 struct ibmvnic_crq_queue *crq = &adapter->crq;
5366 struct vio_dev *vdev = adapter->vdev;
5367 long rc;
5368
Nathan Fontenotf9928872017-03-30 02:48:54 -04005369 if (!crq->msgs)
5370 return;
5371
Thomas Falcon032c5e82015-12-21 11:26:06 -06005372 netdev_dbg(adapter->netdev, "Releasing CRQ\n");
5373 free_irq(vdev->irq, adapter);
Thomas Falcon6c267b32017-02-15 12:17:58 -06005374 tasklet_kill(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005375 do {
5376 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
5377 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
5378
5379 dma_unmap_single(&vdev->dev, crq->msg_token, PAGE_SIZE,
5380 DMA_BIDIRECTIONAL);
5381 free_page((unsigned long)crq->msgs);
Nathan Fontenotf9928872017-03-30 02:48:54 -04005382 crq->msgs = NULL;
Thomas Falcon51536982018-05-23 13:37:56 -05005383 crq->active = false;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005384}
5385
Nathan Fontenotf9928872017-03-30 02:48:54 -04005386static int init_crq_queue(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06005387{
5388 struct ibmvnic_crq_queue *crq = &adapter->crq;
5389 struct device *dev = &adapter->vdev->dev;
5390 struct vio_dev *vdev = adapter->vdev;
5391 int rc, retrc = -ENOMEM;
5392
Nathan Fontenotf9928872017-03-30 02:48:54 -04005393 if (crq->msgs)
5394 return 0;
5395
Thomas Falcon032c5e82015-12-21 11:26:06 -06005396 crq->msgs = (union ibmvnic_crq *)get_zeroed_page(GFP_KERNEL);
5397 /* Should we allocate more than one page? */
5398
5399 if (!crq->msgs)
5400 return -ENOMEM;
5401
5402 crq->size = PAGE_SIZE / sizeof(*crq->msgs);
5403 crq->msg_token = dma_map_single(dev, crq->msgs, PAGE_SIZE,
5404 DMA_BIDIRECTIONAL);
5405 if (dma_mapping_error(dev, crq->msg_token))
5406 goto map_failed;
5407
5408 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
5409 crq->msg_token, PAGE_SIZE);
5410
5411 if (rc == H_RESOURCE)
5412 /* maybe kexecing and resource is busy. try a reset */
5413 rc = ibmvnic_reset_crq(adapter);
5414 retrc = rc;
5415
5416 if (rc == H_CLOSED) {
5417 dev_warn(dev, "Partner adapter not ready\n");
5418 } else if (rc) {
5419 dev_warn(dev, "Error %d opening adapter\n", rc);
5420 goto reg_crq_failed;
5421 }
5422
5423 retrc = 0;
5424
Allen Paisaa7c3fe2020-09-14 12:59:29 +05305425 tasklet_setup(&adapter->tasklet, (void *)ibmvnic_tasklet);
Thomas Falcon6c267b32017-02-15 12:17:58 -06005426
Thomas Falcon032c5e82015-12-21 11:26:06 -06005427 netdev_dbg(adapter->netdev, "registering irq 0x%x\n", vdev->irq);
Murilo Fossa Vicentinie56e2512019-04-25 11:02:33 -03005428 snprintf(crq->name, sizeof(crq->name), "ibmvnic-%x",
5429 adapter->vdev->unit_address);
5430 rc = request_irq(vdev->irq, ibmvnic_interrupt, 0, crq->name, adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005431 if (rc) {
5432 dev_err(dev, "Couldn't register irq 0x%x. rc=%d\n",
5433 vdev->irq, rc);
5434 goto req_irq_failed;
5435 }
5436
5437 rc = vio_enable_interrupts(vdev);
5438 if (rc) {
5439 dev_err(dev, "Error %d enabling interrupts\n", rc);
5440 goto req_irq_failed;
5441 }
5442
5443 crq->cur = 0;
5444 spin_lock_init(&crq->lock);
5445
5446 return retrc;
5447
5448req_irq_failed:
Thomas Falcon6c267b32017-02-15 12:17:58 -06005449 tasklet_kill(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005450 do {
5451 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
5452 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
5453reg_crq_failed:
5454 dma_unmap_single(dev, crq->msg_token, PAGE_SIZE, DMA_BIDIRECTIONAL);
5455map_failed:
5456 free_page((unsigned long)crq->msgs);
Nathan Fontenotf9928872017-03-30 02:48:54 -04005457 crq->msgs = NULL;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005458 return retrc;
5459}
5460
Lijun Pan635e4422020-08-19 17:52:26 -05005461static int ibmvnic_reset_init(struct ibmvnic_adapter *adapter, bool reset)
John Allenf6ef6402017-03-17 17:13:42 -05005462{
5463 struct device *dev = &adapter->vdev->dev;
Dany Madden98c41f02020-11-25 18:04:32 -06005464 unsigned long timeout = msecs_to_jiffies(20000);
Michal Suchanek6881b072021-03-02 20:47:47 +01005465 u64 old_num_rx_queues = adapter->req_rx_queues;
5466 u64 old_num_tx_queues = adapter->req_tx_queues;
John Allenf6ef6402017-03-17 17:13:42 -05005467 int rc;
5468
John Allen017892c12017-05-26 10:30:19 -04005469 adapter->from_passive_init = false;
5470
Michal Suchanek6881b072021-03-02 20:47:47 +01005471 if (reset)
Lijun Pan635e4422020-08-19 17:52:26 -05005472 reinit_completion(&adapter->init_done);
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06005473
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04005474 adapter->init_done_rc = 0;
Lijun Panfa68bfa2020-08-19 17:52:24 -05005475 rc = ibmvnic_send_crq_init(adapter);
5476 if (rc) {
5477 dev_err(dev, "Send crq init failed with error %d\n", rc);
5478 return rc;
5479 }
5480
John Allenf6ef6402017-03-17 17:13:42 -05005481 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
5482 dev_err(dev, "Initialization sequence timed out\n");
John Allen017892c12017-05-26 10:30:19 -04005483 return -1;
5484 }
5485
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04005486 if (adapter->init_done_rc) {
5487 release_crq_queue(adapter);
5488 return adapter->init_done_rc;
5489 }
5490
Lijun Pan785a2b12020-09-17 21:12:46 -05005491 if (adapter->from_passive_init) {
5492 adapter->state = VNIC_OPEN;
5493 adapter->from_passive_init = false;
5494 return -1;
5495 }
5496
Lijun Pan635e4422020-08-19 17:52:26 -05005497 if (reset &&
5498 test_bit(0, &adapter->resetting) && !adapter->wait_for_reset &&
Nathan Fontenot30f79622018-04-06 18:37:06 -05005499 adapter->reset_reason != VNIC_RESET_MOBILITY) {
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06005500 if (adapter->req_rx_queues != old_num_rx_queues ||
5501 adapter->req_tx_queues != old_num_tx_queues) {
5502 release_sub_crqs(adapter, 0);
5503 rc = init_sub_crqs(adapter);
5504 } else {
5505 rc = reset_sub_crq_queues(adapter);
5506 }
5507 } else {
Nathan Fontenot57a49432017-05-26 10:31:12 -04005508 rc = init_sub_crqs(adapter);
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06005509 }
5510
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04005511 if (rc) {
5512 dev_err(dev, "Initialization of sub crqs failed\n");
5513 release_crq_queue(adapter);
Thomas Falcon5df969c2017-06-28 19:55:54 -05005514 return rc;
5515 }
5516
5517 rc = init_sub_crq_irqs(adapter);
5518 if (rc) {
5519 dev_err(dev, "Failed to initialize sub crq irqs\n");
5520 release_crq_queue(adapter);
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04005521 }
5522
5523 return rc;
John Allenf6ef6402017-03-17 17:13:42 -05005524}
5525
Thomas Falcon40c9db82017-06-12 12:35:04 -05005526static struct device_attribute dev_attr_failover;
5527
Thomas Falcon032c5e82015-12-21 11:26:06 -06005528static int ibmvnic_probe(struct vio_dev *dev, const struct vio_device_id *id)
5529{
5530 struct ibmvnic_adapter *adapter;
5531 struct net_device *netdev;
5532 unsigned char *mac_addr_p;
Cristobal Forno53f8b1b2021-06-10 11:08:35 -06005533 bool init_success;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005534 int rc;
5535
5536 dev_dbg(&dev->dev, "entering ibmvnic_probe for UA 0x%x\n",
5537 dev->unit_address);
5538
5539 mac_addr_p = (unsigned char *)vio_get_attribute(dev,
5540 VETH_MAC_ADDR, NULL);
5541 if (!mac_addr_p) {
5542 dev_err(&dev->dev,
5543 "(%s:%3.3d) ERROR: Can't find MAC_ADDR attribute\n",
5544 __FILE__, __LINE__);
5545 return 0;
5546 }
5547
5548 netdev = alloc_etherdev_mq(sizeof(struct ibmvnic_adapter),
Thomas Falcond45cc3a2017-12-18 12:52:11 -06005549 IBMVNIC_MAX_QUEUES);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005550 if (!netdev)
5551 return -ENOMEM;
5552
5553 adapter = netdev_priv(netdev);
Nathan Fontenot90c80142017-05-03 14:04:32 -04005554 adapter->state = VNIC_PROBING;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005555 dev_set_drvdata(&dev->dev, netdev);
5556 adapter->vdev = dev;
5557 adapter->netdev = netdev;
Sukadev Bhattiprolu76cdc5c2020-11-25 18:04:29 -06005558 adapter->login_pending = false;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005559
5560 ether_addr_copy(adapter->mac_addr, mac_addr_p);
5561 ether_addr_copy(netdev->dev_addr, adapter->mac_addr);
5562 netdev->irq = dev->irq;
5563 netdev->netdev_ops = &ibmvnic_netdev_ops;
5564 netdev->ethtool_ops = &ibmvnic_ethtool_ops;
5565 SET_NETDEV_DEV(netdev, &dev->dev);
5566
Nathan Fontenoted651a12017-05-03 14:04:38 -04005567 INIT_WORK(&adapter->ibmvnic_reset, __ibmvnic_reset);
Juliet Kim7ed5b312019-09-20 16:11:23 -04005568 INIT_DELAYED_WORK(&adapter->ibmvnic_delayed_reset,
5569 __ibmvnic_delayed_reset);
Nathan Fontenoted651a12017-05-03 14:04:38 -04005570 INIT_LIST_HEAD(&adapter->rwi_list);
Thomas Falcon6c5c7482018-12-10 15:22:22 -06005571 spin_lock_init(&adapter->rwi_lock);
Juliet Kim7d7195a2020-03-10 09:23:58 -05005572 spin_lock_init(&adapter->state_lock);
Thomas Falconff25dcb2019-11-25 17:12:56 -06005573 mutex_init(&adapter->fw_lock);
Thomas Falconbbd669a2019-04-04 18:58:26 -05005574 init_completion(&adapter->init_done);
Thomas Falcon070eca92019-11-25 17:12:53 -06005575 init_completion(&adapter->fw_done);
5576 init_completion(&adapter->reset_done);
5577 init_completion(&adapter->stats_done);
Juliet Kim7ed5b312019-09-20 16:11:23 -04005578 clear_bit(0, &adapter->resetting);
Nathan Fontenoted651a12017-05-03 14:04:38 -04005579
Cristobal Forno53f8b1b2021-06-10 11:08:35 -06005580 init_success = false;
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04005581 do {
Nathan Fontenot30f79622018-04-06 18:37:06 -05005582 rc = init_crq_queue(adapter);
5583 if (rc) {
5584 dev_err(&dev->dev, "Couldn't initialize crq. rc=%d\n",
5585 rc);
5586 goto ibmvnic_init_fail;
5587 }
5588
Lijun Pan635e4422020-08-19 17:52:26 -05005589 rc = ibmvnic_reset_init(adapter, false);
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04005590 } while (rc == EAGAIN);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005591
Cristobal Forno53f8b1b2021-06-10 11:08:35 -06005592 /* We are ignoring the error from ibmvnic_reset_init() assuming that the
5593 * partner is not ready. CRQ is not active. When the partner becomes
5594 * ready, we will do the passive init reset.
5595 */
5596
5597 if (!rc)
5598 init_success = true;
5599
Thomas Falcon07184212018-05-16 15:49:05 -05005600 rc = init_stats_buffers(adapter);
5601 if (rc)
5602 goto ibmvnic_init_fail;
5603
5604 rc = init_stats_token(adapter);
5605 if (rc)
5606 goto ibmvnic_stats_fail;
5607
Thomas Falcon40c9db82017-06-12 12:35:04 -05005608 rc = device_create_file(&dev->dev, &dev_attr_failover);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05005609 if (rc)
Thomas Falcon07184212018-05-16 15:49:05 -05005610 goto ibmvnic_dev_file_err;
Thomas Falcon40c9db82017-06-12 12:35:04 -05005611
Mick Tarsele876a8a2017-09-28 13:53:18 -07005612 netif_carrier_off(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005613 rc = register_netdev(netdev);
5614 if (rc) {
5615 dev_err(&dev->dev, "failed to register netdev rc=%d\n", rc);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05005616 goto ibmvnic_register_fail;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005617 }
5618 dev_info(&dev->dev, "ibmvnic registered\n");
5619
Cristobal Forno53f8b1b2021-06-10 11:08:35 -06005620 if (init_success) {
5621 adapter->state = VNIC_PROBED;
5622 netdev->mtu = adapter->req_mtu - ETH_HLEN;
5623 netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
5624 netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
5625 } else {
5626 adapter->state = VNIC_DOWN;
5627 }
John Allenc26eba02017-10-26 16:23:25 -05005628
5629 adapter->wait_for_reset = false;
Dany Maddena86d5c62020-11-25 18:04:31 -06005630 adapter->last_reset_time = jiffies;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005631 return 0;
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05005632
5633ibmvnic_register_fail:
5634 device_remove_file(&dev->dev, &dev_attr_failover);
5635
Thomas Falcon07184212018-05-16 15:49:05 -05005636ibmvnic_dev_file_err:
5637 release_stats_token(adapter);
5638
5639ibmvnic_stats_fail:
5640 release_stats_buffers(adapter);
5641
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05005642ibmvnic_init_fail:
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06005643 release_sub_crqs(adapter, 1);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05005644 release_crq_queue(adapter);
Thomas Falconff25dcb2019-11-25 17:12:56 -06005645 mutex_destroy(&adapter->fw_lock);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05005646 free_netdev(netdev);
5647
5648 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005649}
5650
Uwe Kleine-König386a9662021-02-25 23:18:34 +01005651static void ibmvnic_remove(struct vio_dev *dev)
Thomas Falcon032c5e82015-12-21 11:26:06 -06005652{
5653 struct net_device *netdev = dev_get_drvdata(&dev->dev);
Nathan Fontenot37489052017-04-19 13:45:04 -04005654 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Juliet Kim7d7195a2020-03-10 09:23:58 -05005655 unsigned long flags;
5656
5657 spin_lock_irqsave(&adapter->state_lock, flags);
Sukadev Bhattiprolu4a41c422021-02-12 20:42:50 -08005658
5659 /* If ibmvnic_reset() is scheduling a reset, wait for it to
5660 * finish. Then, set the state to REMOVING to prevent it from
5661 * scheduling any more work and to have reset functions ignore
5662 * any resets that have already been scheduled. Drop the lock
5663 * after setting state, so __ibmvnic_reset() which is called
5664 * from the flush_work() below, can make progress.
5665 */
Junlin Yang69cdb792021-03-05 16:48:39 +08005666 spin_lock(&adapter->rwi_lock);
Nathan Fontenot90c80142017-05-03 14:04:32 -04005667 adapter->state = VNIC_REMOVING;
Junlin Yang69cdb792021-03-05 16:48:39 +08005668 spin_unlock(&adapter->rwi_lock);
Sukadev Bhattiprolu4a41c422021-02-12 20:42:50 -08005669
Juliet Kim7d7195a2020-03-10 09:23:58 -05005670 spin_unlock_irqrestore(&adapter->state_lock, flags);
5671
Thomas Falcon6954a9e2020-06-12 13:34:41 -05005672 flush_work(&adapter->ibmvnic_reset);
5673 flush_delayed_work(&adapter->ibmvnic_delayed_reset);
5674
Juliet Kima5681e22018-11-19 15:59:22 -06005675 rtnl_lock();
5676 unregister_netdevice(netdev);
Nathan Fontenot37489052017-04-19 13:45:04 -04005677
5678 release_resources(adapter);
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06005679 release_sub_crqs(adapter, 1);
Nathan Fontenot37489052017-04-19 13:45:04 -04005680 release_crq_queue(adapter);
5681
Thomas Falcon53cc7722018-02-26 18:10:56 -06005682 release_stats_token(adapter);
5683 release_stats_buffers(adapter);
5684
Nathan Fontenot90c80142017-05-03 14:04:32 -04005685 adapter->state = VNIC_REMOVED;
5686
Juliet Kima5681e22018-11-19 15:59:22 -06005687 rtnl_unlock();
Thomas Falconff25dcb2019-11-25 17:12:56 -06005688 mutex_destroy(&adapter->fw_lock);
Thomas Falcon40c9db82017-06-12 12:35:04 -05005689 device_remove_file(&dev->dev, &dev_attr_failover);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005690 free_netdev(netdev);
5691 dev_set_drvdata(&dev->dev, NULL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005692}
5693
Thomas Falcon40c9db82017-06-12 12:35:04 -05005694static ssize_t failover_store(struct device *dev, struct device_attribute *attr,
5695 const char *buf, size_t count)
5696{
5697 struct net_device *netdev = dev_get_drvdata(dev);
5698 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
5699 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
5700 __be64 session_token;
5701 long rc;
5702
5703 if (!sysfs_streq(buf, "1"))
5704 return -EINVAL;
5705
5706 rc = plpar_hcall(H_VIOCTL, retbuf, adapter->vdev->unit_address,
5707 H_GET_SESSION_TOKEN, 0, 0, 0);
5708 if (rc) {
5709 netdev_err(netdev, "Couldn't retrieve session token, rc %ld\n",
5710 rc);
Lijun Pan334c4242021-04-13 03:31:44 -05005711 goto last_resort;
Thomas Falcon40c9db82017-06-12 12:35:04 -05005712 }
5713
5714 session_token = (__be64)retbuf[0];
5715 netdev_dbg(netdev, "Initiating client failover, session id %llx\n",
5716 be64_to_cpu(session_token));
5717 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
5718 H_SESSION_ERR_DETECTED, session_token, 0, 0);
Lijun Pan334c4242021-04-13 03:31:44 -05005719 if (rc)
5720 netdev_err(netdev,
5721 "H_VIOCTL initiated failover failed, rc %ld\n",
Thomas Falcon40c9db82017-06-12 12:35:04 -05005722 rc);
Lijun Pan334c4242021-04-13 03:31:44 -05005723
5724last_resort:
5725 netdev_dbg(netdev, "Trying to send CRQ_CMD, the last resort\n");
5726 ibmvnic_reset(adapter, VNIC_RESET_FAILOVER);
Thomas Falcon40c9db82017-06-12 12:35:04 -05005727
5728 return count;
5729}
Joe Perches6cbaefb2017-12-19 10:15:09 -08005730static DEVICE_ATTR_WO(failover);
Thomas Falcon40c9db82017-06-12 12:35:04 -05005731
Thomas Falcon032c5e82015-12-21 11:26:06 -06005732static unsigned long ibmvnic_get_desired_dma(struct vio_dev *vdev)
5733{
5734 struct net_device *netdev = dev_get_drvdata(&vdev->dev);
5735 struct ibmvnic_adapter *adapter;
5736 struct iommu_table *tbl;
5737 unsigned long ret = 0;
5738 int i;
5739
5740 tbl = get_iommu_table_base(&vdev->dev);
5741
5742 /* netdev inits at probe time along with the structures we need below*/
5743 if (!netdev)
5744 return IOMMU_PAGE_ALIGN(IBMVNIC_IO_ENTITLEMENT_DEFAULT, tbl);
5745
5746 adapter = netdev_priv(netdev);
5747
5748 ret += PAGE_SIZE; /* the crq message queue */
Thomas Falcon032c5e82015-12-21 11:26:06 -06005749 ret += IOMMU_PAGE_ALIGN(sizeof(struct ibmvnic_statistics), tbl);
5750
5751 for (i = 0; i < adapter->req_tx_queues + adapter->req_rx_queues; i++)
5752 ret += 4 * PAGE_SIZE; /* the scrq message queue */
5753
Thomas Falcon507ebe62020-08-21 13:39:01 -05005754 for (i = 0; i < adapter->num_active_rx_pools; i++)
Thomas Falcon032c5e82015-12-21 11:26:06 -06005755 ret += adapter->rx_pool[i].size *
5756 IOMMU_PAGE_ALIGN(adapter->rx_pool[i].buff_size, tbl);
5757
5758 return ret;
5759}
5760
5761static int ibmvnic_resume(struct device *dev)
5762{
5763 struct net_device *netdev = dev_get_drvdata(dev);
5764 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005765
John Allencb89ba22017-06-19 11:27:53 -05005766 if (adapter->state != VNIC_OPEN)
5767 return 0;
5768
John Allena2488782017-07-24 13:26:06 -05005769 tasklet_schedule(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005770
5771 return 0;
5772}
5773
Arvind Yadav8c37bc62017-08-17 18:52:54 +05305774static const struct vio_device_id ibmvnic_device_table[] = {
Thomas Falcon032c5e82015-12-21 11:26:06 -06005775 {"network", "IBM,vnic"},
5776 {"", "" }
5777};
5778MODULE_DEVICE_TABLE(vio, ibmvnic_device_table);
5779
5780static const struct dev_pm_ops ibmvnic_pm_ops = {
5781 .resume = ibmvnic_resume
5782};
5783
5784static struct vio_driver ibmvnic_driver = {
5785 .id_table = ibmvnic_device_table,
5786 .probe = ibmvnic_probe,
5787 .remove = ibmvnic_remove,
5788 .get_desired_dma = ibmvnic_get_desired_dma,
5789 .name = ibmvnic_driver_name,
5790 .pm = &ibmvnic_pm_ops,
5791};
5792
5793/* module functions */
5794static int __init ibmvnic_module_init(void)
5795{
5796 pr_info("%s: %s %s\n", ibmvnic_driver_name, ibmvnic_driver_string,
5797 IBMVNIC_DRIVER_VERSION);
5798
5799 return vio_register_driver(&ibmvnic_driver);
5800}
5801
5802static void __exit ibmvnic_module_exit(void)
5803{
5804 vio_unregister_driver(&ibmvnic_driver);
5805}
5806
5807module_init(ibmvnic_module_init);
5808module_exit(ibmvnic_module_exit);