blob: 59536bd5cab1bd19d437e8e696fb728b0b691b2d [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);
Sukadev Bhattiproluf8ac0bf2021-09-14 20:52:57 -0700111static void free_long_term_buff(struct ibmvnic_adapter *adapter,
112 struct ibmvnic_long_term_buff *ltb);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600113
114struct ibmvnic_stat {
115 char name[ETH_GSTRING_LEN];
116 int offset;
117};
118
119#define IBMVNIC_STAT_OFF(stat) (offsetof(struct ibmvnic_adapter, stats) + \
120 offsetof(struct ibmvnic_statistics, stat))
Lijun Pan91dc5d22021-02-11 00:43:22 -0600121#define IBMVNIC_GET_STAT(a, off) (*((u64 *)(((unsigned long)(a)) + (off))))
Thomas Falcon032c5e82015-12-21 11:26:06 -0600122
123static const struct ibmvnic_stat ibmvnic_stats[] = {
124 {"rx_packets", IBMVNIC_STAT_OFF(rx_packets)},
125 {"rx_bytes", IBMVNIC_STAT_OFF(rx_bytes)},
126 {"tx_packets", IBMVNIC_STAT_OFF(tx_packets)},
127 {"tx_bytes", IBMVNIC_STAT_OFF(tx_bytes)},
128 {"ucast_tx_packets", IBMVNIC_STAT_OFF(ucast_tx_packets)},
129 {"ucast_rx_packets", IBMVNIC_STAT_OFF(ucast_rx_packets)},
130 {"mcast_tx_packets", IBMVNIC_STAT_OFF(mcast_tx_packets)},
131 {"mcast_rx_packets", IBMVNIC_STAT_OFF(mcast_rx_packets)},
132 {"bcast_tx_packets", IBMVNIC_STAT_OFF(bcast_tx_packets)},
133 {"bcast_rx_packets", IBMVNIC_STAT_OFF(bcast_rx_packets)},
134 {"align_errors", IBMVNIC_STAT_OFF(align_errors)},
135 {"fcs_errors", IBMVNIC_STAT_OFF(fcs_errors)},
136 {"single_collision_frames", IBMVNIC_STAT_OFF(single_collision_frames)},
137 {"multi_collision_frames", IBMVNIC_STAT_OFF(multi_collision_frames)},
138 {"sqe_test_errors", IBMVNIC_STAT_OFF(sqe_test_errors)},
139 {"deferred_tx", IBMVNIC_STAT_OFF(deferred_tx)},
140 {"late_collisions", IBMVNIC_STAT_OFF(late_collisions)},
141 {"excess_collisions", IBMVNIC_STAT_OFF(excess_collisions)},
142 {"internal_mac_tx_errors", IBMVNIC_STAT_OFF(internal_mac_tx_errors)},
143 {"carrier_sense", IBMVNIC_STAT_OFF(carrier_sense)},
144 {"too_long_frames", IBMVNIC_STAT_OFF(too_long_frames)},
145 {"internal_mac_rx_errors", IBMVNIC_STAT_OFF(internal_mac_rx_errors)},
146};
147
Cristobal Forno53f8b1b2021-06-10 11:08:35 -0600148static int send_crq_init_complete(struct ibmvnic_adapter *adapter)
149{
150 union ibmvnic_crq crq;
151
152 memset(&crq, 0, sizeof(crq));
153 crq.generic.first = IBMVNIC_CRQ_INIT_CMD;
154 crq.generic.cmd = IBMVNIC_CRQ_INIT_COMPLETE;
155
156 return ibmvnic_send_crq(adapter, &crq);
157}
158
159static int send_version_xchg(struct ibmvnic_adapter *adapter)
160{
161 union ibmvnic_crq crq;
162
163 memset(&crq, 0, sizeof(crq));
164 crq.version_exchange.first = IBMVNIC_CRQ_CMD;
165 crq.version_exchange.cmd = VERSION_EXCHANGE;
166 crq.version_exchange.version = cpu_to_be16(ibmvnic_version);
167
168 return ibmvnic_send_crq(adapter, &crq);
169}
170
Thomas Falcon032c5e82015-12-21 11:26:06 -0600171static long h_reg_sub_crq(unsigned long unit_address, unsigned long token,
172 unsigned long length, unsigned long *number,
173 unsigned long *irq)
174{
175 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
176 long rc;
177
178 rc = plpar_hcall(H_REG_SUB_CRQ, retbuf, unit_address, token, length);
179 *number = retbuf[0];
180 *irq = retbuf[1];
181
182 return rc;
183}
184
Thomas Falcon476d96c2019-11-25 17:12:55 -0600185/**
186 * ibmvnic_wait_for_completion - Check device state and wait for completion
187 * @adapter: private device data
188 * @comp_done: completion structure to wait for
189 * @timeout: time to wait in milliseconds
190 *
191 * Wait for a completion signal or until the timeout limit is reached
192 * while checking that the device is still active.
193 */
194static int ibmvnic_wait_for_completion(struct ibmvnic_adapter *adapter,
195 struct completion *comp_done,
196 unsigned long timeout)
197{
198 struct net_device *netdev;
199 unsigned long div_timeout;
200 u8 retry;
201
202 netdev = adapter->netdev;
203 retry = 5;
204 div_timeout = msecs_to_jiffies(timeout / retry);
205 while (true) {
206 if (!adapter->crq.active) {
207 netdev_err(netdev, "Device down!\n");
208 return -ENODEV;
209 }
Thomas Falcon8f9cc1e2019-12-11 09:38:39 -0600210 if (!retry--)
Thomas Falcon476d96c2019-11-25 17:12:55 -0600211 break;
212 if (wait_for_completion_timeout(comp_done, div_timeout))
213 return 0;
214 }
215 netdev_err(netdev, "Operation timed out.\n");
216 return -ETIMEDOUT;
217}
218
Sukadev Bhattiproluf8ac0bf2021-09-14 20:52:57 -0700219/**
220 * reuse_ltb() - Check if a long term buffer can be reused
221 * @ltb: The long term buffer to be checked
222 * @size: The size of the long term buffer.
223 *
224 * An LTB can be reused unless its size has changed.
225 *
226 * Return: Return true if the LTB can be reused, false otherwise.
227 */
228static bool reuse_ltb(struct ibmvnic_long_term_buff *ltb, int size)
229{
230 return (ltb->buff && ltb->size == size);
231}
232
233/**
234 * alloc_long_term_buff() - Allocate a long term buffer (LTB)
235 *
236 * @adapter: ibmvnic adapter associated to the LTB
237 * @ltb: container object for the LTB
238 * @size: size of the LTB
239 *
240 * Allocate an LTB of the specified size and notify VIOS.
241 *
242 * If the given @ltb already has the correct size, reuse it. Otherwise if
243 * its non-NULL, free it. Then allocate a new one of the correct size.
244 * Notify the VIOS either way since we may now be working with a new VIOS.
245 *
246 * Allocating larger chunks of memory during resets, specially LPM or under
247 * low memory situations can cause resets to fail/timeout and for LPAR to
248 * lose connectivity. So hold onto the LTB even if we fail to communicate
249 * with the VIOS and reuse it on next open. Free LTB when adapter is closed.
250 *
251 * Return: 0 if we were able to allocate the LTB and notify the VIOS and
252 * a negative value otherwise.
253 */
Thomas Falcon032c5e82015-12-21 11:26:06 -0600254static int alloc_long_term_buff(struct ibmvnic_adapter *adapter,
255 struct ibmvnic_long_term_buff *ltb, int size)
256{
257 struct device *dev = &adapter->vdev->dev;
Thomas Falcon9c4eaab2018-05-23 13:37:57 -0500258 int rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600259
Sukadev Bhattiproluf8ac0bf2021-09-14 20:52:57 -0700260 if (!reuse_ltb(ltb, size)) {
261 dev_dbg(dev,
262 "LTB size changed from 0x%llx to 0x%x, reallocating\n",
263 ltb->size, size);
264 free_long_term_buff(adapter, ltb);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600265 }
Sukadev Bhattiproluf8ac0bf2021-09-14 20:52:57 -0700266
267 if (ltb->buff) {
268 dev_dbg(dev, "Reusing LTB [map %d, size 0x%llx]\n",
269 ltb->map_id, ltb->size);
270 } else {
271 ltb->buff = dma_alloc_coherent(dev, size, &ltb->addr,
272 GFP_KERNEL);
273 if (!ltb->buff) {
274 dev_err(dev, "Couldn't alloc long term buffer\n");
275 return -ENOMEM;
276 }
277 ltb->size = size;
278
279 ltb->map_id = find_first_zero_bit(adapter->map_ids,
280 MAX_MAP_ID);
281 bitmap_set(adapter->map_ids, ltb->map_id, 1);
282
283 dev_dbg(dev,
284 "Allocated new LTB [map %d, size 0x%llx]\n",
285 ltb->map_id, ltb->size);
286 }
287
288 /* Ensure ltb is zeroed - specially when reusing it. */
289 memset(ltb->buff, 0, ltb->size);
Nathan Fontenotdb5d0b52017-02-10 13:45:05 -0500290
Thomas Falconff25dcb2019-11-25 17:12:56 -0600291 mutex_lock(&adapter->fw_lock);
292 adapter->fw_done_rc = 0;
Thomas Falcon070eca92019-11-25 17:12:53 -0600293 reinit_completion(&adapter->fw_done);
Sukadev Bhattiprolu552a3372021-06-23 21:13:14 -0700294
295 rc = send_request_map(adapter, ltb->addr, ltb->size, ltb->map_id);
Thomas Falcon9c4eaab2018-05-23 13:37:57 -0500296 if (rc) {
Sukadev Bhattiprolu552a3372021-06-23 21:13:14 -0700297 dev_err(dev, "send_request_map failed, rc = %d\n", rc);
298 goto out;
Thomas Falcon9c4eaab2018-05-23 13:37:57 -0500299 }
Thomas Falcon476d96c2019-11-25 17:12:55 -0600300
301 rc = ibmvnic_wait_for_completion(adapter, &adapter->fw_done, 10000);
302 if (rc) {
Sukadev Bhattiprolu0f2bf312021-09-14 20:52:52 -0700303 dev_err(dev, "LTB map request aborted or timed out, rc = %d\n",
Thomas Falcon476d96c2019-11-25 17:12:55 -0600304 rc);
Sukadev Bhattiprolu552a3372021-06-23 21:13:14 -0700305 goto out;
Thomas Falcon476d96c2019-11-25 17:12:55 -0600306 }
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500307
308 if (adapter->fw_done_rc) {
Sukadev Bhattiprolu0f2bf312021-09-14 20:52:52 -0700309 dev_err(dev, "Couldn't map LTB, rc = %d\n",
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500310 adapter->fw_done_rc);
Dany Maddenb6ee5662021-12-14 00:17:47 -0500311 rc = -EIO;
Sukadev Bhattiprolu552a3372021-06-23 21:13:14 -0700312 goto out;
313 }
314 rc = 0;
315out:
Sukadev Bhattiproluf8ac0bf2021-09-14 20:52:57 -0700316 /* don't free LTB on communication error - see function header */
Thomas Falconff25dcb2019-11-25 17:12:56 -0600317 mutex_unlock(&adapter->fw_lock);
Sukadev Bhattiprolu552a3372021-06-23 21:13:14 -0700318 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600319}
320
321static void free_long_term_buff(struct ibmvnic_adapter *adapter,
322 struct ibmvnic_long_term_buff *ltb)
323{
324 struct device *dev = &adapter->vdev->dev;
325
Nathan Fontenotc657e322017-03-30 02:49:06 -0400326 if (!ltb->buff)
327 return;
328
Lijun Pan7d3a7b92021-02-12 20:49:00 -0600329 /* VIOS automatically unmaps the long term buffer at remote
330 * end for the following resets:
331 * FAILOVER, MOBILITY, TIMEOUT.
332 */
Nathan Fontenoted651a12017-05-03 14:04:38 -0400333 if (adapter->reset_reason != VNIC_RESET_FAILOVER &&
Lijun Pan7d3a7b92021-02-12 20:49:00 -0600334 adapter->reset_reason != VNIC_RESET_MOBILITY &&
335 adapter->reset_reason != VNIC_RESET_TIMEOUT)
Thomas Falcondfad09a2016-08-18 11:37:51 -0500336 send_request_unmap(adapter, ltb->map_id);
Sukadev Bhattiprolu0f2bf312021-09-14 20:52:52 -0700337
Brian King59af56c2017-04-19 13:44:41 -0400338 dma_free_coherent(dev, ltb->size, ltb->buff, ltb->addr);
Sukadev Bhattiprolu0f2bf312021-09-14 20:52:52 -0700339
Sukadev Bhattiprolu552a3372021-06-23 21:13:14 -0700340 ltb->buff = NULL;
Sukadev Bhattiprolu129854f02021-09-14 20:52:56 -0700341 /* mark this map_id free */
342 bitmap_clear(adapter->map_ids, ltb->map_id, 1);
Sukadev Bhattiprolu552a3372021-06-23 21:13:14 -0700343 ltb->map_id = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600344}
345
Thomas Falconf185a492017-05-26 10:30:48 -0400346static void deactivate_rx_pools(struct ibmvnic_adapter *adapter)
347{
348 int i;
349
Thomas Falcon507ebe62020-08-21 13:39:01 -0500350 for (i = 0; i < adapter->num_active_rx_pools; i++)
Thomas Falconf185a492017-05-26 10:30:48 -0400351 adapter->rx_pool[i].active = 0;
352}
353
Thomas Falcon032c5e82015-12-21 11:26:06 -0600354static void replenish_rx_pool(struct ibmvnic_adapter *adapter,
355 struct ibmvnic_rx_pool *pool)
356{
357 int count = pool->size - atomic_read(&pool->available);
Cristobal Fornof3ae59c2020-08-19 13:16:23 -0500358 u64 handle = adapter->rx_scrq[pool->index]->handle;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600359 struct device *dev = &adapter->vdev->dev;
Thomas Falcon4f0b6812020-11-18 19:12:18 -0600360 struct ibmvnic_ind_xmit_queue *ind_bufp;
361 struct ibmvnic_sub_crq_queue *rx_scrq;
362 union sub_crq *sub_crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600363 int buffers_added = 0;
364 unsigned long lpar_rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600365 struct sk_buff *skb;
366 unsigned int offset;
367 dma_addr_t dma_addr;
368 unsigned char *dst;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600369 int shift = 0;
370 int index;
371 int i;
372
Thomas Falconf185a492017-05-26 10:30:48 -0400373 if (!pool->active)
374 return;
375
Thomas Falcon4f0b6812020-11-18 19:12:18 -0600376 rx_scrq = adapter->rx_scrq[pool->index];
377 ind_bufp = &rx_scrq->ind_buf;
Sukadev Bhattiprolu72368f82021-06-23 21:13:13 -0700378
379 /* netdev_skb_alloc() could have failed after we saved a few skbs
380 * in the indir_buf and we would not have sent them to VIOS yet.
381 * To account for them, start the loop at ind_bufp->index rather
382 * than 0. If we pushed all the skbs to VIOS, ind_bufp->index will
383 * be 0.
384 */
385 for (i = ind_bufp->index; i < count; ++i) {
Sukadev Bhattiprolu489de952021-09-14 20:52:58 -0700386 index = pool->free_map[pool->next_free];
387
388 /* We maybe reusing the skb from earlier resets. Allocate
389 * only if necessary. But since the LTB may have changed
390 * during reset (see init_rx_pools()), update LTB below
391 * even if reusing skb.
392 */
393 skb = pool->rx_buff[index].skb;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600394 if (!skb) {
Sukadev Bhattiprolu489de952021-09-14 20:52:58 -0700395 skb = netdev_alloc_skb(adapter->netdev,
396 pool->buff_size);
397 if (!skb) {
398 dev_err(dev, "Couldn't replenish rx buff\n");
399 adapter->replenish_no_mem++;
400 break;
401 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600402 }
403
Sukadev Bhattiprolu38106b22021-09-14 20:52:51 -0700404 pool->free_map[pool->next_free] = IBMVNIC_INVALID_MAP;
405 pool->next_free = (pool->next_free + 1) % pool->size;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600406
Thomas Falcon032c5e82015-12-21 11:26:06 -0600407 /* Copy the skb to the long term mapped DMA buffer */
408 offset = index * pool->buff_size;
409 dst = pool->long_term_buff.buff + offset;
410 memset(dst, 0, pool->buff_size);
411 dma_addr = pool->long_term_buff.addr + offset;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600412
Sukadev Bhattiprolu38106b22021-09-14 20:52:51 -0700413 /* add the skb to an rx_buff in the pool */
414 pool->rx_buff[index].data = dst;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600415 pool->rx_buff[index].dma = dma_addr;
416 pool->rx_buff[index].skb = skb;
417 pool->rx_buff[index].pool_index = pool->index;
418 pool->rx_buff[index].size = pool->buff_size;
419
Sukadev Bhattiprolu38106b22021-09-14 20:52:51 -0700420 /* queue the rx_buff for the next send_subcrq_indirect */
Thomas Falcon4f0b6812020-11-18 19:12:18 -0600421 sub_crq = &ind_bufp->indir_arr[ind_bufp->index++];
422 memset(sub_crq, 0, sizeof(*sub_crq));
423 sub_crq->rx_add.first = IBMVNIC_CRQ_CMD;
424 sub_crq->rx_add.correlator =
Thomas Falcon032c5e82015-12-21 11:26:06 -0600425 cpu_to_be64((u64)&pool->rx_buff[index]);
Thomas Falcon4f0b6812020-11-18 19:12:18 -0600426 sub_crq->rx_add.ioba = cpu_to_be32(dma_addr);
427 sub_crq->rx_add.map_id = pool->long_term_buff.map_id;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600428
429 /* The length field of the sCRQ is defined to be 24 bits so the
430 * buffer size needs to be left shifted by a byte before it is
431 * converted to big endian to prevent the last byte from being
432 * truncated.
433 */
434#ifdef __LITTLE_ENDIAN__
435 shift = 8;
436#endif
Thomas Falcon4f0b6812020-11-18 19:12:18 -0600437 sub_crq->rx_add.len = cpu_to_be32(pool->buff_size << shift);
Sukadev Bhattiprolu38106b22021-09-14 20:52:51 -0700438
439 /* if send_subcrq_indirect queue is full, flush to VIOS */
Thomas Falcon4f0b6812020-11-18 19:12:18 -0600440 if (ind_bufp->index == IBMVNIC_MAX_IND_DESCS ||
441 i == count - 1) {
442 lpar_rc =
443 send_subcrq_indirect(adapter, handle,
444 (u64)ind_bufp->indir_dma,
445 (u64)ind_bufp->index);
446 if (lpar_rc != H_SUCCESS)
447 goto failure;
448 buffers_added += ind_bufp->index;
449 adapter->replenish_add_buff_success += ind_bufp->index;
450 ind_bufp->index = 0;
451 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600452 }
453 atomic_add(buffers_added, &pool->available);
454 return;
455
456failure:
Thomas Falcon2d14d372018-07-13 12:03:32 -0500457 if (lpar_rc != H_PARAMETER && lpar_rc != H_CLOSED)
458 dev_err_ratelimited(dev, "rx: replenish packet buffer failed\n");
Thomas Falcon4f0b6812020-11-18 19:12:18 -0600459 for (i = ind_bufp->index - 1; i >= 0; --i) {
460 struct ibmvnic_rx_buff *rx_buff;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600461
Thomas Falcon4f0b6812020-11-18 19:12:18 -0600462 pool->next_free = pool->next_free == 0 ?
463 pool->size - 1 : pool->next_free - 1;
464 sub_crq = &ind_bufp->indir_arr[i];
465 rx_buff = (struct ibmvnic_rx_buff *)
466 be64_to_cpu(sub_crq->rx_add.correlator);
467 index = (int)(rx_buff - pool->rx_buff);
468 pool->free_map[pool->next_free] = index;
469 dev_kfree_skb_any(pool->rx_buff[index].skb);
470 pool->rx_buff[index].skb = NULL;
471 }
Dwip N. Banerjeec2af6222020-12-09 20:53:31 -0500472 adapter->replenish_add_buff_failure += ind_bufp->index;
473 atomic_add(buffers_added, &pool->available);
Thomas Falcon4f0b6812020-11-18 19:12:18 -0600474 ind_bufp->index = 0;
Thomas Falcon5a18e1e2018-04-06 18:37:05 -0500475 if (lpar_rc == H_CLOSED || adapter->failover_pending) {
Thomas Falconf185a492017-05-26 10:30:48 -0400476 /* Disable buffer pool replenishment and report carrier off if
Thomas Falcon5a18e1e2018-04-06 18:37:05 -0500477 * queue is closed or pending failover.
478 * Firmware guarantees that a signal will be sent to the
479 * driver, triggering a reset.
Thomas Falconf185a492017-05-26 10:30:48 -0400480 */
481 deactivate_rx_pools(adapter);
482 netif_carrier_off(adapter->netdev);
483 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600484}
485
486static void replenish_pools(struct ibmvnic_adapter *adapter)
487{
488 int i;
489
Thomas Falcon032c5e82015-12-21 11:26:06 -0600490 adapter->replenish_task_cycles++;
Thomas Falcon507ebe62020-08-21 13:39:01 -0500491 for (i = 0; i < adapter->num_active_rx_pools; i++) {
Thomas Falcon032c5e82015-12-21 11:26:06 -0600492 if (adapter->rx_pool[i].active)
493 replenish_rx_pool(adapter, &adapter->rx_pool[i]);
494 }
Sukadev Bhattiprolu38bd5ce2020-12-04 18:22:35 -0800495
496 netdev_dbg(adapter->netdev, "Replenished %d pools\n", i);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600497}
498
John Allen3d52b592017-08-02 16:44:14 -0500499static void release_stats_buffers(struct ibmvnic_adapter *adapter)
500{
501 kfree(adapter->tx_stats_buffers);
502 kfree(adapter->rx_stats_buffers);
Thomas Falconb0992ec2018-02-06 17:25:23 -0600503 adapter->tx_stats_buffers = NULL;
504 adapter->rx_stats_buffers = NULL;
John Allen3d52b592017-08-02 16:44:14 -0500505}
506
507static int init_stats_buffers(struct ibmvnic_adapter *adapter)
508{
509 adapter->tx_stats_buffers =
Nathan Fontenotabcae542018-02-19 13:30:47 -0600510 kcalloc(IBMVNIC_MAX_QUEUES,
John Allen3d52b592017-08-02 16:44:14 -0500511 sizeof(struct ibmvnic_tx_queue_stats),
512 GFP_KERNEL);
513 if (!adapter->tx_stats_buffers)
514 return -ENOMEM;
515
516 adapter->rx_stats_buffers =
Nathan Fontenotabcae542018-02-19 13:30:47 -0600517 kcalloc(IBMVNIC_MAX_QUEUES,
John Allen3d52b592017-08-02 16:44:14 -0500518 sizeof(struct ibmvnic_rx_queue_stats),
519 GFP_KERNEL);
520 if (!adapter->rx_stats_buffers)
521 return -ENOMEM;
522
523 return 0;
524}
525
Nathan Fontenot7bbc27a2017-03-30 02:49:23 -0400526static void release_stats_token(struct ibmvnic_adapter *adapter)
527{
528 struct device *dev = &adapter->vdev->dev;
529
530 if (!adapter->stats_token)
531 return;
532
533 dma_unmap_single(dev, adapter->stats_token,
534 sizeof(struct ibmvnic_statistics),
535 DMA_FROM_DEVICE);
536 adapter->stats_token = 0;
537}
538
539static int init_stats_token(struct ibmvnic_adapter *adapter)
540{
541 struct device *dev = &adapter->vdev->dev;
542 dma_addr_t stok;
Dany Maddenb6ee5662021-12-14 00:17:47 -0500543 int rc;
Nathan Fontenot7bbc27a2017-03-30 02:49:23 -0400544
545 stok = dma_map_single(dev, &adapter->stats,
546 sizeof(struct ibmvnic_statistics),
547 DMA_FROM_DEVICE);
Dany Maddenb6ee5662021-12-14 00:17:47 -0500548 rc = dma_mapping_error(dev, stok);
549 if (rc) {
550 dev_err(dev, "Couldn't map stats buffer, rc = %d\n", rc);
551 return rc;
Nathan Fontenot7bbc27a2017-03-30 02:49:23 -0400552 }
553
554 adapter->stats_token = stok;
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500555 netdev_dbg(adapter->netdev, "Stats token initialized (%llx)\n", stok);
Nathan Fontenot7bbc27a2017-03-30 02:49:23 -0400556 return 0;
557}
558
Sukadev Bhattiprolu0f2bf312021-09-14 20:52:52 -0700559/**
560 * release_rx_pools() - Release any rx pools attached to @adapter.
561 * @adapter: ibmvnic adapter
562 *
563 * Safe to call this multiple times - even if no pools are attached.
564 */
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400565static void release_rx_pools(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -0600566{
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400567 struct ibmvnic_rx_pool *rx_pool;
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400568 int i, j;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600569
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400570 if (!adapter->rx_pool)
Thomas Falcon032c5e82015-12-21 11:26:06 -0600571 return;
572
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600573 for (i = 0; i < adapter->num_active_rx_pools; i++) {
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400574 rx_pool = &adapter->rx_pool[i];
575
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500576 netdev_dbg(adapter->netdev, "Releasing rx_pool[%d]\n", i);
577
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400578 kfree(rx_pool->free_map);
Sukadev Bhattiprolu489de952021-09-14 20:52:58 -0700579
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400580 free_long_term_buff(adapter, &rx_pool->long_term_buff);
581
582 if (!rx_pool->rx_buff)
Nathan Fontenote0ebe9422017-05-03 14:04:50 -0400583 continue;
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400584
585 for (j = 0; j < rx_pool->size; j++) {
586 if (rx_pool->rx_buff[j].skb) {
Thomas Falconb7cdec32018-11-21 11:17:58 -0600587 dev_kfree_skb_any(rx_pool->rx_buff[j].skb);
588 rx_pool->rx_buff[j].skb = NULL;
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400589 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600590 }
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400591
592 kfree(rx_pool->rx_buff);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600593 }
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400594
595 kfree(adapter->rx_pool);
596 adapter->rx_pool = NULL;
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600597 adapter->num_active_rx_pools = 0;
Sukadev Bhattiprolu489de952021-09-14 20:52:58 -0700598 adapter->prev_rx_pool_size = 0;
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400599}
600
Sukadev Bhattiprolu489de952021-09-14 20:52:58 -0700601/**
602 * reuse_rx_pools() - Check if the existing rx pools can be reused.
603 * @adapter: ibmvnic adapter
604 *
605 * Check if the existing rx pools in the adapter can be reused. The
606 * pools can be reused if the pool parameters (number of pools,
607 * number of buffers in the pool and size of each buffer) have not
608 * changed.
609 *
610 * NOTE: This assumes that all pools have the same number of buffers
611 * which is the case currently. If that changes, we must fix this.
612 *
613 * Return: true if the rx pools can be reused, false otherwise.
614 */
615static bool reuse_rx_pools(struct ibmvnic_adapter *adapter)
616{
617 u64 old_num_pools, new_num_pools;
618 u64 old_pool_size, new_pool_size;
619 u64 old_buff_size, new_buff_size;
620
621 if (!adapter->rx_pool)
622 return false;
623
624 old_num_pools = adapter->num_active_rx_pools;
625 new_num_pools = adapter->req_rx_queues;
626
627 old_pool_size = adapter->prev_rx_pool_size;
628 new_pool_size = adapter->req_rx_add_entries_per_subcrq;
629
630 old_buff_size = adapter->prev_rx_buf_sz;
631 new_buff_size = adapter->cur_rx_buf_sz;
632
Sukadev Bhattiprolu0584f492021-11-30 21:48:35 -0800633 if (old_buff_size != new_buff_size ||
634 old_num_pools != new_num_pools ||
635 old_pool_size != new_pool_size)
Sukadev Bhattiprolu489de952021-09-14 20:52:58 -0700636 return false;
637
638 return true;
639}
640
641/**
642 * init_rx_pools(): Initialize the set of receiver pools in the adapter.
643 * @netdev: net device associated with the vnic interface
644 *
645 * Initialize the set of receiver pools in the ibmvnic adapter associated
646 * with the net_device @netdev. If possible, reuse the existing rx pools.
647 * Otherwise free any existing pools and allocate a new set of pools
648 * before initializing them.
649 *
650 * Return: 0 on success and negative value on error.
651 */
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400652static int init_rx_pools(struct net_device *netdev)
653{
654 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
655 struct device *dev = &adapter->vdev->dev;
656 struct ibmvnic_rx_pool *rx_pool;
Sukadev Bhattiprolu0df7b9a2021-09-14 20:52:53 -0700657 u64 num_pools;
658 u64 pool_size; /* # of buffers in one pool */
Thomas Falcon507ebe62020-08-21 13:39:01 -0500659 u64 buff_size;
Dany Maddenb6ee5662021-12-14 00:17:47 -0500660 int i, j, rc;
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400661
Sukadev Bhattiprolu0df7b9a2021-09-14 20:52:53 -0700662 pool_size = adapter->req_rx_add_entries_per_subcrq;
Sukadev Bhattiprolu489de952021-09-14 20:52:58 -0700663 num_pools = adapter->req_rx_queues;
Thomas Falcon507ebe62020-08-21 13:39:01 -0500664 buff_size = adapter->cur_rx_buf_sz;
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400665
Sukadev Bhattiprolu489de952021-09-14 20:52:58 -0700666 if (reuse_rx_pools(adapter)) {
667 dev_dbg(dev, "Reusing rx pools\n");
668 goto update_ltb;
669 }
670
671 /* Allocate/populate the pools. */
672 release_rx_pools(adapter);
673
Sukadev Bhattiprolu0df7b9a2021-09-14 20:52:53 -0700674 adapter->rx_pool = kcalloc(num_pools,
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400675 sizeof(struct ibmvnic_rx_pool),
676 GFP_KERNEL);
677 if (!adapter->rx_pool) {
678 dev_err(dev, "Failed to allocate rx pools\n");
Dany Maddenb6ee5662021-12-14 00:17:47 -0500679 return -ENOMEM;
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400680 }
681
Sukadev Bhattiprolu0f2bf312021-09-14 20:52:52 -0700682 /* Set num_active_rx_pools early. If we fail below after partial
683 * allocation, release_rx_pools() will know how many to look for.
684 */
Sukadev Bhattiprolu0df7b9a2021-09-14 20:52:53 -0700685 adapter->num_active_rx_pools = num_pools;
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600686
Sukadev Bhattiprolu0df7b9a2021-09-14 20:52:53 -0700687 for (i = 0; i < num_pools; i++) {
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400688 rx_pool = &adapter->rx_pool[i];
689
690 netdev_dbg(adapter->netdev,
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500691 "Initializing rx_pool[%d], %lld buffs, %lld bytes each\n",
Sukadev Bhattiprolu0df7b9a2021-09-14 20:52:53 -0700692 i, pool_size, buff_size);
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400693
Sukadev Bhattiprolu0df7b9a2021-09-14 20:52:53 -0700694 rx_pool->size = pool_size;
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400695 rx_pool->index = i;
Dwip N. Banerjee9a87c3f2020-11-18 19:12:22 -0600696 rx_pool->buff_size = ALIGN(buff_size, L1_CACHE_BYTES);
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400697
698 rx_pool->free_map = kcalloc(rx_pool->size, sizeof(int),
699 GFP_KERNEL);
700 if (!rx_pool->free_map) {
Sukadev Bhattiprolu0f2bf312021-09-14 20:52:52 -0700701 dev_err(dev, "Couldn't alloc free_map %d\n", i);
Dany Maddenb6ee5662021-12-14 00:17:47 -0500702 rc = -ENOMEM;
Sukadev Bhattiprolu489de952021-09-14 20:52:58 -0700703 goto out_release;
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400704 }
705
706 rx_pool->rx_buff = kcalloc(rx_pool->size,
707 sizeof(struct ibmvnic_rx_buff),
708 GFP_KERNEL);
709 if (!rx_pool->rx_buff) {
710 dev_err(dev, "Couldn't alloc rx buffers\n");
Dany Maddenb6ee5662021-12-14 00:17:47 -0500711 rc = -ENOMEM;
Sukadev Bhattiprolu489de952021-09-14 20:52:58 -0700712 goto out_release;
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400713 }
Sukadev Bhattiprolu489de952021-09-14 20:52:58 -0700714 }
715
716 adapter->prev_rx_pool_size = pool_size;
717 adapter->prev_rx_buf_sz = adapter->cur_rx_buf_sz;
718
719update_ltb:
720 for (i = 0; i < num_pools; i++) {
721 rx_pool = &adapter->rx_pool[i];
722 dev_dbg(dev, "Updating LTB for rx pool %d [%d, %d]\n",
723 i, rx_pool->size, rx_pool->buff_size);
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400724
Dany Maddenb6ee5662021-12-14 00:17:47 -0500725 rc = alloc_long_term_buff(adapter, &rx_pool->long_term_buff,
726 rx_pool->size * rx_pool->buff_size);
727 if (rc)
Sukadev Bhattiprolu489de952021-09-14 20:52:58 -0700728 goto out;
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400729
Sukadev Bhattiprolu489de952021-09-14 20:52:58 -0700730 for (j = 0; j < rx_pool->size; ++j) {
731 struct ibmvnic_rx_buff *rx_buff;
732
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400733 rx_pool->free_map[j] = j;
734
Sukadev Bhattiprolu489de952021-09-14 20:52:58 -0700735 /* NOTE: Don't clear rx_buff->skb here - will leak
736 * memory! replenish_rx_pool() will reuse skbs or
737 * allocate as necessary.
738 */
739 rx_buff = &rx_pool->rx_buff[j];
740 rx_buff->dma = 0;
741 rx_buff->data = 0;
742 rx_buff->size = 0;
743 rx_buff->pool_index = 0;
744 }
745
746 /* Mark pool "empty" so replenish_rx_pools() will
747 * update the LTB info for each buffer
748 */
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400749 atomic_set(&rx_pool->available, 0);
750 rx_pool->next_alloc = 0;
751 rx_pool->next_free = 0;
Sukadev Bhattiprolu489de952021-09-14 20:52:58 -0700752 /* replenish_rx_pool() may have called deactivate_rx_pools()
753 * on failover. Ensure pool is active now.
754 */
755 rx_pool->active = 1;
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400756 }
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400757 return 0;
Sukadev Bhattiprolu489de952021-09-14 20:52:58 -0700758out_release:
759 release_rx_pools(adapter);
760out:
761 /* We failed to allocate one or more LTBs or map them on the VIOS.
762 * Hold onto the pools and any LTBs that we did allocate/map.
763 */
Dany Maddenb6ee5662021-12-14 00:17:47 -0500764 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600765}
766
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -0200767static void release_vpd_data(struct ibmvnic_adapter *adapter)
768{
769 if (!adapter->vpd)
770 return;
771
772 kfree(adapter->vpd->buff);
773 kfree(adapter->vpd);
Thomas Falconb0992ec2018-02-06 17:25:23 -0600774
775 adapter->vpd = NULL;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -0200776}
777
Thomas Falconfb794212018-03-16 20:00:26 -0500778static void release_one_tx_pool(struct ibmvnic_adapter *adapter,
779 struct ibmvnic_tx_pool *tx_pool)
780{
781 kfree(tx_pool->tx_buff);
782 kfree(tx_pool->free_map);
783 free_long_term_buff(adapter, &tx_pool->long_term_buff);
784}
785
Sukadev Bhattiprolu0f2bf312021-09-14 20:52:52 -0700786/**
787 * release_tx_pools() - Release any tx pools attached to @adapter.
788 * @adapter: ibmvnic adapter
789 *
790 * Safe to call this multiple times - even if no pools are attached.
791 */
Nathan Fontenotc657e322017-03-30 02:49:06 -0400792static void release_tx_pools(struct ibmvnic_adapter *adapter)
793{
John Allen896d8692018-01-18 16:26:31 -0600794 int i;
Nathan Fontenotc657e322017-03-30 02:49:06 -0400795
Sukadev Bhattiprolu0f2bf312021-09-14 20:52:52 -0700796 /* init_tx_pools() ensures that ->tx_pool and ->tso_pool are
797 * both NULL or both non-NULL. So we only need to check one.
798 */
Nathan Fontenotc657e322017-03-30 02:49:06 -0400799 if (!adapter->tx_pool)
800 return;
801
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600802 for (i = 0; i < adapter->num_active_tx_pools; i++) {
Thomas Falconfb794212018-03-16 20:00:26 -0500803 release_one_tx_pool(adapter, &adapter->tx_pool[i]);
804 release_one_tx_pool(adapter, &adapter->tso_pool[i]);
Nathan Fontenotc657e322017-03-30 02:49:06 -0400805 }
806
807 kfree(adapter->tx_pool);
808 adapter->tx_pool = NULL;
Thomas Falconfb794212018-03-16 20:00:26 -0500809 kfree(adapter->tso_pool);
810 adapter->tso_pool = NULL;
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600811 adapter->num_active_tx_pools = 0;
Sukadev Bhattiprolubbd80932021-09-14 20:52:59 -0700812 adapter->prev_tx_pool_size = 0;
Nathan Fontenotc657e322017-03-30 02:49:06 -0400813}
814
Thomas Falcon32053062018-03-16 20:00:27 -0500815static int init_one_tx_pool(struct net_device *netdev,
816 struct ibmvnic_tx_pool *tx_pool,
Sukadev Bhattiprolu8243c7e2021-09-14 20:52:54 -0700817 int pool_size, int buf_size)
Thomas Falcon32053062018-03-16 20:00:27 -0500818{
Thomas Falcon32053062018-03-16 20:00:27 -0500819 int i;
820
Sukadev Bhattiprolu8243c7e2021-09-14 20:52:54 -0700821 tx_pool->tx_buff = kcalloc(pool_size,
Thomas Falcon32053062018-03-16 20:00:27 -0500822 sizeof(struct ibmvnic_tx_buff),
823 GFP_KERNEL);
824 if (!tx_pool->tx_buff)
Dany Maddenb6ee5662021-12-14 00:17:47 -0500825 return -ENOMEM;
Thomas Falcon32053062018-03-16 20:00:27 -0500826
Sukadev Bhattiprolu8243c7e2021-09-14 20:52:54 -0700827 tx_pool->free_map = kcalloc(pool_size, sizeof(int), GFP_KERNEL);
Sukadev Bhattiprolubbd80932021-09-14 20:52:59 -0700828 if (!tx_pool->free_map) {
829 kfree(tx_pool->tx_buff);
830 tx_pool->tx_buff = NULL;
Dany Maddenb6ee5662021-12-14 00:17:47 -0500831 return -ENOMEM;
Sukadev Bhattiprolubbd80932021-09-14 20:52:59 -0700832 }
Thomas Falcon32053062018-03-16 20:00:27 -0500833
Sukadev Bhattiprolu8243c7e2021-09-14 20:52:54 -0700834 for (i = 0; i < pool_size; i++)
Thomas Falcon32053062018-03-16 20:00:27 -0500835 tx_pool->free_map[i] = i;
836
837 tx_pool->consumer_index = 0;
838 tx_pool->producer_index = 0;
Sukadev Bhattiprolu8243c7e2021-09-14 20:52:54 -0700839 tx_pool->num_buffers = pool_size;
Thomas Falcon32053062018-03-16 20:00:27 -0500840 tx_pool->buf_size = buf_size;
841
842 return 0;
843}
844
Sukadev Bhattiprolubbd80932021-09-14 20:52:59 -0700845/**
846 * reuse_tx_pools() - Check if the existing tx pools can be reused.
847 * @adapter: ibmvnic adapter
848 *
849 * Check if the existing tx pools in the adapter can be reused. The
850 * pools can be reused if the pool parameters (number of pools,
851 * number of buffers in the pool and mtu) have not changed.
852 *
853 * NOTE: This assumes that all pools have the same number of buffers
854 * which is the case currently. If that changes, we must fix this.
855 *
856 * Return: true if the tx pools can be reused, false otherwise.
857 */
858static bool reuse_tx_pools(struct ibmvnic_adapter *adapter)
859{
860 u64 old_num_pools, new_num_pools;
861 u64 old_pool_size, new_pool_size;
862 u64 old_mtu, new_mtu;
863
864 if (!adapter->tx_pool)
865 return false;
866
867 old_num_pools = adapter->num_active_tx_pools;
868 new_num_pools = adapter->num_active_tx_scrqs;
869 old_pool_size = adapter->prev_tx_pool_size;
870 new_pool_size = adapter->req_tx_entries_per_subcrq;
871 old_mtu = adapter->prev_mtu;
872 new_mtu = adapter->req_mtu;
873
Sukadev Bhattiprolu5b085602021-11-30 21:48:36 -0800874 if (old_mtu != new_mtu ||
875 old_num_pools != new_num_pools ||
876 old_pool_size != new_pool_size)
Sukadev Bhattiprolubbd80932021-09-14 20:52:59 -0700877 return false;
878
879 return true;
880}
881
882/**
883 * init_tx_pools(): Initialize the set of transmit pools in the adapter.
884 * @netdev: net device associated with the vnic interface
885 *
886 * Initialize the set of transmit pools in the ibmvnic adapter associated
887 * with the net_device @netdev. If possible, reuse the existing tx pools.
888 * Otherwise free any existing pools and allocate a new set of pools
889 * before initializing them.
890 *
891 * Return: 0 on success and negative value on error.
892 */
Nathan Fontenotc657e322017-03-30 02:49:06 -0400893static int init_tx_pools(struct net_device *netdev)
894{
895 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Sukadev Bhattiprolu0f2bf312021-09-14 20:52:52 -0700896 struct device *dev = &adapter->vdev->dev;
Sukadev Bhattiprolu8243c7e2021-09-14 20:52:54 -0700897 int num_pools;
898 u64 pool_size; /* # of buffers in pool */
Dwip N. Banerjee9a87c3f2020-11-18 19:12:22 -0600899 u64 buff_size;
Sukadev Bhattiprolubbd80932021-09-14 20:52:59 -0700900 int i, j, rc;
901
902 num_pools = adapter->req_tx_queues;
903
904 /* We must notify the VIOS about the LTB on all resets - but we only
905 * need to alloc/populate pools if either the number of buffers or
906 * size of each buffer in the pool has changed.
907 */
908 if (reuse_tx_pools(adapter)) {
909 netdev_dbg(netdev, "Reusing tx pools\n");
910 goto update_ltb;
911 }
912
913 /* Allocate/populate the pools. */
914 release_tx_pools(adapter);
Nathan Fontenotc657e322017-03-30 02:49:06 -0400915
Sukadev Bhattiprolu8243c7e2021-09-14 20:52:54 -0700916 pool_size = adapter->req_tx_entries_per_subcrq;
917 num_pools = adapter->num_active_tx_scrqs;
918
919 adapter->tx_pool = kcalloc(num_pools,
Nathan Fontenotc657e322017-03-30 02:49:06 -0400920 sizeof(struct ibmvnic_tx_pool), GFP_KERNEL);
921 if (!adapter->tx_pool)
Dany Maddenb6ee5662021-12-14 00:17:47 -0500922 return -ENOMEM;
Nathan Fontenotc657e322017-03-30 02:49:06 -0400923
Sukadev Bhattiprolu8243c7e2021-09-14 20:52:54 -0700924 adapter->tso_pool = kcalloc(num_pools,
Thomas Falcon32053062018-03-16 20:00:27 -0500925 sizeof(struct ibmvnic_tx_pool), GFP_KERNEL);
Sukadev Bhattiprolu0f2bf312021-09-14 20:52:52 -0700926 /* To simplify release_tx_pools() ensure that ->tx_pool and
927 * ->tso_pool are either both NULL or both non-NULL.
928 */
Sukadev Bhattiproluf6ebca82021-06-23 21:13:15 -0700929 if (!adapter->tso_pool) {
930 kfree(adapter->tx_pool);
931 adapter->tx_pool = NULL;
Dany Maddenb6ee5662021-12-14 00:17:47 -0500932 return -ENOMEM;
Sukadev Bhattiproluf6ebca82021-06-23 21:13:15 -0700933 }
Thomas Falcon32053062018-03-16 20:00:27 -0500934
Sukadev Bhattiprolu0f2bf312021-09-14 20:52:52 -0700935 /* Set num_active_tx_pools early. If we fail below after partial
936 * allocation, release_tx_pools() will know how many to look for.
937 */
Sukadev Bhattiprolu8243c7e2021-09-14 20:52:54 -0700938 adapter->num_active_tx_pools = num_pools;
Sukadev Bhattiprolubbd80932021-09-14 20:52:59 -0700939
Sukadev Bhattiprolu0d1af4f2021-09-14 20:52:55 -0700940 buff_size = adapter->req_mtu + VLAN_HLEN;
941 buff_size = ALIGN(buff_size, L1_CACHE_BYTES);
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600942
Sukadev Bhattiprolu8243c7e2021-09-14 20:52:54 -0700943 for (i = 0; i < num_pools; i++) {
Sukadev Bhattiprolu0f2bf312021-09-14 20:52:52 -0700944 dev_dbg(dev, "Init tx pool %d [%llu, %llu]\n",
945 i, adapter->req_tx_entries_per_subcrq, buff_size);
946
Thomas Falcon32053062018-03-16 20:00:27 -0500947 rc = init_one_tx_pool(netdev, &adapter->tx_pool[i],
Sukadev Bhattiprolu8243c7e2021-09-14 20:52:54 -0700948 pool_size, buff_size);
Sukadev Bhattiprolubbd80932021-09-14 20:52:59 -0700949 if (rc)
950 goto out_release;
Nathan Fontenotc657e322017-03-30 02:49:06 -0400951
Thomas Falcon7c940b12019-06-07 16:03:55 -0500952 rc = init_one_tx_pool(netdev, &adapter->tso_pool[i],
953 IBMVNIC_TSO_BUFS,
954 IBMVNIC_TSO_BUF_SZ);
Sukadev Bhattiprolubbd80932021-09-14 20:52:59 -0700955 if (rc)
956 goto out_release;
957 }
958
959 adapter->prev_tx_pool_size = pool_size;
960 adapter->prev_mtu = adapter->req_mtu;
961
962update_ltb:
963 /* NOTE: All tx_pools have the same number of buffers (which is
964 * same as pool_size). All tso_pools have IBMVNIC_TSO_BUFS
965 * buffers (see calls init_one_tx_pool() for these).
966 * For consistency, we use tx_pool->num_buffers and
967 * tso_pool->num_buffers below.
968 */
969 rc = -1;
970 for (i = 0; i < num_pools; i++) {
971 struct ibmvnic_tx_pool *tso_pool;
972 struct ibmvnic_tx_pool *tx_pool;
973 u32 ltb_size;
974
975 tx_pool = &adapter->tx_pool[i];
976 ltb_size = tx_pool->num_buffers * tx_pool->buf_size;
977 if (alloc_long_term_buff(adapter, &tx_pool->long_term_buff,
978 ltb_size))
979 goto out;
980
981 dev_dbg(dev, "Updated LTB for tx pool %d [%p, %d, %d]\n",
982 i, tx_pool->long_term_buff.buff,
983 tx_pool->num_buffers, tx_pool->buf_size);
984
985 tx_pool->consumer_index = 0;
986 tx_pool->producer_index = 0;
987
988 for (j = 0; j < tx_pool->num_buffers; j++)
989 tx_pool->free_map[j] = j;
990
991 tso_pool = &adapter->tso_pool[i];
992 ltb_size = tso_pool->num_buffers * tso_pool->buf_size;
993 if (alloc_long_term_buff(adapter, &tso_pool->long_term_buff,
994 ltb_size))
995 goto out;
996
997 dev_dbg(dev, "Updated LTB for tso pool %d [%p, %d, %d]\n",
998 i, tso_pool->long_term_buff.buff,
999 tso_pool->num_buffers, tso_pool->buf_size);
1000
1001 tso_pool->consumer_index = 0;
1002 tso_pool->producer_index = 0;
1003
1004 for (j = 0; j < tso_pool->num_buffers; j++)
1005 tso_pool->free_map[j] = j;
Nathan Fontenotc657e322017-03-30 02:49:06 -04001006 }
1007
1008 return 0;
Sukadev Bhattiprolubbd80932021-09-14 20:52:59 -07001009out_release:
1010 release_tx_pools(adapter);
1011out:
1012 /* We failed to allocate one or more LTBs or map them on the VIOS.
1013 * Hold onto the pools and any LTBs that we did allocate/map.
1014 */
1015 return rc;
Nathan Fontenotc657e322017-03-30 02:49:06 -04001016}
1017
John Allend944c3d62017-05-26 10:30:13 -04001018static void ibmvnic_napi_enable(struct ibmvnic_adapter *adapter)
1019{
1020 int i;
1021
1022 if (adapter->napi_enabled)
1023 return;
1024
1025 for (i = 0; i < adapter->req_rx_queues; i++)
1026 napi_enable(&adapter->napi[i]);
1027
1028 adapter->napi_enabled = true;
1029}
1030
1031static void ibmvnic_napi_disable(struct ibmvnic_adapter *adapter)
1032{
1033 int i;
1034
1035 if (!adapter->napi_enabled)
1036 return;
1037
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001038 for (i = 0; i < adapter->req_rx_queues; i++) {
1039 netdev_dbg(adapter->netdev, "Disabling napi[%d]\n", i);
John Allend944c3d62017-05-26 10:30:13 -04001040 napi_disable(&adapter->napi[i]);
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001041 }
John Allend944c3d62017-05-26 10:30:13 -04001042
1043 adapter->napi_enabled = false;
1044}
1045
Nathan Fontenot86f669b2018-02-19 13:30:39 -06001046static int init_napi(struct ibmvnic_adapter *adapter)
1047{
1048 int i;
1049
1050 adapter->napi = kcalloc(adapter->req_rx_queues,
1051 sizeof(struct napi_struct), GFP_KERNEL);
1052 if (!adapter->napi)
1053 return -ENOMEM;
1054
1055 for (i = 0; i < adapter->req_rx_queues; i++) {
1056 netdev_dbg(adapter->netdev, "Adding napi[%d]\n", i);
1057 netif_napi_add(adapter->netdev, &adapter->napi[i],
1058 ibmvnic_poll, NAPI_POLL_WEIGHT);
1059 }
1060
Nathan Fontenot82e3be32018-02-21 21:33:56 -06001061 adapter->num_active_rx_napi = adapter->req_rx_queues;
Nathan Fontenot86f669b2018-02-19 13:30:39 -06001062 return 0;
1063}
1064
1065static void release_napi(struct ibmvnic_adapter *adapter)
1066{
1067 int i;
1068
1069 if (!adapter->napi)
1070 return;
1071
Nathan Fontenot82e3be32018-02-21 21:33:56 -06001072 for (i = 0; i < adapter->num_active_rx_napi; i++) {
Wen Yang390de192018-12-11 12:20:46 +08001073 netdev_dbg(adapter->netdev, "Releasing napi[%d]\n", i);
1074 netif_napi_del(&adapter->napi[i]);
Nathan Fontenot86f669b2018-02-19 13:30:39 -06001075 }
1076
1077 kfree(adapter->napi);
1078 adapter->napi = NULL;
Nathan Fontenot82e3be32018-02-21 21:33:56 -06001079 adapter->num_active_rx_napi = 0;
Thomas Falconc3f22412018-05-23 13:37:55 -05001080 adapter->napi_enabled = false;
Nathan Fontenot86f669b2018-02-19 13:30:39 -06001081}
1082
Lijun Pan0666ef72021-04-12 02:41:28 -05001083static const char *adapter_state_to_string(enum vnic_state state)
1084{
1085 switch (state) {
1086 case VNIC_PROBING:
1087 return "PROBING";
1088 case VNIC_PROBED:
1089 return "PROBED";
1090 case VNIC_OPENING:
1091 return "OPENING";
1092 case VNIC_OPEN:
1093 return "OPEN";
1094 case VNIC_CLOSING:
1095 return "CLOSING";
1096 case VNIC_CLOSED:
1097 return "CLOSED";
1098 case VNIC_REMOVING:
1099 return "REMOVING";
1100 case VNIC_REMOVED:
1101 return "REMOVED";
Lijun Pan822ebc22021-06-11 10:35:37 -05001102 case VNIC_DOWN:
1103 return "DOWN";
Lijun Pan0666ef72021-04-12 02:41:28 -05001104 }
Michal Suchanek07b5dc12021-05-20 08:50:34 +02001105 return "UNKNOWN";
Lijun Pan0666ef72021-04-12 02:41:28 -05001106}
1107
John Allena57a5d22017-03-17 17:13:41 -05001108static int ibmvnic_login(struct net_device *netdev)
Thomas Falcon032c5e82015-12-21 11:26:06 -06001109{
1110 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Dany Madden98c41f02020-11-25 18:04:32 -06001111 unsigned long timeout = msecs_to_jiffies(20000);
Nathan Fontenot64d92aa2018-04-11 10:09:32 -05001112 int retry_count = 0;
Thomas Falcondff515a32020-06-15 10:29:23 -05001113 int retries = 10;
Thomas Falconeb110412018-05-24 14:37:53 -05001114 bool retry;
Thomas Falcon4d96f122017-08-01 15:04:36 -05001115 int rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001116
John Allenbd0b6722017-03-17 17:13:40 -05001117 do {
Thomas Falconeb110412018-05-24 14:37:53 -05001118 retry = false;
Thomas Falcondff515a32020-06-15 10:29:23 -05001119 if (retry_count > retries) {
Nathan Fontenot64d92aa2018-04-11 10:09:32 -05001120 netdev_warn(netdev, "Login attempts exceeded\n");
Dany Maddenb6ee5662021-12-14 00:17:47 -05001121 return -EACCES;
Nathan Fontenot64d92aa2018-04-11 10:09:32 -05001122 }
1123
1124 adapter->init_done_rc = 0;
1125 reinit_completion(&adapter->init_done);
1126 rc = send_login(adapter);
Dany Maddenc98d9cc2020-11-25 18:04:30 -06001127 if (rc)
Nathan Fontenot64d92aa2018-04-11 10:09:32 -05001128 return rc;
Nathan Fontenot64d92aa2018-04-11 10:09:32 -05001129
1130 if (!wait_for_completion_timeout(&adapter->init_done,
1131 timeout)) {
Thomas Falcondff515a32020-06-15 10:29:23 -05001132 netdev_warn(netdev, "Login timed out, retrying...\n");
1133 retry = true;
1134 adapter->init_done_rc = 0;
1135 retry_count++;
1136 continue;
Nathan Fontenot64d92aa2018-04-11 10:09:32 -05001137 }
1138
Thomas Falcondff515a32020-06-15 10:29:23 -05001139 if (adapter->init_done_rc == ABORTED) {
1140 netdev_warn(netdev, "Login aborted, retrying...\n");
1141 retry = true;
1142 adapter->init_done_rc = 0;
1143 retry_count++;
1144 /* FW or device may be busy, so
1145 * wait a bit before retrying login
1146 */
1147 msleep(500);
1148 } else if (adapter->init_done_rc == PARTIALSUCCESS) {
Nathan Fontenot64d92aa2018-04-11 10:09:32 -05001149 retry_count++;
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06001150 release_sub_crqs(adapter, 1);
John Allenbd0b6722017-03-17 17:13:40 -05001151
Thomas Falconeb110412018-05-24 14:37:53 -05001152 retry = true;
1153 netdev_dbg(netdev,
1154 "Received partial success, retrying...\n");
Nathan Fontenot64d92aa2018-04-11 10:09:32 -05001155 adapter->init_done_rc = 0;
John Allenbd0b6722017-03-17 17:13:40 -05001156 reinit_completion(&adapter->init_done);
Lijun Pan491099a2020-09-27 20:13:26 -05001157 send_query_cap(adapter);
John Allenbd0b6722017-03-17 17:13:40 -05001158 if (!wait_for_completion_timeout(&adapter->init_done,
1159 timeout)) {
Nathan Fontenot64d92aa2018-04-11 10:09:32 -05001160 netdev_warn(netdev,
1161 "Capabilities query timed out\n");
Dany Maddenb6ee5662021-12-14 00:17:47 -05001162 return -ETIMEDOUT;
John Allenbd0b6722017-03-17 17:13:40 -05001163 }
Nathan Fontenot64d92aa2018-04-11 10:09:32 -05001164
Thomas Falcon4d96f122017-08-01 15:04:36 -05001165 rc = init_sub_crqs(adapter);
1166 if (rc) {
Nathan Fontenot64d92aa2018-04-11 10:09:32 -05001167 netdev_warn(netdev,
1168 "SCRQ initialization failed\n");
Dany Maddenb6ee5662021-12-14 00:17:47 -05001169 return rc;
Thomas Falcon4d96f122017-08-01 15:04:36 -05001170 }
Nathan Fontenot64d92aa2018-04-11 10:09:32 -05001171
Thomas Falcon4d96f122017-08-01 15:04:36 -05001172 rc = init_sub_crq_irqs(adapter);
1173 if (rc) {
Nathan Fontenot64d92aa2018-04-11 10:09:32 -05001174 netdev_warn(netdev,
1175 "SCRQ irq initialization failed\n");
Dany Maddenb6ee5662021-12-14 00:17:47 -05001176 return rc;
Thomas Falcon4d96f122017-08-01 15:04:36 -05001177 }
Nathan Fontenot64d92aa2018-04-11 10:09:32 -05001178 } else if (adapter->init_done_rc) {
Dany Maddenb6ee5662021-12-14 00:17:47 -05001179 netdev_warn(netdev, "Adapter login failed, init_done_rc = %d\n",
1180 adapter->init_done_rc);
1181 return -EIO;
John Allenbd0b6722017-03-17 17:13:40 -05001182 }
Thomas Falconeb110412018-05-24 14:37:53 -05001183 } while (retry);
John Allenbd0b6722017-03-17 17:13:40 -05001184
Thomas Falcon62740e92019-05-09 23:13:43 -05001185 __ibmvnic_set_mac(netdev, adapter->mac_addr);
Thomas Falcon3d166132018-01-10 19:39:52 -06001186
Lijun Pan0666ef72021-04-12 02:41:28 -05001187 netdev_dbg(netdev, "[S:%s] Login succeeded\n", adapter_state_to_string(adapter->state));
John Allena57a5d22017-03-17 17:13:41 -05001188 return 0;
1189}
1190
Thomas Falcon34f0f4e2018-02-13 18:23:40 -06001191static void release_login_buffer(struct ibmvnic_adapter *adapter)
1192{
1193 kfree(adapter->login_buf);
1194 adapter->login_buf = NULL;
1195}
1196
1197static void release_login_rsp_buffer(struct ibmvnic_adapter *adapter)
1198{
1199 kfree(adapter->login_rsp_buf);
1200 adapter->login_rsp_buf = NULL;
1201}
1202
Nathan Fontenot1b8955e2017-03-30 02:49:29 -04001203static void release_resources(struct ibmvnic_adapter *adapter)
1204{
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001205 release_vpd_data(adapter);
1206
Nathan Fontenot86f669b2018-02-19 13:30:39 -06001207 release_napi(adapter);
Lijun Pana0c8be52020-12-19 15:39:19 -06001208 release_login_buffer(adapter);
Thomas Falcon34f0f4e2018-02-13 18:23:40 -06001209 release_login_rsp_buffer(adapter);
Nathan Fontenot1b8955e2017-03-30 02:49:29 -04001210}
1211
Nathan Fontenot53da09e2017-04-21 15:39:04 -04001212static int set_link_state(struct ibmvnic_adapter *adapter, u8 link_state)
1213{
1214 struct net_device *netdev = adapter->netdev;
Dany Madden98c41f02020-11-25 18:04:32 -06001215 unsigned long timeout = msecs_to_jiffies(20000);
Nathan Fontenot53da09e2017-04-21 15:39:04 -04001216 union ibmvnic_crq crq;
1217 bool resend;
1218 int rc;
1219
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001220 netdev_dbg(netdev, "setting link state %d\n", link_state);
1221
Nathan Fontenot53da09e2017-04-21 15:39:04 -04001222 memset(&crq, 0, sizeof(crq));
1223 crq.logical_link_state.first = IBMVNIC_CRQ_CMD;
1224 crq.logical_link_state.cmd = LOGICAL_LINK_STATE;
1225 crq.logical_link_state.link_state = link_state;
1226
1227 do {
1228 resend = false;
1229
1230 reinit_completion(&adapter->init_done);
1231 rc = ibmvnic_send_crq(adapter, &crq);
1232 if (rc) {
1233 netdev_err(netdev, "Failed to set link state\n");
1234 return rc;
1235 }
1236
1237 if (!wait_for_completion_timeout(&adapter->init_done,
1238 timeout)) {
1239 netdev_err(netdev, "timeout setting link state\n");
Dany Maddenb6ee5662021-12-14 00:17:47 -05001240 return -ETIMEDOUT;
Nathan Fontenot53da09e2017-04-21 15:39:04 -04001241 }
1242
Lijun Pan4c5f6af2020-08-19 17:52:23 -05001243 if (adapter->init_done_rc == PARTIALSUCCESS) {
Nathan Fontenot53da09e2017-04-21 15:39:04 -04001244 /* Partuial success, delay and re-send */
1245 mdelay(1000);
1246 resend = true;
Thomas Falconab5ec332018-05-23 13:37:59 -05001247 } else if (adapter->init_done_rc) {
1248 netdev_warn(netdev, "Unable to set link state, rc=%d\n",
1249 adapter->init_done_rc);
1250 return adapter->init_done_rc;
Nathan Fontenot53da09e2017-04-21 15:39:04 -04001251 }
1252 } while (resend);
1253
1254 return 0;
1255}
1256
Thomas Falcon7f3c6e62017-04-21 15:38:40 -04001257static int set_real_num_queues(struct net_device *netdev)
1258{
1259 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1260 int rc;
1261
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001262 netdev_dbg(netdev, "Setting real tx/rx queues (%llx/%llx)\n",
1263 adapter->req_tx_queues, adapter->req_rx_queues);
1264
Thomas Falcon7f3c6e62017-04-21 15:38:40 -04001265 rc = netif_set_real_num_tx_queues(netdev, adapter->req_tx_queues);
1266 if (rc) {
1267 netdev_err(netdev, "failed to set the number of tx queues\n");
1268 return rc;
1269 }
1270
1271 rc = netif_set_real_num_rx_queues(netdev, adapter->req_rx_queues);
1272 if (rc)
1273 netdev_err(netdev, "failed to set the number of rx queues\n");
1274
1275 return rc;
1276}
1277
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001278static int ibmvnic_get_vpd(struct ibmvnic_adapter *adapter)
1279{
1280 struct device *dev = &adapter->vdev->dev;
1281 union ibmvnic_crq crq;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001282 int len = 0;
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05001283 int rc;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001284
1285 if (adapter->vpd->buff)
1286 len = adapter->vpd->len;
1287
Thomas Falconff25dcb2019-11-25 17:12:56 -06001288 mutex_lock(&adapter->fw_lock);
1289 adapter->fw_done_rc = 0;
Thomas Falcon070eca92019-11-25 17:12:53 -06001290 reinit_completion(&adapter->fw_done);
Thomas Falconff25dcb2019-11-25 17:12:56 -06001291
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001292 crq.get_vpd_size.first = IBMVNIC_CRQ_CMD;
1293 crq.get_vpd_size.cmd = GET_VPD_SIZE;
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05001294 rc = ibmvnic_send_crq(adapter, &crq);
Thomas Falconff25dcb2019-11-25 17:12:56 -06001295 if (rc) {
1296 mutex_unlock(&adapter->fw_lock);
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05001297 return rc;
Thomas Falconff25dcb2019-11-25 17:12:56 -06001298 }
Thomas Falcon476d96c2019-11-25 17:12:55 -06001299
1300 rc = ibmvnic_wait_for_completion(adapter, &adapter->fw_done, 10000);
1301 if (rc) {
1302 dev_err(dev, "Could not retrieve VPD size, rc = %d\n", rc);
Thomas Falconff25dcb2019-11-25 17:12:56 -06001303 mutex_unlock(&adapter->fw_lock);
Thomas Falcon476d96c2019-11-25 17:12:55 -06001304 return rc;
1305 }
Thomas Falconff25dcb2019-11-25 17:12:56 -06001306 mutex_unlock(&adapter->fw_lock);
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001307
1308 if (!adapter->vpd->len)
1309 return -ENODATA;
1310
1311 if (!adapter->vpd->buff)
1312 adapter->vpd->buff = kzalloc(adapter->vpd->len, GFP_KERNEL);
1313 else if (adapter->vpd->len != len)
1314 adapter->vpd->buff =
1315 krealloc(adapter->vpd->buff,
1316 adapter->vpd->len, GFP_KERNEL);
1317
1318 if (!adapter->vpd->buff) {
1319 dev_err(dev, "Could allocate VPD buffer\n");
1320 return -ENOMEM;
1321 }
1322
1323 adapter->vpd->dma_addr =
1324 dma_map_single(dev, adapter->vpd->buff, adapter->vpd->len,
1325 DMA_FROM_DEVICE);
Desnes Augusto Nunes do Rosariof7431062017-11-17 09:09:04 -02001326 if (dma_mapping_error(dev, adapter->vpd->dma_addr)) {
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001327 dev_err(dev, "Could not map VPD buffer\n");
1328 kfree(adapter->vpd->buff);
Thomas Falconb0992ec2018-02-06 17:25:23 -06001329 adapter->vpd->buff = NULL;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001330 return -ENOMEM;
1331 }
1332
Thomas Falconff25dcb2019-11-25 17:12:56 -06001333 mutex_lock(&adapter->fw_lock);
1334 adapter->fw_done_rc = 0;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001335 reinit_completion(&adapter->fw_done);
Thomas Falconff25dcb2019-11-25 17:12:56 -06001336
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001337 crq.get_vpd.first = IBMVNIC_CRQ_CMD;
1338 crq.get_vpd.cmd = GET_VPD;
1339 crq.get_vpd.ioba = cpu_to_be32(adapter->vpd->dma_addr);
1340 crq.get_vpd.len = cpu_to_be32((u32)adapter->vpd->len);
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05001341 rc = ibmvnic_send_crq(adapter, &crq);
1342 if (rc) {
1343 kfree(adapter->vpd->buff);
1344 adapter->vpd->buff = NULL;
Thomas Falconff25dcb2019-11-25 17:12:56 -06001345 mutex_unlock(&adapter->fw_lock);
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05001346 return rc;
1347 }
Thomas Falcon476d96c2019-11-25 17:12:55 -06001348
1349 rc = ibmvnic_wait_for_completion(adapter, &adapter->fw_done, 10000);
1350 if (rc) {
1351 dev_err(dev, "Unable to retrieve VPD, rc = %d\n", rc);
1352 kfree(adapter->vpd->buff);
1353 adapter->vpd->buff = NULL;
Thomas Falconff25dcb2019-11-25 17:12:56 -06001354 mutex_unlock(&adapter->fw_lock);
Thomas Falcon476d96c2019-11-25 17:12:55 -06001355 return rc;
1356 }
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001357
Thomas Falconff25dcb2019-11-25 17:12:56 -06001358 mutex_unlock(&adapter->fw_lock);
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001359 return 0;
1360}
1361
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001362static int init_resources(struct ibmvnic_adapter *adapter)
John Allena57a5d22017-03-17 17:13:41 -05001363{
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001364 struct net_device *netdev = adapter->netdev;
Nathan Fontenot86f669b2018-02-19 13:30:39 -06001365 int rc;
John Allena57a5d22017-03-17 17:13:41 -05001366
Thomas Falcon7f3c6e62017-04-21 15:38:40 -04001367 rc = set_real_num_queues(netdev);
1368 if (rc)
1369 return rc;
John Allenbd0b6722017-03-17 17:13:40 -05001370
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001371 adapter->vpd = kzalloc(sizeof(*adapter->vpd), GFP_KERNEL);
1372 if (!adapter->vpd)
1373 return -ENOMEM;
1374
John Allen69d08dc2018-01-18 16:27:58 -06001375 /* Vital Product Data (VPD) */
1376 rc = ibmvnic_get_vpd(adapter);
1377 if (rc) {
1378 netdev_err(netdev, "failed to initialize Vital Product Data (VPD)\n");
1379 return rc;
1380 }
1381
Nathan Fontenot86f669b2018-02-19 13:30:39 -06001382 rc = init_napi(adapter);
1383 if (rc)
1384 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001385
Lijun Pan69980d02020-09-27 20:13:28 -05001386 send_query_map(adapter);
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -04001387
1388 rc = init_rx_pools(netdev);
1389 if (rc)
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001390 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001391
Nathan Fontenotc657e322017-03-30 02:49:06 -04001392 rc = init_tx_pools(netdev);
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001393 return rc;
1394}
1395
Nathan Fontenoted651a12017-05-03 14:04:38 -04001396static int __ibmvnic_open(struct net_device *netdev)
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001397{
1398 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001399 enum vnic_state prev_state = adapter->state;
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001400 int i, rc;
1401
Nathan Fontenot90c80142017-05-03 14:04:32 -04001402 adapter->state = VNIC_OPENING;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001403 replenish_pools(adapter);
John Allend944c3d62017-05-26 10:30:13 -04001404 ibmvnic_napi_enable(adapter);
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001405
Thomas Falcon032c5e82015-12-21 11:26:06 -06001406 /* We're ready to receive frames, enable the sub-crq interrupts and
1407 * set the logical link state to up
1408 */
Nathan Fontenoted651a12017-05-03 14:04:38 -04001409 for (i = 0; i < adapter->req_rx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001410 netdev_dbg(netdev, "Enabling rx_scrq[%d] irq\n", i);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001411 if (prev_state == VNIC_CLOSED)
1412 enable_irq(adapter->rx_scrq[i]->irq);
Thomas Falconf23e0642018-04-15 18:53:36 -05001413 enable_scrq_irq(adapter, adapter->rx_scrq[i]);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001414 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001415
Nathan Fontenoted651a12017-05-03 14:04:38 -04001416 for (i = 0; i < adapter->req_tx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001417 netdev_dbg(netdev, "Enabling tx_scrq[%d] irq\n", i);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001418 if (prev_state == VNIC_CLOSED)
1419 enable_irq(adapter->tx_scrq[i]->irq);
Thomas Falconf23e0642018-04-15 18:53:36 -05001420 enable_scrq_irq(adapter, adapter->tx_scrq[i]);
Thomas Falcon0d973382020-11-18 19:12:19 -06001421 netdev_tx_reset_queue(netdev_get_tx_queue(netdev, i));
Nathan Fontenoted651a12017-05-03 14:04:38 -04001422 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001423
Nathan Fontenot53da09e2017-04-21 15:39:04 -04001424 rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_UP);
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001425 if (rc) {
Lijun Pan0775ebc2021-04-14 02:46:14 -05001426 ibmvnic_napi_disable(adapter);
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001427 release_resources(adapter);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001428 return rc;
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001429 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001430
Nathan Fontenoted651a12017-05-03 14:04:38 -04001431 netif_tx_start_all_queues(netdev);
1432
Dany Madden2ca220f2021-06-23 21:13:11 -07001433 if (prev_state == VNIC_CLOSED) {
1434 for (i = 0; i < adapter->req_rx_queues; i++)
1435 napi_schedule(&adapter->napi[i]);
1436 }
1437
Nathan Fontenoted651a12017-05-03 14:04:38 -04001438 adapter->state = VNIC_OPEN;
1439 return rc;
1440}
1441
1442static int ibmvnic_open(struct net_device *netdev)
1443{
1444 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
John Allen69d08dc2018-01-18 16:27:58 -06001445 int rc;
Nathan Fontenoted651a12017-05-03 14:04:38 -04001446
Sukadev Bhattiprolu8f1c0fd2021-02-23 21:02:29 -08001447 ASSERT_RTNL();
1448
1449 /* If device failover is pending or we are about to reset, just set
1450 * device state and return. Device operation will be handled by reset
1451 * routine.
1452 *
1453 * It should be safe to overwrite the adapter->state here. Since
1454 * we hold the rtnl, either the reset has not actually started or
1455 * the rtnl got dropped during the set_link_state() in do_reset().
1456 * In the former case, no one else is changing the state (again we
1457 * have the rtnl) and in the latter case, do_reset() will detect and
1458 * honor our setting below.
Thomas Falcon5a18e1e2018-04-06 18:37:05 -05001459 */
Sukadev Bhattiprolu8f1c0fd2021-02-23 21:02:29 -08001460 if (adapter->failover_pending || (test_bit(0, &adapter->resetting))) {
Lijun Pan0666ef72021-04-12 02:41:28 -05001461 netdev_dbg(netdev, "[S:%s FOP:%d] Resetting, deferring open\n",
1462 adapter_state_to_string(adapter->state),
1463 adapter->failover_pending);
Thomas Falcon5a18e1e2018-04-06 18:37:05 -05001464 adapter->state = VNIC_OPEN;
Sukadev Bhattiprolu8f1c0fd2021-02-23 21:02:29 -08001465 rc = 0;
1466 goto out;
Thomas Falcon5a18e1e2018-04-06 18:37:05 -05001467 }
1468
Nathan Fontenoted651a12017-05-03 14:04:38 -04001469 if (adapter->state != VNIC_CLOSED) {
1470 rc = ibmvnic_login(netdev);
Juliet Kima5681e22018-11-19 15:59:22 -06001471 if (rc)
Sukadev Bhattiprolu1d850492020-10-30 10:07:11 -07001472 goto out;
Nathan Fontenoted651a12017-05-03 14:04:38 -04001473
1474 rc = init_resources(adapter);
1475 if (rc) {
1476 netdev_err(netdev, "failed to initialize resources\n");
1477 release_resources(adapter);
Sukadev Bhattiprolu489de952021-09-14 20:52:58 -07001478 release_rx_pools(adapter);
Sukadev Bhattiprolubbd80932021-09-14 20:52:59 -07001479 release_tx_pools(adapter);
Sukadev Bhattiprolu1d850492020-10-30 10:07:11 -07001480 goto out;
Nathan Fontenoted651a12017-05-03 14:04:38 -04001481 }
1482 }
1483
1484 rc = __ibmvnic_open(netdev);
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001485
Sukadev Bhattiprolu1d850492020-10-30 10:07:11 -07001486out:
Sukadev Bhattiprolu8f1c0fd2021-02-23 21:02:29 -08001487 /* If open failed and there is a pending failover or in-progress reset,
1488 * set device state and return. Device operation will be handled by
1489 * reset routine. See also comments above regarding rtnl.
Sukadev Bhattiprolu1d850492020-10-30 10:07:11 -07001490 */
Sukadev Bhattiprolu8f1c0fd2021-02-23 21:02:29 -08001491 if (rc &&
1492 (adapter->failover_pending || (test_bit(0, &adapter->resetting)))) {
Sukadev Bhattiprolu1d850492020-10-30 10:07:11 -07001493 adapter->state = VNIC_OPEN;
1494 rc = 0;
1495 }
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001496 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001497}
1498
Thomas Falcond0869c02018-02-13 18:23:43 -06001499static void clean_rx_pools(struct ibmvnic_adapter *adapter)
1500{
1501 struct ibmvnic_rx_pool *rx_pool;
Thomas Falcon637f81d2018-02-26 18:10:57 -06001502 struct ibmvnic_rx_buff *rx_buff;
Thomas Falcond0869c02018-02-13 18:23:43 -06001503 u64 rx_entries;
1504 int rx_scrqs;
1505 int i, j;
1506
1507 if (!adapter->rx_pool)
1508 return;
1509
Thomas Falcon660e3092018-04-20 14:25:32 -05001510 rx_scrqs = adapter->num_active_rx_pools;
Thomas Falcond0869c02018-02-13 18:23:43 -06001511 rx_entries = adapter->req_rx_add_entries_per_subcrq;
1512
1513 /* Free any remaining skbs in the rx buffer pools */
1514 for (i = 0; i < rx_scrqs; i++) {
1515 rx_pool = &adapter->rx_pool[i];
Thomas Falcon637f81d2018-02-26 18:10:57 -06001516 if (!rx_pool || !rx_pool->rx_buff)
Thomas Falcond0869c02018-02-13 18:23:43 -06001517 continue;
1518
1519 netdev_dbg(adapter->netdev, "Cleaning rx_pool[%d]\n", i);
1520 for (j = 0; j < rx_entries; j++) {
Thomas Falcon637f81d2018-02-26 18:10:57 -06001521 rx_buff = &rx_pool->rx_buff[j];
1522 if (rx_buff && rx_buff->skb) {
1523 dev_kfree_skb_any(rx_buff->skb);
1524 rx_buff->skb = NULL;
Thomas Falcond0869c02018-02-13 18:23:43 -06001525 }
1526 }
1527 }
1528}
1529
Thomas Falcone9e1e972018-03-16 20:00:30 -05001530static void clean_one_tx_pool(struct ibmvnic_adapter *adapter,
1531 struct ibmvnic_tx_pool *tx_pool)
Nathan Fontenotb41b83e2017-05-03 14:04:56 -04001532{
Thomas Falcon637f81d2018-02-26 18:10:57 -06001533 struct ibmvnic_tx_buff *tx_buff;
Nathan Fontenotb41b83e2017-05-03 14:04:56 -04001534 u64 tx_entries;
Thomas Falcone9e1e972018-03-16 20:00:30 -05001535 int i;
Nathan Fontenotb41b83e2017-05-03 14:04:56 -04001536
Dan Carpenter050e85c2018-03-23 14:36:15 +03001537 if (!tx_pool || !tx_pool->tx_buff)
Thomas Falcone9e1e972018-03-16 20:00:30 -05001538 return;
1539
1540 tx_entries = tx_pool->num_buffers;
1541
1542 for (i = 0; i < tx_entries; i++) {
1543 tx_buff = &tx_pool->tx_buff[i];
1544 if (tx_buff && tx_buff->skb) {
1545 dev_kfree_skb_any(tx_buff->skb);
1546 tx_buff->skb = NULL;
1547 }
1548 }
1549}
1550
1551static void clean_tx_pools(struct ibmvnic_adapter *adapter)
1552{
1553 int tx_scrqs;
1554 int i;
1555
1556 if (!adapter->tx_pool || !adapter->tso_pool)
Nathan Fontenotb41b83e2017-05-03 14:04:56 -04001557 return;
1558
Thomas Falcon660e3092018-04-20 14:25:32 -05001559 tx_scrqs = adapter->num_active_tx_pools;
Nathan Fontenotb41b83e2017-05-03 14:04:56 -04001560
1561 /* Free any remaining skbs in the tx buffer pools */
1562 for (i = 0; i < tx_scrqs; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001563 netdev_dbg(adapter->netdev, "Cleaning tx_pool[%d]\n", i);
Thomas Falcone9e1e972018-03-16 20:00:30 -05001564 clean_one_tx_pool(adapter, &adapter->tx_pool[i]);
1565 clean_one_tx_pool(adapter, &adapter->tso_pool[i]);
Nathan Fontenotb41b83e2017-05-03 14:04:56 -04001566 }
1567}
1568
John Allen6095e592018-03-30 13:44:21 -05001569static void ibmvnic_disable_irqs(struct ibmvnic_adapter *adapter)
John Allenea5509f2017-03-17 17:13:43 -05001570{
John Allen6095e592018-03-30 13:44:21 -05001571 struct net_device *netdev = adapter->netdev;
John Allenea5509f2017-03-17 17:13:43 -05001572 int i;
1573
Nathan Fontenot46293b92017-05-03 14:05:02 -04001574 if (adapter->tx_scrq) {
1575 for (i = 0; i < adapter->req_tx_queues; i++)
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001576 if (adapter->tx_scrq[i]->irq) {
Thomas Falconf8738662018-03-07 17:51:45 -06001577 netdev_dbg(netdev,
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001578 "Disabling tx_scrq[%d] irq\n", i);
Thomas Falconf23e0642018-04-15 18:53:36 -05001579 disable_scrq_irq(adapter, adapter->tx_scrq[i]);
Nathan Fontenot46293b92017-05-03 14:05:02 -04001580 disable_irq(adapter->tx_scrq[i]->irq);
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001581 }
Nathan Fontenot46293b92017-05-03 14:05:02 -04001582 }
1583
Nathan Fontenot46293b92017-05-03 14:05:02 -04001584 if (adapter->rx_scrq) {
1585 for (i = 0; i < adapter->req_rx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001586 if (adapter->rx_scrq[i]->irq) {
Thomas Falconf8738662018-03-07 17:51:45 -06001587 netdev_dbg(netdev,
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001588 "Disabling rx_scrq[%d] irq\n", i);
Thomas Falconf23e0642018-04-15 18:53:36 -05001589 disable_scrq_irq(adapter, adapter->rx_scrq[i]);
Nathan Fontenot46293b92017-05-03 14:05:02 -04001590 disable_irq(adapter->rx_scrq[i]->irq);
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001591 }
Nathan Fontenot46293b92017-05-03 14:05:02 -04001592 }
1593 }
John Allen6095e592018-03-30 13:44:21 -05001594}
1595
1596static void ibmvnic_cleanup(struct net_device *netdev)
1597{
1598 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1599
1600 /* ensure that transmissions are stopped if called by do_reset */
Juliet Kim7ed5b312019-09-20 16:11:23 -04001601 if (test_bit(0, &adapter->resetting))
John Allen6095e592018-03-30 13:44:21 -05001602 netif_tx_disable(netdev);
1603 else
1604 netif_tx_stop_all_queues(netdev);
1605
1606 ibmvnic_napi_disable(adapter);
1607 ibmvnic_disable_irqs(adapter);
Thomas Falcon01d9bd72018-03-07 17:51:46 -06001608}
1609
1610static int __ibmvnic_close(struct net_device *netdev)
1611{
1612 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1613 int rc = 0;
1614
1615 adapter->state = VNIC_CLOSING;
1616 rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_DN);
Nathan Fontenot90c80142017-05-03 14:04:32 -04001617 adapter->state = VNIC_CLOSED;
Sukadev Bhattiprolud4083d32021-02-10 17:41:43 -08001618 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001619}
1620
Nathan Fontenoted651a12017-05-03 14:04:38 -04001621static int ibmvnic_close(struct net_device *netdev)
1622{
1623 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1624 int rc;
1625
Lijun Pan0666ef72021-04-12 02:41:28 -05001626 netdev_dbg(netdev, "[S:%s FOP:%d FRR:%d] Closing\n",
1627 adapter_state_to_string(adapter->state),
1628 adapter->failover_pending,
Sukadev Bhattiprolu38bd5ce2020-12-04 18:22:35 -08001629 adapter->force_reset_recovery);
1630
Thomas Falcon5a18e1e2018-04-06 18:37:05 -05001631 /* If device failover is pending, just set device state and return.
1632 * Device operation will be handled by reset routine.
1633 */
1634 if (adapter->failover_pending) {
1635 adapter->state = VNIC_CLOSED;
1636 return 0;
1637 }
1638
Nathan Fontenoted651a12017-05-03 14:04:38 -04001639 rc = __ibmvnic_close(netdev);
Nathan Fontenot30f79622018-04-06 18:37:06 -05001640 ibmvnic_cleanup(netdev);
Sukadev Bhattiprolu489de952021-09-14 20:52:58 -07001641 clean_rx_pools(adapter);
Sukadev Bhattiprolubbd80932021-09-14 20:52:59 -07001642 clean_tx_pools(adapter);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001643
1644 return rc;
1645}
1646
Thomas Falconad7775d2016-04-01 17:20:34 -05001647/**
1648 * build_hdr_data - creates L2/L3/L4 header data buffer
Lee Jones80708602021-01-15 20:09:03 +00001649 * @hdr_field: bitfield determining needed headers
1650 * @skb: socket buffer
1651 * @hdr_len: array of header lengths
1652 * @hdr_data: buffer to write the header to
Thomas Falconad7775d2016-04-01 17:20:34 -05001653 *
1654 * Reads hdr_field to determine which headers are needed by firmware.
1655 * Builds a buffer containing these headers. Saves individual header
1656 * lengths and total buffer length to be used to build descriptors.
1657 */
1658static int build_hdr_data(u8 hdr_field, struct sk_buff *skb,
1659 int *hdr_len, u8 *hdr_data)
1660{
1661 int len = 0;
1662 u8 *hdr;
1663
Thomas Falconda75e3b2018-03-12 11:51:02 -05001664 if (skb_vlan_tagged(skb) && !skb_vlan_tag_present(skb))
1665 hdr_len[0] = sizeof(struct vlan_ethhdr);
1666 else
1667 hdr_len[0] = sizeof(struct ethhdr);
Thomas Falconad7775d2016-04-01 17:20:34 -05001668
1669 if (skb->protocol == htons(ETH_P_IP)) {
1670 hdr_len[1] = ip_hdr(skb)->ihl * 4;
1671 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
1672 hdr_len[2] = tcp_hdrlen(skb);
1673 else if (ip_hdr(skb)->protocol == IPPROTO_UDP)
1674 hdr_len[2] = sizeof(struct udphdr);
1675 } else if (skb->protocol == htons(ETH_P_IPV6)) {
1676 hdr_len[1] = sizeof(struct ipv6hdr);
1677 if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
1678 hdr_len[2] = tcp_hdrlen(skb);
1679 else if (ipv6_hdr(skb)->nexthdr == IPPROTO_UDP)
1680 hdr_len[2] = sizeof(struct udphdr);
Thomas Falcon4eb50ce2017-12-18 12:52:40 -06001681 } else if (skb->protocol == htons(ETH_P_ARP)) {
1682 hdr_len[1] = arp_hdr_len(skb->dev);
1683 hdr_len[2] = 0;
Thomas Falconad7775d2016-04-01 17:20:34 -05001684 }
1685
1686 memset(hdr_data, 0, 120);
1687 if ((hdr_field >> 6) & 1) {
1688 hdr = skb_mac_header(skb);
1689 memcpy(hdr_data, hdr, hdr_len[0]);
1690 len += hdr_len[0];
1691 }
1692
1693 if ((hdr_field >> 5) & 1) {
1694 hdr = skb_network_header(skb);
1695 memcpy(hdr_data + len, hdr, hdr_len[1]);
1696 len += hdr_len[1];
1697 }
1698
1699 if ((hdr_field >> 4) & 1) {
1700 hdr = skb_transport_header(skb);
1701 memcpy(hdr_data + len, hdr, hdr_len[2]);
1702 len += hdr_len[2];
1703 }
1704 return len;
1705}
1706
1707/**
1708 * create_hdr_descs - create header and header extension descriptors
Lee Jones80708602021-01-15 20:09:03 +00001709 * @hdr_field: bitfield determining needed headers
1710 * @hdr_data: buffer containing header data
1711 * @len: length of data buffer
1712 * @hdr_len: array of individual header lengths
1713 * @scrq_arr: descriptor array
Thomas Falconad7775d2016-04-01 17:20:34 -05001714 *
1715 * Creates header and, if needed, header extension descriptors and
1716 * places them in a descriptor array, scrq_arr
1717 */
1718
Thomas Falcon2de09682017-10-16 10:02:11 -05001719static int create_hdr_descs(u8 hdr_field, u8 *hdr_data, int len, int *hdr_len,
1720 union sub_crq *scrq_arr)
Thomas Falconad7775d2016-04-01 17:20:34 -05001721{
1722 union sub_crq hdr_desc;
1723 int tmp_len = len;
Thomas Falcon2de09682017-10-16 10:02:11 -05001724 int num_descs = 0;
Thomas Falconad7775d2016-04-01 17:20:34 -05001725 u8 *data, *cur;
1726 int tmp;
1727
1728 while (tmp_len > 0) {
1729 cur = hdr_data + len - tmp_len;
1730
1731 memset(&hdr_desc, 0, sizeof(hdr_desc));
1732 if (cur != hdr_data) {
1733 data = hdr_desc.hdr_ext.data;
1734 tmp = tmp_len > 29 ? 29 : tmp_len;
1735 hdr_desc.hdr_ext.first = IBMVNIC_CRQ_CMD;
1736 hdr_desc.hdr_ext.type = IBMVNIC_HDR_EXT_DESC;
1737 hdr_desc.hdr_ext.len = tmp;
1738 } else {
1739 data = hdr_desc.hdr.data;
1740 tmp = tmp_len > 24 ? 24 : tmp_len;
1741 hdr_desc.hdr.first = IBMVNIC_CRQ_CMD;
1742 hdr_desc.hdr.type = IBMVNIC_HDR_DESC;
1743 hdr_desc.hdr.len = tmp;
1744 hdr_desc.hdr.l2_len = (u8)hdr_len[0];
1745 hdr_desc.hdr.l3_len = cpu_to_be16((u16)hdr_len[1]);
1746 hdr_desc.hdr.l4_len = (u8)hdr_len[2];
1747 hdr_desc.hdr.flag = hdr_field << 1;
1748 }
1749 memcpy(data, cur, tmp);
1750 tmp_len -= tmp;
1751 *scrq_arr = hdr_desc;
1752 scrq_arr++;
Thomas Falcon2de09682017-10-16 10:02:11 -05001753 num_descs++;
Thomas Falconad7775d2016-04-01 17:20:34 -05001754 }
Thomas Falcon2de09682017-10-16 10:02:11 -05001755
1756 return num_descs;
Thomas Falconad7775d2016-04-01 17:20:34 -05001757}
1758
1759/**
1760 * build_hdr_descs_arr - build a header descriptor array
Lijun Pan73214a62021-06-11 10:43:39 -05001761 * @skb: tx socket buffer
1762 * @indir_arr: indirect array
Lee Jones80708602021-01-15 20:09:03 +00001763 * @num_entries: number of descriptors to be sent
1764 * @hdr_field: bit field determining which headers will be sent
Thomas Falconad7775d2016-04-01 17:20:34 -05001765 *
1766 * This function will build a TX descriptor array with applicable
1767 * L2/L3/L4 packet header descriptors to be sent by send_subcrq_indirect.
1768 */
1769
Thomas Falconc62aa372020-11-18 19:12:20 -06001770static void build_hdr_descs_arr(struct sk_buff *skb,
1771 union sub_crq *indir_arr,
Thomas Falconad7775d2016-04-01 17:20:34 -05001772 int *num_entries, u8 hdr_field)
1773{
1774 int hdr_len[3] = {0, 0, 0};
Thomas Falconc62aa372020-11-18 19:12:20 -06001775 u8 hdr_data[140] = {0};
Thomas Falcon2de09682017-10-16 10:02:11 -05001776 int tot_len;
Thomas Falconad7775d2016-04-01 17:20:34 -05001777
Thomas Falconc62aa372020-11-18 19:12:20 -06001778 tot_len = build_hdr_data(hdr_field, skb, hdr_len,
1779 hdr_data);
Thomas Falcon2de09682017-10-16 10:02:11 -05001780 *num_entries += create_hdr_descs(hdr_field, hdr_data, tot_len, hdr_len,
Thomas Falconc62aa372020-11-18 19:12:20 -06001781 indir_arr + 1);
Thomas Falconad7775d2016-04-01 17:20:34 -05001782}
1783
Thomas Falcon1f247a62018-03-12 11:51:04 -05001784static int ibmvnic_xmit_workarounds(struct sk_buff *skb,
1785 struct net_device *netdev)
1786{
1787 /* For some backing devices, mishandling of small packets
1788 * can result in a loss of connection or TX stall. Device
1789 * architects recommend that no packet should be smaller
1790 * than the minimum MTU value provided to the driver, so
1791 * pad any packets to that length
1792 */
1793 if (skb->len < netdev->min_mtu)
1794 return skb_put_padto(skb, netdev->min_mtu);
Thomas Falcon7083a452018-03-12 21:05:26 -05001795
1796 return 0;
Thomas Falcon1f247a62018-03-12 11:51:04 -05001797}
1798
Thomas Falcon0d973382020-11-18 19:12:19 -06001799static void ibmvnic_tx_scrq_clean_buffer(struct ibmvnic_adapter *adapter,
1800 struct ibmvnic_sub_crq_queue *tx_scrq)
1801{
1802 struct ibmvnic_ind_xmit_queue *ind_bufp;
1803 struct ibmvnic_tx_buff *tx_buff;
1804 struct ibmvnic_tx_pool *tx_pool;
1805 union sub_crq tx_scrq_entry;
1806 int queue_num;
1807 int entries;
1808 int index;
1809 int i;
1810
1811 ind_bufp = &tx_scrq->ind_buf;
1812 entries = (u64)ind_bufp->index;
1813 queue_num = tx_scrq->pool_index;
1814
1815 for (i = entries - 1; i >= 0; --i) {
1816 tx_scrq_entry = ind_bufp->indir_arr[i];
1817 if (tx_scrq_entry.v1.type != IBMVNIC_TX_DESC)
1818 continue;
1819 index = be32_to_cpu(tx_scrq_entry.v1.correlator);
1820 if (index & IBMVNIC_TSO_POOL_MASK) {
1821 tx_pool = &adapter->tso_pool[queue_num];
1822 index &= ~IBMVNIC_TSO_POOL_MASK;
1823 } else {
1824 tx_pool = &adapter->tx_pool[queue_num];
1825 }
1826 tx_pool->free_map[tx_pool->consumer_index] = index;
1827 tx_pool->consumer_index = tx_pool->consumer_index == 0 ?
1828 tx_pool->num_buffers - 1 :
1829 tx_pool->consumer_index - 1;
1830 tx_buff = &tx_pool->tx_buff[index];
1831 adapter->netdev->stats.tx_packets--;
1832 adapter->netdev->stats.tx_bytes -= tx_buff->skb->len;
1833 adapter->tx_stats_buffers[queue_num].packets--;
1834 adapter->tx_stats_buffers[queue_num].bytes -=
1835 tx_buff->skb->len;
1836 dev_kfree_skb_any(tx_buff->skb);
1837 tx_buff->skb = NULL;
1838 adapter->netdev->stats.tx_dropped++;
1839 }
1840 ind_bufp->index = 0;
1841 if (atomic_sub_return(entries, &tx_scrq->used) <=
1842 (adapter->req_tx_entries_per_subcrq / 2) &&
Sukadev Bhattiprolu65d64702021-06-23 21:13:12 -07001843 __netif_subqueue_stopped(adapter->netdev, queue_num) &&
1844 !test_bit(0, &adapter->resetting)) {
Thomas Falcon0d973382020-11-18 19:12:19 -06001845 netif_wake_subqueue(adapter->netdev, queue_num);
1846 netdev_dbg(adapter->netdev, "Started queue %d\n",
1847 queue_num);
1848 }
1849}
1850
1851static int ibmvnic_tx_scrq_flush(struct ibmvnic_adapter *adapter,
1852 struct ibmvnic_sub_crq_queue *tx_scrq)
1853{
1854 struct ibmvnic_ind_xmit_queue *ind_bufp;
1855 u64 dma_addr;
1856 u64 entries;
1857 u64 handle;
1858 int rc;
1859
1860 ind_bufp = &tx_scrq->ind_buf;
1861 dma_addr = (u64)ind_bufp->indir_dma;
1862 entries = (u64)ind_bufp->index;
1863 handle = tx_scrq->handle;
1864
1865 if (!entries)
1866 return 0;
1867 rc = send_subcrq_indirect(adapter, handle, dma_addr, entries);
1868 if (rc)
1869 ibmvnic_tx_scrq_clean_buffer(adapter, tx_scrq);
1870 else
1871 ind_bufp->index = 0;
1872 return 0;
1873}
1874
YueHaibing94b2bb22018-09-18 14:35:47 +08001875static netdev_tx_t ibmvnic_xmit(struct sk_buff *skb, struct net_device *netdev)
Thomas Falcon032c5e82015-12-21 11:26:06 -06001876{
1877 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1878 int queue_num = skb_get_queue_mapping(skb);
Thomas Falconad7775d2016-04-01 17:20:34 -05001879 u8 *hdrs = (u8 *)&adapter->tx_rx_desc_req;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001880 struct device *dev = &adapter->vdev->dev;
Thomas Falcon0d973382020-11-18 19:12:19 -06001881 struct ibmvnic_ind_xmit_queue *ind_bufp;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001882 struct ibmvnic_tx_buff *tx_buff = NULL;
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001883 struct ibmvnic_sub_crq_queue *tx_scrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001884 struct ibmvnic_tx_pool *tx_pool;
1885 unsigned int tx_send_failed = 0;
Thomas Falcon0d973382020-11-18 19:12:19 -06001886 netdev_tx_t ret = NETDEV_TX_OK;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001887 unsigned int tx_map_failed = 0;
Thomas Falconc62aa372020-11-18 19:12:20 -06001888 union sub_crq indir_arr[16];
Thomas Falcon032c5e82015-12-21 11:26:06 -06001889 unsigned int tx_dropped = 0;
1890 unsigned int tx_packets = 0;
1891 unsigned int tx_bytes = 0;
1892 dma_addr_t data_dma_addr;
1893 struct netdev_queue *txq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001894 unsigned long lpar_rc;
1895 union sub_crq tx_crq;
1896 unsigned int offset;
Thomas Falconad7775d2016-04-01 17:20:34 -05001897 int num_entries = 1;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001898 unsigned char *dst;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001899 int index = 0;
Thomas Falcona0dca102018-01-18 19:29:48 -06001900 u8 proto = 0;
Thomas Falcon0d973382020-11-18 19:12:19 -06001901
1902 tx_scrq = adapter->tx_scrq[queue_num];
1903 txq = netdev_get_tx_queue(netdev, queue_num);
1904 ind_bufp = &tx_scrq->ind_buf;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001905
Juliet Kim7ed5b312019-09-20 16:11:23 -04001906 if (test_bit(0, &adapter->resetting)) {
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001907 dev_kfree_skb_any(skb);
1908
Thomas Falcon032c5e82015-12-21 11:26:06 -06001909 tx_send_failed++;
1910 tx_dropped++;
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001911 ret = NETDEV_TX_OK;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001912 goto out;
1913 }
1914
Thomas Falcon7083a452018-03-12 21:05:26 -05001915 if (ibmvnic_xmit_workarounds(skb, netdev)) {
Thomas Falcon1f247a62018-03-12 11:51:04 -05001916 tx_dropped++;
1917 tx_send_failed++;
1918 ret = NETDEV_TX_OK;
Thomas Falcon0d973382020-11-18 19:12:19 -06001919 ibmvnic_tx_scrq_flush(adapter, tx_scrq);
Thomas Falcon1f247a62018-03-12 11:51:04 -05001920 goto out;
1921 }
Thomas Falcon06b3e352018-03-16 20:00:28 -05001922 if (skb_is_gso(skb))
1923 tx_pool = &adapter->tso_pool[queue_num];
1924 else
1925 tx_pool = &adapter->tx_pool[queue_num];
Thomas Falcon1f247a62018-03-12 11:51:04 -05001926
Thomas Falcon032c5e82015-12-21 11:26:06 -06001927 index = tx_pool->free_map[tx_pool->consumer_index];
Thomas Falconfdb06102017-10-17 12:36:55 -05001928
Thomas Falcon86b61a52018-03-16 20:00:29 -05001929 if (index == IBMVNIC_INVALID_MAP) {
1930 dev_kfree_skb_any(skb);
1931 tx_send_failed++;
1932 tx_dropped++;
Sukadev Bhattiprolubb553622021-07-20 19:34:39 -07001933 ibmvnic_tx_scrq_flush(adapter, tx_scrq);
Thomas Falcon86b61a52018-03-16 20:00:29 -05001934 ret = NETDEV_TX_OK;
1935 goto out;
1936 }
1937
1938 tx_pool->free_map[tx_pool->consumer_index] = IBMVNIC_INVALID_MAP;
1939
Thomas Falcon06b3e352018-03-16 20:00:28 -05001940 offset = index * tx_pool->buf_size;
1941 dst = tx_pool->long_term_buff.buff + offset;
1942 memset(dst, 0, tx_pool->buf_size);
1943 data_dma_addr = tx_pool->long_term_buff.addr + offset;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001944
Thomas Falcon15482052017-10-17 12:36:54 -05001945 if (skb_shinfo(skb)->nr_frags) {
1946 int cur, i;
1947
1948 /* Copy the head */
1949 skb_copy_from_linear_data(skb, dst, skb_headlen(skb));
1950 cur = skb_headlen(skb);
1951
1952 /* Copy the frags */
1953 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1954 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1955
Christophe JAILLETc3105f82021-04-04 10:54:37 +02001956 memcpy(dst + cur, skb_frag_address(frag),
1957 skb_frag_size(frag));
Thomas Falcon15482052017-10-17 12:36:54 -05001958 cur += skb_frag_size(frag);
1959 }
1960 } else {
1961 skb_copy_from_linear_data(skb, dst, skb->len);
1962 }
1963
Lijun Pan42557da2021-02-12 20:48:40 -06001964 /* post changes to long_term_buff *dst before VIOS accessing it */
1965 dma_wmb();
1966
Thomas Falcon032c5e82015-12-21 11:26:06 -06001967 tx_pool->consumer_index =
Thomas Falcon06b3e352018-03-16 20:00:28 -05001968 (tx_pool->consumer_index + 1) % tx_pool->num_buffers;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001969
1970 tx_buff = &tx_pool->tx_buff[index];
1971 tx_buff->skb = skb;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001972 tx_buff->index = index;
1973 tx_buff->pool_index = queue_num;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001974
1975 memset(&tx_crq, 0, sizeof(tx_crq));
1976 tx_crq.v1.first = IBMVNIC_CRQ_CMD;
1977 tx_crq.v1.type = IBMVNIC_TX_DESC;
1978 tx_crq.v1.n_crq_elem = 1;
1979 tx_crq.v1.n_sge = 1;
1980 tx_crq.v1.flags1 = IBMVNIC_TX_COMP_NEEDED;
Thomas Falcon06b3e352018-03-16 20:00:28 -05001981
Thomas Falconfdb06102017-10-17 12:36:55 -05001982 if (skb_is_gso(skb))
Thomas Falcon06b3e352018-03-16 20:00:28 -05001983 tx_crq.v1.correlator =
1984 cpu_to_be32(index | IBMVNIC_TSO_POOL_MASK);
Thomas Falconfdb06102017-10-17 12:36:55 -05001985 else
Thomas Falcon06b3e352018-03-16 20:00:28 -05001986 tx_crq.v1.correlator = cpu_to_be32(index);
1987 tx_crq.v1.dma_reg = cpu_to_be16(tx_pool->long_term_buff.map_id);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001988 tx_crq.v1.sge_len = cpu_to_be32(skb->len);
1989 tx_crq.v1.ioba = cpu_to_be64(data_dma_addr);
1990
Michał Mirosławe84b4792018-11-07 17:50:52 +01001991 if (adapter->vlan_header_insertion && skb_vlan_tag_present(skb)) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06001992 tx_crq.v1.flags2 |= IBMVNIC_TX_VLAN_INSERT;
1993 tx_crq.v1.vlan_id = cpu_to_be16(skb->vlan_tci);
1994 }
1995
1996 if (skb->protocol == htons(ETH_P_IP)) {
Thomas Falcona0dca102018-01-18 19:29:48 -06001997 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV4;
1998 proto = ip_hdr(skb)->protocol;
1999 } else if (skb->protocol == htons(ETH_P_IPV6)) {
2000 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV6;
2001 proto = ipv6_hdr(skb)->nexthdr;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002002 }
2003
Thomas Falcona0dca102018-01-18 19:29:48 -06002004 if (proto == IPPROTO_TCP)
2005 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_TCP;
2006 else if (proto == IPPROTO_UDP)
2007 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_UDP;
2008
Thomas Falconad7775d2016-04-01 17:20:34 -05002009 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06002010 tx_crq.v1.flags1 |= IBMVNIC_TX_CHKSUM_OFFLOAD;
Thomas Falconad7775d2016-04-01 17:20:34 -05002011 hdrs += 2;
2012 }
Thomas Falconfdb06102017-10-17 12:36:55 -05002013 if (skb_is_gso(skb)) {
2014 tx_crq.v1.flags1 |= IBMVNIC_TX_LSO;
2015 tx_crq.v1.mss = cpu_to_be16(skb_shinfo(skb)->gso_size);
2016 hdrs += 2;
2017 }
Thomas Falcon0d973382020-11-18 19:12:19 -06002018
2019 if ((*hdrs >> 7) & 1)
Thomas Falconc62aa372020-11-18 19:12:20 -06002020 build_hdr_descs_arr(skb, indir_arr, &num_entries, *hdrs);
Thomas Falcon0d973382020-11-18 19:12:19 -06002021
2022 tx_crq.v1.n_crq_elem = num_entries;
2023 tx_buff->num_entries = num_entries;
2024 /* flush buffer if current entry can not fit */
2025 if (num_entries + ind_bufp->index > IBMVNIC_MAX_IND_DESCS) {
2026 lpar_rc = ibmvnic_tx_scrq_flush(adapter, tx_scrq);
2027 if (lpar_rc != H_SUCCESS)
2028 goto tx_flush_err;
Thomas Falconad7775d2016-04-01 17:20:34 -05002029 }
Thomas Falcon7f5b0302017-04-21 15:39:16 -04002030
Thomas Falconc62aa372020-11-18 19:12:20 -06002031 indir_arr[0] = tx_crq;
2032 memcpy(&ind_bufp->indir_arr[ind_bufp->index], &indir_arr[0],
Thomas Falcon0d973382020-11-18 19:12:19 -06002033 num_entries * sizeof(struct ibmvnic_generic_scrq));
2034 ind_bufp->index += num_entries;
2035 if (__netdev_tx_sent_queue(txq, skb->len,
2036 netdev_xmit_more() &&
2037 ind_bufp->index < IBMVNIC_MAX_IND_DESCS)) {
2038 lpar_rc = ibmvnic_tx_scrq_flush(adapter, tx_scrq);
2039 if (lpar_rc != H_SUCCESS)
2040 goto tx_err;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002041 }
Thomas Falcon142c0ac2017-03-05 12:18:41 -06002042
Thomas Falconffc385b2018-02-18 10:08:41 -06002043 if (atomic_add_return(num_entries, &tx_scrq->used)
Brian King58c8c0c2017-04-19 13:44:47 -04002044 >= adapter->req_tx_entries_per_subcrq) {
Thomas Falcon0aecb132018-02-26 18:10:58 -06002045 netdev_dbg(netdev, "Stopping queue %d\n", queue_num);
Thomas Falcon142c0ac2017-03-05 12:18:41 -06002046 netif_stop_subqueue(netdev, queue_num);
2047 }
2048
Thomas Falcon032c5e82015-12-21 11:26:06 -06002049 tx_packets++;
2050 tx_bytes += skb->len;
Eric Dumazet53378242021-11-16 19:29:22 -08002051 txq_trans_cond_update(txq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002052 ret = NETDEV_TX_OK;
Thomas Falcon86b61a52018-03-16 20:00:29 -05002053 goto out;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002054
Thomas Falcon0d973382020-11-18 19:12:19 -06002055tx_flush_err:
2056 dev_kfree_skb_any(skb);
2057 tx_buff->skb = NULL;
2058 tx_pool->consumer_index = tx_pool->consumer_index == 0 ?
2059 tx_pool->num_buffers - 1 :
2060 tx_pool->consumer_index - 1;
2061 tx_dropped++;
2062tx_err:
2063 if (lpar_rc != H_CLOSED && lpar_rc != H_PARAMETER)
2064 dev_err_ratelimited(dev, "tx: send failed\n");
2065
2066 if (lpar_rc == H_CLOSED || adapter->failover_pending) {
2067 /* Disable TX and report carrier off if queue is closed
2068 * or pending failover.
2069 * Firmware guarantees that a signal will be sent to the
2070 * driver, triggering a reset or some other action.
2071 */
2072 netif_tx_stop_all_queues(netdev);
2073 netif_carrier_off(netdev);
2074 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002075out:
2076 netdev->stats.tx_dropped += tx_dropped;
2077 netdev->stats.tx_bytes += tx_bytes;
2078 netdev->stats.tx_packets += tx_packets;
2079 adapter->tx_send_failed += tx_send_failed;
2080 adapter->tx_map_failed += tx_map_failed;
John Allen3d52b592017-08-02 16:44:14 -05002081 adapter->tx_stats_buffers[queue_num].packets += tx_packets;
2082 adapter->tx_stats_buffers[queue_num].bytes += tx_bytes;
2083 adapter->tx_stats_buffers[queue_num].dropped_packets += tx_dropped;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002084
2085 return ret;
2086}
2087
2088static void ibmvnic_set_multi(struct net_device *netdev)
2089{
2090 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2091 struct netdev_hw_addr *ha;
2092 union ibmvnic_crq crq;
2093
2094 memset(&crq, 0, sizeof(crq));
2095 crq.request_capability.first = IBMVNIC_CRQ_CMD;
2096 crq.request_capability.cmd = REQUEST_CAPABILITY;
2097
2098 if (netdev->flags & IFF_PROMISC) {
2099 if (!adapter->promisc_supported)
2100 return;
2101 } else {
2102 if (netdev->flags & IFF_ALLMULTI) {
2103 /* Accept all multicast */
2104 memset(&crq, 0, sizeof(crq));
2105 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
2106 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
2107 crq.multicast_ctrl.flags = IBMVNIC_ENABLE_ALL;
2108 ibmvnic_send_crq(adapter, &crq);
2109 } else if (netdev_mc_empty(netdev)) {
2110 /* Reject all multicast */
2111 memset(&crq, 0, sizeof(crq));
2112 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
2113 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
2114 crq.multicast_ctrl.flags = IBMVNIC_DISABLE_ALL;
2115 ibmvnic_send_crq(adapter, &crq);
2116 } else {
2117 /* Accept one or more multicast(s) */
2118 netdev_for_each_mc_addr(ha, netdev) {
2119 memset(&crq, 0, sizeof(crq));
2120 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
2121 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
2122 crq.multicast_ctrl.flags = IBMVNIC_ENABLE_MC;
2123 ether_addr_copy(&crq.multicast_ctrl.mac_addr[0],
2124 ha->addr);
2125 ibmvnic_send_crq(adapter, &crq);
2126 }
2127 }
2128 }
2129}
2130
Thomas Falcon62740e92019-05-09 23:13:43 -05002131static int __ibmvnic_set_mac(struct net_device *netdev, u8 *dev_addr)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002132{
2133 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002134 union ibmvnic_crq crq;
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05002135 int rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002136
Thomas Falcon62740e92019-05-09 23:13:43 -05002137 if (!is_valid_ether_addr(dev_addr)) {
2138 rc = -EADDRNOTAVAIL;
2139 goto err;
2140 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002141
2142 memset(&crq, 0, sizeof(crq));
2143 crq.change_mac_addr.first = IBMVNIC_CRQ_CMD;
2144 crq.change_mac_addr.cmd = CHANGE_MAC_ADDR;
Thomas Falcon62740e92019-05-09 23:13:43 -05002145 ether_addr_copy(&crq.change_mac_addr.mac_addr[0], dev_addr);
Thomas Falconf8136142018-01-29 13:45:05 -06002146
Thomas Falconff25dcb2019-11-25 17:12:56 -06002147 mutex_lock(&adapter->fw_lock);
2148 adapter->fw_done_rc = 0;
Thomas Falcon070eca92019-11-25 17:12:53 -06002149 reinit_completion(&adapter->fw_done);
Thomas Falconff25dcb2019-11-25 17:12:56 -06002150
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05002151 rc = ibmvnic_send_crq(adapter, &crq);
Thomas Falcon62740e92019-05-09 23:13:43 -05002152 if (rc) {
2153 rc = -EIO;
Thomas Falconff25dcb2019-11-25 17:12:56 -06002154 mutex_unlock(&adapter->fw_lock);
Thomas Falcon62740e92019-05-09 23:13:43 -05002155 goto err;
2156 }
2157
Thomas Falcon476d96c2019-11-25 17:12:55 -06002158 rc = ibmvnic_wait_for_completion(adapter, &adapter->fw_done, 10000);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002159 /* netdev->dev_addr is changed in handle_change_mac_rsp function */
Thomas Falcon476d96c2019-11-25 17:12:55 -06002160 if (rc || adapter->fw_done_rc) {
Thomas Falcon62740e92019-05-09 23:13:43 -05002161 rc = -EIO;
Thomas Falconff25dcb2019-11-25 17:12:56 -06002162 mutex_unlock(&adapter->fw_lock);
Thomas Falcon62740e92019-05-09 23:13:43 -05002163 goto err;
2164 }
Thomas Falconff25dcb2019-11-25 17:12:56 -06002165 mutex_unlock(&adapter->fw_lock);
Thomas Falcon62740e92019-05-09 23:13:43 -05002166 return 0;
2167err:
2168 ether_addr_copy(adapter->mac_addr, netdev->dev_addr);
2169 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002170}
2171
John Allenc26eba02017-10-26 16:23:25 -05002172static int ibmvnic_set_mac(struct net_device *netdev, void *p)
2173{
2174 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2175 struct sockaddr *addr = p;
Thomas Falconf8136142018-01-29 13:45:05 -06002176 int rc;
John Allenc26eba02017-10-26 16:23:25 -05002177
Thomas Falcon62740e92019-05-09 23:13:43 -05002178 rc = 0;
Lijun Pan8fc36722020-10-27 17:04:56 -05002179 if (!is_valid_ether_addr(addr->sa_data))
2180 return -EADDRNOTAVAIL;
2181
Jiri Wiesner67eb2112021-03-04 17:18:28 +01002182 ether_addr_copy(adapter->mac_addr, addr->sa_data);
2183 if (adapter->state != VNIC_PROBED)
Thomas Falcon62740e92019-05-09 23:13:43 -05002184 rc = __ibmvnic_set_mac(netdev, addr->sa_data);
John Allenc26eba02017-10-26 16:23:25 -05002185
Thomas Falconf8136142018-01-29 13:45:05 -06002186 return rc;
John Allenc26eba02017-10-26 16:23:25 -05002187}
2188
Lijun Pancaee7bf2021-04-12 02:41:27 -05002189static const char *reset_reason_to_string(enum ibmvnic_reset_reason reason)
2190{
2191 switch (reason) {
2192 case VNIC_RESET_FAILOVER:
2193 return "FAILOVER";
2194 case VNIC_RESET_MOBILITY:
2195 return "MOBILITY";
2196 case VNIC_RESET_FATAL:
2197 return "FATAL";
2198 case VNIC_RESET_NON_FATAL:
2199 return "NON_FATAL";
2200 case VNIC_RESET_TIMEOUT:
2201 return "TIMEOUT";
2202 case VNIC_RESET_CHANGE_PARAM:
2203 return "CHANGE_PARAM";
Lijun Pan822ebc22021-06-11 10:35:37 -05002204 case VNIC_RESET_PASSIVE_INIT:
2205 return "PASSIVE_INIT";
Lijun Pancaee7bf2021-04-12 02:41:27 -05002206 }
Michal Suchanek07b5dc12021-05-20 08:50:34 +02002207 return "UNKNOWN";
Lijun Pancaee7bf2021-04-12 02:41:27 -05002208}
2209
Lee Jones80708602021-01-15 20:09:03 +00002210/*
Nathan Fontenoted651a12017-05-03 14:04:38 -04002211 * do_reset returns zero if we are able to keep processing reset events, or
2212 * non-zero if we hit a fatal error and must halt.
2213 */
2214static int do_reset(struct ibmvnic_adapter *adapter,
2215 struct ibmvnic_rwi *rwi, u32 reset_state)
2216{
Sukadev Bhattiprolubbd80932021-09-14 20:52:59 -07002217 struct net_device *netdev = adapter->netdev;
John Allen896d8692018-01-18 16:26:31 -06002218 u64 old_num_rx_queues, old_num_tx_queues;
Thomas Falcon5bf032e2018-11-21 11:17:59 -06002219 u64 old_num_rx_slots, old_num_tx_slots;
Lijun Pand3a6abc2021-04-14 02:46:15 -05002220 int rc;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002221
Sukadev Bhattiprolu38bd5ce2020-12-04 18:22:35 -08002222 netdev_dbg(adapter->netdev,
Lijun Pan0666ef72021-04-12 02:41:28 -05002223 "[S:%s FOP:%d] Reset reason: %s, reset_state: %s\n",
2224 adapter_state_to_string(adapter->state),
2225 adapter->failover_pending,
2226 reset_reason_to_string(rwi->reset_reason),
2227 adapter_state_to_string(reset_state));
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002228
Lijun Pan3f5ec372021-01-06 15:35:14 -06002229 adapter->reset_reason = rwi->reset_reason;
2230 /* requestor of VNIC_RESET_CHANGE_PARAM already has the rtnl lock */
2231 if (!(adapter->reset_reason == VNIC_RESET_CHANGE_PARAM))
2232 rtnl_lock();
2233
Lijun Panbab08be2021-02-11 00:43:19 -06002234 /* Now that we have the rtnl lock, clear any pending failover.
Sukadev Bhattiprolu1d850492020-10-30 10:07:11 -07002235 * This will ensure ibmvnic_open() has either completed or will
2236 * block until failover is complete.
2237 */
2238 if (rwi->reset_reason == VNIC_RESET_FAILOVER)
2239 adapter->failover_pending = false;
Juliet Kimb27507b2019-09-20 16:11:22 -04002240
Sukadev Bhattiprolu8f1c0fd2021-02-23 21:02:29 -08002241 /* read the state and check (again) after getting rtnl */
2242 reset_state = adapter->state;
2243
2244 if (reset_state == VNIC_REMOVING || reset_state == VNIC_REMOVED) {
2245 rc = -EBUSY;
2246 goto out;
2247 }
2248
Nathan Fontenoted651a12017-05-03 14:04:38 -04002249 netif_carrier_off(netdev);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002250
John Allen896d8692018-01-18 16:26:31 -06002251 old_num_rx_queues = adapter->req_rx_queues;
2252 old_num_tx_queues = adapter->req_tx_queues;
Thomas Falcon5bf032e2018-11-21 11:17:59 -06002253 old_num_rx_slots = adapter->req_rx_add_entries_per_subcrq;
2254 old_num_tx_slots = adapter->req_tx_entries_per_subcrq;
John Allen896d8692018-01-18 16:26:31 -06002255
Nathan Fontenot30f79622018-04-06 18:37:06 -05002256 ibmvnic_cleanup(netdev);
2257
Thomas Falcon1f946082019-06-07 16:03:53 -05002258 if (reset_state == VNIC_OPEN &&
2259 adapter->reset_reason != VNIC_RESET_MOBILITY &&
Nathan Fontenot30f79622018-04-06 18:37:06 -05002260 adapter->reset_reason != VNIC_RESET_FAILOVER) {
Lijun Pan3f5ec372021-01-06 15:35:14 -06002261 if (adapter->reset_reason == VNIC_RESET_CHANGE_PARAM) {
2262 rc = __ibmvnic_close(netdev);
2263 if (rc)
2264 goto out;
2265 } else {
2266 adapter->state = VNIC_CLOSING;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002267
Lijun Pan3f5ec372021-01-06 15:35:14 -06002268 /* Release the RTNL lock before link state change and
2269 * re-acquire after the link state change to allow
2270 * linkwatch_event to grab the RTNL lock and run during
2271 * a reset.
2272 */
2273 rtnl_unlock();
2274 rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_DN);
2275 rtnl_lock();
2276 if (rc)
2277 goto out;
Juliet Kimb27507b2019-09-20 16:11:22 -04002278
Sukadev Bhattiprolu8f1c0fd2021-02-23 21:02:29 -08002279 if (adapter->state == VNIC_OPEN) {
2280 /* When we dropped rtnl, ibmvnic_open() got
2281 * it and noticed that we are resetting and
2282 * set the adapter state to OPEN. Update our
2283 * new "target" state, and resume the reset
2284 * from VNIC_CLOSING state.
2285 */
2286 netdev_dbg(netdev,
Lijun Pan0666ef72021-04-12 02:41:28 -05002287 "Open changed state from %s, updating.\n",
2288 adapter_state_to_string(reset_state));
Sukadev Bhattiprolu8f1c0fd2021-02-23 21:02:29 -08002289 reset_state = VNIC_OPEN;
2290 adapter->state = VNIC_CLOSING;
2291 }
2292
Lijun Pan3f5ec372021-01-06 15:35:14 -06002293 if (adapter->state != VNIC_CLOSING) {
Sukadev Bhattiprolu8f1c0fd2021-02-23 21:02:29 -08002294 /* If someone else changed the adapter state
2295 * when we dropped the rtnl, fail the reset
2296 */
Dany Maddenb6ee5662021-12-14 00:17:47 -05002297 rc = -EAGAIN;
Lijun Pan3f5ec372021-01-06 15:35:14 -06002298 goto out;
2299 }
Lijun Pan3f5ec372021-01-06 15:35:14 -06002300 adapter->state = VNIC_CLOSED;
Juliet Kimb27507b2019-09-20 16:11:22 -04002301 }
Lijun Pan3f5ec372021-01-06 15:35:14 -06002302 }
Juliet Kimb27507b2019-09-20 16:11:22 -04002303
Lijun Pan3f5ec372021-01-06 15:35:14 -06002304 if (adapter->reset_reason == VNIC_RESET_CHANGE_PARAM) {
2305 release_resources(adapter);
2306 release_sub_crqs(adapter, 1);
2307 release_crq_queue(adapter);
John Allenc26eba02017-10-26 16:23:25 -05002308 }
2309
John Allen8cb31cf2017-05-26 10:30:37 -04002310 if (adapter->reset_reason != VNIC_RESET_NON_FATAL) {
2311 /* remove the closed state so when we call open it appears
2312 * we are coming from the probed state.
2313 */
Nathan Fontenoted651a12017-05-03 14:04:38 -04002314 adapter->state = VNIC_PROBED;
John Allen8cb31cf2017-05-26 10:30:37 -04002315
Lijun Pan3f5ec372021-01-06 15:35:14 -06002316 if (adapter->reset_reason == VNIC_RESET_CHANGE_PARAM) {
2317 rc = init_crq_queue(adapter);
2318 } else if (adapter->reset_reason == VNIC_RESET_MOBILITY) {
Nathan Fontenot30f79622018-04-06 18:37:06 -05002319 rc = ibmvnic_reenable_crq_queue(adapter);
2320 release_sub_crqs(adapter, 1);
2321 } else {
2322 rc = ibmvnic_reset_crq(adapter);
Dany Madden8b40eb732020-06-18 15:24:13 -04002323 if (rc == H_CLOSED || rc == H_SUCCESS) {
Nathan Fontenot30f79622018-04-06 18:37:06 -05002324 rc = vio_enable_interrupts(adapter->vdev);
Dany Madden8b40eb732020-06-18 15:24:13 -04002325 if (rc)
2326 netdev_err(adapter->netdev,
2327 "Reset failed to enable interrupts. rc=%d\n",
2328 rc);
2329 }
Nathan Fontenot30f79622018-04-06 18:37:06 -05002330 }
2331
2332 if (rc) {
2333 netdev_err(adapter->netdev,
Dany Madden8b40eb732020-06-18 15:24:13 -04002334 "Reset couldn't initialize crq. rc=%d\n", rc);
Juliet Kimb27507b2019-09-20 16:11:22 -04002335 goto out;
Nathan Fontenot30f79622018-04-06 18:37:06 -05002336 }
2337
Lijun Pan635e4422020-08-19 17:52:26 -05002338 rc = ibmvnic_reset_init(adapter, true);
Dany Maddenb6ee5662021-12-14 00:17:47 -05002339 if (rc)
Juliet Kimb27507b2019-09-20 16:11:22 -04002340 goto out;
John Allen8cb31cf2017-05-26 10:30:37 -04002341
Cristobal Forno53f8b1b2021-06-10 11:08:35 -06002342 /* If the adapter was in PROBE or DOWN state prior to the reset,
John Allen8cb31cf2017-05-26 10:30:37 -04002343 * exit here.
2344 */
Cristobal Forno53f8b1b2021-06-10 11:08:35 -06002345 if (reset_state == VNIC_PROBED || reset_state == VNIC_DOWN) {
Juliet Kimb27507b2019-09-20 16:11:22 -04002346 rc = 0;
2347 goto out;
2348 }
John Allen8cb31cf2017-05-26 10:30:37 -04002349
2350 rc = ibmvnic_login(netdev);
Lijun Panf78afaa2021-02-11 00:43:20 -06002351 if (rc)
Juliet Kimb27507b2019-09-20 16:11:22 -04002352 goto out;
John Allen8cb31cf2017-05-26 10:30:37 -04002353
Lijun Pan3f5ec372021-01-06 15:35:14 -06002354 if (adapter->reset_reason == VNIC_RESET_CHANGE_PARAM) {
2355 rc = init_resources(adapter);
2356 if (rc)
2357 goto out;
2358 } else if (adapter->req_rx_queues != old_num_rx_queues ||
Juliet Kimb27507b2019-09-20 16:11:22 -04002359 adapter->req_tx_queues != old_num_tx_queues ||
2360 adapter->req_rx_add_entries_per_subcrq !=
2361 old_num_rx_slots ||
2362 adapter->req_tx_entries_per_subcrq !=
Mingming Cao9f134572020-08-25 13:26:41 -04002363 old_num_tx_slots ||
2364 !adapter->rx_pool ||
2365 !adapter->tso_pool ||
2366 !adapter->tx_pool) {
Juliet Kima5681e22018-11-19 15:59:22 -06002367 release_napi(adapter);
2368 release_vpd_data(adapter);
2369
2370 rc = init_resources(adapter);
Thomas Falconf611a5b2018-08-30 13:19:53 -05002371 if (rc)
Juliet Kimb27507b2019-09-20 16:11:22 -04002372 goto out;
Nathan Fontenotd9043c12018-02-19 13:30:14 -06002373
John Allenc26eba02017-10-26 16:23:25 -05002374 } else {
Sukadev Bhattiprolubbd80932021-09-14 20:52:59 -07002375 rc = init_tx_pools(netdev);
Jakub Kicinski8ae4dff2020-09-04 21:07:49 -07002376 if (rc) {
Sukadev Bhattiprolubbd80932021-09-14 20:52:59 -07002377 netdev_dbg(netdev,
2378 "init tx pools failed (%d)\n",
Lijun Pan91dc5d22021-02-11 00:43:22 -06002379 rc);
Juliet Kimb27507b2019-09-20 16:11:22 -04002380 goto out;
Jakub Kicinski8ae4dff2020-09-04 21:07:49 -07002381 }
Nathan Fontenot8c0543a2017-05-26 10:31:06 -04002382
Sukadev Bhattiprolu489de952021-09-14 20:52:58 -07002383 rc = init_rx_pools(netdev);
Jakub Kicinski8ae4dff2020-09-04 21:07:49 -07002384 if (rc) {
Sukadev Bhattiprolu489de952021-09-14 20:52:58 -07002385 netdev_dbg(netdev,
2386 "init rx pools failed (%d)\n",
Lijun Pan91dc5d22021-02-11 00:43:22 -06002387 rc);
Juliet Kimb27507b2019-09-20 16:11:22 -04002388 goto out;
Jakub Kicinski8ae4dff2020-09-04 21:07:49 -07002389 }
John Allenc26eba02017-10-26 16:23:25 -05002390 }
Thomas Falcon134bbe72018-05-16 15:49:04 -05002391 ibmvnic_disable_irqs(adapter);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002392 }
John Allene676d812018-03-14 10:41:29 -05002393 adapter->state = VNIC_CLOSED;
2394
Juliet Kimb27507b2019-09-20 16:11:22 -04002395 if (reset_state == VNIC_CLOSED) {
2396 rc = 0;
2397 goto out;
2398 }
John Allene676d812018-03-14 10:41:29 -05002399
Nathan Fontenoted651a12017-05-03 14:04:38 -04002400 rc = __ibmvnic_open(netdev);
2401 if (rc) {
Juliet Kimb27507b2019-09-20 16:11:22 -04002402 rc = IBMVNIC_OPEN_FAILED;
2403 goto out;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002404 }
2405
Thomas Falconbe32a242019-06-07 16:03:54 -05002406 /* refresh device's multicast list */
2407 ibmvnic_set_multi(netdev);
2408
Lijun Pan98025bc2020-11-20 16:40:12 -06002409 if (adapter->reset_reason == VNIC_RESET_FAILOVER ||
Lijun Pan6be46662020-12-14 15:19:29 -06002410 adapter->reset_reason == VNIC_RESET_MOBILITY)
2411 __netdev_notify_peers(netdev);
Nathan Fontenot61d3e1d2017-06-12 20:47:45 -04002412
Juliet Kimb27507b2019-09-20 16:11:22 -04002413 rc = 0;
2414
2415out:
Dany Madden0cb4bc62020-11-25 18:04:27 -06002416 /* restore the adapter state if reset failed */
2417 if (rc)
2418 adapter->state = reset_state;
Lijun Pan3f5ec372021-01-06 15:35:14 -06002419 /* requestor of VNIC_RESET_CHANGE_PARAM should still hold the rtnl lock */
2420 if (!(adapter->reset_reason == VNIC_RESET_CHANGE_PARAM))
2421 rtnl_unlock();
Juliet Kimb27507b2019-09-20 16:11:22 -04002422
Lijun Pan0666ef72021-04-12 02:41:28 -05002423 netdev_dbg(adapter->netdev, "[S:%s FOP:%d] Reset done, rc %d\n",
2424 adapter_state_to_string(adapter->state),
2425 adapter->failover_pending, rc);
Juliet Kimb27507b2019-09-20 16:11:22 -04002426 return rc;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002427}
2428
Thomas Falcon2770a792018-05-23 13:38:02 -05002429static int do_hard_reset(struct ibmvnic_adapter *adapter,
2430 struct ibmvnic_rwi *rwi, u32 reset_state)
2431{
2432 struct net_device *netdev = adapter->netdev;
2433 int rc;
2434
Lijun Pancaee7bf2021-04-12 02:41:27 -05002435 netdev_dbg(adapter->netdev, "Hard resetting driver (%s)\n",
2436 reset_reason_to_string(rwi->reset_reason));
Thomas Falcon2770a792018-05-23 13:38:02 -05002437
Sukadev Bhattiprolu8f1c0fd2021-02-23 21:02:29 -08002438 /* read the state and check (again) after getting rtnl */
2439 reset_state = adapter->state;
2440
2441 if (reset_state == VNIC_REMOVING || reset_state == VNIC_REMOVED) {
2442 rc = -EBUSY;
2443 goto out;
2444 }
2445
Thomas Falcon2770a792018-05-23 13:38:02 -05002446 netif_carrier_off(netdev);
2447 adapter->reset_reason = rwi->reset_reason;
2448
2449 ibmvnic_cleanup(netdev);
2450 release_resources(adapter);
2451 release_sub_crqs(adapter, 0);
2452 release_crq_queue(adapter);
2453
2454 /* remove the closed state so when we call open it appears
2455 * we are coming from the probed state.
2456 */
2457 adapter->state = VNIC_PROBED;
2458
Thomas Falconbbd669a2019-04-04 18:58:26 -05002459 reinit_completion(&adapter->init_done);
Thomas Falcon2770a792018-05-23 13:38:02 -05002460 rc = init_crq_queue(adapter);
2461 if (rc) {
2462 netdev_err(adapter->netdev,
2463 "Couldn't initialize crq. rc=%d\n", rc);
Dany Madden0cb4bc62020-11-25 18:04:27 -06002464 goto out;
Thomas Falcon2770a792018-05-23 13:38:02 -05002465 }
2466
Lijun Pan635e4422020-08-19 17:52:26 -05002467 rc = ibmvnic_reset_init(adapter, false);
Thomas Falcon2770a792018-05-23 13:38:02 -05002468 if (rc)
Dany Madden0cb4bc62020-11-25 18:04:27 -06002469 goto out;
Thomas Falcon2770a792018-05-23 13:38:02 -05002470
Cristobal Forno53f8b1b2021-06-10 11:08:35 -06002471 /* If the adapter was in PROBE or DOWN state prior to the reset,
Thomas Falcon2770a792018-05-23 13:38:02 -05002472 * exit here.
2473 */
Cristobal Forno53f8b1b2021-06-10 11:08:35 -06002474 if (reset_state == VNIC_PROBED || reset_state == VNIC_DOWN)
Dany Madden0cb4bc62020-11-25 18:04:27 -06002475 goto out;
Thomas Falcon2770a792018-05-23 13:38:02 -05002476
2477 rc = ibmvnic_login(netdev);
Dany Madden0cb4bc62020-11-25 18:04:27 -06002478 if (rc)
2479 goto out;
Juliet Kima5681e22018-11-19 15:59:22 -06002480
2481 rc = init_resources(adapter);
Thomas Falcon2770a792018-05-23 13:38:02 -05002482 if (rc)
Dany Madden0cb4bc62020-11-25 18:04:27 -06002483 goto out;
Thomas Falcon2770a792018-05-23 13:38:02 -05002484
2485 ibmvnic_disable_irqs(adapter);
2486 adapter->state = VNIC_CLOSED;
2487
2488 if (reset_state == VNIC_CLOSED)
Dany Madden0cb4bc62020-11-25 18:04:27 -06002489 goto out;
Thomas Falcon2770a792018-05-23 13:38:02 -05002490
2491 rc = __ibmvnic_open(netdev);
Dany Madden0cb4bc62020-11-25 18:04:27 -06002492 if (rc) {
2493 rc = IBMVNIC_OPEN_FAILED;
2494 goto out;
2495 }
Thomas Falcon2770a792018-05-23 13:38:02 -05002496
Lijun Pan6be46662020-12-14 15:19:29 -06002497 __netdev_notify_peers(netdev);
Dany Madden0cb4bc62020-11-25 18:04:27 -06002498out:
2499 /* restore adapter state if reset failed */
2500 if (rc)
2501 adapter->state = reset_state;
Lijun Pan0666ef72021-04-12 02:41:28 -05002502 netdev_dbg(adapter->netdev, "[S:%s FOP:%d] Hard reset done, rc %d\n",
2503 adapter_state_to_string(adapter->state),
2504 adapter->failover_pending, rc);
Dany Madden0cb4bc62020-11-25 18:04:27 -06002505 return rc;
Thomas Falcon2770a792018-05-23 13:38:02 -05002506}
2507
Nathan Fontenoted651a12017-05-03 14:04:38 -04002508static struct ibmvnic_rwi *get_next_rwi(struct ibmvnic_adapter *adapter)
2509{
2510 struct ibmvnic_rwi *rwi;
Thomas Falcon6c5c7482018-12-10 15:22:22 -06002511 unsigned long flags;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002512
Thomas Falcon6c5c7482018-12-10 15:22:22 -06002513 spin_lock_irqsave(&adapter->rwi_lock, flags);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002514
2515 if (!list_empty(&adapter->rwi_list)) {
2516 rwi = list_first_entry(&adapter->rwi_list, struct ibmvnic_rwi,
2517 list);
2518 list_del(&rwi->list);
2519 } else {
2520 rwi = NULL;
2521 }
2522
Thomas Falcon6c5c7482018-12-10 15:22:22 -06002523 spin_unlock_irqrestore(&adapter->rwi_lock, flags);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002524 return rwi;
2525}
2526
Cristobal Forno53f8b1b2021-06-10 11:08:35 -06002527/**
2528 * do_passive_init - complete probing when partner device is detected.
2529 * @adapter: ibmvnic_adapter struct
2530 *
2531 * If the ibmvnic device does not have a partner device to communicate with at boot
2532 * and that partner device comes online at a later time, this function is called
2533 * to complete the initialization process of ibmvnic device.
2534 * Caller is expected to hold rtnl_lock().
2535 *
2536 * Returns non-zero if sub-CRQs are not initialized properly leaving the device
2537 * in the down state.
2538 * Returns 0 upon success and the device is in PROBED state.
2539 */
2540
2541static int do_passive_init(struct ibmvnic_adapter *adapter)
2542{
2543 unsigned long timeout = msecs_to_jiffies(30000);
2544 struct net_device *netdev = adapter->netdev;
2545 struct device *dev = &adapter->vdev->dev;
2546 int rc;
2547
2548 netdev_dbg(netdev, "Partner device found, probing.\n");
2549
2550 adapter->state = VNIC_PROBING;
2551 reinit_completion(&adapter->init_done);
2552 adapter->init_done_rc = 0;
2553 adapter->crq.active = true;
2554
2555 rc = send_crq_init_complete(adapter);
2556 if (rc)
2557 goto out;
2558
2559 rc = send_version_xchg(adapter);
2560 if (rc)
2561 netdev_dbg(adapter->netdev, "send_version_xchg failed, rc=%d\n", rc);
2562
2563 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
2564 dev_err(dev, "Initialization sequence timed out\n");
2565 rc = -ETIMEDOUT;
2566 goto out;
2567 }
2568
2569 rc = init_sub_crqs(adapter);
2570 if (rc) {
2571 dev_err(dev, "Initialization of sub crqs failed, rc=%d\n", rc);
2572 goto out;
2573 }
2574
2575 rc = init_sub_crq_irqs(adapter);
2576 if (rc) {
2577 dev_err(dev, "Failed to initialize sub crq irqs\n, rc=%d", rc);
2578 goto init_failed;
2579 }
2580
2581 netdev->mtu = adapter->req_mtu - ETH_HLEN;
2582 netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
2583 netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
2584
2585 adapter->state = VNIC_PROBED;
2586 netdev_dbg(netdev, "Probed successfully. Waiting for signal from partner device.\n");
2587
2588 return 0;
2589
2590init_failed:
2591 release_sub_crqs(adapter, 1);
2592out:
2593 adapter->state = VNIC_DOWN;
2594 return rc;
2595}
2596
Nathan Fontenoted651a12017-05-03 14:04:38 -04002597static void __ibmvnic_reset(struct work_struct *work)
2598{
Nathan Fontenoted651a12017-05-03 14:04:38 -04002599 struct ibmvnic_adapter *adapter;
Juliet Kim7d7195a2020-03-10 09:23:58 -05002600 bool saved_state = false;
Sukadev Bhattiprolu4f408e12021-06-30 14:36:17 -04002601 struct ibmvnic_rwi *tmprwi;
2602 struct ibmvnic_rwi *rwi;
Juliet Kim7d7195a2020-03-10 09:23:58 -05002603 unsigned long flags;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002604 u32 reset_state;
John Allenc26eba02017-10-26 16:23:25 -05002605 int rc = 0;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002606
2607 adapter = container_of(work, struct ibmvnic_adapter, ibmvnic_reset);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002608
Juliet Kim7ed5b312019-09-20 16:11:23 -04002609 if (test_and_set_bit_lock(0, &adapter->resetting)) {
Lijun Pan870e04a2021-04-13 14:33:39 -05002610 queue_delayed_work(system_long_wq,
2611 &adapter->ibmvnic_delayed_reset,
2612 IBMVNIC_RESET_DELAY);
Juliet Kim7ed5b312019-09-20 16:11:23 -04002613 return;
2614 }
2615
Nathan Fontenoted651a12017-05-03 14:04:38 -04002616 rwi = get_next_rwi(adapter);
2617 while (rwi) {
Juliet Kim7d7195a2020-03-10 09:23:58 -05002618 spin_lock_irqsave(&adapter->state_lock, flags);
2619
Thomas Falcon36f10312019-08-27 11:10:04 -05002620 if (adapter->state == VNIC_REMOVING ||
Michal Suchanekc8dc5592019-09-09 22:44:51 +02002621 adapter->state == VNIC_REMOVED) {
Juliet Kim7d7195a2020-03-10 09:23:58 -05002622 spin_unlock_irqrestore(&adapter->state_lock, flags);
Juliet Kim1c2977c2019-09-05 17:30:01 -04002623 kfree(rwi);
2624 rc = EBUSY;
2625 break;
2626 }
Thomas Falcon36f10312019-08-27 11:10:04 -05002627
Juliet Kim7d7195a2020-03-10 09:23:58 -05002628 if (!saved_state) {
2629 reset_state = adapter->state;
Juliet Kim7d7195a2020-03-10 09:23:58 -05002630 saved_state = true;
2631 }
2632 spin_unlock_irqrestore(&adapter->state_lock, flags);
2633
Cristobal Forno53f8b1b2021-06-10 11:08:35 -06002634 if (rwi->reset_reason == VNIC_RESET_PASSIVE_INIT) {
2635 rtnl_lock();
2636 rc = do_passive_init(adapter);
2637 rtnl_unlock();
2638 if (!rc)
2639 netif_carrier_on(adapter->netdev);
2640 } else if (adapter->force_reset_recovery) {
Lijun Panbab08be2021-02-11 00:43:19 -06002641 /* Since we are doing a hard reset now, clear the
Sukadev Bhattiprolu1d850492020-10-30 10:07:11 -07002642 * failover_pending flag so we don't ignore any
2643 * future MOBILITY or other resets.
2644 */
2645 adapter->failover_pending = false;
2646
Juliet Kimb27507b2019-09-20 16:11:22 -04002647 /* Transport event occurred during previous reset */
2648 if (adapter->wait_for_reset) {
2649 /* Previous was CHANGE_PARAM; caller locked */
2650 adapter->force_reset_recovery = false;
2651 rc = do_hard_reset(adapter, rwi, reset_state);
2652 } else {
2653 rtnl_lock();
2654 adapter->force_reset_recovery = false;
2655 rc = do_hard_reset(adapter, rwi, reset_state);
2656 rtnl_unlock();
2657 }
Sukadev Bhattiproluf15fde92020-11-25 18:04:28 -06002658 if (rc) {
2659 /* give backing device time to settle down */
2660 netdev_dbg(adapter->netdev,
Lijun Pan0666ef72021-04-12 02:41:28 -05002661 "[S:%s] Hard reset failed, waiting 60 secs\n",
2662 adapter_state_to_string(adapter->state));
Sukadev Bhattiproluf15fde92020-11-25 18:04:28 -06002663 set_current_state(TASK_UNINTERRUPTIBLE);
2664 schedule_timeout(60 * HZ);
2665 }
Lijun Pan1f45dc22020-12-23 14:49:04 -06002666 } else {
Thomas Falcon2770a792018-05-23 13:38:02 -05002667 rc = do_reset(adapter, rwi, reset_state);
2668 }
Sukadev Bhattiprolu4f408e12021-06-30 14:36:17 -04002669 tmprwi = rwi;
Dany Maddena86d5c62020-11-25 18:04:31 -06002670 adapter->last_reset_time = jiffies;
Dany Madden0cb4bc62020-11-25 18:04:27 -06002671
Dany Madden18f141b2020-11-25 18:04:25 -06002672 if (rc)
2673 netdev_dbg(adapter->netdev, "Reset failed, rc=%d\n", rc);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002674
2675 rwi = get_next_rwi(adapter);
Juliet Kim7ed5b312019-09-20 16:11:23 -04002676
Sukadev Bhattiprolu4f408e12021-06-30 14:36:17 -04002677 /*
2678 * If there is another reset queued, free the previous rwi
2679 * and process the new reset even if previous reset failed
2680 * (the previous reset could have failed because of a fail
2681 * over for instance, so process the fail over).
2682 *
2683 * If there are no resets queued and the previous reset failed,
2684 * the adapter would be in an undefined state. So retry the
2685 * previous reset as a hard reset.
2686 */
2687 if (rwi)
2688 kfree(tmprwi);
2689 else if (rc)
2690 rwi = tmprwi;
2691
Juliet Kim7ed5b312019-09-20 16:11:23 -04002692 if (rwi && (rwi->reset_reason == VNIC_RESET_FAILOVER ||
Sukadev Bhattiprolu4f408e12021-06-30 14:36:17 -04002693 rwi->reset_reason == VNIC_RESET_MOBILITY || rc))
Juliet Kim7ed5b312019-09-20 16:11:23 -04002694 adapter->force_reset_recovery = true;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002695 }
2696
John Allenc26eba02017-10-26 16:23:25 -05002697 if (adapter->wait_for_reset) {
John Allenc26eba02017-10-26 16:23:25 -05002698 adapter->reset_done_rc = rc;
2699 complete(&adapter->reset_done);
2700 }
2701
Juliet Kim7ed5b312019-09-20 16:11:23 -04002702 clear_bit_unlock(0, &adapter->resetting);
Sukadev Bhattiprolu38bd5ce2020-12-04 18:22:35 -08002703
2704 netdev_dbg(adapter->netdev,
Lijun Pan0666ef72021-04-12 02:41:28 -05002705 "[S:%s FRR:%d WFR:%d] Done processing resets\n",
2706 adapter_state_to_string(adapter->state),
2707 adapter->force_reset_recovery,
Sukadev Bhattiprolu38bd5ce2020-12-04 18:22:35 -08002708 adapter->wait_for_reset);
Juliet Kim7ed5b312019-09-20 16:11:23 -04002709}
2710
2711static void __ibmvnic_delayed_reset(struct work_struct *work)
2712{
2713 struct ibmvnic_adapter *adapter;
2714
2715 adapter = container_of(work, struct ibmvnic_adapter,
2716 ibmvnic_delayed_reset.work);
2717 __ibmvnic_reset(&adapter->ibmvnic_reset);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002718}
2719
Thomas Falconaf894d22018-04-06 18:37:04 -05002720static int ibmvnic_reset(struct ibmvnic_adapter *adapter,
2721 enum ibmvnic_reset_reason reason)
Nathan Fontenoted651a12017-05-03 14:04:38 -04002722{
Thomas Falcon2770a792018-05-23 13:38:02 -05002723 struct list_head *entry, *tmp_entry;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002724 struct ibmvnic_rwi *rwi, *tmp;
2725 struct net_device *netdev = adapter->netdev;
Thomas Falcon6c5c7482018-12-10 15:22:22 -06002726 unsigned long flags;
Thomas Falconaf894d22018-04-06 18:37:04 -05002727 int ret;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002728
Jakub Kicinskib646acd52021-02-16 22:58:44 -08002729 spin_lock_irqsave(&adapter->rwi_lock, flags);
2730
2731 /* If failover is pending don't schedule any other reset.
Sukadev Bhattiprolu1d850492020-10-30 10:07:11 -07002732 * Instead let the failover complete. If there is already a
2733 * a failover reset scheduled, we will detect and drop the
2734 * duplicate reset when walking the ->rwi_list below.
2735 */
Nathan Fontenoted651a12017-05-03 14:04:38 -04002736 if (adapter->state == VNIC_REMOVING ||
Thomas Falcon5a18e1e2018-04-06 18:37:05 -05002737 adapter->state == VNIC_REMOVED ||
Sukadev Bhattiprolu1d850492020-10-30 10:07:11 -07002738 (adapter->failover_pending && reason != VNIC_RESET_FAILOVER)) {
Thomas Falconaf894d22018-04-06 18:37:04 -05002739 ret = EBUSY;
Thomas Falcon5a18e1e2018-04-06 18:37:05 -05002740 netdev_dbg(netdev, "Adapter removing or pending failover, skipping reset\n");
Thomas Falconaf894d22018-04-06 18:37:04 -05002741 goto err;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002742 }
2743
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04002744 if (adapter->state == VNIC_PROBING) {
2745 netdev_warn(netdev, "Adapter reset during probe\n");
Sukadev Bhattiprolu6b278c02021-10-29 15:03:16 -07002746 adapter->init_done_rc = -EAGAIN;
Lijun Pan91dc5d22021-02-11 00:43:22 -06002747 ret = EAGAIN;
Thomas Falconaf894d22018-04-06 18:37:04 -05002748 goto err;
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04002749 }
2750
Wang Hai3e98ae02021-06-10 20:54:17 +08002751 list_for_each_entry(tmp, &adapter->rwi_list, list) {
Nathan Fontenoted651a12017-05-03 14:04:38 -04002752 if (tmp->reset_reason == reason) {
Lijun Pancaee7bf2021-04-12 02:41:27 -05002753 netdev_dbg(netdev, "Skipping matching reset, reason=%s\n",
2754 reset_reason_to_string(reason));
Thomas Falconaf894d22018-04-06 18:37:04 -05002755 ret = EBUSY;
2756 goto err;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002757 }
2758 }
2759
Thomas Falcon1d1bbc32018-12-10 15:22:23 -06002760 rwi = kzalloc(sizeof(*rwi), GFP_ATOMIC);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002761 if (!rwi) {
Thomas Falconaf894d22018-04-06 18:37:04 -05002762 ret = ENOMEM;
2763 goto err;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002764 }
Thomas Falcon2770a792018-05-23 13:38:02 -05002765 /* if we just received a transport event,
2766 * flush reset queue and process this reset
2767 */
2768 if (adapter->force_reset_recovery && !list_empty(&adapter->rwi_list)) {
2769 list_for_each_safe(entry, tmp_entry, &adapter->rwi_list)
2770 list_del(entry);
2771 }
Nathan Fontenoted651a12017-05-03 14:04:38 -04002772 rwi->reset_reason = reason;
2773 list_add_tail(&rwi->list, &adapter->rwi_list);
Lijun Pancaee7bf2021-04-12 02:41:27 -05002774 netdev_dbg(adapter->netdev, "Scheduling reset (reason %s)\n",
2775 reset_reason_to_string(reason));
Lijun Pan870e04a2021-04-13 14:33:39 -05002776 queue_work(system_long_wq, &adapter->ibmvnic_reset);
Thomas Falconaf894d22018-04-06 18:37:04 -05002777
Sukadev Bhattiprolu4a41c422021-02-12 20:42:50 -08002778 ret = 0;
Thomas Falconaf894d22018-04-06 18:37:04 -05002779err:
Sukadev Bhattiprolu4a41c422021-02-12 20:42:50 -08002780 /* ibmvnic_close() below can block, so drop the lock first */
2781 spin_unlock_irqrestore(&adapter->rwi_lock, flags);
2782
2783 if (ret == ENOMEM)
2784 ibmvnic_close(netdev);
2785
Thomas Falconaf894d22018-04-06 18:37:04 -05002786 return -ret;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002787}
2788
Michael S. Tsirkin0290bd22019-12-10 09:23:51 -05002789static void ibmvnic_tx_timeout(struct net_device *dev, unsigned int txqueue)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002790{
2791 struct ibmvnic_adapter *adapter = netdev_priv(dev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002792
Lijun Pan855a6312020-11-20 16:40:13 -06002793 if (test_bit(0, &adapter->resetting)) {
2794 netdev_err(adapter->netdev,
2795 "Adapter is resetting, skip timeout reset\n");
2796 return;
2797 }
Dany Maddena86d5c62020-11-25 18:04:31 -06002798 /* No queuing up reset until at least 5 seconds (default watchdog val)
2799 * after last reset
2800 */
2801 if (time_before(jiffies, (adapter->last_reset_time + dev->watchdog_timeo))) {
2802 netdev_dbg(dev, "Not yet time to tx timeout.\n");
2803 return;
2804 }
Nathan Fontenoted651a12017-05-03 14:04:38 -04002805 ibmvnic_reset(adapter, VNIC_RESET_TIMEOUT);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002806}
2807
2808static void remove_buff_from_pool(struct ibmvnic_adapter *adapter,
2809 struct ibmvnic_rx_buff *rx_buff)
2810{
2811 struct ibmvnic_rx_pool *pool = &adapter->rx_pool[rx_buff->pool_index];
2812
2813 rx_buff->skb = NULL;
2814
2815 pool->free_map[pool->next_alloc] = (int)(rx_buff - pool->rx_buff);
2816 pool->next_alloc = (pool->next_alloc + 1) % pool->size;
2817
2818 atomic_dec(&pool->available);
2819}
2820
2821static int ibmvnic_poll(struct napi_struct *napi, int budget)
2822{
Dwip N. Banerjeeec20f362020-11-18 19:12:23 -06002823 struct ibmvnic_sub_crq_queue *rx_scrq;
2824 struct ibmvnic_adapter *adapter;
2825 struct net_device *netdev;
2826 int frames_processed;
2827 int scrq_num;
2828
2829 netdev = napi->dev;
2830 adapter = netdev_priv(netdev);
2831 scrq_num = (int)(napi - adapter->napi);
2832 frames_processed = 0;
2833 rx_scrq = adapter->rx_scrq[scrq_num];
Nathan Fontenot152ce472017-05-26 10:30:54 -04002834
Thomas Falcon032c5e82015-12-21 11:26:06 -06002835restart_poll:
2836 while (frames_processed < budget) {
2837 struct sk_buff *skb;
2838 struct ibmvnic_rx_buff *rx_buff;
2839 union sub_crq *next;
2840 u32 length;
2841 u16 offset;
2842 u8 flags = 0;
2843
Juliet Kim7ed5b312019-09-20 16:11:23 -04002844 if (unlikely(test_bit(0, &adapter->resetting) &&
John Allen34686562018-02-06 16:21:49 -06002845 adapter->reset_reason != VNIC_RESET_NON_FATAL)) {
Dwip N. Banerjeeec20f362020-11-18 19:12:23 -06002846 enable_scrq_irq(adapter, rx_scrq);
Thomas Falcon21ecba62017-06-14 23:50:09 -05002847 napi_complete_done(napi, frames_processed);
2848 return frames_processed;
2849 }
2850
Dwip N. Banerjeeec20f362020-11-18 19:12:23 -06002851 if (!pending_scrq(adapter, rx_scrq))
Thomas Falcon032c5e82015-12-21 11:26:06 -06002852 break;
Dwip N. Banerjeeec20f362020-11-18 19:12:23 -06002853 next = ibmvnic_next_scrq(adapter, rx_scrq);
Lijun Pan914789a2021-02-11 00:43:21 -06002854 rx_buff = (struct ibmvnic_rx_buff *)
2855 be64_to_cpu(next->rx_comp.correlator);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002856 /* do error checking */
2857 if (next->rx_comp.rc) {
John Allene1cea2e2017-08-07 15:42:30 -05002858 netdev_dbg(netdev, "rx buffer returned with rc %x\n",
2859 be16_to_cpu(next->rx_comp.rc));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002860 /* free the entry */
2861 next->rx_comp.first = 0;
Thomas Falcon4b9b0f02018-02-13 18:23:42 -06002862 dev_kfree_skb_any(rx_buff->skb);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002863 remove_buff_from_pool(adapter, rx_buff);
Nathan Fontenotca05e312017-05-03 14:05:14 -04002864 continue;
Thomas Falconabe27a82018-02-19 20:12:57 -06002865 } else if (!rx_buff->skb) {
2866 /* free the entry */
2867 next->rx_comp.first = 0;
2868 remove_buff_from_pool(adapter, rx_buff);
2869 continue;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002870 }
2871
2872 length = be32_to_cpu(next->rx_comp.len);
2873 offset = be16_to_cpu(next->rx_comp.off_frame_data);
2874 flags = next->rx_comp.flags;
2875 skb = rx_buff->skb;
Lijun Pan42557da2021-02-12 20:48:40 -06002876 /* load long_term_buff before copying to skb */
2877 dma_rmb();
Thomas Falcon032c5e82015-12-21 11:26:06 -06002878 skb_copy_to_linear_data(skb, rx_buff->data + offset,
2879 length);
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04002880
2881 /* VLAN Header has been stripped by the system firmware and
2882 * needs to be inserted by the driver
2883 */
2884 if (adapter->rx_vlan_header_insertion &&
2885 (flags & IBMVNIC_VLAN_STRIPPED))
2886 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
2887 ntohs(next->rx_comp.vlan_tci));
2888
Thomas Falcon032c5e82015-12-21 11:26:06 -06002889 /* free the entry */
2890 next->rx_comp.first = 0;
2891 remove_buff_from_pool(adapter, rx_buff);
2892
2893 skb_put(skb, length);
2894 skb->protocol = eth_type_trans(skb, netdev);
Thomas Falcon94ca3052017-05-03 14:05:20 -04002895 skb_record_rx_queue(skb, scrq_num);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002896
2897 if (flags & IBMVNIC_IP_CHKSUM_GOOD &&
2898 flags & IBMVNIC_TCP_UDP_CHKSUM_GOOD) {
2899 skb->ip_summed = CHECKSUM_UNNECESSARY;
2900 }
2901
2902 length = skb->len;
2903 napi_gro_receive(napi, skb); /* send it up */
2904 netdev->stats.rx_packets++;
2905 netdev->stats.rx_bytes += length;
John Allen3d52b592017-08-02 16:44:14 -05002906 adapter->rx_stats_buffers[scrq_num].packets++;
2907 adapter->rx_stats_buffers[scrq_num].bytes += length;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002908 frames_processed++;
2909 }
Nathan Fontenot152ce472017-05-26 10:30:54 -04002910
Dwip N. Banerjee41ed0a02020-11-18 19:12:25 -06002911 if (adapter->state != VNIC_CLOSING &&
2912 ((atomic_read(&adapter->rx_pool[scrq_num].available) <
2913 adapter->req_rx_add_entries_per_subcrq / 2) ||
2914 frames_processed < budget))
Nathan Fontenot152ce472017-05-26 10:30:54 -04002915 replenish_rx_pool(adapter, &adapter->rx_pool[scrq_num]);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002916 if (frames_processed < budget) {
Dwip N. Banerjeeec20f362020-11-18 19:12:23 -06002917 if (napi_complete_done(napi, frames_processed)) {
2918 enable_scrq_irq(adapter, rx_scrq);
2919 if (pending_scrq(adapter, rx_scrq)) {
Dwip N. Banerjeeec20f362020-11-18 19:12:23 -06002920 if (napi_reschedule(napi)) {
2921 disable_scrq_irq(adapter, rx_scrq);
2922 goto restart_poll;
2923 }
2924 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002925 }
2926 }
2927 return frames_processed;
2928}
2929
John Allenc26eba02017-10-26 16:23:25 -05002930static int wait_for_reset(struct ibmvnic_adapter *adapter)
2931{
Thomas Falconaf894d22018-04-06 18:37:04 -05002932 int rc, ret;
2933
John Allenc26eba02017-10-26 16:23:25 -05002934 adapter->fallback.mtu = adapter->req_mtu;
2935 adapter->fallback.rx_queues = adapter->req_rx_queues;
2936 adapter->fallback.tx_queues = adapter->req_tx_queues;
2937 adapter->fallback.rx_entries = adapter->req_rx_add_entries_per_subcrq;
2938 adapter->fallback.tx_entries = adapter->req_tx_entries_per_subcrq;
2939
Thomas Falcon070eca92019-11-25 17:12:53 -06002940 reinit_completion(&adapter->reset_done);
John Allenc26eba02017-10-26 16:23:25 -05002941 adapter->wait_for_reset = true;
Thomas Falconaf894d22018-04-06 18:37:04 -05002942 rc = ibmvnic_reset(adapter, VNIC_RESET_CHANGE_PARAM);
Thomas Falcon476d96c2019-11-25 17:12:55 -06002943
2944 if (rc) {
2945 ret = rc;
2946 goto out;
2947 }
2948 rc = ibmvnic_wait_for_completion(adapter, &adapter->reset_done, 60000);
2949 if (rc) {
2950 ret = -ENODEV;
2951 goto out;
2952 }
John Allenc26eba02017-10-26 16:23:25 -05002953
Thomas Falconaf894d22018-04-06 18:37:04 -05002954 ret = 0;
John Allenc26eba02017-10-26 16:23:25 -05002955 if (adapter->reset_done_rc) {
Thomas Falconaf894d22018-04-06 18:37:04 -05002956 ret = -EIO;
John Allenc26eba02017-10-26 16:23:25 -05002957 adapter->desired.mtu = adapter->fallback.mtu;
2958 adapter->desired.rx_queues = adapter->fallback.rx_queues;
2959 adapter->desired.tx_queues = adapter->fallback.tx_queues;
2960 adapter->desired.rx_entries = adapter->fallback.rx_entries;
2961 adapter->desired.tx_entries = adapter->fallback.tx_entries;
2962
Thomas Falcon070eca92019-11-25 17:12:53 -06002963 reinit_completion(&adapter->reset_done);
Thomas Falconaf894d22018-04-06 18:37:04 -05002964 adapter->wait_for_reset = true;
2965 rc = ibmvnic_reset(adapter, VNIC_RESET_CHANGE_PARAM);
Thomas Falcon476d96c2019-11-25 17:12:55 -06002966 if (rc) {
2967 ret = rc;
2968 goto out;
2969 }
2970 rc = ibmvnic_wait_for_completion(adapter, &adapter->reset_done,
2971 60000);
2972 if (rc) {
2973 ret = -ENODEV;
2974 goto out;
2975 }
John Allenc26eba02017-10-26 16:23:25 -05002976 }
Thomas Falcon476d96c2019-11-25 17:12:55 -06002977out:
John Allenc26eba02017-10-26 16:23:25 -05002978 adapter->wait_for_reset = false;
2979
Thomas Falconaf894d22018-04-06 18:37:04 -05002980 return ret;
John Allenc26eba02017-10-26 16:23:25 -05002981}
2982
John Allen3a807b72017-06-06 16:55:52 -05002983static int ibmvnic_change_mtu(struct net_device *netdev, int new_mtu)
2984{
John Allenc26eba02017-10-26 16:23:25 -05002985 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2986
2987 adapter->desired.mtu = new_mtu + ETH_HLEN;
2988
2989 return wait_for_reset(adapter);
John Allen3a807b72017-06-06 16:55:52 -05002990}
2991
Thomas Falconf10b09e2018-03-12 11:51:05 -05002992static netdev_features_t ibmvnic_features_check(struct sk_buff *skb,
2993 struct net_device *dev,
2994 netdev_features_t features)
2995{
2996 /* Some backing hardware adapters can not
2997 * handle packets with a MSS less than 224
2998 * or with only one segment.
2999 */
3000 if (skb_is_gso(skb)) {
3001 if (skb_shinfo(skb)->gso_size < 224 ||
3002 skb_shinfo(skb)->gso_segs == 1)
3003 features &= ~NETIF_F_GSO_MASK;
3004 }
3005
3006 return features;
3007}
3008
Thomas Falcon032c5e82015-12-21 11:26:06 -06003009static const struct net_device_ops ibmvnic_netdev_ops = {
3010 .ndo_open = ibmvnic_open,
3011 .ndo_stop = ibmvnic_close,
3012 .ndo_start_xmit = ibmvnic_xmit,
3013 .ndo_set_rx_mode = ibmvnic_set_multi,
3014 .ndo_set_mac_address = ibmvnic_set_mac,
3015 .ndo_validate_addr = eth_validate_addr,
Thomas Falcon032c5e82015-12-21 11:26:06 -06003016 .ndo_tx_timeout = ibmvnic_tx_timeout,
John Allen3a807b72017-06-06 16:55:52 -05003017 .ndo_change_mtu = ibmvnic_change_mtu,
Thomas Falconf10b09e2018-03-12 11:51:05 -05003018 .ndo_features_check = ibmvnic_features_check,
Thomas Falcon032c5e82015-12-21 11:26:06 -06003019};
3020
3021/* ethtool functions */
3022
Philippe Reynes8a433792017-01-07 22:37:29 +01003023static int ibmvnic_get_link_ksettings(struct net_device *netdev,
3024 struct ethtool_link_ksettings *cmd)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003025{
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03003026 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
3027 int rc;
Philippe Reynes8a433792017-01-07 22:37:29 +01003028
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03003029 rc = send_query_phys_parms(adapter);
3030 if (rc) {
3031 adapter->speed = SPEED_UNKNOWN;
3032 adapter->duplex = DUPLEX_UNKNOWN;
3033 }
3034 cmd->base.speed = adapter->speed;
3035 cmd->base.duplex = adapter->duplex;
Philippe Reynes8a433792017-01-07 22:37:29 +01003036 cmd->base.port = PORT_FIBRE;
3037 cmd->base.phy_address = 0;
3038 cmd->base.autoneg = AUTONEG_ENABLE;
3039
Thomas Falcon032c5e82015-12-21 11:26:06 -06003040 return 0;
3041}
3042
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02003043static void ibmvnic_get_drvinfo(struct net_device *netdev,
Thomas Falcon032c5e82015-12-21 11:26:06 -06003044 struct ethtool_drvinfo *info)
3045{
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02003046 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
3047
Lijun Pan8a96c802021-02-11 00:43:25 -06003048 strscpy(info->driver, ibmvnic_driver_name, sizeof(info->driver));
3049 strscpy(info->version, IBMVNIC_DRIVER_VERSION, sizeof(info->version));
3050 strscpy(info->fw_version, adapter->fw_version,
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02003051 sizeof(info->fw_version));
Thomas Falcon032c5e82015-12-21 11:26:06 -06003052}
3053
3054static u32 ibmvnic_get_msglevel(struct net_device *netdev)
3055{
3056 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
3057
3058 return adapter->msg_enable;
3059}
3060
3061static void ibmvnic_set_msglevel(struct net_device *netdev, u32 data)
3062{
3063 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
3064
3065 adapter->msg_enable = data;
3066}
3067
3068static u32 ibmvnic_get_link(struct net_device *netdev)
3069{
3070 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
3071
3072 /* Don't need to send a query because we request a logical link up at
3073 * init and then we wait for link state indications
3074 */
3075 return adapter->logical_link_state;
3076}
3077
3078static void ibmvnic_get_ringparam(struct net_device *netdev,
Hao Chen74624942021-11-18 20:12:43 +08003079 struct ethtool_ringparam *ring,
3080 struct kernel_ethtool_ringparam *kernel_ring,
3081 struct netlink_ext_ack *extack)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003082{
John Allenbc131b32017-08-02 16:46:30 -05003083 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
3084
Thomas Falcon723ad912018-09-28 18:38:26 -05003085 if (adapter->priv_flags & IBMVNIC_USE_SERVER_MAXES) {
3086 ring->rx_max_pending = adapter->max_rx_add_entries_per_subcrq;
3087 ring->tx_max_pending = adapter->max_tx_entries_per_subcrq;
3088 } else {
3089 ring->rx_max_pending = IBMVNIC_MAX_QUEUE_SZ;
3090 ring->tx_max_pending = IBMVNIC_MAX_QUEUE_SZ;
3091 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06003092 ring->rx_mini_max_pending = 0;
3093 ring->rx_jumbo_max_pending = 0;
John Allenbc131b32017-08-02 16:46:30 -05003094 ring->rx_pending = adapter->req_rx_add_entries_per_subcrq;
3095 ring->tx_pending = adapter->req_tx_entries_per_subcrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003096 ring->rx_mini_pending = 0;
3097 ring->rx_jumbo_pending = 0;
3098}
3099
John Allenc26eba02017-10-26 16:23:25 -05003100static int ibmvnic_set_ringparam(struct net_device *netdev,
Hao Chen74624942021-11-18 20:12:43 +08003101 struct ethtool_ringparam *ring,
3102 struct kernel_ethtool_ringparam *kernel_ring,
3103 struct netlink_ext_ack *extack)
John Allenc26eba02017-10-26 16:23:25 -05003104{
3105 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Thomas Falcon723ad912018-09-28 18:38:26 -05003106 int ret;
John Allenc26eba02017-10-26 16:23:25 -05003107
Thomas Falcon723ad912018-09-28 18:38:26 -05003108 ret = 0;
John Allenc26eba02017-10-26 16:23:25 -05003109 adapter->desired.rx_entries = ring->rx_pending;
3110 adapter->desired.tx_entries = ring->tx_pending;
3111
Thomas Falcon723ad912018-09-28 18:38:26 -05003112 ret = wait_for_reset(adapter);
3113
3114 if (!ret &&
3115 (adapter->req_rx_add_entries_per_subcrq != ring->rx_pending ||
3116 adapter->req_tx_entries_per_subcrq != ring->tx_pending))
3117 netdev_info(netdev,
3118 "Could not match full ringsize request. Requested: RX %d, TX %d; Allowed: RX %llu, TX %llu\n",
3119 ring->rx_pending, ring->tx_pending,
3120 adapter->req_rx_add_entries_per_subcrq,
3121 adapter->req_tx_entries_per_subcrq);
3122 return ret;
John Allenc26eba02017-10-26 16:23:25 -05003123}
3124
John Allenc2dbeb62017-08-02 16:47:17 -05003125static void ibmvnic_get_channels(struct net_device *netdev,
3126 struct ethtool_channels *channels)
3127{
3128 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
3129
Thomas Falcon723ad912018-09-28 18:38:26 -05003130 if (adapter->priv_flags & IBMVNIC_USE_SERVER_MAXES) {
3131 channels->max_rx = adapter->max_rx_queues;
3132 channels->max_tx = adapter->max_tx_queues;
3133 } else {
3134 channels->max_rx = IBMVNIC_MAX_QUEUES;
3135 channels->max_tx = IBMVNIC_MAX_QUEUES;
3136 }
3137
John Allenc2dbeb62017-08-02 16:47:17 -05003138 channels->max_other = 0;
3139 channels->max_combined = 0;
3140 channels->rx_count = adapter->req_rx_queues;
3141 channels->tx_count = adapter->req_tx_queues;
3142 channels->other_count = 0;
3143 channels->combined_count = 0;
3144}
3145
John Allenc26eba02017-10-26 16:23:25 -05003146static int ibmvnic_set_channels(struct net_device *netdev,
3147 struct ethtool_channels *channels)
3148{
3149 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Thomas Falcon723ad912018-09-28 18:38:26 -05003150 int ret;
John Allenc26eba02017-10-26 16:23:25 -05003151
Thomas Falcon723ad912018-09-28 18:38:26 -05003152 ret = 0;
John Allenc26eba02017-10-26 16:23:25 -05003153 adapter->desired.rx_queues = channels->rx_count;
3154 adapter->desired.tx_queues = channels->tx_count;
3155
Thomas Falcon723ad912018-09-28 18:38:26 -05003156 ret = wait_for_reset(adapter);
3157
3158 if (!ret &&
3159 (adapter->req_rx_queues != channels->rx_count ||
3160 adapter->req_tx_queues != channels->tx_count))
3161 netdev_info(netdev,
3162 "Could not match full channels request. Requested: RX %d, TX %d; Allowed: RX %llu, TX %llu\n",
3163 channels->rx_count, channels->tx_count,
3164 adapter->req_rx_queues, adapter->req_tx_queues);
3165 return ret;
John Allenc26eba02017-10-26 16:23:25 -05003166}
3167
Thomas Falcon032c5e82015-12-21 11:26:06 -06003168static void ibmvnic_get_strings(struct net_device *dev, u32 stringset, u8 *data)
3169{
John Allen3d52b592017-08-02 16:44:14 -05003170 struct ibmvnic_adapter *adapter = netdev_priv(dev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003171 int i;
3172
Thomas Falcon723ad912018-09-28 18:38:26 -05003173 switch (stringset) {
3174 case ETH_SS_STATS:
3175 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats);
3176 i++, data += ETH_GSTRING_LEN)
3177 memcpy(data, ibmvnic_stats[i].name, ETH_GSTRING_LEN);
3178
3179 for (i = 0; i < adapter->req_tx_queues; i++) {
3180 snprintf(data, ETH_GSTRING_LEN, "tx%d_packets", i);
3181 data += ETH_GSTRING_LEN;
3182
3183 snprintf(data, ETH_GSTRING_LEN, "tx%d_bytes", i);
3184 data += ETH_GSTRING_LEN;
3185
3186 snprintf(data, ETH_GSTRING_LEN,
3187 "tx%d_dropped_packets", i);
3188 data += ETH_GSTRING_LEN;
3189 }
3190
3191 for (i = 0; i < adapter->req_rx_queues; i++) {
3192 snprintf(data, ETH_GSTRING_LEN, "rx%d_packets", i);
3193 data += ETH_GSTRING_LEN;
3194
3195 snprintf(data, ETH_GSTRING_LEN, "rx%d_bytes", i);
3196 data += ETH_GSTRING_LEN;
3197
3198 snprintf(data, ETH_GSTRING_LEN, "rx%d_interrupts", i);
3199 data += ETH_GSTRING_LEN;
3200 }
3201 break;
3202
3203 case ETH_SS_PRIV_FLAGS:
3204 for (i = 0; i < ARRAY_SIZE(ibmvnic_priv_flags); i++)
3205 strcpy(data + i * ETH_GSTRING_LEN,
3206 ibmvnic_priv_flags[i]);
3207 break;
3208 default:
Thomas Falcon032c5e82015-12-21 11:26:06 -06003209 return;
John Allen3d52b592017-08-02 16:44:14 -05003210 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06003211}
3212
3213static int ibmvnic_get_sset_count(struct net_device *dev, int sset)
3214{
John Allen3d52b592017-08-02 16:44:14 -05003215 struct ibmvnic_adapter *adapter = netdev_priv(dev);
3216
Thomas Falcon032c5e82015-12-21 11:26:06 -06003217 switch (sset) {
3218 case ETH_SS_STATS:
John Allen3d52b592017-08-02 16:44:14 -05003219 return ARRAY_SIZE(ibmvnic_stats) +
3220 adapter->req_tx_queues * NUM_TX_STATS +
3221 adapter->req_rx_queues * NUM_RX_STATS;
Thomas Falcon723ad912018-09-28 18:38:26 -05003222 case ETH_SS_PRIV_FLAGS:
3223 return ARRAY_SIZE(ibmvnic_priv_flags);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003224 default:
3225 return -EOPNOTSUPP;
3226 }
3227}
3228
3229static void ibmvnic_get_ethtool_stats(struct net_device *dev,
3230 struct ethtool_stats *stats, u64 *data)
3231{
3232 struct ibmvnic_adapter *adapter = netdev_priv(dev);
3233 union ibmvnic_crq crq;
John Allen3d52b592017-08-02 16:44:14 -05003234 int i, j;
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05003235 int rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003236
3237 memset(&crq, 0, sizeof(crq));
3238 crq.request_statistics.first = IBMVNIC_CRQ_CMD;
3239 crq.request_statistics.cmd = REQUEST_STATISTICS;
3240 crq.request_statistics.ioba = cpu_to_be32(adapter->stats_token);
3241 crq.request_statistics.len =
3242 cpu_to_be32(sizeof(struct ibmvnic_statistics));
Thomas Falcon032c5e82015-12-21 11:26:06 -06003243
3244 /* Wait for data to be written */
Thomas Falcon070eca92019-11-25 17:12:53 -06003245 reinit_completion(&adapter->stats_done);
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05003246 rc = ibmvnic_send_crq(adapter, &crq);
3247 if (rc)
3248 return;
Thomas Falcon476d96c2019-11-25 17:12:55 -06003249 rc = ibmvnic_wait_for_completion(adapter, &adapter->stats_done, 10000);
3250 if (rc)
3251 return;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003252
3253 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++)
Lijun Pan91dc5d22021-02-11 00:43:22 -06003254 data[i] = be64_to_cpu(IBMVNIC_GET_STAT
3255 (adapter, ibmvnic_stats[i].offset));
John Allen3d52b592017-08-02 16:44:14 -05003256
3257 for (j = 0; j < adapter->req_tx_queues; j++) {
3258 data[i] = adapter->tx_stats_buffers[j].packets;
3259 i++;
3260 data[i] = adapter->tx_stats_buffers[j].bytes;
3261 i++;
3262 data[i] = adapter->tx_stats_buffers[j].dropped_packets;
3263 i++;
3264 }
3265
3266 for (j = 0; j < adapter->req_rx_queues; j++) {
3267 data[i] = adapter->rx_stats_buffers[j].packets;
3268 i++;
3269 data[i] = adapter->rx_stats_buffers[j].bytes;
3270 i++;
3271 data[i] = adapter->rx_stats_buffers[j].interrupts;
3272 i++;
3273 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06003274}
3275
Thomas Falcon723ad912018-09-28 18:38:26 -05003276static u32 ibmvnic_get_priv_flags(struct net_device *netdev)
3277{
3278 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
3279
3280 return adapter->priv_flags;
3281}
3282
3283static int ibmvnic_set_priv_flags(struct net_device *netdev, u32 flags)
3284{
3285 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
3286 bool which_maxes = !!(flags & IBMVNIC_USE_SERVER_MAXES);
3287
3288 if (which_maxes)
3289 adapter->priv_flags |= IBMVNIC_USE_SERVER_MAXES;
3290 else
3291 adapter->priv_flags &= ~IBMVNIC_USE_SERVER_MAXES;
3292
3293 return 0;
3294}
Lijun Pan91dc5d22021-02-11 00:43:22 -06003295
Thomas Falcon032c5e82015-12-21 11:26:06 -06003296static const struct ethtool_ops ibmvnic_ethtool_ops = {
Thomas Falcon032c5e82015-12-21 11:26:06 -06003297 .get_drvinfo = ibmvnic_get_drvinfo,
3298 .get_msglevel = ibmvnic_get_msglevel,
3299 .set_msglevel = ibmvnic_set_msglevel,
3300 .get_link = ibmvnic_get_link,
3301 .get_ringparam = ibmvnic_get_ringparam,
John Allenc26eba02017-10-26 16:23:25 -05003302 .set_ringparam = ibmvnic_set_ringparam,
John Allenc2dbeb62017-08-02 16:47:17 -05003303 .get_channels = ibmvnic_get_channels,
John Allenc26eba02017-10-26 16:23:25 -05003304 .set_channels = ibmvnic_set_channels,
Thomas Falcon032c5e82015-12-21 11:26:06 -06003305 .get_strings = ibmvnic_get_strings,
3306 .get_sset_count = ibmvnic_get_sset_count,
3307 .get_ethtool_stats = ibmvnic_get_ethtool_stats,
Philippe Reynes8a433792017-01-07 22:37:29 +01003308 .get_link_ksettings = ibmvnic_get_link_ksettings,
Thomas Falcon723ad912018-09-28 18:38:26 -05003309 .get_priv_flags = ibmvnic_get_priv_flags,
3310 .set_priv_flags = ibmvnic_set_priv_flags,
Thomas Falcon032c5e82015-12-21 11:26:06 -06003311};
3312
3313/* Routines for managing CRQs/sCRQs */
3314
Nathan Fontenot57a49432017-05-26 10:31:12 -04003315static int reset_one_sub_crq_queue(struct ibmvnic_adapter *adapter,
3316 struct ibmvnic_sub_crq_queue *scrq)
3317{
3318 int rc;
3319
Dany Madden9281cf22020-11-25 18:04:26 -06003320 if (!scrq) {
YANG LI862aecb2020-12-30 15:23:14 +08003321 netdev_dbg(adapter->netdev, "Invalid scrq reset.\n");
Dany Madden9281cf22020-11-25 18:04:26 -06003322 return -EINVAL;
3323 }
3324
Nathan Fontenot57a49432017-05-26 10:31:12 -04003325 if (scrq->irq) {
3326 free_irq(scrq->irq, scrq);
3327 irq_dispose_mapping(scrq->irq);
3328 scrq->irq = 0;
3329 }
3330
Dany Madden9281cf22020-11-25 18:04:26 -06003331 if (scrq->msgs) {
3332 memset(scrq->msgs, 0, 4 * PAGE_SIZE);
3333 atomic_set(&scrq->used, 0);
3334 scrq->cur = 0;
Jakub Kicinski55fd59b2020-12-03 15:42:13 -08003335 scrq->ind_buf.index = 0;
Dany Madden9281cf22020-11-25 18:04:26 -06003336 } else {
3337 netdev_dbg(adapter->netdev, "Invalid scrq reset\n");
3338 return -EINVAL;
3339 }
Nathan Fontenot57a49432017-05-26 10:31:12 -04003340
3341 rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
3342 4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
3343 return rc;
3344}
3345
3346static int reset_sub_crq_queues(struct ibmvnic_adapter *adapter)
3347{
3348 int i, rc;
3349
Lijun Pana0faaa22020-11-23 13:35:45 -06003350 if (!adapter->tx_scrq || !adapter->rx_scrq)
3351 return -EINVAL;
3352
Nathan Fontenot57a49432017-05-26 10:31:12 -04003353 for (i = 0; i < adapter->req_tx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05003354 netdev_dbg(adapter->netdev, "Re-setting tx_scrq[%d]\n", i);
Nathan Fontenot57a49432017-05-26 10:31:12 -04003355 rc = reset_one_sub_crq_queue(adapter, adapter->tx_scrq[i]);
3356 if (rc)
3357 return rc;
3358 }
3359
3360 for (i = 0; i < adapter->req_rx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05003361 netdev_dbg(adapter->netdev, "Re-setting rx_scrq[%d]\n", i);
Nathan Fontenot57a49432017-05-26 10:31:12 -04003362 rc = reset_one_sub_crq_queue(adapter, adapter->rx_scrq[i]);
3363 if (rc)
3364 return rc;
3365 }
3366
Nathan Fontenot57a49432017-05-26 10:31:12 -04003367 return rc;
3368}
3369
Thomas Falcon032c5e82015-12-21 11:26:06 -06003370static void release_sub_crq_queue(struct ibmvnic_adapter *adapter,
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06003371 struct ibmvnic_sub_crq_queue *scrq,
3372 bool do_h_free)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003373{
3374 struct device *dev = &adapter->vdev->dev;
3375 long rc;
3376
3377 netdev_dbg(adapter->netdev, "Releasing sub-CRQ\n");
3378
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06003379 if (do_h_free) {
3380 /* Close the sub-crqs */
3381 do {
3382 rc = plpar_hcall_norets(H_FREE_SUB_CRQ,
3383 adapter->vdev->unit_address,
3384 scrq->crq_num);
3385 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
Thomas Falcon032c5e82015-12-21 11:26:06 -06003386
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06003387 if (rc) {
3388 netdev_err(adapter->netdev,
3389 "Failed to release sub-CRQ %16lx, rc = %ld\n",
3390 scrq->crq_num, rc);
3391 }
Thomas Falconffa73852017-04-19 13:44:29 -04003392 }
3393
Thomas Falconf019fb62020-11-18 19:12:17 -06003394 dma_free_coherent(dev,
3395 IBMVNIC_IND_ARR_SZ,
3396 scrq->ind_buf.indir_arr,
3397 scrq->ind_buf.indir_dma);
3398
Thomas Falcon032c5e82015-12-21 11:26:06 -06003399 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
3400 DMA_BIDIRECTIONAL);
3401 free_pages((unsigned long)scrq->msgs, 2);
3402 kfree(scrq);
3403}
3404
3405static struct ibmvnic_sub_crq_queue *init_sub_crq_queue(struct ibmvnic_adapter
3406 *adapter)
3407{
3408 struct device *dev = &adapter->vdev->dev;
3409 struct ibmvnic_sub_crq_queue *scrq;
3410 int rc;
3411
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04003412 scrq = kzalloc(sizeof(*scrq), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003413 if (!scrq)
3414 return NULL;
3415
Nathan Fontenot7f7adc52017-04-19 13:45:16 -04003416 scrq->msgs =
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04003417 (union sub_crq *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, 2);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003418 if (!scrq->msgs) {
3419 dev_warn(dev, "Couldn't allocate crq queue messages page\n");
3420 goto zero_page_failed;
3421 }
3422
3423 scrq->msg_token = dma_map_single(dev, scrq->msgs, 4 * PAGE_SIZE,
3424 DMA_BIDIRECTIONAL);
3425 if (dma_mapping_error(dev, scrq->msg_token)) {
3426 dev_warn(dev, "Couldn't map crq queue messages page\n");
3427 goto map_failed;
3428 }
3429
3430 rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
3431 4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
3432
3433 if (rc == H_RESOURCE)
3434 rc = ibmvnic_reset_crq(adapter);
3435
3436 if (rc == H_CLOSED) {
3437 dev_warn(dev, "Partner adapter not ready, waiting.\n");
3438 } else if (rc) {
3439 dev_warn(dev, "Error %d registering sub-crq\n", rc);
3440 goto reg_failed;
3441 }
3442
Thomas Falcon032c5e82015-12-21 11:26:06 -06003443 scrq->adapter = adapter;
3444 scrq->size = 4 * PAGE_SIZE / sizeof(*scrq->msgs);
Thomas Falconf019fb62020-11-18 19:12:17 -06003445 scrq->ind_buf.index = 0;
3446
3447 scrq->ind_buf.indir_arr =
3448 dma_alloc_coherent(dev,
3449 IBMVNIC_IND_ARR_SZ,
3450 &scrq->ind_buf.indir_dma,
3451 GFP_KERNEL);
3452
3453 if (!scrq->ind_buf.indir_arr)
3454 goto indir_failed;
3455
Thomas Falcon032c5e82015-12-21 11:26:06 -06003456 spin_lock_init(&scrq->lock);
3457
3458 netdev_dbg(adapter->netdev,
3459 "sub-crq initialized, num %lx, hw_irq=%lx, irq=%x\n",
3460 scrq->crq_num, scrq->hw_irq, scrq->irq);
3461
3462 return scrq;
3463
Thomas Falconf019fb62020-11-18 19:12:17 -06003464indir_failed:
3465 do {
3466 rc = plpar_hcall_norets(H_FREE_SUB_CRQ,
3467 adapter->vdev->unit_address,
3468 scrq->crq_num);
3469 } while (rc == H_BUSY || rc == H_IS_LONG_BUSY(rc));
Thomas Falcon032c5e82015-12-21 11:26:06 -06003470reg_failed:
3471 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
3472 DMA_BIDIRECTIONAL);
3473map_failed:
3474 free_pages((unsigned long)scrq->msgs, 2);
3475zero_page_failed:
3476 kfree(scrq);
3477
3478 return NULL;
3479}
3480
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06003481static void release_sub_crqs(struct ibmvnic_adapter *adapter, bool do_h_free)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003482{
3483 int i;
3484
3485 if (adapter->tx_scrq) {
Nathan Fontenot82e3be32018-02-21 21:33:56 -06003486 for (i = 0; i < adapter->num_active_tx_scrqs; i++) {
Nathan Fontenotb5108882017-03-30 02:49:18 -04003487 if (!adapter->tx_scrq[i])
3488 continue;
3489
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05003490 netdev_dbg(adapter->netdev, "Releasing tx_scrq[%d]\n",
3491 i);
Sukadev Bhattiprolu65d64702021-06-23 21:13:12 -07003492 ibmvnic_tx_scrq_clean_buffer(adapter, adapter->tx_scrq[i]);
Nathan Fontenotb5108882017-03-30 02:49:18 -04003493 if (adapter->tx_scrq[i]->irq) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06003494 free_irq(adapter->tx_scrq[i]->irq,
3495 adapter->tx_scrq[i]);
Thomas Falcon88eb98a2016-07-06 15:35:16 -05003496 irq_dispose_mapping(adapter->tx_scrq[i]->irq);
Nathan Fontenotb5108882017-03-30 02:49:18 -04003497 adapter->tx_scrq[i]->irq = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003498 }
Nathan Fontenotb5108882017-03-30 02:49:18 -04003499
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06003500 release_sub_crq_queue(adapter, adapter->tx_scrq[i],
3501 do_h_free);
Nathan Fontenotb5108882017-03-30 02:49:18 -04003502 }
3503
Nathan Fontenot9501df32017-03-15 23:38:07 -04003504 kfree(adapter->tx_scrq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003505 adapter->tx_scrq = NULL;
Nathan Fontenot82e3be32018-02-21 21:33:56 -06003506 adapter->num_active_tx_scrqs = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003507 }
3508
3509 if (adapter->rx_scrq) {
Nathan Fontenot82e3be32018-02-21 21:33:56 -06003510 for (i = 0; i < adapter->num_active_rx_scrqs; i++) {
Nathan Fontenotb5108882017-03-30 02:49:18 -04003511 if (!adapter->rx_scrq[i])
3512 continue;
3513
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05003514 netdev_dbg(adapter->netdev, "Releasing rx_scrq[%d]\n",
3515 i);
Nathan Fontenotb5108882017-03-30 02:49:18 -04003516 if (adapter->rx_scrq[i]->irq) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06003517 free_irq(adapter->rx_scrq[i]->irq,
3518 adapter->rx_scrq[i]);
Thomas Falcon88eb98a2016-07-06 15:35:16 -05003519 irq_dispose_mapping(adapter->rx_scrq[i]->irq);
Nathan Fontenotb5108882017-03-30 02:49:18 -04003520 adapter->rx_scrq[i]->irq = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003521 }
Nathan Fontenotb5108882017-03-30 02:49:18 -04003522
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06003523 release_sub_crq_queue(adapter, adapter->rx_scrq[i],
3524 do_h_free);
Nathan Fontenotb5108882017-03-30 02:49:18 -04003525 }
3526
Nathan Fontenot9501df32017-03-15 23:38:07 -04003527 kfree(adapter->rx_scrq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003528 adapter->rx_scrq = NULL;
Nathan Fontenot82e3be32018-02-21 21:33:56 -06003529 adapter->num_active_rx_scrqs = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003530 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06003531}
3532
3533static int disable_scrq_irq(struct ibmvnic_adapter *adapter,
3534 struct ibmvnic_sub_crq_queue *scrq)
3535{
3536 struct device *dev = &adapter->vdev->dev;
3537 unsigned long rc;
3538
3539 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
3540 H_DISABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
3541 if (rc)
3542 dev_err(dev, "Couldn't disable scrq irq 0x%lx. rc=%ld\n",
3543 scrq->hw_irq, rc);
3544 return rc;
3545}
3546
3547static int enable_scrq_irq(struct ibmvnic_adapter *adapter,
3548 struct ibmvnic_sub_crq_queue *scrq)
3549{
3550 struct device *dev = &adapter->vdev->dev;
3551 unsigned long rc;
3552
3553 if (scrq->hw_irq > 0x100000000ULL) {
3554 dev_err(dev, "bad hw_irq = %lx\n", scrq->hw_irq);
3555 return 1;
3556 }
3557
Juliet Kim7ed5b312019-09-20 16:11:23 -04003558 if (test_bit(0, &adapter->resetting) &&
Nathan Fontenot73f9d362018-05-22 11:21:10 -05003559 adapter->reset_reason == VNIC_RESET_MOBILITY) {
Juliet Kim284f87d2019-11-20 10:50:03 -05003560 u64 val = (0xff000000) | scrq->hw_irq;
Nathan Fontenot73f9d362018-05-22 11:21:10 -05003561
Juliet Kim284f87d2019-11-20 10:50:03 -05003562 rc = plpar_hcall_norets(H_EOI, val);
Juliet Kim2df5c602019-11-20 10:50:04 -05003563 /* H_EOI would fail with rc = H_FUNCTION when running
3564 * in XIVE mode which is expected, but not an error.
3565 */
Sukadev Bhattiprolu154b3b22021-06-23 21:13:16 -07003566 if (rc && (rc != H_FUNCTION))
Juliet Kim284f87d2019-11-20 10:50:03 -05003567 dev_err(dev, "H_EOI FAILED irq 0x%llx. rc=%ld\n",
3568 val, rc);
Nathan Fontenot73f9d362018-05-22 11:21:10 -05003569 }
Thomas Falconf23e0642018-04-15 18:53:36 -05003570
Thomas Falcon032c5e82015-12-21 11:26:06 -06003571 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
3572 H_ENABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
3573 if (rc)
3574 dev_err(dev, "Couldn't enable scrq irq 0x%lx. rc=%ld\n",
3575 scrq->hw_irq, rc);
3576 return rc;
3577}
3578
3579static int ibmvnic_complete_tx(struct ibmvnic_adapter *adapter,
3580 struct ibmvnic_sub_crq_queue *scrq)
3581{
3582 struct device *dev = &adapter->vdev->dev;
Thomas Falcon06b3e352018-03-16 20:00:28 -05003583 struct ibmvnic_tx_pool *tx_pool;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003584 struct ibmvnic_tx_buff *txbuff;
Thomas Falcon0d973382020-11-18 19:12:19 -06003585 struct netdev_queue *txq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003586 union sub_crq *next;
3587 int index;
Thomas Falconc62aa372020-11-18 19:12:20 -06003588 int i;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003589
3590restart_loop:
3591 while (pending_scrq(adapter, scrq)) {
3592 unsigned int pool = scrq->pool_index;
Thomas Falconffc385b2018-02-18 10:08:41 -06003593 int num_entries = 0;
Thomas Falcon0d973382020-11-18 19:12:19 -06003594 int total_bytes = 0;
3595 int num_packets = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003596
3597 next = ibmvnic_next_scrq(adapter, scrq);
3598 for (i = 0; i < next->tx_comp.num_comps; i++) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06003599 index = be32_to_cpu(next->tx_comp.correlators[i]);
Thomas Falcon06b3e352018-03-16 20:00:28 -05003600 if (index & IBMVNIC_TSO_POOL_MASK) {
3601 tx_pool = &adapter->tso_pool[pool];
3602 index &= ~IBMVNIC_TSO_POOL_MASK;
3603 } else {
3604 tx_pool = &adapter->tx_pool[pool];
3605 }
3606
3607 txbuff = &tx_pool->tx_buff[index];
Thomas Falcon0d973382020-11-18 19:12:19 -06003608 num_packets++;
Thomas Falconffc385b2018-02-18 10:08:41 -06003609 num_entries += txbuff->num_entries;
Thomas Falcon0d973382020-11-18 19:12:19 -06003610 if (txbuff->skb) {
3611 total_bytes += txbuff->skb->len;
Lijun Panca09bf72021-04-13 03:33:25 -05003612 if (next->tx_comp.rcs[i]) {
3613 dev_err(dev, "tx error %x\n",
3614 next->tx_comp.rcs[i]);
3615 dev_kfree_skb_irq(txbuff->skb);
3616 } else {
3617 dev_consume_skb_irq(txbuff->skb);
3618 }
Thomas Falcon0d973382020-11-18 19:12:19 -06003619 txbuff->skb = NULL;
3620 } else {
3621 netdev_warn(adapter->netdev,
3622 "TX completion received with NULL socket buffer\n");
3623 }
Thomas Falcon06b3e352018-03-16 20:00:28 -05003624 tx_pool->free_map[tx_pool->producer_index] = index;
3625 tx_pool->producer_index =
3626 (tx_pool->producer_index + 1) %
3627 tx_pool->num_buffers;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003628 }
3629 /* remove tx_comp scrq*/
3630 next->tx_comp.first = 0;
Nathan Fontenot7c3e7de2017-05-03 14:05:25 -04003631
Thomas Falcon0d973382020-11-18 19:12:19 -06003632 txq = netdev_get_tx_queue(adapter->netdev, scrq->pool_index);
3633 netdev_tx_completed_queue(txq, num_packets, total_bytes);
3634
Thomas Falconffc385b2018-02-18 10:08:41 -06003635 if (atomic_sub_return(num_entries, &scrq->used) <=
Nathan Fontenot7c3e7de2017-05-03 14:05:25 -04003636 (adapter->req_tx_entries_per_subcrq / 2) &&
3637 __netif_subqueue_stopped(adapter->netdev,
3638 scrq->pool_index)) {
3639 netif_wake_subqueue(adapter->netdev, scrq->pool_index);
Thomas Falcon0aecb132018-02-26 18:10:58 -06003640 netdev_dbg(adapter->netdev, "Started queue %d\n",
3641 scrq->pool_index);
Nathan Fontenot7c3e7de2017-05-03 14:05:25 -04003642 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06003643 }
3644
3645 enable_scrq_irq(adapter, scrq);
3646
3647 if (pending_scrq(adapter, scrq)) {
3648 disable_scrq_irq(adapter, scrq);
3649 goto restart_loop;
3650 }
3651
3652 return 0;
3653}
3654
3655static irqreturn_t ibmvnic_interrupt_tx(int irq, void *instance)
3656{
3657 struct ibmvnic_sub_crq_queue *scrq = instance;
3658 struct ibmvnic_adapter *adapter = scrq->adapter;
3659
3660 disable_scrq_irq(adapter, scrq);
3661 ibmvnic_complete_tx(adapter, scrq);
3662
3663 return IRQ_HANDLED;
3664}
3665
3666static irqreturn_t ibmvnic_interrupt_rx(int irq, void *instance)
3667{
3668 struct ibmvnic_sub_crq_queue *scrq = instance;
3669 struct ibmvnic_adapter *adapter = scrq->adapter;
3670
Nathan Fontenot09fb35e2018-01-10 10:40:09 -06003671 /* When booting a kdump kernel we can hit pending interrupts
3672 * prior to completing driver initialization.
3673 */
3674 if (unlikely(adapter->state != VNIC_OPEN))
3675 return IRQ_NONE;
3676
John Allen3d52b592017-08-02 16:44:14 -05003677 adapter->rx_stats_buffers[scrq->scrq_num].interrupts++;
3678
Thomas Falcon032c5e82015-12-21 11:26:06 -06003679 if (napi_schedule_prep(&adapter->napi[scrq->scrq_num])) {
3680 disable_scrq_irq(adapter, scrq);
3681 __napi_schedule(&adapter->napi[scrq->scrq_num]);
3682 }
3683
3684 return IRQ_HANDLED;
3685}
3686
Thomas Falconea22d512016-07-06 15:35:17 -05003687static int init_sub_crq_irqs(struct ibmvnic_adapter *adapter)
3688{
3689 struct device *dev = &adapter->vdev->dev;
3690 struct ibmvnic_sub_crq_queue *scrq;
3691 int i = 0, j = 0;
3692 int rc = 0;
3693
3694 for (i = 0; i < adapter->req_tx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05003695 netdev_dbg(adapter->netdev, "Initializing tx_scrq[%d] irq\n",
3696 i);
Thomas Falconea22d512016-07-06 15:35:17 -05003697 scrq = adapter->tx_scrq[i];
3698 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
3699
Michael Ellerman99c17902016-09-10 19:59:05 +10003700 if (!scrq->irq) {
Thomas Falconea22d512016-07-06 15:35:17 -05003701 rc = -EINVAL;
3702 dev_err(dev, "Error mapping irq\n");
3703 goto req_tx_irq_failed;
3704 }
3705
Murilo Fossa Vicentinie56e2512019-04-25 11:02:33 -03003706 snprintf(scrq->name, sizeof(scrq->name), "ibmvnic-%x-tx%d",
3707 adapter->vdev->unit_address, i);
Thomas Falconea22d512016-07-06 15:35:17 -05003708 rc = request_irq(scrq->irq, ibmvnic_interrupt_tx,
Murilo Fossa Vicentinie56e2512019-04-25 11:02:33 -03003709 0, scrq->name, scrq);
Thomas Falconea22d512016-07-06 15:35:17 -05003710
3711 if (rc) {
3712 dev_err(dev, "Couldn't register tx irq 0x%x. rc=%d\n",
3713 scrq->irq, rc);
3714 irq_dispose_mapping(scrq->irq);
Nathan Fontenotaf9090c2018-02-20 11:04:18 -06003715 goto req_tx_irq_failed;
Thomas Falconea22d512016-07-06 15:35:17 -05003716 }
3717 }
3718
3719 for (i = 0; i < adapter->req_rx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05003720 netdev_dbg(adapter->netdev, "Initializing rx_scrq[%d] irq\n",
3721 i);
Thomas Falconea22d512016-07-06 15:35:17 -05003722 scrq = adapter->rx_scrq[i];
3723 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
Michael Ellerman99c17902016-09-10 19:59:05 +10003724 if (!scrq->irq) {
Thomas Falconea22d512016-07-06 15:35:17 -05003725 rc = -EINVAL;
3726 dev_err(dev, "Error mapping irq\n");
3727 goto req_rx_irq_failed;
3728 }
Murilo Fossa Vicentinie56e2512019-04-25 11:02:33 -03003729 snprintf(scrq->name, sizeof(scrq->name), "ibmvnic-%x-rx%d",
3730 adapter->vdev->unit_address, i);
Thomas Falconea22d512016-07-06 15:35:17 -05003731 rc = request_irq(scrq->irq, ibmvnic_interrupt_rx,
Murilo Fossa Vicentinie56e2512019-04-25 11:02:33 -03003732 0, scrq->name, scrq);
Thomas Falconea22d512016-07-06 15:35:17 -05003733 if (rc) {
3734 dev_err(dev, "Couldn't register rx irq 0x%x. rc=%d\n",
3735 scrq->irq, rc);
3736 irq_dispose_mapping(scrq->irq);
3737 goto req_rx_irq_failed;
3738 }
3739 }
3740 return rc;
3741
3742req_rx_irq_failed:
Thomas Falcon8bf371e2016-10-27 12:28:52 -05003743 for (j = 0; j < i; j++) {
Thomas Falconea22d512016-07-06 15:35:17 -05003744 free_irq(adapter->rx_scrq[j]->irq, adapter->rx_scrq[j]);
3745 irq_dispose_mapping(adapter->rx_scrq[j]->irq);
Thomas Falcon8bf371e2016-10-27 12:28:52 -05003746 }
Thomas Falconea22d512016-07-06 15:35:17 -05003747 i = adapter->req_tx_queues;
3748req_tx_irq_failed:
Thomas Falcon8bf371e2016-10-27 12:28:52 -05003749 for (j = 0; j < i; j++) {
Thomas Falconea22d512016-07-06 15:35:17 -05003750 free_irq(adapter->tx_scrq[j]->irq, adapter->tx_scrq[j]);
Thomas Falcon27a21452020-07-29 16:36:32 -05003751 irq_dispose_mapping(adapter->tx_scrq[j]->irq);
Thomas Falcon8bf371e2016-10-27 12:28:52 -05003752 }
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06003753 release_sub_crqs(adapter, 1);
Thomas Falconea22d512016-07-06 15:35:17 -05003754 return rc;
3755}
3756
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003757static int init_sub_crqs(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003758{
3759 struct device *dev = &adapter->vdev->dev;
3760 struct ibmvnic_sub_crq_queue **allqueues;
3761 int registered_queues = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003762 int total_queues;
3763 int more = 0;
Thomas Falconea22d512016-07-06 15:35:17 -05003764 int i;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003765
Thomas Falcon032c5e82015-12-21 11:26:06 -06003766 total_queues = adapter->req_tx_queues + adapter->req_rx_queues;
3767
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04003768 allqueues = kcalloc(total_queues, sizeof(*allqueues), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003769 if (!allqueues)
Dany Maddenb6ee5662021-12-14 00:17:47 -05003770 return -ENOMEM;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003771
3772 for (i = 0; i < total_queues; i++) {
3773 allqueues[i] = init_sub_crq_queue(adapter);
3774 if (!allqueues[i]) {
3775 dev_warn(dev, "Couldn't allocate all sub-crqs\n");
3776 break;
3777 }
3778 registered_queues++;
3779 }
3780
3781 /* Make sure we were able to register the minimum number of queues */
3782 if (registered_queues <
3783 adapter->min_tx_queues + adapter->min_rx_queues) {
3784 dev_err(dev, "Fatal: Couldn't init min number of sub-crqs\n");
3785 goto tx_failed;
3786 }
3787
3788 /* Distribute the failed allocated queues*/
3789 for (i = 0; i < total_queues - registered_queues + more ; i++) {
3790 netdev_dbg(adapter->netdev, "Reducing number of queues\n");
3791 switch (i % 3) {
3792 case 0:
3793 if (adapter->req_rx_queues > adapter->min_rx_queues)
3794 adapter->req_rx_queues--;
3795 else
3796 more++;
3797 break;
3798 case 1:
3799 if (adapter->req_tx_queues > adapter->min_tx_queues)
3800 adapter->req_tx_queues--;
3801 else
3802 more++;
3803 break;
3804 }
3805 }
3806
3807 adapter->tx_scrq = kcalloc(adapter->req_tx_queues,
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04003808 sizeof(*adapter->tx_scrq), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003809 if (!adapter->tx_scrq)
3810 goto tx_failed;
3811
3812 for (i = 0; i < adapter->req_tx_queues; i++) {
3813 adapter->tx_scrq[i] = allqueues[i];
3814 adapter->tx_scrq[i]->pool_index = i;
Nathan Fontenot82e3be32018-02-21 21:33:56 -06003815 adapter->num_active_tx_scrqs++;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003816 }
3817
3818 adapter->rx_scrq = kcalloc(adapter->req_rx_queues,
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04003819 sizeof(*adapter->rx_scrq), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003820 if (!adapter->rx_scrq)
3821 goto rx_failed;
3822
3823 for (i = 0; i < adapter->req_rx_queues; i++) {
3824 adapter->rx_scrq[i] = allqueues[i + adapter->req_tx_queues];
3825 adapter->rx_scrq[i]->scrq_num = i;
Nathan Fontenot82e3be32018-02-21 21:33:56 -06003826 adapter->num_active_rx_scrqs++;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003827 }
3828
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003829 kfree(allqueues);
3830 return 0;
3831
3832rx_failed:
3833 kfree(adapter->tx_scrq);
3834 adapter->tx_scrq = NULL;
3835tx_failed:
3836 for (i = 0; i < registered_queues; i++)
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06003837 release_sub_crq_queue(adapter, allqueues[i], 1);
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003838 kfree(allqueues);
Dany Maddenb6ee5662021-12-14 00:17:47 -05003839 return -ENOMEM;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003840}
3841
Lijun Pan09081b92020-09-27 20:13:27 -05003842static void send_request_cap(struct ibmvnic_adapter *adapter, int retry)
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003843{
3844 struct device *dev = &adapter->vdev->dev;
3845 union ibmvnic_crq crq;
John Allenc26eba02017-10-26 16:23:25 -05003846 int max_entries;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003847
3848 if (!retry) {
3849 /* Sub-CRQ entries are 32 byte long */
3850 int entries_page = 4 * PAGE_SIZE / (sizeof(u64) * 4);
3851
3852 if (adapter->min_tx_entries_per_subcrq > entries_page ||
3853 adapter->min_rx_add_entries_per_subcrq > entries_page) {
3854 dev_err(dev, "Fatal, invalid entries per sub-crq\n");
3855 return;
3856 }
3857
John Allenc26eba02017-10-26 16:23:25 -05003858 if (adapter->desired.mtu)
3859 adapter->req_mtu = adapter->desired.mtu;
3860 else
3861 adapter->req_mtu = adapter->netdev->mtu + ETH_HLEN;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003862
John Allenc26eba02017-10-26 16:23:25 -05003863 if (!adapter->desired.tx_entries)
3864 adapter->desired.tx_entries =
3865 adapter->max_tx_entries_per_subcrq;
3866 if (!adapter->desired.rx_entries)
3867 adapter->desired.rx_entries =
3868 adapter->max_rx_add_entries_per_subcrq;
3869
3870 max_entries = IBMVNIC_MAX_LTB_SIZE /
3871 (adapter->req_mtu + IBMVNIC_BUFFER_HLEN);
3872
3873 if ((adapter->req_mtu + IBMVNIC_BUFFER_HLEN) *
3874 adapter->desired.tx_entries > IBMVNIC_MAX_LTB_SIZE) {
3875 adapter->desired.tx_entries = max_entries;
3876 }
3877
3878 if ((adapter->req_mtu + IBMVNIC_BUFFER_HLEN) *
3879 adapter->desired.rx_entries > IBMVNIC_MAX_LTB_SIZE) {
3880 adapter->desired.rx_entries = max_entries;
3881 }
3882
3883 if (adapter->desired.tx_entries)
3884 adapter->req_tx_entries_per_subcrq =
3885 adapter->desired.tx_entries;
3886 else
3887 adapter->req_tx_entries_per_subcrq =
3888 adapter->max_tx_entries_per_subcrq;
3889
3890 if (adapter->desired.rx_entries)
3891 adapter->req_rx_add_entries_per_subcrq =
3892 adapter->desired.rx_entries;
3893 else
3894 adapter->req_rx_add_entries_per_subcrq =
3895 adapter->max_rx_add_entries_per_subcrq;
3896
3897 if (adapter->desired.tx_queues)
3898 adapter->req_tx_queues =
3899 adapter->desired.tx_queues;
3900 else
3901 adapter->req_tx_queues =
3902 adapter->opt_tx_comp_sub_queues;
3903
3904 if (adapter->desired.rx_queues)
3905 adapter->req_rx_queues =
3906 adapter->desired.rx_queues;
3907 else
3908 adapter->req_rx_queues =
3909 adapter->opt_rx_comp_queues;
3910
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003911 adapter->req_rx_add_queues = adapter->max_rx_add_queues;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003912 }
3913
Thomas Falcon032c5e82015-12-21 11:26:06 -06003914 memset(&crq, 0, sizeof(crq));
3915 crq.request_capability.first = IBMVNIC_CRQ_CMD;
3916 crq.request_capability.cmd = REQUEST_CAPABILITY;
3917
3918 crq.request_capability.capability = cpu_to_be16(REQ_TX_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06003919 crq.request_capability.number = cpu_to_be64(adapter->req_tx_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06003920 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003921 ibmvnic_send_crq(adapter, &crq);
3922
3923 crq.request_capability.capability = cpu_to_be16(REQ_RX_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06003924 crq.request_capability.number = cpu_to_be64(adapter->req_rx_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06003925 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003926 ibmvnic_send_crq(adapter, &crq);
3927
3928 crq.request_capability.capability = cpu_to_be16(REQ_RX_ADD_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06003929 crq.request_capability.number = cpu_to_be64(adapter->req_rx_add_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06003930 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003931 ibmvnic_send_crq(adapter, &crq);
3932
3933 crq.request_capability.capability =
3934 cpu_to_be16(REQ_TX_ENTRIES_PER_SUBCRQ);
3935 crq.request_capability.number =
Thomas Falconde89e852016-03-01 10:20:09 -06003936 cpu_to_be64(adapter->req_tx_entries_per_subcrq);
Thomas Falcon901e0402017-02-15 12:17:59 -06003937 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003938 ibmvnic_send_crq(adapter, &crq);
3939
3940 crq.request_capability.capability =
3941 cpu_to_be16(REQ_RX_ADD_ENTRIES_PER_SUBCRQ);
3942 crq.request_capability.number =
Thomas Falconde89e852016-03-01 10:20:09 -06003943 cpu_to_be64(adapter->req_rx_add_entries_per_subcrq);
Thomas Falcon901e0402017-02-15 12:17:59 -06003944 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003945 ibmvnic_send_crq(adapter, &crq);
3946
3947 crq.request_capability.capability = cpu_to_be16(REQ_MTU);
Thomas Falconde89e852016-03-01 10:20:09 -06003948 crq.request_capability.number = cpu_to_be64(adapter->req_mtu);
Thomas Falcon901e0402017-02-15 12:17:59 -06003949 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003950 ibmvnic_send_crq(adapter, &crq);
3951
3952 if (adapter->netdev->flags & IFF_PROMISC) {
3953 if (adapter->promisc_supported) {
3954 crq.request_capability.capability =
3955 cpu_to_be16(PROMISC_REQUESTED);
Thomas Falconde89e852016-03-01 10:20:09 -06003956 crq.request_capability.number = cpu_to_be64(1);
Thomas Falcon901e0402017-02-15 12:17:59 -06003957 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003958 ibmvnic_send_crq(adapter, &crq);
3959 }
3960 } else {
3961 crq.request_capability.capability =
3962 cpu_to_be16(PROMISC_REQUESTED);
Thomas Falconde89e852016-03-01 10:20:09 -06003963 crq.request_capability.number = cpu_to_be64(0);
Thomas Falcon901e0402017-02-15 12:17:59 -06003964 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003965 ibmvnic_send_crq(adapter, &crq);
3966 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06003967}
3968
3969static int pending_scrq(struct ibmvnic_adapter *adapter,
3970 struct ibmvnic_sub_crq_queue *scrq)
3971{
3972 union sub_crq *entry = &scrq->msgs[scrq->cur];
Lijun Pan665ab1e2021-01-29 19:19:04 -06003973 int rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003974
Lijun Pan665ab1e2021-01-29 19:19:04 -06003975 rc = !!(entry->generic.first & IBMVNIC_CRQ_CMD_RSP);
3976
3977 /* Ensure that the SCRQ valid flag is loaded prior to loading the
3978 * contents of the SCRQ descriptor
3979 */
3980 dma_rmb();
3981
3982 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003983}
3984
3985static union sub_crq *ibmvnic_next_scrq(struct ibmvnic_adapter *adapter,
3986 struct ibmvnic_sub_crq_queue *scrq)
3987{
3988 union sub_crq *entry;
3989 unsigned long flags;
3990
3991 spin_lock_irqsave(&scrq->lock, flags);
3992 entry = &scrq->msgs[scrq->cur];
3993 if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP) {
3994 if (++scrq->cur == scrq->size)
3995 scrq->cur = 0;
3996 } else {
3997 entry = NULL;
3998 }
3999 spin_unlock_irqrestore(&scrq->lock, flags);
4000
Lijun Pan665ab1e2021-01-29 19:19:04 -06004001 /* Ensure that the SCRQ valid flag is loaded prior to loading the
4002 * contents of the SCRQ descriptor
Thomas Falconb71ec952020-12-01 09:52:10 -06004003 */
4004 dma_rmb();
4005
Thomas Falcon032c5e82015-12-21 11:26:06 -06004006 return entry;
4007}
4008
4009static union ibmvnic_crq *ibmvnic_next_crq(struct ibmvnic_adapter *adapter)
4010{
4011 struct ibmvnic_crq_queue *queue = &adapter->crq;
4012 union ibmvnic_crq *crq;
4013
4014 crq = &queue->msgs[queue->cur];
4015 if (crq->generic.first & IBMVNIC_CRQ_CMD_RSP) {
4016 if (++queue->cur == queue->size)
4017 queue->cur = 0;
4018 } else {
4019 crq = NULL;
4020 }
4021
4022 return crq;
4023}
4024
Thomas Falcon2d14d372018-07-13 12:03:32 -05004025static void print_subcrq_error(struct device *dev, int rc, const char *func)
4026{
4027 switch (rc) {
4028 case H_PARAMETER:
4029 dev_warn_ratelimited(dev,
4030 "%s failed: Send request is malformed or adapter failover pending. (rc=%d)\n",
4031 func, rc);
4032 break;
4033 case H_CLOSED:
4034 dev_warn_ratelimited(dev,
4035 "%s failed: Backing queue closed. Adapter is down or failover pending. (rc=%d)\n",
4036 func, rc);
4037 break;
4038 default:
4039 dev_err_ratelimited(dev, "%s failed: (rc=%d)\n", func, rc);
4040 break;
4041 }
4042}
4043
Thomas Falconad7775d2016-04-01 17:20:34 -05004044static int send_subcrq_indirect(struct ibmvnic_adapter *adapter,
4045 u64 remote_handle, u64 ioba, u64 num_entries)
4046{
4047 unsigned int ua = adapter->vdev->unit_address;
4048 struct device *dev = &adapter->vdev->dev;
4049 int rc;
4050
4051 /* Make sure the hypervisor sees the complete request */
Lijun Pan1a421562021-02-12 20:36:46 -06004052 dma_wmb();
Thomas Falconad7775d2016-04-01 17:20:34 -05004053 rc = plpar_hcall_norets(H_SEND_SUB_CRQ_INDIRECT, ua,
4054 cpu_to_be64(remote_handle),
4055 ioba, num_entries);
4056
Thomas Falcon2d14d372018-07-13 12:03:32 -05004057 if (rc)
4058 print_subcrq_error(dev, rc, __func__);
Thomas Falconad7775d2016-04-01 17:20:34 -05004059
4060 return rc;
4061}
4062
Thomas Falcon032c5e82015-12-21 11:26:06 -06004063static int ibmvnic_send_crq(struct ibmvnic_adapter *adapter,
4064 union ibmvnic_crq *crq)
4065{
4066 unsigned int ua = adapter->vdev->unit_address;
4067 struct device *dev = &adapter->vdev->dev;
4068 u64 *u64_crq = (u64 *)crq;
4069 int rc;
4070
4071 netdev_dbg(adapter->netdev, "Sending CRQ: %016lx %016lx\n",
Lijun Pan429aa362021-02-11 00:43:18 -06004072 (unsigned long)cpu_to_be64(u64_crq[0]),
4073 (unsigned long)cpu_to_be64(u64_crq[1]));
Thomas Falcon032c5e82015-12-21 11:26:06 -06004074
Thomas Falcon51536982018-05-23 13:37:56 -05004075 if (!adapter->crq.active &&
4076 crq->generic.first != IBMVNIC_CRQ_INIT_CMD) {
4077 dev_warn(dev, "Invalid request detected while CRQ is inactive, possible device state change during reset\n");
4078 return -EINVAL;
4079 }
4080
Thomas Falcon032c5e82015-12-21 11:26:06 -06004081 /* Make sure the hypervisor sees the complete request */
Lijun Pan1a421562021-02-12 20:36:46 -06004082 dma_wmb();
Thomas Falcon032c5e82015-12-21 11:26:06 -06004083
4084 rc = plpar_hcall_norets(H_SEND_CRQ, ua,
4085 cpu_to_be64(u64_crq[0]),
4086 cpu_to_be64(u64_crq[1]));
4087
4088 if (rc) {
Nathan Fontenotec95dff2018-02-07 13:00:24 -06004089 if (rc == H_CLOSED) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06004090 dev_warn(dev, "CRQ Queue closed\n");
Lijun Panfa68bfa2020-08-19 17:52:24 -05004091 /* do not reset, report the fail, wait for passive init from server */
Nathan Fontenotec95dff2018-02-07 13:00:24 -06004092 }
4093
Thomas Falcon032c5e82015-12-21 11:26:06 -06004094 dev_warn(dev, "Send error (rc=%d)\n", rc);
4095 }
4096
4097 return rc;
4098}
4099
4100static int ibmvnic_send_crq_init(struct ibmvnic_adapter *adapter)
4101{
Thomas Falcon36a782f2020-08-31 11:59:57 -05004102 struct device *dev = &adapter->vdev->dev;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004103 union ibmvnic_crq crq;
Thomas Falcon36a782f2020-08-31 11:59:57 -05004104 int retries = 100;
4105 int rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004106
4107 memset(&crq, 0, sizeof(crq));
4108 crq.generic.first = IBMVNIC_CRQ_INIT_CMD;
4109 crq.generic.cmd = IBMVNIC_CRQ_INIT;
4110 netdev_dbg(adapter->netdev, "Sending CRQ init\n");
4111
Thomas Falcon36a782f2020-08-31 11:59:57 -05004112 do {
4113 rc = ibmvnic_send_crq(adapter, &crq);
4114 if (rc != H_CLOSED)
4115 break;
4116 retries--;
4117 msleep(50);
4118
4119 } while (retries > 0);
4120
4121 if (rc) {
4122 dev_err(dev, "Failed to send init request, rc = %d\n", rc);
4123 return rc;
4124 }
4125
4126 return 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004127}
4128
Nathan Fontenot37798d02017-11-08 11:23:56 -06004129struct vnic_login_client_data {
4130 u8 type;
4131 __be16 len;
Kees Cook08ea5562018-04-10 15:26:43 -07004132 char name[];
Nathan Fontenot37798d02017-11-08 11:23:56 -06004133} __packed;
4134
4135static int vnic_client_data_len(struct ibmvnic_adapter *adapter)
4136{
4137 int len;
4138
4139 /* Calculate the amount of buffer space needed for the
4140 * vnic client data in the login buffer. There are four entries,
4141 * OS name, LPAR name, device name, and a null last entry.
4142 */
4143 len = 4 * sizeof(struct vnic_login_client_data);
4144 len += 6; /* "Linux" plus NULL */
4145 len += strlen(utsname()->nodename) + 1;
4146 len += strlen(adapter->netdev->name) + 1;
4147
4148 return len;
4149}
4150
4151static void vnic_add_client_data(struct ibmvnic_adapter *adapter,
4152 struct vnic_login_client_data *vlcd)
4153{
4154 const char *os_name = "Linux";
4155 int len;
4156
4157 /* Type 1 - LPAR OS */
4158 vlcd->type = 1;
4159 len = strlen(os_name) + 1;
4160 vlcd->len = cpu_to_be16(len);
Kees Cookef2c3dd2021-06-21 14:35:09 -07004161 strscpy(vlcd->name, os_name, len);
Kees Cook08ea5562018-04-10 15:26:43 -07004162 vlcd = (struct vnic_login_client_data *)(vlcd->name + len);
Nathan Fontenot37798d02017-11-08 11:23:56 -06004163
4164 /* Type 2 - LPAR name */
4165 vlcd->type = 2;
4166 len = strlen(utsname()->nodename) + 1;
4167 vlcd->len = cpu_to_be16(len);
Kees Cookef2c3dd2021-06-21 14:35:09 -07004168 strscpy(vlcd->name, utsname()->nodename, len);
Kees Cook08ea5562018-04-10 15:26:43 -07004169 vlcd = (struct vnic_login_client_data *)(vlcd->name + len);
Nathan Fontenot37798d02017-11-08 11:23:56 -06004170
4171 /* Type 3 - device name */
4172 vlcd->type = 3;
4173 len = strlen(adapter->netdev->name) + 1;
4174 vlcd->len = cpu_to_be16(len);
Kees Cookef2c3dd2021-06-21 14:35:09 -07004175 strscpy(vlcd->name, adapter->netdev->name, len);
Nathan Fontenot37798d02017-11-08 11:23:56 -06004176}
4177
Thomas Falcon20a8ab72018-02-26 18:10:59 -06004178static int send_login(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06004179{
4180 struct ibmvnic_login_rsp_buffer *login_rsp_buffer;
4181 struct ibmvnic_login_buffer *login_buffer;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004182 struct device *dev = &adapter->vdev->dev;
Dany Maddenc98d9cc2020-11-25 18:04:30 -06004183 struct vnic_login_client_data *vlcd;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004184 dma_addr_t rsp_buffer_token;
4185 dma_addr_t buffer_token;
4186 size_t rsp_buffer_size;
4187 union ibmvnic_crq crq;
Dany Maddenc98d9cc2020-11-25 18:04:30 -06004188 int client_data_len;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004189 size_t buffer_size;
4190 __be64 *tx_list_p;
4191 __be64 *rx_list_p;
Dany Maddenc98d9cc2020-11-25 18:04:30 -06004192 int rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004193 int i;
4194
Thomas Falcon20a8ab72018-02-26 18:10:59 -06004195 if (!adapter->tx_scrq || !adapter->rx_scrq) {
4196 netdev_err(adapter->netdev,
4197 "RX or TX queues are not allocated, device login failed\n");
Dany Maddenb6ee5662021-12-14 00:17:47 -05004198 return -ENOMEM;
Thomas Falcon20a8ab72018-02-26 18:10:59 -06004199 }
4200
Lijun Pana0c8be52020-12-19 15:39:19 -06004201 release_login_buffer(adapter);
Thomas Falcon34f0f4e2018-02-13 18:23:40 -06004202 release_login_rsp_buffer(adapter);
Lijun Pana0c8be52020-12-19 15:39:19 -06004203
Nathan Fontenot37798d02017-11-08 11:23:56 -06004204 client_data_len = vnic_client_data_len(adapter);
4205
Thomas Falcon032c5e82015-12-21 11:26:06 -06004206 buffer_size =
4207 sizeof(struct ibmvnic_login_buffer) +
Nathan Fontenot37798d02017-11-08 11:23:56 -06004208 sizeof(u64) * (adapter->req_tx_queues + adapter->req_rx_queues) +
4209 client_data_len;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004210
Nathan Fontenot37798d02017-11-08 11:23:56 -06004211 login_buffer = kzalloc(buffer_size, GFP_ATOMIC);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004212 if (!login_buffer)
4213 goto buf_alloc_failed;
4214
4215 buffer_token = dma_map_single(dev, login_buffer, buffer_size,
4216 DMA_TO_DEVICE);
4217 if (dma_mapping_error(dev, buffer_token)) {
4218 dev_err(dev, "Couldn't map login buffer\n");
4219 goto buf_map_failed;
4220 }
4221
John Allen498cd8e2016-04-06 11:49:55 -05004222 rsp_buffer_size = sizeof(struct ibmvnic_login_rsp_buffer) +
4223 sizeof(u64) * adapter->req_tx_queues +
4224 sizeof(u64) * adapter->req_rx_queues +
4225 sizeof(u64) * adapter->req_rx_queues +
4226 sizeof(u8) * IBMVNIC_TX_DESC_VERSIONS;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004227
4228 login_rsp_buffer = kmalloc(rsp_buffer_size, GFP_ATOMIC);
4229 if (!login_rsp_buffer)
4230 goto buf_rsp_alloc_failed;
4231
4232 rsp_buffer_token = dma_map_single(dev, login_rsp_buffer,
4233 rsp_buffer_size, DMA_FROM_DEVICE);
4234 if (dma_mapping_error(dev, rsp_buffer_token)) {
4235 dev_err(dev, "Couldn't map login rsp buffer\n");
4236 goto buf_rsp_map_failed;
4237 }
Nathan Fontenot661a2622017-04-19 13:44:58 -04004238
Thomas Falcon032c5e82015-12-21 11:26:06 -06004239 adapter->login_buf = login_buffer;
4240 adapter->login_buf_token = buffer_token;
4241 adapter->login_buf_sz = buffer_size;
4242 adapter->login_rsp_buf = login_rsp_buffer;
4243 adapter->login_rsp_buf_token = rsp_buffer_token;
4244 adapter->login_rsp_buf_sz = rsp_buffer_size;
4245
4246 login_buffer->len = cpu_to_be32(buffer_size);
4247 login_buffer->version = cpu_to_be32(INITIAL_VERSION_LB);
4248 login_buffer->num_txcomp_subcrqs = cpu_to_be32(adapter->req_tx_queues);
4249 login_buffer->off_txcomp_subcrqs =
4250 cpu_to_be32(sizeof(struct ibmvnic_login_buffer));
4251 login_buffer->num_rxcomp_subcrqs = cpu_to_be32(adapter->req_rx_queues);
4252 login_buffer->off_rxcomp_subcrqs =
4253 cpu_to_be32(sizeof(struct ibmvnic_login_buffer) +
4254 sizeof(u64) * adapter->req_tx_queues);
4255 login_buffer->login_rsp_ioba = cpu_to_be32(rsp_buffer_token);
4256 login_buffer->login_rsp_len = cpu_to_be32(rsp_buffer_size);
4257
4258 tx_list_p = (__be64 *)((char *)login_buffer +
4259 sizeof(struct ibmvnic_login_buffer));
4260 rx_list_p = (__be64 *)((char *)login_buffer +
4261 sizeof(struct ibmvnic_login_buffer) +
4262 sizeof(u64) * adapter->req_tx_queues);
4263
4264 for (i = 0; i < adapter->req_tx_queues; i++) {
4265 if (adapter->tx_scrq[i]) {
Lijun Pan914789a2021-02-11 00:43:21 -06004266 tx_list_p[i] =
4267 cpu_to_be64(adapter->tx_scrq[i]->crq_num);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004268 }
4269 }
4270
4271 for (i = 0; i < adapter->req_rx_queues; i++) {
4272 if (adapter->rx_scrq[i]) {
Lijun Pan914789a2021-02-11 00:43:21 -06004273 rx_list_p[i] =
4274 cpu_to_be64(adapter->rx_scrq[i]->crq_num);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004275 }
4276 }
4277
Nathan Fontenot37798d02017-11-08 11:23:56 -06004278 /* Insert vNIC login client data */
4279 vlcd = (struct vnic_login_client_data *)
4280 ((char *)rx_list_p + (sizeof(u64) * adapter->req_rx_queues));
4281 login_buffer->client_data_offset =
4282 cpu_to_be32((char *)vlcd - (char *)login_buffer);
4283 login_buffer->client_data_len = cpu_to_be32(client_data_len);
4284
4285 vnic_add_client_data(adapter, vlcd);
4286
Thomas Falcon032c5e82015-12-21 11:26:06 -06004287 netdev_dbg(adapter->netdev, "Login Buffer:\n");
4288 for (i = 0; i < (adapter->login_buf_sz - 1) / 8 + 1; i++) {
4289 netdev_dbg(adapter->netdev, "%016lx\n",
Lijun Pan429aa362021-02-11 00:43:18 -06004290 ((unsigned long *)(adapter->login_buf))[i]);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004291 }
4292
4293 memset(&crq, 0, sizeof(crq));
4294 crq.login.first = IBMVNIC_CRQ_CMD;
4295 crq.login.cmd = LOGIN;
4296 crq.login.ioba = cpu_to_be32(buffer_token);
4297 crq.login.len = cpu_to_be32(buffer_size);
Sukadev Bhattiprolu76cdc5c2020-11-25 18:04:29 -06004298
4299 adapter->login_pending = true;
Dany Maddenc98d9cc2020-11-25 18:04:30 -06004300 rc = ibmvnic_send_crq(adapter, &crq);
4301 if (rc) {
4302 adapter->login_pending = false;
4303 netdev_err(adapter->netdev, "Failed to send login, rc=%d\n", rc);
4304 goto buf_rsp_map_failed;
4305 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06004306
Thomas Falcon20a8ab72018-02-26 18:10:59 -06004307 return 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004308
Thomas Falcon032c5e82015-12-21 11:26:06 -06004309buf_rsp_map_failed:
4310 kfree(login_rsp_buffer);
Dany Maddenc98d9cc2020-11-25 18:04:30 -06004311 adapter->login_rsp_buf = NULL;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004312buf_rsp_alloc_failed:
4313 dma_unmap_single(dev, buffer_token, buffer_size, DMA_TO_DEVICE);
4314buf_map_failed:
4315 kfree(login_buffer);
Dany Maddenc98d9cc2020-11-25 18:04:30 -06004316 adapter->login_buf = NULL;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004317buf_alloc_failed:
Dany Maddenb6ee5662021-12-14 00:17:47 -05004318 return -ENOMEM;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004319}
4320
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05004321static int send_request_map(struct ibmvnic_adapter *adapter, dma_addr_t addr,
4322 u32 len, u8 map_id)
Thomas Falcon032c5e82015-12-21 11:26:06 -06004323{
4324 union ibmvnic_crq crq;
4325
4326 memset(&crq, 0, sizeof(crq));
4327 crq.request_map.first = IBMVNIC_CRQ_CMD;
4328 crq.request_map.cmd = REQUEST_MAP;
4329 crq.request_map.map_id = map_id;
4330 crq.request_map.ioba = cpu_to_be32(addr);
4331 crq.request_map.len = cpu_to_be32(len);
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05004332 return ibmvnic_send_crq(adapter, &crq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004333}
4334
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05004335static int send_request_unmap(struct ibmvnic_adapter *adapter, u8 map_id)
Thomas Falcon032c5e82015-12-21 11:26:06 -06004336{
4337 union ibmvnic_crq crq;
4338
4339 memset(&crq, 0, sizeof(crq));
4340 crq.request_unmap.first = IBMVNIC_CRQ_CMD;
4341 crq.request_unmap.cmd = REQUEST_UNMAP;
4342 crq.request_unmap.map_id = map_id;
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05004343 return ibmvnic_send_crq(adapter, &crq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004344}
4345
Lijun Pan69980d02020-09-27 20:13:28 -05004346static void send_query_map(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06004347{
4348 union ibmvnic_crq crq;
4349
4350 memset(&crq, 0, sizeof(crq));
4351 crq.query_map.first = IBMVNIC_CRQ_CMD;
4352 crq.query_map.cmd = QUERY_MAP;
4353 ibmvnic_send_crq(adapter, &crq);
4354}
4355
4356/* Send a series of CRQs requesting various capabilities of the VNIC server */
Lijun Pan491099a2020-09-27 20:13:26 -05004357static void send_query_cap(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06004358{
4359 union ibmvnic_crq crq;
4360
Thomas Falcon901e0402017-02-15 12:17:59 -06004361 atomic_set(&adapter->running_cap_crqs, 0);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004362 memset(&crq, 0, sizeof(crq));
4363 crq.query_capability.first = IBMVNIC_CRQ_CMD;
4364 crq.query_capability.cmd = QUERY_CAPABILITY;
4365
4366 crq.query_capability.capability = cpu_to_be16(MIN_TX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06004367 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004368 ibmvnic_send_crq(adapter, &crq);
4369
4370 crq.query_capability.capability = cpu_to_be16(MIN_RX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06004371 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004372 ibmvnic_send_crq(adapter, &crq);
4373
4374 crq.query_capability.capability = cpu_to_be16(MIN_RX_ADD_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06004375 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004376 ibmvnic_send_crq(adapter, &crq);
4377
4378 crq.query_capability.capability = cpu_to_be16(MAX_TX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06004379 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004380 ibmvnic_send_crq(adapter, &crq);
4381
4382 crq.query_capability.capability = cpu_to_be16(MAX_RX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06004383 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004384 ibmvnic_send_crq(adapter, &crq);
4385
4386 crq.query_capability.capability = cpu_to_be16(MAX_RX_ADD_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06004387 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004388 ibmvnic_send_crq(adapter, &crq);
4389
4390 crq.query_capability.capability =
4391 cpu_to_be16(MIN_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06004392 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004393 ibmvnic_send_crq(adapter, &crq);
4394
4395 crq.query_capability.capability =
4396 cpu_to_be16(MIN_RX_ADD_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06004397 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004398 ibmvnic_send_crq(adapter, &crq);
4399
4400 crq.query_capability.capability =
4401 cpu_to_be16(MAX_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06004402 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004403 ibmvnic_send_crq(adapter, &crq);
4404
4405 crq.query_capability.capability =
4406 cpu_to_be16(MAX_RX_ADD_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06004407 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004408 ibmvnic_send_crq(adapter, &crq);
4409
4410 crq.query_capability.capability = cpu_to_be16(TCP_IP_OFFLOAD);
Thomas Falcon901e0402017-02-15 12:17:59 -06004411 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004412 ibmvnic_send_crq(adapter, &crq);
4413
4414 crq.query_capability.capability = cpu_to_be16(PROMISC_SUPPORTED);
Thomas Falcon901e0402017-02-15 12:17:59 -06004415 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004416 ibmvnic_send_crq(adapter, &crq);
4417
4418 crq.query_capability.capability = cpu_to_be16(MIN_MTU);
Thomas Falcon901e0402017-02-15 12:17:59 -06004419 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004420 ibmvnic_send_crq(adapter, &crq);
4421
4422 crq.query_capability.capability = cpu_to_be16(MAX_MTU);
Thomas Falcon901e0402017-02-15 12:17:59 -06004423 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004424 ibmvnic_send_crq(adapter, &crq);
4425
4426 crq.query_capability.capability = cpu_to_be16(MAX_MULTICAST_FILTERS);
Thomas Falcon901e0402017-02-15 12:17:59 -06004427 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004428 ibmvnic_send_crq(adapter, &crq);
4429
4430 crq.query_capability.capability = cpu_to_be16(VLAN_HEADER_INSERTION);
Thomas Falcon901e0402017-02-15 12:17:59 -06004431 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004432 ibmvnic_send_crq(adapter, &crq);
4433
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04004434 crq.query_capability.capability = cpu_to_be16(RX_VLAN_HEADER_INSERTION);
4435 atomic_inc(&adapter->running_cap_crqs);
4436 ibmvnic_send_crq(adapter, &crq);
4437
Thomas Falcon032c5e82015-12-21 11:26:06 -06004438 crq.query_capability.capability = cpu_to_be16(MAX_TX_SG_ENTRIES);
Thomas Falcon901e0402017-02-15 12:17:59 -06004439 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004440 ibmvnic_send_crq(adapter, &crq);
4441
4442 crq.query_capability.capability = cpu_to_be16(RX_SG_SUPPORTED);
Thomas Falcon901e0402017-02-15 12:17:59 -06004443 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004444 ibmvnic_send_crq(adapter, &crq);
4445
4446 crq.query_capability.capability = cpu_to_be16(OPT_TX_COMP_SUB_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06004447 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004448 ibmvnic_send_crq(adapter, &crq);
4449
4450 crq.query_capability.capability = cpu_to_be16(OPT_RX_COMP_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06004451 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004452 ibmvnic_send_crq(adapter, &crq);
4453
4454 crq.query_capability.capability =
4455 cpu_to_be16(OPT_RX_BUFADD_Q_PER_RX_COMP_Q);
Thomas Falcon901e0402017-02-15 12:17:59 -06004456 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004457 ibmvnic_send_crq(adapter, &crq);
4458
4459 crq.query_capability.capability =
4460 cpu_to_be16(OPT_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06004461 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004462 ibmvnic_send_crq(adapter, &crq);
4463
4464 crq.query_capability.capability =
4465 cpu_to_be16(OPT_RXBA_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06004466 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004467 ibmvnic_send_crq(adapter, &crq);
4468
4469 crq.query_capability.capability = cpu_to_be16(TX_RX_DESC_REQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06004470 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004471 ibmvnic_send_crq(adapter, &crq);
4472}
4473
Lijun Pan16e811f2020-09-27 20:13:29 -05004474static void send_query_ip_offload(struct ibmvnic_adapter *adapter)
4475{
4476 int buf_sz = sizeof(struct ibmvnic_query_ip_offload_buffer);
4477 struct device *dev = &adapter->vdev->dev;
4478 union ibmvnic_crq crq;
4479
4480 adapter->ip_offload_tok =
4481 dma_map_single(dev,
4482 &adapter->ip_offload_buf,
4483 buf_sz,
4484 DMA_FROM_DEVICE);
4485
4486 if (dma_mapping_error(dev, adapter->ip_offload_tok)) {
4487 if (!firmware_has_feature(FW_FEATURE_CMO))
4488 dev_err(dev, "Couldn't map offload buffer\n");
4489 return;
4490 }
4491
4492 memset(&crq, 0, sizeof(crq));
4493 crq.query_ip_offload.first = IBMVNIC_CRQ_CMD;
4494 crq.query_ip_offload.cmd = QUERY_IP_OFFLOAD;
4495 crq.query_ip_offload.len = cpu_to_be32(buf_sz);
4496 crq.query_ip_offload.ioba =
4497 cpu_to_be32(adapter->ip_offload_tok);
4498
4499 ibmvnic_send_crq(adapter, &crq);
4500}
4501
Lijun Pan46899bd2020-09-27 20:13:30 -05004502static void send_control_ip_offload(struct ibmvnic_adapter *adapter)
4503{
4504 struct ibmvnic_control_ip_offload_buffer *ctrl_buf = &adapter->ip_offload_ctrl;
4505 struct ibmvnic_query_ip_offload_buffer *buf = &adapter->ip_offload_buf;
4506 struct device *dev = &adapter->vdev->dev;
4507 netdev_features_t old_hw_features = 0;
4508 union ibmvnic_crq crq;
4509
4510 adapter->ip_offload_ctrl_tok =
4511 dma_map_single(dev,
4512 ctrl_buf,
4513 sizeof(adapter->ip_offload_ctrl),
4514 DMA_TO_DEVICE);
4515
4516 if (dma_mapping_error(dev, adapter->ip_offload_ctrl_tok)) {
4517 dev_err(dev, "Couldn't map ip offload control buffer\n");
4518 return;
4519 }
4520
4521 ctrl_buf->len = cpu_to_be32(sizeof(adapter->ip_offload_ctrl));
4522 ctrl_buf->version = cpu_to_be32(INITIAL_VERSION_IOB);
4523 ctrl_buf->ipv4_chksum = buf->ipv4_chksum;
4524 ctrl_buf->ipv6_chksum = buf->ipv6_chksum;
4525 ctrl_buf->tcp_ipv4_chksum = buf->tcp_ipv4_chksum;
4526 ctrl_buf->udp_ipv4_chksum = buf->udp_ipv4_chksum;
4527 ctrl_buf->tcp_ipv6_chksum = buf->tcp_ipv6_chksum;
4528 ctrl_buf->udp_ipv6_chksum = buf->udp_ipv6_chksum;
4529 ctrl_buf->large_tx_ipv4 = buf->large_tx_ipv4;
4530 ctrl_buf->large_tx_ipv6 = buf->large_tx_ipv6;
4531
4532 /* large_rx disabled for now, additional features needed */
4533 ctrl_buf->large_rx_ipv4 = 0;
4534 ctrl_buf->large_rx_ipv6 = 0;
4535
4536 if (adapter->state != VNIC_PROBING) {
4537 old_hw_features = adapter->netdev->hw_features;
4538 adapter->netdev->hw_features = 0;
4539 }
4540
4541 adapter->netdev->hw_features = NETIF_F_SG | NETIF_F_GSO | NETIF_F_GRO;
4542
4543 if (buf->tcp_ipv4_chksum || buf->udp_ipv4_chksum)
4544 adapter->netdev->hw_features |= NETIF_F_IP_CSUM;
4545
4546 if (buf->tcp_ipv6_chksum || buf->udp_ipv6_chksum)
4547 adapter->netdev->hw_features |= NETIF_F_IPV6_CSUM;
4548
4549 if ((adapter->netdev->features &
4550 (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)))
4551 adapter->netdev->hw_features |= NETIF_F_RXCSUM;
4552
4553 if (buf->large_tx_ipv4)
4554 adapter->netdev->hw_features |= NETIF_F_TSO;
4555 if (buf->large_tx_ipv6)
4556 adapter->netdev->hw_features |= NETIF_F_TSO6;
4557
4558 if (adapter->state == VNIC_PROBING) {
4559 adapter->netdev->features |= adapter->netdev->hw_features;
4560 } else if (old_hw_features != adapter->netdev->hw_features) {
4561 netdev_features_t tmp = 0;
4562
4563 /* disable features no longer supported */
4564 adapter->netdev->features &= adapter->netdev->hw_features;
4565 /* turn on features now supported if previously enabled */
4566 tmp = (old_hw_features ^ adapter->netdev->hw_features) &
4567 adapter->netdev->hw_features;
4568 adapter->netdev->features |=
4569 tmp & adapter->netdev->wanted_features;
4570 }
4571
4572 memset(&crq, 0, sizeof(crq));
4573 crq.control_ip_offload.first = IBMVNIC_CRQ_CMD;
4574 crq.control_ip_offload.cmd = CONTROL_IP_OFFLOAD;
4575 crq.control_ip_offload.len =
4576 cpu_to_be32(sizeof(adapter->ip_offload_ctrl));
4577 crq.control_ip_offload.ioba = cpu_to_be32(adapter->ip_offload_ctrl_tok);
4578 ibmvnic_send_crq(adapter, &crq);
4579}
4580
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02004581static void handle_vpd_size_rsp(union ibmvnic_crq *crq,
4582 struct ibmvnic_adapter *adapter)
4583{
4584 struct device *dev = &adapter->vdev->dev;
4585
4586 if (crq->get_vpd_size_rsp.rc.code) {
4587 dev_err(dev, "Error retrieving VPD size, rc=%x\n",
4588 crq->get_vpd_size_rsp.rc.code);
4589 complete(&adapter->fw_done);
4590 return;
4591 }
4592
4593 adapter->vpd->len = be64_to_cpu(crq->get_vpd_size_rsp.len);
4594 complete(&adapter->fw_done);
4595}
4596
4597static void handle_vpd_rsp(union ibmvnic_crq *crq,
4598 struct ibmvnic_adapter *adapter)
4599{
4600 struct device *dev = &adapter->vdev->dev;
Desnes Augusto Nunes do Rosario21a25452018-02-05 14:33:55 -02004601 unsigned char *substr = NULL;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02004602 u8 fw_level_len = 0;
4603
4604 memset(adapter->fw_version, 0, 32);
4605
4606 dma_unmap_single(dev, adapter->vpd->dma_addr, adapter->vpd->len,
4607 DMA_FROM_DEVICE);
4608
4609 if (crq->get_vpd_rsp.rc.code) {
4610 dev_err(dev, "Error retrieving VPD from device, rc=%x\n",
4611 crq->get_vpd_rsp.rc.code);
4612 goto complete;
4613 }
4614
4615 /* get the position of the firmware version info
4616 * located after the ASCII 'RM' substring in the buffer
4617 */
4618 substr = strnstr(adapter->vpd->buff, "RM", adapter->vpd->len);
4619 if (!substr) {
Desnes Augusto Nunes do Rosarioa1073112018-02-01 16:04:30 -02004620 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 -02004621 goto complete;
4622 }
4623
4624 /* get length of firmware level ASCII substring */
4625 if ((substr + 2) < (adapter->vpd->buff + adapter->vpd->len)) {
4626 fw_level_len = *(substr + 2);
4627 } else {
4628 dev_info(dev, "Length of FW substr extrapolated VDP buff\n");
4629 goto complete;
4630 }
4631
4632 /* copy firmware version string from vpd into adapter */
4633 if ((substr + 3 + fw_level_len) <
4634 (adapter->vpd->buff + adapter->vpd->len)) {
Desnes Augusto Nunes do Rosario21a25452018-02-05 14:33:55 -02004635 strncpy((char *)adapter->fw_version, substr + 3, fw_level_len);
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02004636 } else {
4637 dev_info(dev, "FW substr extrapolated VPD buff\n");
4638 }
4639
4640complete:
Desnes Augusto Nunes do Rosario21a25452018-02-05 14:33:55 -02004641 if (adapter->fw_version[0] == '\0')
Lijun Pan0b217d32021-06-11 13:33:53 -05004642 strscpy((char *)adapter->fw_version, "N/A", sizeof(adapter->fw_version));
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02004643 complete(&adapter->fw_done);
4644}
4645
Thomas Falcon032c5e82015-12-21 11:26:06 -06004646static void handle_query_ip_offload_rsp(struct ibmvnic_adapter *adapter)
4647{
4648 struct device *dev = &adapter->vdev->dev;
4649 struct ibmvnic_query_ip_offload_buffer *buf = &adapter->ip_offload_buf;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004650 int i;
4651
4652 dma_unmap_single(dev, adapter->ip_offload_tok,
4653 sizeof(adapter->ip_offload_buf), DMA_FROM_DEVICE);
4654
4655 netdev_dbg(adapter->netdev, "Query IP Offload Buffer:\n");
4656 for (i = 0; i < (sizeof(adapter->ip_offload_buf) - 1) / 8 + 1; i++)
4657 netdev_dbg(adapter->netdev, "%016lx\n",
Lijun Pan429aa362021-02-11 00:43:18 -06004658 ((unsigned long *)(buf))[i]);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004659
4660 netdev_dbg(adapter->netdev, "ipv4_chksum = %d\n", buf->ipv4_chksum);
4661 netdev_dbg(adapter->netdev, "ipv6_chksum = %d\n", buf->ipv6_chksum);
4662 netdev_dbg(adapter->netdev, "tcp_ipv4_chksum = %d\n",
4663 buf->tcp_ipv4_chksum);
4664 netdev_dbg(adapter->netdev, "tcp_ipv6_chksum = %d\n",
4665 buf->tcp_ipv6_chksum);
4666 netdev_dbg(adapter->netdev, "udp_ipv4_chksum = %d\n",
4667 buf->udp_ipv4_chksum);
4668 netdev_dbg(adapter->netdev, "udp_ipv6_chksum = %d\n",
4669 buf->udp_ipv6_chksum);
4670 netdev_dbg(adapter->netdev, "large_tx_ipv4 = %d\n",
4671 buf->large_tx_ipv4);
4672 netdev_dbg(adapter->netdev, "large_tx_ipv6 = %d\n",
4673 buf->large_tx_ipv6);
4674 netdev_dbg(adapter->netdev, "large_rx_ipv4 = %d\n",
4675 buf->large_rx_ipv4);
4676 netdev_dbg(adapter->netdev, "large_rx_ipv6 = %d\n",
4677 buf->large_rx_ipv6);
4678 netdev_dbg(adapter->netdev, "max_ipv4_hdr_sz = %d\n",
4679 buf->max_ipv4_header_size);
4680 netdev_dbg(adapter->netdev, "max_ipv6_hdr_sz = %d\n",
4681 buf->max_ipv6_header_size);
4682 netdev_dbg(adapter->netdev, "max_tcp_hdr_size = %d\n",
4683 buf->max_tcp_header_size);
4684 netdev_dbg(adapter->netdev, "max_udp_hdr_size = %d\n",
4685 buf->max_udp_header_size);
4686 netdev_dbg(adapter->netdev, "max_large_tx_size = %d\n",
4687 buf->max_large_tx_size);
4688 netdev_dbg(adapter->netdev, "max_large_rx_size = %d\n",
4689 buf->max_large_rx_size);
4690 netdev_dbg(adapter->netdev, "ipv6_ext_hdr = %d\n",
4691 buf->ipv6_extension_header);
4692 netdev_dbg(adapter->netdev, "tcp_pseudosum_req = %d\n",
4693 buf->tcp_pseudosum_req);
4694 netdev_dbg(adapter->netdev, "num_ipv6_ext_hd = %d\n",
4695 buf->num_ipv6_ext_headers);
4696 netdev_dbg(adapter->netdev, "off_ipv6_ext_hd = %d\n",
4697 buf->off_ipv6_ext_headers);
4698
Lijun Pan46899bd2020-09-27 20:13:30 -05004699 send_control_ip_offload(adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004700}
4701
Thomas Falconc9008d32018-08-06 21:39:59 -05004702static const char *ibmvnic_fw_err_cause(u16 cause)
4703{
4704 switch (cause) {
4705 case ADAPTER_PROBLEM:
4706 return "adapter problem";
4707 case BUS_PROBLEM:
4708 return "bus problem";
4709 case FW_PROBLEM:
4710 return "firmware problem";
4711 case DD_PROBLEM:
4712 return "device driver problem";
4713 case EEH_RECOVERY:
4714 return "EEH recovery";
4715 case FW_UPDATED:
4716 return "firmware updated";
4717 case LOW_MEMORY:
4718 return "low Memory";
4719 default:
4720 return "unknown";
4721 }
4722}
4723
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04004724static void handle_error_indication(union ibmvnic_crq *crq,
4725 struct ibmvnic_adapter *adapter)
4726{
4727 struct device *dev = &adapter->vdev->dev;
Thomas Falconc9008d32018-08-06 21:39:59 -05004728 u16 cause;
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04004729
Thomas Falconc9008d32018-08-06 21:39:59 -05004730 cause = be16_to_cpu(crq->error_indication.error_cause);
4731
4732 dev_warn_ratelimited(dev,
4733 "Firmware reports %serror, cause: %s. Starting recovery...\n",
4734 crq->error_indication.flags
4735 & IBMVNIC_FATAL_ERROR ? "FATAL " : "",
4736 ibmvnic_fw_err_cause(cause));
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04004737
Nathan Fontenoted651a12017-05-03 14:04:38 -04004738 if (crq->error_indication.flags & IBMVNIC_FATAL_ERROR)
4739 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
John Allen8cb31cf2017-05-26 10:30:37 -04004740 else
4741 ibmvnic_reset(adapter, VNIC_RESET_NON_FATAL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004742}
4743
Thomas Falconf8136142018-01-29 13:45:05 -06004744static int handle_change_mac_rsp(union ibmvnic_crq *crq,
4745 struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06004746{
4747 struct net_device *netdev = adapter->netdev;
4748 struct device *dev = &adapter->vdev->dev;
4749 long rc;
4750
4751 rc = crq->change_mac_addr_rsp.rc.code;
4752 if (rc) {
4753 dev_err(dev, "Error %ld in CHANGE_MAC_ADDR_RSP\n", rc);
Thomas Falconf8136142018-01-29 13:45:05 -06004754 goto out;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004755 }
Lijun Pand9b0e592020-10-20 17:39:19 -05004756 /* crq->change_mac_addr.mac_addr is the requested one
4757 * crq->change_mac_addr_rsp.mac_addr is the returned valid one.
4758 */
Jakub Kicinskif3956eb2021-10-01 14:32:23 -07004759 eth_hw_addr_set(netdev, &crq->change_mac_addr_rsp.mac_addr[0]);
Lijun Pand9b0e592020-10-20 17:39:19 -05004760 ether_addr_copy(adapter->mac_addr,
4761 &crq->change_mac_addr_rsp.mac_addr[0]);
Thomas Falconf8136142018-01-29 13:45:05 -06004762out:
4763 complete(&adapter->fw_done);
4764 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004765}
4766
4767static void handle_request_cap_rsp(union ibmvnic_crq *crq,
4768 struct ibmvnic_adapter *adapter)
4769{
4770 struct device *dev = &adapter->vdev->dev;
4771 u64 *req_value;
4772 char *name;
4773
Thomas Falcon901e0402017-02-15 12:17:59 -06004774 atomic_dec(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004775 switch (be16_to_cpu(crq->request_capability_rsp.capability)) {
4776 case REQ_TX_QUEUES:
4777 req_value = &adapter->req_tx_queues;
4778 name = "tx";
4779 break;
4780 case REQ_RX_QUEUES:
4781 req_value = &adapter->req_rx_queues;
4782 name = "rx";
4783 break;
4784 case REQ_RX_ADD_QUEUES:
4785 req_value = &adapter->req_rx_add_queues;
4786 name = "rx_add";
4787 break;
4788 case REQ_TX_ENTRIES_PER_SUBCRQ:
4789 req_value = &adapter->req_tx_entries_per_subcrq;
4790 name = "tx_entries_per_subcrq";
4791 break;
4792 case REQ_RX_ADD_ENTRIES_PER_SUBCRQ:
4793 req_value = &adapter->req_rx_add_entries_per_subcrq;
4794 name = "rx_add_entries_per_subcrq";
4795 break;
4796 case REQ_MTU:
4797 req_value = &adapter->req_mtu;
4798 name = "mtu";
4799 break;
4800 case PROMISC_REQUESTED:
4801 req_value = &adapter->promisc;
4802 name = "promisc";
4803 break;
4804 default:
4805 dev_err(dev, "Got invalid cap request rsp %d\n",
4806 crq->request_capability.capability);
4807 return;
4808 }
4809
4810 switch (crq->request_capability_rsp.rc.code) {
4811 case SUCCESS:
4812 break;
4813 case PARTIALSUCCESS:
4814 dev_info(dev, "req=%lld, rsp=%ld in %s queue, retrying.\n",
4815 *req_value,
Lijun Pan914789a2021-02-11 00:43:21 -06004816 (long)be64_to_cpu(crq->request_capability_rsp.number),
4817 name);
John Allene7913802018-01-18 16:27:12 -06004818
4819 if (be16_to_cpu(crq->request_capability_rsp.capability) ==
4820 REQ_MTU) {
4821 pr_err("mtu of %llu is not supported. Reverting.\n",
4822 *req_value);
4823 *req_value = adapter->fallback.mtu;
4824 } else {
4825 *req_value =
4826 be64_to_cpu(crq->request_capability_rsp.number);
4827 }
4828
Lijun Pan09081b92020-09-27 20:13:27 -05004829 send_request_cap(adapter, 1);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004830 return;
4831 default:
4832 dev_err(dev, "Error %d in request cap rsp\n",
4833 crq->request_capability_rsp.rc.code);
4834 return;
4835 }
4836
4837 /* Done receiving requested capabilities, query IP offload support */
Thomas Falcon901e0402017-02-15 12:17:59 -06004838 if (atomic_read(&adapter->running_cap_crqs) == 0) {
Thomas Falcon249168a2017-02-15 12:18:00 -06004839 adapter->wait_capability = false;
Lijun Pan16e811f2020-09-27 20:13:29 -05004840 send_query_ip_offload(adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004841 }
4842}
4843
4844static int handle_login_rsp(union ibmvnic_crq *login_rsp_crq,
4845 struct ibmvnic_adapter *adapter)
4846{
4847 struct device *dev = &adapter->vdev->dev;
John Allenc26eba02017-10-26 16:23:25 -05004848 struct net_device *netdev = adapter->netdev;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004849 struct ibmvnic_login_rsp_buffer *login_rsp = adapter->login_rsp_buf;
4850 struct ibmvnic_login_buffer *login = adapter->login_buf;
Cristobal Fornof3ae59c2020-08-19 13:16:23 -05004851 u64 *tx_handle_array;
4852 u64 *rx_handle_array;
4853 int num_tx_pools;
4854 int num_rx_pools;
Thomas Falcon507ebe62020-08-21 13:39:01 -05004855 u64 *size_array;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004856 int i;
4857
Sukadev Bhattiprolu76cdc5c2020-11-25 18:04:29 -06004858 /* CHECK: Test/set of login_pending does not need to be atomic
4859 * because only ibmvnic_tasklet tests/clears this.
4860 */
4861 if (!adapter->login_pending) {
4862 netdev_warn(netdev, "Ignoring unexpected login response\n");
4863 return 0;
4864 }
4865 adapter->login_pending = false;
4866
Thomas Falcon032c5e82015-12-21 11:26:06 -06004867 dma_unmap_single(dev, adapter->login_buf_token, adapter->login_buf_sz,
Thomas Falcon37e40fa2018-04-06 18:37:02 -05004868 DMA_TO_DEVICE);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004869 dma_unmap_single(dev, adapter->login_rsp_buf_token,
Thomas Falcon37e40fa2018-04-06 18:37:02 -05004870 adapter->login_rsp_buf_sz, DMA_FROM_DEVICE);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004871
John Allen498cd8e2016-04-06 11:49:55 -05004872 /* If the number of queues requested can't be allocated by the
4873 * server, the login response will return with code 1. We will need
4874 * to resend the login buffer with fewer queues requested.
4875 */
4876 if (login_rsp_crq->generic.rc.code) {
Nathan Fontenot64d92aa2018-04-11 10:09:32 -05004877 adapter->init_done_rc = login_rsp_crq->generic.rc.code;
John Allen498cd8e2016-04-06 11:49:55 -05004878 complete(&adapter->init_done);
4879 return 0;
4880 }
4881
Sukadev Bhattiprolud437f5a2021-09-07 22:07:03 -07004882 if (adapter->failover_pending) {
4883 adapter->init_done_rc = -EAGAIN;
4884 netdev_dbg(netdev, "Failover pending, ignoring login response\n");
4885 complete(&adapter->init_done);
4886 /* login response buffer will be released on reset */
4887 return 0;
4888 }
4889
John Allenc26eba02017-10-26 16:23:25 -05004890 netdev->mtu = adapter->req_mtu - ETH_HLEN;
4891
Thomas Falcon032c5e82015-12-21 11:26:06 -06004892 netdev_dbg(adapter->netdev, "Login Response Buffer:\n");
4893 for (i = 0; i < (adapter->login_rsp_buf_sz - 1) / 8 + 1; i++) {
4894 netdev_dbg(adapter->netdev, "%016lx\n",
Lijun Pan429aa362021-02-11 00:43:18 -06004895 ((unsigned long *)(adapter->login_rsp_buf))[i]);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004896 }
4897
4898 /* Sanity checks */
4899 if (login->num_txcomp_subcrqs != login_rsp->num_txsubm_subcrqs ||
4900 (be32_to_cpu(login->num_rxcomp_subcrqs) *
4901 adapter->req_rx_add_queues !=
4902 be32_to_cpu(login_rsp->num_rxadd_subcrqs))) {
4903 dev_err(dev, "FATAL: Inconsistent login and login rsp\n");
Dany Madden31d6b402020-11-25 18:04:24 -06004904 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004905 return -EIO;
4906 }
Thomas Falcon507ebe62020-08-21 13:39:01 -05004907 size_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
4908 be32_to_cpu(adapter->login_rsp_buf->off_rxadd_buff_size));
4909 /* variable buffer sizes are not supported, so just read the
4910 * first entry.
4911 */
4912 adapter->cur_rx_buf_sz = be64_to_cpu(size_array[0]);
Cristobal Fornof3ae59c2020-08-19 13:16:23 -05004913
4914 num_tx_pools = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
4915 num_rx_pools = be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
4916
4917 tx_handle_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
4918 be32_to_cpu(adapter->login_rsp_buf->off_txsubm_subcrqs));
4919 rx_handle_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
4920 be32_to_cpu(adapter->login_rsp_buf->off_rxadd_subcrqs));
4921
4922 for (i = 0; i < num_tx_pools; i++)
4923 adapter->tx_scrq[i]->handle = tx_handle_array[i];
4924
4925 for (i = 0; i < num_rx_pools; i++)
4926 adapter->rx_scrq[i]->handle = rx_handle_array[i];
4927
Thomas Falcon507ebe62020-08-21 13:39:01 -05004928 adapter->num_active_tx_scrqs = num_tx_pools;
4929 adapter->num_active_rx_scrqs = num_rx_pools;
Cristobal Fornof3ae59c2020-08-19 13:16:23 -05004930 release_login_rsp_buffer(adapter);
Thomas Falcona2c0f032018-02-21 18:18:30 -06004931 release_login_buffer(adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004932 complete(&adapter->init_done);
4933
Thomas Falcon032c5e82015-12-21 11:26:06 -06004934 return 0;
4935}
4936
Thomas Falcon032c5e82015-12-21 11:26:06 -06004937static void handle_request_unmap_rsp(union ibmvnic_crq *crq,
4938 struct ibmvnic_adapter *adapter)
4939{
4940 struct device *dev = &adapter->vdev->dev;
4941 long rc;
4942
4943 rc = crq->request_unmap_rsp.rc.code;
4944 if (rc)
4945 dev_err(dev, "Error %ld in REQUEST_UNMAP_RSP\n", rc);
4946}
4947
4948static void handle_query_map_rsp(union ibmvnic_crq *crq,
4949 struct ibmvnic_adapter *adapter)
4950{
4951 struct net_device *netdev = adapter->netdev;
4952 struct device *dev = &adapter->vdev->dev;
4953 long rc;
4954
4955 rc = crq->query_map_rsp.rc.code;
4956 if (rc) {
4957 dev_err(dev, "Error %ld in QUERY_MAP_RSP\n", rc);
4958 return;
4959 }
Sukadev Bhattiprolu0f2bf312021-09-14 20:52:52 -07004960 netdev_dbg(netdev, "page_size = %d\ntot_pages = %u\nfree_pages = %u\n",
4961 crq->query_map_rsp.page_size,
4962 __be32_to_cpu(crq->query_map_rsp.tot_pages),
4963 __be32_to_cpu(crq->query_map_rsp.free_pages));
Thomas Falcon032c5e82015-12-21 11:26:06 -06004964}
4965
4966static void handle_query_cap_rsp(union ibmvnic_crq *crq,
4967 struct ibmvnic_adapter *adapter)
4968{
4969 struct net_device *netdev = adapter->netdev;
4970 struct device *dev = &adapter->vdev->dev;
4971 long rc;
4972
Thomas Falcon901e0402017-02-15 12:17:59 -06004973 atomic_dec(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004974 netdev_dbg(netdev, "Outstanding queries: %d\n",
Thomas Falcon901e0402017-02-15 12:17:59 -06004975 atomic_read(&adapter->running_cap_crqs));
Thomas Falcon032c5e82015-12-21 11:26:06 -06004976 rc = crq->query_capability.rc.code;
4977 if (rc) {
4978 dev_err(dev, "Error %ld in QUERY_CAP_RSP\n", rc);
4979 goto out;
4980 }
4981
4982 switch (be16_to_cpu(crq->query_capability.capability)) {
4983 case MIN_TX_QUEUES:
4984 adapter->min_tx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004985 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004986 netdev_dbg(netdev, "min_tx_queues = %lld\n",
4987 adapter->min_tx_queues);
4988 break;
4989 case MIN_RX_QUEUES:
4990 adapter->min_rx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004991 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004992 netdev_dbg(netdev, "min_rx_queues = %lld\n",
4993 adapter->min_rx_queues);
4994 break;
4995 case MIN_RX_ADD_QUEUES:
4996 adapter->min_rx_add_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004997 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004998 netdev_dbg(netdev, "min_rx_add_queues = %lld\n",
4999 adapter->min_rx_add_queues);
5000 break;
5001 case MAX_TX_QUEUES:
5002 adapter->max_tx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06005003 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005004 netdev_dbg(netdev, "max_tx_queues = %lld\n",
5005 adapter->max_tx_queues);
5006 break;
5007 case MAX_RX_QUEUES:
5008 adapter->max_rx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06005009 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005010 netdev_dbg(netdev, "max_rx_queues = %lld\n",
5011 adapter->max_rx_queues);
5012 break;
5013 case MAX_RX_ADD_QUEUES:
5014 adapter->max_rx_add_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06005015 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005016 netdev_dbg(netdev, "max_rx_add_queues = %lld\n",
5017 adapter->max_rx_add_queues);
5018 break;
5019 case MIN_TX_ENTRIES_PER_SUBCRQ:
5020 adapter->min_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06005021 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005022 netdev_dbg(netdev, "min_tx_entries_per_subcrq = %lld\n",
5023 adapter->min_tx_entries_per_subcrq);
5024 break;
5025 case MIN_RX_ADD_ENTRIES_PER_SUBCRQ:
5026 adapter->min_rx_add_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06005027 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005028 netdev_dbg(netdev, "min_rx_add_entrs_per_subcrq = %lld\n",
5029 adapter->min_rx_add_entries_per_subcrq);
5030 break;
5031 case MAX_TX_ENTRIES_PER_SUBCRQ:
5032 adapter->max_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06005033 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005034 netdev_dbg(netdev, "max_tx_entries_per_subcrq = %lld\n",
5035 adapter->max_tx_entries_per_subcrq);
5036 break;
5037 case MAX_RX_ADD_ENTRIES_PER_SUBCRQ:
5038 adapter->max_rx_add_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06005039 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005040 netdev_dbg(netdev, "max_rx_add_entrs_per_subcrq = %lld\n",
5041 adapter->max_rx_add_entries_per_subcrq);
5042 break;
5043 case TCP_IP_OFFLOAD:
5044 adapter->tcp_ip_offload =
Thomas Falconde89e852016-03-01 10:20:09 -06005045 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005046 netdev_dbg(netdev, "tcp_ip_offload = %lld\n",
5047 adapter->tcp_ip_offload);
5048 break;
5049 case PROMISC_SUPPORTED:
5050 adapter->promisc_supported =
Thomas Falconde89e852016-03-01 10:20:09 -06005051 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005052 netdev_dbg(netdev, "promisc_supported = %lld\n",
5053 adapter->promisc_supported);
5054 break;
5055 case MIN_MTU:
Thomas Falconde89e852016-03-01 10:20:09 -06005056 adapter->min_mtu = be64_to_cpu(crq->query_capability.number);
Thomas Falconf39f0d12017-02-14 10:22:59 -06005057 netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005058 netdev_dbg(netdev, "min_mtu = %lld\n", adapter->min_mtu);
5059 break;
5060 case MAX_MTU:
Thomas Falconde89e852016-03-01 10:20:09 -06005061 adapter->max_mtu = be64_to_cpu(crq->query_capability.number);
Thomas Falconf39f0d12017-02-14 10:22:59 -06005062 netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005063 netdev_dbg(netdev, "max_mtu = %lld\n", adapter->max_mtu);
5064 break;
5065 case MAX_MULTICAST_FILTERS:
5066 adapter->max_multicast_filters =
Thomas Falconde89e852016-03-01 10:20:09 -06005067 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005068 netdev_dbg(netdev, "max_multicast_filters = %lld\n",
5069 adapter->max_multicast_filters);
5070 break;
5071 case VLAN_HEADER_INSERTION:
5072 adapter->vlan_header_insertion =
Thomas Falconde89e852016-03-01 10:20:09 -06005073 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005074 if (adapter->vlan_header_insertion)
5075 netdev->features |= NETIF_F_HW_VLAN_STAG_TX;
5076 netdev_dbg(netdev, "vlan_header_insertion = %lld\n",
5077 adapter->vlan_header_insertion);
5078 break;
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04005079 case RX_VLAN_HEADER_INSERTION:
5080 adapter->rx_vlan_header_insertion =
5081 be64_to_cpu(crq->query_capability.number);
5082 netdev_dbg(netdev, "rx_vlan_header_insertion = %lld\n",
5083 adapter->rx_vlan_header_insertion);
5084 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005085 case MAX_TX_SG_ENTRIES:
5086 adapter->max_tx_sg_entries =
Thomas Falconde89e852016-03-01 10:20:09 -06005087 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005088 netdev_dbg(netdev, "max_tx_sg_entries = %lld\n",
5089 adapter->max_tx_sg_entries);
5090 break;
5091 case RX_SG_SUPPORTED:
5092 adapter->rx_sg_supported =
Thomas Falconde89e852016-03-01 10:20:09 -06005093 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005094 netdev_dbg(netdev, "rx_sg_supported = %lld\n",
5095 adapter->rx_sg_supported);
5096 break;
5097 case OPT_TX_COMP_SUB_QUEUES:
5098 adapter->opt_tx_comp_sub_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06005099 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005100 netdev_dbg(netdev, "opt_tx_comp_sub_queues = %lld\n",
5101 adapter->opt_tx_comp_sub_queues);
5102 break;
5103 case OPT_RX_COMP_QUEUES:
5104 adapter->opt_rx_comp_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06005105 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005106 netdev_dbg(netdev, "opt_rx_comp_queues = %lld\n",
5107 adapter->opt_rx_comp_queues);
5108 break;
5109 case OPT_RX_BUFADD_Q_PER_RX_COMP_Q:
5110 adapter->opt_rx_bufadd_q_per_rx_comp_q =
Thomas Falconde89e852016-03-01 10:20:09 -06005111 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005112 netdev_dbg(netdev, "opt_rx_bufadd_q_per_rx_comp_q = %lld\n",
5113 adapter->opt_rx_bufadd_q_per_rx_comp_q);
5114 break;
5115 case OPT_TX_ENTRIES_PER_SUBCRQ:
5116 adapter->opt_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06005117 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005118 netdev_dbg(netdev, "opt_tx_entries_per_subcrq = %lld\n",
5119 adapter->opt_tx_entries_per_subcrq);
5120 break;
5121 case OPT_RXBA_ENTRIES_PER_SUBCRQ:
5122 adapter->opt_rxba_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06005123 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005124 netdev_dbg(netdev, "opt_rxba_entries_per_subcrq = %lld\n",
5125 adapter->opt_rxba_entries_per_subcrq);
5126 break;
5127 case TX_RX_DESC_REQ:
5128 adapter->tx_rx_desc_req = crq->query_capability.number;
5129 netdev_dbg(netdev, "tx_rx_desc_req = %llx\n",
5130 adapter->tx_rx_desc_req);
5131 break;
5132
5133 default:
5134 netdev_err(netdev, "Got invalid cap rsp %d\n",
5135 crq->query_capability.capability);
5136 }
5137
5138out:
Thomas Falcon249168a2017-02-15 12:18:00 -06005139 if (atomic_read(&adapter->running_cap_crqs) == 0) {
5140 adapter->wait_capability = false;
Lijun Pan09081b92020-09-27 20:13:27 -05005141 send_request_cap(adapter, 0);
Thomas Falcon249168a2017-02-15 12:18:00 -06005142 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06005143}
5144
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03005145static int send_query_phys_parms(struct ibmvnic_adapter *adapter)
5146{
5147 union ibmvnic_crq crq;
5148 int rc;
5149
5150 memset(&crq, 0, sizeof(crq));
5151 crq.query_phys_parms.first = IBMVNIC_CRQ_CMD;
5152 crq.query_phys_parms.cmd = QUERY_PHYS_PARMS;
Thomas Falconff25dcb2019-11-25 17:12:56 -06005153
5154 mutex_lock(&adapter->fw_lock);
5155 adapter->fw_done_rc = 0;
Thomas Falcon070eca92019-11-25 17:12:53 -06005156 reinit_completion(&adapter->fw_done);
Thomas Falconff25dcb2019-11-25 17:12:56 -06005157
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03005158 rc = ibmvnic_send_crq(adapter, &crq);
Thomas Falconff25dcb2019-11-25 17:12:56 -06005159 if (rc) {
5160 mutex_unlock(&adapter->fw_lock);
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03005161 return rc;
Thomas Falconff25dcb2019-11-25 17:12:56 -06005162 }
Thomas Falcon476d96c2019-11-25 17:12:55 -06005163
5164 rc = ibmvnic_wait_for_completion(adapter, &adapter->fw_done, 10000);
Thomas Falconff25dcb2019-11-25 17:12:56 -06005165 if (rc) {
5166 mutex_unlock(&adapter->fw_lock);
Thomas Falcon476d96c2019-11-25 17:12:55 -06005167 return rc;
Thomas Falconff25dcb2019-11-25 17:12:56 -06005168 }
Thomas Falcon476d96c2019-11-25 17:12:55 -06005169
Thomas Falconff25dcb2019-11-25 17:12:56 -06005170 mutex_unlock(&adapter->fw_lock);
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03005171 return adapter->fw_done_rc ? -EIO : 0;
5172}
5173
5174static int handle_query_phys_parms_rsp(union ibmvnic_crq *crq,
5175 struct ibmvnic_adapter *adapter)
5176{
5177 struct net_device *netdev = adapter->netdev;
5178 int rc;
Murilo Fossa Vicentinidd0f9d82019-09-16 11:50:37 -03005179 __be32 rspeed = cpu_to_be32(crq->query_phys_parms_rsp.speed);
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03005180
5181 rc = crq->query_phys_parms_rsp.rc.code;
5182 if (rc) {
5183 netdev_err(netdev, "Error %d in QUERY_PHYS_PARMS\n", rc);
5184 return rc;
5185 }
Murilo Fossa Vicentinidd0f9d82019-09-16 11:50:37 -03005186 switch (rspeed) {
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03005187 case IBMVNIC_10MBPS:
5188 adapter->speed = SPEED_10;
5189 break;
5190 case IBMVNIC_100MBPS:
5191 adapter->speed = SPEED_100;
5192 break;
5193 case IBMVNIC_1GBPS:
5194 adapter->speed = SPEED_1000;
5195 break;
Lijun Panb9cd7952020-09-27 19:06:25 -05005196 case IBMVNIC_10GBPS:
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03005197 adapter->speed = SPEED_10000;
5198 break;
5199 case IBMVNIC_25GBPS:
5200 adapter->speed = SPEED_25000;
5201 break;
5202 case IBMVNIC_40GBPS:
5203 adapter->speed = SPEED_40000;
5204 break;
5205 case IBMVNIC_50GBPS:
5206 adapter->speed = SPEED_50000;
5207 break;
5208 case IBMVNIC_100GBPS:
5209 adapter->speed = SPEED_100000;
5210 break;
Lijun Panb9cd7952020-09-27 19:06:25 -05005211 case IBMVNIC_200GBPS:
5212 adapter->speed = SPEED_200000;
5213 break;
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03005214 default:
Murilo Fossa Vicentinidd0f9d82019-09-16 11:50:37 -03005215 if (netif_carrier_ok(netdev))
5216 netdev_warn(netdev, "Unknown speed 0x%08x\n", rspeed);
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03005217 adapter->speed = SPEED_UNKNOWN;
5218 }
5219 if (crq->query_phys_parms_rsp.flags1 & IBMVNIC_FULL_DUPLEX)
5220 adapter->duplex = DUPLEX_FULL;
5221 else if (crq->query_phys_parms_rsp.flags1 & IBMVNIC_HALF_DUPLEX)
5222 adapter->duplex = DUPLEX_HALF;
5223 else
5224 adapter->duplex = DUPLEX_UNKNOWN;
5225
5226 return rc;
5227}
5228
Thomas Falcon032c5e82015-12-21 11:26:06 -06005229static void ibmvnic_handle_crq(union ibmvnic_crq *crq,
5230 struct ibmvnic_adapter *adapter)
5231{
5232 struct ibmvnic_generic_crq *gen_crq = &crq->generic;
5233 struct net_device *netdev = adapter->netdev;
5234 struct device *dev = &adapter->vdev->dev;
Murilo Fossa Vicentini993a82b2017-04-19 13:44:35 -04005235 u64 *u64_crq = (u64 *)crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005236 long rc;
5237
5238 netdev_dbg(netdev, "Handling CRQ: %016lx %016lx\n",
Lijun Pan429aa362021-02-11 00:43:18 -06005239 (unsigned long)cpu_to_be64(u64_crq[0]),
5240 (unsigned long)cpu_to_be64(u64_crq[1]));
Thomas Falcon032c5e82015-12-21 11:26:06 -06005241 switch (gen_crq->first) {
5242 case IBMVNIC_CRQ_INIT_RSP:
5243 switch (gen_crq->cmd) {
5244 case IBMVNIC_CRQ_INIT:
5245 dev_info(dev, "Partner initialized\n");
John Allen017892c12017-05-26 10:30:19 -04005246 adapter->from_passive_init = true;
Sukadev Bhattiprolu76cdc5c2020-11-25 18:04:29 -06005247 /* Discard any stale login responses from prev reset.
5248 * CHECK: should we clear even on INIT_COMPLETE?
5249 */
5250 adapter->login_pending = false;
5251
Cristobal Forno53f8b1b2021-06-10 11:08:35 -06005252 if (adapter->state == VNIC_DOWN)
5253 rc = ibmvnic_reset(adapter, VNIC_RESET_PASSIVE_INIT);
5254 else
5255 rc = ibmvnic_reset(adapter, VNIC_RESET_FAILOVER);
5256
Sukadev Bhattiproluef66a1e2021-02-02 21:08:02 -08005257 if (rc && rc != -EBUSY) {
5258 /* We were unable to schedule the failover
5259 * reset either because the adapter was still
5260 * probing (eg: during kexec) or we could not
5261 * allocate memory. Clear the failover_pending
5262 * flag since no one else will. We ignore
5263 * EBUSY because it means either FAILOVER reset
5264 * is already scheduled or the adapter is
5265 * being removed.
5266 */
5267 netdev_err(netdev,
5268 "Error %ld scheduling failover reset\n",
5269 rc);
5270 adapter->failover_pending = false;
5271 }
Sukadev Bhattiprolu6b278c02021-10-29 15:03:16 -07005272
5273 if (!completion_done(&adapter->init_done)) {
5274 complete(&adapter->init_done);
5275 if (!adapter->init_done_rc)
5276 adapter->init_done_rc = -EAGAIN;
5277 }
5278
Thomas Falcon032c5e82015-12-21 11:26:06 -06005279 break;
5280 case IBMVNIC_CRQ_INIT_COMPLETE:
5281 dev_info(dev, "Partner initialization complete\n");
Thomas Falcon51536982018-05-23 13:37:56 -05005282 adapter->crq.active = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005283 send_version_xchg(adapter);
5284 break;
5285 default:
5286 dev_err(dev, "Unknown crq cmd: %d\n", gen_crq->cmd);
5287 }
5288 return;
5289 case IBMVNIC_CRQ_XPORT_EVENT:
Nathan Fontenoted651a12017-05-03 14:04:38 -04005290 netif_carrier_off(netdev);
Thomas Falcon51536982018-05-23 13:37:56 -05005291 adapter->crq.active = false;
Thomas Falcon2147e3d2019-11-25 17:12:54 -06005292 /* terminate any thread waiting for a response
5293 * from the device
5294 */
5295 if (!completion_done(&adapter->fw_done)) {
5296 adapter->fw_done_rc = -EIO;
5297 complete(&adapter->fw_done);
5298 }
5299 if (!completion_done(&adapter->stats_done))
5300 complete(&adapter->stats_done);
Juliet Kim7ed5b312019-09-20 16:11:23 -04005301 if (test_bit(0, &adapter->resetting))
Thomas Falcon2770a792018-05-23 13:38:02 -05005302 adapter->force_reset_recovery = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005303 if (gen_crq->cmd == IBMVNIC_PARTITION_MIGRATED) {
Nathan Fontenoted651a12017-05-03 14:04:38 -04005304 dev_info(dev, "Migrated, re-enabling adapter\n");
5305 ibmvnic_reset(adapter, VNIC_RESET_MOBILITY);
Thomas Falcondfad09a2016-08-18 11:37:51 -05005306 } else if (gen_crq->cmd == IBMVNIC_DEVICE_FAILOVER) {
5307 dev_info(dev, "Backing device failover detected\n");
Thomas Falcon5a18e1e2018-04-06 18:37:05 -05005308 adapter->failover_pending = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005309 } else {
5310 /* The adapter lost the connection */
5311 dev_err(dev, "Virtual Adapter failed (rc=%d)\n",
5312 gen_crq->cmd);
Nathan Fontenoted651a12017-05-03 14:04:38 -04005313 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005314 }
5315 return;
5316 case IBMVNIC_CRQ_CMD_RSP:
5317 break;
5318 default:
5319 dev_err(dev, "Got an invalid msg type 0x%02x\n",
5320 gen_crq->first);
5321 return;
5322 }
5323
5324 switch (gen_crq->cmd) {
5325 case VERSION_EXCHANGE_RSP:
5326 rc = crq->version_exchange_rsp.rc.code;
5327 if (rc) {
5328 dev_err(dev, "Error %ld in VERSION_EXCHG_RSP\n", rc);
5329 break;
5330 }
Thomas Falcon78468892020-05-28 11:19:17 -05005331 ibmvnic_version =
Thomas Falcon032c5e82015-12-21 11:26:06 -06005332 be16_to_cpu(crq->version_exchange_rsp.version);
Thomas Falcon78468892020-05-28 11:19:17 -05005333 dev_info(dev, "Partner protocol version is %d\n",
5334 ibmvnic_version);
Lijun Pan491099a2020-09-27 20:13:26 -05005335 send_query_cap(adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005336 break;
5337 case QUERY_CAPABILITY_RSP:
5338 handle_query_cap_rsp(crq, adapter);
5339 break;
5340 case QUERY_MAP_RSP:
5341 handle_query_map_rsp(crq, adapter);
5342 break;
5343 case REQUEST_MAP_RSP:
Thomas Falconf3be0cb2017-06-21 14:53:01 -05005344 adapter->fw_done_rc = crq->request_map_rsp.rc.code;
5345 complete(&adapter->fw_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005346 break;
5347 case REQUEST_UNMAP_RSP:
5348 handle_request_unmap_rsp(crq, adapter);
5349 break;
5350 case REQUEST_CAPABILITY_RSP:
5351 handle_request_cap_rsp(crq, adapter);
5352 break;
5353 case LOGIN_RSP:
5354 netdev_dbg(netdev, "Got Login Response\n");
5355 handle_login_rsp(crq, adapter);
5356 break;
5357 case LOGICAL_LINK_STATE_RSP:
Nathan Fontenot53da09e2017-04-21 15:39:04 -04005358 netdev_dbg(netdev,
5359 "Got Logical Link State Response, state: %d rc: %d\n",
5360 crq->logical_link_state_rsp.link_state,
5361 crq->logical_link_state_rsp.rc.code);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005362 adapter->logical_link_state =
5363 crq->logical_link_state_rsp.link_state;
Nathan Fontenot53da09e2017-04-21 15:39:04 -04005364 adapter->init_done_rc = crq->logical_link_state_rsp.rc.code;
5365 complete(&adapter->init_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005366 break;
5367 case LINK_STATE_INDICATION:
5368 netdev_dbg(netdev, "Got Logical Link State Indication\n");
5369 adapter->phys_link_state =
5370 crq->link_state_indication.phys_link_state;
5371 adapter->logical_link_state =
5372 crq->link_state_indication.logical_link_state;
Thomas Falcon0655f992019-05-09 23:13:44 -05005373 if (adapter->phys_link_state && adapter->logical_link_state)
5374 netif_carrier_on(netdev);
5375 else
5376 netif_carrier_off(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005377 break;
5378 case CHANGE_MAC_ADDR_RSP:
5379 netdev_dbg(netdev, "Got MAC address change Response\n");
Thomas Falconf8136142018-01-29 13:45:05 -06005380 adapter->fw_done_rc = handle_change_mac_rsp(crq, adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005381 break;
5382 case ERROR_INDICATION:
5383 netdev_dbg(netdev, "Got Error Indication\n");
5384 handle_error_indication(crq, adapter);
5385 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005386 case REQUEST_STATISTICS_RSP:
5387 netdev_dbg(netdev, "Got Statistics Response\n");
5388 complete(&adapter->stats_done);
5389 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005390 case QUERY_IP_OFFLOAD_RSP:
5391 netdev_dbg(netdev, "Got Query IP offload Response\n");
5392 handle_query_ip_offload_rsp(adapter);
5393 break;
5394 case MULTICAST_CTRL_RSP:
5395 netdev_dbg(netdev, "Got multicast control Response\n");
5396 break;
5397 case CONTROL_IP_OFFLOAD_RSP:
5398 netdev_dbg(netdev, "Got Control IP offload Response\n");
5399 dma_unmap_single(dev, adapter->ip_offload_ctrl_tok,
5400 sizeof(adapter->ip_offload_ctrl),
5401 DMA_TO_DEVICE);
John Allenbd0b6722017-03-17 17:13:40 -05005402 complete(&adapter->init_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005403 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005404 case COLLECT_FW_TRACE_RSP:
5405 netdev_dbg(netdev, "Got Collect firmware trace Response\n");
5406 complete(&adapter->fw_done);
5407 break;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02005408 case GET_VPD_SIZE_RSP:
5409 handle_vpd_size_rsp(crq, adapter);
5410 break;
5411 case GET_VPD_RSP:
5412 handle_vpd_rsp(crq, adapter);
5413 break;
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03005414 case QUERY_PHYS_PARMS_RSP:
5415 adapter->fw_done_rc = handle_query_phys_parms_rsp(crq, adapter);
5416 complete(&adapter->fw_done);
5417 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005418 default:
5419 netdev_err(netdev, "Got an invalid cmd type 0x%02x\n",
5420 gen_crq->cmd);
5421 }
5422}
5423
5424static irqreturn_t ibmvnic_interrupt(int irq, void *instance)
5425{
5426 struct ibmvnic_adapter *adapter = instance;
Thomas Falcon6c267b32017-02-15 12:17:58 -06005427
Thomas Falcon6c267b32017-02-15 12:17:58 -06005428 tasklet_schedule(&adapter->tasklet);
Thomas Falcon6c267b32017-02-15 12:17:58 -06005429 return IRQ_HANDLED;
5430}
5431
Allen Paisaa7c3fe2020-09-14 12:59:29 +05305432static void ibmvnic_tasklet(struct tasklet_struct *t)
Thomas Falcon6c267b32017-02-15 12:17:58 -06005433{
Allen Paisaa7c3fe2020-09-14 12:59:29 +05305434 struct ibmvnic_adapter *adapter = from_tasklet(adapter, t, tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005435 struct ibmvnic_crq_queue *queue = &adapter->crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005436 union ibmvnic_crq *crq;
5437 unsigned long flags;
5438 bool done = false;
5439
5440 spin_lock_irqsave(&queue->lock, flags);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005441 while (!done) {
5442 /* Pull all the valid messages off the CRQ */
5443 while ((crq = ibmvnic_next_crq(adapter)) != NULL) {
Lijun Pane41aec72021-01-27 19:34:42 -06005444 /* This barrier makes sure ibmvnic_next_crq()'s
5445 * crq->generic.first & IBMVNIC_CRQ_CMD_RSP is loaded
5446 * before ibmvnic_handle_crq()'s
5447 * switch(gen_crq->first) and switch(gen_crq->cmd).
5448 */
5449 dma_rmb();
Thomas Falcon032c5e82015-12-21 11:26:06 -06005450 ibmvnic_handle_crq(crq, adapter);
5451 crq->generic.first = 0;
5452 }
Brian Kinged7ecbf2017-04-19 13:44:53 -04005453
5454 /* remain in tasklet until all
5455 * capabilities responses are received
5456 */
5457 if (!adapter->wait_capability)
5458 done = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005459 }
Thomas Falcon249168a2017-02-15 12:18:00 -06005460 /* if capabilities CRQ's were sent in this tasklet, the following
5461 * tasklet must wait until all responses are received
5462 */
5463 if (atomic_read(&adapter->running_cap_crqs) != 0)
5464 adapter->wait_capability = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005465 spin_unlock_irqrestore(&queue->lock, flags);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005466}
5467
5468static int ibmvnic_reenable_crq_queue(struct ibmvnic_adapter *adapter)
5469{
5470 struct vio_dev *vdev = adapter->vdev;
5471 int rc;
5472
5473 do {
5474 rc = plpar_hcall_norets(H_ENABLE_CRQ, vdev->unit_address);
5475 } while (rc == H_IN_PROGRESS || rc == H_BUSY || H_IS_LONG_BUSY(rc));
5476
5477 if (rc)
5478 dev_err(&vdev->dev, "Error enabling adapter (rc=%d)\n", rc);
5479
5480 return rc;
5481}
5482
5483static int ibmvnic_reset_crq(struct ibmvnic_adapter *adapter)
5484{
5485 struct ibmvnic_crq_queue *crq = &adapter->crq;
5486 struct device *dev = &adapter->vdev->dev;
5487 struct vio_dev *vdev = adapter->vdev;
5488 int rc;
5489
5490 /* Close the CRQ */
5491 do {
5492 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
5493 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
5494
5495 /* Clean out the queue */
Lijun Pan0e435be2020-11-23 13:35:46 -06005496 if (!crq->msgs)
5497 return -EINVAL;
5498
Thomas Falcon032c5e82015-12-21 11:26:06 -06005499 memset(crq->msgs, 0, PAGE_SIZE);
5500 crq->cur = 0;
Thomas Falcon51536982018-05-23 13:37:56 -05005501 crq->active = false;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005502
5503 /* And re-open it again */
5504 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
5505 crq->msg_token, PAGE_SIZE);
5506
5507 if (rc == H_CLOSED)
5508 /* Adapter is good, but other end is not ready */
5509 dev_warn(dev, "Partner adapter not ready\n");
5510 else if (rc != 0)
5511 dev_warn(dev, "Couldn't register crq (rc=%d)\n", rc);
5512
5513 return rc;
5514}
5515
Nathan Fontenotf9928872017-03-30 02:48:54 -04005516static void release_crq_queue(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06005517{
5518 struct ibmvnic_crq_queue *crq = &adapter->crq;
5519 struct vio_dev *vdev = adapter->vdev;
5520 long rc;
5521
Nathan Fontenotf9928872017-03-30 02:48:54 -04005522 if (!crq->msgs)
5523 return;
5524
Thomas Falcon032c5e82015-12-21 11:26:06 -06005525 netdev_dbg(adapter->netdev, "Releasing CRQ\n");
5526 free_irq(vdev->irq, adapter);
Thomas Falcon6c267b32017-02-15 12:17:58 -06005527 tasklet_kill(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005528 do {
5529 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
5530 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
5531
5532 dma_unmap_single(&vdev->dev, crq->msg_token, PAGE_SIZE,
5533 DMA_BIDIRECTIONAL);
5534 free_page((unsigned long)crq->msgs);
Nathan Fontenotf9928872017-03-30 02:48:54 -04005535 crq->msgs = NULL;
Thomas Falcon51536982018-05-23 13:37:56 -05005536 crq->active = false;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005537}
5538
Nathan Fontenotf9928872017-03-30 02:48:54 -04005539static int init_crq_queue(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06005540{
5541 struct ibmvnic_crq_queue *crq = &adapter->crq;
5542 struct device *dev = &adapter->vdev->dev;
5543 struct vio_dev *vdev = adapter->vdev;
5544 int rc, retrc = -ENOMEM;
5545
Nathan Fontenotf9928872017-03-30 02:48:54 -04005546 if (crq->msgs)
5547 return 0;
5548
Thomas Falcon032c5e82015-12-21 11:26:06 -06005549 crq->msgs = (union ibmvnic_crq *)get_zeroed_page(GFP_KERNEL);
5550 /* Should we allocate more than one page? */
5551
5552 if (!crq->msgs)
5553 return -ENOMEM;
5554
5555 crq->size = PAGE_SIZE / sizeof(*crq->msgs);
5556 crq->msg_token = dma_map_single(dev, crq->msgs, PAGE_SIZE,
5557 DMA_BIDIRECTIONAL);
5558 if (dma_mapping_error(dev, crq->msg_token))
5559 goto map_failed;
5560
5561 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
5562 crq->msg_token, PAGE_SIZE);
5563
5564 if (rc == H_RESOURCE)
5565 /* maybe kexecing and resource is busy. try a reset */
5566 rc = ibmvnic_reset_crq(adapter);
5567 retrc = rc;
5568
5569 if (rc == H_CLOSED) {
5570 dev_warn(dev, "Partner adapter not ready\n");
5571 } else if (rc) {
5572 dev_warn(dev, "Error %d opening adapter\n", rc);
5573 goto reg_crq_failed;
5574 }
5575
5576 retrc = 0;
5577
Allen Paisaa7c3fe2020-09-14 12:59:29 +05305578 tasklet_setup(&adapter->tasklet, (void *)ibmvnic_tasklet);
Thomas Falcon6c267b32017-02-15 12:17:58 -06005579
Thomas Falcon032c5e82015-12-21 11:26:06 -06005580 netdev_dbg(adapter->netdev, "registering irq 0x%x\n", vdev->irq);
Murilo Fossa Vicentinie56e2512019-04-25 11:02:33 -03005581 snprintf(crq->name, sizeof(crq->name), "ibmvnic-%x",
5582 adapter->vdev->unit_address);
5583 rc = request_irq(vdev->irq, ibmvnic_interrupt, 0, crq->name, adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005584 if (rc) {
5585 dev_err(dev, "Couldn't register irq 0x%x. rc=%d\n",
5586 vdev->irq, rc);
5587 goto req_irq_failed;
5588 }
5589
5590 rc = vio_enable_interrupts(vdev);
5591 if (rc) {
5592 dev_err(dev, "Error %d enabling interrupts\n", rc);
5593 goto req_irq_failed;
5594 }
5595
5596 crq->cur = 0;
5597 spin_lock_init(&crq->lock);
5598
Sukadev Bhattiprolu6e20d002021-10-29 15:03:15 -07005599 /* process any CRQs that were queued before we enabled interrupts */
5600 tasklet_schedule(&adapter->tasklet);
5601
Thomas Falcon032c5e82015-12-21 11:26:06 -06005602 return retrc;
5603
5604req_irq_failed:
Thomas Falcon6c267b32017-02-15 12:17:58 -06005605 tasklet_kill(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005606 do {
5607 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
5608 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
5609reg_crq_failed:
5610 dma_unmap_single(dev, crq->msg_token, PAGE_SIZE, DMA_BIDIRECTIONAL);
5611map_failed:
5612 free_page((unsigned long)crq->msgs);
Nathan Fontenotf9928872017-03-30 02:48:54 -04005613 crq->msgs = NULL;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005614 return retrc;
5615}
5616
Lijun Pan635e4422020-08-19 17:52:26 -05005617static int ibmvnic_reset_init(struct ibmvnic_adapter *adapter, bool reset)
John Allenf6ef6402017-03-17 17:13:42 -05005618{
5619 struct device *dev = &adapter->vdev->dev;
Dany Madden98c41f02020-11-25 18:04:32 -06005620 unsigned long timeout = msecs_to_jiffies(20000);
Michal Suchanek6881b072021-03-02 20:47:47 +01005621 u64 old_num_rx_queues = adapter->req_rx_queues;
5622 u64 old_num_tx_queues = adapter->req_tx_queues;
John Allenf6ef6402017-03-17 17:13:42 -05005623 int rc;
5624
John Allen017892c12017-05-26 10:30:19 -04005625 adapter->from_passive_init = false;
5626
Michal Suchanek6881b072021-03-02 20:47:47 +01005627 if (reset)
Lijun Pan635e4422020-08-19 17:52:26 -05005628 reinit_completion(&adapter->init_done);
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06005629
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04005630 adapter->init_done_rc = 0;
Lijun Panfa68bfa2020-08-19 17:52:24 -05005631 rc = ibmvnic_send_crq_init(adapter);
5632 if (rc) {
5633 dev_err(dev, "Send crq init failed with error %d\n", rc);
5634 return rc;
5635 }
5636
John Allenf6ef6402017-03-17 17:13:42 -05005637 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
5638 dev_err(dev, "Initialization sequence timed out\n");
Dany Maddenb6ee5662021-12-14 00:17:47 -05005639 return -ETIMEDOUT;
John Allen017892c12017-05-26 10:30:19 -04005640 }
5641
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04005642 if (adapter->init_done_rc) {
5643 release_crq_queue(adapter);
5644 return adapter->init_done_rc;
5645 }
5646
Lijun Pan785a2b12020-09-17 21:12:46 -05005647 if (adapter->from_passive_init) {
5648 adapter->state = VNIC_OPEN;
5649 adapter->from_passive_init = false;
Dany Maddenb6ee5662021-12-14 00:17:47 -05005650 return -EINVAL;
Lijun Pan785a2b12020-09-17 21:12:46 -05005651 }
5652
Lijun Pan635e4422020-08-19 17:52:26 -05005653 if (reset &&
5654 test_bit(0, &adapter->resetting) && !adapter->wait_for_reset &&
Nathan Fontenot30f79622018-04-06 18:37:06 -05005655 adapter->reset_reason != VNIC_RESET_MOBILITY) {
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06005656 if (adapter->req_rx_queues != old_num_rx_queues ||
5657 adapter->req_tx_queues != old_num_tx_queues) {
5658 release_sub_crqs(adapter, 0);
5659 rc = init_sub_crqs(adapter);
5660 } else {
5661 rc = reset_sub_crq_queues(adapter);
5662 }
5663 } else {
Nathan Fontenot57a49432017-05-26 10:31:12 -04005664 rc = init_sub_crqs(adapter);
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06005665 }
5666
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04005667 if (rc) {
5668 dev_err(dev, "Initialization of sub crqs failed\n");
5669 release_crq_queue(adapter);
Thomas Falcon5df969c2017-06-28 19:55:54 -05005670 return rc;
5671 }
5672
5673 rc = init_sub_crq_irqs(adapter);
5674 if (rc) {
5675 dev_err(dev, "Failed to initialize sub crq irqs\n");
5676 release_crq_queue(adapter);
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04005677 }
5678
5679 return rc;
John Allenf6ef6402017-03-17 17:13:42 -05005680}
5681
Thomas Falcon40c9db82017-06-12 12:35:04 -05005682static struct device_attribute dev_attr_failover;
5683
Thomas Falcon032c5e82015-12-21 11:26:06 -06005684static int ibmvnic_probe(struct vio_dev *dev, const struct vio_device_id *id)
5685{
5686 struct ibmvnic_adapter *adapter;
5687 struct net_device *netdev;
5688 unsigned char *mac_addr_p;
Cristobal Forno53f8b1b2021-06-10 11:08:35 -06005689 bool init_success;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005690 int rc;
5691
5692 dev_dbg(&dev->dev, "entering ibmvnic_probe for UA 0x%x\n",
5693 dev->unit_address);
5694
5695 mac_addr_p = (unsigned char *)vio_get_attribute(dev,
5696 VETH_MAC_ADDR, NULL);
5697 if (!mac_addr_p) {
5698 dev_err(&dev->dev,
5699 "(%s:%3.3d) ERROR: Can't find MAC_ADDR attribute\n",
5700 __FILE__, __LINE__);
5701 return 0;
5702 }
5703
5704 netdev = alloc_etherdev_mq(sizeof(struct ibmvnic_adapter),
Thomas Falcond45cc3a2017-12-18 12:52:11 -06005705 IBMVNIC_MAX_QUEUES);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005706 if (!netdev)
5707 return -ENOMEM;
5708
5709 adapter = netdev_priv(netdev);
Nathan Fontenot90c80142017-05-03 14:04:32 -04005710 adapter->state = VNIC_PROBING;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005711 dev_set_drvdata(&dev->dev, netdev);
5712 adapter->vdev = dev;
5713 adapter->netdev = netdev;
Sukadev Bhattiprolu76cdc5c2020-11-25 18:04:29 -06005714 adapter->login_pending = false;
Sukadev Bhattiprolu129854f02021-09-14 20:52:56 -07005715 memset(&adapter->map_ids, 0, sizeof(adapter->map_ids));
5716 /* map_ids start at 1, so ensure map_id 0 is always "in-use" */
5717 bitmap_set(adapter->map_ids, 0, 1);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005718
5719 ether_addr_copy(adapter->mac_addr, mac_addr_p);
Jakub Kicinskif3956eb2021-10-01 14:32:23 -07005720 eth_hw_addr_set(netdev, adapter->mac_addr);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005721 netdev->irq = dev->irq;
5722 netdev->netdev_ops = &ibmvnic_netdev_ops;
5723 netdev->ethtool_ops = &ibmvnic_ethtool_ops;
5724 SET_NETDEV_DEV(netdev, &dev->dev);
5725
Nathan Fontenoted651a12017-05-03 14:04:38 -04005726 INIT_WORK(&adapter->ibmvnic_reset, __ibmvnic_reset);
Juliet Kim7ed5b312019-09-20 16:11:23 -04005727 INIT_DELAYED_WORK(&adapter->ibmvnic_delayed_reset,
5728 __ibmvnic_delayed_reset);
Nathan Fontenoted651a12017-05-03 14:04:38 -04005729 INIT_LIST_HEAD(&adapter->rwi_list);
Thomas Falcon6c5c7482018-12-10 15:22:22 -06005730 spin_lock_init(&adapter->rwi_lock);
Juliet Kim7d7195a2020-03-10 09:23:58 -05005731 spin_lock_init(&adapter->state_lock);
Thomas Falconff25dcb2019-11-25 17:12:56 -06005732 mutex_init(&adapter->fw_lock);
Thomas Falconbbd669a2019-04-04 18:58:26 -05005733 init_completion(&adapter->init_done);
Thomas Falcon070eca92019-11-25 17:12:53 -06005734 init_completion(&adapter->fw_done);
5735 init_completion(&adapter->reset_done);
5736 init_completion(&adapter->stats_done);
Juliet Kim7ed5b312019-09-20 16:11:23 -04005737 clear_bit(0, &adapter->resetting);
Sukadev Bhattiprolu489de952021-09-14 20:52:58 -07005738 adapter->prev_rx_buf_sz = 0;
Sukadev Bhattiprolubbd80932021-09-14 20:52:59 -07005739 adapter->prev_mtu = 0;
Nathan Fontenoted651a12017-05-03 14:04:38 -04005740
Cristobal Forno53f8b1b2021-06-10 11:08:35 -06005741 init_success = false;
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04005742 do {
Nathan Fontenot30f79622018-04-06 18:37:06 -05005743 rc = init_crq_queue(adapter);
5744 if (rc) {
5745 dev_err(&dev->dev, "Couldn't initialize crq. rc=%d\n",
5746 rc);
5747 goto ibmvnic_init_fail;
5748 }
5749
Lijun Pan635e4422020-08-19 17:52:26 -05005750 rc = ibmvnic_reset_init(adapter, false);
Sukadev Bhattiprolu6b278c02021-10-29 15:03:16 -07005751 } while (rc == -EAGAIN);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005752
Cristobal Forno53f8b1b2021-06-10 11:08:35 -06005753 /* We are ignoring the error from ibmvnic_reset_init() assuming that the
5754 * partner is not ready. CRQ is not active. When the partner becomes
5755 * ready, we will do the passive init reset.
5756 */
5757
5758 if (!rc)
5759 init_success = true;
5760
Thomas Falcon07184212018-05-16 15:49:05 -05005761 rc = init_stats_buffers(adapter);
5762 if (rc)
5763 goto ibmvnic_init_fail;
5764
5765 rc = init_stats_token(adapter);
5766 if (rc)
5767 goto ibmvnic_stats_fail;
5768
Thomas Falcon40c9db82017-06-12 12:35:04 -05005769 rc = device_create_file(&dev->dev, &dev_attr_failover);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05005770 if (rc)
Thomas Falcon07184212018-05-16 15:49:05 -05005771 goto ibmvnic_dev_file_err;
Thomas Falcon40c9db82017-06-12 12:35:04 -05005772
Mick Tarsele876a8a2017-09-28 13:53:18 -07005773 netif_carrier_off(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005774 rc = register_netdev(netdev);
5775 if (rc) {
5776 dev_err(&dev->dev, "failed to register netdev rc=%d\n", rc);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05005777 goto ibmvnic_register_fail;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005778 }
5779 dev_info(&dev->dev, "ibmvnic registered\n");
5780
Cristobal Forno53f8b1b2021-06-10 11:08:35 -06005781 if (init_success) {
5782 adapter->state = VNIC_PROBED;
5783 netdev->mtu = adapter->req_mtu - ETH_HLEN;
5784 netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
5785 netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
5786 } else {
5787 adapter->state = VNIC_DOWN;
5788 }
John Allenc26eba02017-10-26 16:23:25 -05005789
5790 adapter->wait_for_reset = false;
Dany Maddena86d5c62020-11-25 18:04:31 -06005791 adapter->last_reset_time = jiffies;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005792 return 0;
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05005793
5794ibmvnic_register_fail:
5795 device_remove_file(&dev->dev, &dev_attr_failover);
5796
Thomas Falcon07184212018-05-16 15:49:05 -05005797ibmvnic_dev_file_err:
5798 release_stats_token(adapter);
5799
5800ibmvnic_stats_fail:
5801 release_stats_buffers(adapter);
5802
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05005803ibmvnic_init_fail:
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06005804 release_sub_crqs(adapter, 1);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05005805 release_crq_queue(adapter);
Thomas Falconff25dcb2019-11-25 17:12:56 -06005806 mutex_destroy(&adapter->fw_lock);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05005807 free_netdev(netdev);
5808
5809 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005810}
5811
Uwe Kleine-König386a9662021-02-25 23:18:34 +01005812static void ibmvnic_remove(struct vio_dev *dev)
Thomas Falcon032c5e82015-12-21 11:26:06 -06005813{
5814 struct net_device *netdev = dev_get_drvdata(&dev->dev);
Nathan Fontenot37489052017-04-19 13:45:04 -04005815 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Juliet Kim7d7195a2020-03-10 09:23:58 -05005816 unsigned long flags;
5817
5818 spin_lock_irqsave(&adapter->state_lock, flags);
Sukadev Bhattiprolu4a41c422021-02-12 20:42:50 -08005819
5820 /* If ibmvnic_reset() is scheduling a reset, wait for it to
5821 * finish. Then, set the state to REMOVING to prevent it from
5822 * scheduling any more work and to have reset functions ignore
5823 * any resets that have already been scheduled. Drop the lock
5824 * after setting state, so __ibmvnic_reset() which is called
5825 * from the flush_work() below, can make progress.
5826 */
Junlin Yang69cdb792021-03-05 16:48:39 +08005827 spin_lock(&adapter->rwi_lock);
Nathan Fontenot90c80142017-05-03 14:04:32 -04005828 adapter->state = VNIC_REMOVING;
Junlin Yang69cdb792021-03-05 16:48:39 +08005829 spin_unlock(&adapter->rwi_lock);
Sukadev Bhattiprolu4a41c422021-02-12 20:42:50 -08005830
Juliet Kim7d7195a2020-03-10 09:23:58 -05005831 spin_unlock_irqrestore(&adapter->state_lock, flags);
5832
Thomas Falcon6954a9e2020-06-12 13:34:41 -05005833 flush_work(&adapter->ibmvnic_reset);
5834 flush_delayed_work(&adapter->ibmvnic_delayed_reset);
5835
Juliet Kima5681e22018-11-19 15:59:22 -06005836 rtnl_lock();
5837 unregister_netdevice(netdev);
Nathan Fontenot37489052017-04-19 13:45:04 -04005838
5839 release_resources(adapter);
Sukadev Bhattiprolu489de952021-09-14 20:52:58 -07005840 release_rx_pools(adapter);
Sukadev Bhattiprolubbd80932021-09-14 20:52:59 -07005841 release_tx_pools(adapter);
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06005842 release_sub_crqs(adapter, 1);
Nathan Fontenot37489052017-04-19 13:45:04 -04005843 release_crq_queue(adapter);
5844
Thomas Falcon53cc7722018-02-26 18:10:56 -06005845 release_stats_token(adapter);
5846 release_stats_buffers(adapter);
5847
Nathan Fontenot90c80142017-05-03 14:04:32 -04005848 adapter->state = VNIC_REMOVED;
5849
Juliet Kima5681e22018-11-19 15:59:22 -06005850 rtnl_unlock();
Thomas Falconff25dcb2019-11-25 17:12:56 -06005851 mutex_destroy(&adapter->fw_lock);
Thomas Falcon40c9db82017-06-12 12:35:04 -05005852 device_remove_file(&dev->dev, &dev_attr_failover);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005853 free_netdev(netdev);
5854 dev_set_drvdata(&dev->dev, NULL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005855}
5856
Thomas Falcon40c9db82017-06-12 12:35:04 -05005857static ssize_t failover_store(struct device *dev, struct device_attribute *attr,
5858 const char *buf, size_t count)
5859{
5860 struct net_device *netdev = dev_get_drvdata(dev);
5861 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
5862 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
5863 __be64 session_token;
5864 long rc;
5865
5866 if (!sysfs_streq(buf, "1"))
5867 return -EINVAL;
5868
5869 rc = plpar_hcall(H_VIOCTL, retbuf, adapter->vdev->unit_address,
5870 H_GET_SESSION_TOKEN, 0, 0, 0);
5871 if (rc) {
5872 netdev_err(netdev, "Couldn't retrieve session token, rc %ld\n",
5873 rc);
Lijun Pan334c4242021-04-13 03:31:44 -05005874 goto last_resort;
Thomas Falcon40c9db82017-06-12 12:35:04 -05005875 }
5876
5877 session_token = (__be64)retbuf[0];
5878 netdev_dbg(netdev, "Initiating client failover, session id %llx\n",
5879 be64_to_cpu(session_token));
5880 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
5881 H_SESSION_ERR_DETECTED, session_token, 0, 0);
Lijun Pan334c4242021-04-13 03:31:44 -05005882 if (rc)
5883 netdev_err(netdev,
5884 "H_VIOCTL initiated failover failed, rc %ld\n",
Thomas Falcon40c9db82017-06-12 12:35:04 -05005885 rc);
Lijun Pan334c4242021-04-13 03:31:44 -05005886
5887last_resort:
5888 netdev_dbg(netdev, "Trying to send CRQ_CMD, the last resort\n");
5889 ibmvnic_reset(adapter, VNIC_RESET_FAILOVER);
Thomas Falcon40c9db82017-06-12 12:35:04 -05005890
5891 return count;
5892}
Joe Perches6cbaefb2017-12-19 10:15:09 -08005893static DEVICE_ATTR_WO(failover);
Thomas Falcon40c9db82017-06-12 12:35:04 -05005894
Thomas Falcon032c5e82015-12-21 11:26:06 -06005895static unsigned long ibmvnic_get_desired_dma(struct vio_dev *vdev)
5896{
5897 struct net_device *netdev = dev_get_drvdata(&vdev->dev);
5898 struct ibmvnic_adapter *adapter;
5899 struct iommu_table *tbl;
5900 unsigned long ret = 0;
5901 int i;
5902
5903 tbl = get_iommu_table_base(&vdev->dev);
5904
5905 /* netdev inits at probe time along with the structures we need below*/
5906 if (!netdev)
5907 return IOMMU_PAGE_ALIGN(IBMVNIC_IO_ENTITLEMENT_DEFAULT, tbl);
5908
5909 adapter = netdev_priv(netdev);
5910
5911 ret += PAGE_SIZE; /* the crq message queue */
Thomas Falcon032c5e82015-12-21 11:26:06 -06005912 ret += IOMMU_PAGE_ALIGN(sizeof(struct ibmvnic_statistics), tbl);
5913
5914 for (i = 0; i < adapter->req_tx_queues + adapter->req_rx_queues; i++)
5915 ret += 4 * PAGE_SIZE; /* the scrq message queue */
5916
Thomas Falcon507ebe62020-08-21 13:39:01 -05005917 for (i = 0; i < adapter->num_active_rx_pools; i++)
Thomas Falcon032c5e82015-12-21 11:26:06 -06005918 ret += adapter->rx_pool[i].size *
5919 IOMMU_PAGE_ALIGN(adapter->rx_pool[i].buff_size, tbl);
5920
5921 return ret;
5922}
5923
5924static int ibmvnic_resume(struct device *dev)
5925{
5926 struct net_device *netdev = dev_get_drvdata(dev);
5927 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005928
John Allencb89ba22017-06-19 11:27:53 -05005929 if (adapter->state != VNIC_OPEN)
5930 return 0;
5931
John Allena2488782017-07-24 13:26:06 -05005932 tasklet_schedule(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005933
5934 return 0;
5935}
5936
Arvind Yadav8c37bc62017-08-17 18:52:54 +05305937static const struct vio_device_id ibmvnic_device_table[] = {
Thomas Falcon032c5e82015-12-21 11:26:06 -06005938 {"network", "IBM,vnic"},
5939 {"", "" }
5940};
5941MODULE_DEVICE_TABLE(vio, ibmvnic_device_table);
5942
5943static const struct dev_pm_ops ibmvnic_pm_ops = {
5944 .resume = ibmvnic_resume
5945};
5946
5947static struct vio_driver ibmvnic_driver = {
5948 .id_table = ibmvnic_device_table,
5949 .probe = ibmvnic_probe,
5950 .remove = ibmvnic_remove,
5951 .get_desired_dma = ibmvnic_get_desired_dma,
5952 .name = ibmvnic_driver_name,
5953 .pm = &ibmvnic_pm_ops,
5954};
5955
5956/* module functions */
5957static int __init ibmvnic_module_init(void)
5958{
5959 pr_info("%s: %s %s\n", ibmvnic_driver_name, ibmvnic_driver_string,
5960 IBMVNIC_DRIVER_VERSION);
5961
5962 return vio_register_driver(&ibmvnic_driver);
5963}
5964
5965static void __exit ibmvnic_module_exit(void)
5966{
5967 vio_unregister_driver(&ibmvnic_driver);
5968}
5969
5970module_init(ibmvnic_module_init);
5971module_exit(ibmvnic_module_exit);