blob: 2d15b446ceb3545001f972913324633ad78dfa0f [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;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600331 for (i = 0; i < count; ++i) {
Dwip N. Banerjeee552aa32020-11-18 19:12:24 -0600332 skb = netdev_alloc_skb(adapter->netdev, pool->buff_size);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600333 if (!skb) {
334 dev_err(dev, "Couldn't replenish rx buff\n");
335 adapter->replenish_no_mem++;
336 break;
337 }
338
339 index = pool->free_map[pool->next_free];
340
341 if (pool->rx_buff[index].skb)
342 dev_err(dev, "Inconsistent free_map!\n");
343
344 /* Copy the skb to the long term mapped DMA buffer */
345 offset = index * pool->buff_size;
346 dst = pool->long_term_buff.buff + offset;
347 memset(dst, 0, pool->buff_size);
348 dma_addr = pool->long_term_buff.addr + offset;
349 pool->rx_buff[index].data = dst;
350
351 pool->free_map[pool->next_free] = IBMVNIC_INVALID_MAP;
352 pool->rx_buff[index].dma = dma_addr;
353 pool->rx_buff[index].skb = skb;
354 pool->rx_buff[index].pool_index = pool->index;
355 pool->rx_buff[index].size = pool->buff_size;
356
Thomas Falcon4f0b6812020-11-18 19:12:18 -0600357 sub_crq = &ind_bufp->indir_arr[ind_bufp->index++];
358 memset(sub_crq, 0, sizeof(*sub_crq));
359 sub_crq->rx_add.first = IBMVNIC_CRQ_CMD;
360 sub_crq->rx_add.correlator =
Thomas Falcon032c5e82015-12-21 11:26:06 -0600361 cpu_to_be64((u64)&pool->rx_buff[index]);
Thomas Falcon4f0b6812020-11-18 19:12:18 -0600362 sub_crq->rx_add.ioba = cpu_to_be32(dma_addr);
363 sub_crq->rx_add.map_id = pool->long_term_buff.map_id;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600364
365 /* The length field of the sCRQ is defined to be 24 bits so the
366 * buffer size needs to be left shifted by a byte before it is
367 * converted to big endian to prevent the last byte from being
368 * truncated.
369 */
370#ifdef __LITTLE_ENDIAN__
371 shift = 8;
372#endif
Thomas Falcon4f0b6812020-11-18 19:12:18 -0600373 sub_crq->rx_add.len = cpu_to_be32(pool->buff_size << shift);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600374 pool->next_free = (pool->next_free + 1) % pool->size;
Thomas Falcon4f0b6812020-11-18 19:12:18 -0600375 if (ind_bufp->index == IBMVNIC_MAX_IND_DESCS ||
376 i == count - 1) {
377 lpar_rc =
378 send_subcrq_indirect(adapter, handle,
379 (u64)ind_bufp->indir_dma,
380 (u64)ind_bufp->index);
381 if (lpar_rc != H_SUCCESS)
382 goto failure;
383 buffers_added += ind_bufp->index;
384 adapter->replenish_add_buff_success += ind_bufp->index;
385 ind_bufp->index = 0;
386 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600387 }
388 atomic_add(buffers_added, &pool->available);
389 return;
390
391failure:
Thomas Falcon2d14d372018-07-13 12:03:32 -0500392 if (lpar_rc != H_PARAMETER && lpar_rc != H_CLOSED)
393 dev_err_ratelimited(dev, "rx: replenish packet buffer failed\n");
Thomas Falcon4f0b6812020-11-18 19:12:18 -0600394 for (i = ind_bufp->index - 1; i >= 0; --i) {
395 struct ibmvnic_rx_buff *rx_buff;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600396
Thomas Falcon4f0b6812020-11-18 19:12:18 -0600397 pool->next_free = pool->next_free == 0 ?
398 pool->size - 1 : pool->next_free - 1;
399 sub_crq = &ind_bufp->indir_arr[i];
400 rx_buff = (struct ibmvnic_rx_buff *)
401 be64_to_cpu(sub_crq->rx_add.correlator);
402 index = (int)(rx_buff - pool->rx_buff);
403 pool->free_map[pool->next_free] = index;
404 dev_kfree_skb_any(pool->rx_buff[index].skb);
405 pool->rx_buff[index].skb = NULL;
406 }
Dwip N. Banerjeec2af6222020-12-09 20:53:31 -0500407 adapter->replenish_add_buff_failure += ind_bufp->index;
408 atomic_add(buffers_added, &pool->available);
Thomas Falcon4f0b6812020-11-18 19:12:18 -0600409 ind_bufp->index = 0;
Thomas Falcon5a18e1e2018-04-06 18:37:05 -0500410 if (lpar_rc == H_CLOSED || adapter->failover_pending) {
Thomas Falconf185a492017-05-26 10:30:48 -0400411 /* Disable buffer pool replenishment and report carrier off if
Thomas Falcon5a18e1e2018-04-06 18:37:05 -0500412 * queue is closed or pending failover.
413 * Firmware guarantees that a signal will be sent to the
414 * driver, triggering a reset.
Thomas Falconf185a492017-05-26 10:30:48 -0400415 */
416 deactivate_rx_pools(adapter);
417 netif_carrier_off(adapter->netdev);
418 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600419}
420
421static void replenish_pools(struct ibmvnic_adapter *adapter)
422{
423 int i;
424
Thomas Falcon032c5e82015-12-21 11:26:06 -0600425 adapter->replenish_task_cycles++;
Thomas Falcon507ebe62020-08-21 13:39:01 -0500426 for (i = 0; i < adapter->num_active_rx_pools; i++) {
Thomas Falcon032c5e82015-12-21 11:26:06 -0600427 if (adapter->rx_pool[i].active)
428 replenish_rx_pool(adapter, &adapter->rx_pool[i]);
429 }
Sukadev Bhattiprolu38bd5ce2020-12-04 18:22:35 -0800430
431 netdev_dbg(adapter->netdev, "Replenished %d pools\n", i);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600432}
433
John Allen3d52b592017-08-02 16:44:14 -0500434static void release_stats_buffers(struct ibmvnic_adapter *adapter)
435{
436 kfree(adapter->tx_stats_buffers);
437 kfree(adapter->rx_stats_buffers);
Thomas Falconb0992ec2018-02-06 17:25:23 -0600438 adapter->tx_stats_buffers = NULL;
439 adapter->rx_stats_buffers = NULL;
John Allen3d52b592017-08-02 16:44:14 -0500440}
441
442static int init_stats_buffers(struct ibmvnic_adapter *adapter)
443{
444 adapter->tx_stats_buffers =
Nathan Fontenotabcae542018-02-19 13:30:47 -0600445 kcalloc(IBMVNIC_MAX_QUEUES,
John Allen3d52b592017-08-02 16:44:14 -0500446 sizeof(struct ibmvnic_tx_queue_stats),
447 GFP_KERNEL);
448 if (!adapter->tx_stats_buffers)
449 return -ENOMEM;
450
451 adapter->rx_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_rx_queue_stats),
454 GFP_KERNEL);
455 if (!adapter->rx_stats_buffers)
456 return -ENOMEM;
457
458 return 0;
459}
460
Nathan Fontenot7bbc27a2017-03-30 02:49:23 -0400461static void release_stats_token(struct ibmvnic_adapter *adapter)
462{
463 struct device *dev = &adapter->vdev->dev;
464
465 if (!adapter->stats_token)
466 return;
467
468 dma_unmap_single(dev, adapter->stats_token,
469 sizeof(struct ibmvnic_statistics),
470 DMA_FROM_DEVICE);
471 adapter->stats_token = 0;
472}
473
474static int init_stats_token(struct ibmvnic_adapter *adapter)
475{
476 struct device *dev = &adapter->vdev->dev;
477 dma_addr_t stok;
478
479 stok = dma_map_single(dev, &adapter->stats,
480 sizeof(struct ibmvnic_statistics),
481 DMA_FROM_DEVICE);
482 if (dma_mapping_error(dev, stok)) {
483 dev_err(dev, "Couldn't map stats buffer\n");
484 return -1;
485 }
486
487 adapter->stats_token = stok;
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500488 netdev_dbg(adapter->netdev, "Stats token initialized (%llx)\n", stok);
Nathan Fontenot7bbc27a2017-03-30 02:49:23 -0400489 return 0;
490}
491
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400492static int reset_rx_pools(struct ibmvnic_adapter *adapter)
493{
494 struct ibmvnic_rx_pool *rx_pool;
Thomas Falcon507ebe62020-08-21 13:39:01 -0500495 u64 buff_size;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400496 int rx_scrqs;
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500497 int i, j, rc;
John Allen896d8692018-01-18 16:26:31 -0600498
Mingming Cao9f134572020-08-25 13:26:41 -0400499 if (!adapter->rx_pool)
500 return -1;
501
Thomas Falcon507ebe62020-08-21 13:39:01 -0500502 buff_size = adapter->cur_rx_buf_sz;
503 rx_scrqs = adapter->num_active_rx_pools;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400504 for (i = 0; i < rx_scrqs; i++) {
505 rx_pool = &adapter->rx_pool[i];
506
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500507 netdev_dbg(adapter->netdev, "Re-setting rx_pool[%d]\n", i);
508
Thomas Falcon507ebe62020-08-21 13:39:01 -0500509 if (rx_pool->buff_size != buff_size) {
John Allen896d8692018-01-18 16:26:31 -0600510 free_long_term_buff(adapter, &rx_pool->long_term_buff);
Dwip N. Banerjee9a87c3f2020-11-18 19:12:22 -0600511 rx_pool->buff_size = ALIGN(buff_size, L1_CACHE_BYTES);
Thomas Falcon7c940b12019-06-07 16:03:55 -0500512 rc = alloc_long_term_buff(adapter,
513 &rx_pool->long_term_buff,
514 rx_pool->size *
515 rx_pool->buff_size);
John Allen896d8692018-01-18 16:26:31 -0600516 } else {
Sukadev Bhattiprolu0ec13af2021-06-23 21:13:10 -0700517 rc = reset_long_term_buff(adapter,
518 &rx_pool->long_term_buff);
John Allen896d8692018-01-18 16:26:31 -0600519 }
520
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500521 if (rc)
522 return rc;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400523
524 for (j = 0; j < rx_pool->size; j++)
525 rx_pool->free_map[j] = j;
526
527 memset(rx_pool->rx_buff, 0,
528 rx_pool->size * sizeof(struct ibmvnic_rx_buff));
529
530 atomic_set(&rx_pool->available, 0);
531 rx_pool->next_alloc = 0;
532 rx_pool->next_free = 0;
Thomas Falconc3e53b92017-06-14 23:50:05 -0500533 rx_pool->active = 1;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400534 }
535
536 return 0;
537}
538
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400539static void release_rx_pools(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -0600540{
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400541 struct ibmvnic_rx_pool *rx_pool;
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400542 int i, j;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600543
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400544 if (!adapter->rx_pool)
Thomas Falcon032c5e82015-12-21 11:26:06 -0600545 return;
546
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600547 for (i = 0; i < adapter->num_active_rx_pools; i++) {
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400548 rx_pool = &adapter->rx_pool[i];
549
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500550 netdev_dbg(adapter->netdev, "Releasing rx_pool[%d]\n", i);
551
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400552 kfree(rx_pool->free_map);
553 free_long_term_buff(adapter, &rx_pool->long_term_buff);
554
555 if (!rx_pool->rx_buff)
Nathan Fontenote0ebe9422017-05-03 14:04:50 -0400556 continue;
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400557
558 for (j = 0; j < rx_pool->size; j++) {
559 if (rx_pool->rx_buff[j].skb) {
Thomas Falconb7cdec32018-11-21 11:17:58 -0600560 dev_kfree_skb_any(rx_pool->rx_buff[j].skb);
561 rx_pool->rx_buff[j].skb = NULL;
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400562 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600563 }
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400564
565 kfree(rx_pool->rx_buff);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600566 }
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400567
568 kfree(adapter->rx_pool);
569 adapter->rx_pool = NULL;
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600570 adapter->num_active_rx_pools = 0;
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400571}
572
573static int init_rx_pools(struct net_device *netdev)
574{
575 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
576 struct device *dev = &adapter->vdev->dev;
577 struct ibmvnic_rx_pool *rx_pool;
578 int rxadd_subcrqs;
Thomas Falcon507ebe62020-08-21 13:39:01 -0500579 u64 buff_size;
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400580 int i, j;
581
Thomas Falcon507ebe62020-08-21 13:39:01 -0500582 rxadd_subcrqs = adapter->num_active_rx_scrqs;
583 buff_size = adapter->cur_rx_buf_sz;
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400584
585 adapter->rx_pool = kcalloc(rxadd_subcrqs,
586 sizeof(struct ibmvnic_rx_pool),
587 GFP_KERNEL);
588 if (!adapter->rx_pool) {
589 dev_err(dev, "Failed to allocate rx pools\n");
590 return -1;
591 }
592
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600593 adapter->num_active_rx_pools = rxadd_subcrqs;
594
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400595 for (i = 0; i < rxadd_subcrqs; i++) {
596 rx_pool = &adapter->rx_pool[i];
597
598 netdev_dbg(adapter->netdev,
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500599 "Initializing rx_pool[%d], %lld buffs, %lld bytes each\n",
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400600 i, adapter->req_rx_add_entries_per_subcrq,
Thomas Falcon507ebe62020-08-21 13:39:01 -0500601 buff_size);
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400602
603 rx_pool->size = adapter->req_rx_add_entries_per_subcrq;
604 rx_pool->index = i;
Dwip N. Banerjee9a87c3f2020-11-18 19:12:22 -0600605 rx_pool->buff_size = ALIGN(buff_size, L1_CACHE_BYTES);
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400606 rx_pool->active = 1;
607
608 rx_pool->free_map = kcalloc(rx_pool->size, sizeof(int),
609 GFP_KERNEL);
610 if (!rx_pool->free_map) {
611 release_rx_pools(adapter);
612 return -1;
613 }
614
615 rx_pool->rx_buff = kcalloc(rx_pool->size,
616 sizeof(struct ibmvnic_rx_buff),
617 GFP_KERNEL);
618 if (!rx_pool->rx_buff) {
619 dev_err(dev, "Couldn't alloc rx buffers\n");
620 release_rx_pools(adapter);
621 return -1;
622 }
623
624 if (alloc_long_term_buff(adapter, &rx_pool->long_term_buff,
625 rx_pool->size * rx_pool->buff_size)) {
626 release_rx_pools(adapter);
627 return -1;
628 }
629
630 for (j = 0; j < rx_pool->size; ++j)
631 rx_pool->free_map[j] = j;
632
633 atomic_set(&rx_pool->available, 0);
634 rx_pool->next_alloc = 0;
635 rx_pool->next_free = 0;
636 }
637
638 return 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600639}
640
Sukadev Bhattiprolu0ec13af2021-06-23 21:13:10 -0700641static int reset_one_tx_pool(struct ibmvnic_adapter *adapter,
642 struct ibmvnic_tx_pool *tx_pool)
Thomas Falcone26dc252018-03-16 20:00:25 -0500643{
644 int rc, i;
645
Sukadev Bhattiprolu0ec13af2021-06-23 21:13:10 -0700646 rc = reset_long_term_buff(adapter, &tx_pool->long_term_buff);
Thomas Falcone26dc252018-03-16 20:00:25 -0500647 if (rc)
648 return rc;
649
650 memset(tx_pool->tx_buff, 0,
651 tx_pool->num_buffers *
652 sizeof(struct ibmvnic_tx_buff));
653
654 for (i = 0; i < tx_pool->num_buffers; i++)
655 tx_pool->free_map[i] = i;
656
657 tx_pool->consumer_index = 0;
658 tx_pool->producer_index = 0;
659
660 return 0;
661}
662
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400663static int reset_tx_pools(struct ibmvnic_adapter *adapter)
664{
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400665 int tx_scrqs;
Thomas Falcone26dc252018-03-16 20:00:25 -0500666 int i, rc;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400667
Mingming Cao9f134572020-08-25 13:26:41 -0400668 if (!adapter->tx_pool)
669 return -1;
670
Thomas Falcon507ebe62020-08-21 13:39:01 -0500671 tx_scrqs = adapter->num_active_tx_pools;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400672 for (i = 0; i < tx_scrqs; i++) {
Sukadev Bhattiprolu65d64702021-06-23 21:13:12 -0700673 ibmvnic_tx_scrq_clean_buffer(adapter, adapter->tx_scrq[i]);
Sukadev Bhattiprolu0ec13af2021-06-23 21:13:10 -0700674 rc = reset_one_tx_pool(adapter, &adapter->tso_pool[i]);
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500675 if (rc)
676 return rc;
Sukadev Bhattiprolu0ec13af2021-06-23 21:13:10 -0700677 rc = reset_one_tx_pool(adapter, &adapter->tx_pool[i]);
Thomas Falconfdb06102017-10-17 12:36:55 -0500678 if (rc)
679 return rc;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400680 }
681
682 return 0;
683}
684
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -0200685static void release_vpd_data(struct ibmvnic_adapter *adapter)
686{
687 if (!adapter->vpd)
688 return;
689
690 kfree(adapter->vpd->buff);
691 kfree(adapter->vpd);
Thomas Falconb0992ec2018-02-06 17:25:23 -0600692
693 adapter->vpd = NULL;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -0200694}
695
Thomas Falconfb794212018-03-16 20:00:26 -0500696static void release_one_tx_pool(struct ibmvnic_adapter *adapter,
697 struct ibmvnic_tx_pool *tx_pool)
698{
699 kfree(tx_pool->tx_buff);
700 kfree(tx_pool->free_map);
701 free_long_term_buff(adapter, &tx_pool->long_term_buff);
702}
703
Nathan Fontenotc657e322017-03-30 02:49:06 -0400704static void release_tx_pools(struct ibmvnic_adapter *adapter)
705{
John Allen896d8692018-01-18 16:26:31 -0600706 int i;
Nathan Fontenotc657e322017-03-30 02:49:06 -0400707
708 if (!adapter->tx_pool)
709 return;
710
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600711 for (i = 0; i < adapter->num_active_tx_pools; i++) {
Thomas Falconfb794212018-03-16 20:00:26 -0500712 release_one_tx_pool(adapter, &adapter->tx_pool[i]);
713 release_one_tx_pool(adapter, &adapter->tso_pool[i]);
Nathan Fontenotc657e322017-03-30 02:49:06 -0400714 }
715
716 kfree(adapter->tx_pool);
717 adapter->tx_pool = NULL;
Thomas Falconfb794212018-03-16 20:00:26 -0500718 kfree(adapter->tso_pool);
719 adapter->tso_pool = NULL;
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600720 adapter->num_active_tx_pools = 0;
Nathan Fontenotc657e322017-03-30 02:49:06 -0400721}
722
Thomas Falcon32053062018-03-16 20:00:27 -0500723static int init_one_tx_pool(struct net_device *netdev,
724 struct ibmvnic_tx_pool *tx_pool,
725 int num_entries, int buf_size)
726{
727 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
728 int i;
729
730 tx_pool->tx_buff = kcalloc(num_entries,
731 sizeof(struct ibmvnic_tx_buff),
732 GFP_KERNEL);
733 if (!tx_pool->tx_buff)
734 return -1;
735
736 if (alloc_long_term_buff(adapter, &tx_pool->long_term_buff,
737 num_entries * buf_size))
738 return -1;
739
740 tx_pool->free_map = kcalloc(num_entries, sizeof(int), GFP_KERNEL);
741 if (!tx_pool->free_map)
742 return -1;
743
744 for (i = 0; i < num_entries; i++)
745 tx_pool->free_map[i] = i;
746
747 tx_pool->consumer_index = 0;
748 tx_pool->producer_index = 0;
749 tx_pool->num_buffers = num_entries;
750 tx_pool->buf_size = buf_size;
751
752 return 0;
753}
754
Nathan Fontenotc657e322017-03-30 02:49:06 -0400755static int init_tx_pools(struct net_device *netdev)
756{
757 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Nathan Fontenotc657e322017-03-30 02:49:06 -0400758 int tx_subcrqs;
Dwip N. Banerjee9a87c3f2020-11-18 19:12:22 -0600759 u64 buff_size;
Thomas Falcon32053062018-03-16 20:00:27 -0500760 int i, rc;
Nathan Fontenotc657e322017-03-30 02:49:06 -0400761
Thomas Falcon507ebe62020-08-21 13:39:01 -0500762 tx_subcrqs = adapter->num_active_tx_scrqs;
Nathan Fontenotc657e322017-03-30 02:49:06 -0400763 adapter->tx_pool = kcalloc(tx_subcrqs,
764 sizeof(struct ibmvnic_tx_pool), GFP_KERNEL);
765 if (!adapter->tx_pool)
766 return -1;
767
Thomas Falcon32053062018-03-16 20:00:27 -0500768 adapter->tso_pool = kcalloc(tx_subcrqs,
769 sizeof(struct ibmvnic_tx_pool), GFP_KERNEL);
770 if (!adapter->tso_pool)
771 return -1;
772
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600773 adapter->num_active_tx_pools = tx_subcrqs;
774
Nathan Fontenotc657e322017-03-30 02:49:06 -0400775 for (i = 0; i < tx_subcrqs; i++) {
Dwip N. Banerjee9a87c3f2020-11-18 19:12:22 -0600776 buff_size = adapter->req_mtu + VLAN_HLEN;
777 buff_size = ALIGN(buff_size, L1_CACHE_BYTES);
Thomas Falcon32053062018-03-16 20:00:27 -0500778 rc = init_one_tx_pool(netdev, &adapter->tx_pool[i],
779 adapter->req_tx_entries_per_subcrq,
Dwip N. Banerjee9a87c3f2020-11-18 19:12:22 -0600780 buff_size);
Thomas Falcon32053062018-03-16 20:00:27 -0500781 if (rc) {
Nathan Fontenotc657e322017-03-30 02:49:06 -0400782 release_tx_pools(adapter);
Thomas Falcon32053062018-03-16 20:00:27 -0500783 return rc;
Nathan Fontenotc657e322017-03-30 02:49:06 -0400784 }
785
Thomas Falcon7c940b12019-06-07 16:03:55 -0500786 rc = init_one_tx_pool(netdev, &adapter->tso_pool[i],
787 IBMVNIC_TSO_BUFS,
788 IBMVNIC_TSO_BUF_SZ);
Thomas Falcon32053062018-03-16 20:00:27 -0500789 if (rc) {
Nathan Fontenotc657e322017-03-30 02:49:06 -0400790 release_tx_pools(adapter);
Thomas Falcon32053062018-03-16 20:00:27 -0500791 return rc;
Nathan Fontenotc657e322017-03-30 02:49:06 -0400792 }
Nathan Fontenotc657e322017-03-30 02:49:06 -0400793 }
794
795 return 0;
796}
797
John Allend944c3d62017-05-26 10:30:13 -0400798static void ibmvnic_napi_enable(struct ibmvnic_adapter *adapter)
799{
800 int i;
801
802 if (adapter->napi_enabled)
803 return;
804
805 for (i = 0; i < adapter->req_rx_queues; i++)
806 napi_enable(&adapter->napi[i]);
807
808 adapter->napi_enabled = true;
809}
810
811static void ibmvnic_napi_disable(struct ibmvnic_adapter *adapter)
812{
813 int i;
814
815 if (!adapter->napi_enabled)
816 return;
817
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500818 for (i = 0; i < adapter->req_rx_queues; i++) {
819 netdev_dbg(adapter->netdev, "Disabling napi[%d]\n", i);
John Allend944c3d62017-05-26 10:30:13 -0400820 napi_disable(&adapter->napi[i]);
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500821 }
John Allend944c3d62017-05-26 10:30:13 -0400822
823 adapter->napi_enabled = false;
824}
825
Nathan Fontenot86f669b2018-02-19 13:30:39 -0600826static int init_napi(struct ibmvnic_adapter *adapter)
827{
828 int i;
829
830 adapter->napi = kcalloc(adapter->req_rx_queues,
831 sizeof(struct napi_struct), GFP_KERNEL);
832 if (!adapter->napi)
833 return -ENOMEM;
834
835 for (i = 0; i < adapter->req_rx_queues; i++) {
836 netdev_dbg(adapter->netdev, "Adding napi[%d]\n", i);
837 netif_napi_add(adapter->netdev, &adapter->napi[i],
838 ibmvnic_poll, NAPI_POLL_WEIGHT);
839 }
840
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600841 adapter->num_active_rx_napi = adapter->req_rx_queues;
Nathan Fontenot86f669b2018-02-19 13:30:39 -0600842 return 0;
843}
844
845static void release_napi(struct ibmvnic_adapter *adapter)
846{
847 int i;
848
849 if (!adapter->napi)
850 return;
851
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600852 for (i = 0; i < adapter->num_active_rx_napi; i++) {
Wen Yang390de192018-12-11 12:20:46 +0800853 netdev_dbg(adapter->netdev, "Releasing napi[%d]\n", i);
854 netif_napi_del(&adapter->napi[i]);
Nathan Fontenot86f669b2018-02-19 13:30:39 -0600855 }
856
857 kfree(adapter->napi);
858 adapter->napi = NULL;
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600859 adapter->num_active_rx_napi = 0;
Thomas Falconc3f22412018-05-23 13:37:55 -0500860 adapter->napi_enabled = false;
Nathan Fontenot86f669b2018-02-19 13:30:39 -0600861}
862
Lijun Pan0666ef72021-04-12 02:41:28 -0500863static const char *adapter_state_to_string(enum vnic_state state)
864{
865 switch (state) {
866 case VNIC_PROBING:
867 return "PROBING";
868 case VNIC_PROBED:
869 return "PROBED";
870 case VNIC_OPENING:
871 return "OPENING";
872 case VNIC_OPEN:
873 return "OPEN";
874 case VNIC_CLOSING:
875 return "CLOSING";
876 case VNIC_CLOSED:
877 return "CLOSED";
878 case VNIC_REMOVING:
879 return "REMOVING";
880 case VNIC_REMOVED:
881 return "REMOVED";
882 default:
883 return "UNKNOWN";
884 }
885}
886
John Allena57a5d22017-03-17 17:13:41 -0500887static int ibmvnic_login(struct net_device *netdev)
Thomas Falcon032c5e82015-12-21 11:26:06 -0600888{
889 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Dany Madden98c41f02020-11-25 18:04:32 -0600890 unsigned long timeout = msecs_to_jiffies(20000);
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500891 int retry_count = 0;
Thomas Falcondff515a32020-06-15 10:29:23 -0500892 int retries = 10;
Thomas Falconeb110412018-05-24 14:37:53 -0500893 bool retry;
Thomas Falcon4d96f122017-08-01 15:04:36 -0500894 int rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600895
John Allenbd0b6722017-03-17 17:13:40 -0500896 do {
Thomas Falconeb110412018-05-24 14:37:53 -0500897 retry = false;
Thomas Falcondff515a32020-06-15 10:29:23 -0500898 if (retry_count > retries) {
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500899 netdev_warn(netdev, "Login attempts exceeded\n");
900 return -1;
901 }
902
903 adapter->init_done_rc = 0;
904 reinit_completion(&adapter->init_done);
905 rc = send_login(adapter);
Dany Maddenc98d9cc2020-11-25 18:04:30 -0600906 if (rc)
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500907 return rc;
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500908
909 if (!wait_for_completion_timeout(&adapter->init_done,
910 timeout)) {
Thomas Falcondff515a32020-06-15 10:29:23 -0500911 netdev_warn(netdev, "Login timed out, retrying...\n");
912 retry = true;
913 adapter->init_done_rc = 0;
914 retry_count++;
915 continue;
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500916 }
917
Thomas Falcondff515a32020-06-15 10:29:23 -0500918 if (adapter->init_done_rc == ABORTED) {
919 netdev_warn(netdev, "Login aborted, retrying...\n");
920 retry = true;
921 adapter->init_done_rc = 0;
922 retry_count++;
923 /* FW or device may be busy, so
924 * wait a bit before retrying login
925 */
926 msleep(500);
927 } else if (adapter->init_done_rc == PARTIALSUCCESS) {
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500928 retry_count++;
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -0600929 release_sub_crqs(adapter, 1);
John Allenbd0b6722017-03-17 17:13:40 -0500930
Thomas Falconeb110412018-05-24 14:37:53 -0500931 retry = true;
932 netdev_dbg(netdev,
933 "Received partial success, retrying...\n");
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500934 adapter->init_done_rc = 0;
John Allenbd0b6722017-03-17 17:13:40 -0500935 reinit_completion(&adapter->init_done);
Lijun Pan491099a2020-09-27 20:13:26 -0500936 send_query_cap(adapter);
John Allenbd0b6722017-03-17 17:13:40 -0500937 if (!wait_for_completion_timeout(&adapter->init_done,
938 timeout)) {
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500939 netdev_warn(netdev,
940 "Capabilities query timed out\n");
John Allenbd0b6722017-03-17 17:13:40 -0500941 return -1;
942 }
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500943
Thomas Falcon4d96f122017-08-01 15:04:36 -0500944 rc = init_sub_crqs(adapter);
945 if (rc) {
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500946 netdev_warn(netdev,
947 "SCRQ initialization failed\n");
Thomas Falcon4d96f122017-08-01 15:04:36 -0500948 return -1;
949 }
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500950
Thomas Falcon4d96f122017-08-01 15:04:36 -0500951 rc = init_sub_crq_irqs(adapter);
952 if (rc) {
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500953 netdev_warn(netdev,
954 "SCRQ irq initialization failed\n");
Thomas Falcon4d96f122017-08-01 15:04:36 -0500955 return -1;
956 }
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500957 } else if (adapter->init_done_rc) {
958 netdev_warn(netdev, "Adapter login failed\n");
John Allenbd0b6722017-03-17 17:13:40 -0500959 return -1;
960 }
Thomas Falconeb110412018-05-24 14:37:53 -0500961 } while (retry);
John Allenbd0b6722017-03-17 17:13:40 -0500962
Thomas Falcon62740e92019-05-09 23:13:43 -0500963 __ibmvnic_set_mac(netdev, adapter->mac_addr);
Thomas Falcon3d166132018-01-10 19:39:52 -0600964
Lijun Pan0666ef72021-04-12 02:41:28 -0500965 netdev_dbg(netdev, "[S:%s] Login succeeded\n", adapter_state_to_string(adapter->state));
John Allena57a5d22017-03-17 17:13:41 -0500966 return 0;
967}
968
Thomas Falcon34f0f4e2018-02-13 18:23:40 -0600969static void release_login_buffer(struct ibmvnic_adapter *adapter)
970{
971 kfree(adapter->login_buf);
972 adapter->login_buf = NULL;
973}
974
975static void release_login_rsp_buffer(struct ibmvnic_adapter *adapter)
976{
977 kfree(adapter->login_rsp_buf);
978 adapter->login_rsp_buf = NULL;
979}
980
Nathan Fontenot1b8955e2017-03-30 02:49:29 -0400981static void release_resources(struct ibmvnic_adapter *adapter)
982{
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -0200983 release_vpd_data(adapter);
984
Nathan Fontenot1b8955e2017-03-30 02:49:29 -0400985 release_tx_pools(adapter);
986 release_rx_pools(adapter);
987
Nathan Fontenot86f669b2018-02-19 13:30:39 -0600988 release_napi(adapter);
Lijun Pana0c8be52020-12-19 15:39:19 -0600989 release_login_buffer(adapter);
Thomas Falcon34f0f4e2018-02-13 18:23:40 -0600990 release_login_rsp_buffer(adapter);
Nathan Fontenot1b8955e2017-03-30 02:49:29 -0400991}
992
Nathan Fontenot53da09e2017-04-21 15:39:04 -0400993static int set_link_state(struct ibmvnic_adapter *adapter, u8 link_state)
994{
995 struct net_device *netdev = adapter->netdev;
Dany Madden98c41f02020-11-25 18:04:32 -0600996 unsigned long timeout = msecs_to_jiffies(20000);
Nathan Fontenot53da09e2017-04-21 15:39:04 -0400997 union ibmvnic_crq crq;
998 bool resend;
999 int rc;
1000
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001001 netdev_dbg(netdev, "setting link state %d\n", link_state);
1002
Nathan Fontenot53da09e2017-04-21 15:39:04 -04001003 memset(&crq, 0, sizeof(crq));
1004 crq.logical_link_state.first = IBMVNIC_CRQ_CMD;
1005 crq.logical_link_state.cmd = LOGICAL_LINK_STATE;
1006 crq.logical_link_state.link_state = link_state;
1007
1008 do {
1009 resend = false;
1010
1011 reinit_completion(&adapter->init_done);
1012 rc = ibmvnic_send_crq(adapter, &crq);
1013 if (rc) {
1014 netdev_err(netdev, "Failed to set link state\n");
1015 return rc;
1016 }
1017
1018 if (!wait_for_completion_timeout(&adapter->init_done,
1019 timeout)) {
1020 netdev_err(netdev, "timeout setting link state\n");
1021 return -1;
1022 }
1023
Lijun Pan4c5f6af2020-08-19 17:52:23 -05001024 if (adapter->init_done_rc == PARTIALSUCCESS) {
Nathan Fontenot53da09e2017-04-21 15:39:04 -04001025 /* Partuial success, delay and re-send */
1026 mdelay(1000);
1027 resend = true;
Thomas Falconab5ec332018-05-23 13:37:59 -05001028 } else if (adapter->init_done_rc) {
1029 netdev_warn(netdev, "Unable to set link state, rc=%d\n",
1030 adapter->init_done_rc);
1031 return adapter->init_done_rc;
Nathan Fontenot53da09e2017-04-21 15:39:04 -04001032 }
1033 } while (resend);
1034
1035 return 0;
1036}
1037
Thomas Falcon7f3c6e62017-04-21 15:38:40 -04001038static int set_real_num_queues(struct net_device *netdev)
1039{
1040 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1041 int rc;
1042
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001043 netdev_dbg(netdev, "Setting real tx/rx queues (%llx/%llx)\n",
1044 adapter->req_tx_queues, adapter->req_rx_queues);
1045
Thomas Falcon7f3c6e62017-04-21 15:38:40 -04001046 rc = netif_set_real_num_tx_queues(netdev, adapter->req_tx_queues);
1047 if (rc) {
1048 netdev_err(netdev, "failed to set the number of tx queues\n");
1049 return rc;
1050 }
1051
1052 rc = netif_set_real_num_rx_queues(netdev, adapter->req_rx_queues);
1053 if (rc)
1054 netdev_err(netdev, "failed to set the number of rx queues\n");
1055
1056 return rc;
1057}
1058
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001059static int ibmvnic_get_vpd(struct ibmvnic_adapter *adapter)
1060{
1061 struct device *dev = &adapter->vdev->dev;
1062 union ibmvnic_crq crq;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001063 int len = 0;
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05001064 int rc;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001065
1066 if (adapter->vpd->buff)
1067 len = adapter->vpd->len;
1068
Thomas Falconff25dcb2019-11-25 17:12:56 -06001069 mutex_lock(&adapter->fw_lock);
1070 adapter->fw_done_rc = 0;
Thomas Falcon070eca92019-11-25 17:12:53 -06001071 reinit_completion(&adapter->fw_done);
Thomas Falconff25dcb2019-11-25 17:12:56 -06001072
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001073 crq.get_vpd_size.first = IBMVNIC_CRQ_CMD;
1074 crq.get_vpd_size.cmd = GET_VPD_SIZE;
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05001075 rc = ibmvnic_send_crq(adapter, &crq);
Thomas Falconff25dcb2019-11-25 17:12:56 -06001076 if (rc) {
1077 mutex_unlock(&adapter->fw_lock);
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05001078 return rc;
Thomas Falconff25dcb2019-11-25 17:12:56 -06001079 }
Thomas Falcon476d96c2019-11-25 17:12:55 -06001080
1081 rc = ibmvnic_wait_for_completion(adapter, &adapter->fw_done, 10000);
1082 if (rc) {
1083 dev_err(dev, "Could not retrieve VPD size, rc = %d\n", rc);
Thomas Falconff25dcb2019-11-25 17:12:56 -06001084 mutex_unlock(&adapter->fw_lock);
Thomas Falcon476d96c2019-11-25 17:12:55 -06001085 return rc;
1086 }
Thomas Falconff25dcb2019-11-25 17:12:56 -06001087 mutex_unlock(&adapter->fw_lock);
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001088
1089 if (!adapter->vpd->len)
1090 return -ENODATA;
1091
1092 if (!adapter->vpd->buff)
1093 adapter->vpd->buff = kzalloc(adapter->vpd->len, GFP_KERNEL);
1094 else if (adapter->vpd->len != len)
1095 adapter->vpd->buff =
1096 krealloc(adapter->vpd->buff,
1097 adapter->vpd->len, GFP_KERNEL);
1098
1099 if (!adapter->vpd->buff) {
1100 dev_err(dev, "Could allocate VPD buffer\n");
1101 return -ENOMEM;
1102 }
1103
1104 adapter->vpd->dma_addr =
1105 dma_map_single(dev, adapter->vpd->buff, adapter->vpd->len,
1106 DMA_FROM_DEVICE);
Desnes Augusto Nunes do Rosariof7431062017-11-17 09:09:04 -02001107 if (dma_mapping_error(dev, adapter->vpd->dma_addr)) {
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001108 dev_err(dev, "Could not map VPD buffer\n");
1109 kfree(adapter->vpd->buff);
Thomas Falconb0992ec2018-02-06 17:25:23 -06001110 adapter->vpd->buff = NULL;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001111 return -ENOMEM;
1112 }
1113
Thomas Falconff25dcb2019-11-25 17:12:56 -06001114 mutex_lock(&adapter->fw_lock);
1115 adapter->fw_done_rc = 0;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001116 reinit_completion(&adapter->fw_done);
Thomas Falconff25dcb2019-11-25 17:12:56 -06001117
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001118 crq.get_vpd.first = IBMVNIC_CRQ_CMD;
1119 crq.get_vpd.cmd = GET_VPD;
1120 crq.get_vpd.ioba = cpu_to_be32(adapter->vpd->dma_addr);
1121 crq.get_vpd.len = cpu_to_be32((u32)adapter->vpd->len);
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05001122 rc = ibmvnic_send_crq(adapter, &crq);
1123 if (rc) {
1124 kfree(adapter->vpd->buff);
1125 adapter->vpd->buff = NULL;
Thomas Falconff25dcb2019-11-25 17:12:56 -06001126 mutex_unlock(&adapter->fw_lock);
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05001127 return rc;
1128 }
Thomas Falcon476d96c2019-11-25 17:12:55 -06001129
1130 rc = ibmvnic_wait_for_completion(adapter, &adapter->fw_done, 10000);
1131 if (rc) {
1132 dev_err(dev, "Unable to retrieve VPD, rc = %d\n", rc);
1133 kfree(adapter->vpd->buff);
1134 adapter->vpd->buff = NULL;
Thomas Falconff25dcb2019-11-25 17:12:56 -06001135 mutex_unlock(&adapter->fw_lock);
Thomas Falcon476d96c2019-11-25 17:12:55 -06001136 return rc;
1137 }
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001138
Thomas Falconff25dcb2019-11-25 17:12:56 -06001139 mutex_unlock(&adapter->fw_lock);
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001140 return 0;
1141}
1142
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001143static int init_resources(struct ibmvnic_adapter *adapter)
John Allena57a5d22017-03-17 17:13:41 -05001144{
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001145 struct net_device *netdev = adapter->netdev;
Nathan Fontenot86f669b2018-02-19 13:30:39 -06001146 int rc;
John Allena57a5d22017-03-17 17:13:41 -05001147
Thomas Falcon7f3c6e62017-04-21 15:38:40 -04001148 rc = set_real_num_queues(netdev);
1149 if (rc)
1150 return rc;
John Allenbd0b6722017-03-17 17:13:40 -05001151
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001152 adapter->vpd = kzalloc(sizeof(*adapter->vpd), GFP_KERNEL);
1153 if (!adapter->vpd)
1154 return -ENOMEM;
1155
John Allen69d08dc2018-01-18 16:27:58 -06001156 /* Vital Product Data (VPD) */
1157 rc = ibmvnic_get_vpd(adapter);
1158 if (rc) {
1159 netdev_err(netdev, "failed to initialize Vital Product Data (VPD)\n");
1160 return rc;
1161 }
1162
Thomas Falcon032c5e82015-12-21 11:26:06 -06001163 adapter->map_id = 1;
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001164
Nathan Fontenot86f669b2018-02-19 13:30:39 -06001165 rc = init_napi(adapter);
1166 if (rc)
1167 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001168
Lijun Pan69980d02020-09-27 20:13:28 -05001169 send_query_map(adapter);
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -04001170
1171 rc = init_rx_pools(netdev);
1172 if (rc)
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001173 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001174
Nathan Fontenotc657e322017-03-30 02:49:06 -04001175 rc = init_tx_pools(netdev);
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001176 return rc;
1177}
1178
Nathan Fontenoted651a12017-05-03 14:04:38 -04001179static int __ibmvnic_open(struct net_device *netdev)
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001180{
1181 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001182 enum vnic_state prev_state = adapter->state;
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001183 int i, rc;
1184
Nathan Fontenot90c80142017-05-03 14:04:32 -04001185 adapter->state = VNIC_OPENING;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001186 replenish_pools(adapter);
John Allend944c3d62017-05-26 10:30:13 -04001187 ibmvnic_napi_enable(adapter);
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001188
Thomas Falcon032c5e82015-12-21 11:26:06 -06001189 /* We're ready to receive frames, enable the sub-crq interrupts and
1190 * set the logical link state to up
1191 */
Nathan Fontenoted651a12017-05-03 14:04:38 -04001192 for (i = 0; i < adapter->req_rx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001193 netdev_dbg(netdev, "Enabling rx_scrq[%d] irq\n", i);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001194 if (prev_state == VNIC_CLOSED)
1195 enable_irq(adapter->rx_scrq[i]->irq);
Thomas Falconf23e0642018-04-15 18:53:36 -05001196 enable_scrq_irq(adapter, adapter->rx_scrq[i]);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001197 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001198
Nathan Fontenoted651a12017-05-03 14:04:38 -04001199 for (i = 0; i < adapter->req_tx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001200 netdev_dbg(netdev, "Enabling tx_scrq[%d] irq\n", i);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001201 if (prev_state == VNIC_CLOSED)
1202 enable_irq(adapter->tx_scrq[i]->irq);
Thomas Falconf23e0642018-04-15 18:53:36 -05001203 enable_scrq_irq(adapter, adapter->tx_scrq[i]);
Thomas Falcon0d973382020-11-18 19:12:19 -06001204 netdev_tx_reset_queue(netdev_get_tx_queue(netdev, i));
Nathan Fontenoted651a12017-05-03 14:04:38 -04001205 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001206
Nathan Fontenot53da09e2017-04-21 15:39:04 -04001207 rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_UP);
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001208 if (rc) {
Lijun Pan0775ebc2021-04-14 02:46:14 -05001209 ibmvnic_napi_disable(adapter);
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001210 release_resources(adapter);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001211 return rc;
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001212 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001213
Nathan Fontenoted651a12017-05-03 14:04:38 -04001214 netif_tx_start_all_queues(netdev);
1215
Dany Madden2ca220f2021-06-23 21:13:11 -07001216 if (prev_state == VNIC_CLOSED) {
1217 for (i = 0; i < adapter->req_rx_queues; i++)
1218 napi_schedule(&adapter->napi[i]);
1219 }
1220
Nathan Fontenoted651a12017-05-03 14:04:38 -04001221 adapter->state = VNIC_OPEN;
1222 return rc;
1223}
1224
1225static int ibmvnic_open(struct net_device *netdev)
1226{
1227 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
John Allen69d08dc2018-01-18 16:27:58 -06001228 int rc;
Nathan Fontenoted651a12017-05-03 14:04:38 -04001229
Sukadev Bhattiprolu8f1c0fd2021-02-23 21:02:29 -08001230 ASSERT_RTNL();
1231
1232 /* If device failover is pending or we are about to reset, just set
1233 * device state and return. Device operation will be handled by reset
1234 * routine.
1235 *
1236 * It should be safe to overwrite the adapter->state here. Since
1237 * we hold the rtnl, either the reset has not actually started or
1238 * the rtnl got dropped during the set_link_state() in do_reset().
1239 * In the former case, no one else is changing the state (again we
1240 * have the rtnl) and in the latter case, do_reset() will detect and
1241 * honor our setting below.
Thomas Falcon5a18e1e2018-04-06 18:37:05 -05001242 */
Sukadev Bhattiprolu8f1c0fd2021-02-23 21:02:29 -08001243 if (adapter->failover_pending || (test_bit(0, &adapter->resetting))) {
Lijun Pan0666ef72021-04-12 02:41:28 -05001244 netdev_dbg(netdev, "[S:%s FOP:%d] Resetting, deferring open\n",
1245 adapter_state_to_string(adapter->state),
1246 adapter->failover_pending);
Thomas Falcon5a18e1e2018-04-06 18:37:05 -05001247 adapter->state = VNIC_OPEN;
Sukadev Bhattiprolu8f1c0fd2021-02-23 21:02:29 -08001248 rc = 0;
1249 goto out;
Thomas Falcon5a18e1e2018-04-06 18:37:05 -05001250 }
1251
Nathan Fontenoted651a12017-05-03 14:04:38 -04001252 if (adapter->state != VNIC_CLOSED) {
1253 rc = ibmvnic_login(netdev);
Juliet Kima5681e22018-11-19 15:59:22 -06001254 if (rc)
Sukadev Bhattiprolu1d850492020-10-30 10:07:11 -07001255 goto out;
Nathan Fontenoted651a12017-05-03 14:04:38 -04001256
1257 rc = init_resources(adapter);
1258 if (rc) {
1259 netdev_err(netdev, "failed to initialize resources\n");
1260 release_resources(adapter);
Sukadev Bhattiprolu1d850492020-10-30 10:07:11 -07001261 goto out;
Nathan Fontenoted651a12017-05-03 14:04:38 -04001262 }
1263 }
1264
1265 rc = __ibmvnic_open(netdev);
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001266
Sukadev Bhattiprolu1d850492020-10-30 10:07:11 -07001267out:
Sukadev Bhattiprolu8f1c0fd2021-02-23 21:02:29 -08001268 /* If open failed and there is a pending failover or in-progress reset,
1269 * set device state and return. Device operation will be handled by
1270 * reset routine. See also comments above regarding rtnl.
Sukadev Bhattiprolu1d850492020-10-30 10:07:11 -07001271 */
Sukadev Bhattiprolu8f1c0fd2021-02-23 21:02:29 -08001272 if (rc &&
1273 (adapter->failover_pending || (test_bit(0, &adapter->resetting)))) {
Sukadev Bhattiprolu1d850492020-10-30 10:07:11 -07001274 adapter->state = VNIC_OPEN;
1275 rc = 0;
1276 }
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001277 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001278}
1279
Thomas Falcond0869c02018-02-13 18:23:43 -06001280static void clean_rx_pools(struct ibmvnic_adapter *adapter)
1281{
1282 struct ibmvnic_rx_pool *rx_pool;
Thomas Falcon637f81d2018-02-26 18:10:57 -06001283 struct ibmvnic_rx_buff *rx_buff;
Thomas Falcond0869c02018-02-13 18:23:43 -06001284 u64 rx_entries;
1285 int rx_scrqs;
1286 int i, j;
1287
1288 if (!adapter->rx_pool)
1289 return;
1290
Thomas Falcon660e3092018-04-20 14:25:32 -05001291 rx_scrqs = adapter->num_active_rx_pools;
Thomas Falcond0869c02018-02-13 18:23:43 -06001292 rx_entries = adapter->req_rx_add_entries_per_subcrq;
1293
1294 /* Free any remaining skbs in the rx buffer pools */
1295 for (i = 0; i < rx_scrqs; i++) {
1296 rx_pool = &adapter->rx_pool[i];
Thomas Falcon637f81d2018-02-26 18:10:57 -06001297 if (!rx_pool || !rx_pool->rx_buff)
Thomas Falcond0869c02018-02-13 18:23:43 -06001298 continue;
1299
1300 netdev_dbg(adapter->netdev, "Cleaning rx_pool[%d]\n", i);
1301 for (j = 0; j < rx_entries; j++) {
Thomas Falcon637f81d2018-02-26 18:10:57 -06001302 rx_buff = &rx_pool->rx_buff[j];
1303 if (rx_buff && rx_buff->skb) {
1304 dev_kfree_skb_any(rx_buff->skb);
1305 rx_buff->skb = NULL;
Thomas Falcond0869c02018-02-13 18:23:43 -06001306 }
1307 }
1308 }
1309}
1310
Thomas Falcone9e1e972018-03-16 20:00:30 -05001311static void clean_one_tx_pool(struct ibmvnic_adapter *adapter,
1312 struct ibmvnic_tx_pool *tx_pool)
Nathan Fontenotb41b83e2017-05-03 14:04:56 -04001313{
Thomas Falcon637f81d2018-02-26 18:10:57 -06001314 struct ibmvnic_tx_buff *tx_buff;
Nathan Fontenotb41b83e2017-05-03 14:04:56 -04001315 u64 tx_entries;
Thomas Falcone9e1e972018-03-16 20:00:30 -05001316 int i;
Nathan Fontenotb41b83e2017-05-03 14:04:56 -04001317
Dan Carpenter050e85c2018-03-23 14:36:15 +03001318 if (!tx_pool || !tx_pool->tx_buff)
Thomas Falcone9e1e972018-03-16 20:00:30 -05001319 return;
1320
1321 tx_entries = tx_pool->num_buffers;
1322
1323 for (i = 0; i < tx_entries; i++) {
1324 tx_buff = &tx_pool->tx_buff[i];
1325 if (tx_buff && tx_buff->skb) {
1326 dev_kfree_skb_any(tx_buff->skb);
1327 tx_buff->skb = NULL;
1328 }
1329 }
1330}
1331
1332static void clean_tx_pools(struct ibmvnic_adapter *adapter)
1333{
1334 int tx_scrqs;
1335 int i;
1336
1337 if (!adapter->tx_pool || !adapter->tso_pool)
Nathan Fontenotb41b83e2017-05-03 14:04:56 -04001338 return;
1339
Thomas Falcon660e3092018-04-20 14:25:32 -05001340 tx_scrqs = adapter->num_active_tx_pools;
Nathan Fontenotb41b83e2017-05-03 14:04:56 -04001341
1342 /* Free any remaining skbs in the tx buffer pools */
1343 for (i = 0; i < tx_scrqs; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001344 netdev_dbg(adapter->netdev, "Cleaning tx_pool[%d]\n", i);
Thomas Falcone9e1e972018-03-16 20:00:30 -05001345 clean_one_tx_pool(adapter, &adapter->tx_pool[i]);
1346 clean_one_tx_pool(adapter, &adapter->tso_pool[i]);
Nathan Fontenotb41b83e2017-05-03 14:04:56 -04001347 }
1348}
1349
John Allen6095e592018-03-30 13:44:21 -05001350static void ibmvnic_disable_irqs(struct ibmvnic_adapter *adapter)
John Allenea5509f2017-03-17 17:13:43 -05001351{
John Allen6095e592018-03-30 13:44:21 -05001352 struct net_device *netdev = adapter->netdev;
John Allenea5509f2017-03-17 17:13:43 -05001353 int i;
1354
Nathan Fontenot46293b92017-05-03 14:05:02 -04001355 if (adapter->tx_scrq) {
1356 for (i = 0; i < adapter->req_tx_queues; i++)
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001357 if (adapter->tx_scrq[i]->irq) {
Thomas Falconf8738662018-03-07 17:51:45 -06001358 netdev_dbg(netdev,
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001359 "Disabling tx_scrq[%d] irq\n", i);
Thomas Falconf23e0642018-04-15 18:53:36 -05001360 disable_scrq_irq(adapter, adapter->tx_scrq[i]);
Nathan Fontenot46293b92017-05-03 14:05:02 -04001361 disable_irq(adapter->tx_scrq[i]->irq);
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001362 }
Nathan Fontenot46293b92017-05-03 14:05:02 -04001363 }
1364
Nathan Fontenot46293b92017-05-03 14:05:02 -04001365 if (adapter->rx_scrq) {
1366 for (i = 0; i < adapter->req_rx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001367 if (adapter->rx_scrq[i]->irq) {
Thomas Falconf8738662018-03-07 17:51:45 -06001368 netdev_dbg(netdev,
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001369 "Disabling rx_scrq[%d] irq\n", i);
Thomas Falconf23e0642018-04-15 18:53:36 -05001370 disable_scrq_irq(adapter, adapter->rx_scrq[i]);
Nathan Fontenot46293b92017-05-03 14:05:02 -04001371 disable_irq(adapter->rx_scrq[i]->irq);
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001372 }
Nathan Fontenot46293b92017-05-03 14:05:02 -04001373 }
1374 }
John Allen6095e592018-03-30 13:44:21 -05001375}
1376
1377static void ibmvnic_cleanup(struct net_device *netdev)
1378{
1379 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1380
1381 /* ensure that transmissions are stopped if called by do_reset */
Juliet Kim7ed5b312019-09-20 16:11:23 -04001382 if (test_bit(0, &adapter->resetting))
John Allen6095e592018-03-30 13:44:21 -05001383 netif_tx_disable(netdev);
1384 else
1385 netif_tx_stop_all_queues(netdev);
1386
1387 ibmvnic_napi_disable(adapter);
1388 ibmvnic_disable_irqs(adapter);
1389
Thomas Falcond0869c02018-02-13 18:23:43 -06001390 clean_rx_pools(adapter);
Thomas Falcon10f76212017-05-26 10:30:31 -04001391 clean_tx_pools(adapter);
Thomas Falcon01d9bd72018-03-07 17:51:46 -06001392}
1393
1394static int __ibmvnic_close(struct net_device *netdev)
1395{
1396 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1397 int rc = 0;
1398
1399 adapter->state = VNIC_CLOSING;
1400 rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_DN);
Nathan Fontenot90c80142017-05-03 14:04:32 -04001401 adapter->state = VNIC_CLOSED;
Sukadev Bhattiprolud4083d32021-02-10 17:41:43 -08001402 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001403}
1404
Nathan Fontenoted651a12017-05-03 14:04:38 -04001405static int ibmvnic_close(struct net_device *netdev)
1406{
1407 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1408 int rc;
1409
Lijun Pan0666ef72021-04-12 02:41:28 -05001410 netdev_dbg(netdev, "[S:%s FOP:%d FRR:%d] Closing\n",
1411 adapter_state_to_string(adapter->state),
1412 adapter->failover_pending,
Sukadev Bhattiprolu38bd5ce2020-12-04 18:22:35 -08001413 adapter->force_reset_recovery);
1414
Thomas Falcon5a18e1e2018-04-06 18:37:05 -05001415 /* If device failover is pending, just set device state and return.
1416 * Device operation will be handled by reset routine.
1417 */
1418 if (adapter->failover_pending) {
1419 adapter->state = VNIC_CLOSED;
1420 return 0;
1421 }
1422
Nathan Fontenoted651a12017-05-03 14:04:38 -04001423 rc = __ibmvnic_close(netdev);
Nathan Fontenot30f79622018-04-06 18:37:06 -05001424 ibmvnic_cleanup(netdev);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001425
1426 return rc;
1427}
1428
Thomas Falconad7775d2016-04-01 17:20:34 -05001429/**
1430 * build_hdr_data - creates L2/L3/L4 header data buffer
Lee Jones80708602021-01-15 20:09:03 +00001431 * @hdr_field: bitfield determining needed headers
1432 * @skb: socket buffer
1433 * @hdr_len: array of header lengths
1434 * @hdr_data: buffer to write the header to
Thomas Falconad7775d2016-04-01 17:20:34 -05001435 *
1436 * Reads hdr_field to determine which headers are needed by firmware.
1437 * Builds a buffer containing these headers. Saves individual header
1438 * lengths and total buffer length to be used to build descriptors.
1439 */
1440static int build_hdr_data(u8 hdr_field, struct sk_buff *skb,
1441 int *hdr_len, u8 *hdr_data)
1442{
1443 int len = 0;
1444 u8 *hdr;
1445
Thomas Falconda75e3b2018-03-12 11:51:02 -05001446 if (skb_vlan_tagged(skb) && !skb_vlan_tag_present(skb))
1447 hdr_len[0] = sizeof(struct vlan_ethhdr);
1448 else
1449 hdr_len[0] = sizeof(struct ethhdr);
Thomas Falconad7775d2016-04-01 17:20:34 -05001450
1451 if (skb->protocol == htons(ETH_P_IP)) {
1452 hdr_len[1] = ip_hdr(skb)->ihl * 4;
1453 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
1454 hdr_len[2] = tcp_hdrlen(skb);
1455 else if (ip_hdr(skb)->protocol == IPPROTO_UDP)
1456 hdr_len[2] = sizeof(struct udphdr);
1457 } else if (skb->protocol == htons(ETH_P_IPV6)) {
1458 hdr_len[1] = sizeof(struct ipv6hdr);
1459 if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
1460 hdr_len[2] = tcp_hdrlen(skb);
1461 else if (ipv6_hdr(skb)->nexthdr == IPPROTO_UDP)
1462 hdr_len[2] = sizeof(struct udphdr);
Thomas Falcon4eb50ce2017-12-18 12:52:40 -06001463 } else if (skb->protocol == htons(ETH_P_ARP)) {
1464 hdr_len[1] = arp_hdr_len(skb->dev);
1465 hdr_len[2] = 0;
Thomas Falconad7775d2016-04-01 17:20:34 -05001466 }
1467
1468 memset(hdr_data, 0, 120);
1469 if ((hdr_field >> 6) & 1) {
1470 hdr = skb_mac_header(skb);
1471 memcpy(hdr_data, hdr, hdr_len[0]);
1472 len += hdr_len[0];
1473 }
1474
1475 if ((hdr_field >> 5) & 1) {
1476 hdr = skb_network_header(skb);
1477 memcpy(hdr_data + len, hdr, hdr_len[1]);
1478 len += hdr_len[1];
1479 }
1480
1481 if ((hdr_field >> 4) & 1) {
1482 hdr = skb_transport_header(skb);
1483 memcpy(hdr_data + len, hdr, hdr_len[2]);
1484 len += hdr_len[2];
1485 }
1486 return len;
1487}
1488
1489/**
1490 * create_hdr_descs - create header and header extension descriptors
Lee Jones80708602021-01-15 20:09:03 +00001491 * @hdr_field: bitfield determining needed headers
1492 * @hdr_data: buffer containing header data
1493 * @len: length of data buffer
1494 * @hdr_len: array of individual header lengths
1495 * @scrq_arr: descriptor array
Thomas Falconad7775d2016-04-01 17:20:34 -05001496 *
1497 * Creates header and, if needed, header extension descriptors and
1498 * places them in a descriptor array, scrq_arr
1499 */
1500
Thomas Falcon2de09682017-10-16 10:02:11 -05001501static int create_hdr_descs(u8 hdr_field, u8 *hdr_data, int len, int *hdr_len,
1502 union sub_crq *scrq_arr)
Thomas Falconad7775d2016-04-01 17:20:34 -05001503{
1504 union sub_crq hdr_desc;
1505 int tmp_len = len;
Thomas Falcon2de09682017-10-16 10:02:11 -05001506 int num_descs = 0;
Thomas Falconad7775d2016-04-01 17:20:34 -05001507 u8 *data, *cur;
1508 int tmp;
1509
1510 while (tmp_len > 0) {
1511 cur = hdr_data + len - tmp_len;
1512
1513 memset(&hdr_desc, 0, sizeof(hdr_desc));
1514 if (cur != hdr_data) {
1515 data = hdr_desc.hdr_ext.data;
1516 tmp = tmp_len > 29 ? 29 : tmp_len;
1517 hdr_desc.hdr_ext.first = IBMVNIC_CRQ_CMD;
1518 hdr_desc.hdr_ext.type = IBMVNIC_HDR_EXT_DESC;
1519 hdr_desc.hdr_ext.len = tmp;
1520 } else {
1521 data = hdr_desc.hdr.data;
1522 tmp = tmp_len > 24 ? 24 : tmp_len;
1523 hdr_desc.hdr.first = IBMVNIC_CRQ_CMD;
1524 hdr_desc.hdr.type = IBMVNIC_HDR_DESC;
1525 hdr_desc.hdr.len = tmp;
1526 hdr_desc.hdr.l2_len = (u8)hdr_len[0];
1527 hdr_desc.hdr.l3_len = cpu_to_be16((u16)hdr_len[1]);
1528 hdr_desc.hdr.l4_len = (u8)hdr_len[2];
1529 hdr_desc.hdr.flag = hdr_field << 1;
1530 }
1531 memcpy(data, cur, tmp);
1532 tmp_len -= tmp;
1533 *scrq_arr = hdr_desc;
1534 scrq_arr++;
Thomas Falcon2de09682017-10-16 10:02:11 -05001535 num_descs++;
Thomas Falconad7775d2016-04-01 17:20:34 -05001536 }
Thomas Falcon2de09682017-10-16 10:02:11 -05001537
1538 return num_descs;
Thomas Falconad7775d2016-04-01 17:20:34 -05001539}
1540
1541/**
1542 * build_hdr_descs_arr - build a header descriptor array
Lee Jones80708602021-01-15 20:09:03 +00001543 * @txbuff: tx buffer
1544 * @num_entries: number of descriptors to be sent
1545 * @hdr_field: bit field determining which headers will be sent
Thomas Falconad7775d2016-04-01 17:20:34 -05001546 *
1547 * This function will build a TX descriptor array with applicable
1548 * L2/L3/L4 packet header descriptors to be sent by send_subcrq_indirect.
1549 */
1550
Thomas Falconc62aa372020-11-18 19:12:20 -06001551static void build_hdr_descs_arr(struct sk_buff *skb,
1552 union sub_crq *indir_arr,
Thomas Falconad7775d2016-04-01 17:20:34 -05001553 int *num_entries, u8 hdr_field)
1554{
1555 int hdr_len[3] = {0, 0, 0};
Thomas Falconc62aa372020-11-18 19:12:20 -06001556 u8 hdr_data[140] = {0};
Thomas Falcon2de09682017-10-16 10:02:11 -05001557 int tot_len;
Thomas Falconad7775d2016-04-01 17:20:34 -05001558
Thomas Falconc62aa372020-11-18 19:12:20 -06001559 tot_len = build_hdr_data(hdr_field, skb, hdr_len,
1560 hdr_data);
Thomas Falcon2de09682017-10-16 10:02:11 -05001561 *num_entries += create_hdr_descs(hdr_field, hdr_data, tot_len, hdr_len,
Thomas Falconc62aa372020-11-18 19:12:20 -06001562 indir_arr + 1);
Thomas Falconad7775d2016-04-01 17:20:34 -05001563}
1564
Thomas Falcon1f247a62018-03-12 11:51:04 -05001565static int ibmvnic_xmit_workarounds(struct sk_buff *skb,
1566 struct net_device *netdev)
1567{
1568 /* For some backing devices, mishandling of small packets
1569 * can result in a loss of connection or TX stall. Device
1570 * architects recommend that no packet should be smaller
1571 * than the minimum MTU value provided to the driver, so
1572 * pad any packets to that length
1573 */
1574 if (skb->len < netdev->min_mtu)
1575 return skb_put_padto(skb, netdev->min_mtu);
Thomas Falcon7083a452018-03-12 21:05:26 -05001576
1577 return 0;
Thomas Falcon1f247a62018-03-12 11:51:04 -05001578}
1579
Thomas Falcon0d973382020-11-18 19:12:19 -06001580static void ibmvnic_tx_scrq_clean_buffer(struct ibmvnic_adapter *adapter,
1581 struct ibmvnic_sub_crq_queue *tx_scrq)
1582{
1583 struct ibmvnic_ind_xmit_queue *ind_bufp;
1584 struct ibmvnic_tx_buff *tx_buff;
1585 struct ibmvnic_tx_pool *tx_pool;
1586 union sub_crq tx_scrq_entry;
1587 int queue_num;
1588 int entries;
1589 int index;
1590 int i;
1591
1592 ind_bufp = &tx_scrq->ind_buf;
1593 entries = (u64)ind_bufp->index;
1594 queue_num = tx_scrq->pool_index;
1595
1596 for (i = entries - 1; i >= 0; --i) {
1597 tx_scrq_entry = ind_bufp->indir_arr[i];
1598 if (tx_scrq_entry.v1.type != IBMVNIC_TX_DESC)
1599 continue;
1600 index = be32_to_cpu(tx_scrq_entry.v1.correlator);
1601 if (index & IBMVNIC_TSO_POOL_MASK) {
1602 tx_pool = &adapter->tso_pool[queue_num];
1603 index &= ~IBMVNIC_TSO_POOL_MASK;
1604 } else {
1605 tx_pool = &adapter->tx_pool[queue_num];
1606 }
1607 tx_pool->free_map[tx_pool->consumer_index] = index;
1608 tx_pool->consumer_index = tx_pool->consumer_index == 0 ?
1609 tx_pool->num_buffers - 1 :
1610 tx_pool->consumer_index - 1;
1611 tx_buff = &tx_pool->tx_buff[index];
1612 adapter->netdev->stats.tx_packets--;
1613 adapter->netdev->stats.tx_bytes -= tx_buff->skb->len;
1614 adapter->tx_stats_buffers[queue_num].packets--;
1615 adapter->tx_stats_buffers[queue_num].bytes -=
1616 tx_buff->skb->len;
1617 dev_kfree_skb_any(tx_buff->skb);
1618 tx_buff->skb = NULL;
1619 adapter->netdev->stats.tx_dropped++;
1620 }
1621 ind_bufp->index = 0;
1622 if (atomic_sub_return(entries, &tx_scrq->used) <=
1623 (adapter->req_tx_entries_per_subcrq / 2) &&
Sukadev Bhattiprolu65d64702021-06-23 21:13:12 -07001624 __netif_subqueue_stopped(adapter->netdev, queue_num) &&
1625 !test_bit(0, &adapter->resetting)) {
Thomas Falcon0d973382020-11-18 19:12:19 -06001626 netif_wake_subqueue(adapter->netdev, queue_num);
1627 netdev_dbg(adapter->netdev, "Started queue %d\n",
1628 queue_num);
1629 }
1630}
1631
1632static int ibmvnic_tx_scrq_flush(struct ibmvnic_adapter *adapter,
1633 struct ibmvnic_sub_crq_queue *tx_scrq)
1634{
1635 struct ibmvnic_ind_xmit_queue *ind_bufp;
1636 u64 dma_addr;
1637 u64 entries;
1638 u64 handle;
1639 int rc;
1640
1641 ind_bufp = &tx_scrq->ind_buf;
1642 dma_addr = (u64)ind_bufp->indir_dma;
1643 entries = (u64)ind_bufp->index;
1644 handle = tx_scrq->handle;
1645
1646 if (!entries)
1647 return 0;
1648 rc = send_subcrq_indirect(adapter, handle, dma_addr, entries);
1649 if (rc)
1650 ibmvnic_tx_scrq_clean_buffer(adapter, tx_scrq);
1651 else
1652 ind_bufp->index = 0;
1653 return 0;
1654}
1655
YueHaibing94b2bb22018-09-18 14:35:47 +08001656static netdev_tx_t ibmvnic_xmit(struct sk_buff *skb, struct net_device *netdev)
Thomas Falcon032c5e82015-12-21 11:26:06 -06001657{
1658 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1659 int queue_num = skb_get_queue_mapping(skb);
Thomas Falconad7775d2016-04-01 17:20:34 -05001660 u8 *hdrs = (u8 *)&adapter->tx_rx_desc_req;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001661 struct device *dev = &adapter->vdev->dev;
Thomas Falcon0d973382020-11-18 19:12:19 -06001662 struct ibmvnic_ind_xmit_queue *ind_bufp;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001663 struct ibmvnic_tx_buff *tx_buff = NULL;
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001664 struct ibmvnic_sub_crq_queue *tx_scrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001665 struct ibmvnic_tx_pool *tx_pool;
1666 unsigned int tx_send_failed = 0;
Thomas Falcon0d973382020-11-18 19:12:19 -06001667 netdev_tx_t ret = NETDEV_TX_OK;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001668 unsigned int tx_map_failed = 0;
Thomas Falconc62aa372020-11-18 19:12:20 -06001669 union sub_crq indir_arr[16];
Thomas Falcon032c5e82015-12-21 11:26:06 -06001670 unsigned int tx_dropped = 0;
1671 unsigned int tx_packets = 0;
1672 unsigned int tx_bytes = 0;
1673 dma_addr_t data_dma_addr;
1674 struct netdev_queue *txq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001675 unsigned long lpar_rc;
1676 union sub_crq tx_crq;
1677 unsigned int offset;
Thomas Falconad7775d2016-04-01 17:20:34 -05001678 int num_entries = 1;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001679 unsigned char *dst;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001680 int index = 0;
Thomas Falcona0dca102018-01-18 19:29:48 -06001681 u8 proto = 0;
Thomas Falcon0d973382020-11-18 19:12:19 -06001682
1683 tx_scrq = adapter->tx_scrq[queue_num];
1684 txq = netdev_get_tx_queue(netdev, queue_num);
1685 ind_bufp = &tx_scrq->ind_buf;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001686
Juliet Kim7ed5b312019-09-20 16:11:23 -04001687 if (test_bit(0, &adapter->resetting)) {
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001688 if (!netif_subqueue_stopped(netdev, skb))
1689 netif_stop_subqueue(netdev, queue_num);
1690 dev_kfree_skb_any(skb);
1691
Thomas Falcon032c5e82015-12-21 11:26:06 -06001692 tx_send_failed++;
1693 tx_dropped++;
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001694 ret = NETDEV_TX_OK;
Thomas Falcon0d973382020-11-18 19:12:19 -06001695 ibmvnic_tx_scrq_flush(adapter, tx_scrq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001696 goto out;
1697 }
1698
Thomas Falcon7083a452018-03-12 21:05:26 -05001699 if (ibmvnic_xmit_workarounds(skb, netdev)) {
Thomas Falcon1f247a62018-03-12 11:51:04 -05001700 tx_dropped++;
1701 tx_send_failed++;
1702 ret = NETDEV_TX_OK;
Thomas Falcon0d973382020-11-18 19:12:19 -06001703 ibmvnic_tx_scrq_flush(adapter, tx_scrq);
Thomas Falcon1f247a62018-03-12 11:51:04 -05001704 goto out;
1705 }
Thomas Falcon06b3e352018-03-16 20:00:28 -05001706 if (skb_is_gso(skb))
1707 tx_pool = &adapter->tso_pool[queue_num];
1708 else
1709 tx_pool = &adapter->tx_pool[queue_num];
Thomas Falcon1f247a62018-03-12 11:51:04 -05001710
Thomas Falcon032c5e82015-12-21 11:26:06 -06001711 index = tx_pool->free_map[tx_pool->consumer_index];
Thomas Falconfdb06102017-10-17 12:36:55 -05001712
Thomas Falcon86b61a52018-03-16 20:00:29 -05001713 if (index == IBMVNIC_INVALID_MAP) {
1714 dev_kfree_skb_any(skb);
1715 tx_send_failed++;
1716 tx_dropped++;
1717 ret = NETDEV_TX_OK;
1718 goto out;
1719 }
1720
1721 tx_pool->free_map[tx_pool->consumer_index] = IBMVNIC_INVALID_MAP;
1722
Thomas Falcon06b3e352018-03-16 20:00:28 -05001723 offset = index * tx_pool->buf_size;
1724 dst = tx_pool->long_term_buff.buff + offset;
1725 memset(dst, 0, tx_pool->buf_size);
1726 data_dma_addr = tx_pool->long_term_buff.addr + offset;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001727
Thomas Falcon15482052017-10-17 12:36:54 -05001728 if (skb_shinfo(skb)->nr_frags) {
1729 int cur, i;
1730
1731 /* Copy the head */
1732 skb_copy_from_linear_data(skb, dst, skb_headlen(skb));
1733 cur = skb_headlen(skb);
1734
1735 /* Copy the frags */
1736 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1737 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1738
Christophe JAILLETc3105f82021-04-04 10:54:37 +02001739 memcpy(dst + cur, skb_frag_address(frag),
1740 skb_frag_size(frag));
Thomas Falcon15482052017-10-17 12:36:54 -05001741 cur += skb_frag_size(frag);
1742 }
1743 } else {
1744 skb_copy_from_linear_data(skb, dst, skb->len);
1745 }
1746
Lijun Pan42557da2021-02-12 20:48:40 -06001747 /* post changes to long_term_buff *dst before VIOS accessing it */
1748 dma_wmb();
1749
Thomas Falcon032c5e82015-12-21 11:26:06 -06001750 tx_pool->consumer_index =
Thomas Falcon06b3e352018-03-16 20:00:28 -05001751 (tx_pool->consumer_index + 1) % tx_pool->num_buffers;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001752
1753 tx_buff = &tx_pool->tx_buff[index];
1754 tx_buff->skb = skb;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001755 tx_buff->index = index;
1756 tx_buff->pool_index = queue_num;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001757
1758 memset(&tx_crq, 0, sizeof(tx_crq));
1759 tx_crq.v1.first = IBMVNIC_CRQ_CMD;
1760 tx_crq.v1.type = IBMVNIC_TX_DESC;
1761 tx_crq.v1.n_crq_elem = 1;
1762 tx_crq.v1.n_sge = 1;
1763 tx_crq.v1.flags1 = IBMVNIC_TX_COMP_NEEDED;
Thomas Falcon06b3e352018-03-16 20:00:28 -05001764
Thomas Falconfdb06102017-10-17 12:36:55 -05001765 if (skb_is_gso(skb))
Thomas Falcon06b3e352018-03-16 20:00:28 -05001766 tx_crq.v1.correlator =
1767 cpu_to_be32(index | IBMVNIC_TSO_POOL_MASK);
Thomas Falconfdb06102017-10-17 12:36:55 -05001768 else
Thomas Falcon06b3e352018-03-16 20:00:28 -05001769 tx_crq.v1.correlator = cpu_to_be32(index);
1770 tx_crq.v1.dma_reg = cpu_to_be16(tx_pool->long_term_buff.map_id);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001771 tx_crq.v1.sge_len = cpu_to_be32(skb->len);
1772 tx_crq.v1.ioba = cpu_to_be64(data_dma_addr);
1773
Michał Mirosławe84b4792018-11-07 17:50:52 +01001774 if (adapter->vlan_header_insertion && skb_vlan_tag_present(skb)) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06001775 tx_crq.v1.flags2 |= IBMVNIC_TX_VLAN_INSERT;
1776 tx_crq.v1.vlan_id = cpu_to_be16(skb->vlan_tci);
1777 }
1778
1779 if (skb->protocol == htons(ETH_P_IP)) {
Thomas Falcona0dca102018-01-18 19:29:48 -06001780 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV4;
1781 proto = ip_hdr(skb)->protocol;
1782 } else if (skb->protocol == htons(ETH_P_IPV6)) {
1783 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV6;
1784 proto = ipv6_hdr(skb)->nexthdr;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001785 }
1786
Thomas Falcona0dca102018-01-18 19:29:48 -06001787 if (proto == IPPROTO_TCP)
1788 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_TCP;
1789 else if (proto == IPPROTO_UDP)
1790 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_UDP;
1791
Thomas Falconad7775d2016-04-01 17:20:34 -05001792 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06001793 tx_crq.v1.flags1 |= IBMVNIC_TX_CHKSUM_OFFLOAD;
Thomas Falconad7775d2016-04-01 17:20:34 -05001794 hdrs += 2;
1795 }
Thomas Falconfdb06102017-10-17 12:36:55 -05001796 if (skb_is_gso(skb)) {
1797 tx_crq.v1.flags1 |= IBMVNIC_TX_LSO;
1798 tx_crq.v1.mss = cpu_to_be16(skb_shinfo(skb)->gso_size);
1799 hdrs += 2;
1800 }
Thomas Falcon0d973382020-11-18 19:12:19 -06001801
1802 if ((*hdrs >> 7) & 1)
Thomas Falconc62aa372020-11-18 19:12:20 -06001803 build_hdr_descs_arr(skb, indir_arr, &num_entries, *hdrs);
Thomas Falcon0d973382020-11-18 19:12:19 -06001804
1805 tx_crq.v1.n_crq_elem = num_entries;
1806 tx_buff->num_entries = num_entries;
1807 /* flush buffer if current entry can not fit */
1808 if (num_entries + ind_bufp->index > IBMVNIC_MAX_IND_DESCS) {
1809 lpar_rc = ibmvnic_tx_scrq_flush(adapter, tx_scrq);
1810 if (lpar_rc != H_SUCCESS)
1811 goto tx_flush_err;
Thomas Falconad7775d2016-04-01 17:20:34 -05001812 }
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001813
Thomas Falconc62aa372020-11-18 19:12:20 -06001814 indir_arr[0] = tx_crq;
1815 memcpy(&ind_bufp->indir_arr[ind_bufp->index], &indir_arr[0],
Thomas Falcon0d973382020-11-18 19:12:19 -06001816 num_entries * sizeof(struct ibmvnic_generic_scrq));
1817 ind_bufp->index += num_entries;
1818 if (__netdev_tx_sent_queue(txq, skb->len,
1819 netdev_xmit_more() &&
1820 ind_bufp->index < IBMVNIC_MAX_IND_DESCS)) {
1821 lpar_rc = ibmvnic_tx_scrq_flush(adapter, tx_scrq);
1822 if (lpar_rc != H_SUCCESS)
1823 goto tx_err;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001824 }
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001825
Thomas Falconffc385b2018-02-18 10:08:41 -06001826 if (atomic_add_return(num_entries, &tx_scrq->used)
Brian King58c8c0c2017-04-19 13:44:47 -04001827 >= adapter->req_tx_entries_per_subcrq) {
Thomas Falcon0aecb132018-02-26 18:10:58 -06001828 netdev_dbg(netdev, "Stopping queue %d\n", queue_num);
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001829 netif_stop_subqueue(netdev, queue_num);
1830 }
1831
Thomas Falcon032c5e82015-12-21 11:26:06 -06001832 tx_packets++;
1833 tx_bytes += skb->len;
1834 txq->trans_start = jiffies;
1835 ret = NETDEV_TX_OK;
Thomas Falcon86b61a52018-03-16 20:00:29 -05001836 goto out;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001837
Thomas Falcon0d973382020-11-18 19:12:19 -06001838tx_flush_err:
1839 dev_kfree_skb_any(skb);
1840 tx_buff->skb = NULL;
1841 tx_pool->consumer_index = tx_pool->consumer_index == 0 ?
1842 tx_pool->num_buffers - 1 :
1843 tx_pool->consumer_index - 1;
1844 tx_dropped++;
1845tx_err:
1846 if (lpar_rc != H_CLOSED && lpar_rc != H_PARAMETER)
1847 dev_err_ratelimited(dev, "tx: send failed\n");
1848
1849 if (lpar_rc == H_CLOSED || adapter->failover_pending) {
1850 /* Disable TX and report carrier off if queue is closed
1851 * or pending failover.
1852 * Firmware guarantees that a signal will be sent to the
1853 * driver, triggering a reset or some other action.
1854 */
1855 netif_tx_stop_all_queues(netdev);
1856 netif_carrier_off(netdev);
1857 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001858out:
1859 netdev->stats.tx_dropped += tx_dropped;
1860 netdev->stats.tx_bytes += tx_bytes;
1861 netdev->stats.tx_packets += tx_packets;
1862 adapter->tx_send_failed += tx_send_failed;
1863 adapter->tx_map_failed += tx_map_failed;
John Allen3d52b592017-08-02 16:44:14 -05001864 adapter->tx_stats_buffers[queue_num].packets += tx_packets;
1865 adapter->tx_stats_buffers[queue_num].bytes += tx_bytes;
1866 adapter->tx_stats_buffers[queue_num].dropped_packets += tx_dropped;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001867
1868 return ret;
1869}
1870
1871static void ibmvnic_set_multi(struct net_device *netdev)
1872{
1873 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1874 struct netdev_hw_addr *ha;
1875 union ibmvnic_crq crq;
1876
1877 memset(&crq, 0, sizeof(crq));
1878 crq.request_capability.first = IBMVNIC_CRQ_CMD;
1879 crq.request_capability.cmd = REQUEST_CAPABILITY;
1880
1881 if (netdev->flags & IFF_PROMISC) {
1882 if (!adapter->promisc_supported)
1883 return;
1884 } else {
1885 if (netdev->flags & IFF_ALLMULTI) {
1886 /* Accept all multicast */
1887 memset(&crq, 0, sizeof(crq));
1888 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1889 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1890 crq.multicast_ctrl.flags = IBMVNIC_ENABLE_ALL;
1891 ibmvnic_send_crq(adapter, &crq);
1892 } else if (netdev_mc_empty(netdev)) {
1893 /* Reject 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_DISABLE_ALL;
1898 ibmvnic_send_crq(adapter, &crq);
1899 } else {
1900 /* Accept one or more multicast(s) */
1901 netdev_for_each_mc_addr(ha, netdev) {
1902 memset(&crq, 0, sizeof(crq));
1903 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1904 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1905 crq.multicast_ctrl.flags = IBMVNIC_ENABLE_MC;
1906 ether_addr_copy(&crq.multicast_ctrl.mac_addr[0],
1907 ha->addr);
1908 ibmvnic_send_crq(adapter, &crq);
1909 }
1910 }
1911 }
1912}
1913
Thomas Falcon62740e92019-05-09 23:13:43 -05001914static int __ibmvnic_set_mac(struct net_device *netdev, u8 *dev_addr)
Thomas Falcon032c5e82015-12-21 11:26:06 -06001915{
1916 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001917 union ibmvnic_crq crq;
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05001918 int rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001919
Thomas Falcon62740e92019-05-09 23:13:43 -05001920 if (!is_valid_ether_addr(dev_addr)) {
1921 rc = -EADDRNOTAVAIL;
1922 goto err;
1923 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001924
1925 memset(&crq, 0, sizeof(crq));
1926 crq.change_mac_addr.first = IBMVNIC_CRQ_CMD;
1927 crq.change_mac_addr.cmd = CHANGE_MAC_ADDR;
Thomas Falcon62740e92019-05-09 23:13:43 -05001928 ether_addr_copy(&crq.change_mac_addr.mac_addr[0], dev_addr);
Thomas Falconf8136142018-01-29 13:45:05 -06001929
Thomas Falconff25dcb2019-11-25 17:12:56 -06001930 mutex_lock(&adapter->fw_lock);
1931 adapter->fw_done_rc = 0;
Thomas Falcon070eca92019-11-25 17:12:53 -06001932 reinit_completion(&adapter->fw_done);
Thomas Falconff25dcb2019-11-25 17:12:56 -06001933
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05001934 rc = ibmvnic_send_crq(adapter, &crq);
Thomas Falcon62740e92019-05-09 23:13:43 -05001935 if (rc) {
1936 rc = -EIO;
Thomas Falconff25dcb2019-11-25 17:12:56 -06001937 mutex_unlock(&adapter->fw_lock);
Thomas Falcon62740e92019-05-09 23:13:43 -05001938 goto err;
1939 }
1940
Thomas Falcon476d96c2019-11-25 17:12:55 -06001941 rc = ibmvnic_wait_for_completion(adapter, &adapter->fw_done, 10000);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001942 /* netdev->dev_addr is changed in handle_change_mac_rsp function */
Thomas Falcon476d96c2019-11-25 17:12:55 -06001943 if (rc || adapter->fw_done_rc) {
Thomas Falcon62740e92019-05-09 23:13:43 -05001944 rc = -EIO;
Thomas Falconff25dcb2019-11-25 17:12:56 -06001945 mutex_unlock(&adapter->fw_lock);
Thomas Falcon62740e92019-05-09 23:13:43 -05001946 goto err;
1947 }
Thomas Falconff25dcb2019-11-25 17:12:56 -06001948 mutex_unlock(&adapter->fw_lock);
Thomas Falcon62740e92019-05-09 23:13:43 -05001949 return 0;
1950err:
1951 ether_addr_copy(adapter->mac_addr, netdev->dev_addr);
1952 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001953}
1954
John Allenc26eba02017-10-26 16:23:25 -05001955static int ibmvnic_set_mac(struct net_device *netdev, void *p)
1956{
1957 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1958 struct sockaddr *addr = p;
Thomas Falconf8136142018-01-29 13:45:05 -06001959 int rc;
John Allenc26eba02017-10-26 16:23:25 -05001960
Thomas Falcon62740e92019-05-09 23:13:43 -05001961 rc = 0;
Lijun Pan8fc36722020-10-27 17:04:56 -05001962 if (!is_valid_ether_addr(addr->sa_data))
1963 return -EADDRNOTAVAIL;
1964
Jiri Wiesner67eb2112021-03-04 17:18:28 +01001965 ether_addr_copy(adapter->mac_addr, addr->sa_data);
1966 if (adapter->state != VNIC_PROBED)
Thomas Falcon62740e92019-05-09 23:13:43 -05001967 rc = __ibmvnic_set_mac(netdev, addr->sa_data);
John Allenc26eba02017-10-26 16:23:25 -05001968
Thomas Falconf8136142018-01-29 13:45:05 -06001969 return rc;
John Allenc26eba02017-10-26 16:23:25 -05001970}
1971
Lijun Pancaee7bf2021-04-12 02:41:27 -05001972static const char *reset_reason_to_string(enum ibmvnic_reset_reason reason)
1973{
1974 switch (reason) {
1975 case VNIC_RESET_FAILOVER:
1976 return "FAILOVER";
1977 case VNIC_RESET_MOBILITY:
1978 return "MOBILITY";
1979 case VNIC_RESET_FATAL:
1980 return "FATAL";
1981 case VNIC_RESET_NON_FATAL:
1982 return "NON_FATAL";
1983 case VNIC_RESET_TIMEOUT:
1984 return "TIMEOUT";
1985 case VNIC_RESET_CHANGE_PARAM:
1986 return "CHANGE_PARAM";
1987 default:
1988 return "UNKNOWN";
1989 }
1990}
1991
Lee Jones80708602021-01-15 20:09:03 +00001992/*
Nathan Fontenoted651a12017-05-03 14:04:38 -04001993 * do_reset returns zero if we are able to keep processing reset events, or
1994 * non-zero if we hit a fatal error and must halt.
1995 */
1996static int do_reset(struct ibmvnic_adapter *adapter,
1997 struct ibmvnic_rwi *rwi, u32 reset_state)
1998{
John Allen896d8692018-01-18 16:26:31 -06001999 u64 old_num_rx_queues, old_num_tx_queues;
Thomas Falcon5bf032e2018-11-21 11:17:59 -06002000 u64 old_num_rx_slots, old_num_tx_slots;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002001 struct net_device *netdev = adapter->netdev;
Lijun Pand3a6abc2021-04-14 02:46:15 -05002002 int rc;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002003
Sukadev Bhattiprolu38bd5ce2020-12-04 18:22:35 -08002004 netdev_dbg(adapter->netdev,
Lijun Pan0666ef72021-04-12 02:41:28 -05002005 "[S:%s FOP:%d] Reset reason: %s, reset_state: %s\n",
2006 adapter_state_to_string(adapter->state),
2007 adapter->failover_pending,
2008 reset_reason_to_string(rwi->reset_reason),
2009 adapter_state_to_string(reset_state));
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002010
Lijun Pan3f5ec372021-01-06 15:35:14 -06002011 adapter->reset_reason = rwi->reset_reason;
2012 /* requestor of VNIC_RESET_CHANGE_PARAM already has the rtnl lock */
2013 if (!(adapter->reset_reason == VNIC_RESET_CHANGE_PARAM))
2014 rtnl_lock();
2015
Lijun Panbab08be2021-02-11 00:43:19 -06002016 /* Now that we have the rtnl lock, clear any pending failover.
Sukadev Bhattiprolu1d850492020-10-30 10:07:11 -07002017 * This will ensure ibmvnic_open() has either completed or will
2018 * block until failover is complete.
2019 */
2020 if (rwi->reset_reason == VNIC_RESET_FAILOVER)
2021 adapter->failover_pending = false;
Juliet Kimb27507b2019-09-20 16:11:22 -04002022
Sukadev Bhattiprolu8f1c0fd2021-02-23 21:02:29 -08002023 /* read the state and check (again) after getting rtnl */
2024 reset_state = adapter->state;
2025
2026 if (reset_state == VNIC_REMOVING || reset_state == VNIC_REMOVED) {
2027 rc = -EBUSY;
2028 goto out;
2029 }
2030
Nathan Fontenoted651a12017-05-03 14:04:38 -04002031 netif_carrier_off(netdev);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002032
John Allen896d8692018-01-18 16:26:31 -06002033 old_num_rx_queues = adapter->req_rx_queues;
2034 old_num_tx_queues = adapter->req_tx_queues;
Thomas Falcon5bf032e2018-11-21 11:17:59 -06002035 old_num_rx_slots = adapter->req_rx_add_entries_per_subcrq;
2036 old_num_tx_slots = adapter->req_tx_entries_per_subcrq;
John Allen896d8692018-01-18 16:26:31 -06002037
Nathan Fontenot30f79622018-04-06 18:37:06 -05002038 ibmvnic_cleanup(netdev);
2039
Thomas Falcon1f946082019-06-07 16:03:53 -05002040 if (reset_state == VNIC_OPEN &&
2041 adapter->reset_reason != VNIC_RESET_MOBILITY &&
Nathan Fontenot30f79622018-04-06 18:37:06 -05002042 adapter->reset_reason != VNIC_RESET_FAILOVER) {
Lijun Pan3f5ec372021-01-06 15:35:14 -06002043 if (adapter->reset_reason == VNIC_RESET_CHANGE_PARAM) {
2044 rc = __ibmvnic_close(netdev);
2045 if (rc)
2046 goto out;
2047 } else {
2048 adapter->state = VNIC_CLOSING;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002049
Lijun Pan3f5ec372021-01-06 15:35:14 -06002050 /* Release the RTNL lock before link state change and
2051 * re-acquire after the link state change to allow
2052 * linkwatch_event to grab the RTNL lock and run during
2053 * a reset.
2054 */
2055 rtnl_unlock();
2056 rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_DN);
2057 rtnl_lock();
2058 if (rc)
2059 goto out;
Juliet Kimb27507b2019-09-20 16:11:22 -04002060
Sukadev Bhattiprolu8f1c0fd2021-02-23 21:02:29 -08002061 if (adapter->state == VNIC_OPEN) {
2062 /* When we dropped rtnl, ibmvnic_open() got
2063 * it and noticed that we are resetting and
2064 * set the adapter state to OPEN. Update our
2065 * new "target" state, and resume the reset
2066 * from VNIC_CLOSING state.
2067 */
2068 netdev_dbg(netdev,
Lijun Pan0666ef72021-04-12 02:41:28 -05002069 "Open changed state from %s, updating.\n",
2070 adapter_state_to_string(reset_state));
Sukadev Bhattiprolu8f1c0fd2021-02-23 21:02:29 -08002071 reset_state = VNIC_OPEN;
2072 adapter->state = VNIC_CLOSING;
2073 }
2074
Lijun Pan3f5ec372021-01-06 15:35:14 -06002075 if (adapter->state != VNIC_CLOSING) {
Sukadev Bhattiprolu8f1c0fd2021-02-23 21:02:29 -08002076 /* If someone else changed the adapter state
2077 * when we dropped the rtnl, fail the reset
2078 */
Lijun Pan3f5ec372021-01-06 15:35:14 -06002079 rc = -1;
2080 goto out;
2081 }
Lijun Pan3f5ec372021-01-06 15:35:14 -06002082 adapter->state = VNIC_CLOSED;
Juliet Kimb27507b2019-09-20 16:11:22 -04002083 }
Lijun Pan3f5ec372021-01-06 15:35:14 -06002084 }
Juliet Kimb27507b2019-09-20 16:11:22 -04002085
Lijun Pan3f5ec372021-01-06 15:35:14 -06002086 if (adapter->reset_reason == VNIC_RESET_CHANGE_PARAM) {
2087 release_resources(adapter);
2088 release_sub_crqs(adapter, 1);
2089 release_crq_queue(adapter);
John Allenc26eba02017-10-26 16:23:25 -05002090 }
2091
John Allen8cb31cf2017-05-26 10:30:37 -04002092 if (adapter->reset_reason != VNIC_RESET_NON_FATAL) {
2093 /* remove the closed state so when we call open it appears
2094 * we are coming from the probed state.
2095 */
Nathan Fontenoted651a12017-05-03 14:04:38 -04002096 adapter->state = VNIC_PROBED;
John Allen8cb31cf2017-05-26 10:30:37 -04002097
Lijun Pan3f5ec372021-01-06 15:35:14 -06002098 if (adapter->reset_reason == VNIC_RESET_CHANGE_PARAM) {
2099 rc = init_crq_queue(adapter);
2100 } else if (adapter->reset_reason == VNIC_RESET_MOBILITY) {
Nathan Fontenot30f79622018-04-06 18:37:06 -05002101 rc = ibmvnic_reenable_crq_queue(adapter);
2102 release_sub_crqs(adapter, 1);
2103 } else {
2104 rc = ibmvnic_reset_crq(adapter);
Dany Madden8b40eb732020-06-18 15:24:13 -04002105 if (rc == H_CLOSED || rc == H_SUCCESS) {
Nathan Fontenot30f79622018-04-06 18:37:06 -05002106 rc = vio_enable_interrupts(adapter->vdev);
Dany Madden8b40eb732020-06-18 15:24:13 -04002107 if (rc)
2108 netdev_err(adapter->netdev,
2109 "Reset failed to enable interrupts. rc=%d\n",
2110 rc);
2111 }
Nathan Fontenot30f79622018-04-06 18:37:06 -05002112 }
2113
2114 if (rc) {
2115 netdev_err(adapter->netdev,
Dany Madden8b40eb732020-06-18 15:24:13 -04002116 "Reset couldn't initialize crq. rc=%d\n", rc);
Juliet Kimb27507b2019-09-20 16:11:22 -04002117 goto out;
Nathan Fontenot30f79622018-04-06 18:37:06 -05002118 }
2119
Lijun Pan635e4422020-08-19 17:52:26 -05002120 rc = ibmvnic_reset_init(adapter, true);
Juliet Kimb27507b2019-09-20 16:11:22 -04002121 if (rc) {
2122 rc = IBMVNIC_INIT_FAILED;
2123 goto out;
2124 }
John Allen8cb31cf2017-05-26 10:30:37 -04002125
2126 /* If the adapter was in PROBE state prior to the reset,
2127 * exit here.
2128 */
Juliet Kimb27507b2019-09-20 16:11:22 -04002129 if (reset_state == VNIC_PROBED) {
2130 rc = 0;
2131 goto out;
2132 }
John Allen8cb31cf2017-05-26 10:30:37 -04002133
2134 rc = ibmvnic_login(netdev);
Lijun Panf78afaa2021-02-11 00:43:20 -06002135 if (rc)
Juliet Kimb27507b2019-09-20 16:11:22 -04002136 goto out;
John Allen8cb31cf2017-05-26 10:30:37 -04002137
Lijun Pan3f5ec372021-01-06 15:35:14 -06002138 if (adapter->reset_reason == VNIC_RESET_CHANGE_PARAM) {
2139 rc = init_resources(adapter);
2140 if (rc)
2141 goto out;
2142 } else if (adapter->req_rx_queues != old_num_rx_queues ||
Juliet Kimb27507b2019-09-20 16:11:22 -04002143 adapter->req_tx_queues != old_num_tx_queues ||
2144 adapter->req_rx_add_entries_per_subcrq !=
2145 old_num_rx_slots ||
2146 adapter->req_tx_entries_per_subcrq !=
Mingming Cao9f134572020-08-25 13:26:41 -04002147 old_num_tx_slots ||
2148 !adapter->rx_pool ||
2149 !adapter->tso_pool ||
2150 !adapter->tx_pool) {
John Allen896d8692018-01-18 16:26:31 -06002151 release_rx_pools(adapter);
2152 release_tx_pools(adapter);
Juliet Kima5681e22018-11-19 15:59:22 -06002153 release_napi(adapter);
2154 release_vpd_data(adapter);
2155
2156 rc = init_resources(adapter);
Thomas Falconf611a5b2018-08-30 13:19:53 -05002157 if (rc)
Juliet Kimb27507b2019-09-20 16:11:22 -04002158 goto out;
Nathan Fontenotd9043c12018-02-19 13:30:14 -06002159
John Allenc26eba02017-10-26 16:23:25 -05002160 } else {
2161 rc = reset_tx_pools(adapter);
Jakub Kicinski8ae4dff2020-09-04 21:07:49 -07002162 if (rc) {
Mingming Cao9f134572020-08-25 13:26:41 -04002163 netdev_dbg(adapter->netdev, "reset tx pools failed (%d)\n",
Lijun Pan91dc5d22021-02-11 00:43:22 -06002164 rc);
Juliet Kimb27507b2019-09-20 16:11:22 -04002165 goto out;
Jakub Kicinski8ae4dff2020-09-04 21:07:49 -07002166 }
Nathan Fontenot8c0543a2017-05-26 10:31:06 -04002167
John Allenc26eba02017-10-26 16:23:25 -05002168 rc = reset_rx_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 rx 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 }
John Allenc26eba02017-10-26 16:23:25 -05002174 }
Thomas Falcon134bbe72018-05-16 15:49:04 -05002175 ibmvnic_disable_irqs(adapter);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002176 }
John Allene676d812018-03-14 10:41:29 -05002177 adapter->state = VNIC_CLOSED;
2178
Juliet Kimb27507b2019-09-20 16:11:22 -04002179 if (reset_state == VNIC_CLOSED) {
2180 rc = 0;
2181 goto out;
2182 }
John Allene676d812018-03-14 10:41:29 -05002183
Nathan Fontenoted651a12017-05-03 14:04:38 -04002184 rc = __ibmvnic_open(netdev);
2185 if (rc) {
Juliet Kimb27507b2019-09-20 16:11:22 -04002186 rc = IBMVNIC_OPEN_FAILED;
2187 goto out;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002188 }
2189
Thomas Falconbe32a242019-06-07 16:03:54 -05002190 /* refresh device's multicast list */
2191 ibmvnic_set_multi(netdev);
2192
Lijun Pan98025bc2020-11-20 16:40:12 -06002193 if (adapter->reset_reason == VNIC_RESET_FAILOVER ||
Lijun Pan6be46662020-12-14 15:19:29 -06002194 adapter->reset_reason == VNIC_RESET_MOBILITY)
2195 __netdev_notify_peers(netdev);
Nathan Fontenot61d3e1d2017-06-12 20:47:45 -04002196
Juliet Kimb27507b2019-09-20 16:11:22 -04002197 rc = 0;
2198
2199out:
Dany Madden0cb4bc62020-11-25 18:04:27 -06002200 /* restore the adapter state if reset failed */
2201 if (rc)
2202 adapter->state = reset_state;
Lijun Pan3f5ec372021-01-06 15:35:14 -06002203 /* requestor of VNIC_RESET_CHANGE_PARAM should still hold the rtnl lock */
2204 if (!(adapter->reset_reason == VNIC_RESET_CHANGE_PARAM))
2205 rtnl_unlock();
Juliet Kimb27507b2019-09-20 16:11:22 -04002206
Lijun Pan0666ef72021-04-12 02:41:28 -05002207 netdev_dbg(adapter->netdev, "[S:%s FOP:%d] Reset done, rc %d\n",
2208 adapter_state_to_string(adapter->state),
2209 adapter->failover_pending, rc);
Juliet Kimb27507b2019-09-20 16:11:22 -04002210 return rc;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002211}
2212
Thomas Falcon2770a792018-05-23 13:38:02 -05002213static int do_hard_reset(struct ibmvnic_adapter *adapter,
2214 struct ibmvnic_rwi *rwi, u32 reset_state)
2215{
2216 struct net_device *netdev = adapter->netdev;
2217 int rc;
2218
Lijun Pancaee7bf2021-04-12 02:41:27 -05002219 netdev_dbg(adapter->netdev, "Hard resetting driver (%s)\n",
2220 reset_reason_to_string(rwi->reset_reason));
Thomas Falcon2770a792018-05-23 13:38:02 -05002221
Sukadev Bhattiprolu8f1c0fd2021-02-23 21:02:29 -08002222 /* read the state and check (again) after getting rtnl */
2223 reset_state = adapter->state;
2224
2225 if (reset_state == VNIC_REMOVING || reset_state == VNIC_REMOVED) {
2226 rc = -EBUSY;
2227 goto out;
2228 }
2229
Thomas Falcon2770a792018-05-23 13:38:02 -05002230 netif_carrier_off(netdev);
2231 adapter->reset_reason = rwi->reset_reason;
2232
2233 ibmvnic_cleanup(netdev);
2234 release_resources(adapter);
2235 release_sub_crqs(adapter, 0);
2236 release_crq_queue(adapter);
2237
2238 /* remove the closed state so when we call open it appears
2239 * we are coming from the probed state.
2240 */
2241 adapter->state = VNIC_PROBED;
2242
Thomas Falconbbd669a2019-04-04 18:58:26 -05002243 reinit_completion(&adapter->init_done);
Thomas Falcon2770a792018-05-23 13:38:02 -05002244 rc = init_crq_queue(adapter);
2245 if (rc) {
2246 netdev_err(adapter->netdev,
2247 "Couldn't initialize crq. rc=%d\n", rc);
Dany Madden0cb4bc62020-11-25 18:04:27 -06002248 goto out;
Thomas Falcon2770a792018-05-23 13:38:02 -05002249 }
2250
Lijun Pan635e4422020-08-19 17:52:26 -05002251 rc = ibmvnic_reset_init(adapter, false);
Thomas Falcon2770a792018-05-23 13:38:02 -05002252 if (rc)
Dany Madden0cb4bc62020-11-25 18:04:27 -06002253 goto out;
Thomas Falcon2770a792018-05-23 13:38:02 -05002254
2255 /* If the adapter was in PROBE state prior to the reset,
2256 * exit here.
2257 */
2258 if (reset_state == VNIC_PROBED)
Dany Madden0cb4bc62020-11-25 18:04:27 -06002259 goto out;
Thomas Falcon2770a792018-05-23 13:38:02 -05002260
2261 rc = ibmvnic_login(netdev);
Dany Madden0cb4bc62020-11-25 18:04:27 -06002262 if (rc)
2263 goto out;
Juliet Kima5681e22018-11-19 15:59:22 -06002264
2265 rc = init_resources(adapter);
Thomas Falcon2770a792018-05-23 13:38:02 -05002266 if (rc)
Dany Madden0cb4bc62020-11-25 18:04:27 -06002267 goto out;
Thomas Falcon2770a792018-05-23 13:38:02 -05002268
2269 ibmvnic_disable_irqs(adapter);
2270 adapter->state = VNIC_CLOSED;
2271
2272 if (reset_state == VNIC_CLOSED)
Dany Madden0cb4bc62020-11-25 18:04:27 -06002273 goto out;
Thomas Falcon2770a792018-05-23 13:38:02 -05002274
2275 rc = __ibmvnic_open(netdev);
Dany Madden0cb4bc62020-11-25 18:04:27 -06002276 if (rc) {
2277 rc = IBMVNIC_OPEN_FAILED;
2278 goto out;
2279 }
Thomas Falcon2770a792018-05-23 13:38:02 -05002280
Lijun Pan6be46662020-12-14 15:19:29 -06002281 __netdev_notify_peers(netdev);
Dany Madden0cb4bc62020-11-25 18:04:27 -06002282out:
2283 /* restore adapter state if reset failed */
2284 if (rc)
2285 adapter->state = reset_state;
Lijun Pan0666ef72021-04-12 02:41:28 -05002286 netdev_dbg(adapter->netdev, "[S:%s FOP:%d] Hard reset done, rc %d\n",
2287 adapter_state_to_string(adapter->state),
2288 adapter->failover_pending, rc);
Dany Madden0cb4bc62020-11-25 18:04:27 -06002289 return rc;
Thomas Falcon2770a792018-05-23 13:38:02 -05002290}
2291
Nathan Fontenoted651a12017-05-03 14:04:38 -04002292static struct ibmvnic_rwi *get_next_rwi(struct ibmvnic_adapter *adapter)
2293{
2294 struct ibmvnic_rwi *rwi;
Thomas Falcon6c5c7482018-12-10 15:22:22 -06002295 unsigned long flags;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002296
Thomas Falcon6c5c7482018-12-10 15:22:22 -06002297 spin_lock_irqsave(&adapter->rwi_lock, flags);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002298
2299 if (!list_empty(&adapter->rwi_list)) {
2300 rwi = list_first_entry(&adapter->rwi_list, struct ibmvnic_rwi,
2301 list);
2302 list_del(&rwi->list);
2303 } else {
2304 rwi = NULL;
2305 }
2306
Thomas Falcon6c5c7482018-12-10 15:22:22 -06002307 spin_unlock_irqrestore(&adapter->rwi_lock, flags);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002308 return rwi;
2309}
2310
Nathan Fontenoted651a12017-05-03 14:04:38 -04002311static void __ibmvnic_reset(struct work_struct *work)
2312{
2313 struct ibmvnic_rwi *rwi;
2314 struct ibmvnic_adapter *adapter;
Juliet Kim7d7195a2020-03-10 09:23:58 -05002315 bool saved_state = false;
2316 unsigned long flags;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002317 u32 reset_state;
John Allenc26eba02017-10-26 16:23:25 -05002318 int rc = 0;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002319
2320 adapter = container_of(work, struct ibmvnic_adapter, ibmvnic_reset);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002321
Juliet Kim7ed5b312019-09-20 16:11:23 -04002322 if (test_and_set_bit_lock(0, &adapter->resetting)) {
Lijun Pan870e04a2021-04-13 14:33:39 -05002323 queue_delayed_work(system_long_wq,
2324 &adapter->ibmvnic_delayed_reset,
2325 IBMVNIC_RESET_DELAY);
Juliet Kim7ed5b312019-09-20 16:11:23 -04002326 return;
2327 }
2328
Nathan Fontenoted651a12017-05-03 14:04:38 -04002329 rwi = get_next_rwi(adapter);
2330 while (rwi) {
Juliet Kim7d7195a2020-03-10 09:23:58 -05002331 spin_lock_irqsave(&adapter->state_lock, flags);
2332
Thomas Falcon36f10312019-08-27 11:10:04 -05002333 if (adapter->state == VNIC_REMOVING ||
Michal Suchanekc8dc5592019-09-09 22:44:51 +02002334 adapter->state == VNIC_REMOVED) {
Juliet Kim7d7195a2020-03-10 09:23:58 -05002335 spin_unlock_irqrestore(&adapter->state_lock, flags);
Juliet Kim1c2977c2019-09-05 17:30:01 -04002336 kfree(rwi);
2337 rc = EBUSY;
2338 break;
2339 }
Thomas Falcon36f10312019-08-27 11:10:04 -05002340
Juliet Kim7d7195a2020-03-10 09:23:58 -05002341 if (!saved_state) {
2342 reset_state = adapter->state;
Juliet Kim7d7195a2020-03-10 09:23:58 -05002343 saved_state = true;
2344 }
2345 spin_unlock_irqrestore(&adapter->state_lock, flags);
2346
Lijun Pan3f5ec372021-01-06 15:35:14 -06002347 if (adapter->force_reset_recovery) {
Lijun Panbab08be2021-02-11 00:43:19 -06002348 /* Since we are doing a hard reset now, clear the
Sukadev Bhattiprolu1d850492020-10-30 10:07:11 -07002349 * failover_pending flag so we don't ignore any
2350 * future MOBILITY or other resets.
2351 */
2352 adapter->failover_pending = false;
2353
Juliet Kimb27507b2019-09-20 16:11:22 -04002354 /* Transport event occurred during previous reset */
2355 if (adapter->wait_for_reset) {
2356 /* Previous was CHANGE_PARAM; caller locked */
2357 adapter->force_reset_recovery = false;
2358 rc = do_hard_reset(adapter, rwi, reset_state);
2359 } else {
2360 rtnl_lock();
2361 adapter->force_reset_recovery = false;
2362 rc = do_hard_reset(adapter, rwi, reset_state);
2363 rtnl_unlock();
2364 }
Sukadev Bhattiproluf15fde92020-11-25 18:04:28 -06002365 if (rc) {
2366 /* give backing device time to settle down */
2367 netdev_dbg(adapter->netdev,
Lijun Pan0666ef72021-04-12 02:41:28 -05002368 "[S:%s] Hard reset failed, waiting 60 secs\n",
2369 adapter_state_to_string(adapter->state));
Sukadev Bhattiproluf15fde92020-11-25 18:04:28 -06002370 set_current_state(TASK_UNINTERRUPTIBLE);
2371 schedule_timeout(60 * HZ);
2372 }
Lijun Pan1f45dc22020-12-23 14:49:04 -06002373 } else {
Thomas Falcon2770a792018-05-23 13:38:02 -05002374 rc = do_reset(adapter, rwi, reset_state);
2375 }
Nathan Fontenoted651a12017-05-03 14:04:38 -04002376 kfree(rwi);
Dany Maddena86d5c62020-11-25 18:04:31 -06002377 adapter->last_reset_time = jiffies;
Dany Madden0cb4bc62020-11-25 18:04:27 -06002378
Dany Madden18f141b2020-11-25 18:04:25 -06002379 if (rc)
2380 netdev_dbg(adapter->netdev, "Reset failed, rc=%d\n", rc);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002381
2382 rwi = get_next_rwi(adapter);
Juliet Kim7ed5b312019-09-20 16:11:23 -04002383
2384 if (rwi && (rwi->reset_reason == VNIC_RESET_FAILOVER ||
2385 rwi->reset_reason == VNIC_RESET_MOBILITY))
2386 adapter->force_reset_recovery = true;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002387 }
2388
John Allenc26eba02017-10-26 16:23:25 -05002389 if (adapter->wait_for_reset) {
John Allenc26eba02017-10-26 16:23:25 -05002390 adapter->reset_done_rc = rc;
2391 complete(&adapter->reset_done);
2392 }
2393
Juliet Kim7ed5b312019-09-20 16:11:23 -04002394 clear_bit_unlock(0, &adapter->resetting);
Sukadev Bhattiprolu38bd5ce2020-12-04 18:22:35 -08002395
2396 netdev_dbg(adapter->netdev,
Lijun Pan0666ef72021-04-12 02:41:28 -05002397 "[S:%s FRR:%d WFR:%d] Done processing resets\n",
2398 adapter_state_to_string(adapter->state),
2399 adapter->force_reset_recovery,
Sukadev Bhattiprolu38bd5ce2020-12-04 18:22:35 -08002400 adapter->wait_for_reset);
Juliet Kim7ed5b312019-09-20 16:11:23 -04002401}
2402
2403static void __ibmvnic_delayed_reset(struct work_struct *work)
2404{
2405 struct ibmvnic_adapter *adapter;
2406
2407 adapter = container_of(work, struct ibmvnic_adapter,
2408 ibmvnic_delayed_reset.work);
2409 __ibmvnic_reset(&adapter->ibmvnic_reset);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002410}
2411
Thomas Falconaf894d22018-04-06 18:37:04 -05002412static int ibmvnic_reset(struct ibmvnic_adapter *adapter,
2413 enum ibmvnic_reset_reason reason)
Nathan Fontenoted651a12017-05-03 14:04:38 -04002414{
Thomas Falcon2770a792018-05-23 13:38:02 -05002415 struct list_head *entry, *tmp_entry;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002416 struct ibmvnic_rwi *rwi, *tmp;
2417 struct net_device *netdev = adapter->netdev;
Thomas Falcon6c5c7482018-12-10 15:22:22 -06002418 unsigned long flags;
Thomas Falconaf894d22018-04-06 18:37:04 -05002419 int ret;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002420
Jakub Kicinskib646acd52021-02-16 22:58:44 -08002421 spin_lock_irqsave(&adapter->rwi_lock, flags);
2422
2423 /* If failover is pending don't schedule any other reset.
Sukadev Bhattiprolu1d850492020-10-30 10:07:11 -07002424 * Instead let the failover complete. If there is already a
2425 * a failover reset scheduled, we will detect and drop the
2426 * duplicate reset when walking the ->rwi_list below.
2427 */
Nathan Fontenoted651a12017-05-03 14:04:38 -04002428 if (adapter->state == VNIC_REMOVING ||
Thomas Falcon5a18e1e2018-04-06 18:37:05 -05002429 adapter->state == VNIC_REMOVED ||
Sukadev Bhattiprolu1d850492020-10-30 10:07:11 -07002430 (adapter->failover_pending && reason != VNIC_RESET_FAILOVER)) {
Thomas Falconaf894d22018-04-06 18:37:04 -05002431 ret = EBUSY;
Thomas Falcon5a18e1e2018-04-06 18:37:05 -05002432 netdev_dbg(netdev, "Adapter removing or pending failover, skipping reset\n");
Thomas Falconaf894d22018-04-06 18:37:04 -05002433 goto err;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002434 }
2435
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04002436 if (adapter->state == VNIC_PROBING) {
2437 netdev_warn(netdev, "Adapter reset during probe\n");
Lijun Pan91dc5d22021-02-11 00:43:22 -06002438 adapter->init_done_rc = EAGAIN;
2439 ret = EAGAIN;
Thomas Falconaf894d22018-04-06 18:37:04 -05002440 goto err;
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04002441 }
2442
Nathan Fontenoted651a12017-05-03 14:04:38 -04002443 list_for_each(entry, &adapter->rwi_list) {
2444 tmp = list_entry(entry, struct ibmvnic_rwi, list);
2445 if (tmp->reset_reason == reason) {
Lijun Pancaee7bf2021-04-12 02:41:27 -05002446 netdev_dbg(netdev, "Skipping matching reset, reason=%s\n",
2447 reset_reason_to_string(reason));
Thomas Falconaf894d22018-04-06 18:37:04 -05002448 ret = EBUSY;
2449 goto err;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002450 }
2451 }
2452
Thomas Falcon1d1bbc32018-12-10 15:22:23 -06002453 rwi = kzalloc(sizeof(*rwi), GFP_ATOMIC);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002454 if (!rwi) {
Thomas Falconaf894d22018-04-06 18:37:04 -05002455 ret = ENOMEM;
2456 goto err;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002457 }
Thomas Falcon2770a792018-05-23 13:38:02 -05002458 /* if we just received a transport event,
2459 * flush reset queue and process this reset
2460 */
2461 if (adapter->force_reset_recovery && !list_empty(&adapter->rwi_list)) {
2462 list_for_each_safe(entry, tmp_entry, &adapter->rwi_list)
2463 list_del(entry);
2464 }
Nathan Fontenoted651a12017-05-03 14:04:38 -04002465 rwi->reset_reason = reason;
2466 list_add_tail(&rwi->list, &adapter->rwi_list);
Lijun Pancaee7bf2021-04-12 02:41:27 -05002467 netdev_dbg(adapter->netdev, "Scheduling reset (reason %s)\n",
2468 reset_reason_to_string(reason));
Lijun Pan870e04a2021-04-13 14:33:39 -05002469 queue_work(system_long_wq, &adapter->ibmvnic_reset);
Thomas Falconaf894d22018-04-06 18:37:04 -05002470
Sukadev Bhattiprolu4a41c422021-02-12 20:42:50 -08002471 ret = 0;
Thomas Falconaf894d22018-04-06 18:37:04 -05002472err:
Sukadev Bhattiprolu4a41c422021-02-12 20:42:50 -08002473 /* ibmvnic_close() below can block, so drop the lock first */
2474 spin_unlock_irqrestore(&adapter->rwi_lock, flags);
2475
2476 if (ret == ENOMEM)
2477 ibmvnic_close(netdev);
2478
Thomas Falconaf894d22018-04-06 18:37:04 -05002479 return -ret;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002480}
2481
Michael S. Tsirkin0290bd22019-12-10 09:23:51 -05002482static void ibmvnic_tx_timeout(struct net_device *dev, unsigned int txqueue)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002483{
2484 struct ibmvnic_adapter *adapter = netdev_priv(dev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002485
Lijun Pan855a6312020-11-20 16:40:13 -06002486 if (test_bit(0, &adapter->resetting)) {
2487 netdev_err(adapter->netdev,
2488 "Adapter is resetting, skip timeout reset\n");
2489 return;
2490 }
Dany Maddena86d5c62020-11-25 18:04:31 -06002491 /* No queuing up reset until at least 5 seconds (default watchdog val)
2492 * after last reset
2493 */
2494 if (time_before(jiffies, (adapter->last_reset_time + dev->watchdog_timeo))) {
2495 netdev_dbg(dev, "Not yet time to tx timeout.\n");
2496 return;
2497 }
Nathan Fontenoted651a12017-05-03 14:04:38 -04002498 ibmvnic_reset(adapter, VNIC_RESET_TIMEOUT);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002499}
2500
2501static void remove_buff_from_pool(struct ibmvnic_adapter *adapter,
2502 struct ibmvnic_rx_buff *rx_buff)
2503{
2504 struct ibmvnic_rx_pool *pool = &adapter->rx_pool[rx_buff->pool_index];
2505
2506 rx_buff->skb = NULL;
2507
2508 pool->free_map[pool->next_alloc] = (int)(rx_buff - pool->rx_buff);
2509 pool->next_alloc = (pool->next_alloc + 1) % pool->size;
2510
2511 atomic_dec(&pool->available);
2512}
2513
2514static int ibmvnic_poll(struct napi_struct *napi, int budget)
2515{
Dwip N. Banerjeeec20f362020-11-18 19:12:23 -06002516 struct ibmvnic_sub_crq_queue *rx_scrq;
2517 struct ibmvnic_adapter *adapter;
2518 struct net_device *netdev;
2519 int frames_processed;
2520 int scrq_num;
2521
2522 netdev = napi->dev;
2523 adapter = netdev_priv(netdev);
2524 scrq_num = (int)(napi - adapter->napi);
2525 frames_processed = 0;
2526 rx_scrq = adapter->rx_scrq[scrq_num];
Nathan Fontenot152ce472017-05-26 10:30:54 -04002527
Thomas Falcon032c5e82015-12-21 11:26:06 -06002528restart_poll:
2529 while (frames_processed < budget) {
2530 struct sk_buff *skb;
2531 struct ibmvnic_rx_buff *rx_buff;
2532 union sub_crq *next;
2533 u32 length;
2534 u16 offset;
2535 u8 flags = 0;
2536
Juliet Kim7ed5b312019-09-20 16:11:23 -04002537 if (unlikely(test_bit(0, &adapter->resetting) &&
John Allen34686562018-02-06 16:21:49 -06002538 adapter->reset_reason != VNIC_RESET_NON_FATAL)) {
Dwip N. Banerjeeec20f362020-11-18 19:12:23 -06002539 enable_scrq_irq(adapter, rx_scrq);
Thomas Falcon21ecba62017-06-14 23:50:09 -05002540 napi_complete_done(napi, frames_processed);
2541 return frames_processed;
2542 }
2543
Dwip N. Banerjeeec20f362020-11-18 19:12:23 -06002544 if (!pending_scrq(adapter, rx_scrq))
Thomas Falcon032c5e82015-12-21 11:26:06 -06002545 break;
Dwip N. Banerjeeec20f362020-11-18 19:12:23 -06002546 next = ibmvnic_next_scrq(adapter, rx_scrq);
Lijun Pan914789a2021-02-11 00:43:21 -06002547 rx_buff = (struct ibmvnic_rx_buff *)
2548 be64_to_cpu(next->rx_comp.correlator);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002549 /* do error checking */
2550 if (next->rx_comp.rc) {
John Allene1cea2e2017-08-07 15:42:30 -05002551 netdev_dbg(netdev, "rx buffer returned with rc %x\n",
2552 be16_to_cpu(next->rx_comp.rc));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002553 /* free the entry */
2554 next->rx_comp.first = 0;
Thomas Falcon4b9b0f02018-02-13 18:23:42 -06002555 dev_kfree_skb_any(rx_buff->skb);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002556 remove_buff_from_pool(adapter, rx_buff);
Nathan Fontenotca05e312017-05-03 14:05:14 -04002557 continue;
Thomas Falconabe27a82018-02-19 20:12:57 -06002558 } else if (!rx_buff->skb) {
2559 /* free the entry */
2560 next->rx_comp.first = 0;
2561 remove_buff_from_pool(adapter, rx_buff);
2562 continue;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002563 }
2564
2565 length = be32_to_cpu(next->rx_comp.len);
2566 offset = be16_to_cpu(next->rx_comp.off_frame_data);
2567 flags = next->rx_comp.flags;
2568 skb = rx_buff->skb;
Lijun Pan42557da2021-02-12 20:48:40 -06002569 /* load long_term_buff before copying to skb */
2570 dma_rmb();
Thomas Falcon032c5e82015-12-21 11:26:06 -06002571 skb_copy_to_linear_data(skb, rx_buff->data + offset,
2572 length);
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04002573
2574 /* VLAN Header has been stripped by the system firmware and
2575 * needs to be inserted by the driver
2576 */
2577 if (adapter->rx_vlan_header_insertion &&
2578 (flags & IBMVNIC_VLAN_STRIPPED))
2579 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
2580 ntohs(next->rx_comp.vlan_tci));
2581
Thomas Falcon032c5e82015-12-21 11:26:06 -06002582 /* free the entry */
2583 next->rx_comp.first = 0;
2584 remove_buff_from_pool(adapter, rx_buff);
2585
2586 skb_put(skb, length);
2587 skb->protocol = eth_type_trans(skb, netdev);
Thomas Falcon94ca3052017-05-03 14:05:20 -04002588 skb_record_rx_queue(skb, scrq_num);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002589
2590 if (flags & IBMVNIC_IP_CHKSUM_GOOD &&
2591 flags & IBMVNIC_TCP_UDP_CHKSUM_GOOD) {
2592 skb->ip_summed = CHECKSUM_UNNECESSARY;
2593 }
2594
2595 length = skb->len;
2596 napi_gro_receive(napi, skb); /* send it up */
2597 netdev->stats.rx_packets++;
2598 netdev->stats.rx_bytes += length;
John Allen3d52b592017-08-02 16:44:14 -05002599 adapter->rx_stats_buffers[scrq_num].packets++;
2600 adapter->rx_stats_buffers[scrq_num].bytes += length;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002601 frames_processed++;
2602 }
Nathan Fontenot152ce472017-05-26 10:30:54 -04002603
Dwip N. Banerjee41ed0a02020-11-18 19:12:25 -06002604 if (adapter->state != VNIC_CLOSING &&
2605 ((atomic_read(&adapter->rx_pool[scrq_num].available) <
2606 adapter->req_rx_add_entries_per_subcrq / 2) ||
2607 frames_processed < budget))
Nathan Fontenot152ce472017-05-26 10:30:54 -04002608 replenish_rx_pool(adapter, &adapter->rx_pool[scrq_num]);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002609 if (frames_processed < budget) {
Dwip N. Banerjeeec20f362020-11-18 19:12:23 -06002610 if (napi_complete_done(napi, frames_processed)) {
2611 enable_scrq_irq(adapter, rx_scrq);
2612 if (pending_scrq(adapter, rx_scrq)) {
Dwip N. Banerjeeec20f362020-11-18 19:12:23 -06002613 if (napi_reschedule(napi)) {
2614 disable_scrq_irq(adapter, rx_scrq);
2615 goto restart_poll;
2616 }
2617 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002618 }
2619 }
2620 return frames_processed;
2621}
2622
John Allenc26eba02017-10-26 16:23:25 -05002623static int wait_for_reset(struct ibmvnic_adapter *adapter)
2624{
Thomas Falconaf894d22018-04-06 18:37:04 -05002625 int rc, ret;
2626
John Allenc26eba02017-10-26 16:23:25 -05002627 adapter->fallback.mtu = adapter->req_mtu;
2628 adapter->fallback.rx_queues = adapter->req_rx_queues;
2629 adapter->fallback.tx_queues = adapter->req_tx_queues;
2630 adapter->fallback.rx_entries = adapter->req_rx_add_entries_per_subcrq;
2631 adapter->fallback.tx_entries = adapter->req_tx_entries_per_subcrq;
2632
Thomas Falcon070eca92019-11-25 17:12:53 -06002633 reinit_completion(&adapter->reset_done);
John Allenc26eba02017-10-26 16:23:25 -05002634 adapter->wait_for_reset = true;
Thomas Falconaf894d22018-04-06 18:37:04 -05002635 rc = ibmvnic_reset(adapter, VNIC_RESET_CHANGE_PARAM);
Thomas Falcon476d96c2019-11-25 17:12:55 -06002636
2637 if (rc) {
2638 ret = rc;
2639 goto out;
2640 }
2641 rc = ibmvnic_wait_for_completion(adapter, &adapter->reset_done, 60000);
2642 if (rc) {
2643 ret = -ENODEV;
2644 goto out;
2645 }
John Allenc26eba02017-10-26 16:23:25 -05002646
Thomas Falconaf894d22018-04-06 18:37:04 -05002647 ret = 0;
John Allenc26eba02017-10-26 16:23:25 -05002648 if (adapter->reset_done_rc) {
Thomas Falconaf894d22018-04-06 18:37:04 -05002649 ret = -EIO;
John Allenc26eba02017-10-26 16:23:25 -05002650 adapter->desired.mtu = adapter->fallback.mtu;
2651 adapter->desired.rx_queues = adapter->fallback.rx_queues;
2652 adapter->desired.tx_queues = adapter->fallback.tx_queues;
2653 adapter->desired.rx_entries = adapter->fallback.rx_entries;
2654 adapter->desired.tx_entries = adapter->fallback.tx_entries;
2655
Thomas Falcon070eca92019-11-25 17:12:53 -06002656 reinit_completion(&adapter->reset_done);
Thomas Falconaf894d22018-04-06 18:37:04 -05002657 adapter->wait_for_reset = true;
2658 rc = ibmvnic_reset(adapter, VNIC_RESET_CHANGE_PARAM);
Thomas Falcon476d96c2019-11-25 17:12:55 -06002659 if (rc) {
2660 ret = rc;
2661 goto out;
2662 }
2663 rc = ibmvnic_wait_for_completion(adapter, &adapter->reset_done,
2664 60000);
2665 if (rc) {
2666 ret = -ENODEV;
2667 goto out;
2668 }
John Allenc26eba02017-10-26 16:23:25 -05002669 }
Thomas Falcon476d96c2019-11-25 17:12:55 -06002670out:
John Allenc26eba02017-10-26 16:23:25 -05002671 adapter->wait_for_reset = false;
2672
Thomas Falconaf894d22018-04-06 18:37:04 -05002673 return ret;
John Allenc26eba02017-10-26 16:23:25 -05002674}
2675
John Allen3a807b72017-06-06 16:55:52 -05002676static int ibmvnic_change_mtu(struct net_device *netdev, int new_mtu)
2677{
John Allenc26eba02017-10-26 16:23:25 -05002678 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2679
2680 adapter->desired.mtu = new_mtu + ETH_HLEN;
2681
2682 return wait_for_reset(adapter);
John Allen3a807b72017-06-06 16:55:52 -05002683}
2684
Thomas Falconf10b09e2018-03-12 11:51:05 -05002685static netdev_features_t ibmvnic_features_check(struct sk_buff *skb,
2686 struct net_device *dev,
2687 netdev_features_t features)
2688{
2689 /* Some backing hardware adapters can not
2690 * handle packets with a MSS less than 224
2691 * or with only one segment.
2692 */
2693 if (skb_is_gso(skb)) {
2694 if (skb_shinfo(skb)->gso_size < 224 ||
2695 skb_shinfo(skb)->gso_segs == 1)
2696 features &= ~NETIF_F_GSO_MASK;
2697 }
2698
2699 return features;
2700}
2701
Thomas Falcon032c5e82015-12-21 11:26:06 -06002702static const struct net_device_ops ibmvnic_netdev_ops = {
2703 .ndo_open = ibmvnic_open,
2704 .ndo_stop = ibmvnic_close,
2705 .ndo_start_xmit = ibmvnic_xmit,
2706 .ndo_set_rx_mode = ibmvnic_set_multi,
2707 .ndo_set_mac_address = ibmvnic_set_mac,
2708 .ndo_validate_addr = eth_validate_addr,
Thomas Falcon032c5e82015-12-21 11:26:06 -06002709 .ndo_tx_timeout = ibmvnic_tx_timeout,
John Allen3a807b72017-06-06 16:55:52 -05002710 .ndo_change_mtu = ibmvnic_change_mtu,
Thomas Falconf10b09e2018-03-12 11:51:05 -05002711 .ndo_features_check = ibmvnic_features_check,
Thomas Falcon032c5e82015-12-21 11:26:06 -06002712};
2713
2714/* ethtool functions */
2715
Philippe Reynes8a433792017-01-07 22:37:29 +01002716static int ibmvnic_get_link_ksettings(struct net_device *netdev,
2717 struct ethtool_link_ksettings *cmd)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002718{
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03002719 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2720 int rc;
Philippe Reynes8a433792017-01-07 22:37:29 +01002721
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03002722 rc = send_query_phys_parms(adapter);
2723 if (rc) {
2724 adapter->speed = SPEED_UNKNOWN;
2725 adapter->duplex = DUPLEX_UNKNOWN;
2726 }
2727 cmd->base.speed = adapter->speed;
2728 cmd->base.duplex = adapter->duplex;
Philippe Reynes8a433792017-01-07 22:37:29 +01002729 cmd->base.port = PORT_FIBRE;
2730 cmd->base.phy_address = 0;
2731 cmd->base.autoneg = AUTONEG_ENABLE;
2732
Thomas Falcon032c5e82015-12-21 11:26:06 -06002733 return 0;
2734}
2735
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02002736static void ibmvnic_get_drvinfo(struct net_device *netdev,
Thomas Falcon032c5e82015-12-21 11:26:06 -06002737 struct ethtool_drvinfo *info)
2738{
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02002739 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2740
Lijun Pan8a96c802021-02-11 00:43:25 -06002741 strscpy(info->driver, ibmvnic_driver_name, sizeof(info->driver));
2742 strscpy(info->version, IBMVNIC_DRIVER_VERSION, sizeof(info->version));
2743 strscpy(info->fw_version, adapter->fw_version,
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02002744 sizeof(info->fw_version));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002745}
2746
2747static u32 ibmvnic_get_msglevel(struct net_device *netdev)
2748{
2749 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2750
2751 return adapter->msg_enable;
2752}
2753
2754static void ibmvnic_set_msglevel(struct net_device *netdev, u32 data)
2755{
2756 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2757
2758 adapter->msg_enable = data;
2759}
2760
2761static u32 ibmvnic_get_link(struct net_device *netdev)
2762{
2763 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2764
2765 /* Don't need to send a query because we request a logical link up at
2766 * init and then we wait for link state indications
2767 */
2768 return adapter->logical_link_state;
2769}
2770
2771static void ibmvnic_get_ringparam(struct net_device *netdev,
2772 struct ethtool_ringparam *ring)
2773{
John Allenbc131b32017-08-02 16:46:30 -05002774 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2775
Thomas Falcon723ad912018-09-28 18:38:26 -05002776 if (adapter->priv_flags & IBMVNIC_USE_SERVER_MAXES) {
2777 ring->rx_max_pending = adapter->max_rx_add_entries_per_subcrq;
2778 ring->tx_max_pending = adapter->max_tx_entries_per_subcrq;
2779 } else {
2780 ring->rx_max_pending = IBMVNIC_MAX_QUEUE_SZ;
2781 ring->tx_max_pending = IBMVNIC_MAX_QUEUE_SZ;
2782 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002783 ring->rx_mini_max_pending = 0;
2784 ring->rx_jumbo_max_pending = 0;
John Allenbc131b32017-08-02 16:46:30 -05002785 ring->rx_pending = adapter->req_rx_add_entries_per_subcrq;
2786 ring->tx_pending = adapter->req_tx_entries_per_subcrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002787 ring->rx_mini_pending = 0;
2788 ring->rx_jumbo_pending = 0;
2789}
2790
John Allenc26eba02017-10-26 16:23:25 -05002791static int ibmvnic_set_ringparam(struct net_device *netdev,
2792 struct ethtool_ringparam *ring)
2793{
2794 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Thomas Falcon723ad912018-09-28 18:38:26 -05002795 int ret;
John Allenc26eba02017-10-26 16:23:25 -05002796
Thomas Falcon723ad912018-09-28 18:38:26 -05002797 ret = 0;
John Allenc26eba02017-10-26 16:23:25 -05002798 adapter->desired.rx_entries = ring->rx_pending;
2799 adapter->desired.tx_entries = ring->tx_pending;
2800
Thomas Falcon723ad912018-09-28 18:38:26 -05002801 ret = wait_for_reset(adapter);
2802
2803 if (!ret &&
2804 (adapter->req_rx_add_entries_per_subcrq != ring->rx_pending ||
2805 adapter->req_tx_entries_per_subcrq != ring->tx_pending))
2806 netdev_info(netdev,
2807 "Could not match full ringsize request. Requested: RX %d, TX %d; Allowed: RX %llu, TX %llu\n",
2808 ring->rx_pending, ring->tx_pending,
2809 adapter->req_rx_add_entries_per_subcrq,
2810 adapter->req_tx_entries_per_subcrq);
2811 return ret;
John Allenc26eba02017-10-26 16:23:25 -05002812}
2813
John Allenc2dbeb62017-08-02 16:47:17 -05002814static void ibmvnic_get_channels(struct net_device *netdev,
2815 struct ethtool_channels *channels)
2816{
2817 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2818
Thomas Falcon723ad912018-09-28 18:38:26 -05002819 if (adapter->priv_flags & IBMVNIC_USE_SERVER_MAXES) {
2820 channels->max_rx = adapter->max_rx_queues;
2821 channels->max_tx = adapter->max_tx_queues;
2822 } else {
2823 channels->max_rx = IBMVNIC_MAX_QUEUES;
2824 channels->max_tx = IBMVNIC_MAX_QUEUES;
2825 }
2826
John Allenc2dbeb62017-08-02 16:47:17 -05002827 channels->max_other = 0;
2828 channels->max_combined = 0;
2829 channels->rx_count = adapter->req_rx_queues;
2830 channels->tx_count = adapter->req_tx_queues;
2831 channels->other_count = 0;
2832 channels->combined_count = 0;
2833}
2834
John Allenc26eba02017-10-26 16:23:25 -05002835static int ibmvnic_set_channels(struct net_device *netdev,
2836 struct ethtool_channels *channels)
2837{
2838 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Thomas Falcon723ad912018-09-28 18:38:26 -05002839 int ret;
John Allenc26eba02017-10-26 16:23:25 -05002840
Thomas Falcon723ad912018-09-28 18:38:26 -05002841 ret = 0;
John Allenc26eba02017-10-26 16:23:25 -05002842 adapter->desired.rx_queues = channels->rx_count;
2843 adapter->desired.tx_queues = channels->tx_count;
2844
Thomas Falcon723ad912018-09-28 18:38:26 -05002845 ret = wait_for_reset(adapter);
2846
2847 if (!ret &&
2848 (adapter->req_rx_queues != channels->rx_count ||
2849 adapter->req_tx_queues != channels->tx_count))
2850 netdev_info(netdev,
2851 "Could not match full channels request. Requested: RX %d, TX %d; Allowed: RX %llu, TX %llu\n",
2852 channels->rx_count, channels->tx_count,
2853 adapter->req_rx_queues, adapter->req_tx_queues);
2854 return ret;
John Allenc26eba02017-10-26 16:23:25 -05002855}
2856
Thomas Falcon032c5e82015-12-21 11:26:06 -06002857static void ibmvnic_get_strings(struct net_device *dev, u32 stringset, u8 *data)
2858{
John Allen3d52b592017-08-02 16:44:14 -05002859 struct ibmvnic_adapter *adapter = netdev_priv(dev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002860 int i;
2861
Thomas Falcon723ad912018-09-28 18:38:26 -05002862 switch (stringset) {
2863 case ETH_SS_STATS:
2864 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats);
2865 i++, data += ETH_GSTRING_LEN)
2866 memcpy(data, ibmvnic_stats[i].name, ETH_GSTRING_LEN);
2867
2868 for (i = 0; i < adapter->req_tx_queues; i++) {
2869 snprintf(data, ETH_GSTRING_LEN, "tx%d_packets", i);
2870 data += ETH_GSTRING_LEN;
2871
2872 snprintf(data, ETH_GSTRING_LEN, "tx%d_bytes", i);
2873 data += ETH_GSTRING_LEN;
2874
2875 snprintf(data, ETH_GSTRING_LEN,
2876 "tx%d_dropped_packets", i);
2877 data += ETH_GSTRING_LEN;
2878 }
2879
2880 for (i = 0; i < adapter->req_rx_queues; i++) {
2881 snprintf(data, ETH_GSTRING_LEN, "rx%d_packets", i);
2882 data += ETH_GSTRING_LEN;
2883
2884 snprintf(data, ETH_GSTRING_LEN, "rx%d_bytes", i);
2885 data += ETH_GSTRING_LEN;
2886
2887 snprintf(data, ETH_GSTRING_LEN, "rx%d_interrupts", i);
2888 data += ETH_GSTRING_LEN;
2889 }
2890 break;
2891
2892 case ETH_SS_PRIV_FLAGS:
2893 for (i = 0; i < ARRAY_SIZE(ibmvnic_priv_flags); i++)
2894 strcpy(data + i * ETH_GSTRING_LEN,
2895 ibmvnic_priv_flags[i]);
2896 break;
2897 default:
Thomas Falcon032c5e82015-12-21 11:26:06 -06002898 return;
John Allen3d52b592017-08-02 16:44:14 -05002899 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002900}
2901
2902static int ibmvnic_get_sset_count(struct net_device *dev, int sset)
2903{
John Allen3d52b592017-08-02 16:44:14 -05002904 struct ibmvnic_adapter *adapter = netdev_priv(dev);
2905
Thomas Falcon032c5e82015-12-21 11:26:06 -06002906 switch (sset) {
2907 case ETH_SS_STATS:
John Allen3d52b592017-08-02 16:44:14 -05002908 return ARRAY_SIZE(ibmvnic_stats) +
2909 adapter->req_tx_queues * NUM_TX_STATS +
2910 adapter->req_rx_queues * NUM_RX_STATS;
Thomas Falcon723ad912018-09-28 18:38:26 -05002911 case ETH_SS_PRIV_FLAGS:
2912 return ARRAY_SIZE(ibmvnic_priv_flags);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002913 default:
2914 return -EOPNOTSUPP;
2915 }
2916}
2917
2918static void ibmvnic_get_ethtool_stats(struct net_device *dev,
2919 struct ethtool_stats *stats, u64 *data)
2920{
2921 struct ibmvnic_adapter *adapter = netdev_priv(dev);
2922 union ibmvnic_crq crq;
John Allen3d52b592017-08-02 16:44:14 -05002923 int i, j;
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05002924 int rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002925
2926 memset(&crq, 0, sizeof(crq));
2927 crq.request_statistics.first = IBMVNIC_CRQ_CMD;
2928 crq.request_statistics.cmd = REQUEST_STATISTICS;
2929 crq.request_statistics.ioba = cpu_to_be32(adapter->stats_token);
2930 crq.request_statistics.len =
2931 cpu_to_be32(sizeof(struct ibmvnic_statistics));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002932
2933 /* Wait for data to be written */
Thomas Falcon070eca92019-11-25 17:12:53 -06002934 reinit_completion(&adapter->stats_done);
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05002935 rc = ibmvnic_send_crq(adapter, &crq);
2936 if (rc)
2937 return;
Thomas Falcon476d96c2019-11-25 17:12:55 -06002938 rc = ibmvnic_wait_for_completion(adapter, &adapter->stats_done, 10000);
2939 if (rc)
2940 return;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002941
2942 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++)
Lijun Pan91dc5d22021-02-11 00:43:22 -06002943 data[i] = be64_to_cpu(IBMVNIC_GET_STAT
2944 (adapter, ibmvnic_stats[i].offset));
John Allen3d52b592017-08-02 16:44:14 -05002945
2946 for (j = 0; j < adapter->req_tx_queues; j++) {
2947 data[i] = adapter->tx_stats_buffers[j].packets;
2948 i++;
2949 data[i] = adapter->tx_stats_buffers[j].bytes;
2950 i++;
2951 data[i] = adapter->tx_stats_buffers[j].dropped_packets;
2952 i++;
2953 }
2954
2955 for (j = 0; j < adapter->req_rx_queues; j++) {
2956 data[i] = adapter->rx_stats_buffers[j].packets;
2957 i++;
2958 data[i] = adapter->rx_stats_buffers[j].bytes;
2959 i++;
2960 data[i] = adapter->rx_stats_buffers[j].interrupts;
2961 i++;
2962 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002963}
2964
Thomas Falcon723ad912018-09-28 18:38:26 -05002965static u32 ibmvnic_get_priv_flags(struct net_device *netdev)
2966{
2967 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2968
2969 return adapter->priv_flags;
2970}
2971
2972static int ibmvnic_set_priv_flags(struct net_device *netdev, u32 flags)
2973{
2974 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2975 bool which_maxes = !!(flags & IBMVNIC_USE_SERVER_MAXES);
2976
2977 if (which_maxes)
2978 adapter->priv_flags |= IBMVNIC_USE_SERVER_MAXES;
2979 else
2980 adapter->priv_flags &= ~IBMVNIC_USE_SERVER_MAXES;
2981
2982 return 0;
2983}
Lijun Pan91dc5d22021-02-11 00:43:22 -06002984
Thomas Falcon032c5e82015-12-21 11:26:06 -06002985static const struct ethtool_ops ibmvnic_ethtool_ops = {
Thomas Falcon032c5e82015-12-21 11:26:06 -06002986 .get_drvinfo = ibmvnic_get_drvinfo,
2987 .get_msglevel = ibmvnic_get_msglevel,
2988 .set_msglevel = ibmvnic_set_msglevel,
2989 .get_link = ibmvnic_get_link,
2990 .get_ringparam = ibmvnic_get_ringparam,
John Allenc26eba02017-10-26 16:23:25 -05002991 .set_ringparam = ibmvnic_set_ringparam,
John Allenc2dbeb62017-08-02 16:47:17 -05002992 .get_channels = ibmvnic_get_channels,
John Allenc26eba02017-10-26 16:23:25 -05002993 .set_channels = ibmvnic_set_channels,
Thomas Falcon032c5e82015-12-21 11:26:06 -06002994 .get_strings = ibmvnic_get_strings,
2995 .get_sset_count = ibmvnic_get_sset_count,
2996 .get_ethtool_stats = ibmvnic_get_ethtool_stats,
Philippe Reynes8a433792017-01-07 22:37:29 +01002997 .get_link_ksettings = ibmvnic_get_link_ksettings,
Thomas Falcon723ad912018-09-28 18:38:26 -05002998 .get_priv_flags = ibmvnic_get_priv_flags,
2999 .set_priv_flags = ibmvnic_set_priv_flags,
Thomas Falcon032c5e82015-12-21 11:26:06 -06003000};
3001
3002/* Routines for managing CRQs/sCRQs */
3003
Nathan Fontenot57a49432017-05-26 10:31:12 -04003004static int reset_one_sub_crq_queue(struct ibmvnic_adapter *adapter,
3005 struct ibmvnic_sub_crq_queue *scrq)
3006{
3007 int rc;
3008
Dany Madden9281cf22020-11-25 18:04:26 -06003009 if (!scrq) {
YANG LI862aecb2020-12-30 15:23:14 +08003010 netdev_dbg(adapter->netdev, "Invalid scrq reset.\n");
Dany Madden9281cf22020-11-25 18:04:26 -06003011 return -EINVAL;
3012 }
3013
Nathan Fontenot57a49432017-05-26 10:31:12 -04003014 if (scrq->irq) {
3015 free_irq(scrq->irq, scrq);
3016 irq_dispose_mapping(scrq->irq);
3017 scrq->irq = 0;
3018 }
3019
Dany Madden9281cf22020-11-25 18:04:26 -06003020 if (scrq->msgs) {
3021 memset(scrq->msgs, 0, 4 * PAGE_SIZE);
3022 atomic_set(&scrq->used, 0);
3023 scrq->cur = 0;
Jakub Kicinski55fd59b2020-12-03 15:42:13 -08003024 scrq->ind_buf.index = 0;
Dany Madden9281cf22020-11-25 18:04:26 -06003025 } else {
3026 netdev_dbg(adapter->netdev, "Invalid scrq reset\n");
3027 return -EINVAL;
3028 }
Nathan Fontenot57a49432017-05-26 10:31:12 -04003029
3030 rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
3031 4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
3032 return rc;
3033}
3034
3035static int reset_sub_crq_queues(struct ibmvnic_adapter *adapter)
3036{
3037 int i, rc;
3038
Lijun Pana0faaa22020-11-23 13:35:45 -06003039 if (!adapter->tx_scrq || !adapter->rx_scrq)
3040 return -EINVAL;
3041
Nathan Fontenot57a49432017-05-26 10:31:12 -04003042 for (i = 0; i < adapter->req_tx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05003043 netdev_dbg(adapter->netdev, "Re-setting tx_scrq[%d]\n", i);
Nathan Fontenot57a49432017-05-26 10:31:12 -04003044 rc = reset_one_sub_crq_queue(adapter, adapter->tx_scrq[i]);
3045 if (rc)
3046 return rc;
3047 }
3048
3049 for (i = 0; i < adapter->req_rx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05003050 netdev_dbg(adapter->netdev, "Re-setting rx_scrq[%d]\n", i);
Nathan Fontenot57a49432017-05-26 10:31:12 -04003051 rc = reset_one_sub_crq_queue(adapter, adapter->rx_scrq[i]);
3052 if (rc)
3053 return rc;
3054 }
3055
Nathan Fontenot57a49432017-05-26 10:31:12 -04003056 return rc;
3057}
3058
Thomas Falcon032c5e82015-12-21 11:26:06 -06003059static void release_sub_crq_queue(struct ibmvnic_adapter *adapter,
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06003060 struct ibmvnic_sub_crq_queue *scrq,
3061 bool do_h_free)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003062{
3063 struct device *dev = &adapter->vdev->dev;
3064 long rc;
3065
3066 netdev_dbg(adapter->netdev, "Releasing sub-CRQ\n");
3067
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06003068 if (do_h_free) {
3069 /* Close the sub-crqs */
3070 do {
3071 rc = plpar_hcall_norets(H_FREE_SUB_CRQ,
3072 adapter->vdev->unit_address,
3073 scrq->crq_num);
3074 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
Thomas Falcon032c5e82015-12-21 11:26:06 -06003075
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06003076 if (rc) {
3077 netdev_err(adapter->netdev,
3078 "Failed to release sub-CRQ %16lx, rc = %ld\n",
3079 scrq->crq_num, rc);
3080 }
Thomas Falconffa73852017-04-19 13:44:29 -04003081 }
3082
Thomas Falconf019fb62020-11-18 19:12:17 -06003083 dma_free_coherent(dev,
3084 IBMVNIC_IND_ARR_SZ,
3085 scrq->ind_buf.indir_arr,
3086 scrq->ind_buf.indir_dma);
3087
Thomas Falcon032c5e82015-12-21 11:26:06 -06003088 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
3089 DMA_BIDIRECTIONAL);
3090 free_pages((unsigned long)scrq->msgs, 2);
3091 kfree(scrq);
3092}
3093
3094static struct ibmvnic_sub_crq_queue *init_sub_crq_queue(struct ibmvnic_adapter
3095 *adapter)
3096{
3097 struct device *dev = &adapter->vdev->dev;
3098 struct ibmvnic_sub_crq_queue *scrq;
3099 int rc;
3100
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04003101 scrq = kzalloc(sizeof(*scrq), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003102 if (!scrq)
3103 return NULL;
3104
Nathan Fontenot7f7adc52017-04-19 13:45:16 -04003105 scrq->msgs =
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04003106 (union sub_crq *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, 2);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003107 if (!scrq->msgs) {
3108 dev_warn(dev, "Couldn't allocate crq queue messages page\n");
3109 goto zero_page_failed;
3110 }
3111
3112 scrq->msg_token = dma_map_single(dev, scrq->msgs, 4 * PAGE_SIZE,
3113 DMA_BIDIRECTIONAL);
3114 if (dma_mapping_error(dev, scrq->msg_token)) {
3115 dev_warn(dev, "Couldn't map crq queue messages page\n");
3116 goto map_failed;
3117 }
3118
3119 rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
3120 4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
3121
3122 if (rc == H_RESOURCE)
3123 rc = ibmvnic_reset_crq(adapter);
3124
3125 if (rc == H_CLOSED) {
3126 dev_warn(dev, "Partner adapter not ready, waiting.\n");
3127 } else if (rc) {
3128 dev_warn(dev, "Error %d registering sub-crq\n", rc);
3129 goto reg_failed;
3130 }
3131
Thomas Falcon032c5e82015-12-21 11:26:06 -06003132 scrq->adapter = adapter;
3133 scrq->size = 4 * PAGE_SIZE / sizeof(*scrq->msgs);
Thomas Falconf019fb62020-11-18 19:12:17 -06003134 scrq->ind_buf.index = 0;
3135
3136 scrq->ind_buf.indir_arr =
3137 dma_alloc_coherent(dev,
3138 IBMVNIC_IND_ARR_SZ,
3139 &scrq->ind_buf.indir_dma,
3140 GFP_KERNEL);
3141
3142 if (!scrq->ind_buf.indir_arr)
3143 goto indir_failed;
3144
Thomas Falcon032c5e82015-12-21 11:26:06 -06003145 spin_lock_init(&scrq->lock);
3146
3147 netdev_dbg(adapter->netdev,
3148 "sub-crq initialized, num %lx, hw_irq=%lx, irq=%x\n",
3149 scrq->crq_num, scrq->hw_irq, scrq->irq);
3150
3151 return scrq;
3152
Thomas Falconf019fb62020-11-18 19:12:17 -06003153indir_failed:
3154 do {
3155 rc = plpar_hcall_norets(H_FREE_SUB_CRQ,
3156 adapter->vdev->unit_address,
3157 scrq->crq_num);
3158 } while (rc == H_BUSY || rc == H_IS_LONG_BUSY(rc));
Thomas Falcon032c5e82015-12-21 11:26:06 -06003159reg_failed:
3160 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
3161 DMA_BIDIRECTIONAL);
3162map_failed:
3163 free_pages((unsigned long)scrq->msgs, 2);
3164zero_page_failed:
3165 kfree(scrq);
3166
3167 return NULL;
3168}
3169
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06003170static void release_sub_crqs(struct ibmvnic_adapter *adapter, bool do_h_free)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003171{
3172 int i;
3173
3174 if (adapter->tx_scrq) {
Nathan Fontenot82e3be32018-02-21 21:33:56 -06003175 for (i = 0; i < adapter->num_active_tx_scrqs; i++) {
Nathan Fontenotb5108882017-03-30 02:49:18 -04003176 if (!adapter->tx_scrq[i])
3177 continue;
3178
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05003179 netdev_dbg(adapter->netdev, "Releasing tx_scrq[%d]\n",
3180 i);
Sukadev Bhattiprolu65d64702021-06-23 21:13:12 -07003181 ibmvnic_tx_scrq_clean_buffer(adapter, adapter->tx_scrq[i]);
Nathan Fontenotb5108882017-03-30 02:49:18 -04003182 if (adapter->tx_scrq[i]->irq) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06003183 free_irq(adapter->tx_scrq[i]->irq,
3184 adapter->tx_scrq[i]);
Thomas Falcon88eb98a2016-07-06 15:35:16 -05003185 irq_dispose_mapping(adapter->tx_scrq[i]->irq);
Nathan Fontenotb5108882017-03-30 02:49:18 -04003186 adapter->tx_scrq[i]->irq = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003187 }
Nathan Fontenotb5108882017-03-30 02:49:18 -04003188
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06003189 release_sub_crq_queue(adapter, adapter->tx_scrq[i],
3190 do_h_free);
Nathan Fontenotb5108882017-03-30 02:49:18 -04003191 }
3192
Nathan Fontenot9501df32017-03-15 23:38:07 -04003193 kfree(adapter->tx_scrq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003194 adapter->tx_scrq = NULL;
Nathan Fontenot82e3be32018-02-21 21:33:56 -06003195 adapter->num_active_tx_scrqs = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003196 }
3197
3198 if (adapter->rx_scrq) {
Nathan Fontenot82e3be32018-02-21 21:33:56 -06003199 for (i = 0; i < adapter->num_active_rx_scrqs; i++) {
Nathan Fontenotb5108882017-03-30 02:49:18 -04003200 if (!adapter->rx_scrq[i])
3201 continue;
3202
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05003203 netdev_dbg(adapter->netdev, "Releasing rx_scrq[%d]\n",
3204 i);
Nathan Fontenotb5108882017-03-30 02:49:18 -04003205 if (adapter->rx_scrq[i]->irq) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06003206 free_irq(adapter->rx_scrq[i]->irq,
3207 adapter->rx_scrq[i]);
Thomas Falcon88eb98a2016-07-06 15:35:16 -05003208 irq_dispose_mapping(adapter->rx_scrq[i]->irq);
Nathan Fontenotb5108882017-03-30 02:49:18 -04003209 adapter->rx_scrq[i]->irq = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003210 }
Nathan Fontenotb5108882017-03-30 02:49:18 -04003211
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06003212 release_sub_crq_queue(adapter, adapter->rx_scrq[i],
3213 do_h_free);
Nathan Fontenotb5108882017-03-30 02:49:18 -04003214 }
3215
Nathan Fontenot9501df32017-03-15 23:38:07 -04003216 kfree(adapter->rx_scrq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003217 adapter->rx_scrq = NULL;
Nathan Fontenot82e3be32018-02-21 21:33:56 -06003218 adapter->num_active_rx_scrqs = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003219 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06003220}
3221
3222static int disable_scrq_irq(struct ibmvnic_adapter *adapter,
3223 struct ibmvnic_sub_crq_queue *scrq)
3224{
3225 struct device *dev = &adapter->vdev->dev;
3226 unsigned long rc;
3227
3228 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
3229 H_DISABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
3230 if (rc)
3231 dev_err(dev, "Couldn't disable scrq irq 0x%lx. rc=%ld\n",
3232 scrq->hw_irq, rc);
3233 return rc;
3234}
3235
3236static int enable_scrq_irq(struct ibmvnic_adapter *adapter,
3237 struct ibmvnic_sub_crq_queue *scrq)
3238{
3239 struct device *dev = &adapter->vdev->dev;
3240 unsigned long rc;
3241
3242 if (scrq->hw_irq > 0x100000000ULL) {
3243 dev_err(dev, "bad hw_irq = %lx\n", scrq->hw_irq);
3244 return 1;
3245 }
3246
Juliet Kim7ed5b312019-09-20 16:11:23 -04003247 if (test_bit(0, &adapter->resetting) &&
Nathan Fontenot73f9d362018-05-22 11:21:10 -05003248 adapter->reset_reason == VNIC_RESET_MOBILITY) {
Juliet Kim284f87d2019-11-20 10:50:03 -05003249 u64 val = (0xff000000) | scrq->hw_irq;
Nathan Fontenot73f9d362018-05-22 11:21:10 -05003250
Juliet Kim284f87d2019-11-20 10:50:03 -05003251 rc = plpar_hcall_norets(H_EOI, val);
Juliet Kim2df5c602019-11-20 10:50:04 -05003252 /* H_EOI would fail with rc = H_FUNCTION when running
3253 * in XIVE mode which is expected, but not an error.
3254 */
Lijun Pan91dc5d22021-02-11 00:43:22 -06003255 if (rc && rc != H_FUNCTION)
Juliet Kim284f87d2019-11-20 10:50:03 -05003256 dev_err(dev, "H_EOI FAILED irq 0x%llx. rc=%ld\n",
3257 val, rc);
Nathan Fontenot73f9d362018-05-22 11:21:10 -05003258 }
Thomas Falconf23e0642018-04-15 18:53:36 -05003259
Thomas Falcon032c5e82015-12-21 11:26:06 -06003260 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
3261 H_ENABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
3262 if (rc)
3263 dev_err(dev, "Couldn't enable scrq irq 0x%lx. rc=%ld\n",
3264 scrq->hw_irq, rc);
3265 return rc;
3266}
3267
3268static int ibmvnic_complete_tx(struct ibmvnic_adapter *adapter,
3269 struct ibmvnic_sub_crq_queue *scrq)
3270{
3271 struct device *dev = &adapter->vdev->dev;
Thomas Falcon06b3e352018-03-16 20:00:28 -05003272 struct ibmvnic_tx_pool *tx_pool;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003273 struct ibmvnic_tx_buff *txbuff;
Thomas Falcon0d973382020-11-18 19:12:19 -06003274 struct netdev_queue *txq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003275 union sub_crq *next;
3276 int index;
Thomas Falconc62aa372020-11-18 19:12:20 -06003277 int i;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003278
3279restart_loop:
3280 while (pending_scrq(adapter, scrq)) {
3281 unsigned int pool = scrq->pool_index;
Thomas Falconffc385b2018-02-18 10:08:41 -06003282 int num_entries = 0;
Thomas Falcon0d973382020-11-18 19:12:19 -06003283 int total_bytes = 0;
3284 int num_packets = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003285
3286 next = ibmvnic_next_scrq(adapter, scrq);
3287 for (i = 0; i < next->tx_comp.num_comps; i++) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06003288 index = be32_to_cpu(next->tx_comp.correlators[i]);
Thomas Falcon06b3e352018-03-16 20:00:28 -05003289 if (index & IBMVNIC_TSO_POOL_MASK) {
3290 tx_pool = &adapter->tso_pool[pool];
3291 index &= ~IBMVNIC_TSO_POOL_MASK;
3292 } else {
3293 tx_pool = &adapter->tx_pool[pool];
3294 }
3295
3296 txbuff = &tx_pool->tx_buff[index];
Thomas Falcon0d973382020-11-18 19:12:19 -06003297 num_packets++;
Thomas Falconffc385b2018-02-18 10:08:41 -06003298 num_entries += txbuff->num_entries;
Thomas Falcon0d973382020-11-18 19:12:19 -06003299 if (txbuff->skb) {
3300 total_bytes += txbuff->skb->len;
Lijun Panca09bf72021-04-13 03:33:25 -05003301 if (next->tx_comp.rcs[i]) {
3302 dev_err(dev, "tx error %x\n",
3303 next->tx_comp.rcs[i]);
3304 dev_kfree_skb_irq(txbuff->skb);
3305 } else {
3306 dev_consume_skb_irq(txbuff->skb);
3307 }
Thomas Falcon0d973382020-11-18 19:12:19 -06003308 txbuff->skb = NULL;
3309 } else {
3310 netdev_warn(adapter->netdev,
3311 "TX completion received with NULL socket buffer\n");
3312 }
Thomas Falcon06b3e352018-03-16 20:00:28 -05003313 tx_pool->free_map[tx_pool->producer_index] = index;
3314 tx_pool->producer_index =
3315 (tx_pool->producer_index + 1) %
3316 tx_pool->num_buffers;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003317 }
3318 /* remove tx_comp scrq*/
3319 next->tx_comp.first = 0;
Nathan Fontenot7c3e7de2017-05-03 14:05:25 -04003320
Thomas Falcon0d973382020-11-18 19:12:19 -06003321 txq = netdev_get_tx_queue(adapter->netdev, scrq->pool_index);
3322 netdev_tx_completed_queue(txq, num_packets, total_bytes);
3323
Thomas Falconffc385b2018-02-18 10:08:41 -06003324 if (atomic_sub_return(num_entries, &scrq->used) <=
Nathan Fontenot7c3e7de2017-05-03 14:05:25 -04003325 (adapter->req_tx_entries_per_subcrq / 2) &&
3326 __netif_subqueue_stopped(adapter->netdev,
3327 scrq->pool_index)) {
3328 netif_wake_subqueue(adapter->netdev, scrq->pool_index);
Thomas Falcon0aecb132018-02-26 18:10:58 -06003329 netdev_dbg(adapter->netdev, "Started queue %d\n",
3330 scrq->pool_index);
Nathan Fontenot7c3e7de2017-05-03 14:05:25 -04003331 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06003332 }
3333
3334 enable_scrq_irq(adapter, scrq);
3335
3336 if (pending_scrq(adapter, scrq)) {
3337 disable_scrq_irq(adapter, scrq);
3338 goto restart_loop;
3339 }
3340
3341 return 0;
3342}
3343
3344static irqreturn_t ibmvnic_interrupt_tx(int irq, void *instance)
3345{
3346 struct ibmvnic_sub_crq_queue *scrq = instance;
3347 struct ibmvnic_adapter *adapter = scrq->adapter;
3348
3349 disable_scrq_irq(adapter, scrq);
3350 ibmvnic_complete_tx(adapter, scrq);
3351
3352 return IRQ_HANDLED;
3353}
3354
3355static irqreturn_t ibmvnic_interrupt_rx(int irq, void *instance)
3356{
3357 struct ibmvnic_sub_crq_queue *scrq = instance;
3358 struct ibmvnic_adapter *adapter = scrq->adapter;
3359
Nathan Fontenot09fb35e2018-01-10 10:40:09 -06003360 /* When booting a kdump kernel we can hit pending interrupts
3361 * prior to completing driver initialization.
3362 */
3363 if (unlikely(adapter->state != VNIC_OPEN))
3364 return IRQ_NONE;
3365
John Allen3d52b592017-08-02 16:44:14 -05003366 adapter->rx_stats_buffers[scrq->scrq_num].interrupts++;
3367
Thomas Falcon032c5e82015-12-21 11:26:06 -06003368 if (napi_schedule_prep(&adapter->napi[scrq->scrq_num])) {
3369 disable_scrq_irq(adapter, scrq);
3370 __napi_schedule(&adapter->napi[scrq->scrq_num]);
3371 }
3372
3373 return IRQ_HANDLED;
3374}
3375
Thomas Falconea22d512016-07-06 15:35:17 -05003376static int init_sub_crq_irqs(struct ibmvnic_adapter *adapter)
3377{
3378 struct device *dev = &adapter->vdev->dev;
3379 struct ibmvnic_sub_crq_queue *scrq;
3380 int i = 0, j = 0;
3381 int rc = 0;
3382
3383 for (i = 0; i < adapter->req_tx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05003384 netdev_dbg(adapter->netdev, "Initializing tx_scrq[%d] irq\n",
3385 i);
Thomas Falconea22d512016-07-06 15:35:17 -05003386 scrq = adapter->tx_scrq[i];
3387 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
3388
Michael Ellerman99c17902016-09-10 19:59:05 +10003389 if (!scrq->irq) {
Thomas Falconea22d512016-07-06 15:35:17 -05003390 rc = -EINVAL;
3391 dev_err(dev, "Error mapping irq\n");
3392 goto req_tx_irq_failed;
3393 }
3394
Murilo Fossa Vicentinie56e2512019-04-25 11:02:33 -03003395 snprintf(scrq->name, sizeof(scrq->name), "ibmvnic-%x-tx%d",
3396 adapter->vdev->unit_address, i);
Thomas Falconea22d512016-07-06 15:35:17 -05003397 rc = request_irq(scrq->irq, ibmvnic_interrupt_tx,
Murilo Fossa Vicentinie56e2512019-04-25 11:02:33 -03003398 0, scrq->name, scrq);
Thomas Falconea22d512016-07-06 15:35:17 -05003399
3400 if (rc) {
3401 dev_err(dev, "Couldn't register tx irq 0x%x. rc=%d\n",
3402 scrq->irq, rc);
3403 irq_dispose_mapping(scrq->irq);
Nathan Fontenotaf9090c2018-02-20 11:04:18 -06003404 goto req_tx_irq_failed;
Thomas Falconea22d512016-07-06 15:35:17 -05003405 }
3406 }
3407
3408 for (i = 0; i < adapter->req_rx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05003409 netdev_dbg(adapter->netdev, "Initializing rx_scrq[%d] irq\n",
3410 i);
Thomas Falconea22d512016-07-06 15:35:17 -05003411 scrq = adapter->rx_scrq[i];
3412 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
Michael Ellerman99c17902016-09-10 19:59:05 +10003413 if (!scrq->irq) {
Thomas Falconea22d512016-07-06 15:35:17 -05003414 rc = -EINVAL;
3415 dev_err(dev, "Error mapping irq\n");
3416 goto req_rx_irq_failed;
3417 }
Murilo Fossa Vicentinie56e2512019-04-25 11:02:33 -03003418 snprintf(scrq->name, sizeof(scrq->name), "ibmvnic-%x-rx%d",
3419 adapter->vdev->unit_address, i);
Thomas Falconea22d512016-07-06 15:35:17 -05003420 rc = request_irq(scrq->irq, ibmvnic_interrupt_rx,
Murilo Fossa Vicentinie56e2512019-04-25 11:02:33 -03003421 0, scrq->name, scrq);
Thomas Falconea22d512016-07-06 15:35:17 -05003422 if (rc) {
3423 dev_err(dev, "Couldn't register rx irq 0x%x. rc=%d\n",
3424 scrq->irq, rc);
3425 irq_dispose_mapping(scrq->irq);
3426 goto req_rx_irq_failed;
3427 }
3428 }
3429 return rc;
3430
3431req_rx_irq_failed:
Thomas Falcon8bf371e2016-10-27 12:28:52 -05003432 for (j = 0; j < i; j++) {
Thomas Falconea22d512016-07-06 15:35:17 -05003433 free_irq(adapter->rx_scrq[j]->irq, adapter->rx_scrq[j]);
3434 irq_dispose_mapping(adapter->rx_scrq[j]->irq);
Thomas Falcon8bf371e2016-10-27 12:28:52 -05003435 }
Thomas Falconea22d512016-07-06 15:35:17 -05003436 i = adapter->req_tx_queues;
3437req_tx_irq_failed:
Thomas Falcon8bf371e2016-10-27 12:28:52 -05003438 for (j = 0; j < i; j++) {
Thomas Falconea22d512016-07-06 15:35:17 -05003439 free_irq(adapter->tx_scrq[j]->irq, adapter->tx_scrq[j]);
Thomas Falcon27a21452020-07-29 16:36:32 -05003440 irq_dispose_mapping(adapter->tx_scrq[j]->irq);
Thomas Falcon8bf371e2016-10-27 12:28:52 -05003441 }
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06003442 release_sub_crqs(adapter, 1);
Thomas Falconea22d512016-07-06 15:35:17 -05003443 return rc;
3444}
3445
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003446static int init_sub_crqs(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003447{
3448 struct device *dev = &adapter->vdev->dev;
3449 struct ibmvnic_sub_crq_queue **allqueues;
3450 int registered_queues = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003451 int total_queues;
3452 int more = 0;
Thomas Falconea22d512016-07-06 15:35:17 -05003453 int i;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003454
Thomas Falcon032c5e82015-12-21 11:26:06 -06003455 total_queues = adapter->req_tx_queues + adapter->req_rx_queues;
3456
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04003457 allqueues = kcalloc(total_queues, sizeof(*allqueues), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003458 if (!allqueues)
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003459 return -1;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003460
3461 for (i = 0; i < total_queues; i++) {
3462 allqueues[i] = init_sub_crq_queue(adapter);
3463 if (!allqueues[i]) {
3464 dev_warn(dev, "Couldn't allocate all sub-crqs\n");
3465 break;
3466 }
3467 registered_queues++;
3468 }
3469
3470 /* Make sure we were able to register the minimum number of queues */
3471 if (registered_queues <
3472 adapter->min_tx_queues + adapter->min_rx_queues) {
3473 dev_err(dev, "Fatal: Couldn't init min number of sub-crqs\n");
3474 goto tx_failed;
3475 }
3476
3477 /* Distribute the failed allocated queues*/
3478 for (i = 0; i < total_queues - registered_queues + more ; i++) {
3479 netdev_dbg(adapter->netdev, "Reducing number of queues\n");
3480 switch (i % 3) {
3481 case 0:
3482 if (adapter->req_rx_queues > adapter->min_rx_queues)
3483 adapter->req_rx_queues--;
3484 else
3485 more++;
3486 break;
3487 case 1:
3488 if (adapter->req_tx_queues > adapter->min_tx_queues)
3489 adapter->req_tx_queues--;
3490 else
3491 more++;
3492 break;
3493 }
3494 }
3495
3496 adapter->tx_scrq = kcalloc(adapter->req_tx_queues,
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04003497 sizeof(*adapter->tx_scrq), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003498 if (!adapter->tx_scrq)
3499 goto tx_failed;
3500
3501 for (i = 0; i < adapter->req_tx_queues; i++) {
3502 adapter->tx_scrq[i] = allqueues[i];
3503 adapter->tx_scrq[i]->pool_index = i;
Nathan Fontenot82e3be32018-02-21 21:33:56 -06003504 adapter->num_active_tx_scrqs++;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003505 }
3506
3507 adapter->rx_scrq = kcalloc(adapter->req_rx_queues,
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04003508 sizeof(*adapter->rx_scrq), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003509 if (!adapter->rx_scrq)
3510 goto rx_failed;
3511
3512 for (i = 0; i < adapter->req_rx_queues; i++) {
3513 adapter->rx_scrq[i] = allqueues[i + adapter->req_tx_queues];
3514 adapter->rx_scrq[i]->scrq_num = i;
Nathan Fontenot82e3be32018-02-21 21:33:56 -06003515 adapter->num_active_rx_scrqs++;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003516 }
3517
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003518 kfree(allqueues);
3519 return 0;
3520
3521rx_failed:
3522 kfree(adapter->tx_scrq);
3523 adapter->tx_scrq = NULL;
3524tx_failed:
3525 for (i = 0; i < registered_queues; i++)
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06003526 release_sub_crq_queue(adapter, allqueues[i], 1);
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003527 kfree(allqueues);
3528 return -1;
3529}
3530
Lijun Pan09081b92020-09-27 20:13:27 -05003531static void send_request_cap(struct ibmvnic_adapter *adapter, int retry)
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003532{
3533 struct device *dev = &adapter->vdev->dev;
3534 union ibmvnic_crq crq;
John Allenc26eba02017-10-26 16:23:25 -05003535 int max_entries;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003536
3537 if (!retry) {
3538 /* Sub-CRQ entries are 32 byte long */
3539 int entries_page = 4 * PAGE_SIZE / (sizeof(u64) * 4);
3540
3541 if (adapter->min_tx_entries_per_subcrq > entries_page ||
3542 adapter->min_rx_add_entries_per_subcrq > entries_page) {
3543 dev_err(dev, "Fatal, invalid entries per sub-crq\n");
3544 return;
3545 }
3546
John Allenc26eba02017-10-26 16:23:25 -05003547 if (adapter->desired.mtu)
3548 adapter->req_mtu = adapter->desired.mtu;
3549 else
3550 adapter->req_mtu = adapter->netdev->mtu + ETH_HLEN;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003551
John Allenc26eba02017-10-26 16:23:25 -05003552 if (!adapter->desired.tx_entries)
3553 adapter->desired.tx_entries =
3554 adapter->max_tx_entries_per_subcrq;
3555 if (!adapter->desired.rx_entries)
3556 adapter->desired.rx_entries =
3557 adapter->max_rx_add_entries_per_subcrq;
3558
3559 max_entries = IBMVNIC_MAX_LTB_SIZE /
3560 (adapter->req_mtu + IBMVNIC_BUFFER_HLEN);
3561
3562 if ((adapter->req_mtu + IBMVNIC_BUFFER_HLEN) *
3563 adapter->desired.tx_entries > IBMVNIC_MAX_LTB_SIZE) {
3564 adapter->desired.tx_entries = max_entries;
3565 }
3566
3567 if ((adapter->req_mtu + IBMVNIC_BUFFER_HLEN) *
3568 adapter->desired.rx_entries > IBMVNIC_MAX_LTB_SIZE) {
3569 adapter->desired.rx_entries = max_entries;
3570 }
3571
3572 if (adapter->desired.tx_entries)
3573 adapter->req_tx_entries_per_subcrq =
3574 adapter->desired.tx_entries;
3575 else
3576 adapter->req_tx_entries_per_subcrq =
3577 adapter->max_tx_entries_per_subcrq;
3578
3579 if (adapter->desired.rx_entries)
3580 adapter->req_rx_add_entries_per_subcrq =
3581 adapter->desired.rx_entries;
3582 else
3583 adapter->req_rx_add_entries_per_subcrq =
3584 adapter->max_rx_add_entries_per_subcrq;
3585
3586 if (adapter->desired.tx_queues)
3587 adapter->req_tx_queues =
3588 adapter->desired.tx_queues;
3589 else
3590 adapter->req_tx_queues =
3591 adapter->opt_tx_comp_sub_queues;
3592
3593 if (adapter->desired.rx_queues)
3594 adapter->req_rx_queues =
3595 adapter->desired.rx_queues;
3596 else
3597 adapter->req_rx_queues =
3598 adapter->opt_rx_comp_queues;
3599
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003600 adapter->req_rx_add_queues = adapter->max_rx_add_queues;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003601 }
3602
Thomas Falcon032c5e82015-12-21 11:26:06 -06003603 memset(&crq, 0, sizeof(crq));
3604 crq.request_capability.first = IBMVNIC_CRQ_CMD;
3605 crq.request_capability.cmd = REQUEST_CAPABILITY;
3606
3607 crq.request_capability.capability = cpu_to_be16(REQ_TX_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06003608 crq.request_capability.number = cpu_to_be64(adapter->req_tx_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06003609 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003610 ibmvnic_send_crq(adapter, &crq);
3611
3612 crq.request_capability.capability = cpu_to_be16(REQ_RX_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06003613 crq.request_capability.number = cpu_to_be64(adapter->req_rx_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06003614 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003615 ibmvnic_send_crq(adapter, &crq);
3616
3617 crq.request_capability.capability = cpu_to_be16(REQ_RX_ADD_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06003618 crq.request_capability.number = cpu_to_be64(adapter->req_rx_add_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06003619 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003620 ibmvnic_send_crq(adapter, &crq);
3621
3622 crq.request_capability.capability =
3623 cpu_to_be16(REQ_TX_ENTRIES_PER_SUBCRQ);
3624 crq.request_capability.number =
Thomas Falconde89e852016-03-01 10:20:09 -06003625 cpu_to_be64(adapter->req_tx_entries_per_subcrq);
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_RX_ADD_ENTRIES_PER_SUBCRQ);
3631 crq.request_capability.number =
Thomas Falconde89e852016-03-01 10:20:09 -06003632 cpu_to_be64(adapter->req_rx_add_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 = cpu_to_be16(REQ_MTU);
Thomas Falconde89e852016-03-01 10:20:09 -06003637 crq.request_capability.number = cpu_to_be64(adapter->req_mtu);
Thomas Falcon901e0402017-02-15 12:17:59 -06003638 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003639 ibmvnic_send_crq(adapter, &crq);
3640
3641 if (adapter->netdev->flags & IFF_PROMISC) {
3642 if (adapter->promisc_supported) {
3643 crq.request_capability.capability =
3644 cpu_to_be16(PROMISC_REQUESTED);
Thomas Falconde89e852016-03-01 10:20:09 -06003645 crq.request_capability.number = cpu_to_be64(1);
Thomas Falcon901e0402017-02-15 12:17:59 -06003646 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003647 ibmvnic_send_crq(adapter, &crq);
3648 }
3649 } else {
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(0);
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 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06003656}
3657
3658static int pending_scrq(struct ibmvnic_adapter *adapter,
3659 struct ibmvnic_sub_crq_queue *scrq)
3660{
3661 union sub_crq *entry = &scrq->msgs[scrq->cur];
Lijun Pan665ab1e2021-01-29 19:19:04 -06003662 int rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003663
Lijun Pan665ab1e2021-01-29 19:19:04 -06003664 rc = !!(entry->generic.first & IBMVNIC_CRQ_CMD_RSP);
3665
3666 /* Ensure that the SCRQ valid flag is loaded prior to loading the
3667 * contents of the SCRQ descriptor
3668 */
3669 dma_rmb();
3670
3671 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003672}
3673
3674static union sub_crq *ibmvnic_next_scrq(struct ibmvnic_adapter *adapter,
3675 struct ibmvnic_sub_crq_queue *scrq)
3676{
3677 union sub_crq *entry;
3678 unsigned long flags;
3679
3680 spin_lock_irqsave(&scrq->lock, flags);
3681 entry = &scrq->msgs[scrq->cur];
3682 if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP) {
3683 if (++scrq->cur == scrq->size)
3684 scrq->cur = 0;
3685 } else {
3686 entry = NULL;
3687 }
3688 spin_unlock_irqrestore(&scrq->lock, flags);
3689
Lijun Pan665ab1e2021-01-29 19:19:04 -06003690 /* Ensure that the SCRQ valid flag is loaded prior to loading the
3691 * contents of the SCRQ descriptor
Thomas Falconb71ec952020-12-01 09:52:10 -06003692 */
3693 dma_rmb();
3694
Thomas Falcon032c5e82015-12-21 11:26:06 -06003695 return entry;
3696}
3697
3698static union ibmvnic_crq *ibmvnic_next_crq(struct ibmvnic_adapter *adapter)
3699{
3700 struct ibmvnic_crq_queue *queue = &adapter->crq;
3701 union ibmvnic_crq *crq;
3702
3703 crq = &queue->msgs[queue->cur];
3704 if (crq->generic.first & IBMVNIC_CRQ_CMD_RSP) {
3705 if (++queue->cur == queue->size)
3706 queue->cur = 0;
3707 } else {
3708 crq = NULL;
3709 }
3710
3711 return crq;
3712}
3713
Thomas Falcon2d14d372018-07-13 12:03:32 -05003714static void print_subcrq_error(struct device *dev, int rc, const char *func)
3715{
3716 switch (rc) {
3717 case H_PARAMETER:
3718 dev_warn_ratelimited(dev,
3719 "%s failed: Send request is malformed or adapter failover pending. (rc=%d)\n",
3720 func, rc);
3721 break;
3722 case H_CLOSED:
3723 dev_warn_ratelimited(dev,
3724 "%s failed: Backing queue closed. Adapter is down or failover pending. (rc=%d)\n",
3725 func, rc);
3726 break;
3727 default:
3728 dev_err_ratelimited(dev, "%s failed: (rc=%d)\n", func, rc);
3729 break;
3730 }
3731}
3732
Thomas Falconad7775d2016-04-01 17:20:34 -05003733static int send_subcrq_indirect(struct ibmvnic_adapter *adapter,
3734 u64 remote_handle, u64 ioba, u64 num_entries)
3735{
3736 unsigned int ua = adapter->vdev->unit_address;
3737 struct device *dev = &adapter->vdev->dev;
3738 int rc;
3739
3740 /* Make sure the hypervisor sees the complete request */
Lijun Pan1a421562021-02-12 20:36:46 -06003741 dma_wmb();
Thomas Falconad7775d2016-04-01 17:20:34 -05003742 rc = plpar_hcall_norets(H_SEND_SUB_CRQ_INDIRECT, ua,
3743 cpu_to_be64(remote_handle),
3744 ioba, num_entries);
3745
Thomas Falcon2d14d372018-07-13 12:03:32 -05003746 if (rc)
3747 print_subcrq_error(dev, rc, __func__);
Thomas Falconad7775d2016-04-01 17:20:34 -05003748
3749 return rc;
3750}
3751
Thomas Falcon032c5e82015-12-21 11:26:06 -06003752static int ibmvnic_send_crq(struct ibmvnic_adapter *adapter,
3753 union ibmvnic_crq *crq)
3754{
3755 unsigned int ua = adapter->vdev->unit_address;
3756 struct device *dev = &adapter->vdev->dev;
3757 u64 *u64_crq = (u64 *)crq;
3758 int rc;
3759
3760 netdev_dbg(adapter->netdev, "Sending CRQ: %016lx %016lx\n",
Lijun Pan429aa362021-02-11 00:43:18 -06003761 (unsigned long)cpu_to_be64(u64_crq[0]),
3762 (unsigned long)cpu_to_be64(u64_crq[1]));
Thomas Falcon032c5e82015-12-21 11:26:06 -06003763
Thomas Falcon51536982018-05-23 13:37:56 -05003764 if (!adapter->crq.active &&
3765 crq->generic.first != IBMVNIC_CRQ_INIT_CMD) {
3766 dev_warn(dev, "Invalid request detected while CRQ is inactive, possible device state change during reset\n");
3767 return -EINVAL;
3768 }
3769
Thomas Falcon032c5e82015-12-21 11:26:06 -06003770 /* Make sure the hypervisor sees the complete request */
Lijun Pan1a421562021-02-12 20:36:46 -06003771 dma_wmb();
Thomas Falcon032c5e82015-12-21 11:26:06 -06003772
3773 rc = plpar_hcall_norets(H_SEND_CRQ, ua,
3774 cpu_to_be64(u64_crq[0]),
3775 cpu_to_be64(u64_crq[1]));
3776
3777 if (rc) {
Nathan Fontenotec95dff2018-02-07 13:00:24 -06003778 if (rc == H_CLOSED) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06003779 dev_warn(dev, "CRQ Queue closed\n");
Lijun Panfa68bfa2020-08-19 17:52:24 -05003780 /* do not reset, report the fail, wait for passive init from server */
Nathan Fontenotec95dff2018-02-07 13:00:24 -06003781 }
3782
Thomas Falcon032c5e82015-12-21 11:26:06 -06003783 dev_warn(dev, "Send error (rc=%d)\n", rc);
3784 }
3785
3786 return rc;
3787}
3788
3789static int ibmvnic_send_crq_init(struct ibmvnic_adapter *adapter)
3790{
Thomas Falcon36a782f2020-08-31 11:59:57 -05003791 struct device *dev = &adapter->vdev->dev;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003792 union ibmvnic_crq crq;
Thomas Falcon36a782f2020-08-31 11:59:57 -05003793 int retries = 100;
3794 int rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003795
3796 memset(&crq, 0, sizeof(crq));
3797 crq.generic.first = IBMVNIC_CRQ_INIT_CMD;
3798 crq.generic.cmd = IBMVNIC_CRQ_INIT;
3799 netdev_dbg(adapter->netdev, "Sending CRQ init\n");
3800
Thomas Falcon36a782f2020-08-31 11:59:57 -05003801 do {
3802 rc = ibmvnic_send_crq(adapter, &crq);
3803 if (rc != H_CLOSED)
3804 break;
3805 retries--;
3806 msleep(50);
3807
3808 } while (retries > 0);
3809
3810 if (rc) {
3811 dev_err(dev, "Failed to send init request, rc = %d\n", rc);
3812 return rc;
3813 }
3814
3815 return 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003816}
3817
Thomas Falcon032c5e82015-12-21 11:26:06 -06003818static int send_version_xchg(struct ibmvnic_adapter *adapter)
3819{
3820 union ibmvnic_crq crq;
3821
3822 memset(&crq, 0, sizeof(crq));
3823 crq.version_exchange.first = IBMVNIC_CRQ_CMD;
3824 crq.version_exchange.cmd = VERSION_EXCHANGE;
3825 crq.version_exchange.version = cpu_to_be16(ibmvnic_version);
3826
3827 return ibmvnic_send_crq(adapter, &crq);
3828}
3829
Nathan Fontenot37798d02017-11-08 11:23:56 -06003830struct vnic_login_client_data {
3831 u8 type;
3832 __be16 len;
Kees Cook08ea5562018-04-10 15:26:43 -07003833 char name[];
Nathan Fontenot37798d02017-11-08 11:23:56 -06003834} __packed;
3835
3836static int vnic_client_data_len(struct ibmvnic_adapter *adapter)
3837{
3838 int len;
3839
3840 /* Calculate the amount of buffer space needed for the
3841 * vnic client data in the login buffer. There are four entries,
3842 * OS name, LPAR name, device name, and a null last entry.
3843 */
3844 len = 4 * sizeof(struct vnic_login_client_data);
3845 len += 6; /* "Linux" plus NULL */
3846 len += strlen(utsname()->nodename) + 1;
3847 len += strlen(adapter->netdev->name) + 1;
3848
3849 return len;
3850}
3851
3852static void vnic_add_client_data(struct ibmvnic_adapter *adapter,
3853 struct vnic_login_client_data *vlcd)
3854{
3855 const char *os_name = "Linux";
3856 int len;
3857
3858 /* Type 1 - LPAR OS */
3859 vlcd->type = 1;
3860 len = strlen(os_name) + 1;
3861 vlcd->len = cpu_to_be16(len);
Kees Cook08ea5562018-04-10 15:26:43 -07003862 strncpy(vlcd->name, os_name, len);
3863 vlcd = (struct vnic_login_client_data *)(vlcd->name + len);
Nathan Fontenot37798d02017-11-08 11:23:56 -06003864
3865 /* Type 2 - LPAR name */
3866 vlcd->type = 2;
3867 len = strlen(utsname()->nodename) + 1;
3868 vlcd->len = cpu_to_be16(len);
Kees Cook08ea5562018-04-10 15:26:43 -07003869 strncpy(vlcd->name, utsname()->nodename, len);
3870 vlcd = (struct vnic_login_client_data *)(vlcd->name + len);
Nathan Fontenot37798d02017-11-08 11:23:56 -06003871
3872 /* Type 3 - device name */
3873 vlcd->type = 3;
3874 len = strlen(adapter->netdev->name) + 1;
3875 vlcd->len = cpu_to_be16(len);
Kees Cook08ea5562018-04-10 15:26:43 -07003876 strncpy(vlcd->name, adapter->netdev->name, len);
Nathan Fontenot37798d02017-11-08 11:23:56 -06003877}
3878
Thomas Falcon20a8ab72018-02-26 18:10:59 -06003879static int send_login(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003880{
3881 struct ibmvnic_login_rsp_buffer *login_rsp_buffer;
3882 struct ibmvnic_login_buffer *login_buffer;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003883 struct device *dev = &adapter->vdev->dev;
Dany Maddenc98d9cc2020-11-25 18:04:30 -06003884 struct vnic_login_client_data *vlcd;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003885 dma_addr_t rsp_buffer_token;
3886 dma_addr_t buffer_token;
3887 size_t rsp_buffer_size;
3888 union ibmvnic_crq crq;
Dany Maddenc98d9cc2020-11-25 18:04:30 -06003889 int client_data_len;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003890 size_t buffer_size;
3891 __be64 *tx_list_p;
3892 __be64 *rx_list_p;
Dany Maddenc98d9cc2020-11-25 18:04:30 -06003893 int rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003894 int i;
3895
Thomas Falcon20a8ab72018-02-26 18:10:59 -06003896 if (!adapter->tx_scrq || !adapter->rx_scrq) {
3897 netdev_err(adapter->netdev,
3898 "RX or TX queues are not allocated, device login failed\n");
3899 return -1;
3900 }
3901
Lijun Pana0c8be52020-12-19 15:39:19 -06003902 release_login_buffer(adapter);
Thomas Falcon34f0f4e2018-02-13 18:23:40 -06003903 release_login_rsp_buffer(adapter);
Lijun Pana0c8be52020-12-19 15:39:19 -06003904
Nathan Fontenot37798d02017-11-08 11:23:56 -06003905 client_data_len = vnic_client_data_len(adapter);
3906
Thomas Falcon032c5e82015-12-21 11:26:06 -06003907 buffer_size =
3908 sizeof(struct ibmvnic_login_buffer) +
Nathan Fontenot37798d02017-11-08 11:23:56 -06003909 sizeof(u64) * (adapter->req_tx_queues + adapter->req_rx_queues) +
3910 client_data_len;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003911
Nathan Fontenot37798d02017-11-08 11:23:56 -06003912 login_buffer = kzalloc(buffer_size, GFP_ATOMIC);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003913 if (!login_buffer)
3914 goto buf_alloc_failed;
3915
3916 buffer_token = dma_map_single(dev, login_buffer, buffer_size,
3917 DMA_TO_DEVICE);
3918 if (dma_mapping_error(dev, buffer_token)) {
3919 dev_err(dev, "Couldn't map login buffer\n");
3920 goto buf_map_failed;
3921 }
3922
John Allen498cd8e2016-04-06 11:49:55 -05003923 rsp_buffer_size = sizeof(struct ibmvnic_login_rsp_buffer) +
3924 sizeof(u64) * adapter->req_tx_queues +
3925 sizeof(u64) * adapter->req_rx_queues +
3926 sizeof(u64) * adapter->req_rx_queues +
3927 sizeof(u8) * IBMVNIC_TX_DESC_VERSIONS;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003928
3929 login_rsp_buffer = kmalloc(rsp_buffer_size, GFP_ATOMIC);
3930 if (!login_rsp_buffer)
3931 goto buf_rsp_alloc_failed;
3932
3933 rsp_buffer_token = dma_map_single(dev, login_rsp_buffer,
3934 rsp_buffer_size, DMA_FROM_DEVICE);
3935 if (dma_mapping_error(dev, rsp_buffer_token)) {
3936 dev_err(dev, "Couldn't map login rsp buffer\n");
3937 goto buf_rsp_map_failed;
3938 }
Nathan Fontenot661a2622017-04-19 13:44:58 -04003939
Thomas Falcon032c5e82015-12-21 11:26:06 -06003940 adapter->login_buf = login_buffer;
3941 adapter->login_buf_token = buffer_token;
3942 adapter->login_buf_sz = buffer_size;
3943 adapter->login_rsp_buf = login_rsp_buffer;
3944 adapter->login_rsp_buf_token = rsp_buffer_token;
3945 adapter->login_rsp_buf_sz = rsp_buffer_size;
3946
3947 login_buffer->len = cpu_to_be32(buffer_size);
3948 login_buffer->version = cpu_to_be32(INITIAL_VERSION_LB);
3949 login_buffer->num_txcomp_subcrqs = cpu_to_be32(adapter->req_tx_queues);
3950 login_buffer->off_txcomp_subcrqs =
3951 cpu_to_be32(sizeof(struct ibmvnic_login_buffer));
3952 login_buffer->num_rxcomp_subcrqs = cpu_to_be32(adapter->req_rx_queues);
3953 login_buffer->off_rxcomp_subcrqs =
3954 cpu_to_be32(sizeof(struct ibmvnic_login_buffer) +
3955 sizeof(u64) * adapter->req_tx_queues);
3956 login_buffer->login_rsp_ioba = cpu_to_be32(rsp_buffer_token);
3957 login_buffer->login_rsp_len = cpu_to_be32(rsp_buffer_size);
3958
3959 tx_list_p = (__be64 *)((char *)login_buffer +
3960 sizeof(struct ibmvnic_login_buffer));
3961 rx_list_p = (__be64 *)((char *)login_buffer +
3962 sizeof(struct ibmvnic_login_buffer) +
3963 sizeof(u64) * adapter->req_tx_queues);
3964
3965 for (i = 0; i < adapter->req_tx_queues; i++) {
3966 if (adapter->tx_scrq[i]) {
Lijun Pan914789a2021-02-11 00:43:21 -06003967 tx_list_p[i] =
3968 cpu_to_be64(adapter->tx_scrq[i]->crq_num);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003969 }
3970 }
3971
3972 for (i = 0; i < adapter->req_rx_queues; i++) {
3973 if (adapter->rx_scrq[i]) {
Lijun Pan914789a2021-02-11 00:43:21 -06003974 rx_list_p[i] =
3975 cpu_to_be64(adapter->rx_scrq[i]->crq_num);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003976 }
3977 }
3978
Nathan Fontenot37798d02017-11-08 11:23:56 -06003979 /* Insert vNIC login client data */
3980 vlcd = (struct vnic_login_client_data *)
3981 ((char *)rx_list_p + (sizeof(u64) * adapter->req_rx_queues));
3982 login_buffer->client_data_offset =
3983 cpu_to_be32((char *)vlcd - (char *)login_buffer);
3984 login_buffer->client_data_len = cpu_to_be32(client_data_len);
3985
3986 vnic_add_client_data(adapter, vlcd);
3987
Thomas Falcon032c5e82015-12-21 11:26:06 -06003988 netdev_dbg(adapter->netdev, "Login Buffer:\n");
3989 for (i = 0; i < (adapter->login_buf_sz - 1) / 8 + 1; i++) {
3990 netdev_dbg(adapter->netdev, "%016lx\n",
Lijun Pan429aa362021-02-11 00:43:18 -06003991 ((unsigned long *)(adapter->login_buf))[i]);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003992 }
3993
3994 memset(&crq, 0, sizeof(crq));
3995 crq.login.first = IBMVNIC_CRQ_CMD;
3996 crq.login.cmd = LOGIN;
3997 crq.login.ioba = cpu_to_be32(buffer_token);
3998 crq.login.len = cpu_to_be32(buffer_size);
Sukadev Bhattiprolu76cdc5c2020-11-25 18:04:29 -06003999
4000 adapter->login_pending = true;
Dany Maddenc98d9cc2020-11-25 18:04:30 -06004001 rc = ibmvnic_send_crq(adapter, &crq);
4002 if (rc) {
4003 adapter->login_pending = false;
4004 netdev_err(adapter->netdev, "Failed to send login, rc=%d\n", rc);
4005 goto buf_rsp_map_failed;
4006 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06004007
Thomas Falcon20a8ab72018-02-26 18:10:59 -06004008 return 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004009
Thomas Falcon032c5e82015-12-21 11:26:06 -06004010buf_rsp_map_failed:
4011 kfree(login_rsp_buffer);
Dany Maddenc98d9cc2020-11-25 18:04:30 -06004012 adapter->login_rsp_buf = NULL;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004013buf_rsp_alloc_failed:
4014 dma_unmap_single(dev, buffer_token, buffer_size, DMA_TO_DEVICE);
4015buf_map_failed:
4016 kfree(login_buffer);
Dany Maddenc98d9cc2020-11-25 18:04:30 -06004017 adapter->login_buf = NULL;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004018buf_alloc_failed:
Thomas Falcon20a8ab72018-02-26 18:10:59 -06004019 return -1;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004020}
4021
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05004022static int send_request_map(struct ibmvnic_adapter *adapter, dma_addr_t addr,
4023 u32 len, u8 map_id)
Thomas Falcon032c5e82015-12-21 11:26:06 -06004024{
4025 union ibmvnic_crq crq;
4026
4027 memset(&crq, 0, sizeof(crq));
4028 crq.request_map.first = IBMVNIC_CRQ_CMD;
4029 crq.request_map.cmd = REQUEST_MAP;
4030 crq.request_map.map_id = map_id;
4031 crq.request_map.ioba = cpu_to_be32(addr);
4032 crq.request_map.len = cpu_to_be32(len);
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05004033 return ibmvnic_send_crq(adapter, &crq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004034}
4035
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05004036static int send_request_unmap(struct ibmvnic_adapter *adapter, u8 map_id)
Thomas Falcon032c5e82015-12-21 11:26:06 -06004037{
4038 union ibmvnic_crq crq;
4039
4040 memset(&crq, 0, sizeof(crq));
4041 crq.request_unmap.first = IBMVNIC_CRQ_CMD;
4042 crq.request_unmap.cmd = REQUEST_UNMAP;
4043 crq.request_unmap.map_id = map_id;
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05004044 return ibmvnic_send_crq(adapter, &crq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004045}
4046
Lijun Pan69980d02020-09-27 20:13:28 -05004047static void send_query_map(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06004048{
4049 union ibmvnic_crq crq;
4050
4051 memset(&crq, 0, sizeof(crq));
4052 crq.query_map.first = IBMVNIC_CRQ_CMD;
4053 crq.query_map.cmd = QUERY_MAP;
4054 ibmvnic_send_crq(adapter, &crq);
4055}
4056
4057/* Send a series of CRQs requesting various capabilities of the VNIC server */
Lijun Pan491099a2020-09-27 20:13:26 -05004058static void send_query_cap(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06004059{
4060 union ibmvnic_crq crq;
4061
Thomas Falcon901e0402017-02-15 12:17:59 -06004062 atomic_set(&adapter->running_cap_crqs, 0);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004063 memset(&crq, 0, sizeof(crq));
4064 crq.query_capability.first = IBMVNIC_CRQ_CMD;
4065 crq.query_capability.cmd = QUERY_CAPABILITY;
4066
4067 crq.query_capability.capability = cpu_to_be16(MIN_TX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06004068 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004069 ibmvnic_send_crq(adapter, &crq);
4070
4071 crq.query_capability.capability = cpu_to_be16(MIN_RX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06004072 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004073 ibmvnic_send_crq(adapter, &crq);
4074
4075 crq.query_capability.capability = cpu_to_be16(MIN_RX_ADD_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06004076 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004077 ibmvnic_send_crq(adapter, &crq);
4078
4079 crq.query_capability.capability = cpu_to_be16(MAX_TX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06004080 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004081 ibmvnic_send_crq(adapter, &crq);
4082
4083 crq.query_capability.capability = cpu_to_be16(MAX_RX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06004084 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004085 ibmvnic_send_crq(adapter, &crq);
4086
4087 crq.query_capability.capability = cpu_to_be16(MAX_RX_ADD_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06004088 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004089 ibmvnic_send_crq(adapter, &crq);
4090
4091 crq.query_capability.capability =
4092 cpu_to_be16(MIN_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06004093 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004094 ibmvnic_send_crq(adapter, &crq);
4095
4096 crq.query_capability.capability =
4097 cpu_to_be16(MIN_RX_ADD_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06004098 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004099 ibmvnic_send_crq(adapter, &crq);
4100
4101 crq.query_capability.capability =
4102 cpu_to_be16(MAX_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06004103 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004104 ibmvnic_send_crq(adapter, &crq);
4105
4106 crq.query_capability.capability =
4107 cpu_to_be16(MAX_RX_ADD_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06004108 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004109 ibmvnic_send_crq(adapter, &crq);
4110
4111 crq.query_capability.capability = cpu_to_be16(TCP_IP_OFFLOAD);
Thomas Falcon901e0402017-02-15 12:17:59 -06004112 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004113 ibmvnic_send_crq(adapter, &crq);
4114
4115 crq.query_capability.capability = cpu_to_be16(PROMISC_SUPPORTED);
Thomas Falcon901e0402017-02-15 12:17:59 -06004116 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004117 ibmvnic_send_crq(adapter, &crq);
4118
4119 crq.query_capability.capability = cpu_to_be16(MIN_MTU);
Thomas Falcon901e0402017-02-15 12:17:59 -06004120 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004121 ibmvnic_send_crq(adapter, &crq);
4122
4123 crq.query_capability.capability = cpu_to_be16(MAX_MTU);
Thomas Falcon901e0402017-02-15 12:17:59 -06004124 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004125 ibmvnic_send_crq(adapter, &crq);
4126
4127 crq.query_capability.capability = cpu_to_be16(MAX_MULTICAST_FILTERS);
Thomas Falcon901e0402017-02-15 12:17:59 -06004128 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004129 ibmvnic_send_crq(adapter, &crq);
4130
4131 crq.query_capability.capability = cpu_to_be16(VLAN_HEADER_INSERTION);
Thomas Falcon901e0402017-02-15 12:17:59 -06004132 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004133 ibmvnic_send_crq(adapter, &crq);
4134
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04004135 crq.query_capability.capability = cpu_to_be16(RX_VLAN_HEADER_INSERTION);
4136 atomic_inc(&adapter->running_cap_crqs);
4137 ibmvnic_send_crq(adapter, &crq);
4138
Thomas Falcon032c5e82015-12-21 11:26:06 -06004139 crq.query_capability.capability = cpu_to_be16(MAX_TX_SG_ENTRIES);
Thomas Falcon901e0402017-02-15 12:17:59 -06004140 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004141 ibmvnic_send_crq(adapter, &crq);
4142
4143 crq.query_capability.capability = cpu_to_be16(RX_SG_SUPPORTED);
Thomas Falcon901e0402017-02-15 12:17:59 -06004144 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004145 ibmvnic_send_crq(adapter, &crq);
4146
4147 crq.query_capability.capability = cpu_to_be16(OPT_TX_COMP_SUB_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06004148 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004149 ibmvnic_send_crq(adapter, &crq);
4150
4151 crq.query_capability.capability = cpu_to_be16(OPT_RX_COMP_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06004152 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004153 ibmvnic_send_crq(adapter, &crq);
4154
4155 crq.query_capability.capability =
4156 cpu_to_be16(OPT_RX_BUFADD_Q_PER_RX_COMP_Q);
Thomas Falcon901e0402017-02-15 12:17:59 -06004157 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004158 ibmvnic_send_crq(adapter, &crq);
4159
4160 crq.query_capability.capability =
4161 cpu_to_be16(OPT_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06004162 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004163 ibmvnic_send_crq(adapter, &crq);
4164
4165 crq.query_capability.capability =
4166 cpu_to_be16(OPT_RXBA_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06004167 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004168 ibmvnic_send_crq(adapter, &crq);
4169
4170 crq.query_capability.capability = cpu_to_be16(TX_RX_DESC_REQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06004171 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004172 ibmvnic_send_crq(adapter, &crq);
4173}
4174
Lijun Pan16e811f2020-09-27 20:13:29 -05004175static void send_query_ip_offload(struct ibmvnic_adapter *adapter)
4176{
4177 int buf_sz = sizeof(struct ibmvnic_query_ip_offload_buffer);
4178 struct device *dev = &adapter->vdev->dev;
4179 union ibmvnic_crq crq;
4180
4181 adapter->ip_offload_tok =
4182 dma_map_single(dev,
4183 &adapter->ip_offload_buf,
4184 buf_sz,
4185 DMA_FROM_DEVICE);
4186
4187 if (dma_mapping_error(dev, adapter->ip_offload_tok)) {
4188 if (!firmware_has_feature(FW_FEATURE_CMO))
4189 dev_err(dev, "Couldn't map offload buffer\n");
4190 return;
4191 }
4192
4193 memset(&crq, 0, sizeof(crq));
4194 crq.query_ip_offload.first = IBMVNIC_CRQ_CMD;
4195 crq.query_ip_offload.cmd = QUERY_IP_OFFLOAD;
4196 crq.query_ip_offload.len = cpu_to_be32(buf_sz);
4197 crq.query_ip_offload.ioba =
4198 cpu_to_be32(adapter->ip_offload_tok);
4199
4200 ibmvnic_send_crq(adapter, &crq);
4201}
4202
Lijun Pan46899bd2020-09-27 20:13:30 -05004203static void send_control_ip_offload(struct ibmvnic_adapter *adapter)
4204{
4205 struct ibmvnic_control_ip_offload_buffer *ctrl_buf = &adapter->ip_offload_ctrl;
4206 struct ibmvnic_query_ip_offload_buffer *buf = &adapter->ip_offload_buf;
4207 struct device *dev = &adapter->vdev->dev;
4208 netdev_features_t old_hw_features = 0;
4209 union ibmvnic_crq crq;
4210
4211 adapter->ip_offload_ctrl_tok =
4212 dma_map_single(dev,
4213 ctrl_buf,
4214 sizeof(adapter->ip_offload_ctrl),
4215 DMA_TO_DEVICE);
4216
4217 if (dma_mapping_error(dev, adapter->ip_offload_ctrl_tok)) {
4218 dev_err(dev, "Couldn't map ip offload control buffer\n");
4219 return;
4220 }
4221
4222 ctrl_buf->len = cpu_to_be32(sizeof(adapter->ip_offload_ctrl));
4223 ctrl_buf->version = cpu_to_be32(INITIAL_VERSION_IOB);
4224 ctrl_buf->ipv4_chksum = buf->ipv4_chksum;
4225 ctrl_buf->ipv6_chksum = buf->ipv6_chksum;
4226 ctrl_buf->tcp_ipv4_chksum = buf->tcp_ipv4_chksum;
4227 ctrl_buf->udp_ipv4_chksum = buf->udp_ipv4_chksum;
4228 ctrl_buf->tcp_ipv6_chksum = buf->tcp_ipv6_chksum;
4229 ctrl_buf->udp_ipv6_chksum = buf->udp_ipv6_chksum;
4230 ctrl_buf->large_tx_ipv4 = buf->large_tx_ipv4;
4231 ctrl_buf->large_tx_ipv6 = buf->large_tx_ipv6;
4232
4233 /* large_rx disabled for now, additional features needed */
4234 ctrl_buf->large_rx_ipv4 = 0;
4235 ctrl_buf->large_rx_ipv6 = 0;
4236
4237 if (adapter->state != VNIC_PROBING) {
4238 old_hw_features = adapter->netdev->hw_features;
4239 adapter->netdev->hw_features = 0;
4240 }
4241
4242 adapter->netdev->hw_features = NETIF_F_SG | NETIF_F_GSO | NETIF_F_GRO;
4243
4244 if (buf->tcp_ipv4_chksum || buf->udp_ipv4_chksum)
4245 adapter->netdev->hw_features |= NETIF_F_IP_CSUM;
4246
4247 if (buf->tcp_ipv6_chksum || buf->udp_ipv6_chksum)
4248 adapter->netdev->hw_features |= NETIF_F_IPV6_CSUM;
4249
4250 if ((adapter->netdev->features &
4251 (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)))
4252 adapter->netdev->hw_features |= NETIF_F_RXCSUM;
4253
4254 if (buf->large_tx_ipv4)
4255 adapter->netdev->hw_features |= NETIF_F_TSO;
4256 if (buf->large_tx_ipv6)
4257 adapter->netdev->hw_features |= NETIF_F_TSO6;
4258
4259 if (adapter->state == VNIC_PROBING) {
4260 adapter->netdev->features |= adapter->netdev->hw_features;
4261 } else if (old_hw_features != adapter->netdev->hw_features) {
4262 netdev_features_t tmp = 0;
4263
4264 /* disable features no longer supported */
4265 adapter->netdev->features &= adapter->netdev->hw_features;
4266 /* turn on features now supported if previously enabled */
4267 tmp = (old_hw_features ^ adapter->netdev->hw_features) &
4268 adapter->netdev->hw_features;
4269 adapter->netdev->features |=
4270 tmp & adapter->netdev->wanted_features;
4271 }
4272
4273 memset(&crq, 0, sizeof(crq));
4274 crq.control_ip_offload.first = IBMVNIC_CRQ_CMD;
4275 crq.control_ip_offload.cmd = CONTROL_IP_OFFLOAD;
4276 crq.control_ip_offload.len =
4277 cpu_to_be32(sizeof(adapter->ip_offload_ctrl));
4278 crq.control_ip_offload.ioba = cpu_to_be32(adapter->ip_offload_ctrl_tok);
4279 ibmvnic_send_crq(adapter, &crq);
4280}
4281
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02004282static void handle_vpd_size_rsp(union ibmvnic_crq *crq,
4283 struct ibmvnic_adapter *adapter)
4284{
4285 struct device *dev = &adapter->vdev->dev;
4286
4287 if (crq->get_vpd_size_rsp.rc.code) {
4288 dev_err(dev, "Error retrieving VPD size, rc=%x\n",
4289 crq->get_vpd_size_rsp.rc.code);
4290 complete(&adapter->fw_done);
4291 return;
4292 }
4293
4294 adapter->vpd->len = be64_to_cpu(crq->get_vpd_size_rsp.len);
4295 complete(&adapter->fw_done);
4296}
4297
4298static void handle_vpd_rsp(union ibmvnic_crq *crq,
4299 struct ibmvnic_adapter *adapter)
4300{
4301 struct device *dev = &adapter->vdev->dev;
Desnes Augusto Nunes do Rosario21a25452018-02-05 14:33:55 -02004302 unsigned char *substr = NULL;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02004303 u8 fw_level_len = 0;
4304
4305 memset(adapter->fw_version, 0, 32);
4306
4307 dma_unmap_single(dev, adapter->vpd->dma_addr, adapter->vpd->len,
4308 DMA_FROM_DEVICE);
4309
4310 if (crq->get_vpd_rsp.rc.code) {
4311 dev_err(dev, "Error retrieving VPD from device, rc=%x\n",
4312 crq->get_vpd_rsp.rc.code);
4313 goto complete;
4314 }
4315
4316 /* get the position of the firmware version info
4317 * located after the ASCII 'RM' substring in the buffer
4318 */
4319 substr = strnstr(adapter->vpd->buff, "RM", adapter->vpd->len);
4320 if (!substr) {
Desnes Augusto Nunes do Rosarioa1073112018-02-01 16:04:30 -02004321 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 -02004322 goto complete;
4323 }
4324
4325 /* get length of firmware level ASCII substring */
4326 if ((substr + 2) < (adapter->vpd->buff + adapter->vpd->len)) {
4327 fw_level_len = *(substr + 2);
4328 } else {
4329 dev_info(dev, "Length of FW substr extrapolated VDP buff\n");
4330 goto complete;
4331 }
4332
4333 /* copy firmware version string from vpd into adapter */
4334 if ((substr + 3 + fw_level_len) <
4335 (adapter->vpd->buff + adapter->vpd->len)) {
Desnes Augusto Nunes do Rosario21a25452018-02-05 14:33:55 -02004336 strncpy((char *)adapter->fw_version, substr + 3, fw_level_len);
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02004337 } else {
4338 dev_info(dev, "FW substr extrapolated VPD buff\n");
4339 }
4340
4341complete:
Desnes Augusto Nunes do Rosario21a25452018-02-05 14:33:55 -02004342 if (adapter->fw_version[0] == '\0')
4343 strncpy((char *)adapter->fw_version, "N/A", 3 * sizeof(char));
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02004344 complete(&adapter->fw_done);
4345}
4346
Thomas Falcon032c5e82015-12-21 11:26:06 -06004347static void handle_query_ip_offload_rsp(struct ibmvnic_adapter *adapter)
4348{
4349 struct device *dev = &adapter->vdev->dev;
4350 struct ibmvnic_query_ip_offload_buffer *buf = &adapter->ip_offload_buf;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004351 int i;
4352
4353 dma_unmap_single(dev, adapter->ip_offload_tok,
4354 sizeof(adapter->ip_offload_buf), DMA_FROM_DEVICE);
4355
4356 netdev_dbg(adapter->netdev, "Query IP Offload Buffer:\n");
4357 for (i = 0; i < (sizeof(adapter->ip_offload_buf) - 1) / 8 + 1; i++)
4358 netdev_dbg(adapter->netdev, "%016lx\n",
Lijun Pan429aa362021-02-11 00:43:18 -06004359 ((unsigned long *)(buf))[i]);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004360
4361 netdev_dbg(adapter->netdev, "ipv4_chksum = %d\n", buf->ipv4_chksum);
4362 netdev_dbg(adapter->netdev, "ipv6_chksum = %d\n", buf->ipv6_chksum);
4363 netdev_dbg(adapter->netdev, "tcp_ipv4_chksum = %d\n",
4364 buf->tcp_ipv4_chksum);
4365 netdev_dbg(adapter->netdev, "tcp_ipv6_chksum = %d\n",
4366 buf->tcp_ipv6_chksum);
4367 netdev_dbg(adapter->netdev, "udp_ipv4_chksum = %d\n",
4368 buf->udp_ipv4_chksum);
4369 netdev_dbg(adapter->netdev, "udp_ipv6_chksum = %d\n",
4370 buf->udp_ipv6_chksum);
4371 netdev_dbg(adapter->netdev, "large_tx_ipv4 = %d\n",
4372 buf->large_tx_ipv4);
4373 netdev_dbg(adapter->netdev, "large_tx_ipv6 = %d\n",
4374 buf->large_tx_ipv6);
4375 netdev_dbg(adapter->netdev, "large_rx_ipv4 = %d\n",
4376 buf->large_rx_ipv4);
4377 netdev_dbg(adapter->netdev, "large_rx_ipv6 = %d\n",
4378 buf->large_rx_ipv6);
4379 netdev_dbg(adapter->netdev, "max_ipv4_hdr_sz = %d\n",
4380 buf->max_ipv4_header_size);
4381 netdev_dbg(adapter->netdev, "max_ipv6_hdr_sz = %d\n",
4382 buf->max_ipv6_header_size);
4383 netdev_dbg(adapter->netdev, "max_tcp_hdr_size = %d\n",
4384 buf->max_tcp_header_size);
4385 netdev_dbg(adapter->netdev, "max_udp_hdr_size = %d\n",
4386 buf->max_udp_header_size);
4387 netdev_dbg(adapter->netdev, "max_large_tx_size = %d\n",
4388 buf->max_large_tx_size);
4389 netdev_dbg(adapter->netdev, "max_large_rx_size = %d\n",
4390 buf->max_large_rx_size);
4391 netdev_dbg(adapter->netdev, "ipv6_ext_hdr = %d\n",
4392 buf->ipv6_extension_header);
4393 netdev_dbg(adapter->netdev, "tcp_pseudosum_req = %d\n",
4394 buf->tcp_pseudosum_req);
4395 netdev_dbg(adapter->netdev, "num_ipv6_ext_hd = %d\n",
4396 buf->num_ipv6_ext_headers);
4397 netdev_dbg(adapter->netdev, "off_ipv6_ext_hd = %d\n",
4398 buf->off_ipv6_ext_headers);
4399
Lijun Pan46899bd2020-09-27 20:13:30 -05004400 send_control_ip_offload(adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004401}
4402
Thomas Falconc9008d32018-08-06 21:39:59 -05004403static const char *ibmvnic_fw_err_cause(u16 cause)
4404{
4405 switch (cause) {
4406 case ADAPTER_PROBLEM:
4407 return "adapter problem";
4408 case BUS_PROBLEM:
4409 return "bus problem";
4410 case FW_PROBLEM:
4411 return "firmware problem";
4412 case DD_PROBLEM:
4413 return "device driver problem";
4414 case EEH_RECOVERY:
4415 return "EEH recovery";
4416 case FW_UPDATED:
4417 return "firmware updated";
4418 case LOW_MEMORY:
4419 return "low Memory";
4420 default:
4421 return "unknown";
4422 }
4423}
4424
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04004425static void handle_error_indication(union ibmvnic_crq *crq,
4426 struct ibmvnic_adapter *adapter)
4427{
4428 struct device *dev = &adapter->vdev->dev;
Thomas Falconc9008d32018-08-06 21:39:59 -05004429 u16 cause;
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04004430
Thomas Falconc9008d32018-08-06 21:39:59 -05004431 cause = be16_to_cpu(crq->error_indication.error_cause);
4432
4433 dev_warn_ratelimited(dev,
4434 "Firmware reports %serror, cause: %s. Starting recovery...\n",
4435 crq->error_indication.flags
4436 & IBMVNIC_FATAL_ERROR ? "FATAL " : "",
4437 ibmvnic_fw_err_cause(cause));
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04004438
Nathan Fontenoted651a12017-05-03 14:04:38 -04004439 if (crq->error_indication.flags & IBMVNIC_FATAL_ERROR)
4440 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
John Allen8cb31cf2017-05-26 10:30:37 -04004441 else
4442 ibmvnic_reset(adapter, VNIC_RESET_NON_FATAL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004443}
4444
Thomas Falconf8136142018-01-29 13:45:05 -06004445static int handle_change_mac_rsp(union ibmvnic_crq *crq,
4446 struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06004447{
4448 struct net_device *netdev = adapter->netdev;
4449 struct device *dev = &adapter->vdev->dev;
4450 long rc;
4451
4452 rc = crq->change_mac_addr_rsp.rc.code;
4453 if (rc) {
4454 dev_err(dev, "Error %ld in CHANGE_MAC_ADDR_RSP\n", rc);
Thomas Falconf8136142018-01-29 13:45:05 -06004455 goto out;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004456 }
Lijun Pand9b0e592020-10-20 17:39:19 -05004457 /* crq->change_mac_addr.mac_addr is the requested one
4458 * crq->change_mac_addr_rsp.mac_addr is the returned valid one.
4459 */
Thomas Falcon62740e92019-05-09 23:13:43 -05004460 ether_addr_copy(netdev->dev_addr,
4461 &crq->change_mac_addr_rsp.mac_addr[0]);
Lijun Pand9b0e592020-10-20 17:39:19 -05004462 ether_addr_copy(adapter->mac_addr,
4463 &crq->change_mac_addr_rsp.mac_addr[0]);
Thomas Falconf8136142018-01-29 13:45:05 -06004464out:
4465 complete(&adapter->fw_done);
4466 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004467}
4468
4469static void handle_request_cap_rsp(union ibmvnic_crq *crq,
4470 struct ibmvnic_adapter *adapter)
4471{
4472 struct device *dev = &adapter->vdev->dev;
4473 u64 *req_value;
4474 char *name;
4475
Thomas Falcon901e0402017-02-15 12:17:59 -06004476 atomic_dec(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004477 switch (be16_to_cpu(crq->request_capability_rsp.capability)) {
4478 case REQ_TX_QUEUES:
4479 req_value = &adapter->req_tx_queues;
4480 name = "tx";
4481 break;
4482 case REQ_RX_QUEUES:
4483 req_value = &adapter->req_rx_queues;
4484 name = "rx";
4485 break;
4486 case REQ_RX_ADD_QUEUES:
4487 req_value = &adapter->req_rx_add_queues;
4488 name = "rx_add";
4489 break;
4490 case REQ_TX_ENTRIES_PER_SUBCRQ:
4491 req_value = &adapter->req_tx_entries_per_subcrq;
4492 name = "tx_entries_per_subcrq";
4493 break;
4494 case REQ_RX_ADD_ENTRIES_PER_SUBCRQ:
4495 req_value = &adapter->req_rx_add_entries_per_subcrq;
4496 name = "rx_add_entries_per_subcrq";
4497 break;
4498 case REQ_MTU:
4499 req_value = &adapter->req_mtu;
4500 name = "mtu";
4501 break;
4502 case PROMISC_REQUESTED:
4503 req_value = &adapter->promisc;
4504 name = "promisc";
4505 break;
4506 default:
4507 dev_err(dev, "Got invalid cap request rsp %d\n",
4508 crq->request_capability.capability);
4509 return;
4510 }
4511
4512 switch (crq->request_capability_rsp.rc.code) {
4513 case SUCCESS:
4514 break;
4515 case PARTIALSUCCESS:
4516 dev_info(dev, "req=%lld, rsp=%ld in %s queue, retrying.\n",
4517 *req_value,
Lijun Pan914789a2021-02-11 00:43:21 -06004518 (long)be64_to_cpu(crq->request_capability_rsp.number),
4519 name);
John Allene7913802018-01-18 16:27:12 -06004520
4521 if (be16_to_cpu(crq->request_capability_rsp.capability) ==
4522 REQ_MTU) {
4523 pr_err("mtu of %llu is not supported. Reverting.\n",
4524 *req_value);
4525 *req_value = adapter->fallback.mtu;
4526 } else {
4527 *req_value =
4528 be64_to_cpu(crq->request_capability_rsp.number);
4529 }
4530
Lijun Pan09081b92020-09-27 20:13:27 -05004531 send_request_cap(adapter, 1);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004532 return;
4533 default:
4534 dev_err(dev, "Error %d in request cap rsp\n",
4535 crq->request_capability_rsp.rc.code);
4536 return;
4537 }
4538
4539 /* Done receiving requested capabilities, query IP offload support */
Thomas Falcon901e0402017-02-15 12:17:59 -06004540 if (atomic_read(&adapter->running_cap_crqs) == 0) {
Thomas Falcon249168a2017-02-15 12:18:00 -06004541 adapter->wait_capability = false;
Lijun Pan16e811f2020-09-27 20:13:29 -05004542 send_query_ip_offload(adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004543 }
4544}
4545
4546static int handle_login_rsp(union ibmvnic_crq *login_rsp_crq,
4547 struct ibmvnic_adapter *adapter)
4548{
4549 struct device *dev = &adapter->vdev->dev;
John Allenc26eba02017-10-26 16:23:25 -05004550 struct net_device *netdev = adapter->netdev;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004551 struct ibmvnic_login_rsp_buffer *login_rsp = adapter->login_rsp_buf;
4552 struct ibmvnic_login_buffer *login = adapter->login_buf;
Cristobal Fornof3ae59c2020-08-19 13:16:23 -05004553 u64 *tx_handle_array;
4554 u64 *rx_handle_array;
4555 int num_tx_pools;
4556 int num_rx_pools;
Thomas Falcon507ebe62020-08-21 13:39:01 -05004557 u64 *size_array;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004558 int i;
4559
Sukadev Bhattiprolu76cdc5c2020-11-25 18:04:29 -06004560 /* CHECK: Test/set of login_pending does not need to be atomic
4561 * because only ibmvnic_tasklet tests/clears this.
4562 */
4563 if (!adapter->login_pending) {
4564 netdev_warn(netdev, "Ignoring unexpected login response\n");
4565 return 0;
4566 }
4567 adapter->login_pending = false;
4568
Thomas Falcon032c5e82015-12-21 11:26:06 -06004569 dma_unmap_single(dev, adapter->login_buf_token, adapter->login_buf_sz,
Thomas Falcon37e40fa2018-04-06 18:37:02 -05004570 DMA_TO_DEVICE);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004571 dma_unmap_single(dev, adapter->login_rsp_buf_token,
Thomas Falcon37e40fa2018-04-06 18:37:02 -05004572 adapter->login_rsp_buf_sz, DMA_FROM_DEVICE);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004573
John Allen498cd8e2016-04-06 11:49:55 -05004574 /* If the number of queues requested can't be allocated by the
4575 * server, the login response will return with code 1. We will need
4576 * to resend the login buffer with fewer queues requested.
4577 */
4578 if (login_rsp_crq->generic.rc.code) {
Nathan Fontenot64d92aa2018-04-11 10:09:32 -05004579 adapter->init_done_rc = login_rsp_crq->generic.rc.code;
John Allen498cd8e2016-04-06 11:49:55 -05004580 complete(&adapter->init_done);
4581 return 0;
4582 }
4583
John Allenc26eba02017-10-26 16:23:25 -05004584 netdev->mtu = adapter->req_mtu - ETH_HLEN;
4585
Thomas Falcon032c5e82015-12-21 11:26:06 -06004586 netdev_dbg(adapter->netdev, "Login Response Buffer:\n");
4587 for (i = 0; i < (adapter->login_rsp_buf_sz - 1) / 8 + 1; i++) {
4588 netdev_dbg(adapter->netdev, "%016lx\n",
Lijun Pan429aa362021-02-11 00:43:18 -06004589 ((unsigned long *)(adapter->login_rsp_buf))[i]);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004590 }
4591
4592 /* Sanity checks */
4593 if (login->num_txcomp_subcrqs != login_rsp->num_txsubm_subcrqs ||
4594 (be32_to_cpu(login->num_rxcomp_subcrqs) *
4595 adapter->req_rx_add_queues !=
4596 be32_to_cpu(login_rsp->num_rxadd_subcrqs))) {
4597 dev_err(dev, "FATAL: Inconsistent login and login rsp\n");
Dany Madden31d6b402020-11-25 18:04:24 -06004598 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004599 return -EIO;
4600 }
Thomas Falcon507ebe62020-08-21 13:39:01 -05004601 size_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
4602 be32_to_cpu(adapter->login_rsp_buf->off_rxadd_buff_size));
4603 /* variable buffer sizes are not supported, so just read the
4604 * first entry.
4605 */
4606 adapter->cur_rx_buf_sz = be64_to_cpu(size_array[0]);
Cristobal Fornof3ae59c2020-08-19 13:16:23 -05004607
4608 num_tx_pools = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
4609 num_rx_pools = be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
4610
4611 tx_handle_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
4612 be32_to_cpu(adapter->login_rsp_buf->off_txsubm_subcrqs));
4613 rx_handle_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
4614 be32_to_cpu(adapter->login_rsp_buf->off_rxadd_subcrqs));
4615
4616 for (i = 0; i < num_tx_pools; i++)
4617 adapter->tx_scrq[i]->handle = tx_handle_array[i];
4618
4619 for (i = 0; i < num_rx_pools; i++)
4620 adapter->rx_scrq[i]->handle = rx_handle_array[i];
4621
Thomas Falcon507ebe62020-08-21 13:39:01 -05004622 adapter->num_active_tx_scrqs = num_tx_pools;
4623 adapter->num_active_rx_scrqs = num_rx_pools;
Cristobal Fornof3ae59c2020-08-19 13:16:23 -05004624 release_login_rsp_buffer(adapter);
Thomas Falcona2c0f032018-02-21 18:18:30 -06004625 release_login_buffer(adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004626 complete(&adapter->init_done);
4627
Thomas Falcon032c5e82015-12-21 11:26:06 -06004628 return 0;
4629}
4630
Thomas Falcon032c5e82015-12-21 11:26:06 -06004631static void handle_request_unmap_rsp(union ibmvnic_crq *crq,
4632 struct ibmvnic_adapter *adapter)
4633{
4634 struct device *dev = &adapter->vdev->dev;
4635 long rc;
4636
4637 rc = crq->request_unmap_rsp.rc.code;
4638 if (rc)
4639 dev_err(dev, "Error %ld in REQUEST_UNMAP_RSP\n", rc);
4640}
4641
4642static void handle_query_map_rsp(union ibmvnic_crq *crq,
4643 struct ibmvnic_adapter *adapter)
4644{
4645 struct net_device *netdev = adapter->netdev;
4646 struct device *dev = &adapter->vdev->dev;
4647 long rc;
4648
4649 rc = crq->query_map_rsp.rc.code;
4650 if (rc) {
4651 dev_err(dev, "Error %ld in QUERY_MAP_RSP\n", rc);
4652 return;
4653 }
4654 netdev_dbg(netdev, "page_size = %d\ntot_pages = %d\nfree_pages = %d\n",
4655 crq->query_map_rsp.page_size, crq->query_map_rsp.tot_pages,
4656 crq->query_map_rsp.free_pages);
4657}
4658
4659static void handle_query_cap_rsp(union ibmvnic_crq *crq,
4660 struct ibmvnic_adapter *adapter)
4661{
4662 struct net_device *netdev = adapter->netdev;
4663 struct device *dev = &adapter->vdev->dev;
4664 long rc;
4665
Thomas Falcon901e0402017-02-15 12:17:59 -06004666 atomic_dec(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004667 netdev_dbg(netdev, "Outstanding queries: %d\n",
Thomas Falcon901e0402017-02-15 12:17:59 -06004668 atomic_read(&adapter->running_cap_crqs));
Thomas Falcon032c5e82015-12-21 11:26:06 -06004669 rc = crq->query_capability.rc.code;
4670 if (rc) {
4671 dev_err(dev, "Error %ld in QUERY_CAP_RSP\n", rc);
4672 goto out;
4673 }
4674
4675 switch (be16_to_cpu(crq->query_capability.capability)) {
4676 case MIN_TX_QUEUES:
4677 adapter->min_tx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004678 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004679 netdev_dbg(netdev, "min_tx_queues = %lld\n",
4680 adapter->min_tx_queues);
4681 break;
4682 case MIN_RX_QUEUES:
4683 adapter->min_rx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004684 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004685 netdev_dbg(netdev, "min_rx_queues = %lld\n",
4686 adapter->min_rx_queues);
4687 break;
4688 case MIN_RX_ADD_QUEUES:
4689 adapter->min_rx_add_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004690 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004691 netdev_dbg(netdev, "min_rx_add_queues = %lld\n",
4692 adapter->min_rx_add_queues);
4693 break;
4694 case MAX_TX_QUEUES:
4695 adapter->max_tx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004696 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004697 netdev_dbg(netdev, "max_tx_queues = %lld\n",
4698 adapter->max_tx_queues);
4699 break;
4700 case MAX_RX_QUEUES:
4701 adapter->max_rx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004702 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004703 netdev_dbg(netdev, "max_rx_queues = %lld\n",
4704 adapter->max_rx_queues);
4705 break;
4706 case MAX_RX_ADD_QUEUES:
4707 adapter->max_rx_add_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004708 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004709 netdev_dbg(netdev, "max_rx_add_queues = %lld\n",
4710 adapter->max_rx_add_queues);
4711 break;
4712 case MIN_TX_ENTRIES_PER_SUBCRQ:
4713 adapter->min_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06004714 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004715 netdev_dbg(netdev, "min_tx_entries_per_subcrq = %lld\n",
4716 adapter->min_tx_entries_per_subcrq);
4717 break;
4718 case MIN_RX_ADD_ENTRIES_PER_SUBCRQ:
4719 adapter->min_rx_add_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06004720 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004721 netdev_dbg(netdev, "min_rx_add_entrs_per_subcrq = %lld\n",
4722 adapter->min_rx_add_entries_per_subcrq);
4723 break;
4724 case MAX_TX_ENTRIES_PER_SUBCRQ:
4725 adapter->max_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06004726 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004727 netdev_dbg(netdev, "max_tx_entries_per_subcrq = %lld\n",
4728 adapter->max_tx_entries_per_subcrq);
4729 break;
4730 case MAX_RX_ADD_ENTRIES_PER_SUBCRQ:
4731 adapter->max_rx_add_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06004732 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004733 netdev_dbg(netdev, "max_rx_add_entrs_per_subcrq = %lld\n",
4734 adapter->max_rx_add_entries_per_subcrq);
4735 break;
4736 case TCP_IP_OFFLOAD:
4737 adapter->tcp_ip_offload =
Thomas Falconde89e852016-03-01 10:20:09 -06004738 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004739 netdev_dbg(netdev, "tcp_ip_offload = %lld\n",
4740 adapter->tcp_ip_offload);
4741 break;
4742 case PROMISC_SUPPORTED:
4743 adapter->promisc_supported =
Thomas Falconde89e852016-03-01 10:20:09 -06004744 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004745 netdev_dbg(netdev, "promisc_supported = %lld\n",
4746 adapter->promisc_supported);
4747 break;
4748 case MIN_MTU:
Thomas Falconde89e852016-03-01 10:20:09 -06004749 adapter->min_mtu = be64_to_cpu(crq->query_capability.number);
Thomas Falconf39f0d12017-02-14 10:22:59 -06004750 netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004751 netdev_dbg(netdev, "min_mtu = %lld\n", adapter->min_mtu);
4752 break;
4753 case MAX_MTU:
Thomas Falconde89e852016-03-01 10:20:09 -06004754 adapter->max_mtu = be64_to_cpu(crq->query_capability.number);
Thomas Falconf39f0d12017-02-14 10:22:59 -06004755 netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004756 netdev_dbg(netdev, "max_mtu = %lld\n", adapter->max_mtu);
4757 break;
4758 case MAX_MULTICAST_FILTERS:
4759 adapter->max_multicast_filters =
Thomas Falconde89e852016-03-01 10:20:09 -06004760 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004761 netdev_dbg(netdev, "max_multicast_filters = %lld\n",
4762 adapter->max_multicast_filters);
4763 break;
4764 case VLAN_HEADER_INSERTION:
4765 adapter->vlan_header_insertion =
Thomas Falconde89e852016-03-01 10:20:09 -06004766 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004767 if (adapter->vlan_header_insertion)
4768 netdev->features |= NETIF_F_HW_VLAN_STAG_TX;
4769 netdev_dbg(netdev, "vlan_header_insertion = %lld\n",
4770 adapter->vlan_header_insertion);
4771 break;
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04004772 case RX_VLAN_HEADER_INSERTION:
4773 adapter->rx_vlan_header_insertion =
4774 be64_to_cpu(crq->query_capability.number);
4775 netdev_dbg(netdev, "rx_vlan_header_insertion = %lld\n",
4776 adapter->rx_vlan_header_insertion);
4777 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004778 case MAX_TX_SG_ENTRIES:
4779 adapter->max_tx_sg_entries =
Thomas Falconde89e852016-03-01 10:20:09 -06004780 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004781 netdev_dbg(netdev, "max_tx_sg_entries = %lld\n",
4782 adapter->max_tx_sg_entries);
4783 break;
4784 case RX_SG_SUPPORTED:
4785 adapter->rx_sg_supported =
Thomas Falconde89e852016-03-01 10:20:09 -06004786 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004787 netdev_dbg(netdev, "rx_sg_supported = %lld\n",
4788 adapter->rx_sg_supported);
4789 break;
4790 case OPT_TX_COMP_SUB_QUEUES:
4791 adapter->opt_tx_comp_sub_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004792 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004793 netdev_dbg(netdev, "opt_tx_comp_sub_queues = %lld\n",
4794 adapter->opt_tx_comp_sub_queues);
4795 break;
4796 case OPT_RX_COMP_QUEUES:
4797 adapter->opt_rx_comp_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004798 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004799 netdev_dbg(netdev, "opt_rx_comp_queues = %lld\n",
4800 adapter->opt_rx_comp_queues);
4801 break;
4802 case OPT_RX_BUFADD_Q_PER_RX_COMP_Q:
4803 adapter->opt_rx_bufadd_q_per_rx_comp_q =
Thomas Falconde89e852016-03-01 10:20:09 -06004804 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004805 netdev_dbg(netdev, "opt_rx_bufadd_q_per_rx_comp_q = %lld\n",
4806 adapter->opt_rx_bufadd_q_per_rx_comp_q);
4807 break;
4808 case OPT_TX_ENTRIES_PER_SUBCRQ:
4809 adapter->opt_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06004810 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004811 netdev_dbg(netdev, "opt_tx_entries_per_subcrq = %lld\n",
4812 adapter->opt_tx_entries_per_subcrq);
4813 break;
4814 case OPT_RXBA_ENTRIES_PER_SUBCRQ:
4815 adapter->opt_rxba_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06004816 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004817 netdev_dbg(netdev, "opt_rxba_entries_per_subcrq = %lld\n",
4818 adapter->opt_rxba_entries_per_subcrq);
4819 break;
4820 case TX_RX_DESC_REQ:
4821 adapter->tx_rx_desc_req = crq->query_capability.number;
4822 netdev_dbg(netdev, "tx_rx_desc_req = %llx\n",
4823 adapter->tx_rx_desc_req);
4824 break;
4825
4826 default:
4827 netdev_err(netdev, "Got invalid cap rsp %d\n",
4828 crq->query_capability.capability);
4829 }
4830
4831out:
Thomas Falcon249168a2017-02-15 12:18:00 -06004832 if (atomic_read(&adapter->running_cap_crqs) == 0) {
4833 adapter->wait_capability = false;
Lijun Pan09081b92020-09-27 20:13:27 -05004834 send_request_cap(adapter, 0);
Thomas Falcon249168a2017-02-15 12:18:00 -06004835 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06004836}
4837
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03004838static int send_query_phys_parms(struct ibmvnic_adapter *adapter)
4839{
4840 union ibmvnic_crq crq;
4841 int rc;
4842
4843 memset(&crq, 0, sizeof(crq));
4844 crq.query_phys_parms.first = IBMVNIC_CRQ_CMD;
4845 crq.query_phys_parms.cmd = QUERY_PHYS_PARMS;
Thomas Falconff25dcb2019-11-25 17:12:56 -06004846
4847 mutex_lock(&adapter->fw_lock);
4848 adapter->fw_done_rc = 0;
Thomas Falcon070eca92019-11-25 17:12:53 -06004849 reinit_completion(&adapter->fw_done);
Thomas Falconff25dcb2019-11-25 17:12:56 -06004850
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03004851 rc = ibmvnic_send_crq(adapter, &crq);
Thomas Falconff25dcb2019-11-25 17:12:56 -06004852 if (rc) {
4853 mutex_unlock(&adapter->fw_lock);
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03004854 return rc;
Thomas Falconff25dcb2019-11-25 17:12:56 -06004855 }
Thomas Falcon476d96c2019-11-25 17:12:55 -06004856
4857 rc = ibmvnic_wait_for_completion(adapter, &adapter->fw_done, 10000);
Thomas Falconff25dcb2019-11-25 17:12:56 -06004858 if (rc) {
4859 mutex_unlock(&adapter->fw_lock);
Thomas Falcon476d96c2019-11-25 17:12:55 -06004860 return rc;
Thomas Falconff25dcb2019-11-25 17:12:56 -06004861 }
Thomas Falcon476d96c2019-11-25 17:12:55 -06004862
Thomas Falconff25dcb2019-11-25 17:12:56 -06004863 mutex_unlock(&adapter->fw_lock);
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03004864 return adapter->fw_done_rc ? -EIO : 0;
4865}
4866
4867static int handle_query_phys_parms_rsp(union ibmvnic_crq *crq,
4868 struct ibmvnic_adapter *adapter)
4869{
4870 struct net_device *netdev = adapter->netdev;
4871 int rc;
Murilo Fossa Vicentinidd0f9d82019-09-16 11:50:37 -03004872 __be32 rspeed = cpu_to_be32(crq->query_phys_parms_rsp.speed);
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03004873
4874 rc = crq->query_phys_parms_rsp.rc.code;
4875 if (rc) {
4876 netdev_err(netdev, "Error %d in QUERY_PHYS_PARMS\n", rc);
4877 return rc;
4878 }
Murilo Fossa Vicentinidd0f9d82019-09-16 11:50:37 -03004879 switch (rspeed) {
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03004880 case IBMVNIC_10MBPS:
4881 adapter->speed = SPEED_10;
4882 break;
4883 case IBMVNIC_100MBPS:
4884 adapter->speed = SPEED_100;
4885 break;
4886 case IBMVNIC_1GBPS:
4887 adapter->speed = SPEED_1000;
4888 break;
Lijun Panb9cd7952020-09-27 19:06:25 -05004889 case IBMVNIC_10GBPS:
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03004890 adapter->speed = SPEED_10000;
4891 break;
4892 case IBMVNIC_25GBPS:
4893 adapter->speed = SPEED_25000;
4894 break;
4895 case IBMVNIC_40GBPS:
4896 adapter->speed = SPEED_40000;
4897 break;
4898 case IBMVNIC_50GBPS:
4899 adapter->speed = SPEED_50000;
4900 break;
4901 case IBMVNIC_100GBPS:
4902 adapter->speed = SPEED_100000;
4903 break;
Lijun Panb9cd7952020-09-27 19:06:25 -05004904 case IBMVNIC_200GBPS:
4905 adapter->speed = SPEED_200000;
4906 break;
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03004907 default:
Murilo Fossa Vicentinidd0f9d82019-09-16 11:50:37 -03004908 if (netif_carrier_ok(netdev))
4909 netdev_warn(netdev, "Unknown speed 0x%08x\n", rspeed);
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03004910 adapter->speed = SPEED_UNKNOWN;
4911 }
4912 if (crq->query_phys_parms_rsp.flags1 & IBMVNIC_FULL_DUPLEX)
4913 adapter->duplex = DUPLEX_FULL;
4914 else if (crq->query_phys_parms_rsp.flags1 & IBMVNIC_HALF_DUPLEX)
4915 adapter->duplex = DUPLEX_HALF;
4916 else
4917 adapter->duplex = DUPLEX_UNKNOWN;
4918
4919 return rc;
4920}
4921
Thomas Falcon032c5e82015-12-21 11:26:06 -06004922static void ibmvnic_handle_crq(union ibmvnic_crq *crq,
4923 struct ibmvnic_adapter *adapter)
4924{
4925 struct ibmvnic_generic_crq *gen_crq = &crq->generic;
4926 struct net_device *netdev = adapter->netdev;
4927 struct device *dev = &adapter->vdev->dev;
Murilo Fossa Vicentini993a82b2017-04-19 13:44:35 -04004928 u64 *u64_crq = (u64 *)crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004929 long rc;
4930
4931 netdev_dbg(netdev, "Handling CRQ: %016lx %016lx\n",
Lijun Pan429aa362021-02-11 00:43:18 -06004932 (unsigned long)cpu_to_be64(u64_crq[0]),
4933 (unsigned long)cpu_to_be64(u64_crq[1]));
Thomas Falcon032c5e82015-12-21 11:26:06 -06004934 switch (gen_crq->first) {
4935 case IBMVNIC_CRQ_INIT_RSP:
4936 switch (gen_crq->cmd) {
4937 case IBMVNIC_CRQ_INIT:
4938 dev_info(dev, "Partner initialized\n");
John Allen017892c12017-05-26 10:30:19 -04004939 adapter->from_passive_init = true;
Sukadev Bhattiprolu76cdc5c2020-11-25 18:04:29 -06004940 /* Discard any stale login responses from prev reset.
4941 * CHECK: should we clear even on INIT_COMPLETE?
4942 */
4943 adapter->login_pending = false;
4944
Thomas Falcon17c87052018-05-23 13:37:58 -05004945 if (!completion_done(&adapter->init_done)) {
4946 complete(&adapter->init_done);
4947 adapter->init_done_rc = -EIO;
4948 }
Sukadev Bhattiproluef66a1e2021-02-02 21:08:02 -08004949 rc = ibmvnic_reset(adapter, VNIC_RESET_FAILOVER);
4950 if (rc && rc != -EBUSY) {
4951 /* We were unable to schedule the failover
4952 * reset either because the adapter was still
4953 * probing (eg: during kexec) or we could not
4954 * allocate memory. Clear the failover_pending
4955 * flag since no one else will. We ignore
4956 * EBUSY because it means either FAILOVER reset
4957 * is already scheduled or the adapter is
4958 * being removed.
4959 */
4960 netdev_err(netdev,
4961 "Error %ld scheduling failover reset\n",
4962 rc);
4963 adapter->failover_pending = false;
4964 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06004965 break;
4966 case IBMVNIC_CRQ_INIT_COMPLETE:
4967 dev_info(dev, "Partner initialization complete\n");
Thomas Falcon51536982018-05-23 13:37:56 -05004968 adapter->crq.active = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004969 send_version_xchg(adapter);
4970 break;
4971 default:
4972 dev_err(dev, "Unknown crq cmd: %d\n", gen_crq->cmd);
4973 }
4974 return;
4975 case IBMVNIC_CRQ_XPORT_EVENT:
Nathan Fontenoted651a12017-05-03 14:04:38 -04004976 netif_carrier_off(netdev);
Thomas Falcon51536982018-05-23 13:37:56 -05004977 adapter->crq.active = false;
Thomas Falcon2147e3d2019-11-25 17:12:54 -06004978 /* terminate any thread waiting for a response
4979 * from the device
4980 */
4981 if (!completion_done(&adapter->fw_done)) {
4982 adapter->fw_done_rc = -EIO;
4983 complete(&adapter->fw_done);
4984 }
4985 if (!completion_done(&adapter->stats_done))
4986 complete(&adapter->stats_done);
Juliet Kim7ed5b312019-09-20 16:11:23 -04004987 if (test_bit(0, &adapter->resetting))
Thomas Falcon2770a792018-05-23 13:38:02 -05004988 adapter->force_reset_recovery = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004989 if (gen_crq->cmd == IBMVNIC_PARTITION_MIGRATED) {
Nathan Fontenoted651a12017-05-03 14:04:38 -04004990 dev_info(dev, "Migrated, re-enabling adapter\n");
4991 ibmvnic_reset(adapter, VNIC_RESET_MOBILITY);
Thomas Falcondfad09a2016-08-18 11:37:51 -05004992 } else if (gen_crq->cmd == IBMVNIC_DEVICE_FAILOVER) {
4993 dev_info(dev, "Backing device failover detected\n");
Thomas Falcon5a18e1e2018-04-06 18:37:05 -05004994 adapter->failover_pending = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004995 } else {
4996 /* The adapter lost the connection */
4997 dev_err(dev, "Virtual Adapter failed (rc=%d)\n",
4998 gen_crq->cmd);
Nathan Fontenoted651a12017-05-03 14:04:38 -04004999 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005000 }
5001 return;
5002 case IBMVNIC_CRQ_CMD_RSP:
5003 break;
5004 default:
5005 dev_err(dev, "Got an invalid msg type 0x%02x\n",
5006 gen_crq->first);
5007 return;
5008 }
5009
5010 switch (gen_crq->cmd) {
5011 case VERSION_EXCHANGE_RSP:
5012 rc = crq->version_exchange_rsp.rc.code;
5013 if (rc) {
5014 dev_err(dev, "Error %ld in VERSION_EXCHG_RSP\n", rc);
5015 break;
5016 }
Thomas Falcon78468892020-05-28 11:19:17 -05005017 ibmvnic_version =
Thomas Falcon032c5e82015-12-21 11:26:06 -06005018 be16_to_cpu(crq->version_exchange_rsp.version);
Thomas Falcon78468892020-05-28 11:19:17 -05005019 dev_info(dev, "Partner protocol version is %d\n",
5020 ibmvnic_version);
Lijun Pan491099a2020-09-27 20:13:26 -05005021 send_query_cap(adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005022 break;
5023 case QUERY_CAPABILITY_RSP:
5024 handle_query_cap_rsp(crq, adapter);
5025 break;
5026 case QUERY_MAP_RSP:
5027 handle_query_map_rsp(crq, adapter);
5028 break;
5029 case REQUEST_MAP_RSP:
Thomas Falconf3be0cb2017-06-21 14:53:01 -05005030 adapter->fw_done_rc = crq->request_map_rsp.rc.code;
5031 complete(&adapter->fw_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005032 break;
5033 case REQUEST_UNMAP_RSP:
5034 handle_request_unmap_rsp(crq, adapter);
5035 break;
5036 case REQUEST_CAPABILITY_RSP:
5037 handle_request_cap_rsp(crq, adapter);
5038 break;
5039 case LOGIN_RSP:
5040 netdev_dbg(netdev, "Got Login Response\n");
5041 handle_login_rsp(crq, adapter);
5042 break;
5043 case LOGICAL_LINK_STATE_RSP:
Nathan Fontenot53da09e2017-04-21 15:39:04 -04005044 netdev_dbg(netdev,
5045 "Got Logical Link State Response, state: %d rc: %d\n",
5046 crq->logical_link_state_rsp.link_state,
5047 crq->logical_link_state_rsp.rc.code);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005048 adapter->logical_link_state =
5049 crq->logical_link_state_rsp.link_state;
Nathan Fontenot53da09e2017-04-21 15:39:04 -04005050 adapter->init_done_rc = crq->logical_link_state_rsp.rc.code;
5051 complete(&adapter->init_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005052 break;
5053 case LINK_STATE_INDICATION:
5054 netdev_dbg(netdev, "Got Logical Link State Indication\n");
5055 adapter->phys_link_state =
5056 crq->link_state_indication.phys_link_state;
5057 adapter->logical_link_state =
5058 crq->link_state_indication.logical_link_state;
Thomas Falcon0655f992019-05-09 23:13:44 -05005059 if (adapter->phys_link_state && adapter->logical_link_state)
5060 netif_carrier_on(netdev);
5061 else
5062 netif_carrier_off(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005063 break;
5064 case CHANGE_MAC_ADDR_RSP:
5065 netdev_dbg(netdev, "Got MAC address change Response\n");
Thomas Falconf8136142018-01-29 13:45:05 -06005066 adapter->fw_done_rc = handle_change_mac_rsp(crq, adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005067 break;
5068 case ERROR_INDICATION:
5069 netdev_dbg(netdev, "Got Error Indication\n");
5070 handle_error_indication(crq, adapter);
5071 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005072 case REQUEST_STATISTICS_RSP:
5073 netdev_dbg(netdev, "Got Statistics Response\n");
5074 complete(&adapter->stats_done);
5075 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005076 case QUERY_IP_OFFLOAD_RSP:
5077 netdev_dbg(netdev, "Got Query IP offload Response\n");
5078 handle_query_ip_offload_rsp(adapter);
5079 break;
5080 case MULTICAST_CTRL_RSP:
5081 netdev_dbg(netdev, "Got multicast control Response\n");
5082 break;
5083 case CONTROL_IP_OFFLOAD_RSP:
5084 netdev_dbg(netdev, "Got Control IP offload Response\n");
5085 dma_unmap_single(dev, adapter->ip_offload_ctrl_tok,
5086 sizeof(adapter->ip_offload_ctrl),
5087 DMA_TO_DEVICE);
John Allenbd0b6722017-03-17 17:13:40 -05005088 complete(&adapter->init_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005089 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005090 case COLLECT_FW_TRACE_RSP:
5091 netdev_dbg(netdev, "Got Collect firmware trace Response\n");
5092 complete(&adapter->fw_done);
5093 break;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02005094 case GET_VPD_SIZE_RSP:
5095 handle_vpd_size_rsp(crq, adapter);
5096 break;
5097 case GET_VPD_RSP:
5098 handle_vpd_rsp(crq, adapter);
5099 break;
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03005100 case QUERY_PHYS_PARMS_RSP:
5101 adapter->fw_done_rc = handle_query_phys_parms_rsp(crq, adapter);
5102 complete(&adapter->fw_done);
5103 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005104 default:
5105 netdev_err(netdev, "Got an invalid cmd type 0x%02x\n",
5106 gen_crq->cmd);
5107 }
5108}
5109
5110static irqreturn_t ibmvnic_interrupt(int irq, void *instance)
5111{
5112 struct ibmvnic_adapter *adapter = instance;
Thomas Falcon6c267b32017-02-15 12:17:58 -06005113
Thomas Falcon6c267b32017-02-15 12:17:58 -06005114 tasklet_schedule(&adapter->tasklet);
Thomas Falcon6c267b32017-02-15 12:17:58 -06005115 return IRQ_HANDLED;
5116}
5117
Allen Paisaa7c3fe2020-09-14 12:59:29 +05305118static void ibmvnic_tasklet(struct tasklet_struct *t)
Thomas Falcon6c267b32017-02-15 12:17:58 -06005119{
Allen Paisaa7c3fe2020-09-14 12:59:29 +05305120 struct ibmvnic_adapter *adapter = from_tasklet(adapter, t, tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005121 struct ibmvnic_crq_queue *queue = &adapter->crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005122 union ibmvnic_crq *crq;
5123 unsigned long flags;
5124 bool done = false;
5125
5126 spin_lock_irqsave(&queue->lock, flags);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005127 while (!done) {
5128 /* Pull all the valid messages off the CRQ */
5129 while ((crq = ibmvnic_next_crq(adapter)) != NULL) {
Lijun Pane41aec72021-01-27 19:34:42 -06005130 /* This barrier makes sure ibmvnic_next_crq()'s
5131 * crq->generic.first & IBMVNIC_CRQ_CMD_RSP is loaded
5132 * before ibmvnic_handle_crq()'s
5133 * switch(gen_crq->first) and switch(gen_crq->cmd).
5134 */
5135 dma_rmb();
Thomas Falcon032c5e82015-12-21 11:26:06 -06005136 ibmvnic_handle_crq(crq, adapter);
5137 crq->generic.first = 0;
5138 }
Brian Kinged7ecbf2017-04-19 13:44:53 -04005139
5140 /* remain in tasklet until all
5141 * capabilities responses are received
5142 */
5143 if (!adapter->wait_capability)
5144 done = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005145 }
Thomas Falcon249168a2017-02-15 12:18:00 -06005146 /* if capabilities CRQ's were sent in this tasklet, the following
5147 * tasklet must wait until all responses are received
5148 */
5149 if (atomic_read(&adapter->running_cap_crqs) != 0)
5150 adapter->wait_capability = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005151 spin_unlock_irqrestore(&queue->lock, flags);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005152}
5153
5154static int ibmvnic_reenable_crq_queue(struct ibmvnic_adapter *adapter)
5155{
5156 struct vio_dev *vdev = adapter->vdev;
5157 int rc;
5158
5159 do {
5160 rc = plpar_hcall_norets(H_ENABLE_CRQ, vdev->unit_address);
5161 } while (rc == H_IN_PROGRESS || rc == H_BUSY || H_IS_LONG_BUSY(rc));
5162
5163 if (rc)
5164 dev_err(&vdev->dev, "Error enabling adapter (rc=%d)\n", rc);
5165
5166 return rc;
5167}
5168
5169static int ibmvnic_reset_crq(struct ibmvnic_adapter *adapter)
5170{
5171 struct ibmvnic_crq_queue *crq = &adapter->crq;
5172 struct device *dev = &adapter->vdev->dev;
5173 struct vio_dev *vdev = adapter->vdev;
5174 int rc;
5175
5176 /* Close the CRQ */
5177 do {
5178 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
5179 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
5180
5181 /* Clean out the queue */
Lijun Pan0e435be2020-11-23 13:35:46 -06005182 if (!crq->msgs)
5183 return -EINVAL;
5184
Thomas Falcon032c5e82015-12-21 11:26:06 -06005185 memset(crq->msgs, 0, PAGE_SIZE);
5186 crq->cur = 0;
Thomas Falcon51536982018-05-23 13:37:56 -05005187 crq->active = false;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005188
5189 /* And re-open it again */
5190 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
5191 crq->msg_token, PAGE_SIZE);
5192
5193 if (rc == H_CLOSED)
5194 /* Adapter is good, but other end is not ready */
5195 dev_warn(dev, "Partner adapter not ready\n");
5196 else if (rc != 0)
5197 dev_warn(dev, "Couldn't register crq (rc=%d)\n", rc);
5198
5199 return rc;
5200}
5201
Nathan Fontenotf9928872017-03-30 02:48:54 -04005202static void release_crq_queue(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06005203{
5204 struct ibmvnic_crq_queue *crq = &adapter->crq;
5205 struct vio_dev *vdev = adapter->vdev;
5206 long rc;
5207
Nathan Fontenotf9928872017-03-30 02:48:54 -04005208 if (!crq->msgs)
5209 return;
5210
Thomas Falcon032c5e82015-12-21 11:26:06 -06005211 netdev_dbg(adapter->netdev, "Releasing CRQ\n");
5212 free_irq(vdev->irq, adapter);
Thomas Falcon6c267b32017-02-15 12:17:58 -06005213 tasklet_kill(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005214 do {
5215 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
5216 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
5217
5218 dma_unmap_single(&vdev->dev, crq->msg_token, PAGE_SIZE,
5219 DMA_BIDIRECTIONAL);
5220 free_page((unsigned long)crq->msgs);
Nathan Fontenotf9928872017-03-30 02:48:54 -04005221 crq->msgs = NULL;
Thomas Falcon51536982018-05-23 13:37:56 -05005222 crq->active = false;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005223}
5224
Nathan Fontenotf9928872017-03-30 02:48:54 -04005225static int init_crq_queue(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06005226{
5227 struct ibmvnic_crq_queue *crq = &adapter->crq;
5228 struct device *dev = &adapter->vdev->dev;
5229 struct vio_dev *vdev = adapter->vdev;
5230 int rc, retrc = -ENOMEM;
5231
Nathan Fontenotf9928872017-03-30 02:48:54 -04005232 if (crq->msgs)
5233 return 0;
5234
Thomas Falcon032c5e82015-12-21 11:26:06 -06005235 crq->msgs = (union ibmvnic_crq *)get_zeroed_page(GFP_KERNEL);
5236 /* Should we allocate more than one page? */
5237
5238 if (!crq->msgs)
5239 return -ENOMEM;
5240
5241 crq->size = PAGE_SIZE / sizeof(*crq->msgs);
5242 crq->msg_token = dma_map_single(dev, crq->msgs, PAGE_SIZE,
5243 DMA_BIDIRECTIONAL);
5244 if (dma_mapping_error(dev, crq->msg_token))
5245 goto map_failed;
5246
5247 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
5248 crq->msg_token, PAGE_SIZE);
5249
5250 if (rc == H_RESOURCE)
5251 /* maybe kexecing and resource is busy. try a reset */
5252 rc = ibmvnic_reset_crq(adapter);
5253 retrc = rc;
5254
5255 if (rc == H_CLOSED) {
5256 dev_warn(dev, "Partner adapter not ready\n");
5257 } else if (rc) {
5258 dev_warn(dev, "Error %d opening adapter\n", rc);
5259 goto reg_crq_failed;
5260 }
5261
5262 retrc = 0;
5263
Allen Paisaa7c3fe2020-09-14 12:59:29 +05305264 tasklet_setup(&adapter->tasklet, (void *)ibmvnic_tasklet);
Thomas Falcon6c267b32017-02-15 12:17:58 -06005265
Thomas Falcon032c5e82015-12-21 11:26:06 -06005266 netdev_dbg(adapter->netdev, "registering irq 0x%x\n", vdev->irq);
Murilo Fossa Vicentinie56e2512019-04-25 11:02:33 -03005267 snprintf(crq->name, sizeof(crq->name), "ibmvnic-%x",
5268 adapter->vdev->unit_address);
5269 rc = request_irq(vdev->irq, ibmvnic_interrupt, 0, crq->name, adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005270 if (rc) {
5271 dev_err(dev, "Couldn't register irq 0x%x. rc=%d\n",
5272 vdev->irq, rc);
5273 goto req_irq_failed;
5274 }
5275
5276 rc = vio_enable_interrupts(vdev);
5277 if (rc) {
5278 dev_err(dev, "Error %d enabling interrupts\n", rc);
5279 goto req_irq_failed;
5280 }
5281
5282 crq->cur = 0;
5283 spin_lock_init(&crq->lock);
5284
5285 return retrc;
5286
5287req_irq_failed:
Thomas Falcon6c267b32017-02-15 12:17:58 -06005288 tasklet_kill(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005289 do {
5290 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
5291 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
5292reg_crq_failed:
5293 dma_unmap_single(dev, crq->msg_token, PAGE_SIZE, DMA_BIDIRECTIONAL);
5294map_failed:
5295 free_page((unsigned long)crq->msgs);
Nathan Fontenotf9928872017-03-30 02:48:54 -04005296 crq->msgs = NULL;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005297 return retrc;
5298}
5299
Lijun Pan635e4422020-08-19 17:52:26 -05005300static int ibmvnic_reset_init(struct ibmvnic_adapter *adapter, bool reset)
John Allenf6ef6402017-03-17 17:13:42 -05005301{
5302 struct device *dev = &adapter->vdev->dev;
Dany Madden98c41f02020-11-25 18:04:32 -06005303 unsigned long timeout = msecs_to_jiffies(20000);
Michal Suchanek6881b072021-03-02 20:47:47 +01005304 u64 old_num_rx_queues = adapter->req_rx_queues;
5305 u64 old_num_tx_queues = adapter->req_tx_queues;
John Allenf6ef6402017-03-17 17:13:42 -05005306 int rc;
5307
John Allen017892c12017-05-26 10:30:19 -04005308 adapter->from_passive_init = false;
5309
Michal Suchanek6881b072021-03-02 20:47:47 +01005310 if (reset)
Lijun Pan635e4422020-08-19 17:52:26 -05005311 reinit_completion(&adapter->init_done);
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06005312
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04005313 adapter->init_done_rc = 0;
Lijun Panfa68bfa2020-08-19 17:52:24 -05005314 rc = ibmvnic_send_crq_init(adapter);
5315 if (rc) {
5316 dev_err(dev, "Send crq init failed with error %d\n", rc);
5317 return rc;
5318 }
5319
John Allenf6ef6402017-03-17 17:13:42 -05005320 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
5321 dev_err(dev, "Initialization sequence timed out\n");
John Allen017892c12017-05-26 10:30:19 -04005322 return -1;
5323 }
5324
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04005325 if (adapter->init_done_rc) {
5326 release_crq_queue(adapter);
5327 return adapter->init_done_rc;
5328 }
5329
Lijun Pan785a2b12020-09-17 21:12:46 -05005330 if (adapter->from_passive_init) {
5331 adapter->state = VNIC_OPEN;
5332 adapter->from_passive_init = false;
5333 return -1;
5334 }
5335
Lijun Pan635e4422020-08-19 17:52:26 -05005336 if (reset &&
5337 test_bit(0, &adapter->resetting) && !adapter->wait_for_reset &&
Nathan Fontenot30f79622018-04-06 18:37:06 -05005338 adapter->reset_reason != VNIC_RESET_MOBILITY) {
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06005339 if (adapter->req_rx_queues != old_num_rx_queues ||
5340 adapter->req_tx_queues != old_num_tx_queues) {
5341 release_sub_crqs(adapter, 0);
5342 rc = init_sub_crqs(adapter);
5343 } else {
5344 rc = reset_sub_crq_queues(adapter);
5345 }
5346 } else {
Nathan Fontenot57a49432017-05-26 10:31:12 -04005347 rc = init_sub_crqs(adapter);
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06005348 }
5349
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04005350 if (rc) {
5351 dev_err(dev, "Initialization of sub crqs failed\n");
5352 release_crq_queue(adapter);
Thomas Falcon5df969c2017-06-28 19:55:54 -05005353 return rc;
5354 }
5355
5356 rc = init_sub_crq_irqs(adapter);
5357 if (rc) {
5358 dev_err(dev, "Failed to initialize sub crq irqs\n");
5359 release_crq_queue(adapter);
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04005360 }
5361
5362 return rc;
John Allenf6ef6402017-03-17 17:13:42 -05005363}
5364
Thomas Falcon40c9db82017-06-12 12:35:04 -05005365static struct device_attribute dev_attr_failover;
5366
Thomas Falcon032c5e82015-12-21 11:26:06 -06005367static int ibmvnic_probe(struct vio_dev *dev, const struct vio_device_id *id)
5368{
5369 struct ibmvnic_adapter *adapter;
5370 struct net_device *netdev;
5371 unsigned char *mac_addr_p;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005372 int rc;
5373
5374 dev_dbg(&dev->dev, "entering ibmvnic_probe for UA 0x%x\n",
5375 dev->unit_address);
5376
5377 mac_addr_p = (unsigned char *)vio_get_attribute(dev,
5378 VETH_MAC_ADDR, NULL);
5379 if (!mac_addr_p) {
5380 dev_err(&dev->dev,
5381 "(%s:%3.3d) ERROR: Can't find MAC_ADDR attribute\n",
5382 __FILE__, __LINE__);
5383 return 0;
5384 }
5385
5386 netdev = alloc_etherdev_mq(sizeof(struct ibmvnic_adapter),
Thomas Falcond45cc3a2017-12-18 12:52:11 -06005387 IBMVNIC_MAX_QUEUES);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005388 if (!netdev)
5389 return -ENOMEM;
5390
5391 adapter = netdev_priv(netdev);
Nathan Fontenot90c80142017-05-03 14:04:32 -04005392 adapter->state = VNIC_PROBING;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005393 dev_set_drvdata(&dev->dev, netdev);
5394 adapter->vdev = dev;
5395 adapter->netdev = netdev;
Sukadev Bhattiprolu76cdc5c2020-11-25 18:04:29 -06005396 adapter->login_pending = false;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005397
5398 ether_addr_copy(adapter->mac_addr, mac_addr_p);
5399 ether_addr_copy(netdev->dev_addr, adapter->mac_addr);
5400 netdev->irq = dev->irq;
5401 netdev->netdev_ops = &ibmvnic_netdev_ops;
5402 netdev->ethtool_ops = &ibmvnic_ethtool_ops;
5403 SET_NETDEV_DEV(netdev, &dev->dev);
5404
Nathan Fontenoted651a12017-05-03 14:04:38 -04005405 INIT_WORK(&adapter->ibmvnic_reset, __ibmvnic_reset);
Juliet Kim7ed5b312019-09-20 16:11:23 -04005406 INIT_DELAYED_WORK(&adapter->ibmvnic_delayed_reset,
5407 __ibmvnic_delayed_reset);
Nathan Fontenoted651a12017-05-03 14:04:38 -04005408 INIT_LIST_HEAD(&adapter->rwi_list);
Thomas Falcon6c5c7482018-12-10 15:22:22 -06005409 spin_lock_init(&adapter->rwi_lock);
Juliet Kim7d7195a2020-03-10 09:23:58 -05005410 spin_lock_init(&adapter->state_lock);
Thomas Falconff25dcb2019-11-25 17:12:56 -06005411 mutex_init(&adapter->fw_lock);
Thomas Falconbbd669a2019-04-04 18:58:26 -05005412 init_completion(&adapter->init_done);
Thomas Falcon070eca92019-11-25 17:12:53 -06005413 init_completion(&adapter->fw_done);
5414 init_completion(&adapter->reset_done);
5415 init_completion(&adapter->stats_done);
Juliet Kim7ed5b312019-09-20 16:11:23 -04005416 clear_bit(0, &adapter->resetting);
Nathan Fontenoted651a12017-05-03 14:04:38 -04005417
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04005418 do {
Nathan Fontenot30f79622018-04-06 18:37:06 -05005419 rc = init_crq_queue(adapter);
5420 if (rc) {
5421 dev_err(&dev->dev, "Couldn't initialize crq. rc=%d\n",
5422 rc);
5423 goto ibmvnic_init_fail;
5424 }
5425
Lijun Pan635e4422020-08-19 17:52:26 -05005426 rc = ibmvnic_reset_init(adapter, false);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05005427 if (rc && rc != EAGAIN)
5428 goto ibmvnic_init_fail;
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04005429 } while (rc == EAGAIN);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005430
Thomas Falcon07184212018-05-16 15:49:05 -05005431 rc = init_stats_buffers(adapter);
5432 if (rc)
5433 goto ibmvnic_init_fail;
5434
5435 rc = init_stats_token(adapter);
5436 if (rc)
5437 goto ibmvnic_stats_fail;
5438
Thomas Falconf39f0d12017-02-14 10:22:59 -06005439 netdev->mtu = adapter->req_mtu - ETH_HLEN;
John Allenc26eba02017-10-26 16:23:25 -05005440 netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
5441 netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005442
Thomas Falcon40c9db82017-06-12 12:35:04 -05005443 rc = device_create_file(&dev->dev, &dev_attr_failover);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05005444 if (rc)
Thomas Falcon07184212018-05-16 15:49:05 -05005445 goto ibmvnic_dev_file_err;
Thomas Falcon40c9db82017-06-12 12:35:04 -05005446
Mick Tarsele876a8a2017-09-28 13:53:18 -07005447 netif_carrier_off(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005448 rc = register_netdev(netdev);
5449 if (rc) {
5450 dev_err(&dev->dev, "failed to register netdev rc=%d\n", rc);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05005451 goto ibmvnic_register_fail;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005452 }
5453 dev_info(&dev->dev, "ibmvnic registered\n");
5454
Nathan Fontenot90c80142017-05-03 14:04:32 -04005455 adapter->state = VNIC_PROBED;
John Allenc26eba02017-10-26 16:23:25 -05005456
5457 adapter->wait_for_reset = false;
Dany Maddena86d5c62020-11-25 18:04:31 -06005458 adapter->last_reset_time = jiffies;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005459 return 0;
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05005460
5461ibmvnic_register_fail:
5462 device_remove_file(&dev->dev, &dev_attr_failover);
5463
Thomas Falcon07184212018-05-16 15:49:05 -05005464ibmvnic_dev_file_err:
5465 release_stats_token(adapter);
5466
5467ibmvnic_stats_fail:
5468 release_stats_buffers(adapter);
5469
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05005470ibmvnic_init_fail:
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06005471 release_sub_crqs(adapter, 1);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05005472 release_crq_queue(adapter);
Thomas Falconff25dcb2019-11-25 17:12:56 -06005473 mutex_destroy(&adapter->fw_lock);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05005474 free_netdev(netdev);
5475
5476 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005477}
5478
Uwe Kleine-König386a9662021-02-25 23:18:34 +01005479static void ibmvnic_remove(struct vio_dev *dev)
Thomas Falcon032c5e82015-12-21 11:26:06 -06005480{
5481 struct net_device *netdev = dev_get_drvdata(&dev->dev);
Nathan Fontenot37489052017-04-19 13:45:04 -04005482 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Juliet Kim7d7195a2020-03-10 09:23:58 -05005483 unsigned long flags;
5484
5485 spin_lock_irqsave(&adapter->state_lock, flags);
Sukadev Bhattiprolu4a41c422021-02-12 20:42:50 -08005486
5487 /* If ibmvnic_reset() is scheduling a reset, wait for it to
5488 * finish. Then, set the state to REMOVING to prevent it from
5489 * scheduling any more work and to have reset functions ignore
5490 * any resets that have already been scheduled. Drop the lock
5491 * after setting state, so __ibmvnic_reset() which is called
5492 * from the flush_work() below, can make progress.
5493 */
Junlin Yang69cdb792021-03-05 16:48:39 +08005494 spin_lock(&adapter->rwi_lock);
Nathan Fontenot90c80142017-05-03 14:04:32 -04005495 adapter->state = VNIC_REMOVING;
Junlin Yang69cdb792021-03-05 16:48:39 +08005496 spin_unlock(&adapter->rwi_lock);
Sukadev Bhattiprolu4a41c422021-02-12 20:42:50 -08005497
Juliet Kim7d7195a2020-03-10 09:23:58 -05005498 spin_unlock_irqrestore(&adapter->state_lock, flags);
5499
Thomas Falcon6954a9e2020-06-12 13:34:41 -05005500 flush_work(&adapter->ibmvnic_reset);
5501 flush_delayed_work(&adapter->ibmvnic_delayed_reset);
5502
Juliet Kima5681e22018-11-19 15:59:22 -06005503 rtnl_lock();
5504 unregister_netdevice(netdev);
Nathan Fontenot37489052017-04-19 13:45:04 -04005505
5506 release_resources(adapter);
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06005507 release_sub_crqs(adapter, 1);
Nathan Fontenot37489052017-04-19 13:45:04 -04005508 release_crq_queue(adapter);
5509
Thomas Falcon53cc7722018-02-26 18:10:56 -06005510 release_stats_token(adapter);
5511 release_stats_buffers(adapter);
5512
Nathan Fontenot90c80142017-05-03 14:04:32 -04005513 adapter->state = VNIC_REMOVED;
5514
Juliet Kima5681e22018-11-19 15:59:22 -06005515 rtnl_unlock();
Thomas Falconff25dcb2019-11-25 17:12:56 -06005516 mutex_destroy(&adapter->fw_lock);
Thomas Falcon40c9db82017-06-12 12:35:04 -05005517 device_remove_file(&dev->dev, &dev_attr_failover);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005518 free_netdev(netdev);
5519 dev_set_drvdata(&dev->dev, NULL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005520}
5521
Thomas Falcon40c9db82017-06-12 12:35:04 -05005522static ssize_t failover_store(struct device *dev, struct device_attribute *attr,
5523 const char *buf, size_t count)
5524{
5525 struct net_device *netdev = dev_get_drvdata(dev);
5526 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
5527 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
5528 __be64 session_token;
5529 long rc;
5530
5531 if (!sysfs_streq(buf, "1"))
5532 return -EINVAL;
5533
5534 rc = plpar_hcall(H_VIOCTL, retbuf, adapter->vdev->unit_address,
5535 H_GET_SESSION_TOKEN, 0, 0, 0);
5536 if (rc) {
5537 netdev_err(netdev, "Couldn't retrieve session token, rc %ld\n",
5538 rc);
Lijun Pan334c4242021-04-13 03:31:44 -05005539 goto last_resort;
Thomas Falcon40c9db82017-06-12 12:35:04 -05005540 }
5541
5542 session_token = (__be64)retbuf[0];
5543 netdev_dbg(netdev, "Initiating client failover, session id %llx\n",
5544 be64_to_cpu(session_token));
5545 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
5546 H_SESSION_ERR_DETECTED, session_token, 0, 0);
Lijun Pan334c4242021-04-13 03:31:44 -05005547 if (rc)
5548 netdev_err(netdev,
5549 "H_VIOCTL initiated failover failed, rc %ld\n",
Thomas Falcon40c9db82017-06-12 12:35:04 -05005550 rc);
Lijun Pan334c4242021-04-13 03:31:44 -05005551
5552last_resort:
5553 netdev_dbg(netdev, "Trying to send CRQ_CMD, the last resort\n");
5554 ibmvnic_reset(adapter, VNIC_RESET_FAILOVER);
Thomas Falcon40c9db82017-06-12 12:35:04 -05005555
5556 return count;
5557}
Joe Perches6cbaefb2017-12-19 10:15:09 -08005558static DEVICE_ATTR_WO(failover);
Thomas Falcon40c9db82017-06-12 12:35:04 -05005559
Thomas Falcon032c5e82015-12-21 11:26:06 -06005560static unsigned long ibmvnic_get_desired_dma(struct vio_dev *vdev)
5561{
5562 struct net_device *netdev = dev_get_drvdata(&vdev->dev);
5563 struct ibmvnic_adapter *adapter;
5564 struct iommu_table *tbl;
5565 unsigned long ret = 0;
5566 int i;
5567
5568 tbl = get_iommu_table_base(&vdev->dev);
5569
5570 /* netdev inits at probe time along with the structures we need below*/
5571 if (!netdev)
5572 return IOMMU_PAGE_ALIGN(IBMVNIC_IO_ENTITLEMENT_DEFAULT, tbl);
5573
5574 adapter = netdev_priv(netdev);
5575
5576 ret += PAGE_SIZE; /* the crq message queue */
Thomas Falcon032c5e82015-12-21 11:26:06 -06005577 ret += IOMMU_PAGE_ALIGN(sizeof(struct ibmvnic_statistics), tbl);
5578
5579 for (i = 0; i < adapter->req_tx_queues + adapter->req_rx_queues; i++)
5580 ret += 4 * PAGE_SIZE; /* the scrq message queue */
5581
Thomas Falcon507ebe62020-08-21 13:39:01 -05005582 for (i = 0; i < adapter->num_active_rx_pools; i++)
Thomas Falcon032c5e82015-12-21 11:26:06 -06005583 ret += adapter->rx_pool[i].size *
5584 IOMMU_PAGE_ALIGN(adapter->rx_pool[i].buff_size, tbl);
5585
5586 return ret;
5587}
5588
5589static int ibmvnic_resume(struct device *dev)
5590{
5591 struct net_device *netdev = dev_get_drvdata(dev);
5592 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005593
John Allencb89ba22017-06-19 11:27:53 -05005594 if (adapter->state != VNIC_OPEN)
5595 return 0;
5596
John Allena2488782017-07-24 13:26:06 -05005597 tasklet_schedule(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005598
5599 return 0;
5600}
5601
Arvind Yadav8c37bc62017-08-17 18:52:54 +05305602static const struct vio_device_id ibmvnic_device_table[] = {
Thomas Falcon032c5e82015-12-21 11:26:06 -06005603 {"network", "IBM,vnic"},
5604 {"", "" }
5605};
5606MODULE_DEVICE_TABLE(vio, ibmvnic_device_table);
5607
5608static const struct dev_pm_ops ibmvnic_pm_ops = {
5609 .resume = ibmvnic_resume
5610};
5611
5612static struct vio_driver ibmvnic_driver = {
5613 .id_table = ibmvnic_device_table,
5614 .probe = ibmvnic_probe,
5615 .remove = ibmvnic_remove,
5616 .get_desired_dma = ibmvnic_get_desired_dma,
5617 .name = ibmvnic_driver_name,
5618 .pm = &ibmvnic_pm_ops,
5619};
5620
5621/* module functions */
5622static int __init ibmvnic_module_init(void)
5623{
5624 pr_info("%s: %s %s\n", ibmvnic_driver_name, ibmvnic_driver_string,
5625 IBMVNIC_DRIVER_VERSION);
5626
5627 return vio_register_driver(&ibmvnic_driver);
5628}
5629
5630static void __exit ibmvnic_module_exit(void)
5631{
5632 vio_unregister_driver(&ibmvnic_driver);
5633}
5634
5635module_init(ibmvnic_module_init);
5636module_exit(ibmvnic_module_exit);