blob: 779de81a54a61b51174d96394aa11daa791449dc [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);
Thomas Falcon9c4eaab2018-05-23 13:37:57 -050098static int send_request_map(struct ibmvnic_adapter *, dma_addr_t, __be32, u8);
99static int send_request_unmap(struct ibmvnic_adapter *, u8);
Thomas Falcon20a8ab72018-02-26 18:10:59 -0600100static int send_login(struct ibmvnic_adapter *adapter);
Lijun Pan491099a2020-09-27 20:13:26 -0500101static void send_query_cap(struct ibmvnic_adapter *adapter);
Thomas Falcon4d96f122017-08-01 15:04:36 -0500102static int init_sub_crqs(struct ibmvnic_adapter *);
John Allenbd0b6722017-03-17 17:13:40 -0500103static int init_sub_crq_irqs(struct ibmvnic_adapter *adapter);
Lijun Pan635e4422020-08-19 17:52:26 -0500104static int ibmvnic_reset_init(struct ibmvnic_adapter *, bool reset);
Nathan Fontenotf9928872017-03-30 02:48:54 -0400105static void release_crq_queue(struct ibmvnic_adapter *);
Thomas Falcon62740e92019-05-09 23:13:43 -0500106static int __ibmvnic_set_mac(struct net_device *, u8 *);
Nathan Fontenot30f79622018-04-06 18:37:06 -0500107static int init_crq_queue(struct ibmvnic_adapter *adapter);
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -0300108static int send_query_phys_parms(struct ibmvnic_adapter *adapter);
Sukadev Bhattiprolu65d64702021-06-23 21:13:12 -0700109static void ibmvnic_tx_scrq_clean_buffer(struct ibmvnic_adapter *adapter,
110 struct ibmvnic_sub_crq_queue *tx_scrq);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600111
112struct ibmvnic_stat {
113 char name[ETH_GSTRING_LEN];
114 int offset;
115};
116
117#define IBMVNIC_STAT_OFF(stat) (offsetof(struct ibmvnic_adapter, stats) + \
118 offsetof(struct ibmvnic_statistics, stat))
Lijun Pan91dc5d22021-02-11 00:43:22 -0600119#define IBMVNIC_GET_STAT(a, off) (*((u64 *)(((unsigned long)(a)) + (off))))
Thomas Falcon032c5e82015-12-21 11:26:06 -0600120
121static const struct ibmvnic_stat ibmvnic_stats[] = {
122 {"rx_packets", IBMVNIC_STAT_OFF(rx_packets)},
123 {"rx_bytes", IBMVNIC_STAT_OFF(rx_bytes)},
124 {"tx_packets", IBMVNIC_STAT_OFF(tx_packets)},
125 {"tx_bytes", IBMVNIC_STAT_OFF(tx_bytes)},
126 {"ucast_tx_packets", IBMVNIC_STAT_OFF(ucast_tx_packets)},
127 {"ucast_rx_packets", IBMVNIC_STAT_OFF(ucast_rx_packets)},
128 {"mcast_tx_packets", IBMVNIC_STAT_OFF(mcast_tx_packets)},
129 {"mcast_rx_packets", IBMVNIC_STAT_OFF(mcast_rx_packets)},
130 {"bcast_tx_packets", IBMVNIC_STAT_OFF(bcast_tx_packets)},
131 {"bcast_rx_packets", IBMVNIC_STAT_OFF(bcast_rx_packets)},
132 {"align_errors", IBMVNIC_STAT_OFF(align_errors)},
133 {"fcs_errors", IBMVNIC_STAT_OFF(fcs_errors)},
134 {"single_collision_frames", IBMVNIC_STAT_OFF(single_collision_frames)},
135 {"multi_collision_frames", IBMVNIC_STAT_OFF(multi_collision_frames)},
136 {"sqe_test_errors", IBMVNIC_STAT_OFF(sqe_test_errors)},
137 {"deferred_tx", IBMVNIC_STAT_OFF(deferred_tx)},
138 {"late_collisions", IBMVNIC_STAT_OFF(late_collisions)},
139 {"excess_collisions", IBMVNIC_STAT_OFF(excess_collisions)},
140 {"internal_mac_tx_errors", IBMVNIC_STAT_OFF(internal_mac_tx_errors)},
141 {"carrier_sense", IBMVNIC_STAT_OFF(carrier_sense)},
142 {"too_long_frames", IBMVNIC_STAT_OFF(too_long_frames)},
143 {"internal_mac_rx_errors", IBMVNIC_STAT_OFF(internal_mac_rx_errors)},
144};
145
146static long h_reg_sub_crq(unsigned long unit_address, unsigned long token,
147 unsigned long length, unsigned long *number,
148 unsigned long *irq)
149{
150 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
151 long rc;
152
153 rc = plpar_hcall(H_REG_SUB_CRQ, retbuf, unit_address, token, length);
154 *number = retbuf[0];
155 *irq = retbuf[1];
156
157 return rc;
158}
159
Thomas Falcon476d96c2019-11-25 17:12:55 -0600160/**
161 * ibmvnic_wait_for_completion - Check device state and wait for completion
162 * @adapter: private device data
163 * @comp_done: completion structure to wait for
164 * @timeout: time to wait in milliseconds
165 *
166 * Wait for a completion signal or until the timeout limit is reached
167 * while checking that the device is still active.
168 */
169static int ibmvnic_wait_for_completion(struct ibmvnic_adapter *adapter,
170 struct completion *comp_done,
171 unsigned long timeout)
172{
173 struct net_device *netdev;
174 unsigned long div_timeout;
175 u8 retry;
176
177 netdev = adapter->netdev;
178 retry = 5;
179 div_timeout = msecs_to_jiffies(timeout / retry);
180 while (true) {
181 if (!adapter->crq.active) {
182 netdev_err(netdev, "Device down!\n");
183 return -ENODEV;
184 }
Thomas Falcon8f9cc1e2019-12-11 09:38:39 -0600185 if (!retry--)
Thomas Falcon476d96c2019-11-25 17:12:55 -0600186 break;
187 if (wait_for_completion_timeout(comp_done, div_timeout))
188 return 0;
189 }
190 netdev_err(netdev, "Operation timed out.\n");
191 return -ETIMEDOUT;
192}
193
Thomas Falcon032c5e82015-12-21 11:26:06 -0600194static int alloc_long_term_buff(struct ibmvnic_adapter *adapter,
195 struct ibmvnic_long_term_buff *ltb, int size)
196{
197 struct device *dev = &adapter->vdev->dev;
Thomas Falcon9c4eaab2018-05-23 13:37:57 -0500198 int rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600199
200 ltb->size = size;
201 ltb->buff = dma_alloc_coherent(dev, ltb->size, &ltb->addr,
202 GFP_KERNEL);
203
204 if (!ltb->buff) {
205 dev_err(dev, "Couldn't alloc long term buffer\n");
206 return -ENOMEM;
207 }
208 ltb->map_id = adapter->map_id;
209 adapter->map_id++;
Nathan Fontenotdb5d0b52017-02-10 13:45:05 -0500210
Thomas Falconff25dcb2019-11-25 17:12:56 -0600211 mutex_lock(&adapter->fw_lock);
212 adapter->fw_done_rc = 0;
Thomas Falcon070eca92019-11-25 17:12:53 -0600213 reinit_completion(&adapter->fw_done);
Thomas Falcon9c4eaab2018-05-23 13:37:57 -0500214 rc = send_request_map(adapter, ltb->addr,
215 ltb->size, ltb->map_id);
216 if (rc) {
217 dma_free_coherent(dev, ltb->size, ltb->buff, ltb->addr);
Thomas Falconff25dcb2019-11-25 17:12:56 -0600218 mutex_unlock(&adapter->fw_lock);
Thomas Falcon9c4eaab2018-05-23 13:37:57 -0500219 return rc;
220 }
Thomas Falcon476d96c2019-11-25 17:12:55 -0600221
222 rc = ibmvnic_wait_for_completion(adapter, &adapter->fw_done, 10000);
223 if (rc) {
224 dev_err(dev,
225 "Long term map request aborted or timed out,rc = %d\n",
226 rc);
227 dma_free_coherent(dev, ltb->size, ltb->buff, ltb->addr);
Thomas Falconff25dcb2019-11-25 17:12:56 -0600228 mutex_unlock(&adapter->fw_lock);
Thomas Falcon476d96c2019-11-25 17:12:55 -0600229 return rc;
230 }
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500231
232 if (adapter->fw_done_rc) {
233 dev_err(dev, "Couldn't map long term buffer,rc = %d\n",
234 adapter->fw_done_rc);
Thomas Falcon4cf2ddf32018-05-16 15:49:03 -0500235 dma_free_coherent(dev, ltb->size, ltb->buff, ltb->addr);
Thomas Falconff25dcb2019-11-25 17:12:56 -0600236 mutex_unlock(&adapter->fw_lock);
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500237 return -1;
238 }
Thomas Falconff25dcb2019-11-25 17:12:56 -0600239 mutex_unlock(&adapter->fw_lock);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600240 return 0;
241}
242
243static void free_long_term_buff(struct ibmvnic_adapter *adapter,
244 struct ibmvnic_long_term_buff *ltb)
245{
246 struct device *dev = &adapter->vdev->dev;
247
Nathan Fontenotc657e322017-03-30 02:49:06 -0400248 if (!ltb->buff)
249 return;
250
Lijun Pan7d3a7b92021-02-12 20:49:00 -0600251 /* VIOS automatically unmaps the long term buffer at remote
252 * end for the following resets:
253 * FAILOVER, MOBILITY, TIMEOUT.
254 */
Nathan Fontenoted651a12017-05-03 14:04:38 -0400255 if (adapter->reset_reason != VNIC_RESET_FAILOVER &&
Lijun Pan7d3a7b92021-02-12 20:49:00 -0600256 adapter->reset_reason != VNIC_RESET_MOBILITY &&
257 adapter->reset_reason != VNIC_RESET_TIMEOUT)
Thomas Falcondfad09a2016-08-18 11:37:51 -0500258 send_request_unmap(adapter, ltb->map_id);
Brian King59af56c2017-04-19 13:44:41 -0400259 dma_free_coherent(dev, ltb->size, ltb->buff, ltb->addr);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600260}
261
Sukadev Bhattiprolu0ec13af2021-06-23 21:13:10 -0700262static int reset_long_term_buff(struct ibmvnic_adapter *adapter,
263 struct ibmvnic_long_term_buff *ltb)
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500264{
Sukadev Bhattiprolu0ec13af2021-06-23 21:13:10 -0700265 struct device *dev = &adapter->vdev->dev;
266 int rc;
Thomas Falcon9c4eaab2018-05-23 13:37:57 -0500267
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500268 memset(ltb->buff, 0, ltb->size);
Sukadev Bhattiprolu0ec13af2021-06-23 21:13:10 -0700269
270 mutex_lock(&adapter->fw_lock);
271 adapter->fw_done_rc = 0;
272
273 reinit_completion(&adapter->fw_done);
274 rc = send_request_map(adapter, ltb->addr, ltb->size, ltb->map_id);
275 if (rc) {
276 mutex_unlock(&adapter->fw_lock);
277 return rc;
278 }
279
280 rc = ibmvnic_wait_for_completion(adapter, &adapter->fw_done, 10000);
281 if (rc) {
282 dev_info(dev,
283 "Reset failed, long term map request timed out or aborted\n");
284 mutex_unlock(&adapter->fw_lock);
285 return rc;
286 }
287
288 if (adapter->fw_done_rc) {
289 dev_info(dev,
290 "Reset failed, attempting to free and reallocate buffer\n");
291 free_long_term_buff(adapter, ltb);
292 mutex_unlock(&adapter->fw_lock);
293 return alloc_long_term_buff(adapter, ltb, ltb->size);
294 }
295 mutex_unlock(&adapter->fw_lock);
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500296 return 0;
297}
298
Thomas Falconf185a492017-05-26 10:30:48 -0400299static void deactivate_rx_pools(struct ibmvnic_adapter *adapter)
300{
301 int i;
302
Thomas Falcon507ebe62020-08-21 13:39:01 -0500303 for (i = 0; i < adapter->num_active_rx_pools; i++)
Thomas Falconf185a492017-05-26 10:30:48 -0400304 adapter->rx_pool[i].active = 0;
305}
306
Thomas Falcon032c5e82015-12-21 11:26:06 -0600307static void replenish_rx_pool(struct ibmvnic_adapter *adapter,
308 struct ibmvnic_rx_pool *pool)
309{
310 int count = pool->size - atomic_read(&pool->available);
Cristobal Fornof3ae59c2020-08-19 13:16:23 -0500311 u64 handle = adapter->rx_scrq[pool->index]->handle;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600312 struct device *dev = &adapter->vdev->dev;
Thomas Falcon4f0b6812020-11-18 19:12:18 -0600313 struct ibmvnic_ind_xmit_queue *ind_bufp;
314 struct ibmvnic_sub_crq_queue *rx_scrq;
315 union sub_crq *sub_crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600316 int buffers_added = 0;
317 unsigned long lpar_rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600318 struct sk_buff *skb;
319 unsigned int offset;
320 dma_addr_t dma_addr;
321 unsigned char *dst;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600322 int shift = 0;
323 int index;
324 int i;
325
Thomas Falconf185a492017-05-26 10:30:48 -0400326 if (!pool->active)
327 return;
328
Thomas Falcon4f0b6812020-11-18 19:12:18 -0600329 rx_scrq = adapter->rx_scrq[pool->index];
330 ind_bufp = &rx_scrq->ind_buf;
Sukadev Bhattiprolu72368f82021-06-23 21:13:13 -0700331
332 /* netdev_skb_alloc() could have failed after we saved a few skbs
333 * in the indir_buf and we would not have sent them to VIOS yet.
334 * To account for them, start the loop at ind_bufp->index rather
335 * than 0. If we pushed all the skbs to VIOS, ind_bufp->index will
336 * be 0.
337 */
338 for (i = ind_bufp->index; i < count; ++i) {
Dwip N. Banerjeee552aa32020-11-18 19:12:24 -0600339 skb = netdev_alloc_skb(adapter->netdev, pool->buff_size);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600340 if (!skb) {
341 dev_err(dev, "Couldn't replenish rx buff\n");
342 adapter->replenish_no_mem++;
343 break;
344 }
345
346 index = pool->free_map[pool->next_free];
347
348 if (pool->rx_buff[index].skb)
349 dev_err(dev, "Inconsistent free_map!\n");
350
351 /* Copy the skb to the long term mapped DMA buffer */
352 offset = index * pool->buff_size;
353 dst = pool->long_term_buff.buff + offset;
354 memset(dst, 0, pool->buff_size);
355 dma_addr = pool->long_term_buff.addr + offset;
356 pool->rx_buff[index].data = dst;
357
358 pool->free_map[pool->next_free] = IBMVNIC_INVALID_MAP;
359 pool->rx_buff[index].dma = dma_addr;
360 pool->rx_buff[index].skb = skb;
361 pool->rx_buff[index].pool_index = pool->index;
362 pool->rx_buff[index].size = pool->buff_size;
363
Thomas Falcon4f0b6812020-11-18 19:12:18 -0600364 sub_crq = &ind_bufp->indir_arr[ind_bufp->index++];
365 memset(sub_crq, 0, sizeof(*sub_crq));
366 sub_crq->rx_add.first = IBMVNIC_CRQ_CMD;
367 sub_crq->rx_add.correlator =
Thomas Falcon032c5e82015-12-21 11:26:06 -0600368 cpu_to_be64((u64)&pool->rx_buff[index]);
Thomas Falcon4f0b6812020-11-18 19:12:18 -0600369 sub_crq->rx_add.ioba = cpu_to_be32(dma_addr);
370 sub_crq->rx_add.map_id = pool->long_term_buff.map_id;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600371
372 /* The length field of the sCRQ is defined to be 24 bits so the
373 * buffer size needs to be left shifted by a byte before it is
374 * converted to big endian to prevent the last byte from being
375 * truncated.
376 */
377#ifdef __LITTLE_ENDIAN__
378 shift = 8;
379#endif
Thomas Falcon4f0b6812020-11-18 19:12:18 -0600380 sub_crq->rx_add.len = cpu_to_be32(pool->buff_size << shift);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600381 pool->next_free = (pool->next_free + 1) % pool->size;
Thomas Falcon4f0b6812020-11-18 19:12:18 -0600382 if (ind_bufp->index == IBMVNIC_MAX_IND_DESCS ||
383 i == count - 1) {
384 lpar_rc =
385 send_subcrq_indirect(adapter, handle,
386 (u64)ind_bufp->indir_dma,
387 (u64)ind_bufp->index);
388 if (lpar_rc != H_SUCCESS)
389 goto failure;
390 buffers_added += ind_bufp->index;
391 adapter->replenish_add_buff_success += ind_bufp->index;
392 ind_bufp->index = 0;
393 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600394 }
395 atomic_add(buffers_added, &pool->available);
396 return;
397
398failure:
Thomas Falcon2d14d372018-07-13 12:03:32 -0500399 if (lpar_rc != H_PARAMETER && lpar_rc != H_CLOSED)
400 dev_err_ratelimited(dev, "rx: replenish packet buffer failed\n");
Thomas Falcon4f0b6812020-11-18 19:12:18 -0600401 for (i = ind_bufp->index - 1; i >= 0; --i) {
402 struct ibmvnic_rx_buff *rx_buff;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600403
Thomas Falcon4f0b6812020-11-18 19:12:18 -0600404 pool->next_free = pool->next_free == 0 ?
405 pool->size - 1 : pool->next_free - 1;
406 sub_crq = &ind_bufp->indir_arr[i];
407 rx_buff = (struct ibmvnic_rx_buff *)
408 be64_to_cpu(sub_crq->rx_add.correlator);
409 index = (int)(rx_buff - pool->rx_buff);
410 pool->free_map[pool->next_free] = index;
411 dev_kfree_skb_any(pool->rx_buff[index].skb);
412 pool->rx_buff[index].skb = NULL;
413 }
Dwip N. Banerjeec2af6222020-12-09 20:53:31 -0500414 adapter->replenish_add_buff_failure += ind_bufp->index;
415 atomic_add(buffers_added, &pool->available);
Thomas Falcon4f0b6812020-11-18 19:12:18 -0600416 ind_bufp->index = 0;
Thomas Falcon5a18e1e2018-04-06 18:37:05 -0500417 if (lpar_rc == H_CLOSED || adapter->failover_pending) {
Thomas Falconf185a492017-05-26 10:30:48 -0400418 /* Disable buffer pool replenishment and report carrier off if
Thomas Falcon5a18e1e2018-04-06 18:37:05 -0500419 * queue is closed or pending failover.
420 * Firmware guarantees that a signal will be sent to the
421 * driver, triggering a reset.
Thomas Falconf185a492017-05-26 10:30:48 -0400422 */
423 deactivate_rx_pools(adapter);
424 netif_carrier_off(adapter->netdev);
425 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600426}
427
428static void replenish_pools(struct ibmvnic_adapter *adapter)
429{
430 int i;
431
Thomas Falcon032c5e82015-12-21 11:26:06 -0600432 adapter->replenish_task_cycles++;
Thomas Falcon507ebe62020-08-21 13:39:01 -0500433 for (i = 0; i < adapter->num_active_rx_pools; i++) {
Thomas Falcon032c5e82015-12-21 11:26:06 -0600434 if (adapter->rx_pool[i].active)
435 replenish_rx_pool(adapter, &adapter->rx_pool[i]);
436 }
Sukadev Bhattiprolu38bd5ce2020-12-04 18:22:35 -0800437
438 netdev_dbg(adapter->netdev, "Replenished %d pools\n", i);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600439}
440
John Allen3d52b592017-08-02 16:44:14 -0500441static void release_stats_buffers(struct ibmvnic_adapter *adapter)
442{
443 kfree(adapter->tx_stats_buffers);
444 kfree(adapter->rx_stats_buffers);
Thomas Falconb0992ec2018-02-06 17:25:23 -0600445 adapter->tx_stats_buffers = NULL;
446 adapter->rx_stats_buffers = NULL;
John Allen3d52b592017-08-02 16:44:14 -0500447}
448
449static int init_stats_buffers(struct ibmvnic_adapter *adapter)
450{
451 adapter->tx_stats_buffers =
Nathan Fontenotabcae542018-02-19 13:30:47 -0600452 kcalloc(IBMVNIC_MAX_QUEUES,
John Allen3d52b592017-08-02 16:44:14 -0500453 sizeof(struct ibmvnic_tx_queue_stats),
454 GFP_KERNEL);
455 if (!adapter->tx_stats_buffers)
456 return -ENOMEM;
457
458 adapter->rx_stats_buffers =
Nathan Fontenotabcae542018-02-19 13:30:47 -0600459 kcalloc(IBMVNIC_MAX_QUEUES,
John Allen3d52b592017-08-02 16:44:14 -0500460 sizeof(struct ibmvnic_rx_queue_stats),
461 GFP_KERNEL);
462 if (!adapter->rx_stats_buffers)
463 return -ENOMEM;
464
465 return 0;
466}
467
Nathan Fontenot7bbc27a2017-03-30 02:49:23 -0400468static void release_stats_token(struct ibmvnic_adapter *adapter)
469{
470 struct device *dev = &adapter->vdev->dev;
471
472 if (!adapter->stats_token)
473 return;
474
475 dma_unmap_single(dev, adapter->stats_token,
476 sizeof(struct ibmvnic_statistics),
477 DMA_FROM_DEVICE);
478 adapter->stats_token = 0;
479}
480
481static int init_stats_token(struct ibmvnic_adapter *adapter)
482{
483 struct device *dev = &adapter->vdev->dev;
484 dma_addr_t stok;
485
486 stok = dma_map_single(dev, &adapter->stats,
487 sizeof(struct ibmvnic_statistics),
488 DMA_FROM_DEVICE);
489 if (dma_mapping_error(dev, stok)) {
490 dev_err(dev, "Couldn't map stats buffer\n");
491 return -1;
492 }
493
494 adapter->stats_token = stok;
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500495 netdev_dbg(adapter->netdev, "Stats token initialized (%llx)\n", stok);
Nathan Fontenot7bbc27a2017-03-30 02:49:23 -0400496 return 0;
497}
498
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400499static int reset_rx_pools(struct ibmvnic_adapter *adapter)
500{
501 struct ibmvnic_rx_pool *rx_pool;
Thomas Falcon507ebe62020-08-21 13:39:01 -0500502 u64 buff_size;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400503 int rx_scrqs;
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500504 int i, j, rc;
John Allen896d8692018-01-18 16:26:31 -0600505
Mingming Cao9f134572020-08-25 13:26:41 -0400506 if (!adapter->rx_pool)
507 return -1;
508
Thomas Falcon507ebe62020-08-21 13:39:01 -0500509 buff_size = adapter->cur_rx_buf_sz;
510 rx_scrqs = adapter->num_active_rx_pools;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400511 for (i = 0; i < rx_scrqs; i++) {
512 rx_pool = &adapter->rx_pool[i];
513
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500514 netdev_dbg(adapter->netdev, "Re-setting rx_pool[%d]\n", i);
515
Thomas Falcon507ebe62020-08-21 13:39:01 -0500516 if (rx_pool->buff_size != buff_size) {
John Allen896d8692018-01-18 16:26:31 -0600517 free_long_term_buff(adapter, &rx_pool->long_term_buff);
Dwip N. Banerjee9a87c3f2020-11-18 19:12:22 -0600518 rx_pool->buff_size = ALIGN(buff_size, L1_CACHE_BYTES);
Thomas Falcon7c940b12019-06-07 16:03:55 -0500519 rc = alloc_long_term_buff(adapter,
520 &rx_pool->long_term_buff,
521 rx_pool->size *
522 rx_pool->buff_size);
John Allen896d8692018-01-18 16:26:31 -0600523 } else {
Sukadev Bhattiprolu0ec13af2021-06-23 21:13:10 -0700524 rc = reset_long_term_buff(adapter,
525 &rx_pool->long_term_buff);
John Allen896d8692018-01-18 16:26:31 -0600526 }
527
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500528 if (rc)
529 return rc;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400530
531 for (j = 0; j < rx_pool->size; j++)
532 rx_pool->free_map[j] = j;
533
534 memset(rx_pool->rx_buff, 0,
535 rx_pool->size * sizeof(struct ibmvnic_rx_buff));
536
537 atomic_set(&rx_pool->available, 0);
538 rx_pool->next_alloc = 0;
539 rx_pool->next_free = 0;
Thomas Falconc3e53b92017-06-14 23:50:05 -0500540 rx_pool->active = 1;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400541 }
542
543 return 0;
544}
545
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400546static void release_rx_pools(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -0600547{
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400548 struct ibmvnic_rx_pool *rx_pool;
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400549 int i, j;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600550
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400551 if (!adapter->rx_pool)
Thomas Falcon032c5e82015-12-21 11:26:06 -0600552 return;
553
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600554 for (i = 0; i < adapter->num_active_rx_pools; i++) {
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400555 rx_pool = &adapter->rx_pool[i];
556
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500557 netdev_dbg(adapter->netdev, "Releasing rx_pool[%d]\n", i);
558
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400559 kfree(rx_pool->free_map);
560 free_long_term_buff(adapter, &rx_pool->long_term_buff);
561
562 if (!rx_pool->rx_buff)
Nathan Fontenote0ebe9422017-05-03 14:04:50 -0400563 continue;
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400564
565 for (j = 0; j < rx_pool->size; j++) {
566 if (rx_pool->rx_buff[j].skb) {
Thomas Falconb7cdec32018-11-21 11:17:58 -0600567 dev_kfree_skb_any(rx_pool->rx_buff[j].skb);
568 rx_pool->rx_buff[j].skb = NULL;
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400569 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600570 }
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400571
572 kfree(rx_pool->rx_buff);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600573 }
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400574
575 kfree(adapter->rx_pool);
576 adapter->rx_pool = NULL;
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600577 adapter->num_active_rx_pools = 0;
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400578}
579
580static int init_rx_pools(struct net_device *netdev)
581{
582 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
583 struct device *dev = &adapter->vdev->dev;
584 struct ibmvnic_rx_pool *rx_pool;
585 int rxadd_subcrqs;
Thomas Falcon507ebe62020-08-21 13:39:01 -0500586 u64 buff_size;
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400587 int i, j;
588
Thomas Falcon507ebe62020-08-21 13:39:01 -0500589 rxadd_subcrqs = adapter->num_active_rx_scrqs;
590 buff_size = adapter->cur_rx_buf_sz;
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400591
592 adapter->rx_pool = kcalloc(rxadd_subcrqs,
593 sizeof(struct ibmvnic_rx_pool),
594 GFP_KERNEL);
595 if (!adapter->rx_pool) {
596 dev_err(dev, "Failed to allocate rx pools\n");
597 return -1;
598 }
599
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600600 adapter->num_active_rx_pools = rxadd_subcrqs;
601
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400602 for (i = 0; i < rxadd_subcrqs; i++) {
603 rx_pool = &adapter->rx_pool[i];
604
605 netdev_dbg(adapter->netdev,
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500606 "Initializing rx_pool[%d], %lld buffs, %lld bytes each\n",
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400607 i, adapter->req_rx_add_entries_per_subcrq,
Thomas Falcon507ebe62020-08-21 13:39:01 -0500608 buff_size);
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400609
610 rx_pool->size = adapter->req_rx_add_entries_per_subcrq;
611 rx_pool->index = i;
Dwip N. Banerjee9a87c3f2020-11-18 19:12:22 -0600612 rx_pool->buff_size = ALIGN(buff_size, L1_CACHE_BYTES);
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400613 rx_pool->active = 1;
614
615 rx_pool->free_map = kcalloc(rx_pool->size, sizeof(int),
616 GFP_KERNEL);
617 if (!rx_pool->free_map) {
618 release_rx_pools(adapter);
619 return -1;
620 }
621
622 rx_pool->rx_buff = kcalloc(rx_pool->size,
623 sizeof(struct ibmvnic_rx_buff),
624 GFP_KERNEL);
625 if (!rx_pool->rx_buff) {
626 dev_err(dev, "Couldn't alloc rx buffers\n");
627 release_rx_pools(adapter);
628 return -1;
629 }
630
631 if (alloc_long_term_buff(adapter, &rx_pool->long_term_buff,
632 rx_pool->size * rx_pool->buff_size)) {
633 release_rx_pools(adapter);
634 return -1;
635 }
636
637 for (j = 0; j < rx_pool->size; ++j)
638 rx_pool->free_map[j] = j;
639
640 atomic_set(&rx_pool->available, 0);
641 rx_pool->next_alloc = 0;
642 rx_pool->next_free = 0;
643 }
644
645 return 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600646}
647
Sukadev Bhattiprolu0ec13af2021-06-23 21:13:10 -0700648static int reset_one_tx_pool(struct ibmvnic_adapter *adapter,
649 struct ibmvnic_tx_pool *tx_pool)
Thomas Falcone26dc252018-03-16 20:00:25 -0500650{
651 int rc, i;
652
Sukadev Bhattiprolu0ec13af2021-06-23 21:13:10 -0700653 rc = reset_long_term_buff(adapter, &tx_pool->long_term_buff);
Thomas Falcone26dc252018-03-16 20:00:25 -0500654 if (rc)
655 return rc;
656
657 memset(tx_pool->tx_buff, 0,
658 tx_pool->num_buffers *
659 sizeof(struct ibmvnic_tx_buff));
660
661 for (i = 0; i < tx_pool->num_buffers; i++)
662 tx_pool->free_map[i] = i;
663
664 tx_pool->consumer_index = 0;
665 tx_pool->producer_index = 0;
666
667 return 0;
668}
669
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400670static int reset_tx_pools(struct ibmvnic_adapter *adapter)
671{
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400672 int tx_scrqs;
Thomas Falcone26dc252018-03-16 20:00:25 -0500673 int i, rc;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400674
Mingming Cao9f134572020-08-25 13:26:41 -0400675 if (!adapter->tx_pool)
676 return -1;
677
Thomas Falcon507ebe62020-08-21 13:39:01 -0500678 tx_scrqs = adapter->num_active_tx_pools;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400679 for (i = 0; i < tx_scrqs; i++) {
Sukadev Bhattiprolu65d64702021-06-23 21:13:12 -0700680 ibmvnic_tx_scrq_clean_buffer(adapter, adapter->tx_scrq[i]);
Sukadev Bhattiprolu0ec13af2021-06-23 21:13:10 -0700681 rc = reset_one_tx_pool(adapter, &adapter->tso_pool[i]);
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500682 if (rc)
683 return rc;
Sukadev Bhattiprolu0ec13af2021-06-23 21:13:10 -0700684 rc = reset_one_tx_pool(adapter, &adapter->tx_pool[i]);
Thomas Falconfdb06102017-10-17 12:36:55 -0500685 if (rc)
686 return rc;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400687 }
688
689 return 0;
690}
691
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -0200692static void release_vpd_data(struct ibmvnic_adapter *adapter)
693{
694 if (!adapter->vpd)
695 return;
696
697 kfree(adapter->vpd->buff);
698 kfree(adapter->vpd);
Thomas Falconb0992ec2018-02-06 17:25:23 -0600699
700 adapter->vpd = NULL;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -0200701}
702
Thomas Falconfb794212018-03-16 20:00:26 -0500703static void release_one_tx_pool(struct ibmvnic_adapter *adapter,
704 struct ibmvnic_tx_pool *tx_pool)
705{
706 kfree(tx_pool->tx_buff);
707 kfree(tx_pool->free_map);
708 free_long_term_buff(adapter, &tx_pool->long_term_buff);
709}
710
Nathan Fontenotc657e322017-03-30 02:49:06 -0400711static void release_tx_pools(struct ibmvnic_adapter *adapter)
712{
John Allen896d8692018-01-18 16:26:31 -0600713 int i;
Nathan Fontenotc657e322017-03-30 02:49:06 -0400714
715 if (!adapter->tx_pool)
716 return;
717
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600718 for (i = 0; i < adapter->num_active_tx_pools; i++) {
Thomas Falconfb794212018-03-16 20:00:26 -0500719 release_one_tx_pool(adapter, &adapter->tx_pool[i]);
720 release_one_tx_pool(adapter, &adapter->tso_pool[i]);
Nathan Fontenotc657e322017-03-30 02:49:06 -0400721 }
722
723 kfree(adapter->tx_pool);
724 adapter->tx_pool = NULL;
Thomas Falconfb794212018-03-16 20:00:26 -0500725 kfree(adapter->tso_pool);
726 adapter->tso_pool = NULL;
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600727 adapter->num_active_tx_pools = 0;
Nathan Fontenotc657e322017-03-30 02:49:06 -0400728}
729
Thomas Falcon32053062018-03-16 20:00:27 -0500730static int init_one_tx_pool(struct net_device *netdev,
731 struct ibmvnic_tx_pool *tx_pool,
732 int num_entries, int buf_size)
733{
734 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
735 int i;
736
737 tx_pool->tx_buff = kcalloc(num_entries,
738 sizeof(struct ibmvnic_tx_buff),
739 GFP_KERNEL);
740 if (!tx_pool->tx_buff)
741 return -1;
742
743 if (alloc_long_term_buff(adapter, &tx_pool->long_term_buff,
744 num_entries * buf_size))
745 return -1;
746
747 tx_pool->free_map = kcalloc(num_entries, sizeof(int), GFP_KERNEL);
748 if (!tx_pool->free_map)
749 return -1;
750
751 for (i = 0; i < num_entries; i++)
752 tx_pool->free_map[i] = i;
753
754 tx_pool->consumer_index = 0;
755 tx_pool->producer_index = 0;
756 tx_pool->num_buffers = num_entries;
757 tx_pool->buf_size = buf_size;
758
759 return 0;
760}
761
Nathan Fontenotc657e322017-03-30 02:49:06 -0400762static int init_tx_pools(struct net_device *netdev)
763{
764 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Nathan Fontenotc657e322017-03-30 02:49:06 -0400765 int tx_subcrqs;
Dwip N. Banerjee9a87c3f2020-11-18 19:12:22 -0600766 u64 buff_size;
Thomas Falcon32053062018-03-16 20:00:27 -0500767 int i, rc;
Nathan Fontenotc657e322017-03-30 02:49:06 -0400768
Thomas Falcon507ebe62020-08-21 13:39:01 -0500769 tx_subcrqs = adapter->num_active_tx_scrqs;
Nathan Fontenotc657e322017-03-30 02:49:06 -0400770 adapter->tx_pool = kcalloc(tx_subcrqs,
771 sizeof(struct ibmvnic_tx_pool), GFP_KERNEL);
772 if (!adapter->tx_pool)
773 return -1;
774
Thomas Falcon32053062018-03-16 20:00:27 -0500775 adapter->tso_pool = kcalloc(tx_subcrqs,
776 sizeof(struct ibmvnic_tx_pool), GFP_KERNEL);
777 if (!adapter->tso_pool)
778 return -1;
779
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600780 adapter->num_active_tx_pools = tx_subcrqs;
781
Nathan Fontenotc657e322017-03-30 02:49:06 -0400782 for (i = 0; i < tx_subcrqs; i++) {
Dwip N. Banerjee9a87c3f2020-11-18 19:12:22 -0600783 buff_size = adapter->req_mtu + VLAN_HLEN;
784 buff_size = ALIGN(buff_size, L1_CACHE_BYTES);
Thomas Falcon32053062018-03-16 20:00:27 -0500785 rc = init_one_tx_pool(netdev, &adapter->tx_pool[i],
786 adapter->req_tx_entries_per_subcrq,
Dwip N. Banerjee9a87c3f2020-11-18 19:12:22 -0600787 buff_size);
Thomas Falcon32053062018-03-16 20:00:27 -0500788 if (rc) {
Nathan Fontenotc657e322017-03-30 02:49:06 -0400789 release_tx_pools(adapter);
Thomas Falcon32053062018-03-16 20:00:27 -0500790 return rc;
Nathan Fontenotc657e322017-03-30 02:49:06 -0400791 }
792
Thomas Falcon7c940b12019-06-07 16:03:55 -0500793 rc = init_one_tx_pool(netdev, &adapter->tso_pool[i],
794 IBMVNIC_TSO_BUFS,
795 IBMVNIC_TSO_BUF_SZ);
Thomas Falcon32053062018-03-16 20:00:27 -0500796 if (rc) {
Nathan Fontenotc657e322017-03-30 02:49:06 -0400797 release_tx_pools(adapter);
Thomas Falcon32053062018-03-16 20:00:27 -0500798 return rc;
Nathan Fontenotc657e322017-03-30 02:49:06 -0400799 }
Nathan Fontenotc657e322017-03-30 02:49:06 -0400800 }
801
802 return 0;
803}
804
John Allend944c3d62017-05-26 10:30:13 -0400805static void ibmvnic_napi_enable(struct ibmvnic_adapter *adapter)
806{
807 int i;
808
809 if (adapter->napi_enabled)
810 return;
811
812 for (i = 0; i < adapter->req_rx_queues; i++)
813 napi_enable(&adapter->napi[i]);
814
815 adapter->napi_enabled = true;
816}
817
818static void ibmvnic_napi_disable(struct ibmvnic_adapter *adapter)
819{
820 int i;
821
822 if (!adapter->napi_enabled)
823 return;
824
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500825 for (i = 0; i < adapter->req_rx_queues; i++) {
826 netdev_dbg(adapter->netdev, "Disabling napi[%d]\n", i);
John Allend944c3d62017-05-26 10:30:13 -0400827 napi_disable(&adapter->napi[i]);
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500828 }
John Allend944c3d62017-05-26 10:30:13 -0400829
830 adapter->napi_enabled = false;
831}
832
Nathan Fontenot86f669b2018-02-19 13:30:39 -0600833static int init_napi(struct ibmvnic_adapter *adapter)
834{
835 int i;
836
837 adapter->napi = kcalloc(adapter->req_rx_queues,
838 sizeof(struct napi_struct), GFP_KERNEL);
839 if (!adapter->napi)
840 return -ENOMEM;
841
842 for (i = 0; i < adapter->req_rx_queues; i++) {
843 netdev_dbg(adapter->netdev, "Adding napi[%d]\n", i);
844 netif_napi_add(adapter->netdev, &adapter->napi[i],
845 ibmvnic_poll, NAPI_POLL_WEIGHT);
846 }
847
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600848 adapter->num_active_rx_napi = adapter->req_rx_queues;
Nathan Fontenot86f669b2018-02-19 13:30:39 -0600849 return 0;
850}
851
852static void release_napi(struct ibmvnic_adapter *adapter)
853{
854 int i;
855
856 if (!adapter->napi)
857 return;
858
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600859 for (i = 0; i < adapter->num_active_rx_napi; i++) {
Wen Yang390de192018-12-11 12:20:46 +0800860 netdev_dbg(adapter->netdev, "Releasing napi[%d]\n", i);
861 netif_napi_del(&adapter->napi[i]);
Nathan Fontenot86f669b2018-02-19 13:30:39 -0600862 }
863
864 kfree(adapter->napi);
865 adapter->napi = NULL;
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600866 adapter->num_active_rx_napi = 0;
Thomas Falconc3f22412018-05-23 13:37:55 -0500867 adapter->napi_enabled = false;
Nathan Fontenot86f669b2018-02-19 13:30:39 -0600868}
869
Lijun Pan0666ef72021-04-12 02:41:28 -0500870static const char *adapter_state_to_string(enum vnic_state state)
871{
872 switch (state) {
873 case VNIC_PROBING:
874 return "PROBING";
875 case VNIC_PROBED:
876 return "PROBED";
877 case VNIC_OPENING:
878 return "OPENING";
879 case VNIC_OPEN:
880 return "OPEN";
881 case VNIC_CLOSING:
882 return "CLOSING";
883 case VNIC_CLOSED:
884 return "CLOSED";
885 case VNIC_REMOVING:
886 return "REMOVING";
887 case VNIC_REMOVED:
888 return "REMOVED";
889 default:
890 return "UNKNOWN";
891 }
892}
893
John Allena57a5d22017-03-17 17:13:41 -0500894static int ibmvnic_login(struct net_device *netdev)
Thomas Falcon032c5e82015-12-21 11:26:06 -0600895{
896 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Dany Madden98c41f02020-11-25 18:04:32 -0600897 unsigned long timeout = msecs_to_jiffies(20000);
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500898 int retry_count = 0;
Thomas Falcondff515a32020-06-15 10:29:23 -0500899 int retries = 10;
Thomas Falconeb110412018-05-24 14:37:53 -0500900 bool retry;
Thomas Falcon4d96f122017-08-01 15:04:36 -0500901 int rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600902
John Allenbd0b6722017-03-17 17:13:40 -0500903 do {
Thomas Falconeb110412018-05-24 14:37:53 -0500904 retry = false;
Thomas Falcondff515a32020-06-15 10:29:23 -0500905 if (retry_count > retries) {
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500906 netdev_warn(netdev, "Login attempts exceeded\n");
907 return -1;
908 }
909
910 adapter->init_done_rc = 0;
911 reinit_completion(&adapter->init_done);
912 rc = send_login(adapter);
Dany Maddenc98d9cc2020-11-25 18:04:30 -0600913 if (rc)
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500914 return rc;
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500915
916 if (!wait_for_completion_timeout(&adapter->init_done,
917 timeout)) {
Thomas Falcondff515a32020-06-15 10:29:23 -0500918 netdev_warn(netdev, "Login timed out, retrying...\n");
919 retry = true;
920 adapter->init_done_rc = 0;
921 retry_count++;
922 continue;
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500923 }
924
Thomas Falcondff515a32020-06-15 10:29:23 -0500925 if (adapter->init_done_rc == ABORTED) {
926 netdev_warn(netdev, "Login aborted, retrying...\n");
927 retry = true;
928 adapter->init_done_rc = 0;
929 retry_count++;
930 /* FW or device may be busy, so
931 * wait a bit before retrying login
932 */
933 msleep(500);
934 } else if (adapter->init_done_rc == PARTIALSUCCESS) {
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500935 retry_count++;
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -0600936 release_sub_crqs(adapter, 1);
John Allenbd0b6722017-03-17 17:13:40 -0500937
Thomas Falconeb110412018-05-24 14:37:53 -0500938 retry = true;
939 netdev_dbg(netdev,
940 "Received partial success, retrying...\n");
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500941 adapter->init_done_rc = 0;
John Allenbd0b6722017-03-17 17:13:40 -0500942 reinit_completion(&adapter->init_done);
Lijun Pan491099a2020-09-27 20:13:26 -0500943 send_query_cap(adapter);
John Allenbd0b6722017-03-17 17:13:40 -0500944 if (!wait_for_completion_timeout(&adapter->init_done,
945 timeout)) {
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500946 netdev_warn(netdev,
947 "Capabilities query timed out\n");
John Allenbd0b6722017-03-17 17:13:40 -0500948 return -1;
949 }
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500950
Thomas Falcon4d96f122017-08-01 15:04:36 -0500951 rc = init_sub_crqs(adapter);
952 if (rc) {
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500953 netdev_warn(netdev,
954 "SCRQ initialization failed\n");
Thomas Falcon4d96f122017-08-01 15:04:36 -0500955 return -1;
956 }
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500957
Thomas Falcon4d96f122017-08-01 15:04:36 -0500958 rc = init_sub_crq_irqs(adapter);
959 if (rc) {
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500960 netdev_warn(netdev,
961 "SCRQ irq initialization failed\n");
Thomas Falcon4d96f122017-08-01 15:04:36 -0500962 return -1;
963 }
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500964 } else if (adapter->init_done_rc) {
965 netdev_warn(netdev, "Adapter login failed\n");
John Allenbd0b6722017-03-17 17:13:40 -0500966 return -1;
967 }
Thomas Falconeb110412018-05-24 14:37:53 -0500968 } while (retry);
John Allenbd0b6722017-03-17 17:13:40 -0500969
Thomas Falcon62740e92019-05-09 23:13:43 -0500970 __ibmvnic_set_mac(netdev, adapter->mac_addr);
Thomas Falcon3d166132018-01-10 19:39:52 -0600971
Lijun Pan0666ef72021-04-12 02:41:28 -0500972 netdev_dbg(netdev, "[S:%s] Login succeeded\n", adapter_state_to_string(adapter->state));
John Allena57a5d22017-03-17 17:13:41 -0500973 return 0;
974}
975
Thomas Falcon34f0f4e2018-02-13 18:23:40 -0600976static void release_login_buffer(struct ibmvnic_adapter *adapter)
977{
978 kfree(adapter->login_buf);
979 adapter->login_buf = NULL;
980}
981
982static void release_login_rsp_buffer(struct ibmvnic_adapter *adapter)
983{
984 kfree(adapter->login_rsp_buf);
985 adapter->login_rsp_buf = NULL;
986}
987
Nathan Fontenot1b8955e2017-03-30 02:49:29 -0400988static void release_resources(struct ibmvnic_adapter *adapter)
989{
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -0200990 release_vpd_data(adapter);
991
Nathan Fontenot1b8955e2017-03-30 02:49:29 -0400992 release_tx_pools(adapter);
993 release_rx_pools(adapter);
994
Nathan Fontenot86f669b2018-02-19 13:30:39 -0600995 release_napi(adapter);
Lijun Pana0c8be52020-12-19 15:39:19 -0600996 release_login_buffer(adapter);
Thomas Falcon34f0f4e2018-02-13 18:23:40 -0600997 release_login_rsp_buffer(adapter);
Nathan Fontenot1b8955e2017-03-30 02:49:29 -0400998}
999
Nathan Fontenot53da09e2017-04-21 15:39:04 -04001000static int set_link_state(struct ibmvnic_adapter *adapter, u8 link_state)
1001{
1002 struct net_device *netdev = adapter->netdev;
Dany Madden98c41f02020-11-25 18:04:32 -06001003 unsigned long timeout = msecs_to_jiffies(20000);
Nathan Fontenot53da09e2017-04-21 15:39:04 -04001004 union ibmvnic_crq crq;
1005 bool resend;
1006 int rc;
1007
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001008 netdev_dbg(netdev, "setting link state %d\n", link_state);
1009
Nathan Fontenot53da09e2017-04-21 15:39:04 -04001010 memset(&crq, 0, sizeof(crq));
1011 crq.logical_link_state.first = IBMVNIC_CRQ_CMD;
1012 crq.logical_link_state.cmd = LOGICAL_LINK_STATE;
1013 crq.logical_link_state.link_state = link_state;
1014
1015 do {
1016 resend = false;
1017
1018 reinit_completion(&adapter->init_done);
1019 rc = ibmvnic_send_crq(adapter, &crq);
1020 if (rc) {
1021 netdev_err(netdev, "Failed to set link state\n");
1022 return rc;
1023 }
1024
1025 if (!wait_for_completion_timeout(&adapter->init_done,
1026 timeout)) {
1027 netdev_err(netdev, "timeout setting link state\n");
1028 return -1;
1029 }
1030
Lijun Pan4c5f6af2020-08-19 17:52:23 -05001031 if (adapter->init_done_rc == PARTIALSUCCESS) {
Nathan Fontenot53da09e2017-04-21 15:39:04 -04001032 /* Partuial success, delay and re-send */
1033 mdelay(1000);
1034 resend = true;
Thomas Falconab5ec332018-05-23 13:37:59 -05001035 } else if (adapter->init_done_rc) {
1036 netdev_warn(netdev, "Unable to set link state, rc=%d\n",
1037 adapter->init_done_rc);
1038 return adapter->init_done_rc;
Nathan Fontenot53da09e2017-04-21 15:39:04 -04001039 }
1040 } while (resend);
1041
1042 return 0;
1043}
1044
Thomas Falcon7f3c6e62017-04-21 15:38:40 -04001045static int set_real_num_queues(struct net_device *netdev)
1046{
1047 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1048 int rc;
1049
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001050 netdev_dbg(netdev, "Setting real tx/rx queues (%llx/%llx)\n",
1051 adapter->req_tx_queues, adapter->req_rx_queues);
1052
Thomas Falcon7f3c6e62017-04-21 15:38:40 -04001053 rc = netif_set_real_num_tx_queues(netdev, adapter->req_tx_queues);
1054 if (rc) {
1055 netdev_err(netdev, "failed to set the number of tx queues\n");
1056 return rc;
1057 }
1058
1059 rc = netif_set_real_num_rx_queues(netdev, adapter->req_rx_queues);
1060 if (rc)
1061 netdev_err(netdev, "failed to set the number of rx queues\n");
1062
1063 return rc;
1064}
1065
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001066static int ibmvnic_get_vpd(struct ibmvnic_adapter *adapter)
1067{
1068 struct device *dev = &adapter->vdev->dev;
1069 union ibmvnic_crq crq;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001070 int len = 0;
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05001071 int rc;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001072
1073 if (adapter->vpd->buff)
1074 len = adapter->vpd->len;
1075
Thomas Falconff25dcb2019-11-25 17:12:56 -06001076 mutex_lock(&adapter->fw_lock);
1077 adapter->fw_done_rc = 0;
Thomas Falcon070eca92019-11-25 17:12:53 -06001078 reinit_completion(&adapter->fw_done);
Thomas Falconff25dcb2019-11-25 17:12:56 -06001079
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001080 crq.get_vpd_size.first = IBMVNIC_CRQ_CMD;
1081 crq.get_vpd_size.cmd = GET_VPD_SIZE;
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05001082 rc = ibmvnic_send_crq(adapter, &crq);
Thomas Falconff25dcb2019-11-25 17:12:56 -06001083 if (rc) {
1084 mutex_unlock(&adapter->fw_lock);
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05001085 return rc;
Thomas Falconff25dcb2019-11-25 17:12:56 -06001086 }
Thomas Falcon476d96c2019-11-25 17:12:55 -06001087
1088 rc = ibmvnic_wait_for_completion(adapter, &adapter->fw_done, 10000);
1089 if (rc) {
1090 dev_err(dev, "Could not retrieve VPD size, rc = %d\n", rc);
Thomas Falconff25dcb2019-11-25 17:12:56 -06001091 mutex_unlock(&adapter->fw_lock);
Thomas Falcon476d96c2019-11-25 17:12:55 -06001092 return rc;
1093 }
Thomas Falconff25dcb2019-11-25 17:12:56 -06001094 mutex_unlock(&adapter->fw_lock);
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001095
1096 if (!adapter->vpd->len)
1097 return -ENODATA;
1098
1099 if (!adapter->vpd->buff)
1100 adapter->vpd->buff = kzalloc(adapter->vpd->len, GFP_KERNEL);
1101 else if (adapter->vpd->len != len)
1102 adapter->vpd->buff =
1103 krealloc(adapter->vpd->buff,
1104 adapter->vpd->len, GFP_KERNEL);
1105
1106 if (!adapter->vpd->buff) {
1107 dev_err(dev, "Could allocate VPD buffer\n");
1108 return -ENOMEM;
1109 }
1110
1111 adapter->vpd->dma_addr =
1112 dma_map_single(dev, adapter->vpd->buff, adapter->vpd->len,
1113 DMA_FROM_DEVICE);
Desnes Augusto Nunes do Rosariof7431062017-11-17 09:09:04 -02001114 if (dma_mapping_error(dev, adapter->vpd->dma_addr)) {
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001115 dev_err(dev, "Could not map VPD buffer\n");
1116 kfree(adapter->vpd->buff);
Thomas Falconb0992ec2018-02-06 17:25:23 -06001117 adapter->vpd->buff = NULL;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001118 return -ENOMEM;
1119 }
1120
Thomas Falconff25dcb2019-11-25 17:12:56 -06001121 mutex_lock(&adapter->fw_lock);
1122 adapter->fw_done_rc = 0;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001123 reinit_completion(&adapter->fw_done);
Thomas Falconff25dcb2019-11-25 17:12:56 -06001124
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001125 crq.get_vpd.first = IBMVNIC_CRQ_CMD;
1126 crq.get_vpd.cmd = GET_VPD;
1127 crq.get_vpd.ioba = cpu_to_be32(adapter->vpd->dma_addr);
1128 crq.get_vpd.len = cpu_to_be32((u32)adapter->vpd->len);
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05001129 rc = ibmvnic_send_crq(adapter, &crq);
1130 if (rc) {
1131 kfree(adapter->vpd->buff);
1132 adapter->vpd->buff = NULL;
Thomas Falconff25dcb2019-11-25 17:12:56 -06001133 mutex_unlock(&adapter->fw_lock);
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05001134 return rc;
1135 }
Thomas Falcon476d96c2019-11-25 17:12:55 -06001136
1137 rc = ibmvnic_wait_for_completion(adapter, &adapter->fw_done, 10000);
1138 if (rc) {
1139 dev_err(dev, "Unable to retrieve VPD, rc = %d\n", rc);
1140 kfree(adapter->vpd->buff);
1141 adapter->vpd->buff = NULL;
Thomas Falconff25dcb2019-11-25 17:12:56 -06001142 mutex_unlock(&adapter->fw_lock);
Thomas Falcon476d96c2019-11-25 17:12:55 -06001143 return rc;
1144 }
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001145
Thomas Falconff25dcb2019-11-25 17:12:56 -06001146 mutex_unlock(&adapter->fw_lock);
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001147 return 0;
1148}
1149
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001150static int init_resources(struct ibmvnic_adapter *adapter)
John Allena57a5d22017-03-17 17:13:41 -05001151{
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001152 struct net_device *netdev = adapter->netdev;
Nathan Fontenot86f669b2018-02-19 13:30:39 -06001153 int rc;
John Allena57a5d22017-03-17 17:13:41 -05001154
Thomas Falcon7f3c6e62017-04-21 15:38:40 -04001155 rc = set_real_num_queues(netdev);
1156 if (rc)
1157 return rc;
John Allenbd0b6722017-03-17 17:13:40 -05001158
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001159 adapter->vpd = kzalloc(sizeof(*adapter->vpd), GFP_KERNEL);
1160 if (!adapter->vpd)
1161 return -ENOMEM;
1162
John Allen69d08dc2018-01-18 16:27:58 -06001163 /* Vital Product Data (VPD) */
1164 rc = ibmvnic_get_vpd(adapter);
1165 if (rc) {
1166 netdev_err(netdev, "failed to initialize Vital Product Data (VPD)\n");
1167 return rc;
1168 }
1169
Thomas Falcon032c5e82015-12-21 11:26:06 -06001170 adapter->map_id = 1;
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001171
Nathan Fontenot86f669b2018-02-19 13:30:39 -06001172 rc = init_napi(adapter);
1173 if (rc)
1174 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001175
Lijun Pan69980d02020-09-27 20:13:28 -05001176 send_query_map(adapter);
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -04001177
1178 rc = init_rx_pools(netdev);
1179 if (rc)
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001180 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001181
Nathan Fontenotc657e322017-03-30 02:49:06 -04001182 rc = init_tx_pools(netdev);
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001183 return rc;
1184}
1185
Nathan Fontenoted651a12017-05-03 14:04:38 -04001186static int __ibmvnic_open(struct net_device *netdev)
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001187{
1188 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001189 enum vnic_state prev_state = adapter->state;
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001190 int i, rc;
1191
Nathan Fontenot90c80142017-05-03 14:04:32 -04001192 adapter->state = VNIC_OPENING;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001193 replenish_pools(adapter);
John Allend944c3d62017-05-26 10:30:13 -04001194 ibmvnic_napi_enable(adapter);
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001195
Thomas Falcon032c5e82015-12-21 11:26:06 -06001196 /* We're ready to receive frames, enable the sub-crq interrupts and
1197 * set the logical link state to up
1198 */
Nathan Fontenoted651a12017-05-03 14:04:38 -04001199 for (i = 0; i < adapter->req_rx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001200 netdev_dbg(netdev, "Enabling rx_scrq[%d] irq\n", i);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001201 if (prev_state == VNIC_CLOSED)
1202 enable_irq(adapter->rx_scrq[i]->irq);
Thomas Falconf23e0642018-04-15 18:53:36 -05001203 enable_scrq_irq(adapter, adapter->rx_scrq[i]);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001204 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001205
Nathan Fontenoted651a12017-05-03 14:04:38 -04001206 for (i = 0; i < adapter->req_tx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001207 netdev_dbg(netdev, "Enabling tx_scrq[%d] irq\n", i);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001208 if (prev_state == VNIC_CLOSED)
1209 enable_irq(adapter->tx_scrq[i]->irq);
Thomas Falconf23e0642018-04-15 18:53:36 -05001210 enable_scrq_irq(adapter, adapter->tx_scrq[i]);
Thomas Falcon0d973382020-11-18 19:12:19 -06001211 netdev_tx_reset_queue(netdev_get_tx_queue(netdev, i));
Nathan Fontenoted651a12017-05-03 14:04:38 -04001212 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001213
Nathan Fontenot53da09e2017-04-21 15:39:04 -04001214 rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_UP);
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001215 if (rc) {
Lijun Pan0775ebc2021-04-14 02:46:14 -05001216 ibmvnic_napi_disable(adapter);
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001217 release_resources(adapter);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001218 return rc;
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001219 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001220
Nathan Fontenoted651a12017-05-03 14:04:38 -04001221 netif_tx_start_all_queues(netdev);
1222
Dany Madden2ca220f2021-06-23 21:13:11 -07001223 if (prev_state == VNIC_CLOSED) {
1224 for (i = 0; i < adapter->req_rx_queues; i++)
1225 napi_schedule(&adapter->napi[i]);
1226 }
1227
Nathan Fontenoted651a12017-05-03 14:04:38 -04001228 adapter->state = VNIC_OPEN;
1229 return rc;
1230}
1231
1232static int ibmvnic_open(struct net_device *netdev)
1233{
1234 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
John Allen69d08dc2018-01-18 16:27:58 -06001235 int rc;
Nathan Fontenoted651a12017-05-03 14:04:38 -04001236
Sukadev Bhattiprolu8f1c0fd2021-02-23 21:02:29 -08001237 ASSERT_RTNL();
1238
1239 /* If device failover is pending or we are about to reset, just set
1240 * device state and return. Device operation will be handled by reset
1241 * routine.
1242 *
1243 * It should be safe to overwrite the adapter->state here. Since
1244 * we hold the rtnl, either the reset has not actually started or
1245 * the rtnl got dropped during the set_link_state() in do_reset().
1246 * In the former case, no one else is changing the state (again we
1247 * have the rtnl) and in the latter case, do_reset() will detect and
1248 * honor our setting below.
Thomas Falcon5a18e1e2018-04-06 18:37:05 -05001249 */
Sukadev Bhattiprolu8f1c0fd2021-02-23 21:02:29 -08001250 if (adapter->failover_pending || (test_bit(0, &adapter->resetting))) {
Lijun Pan0666ef72021-04-12 02:41:28 -05001251 netdev_dbg(netdev, "[S:%s FOP:%d] Resetting, deferring open\n",
1252 adapter_state_to_string(adapter->state),
1253 adapter->failover_pending);
Thomas Falcon5a18e1e2018-04-06 18:37:05 -05001254 adapter->state = VNIC_OPEN;
Sukadev Bhattiprolu8f1c0fd2021-02-23 21:02:29 -08001255 rc = 0;
1256 goto out;
Thomas Falcon5a18e1e2018-04-06 18:37:05 -05001257 }
1258
Nathan Fontenoted651a12017-05-03 14:04:38 -04001259 if (adapter->state != VNIC_CLOSED) {
1260 rc = ibmvnic_login(netdev);
Juliet Kima5681e22018-11-19 15:59:22 -06001261 if (rc)
Sukadev Bhattiprolu1d850492020-10-30 10:07:11 -07001262 goto out;
Nathan Fontenoted651a12017-05-03 14:04:38 -04001263
1264 rc = init_resources(adapter);
1265 if (rc) {
1266 netdev_err(netdev, "failed to initialize resources\n");
1267 release_resources(adapter);
Sukadev Bhattiprolu1d850492020-10-30 10:07:11 -07001268 goto out;
Nathan Fontenoted651a12017-05-03 14:04:38 -04001269 }
1270 }
1271
1272 rc = __ibmvnic_open(netdev);
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001273
Sukadev Bhattiprolu1d850492020-10-30 10:07:11 -07001274out:
Sukadev Bhattiprolu8f1c0fd2021-02-23 21:02:29 -08001275 /* If open failed and there is a pending failover or in-progress reset,
1276 * set device state and return. Device operation will be handled by
1277 * reset routine. See also comments above regarding rtnl.
Sukadev Bhattiprolu1d850492020-10-30 10:07:11 -07001278 */
Sukadev Bhattiprolu8f1c0fd2021-02-23 21:02:29 -08001279 if (rc &&
1280 (adapter->failover_pending || (test_bit(0, &adapter->resetting)))) {
Sukadev Bhattiprolu1d850492020-10-30 10:07:11 -07001281 adapter->state = VNIC_OPEN;
1282 rc = 0;
1283 }
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001284 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001285}
1286
Thomas Falcond0869c02018-02-13 18:23:43 -06001287static void clean_rx_pools(struct ibmvnic_adapter *adapter)
1288{
1289 struct ibmvnic_rx_pool *rx_pool;
Thomas Falcon637f81d2018-02-26 18:10:57 -06001290 struct ibmvnic_rx_buff *rx_buff;
Thomas Falcond0869c02018-02-13 18:23:43 -06001291 u64 rx_entries;
1292 int rx_scrqs;
1293 int i, j;
1294
1295 if (!adapter->rx_pool)
1296 return;
1297
Thomas Falcon660e3092018-04-20 14:25:32 -05001298 rx_scrqs = adapter->num_active_rx_pools;
Thomas Falcond0869c02018-02-13 18:23:43 -06001299 rx_entries = adapter->req_rx_add_entries_per_subcrq;
1300
1301 /* Free any remaining skbs in the rx buffer pools */
1302 for (i = 0; i < rx_scrqs; i++) {
1303 rx_pool = &adapter->rx_pool[i];
Thomas Falcon637f81d2018-02-26 18:10:57 -06001304 if (!rx_pool || !rx_pool->rx_buff)
Thomas Falcond0869c02018-02-13 18:23:43 -06001305 continue;
1306
1307 netdev_dbg(adapter->netdev, "Cleaning rx_pool[%d]\n", i);
1308 for (j = 0; j < rx_entries; j++) {
Thomas Falcon637f81d2018-02-26 18:10:57 -06001309 rx_buff = &rx_pool->rx_buff[j];
1310 if (rx_buff && rx_buff->skb) {
1311 dev_kfree_skb_any(rx_buff->skb);
1312 rx_buff->skb = NULL;
Thomas Falcond0869c02018-02-13 18:23:43 -06001313 }
1314 }
1315 }
1316}
1317
Thomas Falcone9e1e972018-03-16 20:00:30 -05001318static void clean_one_tx_pool(struct ibmvnic_adapter *adapter,
1319 struct ibmvnic_tx_pool *tx_pool)
Nathan Fontenotb41b83e2017-05-03 14:04:56 -04001320{
Thomas Falcon637f81d2018-02-26 18:10:57 -06001321 struct ibmvnic_tx_buff *tx_buff;
Nathan Fontenotb41b83e2017-05-03 14:04:56 -04001322 u64 tx_entries;
Thomas Falcone9e1e972018-03-16 20:00:30 -05001323 int i;
Nathan Fontenotb41b83e2017-05-03 14:04:56 -04001324
Dan Carpenter050e85c2018-03-23 14:36:15 +03001325 if (!tx_pool || !tx_pool->tx_buff)
Thomas Falcone9e1e972018-03-16 20:00:30 -05001326 return;
1327
1328 tx_entries = tx_pool->num_buffers;
1329
1330 for (i = 0; i < tx_entries; i++) {
1331 tx_buff = &tx_pool->tx_buff[i];
1332 if (tx_buff && tx_buff->skb) {
1333 dev_kfree_skb_any(tx_buff->skb);
1334 tx_buff->skb = NULL;
1335 }
1336 }
1337}
1338
1339static void clean_tx_pools(struct ibmvnic_adapter *adapter)
1340{
1341 int tx_scrqs;
1342 int i;
1343
1344 if (!adapter->tx_pool || !adapter->tso_pool)
Nathan Fontenotb41b83e2017-05-03 14:04:56 -04001345 return;
1346
Thomas Falcon660e3092018-04-20 14:25:32 -05001347 tx_scrqs = adapter->num_active_tx_pools;
Nathan Fontenotb41b83e2017-05-03 14:04:56 -04001348
1349 /* Free any remaining skbs in the tx buffer pools */
1350 for (i = 0; i < tx_scrqs; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001351 netdev_dbg(adapter->netdev, "Cleaning tx_pool[%d]\n", i);
Thomas Falcone9e1e972018-03-16 20:00:30 -05001352 clean_one_tx_pool(adapter, &adapter->tx_pool[i]);
1353 clean_one_tx_pool(adapter, &adapter->tso_pool[i]);
Nathan Fontenotb41b83e2017-05-03 14:04:56 -04001354 }
1355}
1356
John Allen6095e592018-03-30 13:44:21 -05001357static void ibmvnic_disable_irqs(struct ibmvnic_adapter *adapter)
John Allenea5509f2017-03-17 17:13:43 -05001358{
John Allen6095e592018-03-30 13:44:21 -05001359 struct net_device *netdev = adapter->netdev;
John Allenea5509f2017-03-17 17:13:43 -05001360 int i;
1361
Nathan Fontenot46293b92017-05-03 14:05:02 -04001362 if (adapter->tx_scrq) {
1363 for (i = 0; i < adapter->req_tx_queues; i++)
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001364 if (adapter->tx_scrq[i]->irq) {
Thomas Falconf8738662018-03-07 17:51:45 -06001365 netdev_dbg(netdev,
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001366 "Disabling tx_scrq[%d] irq\n", i);
Thomas Falconf23e0642018-04-15 18:53:36 -05001367 disable_scrq_irq(adapter, adapter->tx_scrq[i]);
Nathan Fontenot46293b92017-05-03 14:05:02 -04001368 disable_irq(adapter->tx_scrq[i]->irq);
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001369 }
Nathan Fontenot46293b92017-05-03 14:05:02 -04001370 }
1371
Nathan Fontenot46293b92017-05-03 14:05:02 -04001372 if (adapter->rx_scrq) {
1373 for (i = 0; i < adapter->req_rx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001374 if (adapter->rx_scrq[i]->irq) {
Thomas Falconf8738662018-03-07 17:51:45 -06001375 netdev_dbg(netdev,
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001376 "Disabling rx_scrq[%d] irq\n", i);
Thomas Falconf23e0642018-04-15 18:53:36 -05001377 disable_scrq_irq(adapter, adapter->rx_scrq[i]);
Nathan Fontenot46293b92017-05-03 14:05:02 -04001378 disable_irq(adapter->rx_scrq[i]->irq);
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001379 }
Nathan Fontenot46293b92017-05-03 14:05:02 -04001380 }
1381 }
John Allen6095e592018-03-30 13:44:21 -05001382}
1383
1384static void ibmvnic_cleanup(struct net_device *netdev)
1385{
1386 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1387
1388 /* ensure that transmissions are stopped if called by do_reset */
Juliet Kim7ed5b312019-09-20 16:11:23 -04001389 if (test_bit(0, &adapter->resetting))
John Allen6095e592018-03-30 13:44:21 -05001390 netif_tx_disable(netdev);
1391 else
1392 netif_tx_stop_all_queues(netdev);
1393
1394 ibmvnic_napi_disable(adapter);
1395 ibmvnic_disable_irqs(adapter);
1396
Thomas Falcond0869c02018-02-13 18:23:43 -06001397 clean_rx_pools(adapter);
Thomas Falcon10f76212017-05-26 10:30:31 -04001398 clean_tx_pools(adapter);
Thomas Falcon01d9bd72018-03-07 17:51:46 -06001399}
1400
1401static int __ibmvnic_close(struct net_device *netdev)
1402{
1403 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1404 int rc = 0;
1405
1406 adapter->state = VNIC_CLOSING;
1407 rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_DN);
Nathan Fontenot90c80142017-05-03 14:04:32 -04001408 adapter->state = VNIC_CLOSED;
Sukadev Bhattiprolud4083d32021-02-10 17:41:43 -08001409 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001410}
1411
Nathan Fontenoted651a12017-05-03 14:04:38 -04001412static int ibmvnic_close(struct net_device *netdev)
1413{
1414 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1415 int rc;
1416
Lijun Pan0666ef72021-04-12 02:41:28 -05001417 netdev_dbg(netdev, "[S:%s FOP:%d FRR:%d] Closing\n",
1418 adapter_state_to_string(adapter->state),
1419 adapter->failover_pending,
Sukadev Bhattiprolu38bd5ce2020-12-04 18:22:35 -08001420 adapter->force_reset_recovery);
1421
Thomas Falcon5a18e1e2018-04-06 18:37:05 -05001422 /* If device failover is pending, just set device state and return.
1423 * Device operation will be handled by reset routine.
1424 */
1425 if (adapter->failover_pending) {
1426 adapter->state = VNIC_CLOSED;
1427 return 0;
1428 }
1429
Nathan Fontenoted651a12017-05-03 14:04:38 -04001430 rc = __ibmvnic_close(netdev);
Nathan Fontenot30f79622018-04-06 18:37:06 -05001431 ibmvnic_cleanup(netdev);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001432
1433 return rc;
1434}
1435
Thomas Falconad7775d2016-04-01 17:20:34 -05001436/**
1437 * build_hdr_data - creates L2/L3/L4 header data buffer
Lee Jones80708602021-01-15 20:09:03 +00001438 * @hdr_field: bitfield determining needed headers
1439 * @skb: socket buffer
1440 * @hdr_len: array of header lengths
1441 * @hdr_data: buffer to write the header to
Thomas Falconad7775d2016-04-01 17:20:34 -05001442 *
1443 * Reads hdr_field to determine which headers are needed by firmware.
1444 * Builds a buffer containing these headers. Saves individual header
1445 * lengths and total buffer length to be used to build descriptors.
1446 */
1447static int build_hdr_data(u8 hdr_field, struct sk_buff *skb,
1448 int *hdr_len, u8 *hdr_data)
1449{
1450 int len = 0;
1451 u8 *hdr;
1452
Thomas Falconda75e3b2018-03-12 11:51:02 -05001453 if (skb_vlan_tagged(skb) && !skb_vlan_tag_present(skb))
1454 hdr_len[0] = sizeof(struct vlan_ethhdr);
1455 else
1456 hdr_len[0] = sizeof(struct ethhdr);
Thomas Falconad7775d2016-04-01 17:20:34 -05001457
1458 if (skb->protocol == htons(ETH_P_IP)) {
1459 hdr_len[1] = ip_hdr(skb)->ihl * 4;
1460 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
1461 hdr_len[2] = tcp_hdrlen(skb);
1462 else if (ip_hdr(skb)->protocol == IPPROTO_UDP)
1463 hdr_len[2] = sizeof(struct udphdr);
1464 } else if (skb->protocol == htons(ETH_P_IPV6)) {
1465 hdr_len[1] = sizeof(struct ipv6hdr);
1466 if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
1467 hdr_len[2] = tcp_hdrlen(skb);
1468 else if (ipv6_hdr(skb)->nexthdr == IPPROTO_UDP)
1469 hdr_len[2] = sizeof(struct udphdr);
Thomas Falcon4eb50ce2017-12-18 12:52:40 -06001470 } else if (skb->protocol == htons(ETH_P_ARP)) {
1471 hdr_len[1] = arp_hdr_len(skb->dev);
1472 hdr_len[2] = 0;
Thomas Falconad7775d2016-04-01 17:20:34 -05001473 }
1474
1475 memset(hdr_data, 0, 120);
1476 if ((hdr_field >> 6) & 1) {
1477 hdr = skb_mac_header(skb);
1478 memcpy(hdr_data, hdr, hdr_len[0]);
1479 len += hdr_len[0];
1480 }
1481
1482 if ((hdr_field >> 5) & 1) {
1483 hdr = skb_network_header(skb);
1484 memcpy(hdr_data + len, hdr, hdr_len[1]);
1485 len += hdr_len[1];
1486 }
1487
1488 if ((hdr_field >> 4) & 1) {
1489 hdr = skb_transport_header(skb);
1490 memcpy(hdr_data + len, hdr, hdr_len[2]);
1491 len += hdr_len[2];
1492 }
1493 return len;
1494}
1495
1496/**
1497 * create_hdr_descs - create header and header extension descriptors
Lee Jones80708602021-01-15 20:09:03 +00001498 * @hdr_field: bitfield determining needed headers
1499 * @hdr_data: buffer containing header data
1500 * @len: length of data buffer
1501 * @hdr_len: array of individual header lengths
1502 * @scrq_arr: descriptor array
Thomas Falconad7775d2016-04-01 17:20:34 -05001503 *
1504 * Creates header and, if needed, header extension descriptors and
1505 * places them in a descriptor array, scrq_arr
1506 */
1507
Thomas Falcon2de09682017-10-16 10:02:11 -05001508static int create_hdr_descs(u8 hdr_field, u8 *hdr_data, int len, int *hdr_len,
1509 union sub_crq *scrq_arr)
Thomas Falconad7775d2016-04-01 17:20:34 -05001510{
1511 union sub_crq hdr_desc;
1512 int tmp_len = len;
Thomas Falcon2de09682017-10-16 10:02:11 -05001513 int num_descs = 0;
Thomas Falconad7775d2016-04-01 17:20:34 -05001514 u8 *data, *cur;
1515 int tmp;
1516
1517 while (tmp_len > 0) {
1518 cur = hdr_data + len - tmp_len;
1519
1520 memset(&hdr_desc, 0, sizeof(hdr_desc));
1521 if (cur != hdr_data) {
1522 data = hdr_desc.hdr_ext.data;
1523 tmp = tmp_len > 29 ? 29 : tmp_len;
1524 hdr_desc.hdr_ext.first = IBMVNIC_CRQ_CMD;
1525 hdr_desc.hdr_ext.type = IBMVNIC_HDR_EXT_DESC;
1526 hdr_desc.hdr_ext.len = tmp;
1527 } else {
1528 data = hdr_desc.hdr.data;
1529 tmp = tmp_len > 24 ? 24 : tmp_len;
1530 hdr_desc.hdr.first = IBMVNIC_CRQ_CMD;
1531 hdr_desc.hdr.type = IBMVNIC_HDR_DESC;
1532 hdr_desc.hdr.len = tmp;
1533 hdr_desc.hdr.l2_len = (u8)hdr_len[0];
1534 hdr_desc.hdr.l3_len = cpu_to_be16((u16)hdr_len[1]);
1535 hdr_desc.hdr.l4_len = (u8)hdr_len[2];
1536 hdr_desc.hdr.flag = hdr_field << 1;
1537 }
1538 memcpy(data, cur, tmp);
1539 tmp_len -= tmp;
1540 *scrq_arr = hdr_desc;
1541 scrq_arr++;
Thomas Falcon2de09682017-10-16 10:02:11 -05001542 num_descs++;
Thomas Falconad7775d2016-04-01 17:20:34 -05001543 }
Thomas Falcon2de09682017-10-16 10:02:11 -05001544
1545 return num_descs;
Thomas Falconad7775d2016-04-01 17:20:34 -05001546}
1547
1548/**
1549 * build_hdr_descs_arr - build a header descriptor array
Lee Jones80708602021-01-15 20:09:03 +00001550 * @txbuff: tx buffer
1551 * @num_entries: number of descriptors to be sent
1552 * @hdr_field: bit field determining which headers will be sent
Thomas Falconad7775d2016-04-01 17:20:34 -05001553 *
1554 * This function will build a TX descriptor array with applicable
1555 * L2/L3/L4 packet header descriptors to be sent by send_subcrq_indirect.
1556 */
1557
Thomas Falconc62aa372020-11-18 19:12:20 -06001558static void build_hdr_descs_arr(struct sk_buff *skb,
1559 union sub_crq *indir_arr,
Thomas Falconad7775d2016-04-01 17:20:34 -05001560 int *num_entries, u8 hdr_field)
1561{
1562 int hdr_len[3] = {0, 0, 0};
Thomas Falconc62aa372020-11-18 19:12:20 -06001563 u8 hdr_data[140] = {0};
Thomas Falcon2de09682017-10-16 10:02:11 -05001564 int tot_len;
Thomas Falconad7775d2016-04-01 17:20:34 -05001565
Thomas Falconc62aa372020-11-18 19:12:20 -06001566 tot_len = build_hdr_data(hdr_field, skb, hdr_len,
1567 hdr_data);
Thomas Falcon2de09682017-10-16 10:02:11 -05001568 *num_entries += create_hdr_descs(hdr_field, hdr_data, tot_len, hdr_len,
Thomas Falconc62aa372020-11-18 19:12:20 -06001569 indir_arr + 1);
Thomas Falconad7775d2016-04-01 17:20:34 -05001570}
1571
Thomas Falcon1f247a62018-03-12 11:51:04 -05001572static int ibmvnic_xmit_workarounds(struct sk_buff *skb,
1573 struct net_device *netdev)
1574{
1575 /* For some backing devices, mishandling of small packets
1576 * can result in a loss of connection or TX stall. Device
1577 * architects recommend that no packet should be smaller
1578 * than the minimum MTU value provided to the driver, so
1579 * pad any packets to that length
1580 */
1581 if (skb->len < netdev->min_mtu)
1582 return skb_put_padto(skb, netdev->min_mtu);
Thomas Falcon7083a452018-03-12 21:05:26 -05001583
1584 return 0;
Thomas Falcon1f247a62018-03-12 11:51:04 -05001585}
1586
Thomas Falcon0d973382020-11-18 19:12:19 -06001587static void ibmvnic_tx_scrq_clean_buffer(struct ibmvnic_adapter *adapter,
1588 struct ibmvnic_sub_crq_queue *tx_scrq)
1589{
1590 struct ibmvnic_ind_xmit_queue *ind_bufp;
1591 struct ibmvnic_tx_buff *tx_buff;
1592 struct ibmvnic_tx_pool *tx_pool;
1593 union sub_crq tx_scrq_entry;
1594 int queue_num;
1595 int entries;
1596 int index;
1597 int i;
1598
1599 ind_bufp = &tx_scrq->ind_buf;
1600 entries = (u64)ind_bufp->index;
1601 queue_num = tx_scrq->pool_index;
1602
1603 for (i = entries - 1; i >= 0; --i) {
1604 tx_scrq_entry = ind_bufp->indir_arr[i];
1605 if (tx_scrq_entry.v1.type != IBMVNIC_TX_DESC)
1606 continue;
1607 index = be32_to_cpu(tx_scrq_entry.v1.correlator);
1608 if (index & IBMVNIC_TSO_POOL_MASK) {
1609 tx_pool = &adapter->tso_pool[queue_num];
1610 index &= ~IBMVNIC_TSO_POOL_MASK;
1611 } else {
1612 tx_pool = &adapter->tx_pool[queue_num];
1613 }
1614 tx_pool->free_map[tx_pool->consumer_index] = index;
1615 tx_pool->consumer_index = tx_pool->consumer_index == 0 ?
1616 tx_pool->num_buffers - 1 :
1617 tx_pool->consumer_index - 1;
1618 tx_buff = &tx_pool->tx_buff[index];
1619 adapter->netdev->stats.tx_packets--;
1620 adapter->netdev->stats.tx_bytes -= tx_buff->skb->len;
1621 adapter->tx_stats_buffers[queue_num].packets--;
1622 adapter->tx_stats_buffers[queue_num].bytes -=
1623 tx_buff->skb->len;
1624 dev_kfree_skb_any(tx_buff->skb);
1625 tx_buff->skb = NULL;
1626 adapter->netdev->stats.tx_dropped++;
1627 }
1628 ind_bufp->index = 0;
1629 if (atomic_sub_return(entries, &tx_scrq->used) <=
1630 (adapter->req_tx_entries_per_subcrq / 2) &&
Sukadev Bhattiprolu65d64702021-06-23 21:13:12 -07001631 __netif_subqueue_stopped(adapter->netdev, queue_num) &&
1632 !test_bit(0, &adapter->resetting)) {
Thomas Falcon0d973382020-11-18 19:12:19 -06001633 netif_wake_subqueue(adapter->netdev, queue_num);
1634 netdev_dbg(adapter->netdev, "Started queue %d\n",
1635 queue_num);
1636 }
1637}
1638
1639static int ibmvnic_tx_scrq_flush(struct ibmvnic_adapter *adapter,
1640 struct ibmvnic_sub_crq_queue *tx_scrq)
1641{
1642 struct ibmvnic_ind_xmit_queue *ind_bufp;
1643 u64 dma_addr;
1644 u64 entries;
1645 u64 handle;
1646 int rc;
1647
1648 ind_bufp = &tx_scrq->ind_buf;
1649 dma_addr = (u64)ind_bufp->indir_dma;
1650 entries = (u64)ind_bufp->index;
1651 handle = tx_scrq->handle;
1652
1653 if (!entries)
1654 return 0;
1655 rc = send_subcrq_indirect(adapter, handle, dma_addr, entries);
1656 if (rc)
1657 ibmvnic_tx_scrq_clean_buffer(adapter, tx_scrq);
1658 else
1659 ind_bufp->index = 0;
1660 return 0;
1661}
1662
YueHaibing94b2bb22018-09-18 14:35:47 +08001663static netdev_tx_t ibmvnic_xmit(struct sk_buff *skb, struct net_device *netdev)
Thomas Falcon032c5e82015-12-21 11:26:06 -06001664{
1665 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1666 int queue_num = skb_get_queue_mapping(skb);
Thomas Falconad7775d2016-04-01 17:20:34 -05001667 u8 *hdrs = (u8 *)&adapter->tx_rx_desc_req;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001668 struct device *dev = &adapter->vdev->dev;
Thomas Falcon0d973382020-11-18 19:12:19 -06001669 struct ibmvnic_ind_xmit_queue *ind_bufp;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001670 struct ibmvnic_tx_buff *tx_buff = NULL;
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001671 struct ibmvnic_sub_crq_queue *tx_scrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001672 struct ibmvnic_tx_pool *tx_pool;
1673 unsigned int tx_send_failed = 0;
Thomas Falcon0d973382020-11-18 19:12:19 -06001674 netdev_tx_t ret = NETDEV_TX_OK;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001675 unsigned int tx_map_failed = 0;
Thomas Falconc62aa372020-11-18 19:12:20 -06001676 union sub_crq indir_arr[16];
Thomas Falcon032c5e82015-12-21 11:26:06 -06001677 unsigned int tx_dropped = 0;
1678 unsigned int tx_packets = 0;
1679 unsigned int tx_bytes = 0;
1680 dma_addr_t data_dma_addr;
1681 struct netdev_queue *txq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001682 unsigned long lpar_rc;
1683 union sub_crq tx_crq;
1684 unsigned int offset;
Thomas Falconad7775d2016-04-01 17:20:34 -05001685 int num_entries = 1;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001686 unsigned char *dst;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001687 int index = 0;
Thomas Falcona0dca102018-01-18 19:29:48 -06001688 u8 proto = 0;
Thomas Falcon0d973382020-11-18 19:12:19 -06001689
1690 tx_scrq = adapter->tx_scrq[queue_num];
1691 txq = netdev_get_tx_queue(netdev, queue_num);
1692 ind_bufp = &tx_scrq->ind_buf;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001693
Juliet Kim7ed5b312019-09-20 16:11:23 -04001694 if (test_bit(0, &adapter->resetting)) {
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001695 if (!netif_subqueue_stopped(netdev, skb))
1696 netif_stop_subqueue(netdev, queue_num);
1697 dev_kfree_skb_any(skb);
1698
Thomas Falcon032c5e82015-12-21 11:26:06 -06001699 tx_send_failed++;
1700 tx_dropped++;
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001701 ret = NETDEV_TX_OK;
Thomas Falcon0d973382020-11-18 19:12:19 -06001702 ibmvnic_tx_scrq_flush(adapter, tx_scrq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001703 goto out;
1704 }
1705
Thomas Falcon7083a452018-03-12 21:05:26 -05001706 if (ibmvnic_xmit_workarounds(skb, netdev)) {
Thomas Falcon1f247a62018-03-12 11:51:04 -05001707 tx_dropped++;
1708 tx_send_failed++;
1709 ret = NETDEV_TX_OK;
Thomas Falcon0d973382020-11-18 19:12:19 -06001710 ibmvnic_tx_scrq_flush(adapter, tx_scrq);
Thomas Falcon1f247a62018-03-12 11:51:04 -05001711 goto out;
1712 }
Thomas Falcon06b3e352018-03-16 20:00:28 -05001713 if (skb_is_gso(skb))
1714 tx_pool = &adapter->tso_pool[queue_num];
1715 else
1716 tx_pool = &adapter->tx_pool[queue_num];
Thomas Falcon1f247a62018-03-12 11:51:04 -05001717
Thomas Falcon032c5e82015-12-21 11:26:06 -06001718 index = tx_pool->free_map[tx_pool->consumer_index];
Thomas Falconfdb06102017-10-17 12:36:55 -05001719
Thomas Falcon86b61a52018-03-16 20:00:29 -05001720 if (index == IBMVNIC_INVALID_MAP) {
1721 dev_kfree_skb_any(skb);
1722 tx_send_failed++;
1723 tx_dropped++;
1724 ret = NETDEV_TX_OK;
1725 goto out;
1726 }
1727
1728 tx_pool->free_map[tx_pool->consumer_index] = IBMVNIC_INVALID_MAP;
1729
Thomas Falcon06b3e352018-03-16 20:00:28 -05001730 offset = index * tx_pool->buf_size;
1731 dst = tx_pool->long_term_buff.buff + offset;
1732 memset(dst, 0, tx_pool->buf_size);
1733 data_dma_addr = tx_pool->long_term_buff.addr + offset;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001734
Thomas Falcon15482052017-10-17 12:36:54 -05001735 if (skb_shinfo(skb)->nr_frags) {
1736 int cur, i;
1737
1738 /* Copy the head */
1739 skb_copy_from_linear_data(skb, dst, skb_headlen(skb));
1740 cur = skb_headlen(skb);
1741
1742 /* Copy the frags */
1743 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1744 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1745
Christophe JAILLETc3105f82021-04-04 10:54:37 +02001746 memcpy(dst + cur, skb_frag_address(frag),
1747 skb_frag_size(frag));
Thomas Falcon15482052017-10-17 12:36:54 -05001748 cur += skb_frag_size(frag);
1749 }
1750 } else {
1751 skb_copy_from_linear_data(skb, dst, skb->len);
1752 }
1753
Lijun Pan42557da2021-02-12 20:48:40 -06001754 /* post changes to long_term_buff *dst before VIOS accessing it */
1755 dma_wmb();
1756
Thomas Falcon032c5e82015-12-21 11:26:06 -06001757 tx_pool->consumer_index =
Thomas Falcon06b3e352018-03-16 20:00:28 -05001758 (tx_pool->consumer_index + 1) % tx_pool->num_buffers;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001759
1760 tx_buff = &tx_pool->tx_buff[index];
1761 tx_buff->skb = skb;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001762 tx_buff->index = index;
1763 tx_buff->pool_index = queue_num;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001764
1765 memset(&tx_crq, 0, sizeof(tx_crq));
1766 tx_crq.v1.first = IBMVNIC_CRQ_CMD;
1767 tx_crq.v1.type = IBMVNIC_TX_DESC;
1768 tx_crq.v1.n_crq_elem = 1;
1769 tx_crq.v1.n_sge = 1;
1770 tx_crq.v1.flags1 = IBMVNIC_TX_COMP_NEEDED;
Thomas Falcon06b3e352018-03-16 20:00:28 -05001771
Thomas Falconfdb06102017-10-17 12:36:55 -05001772 if (skb_is_gso(skb))
Thomas Falcon06b3e352018-03-16 20:00:28 -05001773 tx_crq.v1.correlator =
1774 cpu_to_be32(index | IBMVNIC_TSO_POOL_MASK);
Thomas Falconfdb06102017-10-17 12:36:55 -05001775 else
Thomas Falcon06b3e352018-03-16 20:00:28 -05001776 tx_crq.v1.correlator = cpu_to_be32(index);
1777 tx_crq.v1.dma_reg = cpu_to_be16(tx_pool->long_term_buff.map_id);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001778 tx_crq.v1.sge_len = cpu_to_be32(skb->len);
1779 tx_crq.v1.ioba = cpu_to_be64(data_dma_addr);
1780
Michał Mirosławe84b4792018-11-07 17:50:52 +01001781 if (adapter->vlan_header_insertion && skb_vlan_tag_present(skb)) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06001782 tx_crq.v1.flags2 |= IBMVNIC_TX_VLAN_INSERT;
1783 tx_crq.v1.vlan_id = cpu_to_be16(skb->vlan_tci);
1784 }
1785
1786 if (skb->protocol == htons(ETH_P_IP)) {
Thomas Falcona0dca102018-01-18 19:29:48 -06001787 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV4;
1788 proto = ip_hdr(skb)->protocol;
1789 } else if (skb->protocol == htons(ETH_P_IPV6)) {
1790 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV6;
1791 proto = ipv6_hdr(skb)->nexthdr;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001792 }
1793
Thomas Falcona0dca102018-01-18 19:29:48 -06001794 if (proto == IPPROTO_TCP)
1795 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_TCP;
1796 else if (proto == IPPROTO_UDP)
1797 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_UDP;
1798
Thomas Falconad7775d2016-04-01 17:20:34 -05001799 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06001800 tx_crq.v1.flags1 |= IBMVNIC_TX_CHKSUM_OFFLOAD;
Thomas Falconad7775d2016-04-01 17:20:34 -05001801 hdrs += 2;
1802 }
Thomas Falconfdb06102017-10-17 12:36:55 -05001803 if (skb_is_gso(skb)) {
1804 tx_crq.v1.flags1 |= IBMVNIC_TX_LSO;
1805 tx_crq.v1.mss = cpu_to_be16(skb_shinfo(skb)->gso_size);
1806 hdrs += 2;
1807 }
Thomas Falcon0d973382020-11-18 19:12:19 -06001808
1809 if ((*hdrs >> 7) & 1)
Thomas Falconc62aa372020-11-18 19:12:20 -06001810 build_hdr_descs_arr(skb, indir_arr, &num_entries, *hdrs);
Thomas Falcon0d973382020-11-18 19:12:19 -06001811
1812 tx_crq.v1.n_crq_elem = num_entries;
1813 tx_buff->num_entries = num_entries;
1814 /* flush buffer if current entry can not fit */
1815 if (num_entries + ind_bufp->index > IBMVNIC_MAX_IND_DESCS) {
1816 lpar_rc = ibmvnic_tx_scrq_flush(adapter, tx_scrq);
1817 if (lpar_rc != H_SUCCESS)
1818 goto tx_flush_err;
Thomas Falconad7775d2016-04-01 17:20:34 -05001819 }
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001820
Thomas Falconc62aa372020-11-18 19:12:20 -06001821 indir_arr[0] = tx_crq;
1822 memcpy(&ind_bufp->indir_arr[ind_bufp->index], &indir_arr[0],
Thomas Falcon0d973382020-11-18 19:12:19 -06001823 num_entries * sizeof(struct ibmvnic_generic_scrq));
1824 ind_bufp->index += num_entries;
1825 if (__netdev_tx_sent_queue(txq, skb->len,
1826 netdev_xmit_more() &&
1827 ind_bufp->index < IBMVNIC_MAX_IND_DESCS)) {
1828 lpar_rc = ibmvnic_tx_scrq_flush(adapter, tx_scrq);
1829 if (lpar_rc != H_SUCCESS)
1830 goto tx_err;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001831 }
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001832
Thomas Falconffc385b2018-02-18 10:08:41 -06001833 if (atomic_add_return(num_entries, &tx_scrq->used)
Brian King58c8c0c2017-04-19 13:44:47 -04001834 >= adapter->req_tx_entries_per_subcrq) {
Thomas Falcon0aecb132018-02-26 18:10:58 -06001835 netdev_dbg(netdev, "Stopping queue %d\n", queue_num);
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001836 netif_stop_subqueue(netdev, queue_num);
1837 }
1838
Thomas Falcon032c5e82015-12-21 11:26:06 -06001839 tx_packets++;
1840 tx_bytes += skb->len;
1841 txq->trans_start = jiffies;
1842 ret = NETDEV_TX_OK;
Thomas Falcon86b61a52018-03-16 20:00:29 -05001843 goto out;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001844
Thomas Falcon0d973382020-11-18 19:12:19 -06001845tx_flush_err:
1846 dev_kfree_skb_any(skb);
1847 tx_buff->skb = NULL;
1848 tx_pool->consumer_index = tx_pool->consumer_index == 0 ?
1849 tx_pool->num_buffers - 1 :
1850 tx_pool->consumer_index - 1;
1851 tx_dropped++;
1852tx_err:
1853 if (lpar_rc != H_CLOSED && lpar_rc != H_PARAMETER)
1854 dev_err_ratelimited(dev, "tx: send failed\n");
1855
1856 if (lpar_rc == H_CLOSED || adapter->failover_pending) {
1857 /* Disable TX and report carrier off if queue is closed
1858 * or pending failover.
1859 * Firmware guarantees that a signal will be sent to the
1860 * driver, triggering a reset or some other action.
1861 */
1862 netif_tx_stop_all_queues(netdev);
1863 netif_carrier_off(netdev);
1864 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001865out:
1866 netdev->stats.tx_dropped += tx_dropped;
1867 netdev->stats.tx_bytes += tx_bytes;
1868 netdev->stats.tx_packets += tx_packets;
1869 adapter->tx_send_failed += tx_send_failed;
1870 adapter->tx_map_failed += tx_map_failed;
John Allen3d52b592017-08-02 16:44:14 -05001871 adapter->tx_stats_buffers[queue_num].packets += tx_packets;
1872 adapter->tx_stats_buffers[queue_num].bytes += tx_bytes;
1873 adapter->tx_stats_buffers[queue_num].dropped_packets += tx_dropped;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001874
1875 return ret;
1876}
1877
1878static void ibmvnic_set_multi(struct net_device *netdev)
1879{
1880 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1881 struct netdev_hw_addr *ha;
1882 union ibmvnic_crq crq;
1883
1884 memset(&crq, 0, sizeof(crq));
1885 crq.request_capability.first = IBMVNIC_CRQ_CMD;
1886 crq.request_capability.cmd = REQUEST_CAPABILITY;
1887
1888 if (netdev->flags & IFF_PROMISC) {
1889 if (!adapter->promisc_supported)
1890 return;
1891 } else {
1892 if (netdev->flags & IFF_ALLMULTI) {
1893 /* Accept all multicast */
1894 memset(&crq, 0, sizeof(crq));
1895 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1896 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1897 crq.multicast_ctrl.flags = IBMVNIC_ENABLE_ALL;
1898 ibmvnic_send_crq(adapter, &crq);
1899 } else if (netdev_mc_empty(netdev)) {
1900 /* Reject all multicast */
1901 memset(&crq, 0, sizeof(crq));
1902 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1903 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1904 crq.multicast_ctrl.flags = IBMVNIC_DISABLE_ALL;
1905 ibmvnic_send_crq(adapter, &crq);
1906 } else {
1907 /* Accept one or more multicast(s) */
1908 netdev_for_each_mc_addr(ha, netdev) {
1909 memset(&crq, 0, sizeof(crq));
1910 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1911 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1912 crq.multicast_ctrl.flags = IBMVNIC_ENABLE_MC;
1913 ether_addr_copy(&crq.multicast_ctrl.mac_addr[0],
1914 ha->addr);
1915 ibmvnic_send_crq(adapter, &crq);
1916 }
1917 }
1918 }
1919}
1920
Thomas Falcon62740e92019-05-09 23:13:43 -05001921static int __ibmvnic_set_mac(struct net_device *netdev, u8 *dev_addr)
Thomas Falcon032c5e82015-12-21 11:26:06 -06001922{
1923 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001924 union ibmvnic_crq crq;
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05001925 int rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001926
Thomas Falcon62740e92019-05-09 23:13:43 -05001927 if (!is_valid_ether_addr(dev_addr)) {
1928 rc = -EADDRNOTAVAIL;
1929 goto err;
1930 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001931
1932 memset(&crq, 0, sizeof(crq));
1933 crq.change_mac_addr.first = IBMVNIC_CRQ_CMD;
1934 crq.change_mac_addr.cmd = CHANGE_MAC_ADDR;
Thomas Falcon62740e92019-05-09 23:13:43 -05001935 ether_addr_copy(&crq.change_mac_addr.mac_addr[0], dev_addr);
Thomas Falconf8136142018-01-29 13:45:05 -06001936
Thomas Falconff25dcb2019-11-25 17:12:56 -06001937 mutex_lock(&adapter->fw_lock);
1938 adapter->fw_done_rc = 0;
Thomas Falcon070eca92019-11-25 17:12:53 -06001939 reinit_completion(&adapter->fw_done);
Thomas Falconff25dcb2019-11-25 17:12:56 -06001940
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05001941 rc = ibmvnic_send_crq(adapter, &crq);
Thomas Falcon62740e92019-05-09 23:13:43 -05001942 if (rc) {
1943 rc = -EIO;
Thomas Falconff25dcb2019-11-25 17:12:56 -06001944 mutex_unlock(&adapter->fw_lock);
Thomas Falcon62740e92019-05-09 23:13:43 -05001945 goto err;
1946 }
1947
Thomas Falcon476d96c2019-11-25 17:12:55 -06001948 rc = ibmvnic_wait_for_completion(adapter, &adapter->fw_done, 10000);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001949 /* netdev->dev_addr is changed in handle_change_mac_rsp function */
Thomas Falcon476d96c2019-11-25 17:12:55 -06001950 if (rc || adapter->fw_done_rc) {
Thomas Falcon62740e92019-05-09 23:13:43 -05001951 rc = -EIO;
Thomas Falconff25dcb2019-11-25 17:12:56 -06001952 mutex_unlock(&adapter->fw_lock);
Thomas Falcon62740e92019-05-09 23:13:43 -05001953 goto err;
1954 }
Thomas Falconff25dcb2019-11-25 17:12:56 -06001955 mutex_unlock(&adapter->fw_lock);
Thomas Falcon62740e92019-05-09 23:13:43 -05001956 return 0;
1957err:
1958 ether_addr_copy(adapter->mac_addr, netdev->dev_addr);
1959 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001960}
1961
John Allenc26eba02017-10-26 16:23:25 -05001962static int ibmvnic_set_mac(struct net_device *netdev, void *p)
1963{
1964 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1965 struct sockaddr *addr = p;
Thomas Falconf8136142018-01-29 13:45:05 -06001966 int rc;
John Allenc26eba02017-10-26 16:23:25 -05001967
Thomas Falcon62740e92019-05-09 23:13:43 -05001968 rc = 0;
Lijun Pan8fc36722020-10-27 17:04:56 -05001969 if (!is_valid_ether_addr(addr->sa_data))
1970 return -EADDRNOTAVAIL;
1971
Jiri Wiesner67eb2112021-03-04 17:18:28 +01001972 ether_addr_copy(adapter->mac_addr, addr->sa_data);
1973 if (adapter->state != VNIC_PROBED)
Thomas Falcon62740e92019-05-09 23:13:43 -05001974 rc = __ibmvnic_set_mac(netdev, addr->sa_data);
John Allenc26eba02017-10-26 16:23:25 -05001975
Thomas Falconf8136142018-01-29 13:45:05 -06001976 return rc;
John Allenc26eba02017-10-26 16:23:25 -05001977}
1978
Lijun Pancaee7bf2021-04-12 02:41:27 -05001979static const char *reset_reason_to_string(enum ibmvnic_reset_reason reason)
1980{
1981 switch (reason) {
1982 case VNIC_RESET_FAILOVER:
1983 return "FAILOVER";
1984 case VNIC_RESET_MOBILITY:
1985 return "MOBILITY";
1986 case VNIC_RESET_FATAL:
1987 return "FATAL";
1988 case VNIC_RESET_NON_FATAL:
1989 return "NON_FATAL";
1990 case VNIC_RESET_TIMEOUT:
1991 return "TIMEOUT";
1992 case VNIC_RESET_CHANGE_PARAM:
1993 return "CHANGE_PARAM";
1994 default:
1995 return "UNKNOWN";
1996 }
1997}
1998
Lee Jones80708602021-01-15 20:09:03 +00001999/*
Nathan Fontenoted651a12017-05-03 14:04:38 -04002000 * do_reset returns zero if we are able to keep processing reset events, or
2001 * non-zero if we hit a fatal error and must halt.
2002 */
2003static int do_reset(struct ibmvnic_adapter *adapter,
2004 struct ibmvnic_rwi *rwi, u32 reset_state)
2005{
John Allen896d8692018-01-18 16:26:31 -06002006 u64 old_num_rx_queues, old_num_tx_queues;
Thomas Falcon5bf032e2018-11-21 11:17:59 -06002007 u64 old_num_rx_slots, old_num_tx_slots;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002008 struct net_device *netdev = adapter->netdev;
Lijun Pand3a6abc2021-04-14 02:46:15 -05002009 int rc;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002010
Sukadev Bhattiprolu38bd5ce2020-12-04 18:22:35 -08002011 netdev_dbg(adapter->netdev,
Lijun Pan0666ef72021-04-12 02:41:28 -05002012 "[S:%s FOP:%d] Reset reason: %s, reset_state: %s\n",
2013 adapter_state_to_string(adapter->state),
2014 adapter->failover_pending,
2015 reset_reason_to_string(rwi->reset_reason),
2016 adapter_state_to_string(reset_state));
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002017
Lijun Pan3f5ec372021-01-06 15:35:14 -06002018 adapter->reset_reason = rwi->reset_reason;
2019 /* requestor of VNIC_RESET_CHANGE_PARAM already has the rtnl lock */
2020 if (!(adapter->reset_reason == VNIC_RESET_CHANGE_PARAM))
2021 rtnl_lock();
2022
Lijun Panbab08be2021-02-11 00:43:19 -06002023 /* Now that we have the rtnl lock, clear any pending failover.
Sukadev Bhattiprolu1d850492020-10-30 10:07:11 -07002024 * This will ensure ibmvnic_open() has either completed or will
2025 * block until failover is complete.
2026 */
2027 if (rwi->reset_reason == VNIC_RESET_FAILOVER)
2028 adapter->failover_pending = false;
Juliet Kimb27507b2019-09-20 16:11:22 -04002029
Sukadev Bhattiprolu8f1c0fd2021-02-23 21:02:29 -08002030 /* read the state and check (again) after getting rtnl */
2031 reset_state = adapter->state;
2032
2033 if (reset_state == VNIC_REMOVING || reset_state == VNIC_REMOVED) {
2034 rc = -EBUSY;
2035 goto out;
2036 }
2037
Nathan Fontenoted651a12017-05-03 14:04:38 -04002038 netif_carrier_off(netdev);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002039
John Allen896d8692018-01-18 16:26:31 -06002040 old_num_rx_queues = adapter->req_rx_queues;
2041 old_num_tx_queues = adapter->req_tx_queues;
Thomas Falcon5bf032e2018-11-21 11:17:59 -06002042 old_num_rx_slots = adapter->req_rx_add_entries_per_subcrq;
2043 old_num_tx_slots = adapter->req_tx_entries_per_subcrq;
John Allen896d8692018-01-18 16:26:31 -06002044
Nathan Fontenot30f79622018-04-06 18:37:06 -05002045 ibmvnic_cleanup(netdev);
2046
Thomas Falcon1f946082019-06-07 16:03:53 -05002047 if (reset_state == VNIC_OPEN &&
2048 adapter->reset_reason != VNIC_RESET_MOBILITY &&
Nathan Fontenot30f79622018-04-06 18:37:06 -05002049 adapter->reset_reason != VNIC_RESET_FAILOVER) {
Lijun Pan3f5ec372021-01-06 15:35:14 -06002050 if (adapter->reset_reason == VNIC_RESET_CHANGE_PARAM) {
2051 rc = __ibmvnic_close(netdev);
2052 if (rc)
2053 goto out;
2054 } else {
2055 adapter->state = VNIC_CLOSING;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002056
Lijun Pan3f5ec372021-01-06 15:35:14 -06002057 /* Release the RTNL lock before link state change and
2058 * re-acquire after the link state change to allow
2059 * linkwatch_event to grab the RTNL lock and run during
2060 * a reset.
2061 */
2062 rtnl_unlock();
2063 rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_DN);
2064 rtnl_lock();
2065 if (rc)
2066 goto out;
Juliet Kimb27507b2019-09-20 16:11:22 -04002067
Sukadev Bhattiprolu8f1c0fd2021-02-23 21:02:29 -08002068 if (adapter->state == VNIC_OPEN) {
2069 /* When we dropped rtnl, ibmvnic_open() got
2070 * it and noticed that we are resetting and
2071 * set the adapter state to OPEN. Update our
2072 * new "target" state, and resume the reset
2073 * from VNIC_CLOSING state.
2074 */
2075 netdev_dbg(netdev,
Lijun Pan0666ef72021-04-12 02:41:28 -05002076 "Open changed state from %s, updating.\n",
2077 adapter_state_to_string(reset_state));
Sukadev Bhattiprolu8f1c0fd2021-02-23 21:02:29 -08002078 reset_state = VNIC_OPEN;
2079 adapter->state = VNIC_CLOSING;
2080 }
2081
Lijun Pan3f5ec372021-01-06 15:35:14 -06002082 if (adapter->state != VNIC_CLOSING) {
Sukadev Bhattiprolu8f1c0fd2021-02-23 21:02:29 -08002083 /* If someone else changed the adapter state
2084 * when we dropped the rtnl, fail the reset
2085 */
Lijun Pan3f5ec372021-01-06 15:35:14 -06002086 rc = -1;
2087 goto out;
2088 }
Lijun Pan3f5ec372021-01-06 15:35:14 -06002089 adapter->state = VNIC_CLOSED;
Juliet Kimb27507b2019-09-20 16:11:22 -04002090 }
Lijun Pan3f5ec372021-01-06 15:35:14 -06002091 }
Juliet Kimb27507b2019-09-20 16:11:22 -04002092
Lijun Pan3f5ec372021-01-06 15:35:14 -06002093 if (adapter->reset_reason == VNIC_RESET_CHANGE_PARAM) {
2094 release_resources(adapter);
2095 release_sub_crqs(adapter, 1);
2096 release_crq_queue(adapter);
John Allenc26eba02017-10-26 16:23:25 -05002097 }
2098
John Allen8cb31cf2017-05-26 10:30:37 -04002099 if (adapter->reset_reason != VNIC_RESET_NON_FATAL) {
2100 /* remove the closed state so when we call open it appears
2101 * we are coming from the probed state.
2102 */
Nathan Fontenoted651a12017-05-03 14:04:38 -04002103 adapter->state = VNIC_PROBED;
John Allen8cb31cf2017-05-26 10:30:37 -04002104
Lijun Pan3f5ec372021-01-06 15:35:14 -06002105 if (adapter->reset_reason == VNIC_RESET_CHANGE_PARAM) {
2106 rc = init_crq_queue(adapter);
2107 } else if (adapter->reset_reason == VNIC_RESET_MOBILITY) {
Nathan Fontenot30f79622018-04-06 18:37:06 -05002108 rc = ibmvnic_reenable_crq_queue(adapter);
2109 release_sub_crqs(adapter, 1);
2110 } else {
2111 rc = ibmvnic_reset_crq(adapter);
Dany Madden8b40eb732020-06-18 15:24:13 -04002112 if (rc == H_CLOSED || rc == H_SUCCESS) {
Nathan Fontenot30f79622018-04-06 18:37:06 -05002113 rc = vio_enable_interrupts(adapter->vdev);
Dany Madden8b40eb732020-06-18 15:24:13 -04002114 if (rc)
2115 netdev_err(adapter->netdev,
2116 "Reset failed to enable interrupts. rc=%d\n",
2117 rc);
2118 }
Nathan Fontenot30f79622018-04-06 18:37:06 -05002119 }
2120
2121 if (rc) {
2122 netdev_err(adapter->netdev,
Dany Madden8b40eb732020-06-18 15:24:13 -04002123 "Reset couldn't initialize crq. rc=%d\n", rc);
Juliet Kimb27507b2019-09-20 16:11:22 -04002124 goto out;
Nathan Fontenot30f79622018-04-06 18:37:06 -05002125 }
2126
Lijun Pan635e4422020-08-19 17:52:26 -05002127 rc = ibmvnic_reset_init(adapter, true);
Juliet Kimb27507b2019-09-20 16:11:22 -04002128 if (rc) {
2129 rc = IBMVNIC_INIT_FAILED;
2130 goto out;
2131 }
John Allen8cb31cf2017-05-26 10:30:37 -04002132
2133 /* If the adapter was in PROBE state prior to the reset,
2134 * exit here.
2135 */
Juliet Kimb27507b2019-09-20 16:11:22 -04002136 if (reset_state == VNIC_PROBED) {
2137 rc = 0;
2138 goto out;
2139 }
John Allen8cb31cf2017-05-26 10:30:37 -04002140
2141 rc = ibmvnic_login(netdev);
Lijun Panf78afaa2021-02-11 00:43:20 -06002142 if (rc)
Juliet Kimb27507b2019-09-20 16:11:22 -04002143 goto out;
John Allen8cb31cf2017-05-26 10:30:37 -04002144
Lijun Pan3f5ec372021-01-06 15:35:14 -06002145 if (adapter->reset_reason == VNIC_RESET_CHANGE_PARAM) {
2146 rc = init_resources(adapter);
2147 if (rc)
2148 goto out;
2149 } else if (adapter->req_rx_queues != old_num_rx_queues ||
Juliet Kimb27507b2019-09-20 16:11:22 -04002150 adapter->req_tx_queues != old_num_tx_queues ||
2151 adapter->req_rx_add_entries_per_subcrq !=
2152 old_num_rx_slots ||
2153 adapter->req_tx_entries_per_subcrq !=
Mingming Cao9f134572020-08-25 13:26:41 -04002154 old_num_tx_slots ||
2155 !adapter->rx_pool ||
2156 !adapter->tso_pool ||
2157 !adapter->tx_pool) {
John Allen896d8692018-01-18 16:26:31 -06002158 release_rx_pools(adapter);
2159 release_tx_pools(adapter);
Juliet Kima5681e22018-11-19 15:59:22 -06002160 release_napi(adapter);
2161 release_vpd_data(adapter);
2162
2163 rc = init_resources(adapter);
Thomas Falconf611a5b2018-08-30 13:19:53 -05002164 if (rc)
Juliet Kimb27507b2019-09-20 16:11:22 -04002165 goto out;
Nathan Fontenotd9043c12018-02-19 13:30:14 -06002166
John Allenc26eba02017-10-26 16:23:25 -05002167 } else {
2168 rc = reset_tx_pools(adapter);
Jakub Kicinski8ae4dff2020-09-04 21:07:49 -07002169 if (rc) {
Mingming Cao9f134572020-08-25 13:26:41 -04002170 netdev_dbg(adapter->netdev, "reset tx pools failed (%d)\n",
Lijun Pan91dc5d22021-02-11 00:43:22 -06002171 rc);
Juliet Kimb27507b2019-09-20 16:11:22 -04002172 goto out;
Jakub Kicinski8ae4dff2020-09-04 21:07:49 -07002173 }
Nathan Fontenot8c0543a2017-05-26 10:31:06 -04002174
John Allenc26eba02017-10-26 16:23:25 -05002175 rc = reset_rx_pools(adapter);
Jakub Kicinski8ae4dff2020-09-04 21:07:49 -07002176 if (rc) {
Mingming Cao9f134572020-08-25 13:26:41 -04002177 netdev_dbg(adapter->netdev, "reset rx pools failed (%d)\n",
Lijun Pan91dc5d22021-02-11 00:43:22 -06002178 rc);
Juliet Kimb27507b2019-09-20 16:11:22 -04002179 goto out;
Jakub Kicinski8ae4dff2020-09-04 21:07:49 -07002180 }
John Allenc26eba02017-10-26 16:23:25 -05002181 }
Thomas Falcon134bbe72018-05-16 15:49:04 -05002182 ibmvnic_disable_irqs(adapter);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002183 }
John Allene676d812018-03-14 10:41:29 -05002184 adapter->state = VNIC_CLOSED;
2185
Juliet Kimb27507b2019-09-20 16:11:22 -04002186 if (reset_state == VNIC_CLOSED) {
2187 rc = 0;
2188 goto out;
2189 }
John Allene676d812018-03-14 10:41:29 -05002190
Nathan Fontenoted651a12017-05-03 14:04:38 -04002191 rc = __ibmvnic_open(netdev);
2192 if (rc) {
Juliet Kimb27507b2019-09-20 16:11:22 -04002193 rc = IBMVNIC_OPEN_FAILED;
2194 goto out;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002195 }
2196
Thomas Falconbe32a242019-06-07 16:03:54 -05002197 /* refresh device's multicast list */
2198 ibmvnic_set_multi(netdev);
2199
Lijun Pan98025bc2020-11-20 16:40:12 -06002200 if (adapter->reset_reason == VNIC_RESET_FAILOVER ||
Lijun Pan6be46662020-12-14 15:19:29 -06002201 adapter->reset_reason == VNIC_RESET_MOBILITY)
2202 __netdev_notify_peers(netdev);
Nathan Fontenot61d3e1d2017-06-12 20:47:45 -04002203
Juliet Kimb27507b2019-09-20 16:11:22 -04002204 rc = 0;
2205
2206out:
Dany Madden0cb4bc62020-11-25 18:04:27 -06002207 /* restore the adapter state if reset failed */
2208 if (rc)
2209 adapter->state = reset_state;
Lijun Pan3f5ec372021-01-06 15:35:14 -06002210 /* requestor of VNIC_RESET_CHANGE_PARAM should still hold the rtnl lock */
2211 if (!(adapter->reset_reason == VNIC_RESET_CHANGE_PARAM))
2212 rtnl_unlock();
Juliet Kimb27507b2019-09-20 16:11:22 -04002213
Lijun Pan0666ef72021-04-12 02:41:28 -05002214 netdev_dbg(adapter->netdev, "[S:%s FOP:%d] Reset done, rc %d\n",
2215 adapter_state_to_string(adapter->state),
2216 adapter->failover_pending, rc);
Juliet Kimb27507b2019-09-20 16:11:22 -04002217 return rc;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002218}
2219
Thomas Falcon2770a792018-05-23 13:38:02 -05002220static int do_hard_reset(struct ibmvnic_adapter *adapter,
2221 struct ibmvnic_rwi *rwi, u32 reset_state)
2222{
2223 struct net_device *netdev = adapter->netdev;
2224 int rc;
2225
Lijun Pancaee7bf2021-04-12 02:41:27 -05002226 netdev_dbg(adapter->netdev, "Hard resetting driver (%s)\n",
2227 reset_reason_to_string(rwi->reset_reason));
Thomas Falcon2770a792018-05-23 13:38:02 -05002228
Sukadev Bhattiprolu8f1c0fd2021-02-23 21:02:29 -08002229 /* read the state and check (again) after getting rtnl */
2230 reset_state = adapter->state;
2231
2232 if (reset_state == VNIC_REMOVING || reset_state == VNIC_REMOVED) {
2233 rc = -EBUSY;
2234 goto out;
2235 }
2236
Thomas Falcon2770a792018-05-23 13:38:02 -05002237 netif_carrier_off(netdev);
2238 adapter->reset_reason = rwi->reset_reason;
2239
2240 ibmvnic_cleanup(netdev);
2241 release_resources(adapter);
2242 release_sub_crqs(adapter, 0);
2243 release_crq_queue(adapter);
2244
2245 /* remove the closed state so when we call open it appears
2246 * we are coming from the probed state.
2247 */
2248 adapter->state = VNIC_PROBED;
2249
Thomas Falconbbd669a2019-04-04 18:58:26 -05002250 reinit_completion(&adapter->init_done);
Thomas Falcon2770a792018-05-23 13:38:02 -05002251 rc = init_crq_queue(adapter);
2252 if (rc) {
2253 netdev_err(adapter->netdev,
2254 "Couldn't initialize crq. rc=%d\n", rc);
Dany Madden0cb4bc62020-11-25 18:04:27 -06002255 goto out;
Thomas Falcon2770a792018-05-23 13:38:02 -05002256 }
2257
Lijun Pan635e4422020-08-19 17:52:26 -05002258 rc = ibmvnic_reset_init(adapter, false);
Thomas Falcon2770a792018-05-23 13:38:02 -05002259 if (rc)
Dany Madden0cb4bc62020-11-25 18:04:27 -06002260 goto out;
Thomas Falcon2770a792018-05-23 13:38:02 -05002261
2262 /* If the adapter was in PROBE state prior to the reset,
2263 * exit here.
2264 */
2265 if (reset_state == VNIC_PROBED)
Dany Madden0cb4bc62020-11-25 18:04:27 -06002266 goto out;
Thomas Falcon2770a792018-05-23 13:38:02 -05002267
2268 rc = ibmvnic_login(netdev);
Dany Madden0cb4bc62020-11-25 18:04:27 -06002269 if (rc)
2270 goto out;
Juliet Kima5681e22018-11-19 15:59:22 -06002271
2272 rc = init_resources(adapter);
Thomas Falcon2770a792018-05-23 13:38:02 -05002273 if (rc)
Dany Madden0cb4bc62020-11-25 18:04:27 -06002274 goto out;
Thomas Falcon2770a792018-05-23 13:38:02 -05002275
2276 ibmvnic_disable_irqs(adapter);
2277 adapter->state = VNIC_CLOSED;
2278
2279 if (reset_state == VNIC_CLOSED)
Dany Madden0cb4bc62020-11-25 18:04:27 -06002280 goto out;
Thomas Falcon2770a792018-05-23 13:38:02 -05002281
2282 rc = __ibmvnic_open(netdev);
Dany Madden0cb4bc62020-11-25 18:04:27 -06002283 if (rc) {
2284 rc = IBMVNIC_OPEN_FAILED;
2285 goto out;
2286 }
Thomas Falcon2770a792018-05-23 13:38:02 -05002287
Lijun Pan6be46662020-12-14 15:19:29 -06002288 __netdev_notify_peers(netdev);
Dany Madden0cb4bc62020-11-25 18:04:27 -06002289out:
2290 /* restore adapter state if reset failed */
2291 if (rc)
2292 adapter->state = reset_state;
Lijun Pan0666ef72021-04-12 02:41:28 -05002293 netdev_dbg(adapter->netdev, "[S:%s FOP:%d] Hard reset done, rc %d\n",
2294 adapter_state_to_string(adapter->state),
2295 adapter->failover_pending, rc);
Dany Madden0cb4bc62020-11-25 18:04:27 -06002296 return rc;
Thomas Falcon2770a792018-05-23 13:38:02 -05002297}
2298
Nathan Fontenoted651a12017-05-03 14:04:38 -04002299static struct ibmvnic_rwi *get_next_rwi(struct ibmvnic_adapter *adapter)
2300{
2301 struct ibmvnic_rwi *rwi;
Thomas Falcon6c5c7482018-12-10 15:22:22 -06002302 unsigned long flags;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002303
Thomas Falcon6c5c7482018-12-10 15:22:22 -06002304 spin_lock_irqsave(&adapter->rwi_lock, flags);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002305
2306 if (!list_empty(&adapter->rwi_list)) {
2307 rwi = list_first_entry(&adapter->rwi_list, struct ibmvnic_rwi,
2308 list);
2309 list_del(&rwi->list);
2310 } else {
2311 rwi = NULL;
2312 }
2313
Thomas Falcon6c5c7482018-12-10 15:22:22 -06002314 spin_unlock_irqrestore(&adapter->rwi_lock, flags);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002315 return rwi;
2316}
2317
Nathan Fontenoted651a12017-05-03 14:04:38 -04002318static void __ibmvnic_reset(struct work_struct *work)
2319{
2320 struct ibmvnic_rwi *rwi;
2321 struct ibmvnic_adapter *adapter;
Juliet Kim7d7195a2020-03-10 09:23:58 -05002322 bool saved_state = false;
2323 unsigned long flags;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002324 u32 reset_state;
John Allenc26eba02017-10-26 16:23:25 -05002325 int rc = 0;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002326
2327 adapter = container_of(work, struct ibmvnic_adapter, ibmvnic_reset);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002328
Juliet Kim7ed5b312019-09-20 16:11:23 -04002329 if (test_and_set_bit_lock(0, &adapter->resetting)) {
Lijun Pan870e04a2021-04-13 14:33:39 -05002330 queue_delayed_work(system_long_wq,
2331 &adapter->ibmvnic_delayed_reset,
2332 IBMVNIC_RESET_DELAY);
Juliet Kim7ed5b312019-09-20 16:11:23 -04002333 return;
2334 }
2335
Nathan Fontenoted651a12017-05-03 14:04:38 -04002336 rwi = get_next_rwi(adapter);
2337 while (rwi) {
Juliet Kim7d7195a2020-03-10 09:23:58 -05002338 spin_lock_irqsave(&adapter->state_lock, flags);
2339
Thomas Falcon36f10312019-08-27 11:10:04 -05002340 if (adapter->state == VNIC_REMOVING ||
Michal Suchanekc8dc5592019-09-09 22:44:51 +02002341 adapter->state == VNIC_REMOVED) {
Juliet Kim7d7195a2020-03-10 09:23:58 -05002342 spin_unlock_irqrestore(&adapter->state_lock, flags);
Juliet Kim1c2977c2019-09-05 17:30:01 -04002343 kfree(rwi);
2344 rc = EBUSY;
2345 break;
2346 }
Thomas Falcon36f10312019-08-27 11:10:04 -05002347
Juliet Kim7d7195a2020-03-10 09:23:58 -05002348 if (!saved_state) {
2349 reset_state = adapter->state;
Juliet Kim7d7195a2020-03-10 09:23:58 -05002350 saved_state = true;
2351 }
2352 spin_unlock_irqrestore(&adapter->state_lock, flags);
2353
Lijun Pan3f5ec372021-01-06 15:35:14 -06002354 if (adapter->force_reset_recovery) {
Lijun Panbab08be2021-02-11 00:43:19 -06002355 /* Since we are doing a hard reset now, clear the
Sukadev Bhattiprolu1d850492020-10-30 10:07:11 -07002356 * failover_pending flag so we don't ignore any
2357 * future MOBILITY or other resets.
2358 */
2359 adapter->failover_pending = false;
2360
Juliet Kimb27507b2019-09-20 16:11:22 -04002361 /* Transport event occurred during previous reset */
2362 if (adapter->wait_for_reset) {
2363 /* Previous was CHANGE_PARAM; caller locked */
2364 adapter->force_reset_recovery = false;
2365 rc = do_hard_reset(adapter, rwi, reset_state);
2366 } else {
2367 rtnl_lock();
2368 adapter->force_reset_recovery = false;
2369 rc = do_hard_reset(adapter, rwi, reset_state);
2370 rtnl_unlock();
2371 }
Sukadev Bhattiproluf15fde92020-11-25 18:04:28 -06002372 if (rc) {
2373 /* give backing device time to settle down */
2374 netdev_dbg(adapter->netdev,
Lijun Pan0666ef72021-04-12 02:41:28 -05002375 "[S:%s] Hard reset failed, waiting 60 secs\n",
2376 adapter_state_to_string(adapter->state));
Sukadev Bhattiproluf15fde92020-11-25 18:04:28 -06002377 set_current_state(TASK_UNINTERRUPTIBLE);
2378 schedule_timeout(60 * HZ);
2379 }
Lijun Pan1f45dc22020-12-23 14:49:04 -06002380 } else {
Thomas Falcon2770a792018-05-23 13:38:02 -05002381 rc = do_reset(adapter, rwi, reset_state);
2382 }
Nathan Fontenoted651a12017-05-03 14:04:38 -04002383 kfree(rwi);
Dany Maddena86d5c62020-11-25 18:04:31 -06002384 adapter->last_reset_time = jiffies;
Dany Madden0cb4bc62020-11-25 18:04:27 -06002385
Dany Madden18f141b2020-11-25 18:04:25 -06002386 if (rc)
2387 netdev_dbg(adapter->netdev, "Reset failed, rc=%d\n", rc);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002388
2389 rwi = get_next_rwi(adapter);
Juliet Kim7ed5b312019-09-20 16:11:23 -04002390
2391 if (rwi && (rwi->reset_reason == VNIC_RESET_FAILOVER ||
2392 rwi->reset_reason == VNIC_RESET_MOBILITY))
2393 adapter->force_reset_recovery = true;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002394 }
2395
John Allenc26eba02017-10-26 16:23:25 -05002396 if (adapter->wait_for_reset) {
John Allenc26eba02017-10-26 16:23:25 -05002397 adapter->reset_done_rc = rc;
2398 complete(&adapter->reset_done);
2399 }
2400
Juliet Kim7ed5b312019-09-20 16:11:23 -04002401 clear_bit_unlock(0, &adapter->resetting);
Sukadev Bhattiprolu38bd5ce2020-12-04 18:22:35 -08002402
2403 netdev_dbg(adapter->netdev,
Lijun Pan0666ef72021-04-12 02:41:28 -05002404 "[S:%s FRR:%d WFR:%d] Done processing resets\n",
2405 adapter_state_to_string(adapter->state),
2406 adapter->force_reset_recovery,
Sukadev Bhattiprolu38bd5ce2020-12-04 18:22:35 -08002407 adapter->wait_for_reset);
Juliet Kim7ed5b312019-09-20 16:11:23 -04002408}
2409
2410static void __ibmvnic_delayed_reset(struct work_struct *work)
2411{
2412 struct ibmvnic_adapter *adapter;
2413
2414 adapter = container_of(work, struct ibmvnic_adapter,
2415 ibmvnic_delayed_reset.work);
2416 __ibmvnic_reset(&adapter->ibmvnic_reset);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002417}
2418
Thomas Falconaf894d22018-04-06 18:37:04 -05002419static int ibmvnic_reset(struct ibmvnic_adapter *adapter,
2420 enum ibmvnic_reset_reason reason)
Nathan Fontenoted651a12017-05-03 14:04:38 -04002421{
Thomas Falcon2770a792018-05-23 13:38:02 -05002422 struct list_head *entry, *tmp_entry;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002423 struct ibmvnic_rwi *rwi, *tmp;
2424 struct net_device *netdev = adapter->netdev;
Thomas Falcon6c5c7482018-12-10 15:22:22 -06002425 unsigned long flags;
Thomas Falconaf894d22018-04-06 18:37:04 -05002426 int ret;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002427
Jakub Kicinskib646acd52021-02-16 22:58:44 -08002428 spin_lock_irqsave(&adapter->rwi_lock, flags);
2429
2430 /* If failover is pending don't schedule any other reset.
Sukadev Bhattiprolu1d850492020-10-30 10:07:11 -07002431 * Instead let the failover complete. If there is already a
2432 * a failover reset scheduled, we will detect and drop the
2433 * duplicate reset when walking the ->rwi_list below.
2434 */
Nathan Fontenoted651a12017-05-03 14:04:38 -04002435 if (adapter->state == VNIC_REMOVING ||
Thomas Falcon5a18e1e2018-04-06 18:37:05 -05002436 adapter->state == VNIC_REMOVED ||
Sukadev Bhattiprolu1d850492020-10-30 10:07:11 -07002437 (adapter->failover_pending && reason != VNIC_RESET_FAILOVER)) {
Thomas Falconaf894d22018-04-06 18:37:04 -05002438 ret = EBUSY;
Thomas Falcon5a18e1e2018-04-06 18:37:05 -05002439 netdev_dbg(netdev, "Adapter removing or pending failover, skipping reset\n");
Thomas Falconaf894d22018-04-06 18:37:04 -05002440 goto err;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002441 }
2442
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04002443 if (adapter->state == VNIC_PROBING) {
2444 netdev_warn(netdev, "Adapter reset during probe\n");
Lijun Pan91dc5d22021-02-11 00:43:22 -06002445 adapter->init_done_rc = EAGAIN;
2446 ret = EAGAIN;
Thomas Falconaf894d22018-04-06 18:37:04 -05002447 goto err;
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04002448 }
2449
Nathan Fontenoted651a12017-05-03 14:04:38 -04002450 list_for_each(entry, &adapter->rwi_list) {
2451 tmp = list_entry(entry, struct ibmvnic_rwi, list);
2452 if (tmp->reset_reason == reason) {
Lijun Pancaee7bf2021-04-12 02:41:27 -05002453 netdev_dbg(netdev, "Skipping matching reset, reason=%s\n",
2454 reset_reason_to_string(reason));
Thomas Falconaf894d22018-04-06 18:37:04 -05002455 ret = EBUSY;
2456 goto err;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002457 }
2458 }
2459
Thomas Falcon1d1bbc32018-12-10 15:22:23 -06002460 rwi = kzalloc(sizeof(*rwi), GFP_ATOMIC);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002461 if (!rwi) {
Thomas Falconaf894d22018-04-06 18:37:04 -05002462 ret = ENOMEM;
2463 goto err;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002464 }
Thomas Falcon2770a792018-05-23 13:38:02 -05002465 /* if we just received a transport event,
2466 * flush reset queue and process this reset
2467 */
2468 if (adapter->force_reset_recovery && !list_empty(&adapter->rwi_list)) {
2469 list_for_each_safe(entry, tmp_entry, &adapter->rwi_list)
2470 list_del(entry);
2471 }
Nathan Fontenoted651a12017-05-03 14:04:38 -04002472 rwi->reset_reason = reason;
2473 list_add_tail(&rwi->list, &adapter->rwi_list);
Lijun Pancaee7bf2021-04-12 02:41:27 -05002474 netdev_dbg(adapter->netdev, "Scheduling reset (reason %s)\n",
2475 reset_reason_to_string(reason));
Lijun Pan870e04a2021-04-13 14:33:39 -05002476 queue_work(system_long_wq, &adapter->ibmvnic_reset);
Thomas Falconaf894d22018-04-06 18:37:04 -05002477
Sukadev Bhattiprolu4a41c422021-02-12 20:42:50 -08002478 ret = 0;
Thomas Falconaf894d22018-04-06 18:37:04 -05002479err:
Sukadev Bhattiprolu4a41c422021-02-12 20:42:50 -08002480 /* ibmvnic_close() below can block, so drop the lock first */
2481 spin_unlock_irqrestore(&adapter->rwi_lock, flags);
2482
2483 if (ret == ENOMEM)
2484 ibmvnic_close(netdev);
2485
Thomas Falconaf894d22018-04-06 18:37:04 -05002486 return -ret;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002487}
2488
Michael S. Tsirkin0290bd22019-12-10 09:23:51 -05002489static void ibmvnic_tx_timeout(struct net_device *dev, unsigned int txqueue)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002490{
2491 struct ibmvnic_adapter *adapter = netdev_priv(dev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002492
Lijun Pan855a6312020-11-20 16:40:13 -06002493 if (test_bit(0, &adapter->resetting)) {
2494 netdev_err(adapter->netdev,
2495 "Adapter is resetting, skip timeout reset\n");
2496 return;
2497 }
Dany Maddena86d5c62020-11-25 18:04:31 -06002498 /* No queuing up reset until at least 5 seconds (default watchdog val)
2499 * after last reset
2500 */
2501 if (time_before(jiffies, (adapter->last_reset_time + dev->watchdog_timeo))) {
2502 netdev_dbg(dev, "Not yet time to tx timeout.\n");
2503 return;
2504 }
Nathan Fontenoted651a12017-05-03 14:04:38 -04002505 ibmvnic_reset(adapter, VNIC_RESET_TIMEOUT);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002506}
2507
2508static void remove_buff_from_pool(struct ibmvnic_adapter *adapter,
2509 struct ibmvnic_rx_buff *rx_buff)
2510{
2511 struct ibmvnic_rx_pool *pool = &adapter->rx_pool[rx_buff->pool_index];
2512
2513 rx_buff->skb = NULL;
2514
2515 pool->free_map[pool->next_alloc] = (int)(rx_buff - pool->rx_buff);
2516 pool->next_alloc = (pool->next_alloc + 1) % pool->size;
2517
2518 atomic_dec(&pool->available);
2519}
2520
2521static int ibmvnic_poll(struct napi_struct *napi, int budget)
2522{
Dwip N. Banerjeeec20f362020-11-18 19:12:23 -06002523 struct ibmvnic_sub_crq_queue *rx_scrq;
2524 struct ibmvnic_adapter *adapter;
2525 struct net_device *netdev;
2526 int frames_processed;
2527 int scrq_num;
2528
2529 netdev = napi->dev;
2530 adapter = netdev_priv(netdev);
2531 scrq_num = (int)(napi - adapter->napi);
2532 frames_processed = 0;
2533 rx_scrq = adapter->rx_scrq[scrq_num];
Nathan Fontenot152ce472017-05-26 10:30:54 -04002534
Thomas Falcon032c5e82015-12-21 11:26:06 -06002535restart_poll:
2536 while (frames_processed < budget) {
2537 struct sk_buff *skb;
2538 struct ibmvnic_rx_buff *rx_buff;
2539 union sub_crq *next;
2540 u32 length;
2541 u16 offset;
2542 u8 flags = 0;
2543
Juliet Kim7ed5b312019-09-20 16:11:23 -04002544 if (unlikely(test_bit(0, &adapter->resetting) &&
John Allen34686562018-02-06 16:21:49 -06002545 adapter->reset_reason != VNIC_RESET_NON_FATAL)) {
Dwip N. Banerjeeec20f362020-11-18 19:12:23 -06002546 enable_scrq_irq(adapter, rx_scrq);
Thomas Falcon21ecba62017-06-14 23:50:09 -05002547 napi_complete_done(napi, frames_processed);
2548 return frames_processed;
2549 }
2550
Dwip N. Banerjeeec20f362020-11-18 19:12:23 -06002551 if (!pending_scrq(adapter, rx_scrq))
Thomas Falcon032c5e82015-12-21 11:26:06 -06002552 break;
Dwip N. Banerjeeec20f362020-11-18 19:12:23 -06002553 next = ibmvnic_next_scrq(adapter, rx_scrq);
Lijun Pan914789a2021-02-11 00:43:21 -06002554 rx_buff = (struct ibmvnic_rx_buff *)
2555 be64_to_cpu(next->rx_comp.correlator);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002556 /* do error checking */
2557 if (next->rx_comp.rc) {
John Allene1cea2e2017-08-07 15:42:30 -05002558 netdev_dbg(netdev, "rx buffer returned with rc %x\n",
2559 be16_to_cpu(next->rx_comp.rc));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002560 /* free the entry */
2561 next->rx_comp.first = 0;
Thomas Falcon4b9b0f02018-02-13 18:23:42 -06002562 dev_kfree_skb_any(rx_buff->skb);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002563 remove_buff_from_pool(adapter, rx_buff);
Nathan Fontenotca05e312017-05-03 14:05:14 -04002564 continue;
Thomas Falconabe27a82018-02-19 20:12:57 -06002565 } else if (!rx_buff->skb) {
2566 /* free the entry */
2567 next->rx_comp.first = 0;
2568 remove_buff_from_pool(adapter, rx_buff);
2569 continue;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002570 }
2571
2572 length = be32_to_cpu(next->rx_comp.len);
2573 offset = be16_to_cpu(next->rx_comp.off_frame_data);
2574 flags = next->rx_comp.flags;
2575 skb = rx_buff->skb;
Lijun Pan42557da2021-02-12 20:48:40 -06002576 /* load long_term_buff before copying to skb */
2577 dma_rmb();
Thomas Falcon032c5e82015-12-21 11:26:06 -06002578 skb_copy_to_linear_data(skb, rx_buff->data + offset,
2579 length);
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04002580
2581 /* VLAN Header has been stripped by the system firmware and
2582 * needs to be inserted by the driver
2583 */
2584 if (adapter->rx_vlan_header_insertion &&
2585 (flags & IBMVNIC_VLAN_STRIPPED))
2586 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
2587 ntohs(next->rx_comp.vlan_tci));
2588
Thomas Falcon032c5e82015-12-21 11:26:06 -06002589 /* free the entry */
2590 next->rx_comp.first = 0;
2591 remove_buff_from_pool(adapter, rx_buff);
2592
2593 skb_put(skb, length);
2594 skb->protocol = eth_type_trans(skb, netdev);
Thomas Falcon94ca3052017-05-03 14:05:20 -04002595 skb_record_rx_queue(skb, scrq_num);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002596
2597 if (flags & IBMVNIC_IP_CHKSUM_GOOD &&
2598 flags & IBMVNIC_TCP_UDP_CHKSUM_GOOD) {
2599 skb->ip_summed = CHECKSUM_UNNECESSARY;
2600 }
2601
2602 length = skb->len;
2603 napi_gro_receive(napi, skb); /* send it up */
2604 netdev->stats.rx_packets++;
2605 netdev->stats.rx_bytes += length;
John Allen3d52b592017-08-02 16:44:14 -05002606 adapter->rx_stats_buffers[scrq_num].packets++;
2607 adapter->rx_stats_buffers[scrq_num].bytes += length;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002608 frames_processed++;
2609 }
Nathan Fontenot152ce472017-05-26 10:30:54 -04002610
Dwip N. Banerjee41ed0a02020-11-18 19:12:25 -06002611 if (adapter->state != VNIC_CLOSING &&
2612 ((atomic_read(&adapter->rx_pool[scrq_num].available) <
2613 adapter->req_rx_add_entries_per_subcrq / 2) ||
2614 frames_processed < budget))
Nathan Fontenot152ce472017-05-26 10:30:54 -04002615 replenish_rx_pool(adapter, &adapter->rx_pool[scrq_num]);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002616 if (frames_processed < budget) {
Dwip N. Banerjeeec20f362020-11-18 19:12:23 -06002617 if (napi_complete_done(napi, frames_processed)) {
2618 enable_scrq_irq(adapter, rx_scrq);
2619 if (pending_scrq(adapter, rx_scrq)) {
Dwip N. Banerjeeec20f362020-11-18 19:12:23 -06002620 if (napi_reschedule(napi)) {
2621 disable_scrq_irq(adapter, rx_scrq);
2622 goto restart_poll;
2623 }
2624 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002625 }
2626 }
2627 return frames_processed;
2628}
2629
John Allenc26eba02017-10-26 16:23:25 -05002630static int wait_for_reset(struct ibmvnic_adapter *adapter)
2631{
Thomas Falconaf894d22018-04-06 18:37:04 -05002632 int rc, ret;
2633
John Allenc26eba02017-10-26 16:23:25 -05002634 adapter->fallback.mtu = adapter->req_mtu;
2635 adapter->fallback.rx_queues = adapter->req_rx_queues;
2636 adapter->fallback.tx_queues = adapter->req_tx_queues;
2637 adapter->fallback.rx_entries = adapter->req_rx_add_entries_per_subcrq;
2638 adapter->fallback.tx_entries = adapter->req_tx_entries_per_subcrq;
2639
Thomas Falcon070eca92019-11-25 17:12:53 -06002640 reinit_completion(&adapter->reset_done);
John Allenc26eba02017-10-26 16:23:25 -05002641 adapter->wait_for_reset = true;
Thomas Falconaf894d22018-04-06 18:37:04 -05002642 rc = ibmvnic_reset(adapter, VNIC_RESET_CHANGE_PARAM);
Thomas Falcon476d96c2019-11-25 17:12:55 -06002643
2644 if (rc) {
2645 ret = rc;
2646 goto out;
2647 }
2648 rc = ibmvnic_wait_for_completion(adapter, &adapter->reset_done, 60000);
2649 if (rc) {
2650 ret = -ENODEV;
2651 goto out;
2652 }
John Allenc26eba02017-10-26 16:23:25 -05002653
Thomas Falconaf894d22018-04-06 18:37:04 -05002654 ret = 0;
John Allenc26eba02017-10-26 16:23:25 -05002655 if (adapter->reset_done_rc) {
Thomas Falconaf894d22018-04-06 18:37:04 -05002656 ret = -EIO;
John Allenc26eba02017-10-26 16:23:25 -05002657 adapter->desired.mtu = adapter->fallback.mtu;
2658 adapter->desired.rx_queues = adapter->fallback.rx_queues;
2659 adapter->desired.tx_queues = adapter->fallback.tx_queues;
2660 adapter->desired.rx_entries = adapter->fallback.rx_entries;
2661 adapter->desired.tx_entries = adapter->fallback.tx_entries;
2662
Thomas Falcon070eca92019-11-25 17:12:53 -06002663 reinit_completion(&adapter->reset_done);
Thomas Falconaf894d22018-04-06 18:37:04 -05002664 adapter->wait_for_reset = true;
2665 rc = ibmvnic_reset(adapter, VNIC_RESET_CHANGE_PARAM);
Thomas Falcon476d96c2019-11-25 17:12:55 -06002666 if (rc) {
2667 ret = rc;
2668 goto out;
2669 }
2670 rc = ibmvnic_wait_for_completion(adapter, &adapter->reset_done,
2671 60000);
2672 if (rc) {
2673 ret = -ENODEV;
2674 goto out;
2675 }
John Allenc26eba02017-10-26 16:23:25 -05002676 }
Thomas Falcon476d96c2019-11-25 17:12:55 -06002677out:
John Allenc26eba02017-10-26 16:23:25 -05002678 adapter->wait_for_reset = false;
2679
Thomas Falconaf894d22018-04-06 18:37:04 -05002680 return ret;
John Allenc26eba02017-10-26 16:23:25 -05002681}
2682
John Allen3a807b72017-06-06 16:55:52 -05002683static int ibmvnic_change_mtu(struct net_device *netdev, int new_mtu)
2684{
John Allenc26eba02017-10-26 16:23:25 -05002685 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2686
2687 adapter->desired.mtu = new_mtu + ETH_HLEN;
2688
2689 return wait_for_reset(adapter);
John Allen3a807b72017-06-06 16:55:52 -05002690}
2691
Thomas Falconf10b09e2018-03-12 11:51:05 -05002692static netdev_features_t ibmvnic_features_check(struct sk_buff *skb,
2693 struct net_device *dev,
2694 netdev_features_t features)
2695{
2696 /* Some backing hardware adapters can not
2697 * handle packets with a MSS less than 224
2698 * or with only one segment.
2699 */
2700 if (skb_is_gso(skb)) {
2701 if (skb_shinfo(skb)->gso_size < 224 ||
2702 skb_shinfo(skb)->gso_segs == 1)
2703 features &= ~NETIF_F_GSO_MASK;
2704 }
2705
2706 return features;
2707}
2708
Thomas Falcon032c5e82015-12-21 11:26:06 -06002709static const struct net_device_ops ibmvnic_netdev_ops = {
2710 .ndo_open = ibmvnic_open,
2711 .ndo_stop = ibmvnic_close,
2712 .ndo_start_xmit = ibmvnic_xmit,
2713 .ndo_set_rx_mode = ibmvnic_set_multi,
2714 .ndo_set_mac_address = ibmvnic_set_mac,
2715 .ndo_validate_addr = eth_validate_addr,
Thomas Falcon032c5e82015-12-21 11:26:06 -06002716 .ndo_tx_timeout = ibmvnic_tx_timeout,
John Allen3a807b72017-06-06 16:55:52 -05002717 .ndo_change_mtu = ibmvnic_change_mtu,
Thomas Falconf10b09e2018-03-12 11:51:05 -05002718 .ndo_features_check = ibmvnic_features_check,
Thomas Falcon032c5e82015-12-21 11:26:06 -06002719};
2720
2721/* ethtool functions */
2722
Philippe Reynes8a433792017-01-07 22:37:29 +01002723static int ibmvnic_get_link_ksettings(struct net_device *netdev,
2724 struct ethtool_link_ksettings *cmd)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002725{
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03002726 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2727 int rc;
Philippe Reynes8a433792017-01-07 22:37:29 +01002728
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03002729 rc = send_query_phys_parms(adapter);
2730 if (rc) {
2731 adapter->speed = SPEED_UNKNOWN;
2732 adapter->duplex = DUPLEX_UNKNOWN;
2733 }
2734 cmd->base.speed = adapter->speed;
2735 cmd->base.duplex = adapter->duplex;
Philippe Reynes8a433792017-01-07 22:37:29 +01002736 cmd->base.port = PORT_FIBRE;
2737 cmd->base.phy_address = 0;
2738 cmd->base.autoneg = AUTONEG_ENABLE;
2739
Thomas Falcon032c5e82015-12-21 11:26:06 -06002740 return 0;
2741}
2742
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02002743static void ibmvnic_get_drvinfo(struct net_device *netdev,
Thomas Falcon032c5e82015-12-21 11:26:06 -06002744 struct ethtool_drvinfo *info)
2745{
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02002746 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2747
Lijun Pan8a96c802021-02-11 00:43:25 -06002748 strscpy(info->driver, ibmvnic_driver_name, sizeof(info->driver));
2749 strscpy(info->version, IBMVNIC_DRIVER_VERSION, sizeof(info->version));
2750 strscpy(info->fw_version, adapter->fw_version,
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02002751 sizeof(info->fw_version));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002752}
2753
2754static u32 ibmvnic_get_msglevel(struct net_device *netdev)
2755{
2756 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2757
2758 return adapter->msg_enable;
2759}
2760
2761static void ibmvnic_set_msglevel(struct net_device *netdev, u32 data)
2762{
2763 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2764
2765 adapter->msg_enable = data;
2766}
2767
2768static u32 ibmvnic_get_link(struct net_device *netdev)
2769{
2770 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2771
2772 /* Don't need to send a query because we request a logical link up at
2773 * init and then we wait for link state indications
2774 */
2775 return adapter->logical_link_state;
2776}
2777
2778static void ibmvnic_get_ringparam(struct net_device *netdev,
2779 struct ethtool_ringparam *ring)
2780{
John Allenbc131b32017-08-02 16:46:30 -05002781 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2782
Thomas Falcon723ad912018-09-28 18:38:26 -05002783 if (adapter->priv_flags & IBMVNIC_USE_SERVER_MAXES) {
2784 ring->rx_max_pending = adapter->max_rx_add_entries_per_subcrq;
2785 ring->tx_max_pending = adapter->max_tx_entries_per_subcrq;
2786 } else {
2787 ring->rx_max_pending = IBMVNIC_MAX_QUEUE_SZ;
2788 ring->tx_max_pending = IBMVNIC_MAX_QUEUE_SZ;
2789 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002790 ring->rx_mini_max_pending = 0;
2791 ring->rx_jumbo_max_pending = 0;
John Allenbc131b32017-08-02 16:46:30 -05002792 ring->rx_pending = adapter->req_rx_add_entries_per_subcrq;
2793 ring->tx_pending = adapter->req_tx_entries_per_subcrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002794 ring->rx_mini_pending = 0;
2795 ring->rx_jumbo_pending = 0;
2796}
2797
John Allenc26eba02017-10-26 16:23:25 -05002798static int ibmvnic_set_ringparam(struct net_device *netdev,
2799 struct ethtool_ringparam *ring)
2800{
2801 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Thomas Falcon723ad912018-09-28 18:38:26 -05002802 int ret;
John Allenc26eba02017-10-26 16:23:25 -05002803
Thomas Falcon723ad912018-09-28 18:38:26 -05002804 ret = 0;
John Allenc26eba02017-10-26 16:23:25 -05002805 adapter->desired.rx_entries = ring->rx_pending;
2806 adapter->desired.tx_entries = ring->tx_pending;
2807
Thomas Falcon723ad912018-09-28 18:38:26 -05002808 ret = wait_for_reset(adapter);
2809
2810 if (!ret &&
2811 (adapter->req_rx_add_entries_per_subcrq != ring->rx_pending ||
2812 adapter->req_tx_entries_per_subcrq != ring->tx_pending))
2813 netdev_info(netdev,
2814 "Could not match full ringsize request. Requested: RX %d, TX %d; Allowed: RX %llu, TX %llu\n",
2815 ring->rx_pending, ring->tx_pending,
2816 adapter->req_rx_add_entries_per_subcrq,
2817 adapter->req_tx_entries_per_subcrq);
2818 return ret;
John Allenc26eba02017-10-26 16:23:25 -05002819}
2820
John Allenc2dbeb62017-08-02 16:47:17 -05002821static void ibmvnic_get_channels(struct net_device *netdev,
2822 struct ethtool_channels *channels)
2823{
2824 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2825
Thomas Falcon723ad912018-09-28 18:38:26 -05002826 if (adapter->priv_flags & IBMVNIC_USE_SERVER_MAXES) {
2827 channels->max_rx = adapter->max_rx_queues;
2828 channels->max_tx = adapter->max_tx_queues;
2829 } else {
2830 channels->max_rx = IBMVNIC_MAX_QUEUES;
2831 channels->max_tx = IBMVNIC_MAX_QUEUES;
2832 }
2833
John Allenc2dbeb62017-08-02 16:47:17 -05002834 channels->max_other = 0;
2835 channels->max_combined = 0;
2836 channels->rx_count = adapter->req_rx_queues;
2837 channels->tx_count = adapter->req_tx_queues;
2838 channels->other_count = 0;
2839 channels->combined_count = 0;
2840}
2841
John Allenc26eba02017-10-26 16:23:25 -05002842static int ibmvnic_set_channels(struct net_device *netdev,
2843 struct ethtool_channels *channels)
2844{
2845 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Thomas Falcon723ad912018-09-28 18:38:26 -05002846 int ret;
John Allenc26eba02017-10-26 16:23:25 -05002847
Thomas Falcon723ad912018-09-28 18:38:26 -05002848 ret = 0;
John Allenc26eba02017-10-26 16:23:25 -05002849 adapter->desired.rx_queues = channels->rx_count;
2850 adapter->desired.tx_queues = channels->tx_count;
2851
Thomas Falcon723ad912018-09-28 18:38:26 -05002852 ret = wait_for_reset(adapter);
2853
2854 if (!ret &&
2855 (adapter->req_rx_queues != channels->rx_count ||
2856 adapter->req_tx_queues != channels->tx_count))
2857 netdev_info(netdev,
2858 "Could not match full channels request. Requested: RX %d, TX %d; Allowed: RX %llu, TX %llu\n",
2859 channels->rx_count, channels->tx_count,
2860 adapter->req_rx_queues, adapter->req_tx_queues);
2861 return ret;
John Allenc26eba02017-10-26 16:23:25 -05002862}
2863
Thomas Falcon032c5e82015-12-21 11:26:06 -06002864static void ibmvnic_get_strings(struct net_device *dev, u32 stringset, u8 *data)
2865{
John Allen3d52b592017-08-02 16:44:14 -05002866 struct ibmvnic_adapter *adapter = netdev_priv(dev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002867 int i;
2868
Thomas Falcon723ad912018-09-28 18:38:26 -05002869 switch (stringset) {
2870 case ETH_SS_STATS:
2871 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats);
2872 i++, data += ETH_GSTRING_LEN)
2873 memcpy(data, ibmvnic_stats[i].name, ETH_GSTRING_LEN);
2874
2875 for (i = 0; i < adapter->req_tx_queues; i++) {
2876 snprintf(data, ETH_GSTRING_LEN, "tx%d_packets", i);
2877 data += ETH_GSTRING_LEN;
2878
2879 snprintf(data, ETH_GSTRING_LEN, "tx%d_bytes", i);
2880 data += ETH_GSTRING_LEN;
2881
2882 snprintf(data, ETH_GSTRING_LEN,
2883 "tx%d_dropped_packets", i);
2884 data += ETH_GSTRING_LEN;
2885 }
2886
2887 for (i = 0; i < adapter->req_rx_queues; i++) {
2888 snprintf(data, ETH_GSTRING_LEN, "rx%d_packets", i);
2889 data += ETH_GSTRING_LEN;
2890
2891 snprintf(data, ETH_GSTRING_LEN, "rx%d_bytes", i);
2892 data += ETH_GSTRING_LEN;
2893
2894 snprintf(data, ETH_GSTRING_LEN, "rx%d_interrupts", i);
2895 data += ETH_GSTRING_LEN;
2896 }
2897 break;
2898
2899 case ETH_SS_PRIV_FLAGS:
2900 for (i = 0; i < ARRAY_SIZE(ibmvnic_priv_flags); i++)
2901 strcpy(data + i * ETH_GSTRING_LEN,
2902 ibmvnic_priv_flags[i]);
2903 break;
2904 default:
Thomas Falcon032c5e82015-12-21 11:26:06 -06002905 return;
John Allen3d52b592017-08-02 16:44:14 -05002906 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002907}
2908
2909static int ibmvnic_get_sset_count(struct net_device *dev, int sset)
2910{
John Allen3d52b592017-08-02 16:44:14 -05002911 struct ibmvnic_adapter *adapter = netdev_priv(dev);
2912
Thomas Falcon032c5e82015-12-21 11:26:06 -06002913 switch (sset) {
2914 case ETH_SS_STATS:
John Allen3d52b592017-08-02 16:44:14 -05002915 return ARRAY_SIZE(ibmvnic_stats) +
2916 adapter->req_tx_queues * NUM_TX_STATS +
2917 adapter->req_rx_queues * NUM_RX_STATS;
Thomas Falcon723ad912018-09-28 18:38:26 -05002918 case ETH_SS_PRIV_FLAGS:
2919 return ARRAY_SIZE(ibmvnic_priv_flags);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002920 default:
2921 return -EOPNOTSUPP;
2922 }
2923}
2924
2925static void ibmvnic_get_ethtool_stats(struct net_device *dev,
2926 struct ethtool_stats *stats, u64 *data)
2927{
2928 struct ibmvnic_adapter *adapter = netdev_priv(dev);
2929 union ibmvnic_crq crq;
John Allen3d52b592017-08-02 16:44:14 -05002930 int i, j;
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05002931 int rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002932
2933 memset(&crq, 0, sizeof(crq));
2934 crq.request_statistics.first = IBMVNIC_CRQ_CMD;
2935 crq.request_statistics.cmd = REQUEST_STATISTICS;
2936 crq.request_statistics.ioba = cpu_to_be32(adapter->stats_token);
2937 crq.request_statistics.len =
2938 cpu_to_be32(sizeof(struct ibmvnic_statistics));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002939
2940 /* Wait for data to be written */
Thomas Falcon070eca92019-11-25 17:12:53 -06002941 reinit_completion(&adapter->stats_done);
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05002942 rc = ibmvnic_send_crq(adapter, &crq);
2943 if (rc)
2944 return;
Thomas Falcon476d96c2019-11-25 17:12:55 -06002945 rc = ibmvnic_wait_for_completion(adapter, &adapter->stats_done, 10000);
2946 if (rc)
2947 return;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002948
2949 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++)
Lijun Pan91dc5d22021-02-11 00:43:22 -06002950 data[i] = be64_to_cpu(IBMVNIC_GET_STAT
2951 (adapter, ibmvnic_stats[i].offset));
John Allen3d52b592017-08-02 16:44:14 -05002952
2953 for (j = 0; j < adapter->req_tx_queues; j++) {
2954 data[i] = adapter->tx_stats_buffers[j].packets;
2955 i++;
2956 data[i] = adapter->tx_stats_buffers[j].bytes;
2957 i++;
2958 data[i] = adapter->tx_stats_buffers[j].dropped_packets;
2959 i++;
2960 }
2961
2962 for (j = 0; j < adapter->req_rx_queues; j++) {
2963 data[i] = adapter->rx_stats_buffers[j].packets;
2964 i++;
2965 data[i] = adapter->rx_stats_buffers[j].bytes;
2966 i++;
2967 data[i] = adapter->rx_stats_buffers[j].interrupts;
2968 i++;
2969 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002970}
2971
Thomas Falcon723ad912018-09-28 18:38:26 -05002972static u32 ibmvnic_get_priv_flags(struct net_device *netdev)
2973{
2974 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2975
2976 return adapter->priv_flags;
2977}
2978
2979static int ibmvnic_set_priv_flags(struct net_device *netdev, u32 flags)
2980{
2981 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2982 bool which_maxes = !!(flags & IBMVNIC_USE_SERVER_MAXES);
2983
2984 if (which_maxes)
2985 adapter->priv_flags |= IBMVNIC_USE_SERVER_MAXES;
2986 else
2987 adapter->priv_flags &= ~IBMVNIC_USE_SERVER_MAXES;
2988
2989 return 0;
2990}
Lijun Pan91dc5d22021-02-11 00:43:22 -06002991
Thomas Falcon032c5e82015-12-21 11:26:06 -06002992static const struct ethtool_ops ibmvnic_ethtool_ops = {
Thomas Falcon032c5e82015-12-21 11:26:06 -06002993 .get_drvinfo = ibmvnic_get_drvinfo,
2994 .get_msglevel = ibmvnic_get_msglevel,
2995 .set_msglevel = ibmvnic_set_msglevel,
2996 .get_link = ibmvnic_get_link,
2997 .get_ringparam = ibmvnic_get_ringparam,
John Allenc26eba02017-10-26 16:23:25 -05002998 .set_ringparam = ibmvnic_set_ringparam,
John Allenc2dbeb62017-08-02 16:47:17 -05002999 .get_channels = ibmvnic_get_channels,
John Allenc26eba02017-10-26 16:23:25 -05003000 .set_channels = ibmvnic_set_channels,
Thomas Falcon032c5e82015-12-21 11:26:06 -06003001 .get_strings = ibmvnic_get_strings,
3002 .get_sset_count = ibmvnic_get_sset_count,
3003 .get_ethtool_stats = ibmvnic_get_ethtool_stats,
Philippe Reynes8a433792017-01-07 22:37:29 +01003004 .get_link_ksettings = ibmvnic_get_link_ksettings,
Thomas Falcon723ad912018-09-28 18:38:26 -05003005 .get_priv_flags = ibmvnic_get_priv_flags,
3006 .set_priv_flags = ibmvnic_set_priv_flags,
Thomas Falcon032c5e82015-12-21 11:26:06 -06003007};
3008
3009/* Routines for managing CRQs/sCRQs */
3010
Nathan Fontenot57a49432017-05-26 10:31:12 -04003011static int reset_one_sub_crq_queue(struct ibmvnic_adapter *adapter,
3012 struct ibmvnic_sub_crq_queue *scrq)
3013{
3014 int rc;
3015
Dany Madden9281cf22020-11-25 18:04:26 -06003016 if (!scrq) {
YANG LI862aecb2020-12-30 15:23:14 +08003017 netdev_dbg(adapter->netdev, "Invalid scrq reset.\n");
Dany Madden9281cf22020-11-25 18:04:26 -06003018 return -EINVAL;
3019 }
3020
Nathan Fontenot57a49432017-05-26 10:31:12 -04003021 if (scrq->irq) {
3022 free_irq(scrq->irq, scrq);
3023 irq_dispose_mapping(scrq->irq);
3024 scrq->irq = 0;
3025 }
3026
Dany Madden9281cf22020-11-25 18:04:26 -06003027 if (scrq->msgs) {
3028 memset(scrq->msgs, 0, 4 * PAGE_SIZE);
3029 atomic_set(&scrq->used, 0);
3030 scrq->cur = 0;
Jakub Kicinski55fd59b2020-12-03 15:42:13 -08003031 scrq->ind_buf.index = 0;
Dany Madden9281cf22020-11-25 18:04:26 -06003032 } else {
3033 netdev_dbg(adapter->netdev, "Invalid scrq reset\n");
3034 return -EINVAL;
3035 }
Nathan Fontenot57a49432017-05-26 10:31:12 -04003036
3037 rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
3038 4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
3039 return rc;
3040}
3041
3042static int reset_sub_crq_queues(struct ibmvnic_adapter *adapter)
3043{
3044 int i, rc;
3045
Lijun Pana0faaa22020-11-23 13:35:45 -06003046 if (!adapter->tx_scrq || !adapter->rx_scrq)
3047 return -EINVAL;
3048
Nathan Fontenot57a49432017-05-26 10:31:12 -04003049 for (i = 0; i < adapter->req_tx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05003050 netdev_dbg(adapter->netdev, "Re-setting tx_scrq[%d]\n", i);
Nathan Fontenot57a49432017-05-26 10:31:12 -04003051 rc = reset_one_sub_crq_queue(adapter, adapter->tx_scrq[i]);
3052 if (rc)
3053 return rc;
3054 }
3055
3056 for (i = 0; i < adapter->req_rx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05003057 netdev_dbg(adapter->netdev, "Re-setting rx_scrq[%d]\n", i);
Nathan Fontenot57a49432017-05-26 10:31:12 -04003058 rc = reset_one_sub_crq_queue(adapter, adapter->rx_scrq[i]);
3059 if (rc)
3060 return rc;
3061 }
3062
Nathan Fontenot57a49432017-05-26 10:31:12 -04003063 return rc;
3064}
3065
Thomas Falcon032c5e82015-12-21 11:26:06 -06003066static void release_sub_crq_queue(struct ibmvnic_adapter *adapter,
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06003067 struct ibmvnic_sub_crq_queue *scrq,
3068 bool do_h_free)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003069{
3070 struct device *dev = &adapter->vdev->dev;
3071 long rc;
3072
3073 netdev_dbg(adapter->netdev, "Releasing sub-CRQ\n");
3074
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06003075 if (do_h_free) {
3076 /* Close the sub-crqs */
3077 do {
3078 rc = plpar_hcall_norets(H_FREE_SUB_CRQ,
3079 adapter->vdev->unit_address,
3080 scrq->crq_num);
3081 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
Thomas Falcon032c5e82015-12-21 11:26:06 -06003082
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06003083 if (rc) {
3084 netdev_err(adapter->netdev,
3085 "Failed to release sub-CRQ %16lx, rc = %ld\n",
3086 scrq->crq_num, rc);
3087 }
Thomas Falconffa73852017-04-19 13:44:29 -04003088 }
3089
Thomas Falconf019fb62020-11-18 19:12:17 -06003090 dma_free_coherent(dev,
3091 IBMVNIC_IND_ARR_SZ,
3092 scrq->ind_buf.indir_arr,
3093 scrq->ind_buf.indir_dma);
3094
Thomas Falcon032c5e82015-12-21 11:26:06 -06003095 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
3096 DMA_BIDIRECTIONAL);
3097 free_pages((unsigned long)scrq->msgs, 2);
3098 kfree(scrq);
3099}
3100
3101static struct ibmvnic_sub_crq_queue *init_sub_crq_queue(struct ibmvnic_adapter
3102 *adapter)
3103{
3104 struct device *dev = &adapter->vdev->dev;
3105 struct ibmvnic_sub_crq_queue *scrq;
3106 int rc;
3107
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04003108 scrq = kzalloc(sizeof(*scrq), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003109 if (!scrq)
3110 return NULL;
3111
Nathan Fontenot7f7adc52017-04-19 13:45:16 -04003112 scrq->msgs =
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04003113 (union sub_crq *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, 2);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003114 if (!scrq->msgs) {
3115 dev_warn(dev, "Couldn't allocate crq queue messages page\n");
3116 goto zero_page_failed;
3117 }
3118
3119 scrq->msg_token = dma_map_single(dev, scrq->msgs, 4 * PAGE_SIZE,
3120 DMA_BIDIRECTIONAL);
3121 if (dma_mapping_error(dev, scrq->msg_token)) {
3122 dev_warn(dev, "Couldn't map crq queue messages page\n");
3123 goto map_failed;
3124 }
3125
3126 rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
3127 4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
3128
3129 if (rc == H_RESOURCE)
3130 rc = ibmvnic_reset_crq(adapter);
3131
3132 if (rc == H_CLOSED) {
3133 dev_warn(dev, "Partner adapter not ready, waiting.\n");
3134 } else if (rc) {
3135 dev_warn(dev, "Error %d registering sub-crq\n", rc);
3136 goto reg_failed;
3137 }
3138
Thomas Falcon032c5e82015-12-21 11:26:06 -06003139 scrq->adapter = adapter;
3140 scrq->size = 4 * PAGE_SIZE / sizeof(*scrq->msgs);
Thomas Falconf019fb62020-11-18 19:12:17 -06003141 scrq->ind_buf.index = 0;
3142
3143 scrq->ind_buf.indir_arr =
3144 dma_alloc_coherent(dev,
3145 IBMVNIC_IND_ARR_SZ,
3146 &scrq->ind_buf.indir_dma,
3147 GFP_KERNEL);
3148
3149 if (!scrq->ind_buf.indir_arr)
3150 goto indir_failed;
3151
Thomas Falcon032c5e82015-12-21 11:26:06 -06003152 spin_lock_init(&scrq->lock);
3153
3154 netdev_dbg(adapter->netdev,
3155 "sub-crq initialized, num %lx, hw_irq=%lx, irq=%x\n",
3156 scrq->crq_num, scrq->hw_irq, scrq->irq);
3157
3158 return scrq;
3159
Thomas Falconf019fb62020-11-18 19:12:17 -06003160indir_failed:
3161 do {
3162 rc = plpar_hcall_norets(H_FREE_SUB_CRQ,
3163 adapter->vdev->unit_address,
3164 scrq->crq_num);
3165 } while (rc == H_BUSY || rc == H_IS_LONG_BUSY(rc));
Thomas Falcon032c5e82015-12-21 11:26:06 -06003166reg_failed:
3167 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
3168 DMA_BIDIRECTIONAL);
3169map_failed:
3170 free_pages((unsigned long)scrq->msgs, 2);
3171zero_page_failed:
3172 kfree(scrq);
3173
3174 return NULL;
3175}
3176
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06003177static void release_sub_crqs(struct ibmvnic_adapter *adapter, bool do_h_free)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003178{
3179 int i;
3180
3181 if (adapter->tx_scrq) {
Nathan Fontenot82e3be32018-02-21 21:33:56 -06003182 for (i = 0; i < adapter->num_active_tx_scrqs; i++) {
Nathan Fontenotb5108882017-03-30 02:49:18 -04003183 if (!adapter->tx_scrq[i])
3184 continue;
3185
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05003186 netdev_dbg(adapter->netdev, "Releasing tx_scrq[%d]\n",
3187 i);
Sukadev Bhattiprolu65d64702021-06-23 21:13:12 -07003188 ibmvnic_tx_scrq_clean_buffer(adapter, adapter->tx_scrq[i]);
Nathan Fontenotb5108882017-03-30 02:49:18 -04003189 if (adapter->tx_scrq[i]->irq) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06003190 free_irq(adapter->tx_scrq[i]->irq,
3191 adapter->tx_scrq[i]);
Thomas Falcon88eb98a2016-07-06 15:35:16 -05003192 irq_dispose_mapping(adapter->tx_scrq[i]->irq);
Nathan Fontenotb5108882017-03-30 02:49:18 -04003193 adapter->tx_scrq[i]->irq = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003194 }
Nathan Fontenotb5108882017-03-30 02:49:18 -04003195
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06003196 release_sub_crq_queue(adapter, adapter->tx_scrq[i],
3197 do_h_free);
Nathan Fontenotb5108882017-03-30 02:49:18 -04003198 }
3199
Nathan Fontenot9501df32017-03-15 23:38:07 -04003200 kfree(adapter->tx_scrq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003201 adapter->tx_scrq = NULL;
Nathan Fontenot82e3be32018-02-21 21:33:56 -06003202 adapter->num_active_tx_scrqs = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003203 }
3204
3205 if (adapter->rx_scrq) {
Nathan Fontenot82e3be32018-02-21 21:33:56 -06003206 for (i = 0; i < adapter->num_active_rx_scrqs; i++) {
Nathan Fontenotb5108882017-03-30 02:49:18 -04003207 if (!adapter->rx_scrq[i])
3208 continue;
3209
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05003210 netdev_dbg(adapter->netdev, "Releasing rx_scrq[%d]\n",
3211 i);
Nathan Fontenotb5108882017-03-30 02:49:18 -04003212 if (adapter->rx_scrq[i]->irq) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06003213 free_irq(adapter->rx_scrq[i]->irq,
3214 adapter->rx_scrq[i]);
Thomas Falcon88eb98a2016-07-06 15:35:16 -05003215 irq_dispose_mapping(adapter->rx_scrq[i]->irq);
Nathan Fontenotb5108882017-03-30 02:49:18 -04003216 adapter->rx_scrq[i]->irq = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003217 }
Nathan Fontenotb5108882017-03-30 02:49:18 -04003218
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06003219 release_sub_crq_queue(adapter, adapter->rx_scrq[i],
3220 do_h_free);
Nathan Fontenotb5108882017-03-30 02:49:18 -04003221 }
3222
Nathan Fontenot9501df32017-03-15 23:38:07 -04003223 kfree(adapter->rx_scrq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003224 adapter->rx_scrq = NULL;
Nathan Fontenot82e3be32018-02-21 21:33:56 -06003225 adapter->num_active_rx_scrqs = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003226 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06003227}
3228
3229static int disable_scrq_irq(struct ibmvnic_adapter *adapter,
3230 struct ibmvnic_sub_crq_queue *scrq)
3231{
3232 struct device *dev = &adapter->vdev->dev;
3233 unsigned long rc;
3234
3235 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
3236 H_DISABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
3237 if (rc)
3238 dev_err(dev, "Couldn't disable scrq irq 0x%lx. rc=%ld\n",
3239 scrq->hw_irq, rc);
3240 return rc;
3241}
3242
3243static int enable_scrq_irq(struct ibmvnic_adapter *adapter,
3244 struct ibmvnic_sub_crq_queue *scrq)
3245{
3246 struct device *dev = &adapter->vdev->dev;
3247 unsigned long rc;
3248
3249 if (scrq->hw_irq > 0x100000000ULL) {
3250 dev_err(dev, "bad hw_irq = %lx\n", scrq->hw_irq);
3251 return 1;
3252 }
3253
Juliet Kim7ed5b312019-09-20 16:11:23 -04003254 if (test_bit(0, &adapter->resetting) &&
Nathan Fontenot73f9d362018-05-22 11:21:10 -05003255 adapter->reset_reason == VNIC_RESET_MOBILITY) {
Juliet Kim284f87d2019-11-20 10:50:03 -05003256 u64 val = (0xff000000) | scrq->hw_irq;
Nathan Fontenot73f9d362018-05-22 11:21:10 -05003257
Juliet Kim284f87d2019-11-20 10:50:03 -05003258 rc = plpar_hcall_norets(H_EOI, val);
Juliet Kim2df5c602019-11-20 10:50:04 -05003259 /* H_EOI would fail with rc = H_FUNCTION when running
3260 * in XIVE mode which is expected, but not an error.
3261 */
Lijun Pan91dc5d22021-02-11 00:43:22 -06003262 if (rc && rc != H_FUNCTION)
Juliet Kim284f87d2019-11-20 10:50:03 -05003263 dev_err(dev, "H_EOI FAILED irq 0x%llx. rc=%ld\n",
3264 val, rc);
Nathan Fontenot73f9d362018-05-22 11:21:10 -05003265 }
Thomas Falconf23e0642018-04-15 18:53:36 -05003266
Thomas Falcon032c5e82015-12-21 11:26:06 -06003267 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
3268 H_ENABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
3269 if (rc)
3270 dev_err(dev, "Couldn't enable scrq irq 0x%lx. rc=%ld\n",
3271 scrq->hw_irq, rc);
3272 return rc;
3273}
3274
3275static int ibmvnic_complete_tx(struct ibmvnic_adapter *adapter,
3276 struct ibmvnic_sub_crq_queue *scrq)
3277{
3278 struct device *dev = &adapter->vdev->dev;
Thomas Falcon06b3e352018-03-16 20:00:28 -05003279 struct ibmvnic_tx_pool *tx_pool;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003280 struct ibmvnic_tx_buff *txbuff;
Thomas Falcon0d973382020-11-18 19:12:19 -06003281 struct netdev_queue *txq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003282 union sub_crq *next;
3283 int index;
Thomas Falconc62aa372020-11-18 19:12:20 -06003284 int i;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003285
3286restart_loop:
3287 while (pending_scrq(adapter, scrq)) {
3288 unsigned int pool = scrq->pool_index;
Thomas Falconffc385b2018-02-18 10:08:41 -06003289 int num_entries = 0;
Thomas Falcon0d973382020-11-18 19:12:19 -06003290 int total_bytes = 0;
3291 int num_packets = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003292
3293 next = ibmvnic_next_scrq(adapter, scrq);
3294 for (i = 0; i < next->tx_comp.num_comps; i++) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06003295 index = be32_to_cpu(next->tx_comp.correlators[i]);
Thomas Falcon06b3e352018-03-16 20:00:28 -05003296 if (index & IBMVNIC_TSO_POOL_MASK) {
3297 tx_pool = &adapter->tso_pool[pool];
3298 index &= ~IBMVNIC_TSO_POOL_MASK;
3299 } else {
3300 tx_pool = &adapter->tx_pool[pool];
3301 }
3302
3303 txbuff = &tx_pool->tx_buff[index];
Thomas Falcon0d973382020-11-18 19:12:19 -06003304 num_packets++;
Thomas Falconffc385b2018-02-18 10:08:41 -06003305 num_entries += txbuff->num_entries;
Thomas Falcon0d973382020-11-18 19:12:19 -06003306 if (txbuff->skb) {
3307 total_bytes += txbuff->skb->len;
Lijun Panca09bf72021-04-13 03:33:25 -05003308 if (next->tx_comp.rcs[i]) {
3309 dev_err(dev, "tx error %x\n",
3310 next->tx_comp.rcs[i]);
3311 dev_kfree_skb_irq(txbuff->skb);
3312 } else {
3313 dev_consume_skb_irq(txbuff->skb);
3314 }
Thomas Falcon0d973382020-11-18 19:12:19 -06003315 txbuff->skb = NULL;
3316 } else {
3317 netdev_warn(adapter->netdev,
3318 "TX completion received with NULL socket buffer\n");
3319 }
Thomas Falcon06b3e352018-03-16 20:00:28 -05003320 tx_pool->free_map[tx_pool->producer_index] = index;
3321 tx_pool->producer_index =
3322 (tx_pool->producer_index + 1) %
3323 tx_pool->num_buffers;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003324 }
3325 /* remove tx_comp scrq*/
3326 next->tx_comp.first = 0;
Nathan Fontenot7c3e7de2017-05-03 14:05:25 -04003327
Thomas Falcon0d973382020-11-18 19:12:19 -06003328 txq = netdev_get_tx_queue(adapter->netdev, scrq->pool_index);
3329 netdev_tx_completed_queue(txq, num_packets, total_bytes);
3330
Thomas Falconffc385b2018-02-18 10:08:41 -06003331 if (atomic_sub_return(num_entries, &scrq->used) <=
Nathan Fontenot7c3e7de2017-05-03 14:05:25 -04003332 (adapter->req_tx_entries_per_subcrq / 2) &&
3333 __netif_subqueue_stopped(adapter->netdev,
3334 scrq->pool_index)) {
3335 netif_wake_subqueue(adapter->netdev, scrq->pool_index);
Thomas Falcon0aecb132018-02-26 18:10:58 -06003336 netdev_dbg(adapter->netdev, "Started queue %d\n",
3337 scrq->pool_index);
Nathan Fontenot7c3e7de2017-05-03 14:05:25 -04003338 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06003339 }
3340
3341 enable_scrq_irq(adapter, scrq);
3342
3343 if (pending_scrq(adapter, scrq)) {
3344 disable_scrq_irq(adapter, scrq);
3345 goto restart_loop;
3346 }
3347
3348 return 0;
3349}
3350
3351static irqreturn_t ibmvnic_interrupt_tx(int irq, void *instance)
3352{
3353 struct ibmvnic_sub_crq_queue *scrq = instance;
3354 struct ibmvnic_adapter *adapter = scrq->adapter;
3355
3356 disable_scrq_irq(adapter, scrq);
3357 ibmvnic_complete_tx(adapter, scrq);
3358
3359 return IRQ_HANDLED;
3360}
3361
3362static irqreturn_t ibmvnic_interrupt_rx(int irq, void *instance)
3363{
3364 struct ibmvnic_sub_crq_queue *scrq = instance;
3365 struct ibmvnic_adapter *adapter = scrq->adapter;
3366
Nathan Fontenot09fb35e2018-01-10 10:40:09 -06003367 /* When booting a kdump kernel we can hit pending interrupts
3368 * prior to completing driver initialization.
3369 */
3370 if (unlikely(adapter->state != VNIC_OPEN))
3371 return IRQ_NONE;
3372
John Allen3d52b592017-08-02 16:44:14 -05003373 adapter->rx_stats_buffers[scrq->scrq_num].interrupts++;
3374
Thomas Falcon032c5e82015-12-21 11:26:06 -06003375 if (napi_schedule_prep(&adapter->napi[scrq->scrq_num])) {
3376 disable_scrq_irq(adapter, scrq);
3377 __napi_schedule(&adapter->napi[scrq->scrq_num]);
3378 }
3379
3380 return IRQ_HANDLED;
3381}
3382
Thomas Falconea22d512016-07-06 15:35:17 -05003383static int init_sub_crq_irqs(struct ibmvnic_adapter *adapter)
3384{
3385 struct device *dev = &adapter->vdev->dev;
3386 struct ibmvnic_sub_crq_queue *scrq;
3387 int i = 0, j = 0;
3388 int rc = 0;
3389
3390 for (i = 0; i < adapter->req_tx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05003391 netdev_dbg(adapter->netdev, "Initializing tx_scrq[%d] irq\n",
3392 i);
Thomas Falconea22d512016-07-06 15:35:17 -05003393 scrq = adapter->tx_scrq[i];
3394 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
3395
Michael Ellerman99c17902016-09-10 19:59:05 +10003396 if (!scrq->irq) {
Thomas Falconea22d512016-07-06 15:35:17 -05003397 rc = -EINVAL;
3398 dev_err(dev, "Error mapping irq\n");
3399 goto req_tx_irq_failed;
3400 }
3401
Murilo Fossa Vicentinie56e2512019-04-25 11:02:33 -03003402 snprintf(scrq->name, sizeof(scrq->name), "ibmvnic-%x-tx%d",
3403 adapter->vdev->unit_address, i);
Thomas Falconea22d512016-07-06 15:35:17 -05003404 rc = request_irq(scrq->irq, ibmvnic_interrupt_tx,
Murilo Fossa Vicentinie56e2512019-04-25 11:02:33 -03003405 0, scrq->name, scrq);
Thomas Falconea22d512016-07-06 15:35:17 -05003406
3407 if (rc) {
3408 dev_err(dev, "Couldn't register tx irq 0x%x. rc=%d\n",
3409 scrq->irq, rc);
3410 irq_dispose_mapping(scrq->irq);
Nathan Fontenotaf9090c2018-02-20 11:04:18 -06003411 goto req_tx_irq_failed;
Thomas Falconea22d512016-07-06 15:35:17 -05003412 }
3413 }
3414
3415 for (i = 0; i < adapter->req_rx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05003416 netdev_dbg(adapter->netdev, "Initializing rx_scrq[%d] irq\n",
3417 i);
Thomas Falconea22d512016-07-06 15:35:17 -05003418 scrq = adapter->rx_scrq[i];
3419 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
Michael Ellerman99c17902016-09-10 19:59:05 +10003420 if (!scrq->irq) {
Thomas Falconea22d512016-07-06 15:35:17 -05003421 rc = -EINVAL;
3422 dev_err(dev, "Error mapping irq\n");
3423 goto req_rx_irq_failed;
3424 }
Murilo Fossa Vicentinie56e2512019-04-25 11:02:33 -03003425 snprintf(scrq->name, sizeof(scrq->name), "ibmvnic-%x-rx%d",
3426 adapter->vdev->unit_address, i);
Thomas Falconea22d512016-07-06 15:35:17 -05003427 rc = request_irq(scrq->irq, ibmvnic_interrupt_rx,
Murilo Fossa Vicentinie56e2512019-04-25 11:02:33 -03003428 0, scrq->name, scrq);
Thomas Falconea22d512016-07-06 15:35:17 -05003429 if (rc) {
3430 dev_err(dev, "Couldn't register rx irq 0x%x. rc=%d\n",
3431 scrq->irq, rc);
3432 irq_dispose_mapping(scrq->irq);
3433 goto req_rx_irq_failed;
3434 }
3435 }
3436 return rc;
3437
3438req_rx_irq_failed:
Thomas Falcon8bf371e2016-10-27 12:28:52 -05003439 for (j = 0; j < i; j++) {
Thomas Falconea22d512016-07-06 15:35:17 -05003440 free_irq(adapter->rx_scrq[j]->irq, adapter->rx_scrq[j]);
3441 irq_dispose_mapping(adapter->rx_scrq[j]->irq);
Thomas Falcon8bf371e2016-10-27 12:28:52 -05003442 }
Thomas Falconea22d512016-07-06 15:35:17 -05003443 i = adapter->req_tx_queues;
3444req_tx_irq_failed:
Thomas Falcon8bf371e2016-10-27 12:28:52 -05003445 for (j = 0; j < i; j++) {
Thomas Falconea22d512016-07-06 15:35:17 -05003446 free_irq(adapter->tx_scrq[j]->irq, adapter->tx_scrq[j]);
Thomas Falcon27a21452020-07-29 16:36:32 -05003447 irq_dispose_mapping(adapter->tx_scrq[j]->irq);
Thomas Falcon8bf371e2016-10-27 12:28:52 -05003448 }
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06003449 release_sub_crqs(adapter, 1);
Thomas Falconea22d512016-07-06 15:35:17 -05003450 return rc;
3451}
3452
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003453static int init_sub_crqs(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003454{
3455 struct device *dev = &adapter->vdev->dev;
3456 struct ibmvnic_sub_crq_queue **allqueues;
3457 int registered_queues = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003458 int total_queues;
3459 int more = 0;
Thomas Falconea22d512016-07-06 15:35:17 -05003460 int i;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003461
Thomas Falcon032c5e82015-12-21 11:26:06 -06003462 total_queues = adapter->req_tx_queues + adapter->req_rx_queues;
3463
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04003464 allqueues = kcalloc(total_queues, sizeof(*allqueues), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003465 if (!allqueues)
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003466 return -1;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003467
3468 for (i = 0; i < total_queues; i++) {
3469 allqueues[i] = init_sub_crq_queue(adapter);
3470 if (!allqueues[i]) {
3471 dev_warn(dev, "Couldn't allocate all sub-crqs\n");
3472 break;
3473 }
3474 registered_queues++;
3475 }
3476
3477 /* Make sure we were able to register the minimum number of queues */
3478 if (registered_queues <
3479 adapter->min_tx_queues + adapter->min_rx_queues) {
3480 dev_err(dev, "Fatal: Couldn't init min number of sub-crqs\n");
3481 goto tx_failed;
3482 }
3483
3484 /* Distribute the failed allocated queues*/
3485 for (i = 0; i < total_queues - registered_queues + more ; i++) {
3486 netdev_dbg(adapter->netdev, "Reducing number of queues\n");
3487 switch (i % 3) {
3488 case 0:
3489 if (adapter->req_rx_queues > adapter->min_rx_queues)
3490 adapter->req_rx_queues--;
3491 else
3492 more++;
3493 break;
3494 case 1:
3495 if (adapter->req_tx_queues > adapter->min_tx_queues)
3496 adapter->req_tx_queues--;
3497 else
3498 more++;
3499 break;
3500 }
3501 }
3502
3503 adapter->tx_scrq = kcalloc(adapter->req_tx_queues,
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04003504 sizeof(*adapter->tx_scrq), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003505 if (!adapter->tx_scrq)
3506 goto tx_failed;
3507
3508 for (i = 0; i < adapter->req_tx_queues; i++) {
3509 adapter->tx_scrq[i] = allqueues[i];
3510 adapter->tx_scrq[i]->pool_index = i;
Nathan Fontenot82e3be32018-02-21 21:33:56 -06003511 adapter->num_active_tx_scrqs++;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003512 }
3513
3514 adapter->rx_scrq = kcalloc(adapter->req_rx_queues,
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04003515 sizeof(*adapter->rx_scrq), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003516 if (!adapter->rx_scrq)
3517 goto rx_failed;
3518
3519 for (i = 0; i < adapter->req_rx_queues; i++) {
3520 adapter->rx_scrq[i] = allqueues[i + adapter->req_tx_queues];
3521 adapter->rx_scrq[i]->scrq_num = i;
Nathan Fontenot82e3be32018-02-21 21:33:56 -06003522 adapter->num_active_rx_scrqs++;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003523 }
3524
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003525 kfree(allqueues);
3526 return 0;
3527
3528rx_failed:
3529 kfree(adapter->tx_scrq);
3530 adapter->tx_scrq = NULL;
3531tx_failed:
3532 for (i = 0; i < registered_queues; i++)
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06003533 release_sub_crq_queue(adapter, allqueues[i], 1);
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003534 kfree(allqueues);
3535 return -1;
3536}
3537
Lijun Pan09081b92020-09-27 20:13:27 -05003538static void send_request_cap(struct ibmvnic_adapter *adapter, int retry)
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003539{
3540 struct device *dev = &adapter->vdev->dev;
3541 union ibmvnic_crq crq;
John Allenc26eba02017-10-26 16:23:25 -05003542 int max_entries;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003543
3544 if (!retry) {
3545 /* Sub-CRQ entries are 32 byte long */
3546 int entries_page = 4 * PAGE_SIZE / (sizeof(u64) * 4);
3547
3548 if (adapter->min_tx_entries_per_subcrq > entries_page ||
3549 adapter->min_rx_add_entries_per_subcrq > entries_page) {
3550 dev_err(dev, "Fatal, invalid entries per sub-crq\n");
3551 return;
3552 }
3553
John Allenc26eba02017-10-26 16:23:25 -05003554 if (adapter->desired.mtu)
3555 adapter->req_mtu = adapter->desired.mtu;
3556 else
3557 adapter->req_mtu = adapter->netdev->mtu + ETH_HLEN;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003558
John Allenc26eba02017-10-26 16:23:25 -05003559 if (!adapter->desired.tx_entries)
3560 adapter->desired.tx_entries =
3561 adapter->max_tx_entries_per_subcrq;
3562 if (!adapter->desired.rx_entries)
3563 adapter->desired.rx_entries =
3564 adapter->max_rx_add_entries_per_subcrq;
3565
3566 max_entries = IBMVNIC_MAX_LTB_SIZE /
3567 (adapter->req_mtu + IBMVNIC_BUFFER_HLEN);
3568
3569 if ((adapter->req_mtu + IBMVNIC_BUFFER_HLEN) *
3570 adapter->desired.tx_entries > IBMVNIC_MAX_LTB_SIZE) {
3571 adapter->desired.tx_entries = max_entries;
3572 }
3573
3574 if ((adapter->req_mtu + IBMVNIC_BUFFER_HLEN) *
3575 adapter->desired.rx_entries > IBMVNIC_MAX_LTB_SIZE) {
3576 adapter->desired.rx_entries = max_entries;
3577 }
3578
3579 if (adapter->desired.tx_entries)
3580 adapter->req_tx_entries_per_subcrq =
3581 adapter->desired.tx_entries;
3582 else
3583 adapter->req_tx_entries_per_subcrq =
3584 adapter->max_tx_entries_per_subcrq;
3585
3586 if (adapter->desired.rx_entries)
3587 adapter->req_rx_add_entries_per_subcrq =
3588 adapter->desired.rx_entries;
3589 else
3590 adapter->req_rx_add_entries_per_subcrq =
3591 adapter->max_rx_add_entries_per_subcrq;
3592
3593 if (adapter->desired.tx_queues)
3594 adapter->req_tx_queues =
3595 adapter->desired.tx_queues;
3596 else
3597 adapter->req_tx_queues =
3598 adapter->opt_tx_comp_sub_queues;
3599
3600 if (adapter->desired.rx_queues)
3601 adapter->req_rx_queues =
3602 adapter->desired.rx_queues;
3603 else
3604 adapter->req_rx_queues =
3605 adapter->opt_rx_comp_queues;
3606
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003607 adapter->req_rx_add_queues = adapter->max_rx_add_queues;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003608 }
3609
Thomas Falcon032c5e82015-12-21 11:26:06 -06003610 memset(&crq, 0, sizeof(crq));
3611 crq.request_capability.first = IBMVNIC_CRQ_CMD;
3612 crq.request_capability.cmd = REQUEST_CAPABILITY;
3613
3614 crq.request_capability.capability = cpu_to_be16(REQ_TX_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06003615 crq.request_capability.number = cpu_to_be64(adapter->req_tx_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06003616 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003617 ibmvnic_send_crq(adapter, &crq);
3618
3619 crq.request_capability.capability = cpu_to_be16(REQ_RX_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06003620 crq.request_capability.number = cpu_to_be64(adapter->req_rx_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06003621 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003622 ibmvnic_send_crq(adapter, &crq);
3623
3624 crq.request_capability.capability = cpu_to_be16(REQ_RX_ADD_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06003625 crq.request_capability.number = cpu_to_be64(adapter->req_rx_add_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06003626 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003627 ibmvnic_send_crq(adapter, &crq);
3628
3629 crq.request_capability.capability =
3630 cpu_to_be16(REQ_TX_ENTRIES_PER_SUBCRQ);
3631 crq.request_capability.number =
Thomas Falconde89e852016-03-01 10:20:09 -06003632 cpu_to_be64(adapter->req_tx_entries_per_subcrq);
Thomas Falcon901e0402017-02-15 12:17:59 -06003633 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003634 ibmvnic_send_crq(adapter, &crq);
3635
3636 crq.request_capability.capability =
3637 cpu_to_be16(REQ_RX_ADD_ENTRIES_PER_SUBCRQ);
3638 crq.request_capability.number =
Thomas Falconde89e852016-03-01 10:20:09 -06003639 cpu_to_be64(adapter->req_rx_add_entries_per_subcrq);
Thomas Falcon901e0402017-02-15 12:17:59 -06003640 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003641 ibmvnic_send_crq(adapter, &crq);
3642
3643 crq.request_capability.capability = cpu_to_be16(REQ_MTU);
Thomas Falconde89e852016-03-01 10:20:09 -06003644 crq.request_capability.number = cpu_to_be64(adapter->req_mtu);
Thomas Falcon901e0402017-02-15 12:17:59 -06003645 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003646 ibmvnic_send_crq(adapter, &crq);
3647
3648 if (adapter->netdev->flags & IFF_PROMISC) {
3649 if (adapter->promisc_supported) {
3650 crq.request_capability.capability =
3651 cpu_to_be16(PROMISC_REQUESTED);
Thomas Falconde89e852016-03-01 10:20:09 -06003652 crq.request_capability.number = cpu_to_be64(1);
Thomas Falcon901e0402017-02-15 12:17:59 -06003653 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003654 ibmvnic_send_crq(adapter, &crq);
3655 }
3656 } else {
3657 crq.request_capability.capability =
3658 cpu_to_be16(PROMISC_REQUESTED);
Thomas Falconde89e852016-03-01 10:20:09 -06003659 crq.request_capability.number = cpu_to_be64(0);
Thomas Falcon901e0402017-02-15 12:17:59 -06003660 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003661 ibmvnic_send_crq(adapter, &crq);
3662 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06003663}
3664
3665static int pending_scrq(struct ibmvnic_adapter *adapter,
3666 struct ibmvnic_sub_crq_queue *scrq)
3667{
3668 union sub_crq *entry = &scrq->msgs[scrq->cur];
Lijun Pan665ab1e2021-01-29 19:19:04 -06003669 int rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003670
Lijun Pan665ab1e2021-01-29 19:19:04 -06003671 rc = !!(entry->generic.first & IBMVNIC_CRQ_CMD_RSP);
3672
3673 /* Ensure that the SCRQ valid flag is loaded prior to loading the
3674 * contents of the SCRQ descriptor
3675 */
3676 dma_rmb();
3677
3678 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003679}
3680
3681static union sub_crq *ibmvnic_next_scrq(struct ibmvnic_adapter *adapter,
3682 struct ibmvnic_sub_crq_queue *scrq)
3683{
3684 union sub_crq *entry;
3685 unsigned long flags;
3686
3687 spin_lock_irqsave(&scrq->lock, flags);
3688 entry = &scrq->msgs[scrq->cur];
3689 if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP) {
3690 if (++scrq->cur == scrq->size)
3691 scrq->cur = 0;
3692 } else {
3693 entry = NULL;
3694 }
3695 spin_unlock_irqrestore(&scrq->lock, flags);
3696
Lijun Pan665ab1e2021-01-29 19:19:04 -06003697 /* Ensure that the SCRQ valid flag is loaded prior to loading the
3698 * contents of the SCRQ descriptor
Thomas Falconb71ec952020-12-01 09:52:10 -06003699 */
3700 dma_rmb();
3701
Thomas Falcon032c5e82015-12-21 11:26:06 -06003702 return entry;
3703}
3704
3705static union ibmvnic_crq *ibmvnic_next_crq(struct ibmvnic_adapter *adapter)
3706{
3707 struct ibmvnic_crq_queue *queue = &adapter->crq;
3708 union ibmvnic_crq *crq;
3709
3710 crq = &queue->msgs[queue->cur];
3711 if (crq->generic.first & IBMVNIC_CRQ_CMD_RSP) {
3712 if (++queue->cur == queue->size)
3713 queue->cur = 0;
3714 } else {
3715 crq = NULL;
3716 }
3717
3718 return crq;
3719}
3720
Thomas Falcon2d14d372018-07-13 12:03:32 -05003721static void print_subcrq_error(struct device *dev, int rc, const char *func)
3722{
3723 switch (rc) {
3724 case H_PARAMETER:
3725 dev_warn_ratelimited(dev,
3726 "%s failed: Send request is malformed or adapter failover pending. (rc=%d)\n",
3727 func, rc);
3728 break;
3729 case H_CLOSED:
3730 dev_warn_ratelimited(dev,
3731 "%s failed: Backing queue closed. Adapter is down or failover pending. (rc=%d)\n",
3732 func, rc);
3733 break;
3734 default:
3735 dev_err_ratelimited(dev, "%s failed: (rc=%d)\n", func, rc);
3736 break;
3737 }
3738}
3739
Thomas Falconad7775d2016-04-01 17:20:34 -05003740static int send_subcrq_indirect(struct ibmvnic_adapter *adapter,
3741 u64 remote_handle, u64 ioba, u64 num_entries)
3742{
3743 unsigned int ua = adapter->vdev->unit_address;
3744 struct device *dev = &adapter->vdev->dev;
3745 int rc;
3746
3747 /* Make sure the hypervisor sees the complete request */
Lijun Pan1a421562021-02-12 20:36:46 -06003748 dma_wmb();
Thomas Falconad7775d2016-04-01 17:20:34 -05003749 rc = plpar_hcall_norets(H_SEND_SUB_CRQ_INDIRECT, ua,
3750 cpu_to_be64(remote_handle),
3751 ioba, num_entries);
3752
Thomas Falcon2d14d372018-07-13 12:03:32 -05003753 if (rc)
3754 print_subcrq_error(dev, rc, __func__);
Thomas Falconad7775d2016-04-01 17:20:34 -05003755
3756 return rc;
3757}
3758
Thomas Falcon032c5e82015-12-21 11:26:06 -06003759static int ibmvnic_send_crq(struct ibmvnic_adapter *adapter,
3760 union ibmvnic_crq *crq)
3761{
3762 unsigned int ua = adapter->vdev->unit_address;
3763 struct device *dev = &adapter->vdev->dev;
3764 u64 *u64_crq = (u64 *)crq;
3765 int rc;
3766
3767 netdev_dbg(adapter->netdev, "Sending CRQ: %016lx %016lx\n",
Lijun Pan429aa362021-02-11 00:43:18 -06003768 (unsigned long)cpu_to_be64(u64_crq[0]),
3769 (unsigned long)cpu_to_be64(u64_crq[1]));
Thomas Falcon032c5e82015-12-21 11:26:06 -06003770
Thomas Falcon51536982018-05-23 13:37:56 -05003771 if (!adapter->crq.active &&
3772 crq->generic.first != IBMVNIC_CRQ_INIT_CMD) {
3773 dev_warn(dev, "Invalid request detected while CRQ is inactive, possible device state change during reset\n");
3774 return -EINVAL;
3775 }
3776
Thomas Falcon032c5e82015-12-21 11:26:06 -06003777 /* Make sure the hypervisor sees the complete request */
Lijun Pan1a421562021-02-12 20:36:46 -06003778 dma_wmb();
Thomas Falcon032c5e82015-12-21 11:26:06 -06003779
3780 rc = plpar_hcall_norets(H_SEND_CRQ, ua,
3781 cpu_to_be64(u64_crq[0]),
3782 cpu_to_be64(u64_crq[1]));
3783
3784 if (rc) {
Nathan Fontenotec95dff2018-02-07 13:00:24 -06003785 if (rc == H_CLOSED) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06003786 dev_warn(dev, "CRQ Queue closed\n");
Lijun Panfa68bfa2020-08-19 17:52:24 -05003787 /* do not reset, report the fail, wait for passive init from server */
Nathan Fontenotec95dff2018-02-07 13:00:24 -06003788 }
3789
Thomas Falcon032c5e82015-12-21 11:26:06 -06003790 dev_warn(dev, "Send error (rc=%d)\n", rc);
3791 }
3792
3793 return rc;
3794}
3795
3796static int ibmvnic_send_crq_init(struct ibmvnic_adapter *adapter)
3797{
Thomas Falcon36a782f2020-08-31 11:59:57 -05003798 struct device *dev = &adapter->vdev->dev;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003799 union ibmvnic_crq crq;
Thomas Falcon36a782f2020-08-31 11:59:57 -05003800 int retries = 100;
3801 int rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003802
3803 memset(&crq, 0, sizeof(crq));
3804 crq.generic.first = IBMVNIC_CRQ_INIT_CMD;
3805 crq.generic.cmd = IBMVNIC_CRQ_INIT;
3806 netdev_dbg(adapter->netdev, "Sending CRQ init\n");
3807
Thomas Falcon36a782f2020-08-31 11:59:57 -05003808 do {
3809 rc = ibmvnic_send_crq(adapter, &crq);
3810 if (rc != H_CLOSED)
3811 break;
3812 retries--;
3813 msleep(50);
3814
3815 } while (retries > 0);
3816
3817 if (rc) {
3818 dev_err(dev, "Failed to send init request, rc = %d\n", rc);
3819 return rc;
3820 }
3821
3822 return 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003823}
3824
Thomas Falcon032c5e82015-12-21 11:26:06 -06003825static int send_version_xchg(struct ibmvnic_adapter *adapter)
3826{
3827 union ibmvnic_crq crq;
3828
3829 memset(&crq, 0, sizeof(crq));
3830 crq.version_exchange.first = IBMVNIC_CRQ_CMD;
3831 crq.version_exchange.cmd = VERSION_EXCHANGE;
3832 crq.version_exchange.version = cpu_to_be16(ibmvnic_version);
3833
3834 return ibmvnic_send_crq(adapter, &crq);
3835}
3836
Nathan Fontenot37798d02017-11-08 11:23:56 -06003837struct vnic_login_client_data {
3838 u8 type;
3839 __be16 len;
Kees Cook08ea5562018-04-10 15:26:43 -07003840 char name[];
Nathan Fontenot37798d02017-11-08 11:23:56 -06003841} __packed;
3842
3843static int vnic_client_data_len(struct ibmvnic_adapter *adapter)
3844{
3845 int len;
3846
3847 /* Calculate the amount of buffer space needed for the
3848 * vnic client data in the login buffer. There are four entries,
3849 * OS name, LPAR name, device name, and a null last entry.
3850 */
3851 len = 4 * sizeof(struct vnic_login_client_data);
3852 len += 6; /* "Linux" plus NULL */
3853 len += strlen(utsname()->nodename) + 1;
3854 len += strlen(adapter->netdev->name) + 1;
3855
3856 return len;
3857}
3858
3859static void vnic_add_client_data(struct ibmvnic_adapter *adapter,
3860 struct vnic_login_client_data *vlcd)
3861{
3862 const char *os_name = "Linux";
3863 int len;
3864
3865 /* Type 1 - LPAR OS */
3866 vlcd->type = 1;
3867 len = strlen(os_name) + 1;
3868 vlcd->len = cpu_to_be16(len);
Kees Cook08ea5562018-04-10 15:26:43 -07003869 strncpy(vlcd->name, os_name, len);
3870 vlcd = (struct vnic_login_client_data *)(vlcd->name + len);
Nathan Fontenot37798d02017-11-08 11:23:56 -06003871
3872 /* Type 2 - LPAR name */
3873 vlcd->type = 2;
3874 len = strlen(utsname()->nodename) + 1;
3875 vlcd->len = cpu_to_be16(len);
Kees Cook08ea5562018-04-10 15:26:43 -07003876 strncpy(vlcd->name, utsname()->nodename, len);
3877 vlcd = (struct vnic_login_client_data *)(vlcd->name + len);
Nathan Fontenot37798d02017-11-08 11:23:56 -06003878
3879 /* Type 3 - device name */
3880 vlcd->type = 3;
3881 len = strlen(adapter->netdev->name) + 1;
3882 vlcd->len = cpu_to_be16(len);
Kees Cook08ea5562018-04-10 15:26:43 -07003883 strncpy(vlcd->name, adapter->netdev->name, len);
Nathan Fontenot37798d02017-11-08 11:23:56 -06003884}
3885
Thomas Falcon20a8ab72018-02-26 18:10:59 -06003886static int send_login(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003887{
3888 struct ibmvnic_login_rsp_buffer *login_rsp_buffer;
3889 struct ibmvnic_login_buffer *login_buffer;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003890 struct device *dev = &adapter->vdev->dev;
Dany Maddenc98d9cc2020-11-25 18:04:30 -06003891 struct vnic_login_client_data *vlcd;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003892 dma_addr_t rsp_buffer_token;
3893 dma_addr_t buffer_token;
3894 size_t rsp_buffer_size;
3895 union ibmvnic_crq crq;
Dany Maddenc98d9cc2020-11-25 18:04:30 -06003896 int client_data_len;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003897 size_t buffer_size;
3898 __be64 *tx_list_p;
3899 __be64 *rx_list_p;
Dany Maddenc98d9cc2020-11-25 18:04:30 -06003900 int rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003901 int i;
3902
Thomas Falcon20a8ab72018-02-26 18:10:59 -06003903 if (!adapter->tx_scrq || !adapter->rx_scrq) {
3904 netdev_err(adapter->netdev,
3905 "RX or TX queues are not allocated, device login failed\n");
3906 return -1;
3907 }
3908
Lijun Pana0c8be52020-12-19 15:39:19 -06003909 release_login_buffer(adapter);
Thomas Falcon34f0f4e2018-02-13 18:23:40 -06003910 release_login_rsp_buffer(adapter);
Lijun Pana0c8be52020-12-19 15:39:19 -06003911
Nathan Fontenot37798d02017-11-08 11:23:56 -06003912 client_data_len = vnic_client_data_len(adapter);
3913
Thomas Falcon032c5e82015-12-21 11:26:06 -06003914 buffer_size =
3915 sizeof(struct ibmvnic_login_buffer) +
Nathan Fontenot37798d02017-11-08 11:23:56 -06003916 sizeof(u64) * (adapter->req_tx_queues + adapter->req_rx_queues) +
3917 client_data_len;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003918
Nathan Fontenot37798d02017-11-08 11:23:56 -06003919 login_buffer = kzalloc(buffer_size, GFP_ATOMIC);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003920 if (!login_buffer)
3921 goto buf_alloc_failed;
3922
3923 buffer_token = dma_map_single(dev, login_buffer, buffer_size,
3924 DMA_TO_DEVICE);
3925 if (dma_mapping_error(dev, buffer_token)) {
3926 dev_err(dev, "Couldn't map login buffer\n");
3927 goto buf_map_failed;
3928 }
3929
John Allen498cd8e2016-04-06 11:49:55 -05003930 rsp_buffer_size = sizeof(struct ibmvnic_login_rsp_buffer) +
3931 sizeof(u64) * adapter->req_tx_queues +
3932 sizeof(u64) * adapter->req_rx_queues +
3933 sizeof(u64) * adapter->req_rx_queues +
3934 sizeof(u8) * IBMVNIC_TX_DESC_VERSIONS;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003935
3936 login_rsp_buffer = kmalloc(rsp_buffer_size, GFP_ATOMIC);
3937 if (!login_rsp_buffer)
3938 goto buf_rsp_alloc_failed;
3939
3940 rsp_buffer_token = dma_map_single(dev, login_rsp_buffer,
3941 rsp_buffer_size, DMA_FROM_DEVICE);
3942 if (dma_mapping_error(dev, rsp_buffer_token)) {
3943 dev_err(dev, "Couldn't map login rsp buffer\n");
3944 goto buf_rsp_map_failed;
3945 }
Nathan Fontenot661a2622017-04-19 13:44:58 -04003946
Thomas Falcon032c5e82015-12-21 11:26:06 -06003947 adapter->login_buf = login_buffer;
3948 adapter->login_buf_token = buffer_token;
3949 adapter->login_buf_sz = buffer_size;
3950 adapter->login_rsp_buf = login_rsp_buffer;
3951 adapter->login_rsp_buf_token = rsp_buffer_token;
3952 adapter->login_rsp_buf_sz = rsp_buffer_size;
3953
3954 login_buffer->len = cpu_to_be32(buffer_size);
3955 login_buffer->version = cpu_to_be32(INITIAL_VERSION_LB);
3956 login_buffer->num_txcomp_subcrqs = cpu_to_be32(adapter->req_tx_queues);
3957 login_buffer->off_txcomp_subcrqs =
3958 cpu_to_be32(sizeof(struct ibmvnic_login_buffer));
3959 login_buffer->num_rxcomp_subcrqs = cpu_to_be32(adapter->req_rx_queues);
3960 login_buffer->off_rxcomp_subcrqs =
3961 cpu_to_be32(sizeof(struct ibmvnic_login_buffer) +
3962 sizeof(u64) * adapter->req_tx_queues);
3963 login_buffer->login_rsp_ioba = cpu_to_be32(rsp_buffer_token);
3964 login_buffer->login_rsp_len = cpu_to_be32(rsp_buffer_size);
3965
3966 tx_list_p = (__be64 *)((char *)login_buffer +
3967 sizeof(struct ibmvnic_login_buffer));
3968 rx_list_p = (__be64 *)((char *)login_buffer +
3969 sizeof(struct ibmvnic_login_buffer) +
3970 sizeof(u64) * adapter->req_tx_queues);
3971
3972 for (i = 0; i < adapter->req_tx_queues; i++) {
3973 if (adapter->tx_scrq[i]) {
Lijun Pan914789a2021-02-11 00:43:21 -06003974 tx_list_p[i] =
3975 cpu_to_be64(adapter->tx_scrq[i]->crq_num);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003976 }
3977 }
3978
3979 for (i = 0; i < adapter->req_rx_queues; i++) {
3980 if (adapter->rx_scrq[i]) {
Lijun Pan914789a2021-02-11 00:43:21 -06003981 rx_list_p[i] =
3982 cpu_to_be64(adapter->rx_scrq[i]->crq_num);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003983 }
3984 }
3985
Nathan Fontenot37798d02017-11-08 11:23:56 -06003986 /* Insert vNIC login client data */
3987 vlcd = (struct vnic_login_client_data *)
3988 ((char *)rx_list_p + (sizeof(u64) * adapter->req_rx_queues));
3989 login_buffer->client_data_offset =
3990 cpu_to_be32((char *)vlcd - (char *)login_buffer);
3991 login_buffer->client_data_len = cpu_to_be32(client_data_len);
3992
3993 vnic_add_client_data(adapter, vlcd);
3994
Thomas Falcon032c5e82015-12-21 11:26:06 -06003995 netdev_dbg(adapter->netdev, "Login Buffer:\n");
3996 for (i = 0; i < (adapter->login_buf_sz - 1) / 8 + 1; i++) {
3997 netdev_dbg(adapter->netdev, "%016lx\n",
Lijun Pan429aa362021-02-11 00:43:18 -06003998 ((unsigned long *)(adapter->login_buf))[i]);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003999 }
4000
4001 memset(&crq, 0, sizeof(crq));
4002 crq.login.first = IBMVNIC_CRQ_CMD;
4003 crq.login.cmd = LOGIN;
4004 crq.login.ioba = cpu_to_be32(buffer_token);
4005 crq.login.len = cpu_to_be32(buffer_size);
Sukadev Bhattiprolu76cdc5c2020-11-25 18:04:29 -06004006
4007 adapter->login_pending = true;
Dany Maddenc98d9cc2020-11-25 18:04:30 -06004008 rc = ibmvnic_send_crq(adapter, &crq);
4009 if (rc) {
4010 adapter->login_pending = false;
4011 netdev_err(adapter->netdev, "Failed to send login, rc=%d\n", rc);
4012 goto buf_rsp_map_failed;
4013 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06004014
Thomas Falcon20a8ab72018-02-26 18:10:59 -06004015 return 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004016
Thomas Falcon032c5e82015-12-21 11:26:06 -06004017buf_rsp_map_failed:
4018 kfree(login_rsp_buffer);
Dany Maddenc98d9cc2020-11-25 18:04:30 -06004019 adapter->login_rsp_buf = NULL;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004020buf_rsp_alloc_failed:
4021 dma_unmap_single(dev, buffer_token, buffer_size, DMA_TO_DEVICE);
4022buf_map_failed:
4023 kfree(login_buffer);
Dany Maddenc98d9cc2020-11-25 18:04:30 -06004024 adapter->login_buf = NULL;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004025buf_alloc_failed:
Thomas Falcon20a8ab72018-02-26 18:10:59 -06004026 return -1;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004027}
4028
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05004029static int send_request_map(struct ibmvnic_adapter *adapter, dma_addr_t addr,
4030 u32 len, u8 map_id)
Thomas Falcon032c5e82015-12-21 11:26:06 -06004031{
4032 union ibmvnic_crq crq;
4033
4034 memset(&crq, 0, sizeof(crq));
4035 crq.request_map.first = IBMVNIC_CRQ_CMD;
4036 crq.request_map.cmd = REQUEST_MAP;
4037 crq.request_map.map_id = map_id;
4038 crq.request_map.ioba = cpu_to_be32(addr);
4039 crq.request_map.len = cpu_to_be32(len);
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05004040 return ibmvnic_send_crq(adapter, &crq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004041}
4042
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05004043static int send_request_unmap(struct ibmvnic_adapter *adapter, u8 map_id)
Thomas Falcon032c5e82015-12-21 11:26:06 -06004044{
4045 union ibmvnic_crq crq;
4046
4047 memset(&crq, 0, sizeof(crq));
4048 crq.request_unmap.first = IBMVNIC_CRQ_CMD;
4049 crq.request_unmap.cmd = REQUEST_UNMAP;
4050 crq.request_unmap.map_id = map_id;
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05004051 return ibmvnic_send_crq(adapter, &crq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004052}
4053
Lijun Pan69980d02020-09-27 20:13:28 -05004054static void send_query_map(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06004055{
4056 union ibmvnic_crq crq;
4057
4058 memset(&crq, 0, sizeof(crq));
4059 crq.query_map.first = IBMVNIC_CRQ_CMD;
4060 crq.query_map.cmd = QUERY_MAP;
4061 ibmvnic_send_crq(adapter, &crq);
4062}
4063
4064/* Send a series of CRQs requesting various capabilities of the VNIC server */
Lijun Pan491099a2020-09-27 20:13:26 -05004065static void send_query_cap(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06004066{
4067 union ibmvnic_crq crq;
4068
Thomas Falcon901e0402017-02-15 12:17:59 -06004069 atomic_set(&adapter->running_cap_crqs, 0);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004070 memset(&crq, 0, sizeof(crq));
4071 crq.query_capability.first = IBMVNIC_CRQ_CMD;
4072 crq.query_capability.cmd = QUERY_CAPABILITY;
4073
4074 crq.query_capability.capability = cpu_to_be16(MIN_TX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06004075 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004076 ibmvnic_send_crq(adapter, &crq);
4077
4078 crq.query_capability.capability = cpu_to_be16(MIN_RX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06004079 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004080 ibmvnic_send_crq(adapter, &crq);
4081
4082 crq.query_capability.capability = cpu_to_be16(MIN_RX_ADD_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06004083 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004084 ibmvnic_send_crq(adapter, &crq);
4085
4086 crq.query_capability.capability = cpu_to_be16(MAX_TX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06004087 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004088 ibmvnic_send_crq(adapter, &crq);
4089
4090 crq.query_capability.capability = cpu_to_be16(MAX_RX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06004091 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004092 ibmvnic_send_crq(adapter, &crq);
4093
4094 crq.query_capability.capability = cpu_to_be16(MAX_RX_ADD_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06004095 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004096 ibmvnic_send_crq(adapter, &crq);
4097
4098 crq.query_capability.capability =
4099 cpu_to_be16(MIN_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06004100 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004101 ibmvnic_send_crq(adapter, &crq);
4102
4103 crq.query_capability.capability =
4104 cpu_to_be16(MIN_RX_ADD_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06004105 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004106 ibmvnic_send_crq(adapter, &crq);
4107
4108 crq.query_capability.capability =
4109 cpu_to_be16(MAX_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06004110 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004111 ibmvnic_send_crq(adapter, &crq);
4112
4113 crq.query_capability.capability =
4114 cpu_to_be16(MAX_RX_ADD_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06004115 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004116 ibmvnic_send_crq(adapter, &crq);
4117
4118 crq.query_capability.capability = cpu_to_be16(TCP_IP_OFFLOAD);
Thomas Falcon901e0402017-02-15 12:17:59 -06004119 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004120 ibmvnic_send_crq(adapter, &crq);
4121
4122 crq.query_capability.capability = cpu_to_be16(PROMISC_SUPPORTED);
Thomas Falcon901e0402017-02-15 12:17:59 -06004123 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004124 ibmvnic_send_crq(adapter, &crq);
4125
4126 crq.query_capability.capability = cpu_to_be16(MIN_MTU);
Thomas Falcon901e0402017-02-15 12:17:59 -06004127 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004128 ibmvnic_send_crq(adapter, &crq);
4129
4130 crq.query_capability.capability = cpu_to_be16(MAX_MTU);
Thomas Falcon901e0402017-02-15 12:17:59 -06004131 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004132 ibmvnic_send_crq(adapter, &crq);
4133
4134 crq.query_capability.capability = cpu_to_be16(MAX_MULTICAST_FILTERS);
Thomas Falcon901e0402017-02-15 12:17:59 -06004135 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004136 ibmvnic_send_crq(adapter, &crq);
4137
4138 crq.query_capability.capability = cpu_to_be16(VLAN_HEADER_INSERTION);
Thomas Falcon901e0402017-02-15 12:17:59 -06004139 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004140 ibmvnic_send_crq(adapter, &crq);
4141
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04004142 crq.query_capability.capability = cpu_to_be16(RX_VLAN_HEADER_INSERTION);
4143 atomic_inc(&adapter->running_cap_crqs);
4144 ibmvnic_send_crq(adapter, &crq);
4145
Thomas Falcon032c5e82015-12-21 11:26:06 -06004146 crq.query_capability.capability = cpu_to_be16(MAX_TX_SG_ENTRIES);
Thomas Falcon901e0402017-02-15 12:17:59 -06004147 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004148 ibmvnic_send_crq(adapter, &crq);
4149
4150 crq.query_capability.capability = cpu_to_be16(RX_SG_SUPPORTED);
Thomas Falcon901e0402017-02-15 12:17:59 -06004151 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004152 ibmvnic_send_crq(adapter, &crq);
4153
4154 crq.query_capability.capability = cpu_to_be16(OPT_TX_COMP_SUB_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06004155 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004156 ibmvnic_send_crq(adapter, &crq);
4157
4158 crq.query_capability.capability = cpu_to_be16(OPT_RX_COMP_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06004159 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004160 ibmvnic_send_crq(adapter, &crq);
4161
4162 crq.query_capability.capability =
4163 cpu_to_be16(OPT_RX_BUFADD_Q_PER_RX_COMP_Q);
Thomas Falcon901e0402017-02-15 12:17:59 -06004164 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004165 ibmvnic_send_crq(adapter, &crq);
4166
4167 crq.query_capability.capability =
4168 cpu_to_be16(OPT_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06004169 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004170 ibmvnic_send_crq(adapter, &crq);
4171
4172 crq.query_capability.capability =
4173 cpu_to_be16(OPT_RXBA_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06004174 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004175 ibmvnic_send_crq(adapter, &crq);
4176
4177 crq.query_capability.capability = cpu_to_be16(TX_RX_DESC_REQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06004178 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004179 ibmvnic_send_crq(adapter, &crq);
4180}
4181
Lijun Pan16e811f2020-09-27 20:13:29 -05004182static void send_query_ip_offload(struct ibmvnic_adapter *adapter)
4183{
4184 int buf_sz = sizeof(struct ibmvnic_query_ip_offload_buffer);
4185 struct device *dev = &adapter->vdev->dev;
4186 union ibmvnic_crq crq;
4187
4188 adapter->ip_offload_tok =
4189 dma_map_single(dev,
4190 &adapter->ip_offload_buf,
4191 buf_sz,
4192 DMA_FROM_DEVICE);
4193
4194 if (dma_mapping_error(dev, adapter->ip_offload_tok)) {
4195 if (!firmware_has_feature(FW_FEATURE_CMO))
4196 dev_err(dev, "Couldn't map offload buffer\n");
4197 return;
4198 }
4199
4200 memset(&crq, 0, sizeof(crq));
4201 crq.query_ip_offload.first = IBMVNIC_CRQ_CMD;
4202 crq.query_ip_offload.cmd = QUERY_IP_OFFLOAD;
4203 crq.query_ip_offload.len = cpu_to_be32(buf_sz);
4204 crq.query_ip_offload.ioba =
4205 cpu_to_be32(adapter->ip_offload_tok);
4206
4207 ibmvnic_send_crq(adapter, &crq);
4208}
4209
Lijun Pan46899bd2020-09-27 20:13:30 -05004210static void send_control_ip_offload(struct ibmvnic_adapter *adapter)
4211{
4212 struct ibmvnic_control_ip_offload_buffer *ctrl_buf = &adapter->ip_offload_ctrl;
4213 struct ibmvnic_query_ip_offload_buffer *buf = &adapter->ip_offload_buf;
4214 struct device *dev = &adapter->vdev->dev;
4215 netdev_features_t old_hw_features = 0;
4216 union ibmvnic_crq crq;
4217
4218 adapter->ip_offload_ctrl_tok =
4219 dma_map_single(dev,
4220 ctrl_buf,
4221 sizeof(adapter->ip_offload_ctrl),
4222 DMA_TO_DEVICE);
4223
4224 if (dma_mapping_error(dev, adapter->ip_offload_ctrl_tok)) {
4225 dev_err(dev, "Couldn't map ip offload control buffer\n");
4226 return;
4227 }
4228
4229 ctrl_buf->len = cpu_to_be32(sizeof(adapter->ip_offload_ctrl));
4230 ctrl_buf->version = cpu_to_be32(INITIAL_VERSION_IOB);
4231 ctrl_buf->ipv4_chksum = buf->ipv4_chksum;
4232 ctrl_buf->ipv6_chksum = buf->ipv6_chksum;
4233 ctrl_buf->tcp_ipv4_chksum = buf->tcp_ipv4_chksum;
4234 ctrl_buf->udp_ipv4_chksum = buf->udp_ipv4_chksum;
4235 ctrl_buf->tcp_ipv6_chksum = buf->tcp_ipv6_chksum;
4236 ctrl_buf->udp_ipv6_chksum = buf->udp_ipv6_chksum;
4237 ctrl_buf->large_tx_ipv4 = buf->large_tx_ipv4;
4238 ctrl_buf->large_tx_ipv6 = buf->large_tx_ipv6;
4239
4240 /* large_rx disabled for now, additional features needed */
4241 ctrl_buf->large_rx_ipv4 = 0;
4242 ctrl_buf->large_rx_ipv6 = 0;
4243
4244 if (adapter->state != VNIC_PROBING) {
4245 old_hw_features = adapter->netdev->hw_features;
4246 adapter->netdev->hw_features = 0;
4247 }
4248
4249 adapter->netdev->hw_features = NETIF_F_SG | NETIF_F_GSO | NETIF_F_GRO;
4250
4251 if (buf->tcp_ipv4_chksum || buf->udp_ipv4_chksum)
4252 adapter->netdev->hw_features |= NETIF_F_IP_CSUM;
4253
4254 if (buf->tcp_ipv6_chksum || buf->udp_ipv6_chksum)
4255 adapter->netdev->hw_features |= NETIF_F_IPV6_CSUM;
4256
4257 if ((adapter->netdev->features &
4258 (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)))
4259 adapter->netdev->hw_features |= NETIF_F_RXCSUM;
4260
4261 if (buf->large_tx_ipv4)
4262 adapter->netdev->hw_features |= NETIF_F_TSO;
4263 if (buf->large_tx_ipv6)
4264 adapter->netdev->hw_features |= NETIF_F_TSO6;
4265
4266 if (adapter->state == VNIC_PROBING) {
4267 adapter->netdev->features |= adapter->netdev->hw_features;
4268 } else if (old_hw_features != adapter->netdev->hw_features) {
4269 netdev_features_t tmp = 0;
4270
4271 /* disable features no longer supported */
4272 adapter->netdev->features &= adapter->netdev->hw_features;
4273 /* turn on features now supported if previously enabled */
4274 tmp = (old_hw_features ^ adapter->netdev->hw_features) &
4275 adapter->netdev->hw_features;
4276 adapter->netdev->features |=
4277 tmp & adapter->netdev->wanted_features;
4278 }
4279
4280 memset(&crq, 0, sizeof(crq));
4281 crq.control_ip_offload.first = IBMVNIC_CRQ_CMD;
4282 crq.control_ip_offload.cmd = CONTROL_IP_OFFLOAD;
4283 crq.control_ip_offload.len =
4284 cpu_to_be32(sizeof(adapter->ip_offload_ctrl));
4285 crq.control_ip_offload.ioba = cpu_to_be32(adapter->ip_offload_ctrl_tok);
4286 ibmvnic_send_crq(adapter, &crq);
4287}
4288
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02004289static void handle_vpd_size_rsp(union ibmvnic_crq *crq,
4290 struct ibmvnic_adapter *adapter)
4291{
4292 struct device *dev = &adapter->vdev->dev;
4293
4294 if (crq->get_vpd_size_rsp.rc.code) {
4295 dev_err(dev, "Error retrieving VPD size, rc=%x\n",
4296 crq->get_vpd_size_rsp.rc.code);
4297 complete(&adapter->fw_done);
4298 return;
4299 }
4300
4301 adapter->vpd->len = be64_to_cpu(crq->get_vpd_size_rsp.len);
4302 complete(&adapter->fw_done);
4303}
4304
4305static void handle_vpd_rsp(union ibmvnic_crq *crq,
4306 struct ibmvnic_adapter *adapter)
4307{
4308 struct device *dev = &adapter->vdev->dev;
Desnes Augusto Nunes do Rosario21a25452018-02-05 14:33:55 -02004309 unsigned char *substr = NULL;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02004310 u8 fw_level_len = 0;
4311
4312 memset(adapter->fw_version, 0, 32);
4313
4314 dma_unmap_single(dev, adapter->vpd->dma_addr, adapter->vpd->len,
4315 DMA_FROM_DEVICE);
4316
4317 if (crq->get_vpd_rsp.rc.code) {
4318 dev_err(dev, "Error retrieving VPD from device, rc=%x\n",
4319 crq->get_vpd_rsp.rc.code);
4320 goto complete;
4321 }
4322
4323 /* get the position of the firmware version info
4324 * located after the ASCII 'RM' substring in the buffer
4325 */
4326 substr = strnstr(adapter->vpd->buff, "RM", adapter->vpd->len);
4327 if (!substr) {
Desnes Augusto Nunes do Rosarioa1073112018-02-01 16:04:30 -02004328 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 -02004329 goto complete;
4330 }
4331
4332 /* get length of firmware level ASCII substring */
4333 if ((substr + 2) < (adapter->vpd->buff + adapter->vpd->len)) {
4334 fw_level_len = *(substr + 2);
4335 } else {
4336 dev_info(dev, "Length of FW substr extrapolated VDP buff\n");
4337 goto complete;
4338 }
4339
4340 /* copy firmware version string from vpd into adapter */
4341 if ((substr + 3 + fw_level_len) <
4342 (adapter->vpd->buff + adapter->vpd->len)) {
Desnes Augusto Nunes do Rosario21a25452018-02-05 14:33:55 -02004343 strncpy((char *)adapter->fw_version, substr + 3, fw_level_len);
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02004344 } else {
4345 dev_info(dev, "FW substr extrapolated VPD buff\n");
4346 }
4347
4348complete:
Desnes Augusto Nunes do Rosario21a25452018-02-05 14:33:55 -02004349 if (adapter->fw_version[0] == '\0')
4350 strncpy((char *)adapter->fw_version, "N/A", 3 * sizeof(char));
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02004351 complete(&adapter->fw_done);
4352}
4353
Thomas Falcon032c5e82015-12-21 11:26:06 -06004354static void handle_query_ip_offload_rsp(struct ibmvnic_adapter *adapter)
4355{
4356 struct device *dev = &adapter->vdev->dev;
4357 struct ibmvnic_query_ip_offload_buffer *buf = &adapter->ip_offload_buf;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004358 int i;
4359
4360 dma_unmap_single(dev, adapter->ip_offload_tok,
4361 sizeof(adapter->ip_offload_buf), DMA_FROM_DEVICE);
4362
4363 netdev_dbg(adapter->netdev, "Query IP Offload Buffer:\n");
4364 for (i = 0; i < (sizeof(adapter->ip_offload_buf) - 1) / 8 + 1; i++)
4365 netdev_dbg(adapter->netdev, "%016lx\n",
Lijun Pan429aa362021-02-11 00:43:18 -06004366 ((unsigned long *)(buf))[i]);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004367
4368 netdev_dbg(adapter->netdev, "ipv4_chksum = %d\n", buf->ipv4_chksum);
4369 netdev_dbg(adapter->netdev, "ipv6_chksum = %d\n", buf->ipv6_chksum);
4370 netdev_dbg(adapter->netdev, "tcp_ipv4_chksum = %d\n",
4371 buf->tcp_ipv4_chksum);
4372 netdev_dbg(adapter->netdev, "tcp_ipv6_chksum = %d\n",
4373 buf->tcp_ipv6_chksum);
4374 netdev_dbg(adapter->netdev, "udp_ipv4_chksum = %d\n",
4375 buf->udp_ipv4_chksum);
4376 netdev_dbg(adapter->netdev, "udp_ipv6_chksum = %d\n",
4377 buf->udp_ipv6_chksum);
4378 netdev_dbg(adapter->netdev, "large_tx_ipv4 = %d\n",
4379 buf->large_tx_ipv4);
4380 netdev_dbg(adapter->netdev, "large_tx_ipv6 = %d\n",
4381 buf->large_tx_ipv6);
4382 netdev_dbg(adapter->netdev, "large_rx_ipv4 = %d\n",
4383 buf->large_rx_ipv4);
4384 netdev_dbg(adapter->netdev, "large_rx_ipv6 = %d\n",
4385 buf->large_rx_ipv6);
4386 netdev_dbg(adapter->netdev, "max_ipv4_hdr_sz = %d\n",
4387 buf->max_ipv4_header_size);
4388 netdev_dbg(adapter->netdev, "max_ipv6_hdr_sz = %d\n",
4389 buf->max_ipv6_header_size);
4390 netdev_dbg(adapter->netdev, "max_tcp_hdr_size = %d\n",
4391 buf->max_tcp_header_size);
4392 netdev_dbg(adapter->netdev, "max_udp_hdr_size = %d\n",
4393 buf->max_udp_header_size);
4394 netdev_dbg(adapter->netdev, "max_large_tx_size = %d\n",
4395 buf->max_large_tx_size);
4396 netdev_dbg(adapter->netdev, "max_large_rx_size = %d\n",
4397 buf->max_large_rx_size);
4398 netdev_dbg(adapter->netdev, "ipv6_ext_hdr = %d\n",
4399 buf->ipv6_extension_header);
4400 netdev_dbg(adapter->netdev, "tcp_pseudosum_req = %d\n",
4401 buf->tcp_pseudosum_req);
4402 netdev_dbg(adapter->netdev, "num_ipv6_ext_hd = %d\n",
4403 buf->num_ipv6_ext_headers);
4404 netdev_dbg(adapter->netdev, "off_ipv6_ext_hd = %d\n",
4405 buf->off_ipv6_ext_headers);
4406
Lijun Pan46899bd2020-09-27 20:13:30 -05004407 send_control_ip_offload(adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004408}
4409
Thomas Falconc9008d32018-08-06 21:39:59 -05004410static const char *ibmvnic_fw_err_cause(u16 cause)
4411{
4412 switch (cause) {
4413 case ADAPTER_PROBLEM:
4414 return "adapter problem";
4415 case BUS_PROBLEM:
4416 return "bus problem";
4417 case FW_PROBLEM:
4418 return "firmware problem";
4419 case DD_PROBLEM:
4420 return "device driver problem";
4421 case EEH_RECOVERY:
4422 return "EEH recovery";
4423 case FW_UPDATED:
4424 return "firmware updated";
4425 case LOW_MEMORY:
4426 return "low Memory";
4427 default:
4428 return "unknown";
4429 }
4430}
4431
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04004432static void handle_error_indication(union ibmvnic_crq *crq,
4433 struct ibmvnic_adapter *adapter)
4434{
4435 struct device *dev = &adapter->vdev->dev;
Thomas Falconc9008d32018-08-06 21:39:59 -05004436 u16 cause;
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04004437
Thomas Falconc9008d32018-08-06 21:39:59 -05004438 cause = be16_to_cpu(crq->error_indication.error_cause);
4439
4440 dev_warn_ratelimited(dev,
4441 "Firmware reports %serror, cause: %s. Starting recovery...\n",
4442 crq->error_indication.flags
4443 & IBMVNIC_FATAL_ERROR ? "FATAL " : "",
4444 ibmvnic_fw_err_cause(cause));
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04004445
Nathan Fontenoted651a12017-05-03 14:04:38 -04004446 if (crq->error_indication.flags & IBMVNIC_FATAL_ERROR)
4447 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
John Allen8cb31cf2017-05-26 10:30:37 -04004448 else
4449 ibmvnic_reset(adapter, VNIC_RESET_NON_FATAL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004450}
4451
Thomas Falconf8136142018-01-29 13:45:05 -06004452static int handle_change_mac_rsp(union ibmvnic_crq *crq,
4453 struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06004454{
4455 struct net_device *netdev = adapter->netdev;
4456 struct device *dev = &adapter->vdev->dev;
4457 long rc;
4458
4459 rc = crq->change_mac_addr_rsp.rc.code;
4460 if (rc) {
4461 dev_err(dev, "Error %ld in CHANGE_MAC_ADDR_RSP\n", rc);
Thomas Falconf8136142018-01-29 13:45:05 -06004462 goto out;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004463 }
Lijun Pand9b0e592020-10-20 17:39:19 -05004464 /* crq->change_mac_addr.mac_addr is the requested one
4465 * crq->change_mac_addr_rsp.mac_addr is the returned valid one.
4466 */
Thomas Falcon62740e92019-05-09 23:13:43 -05004467 ether_addr_copy(netdev->dev_addr,
4468 &crq->change_mac_addr_rsp.mac_addr[0]);
Lijun Pand9b0e592020-10-20 17:39:19 -05004469 ether_addr_copy(adapter->mac_addr,
4470 &crq->change_mac_addr_rsp.mac_addr[0]);
Thomas Falconf8136142018-01-29 13:45:05 -06004471out:
4472 complete(&adapter->fw_done);
4473 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004474}
4475
4476static void handle_request_cap_rsp(union ibmvnic_crq *crq,
4477 struct ibmvnic_adapter *adapter)
4478{
4479 struct device *dev = &adapter->vdev->dev;
4480 u64 *req_value;
4481 char *name;
4482
Thomas Falcon901e0402017-02-15 12:17:59 -06004483 atomic_dec(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004484 switch (be16_to_cpu(crq->request_capability_rsp.capability)) {
4485 case REQ_TX_QUEUES:
4486 req_value = &adapter->req_tx_queues;
4487 name = "tx";
4488 break;
4489 case REQ_RX_QUEUES:
4490 req_value = &adapter->req_rx_queues;
4491 name = "rx";
4492 break;
4493 case REQ_RX_ADD_QUEUES:
4494 req_value = &adapter->req_rx_add_queues;
4495 name = "rx_add";
4496 break;
4497 case REQ_TX_ENTRIES_PER_SUBCRQ:
4498 req_value = &adapter->req_tx_entries_per_subcrq;
4499 name = "tx_entries_per_subcrq";
4500 break;
4501 case REQ_RX_ADD_ENTRIES_PER_SUBCRQ:
4502 req_value = &adapter->req_rx_add_entries_per_subcrq;
4503 name = "rx_add_entries_per_subcrq";
4504 break;
4505 case REQ_MTU:
4506 req_value = &adapter->req_mtu;
4507 name = "mtu";
4508 break;
4509 case PROMISC_REQUESTED:
4510 req_value = &adapter->promisc;
4511 name = "promisc";
4512 break;
4513 default:
4514 dev_err(dev, "Got invalid cap request rsp %d\n",
4515 crq->request_capability.capability);
4516 return;
4517 }
4518
4519 switch (crq->request_capability_rsp.rc.code) {
4520 case SUCCESS:
4521 break;
4522 case PARTIALSUCCESS:
4523 dev_info(dev, "req=%lld, rsp=%ld in %s queue, retrying.\n",
4524 *req_value,
Lijun Pan914789a2021-02-11 00:43:21 -06004525 (long)be64_to_cpu(crq->request_capability_rsp.number),
4526 name);
John Allene7913802018-01-18 16:27:12 -06004527
4528 if (be16_to_cpu(crq->request_capability_rsp.capability) ==
4529 REQ_MTU) {
4530 pr_err("mtu of %llu is not supported. Reverting.\n",
4531 *req_value);
4532 *req_value = adapter->fallback.mtu;
4533 } else {
4534 *req_value =
4535 be64_to_cpu(crq->request_capability_rsp.number);
4536 }
4537
Lijun Pan09081b92020-09-27 20:13:27 -05004538 send_request_cap(adapter, 1);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004539 return;
4540 default:
4541 dev_err(dev, "Error %d in request cap rsp\n",
4542 crq->request_capability_rsp.rc.code);
4543 return;
4544 }
4545
4546 /* Done receiving requested capabilities, query IP offload support */
Thomas Falcon901e0402017-02-15 12:17:59 -06004547 if (atomic_read(&adapter->running_cap_crqs) == 0) {
Thomas Falcon249168a2017-02-15 12:18:00 -06004548 adapter->wait_capability = false;
Lijun Pan16e811f2020-09-27 20:13:29 -05004549 send_query_ip_offload(adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004550 }
4551}
4552
4553static int handle_login_rsp(union ibmvnic_crq *login_rsp_crq,
4554 struct ibmvnic_adapter *adapter)
4555{
4556 struct device *dev = &adapter->vdev->dev;
John Allenc26eba02017-10-26 16:23:25 -05004557 struct net_device *netdev = adapter->netdev;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004558 struct ibmvnic_login_rsp_buffer *login_rsp = adapter->login_rsp_buf;
4559 struct ibmvnic_login_buffer *login = adapter->login_buf;
Cristobal Fornof3ae59c2020-08-19 13:16:23 -05004560 u64 *tx_handle_array;
4561 u64 *rx_handle_array;
4562 int num_tx_pools;
4563 int num_rx_pools;
Thomas Falcon507ebe62020-08-21 13:39:01 -05004564 u64 *size_array;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004565 int i;
4566
Sukadev Bhattiprolu76cdc5c2020-11-25 18:04:29 -06004567 /* CHECK: Test/set of login_pending does not need to be atomic
4568 * because only ibmvnic_tasklet tests/clears this.
4569 */
4570 if (!adapter->login_pending) {
4571 netdev_warn(netdev, "Ignoring unexpected login response\n");
4572 return 0;
4573 }
4574 adapter->login_pending = false;
4575
Thomas Falcon032c5e82015-12-21 11:26:06 -06004576 dma_unmap_single(dev, adapter->login_buf_token, adapter->login_buf_sz,
Thomas Falcon37e40fa2018-04-06 18:37:02 -05004577 DMA_TO_DEVICE);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004578 dma_unmap_single(dev, adapter->login_rsp_buf_token,
Thomas Falcon37e40fa2018-04-06 18:37:02 -05004579 adapter->login_rsp_buf_sz, DMA_FROM_DEVICE);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004580
John Allen498cd8e2016-04-06 11:49:55 -05004581 /* If the number of queues requested can't be allocated by the
4582 * server, the login response will return with code 1. We will need
4583 * to resend the login buffer with fewer queues requested.
4584 */
4585 if (login_rsp_crq->generic.rc.code) {
Nathan Fontenot64d92aa2018-04-11 10:09:32 -05004586 adapter->init_done_rc = login_rsp_crq->generic.rc.code;
John Allen498cd8e2016-04-06 11:49:55 -05004587 complete(&adapter->init_done);
4588 return 0;
4589 }
4590
John Allenc26eba02017-10-26 16:23:25 -05004591 netdev->mtu = adapter->req_mtu - ETH_HLEN;
4592
Thomas Falcon032c5e82015-12-21 11:26:06 -06004593 netdev_dbg(adapter->netdev, "Login Response Buffer:\n");
4594 for (i = 0; i < (adapter->login_rsp_buf_sz - 1) / 8 + 1; i++) {
4595 netdev_dbg(adapter->netdev, "%016lx\n",
Lijun Pan429aa362021-02-11 00:43:18 -06004596 ((unsigned long *)(adapter->login_rsp_buf))[i]);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004597 }
4598
4599 /* Sanity checks */
4600 if (login->num_txcomp_subcrqs != login_rsp->num_txsubm_subcrqs ||
4601 (be32_to_cpu(login->num_rxcomp_subcrqs) *
4602 adapter->req_rx_add_queues !=
4603 be32_to_cpu(login_rsp->num_rxadd_subcrqs))) {
4604 dev_err(dev, "FATAL: Inconsistent login and login rsp\n");
Dany Madden31d6b402020-11-25 18:04:24 -06004605 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004606 return -EIO;
4607 }
Thomas Falcon507ebe62020-08-21 13:39:01 -05004608 size_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
4609 be32_to_cpu(adapter->login_rsp_buf->off_rxadd_buff_size));
4610 /* variable buffer sizes are not supported, so just read the
4611 * first entry.
4612 */
4613 adapter->cur_rx_buf_sz = be64_to_cpu(size_array[0]);
Cristobal Fornof3ae59c2020-08-19 13:16:23 -05004614
4615 num_tx_pools = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
4616 num_rx_pools = be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
4617
4618 tx_handle_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
4619 be32_to_cpu(adapter->login_rsp_buf->off_txsubm_subcrqs));
4620 rx_handle_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
4621 be32_to_cpu(adapter->login_rsp_buf->off_rxadd_subcrqs));
4622
4623 for (i = 0; i < num_tx_pools; i++)
4624 adapter->tx_scrq[i]->handle = tx_handle_array[i];
4625
4626 for (i = 0; i < num_rx_pools; i++)
4627 adapter->rx_scrq[i]->handle = rx_handle_array[i];
4628
Thomas Falcon507ebe62020-08-21 13:39:01 -05004629 adapter->num_active_tx_scrqs = num_tx_pools;
4630 adapter->num_active_rx_scrqs = num_rx_pools;
Cristobal Fornof3ae59c2020-08-19 13:16:23 -05004631 release_login_rsp_buffer(adapter);
Thomas Falcona2c0f032018-02-21 18:18:30 -06004632 release_login_buffer(adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004633 complete(&adapter->init_done);
4634
Thomas Falcon032c5e82015-12-21 11:26:06 -06004635 return 0;
4636}
4637
Thomas Falcon032c5e82015-12-21 11:26:06 -06004638static void handle_request_unmap_rsp(union ibmvnic_crq *crq,
4639 struct ibmvnic_adapter *adapter)
4640{
4641 struct device *dev = &adapter->vdev->dev;
4642 long rc;
4643
4644 rc = crq->request_unmap_rsp.rc.code;
4645 if (rc)
4646 dev_err(dev, "Error %ld in REQUEST_UNMAP_RSP\n", rc);
4647}
4648
4649static void handle_query_map_rsp(union ibmvnic_crq *crq,
4650 struct ibmvnic_adapter *adapter)
4651{
4652 struct net_device *netdev = adapter->netdev;
4653 struct device *dev = &adapter->vdev->dev;
4654 long rc;
4655
4656 rc = crq->query_map_rsp.rc.code;
4657 if (rc) {
4658 dev_err(dev, "Error %ld in QUERY_MAP_RSP\n", rc);
4659 return;
4660 }
4661 netdev_dbg(netdev, "page_size = %d\ntot_pages = %d\nfree_pages = %d\n",
4662 crq->query_map_rsp.page_size, crq->query_map_rsp.tot_pages,
4663 crq->query_map_rsp.free_pages);
4664}
4665
4666static void handle_query_cap_rsp(union ibmvnic_crq *crq,
4667 struct ibmvnic_adapter *adapter)
4668{
4669 struct net_device *netdev = adapter->netdev;
4670 struct device *dev = &adapter->vdev->dev;
4671 long rc;
4672
Thomas Falcon901e0402017-02-15 12:17:59 -06004673 atomic_dec(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004674 netdev_dbg(netdev, "Outstanding queries: %d\n",
Thomas Falcon901e0402017-02-15 12:17:59 -06004675 atomic_read(&adapter->running_cap_crqs));
Thomas Falcon032c5e82015-12-21 11:26:06 -06004676 rc = crq->query_capability.rc.code;
4677 if (rc) {
4678 dev_err(dev, "Error %ld in QUERY_CAP_RSP\n", rc);
4679 goto out;
4680 }
4681
4682 switch (be16_to_cpu(crq->query_capability.capability)) {
4683 case MIN_TX_QUEUES:
4684 adapter->min_tx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004685 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004686 netdev_dbg(netdev, "min_tx_queues = %lld\n",
4687 adapter->min_tx_queues);
4688 break;
4689 case MIN_RX_QUEUES:
4690 adapter->min_rx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004691 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004692 netdev_dbg(netdev, "min_rx_queues = %lld\n",
4693 adapter->min_rx_queues);
4694 break;
4695 case MIN_RX_ADD_QUEUES:
4696 adapter->min_rx_add_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004697 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004698 netdev_dbg(netdev, "min_rx_add_queues = %lld\n",
4699 adapter->min_rx_add_queues);
4700 break;
4701 case MAX_TX_QUEUES:
4702 adapter->max_tx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004703 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004704 netdev_dbg(netdev, "max_tx_queues = %lld\n",
4705 adapter->max_tx_queues);
4706 break;
4707 case MAX_RX_QUEUES:
4708 adapter->max_rx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004709 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004710 netdev_dbg(netdev, "max_rx_queues = %lld\n",
4711 adapter->max_rx_queues);
4712 break;
4713 case MAX_RX_ADD_QUEUES:
4714 adapter->max_rx_add_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004715 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004716 netdev_dbg(netdev, "max_rx_add_queues = %lld\n",
4717 adapter->max_rx_add_queues);
4718 break;
4719 case MIN_TX_ENTRIES_PER_SUBCRQ:
4720 adapter->min_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06004721 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004722 netdev_dbg(netdev, "min_tx_entries_per_subcrq = %lld\n",
4723 adapter->min_tx_entries_per_subcrq);
4724 break;
4725 case MIN_RX_ADD_ENTRIES_PER_SUBCRQ:
4726 adapter->min_rx_add_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06004727 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004728 netdev_dbg(netdev, "min_rx_add_entrs_per_subcrq = %lld\n",
4729 adapter->min_rx_add_entries_per_subcrq);
4730 break;
4731 case MAX_TX_ENTRIES_PER_SUBCRQ:
4732 adapter->max_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06004733 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004734 netdev_dbg(netdev, "max_tx_entries_per_subcrq = %lld\n",
4735 adapter->max_tx_entries_per_subcrq);
4736 break;
4737 case MAX_RX_ADD_ENTRIES_PER_SUBCRQ:
4738 adapter->max_rx_add_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06004739 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004740 netdev_dbg(netdev, "max_rx_add_entrs_per_subcrq = %lld\n",
4741 adapter->max_rx_add_entries_per_subcrq);
4742 break;
4743 case TCP_IP_OFFLOAD:
4744 adapter->tcp_ip_offload =
Thomas Falconde89e852016-03-01 10:20:09 -06004745 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004746 netdev_dbg(netdev, "tcp_ip_offload = %lld\n",
4747 adapter->tcp_ip_offload);
4748 break;
4749 case PROMISC_SUPPORTED:
4750 adapter->promisc_supported =
Thomas Falconde89e852016-03-01 10:20:09 -06004751 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004752 netdev_dbg(netdev, "promisc_supported = %lld\n",
4753 adapter->promisc_supported);
4754 break;
4755 case MIN_MTU:
Thomas Falconde89e852016-03-01 10:20:09 -06004756 adapter->min_mtu = be64_to_cpu(crq->query_capability.number);
Thomas Falconf39f0d12017-02-14 10:22:59 -06004757 netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004758 netdev_dbg(netdev, "min_mtu = %lld\n", adapter->min_mtu);
4759 break;
4760 case MAX_MTU:
Thomas Falconde89e852016-03-01 10:20:09 -06004761 adapter->max_mtu = be64_to_cpu(crq->query_capability.number);
Thomas Falconf39f0d12017-02-14 10:22:59 -06004762 netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004763 netdev_dbg(netdev, "max_mtu = %lld\n", adapter->max_mtu);
4764 break;
4765 case MAX_MULTICAST_FILTERS:
4766 adapter->max_multicast_filters =
Thomas Falconde89e852016-03-01 10:20:09 -06004767 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004768 netdev_dbg(netdev, "max_multicast_filters = %lld\n",
4769 adapter->max_multicast_filters);
4770 break;
4771 case VLAN_HEADER_INSERTION:
4772 adapter->vlan_header_insertion =
Thomas Falconde89e852016-03-01 10:20:09 -06004773 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004774 if (adapter->vlan_header_insertion)
4775 netdev->features |= NETIF_F_HW_VLAN_STAG_TX;
4776 netdev_dbg(netdev, "vlan_header_insertion = %lld\n",
4777 adapter->vlan_header_insertion);
4778 break;
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04004779 case RX_VLAN_HEADER_INSERTION:
4780 adapter->rx_vlan_header_insertion =
4781 be64_to_cpu(crq->query_capability.number);
4782 netdev_dbg(netdev, "rx_vlan_header_insertion = %lld\n",
4783 adapter->rx_vlan_header_insertion);
4784 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004785 case MAX_TX_SG_ENTRIES:
4786 adapter->max_tx_sg_entries =
Thomas Falconde89e852016-03-01 10:20:09 -06004787 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004788 netdev_dbg(netdev, "max_tx_sg_entries = %lld\n",
4789 adapter->max_tx_sg_entries);
4790 break;
4791 case RX_SG_SUPPORTED:
4792 adapter->rx_sg_supported =
Thomas Falconde89e852016-03-01 10:20:09 -06004793 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004794 netdev_dbg(netdev, "rx_sg_supported = %lld\n",
4795 adapter->rx_sg_supported);
4796 break;
4797 case OPT_TX_COMP_SUB_QUEUES:
4798 adapter->opt_tx_comp_sub_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004799 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004800 netdev_dbg(netdev, "opt_tx_comp_sub_queues = %lld\n",
4801 adapter->opt_tx_comp_sub_queues);
4802 break;
4803 case OPT_RX_COMP_QUEUES:
4804 adapter->opt_rx_comp_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004805 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004806 netdev_dbg(netdev, "opt_rx_comp_queues = %lld\n",
4807 adapter->opt_rx_comp_queues);
4808 break;
4809 case OPT_RX_BUFADD_Q_PER_RX_COMP_Q:
4810 adapter->opt_rx_bufadd_q_per_rx_comp_q =
Thomas Falconde89e852016-03-01 10:20:09 -06004811 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004812 netdev_dbg(netdev, "opt_rx_bufadd_q_per_rx_comp_q = %lld\n",
4813 adapter->opt_rx_bufadd_q_per_rx_comp_q);
4814 break;
4815 case OPT_TX_ENTRIES_PER_SUBCRQ:
4816 adapter->opt_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06004817 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004818 netdev_dbg(netdev, "opt_tx_entries_per_subcrq = %lld\n",
4819 adapter->opt_tx_entries_per_subcrq);
4820 break;
4821 case OPT_RXBA_ENTRIES_PER_SUBCRQ:
4822 adapter->opt_rxba_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06004823 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004824 netdev_dbg(netdev, "opt_rxba_entries_per_subcrq = %lld\n",
4825 adapter->opt_rxba_entries_per_subcrq);
4826 break;
4827 case TX_RX_DESC_REQ:
4828 adapter->tx_rx_desc_req = crq->query_capability.number;
4829 netdev_dbg(netdev, "tx_rx_desc_req = %llx\n",
4830 adapter->tx_rx_desc_req);
4831 break;
4832
4833 default:
4834 netdev_err(netdev, "Got invalid cap rsp %d\n",
4835 crq->query_capability.capability);
4836 }
4837
4838out:
Thomas Falcon249168a2017-02-15 12:18:00 -06004839 if (atomic_read(&adapter->running_cap_crqs) == 0) {
4840 adapter->wait_capability = false;
Lijun Pan09081b92020-09-27 20:13:27 -05004841 send_request_cap(adapter, 0);
Thomas Falcon249168a2017-02-15 12:18:00 -06004842 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06004843}
4844
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03004845static int send_query_phys_parms(struct ibmvnic_adapter *adapter)
4846{
4847 union ibmvnic_crq crq;
4848 int rc;
4849
4850 memset(&crq, 0, sizeof(crq));
4851 crq.query_phys_parms.first = IBMVNIC_CRQ_CMD;
4852 crq.query_phys_parms.cmd = QUERY_PHYS_PARMS;
Thomas Falconff25dcb2019-11-25 17:12:56 -06004853
4854 mutex_lock(&adapter->fw_lock);
4855 adapter->fw_done_rc = 0;
Thomas Falcon070eca92019-11-25 17:12:53 -06004856 reinit_completion(&adapter->fw_done);
Thomas Falconff25dcb2019-11-25 17:12:56 -06004857
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03004858 rc = ibmvnic_send_crq(adapter, &crq);
Thomas Falconff25dcb2019-11-25 17:12:56 -06004859 if (rc) {
4860 mutex_unlock(&adapter->fw_lock);
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03004861 return rc;
Thomas Falconff25dcb2019-11-25 17:12:56 -06004862 }
Thomas Falcon476d96c2019-11-25 17:12:55 -06004863
4864 rc = ibmvnic_wait_for_completion(adapter, &adapter->fw_done, 10000);
Thomas Falconff25dcb2019-11-25 17:12:56 -06004865 if (rc) {
4866 mutex_unlock(&adapter->fw_lock);
Thomas Falcon476d96c2019-11-25 17:12:55 -06004867 return rc;
Thomas Falconff25dcb2019-11-25 17:12:56 -06004868 }
Thomas Falcon476d96c2019-11-25 17:12:55 -06004869
Thomas Falconff25dcb2019-11-25 17:12:56 -06004870 mutex_unlock(&adapter->fw_lock);
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03004871 return adapter->fw_done_rc ? -EIO : 0;
4872}
4873
4874static int handle_query_phys_parms_rsp(union ibmvnic_crq *crq,
4875 struct ibmvnic_adapter *adapter)
4876{
4877 struct net_device *netdev = adapter->netdev;
4878 int rc;
Murilo Fossa Vicentinidd0f9d82019-09-16 11:50:37 -03004879 __be32 rspeed = cpu_to_be32(crq->query_phys_parms_rsp.speed);
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03004880
4881 rc = crq->query_phys_parms_rsp.rc.code;
4882 if (rc) {
4883 netdev_err(netdev, "Error %d in QUERY_PHYS_PARMS\n", rc);
4884 return rc;
4885 }
Murilo Fossa Vicentinidd0f9d82019-09-16 11:50:37 -03004886 switch (rspeed) {
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03004887 case IBMVNIC_10MBPS:
4888 adapter->speed = SPEED_10;
4889 break;
4890 case IBMVNIC_100MBPS:
4891 adapter->speed = SPEED_100;
4892 break;
4893 case IBMVNIC_1GBPS:
4894 adapter->speed = SPEED_1000;
4895 break;
Lijun Panb9cd7952020-09-27 19:06:25 -05004896 case IBMVNIC_10GBPS:
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03004897 adapter->speed = SPEED_10000;
4898 break;
4899 case IBMVNIC_25GBPS:
4900 adapter->speed = SPEED_25000;
4901 break;
4902 case IBMVNIC_40GBPS:
4903 adapter->speed = SPEED_40000;
4904 break;
4905 case IBMVNIC_50GBPS:
4906 adapter->speed = SPEED_50000;
4907 break;
4908 case IBMVNIC_100GBPS:
4909 adapter->speed = SPEED_100000;
4910 break;
Lijun Panb9cd7952020-09-27 19:06:25 -05004911 case IBMVNIC_200GBPS:
4912 adapter->speed = SPEED_200000;
4913 break;
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03004914 default:
Murilo Fossa Vicentinidd0f9d82019-09-16 11:50:37 -03004915 if (netif_carrier_ok(netdev))
4916 netdev_warn(netdev, "Unknown speed 0x%08x\n", rspeed);
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03004917 adapter->speed = SPEED_UNKNOWN;
4918 }
4919 if (crq->query_phys_parms_rsp.flags1 & IBMVNIC_FULL_DUPLEX)
4920 adapter->duplex = DUPLEX_FULL;
4921 else if (crq->query_phys_parms_rsp.flags1 & IBMVNIC_HALF_DUPLEX)
4922 adapter->duplex = DUPLEX_HALF;
4923 else
4924 adapter->duplex = DUPLEX_UNKNOWN;
4925
4926 return rc;
4927}
4928
Thomas Falcon032c5e82015-12-21 11:26:06 -06004929static void ibmvnic_handle_crq(union ibmvnic_crq *crq,
4930 struct ibmvnic_adapter *adapter)
4931{
4932 struct ibmvnic_generic_crq *gen_crq = &crq->generic;
4933 struct net_device *netdev = adapter->netdev;
4934 struct device *dev = &adapter->vdev->dev;
Murilo Fossa Vicentini993a82b2017-04-19 13:44:35 -04004935 u64 *u64_crq = (u64 *)crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004936 long rc;
4937
4938 netdev_dbg(netdev, "Handling CRQ: %016lx %016lx\n",
Lijun Pan429aa362021-02-11 00:43:18 -06004939 (unsigned long)cpu_to_be64(u64_crq[0]),
4940 (unsigned long)cpu_to_be64(u64_crq[1]));
Thomas Falcon032c5e82015-12-21 11:26:06 -06004941 switch (gen_crq->first) {
4942 case IBMVNIC_CRQ_INIT_RSP:
4943 switch (gen_crq->cmd) {
4944 case IBMVNIC_CRQ_INIT:
4945 dev_info(dev, "Partner initialized\n");
John Allen017892c12017-05-26 10:30:19 -04004946 adapter->from_passive_init = true;
Sukadev Bhattiprolu76cdc5c2020-11-25 18:04:29 -06004947 /* Discard any stale login responses from prev reset.
4948 * CHECK: should we clear even on INIT_COMPLETE?
4949 */
4950 adapter->login_pending = false;
4951
Thomas Falcon17c87052018-05-23 13:37:58 -05004952 if (!completion_done(&adapter->init_done)) {
4953 complete(&adapter->init_done);
4954 adapter->init_done_rc = -EIO;
4955 }
Sukadev Bhattiproluef66a1e2021-02-02 21:08:02 -08004956 rc = ibmvnic_reset(adapter, VNIC_RESET_FAILOVER);
4957 if (rc && rc != -EBUSY) {
4958 /* We were unable to schedule the failover
4959 * reset either because the adapter was still
4960 * probing (eg: during kexec) or we could not
4961 * allocate memory. Clear the failover_pending
4962 * flag since no one else will. We ignore
4963 * EBUSY because it means either FAILOVER reset
4964 * is already scheduled or the adapter is
4965 * being removed.
4966 */
4967 netdev_err(netdev,
4968 "Error %ld scheduling failover reset\n",
4969 rc);
4970 adapter->failover_pending = false;
4971 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06004972 break;
4973 case IBMVNIC_CRQ_INIT_COMPLETE:
4974 dev_info(dev, "Partner initialization complete\n");
Thomas Falcon51536982018-05-23 13:37:56 -05004975 adapter->crq.active = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004976 send_version_xchg(adapter);
4977 break;
4978 default:
4979 dev_err(dev, "Unknown crq cmd: %d\n", gen_crq->cmd);
4980 }
4981 return;
4982 case IBMVNIC_CRQ_XPORT_EVENT:
Nathan Fontenoted651a12017-05-03 14:04:38 -04004983 netif_carrier_off(netdev);
Thomas Falcon51536982018-05-23 13:37:56 -05004984 adapter->crq.active = false;
Thomas Falcon2147e3d2019-11-25 17:12:54 -06004985 /* terminate any thread waiting for a response
4986 * from the device
4987 */
4988 if (!completion_done(&adapter->fw_done)) {
4989 adapter->fw_done_rc = -EIO;
4990 complete(&adapter->fw_done);
4991 }
4992 if (!completion_done(&adapter->stats_done))
4993 complete(&adapter->stats_done);
Juliet Kim7ed5b312019-09-20 16:11:23 -04004994 if (test_bit(0, &adapter->resetting))
Thomas Falcon2770a792018-05-23 13:38:02 -05004995 adapter->force_reset_recovery = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004996 if (gen_crq->cmd == IBMVNIC_PARTITION_MIGRATED) {
Nathan Fontenoted651a12017-05-03 14:04:38 -04004997 dev_info(dev, "Migrated, re-enabling adapter\n");
4998 ibmvnic_reset(adapter, VNIC_RESET_MOBILITY);
Thomas Falcondfad09a2016-08-18 11:37:51 -05004999 } else if (gen_crq->cmd == IBMVNIC_DEVICE_FAILOVER) {
5000 dev_info(dev, "Backing device failover detected\n");
Thomas Falcon5a18e1e2018-04-06 18:37:05 -05005001 adapter->failover_pending = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005002 } else {
5003 /* The adapter lost the connection */
5004 dev_err(dev, "Virtual Adapter failed (rc=%d)\n",
5005 gen_crq->cmd);
Nathan Fontenoted651a12017-05-03 14:04:38 -04005006 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005007 }
5008 return;
5009 case IBMVNIC_CRQ_CMD_RSP:
5010 break;
5011 default:
5012 dev_err(dev, "Got an invalid msg type 0x%02x\n",
5013 gen_crq->first);
5014 return;
5015 }
5016
5017 switch (gen_crq->cmd) {
5018 case VERSION_EXCHANGE_RSP:
5019 rc = crq->version_exchange_rsp.rc.code;
5020 if (rc) {
5021 dev_err(dev, "Error %ld in VERSION_EXCHG_RSP\n", rc);
5022 break;
5023 }
Thomas Falcon78468892020-05-28 11:19:17 -05005024 ibmvnic_version =
Thomas Falcon032c5e82015-12-21 11:26:06 -06005025 be16_to_cpu(crq->version_exchange_rsp.version);
Thomas Falcon78468892020-05-28 11:19:17 -05005026 dev_info(dev, "Partner protocol version is %d\n",
5027 ibmvnic_version);
Lijun Pan491099a2020-09-27 20:13:26 -05005028 send_query_cap(adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005029 break;
5030 case QUERY_CAPABILITY_RSP:
5031 handle_query_cap_rsp(crq, adapter);
5032 break;
5033 case QUERY_MAP_RSP:
5034 handle_query_map_rsp(crq, adapter);
5035 break;
5036 case REQUEST_MAP_RSP:
Thomas Falconf3be0cb2017-06-21 14:53:01 -05005037 adapter->fw_done_rc = crq->request_map_rsp.rc.code;
5038 complete(&adapter->fw_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005039 break;
5040 case REQUEST_UNMAP_RSP:
5041 handle_request_unmap_rsp(crq, adapter);
5042 break;
5043 case REQUEST_CAPABILITY_RSP:
5044 handle_request_cap_rsp(crq, adapter);
5045 break;
5046 case LOGIN_RSP:
5047 netdev_dbg(netdev, "Got Login Response\n");
5048 handle_login_rsp(crq, adapter);
5049 break;
5050 case LOGICAL_LINK_STATE_RSP:
Nathan Fontenot53da09e2017-04-21 15:39:04 -04005051 netdev_dbg(netdev,
5052 "Got Logical Link State Response, state: %d rc: %d\n",
5053 crq->logical_link_state_rsp.link_state,
5054 crq->logical_link_state_rsp.rc.code);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005055 adapter->logical_link_state =
5056 crq->logical_link_state_rsp.link_state;
Nathan Fontenot53da09e2017-04-21 15:39:04 -04005057 adapter->init_done_rc = crq->logical_link_state_rsp.rc.code;
5058 complete(&adapter->init_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005059 break;
5060 case LINK_STATE_INDICATION:
5061 netdev_dbg(netdev, "Got Logical Link State Indication\n");
5062 adapter->phys_link_state =
5063 crq->link_state_indication.phys_link_state;
5064 adapter->logical_link_state =
5065 crq->link_state_indication.logical_link_state;
Thomas Falcon0655f992019-05-09 23:13:44 -05005066 if (adapter->phys_link_state && adapter->logical_link_state)
5067 netif_carrier_on(netdev);
5068 else
5069 netif_carrier_off(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005070 break;
5071 case CHANGE_MAC_ADDR_RSP:
5072 netdev_dbg(netdev, "Got MAC address change Response\n");
Thomas Falconf8136142018-01-29 13:45:05 -06005073 adapter->fw_done_rc = handle_change_mac_rsp(crq, adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005074 break;
5075 case ERROR_INDICATION:
5076 netdev_dbg(netdev, "Got Error Indication\n");
5077 handle_error_indication(crq, adapter);
5078 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005079 case REQUEST_STATISTICS_RSP:
5080 netdev_dbg(netdev, "Got Statistics Response\n");
5081 complete(&adapter->stats_done);
5082 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005083 case QUERY_IP_OFFLOAD_RSP:
5084 netdev_dbg(netdev, "Got Query IP offload Response\n");
5085 handle_query_ip_offload_rsp(adapter);
5086 break;
5087 case MULTICAST_CTRL_RSP:
5088 netdev_dbg(netdev, "Got multicast control Response\n");
5089 break;
5090 case CONTROL_IP_OFFLOAD_RSP:
5091 netdev_dbg(netdev, "Got Control IP offload Response\n");
5092 dma_unmap_single(dev, adapter->ip_offload_ctrl_tok,
5093 sizeof(adapter->ip_offload_ctrl),
5094 DMA_TO_DEVICE);
John Allenbd0b6722017-03-17 17:13:40 -05005095 complete(&adapter->init_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005096 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005097 case COLLECT_FW_TRACE_RSP:
5098 netdev_dbg(netdev, "Got Collect firmware trace Response\n");
5099 complete(&adapter->fw_done);
5100 break;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02005101 case GET_VPD_SIZE_RSP:
5102 handle_vpd_size_rsp(crq, adapter);
5103 break;
5104 case GET_VPD_RSP:
5105 handle_vpd_rsp(crq, adapter);
5106 break;
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03005107 case QUERY_PHYS_PARMS_RSP:
5108 adapter->fw_done_rc = handle_query_phys_parms_rsp(crq, adapter);
5109 complete(&adapter->fw_done);
5110 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005111 default:
5112 netdev_err(netdev, "Got an invalid cmd type 0x%02x\n",
5113 gen_crq->cmd);
5114 }
5115}
5116
5117static irqreturn_t ibmvnic_interrupt(int irq, void *instance)
5118{
5119 struct ibmvnic_adapter *adapter = instance;
Thomas Falcon6c267b32017-02-15 12:17:58 -06005120
Thomas Falcon6c267b32017-02-15 12:17:58 -06005121 tasklet_schedule(&adapter->tasklet);
Thomas Falcon6c267b32017-02-15 12:17:58 -06005122 return IRQ_HANDLED;
5123}
5124
Allen Paisaa7c3fe2020-09-14 12:59:29 +05305125static void ibmvnic_tasklet(struct tasklet_struct *t)
Thomas Falcon6c267b32017-02-15 12:17:58 -06005126{
Allen Paisaa7c3fe2020-09-14 12:59:29 +05305127 struct ibmvnic_adapter *adapter = from_tasklet(adapter, t, tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005128 struct ibmvnic_crq_queue *queue = &adapter->crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005129 union ibmvnic_crq *crq;
5130 unsigned long flags;
5131 bool done = false;
5132
5133 spin_lock_irqsave(&queue->lock, flags);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005134 while (!done) {
5135 /* Pull all the valid messages off the CRQ */
5136 while ((crq = ibmvnic_next_crq(adapter)) != NULL) {
Lijun Pane41aec72021-01-27 19:34:42 -06005137 /* This barrier makes sure ibmvnic_next_crq()'s
5138 * crq->generic.first & IBMVNIC_CRQ_CMD_RSP is loaded
5139 * before ibmvnic_handle_crq()'s
5140 * switch(gen_crq->first) and switch(gen_crq->cmd).
5141 */
5142 dma_rmb();
Thomas Falcon032c5e82015-12-21 11:26:06 -06005143 ibmvnic_handle_crq(crq, adapter);
5144 crq->generic.first = 0;
5145 }
Brian Kinged7ecbf2017-04-19 13:44:53 -04005146
5147 /* remain in tasklet until all
5148 * capabilities responses are received
5149 */
5150 if (!adapter->wait_capability)
5151 done = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005152 }
Thomas Falcon249168a2017-02-15 12:18:00 -06005153 /* if capabilities CRQ's were sent in this tasklet, the following
5154 * tasklet must wait until all responses are received
5155 */
5156 if (atomic_read(&adapter->running_cap_crqs) != 0)
5157 adapter->wait_capability = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005158 spin_unlock_irqrestore(&queue->lock, flags);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005159}
5160
5161static int ibmvnic_reenable_crq_queue(struct ibmvnic_adapter *adapter)
5162{
5163 struct vio_dev *vdev = adapter->vdev;
5164 int rc;
5165
5166 do {
5167 rc = plpar_hcall_norets(H_ENABLE_CRQ, vdev->unit_address);
5168 } while (rc == H_IN_PROGRESS || rc == H_BUSY || H_IS_LONG_BUSY(rc));
5169
5170 if (rc)
5171 dev_err(&vdev->dev, "Error enabling adapter (rc=%d)\n", rc);
5172
5173 return rc;
5174}
5175
5176static int ibmvnic_reset_crq(struct ibmvnic_adapter *adapter)
5177{
5178 struct ibmvnic_crq_queue *crq = &adapter->crq;
5179 struct device *dev = &adapter->vdev->dev;
5180 struct vio_dev *vdev = adapter->vdev;
5181 int rc;
5182
5183 /* Close the CRQ */
5184 do {
5185 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
5186 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
5187
5188 /* Clean out the queue */
Lijun Pan0e435be2020-11-23 13:35:46 -06005189 if (!crq->msgs)
5190 return -EINVAL;
5191
Thomas Falcon032c5e82015-12-21 11:26:06 -06005192 memset(crq->msgs, 0, PAGE_SIZE);
5193 crq->cur = 0;
Thomas Falcon51536982018-05-23 13:37:56 -05005194 crq->active = false;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005195
5196 /* And re-open it again */
5197 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
5198 crq->msg_token, PAGE_SIZE);
5199
5200 if (rc == H_CLOSED)
5201 /* Adapter is good, but other end is not ready */
5202 dev_warn(dev, "Partner adapter not ready\n");
5203 else if (rc != 0)
5204 dev_warn(dev, "Couldn't register crq (rc=%d)\n", rc);
5205
5206 return rc;
5207}
5208
Nathan Fontenotf9928872017-03-30 02:48:54 -04005209static void release_crq_queue(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06005210{
5211 struct ibmvnic_crq_queue *crq = &adapter->crq;
5212 struct vio_dev *vdev = adapter->vdev;
5213 long rc;
5214
Nathan Fontenotf9928872017-03-30 02:48:54 -04005215 if (!crq->msgs)
5216 return;
5217
Thomas Falcon032c5e82015-12-21 11:26:06 -06005218 netdev_dbg(adapter->netdev, "Releasing CRQ\n");
5219 free_irq(vdev->irq, adapter);
Thomas Falcon6c267b32017-02-15 12:17:58 -06005220 tasklet_kill(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005221 do {
5222 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
5223 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
5224
5225 dma_unmap_single(&vdev->dev, crq->msg_token, PAGE_SIZE,
5226 DMA_BIDIRECTIONAL);
5227 free_page((unsigned long)crq->msgs);
Nathan Fontenotf9928872017-03-30 02:48:54 -04005228 crq->msgs = NULL;
Thomas Falcon51536982018-05-23 13:37:56 -05005229 crq->active = false;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005230}
5231
Nathan Fontenotf9928872017-03-30 02:48:54 -04005232static int init_crq_queue(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06005233{
5234 struct ibmvnic_crq_queue *crq = &adapter->crq;
5235 struct device *dev = &adapter->vdev->dev;
5236 struct vio_dev *vdev = adapter->vdev;
5237 int rc, retrc = -ENOMEM;
5238
Nathan Fontenotf9928872017-03-30 02:48:54 -04005239 if (crq->msgs)
5240 return 0;
5241
Thomas Falcon032c5e82015-12-21 11:26:06 -06005242 crq->msgs = (union ibmvnic_crq *)get_zeroed_page(GFP_KERNEL);
5243 /* Should we allocate more than one page? */
5244
5245 if (!crq->msgs)
5246 return -ENOMEM;
5247
5248 crq->size = PAGE_SIZE / sizeof(*crq->msgs);
5249 crq->msg_token = dma_map_single(dev, crq->msgs, PAGE_SIZE,
5250 DMA_BIDIRECTIONAL);
5251 if (dma_mapping_error(dev, crq->msg_token))
5252 goto map_failed;
5253
5254 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
5255 crq->msg_token, PAGE_SIZE);
5256
5257 if (rc == H_RESOURCE)
5258 /* maybe kexecing and resource is busy. try a reset */
5259 rc = ibmvnic_reset_crq(adapter);
5260 retrc = rc;
5261
5262 if (rc == H_CLOSED) {
5263 dev_warn(dev, "Partner adapter not ready\n");
5264 } else if (rc) {
5265 dev_warn(dev, "Error %d opening adapter\n", rc);
5266 goto reg_crq_failed;
5267 }
5268
5269 retrc = 0;
5270
Allen Paisaa7c3fe2020-09-14 12:59:29 +05305271 tasklet_setup(&adapter->tasklet, (void *)ibmvnic_tasklet);
Thomas Falcon6c267b32017-02-15 12:17:58 -06005272
Thomas Falcon032c5e82015-12-21 11:26:06 -06005273 netdev_dbg(adapter->netdev, "registering irq 0x%x\n", vdev->irq);
Murilo Fossa Vicentinie56e2512019-04-25 11:02:33 -03005274 snprintf(crq->name, sizeof(crq->name), "ibmvnic-%x",
5275 adapter->vdev->unit_address);
5276 rc = request_irq(vdev->irq, ibmvnic_interrupt, 0, crq->name, adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005277 if (rc) {
5278 dev_err(dev, "Couldn't register irq 0x%x. rc=%d\n",
5279 vdev->irq, rc);
5280 goto req_irq_failed;
5281 }
5282
5283 rc = vio_enable_interrupts(vdev);
5284 if (rc) {
5285 dev_err(dev, "Error %d enabling interrupts\n", rc);
5286 goto req_irq_failed;
5287 }
5288
5289 crq->cur = 0;
5290 spin_lock_init(&crq->lock);
5291
5292 return retrc;
5293
5294req_irq_failed:
Thomas Falcon6c267b32017-02-15 12:17:58 -06005295 tasklet_kill(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005296 do {
5297 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
5298 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
5299reg_crq_failed:
5300 dma_unmap_single(dev, crq->msg_token, PAGE_SIZE, DMA_BIDIRECTIONAL);
5301map_failed:
5302 free_page((unsigned long)crq->msgs);
Nathan Fontenotf9928872017-03-30 02:48:54 -04005303 crq->msgs = NULL;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005304 return retrc;
5305}
5306
Lijun Pan635e4422020-08-19 17:52:26 -05005307static int ibmvnic_reset_init(struct ibmvnic_adapter *adapter, bool reset)
John Allenf6ef6402017-03-17 17:13:42 -05005308{
5309 struct device *dev = &adapter->vdev->dev;
Dany Madden98c41f02020-11-25 18:04:32 -06005310 unsigned long timeout = msecs_to_jiffies(20000);
Michal Suchanek6881b072021-03-02 20:47:47 +01005311 u64 old_num_rx_queues = adapter->req_rx_queues;
5312 u64 old_num_tx_queues = adapter->req_tx_queues;
John Allenf6ef6402017-03-17 17:13:42 -05005313 int rc;
5314
John Allen017892c12017-05-26 10:30:19 -04005315 adapter->from_passive_init = false;
5316
Michal Suchanek6881b072021-03-02 20:47:47 +01005317 if (reset)
Lijun Pan635e4422020-08-19 17:52:26 -05005318 reinit_completion(&adapter->init_done);
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06005319
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04005320 adapter->init_done_rc = 0;
Lijun Panfa68bfa2020-08-19 17:52:24 -05005321 rc = ibmvnic_send_crq_init(adapter);
5322 if (rc) {
5323 dev_err(dev, "Send crq init failed with error %d\n", rc);
5324 return rc;
5325 }
5326
John Allenf6ef6402017-03-17 17:13:42 -05005327 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
5328 dev_err(dev, "Initialization sequence timed out\n");
John Allen017892c12017-05-26 10:30:19 -04005329 return -1;
5330 }
5331
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04005332 if (adapter->init_done_rc) {
5333 release_crq_queue(adapter);
5334 return adapter->init_done_rc;
5335 }
5336
Lijun Pan785a2b12020-09-17 21:12:46 -05005337 if (adapter->from_passive_init) {
5338 adapter->state = VNIC_OPEN;
5339 adapter->from_passive_init = false;
5340 return -1;
5341 }
5342
Lijun Pan635e4422020-08-19 17:52:26 -05005343 if (reset &&
5344 test_bit(0, &adapter->resetting) && !adapter->wait_for_reset &&
Nathan Fontenot30f79622018-04-06 18:37:06 -05005345 adapter->reset_reason != VNIC_RESET_MOBILITY) {
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06005346 if (adapter->req_rx_queues != old_num_rx_queues ||
5347 adapter->req_tx_queues != old_num_tx_queues) {
5348 release_sub_crqs(adapter, 0);
5349 rc = init_sub_crqs(adapter);
5350 } else {
5351 rc = reset_sub_crq_queues(adapter);
5352 }
5353 } else {
Nathan Fontenot57a49432017-05-26 10:31:12 -04005354 rc = init_sub_crqs(adapter);
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06005355 }
5356
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04005357 if (rc) {
5358 dev_err(dev, "Initialization of sub crqs failed\n");
5359 release_crq_queue(adapter);
Thomas Falcon5df969c2017-06-28 19:55:54 -05005360 return rc;
5361 }
5362
5363 rc = init_sub_crq_irqs(adapter);
5364 if (rc) {
5365 dev_err(dev, "Failed to initialize sub crq irqs\n");
5366 release_crq_queue(adapter);
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04005367 }
5368
5369 return rc;
John Allenf6ef6402017-03-17 17:13:42 -05005370}
5371
Thomas Falcon40c9db82017-06-12 12:35:04 -05005372static struct device_attribute dev_attr_failover;
5373
Thomas Falcon032c5e82015-12-21 11:26:06 -06005374static int ibmvnic_probe(struct vio_dev *dev, const struct vio_device_id *id)
5375{
5376 struct ibmvnic_adapter *adapter;
5377 struct net_device *netdev;
5378 unsigned char *mac_addr_p;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005379 int rc;
5380
5381 dev_dbg(&dev->dev, "entering ibmvnic_probe for UA 0x%x\n",
5382 dev->unit_address);
5383
5384 mac_addr_p = (unsigned char *)vio_get_attribute(dev,
5385 VETH_MAC_ADDR, NULL);
5386 if (!mac_addr_p) {
5387 dev_err(&dev->dev,
5388 "(%s:%3.3d) ERROR: Can't find MAC_ADDR attribute\n",
5389 __FILE__, __LINE__);
5390 return 0;
5391 }
5392
5393 netdev = alloc_etherdev_mq(sizeof(struct ibmvnic_adapter),
Thomas Falcond45cc3a2017-12-18 12:52:11 -06005394 IBMVNIC_MAX_QUEUES);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005395 if (!netdev)
5396 return -ENOMEM;
5397
5398 adapter = netdev_priv(netdev);
Nathan Fontenot90c80142017-05-03 14:04:32 -04005399 adapter->state = VNIC_PROBING;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005400 dev_set_drvdata(&dev->dev, netdev);
5401 adapter->vdev = dev;
5402 adapter->netdev = netdev;
Sukadev Bhattiprolu76cdc5c2020-11-25 18:04:29 -06005403 adapter->login_pending = false;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005404
5405 ether_addr_copy(adapter->mac_addr, mac_addr_p);
5406 ether_addr_copy(netdev->dev_addr, adapter->mac_addr);
5407 netdev->irq = dev->irq;
5408 netdev->netdev_ops = &ibmvnic_netdev_ops;
5409 netdev->ethtool_ops = &ibmvnic_ethtool_ops;
5410 SET_NETDEV_DEV(netdev, &dev->dev);
5411
Nathan Fontenoted651a12017-05-03 14:04:38 -04005412 INIT_WORK(&adapter->ibmvnic_reset, __ibmvnic_reset);
Juliet Kim7ed5b312019-09-20 16:11:23 -04005413 INIT_DELAYED_WORK(&adapter->ibmvnic_delayed_reset,
5414 __ibmvnic_delayed_reset);
Nathan Fontenoted651a12017-05-03 14:04:38 -04005415 INIT_LIST_HEAD(&adapter->rwi_list);
Thomas Falcon6c5c7482018-12-10 15:22:22 -06005416 spin_lock_init(&adapter->rwi_lock);
Juliet Kim7d7195a2020-03-10 09:23:58 -05005417 spin_lock_init(&adapter->state_lock);
Thomas Falconff25dcb2019-11-25 17:12:56 -06005418 mutex_init(&adapter->fw_lock);
Thomas Falconbbd669a2019-04-04 18:58:26 -05005419 init_completion(&adapter->init_done);
Thomas Falcon070eca92019-11-25 17:12:53 -06005420 init_completion(&adapter->fw_done);
5421 init_completion(&adapter->reset_done);
5422 init_completion(&adapter->stats_done);
Juliet Kim7ed5b312019-09-20 16:11:23 -04005423 clear_bit(0, &adapter->resetting);
Nathan Fontenoted651a12017-05-03 14:04:38 -04005424
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04005425 do {
Nathan Fontenot30f79622018-04-06 18:37:06 -05005426 rc = init_crq_queue(adapter);
5427 if (rc) {
5428 dev_err(&dev->dev, "Couldn't initialize crq. rc=%d\n",
5429 rc);
5430 goto ibmvnic_init_fail;
5431 }
5432
Lijun Pan635e4422020-08-19 17:52:26 -05005433 rc = ibmvnic_reset_init(adapter, false);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05005434 if (rc && rc != EAGAIN)
5435 goto ibmvnic_init_fail;
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04005436 } while (rc == EAGAIN);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005437
Thomas Falcon07184212018-05-16 15:49:05 -05005438 rc = init_stats_buffers(adapter);
5439 if (rc)
5440 goto ibmvnic_init_fail;
5441
5442 rc = init_stats_token(adapter);
5443 if (rc)
5444 goto ibmvnic_stats_fail;
5445
Thomas Falconf39f0d12017-02-14 10:22:59 -06005446 netdev->mtu = adapter->req_mtu - ETH_HLEN;
John Allenc26eba02017-10-26 16:23:25 -05005447 netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
5448 netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005449
Thomas Falcon40c9db82017-06-12 12:35:04 -05005450 rc = device_create_file(&dev->dev, &dev_attr_failover);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05005451 if (rc)
Thomas Falcon07184212018-05-16 15:49:05 -05005452 goto ibmvnic_dev_file_err;
Thomas Falcon40c9db82017-06-12 12:35:04 -05005453
Mick Tarsele876a8a2017-09-28 13:53:18 -07005454 netif_carrier_off(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005455 rc = register_netdev(netdev);
5456 if (rc) {
5457 dev_err(&dev->dev, "failed to register netdev rc=%d\n", rc);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05005458 goto ibmvnic_register_fail;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005459 }
5460 dev_info(&dev->dev, "ibmvnic registered\n");
5461
Nathan Fontenot90c80142017-05-03 14:04:32 -04005462 adapter->state = VNIC_PROBED;
John Allenc26eba02017-10-26 16:23:25 -05005463
5464 adapter->wait_for_reset = false;
Dany Maddena86d5c62020-11-25 18:04:31 -06005465 adapter->last_reset_time = jiffies;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005466 return 0;
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05005467
5468ibmvnic_register_fail:
5469 device_remove_file(&dev->dev, &dev_attr_failover);
5470
Thomas Falcon07184212018-05-16 15:49:05 -05005471ibmvnic_dev_file_err:
5472 release_stats_token(adapter);
5473
5474ibmvnic_stats_fail:
5475 release_stats_buffers(adapter);
5476
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05005477ibmvnic_init_fail:
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06005478 release_sub_crqs(adapter, 1);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05005479 release_crq_queue(adapter);
Thomas Falconff25dcb2019-11-25 17:12:56 -06005480 mutex_destroy(&adapter->fw_lock);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05005481 free_netdev(netdev);
5482
5483 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005484}
5485
Uwe Kleine-König386a9662021-02-25 23:18:34 +01005486static void ibmvnic_remove(struct vio_dev *dev)
Thomas Falcon032c5e82015-12-21 11:26:06 -06005487{
5488 struct net_device *netdev = dev_get_drvdata(&dev->dev);
Nathan Fontenot37489052017-04-19 13:45:04 -04005489 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Juliet Kim7d7195a2020-03-10 09:23:58 -05005490 unsigned long flags;
5491
5492 spin_lock_irqsave(&adapter->state_lock, flags);
Sukadev Bhattiprolu4a41c422021-02-12 20:42:50 -08005493
5494 /* If ibmvnic_reset() is scheduling a reset, wait for it to
5495 * finish. Then, set the state to REMOVING to prevent it from
5496 * scheduling any more work and to have reset functions ignore
5497 * any resets that have already been scheduled. Drop the lock
5498 * after setting state, so __ibmvnic_reset() which is called
5499 * from the flush_work() below, can make progress.
5500 */
Junlin Yang69cdb792021-03-05 16:48:39 +08005501 spin_lock(&adapter->rwi_lock);
Nathan Fontenot90c80142017-05-03 14:04:32 -04005502 adapter->state = VNIC_REMOVING;
Junlin Yang69cdb792021-03-05 16:48:39 +08005503 spin_unlock(&adapter->rwi_lock);
Sukadev Bhattiprolu4a41c422021-02-12 20:42:50 -08005504
Juliet Kim7d7195a2020-03-10 09:23:58 -05005505 spin_unlock_irqrestore(&adapter->state_lock, flags);
5506
Thomas Falcon6954a9e2020-06-12 13:34:41 -05005507 flush_work(&adapter->ibmvnic_reset);
5508 flush_delayed_work(&adapter->ibmvnic_delayed_reset);
5509
Juliet Kima5681e22018-11-19 15:59:22 -06005510 rtnl_lock();
5511 unregister_netdevice(netdev);
Nathan Fontenot37489052017-04-19 13:45:04 -04005512
5513 release_resources(adapter);
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06005514 release_sub_crqs(adapter, 1);
Nathan Fontenot37489052017-04-19 13:45:04 -04005515 release_crq_queue(adapter);
5516
Thomas Falcon53cc7722018-02-26 18:10:56 -06005517 release_stats_token(adapter);
5518 release_stats_buffers(adapter);
5519
Nathan Fontenot90c80142017-05-03 14:04:32 -04005520 adapter->state = VNIC_REMOVED;
5521
Juliet Kima5681e22018-11-19 15:59:22 -06005522 rtnl_unlock();
Thomas Falconff25dcb2019-11-25 17:12:56 -06005523 mutex_destroy(&adapter->fw_lock);
Thomas Falcon40c9db82017-06-12 12:35:04 -05005524 device_remove_file(&dev->dev, &dev_attr_failover);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005525 free_netdev(netdev);
5526 dev_set_drvdata(&dev->dev, NULL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005527}
5528
Thomas Falcon40c9db82017-06-12 12:35:04 -05005529static ssize_t failover_store(struct device *dev, struct device_attribute *attr,
5530 const char *buf, size_t count)
5531{
5532 struct net_device *netdev = dev_get_drvdata(dev);
5533 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
5534 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
5535 __be64 session_token;
5536 long rc;
5537
5538 if (!sysfs_streq(buf, "1"))
5539 return -EINVAL;
5540
5541 rc = plpar_hcall(H_VIOCTL, retbuf, adapter->vdev->unit_address,
5542 H_GET_SESSION_TOKEN, 0, 0, 0);
5543 if (rc) {
5544 netdev_err(netdev, "Couldn't retrieve session token, rc %ld\n",
5545 rc);
Lijun Pan334c4242021-04-13 03:31:44 -05005546 goto last_resort;
Thomas Falcon40c9db82017-06-12 12:35:04 -05005547 }
5548
5549 session_token = (__be64)retbuf[0];
5550 netdev_dbg(netdev, "Initiating client failover, session id %llx\n",
5551 be64_to_cpu(session_token));
5552 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
5553 H_SESSION_ERR_DETECTED, session_token, 0, 0);
Lijun Pan334c4242021-04-13 03:31:44 -05005554 if (rc)
5555 netdev_err(netdev,
5556 "H_VIOCTL initiated failover failed, rc %ld\n",
Thomas Falcon40c9db82017-06-12 12:35:04 -05005557 rc);
Lijun Pan334c4242021-04-13 03:31:44 -05005558
5559last_resort:
5560 netdev_dbg(netdev, "Trying to send CRQ_CMD, the last resort\n");
5561 ibmvnic_reset(adapter, VNIC_RESET_FAILOVER);
Thomas Falcon40c9db82017-06-12 12:35:04 -05005562
5563 return count;
5564}
Joe Perches6cbaefb2017-12-19 10:15:09 -08005565static DEVICE_ATTR_WO(failover);
Thomas Falcon40c9db82017-06-12 12:35:04 -05005566
Thomas Falcon032c5e82015-12-21 11:26:06 -06005567static unsigned long ibmvnic_get_desired_dma(struct vio_dev *vdev)
5568{
5569 struct net_device *netdev = dev_get_drvdata(&vdev->dev);
5570 struct ibmvnic_adapter *adapter;
5571 struct iommu_table *tbl;
5572 unsigned long ret = 0;
5573 int i;
5574
5575 tbl = get_iommu_table_base(&vdev->dev);
5576
5577 /* netdev inits at probe time along with the structures we need below*/
5578 if (!netdev)
5579 return IOMMU_PAGE_ALIGN(IBMVNIC_IO_ENTITLEMENT_DEFAULT, tbl);
5580
5581 adapter = netdev_priv(netdev);
5582
5583 ret += PAGE_SIZE; /* the crq message queue */
Thomas Falcon032c5e82015-12-21 11:26:06 -06005584 ret += IOMMU_PAGE_ALIGN(sizeof(struct ibmvnic_statistics), tbl);
5585
5586 for (i = 0; i < adapter->req_tx_queues + adapter->req_rx_queues; i++)
5587 ret += 4 * PAGE_SIZE; /* the scrq message queue */
5588
Thomas Falcon507ebe62020-08-21 13:39:01 -05005589 for (i = 0; i < adapter->num_active_rx_pools; i++)
Thomas Falcon032c5e82015-12-21 11:26:06 -06005590 ret += adapter->rx_pool[i].size *
5591 IOMMU_PAGE_ALIGN(adapter->rx_pool[i].buff_size, tbl);
5592
5593 return ret;
5594}
5595
5596static int ibmvnic_resume(struct device *dev)
5597{
5598 struct net_device *netdev = dev_get_drvdata(dev);
5599 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005600
John Allencb89ba22017-06-19 11:27:53 -05005601 if (adapter->state != VNIC_OPEN)
5602 return 0;
5603
John Allena2488782017-07-24 13:26:06 -05005604 tasklet_schedule(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005605
5606 return 0;
5607}
5608
Arvind Yadav8c37bc62017-08-17 18:52:54 +05305609static const struct vio_device_id ibmvnic_device_table[] = {
Thomas Falcon032c5e82015-12-21 11:26:06 -06005610 {"network", "IBM,vnic"},
5611 {"", "" }
5612};
5613MODULE_DEVICE_TABLE(vio, ibmvnic_device_table);
5614
5615static const struct dev_pm_ops ibmvnic_pm_ops = {
5616 .resume = ibmvnic_resume
5617};
5618
5619static struct vio_driver ibmvnic_driver = {
5620 .id_table = ibmvnic_device_table,
5621 .probe = ibmvnic_probe,
5622 .remove = ibmvnic_remove,
5623 .get_desired_dma = ibmvnic_get_desired_dma,
5624 .name = ibmvnic_driver_name,
5625 .pm = &ibmvnic_pm_ops,
5626};
5627
5628/* module functions */
5629static int __init ibmvnic_module_init(void)
5630{
5631 pr_info("%s: %s %s\n", ibmvnic_driver_name, ibmvnic_driver_string,
5632 IBMVNIC_DRIVER_VERSION);
5633
5634 return vio_register_driver(&ibmvnic_driver);
5635}
5636
5637static void __exit ibmvnic_module_exit(void)
5638{
5639 vio_unregister_driver(&ibmvnic_driver);
5640}
5641
5642module_init(ibmvnic_module_init);
5643module_exit(ibmvnic_module_exit);