blob: 8b2f6eb8eb2139922e2c49d9723d8f05b04a06fa [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);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600109
110struct ibmvnic_stat {
111 char name[ETH_GSTRING_LEN];
112 int offset;
113};
114
115#define IBMVNIC_STAT_OFF(stat) (offsetof(struct ibmvnic_adapter, stats) + \
116 offsetof(struct ibmvnic_statistics, stat))
Lijun Pan91dc5d22021-02-11 00:43:22 -0600117#define IBMVNIC_GET_STAT(a, off) (*((u64 *)(((unsigned long)(a)) + (off))))
Thomas Falcon032c5e82015-12-21 11:26:06 -0600118
119static const struct ibmvnic_stat ibmvnic_stats[] = {
120 {"rx_packets", IBMVNIC_STAT_OFF(rx_packets)},
121 {"rx_bytes", IBMVNIC_STAT_OFF(rx_bytes)},
122 {"tx_packets", IBMVNIC_STAT_OFF(tx_packets)},
123 {"tx_bytes", IBMVNIC_STAT_OFF(tx_bytes)},
124 {"ucast_tx_packets", IBMVNIC_STAT_OFF(ucast_tx_packets)},
125 {"ucast_rx_packets", IBMVNIC_STAT_OFF(ucast_rx_packets)},
126 {"mcast_tx_packets", IBMVNIC_STAT_OFF(mcast_tx_packets)},
127 {"mcast_rx_packets", IBMVNIC_STAT_OFF(mcast_rx_packets)},
128 {"bcast_tx_packets", IBMVNIC_STAT_OFF(bcast_tx_packets)},
129 {"bcast_rx_packets", IBMVNIC_STAT_OFF(bcast_rx_packets)},
130 {"align_errors", IBMVNIC_STAT_OFF(align_errors)},
131 {"fcs_errors", IBMVNIC_STAT_OFF(fcs_errors)},
132 {"single_collision_frames", IBMVNIC_STAT_OFF(single_collision_frames)},
133 {"multi_collision_frames", IBMVNIC_STAT_OFF(multi_collision_frames)},
134 {"sqe_test_errors", IBMVNIC_STAT_OFF(sqe_test_errors)},
135 {"deferred_tx", IBMVNIC_STAT_OFF(deferred_tx)},
136 {"late_collisions", IBMVNIC_STAT_OFF(late_collisions)},
137 {"excess_collisions", IBMVNIC_STAT_OFF(excess_collisions)},
138 {"internal_mac_tx_errors", IBMVNIC_STAT_OFF(internal_mac_tx_errors)},
139 {"carrier_sense", IBMVNIC_STAT_OFF(carrier_sense)},
140 {"too_long_frames", IBMVNIC_STAT_OFF(too_long_frames)},
141 {"internal_mac_rx_errors", IBMVNIC_STAT_OFF(internal_mac_rx_errors)},
142};
143
144static long h_reg_sub_crq(unsigned long unit_address, unsigned long token,
145 unsigned long length, unsigned long *number,
146 unsigned long *irq)
147{
148 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
149 long rc;
150
151 rc = plpar_hcall(H_REG_SUB_CRQ, retbuf, unit_address, token, length);
152 *number = retbuf[0];
153 *irq = retbuf[1];
154
155 return rc;
156}
157
Thomas Falcon476d96c2019-11-25 17:12:55 -0600158/**
159 * ibmvnic_wait_for_completion - Check device state and wait for completion
160 * @adapter: private device data
161 * @comp_done: completion structure to wait for
162 * @timeout: time to wait in milliseconds
163 *
164 * Wait for a completion signal or until the timeout limit is reached
165 * while checking that the device is still active.
166 */
167static int ibmvnic_wait_for_completion(struct ibmvnic_adapter *adapter,
168 struct completion *comp_done,
169 unsigned long timeout)
170{
171 struct net_device *netdev;
172 unsigned long div_timeout;
173 u8 retry;
174
175 netdev = adapter->netdev;
176 retry = 5;
177 div_timeout = msecs_to_jiffies(timeout / retry);
178 while (true) {
179 if (!adapter->crq.active) {
180 netdev_err(netdev, "Device down!\n");
181 return -ENODEV;
182 }
Thomas Falcon8f9cc1e2019-12-11 09:38:39 -0600183 if (!retry--)
Thomas Falcon476d96c2019-11-25 17:12:55 -0600184 break;
185 if (wait_for_completion_timeout(comp_done, div_timeout))
186 return 0;
187 }
188 netdev_err(netdev, "Operation timed out.\n");
189 return -ETIMEDOUT;
190}
191
Thomas Falcon032c5e82015-12-21 11:26:06 -0600192static int alloc_long_term_buff(struct ibmvnic_adapter *adapter,
193 struct ibmvnic_long_term_buff *ltb, int size)
194{
195 struct device *dev = &adapter->vdev->dev;
Thomas Falcon9c4eaab2018-05-23 13:37:57 -0500196 int rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600197
198 ltb->size = size;
199 ltb->buff = dma_alloc_coherent(dev, ltb->size, &ltb->addr,
200 GFP_KERNEL);
201
202 if (!ltb->buff) {
203 dev_err(dev, "Couldn't alloc long term buffer\n");
204 return -ENOMEM;
205 }
206 ltb->map_id = adapter->map_id;
207 adapter->map_id++;
Nathan Fontenotdb5d0b52017-02-10 13:45:05 -0500208
Thomas Falconff25dcb2019-11-25 17:12:56 -0600209 mutex_lock(&adapter->fw_lock);
210 adapter->fw_done_rc = 0;
Thomas Falcon070eca92019-11-25 17:12:53 -0600211 reinit_completion(&adapter->fw_done);
Thomas Falcon9c4eaab2018-05-23 13:37:57 -0500212 rc = send_request_map(adapter, ltb->addr,
213 ltb->size, ltb->map_id);
214 if (rc) {
215 dma_free_coherent(dev, ltb->size, ltb->buff, ltb->addr);
Thomas Falconff25dcb2019-11-25 17:12:56 -0600216 mutex_unlock(&adapter->fw_lock);
Thomas Falcon9c4eaab2018-05-23 13:37:57 -0500217 return rc;
218 }
Thomas Falcon476d96c2019-11-25 17:12:55 -0600219
220 rc = ibmvnic_wait_for_completion(adapter, &adapter->fw_done, 10000);
221 if (rc) {
222 dev_err(dev,
223 "Long term map request aborted or timed out,rc = %d\n",
224 rc);
225 dma_free_coherent(dev, ltb->size, ltb->buff, ltb->addr);
Thomas Falconff25dcb2019-11-25 17:12:56 -0600226 mutex_unlock(&adapter->fw_lock);
Thomas Falcon476d96c2019-11-25 17:12:55 -0600227 return rc;
228 }
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500229
230 if (adapter->fw_done_rc) {
231 dev_err(dev, "Couldn't map long term buffer,rc = %d\n",
232 adapter->fw_done_rc);
Thomas Falcon4cf2ddf32018-05-16 15:49:03 -0500233 dma_free_coherent(dev, ltb->size, ltb->buff, ltb->addr);
Thomas Falconff25dcb2019-11-25 17:12:56 -0600234 mutex_unlock(&adapter->fw_lock);
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500235 return -1;
236 }
Thomas Falconff25dcb2019-11-25 17:12:56 -0600237 mutex_unlock(&adapter->fw_lock);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600238 return 0;
239}
240
241static void free_long_term_buff(struct ibmvnic_adapter *adapter,
242 struct ibmvnic_long_term_buff *ltb)
243{
244 struct device *dev = &adapter->vdev->dev;
245
Nathan Fontenotc657e322017-03-30 02:49:06 -0400246 if (!ltb->buff)
247 return;
248
Lijun Pan7d3a7b92021-02-12 20:49:00 -0600249 /* VIOS automatically unmaps the long term buffer at remote
250 * end for the following resets:
251 * FAILOVER, MOBILITY, TIMEOUT.
252 */
Nathan Fontenoted651a12017-05-03 14:04:38 -0400253 if (adapter->reset_reason != VNIC_RESET_FAILOVER &&
Lijun Pan7d3a7b92021-02-12 20:49:00 -0600254 adapter->reset_reason != VNIC_RESET_MOBILITY &&
255 adapter->reset_reason != VNIC_RESET_TIMEOUT)
Thomas Falcondfad09a2016-08-18 11:37:51 -0500256 send_request_unmap(adapter, ltb->map_id);
Brian King59af56c2017-04-19 13:44:41 -0400257 dma_free_coherent(dev, ltb->size, ltb->buff, ltb->addr);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600258}
259
Sukadev Bhattiprolu0ec13af2021-06-23 21:13:10 -0700260static int reset_long_term_buff(struct ibmvnic_adapter *adapter,
261 struct ibmvnic_long_term_buff *ltb)
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500262{
Sukadev Bhattiprolu0ec13af2021-06-23 21:13:10 -0700263 struct device *dev = &adapter->vdev->dev;
264 int rc;
Thomas Falcon9c4eaab2018-05-23 13:37:57 -0500265
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500266 memset(ltb->buff, 0, ltb->size);
Sukadev Bhattiprolu0ec13af2021-06-23 21:13:10 -0700267
268 mutex_lock(&adapter->fw_lock);
269 adapter->fw_done_rc = 0;
270
271 reinit_completion(&adapter->fw_done);
272 rc = send_request_map(adapter, ltb->addr, ltb->size, ltb->map_id);
273 if (rc) {
274 mutex_unlock(&adapter->fw_lock);
275 return rc;
276 }
277
278 rc = ibmvnic_wait_for_completion(adapter, &adapter->fw_done, 10000);
279 if (rc) {
280 dev_info(dev,
281 "Reset failed, long term map request timed out or aborted\n");
282 mutex_unlock(&adapter->fw_lock);
283 return rc;
284 }
285
286 if (adapter->fw_done_rc) {
287 dev_info(dev,
288 "Reset failed, attempting to free and reallocate buffer\n");
289 free_long_term_buff(adapter, ltb);
290 mutex_unlock(&adapter->fw_lock);
291 return alloc_long_term_buff(adapter, ltb, ltb->size);
292 }
293 mutex_unlock(&adapter->fw_lock);
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500294 return 0;
295}
296
Thomas Falconf185a492017-05-26 10:30:48 -0400297static void deactivate_rx_pools(struct ibmvnic_adapter *adapter)
298{
299 int i;
300
Thomas Falcon507ebe62020-08-21 13:39:01 -0500301 for (i = 0; i < adapter->num_active_rx_pools; i++)
Thomas Falconf185a492017-05-26 10:30:48 -0400302 adapter->rx_pool[i].active = 0;
303}
304
Thomas Falcon032c5e82015-12-21 11:26:06 -0600305static void replenish_rx_pool(struct ibmvnic_adapter *adapter,
306 struct ibmvnic_rx_pool *pool)
307{
308 int count = pool->size - atomic_read(&pool->available);
Cristobal Fornof3ae59c2020-08-19 13:16:23 -0500309 u64 handle = adapter->rx_scrq[pool->index]->handle;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600310 struct device *dev = &adapter->vdev->dev;
Thomas Falcon4f0b6812020-11-18 19:12:18 -0600311 struct ibmvnic_ind_xmit_queue *ind_bufp;
312 struct ibmvnic_sub_crq_queue *rx_scrq;
313 union sub_crq *sub_crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600314 int buffers_added = 0;
315 unsigned long lpar_rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600316 struct sk_buff *skb;
317 unsigned int offset;
318 dma_addr_t dma_addr;
319 unsigned char *dst;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600320 int shift = 0;
321 int index;
322 int i;
323
Thomas Falconf185a492017-05-26 10:30:48 -0400324 if (!pool->active)
325 return;
326
Thomas Falcon4f0b6812020-11-18 19:12:18 -0600327 rx_scrq = adapter->rx_scrq[pool->index];
328 ind_bufp = &rx_scrq->ind_buf;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600329 for (i = 0; i < count; ++i) {
Dwip N. Banerjeee552aa32020-11-18 19:12:24 -0600330 skb = netdev_alloc_skb(adapter->netdev, pool->buff_size);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600331 if (!skb) {
332 dev_err(dev, "Couldn't replenish rx buff\n");
333 adapter->replenish_no_mem++;
334 break;
335 }
336
337 index = pool->free_map[pool->next_free];
338
339 if (pool->rx_buff[index].skb)
340 dev_err(dev, "Inconsistent free_map!\n");
341
342 /* Copy the skb to the long term mapped DMA buffer */
343 offset = index * pool->buff_size;
344 dst = pool->long_term_buff.buff + offset;
345 memset(dst, 0, pool->buff_size);
346 dma_addr = pool->long_term_buff.addr + offset;
347 pool->rx_buff[index].data = dst;
348
349 pool->free_map[pool->next_free] = IBMVNIC_INVALID_MAP;
350 pool->rx_buff[index].dma = dma_addr;
351 pool->rx_buff[index].skb = skb;
352 pool->rx_buff[index].pool_index = pool->index;
353 pool->rx_buff[index].size = pool->buff_size;
354
Thomas Falcon4f0b6812020-11-18 19:12:18 -0600355 sub_crq = &ind_bufp->indir_arr[ind_bufp->index++];
356 memset(sub_crq, 0, sizeof(*sub_crq));
357 sub_crq->rx_add.first = IBMVNIC_CRQ_CMD;
358 sub_crq->rx_add.correlator =
Thomas Falcon032c5e82015-12-21 11:26:06 -0600359 cpu_to_be64((u64)&pool->rx_buff[index]);
Thomas Falcon4f0b6812020-11-18 19:12:18 -0600360 sub_crq->rx_add.ioba = cpu_to_be32(dma_addr);
361 sub_crq->rx_add.map_id = pool->long_term_buff.map_id;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600362
363 /* The length field of the sCRQ is defined to be 24 bits so the
364 * buffer size needs to be left shifted by a byte before it is
365 * converted to big endian to prevent the last byte from being
366 * truncated.
367 */
368#ifdef __LITTLE_ENDIAN__
369 shift = 8;
370#endif
Thomas Falcon4f0b6812020-11-18 19:12:18 -0600371 sub_crq->rx_add.len = cpu_to_be32(pool->buff_size << shift);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600372 pool->next_free = (pool->next_free + 1) % pool->size;
Thomas Falcon4f0b6812020-11-18 19:12:18 -0600373 if (ind_bufp->index == IBMVNIC_MAX_IND_DESCS ||
374 i == count - 1) {
375 lpar_rc =
376 send_subcrq_indirect(adapter, handle,
377 (u64)ind_bufp->indir_dma,
378 (u64)ind_bufp->index);
379 if (lpar_rc != H_SUCCESS)
380 goto failure;
381 buffers_added += ind_bufp->index;
382 adapter->replenish_add_buff_success += ind_bufp->index;
383 ind_bufp->index = 0;
384 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600385 }
386 atomic_add(buffers_added, &pool->available);
387 return;
388
389failure:
Thomas Falcon2d14d372018-07-13 12:03:32 -0500390 if (lpar_rc != H_PARAMETER && lpar_rc != H_CLOSED)
391 dev_err_ratelimited(dev, "rx: replenish packet buffer failed\n");
Thomas Falcon4f0b6812020-11-18 19:12:18 -0600392 for (i = ind_bufp->index - 1; i >= 0; --i) {
393 struct ibmvnic_rx_buff *rx_buff;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600394
Thomas Falcon4f0b6812020-11-18 19:12:18 -0600395 pool->next_free = pool->next_free == 0 ?
396 pool->size - 1 : pool->next_free - 1;
397 sub_crq = &ind_bufp->indir_arr[i];
398 rx_buff = (struct ibmvnic_rx_buff *)
399 be64_to_cpu(sub_crq->rx_add.correlator);
400 index = (int)(rx_buff - pool->rx_buff);
401 pool->free_map[pool->next_free] = index;
402 dev_kfree_skb_any(pool->rx_buff[index].skb);
403 pool->rx_buff[index].skb = NULL;
404 }
Dwip N. Banerjeec2af6222020-12-09 20:53:31 -0500405 adapter->replenish_add_buff_failure += ind_bufp->index;
406 atomic_add(buffers_added, &pool->available);
Thomas Falcon4f0b6812020-11-18 19:12:18 -0600407 ind_bufp->index = 0;
Thomas Falcon5a18e1e2018-04-06 18:37:05 -0500408 if (lpar_rc == H_CLOSED || adapter->failover_pending) {
Thomas Falconf185a492017-05-26 10:30:48 -0400409 /* Disable buffer pool replenishment and report carrier off if
Thomas Falcon5a18e1e2018-04-06 18:37:05 -0500410 * queue is closed or pending failover.
411 * Firmware guarantees that a signal will be sent to the
412 * driver, triggering a reset.
Thomas Falconf185a492017-05-26 10:30:48 -0400413 */
414 deactivate_rx_pools(adapter);
415 netif_carrier_off(adapter->netdev);
416 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600417}
418
419static void replenish_pools(struct ibmvnic_adapter *adapter)
420{
421 int i;
422
Thomas Falcon032c5e82015-12-21 11:26:06 -0600423 adapter->replenish_task_cycles++;
Thomas Falcon507ebe62020-08-21 13:39:01 -0500424 for (i = 0; i < adapter->num_active_rx_pools; i++) {
Thomas Falcon032c5e82015-12-21 11:26:06 -0600425 if (adapter->rx_pool[i].active)
426 replenish_rx_pool(adapter, &adapter->rx_pool[i]);
427 }
Sukadev Bhattiprolu38bd5ce2020-12-04 18:22:35 -0800428
429 netdev_dbg(adapter->netdev, "Replenished %d pools\n", i);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600430}
431
John Allen3d52b592017-08-02 16:44:14 -0500432static void release_stats_buffers(struct ibmvnic_adapter *adapter)
433{
434 kfree(adapter->tx_stats_buffers);
435 kfree(adapter->rx_stats_buffers);
Thomas Falconb0992ec2018-02-06 17:25:23 -0600436 adapter->tx_stats_buffers = NULL;
437 adapter->rx_stats_buffers = NULL;
John Allen3d52b592017-08-02 16:44:14 -0500438}
439
440static int init_stats_buffers(struct ibmvnic_adapter *adapter)
441{
442 adapter->tx_stats_buffers =
Nathan Fontenotabcae542018-02-19 13:30:47 -0600443 kcalloc(IBMVNIC_MAX_QUEUES,
John Allen3d52b592017-08-02 16:44:14 -0500444 sizeof(struct ibmvnic_tx_queue_stats),
445 GFP_KERNEL);
446 if (!adapter->tx_stats_buffers)
447 return -ENOMEM;
448
449 adapter->rx_stats_buffers =
Nathan Fontenotabcae542018-02-19 13:30:47 -0600450 kcalloc(IBMVNIC_MAX_QUEUES,
John Allen3d52b592017-08-02 16:44:14 -0500451 sizeof(struct ibmvnic_rx_queue_stats),
452 GFP_KERNEL);
453 if (!adapter->rx_stats_buffers)
454 return -ENOMEM;
455
456 return 0;
457}
458
Nathan Fontenot7bbc27a2017-03-30 02:49:23 -0400459static void release_stats_token(struct ibmvnic_adapter *adapter)
460{
461 struct device *dev = &adapter->vdev->dev;
462
463 if (!adapter->stats_token)
464 return;
465
466 dma_unmap_single(dev, adapter->stats_token,
467 sizeof(struct ibmvnic_statistics),
468 DMA_FROM_DEVICE);
469 adapter->stats_token = 0;
470}
471
472static int init_stats_token(struct ibmvnic_adapter *adapter)
473{
474 struct device *dev = &adapter->vdev->dev;
475 dma_addr_t stok;
476
477 stok = dma_map_single(dev, &adapter->stats,
478 sizeof(struct ibmvnic_statistics),
479 DMA_FROM_DEVICE);
480 if (dma_mapping_error(dev, stok)) {
481 dev_err(dev, "Couldn't map stats buffer\n");
482 return -1;
483 }
484
485 adapter->stats_token = stok;
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500486 netdev_dbg(adapter->netdev, "Stats token initialized (%llx)\n", stok);
Nathan Fontenot7bbc27a2017-03-30 02:49:23 -0400487 return 0;
488}
489
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400490static int reset_rx_pools(struct ibmvnic_adapter *adapter)
491{
492 struct ibmvnic_rx_pool *rx_pool;
Thomas Falcon507ebe62020-08-21 13:39:01 -0500493 u64 buff_size;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400494 int rx_scrqs;
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500495 int i, j, rc;
John Allen896d8692018-01-18 16:26:31 -0600496
Mingming Cao9f134572020-08-25 13:26:41 -0400497 if (!adapter->rx_pool)
498 return -1;
499
Thomas Falcon507ebe62020-08-21 13:39:01 -0500500 buff_size = adapter->cur_rx_buf_sz;
501 rx_scrqs = adapter->num_active_rx_pools;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400502 for (i = 0; i < rx_scrqs; i++) {
503 rx_pool = &adapter->rx_pool[i];
504
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500505 netdev_dbg(adapter->netdev, "Re-setting rx_pool[%d]\n", i);
506
Thomas Falcon507ebe62020-08-21 13:39:01 -0500507 if (rx_pool->buff_size != buff_size) {
John Allen896d8692018-01-18 16:26:31 -0600508 free_long_term_buff(adapter, &rx_pool->long_term_buff);
Dwip N. Banerjee9a87c3f2020-11-18 19:12:22 -0600509 rx_pool->buff_size = ALIGN(buff_size, L1_CACHE_BYTES);
Thomas Falcon7c940b12019-06-07 16:03:55 -0500510 rc = alloc_long_term_buff(adapter,
511 &rx_pool->long_term_buff,
512 rx_pool->size *
513 rx_pool->buff_size);
John Allen896d8692018-01-18 16:26:31 -0600514 } else {
Sukadev Bhattiprolu0ec13af2021-06-23 21:13:10 -0700515 rc = reset_long_term_buff(adapter,
516 &rx_pool->long_term_buff);
John Allen896d8692018-01-18 16:26:31 -0600517 }
518
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500519 if (rc)
520 return rc;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400521
522 for (j = 0; j < rx_pool->size; j++)
523 rx_pool->free_map[j] = j;
524
525 memset(rx_pool->rx_buff, 0,
526 rx_pool->size * sizeof(struct ibmvnic_rx_buff));
527
528 atomic_set(&rx_pool->available, 0);
529 rx_pool->next_alloc = 0;
530 rx_pool->next_free = 0;
Thomas Falconc3e53b92017-06-14 23:50:05 -0500531 rx_pool->active = 1;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400532 }
533
534 return 0;
535}
536
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400537static void release_rx_pools(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -0600538{
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400539 struct ibmvnic_rx_pool *rx_pool;
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400540 int i, j;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600541
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400542 if (!adapter->rx_pool)
Thomas Falcon032c5e82015-12-21 11:26:06 -0600543 return;
544
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600545 for (i = 0; i < adapter->num_active_rx_pools; i++) {
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400546 rx_pool = &adapter->rx_pool[i];
547
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500548 netdev_dbg(adapter->netdev, "Releasing rx_pool[%d]\n", i);
549
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400550 kfree(rx_pool->free_map);
551 free_long_term_buff(adapter, &rx_pool->long_term_buff);
552
553 if (!rx_pool->rx_buff)
Nathan Fontenote0ebe9422017-05-03 14:04:50 -0400554 continue;
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400555
556 for (j = 0; j < rx_pool->size; j++) {
557 if (rx_pool->rx_buff[j].skb) {
Thomas Falconb7cdec32018-11-21 11:17:58 -0600558 dev_kfree_skb_any(rx_pool->rx_buff[j].skb);
559 rx_pool->rx_buff[j].skb = NULL;
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400560 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600561 }
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400562
563 kfree(rx_pool->rx_buff);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600564 }
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400565
566 kfree(adapter->rx_pool);
567 adapter->rx_pool = NULL;
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600568 adapter->num_active_rx_pools = 0;
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400569}
570
571static int init_rx_pools(struct net_device *netdev)
572{
573 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
574 struct device *dev = &adapter->vdev->dev;
575 struct ibmvnic_rx_pool *rx_pool;
576 int rxadd_subcrqs;
Thomas Falcon507ebe62020-08-21 13:39:01 -0500577 u64 buff_size;
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400578 int i, j;
579
Thomas Falcon507ebe62020-08-21 13:39:01 -0500580 rxadd_subcrqs = adapter->num_active_rx_scrqs;
581 buff_size = adapter->cur_rx_buf_sz;
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400582
583 adapter->rx_pool = kcalloc(rxadd_subcrqs,
584 sizeof(struct ibmvnic_rx_pool),
585 GFP_KERNEL);
586 if (!adapter->rx_pool) {
587 dev_err(dev, "Failed to allocate rx pools\n");
588 return -1;
589 }
590
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600591 adapter->num_active_rx_pools = rxadd_subcrqs;
592
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400593 for (i = 0; i < rxadd_subcrqs; i++) {
594 rx_pool = &adapter->rx_pool[i];
595
596 netdev_dbg(adapter->netdev,
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500597 "Initializing rx_pool[%d], %lld buffs, %lld bytes each\n",
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400598 i, adapter->req_rx_add_entries_per_subcrq,
Thomas Falcon507ebe62020-08-21 13:39:01 -0500599 buff_size);
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400600
601 rx_pool->size = adapter->req_rx_add_entries_per_subcrq;
602 rx_pool->index = i;
Dwip N. Banerjee9a87c3f2020-11-18 19:12:22 -0600603 rx_pool->buff_size = ALIGN(buff_size, L1_CACHE_BYTES);
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400604 rx_pool->active = 1;
605
606 rx_pool->free_map = kcalloc(rx_pool->size, sizeof(int),
607 GFP_KERNEL);
608 if (!rx_pool->free_map) {
609 release_rx_pools(adapter);
610 return -1;
611 }
612
613 rx_pool->rx_buff = kcalloc(rx_pool->size,
614 sizeof(struct ibmvnic_rx_buff),
615 GFP_KERNEL);
616 if (!rx_pool->rx_buff) {
617 dev_err(dev, "Couldn't alloc rx buffers\n");
618 release_rx_pools(adapter);
619 return -1;
620 }
621
622 if (alloc_long_term_buff(adapter, &rx_pool->long_term_buff,
623 rx_pool->size * rx_pool->buff_size)) {
624 release_rx_pools(adapter);
625 return -1;
626 }
627
628 for (j = 0; j < rx_pool->size; ++j)
629 rx_pool->free_map[j] = j;
630
631 atomic_set(&rx_pool->available, 0);
632 rx_pool->next_alloc = 0;
633 rx_pool->next_free = 0;
634 }
635
636 return 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600637}
638
Sukadev Bhattiprolu0ec13af2021-06-23 21:13:10 -0700639static int reset_one_tx_pool(struct ibmvnic_adapter *adapter,
640 struct ibmvnic_tx_pool *tx_pool)
Thomas Falcone26dc252018-03-16 20:00:25 -0500641{
642 int rc, i;
643
Sukadev Bhattiprolu0ec13af2021-06-23 21:13:10 -0700644 rc = reset_long_term_buff(adapter, &tx_pool->long_term_buff);
Thomas Falcone26dc252018-03-16 20:00:25 -0500645 if (rc)
646 return rc;
647
648 memset(tx_pool->tx_buff, 0,
649 tx_pool->num_buffers *
650 sizeof(struct ibmvnic_tx_buff));
651
652 for (i = 0; i < tx_pool->num_buffers; i++)
653 tx_pool->free_map[i] = i;
654
655 tx_pool->consumer_index = 0;
656 tx_pool->producer_index = 0;
657
658 return 0;
659}
660
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400661static int reset_tx_pools(struct ibmvnic_adapter *adapter)
662{
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400663 int tx_scrqs;
Thomas Falcone26dc252018-03-16 20:00:25 -0500664 int i, rc;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400665
Mingming Cao9f134572020-08-25 13:26:41 -0400666 if (!adapter->tx_pool)
667 return -1;
668
Thomas Falcon507ebe62020-08-21 13:39:01 -0500669 tx_scrqs = adapter->num_active_tx_pools;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400670 for (i = 0; i < tx_scrqs; i++) {
Sukadev Bhattiprolu0ec13af2021-06-23 21:13:10 -0700671 rc = reset_one_tx_pool(adapter, &adapter->tso_pool[i]);
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500672 if (rc)
673 return rc;
Sukadev Bhattiprolu0ec13af2021-06-23 21:13:10 -0700674 rc = reset_one_tx_pool(adapter, &adapter->tx_pool[i]);
Thomas Falconfdb06102017-10-17 12:36:55 -0500675 if (rc)
676 return rc;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400677 }
678
679 return 0;
680}
681
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -0200682static void release_vpd_data(struct ibmvnic_adapter *adapter)
683{
684 if (!adapter->vpd)
685 return;
686
687 kfree(adapter->vpd->buff);
688 kfree(adapter->vpd);
Thomas Falconb0992ec2018-02-06 17:25:23 -0600689
690 adapter->vpd = NULL;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -0200691}
692
Thomas Falconfb794212018-03-16 20:00:26 -0500693static void release_one_tx_pool(struct ibmvnic_adapter *adapter,
694 struct ibmvnic_tx_pool *tx_pool)
695{
696 kfree(tx_pool->tx_buff);
697 kfree(tx_pool->free_map);
698 free_long_term_buff(adapter, &tx_pool->long_term_buff);
699}
700
Nathan Fontenotc657e322017-03-30 02:49:06 -0400701static void release_tx_pools(struct ibmvnic_adapter *adapter)
702{
John Allen896d8692018-01-18 16:26:31 -0600703 int i;
Nathan Fontenotc657e322017-03-30 02:49:06 -0400704
705 if (!adapter->tx_pool)
706 return;
707
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600708 for (i = 0; i < adapter->num_active_tx_pools; i++) {
Thomas Falconfb794212018-03-16 20:00:26 -0500709 release_one_tx_pool(adapter, &adapter->tx_pool[i]);
710 release_one_tx_pool(adapter, &adapter->tso_pool[i]);
Nathan Fontenotc657e322017-03-30 02:49:06 -0400711 }
712
713 kfree(adapter->tx_pool);
714 adapter->tx_pool = NULL;
Thomas Falconfb794212018-03-16 20:00:26 -0500715 kfree(adapter->tso_pool);
716 adapter->tso_pool = NULL;
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600717 adapter->num_active_tx_pools = 0;
Nathan Fontenotc657e322017-03-30 02:49:06 -0400718}
719
Thomas Falcon32053062018-03-16 20:00:27 -0500720static int init_one_tx_pool(struct net_device *netdev,
721 struct ibmvnic_tx_pool *tx_pool,
722 int num_entries, int buf_size)
723{
724 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
725 int i;
726
727 tx_pool->tx_buff = kcalloc(num_entries,
728 sizeof(struct ibmvnic_tx_buff),
729 GFP_KERNEL);
730 if (!tx_pool->tx_buff)
731 return -1;
732
733 if (alloc_long_term_buff(adapter, &tx_pool->long_term_buff,
734 num_entries * buf_size))
735 return -1;
736
737 tx_pool->free_map = kcalloc(num_entries, sizeof(int), GFP_KERNEL);
738 if (!tx_pool->free_map)
739 return -1;
740
741 for (i = 0; i < num_entries; i++)
742 tx_pool->free_map[i] = i;
743
744 tx_pool->consumer_index = 0;
745 tx_pool->producer_index = 0;
746 tx_pool->num_buffers = num_entries;
747 tx_pool->buf_size = buf_size;
748
749 return 0;
750}
751
Nathan Fontenotc657e322017-03-30 02:49:06 -0400752static int init_tx_pools(struct net_device *netdev)
753{
754 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Nathan Fontenotc657e322017-03-30 02:49:06 -0400755 int tx_subcrqs;
Dwip N. Banerjee9a87c3f2020-11-18 19:12:22 -0600756 u64 buff_size;
Thomas Falcon32053062018-03-16 20:00:27 -0500757 int i, rc;
Nathan Fontenotc657e322017-03-30 02:49:06 -0400758
Thomas Falcon507ebe62020-08-21 13:39:01 -0500759 tx_subcrqs = adapter->num_active_tx_scrqs;
Nathan Fontenotc657e322017-03-30 02:49:06 -0400760 adapter->tx_pool = kcalloc(tx_subcrqs,
761 sizeof(struct ibmvnic_tx_pool), GFP_KERNEL);
762 if (!adapter->tx_pool)
763 return -1;
764
Thomas Falcon32053062018-03-16 20:00:27 -0500765 adapter->tso_pool = kcalloc(tx_subcrqs,
766 sizeof(struct ibmvnic_tx_pool), GFP_KERNEL);
767 if (!adapter->tso_pool)
768 return -1;
769
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600770 adapter->num_active_tx_pools = tx_subcrqs;
771
Nathan Fontenotc657e322017-03-30 02:49:06 -0400772 for (i = 0; i < tx_subcrqs; i++) {
Dwip N. Banerjee9a87c3f2020-11-18 19:12:22 -0600773 buff_size = adapter->req_mtu + VLAN_HLEN;
774 buff_size = ALIGN(buff_size, L1_CACHE_BYTES);
Thomas Falcon32053062018-03-16 20:00:27 -0500775 rc = init_one_tx_pool(netdev, &adapter->tx_pool[i],
776 adapter->req_tx_entries_per_subcrq,
Dwip N. Banerjee9a87c3f2020-11-18 19:12:22 -0600777 buff_size);
Thomas Falcon32053062018-03-16 20:00:27 -0500778 if (rc) {
Nathan Fontenotc657e322017-03-30 02:49:06 -0400779 release_tx_pools(adapter);
Thomas Falcon32053062018-03-16 20:00:27 -0500780 return rc;
Nathan Fontenotc657e322017-03-30 02:49:06 -0400781 }
782
Thomas Falcon7c940b12019-06-07 16:03:55 -0500783 rc = init_one_tx_pool(netdev, &adapter->tso_pool[i],
784 IBMVNIC_TSO_BUFS,
785 IBMVNIC_TSO_BUF_SZ);
Thomas Falcon32053062018-03-16 20:00:27 -0500786 if (rc) {
Nathan Fontenotc657e322017-03-30 02:49:06 -0400787 release_tx_pools(adapter);
Thomas Falcon32053062018-03-16 20:00:27 -0500788 return rc;
Nathan Fontenotc657e322017-03-30 02:49:06 -0400789 }
Nathan Fontenotc657e322017-03-30 02:49:06 -0400790 }
791
792 return 0;
793}
794
John Allend944c3d62017-05-26 10:30:13 -0400795static void ibmvnic_napi_enable(struct ibmvnic_adapter *adapter)
796{
797 int i;
798
799 if (adapter->napi_enabled)
800 return;
801
802 for (i = 0; i < adapter->req_rx_queues; i++)
803 napi_enable(&adapter->napi[i]);
804
805 adapter->napi_enabled = true;
806}
807
808static void ibmvnic_napi_disable(struct ibmvnic_adapter *adapter)
809{
810 int i;
811
812 if (!adapter->napi_enabled)
813 return;
814
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500815 for (i = 0; i < adapter->req_rx_queues; i++) {
816 netdev_dbg(adapter->netdev, "Disabling napi[%d]\n", i);
John Allend944c3d62017-05-26 10:30:13 -0400817 napi_disable(&adapter->napi[i]);
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500818 }
John Allend944c3d62017-05-26 10:30:13 -0400819
820 adapter->napi_enabled = false;
821}
822
Nathan Fontenot86f669b2018-02-19 13:30:39 -0600823static int init_napi(struct ibmvnic_adapter *adapter)
824{
825 int i;
826
827 adapter->napi = kcalloc(adapter->req_rx_queues,
828 sizeof(struct napi_struct), GFP_KERNEL);
829 if (!adapter->napi)
830 return -ENOMEM;
831
832 for (i = 0; i < adapter->req_rx_queues; i++) {
833 netdev_dbg(adapter->netdev, "Adding napi[%d]\n", i);
834 netif_napi_add(adapter->netdev, &adapter->napi[i],
835 ibmvnic_poll, NAPI_POLL_WEIGHT);
836 }
837
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600838 adapter->num_active_rx_napi = adapter->req_rx_queues;
Nathan Fontenot86f669b2018-02-19 13:30:39 -0600839 return 0;
840}
841
842static void release_napi(struct ibmvnic_adapter *adapter)
843{
844 int i;
845
846 if (!adapter->napi)
847 return;
848
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600849 for (i = 0; i < adapter->num_active_rx_napi; i++) {
Wen Yang390de192018-12-11 12:20:46 +0800850 netdev_dbg(adapter->netdev, "Releasing napi[%d]\n", i);
851 netif_napi_del(&adapter->napi[i]);
Nathan Fontenot86f669b2018-02-19 13:30:39 -0600852 }
853
854 kfree(adapter->napi);
855 adapter->napi = NULL;
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600856 adapter->num_active_rx_napi = 0;
Thomas Falconc3f22412018-05-23 13:37:55 -0500857 adapter->napi_enabled = false;
Nathan Fontenot86f669b2018-02-19 13:30:39 -0600858}
859
Lijun Pan0666ef72021-04-12 02:41:28 -0500860static const char *adapter_state_to_string(enum vnic_state state)
861{
862 switch (state) {
863 case VNIC_PROBING:
864 return "PROBING";
865 case VNIC_PROBED:
866 return "PROBED";
867 case VNIC_OPENING:
868 return "OPENING";
869 case VNIC_OPEN:
870 return "OPEN";
871 case VNIC_CLOSING:
872 return "CLOSING";
873 case VNIC_CLOSED:
874 return "CLOSED";
875 case VNIC_REMOVING:
876 return "REMOVING";
877 case VNIC_REMOVED:
878 return "REMOVED";
879 default:
880 return "UNKNOWN";
881 }
882}
883
John Allena57a5d22017-03-17 17:13:41 -0500884static int ibmvnic_login(struct net_device *netdev)
Thomas Falcon032c5e82015-12-21 11:26:06 -0600885{
886 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Dany Madden98c41f02020-11-25 18:04:32 -0600887 unsigned long timeout = msecs_to_jiffies(20000);
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500888 int retry_count = 0;
Thomas Falcondff515a32020-06-15 10:29:23 -0500889 int retries = 10;
Thomas Falconeb110412018-05-24 14:37:53 -0500890 bool retry;
Thomas Falcon4d96f122017-08-01 15:04:36 -0500891 int rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600892
John Allenbd0b6722017-03-17 17:13:40 -0500893 do {
Thomas Falconeb110412018-05-24 14:37:53 -0500894 retry = false;
Thomas Falcondff515a32020-06-15 10:29:23 -0500895 if (retry_count > retries) {
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500896 netdev_warn(netdev, "Login attempts exceeded\n");
897 return -1;
898 }
899
900 adapter->init_done_rc = 0;
901 reinit_completion(&adapter->init_done);
902 rc = send_login(adapter);
Dany Maddenc98d9cc2020-11-25 18:04:30 -0600903 if (rc)
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500904 return rc;
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500905
906 if (!wait_for_completion_timeout(&adapter->init_done,
907 timeout)) {
Thomas Falcondff515a32020-06-15 10:29:23 -0500908 netdev_warn(netdev, "Login timed out, retrying...\n");
909 retry = true;
910 adapter->init_done_rc = 0;
911 retry_count++;
912 continue;
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500913 }
914
Thomas Falcondff515a32020-06-15 10:29:23 -0500915 if (adapter->init_done_rc == ABORTED) {
916 netdev_warn(netdev, "Login aborted, retrying...\n");
917 retry = true;
918 adapter->init_done_rc = 0;
919 retry_count++;
920 /* FW or device may be busy, so
921 * wait a bit before retrying login
922 */
923 msleep(500);
924 } else if (adapter->init_done_rc == PARTIALSUCCESS) {
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500925 retry_count++;
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -0600926 release_sub_crqs(adapter, 1);
John Allenbd0b6722017-03-17 17:13:40 -0500927
Thomas Falconeb110412018-05-24 14:37:53 -0500928 retry = true;
929 netdev_dbg(netdev,
930 "Received partial success, retrying...\n");
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500931 adapter->init_done_rc = 0;
John Allenbd0b6722017-03-17 17:13:40 -0500932 reinit_completion(&adapter->init_done);
Lijun Pan491099a2020-09-27 20:13:26 -0500933 send_query_cap(adapter);
John Allenbd0b6722017-03-17 17:13:40 -0500934 if (!wait_for_completion_timeout(&adapter->init_done,
935 timeout)) {
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500936 netdev_warn(netdev,
937 "Capabilities query timed out\n");
John Allenbd0b6722017-03-17 17:13:40 -0500938 return -1;
939 }
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500940
Thomas Falcon4d96f122017-08-01 15:04:36 -0500941 rc = init_sub_crqs(adapter);
942 if (rc) {
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500943 netdev_warn(netdev,
944 "SCRQ initialization failed\n");
Thomas Falcon4d96f122017-08-01 15:04:36 -0500945 return -1;
946 }
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500947
Thomas Falcon4d96f122017-08-01 15:04:36 -0500948 rc = init_sub_crq_irqs(adapter);
949 if (rc) {
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500950 netdev_warn(netdev,
951 "SCRQ irq initialization failed\n");
Thomas Falcon4d96f122017-08-01 15:04:36 -0500952 return -1;
953 }
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500954 } else if (adapter->init_done_rc) {
955 netdev_warn(netdev, "Adapter login failed\n");
John Allenbd0b6722017-03-17 17:13:40 -0500956 return -1;
957 }
Thomas Falconeb110412018-05-24 14:37:53 -0500958 } while (retry);
John Allenbd0b6722017-03-17 17:13:40 -0500959
Thomas Falcon62740e92019-05-09 23:13:43 -0500960 __ibmvnic_set_mac(netdev, adapter->mac_addr);
Thomas Falcon3d166132018-01-10 19:39:52 -0600961
Lijun Pan0666ef72021-04-12 02:41:28 -0500962 netdev_dbg(netdev, "[S:%s] Login succeeded\n", adapter_state_to_string(adapter->state));
John Allena57a5d22017-03-17 17:13:41 -0500963 return 0;
964}
965
Thomas Falcon34f0f4e2018-02-13 18:23:40 -0600966static void release_login_buffer(struct ibmvnic_adapter *adapter)
967{
968 kfree(adapter->login_buf);
969 adapter->login_buf = NULL;
970}
971
972static void release_login_rsp_buffer(struct ibmvnic_adapter *adapter)
973{
974 kfree(adapter->login_rsp_buf);
975 adapter->login_rsp_buf = NULL;
976}
977
Nathan Fontenot1b8955e2017-03-30 02:49:29 -0400978static void release_resources(struct ibmvnic_adapter *adapter)
979{
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -0200980 release_vpd_data(adapter);
981
Nathan Fontenot1b8955e2017-03-30 02:49:29 -0400982 release_tx_pools(adapter);
983 release_rx_pools(adapter);
984
Nathan Fontenot86f669b2018-02-19 13:30:39 -0600985 release_napi(adapter);
Lijun Pana0c8be52020-12-19 15:39:19 -0600986 release_login_buffer(adapter);
Thomas Falcon34f0f4e2018-02-13 18:23:40 -0600987 release_login_rsp_buffer(adapter);
Nathan Fontenot1b8955e2017-03-30 02:49:29 -0400988}
989
Nathan Fontenot53da09e2017-04-21 15:39:04 -0400990static int set_link_state(struct ibmvnic_adapter *adapter, u8 link_state)
991{
992 struct net_device *netdev = adapter->netdev;
Dany Madden98c41f02020-11-25 18:04:32 -0600993 unsigned long timeout = msecs_to_jiffies(20000);
Nathan Fontenot53da09e2017-04-21 15:39:04 -0400994 union ibmvnic_crq crq;
995 bool resend;
996 int rc;
997
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500998 netdev_dbg(netdev, "setting link state %d\n", link_state);
999
Nathan Fontenot53da09e2017-04-21 15:39:04 -04001000 memset(&crq, 0, sizeof(crq));
1001 crq.logical_link_state.first = IBMVNIC_CRQ_CMD;
1002 crq.logical_link_state.cmd = LOGICAL_LINK_STATE;
1003 crq.logical_link_state.link_state = link_state;
1004
1005 do {
1006 resend = false;
1007
1008 reinit_completion(&adapter->init_done);
1009 rc = ibmvnic_send_crq(adapter, &crq);
1010 if (rc) {
1011 netdev_err(netdev, "Failed to set link state\n");
1012 return rc;
1013 }
1014
1015 if (!wait_for_completion_timeout(&adapter->init_done,
1016 timeout)) {
1017 netdev_err(netdev, "timeout setting link state\n");
1018 return -1;
1019 }
1020
Lijun Pan4c5f6af2020-08-19 17:52:23 -05001021 if (adapter->init_done_rc == PARTIALSUCCESS) {
Nathan Fontenot53da09e2017-04-21 15:39:04 -04001022 /* Partuial success, delay and re-send */
1023 mdelay(1000);
1024 resend = true;
Thomas Falconab5ec332018-05-23 13:37:59 -05001025 } else if (adapter->init_done_rc) {
1026 netdev_warn(netdev, "Unable to set link state, rc=%d\n",
1027 adapter->init_done_rc);
1028 return adapter->init_done_rc;
Nathan Fontenot53da09e2017-04-21 15:39:04 -04001029 }
1030 } while (resend);
1031
1032 return 0;
1033}
1034
Thomas Falcon7f3c6e62017-04-21 15:38:40 -04001035static int set_real_num_queues(struct net_device *netdev)
1036{
1037 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1038 int rc;
1039
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001040 netdev_dbg(netdev, "Setting real tx/rx queues (%llx/%llx)\n",
1041 adapter->req_tx_queues, adapter->req_rx_queues);
1042
Thomas Falcon7f3c6e62017-04-21 15:38:40 -04001043 rc = netif_set_real_num_tx_queues(netdev, adapter->req_tx_queues);
1044 if (rc) {
1045 netdev_err(netdev, "failed to set the number of tx queues\n");
1046 return rc;
1047 }
1048
1049 rc = netif_set_real_num_rx_queues(netdev, adapter->req_rx_queues);
1050 if (rc)
1051 netdev_err(netdev, "failed to set the number of rx queues\n");
1052
1053 return rc;
1054}
1055
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001056static int ibmvnic_get_vpd(struct ibmvnic_adapter *adapter)
1057{
1058 struct device *dev = &adapter->vdev->dev;
1059 union ibmvnic_crq crq;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001060 int len = 0;
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05001061 int rc;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001062
1063 if (adapter->vpd->buff)
1064 len = adapter->vpd->len;
1065
Thomas Falconff25dcb2019-11-25 17:12:56 -06001066 mutex_lock(&adapter->fw_lock);
1067 adapter->fw_done_rc = 0;
Thomas Falcon070eca92019-11-25 17:12:53 -06001068 reinit_completion(&adapter->fw_done);
Thomas Falconff25dcb2019-11-25 17:12:56 -06001069
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001070 crq.get_vpd_size.first = IBMVNIC_CRQ_CMD;
1071 crq.get_vpd_size.cmd = GET_VPD_SIZE;
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05001072 rc = ibmvnic_send_crq(adapter, &crq);
Thomas Falconff25dcb2019-11-25 17:12:56 -06001073 if (rc) {
1074 mutex_unlock(&adapter->fw_lock);
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05001075 return rc;
Thomas Falconff25dcb2019-11-25 17:12:56 -06001076 }
Thomas Falcon476d96c2019-11-25 17:12:55 -06001077
1078 rc = ibmvnic_wait_for_completion(adapter, &adapter->fw_done, 10000);
1079 if (rc) {
1080 dev_err(dev, "Could not retrieve VPD size, rc = %d\n", rc);
Thomas Falconff25dcb2019-11-25 17:12:56 -06001081 mutex_unlock(&adapter->fw_lock);
Thomas Falcon476d96c2019-11-25 17:12:55 -06001082 return rc;
1083 }
Thomas Falconff25dcb2019-11-25 17:12:56 -06001084 mutex_unlock(&adapter->fw_lock);
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001085
1086 if (!adapter->vpd->len)
1087 return -ENODATA;
1088
1089 if (!adapter->vpd->buff)
1090 adapter->vpd->buff = kzalloc(adapter->vpd->len, GFP_KERNEL);
1091 else if (adapter->vpd->len != len)
1092 adapter->vpd->buff =
1093 krealloc(adapter->vpd->buff,
1094 adapter->vpd->len, GFP_KERNEL);
1095
1096 if (!adapter->vpd->buff) {
1097 dev_err(dev, "Could allocate VPD buffer\n");
1098 return -ENOMEM;
1099 }
1100
1101 adapter->vpd->dma_addr =
1102 dma_map_single(dev, adapter->vpd->buff, adapter->vpd->len,
1103 DMA_FROM_DEVICE);
Desnes Augusto Nunes do Rosariof7431062017-11-17 09:09:04 -02001104 if (dma_mapping_error(dev, adapter->vpd->dma_addr)) {
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001105 dev_err(dev, "Could not map VPD buffer\n");
1106 kfree(adapter->vpd->buff);
Thomas Falconb0992ec2018-02-06 17:25:23 -06001107 adapter->vpd->buff = NULL;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001108 return -ENOMEM;
1109 }
1110
Thomas Falconff25dcb2019-11-25 17:12:56 -06001111 mutex_lock(&adapter->fw_lock);
1112 adapter->fw_done_rc = 0;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001113 reinit_completion(&adapter->fw_done);
Thomas Falconff25dcb2019-11-25 17:12:56 -06001114
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001115 crq.get_vpd.first = IBMVNIC_CRQ_CMD;
1116 crq.get_vpd.cmd = GET_VPD;
1117 crq.get_vpd.ioba = cpu_to_be32(adapter->vpd->dma_addr);
1118 crq.get_vpd.len = cpu_to_be32((u32)adapter->vpd->len);
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05001119 rc = ibmvnic_send_crq(adapter, &crq);
1120 if (rc) {
1121 kfree(adapter->vpd->buff);
1122 adapter->vpd->buff = NULL;
Thomas Falconff25dcb2019-11-25 17:12:56 -06001123 mutex_unlock(&adapter->fw_lock);
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05001124 return rc;
1125 }
Thomas Falcon476d96c2019-11-25 17:12:55 -06001126
1127 rc = ibmvnic_wait_for_completion(adapter, &adapter->fw_done, 10000);
1128 if (rc) {
1129 dev_err(dev, "Unable to retrieve VPD, rc = %d\n", rc);
1130 kfree(adapter->vpd->buff);
1131 adapter->vpd->buff = NULL;
Thomas Falconff25dcb2019-11-25 17:12:56 -06001132 mutex_unlock(&adapter->fw_lock);
Thomas Falcon476d96c2019-11-25 17:12:55 -06001133 return rc;
1134 }
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001135
Thomas Falconff25dcb2019-11-25 17:12:56 -06001136 mutex_unlock(&adapter->fw_lock);
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001137 return 0;
1138}
1139
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001140static int init_resources(struct ibmvnic_adapter *adapter)
John Allena57a5d22017-03-17 17:13:41 -05001141{
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001142 struct net_device *netdev = adapter->netdev;
Nathan Fontenot86f669b2018-02-19 13:30:39 -06001143 int rc;
John Allena57a5d22017-03-17 17:13:41 -05001144
Thomas Falcon7f3c6e62017-04-21 15:38:40 -04001145 rc = set_real_num_queues(netdev);
1146 if (rc)
1147 return rc;
John Allenbd0b6722017-03-17 17:13:40 -05001148
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001149 adapter->vpd = kzalloc(sizeof(*adapter->vpd), GFP_KERNEL);
1150 if (!adapter->vpd)
1151 return -ENOMEM;
1152
John Allen69d08dc2018-01-18 16:27:58 -06001153 /* Vital Product Data (VPD) */
1154 rc = ibmvnic_get_vpd(adapter);
1155 if (rc) {
1156 netdev_err(netdev, "failed to initialize Vital Product Data (VPD)\n");
1157 return rc;
1158 }
1159
Thomas Falcon032c5e82015-12-21 11:26:06 -06001160 adapter->map_id = 1;
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001161
Nathan Fontenot86f669b2018-02-19 13:30:39 -06001162 rc = init_napi(adapter);
1163 if (rc)
1164 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001165
Lijun Pan69980d02020-09-27 20:13:28 -05001166 send_query_map(adapter);
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -04001167
1168 rc = init_rx_pools(netdev);
1169 if (rc)
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001170 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001171
Nathan Fontenotc657e322017-03-30 02:49:06 -04001172 rc = init_tx_pools(netdev);
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001173 return rc;
1174}
1175
Nathan Fontenoted651a12017-05-03 14:04:38 -04001176static int __ibmvnic_open(struct net_device *netdev)
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001177{
1178 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001179 enum vnic_state prev_state = adapter->state;
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001180 int i, rc;
1181
Nathan Fontenot90c80142017-05-03 14:04:32 -04001182 adapter->state = VNIC_OPENING;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001183 replenish_pools(adapter);
John Allend944c3d62017-05-26 10:30:13 -04001184 ibmvnic_napi_enable(adapter);
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001185
Thomas Falcon032c5e82015-12-21 11:26:06 -06001186 /* We're ready to receive frames, enable the sub-crq interrupts and
1187 * set the logical link state to up
1188 */
Nathan Fontenoted651a12017-05-03 14:04:38 -04001189 for (i = 0; i < adapter->req_rx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001190 netdev_dbg(netdev, "Enabling rx_scrq[%d] irq\n", i);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001191 if (prev_state == VNIC_CLOSED)
1192 enable_irq(adapter->rx_scrq[i]->irq);
Thomas Falconf23e0642018-04-15 18:53:36 -05001193 enable_scrq_irq(adapter, adapter->rx_scrq[i]);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001194 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001195
Nathan Fontenoted651a12017-05-03 14:04:38 -04001196 for (i = 0; i < adapter->req_tx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001197 netdev_dbg(netdev, "Enabling tx_scrq[%d] irq\n", i);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001198 if (prev_state == VNIC_CLOSED)
1199 enable_irq(adapter->tx_scrq[i]->irq);
Thomas Falconf23e0642018-04-15 18:53:36 -05001200 enable_scrq_irq(adapter, adapter->tx_scrq[i]);
Thomas Falcon0d973382020-11-18 19:12:19 -06001201 netdev_tx_reset_queue(netdev_get_tx_queue(netdev, i));
Nathan Fontenoted651a12017-05-03 14:04:38 -04001202 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001203
Nathan Fontenot53da09e2017-04-21 15:39:04 -04001204 rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_UP);
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001205 if (rc) {
Lijun Pan0775ebc2021-04-14 02:46:14 -05001206 ibmvnic_napi_disable(adapter);
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001207 release_resources(adapter);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001208 return rc;
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001209 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001210
Nathan Fontenoted651a12017-05-03 14:04:38 -04001211 netif_tx_start_all_queues(netdev);
1212
Dany Madden2ca220f2021-06-23 21:13:11 -07001213 if (prev_state == VNIC_CLOSED) {
1214 for (i = 0; i < adapter->req_rx_queues; i++)
1215 napi_schedule(&adapter->napi[i]);
1216 }
1217
Nathan Fontenoted651a12017-05-03 14:04:38 -04001218 adapter->state = VNIC_OPEN;
1219 return rc;
1220}
1221
1222static int ibmvnic_open(struct net_device *netdev)
1223{
1224 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
John Allen69d08dc2018-01-18 16:27:58 -06001225 int rc;
Nathan Fontenoted651a12017-05-03 14:04:38 -04001226
Sukadev Bhattiprolu8f1c0fd2021-02-23 21:02:29 -08001227 ASSERT_RTNL();
1228
1229 /* If device failover is pending or we are about to reset, just set
1230 * device state and return. Device operation will be handled by reset
1231 * routine.
1232 *
1233 * It should be safe to overwrite the adapter->state here. Since
1234 * we hold the rtnl, either the reset has not actually started or
1235 * the rtnl got dropped during the set_link_state() in do_reset().
1236 * In the former case, no one else is changing the state (again we
1237 * have the rtnl) and in the latter case, do_reset() will detect and
1238 * honor our setting below.
Thomas Falcon5a18e1e2018-04-06 18:37:05 -05001239 */
Sukadev Bhattiprolu8f1c0fd2021-02-23 21:02:29 -08001240 if (adapter->failover_pending || (test_bit(0, &adapter->resetting))) {
Lijun Pan0666ef72021-04-12 02:41:28 -05001241 netdev_dbg(netdev, "[S:%s FOP:%d] Resetting, deferring open\n",
1242 adapter_state_to_string(adapter->state),
1243 adapter->failover_pending);
Thomas Falcon5a18e1e2018-04-06 18:37:05 -05001244 adapter->state = VNIC_OPEN;
Sukadev Bhattiprolu8f1c0fd2021-02-23 21:02:29 -08001245 rc = 0;
1246 goto out;
Thomas Falcon5a18e1e2018-04-06 18:37:05 -05001247 }
1248
Nathan Fontenoted651a12017-05-03 14:04:38 -04001249 if (adapter->state != VNIC_CLOSED) {
1250 rc = ibmvnic_login(netdev);
Juliet Kima5681e22018-11-19 15:59:22 -06001251 if (rc)
Sukadev Bhattiprolu1d850492020-10-30 10:07:11 -07001252 goto out;
Nathan Fontenoted651a12017-05-03 14:04:38 -04001253
1254 rc = init_resources(adapter);
1255 if (rc) {
1256 netdev_err(netdev, "failed to initialize resources\n");
1257 release_resources(adapter);
Sukadev Bhattiprolu1d850492020-10-30 10:07:11 -07001258 goto out;
Nathan Fontenoted651a12017-05-03 14:04:38 -04001259 }
1260 }
1261
1262 rc = __ibmvnic_open(netdev);
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001263
Sukadev Bhattiprolu1d850492020-10-30 10:07:11 -07001264out:
Sukadev Bhattiprolu8f1c0fd2021-02-23 21:02:29 -08001265 /* If open failed and there is a pending failover or in-progress reset,
1266 * set device state and return. Device operation will be handled by
1267 * reset routine. See also comments above regarding rtnl.
Sukadev Bhattiprolu1d850492020-10-30 10:07:11 -07001268 */
Sukadev Bhattiprolu8f1c0fd2021-02-23 21:02:29 -08001269 if (rc &&
1270 (adapter->failover_pending || (test_bit(0, &adapter->resetting)))) {
Sukadev Bhattiprolu1d850492020-10-30 10:07:11 -07001271 adapter->state = VNIC_OPEN;
1272 rc = 0;
1273 }
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001274 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001275}
1276
Thomas Falcond0869c02018-02-13 18:23:43 -06001277static void clean_rx_pools(struct ibmvnic_adapter *adapter)
1278{
1279 struct ibmvnic_rx_pool *rx_pool;
Thomas Falcon637f81d2018-02-26 18:10:57 -06001280 struct ibmvnic_rx_buff *rx_buff;
Thomas Falcond0869c02018-02-13 18:23:43 -06001281 u64 rx_entries;
1282 int rx_scrqs;
1283 int i, j;
1284
1285 if (!adapter->rx_pool)
1286 return;
1287
Thomas Falcon660e3092018-04-20 14:25:32 -05001288 rx_scrqs = adapter->num_active_rx_pools;
Thomas Falcond0869c02018-02-13 18:23:43 -06001289 rx_entries = adapter->req_rx_add_entries_per_subcrq;
1290
1291 /* Free any remaining skbs in the rx buffer pools */
1292 for (i = 0; i < rx_scrqs; i++) {
1293 rx_pool = &adapter->rx_pool[i];
Thomas Falcon637f81d2018-02-26 18:10:57 -06001294 if (!rx_pool || !rx_pool->rx_buff)
Thomas Falcond0869c02018-02-13 18:23:43 -06001295 continue;
1296
1297 netdev_dbg(adapter->netdev, "Cleaning rx_pool[%d]\n", i);
1298 for (j = 0; j < rx_entries; j++) {
Thomas Falcon637f81d2018-02-26 18:10:57 -06001299 rx_buff = &rx_pool->rx_buff[j];
1300 if (rx_buff && rx_buff->skb) {
1301 dev_kfree_skb_any(rx_buff->skb);
1302 rx_buff->skb = NULL;
Thomas Falcond0869c02018-02-13 18:23:43 -06001303 }
1304 }
1305 }
1306}
1307
Thomas Falcone9e1e972018-03-16 20:00:30 -05001308static void clean_one_tx_pool(struct ibmvnic_adapter *adapter,
1309 struct ibmvnic_tx_pool *tx_pool)
Nathan Fontenotb41b83e2017-05-03 14:04:56 -04001310{
Thomas Falcon637f81d2018-02-26 18:10:57 -06001311 struct ibmvnic_tx_buff *tx_buff;
Nathan Fontenotb41b83e2017-05-03 14:04:56 -04001312 u64 tx_entries;
Thomas Falcone9e1e972018-03-16 20:00:30 -05001313 int i;
Nathan Fontenotb41b83e2017-05-03 14:04:56 -04001314
Dan Carpenter050e85c2018-03-23 14:36:15 +03001315 if (!tx_pool || !tx_pool->tx_buff)
Thomas Falcone9e1e972018-03-16 20:00:30 -05001316 return;
1317
1318 tx_entries = tx_pool->num_buffers;
1319
1320 for (i = 0; i < tx_entries; i++) {
1321 tx_buff = &tx_pool->tx_buff[i];
1322 if (tx_buff && tx_buff->skb) {
1323 dev_kfree_skb_any(tx_buff->skb);
1324 tx_buff->skb = NULL;
1325 }
1326 }
1327}
1328
1329static void clean_tx_pools(struct ibmvnic_adapter *adapter)
1330{
1331 int tx_scrqs;
1332 int i;
1333
1334 if (!adapter->tx_pool || !adapter->tso_pool)
Nathan Fontenotb41b83e2017-05-03 14:04:56 -04001335 return;
1336
Thomas Falcon660e3092018-04-20 14:25:32 -05001337 tx_scrqs = adapter->num_active_tx_pools;
Nathan Fontenotb41b83e2017-05-03 14:04:56 -04001338
1339 /* Free any remaining skbs in the tx buffer pools */
1340 for (i = 0; i < tx_scrqs; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001341 netdev_dbg(adapter->netdev, "Cleaning tx_pool[%d]\n", i);
Thomas Falcone9e1e972018-03-16 20:00:30 -05001342 clean_one_tx_pool(adapter, &adapter->tx_pool[i]);
1343 clean_one_tx_pool(adapter, &adapter->tso_pool[i]);
Nathan Fontenotb41b83e2017-05-03 14:04:56 -04001344 }
1345}
1346
John Allen6095e592018-03-30 13:44:21 -05001347static void ibmvnic_disable_irqs(struct ibmvnic_adapter *adapter)
John Allenea5509f2017-03-17 17:13:43 -05001348{
John Allen6095e592018-03-30 13:44:21 -05001349 struct net_device *netdev = adapter->netdev;
John Allenea5509f2017-03-17 17:13:43 -05001350 int i;
1351
Nathan Fontenot46293b92017-05-03 14:05:02 -04001352 if (adapter->tx_scrq) {
1353 for (i = 0; i < adapter->req_tx_queues; i++)
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001354 if (adapter->tx_scrq[i]->irq) {
Thomas Falconf8738662018-03-07 17:51:45 -06001355 netdev_dbg(netdev,
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001356 "Disabling tx_scrq[%d] irq\n", i);
Thomas Falconf23e0642018-04-15 18:53:36 -05001357 disable_scrq_irq(adapter, adapter->tx_scrq[i]);
Nathan Fontenot46293b92017-05-03 14:05:02 -04001358 disable_irq(adapter->tx_scrq[i]->irq);
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001359 }
Nathan Fontenot46293b92017-05-03 14:05:02 -04001360 }
1361
Nathan Fontenot46293b92017-05-03 14:05:02 -04001362 if (adapter->rx_scrq) {
1363 for (i = 0; i < adapter->req_rx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001364 if (adapter->rx_scrq[i]->irq) {
Thomas Falconf8738662018-03-07 17:51:45 -06001365 netdev_dbg(netdev,
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001366 "Disabling rx_scrq[%d] irq\n", i);
Thomas Falconf23e0642018-04-15 18:53:36 -05001367 disable_scrq_irq(adapter, adapter->rx_scrq[i]);
Nathan Fontenot46293b92017-05-03 14:05:02 -04001368 disable_irq(adapter->rx_scrq[i]->irq);
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001369 }
Nathan Fontenot46293b92017-05-03 14:05:02 -04001370 }
1371 }
John Allen6095e592018-03-30 13:44:21 -05001372}
1373
1374static void ibmvnic_cleanup(struct net_device *netdev)
1375{
1376 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1377
1378 /* ensure that transmissions are stopped if called by do_reset */
Juliet Kim7ed5b312019-09-20 16:11:23 -04001379 if (test_bit(0, &adapter->resetting))
John Allen6095e592018-03-30 13:44:21 -05001380 netif_tx_disable(netdev);
1381 else
1382 netif_tx_stop_all_queues(netdev);
1383
1384 ibmvnic_napi_disable(adapter);
1385 ibmvnic_disable_irqs(adapter);
1386
Thomas Falcond0869c02018-02-13 18:23:43 -06001387 clean_rx_pools(adapter);
Thomas Falcon10f76212017-05-26 10:30:31 -04001388 clean_tx_pools(adapter);
Thomas Falcon01d9bd72018-03-07 17:51:46 -06001389}
1390
1391static int __ibmvnic_close(struct net_device *netdev)
1392{
1393 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1394 int rc = 0;
1395
1396 adapter->state = VNIC_CLOSING;
1397 rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_DN);
Nathan Fontenot90c80142017-05-03 14:04:32 -04001398 adapter->state = VNIC_CLOSED;
Sukadev Bhattiprolud4083d32021-02-10 17:41:43 -08001399 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001400}
1401
Nathan Fontenoted651a12017-05-03 14:04:38 -04001402static int ibmvnic_close(struct net_device *netdev)
1403{
1404 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1405 int rc;
1406
Lijun Pan0666ef72021-04-12 02:41:28 -05001407 netdev_dbg(netdev, "[S:%s FOP:%d FRR:%d] Closing\n",
1408 adapter_state_to_string(adapter->state),
1409 adapter->failover_pending,
Sukadev Bhattiprolu38bd5ce2020-12-04 18:22:35 -08001410 adapter->force_reset_recovery);
1411
Thomas Falcon5a18e1e2018-04-06 18:37:05 -05001412 /* If device failover is pending, just set device state and return.
1413 * Device operation will be handled by reset routine.
1414 */
1415 if (adapter->failover_pending) {
1416 adapter->state = VNIC_CLOSED;
1417 return 0;
1418 }
1419
Nathan Fontenoted651a12017-05-03 14:04:38 -04001420 rc = __ibmvnic_close(netdev);
Nathan Fontenot30f79622018-04-06 18:37:06 -05001421 ibmvnic_cleanup(netdev);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001422
1423 return rc;
1424}
1425
Thomas Falconad7775d2016-04-01 17:20:34 -05001426/**
1427 * build_hdr_data - creates L2/L3/L4 header data buffer
Lee Jones80708602021-01-15 20:09:03 +00001428 * @hdr_field: bitfield determining needed headers
1429 * @skb: socket buffer
1430 * @hdr_len: array of header lengths
1431 * @hdr_data: buffer to write the header to
Thomas Falconad7775d2016-04-01 17:20:34 -05001432 *
1433 * Reads hdr_field to determine which headers are needed by firmware.
1434 * Builds a buffer containing these headers. Saves individual header
1435 * lengths and total buffer length to be used to build descriptors.
1436 */
1437static int build_hdr_data(u8 hdr_field, struct sk_buff *skb,
1438 int *hdr_len, u8 *hdr_data)
1439{
1440 int len = 0;
1441 u8 *hdr;
1442
Thomas Falconda75e3b2018-03-12 11:51:02 -05001443 if (skb_vlan_tagged(skb) && !skb_vlan_tag_present(skb))
1444 hdr_len[0] = sizeof(struct vlan_ethhdr);
1445 else
1446 hdr_len[0] = sizeof(struct ethhdr);
Thomas Falconad7775d2016-04-01 17:20:34 -05001447
1448 if (skb->protocol == htons(ETH_P_IP)) {
1449 hdr_len[1] = ip_hdr(skb)->ihl * 4;
1450 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
1451 hdr_len[2] = tcp_hdrlen(skb);
1452 else if (ip_hdr(skb)->protocol == IPPROTO_UDP)
1453 hdr_len[2] = sizeof(struct udphdr);
1454 } else if (skb->protocol == htons(ETH_P_IPV6)) {
1455 hdr_len[1] = sizeof(struct ipv6hdr);
1456 if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
1457 hdr_len[2] = tcp_hdrlen(skb);
1458 else if (ipv6_hdr(skb)->nexthdr == IPPROTO_UDP)
1459 hdr_len[2] = sizeof(struct udphdr);
Thomas Falcon4eb50ce2017-12-18 12:52:40 -06001460 } else if (skb->protocol == htons(ETH_P_ARP)) {
1461 hdr_len[1] = arp_hdr_len(skb->dev);
1462 hdr_len[2] = 0;
Thomas Falconad7775d2016-04-01 17:20:34 -05001463 }
1464
1465 memset(hdr_data, 0, 120);
1466 if ((hdr_field >> 6) & 1) {
1467 hdr = skb_mac_header(skb);
1468 memcpy(hdr_data, hdr, hdr_len[0]);
1469 len += hdr_len[0];
1470 }
1471
1472 if ((hdr_field >> 5) & 1) {
1473 hdr = skb_network_header(skb);
1474 memcpy(hdr_data + len, hdr, hdr_len[1]);
1475 len += hdr_len[1];
1476 }
1477
1478 if ((hdr_field >> 4) & 1) {
1479 hdr = skb_transport_header(skb);
1480 memcpy(hdr_data + len, hdr, hdr_len[2]);
1481 len += hdr_len[2];
1482 }
1483 return len;
1484}
1485
1486/**
1487 * create_hdr_descs - create header and header extension descriptors
Lee Jones80708602021-01-15 20:09:03 +00001488 * @hdr_field: bitfield determining needed headers
1489 * @hdr_data: buffer containing header data
1490 * @len: length of data buffer
1491 * @hdr_len: array of individual header lengths
1492 * @scrq_arr: descriptor array
Thomas Falconad7775d2016-04-01 17:20:34 -05001493 *
1494 * Creates header and, if needed, header extension descriptors and
1495 * places them in a descriptor array, scrq_arr
1496 */
1497
Thomas Falcon2de09682017-10-16 10:02:11 -05001498static int create_hdr_descs(u8 hdr_field, u8 *hdr_data, int len, int *hdr_len,
1499 union sub_crq *scrq_arr)
Thomas Falconad7775d2016-04-01 17:20:34 -05001500{
1501 union sub_crq hdr_desc;
1502 int tmp_len = len;
Thomas Falcon2de09682017-10-16 10:02:11 -05001503 int num_descs = 0;
Thomas Falconad7775d2016-04-01 17:20:34 -05001504 u8 *data, *cur;
1505 int tmp;
1506
1507 while (tmp_len > 0) {
1508 cur = hdr_data + len - tmp_len;
1509
1510 memset(&hdr_desc, 0, sizeof(hdr_desc));
1511 if (cur != hdr_data) {
1512 data = hdr_desc.hdr_ext.data;
1513 tmp = tmp_len > 29 ? 29 : tmp_len;
1514 hdr_desc.hdr_ext.first = IBMVNIC_CRQ_CMD;
1515 hdr_desc.hdr_ext.type = IBMVNIC_HDR_EXT_DESC;
1516 hdr_desc.hdr_ext.len = tmp;
1517 } else {
1518 data = hdr_desc.hdr.data;
1519 tmp = tmp_len > 24 ? 24 : tmp_len;
1520 hdr_desc.hdr.first = IBMVNIC_CRQ_CMD;
1521 hdr_desc.hdr.type = IBMVNIC_HDR_DESC;
1522 hdr_desc.hdr.len = tmp;
1523 hdr_desc.hdr.l2_len = (u8)hdr_len[0];
1524 hdr_desc.hdr.l3_len = cpu_to_be16((u16)hdr_len[1]);
1525 hdr_desc.hdr.l4_len = (u8)hdr_len[2];
1526 hdr_desc.hdr.flag = hdr_field << 1;
1527 }
1528 memcpy(data, cur, tmp);
1529 tmp_len -= tmp;
1530 *scrq_arr = hdr_desc;
1531 scrq_arr++;
Thomas Falcon2de09682017-10-16 10:02:11 -05001532 num_descs++;
Thomas Falconad7775d2016-04-01 17:20:34 -05001533 }
Thomas Falcon2de09682017-10-16 10:02:11 -05001534
1535 return num_descs;
Thomas Falconad7775d2016-04-01 17:20:34 -05001536}
1537
1538/**
1539 * build_hdr_descs_arr - build a header descriptor array
Lee Jones80708602021-01-15 20:09:03 +00001540 * @txbuff: tx buffer
1541 * @num_entries: number of descriptors to be sent
1542 * @hdr_field: bit field determining which headers will be sent
Thomas Falconad7775d2016-04-01 17:20:34 -05001543 *
1544 * This function will build a TX descriptor array with applicable
1545 * L2/L3/L4 packet header descriptors to be sent by send_subcrq_indirect.
1546 */
1547
Thomas Falconc62aa372020-11-18 19:12:20 -06001548static void build_hdr_descs_arr(struct sk_buff *skb,
1549 union sub_crq *indir_arr,
Thomas Falconad7775d2016-04-01 17:20:34 -05001550 int *num_entries, u8 hdr_field)
1551{
1552 int hdr_len[3] = {0, 0, 0};
Thomas Falconc62aa372020-11-18 19:12:20 -06001553 u8 hdr_data[140] = {0};
Thomas Falcon2de09682017-10-16 10:02:11 -05001554 int tot_len;
Thomas Falconad7775d2016-04-01 17:20:34 -05001555
Thomas Falconc62aa372020-11-18 19:12:20 -06001556 tot_len = build_hdr_data(hdr_field, skb, hdr_len,
1557 hdr_data);
Thomas Falcon2de09682017-10-16 10:02:11 -05001558 *num_entries += create_hdr_descs(hdr_field, hdr_data, tot_len, hdr_len,
Thomas Falconc62aa372020-11-18 19:12:20 -06001559 indir_arr + 1);
Thomas Falconad7775d2016-04-01 17:20:34 -05001560}
1561
Thomas Falcon1f247a62018-03-12 11:51:04 -05001562static int ibmvnic_xmit_workarounds(struct sk_buff *skb,
1563 struct net_device *netdev)
1564{
1565 /* For some backing devices, mishandling of small packets
1566 * can result in a loss of connection or TX stall. Device
1567 * architects recommend that no packet should be smaller
1568 * than the minimum MTU value provided to the driver, so
1569 * pad any packets to that length
1570 */
1571 if (skb->len < netdev->min_mtu)
1572 return skb_put_padto(skb, netdev->min_mtu);
Thomas Falcon7083a452018-03-12 21:05:26 -05001573
1574 return 0;
Thomas Falcon1f247a62018-03-12 11:51:04 -05001575}
1576
Thomas Falcon0d973382020-11-18 19:12:19 -06001577static void ibmvnic_tx_scrq_clean_buffer(struct ibmvnic_adapter *adapter,
1578 struct ibmvnic_sub_crq_queue *tx_scrq)
1579{
1580 struct ibmvnic_ind_xmit_queue *ind_bufp;
1581 struct ibmvnic_tx_buff *tx_buff;
1582 struct ibmvnic_tx_pool *tx_pool;
1583 union sub_crq tx_scrq_entry;
1584 int queue_num;
1585 int entries;
1586 int index;
1587 int i;
1588
1589 ind_bufp = &tx_scrq->ind_buf;
1590 entries = (u64)ind_bufp->index;
1591 queue_num = tx_scrq->pool_index;
1592
1593 for (i = entries - 1; i >= 0; --i) {
1594 tx_scrq_entry = ind_bufp->indir_arr[i];
1595 if (tx_scrq_entry.v1.type != IBMVNIC_TX_DESC)
1596 continue;
1597 index = be32_to_cpu(tx_scrq_entry.v1.correlator);
1598 if (index & IBMVNIC_TSO_POOL_MASK) {
1599 tx_pool = &adapter->tso_pool[queue_num];
1600 index &= ~IBMVNIC_TSO_POOL_MASK;
1601 } else {
1602 tx_pool = &adapter->tx_pool[queue_num];
1603 }
1604 tx_pool->free_map[tx_pool->consumer_index] = index;
1605 tx_pool->consumer_index = tx_pool->consumer_index == 0 ?
1606 tx_pool->num_buffers - 1 :
1607 tx_pool->consumer_index - 1;
1608 tx_buff = &tx_pool->tx_buff[index];
1609 adapter->netdev->stats.tx_packets--;
1610 adapter->netdev->stats.tx_bytes -= tx_buff->skb->len;
1611 adapter->tx_stats_buffers[queue_num].packets--;
1612 adapter->tx_stats_buffers[queue_num].bytes -=
1613 tx_buff->skb->len;
1614 dev_kfree_skb_any(tx_buff->skb);
1615 tx_buff->skb = NULL;
1616 adapter->netdev->stats.tx_dropped++;
1617 }
1618 ind_bufp->index = 0;
1619 if (atomic_sub_return(entries, &tx_scrq->used) <=
1620 (adapter->req_tx_entries_per_subcrq / 2) &&
1621 __netif_subqueue_stopped(adapter->netdev, queue_num)) {
1622 netif_wake_subqueue(adapter->netdev, queue_num);
1623 netdev_dbg(adapter->netdev, "Started queue %d\n",
1624 queue_num);
1625 }
1626}
1627
1628static int ibmvnic_tx_scrq_flush(struct ibmvnic_adapter *adapter,
1629 struct ibmvnic_sub_crq_queue *tx_scrq)
1630{
1631 struct ibmvnic_ind_xmit_queue *ind_bufp;
1632 u64 dma_addr;
1633 u64 entries;
1634 u64 handle;
1635 int rc;
1636
1637 ind_bufp = &tx_scrq->ind_buf;
1638 dma_addr = (u64)ind_bufp->indir_dma;
1639 entries = (u64)ind_bufp->index;
1640 handle = tx_scrq->handle;
1641
1642 if (!entries)
1643 return 0;
1644 rc = send_subcrq_indirect(adapter, handle, dma_addr, entries);
1645 if (rc)
1646 ibmvnic_tx_scrq_clean_buffer(adapter, tx_scrq);
1647 else
1648 ind_bufp->index = 0;
1649 return 0;
1650}
1651
YueHaibing94b2bb22018-09-18 14:35:47 +08001652static netdev_tx_t ibmvnic_xmit(struct sk_buff *skb, struct net_device *netdev)
Thomas Falcon032c5e82015-12-21 11:26:06 -06001653{
1654 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1655 int queue_num = skb_get_queue_mapping(skb);
Thomas Falconad7775d2016-04-01 17:20:34 -05001656 u8 *hdrs = (u8 *)&adapter->tx_rx_desc_req;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001657 struct device *dev = &adapter->vdev->dev;
Thomas Falcon0d973382020-11-18 19:12:19 -06001658 struct ibmvnic_ind_xmit_queue *ind_bufp;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001659 struct ibmvnic_tx_buff *tx_buff = NULL;
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001660 struct ibmvnic_sub_crq_queue *tx_scrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001661 struct ibmvnic_tx_pool *tx_pool;
1662 unsigned int tx_send_failed = 0;
Thomas Falcon0d973382020-11-18 19:12:19 -06001663 netdev_tx_t ret = NETDEV_TX_OK;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001664 unsigned int tx_map_failed = 0;
Thomas Falconc62aa372020-11-18 19:12:20 -06001665 union sub_crq indir_arr[16];
Thomas Falcon032c5e82015-12-21 11:26:06 -06001666 unsigned int tx_dropped = 0;
1667 unsigned int tx_packets = 0;
1668 unsigned int tx_bytes = 0;
1669 dma_addr_t data_dma_addr;
1670 struct netdev_queue *txq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001671 unsigned long lpar_rc;
1672 union sub_crq tx_crq;
1673 unsigned int offset;
Thomas Falconad7775d2016-04-01 17:20:34 -05001674 int num_entries = 1;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001675 unsigned char *dst;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001676 int index = 0;
Thomas Falcona0dca102018-01-18 19:29:48 -06001677 u8 proto = 0;
Thomas Falcon0d973382020-11-18 19:12:19 -06001678
1679 tx_scrq = adapter->tx_scrq[queue_num];
1680 txq = netdev_get_tx_queue(netdev, queue_num);
1681 ind_bufp = &tx_scrq->ind_buf;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001682
Juliet Kim7ed5b312019-09-20 16:11:23 -04001683 if (test_bit(0, &adapter->resetting)) {
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001684 if (!netif_subqueue_stopped(netdev, skb))
1685 netif_stop_subqueue(netdev, queue_num);
1686 dev_kfree_skb_any(skb);
1687
Thomas Falcon032c5e82015-12-21 11:26:06 -06001688 tx_send_failed++;
1689 tx_dropped++;
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001690 ret = NETDEV_TX_OK;
Thomas Falcon0d973382020-11-18 19:12:19 -06001691 ibmvnic_tx_scrq_flush(adapter, tx_scrq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001692 goto out;
1693 }
1694
Thomas Falcon7083a452018-03-12 21:05:26 -05001695 if (ibmvnic_xmit_workarounds(skb, netdev)) {
Thomas Falcon1f247a62018-03-12 11:51:04 -05001696 tx_dropped++;
1697 tx_send_failed++;
1698 ret = NETDEV_TX_OK;
Thomas Falcon0d973382020-11-18 19:12:19 -06001699 ibmvnic_tx_scrq_flush(adapter, tx_scrq);
Thomas Falcon1f247a62018-03-12 11:51:04 -05001700 goto out;
1701 }
Thomas Falcon06b3e352018-03-16 20:00:28 -05001702 if (skb_is_gso(skb))
1703 tx_pool = &adapter->tso_pool[queue_num];
1704 else
1705 tx_pool = &adapter->tx_pool[queue_num];
Thomas Falcon1f247a62018-03-12 11:51:04 -05001706
Thomas Falcon032c5e82015-12-21 11:26:06 -06001707 index = tx_pool->free_map[tx_pool->consumer_index];
Thomas Falconfdb06102017-10-17 12:36:55 -05001708
Thomas Falcon86b61a52018-03-16 20:00:29 -05001709 if (index == IBMVNIC_INVALID_MAP) {
1710 dev_kfree_skb_any(skb);
1711 tx_send_failed++;
1712 tx_dropped++;
1713 ret = NETDEV_TX_OK;
Thomas Falcon0d973382020-11-18 19:12:19 -06001714 ibmvnic_tx_scrq_flush(adapter, tx_scrq);
Thomas Falcon86b61a52018-03-16 20:00:29 -05001715 goto out;
1716 }
1717
1718 tx_pool->free_map[tx_pool->consumer_index] = IBMVNIC_INVALID_MAP;
1719
Thomas Falcon06b3e352018-03-16 20:00:28 -05001720 offset = index * tx_pool->buf_size;
1721 dst = tx_pool->long_term_buff.buff + offset;
1722 memset(dst, 0, tx_pool->buf_size);
1723 data_dma_addr = tx_pool->long_term_buff.addr + offset;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001724
Thomas Falcon15482052017-10-17 12:36:54 -05001725 if (skb_shinfo(skb)->nr_frags) {
1726 int cur, i;
1727
1728 /* Copy the head */
1729 skb_copy_from_linear_data(skb, dst, skb_headlen(skb));
1730 cur = skb_headlen(skb);
1731
1732 /* Copy the frags */
1733 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1734 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1735
Christophe JAILLETc3105f82021-04-04 10:54:37 +02001736 memcpy(dst + cur, skb_frag_address(frag),
1737 skb_frag_size(frag));
Thomas Falcon15482052017-10-17 12:36:54 -05001738 cur += skb_frag_size(frag);
1739 }
1740 } else {
1741 skb_copy_from_linear_data(skb, dst, skb->len);
1742 }
1743
Lijun Pan42557da2021-02-12 20:48:40 -06001744 /* post changes to long_term_buff *dst before VIOS accessing it */
1745 dma_wmb();
1746
Thomas Falcon032c5e82015-12-21 11:26:06 -06001747 tx_pool->consumer_index =
Thomas Falcon06b3e352018-03-16 20:00:28 -05001748 (tx_pool->consumer_index + 1) % tx_pool->num_buffers;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001749
1750 tx_buff = &tx_pool->tx_buff[index];
1751 tx_buff->skb = skb;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001752 tx_buff->index = index;
1753 tx_buff->pool_index = queue_num;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001754
1755 memset(&tx_crq, 0, sizeof(tx_crq));
1756 tx_crq.v1.first = IBMVNIC_CRQ_CMD;
1757 tx_crq.v1.type = IBMVNIC_TX_DESC;
1758 tx_crq.v1.n_crq_elem = 1;
1759 tx_crq.v1.n_sge = 1;
1760 tx_crq.v1.flags1 = IBMVNIC_TX_COMP_NEEDED;
Thomas Falcon06b3e352018-03-16 20:00:28 -05001761
Thomas Falconfdb06102017-10-17 12:36:55 -05001762 if (skb_is_gso(skb))
Thomas Falcon06b3e352018-03-16 20:00:28 -05001763 tx_crq.v1.correlator =
1764 cpu_to_be32(index | IBMVNIC_TSO_POOL_MASK);
Thomas Falconfdb06102017-10-17 12:36:55 -05001765 else
Thomas Falcon06b3e352018-03-16 20:00:28 -05001766 tx_crq.v1.correlator = cpu_to_be32(index);
1767 tx_crq.v1.dma_reg = cpu_to_be16(tx_pool->long_term_buff.map_id);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001768 tx_crq.v1.sge_len = cpu_to_be32(skb->len);
1769 tx_crq.v1.ioba = cpu_to_be64(data_dma_addr);
1770
Michał Mirosławe84b4792018-11-07 17:50:52 +01001771 if (adapter->vlan_header_insertion && skb_vlan_tag_present(skb)) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06001772 tx_crq.v1.flags2 |= IBMVNIC_TX_VLAN_INSERT;
1773 tx_crq.v1.vlan_id = cpu_to_be16(skb->vlan_tci);
1774 }
1775
1776 if (skb->protocol == htons(ETH_P_IP)) {
Thomas Falcona0dca102018-01-18 19:29:48 -06001777 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV4;
1778 proto = ip_hdr(skb)->protocol;
1779 } else if (skb->protocol == htons(ETH_P_IPV6)) {
1780 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV6;
1781 proto = ipv6_hdr(skb)->nexthdr;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001782 }
1783
Thomas Falcona0dca102018-01-18 19:29:48 -06001784 if (proto == IPPROTO_TCP)
1785 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_TCP;
1786 else if (proto == IPPROTO_UDP)
1787 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_UDP;
1788
Thomas Falconad7775d2016-04-01 17:20:34 -05001789 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06001790 tx_crq.v1.flags1 |= IBMVNIC_TX_CHKSUM_OFFLOAD;
Thomas Falconad7775d2016-04-01 17:20:34 -05001791 hdrs += 2;
1792 }
Thomas Falconfdb06102017-10-17 12:36:55 -05001793 if (skb_is_gso(skb)) {
1794 tx_crq.v1.flags1 |= IBMVNIC_TX_LSO;
1795 tx_crq.v1.mss = cpu_to_be16(skb_shinfo(skb)->gso_size);
1796 hdrs += 2;
1797 }
Thomas Falcon0d973382020-11-18 19:12:19 -06001798
1799 if ((*hdrs >> 7) & 1)
Thomas Falconc62aa372020-11-18 19:12:20 -06001800 build_hdr_descs_arr(skb, indir_arr, &num_entries, *hdrs);
Thomas Falcon0d973382020-11-18 19:12:19 -06001801
1802 tx_crq.v1.n_crq_elem = num_entries;
1803 tx_buff->num_entries = num_entries;
1804 /* flush buffer if current entry can not fit */
1805 if (num_entries + ind_bufp->index > IBMVNIC_MAX_IND_DESCS) {
1806 lpar_rc = ibmvnic_tx_scrq_flush(adapter, tx_scrq);
1807 if (lpar_rc != H_SUCCESS)
1808 goto tx_flush_err;
Thomas Falconad7775d2016-04-01 17:20:34 -05001809 }
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001810
Thomas Falconc62aa372020-11-18 19:12:20 -06001811 indir_arr[0] = tx_crq;
1812 memcpy(&ind_bufp->indir_arr[ind_bufp->index], &indir_arr[0],
Thomas Falcon0d973382020-11-18 19:12:19 -06001813 num_entries * sizeof(struct ibmvnic_generic_scrq));
1814 ind_bufp->index += num_entries;
1815 if (__netdev_tx_sent_queue(txq, skb->len,
1816 netdev_xmit_more() &&
1817 ind_bufp->index < IBMVNIC_MAX_IND_DESCS)) {
1818 lpar_rc = ibmvnic_tx_scrq_flush(adapter, tx_scrq);
1819 if (lpar_rc != H_SUCCESS)
1820 goto tx_err;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001821 }
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001822
Thomas Falconffc385b2018-02-18 10:08:41 -06001823 if (atomic_add_return(num_entries, &tx_scrq->used)
Brian King58c8c0c2017-04-19 13:44:47 -04001824 >= adapter->req_tx_entries_per_subcrq) {
Thomas Falcon0aecb132018-02-26 18:10:58 -06001825 netdev_dbg(netdev, "Stopping queue %d\n", queue_num);
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001826 netif_stop_subqueue(netdev, queue_num);
1827 }
1828
Thomas Falcon032c5e82015-12-21 11:26:06 -06001829 tx_packets++;
1830 tx_bytes += skb->len;
1831 txq->trans_start = jiffies;
1832 ret = NETDEV_TX_OK;
Thomas Falcon86b61a52018-03-16 20:00:29 -05001833 goto out;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001834
Thomas Falcon0d973382020-11-18 19:12:19 -06001835tx_flush_err:
1836 dev_kfree_skb_any(skb);
1837 tx_buff->skb = NULL;
1838 tx_pool->consumer_index = tx_pool->consumer_index == 0 ?
1839 tx_pool->num_buffers - 1 :
1840 tx_pool->consumer_index - 1;
1841 tx_dropped++;
1842tx_err:
1843 if (lpar_rc != H_CLOSED && lpar_rc != H_PARAMETER)
1844 dev_err_ratelimited(dev, "tx: send failed\n");
1845
1846 if (lpar_rc == H_CLOSED || adapter->failover_pending) {
1847 /* Disable TX and report carrier off if queue is closed
1848 * or pending failover.
1849 * Firmware guarantees that a signal will be sent to the
1850 * driver, triggering a reset or some other action.
1851 */
1852 netif_tx_stop_all_queues(netdev);
1853 netif_carrier_off(netdev);
1854 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001855out:
1856 netdev->stats.tx_dropped += tx_dropped;
1857 netdev->stats.tx_bytes += tx_bytes;
1858 netdev->stats.tx_packets += tx_packets;
1859 adapter->tx_send_failed += tx_send_failed;
1860 adapter->tx_map_failed += tx_map_failed;
John Allen3d52b592017-08-02 16:44:14 -05001861 adapter->tx_stats_buffers[queue_num].packets += tx_packets;
1862 adapter->tx_stats_buffers[queue_num].bytes += tx_bytes;
1863 adapter->tx_stats_buffers[queue_num].dropped_packets += tx_dropped;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001864
1865 return ret;
1866}
1867
1868static void ibmvnic_set_multi(struct net_device *netdev)
1869{
1870 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1871 struct netdev_hw_addr *ha;
1872 union ibmvnic_crq crq;
1873
1874 memset(&crq, 0, sizeof(crq));
1875 crq.request_capability.first = IBMVNIC_CRQ_CMD;
1876 crq.request_capability.cmd = REQUEST_CAPABILITY;
1877
1878 if (netdev->flags & IFF_PROMISC) {
1879 if (!adapter->promisc_supported)
1880 return;
1881 } else {
1882 if (netdev->flags & IFF_ALLMULTI) {
1883 /* Accept all multicast */
1884 memset(&crq, 0, sizeof(crq));
1885 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1886 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1887 crq.multicast_ctrl.flags = IBMVNIC_ENABLE_ALL;
1888 ibmvnic_send_crq(adapter, &crq);
1889 } else if (netdev_mc_empty(netdev)) {
1890 /* Reject all multicast */
1891 memset(&crq, 0, sizeof(crq));
1892 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1893 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1894 crq.multicast_ctrl.flags = IBMVNIC_DISABLE_ALL;
1895 ibmvnic_send_crq(adapter, &crq);
1896 } else {
1897 /* Accept one or more multicast(s) */
1898 netdev_for_each_mc_addr(ha, netdev) {
1899 memset(&crq, 0, sizeof(crq));
1900 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1901 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1902 crq.multicast_ctrl.flags = IBMVNIC_ENABLE_MC;
1903 ether_addr_copy(&crq.multicast_ctrl.mac_addr[0],
1904 ha->addr);
1905 ibmvnic_send_crq(adapter, &crq);
1906 }
1907 }
1908 }
1909}
1910
Thomas Falcon62740e92019-05-09 23:13:43 -05001911static int __ibmvnic_set_mac(struct net_device *netdev, u8 *dev_addr)
Thomas Falcon032c5e82015-12-21 11:26:06 -06001912{
1913 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001914 union ibmvnic_crq crq;
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05001915 int rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001916
Thomas Falcon62740e92019-05-09 23:13:43 -05001917 if (!is_valid_ether_addr(dev_addr)) {
1918 rc = -EADDRNOTAVAIL;
1919 goto err;
1920 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001921
1922 memset(&crq, 0, sizeof(crq));
1923 crq.change_mac_addr.first = IBMVNIC_CRQ_CMD;
1924 crq.change_mac_addr.cmd = CHANGE_MAC_ADDR;
Thomas Falcon62740e92019-05-09 23:13:43 -05001925 ether_addr_copy(&crq.change_mac_addr.mac_addr[0], dev_addr);
Thomas Falconf8136142018-01-29 13:45:05 -06001926
Thomas Falconff25dcb2019-11-25 17:12:56 -06001927 mutex_lock(&adapter->fw_lock);
1928 adapter->fw_done_rc = 0;
Thomas Falcon070eca92019-11-25 17:12:53 -06001929 reinit_completion(&adapter->fw_done);
Thomas Falconff25dcb2019-11-25 17:12:56 -06001930
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05001931 rc = ibmvnic_send_crq(adapter, &crq);
Thomas Falcon62740e92019-05-09 23:13:43 -05001932 if (rc) {
1933 rc = -EIO;
Thomas Falconff25dcb2019-11-25 17:12:56 -06001934 mutex_unlock(&adapter->fw_lock);
Thomas Falcon62740e92019-05-09 23:13:43 -05001935 goto err;
1936 }
1937
Thomas Falcon476d96c2019-11-25 17:12:55 -06001938 rc = ibmvnic_wait_for_completion(adapter, &adapter->fw_done, 10000);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001939 /* netdev->dev_addr is changed in handle_change_mac_rsp function */
Thomas Falcon476d96c2019-11-25 17:12:55 -06001940 if (rc || adapter->fw_done_rc) {
Thomas Falcon62740e92019-05-09 23:13:43 -05001941 rc = -EIO;
Thomas Falconff25dcb2019-11-25 17:12:56 -06001942 mutex_unlock(&adapter->fw_lock);
Thomas Falcon62740e92019-05-09 23:13:43 -05001943 goto err;
1944 }
Thomas Falconff25dcb2019-11-25 17:12:56 -06001945 mutex_unlock(&adapter->fw_lock);
Thomas Falcon62740e92019-05-09 23:13:43 -05001946 return 0;
1947err:
1948 ether_addr_copy(adapter->mac_addr, netdev->dev_addr);
1949 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001950}
1951
John Allenc26eba02017-10-26 16:23:25 -05001952static int ibmvnic_set_mac(struct net_device *netdev, void *p)
1953{
1954 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1955 struct sockaddr *addr = p;
Thomas Falconf8136142018-01-29 13:45:05 -06001956 int rc;
John Allenc26eba02017-10-26 16:23:25 -05001957
Thomas Falcon62740e92019-05-09 23:13:43 -05001958 rc = 0;
Lijun Pan8fc36722020-10-27 17:04:56 -05001959 if (!is_valid_ether_addr(addr->sa_data))
1960 return -EADDRNOTAVAIL;
1961
Jiri Wiesner67eb2112021-03-04 17:18:28 +01001962 ether_addr_copy(adapter->mac_addr, addr->sa_data);
1963 if (adapter->state != VNIC_PROBED)
Thomas Falcon62740e92019-05-09 23:13:43 -05001964 rc = __ibmvnic_set_mac(netdev, addr->sa_data);
John Allenc26eba02017-10-26 16:23:25 -05001965
Thomas Falconf8136142018-01-29 13:45:05 -06001966 return rc;
John Allenc26eba02017-10-26 16:23:25 -05001967}
1968
Lijun Pancaee7bf2021-04-12 02:41:27 -05001969static const char *reset_reason_to_string(enum ibmvnic_reset_reason reason)
1970{
1971 switch (reason) {
1972 case VNIC_RESET_FAILOVER:
1973 return "FAILOVER";
1974 case VNIC_RESET_MOBILITY:
1975 return "MOBILITY";
1976 case VNIC_RESET_FATAL:
1977 return "FATAL";
1978 case VNIC_RESET_NON_FATAL:
1979 return "NON_FATAL";
1980 case VNIC_RESET_TIMEOUT:
1981 return "TIMEOUT";
1982 case VNIC_RESET_CHANGE_PARAM:
1983 return "CHANGE_PARAM";
1984 default:
1985 return "UNKNOWN";
1986 }
1987}
1988
Lee Jones80708602021-01-15 20:09:03 +00001989/*
Nathan Fontenoted651a12017-05-03 14:04:38 -04001990 * do_reset returns zero if we are able to keep processing reset events, or
1991 * non-zero if we hit a fatal error and must halt.
1992 */
1993static int do_reset(struct ibmvnic_adapter *adapter,
1994 struct ibmvnic_rwi *rwi, u32 reset_state)
1995{
John Allen896d8692018-01-18 16:26:31 -06001996 u64 old_num_rx_queues, old_num_tx_queues;
Thomas Falcon5bf032e2018-11-21 11:17:59 -06001997 u64 old_num_rx_slots, old_num_tx_slots;
Nathan Fontenoted651a12017-05-03 14:04:38 -04001998 struct net_device *netdev = adapter->netdev;
Lijun Pand3a6abc2021-04-14 02:46:15 -05001999 int rc;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002000
Sukadev Bhattiprolu38bd5ce2020-12-04 18:22:35 -08002001 netdev_dbg(adapter->netdev,
Lijun Pan0666ef72021-04-12 02:41:28 -05002002 "[S:%s FOP:%d] Reset reason: %s, reset_state: %s\n",
2003 adapter_state_to_string(adapter->state),
2004 adapter->failover_pending,
2005 reset_reason_to_string(rwi->reset_reason),
2006 adapter_state_to_string(reset_state));
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002007
Lijun Pan3f5ec372021-01-06 15:35:14 -06002008 adapter->reset_reason = rwi->reset_reason;
2009 /* requestor of VNIC_RESET_CHANGE_PARAM already has the rtnl lock */
2010 if (!(adapter->reset_reason == VNIC_RESET_CHANGE_PARAM))
2011 rtnl_lock();
2012
Lijun Panbab08be2021-02-11 00:43:19 -06002013 /* Now that we have the rtnl lock, clear any pending failover.
Sukadev Bhattiprolu1d850492020-10-30 10:07:11 -07002014 * This will ensure ibmvnic_open() has either completed or will
2015 * block until failover is complete.
2016 */
2017 if (rwi->reset_reason == VNIC_RESET_FAILOVER)
2018 adapter->failover_pending = false;
Juliet Kimb27507b2019-09-20 16:11:22 -04002019
Sukadev Bhattiprolu8f1c0fd2021-02-23 21:02:29 -08002020 /* read the state and check (again) after getting rtnl */
2021 reset_state = adapter->state;
2022
2023 if (reset_state == VNIC_REMOVING || reset_state == VNIC_REMOVED) {
2024 rc = -EBUSY;
2025 goto out;
2026 }
2027
Nathan Fontenoted651a12017-05-03 14:04:38 -04002028 netif_carrier_off(netdev);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002029
John Allen896d8692018-01-18 16:26:31 -06002030 old_num_rx_queues = adapter->req_rx_queues;
2031 old_num_tx_queues = adapter->req_tx_queues;
Thomas Falcon5bf032e2018-11-21 11:17:59 -06002032 old_num_rx_slots = adapter->req_rx_add_entries_per_subcrq;
2033 old_num_tx_slots = adapter->req_tx_entries_per_subcrq;
John Allen896d8692018-01-18 16:26:31 -06002034
Nathan Fontenot30f79622018-04-06 18:37:06 -05002035 ibmvnic_cleanup(netdev);
2036
Thomas Falcon1f946082019-06-07 16:03:53 -05002037 if (reset_state == VNIC_OPEN &&
2038 adapter->reset_reason != VNIC_RESET_MOBILITY &&
Nathan Fontenot30f79622018-04-06 18:37:06 -05002039 adapter->reset_reason != VNIC_RESET_FAILOVER) {
Lijun Pan3f5ec372021-01-06 15:35:14 -06002040 if (adapter->reset_reason == VNIC_RESET_CHANGE_PARAM) {
2041 rc = __ibmvnic_close(netdev);
2042 if (rc)
2043 goto out;
2044 } else {
2045 adapter->state = VNIC_CLOSING;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002046
Lijun Pan3f5ec372021-01-06 15:35:14 -06002047 /* Release the RTNL lock before link state change and
2048 * re-acquire after the link state change to allow
2049 * linkwatch_event to grab the RTNL lock and run during
2050 * a reset.
2051 */
2052 rtnl_unlock();
2053 rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_DN);
2054 rtnl_lock();
2055 if (rc)
2056 goto out;
Juliet Kimb27507b2019-09-20 16:11:22 -04002057
Sukadev Bhattiprolu8f1c0fd2021-02-23 21:02:29 -08002058 if (adapter->state == VNIC_OPEN) {
2059 /* When we dropped rtnl, ibmvnic_open() got
2060 * it and noticed that we are resetting and
2061 * set the adapter state to OPEN. Update our
2062 * new "target" state, and resume the reset
2063 * from VNIC_CLOSING state.
2064 */
2065 netdev_dbg(netdev,
Lijun Pan0666ef72021-04-12 02:41:28 -05002066 "Open changed state from %s, updating.\n",
2067 adapter_state_to_string(reset_state));
Sukadev Bhattiprolu8f1c0fd2021-02-23 21:02:29 -08002068 reset_state = VNIC_OPEN;
2069 adapter->state = VNIC_CLOSING;
2070 }
2071
Lijun Pan3f5ec372021-01-06 15:35:14 -06002072 if (adapter->state != VNIC_CLOSING) {
Sukadev Bhattiprolu8f1c0fd2021-02-23 21:02:29 -08002073 /* If someone else changed the adapter state
2074 * when we dropped the rtnl, fail the reset
2075 */
Lijun Pan3f5ec372021-01-06 15:35:14 -06002076 rc = -1;
2077 goto out;
2078 }
Lijun Pan3f5ec372021-01-06 15:35:14 -06002079 adapter->state = VNIC_CLOSED;
Juliet Kimb27507b2019-09-20 16:11:22 -04002080 }
Lijun Pan3f5ec372021-01-06 15:35:14 -06002081 }
Juliet Kimb27507b2019-09-20 16:11:22 -04002082
Lijun Pan3f5ec372021-01-06 15:35:14 -06002083 if (adapter->reset_reason == VNIC_RESET_CHANGE_PARAM) {
2084 release_resources(adapter);
2085 release_sub_crqs(adapter, 1);
2086 release_crq_queue(adapter);
John Allenc26eba02017-10-26 16:23:25 -05002087 }
2088
John Allen8cb31cf2017-05-26 10:30:37 -04002089 if (adapter->reset_reason != VNIC_RESET_NON_FATAL) {
2090 /* remove the closed state so when we call open it appears
2091 * we are coming from the probed state.
2092 */
Nathan Fontenoted651a12017-05-03 14:04:38 -04002093 adapter->state = VNIC_PROBED;
John Allen8cb31cf2017-05-26 10:30:37 -04002094
Lijun Pan3f5ec372021-01-06 15:35:14 -06002095 if (adapter->reset_reason == VNIC_RESET_CHANGE_PARAM) {
2096 rc = init_crq_queue(adapter);
2097 } else if (adapter->reset_reason == VNIC_RESET_MOBILITY) {
Nathan Fontenot30f79622018-04-06 18:37:06 -05002098 rc = ibmvnic_reenable_crq_queue(adapter);
2099 release_sub_crqs(adapter, 1);
2100 } else {
2101 rc = ibmvnic_reset_crq(adapter);
Dany Madden8b40eb732020-06-18 15:24:13 -04002102 if (rc == H_CLOSED || rc == H_SUCCESS) {
Nathan Fontenot30f79622018-04-06 18:37:06 -05002103 rc = vio_enable_interrupts(adapter->vdev);
Dany Madden8b40eb732020-06-18 15:24:13 -04002104 if (rc)
2105 netdev_err(adapter->netdev,
2106 "Reset failed to enable interrupts. rc=%d\n",
2107 rc);
2108 }
Nathan Fontenot30f79622018-04-06 18:37:06 -05002109 }
2110
2111 if (rc) {
2112 netdev_err(adapter->netdev,
Dany Madden8b40eb732020-06-18 15:24:13 -04002113 "Reset couldn't initialize crq. rc=%d\n", rc);
Juliet Kimb27507b2019-09-20 16:11:22 -04002114 goto out;
Nathan Fontenot30f79622018-04-06 18:37:06 -05002115 }
2116
Lijun Pan635e4422020-08-19 17:52:26 -05002117 rc = ibmvnic_reset_init(adapter, true);
Juliet Kimb27507b2019-09-20 16:11:22 -04002118 if (rc) {
2119 rc = IBMVNIC_INIT_FAILED;
2120 goto out;
2121 }
John Allen8cb31cf2017-05-26 10:30:37 -04002122
2123 /* If the adapter was in PROBE state prior to the reset,
2124 * exit here.
2125 */
Juliet Kimb27507b2019-09-20 16:11:22 -04002126 if (reset_state == VNIC_PROBED) {
2127 rc = 0;
2128 goto out;
2129 }
John Allen8cb31cf2017-05-26 10:30:37 -04002130
2131 rc = ibmvnic_login(netdev);
Lijun Panf78afaa2021-02-11 00:43:20 -06002132 if (rc)
Juliet Kimb27507b2019-09-20 16:11:22 -04002133 goto out;
John Allen8cb31cf2017-05-26 10:30:37 -04002134
Lijun Pan3f5ec372021-01-06 15:35:14 -06002135 if (adapter->reset_reason == VNIC_RESET_CHANGE_PARAM) {
2136 rc = init_resources(adapter);
2137 if (rc)
2138 goto out;
2139 } else if (adapter->req_rx_queues != old_num_rx_queues ||
Juliet Kimb27507b2019-09-20 16:11:22 -04002140 adapter->req_tx_queues != old_num_tx_queues ||
2141 adapter->req_rx_add_entries_per_subcrq !=
2142 old_num_rx_slots ||
2143 adapter->req_tx_entries_per_subcrq !=
Mingming Cao9f134572020-08-25 13:26:41 -04002144 old_num_tx_slots ||
2145 !adapter->rx_pool ||
2146 !adapter->tso_pool ||
2147 !adapter->tx_pool) {
John Allen896d8692018-01-18 16:26:31 -06002148 release_rx_pools(adapter);
2149 release_tx_pools(adapter);
Juliet Kima5681e22018-11-19 15:59:22 -06002150 release_napi(adapter);
2151 release_vpd_data(adapter);
2152
2153 rc = init_resources(adapter);
Thomas Falconf611a5b2018-08-30 13:19:53 -05002154 if (rc)
Juliet Kimb27507b2019-09-20 16:11:22 -04002155 goto out;
Nathan Fontenotd9043c12018-02-19 13:30:14 -06002156
John Allenc26eba02017-10-26 16:23:25 -05002157 } else {
2158 rc = reset_tx_pools(adapter);
Jakub Kicinski8ae4dff2020-09-04 21:07:49 -07002159 if (rc) {
Mingming Cao9f134572020-08-25 13:26:41 -04002160 netdev_dbg(adapter->netdev, "reset tx pools failed (%d)\n",
Lijun Pan91dc5d22021-02-11 00:43:22 -06002161 rc);
Juliet Kimb27507b2019-09-20 16:11:22 -04002162 goto out;
Jakub Kicinski8ae4dff2020-09-04 21:07:49 -07002163 }
Nathan Fontenot8c0543a2017-05-26 10:31:06 -04002164
John Allenc26eba02017-10-26 16:23:25 -05002165 rc = reset_rx_pools(adapter);
Jakub Kicinski8ae4dff2020-09-04 21:07:49 -07002166 if (rc) {
Mingming Cao9f134572020-08-25 13:26:41 -04002167 netdev_dbg(adapter->netdev, "reset rx pools failed (%d)\n",
Lijun Pan91dc5d22021-02-11 00:43:22 -06002168 rc);
Juliet Kimb27507b2019-09-20 16:11:22 -04002169 goto out;
Jakub Kicinski8ae4dff2020-09-04 21:07:49 -07002170 }
John Allenc26eba02017-10-26 16:23:25 -05002171 }
Thomas Falcon134bbe72018-05-16 15:49:04 -05002172 ibmvnic_disable_irqs(adapter);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002173 }
John Allene676d812018-03-14 10:41:29 -05002174 adapter->state = VNIC_CLOSED;
2175
Juliet Kimb27507b2019-09-20 16:11:22 -04002176 if (reset_state == VNIC_CLOSED) {
2177 rc = 0;
2178 goto out;
2179 }
John Allene676d812018-03-14 10:41:29 -05002180
Nathan Fontenoted651a12017-05-03 14:04:38 -04002181 rc = __ibmvnic_open(netdev);
2182 if (rc) {
Juliet Kimb27507b2019-09-20 16:11:22 -04002183 rc = IBMVNIC_OPEN_FAILED;
2184 goto out;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002185 }
2186
Thomas Falconbe32a242019-06-07 16:03:54 -05002187 /* refresh device's multicast list */
2188 ibmvnic_set_multi(netdev);
2189
Lijun Pan98025bc2020-11-20 16:40:12 -06002190 if (adapter->reset_reason == VNIC_RESET_FAILOVER ||
Lijun Pan6be46662020-12-14 15:19:29 -06002191 adapter->reset_reason == VNIC_RESET_MOBILITY)
2192 __netdev_notify_peers(netdev);
Nathan Fontenot61d3e1d2017-06-12 20:47:45 -04002193
Juliet Kimb27507b2019-09-20 16:11:22 -04002194 rc = 0;
2195
2196out:
Dany Madden0cb4bc62020-11-25 18:04:27 -06002197 /* restore the adapter state if reset failed */
2198 if (rc)
2199 adapter->state = reset_state;
Lijun Pan3f5ec372021-01-06 15:35:14 -06002200 /* requestor of VNIC_RESET_CHANGE_PARAM should still hold the rtnl lock */
2201 if (!(adapter->reset_reason == VNIC_RESET_CHANGE_PARAM))
2202 rtnl_unlock();
Juliet Kimb27507b2019-09-20 16:11:22 -04002203
Lijun Pan0666ef72021-04-12 02:41:28 -05002204 netdev_dbg(adapter->netdev, "[S:%s FOP:%d] Reset done, rc %d\n",
2205 adapter_state_to_string(adapter->state),
2206 adapter->failover_pending, rc);
Juliet Kimb27507b2019-09-20 16:11:22 -04002207 return rc;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002208}
2209
Thomas Falcon2770a792018-05-23 13:38:02 -05002210static int do_hard_reset(struct ibmvnic_adapter *adapter,
2211 struct ibmvnic_rwi *rwi, u32 reset_state)
2212{
2213 struct net_device *netdev = adapter->netdev;
2214 int rc;
2215
Lijun Pancaee7bf2021-04-12 02:41:27 -05002216 netdev_dbg(adapter->netdev, "Hard resetting driver (%s)\n",
2217 reset_reason_to_string(rwi->reset_reason));
Thomas Falcon2770a792018-05-23 13:38:02 -05002218
Sukadev Bhattiprolu8f1c0fd2021-02-23 21:02:29 -08002219 /* read the state and check (again) after getting rtnl */
2220 reset_state = adapter->state;
2221
2222 if (reset_state == VNIC_REMOVING || reset_state == VNIC_REMOVED) {
2223 rc = -EBUSY;
2224 goto out;
2225 }
2226
Thomas Falcon2770a792018-05-23 13:38:02 -05002227 netif_carrier_off(netdev);
2228 adapter->reset_reason = rwi->reset_reason;
2229
2230 ibmvnic_cleanup(netdev);
2231 release_resources(adapter);
2232 release_sub_crqs(adapter, 0);
2233 release_crq_queue(adapter);
2234
2235 /* remove the closed state so when we call open it appears
2236 * we are coming from the probed state.
2237 */
2238 adapter->state = VNIC_PROBED;
2239
Thomas Falconbbd669a2019-04-04 18:58:26 -05002240 reinit_completion(&adapter->init_done);
Thomas Falcon2770a792018-05-23 13:38:02 -05002241 rc = init_crq_queue(adapter);
2242 if (rc) {
2243 netdev_err(adapter->netdev,
2244 "Couldn't initialize crq. rc=%d\n", rc);
Dany Madden0cb4bc62020-11-25 18:04:27 -06002245 goto out;
Thomas Falcon2770a792018-05-23 13:38:02 -05002246 }
2247
Lijun Pan635e4422020-08-19 17:52:26 -05002248 rc = ibmvnic_reset_init(adapter, false);
Thomas Falcon2770a792018-05-23 13:38:02 -05002249 if (rc)
Dany Madden0cb4bc62020-11-25 18:04:27 -06002250 goto out;
Thomas Falcon2770a792018-05-23 13:38:02 -05002251
2252 /* If the adapter was in PROBE state prior to the reset,
2253 * exit here.
2254 */
2255 if (reset_state == VNIC_PROBED)
Dany Madden0cb4bc62020-11-25 18:04:27 -06002256 goto out;
Thomas Falcon2770a792018-05-23 13:38:02 -05002257
2258 rc = ibmvnic_login(netdev);
Dany Madden0cb4bc62020-11-25 18:04:27 -06002259 if (rc)
2260 goto out;
Juliet Kima5681e22018-11-19 15:59:22 -06002261
2262 rc = init_resources(adapter);
Thomas Falcon2770a792018-05-23 13:38:02 -05002263 if (rc)
Dany Madden0cb4bc62020-11-25 18:04:27 -06002264 goto out;
Thomas Falcon2770a792018-05-23 13:38:02 -05002265
2266 ibmvnic_disable_irqs(adapter);
2267 adapter->state = VNIC_CLOSED;
2268
2269 if (reset_state == VNIC_CLOSED)
Dany Madden0cb4bc62020-11-25 18:04:27 -06002270 goto out;
Thomas Falcon2770a792018-05-23 13:38:02 -05002271
2272 rc = __ibmvnic_open(netdev);
Dany Madden0cb4bc62020-11-25 18:04:27 -06002273 if (rc) {
2274 rc = IBMVNIC_OPEN_FAILED;
2275 goto out;
2276 }
Thomas Falcon2770a792018-05-23 13:38:02 -05002277
Lijun Pan6be46662020-12-14 15:19:29 -06002278 __netdev_notify_peers(netdev);
Dany Madden0cb4bc62020-11-25 18:04:27 -06002279out:
2280 /* restore adapter state if reset failed */
2281 if (rc)
2282 adapter->state = reset_state;
Lijun Pan0666ef72021-04-12 02:41:28 -05002283 netdev_dbg(adapter->netdev, "[S:%s FOP:%d] Hard reset done, rc %d\n",
2284 adapter_state_to_string(adapter->state),
2285 adapter->failover_pending, rc);
Dany Madden0cb4bc62020-11-25 18:04:27 -06002286 return rc;
Thomas Falcon2770a792018-05-23 13:38:02 -05002287}
2288
Nathan Fontenoted651a12017-05-03 14:04:38 -04002289static struct ibmvnic_rwi *get_next_rwi(struct ibmvnic_adapter *adapter)
2290{
2291 struct ibmvnic_rwi *rwi;
Thomas Falcon6c5c7482018-12-10 15:22:22 -06002292 unsigned long flags;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002293
Thomas Falcon6c5c7482018-12-10 15:22:22 -06002294 spin_lock_irqsave(&adapter->rwi_lock, flags);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002295
2296 if (!list_empty(&adapter->rwi_list)) {
2297 rwi = list_first_entry(&adapter->rwi_list, struct ibmvnic_rwi,
2298 list);
2299 list_del(&rwi->list);
2300 } else {
2301 rwi = NULL;
2302 }
2303
Thomas Falcon6c5c7482018-12-10 15:22:22 -06002304 spin_unlock_irqrestore(&adapter->rwi_lock, flags);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002305 return rwi;
2306}
2307
Nathan Fontenoted651a12017-05-03 14:04:38 -04002308static void __ibmvnic_reset(struct work_struct *work)
2309{
2310 struct ibmvnic_rwi *rwi;
2311 struct ibmvnic_adapter *adapter;
Juliet Kim7d7195a2020-03-10 09:23:58 -05002312 bool saved_state = false;
2313 unsigned long flags;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002314 u32 reset_state;
John Allenc26eba02017-10-26 16:23:25 -05002315 int rc = 0;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002316
2317 adapter = container_of(work, struct ibmvnic_adapter, ibmvnic_reset);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002318
Juliet Kim7ed5b312019-09-20 16:11:23 -04002319 if (test_and_set_bit_lock(0, &adapter->resetting)) {
Lijun Pan870e04a2021-04-13 14:33:39 -05002320 queue_delayed_work(system_long_wq,
2321 &adapter->ibmvnic_delayed_reset,
2322 IBMVNIC_RESET_DELAY);
Juliet Kim7ed5b312019-09-20 16:11:23 -04002323 return;
2324 }
2325
Nathan Fontenoted651a12017-05-03 14:04:38 -04002326 rwi = get_next_rwi(adapter);
2327 while (rwi) {
Juliet Kim7d7195a2020-03-10 09:23:58 -05002328 spin_lock_irqsave(&adapter->state_lock, flags);
2329
Thomas Falcon36f10312019-08-27 11:10:04 -05002330 if (adapter->state == VNIC_REMOVING ||
Michal Suchanekc8dc5592019-09-09 22:44:51 +02002331 adapter->state == VNIC_REMOVED) {
Juliet Kim7d7195a2020-03-10 09:23:58 -05002332 spin_unlock_irqrestore(&adapter->state_lock, flags);
Juliet Kim1c2977c2019-09-05 17:30:01 -04002333 kfree(rwi);
2334 rc = EBUSY;
2335 break;
2336 }
Thomas Falcon36f10312019-08-27 11:10:04 -05002337
Juliet Kim7d7195a2020-03-10 09:23:58 -05002338 if (!saved_state) {
2339 reset_state = adapter->state;
Juliet Kim7d7195a2020-03-10 09:23:58 -05002340 saved_state = true;
2341 }
2342 spin_unlock_irqrestore(&adapter->state_lock, flags);
2343
Lijun Pan3f5ec372021-01-06 15:35:14 -06002344 if (adapter->force_reset_recovery) {
Lijun Panbab08be2021-02-11 00:43:19 -06002345 /* Since we are doing a hard reset now, clear the
Sukadev Bhattiprolu1d850492020-10-30 10:07:11 -07002346 * failover_pending flag so we don't ignore any
2347 * future MOBILITY or other resets.
2348 */
2349 adapter->failover_pending = false;
2350
Juliet Kimb27507b2019-09-20 16:11:22 -04002351 /* Transport event occurred during previous reset */
2352 if (adapter->wait_for_reset) {
2353 /* Previous was CHANGE_PARAM; caller locked */
2354 adapter->force_reset_recovery = false;
2355 rc = do_hard_reset(adapter, rwi, reset_state);
2356 } else {
2357 rtnl_lock();
2358 adapter->force_reset_recovery = false;
2359 rc = do_hard_reset(adapter, rwi, reset_state);
2360 rtnl_unlock();
2361 }
Sukadev Bhattiproluf15fde92020-11-25 18:04:28 -06002362 if (rc) {
2363 /* give backing device time to settle down */
2364 netdev_dbg(adapter->netdev,
Lijun Pan0666ef72021-04-12 02:41:28 -05002365 "[S:%s] Hard reset failed, waiting 60 secs\n",
2366 adapter_state_to_string(adapter->state));
Sukadev Bhattiproluf15fde92020-11-25 18:04:28 -06002367 set_current_state(TASK_UNINTERRUPTIBLE);
2368 schedule_timeout(60 * HZ);
2369 }
Lijun Pan1f45dc22020-12-23 14:49:04 -06002370 } else {
Thomas Falcon2770a792018-05-23 13:38:02 -05002371 rc = do_reset(adapter, rwi, reset_state);
2372 }
Nathan Fontenoted651a12017-05-03 14:04:38 -04002373 kfree(rwi);
Dany Maddena86d5c62020-11-25 18:04:31 -06002374 adapter->last_reset_time = jiffies;
Dany Madden0cb4bc62020-11-25 18:04:27 -06002375
Dany Madden18f141b2020-11-25 18:04:25 -06002376 if (rc)
2377 netdev_dbg(adapter->netdev, "Reset failed, rc=%d\n", rc);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002378
2379 rwi = get_next_rwi(adapter);
Juliet Kim7ed5b312019-09-20 16:11:23 -04002380
2381 if (rwi && (rwi->reset_reason == VNIC_RESET_FAILOVER ||
2382 rwi->reset_reason == VNIC_RESET_MOBILITY))
2383 adapter->force_reset_recovery = true;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002384 }
2385
John Allenc26eba02017-10-26 16:23:25 -05002386 if (adapter->wait_for_reset) {
John Allenc26eba02017-10-26 16:23:25 -05002387 adapter->reset_done_rc = rc;
2388 complete(&adapter->reset_done);
2389 }
2390
Juliet Kim7ed5b312019-09-20 16:11:23 -04002391 clear_bit_unlock(0, &adapter->resetting);
Sukadev Bhattiprolu38bd5ce2020-12-04 18:22:35 -08002392
2393 netdev_dbg(adapter->netdev,
Lijun Pan0666ef72021-04-12 02:41:28 -05002394 "[S:%s FRR:%d WFR:%d] Done processing resets\n",
2395 adapter_state_to_string(adapter->state),
2396 adapter->force_reset_recovery,
Sukadev Bhattiprolu38bd5ce2020-12-04 18:22:35 -08002397 adapter->wait_for_reset);
Juliet Kim7ed5b312019-09-20 16:11:23 -04002398}
2399
2400static void __ibmvnic_delayed_reset(struct work_struct *work)
2401{
2402 struct ibmvnic_adapter *adapter;
2403
2404 adapter = container_of(work, struct ibmvnic_adapter,
2405 ibmvnic_delayed_reset.work);
2406 __ibmvnic_reset(&adapter->ibmvnic_reset);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002407}
2408
Thomas Falconaf894d22018-04-06 18:37:04 -05002409static int ibmvnic_reset(struct ibmvnic_adapter *adapter,
2410 enum ibmvnic_reset_reason reason)
Nathan Fontenoted651a12017-05-03 14:04:38 -04002411{
Thomas Falcon2770a792018-05-23 13:38:02 -05002412 struct list_head *entry, *tmp_entry;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002413 struct ibmvnic_rwi *rwi, *tmp;
2414 struct net_device *netdev = adapter->netdev;
Thomas Falcon6c5c7482018-12-10 15:22:22 -06002415 unsigned long flags;
Thomas Falconaf894d22018-04-06 18:37:04 -05002416 int ret;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002417
Jakub Kicinskib646acd52021-02-16 22:58:44 -08002418 spin_lock_irqsave(&adapter->rwi_lock, flags);
2419
2420 /* If failover is pending don't schedule any other reset.
Sukadev Bhattiprolu1d850492020-10-30 10:07:11 -07002421 * Instead let the failover complete. If there is already a
2422 * a failover reset scheduled, we will detect and drop the
2423 * duplicate reset when walking the ->rwi_list below.
2424 */
Nathan Fontenoted651a12017-05-03 14:04:38 -04002425 if (adapter->state == VNIC_REMOVING ||
Thomas Falcon5a18e1e2018-04-06 18:37:05 -05002426 adapter->state == VNIC_REMOVED ||
Sukadev Bhattiprolu1d850492020-10-30 10:07:11 -07002427 (adapter->failover_pending && reason != VNIC_RESET_FAILOVER)) {
Thomas Falconaf894d22018-04-06 18:37:04 -05002428 ret = EBUSY;
Thomas Falcon5a18e1e2018-04-06 18:37:05 -05002429 netdev_dbg(netdev, "Adapter removing or pending failover, skipping reset\n");
Thomas Falconaf894d22018-04-06 18:37:04 -05002430 goto err;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002431 }
2432
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04002433 if (adapter->state == VNIC_PROBING) {
2434 netdev_warn(netdev, "Adapter reset during probe\n");
Lijun Pan91dc5d22021-02-11 00:43:22 -06002435 adapter->init_done_rc = EAGAIN;
2436 ret = EAGAIN;
Thomas Falconaf894d22018-04-06 18:37:04 -05002437 goto err;
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04002438 }
2439
Nathan Fontenoted651a12017-05-03 14:04:38 -04002440 list_for_each(entry, &adapter->rwi_list) {
2441 tmp = list_entry(entry, struct ibmvnic_rwi, list);
2442 if (tmp->reset_reason == reason) {
Lijun Pancaee7bf2021-04-12 02:41:27 -05002443 netdev_dbg(netdev, "Skipping matching reset, reason=%s\n",
2444 reset_reason_to_string(reason));
Thomas Falconaf894d22018-04-06 18:37:04 -05002445 ret = EBUSY;
2446 goto err;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002447 }
2448 }
2449
Thomas Falcon1d1bbc32018-12-10 15:22:23 -06002450 rwi = kzalloc(sizeof(*rwi), GFP_ATOMIC);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002451 if (!rwi) {
Thomas Falconaf894d22018-04-06 18:37:04 -05002452 ret = ENOMEM;
2453 goto err;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002454 }
Thomas Falcon2770a792018-05-23 13:38:02 -05002455 /* if we just received a transport event,
2456 * flush reset queue and process this reset
2457 */
2458 if (adapter->force_reset_recovery && !list_empty(&adapter->rwi_list)) {
2459 list_for_each_safe(entry, tmp_entry, &adapter->rwi_list)
2460 list_del(entry);
2461 }
Nathan Fontenoted651a12017-05-03 14:04:38 -04002462 rwi->reset_reason = reason;
2463 list_add_tail(&rwi->list, &adapter->rwi_list);
Lijun Pancaee7bf2021-04-12 02:41:27 -05002464 netdev_dbg(adapter->netdev, "Scheduling reset (reason %s)\n",
2465 reset_reason_to_string(reason));
Lijun Pan870e04a2021-04-13 14:33:39 -05002466 queue_work(system_long_wq, &adapter->ibmvnic_reset);
Thomas Falconaf894d22018-04-06 18:37:04 -05002467
Sukadev Bhattiprolu4a41c422021-02-12 20:42:50 -08002468 ret = 0;
Thomas Falconaf894d22018-04-06 18:37:04 -05002469err:
Sukadev Bhattiprolu4a41c422021-02-12 20:42:50 -08002470 /* ibmvnic_close() below can block, so drop the lock first */
2471 spin_unlock_irqrestore(&adapter->rwi_lock, flags);
2472
2473 if (ret == ENOMEM)
2474 ibmvnic_close(netdev);
2475
Thomas Falconaf894d22018-04-06 18:37:04 -05002476 return -ret;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002477}
2478
Michael S. Tsirkin0290bd22019-12-10 09:23:51 -05002479static void ibmvnic_tx_timeout(struct net_device *dev, unsigned int txqueue)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002480{
2481 struct ibmvnic_adapter *adapter = netdev_priv(dev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002482
Lijun Pan855a6312020-11-20 16:40:13 -06002483 if (test_bit(0, &adapter->resetting)) {
2484 netdev_err(adapter->netdev,
2485 "Adapter is resetting, skip timeout reset\n");
2486 return;
2487 }
Dany Maddena86d5c62020-11-25 18:04:31 -06002488 /* No queuing up reset until at least 5 seconds (default watchdog val)
2489 * after last reset
2490 */
2491 if (time_before(jiffies, (adapter->last_reset_time + dev->watchdog_timeo))) {
2492 netdev_dbg(dev, "Not yet time to tx timeout.\n");
2493 return;
2494 }
Nathan Fontenoted651a12017-05-03 14:04:38 -04002495 ibmvnic_reset(adapter, VNIC_RESET_TIMEOUT);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002496}
2497
2498static void remove_buff_from_pool(struct ibmvnic_adapter *adapter,
2499 struct ibmvnic_rx_buff *rx_buff)
2500{
2501 struct ibmvnic_rx_pool *pool = &adapter->rx_pool[rx_buff->pool_index];
2502
2503 rx_buff->skb = NULL;
2504
2505 pool->free_map[pool->next_alloc] = (int)(rx_buff - pool->rx_buff);
2506 pool->next_alloc = (pool->next_alloc + 1) % pool->size;
2507
2508 atomic_dec(&pool->available);
2509}
2510
2511static int ibmvnic_poll(struct napi_struct *napi, int budget)
2512{
Dwip N. Banerjeeec20f362020-11-18 19:12:23 -06002513 struct ibmvnic_sub_crq_queue *rx_scrq;
2514 struct ibmvnic_adapter *adapter;
2515 struct net_device *netdev;
2516 int frames_processed;
2517 int scrq_num;
2518
2519 netdev = napi->dev;
2520 adapter = netdev_priv(netdev);
2521 scrq_num = (int)(napi - adapter->napi);
2522 frames_processed = 0;
2523 rx_scrq = adapter->rx_scrq[scrq_num];
Nathan Fontenot152ce472017-05-26 10:30:54 -04002524
Thomas Falcon032c5e82015-12-21 11:26:06 -06002525restart_poll:
2526 while (frames_processed < budget) {
2527 struct sk_buff *skb;
2528 struct ibmvnic_rx_buff *rx_buff;
2529 union sub_crq *next;
2530 u32 length;
2531 u16 offset;
2532 u8 flags = 0;
2533
Juliet Kim7ed5b312019-09-20 16:11:23 -04002534 if (unlikely(test_bit(0, &adapter->resetting) &&
John Allen34686562018-02-06 16:21:49 -06002535 adapter->reset_reason != VNIC_RESET_NON_FATAL)) {
Dwip N. Banerjeeec20f362020-11-18 19:12:23 -06002536 enable_scrq_irq(adapter, rx_scrq);
Thomas Falcon21ecba62017-06-14 23:50:09 -05002537 napi_complete_done(napi, frames_processed);
2538 return frames_processed;
2539 }
2540
Dwip N. Banerjeeec20f362020-11-18 19:12:23 -06002541 if (!pending_scrq(adapter, rx_scrq))
Thomas Falcon032c5e82015-12-21 11:26:06 -06002542 break;
Dwip N. Banerjeeec20f362020-11-18 19:12:23 -06002543 next = ibmvnic_next_scrq(adapter, rx_scrq);
Lijun Pan914789a2021-02-11 00:43:21 -06002544 rx_buff = (struct ibmvnic_rx_buff *)
2545 be64_to_cpu(next->rx_comp.correlator);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002546 /* do error checking */
2547 if (next->rx_comp.rc) {
John Allene1cea2e2017-08-07 15:42:30 -05002548 netdev_dbg(netdev, "rx buffer returned with rc %x\n",
2549 be16_to_cpu(next->rx_comp.rc));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002550 /* free the entry */
2551 next->rx_comp.first = 0;
Thomas Falcon4b9b0f02018-02-13 18:23:42 -06002552 dev_kfree_skb_any(rx_buff->skb);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002553 remove_buff_from_pool(adapter, rx_buff);
Nathan Fontenotca05e312017-05-03 14:05:14 -04002554 continue;
Thomas Falconabe27a82018-02-19 20:12:57 -06002555 } else if (!rx_buff->skb) {
2556 /* free the entry */
2557 next->rx_comp.first = 0;
2558 remove_buff_from_pool(adapter, rx_buff);
2559 continue;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002560 }
2561
2562 length = be32_to_cpu(next->rx_comp.len);
2563 offset = be16_to_cpu(next->rx_comp.off_frame_data);
2564 flags = next->rx_comp.flags;
2565 skb = rx_buff->skb;
Lijun Pan42557da2021-02-12 20:48:40 -06002566 /* load long_term_buff before copying to skb */
2567 dma_rmb();
Thomas Falcon032c5e82015-12-21 11:26:06 -06002568 skb_copy_to_linear_data(skb, rx_buff->data + offset,
2569 length);
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04002570
2571 /* VLAN Header has been stripped by the system firmware and
2572 * needs to be inserted by the driver
2573 */
2574 if (adapter->rx_vlan_header_insertion &&
2575 (flags & IBMVNIC_VLAN_STRIPPED))
2576 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
2577 ntohs(next->rx_comp.vlan_tci));
2578
Thomas Falcon032c5e82015-12-21 11:26:06 -06002579 /* free the entry */
2580 next->rx_comp.first = 0;
2581 remove_buff_from_pool(adapter, rx_buff);
2582
2583 skb_put(skb, length);
2584 skb->protocol = eth_type_trans(skb, netdev);
Thomas Falcon94ca3052017-05-03 14:05:20 -04002585 skb_record_rx_queue(skb, scrq_num);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002586
2587 if (flags & IBMVNIC_IP_CHKSUM_GOOD &&
2588 flags & IBMVNIC_TCP_UDP_CHKSUM_GOOD) {
2589 skb->ip_summed = CHECKSUM_UNNECESSARY;
2590 }
2591
2592 length = skb->len;
2593 napi_gro_receive(napi, skb); /* send it up */
2594 netdev->stats.rx_packets++;
2595 netdev->stats.rx_bytes += length;
John Allen3d52b592017-08-02 16:44:14 -05002596 adapter->rx_stats_buffers[scrq_num].packets++;
2597 adapter->rx_stats_buffers[scrq_num].bytes += length;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002598 frames_processed++;
2599 }
Nathan Fontenot152ce472017-05-26 10:30:54 -04002600
Dwip N. Banerjee41ed0a02020-11-18 19:12:25 -06002601 if (adapter->state != VNIC_CLOSING &&
2602 ((atomic_read(&adapter->rx_pool[scrq_num].available) <
2603 adapter->req_rx_add_entries_per_subcrq / 2) ||
2604 frames_processed < budget))
Nathan Fontenot152ce472017-05-26 10:30:54 -04002605 replenish_rx_pool(adapter, &adapter->rx_pool[scrq_num]);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002606 if (frames_processed < budget) {
Dwip N. Banerjeeec20f362020-11-18 19:12:23 -06002607 if (napi_complete_done(napi, frames_processed)) {
2608 enable_scrq_irq(adapter, rx_scrq);
2609 if (pending_scrq(adapter, rx_scrq)) {
Dwip N. Banerjeeec20f362020-11-18 19:12:23 -06002610 if (napi_reschedule(napi)) {
2611 disable_scrq_irq(adapter, rx_scrq);
2612 goto restart_poll;
2613 }
2614 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002615 }
2616 }
2617 return frames_processed;
2618}
2619
John Allenc26eba02017-10-26 16:23:25 -05002620static int wait_for_reset(struct ibmvnic_adapter *adapter)
2621{
Thomas Falconaf894d22018-04-06 18:37:04 -05002622 int rc, ret;
2623
John Allenc26eba02017-10-26 16:23:25 -05002624 adapter->fallback.mtu = adapter->req_mtu;
2625 adapter->fallback.rx_queues = adapter->req_rx_queues;
2626 adapter->fallback.tx_queues = adapter->req_tx_queues;
2627 adapter->fallback.rx_entries = adapter->req_rx_add_entries_per_subcrq;
2628 adapter->fallback.tx_entries = adapter->req_tx_entries_per_subcrq;
2629
Thomas Falcon070eca92019-11-25 17:12:53 -06002630 reinit_completion(&adapter->reset_done);
John Allenc26eba02017-10-26 16:23:25 -05002631 adapter->wait_for_reset = true;
Thomas Falconaf894d22018-04-06 18:37:04 -05002632 rc = ibmvnic_reset(adapter, VNIC_RESET_CHANGE_PARAM);
Thomas Falcon476d96c2019-11-25 17:12:55 -06002633
2634 if (rc) {
2635 ret = rc;
2636 goto out;
2637 }
2638 rc = ibmvnic_wait_for_completion(adapter, &adapter->reset_done, 60000);
2639 if (rc) {
2640 ret = -ENODEV;
2641 goto out;
2642 }
John Allenc26eba02017-10-26 16:23:25 -05002643
Thomas Falconaf894d22018-04-06 18:37:04 -05002644 ret = 0;
John Allenc26eba02017-10-26 16:23:25 -05002645 if (adapter->reset_done_rc) {
Thomas Falconaf894d22018-04-06 18:37:04 -05002646 ret = -EIO;
John Allenc26eba02017-10-26 16:23:25 -05002647 adapter->desired.mtu = adapter->fallback.mtu;
2648 adapter->desired.rx_queues = adapter->fallback.rx_queues;
2649 adapter->desired.tx_queues = adapter->fallback.tx_queues;
2650 adapter->desired.rx_entries = adapter->fallback.rx_entries;
2651 adapter->desired.tx_entries = adapter->fallback.tx_entries;
2652
Thomas Falcon070eca92019-11-25 17:12:53 -06002653 reinit_completion(&adapter->reset_done);
Thomas Falconaf894d22018-04-06 18:37:04 -05002654 adapter->wait_for_reset = true;
2655 rc = ibmvnic_reset(adapter, VNIC_RESET_CHANGE_PARAM);
Thomas Falcon476d96c2019-11-25 17:12:55 -06002656 if (rc) {
2657 ret = rc;
2658 goto out;
2659 }
2660 rc = ibmvnic_wait_for_completion(adapter, &adapter->reset_done,
2661 60000);
2662 if (rc) {
2663 ret = -ENODEV;
2664 goto out;
2665 }
John Allenc26eba02017-10-26 16:23:25 -05002666 }
Thomas Falcon476d96c2019-11-25 17:12:55 -06002667out:
John Allenc26eba02017-10-26 16:23:25 -05002668 adapter->wait_for_reset = false;
2669
Thomas Falconaf894d22018-04-06 18:37:04 -05002670 return ret;
John Allenc26eba02017-10-26 16:23:25 -05002671}
2672
John Allen3a807b72017-06-06 16:55:52 -05002673static int ibmvnic_change_mtu(struct net_device *netdev, int new_mtu)
2674{
John Allenc26eba02017-10-26 16:23:25 -05002675 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2676
2677 adapter->desired.mtu = new_mtu + ETH_HLEN;
2678
2679 return wait_for_reset(adapter);
John Allen3a807b72017-06-06 16:55:52 -05002680}
2681
Thomas Falconf10b09e2018-03-12 11:51:05 -05002682static netdev_features_t ibmvnic_features_check(struct sk_buff *skb,
2683 struct net_device *dev,
2684 netdev_features_t features)
2685{
2686 /* Some backing hardware adapters can not
2687 * handle packets with a MSS less than 224
2688 * or with only one segment.
2689 */
2690 if (skb_is_gso(skb)) {
2691 if (skb_shinfo(skb)->gso_size < 224 ||
2692 skb_shinfo(skb)->gso_segs == 1)
2693 features &= ~NETIF_F_GSO_MASK;
2694 }
2695
2696 return features;
2697}
2698
Thomas Falcon032c5e82015-12-21 11:26:06 -06002699static const struct net_device_ops ibmvnic_netdev_ops = {
2700 .ndo_open = ibmvnic_open,
2701 .ndo_stop = ibmvnic_close,
2702 .ndo_start_xmit = ibmvnic_xmit,
2703 .ndo_set_rx_mode = ibmvnic_set_multi,
2704 .ndo_set_mac_address = ibmvnic_set_mac,
2705 .ndo_validate_addr = eth_validate_addr,
Thomas Falcon032c5e82015-12-21 11:26:06 -06002706 .ndo_tx_timeout = ibmvnic_tx_timeout,
John Allen3a807b72017-06-06 16:55:52 -05002707 .ndo_change_mtu = ibmvnic_change_mtu,
Thomas Falconf10b09e2018-03-12 11:51:05 -05002708 .ndo_features_check = ibmvnic_features_check,
Thomas Falcon032c5e82015-12-21 11:26:06 -06002709};
2710
2711/* ethtool functions */
2712
Philippe Reynes8a433792017-01-07 22:37:29 +01002713static int ibmvnic_get_link_ksettings(struct net_device *netdev,
2714 struct ethtool_link_ksettings *cmd)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002715{
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03002716 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2717 int rc;
Philippe Reynes8a433792017-01-07 22:37:29 +01002718
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03002719 rc = send_query_phys_parms(adapter);
2720 if (rc) {
2721 adapter->speed = SPEED_UNKNOWN;
2722 adapter->duplex = DUPLEX_UNKNOWN;
2723 }
2724 cmd->base.speed = adapter->speed;
2725 cmd->base.duplex = adapter->duplex;
Philippe Reynes8a433792017-01-07 22:37:29 +01002726 cmd->base.port = PORT_FIBRE;
2727 cmd->base.phy_address = 0;
2728 cmd->base.autoneg = AUTONEG_ENABLE;
2729
Thomas Falcon032c5e82015-12-21 11:26:06 -06002730 return 0;
2731}
2732
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02002733static void ibmvnic_get_drvinfo(struct net_device *netdev,
Thomas Falcon032c5e82015-12-21 11:26:06 -06002734 struct ethtool_drvinfo *info)
2735{
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02002736 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2737
Lijun Pan8a96c802021-02-11 00:43:25 -06002738 strscpy(info->driver, ibmvnic_driver_name, sizeof(info->driver));
2739 strscpy(info->version, IBMVNIC_DRIVER_VERSION, sizeof(info->version));
2740 strscpy(info->fw_version, adapter->fw_version,
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02002741 sizeof(info->fw_version));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002742}
2743
2744static u32 ibmvnic_get_msglevel(struct net_device *netdev)
2745{
2746 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2747
2748 return adapter->msg_enable;
2749}
2750
2751static void ibmvnic_set_msglevel(struct net_device *netdev, u32 data)
2752{
2753 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2754
2755 adapter->msg_enable = data;
2756}
2757
2758static u32 ibmvnic_get_link(struct net_device *netdev)
2759{
2760 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2761
2762 /* Don't need to send a query because we request a logical link up at
2763 * init and then we wait for link state indications
2764 */
2765 return adapter->logical_link_state;
2766}
2767
2768static void ibmvnic_get_ringparam(struct net_device *netdev,
2769 struct ethtool_ringparam *ring)
2770{
John Allenbc131b32017-08-02 16:46:30 -05002771 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2772
Thomas Falcon723ad912018-09-28 18:38:26 -05002773 if (adapter->priv_flags & IBMVNIC_USE_SERVER_MAXES) {
2774 ring->rx_max_pending = adapter->max_rx_add_entries_per_subcrq;
2775 ring->tx_max_pending = adapter->max_tx_entries_per_subcrq;
2776 } else {
2777 ring->rx_max_pending = IBMVNIC_MAX_QUEUE_SZ;
2778 ring->tx_max_pending = IBMVNIC_MAX_QUEUE_SZ;
2779 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002780 ring->rx_mini_max_pending = 0;
2781 ring->rx_jumbo_max_pending = 0;
John Allenbc131b32017-08-02 16:46:30 -05002782 ring->rx_pending = adapter->req_rx_add_entries_per_subcrq;
2783 ring->tx_pending = adapter->req_tx_entries_per_subcrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002784 ring->rx_mini_pending = 0;
2785 ring->rx_jumbo_pending = 0;
2786}
2787
John Allenc26eba02017-10-26 16:23:25 -05002788static int ibmvnic_set_ringparam(struct net_device *netdev,
2789 struct ethtool_ringparam *ring)
2790{
2791 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Thomas Falcon723ad912018-09-28 18:38:26 -05002792 int ret;
John Allenc26eba02017-10-26 16:23:25 -05002793
Thomas Falcon723ad912018-09-28 18:38:26 -05002794 ret = 0;
John Allenc26eba02017-10-26 16:23:25 -05002795 adapter->desired.rx_entries = ring->rx_pending;
2796 adapter->desired.tx_entries = ring->tx_pending;
2797
Thomas Falcon723ad912018-09-28 18:38:26 -05002798 ret = wait_for_reset(adapter);
2799
2800 if (!ret &&
2801 (adapter->req_rx_add_entries_per_subcrq != ring->rx_pending ||
2802 adapter->req_tx_entries_per_subcrq != ring->tx_pending))
2803 netdev_info(netdev,
2804 "Could not match full ringsize request. Requested: RX %d, TX %d; Allowed: RX %llu, TX %llu\n",
2805 ring->rx_pending, ring->tx_pending,
2806 adapter->req_rx_add_entries_per_subcrq,
2807 adapter->req_tx_entries_per_subcrq);
2808 return ret;
John Allenc26eba02017-10-26 16:23:25 -05002809}
2810
John Allenc2dbeb62017-08-02 16:47:17 -05002811static void ibmvnic_get_channels(struct net_device *netdev,
2812 struct ethtool_channels *channels)
2813{
2814 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2815
Thomas Falcon723ad912018-09-28 18:38:26 -05002816 if (adapter->priv_flags & IBMVNIC_USE_SERVER_MAXES) {
2817 channels->max_rx = adapter->max_rx_queues;
2818 channels->max_tx = adapter->max_tx_queues;
2819 } else {
2820 channels->max_rx = IBMVNIC_MAX_QUEUES;
2821 channels->max_tx = IBMVNIC_MAX_QUEUES;
2822 }
2823
John Allenc2dbeb62017-08-02 16:47:17 -05002824 channels->max_other = 0;
2825 channels->max_combined = 0;
2826 channels->rx_count = adapter->req_rx_queues;
2827 channels->tx_count = adapter->req_tx_queues;
2828 channels->other_count = 0;
2829 channels->combined_count = 0;
2830}
2831
John Allenc26eba02017-10-26 16:23:25 -05002832static int ibmvnic_set_channels(struct net_device *netdev,
2833 struct ethtool_channels *channels)
2834{
2835 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Thomas Falcon723ad912018-09-28 18:38:26 -05002836 int ret;
John Allenc26eba02017-10-26 16:23:25 -05002837
Thomas Falcon723ad912018-09-28 18:38:26 -05002838 ret = 0;
John Allenc26eba02017-10-26 16:23:25 -05002839 adapter->desired.rx_queues = channels->rx_count;
2840 adapter->desired.tx_queues = channels->tx_count;
2841
Thomas Falcon723ad912018-09-28 18:38:26 -05002842 ret = wait_for_reset(adapter);
2843
2844 if (!ret &&
2845 (adapter->req_rx_queues != channels->rx_count ||
2846 adapter->req_tx_queues != channels->tx_count))
2847 netdev_info(netdev,
2848 "Could not match full channels request. Requested: RX %d, TX %d; Allowed: RX %llu, TX %llu\n",
2849 channels->rx_count, channels->tx_count,
2850 adapter->req_rx_queues, adapter->req_tx_queues);
2851 return ret;
John Allenc26eba02017-10-26 16:23:25 -05002852}
2853
Thomas Falcon032c5e82015-12-21 11:26:06 -06002854static void ibmvnic_get_strings(struct net_device *dev, u32 stringset, u8 *data)
2855{
John Allen3d52b592017-08-02 16:44:14 -05002856 struct ibmvnic_adapter *adapter = netdev_priv(dev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002857 int i;
2858
Thomas Falcon723ad912018-09-28 18:38:26 -05002859 switch (stringset) {
2860 case ETH_SS_STATS:
2861 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats);
2862 i++, data += ETH_GSTRING_LEN)
2863 memcpy(data, ibmvnic_stats[i].name, ETH_GSTRING_LEN);
2864
2865 for (i = 0; i < adapter->req_tx_queues; i++) {
2866 snprintf(data, ETH_GSTRING_LEN, "tx%d_packets", i);
2867 data += ETH_GSTRING_LEN;
2868
2869 snprintf(data, ETH_GSTRING_LEN, "tx%d_bytes", i);
2870 data += ETH_GSTRING_LEN;
2871
2872 snprintf(data, ETH_GSTRING_LEN,
2873 "tx%d_dropped_packets", i);
2874 data += ETH_GSTRING_LEN;
2875 }
2876
2877 for (i = 0; i < adapter->req_rx_queues; i++) {
2878 snprintf(data, ETH_GSTRING_LEN, "rx%d_packets", i);
2879 data += ETH_GSTRING_LEN;
2880
2881 snprintf(data, ETH_GSTRING_LEN, "rx%d_bytes", i);
2882 data += ETH_GSTRING_LEN;
2883
2884 snprintf(data, ETH_GSTRING_LEN, "rx%d_interrupts", i);
2885 data += ETH_GSTRING_LEN;
2886 }
2887 break;
2888
2889 case ETH_SS_PRIV_FLAGS:
2890 for (i = 0; i < ARRAY_SIZE(ibmvnic_priv_flags); i++)
2891 strcpy(data + i * ETH_GSTRING_LEN,
2892 ibmvnic_priv_flags[i]);
2893 break;
2894 default:
Thomas Falcon032c5e82015-12-21 11:26:06 -06002895 return;
John Allen3d52b592017-08-02 16:44:14 -05002896 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002897}
2898
2899static int ibmvnic_get_sset_count(struct net_device *dev, int sset)
2900{
John Allen3d52b592017-08-02 16:44:14 -05002901 struct ibmvnic_adapter *adapter = netdev_priv(dev);
2902
Thomas Falcon032c5e82015-12-21 11:26:06 -06002903 switch (sset) {
2904 case ETH_SS_STATS:
John Allen3d52b592017-08-02 16:44:14 -05002905 return ARRAY_SIZE(ibmvnic_stats) +
2906 adapter->req_tx_queues * NUM_TX_STATS +
2907 adapter->req_rx_queues * NUM_RX_STATS;
Thomas Falcon723ad912018-09-28 18:38:26 -05002908 case ETH_SS_PRIV_FLAGS:
2909 return ARRAY_SIZE(ibmvnic_priv_flags);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002910 default:
2911 return -EOPNOTSUPP;
2912 }
2913}
2914
2915static void ibmvnic_get_ethtool_stats(struct net_device *dev,
2916 struct ethtool_stats *stats, u64 *data)
2917{
2918 struct ibmvnic_adapter *adapter = netdev_priv(dev);
2919 union ibmvnic_crq crq;
John Allen3d52b592017-08-02 16:44:14 -05002920 int i, j;
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05002921 int rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002922
2923 memset(&crq, 0, sizeof(crq));
2924 crq.request_statistics.first = IBMVNIC_CRQ_CMD;
2925 crq.request_statistics.cmd = REQUEST_STATISTICS;
2926 crq.request_statistics.ioba = cpu_to_be32(adapter->stats_token);
2927 crq.request_statistics.len =
2928 cpu_to_be32(sizeof(struct ibmvnic_statistics));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002929
2930 /* Wait for data to be written */
Thomas Falcon070eca92019-11-25 17:12:53 -06002931 reinit_completion(&adapter->stats_done);
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05002932 rc = ibmvnic_send_crq(adapter, &crq);
2933 if (rc)
2934 return;
Thomas Falcon476d96c2019-11-25 17:12:55 -06002935 rc = ibmvnic_wait_for_completion(adapter, &adapter->stats_done, 10000);
2936 if (rc)
2937 return;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002938
2939 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++)
Lijun Pan91dc5d22021-02-11 00:43:22 -06002940 data[i] = be64_to_cpu(IBMVNIC_GET_STAT
2941 (adapter, ibmvnic_stats[i].offset));
John Allen3d52b592017-08-02 16:44:14 -05002942
2943 for (j = 0; j < adapter->req_tx_queues; j++) {
2944 data[i] = adapter->tx_stats_buffers[j].packets;
2945 i++;
2946 data[i] = adapter->tx_stats_buffers[j].bytes;
2947 i++;
2948 data[i] = adapter->tx_stats_buffers[j].dropped_packets;
2949 i++;
2950 }
2951
2952 for (j = 0; j < adapter->req_rx_queues; j++) {
2953 data[i] = adapter->rx_stats_buffers[j].packets;
2954 i++;
2955 data[i] = adapter->rx_stats_buffers[j].bytes;
2956 i++;
2957 data[i] = adapter->rx_stats_buffers[j].interrupts;
2958 i++;
2959 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002960}
2961
Thomas Falcon723ad912018-09-28 18:38:26 -05002962static u32 ibmvnic_get_priv_flags(struct net_device *netdev)
2963{
2964 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2965
2966 return adapter->priv_flags;
2967}
2968
2969static int ibmvnic_set_priv_flags(struct net_device *netdev, u32 flags)
2970{
2971 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2972 bool which_maxes = !!(flags & IBMVNIC_USE_SERVER_MAXES);
2973
2974 if (which_maxes)
2975 adapter->priv_flags |= IBMVNIC_USE_SERVER_MAXES;
2976 else
2977 adapter->priv_flags &= ~IBMVNIC_USE_SERVER_MAXES;
2978
2979 return 0;
2980}
Lijun Pan91dc5d22021-02-11 00:43:22 -06002981
Thomas Falcon032c5e82015-12-21 11:26:06 -06002982static const struct ethtool_ops ibmvnic_ethtool_ops = {
Thomas Falcon032c5e82015-12-21 11:26:06 -06002983 .get_drvinfo = ibmvnic_get_drvinfo,
2984 .get_msglevel = ibmvnic_get_msglevel,
2985 .set_msglevel = ibmvnic_set_msglevel,
2986 .get_link = ibmvnic_get_link,
2987 .get_ringparam = ibmvnic_get_ringparam,
John Allenc26eba02017-10-26 16:23:25 -05002988 .set_ringparam = ibmvnic_set_ringparam,
John Allenc2dbeb62017-08-02 16:47:17 -05002989 .get_channels = ibmvnic_get_channels,
John Allenc26eba02017-10-26 16:23:25 -05002990 .set_channels = ibmvnic_set_channels,
Thomas Falcon032c5e82015-12-21 11:26:06 -06002991 .get_strings = ibmvnic_get_strings,
2992 .get_sset_count = ibmvnic_get_sset_count,
2993 .get_ethtool_stats = ibmvnic_get_ethtool_stats,
Philippe Reynes8a433792017-01-07 22:37:29 +01002994 .get_link_ksettings = ibmvnic_get_link_ksettings,
Thomas Falcon723ad912018-09-28 18:38:26 -05002995 .get_priv_flags = ibmvnic_get_priv_flags,
2996 .set_priv_flags = ibmvnic_set_priv_flags,
Thomas Falcon032c5e82015-12-21 11:26:06 -06002997};
2998
2999/* Routines for managing CRQs/sCRQs */
3000
Nathan Fontenot57a49432017-05-26 10:31:12 -04003001static int reset_one_sub_crq_queue(struct ibmvnic_adapter *adapter,
3002 struct ibmvnic_sub_crq_queue *scrq)
3003{
3004 int rc;
3005
Dany Madden9281cf22020-11-25 18:04:26 -06003006 if (!scrq) {
YANG LI862aecb2020-12-30 15:23:14 +08003007 netdev_dbg(adapter->netdev, "Invalid scrq reset.\n");
Dany Madden9281cf22020-11-25 18:04:26 -06003008 return -EINVAL;
3009 }
3010
Nathan Fontenot57a49432017-05-26 10:31:12 -04003011 if (scrq->irq) {
3012 free_irq(scrq->irq, scrq);
3013 irq_dispose_mapping(scrq->irq);
3014 scrq->irq = 0;
3015 }
3016
Dany Madden9281cf22020-11-25 18:04:26 -06003017 if (scrq->msgs) {
3018 memset(scrq->msgs, 0, 4 * PAGE_SIZE);
3019 atomic_set(&scrq->used, 0);
3020 scrq->cur = 0;
Jakub Kicinski55fd59b2020-12-03 15:42:13 -08003021 scrq->ind_buf.index = 0;
Dany Madden9281cf22020-11-25 18:04:26 -06003022 } else {
3023 netdev_dbg(adapter->netdev, "Invalid scrq reset\n");
3024 return -EINVAL;
3025 }
Nathan Fontenot57a49432017-05-26 10:31:12 -04003026
3027 rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
3028 4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
3029 return rc;
3030}
3031
3032static int reset_sub_crq_queues(struct ibmvnic_adapter *adapter)
3033{
3034 int i, rc;
3035
Lijun Pana0faaa22020-11-23 13:35:45 -06003036 if (!adapter->tx_scrq || !adapter->rx_scrq)
3037 return -EINVAL;
3038
Nathan Fontenot57a49432017-05-26 10:31:12 -04003039 for (i = 0; i < adapter->req_tx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05003040 netdev_dbg(adapter->netdev, "Re-setting tx_scrq[%d]\n", i);
Nathan Fontenot57a49432017-05-26 10:31:12 -04003041 rc = reset_one_sub_crq_queue(adapter, adapter->tx_scrq[i]);
3042 if (rc)
3043 return rc;
3044 }
3045
3046 for (i = 0; i < adapter->req_rx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05003047 netdev_dbg(adapter->netdev, "Re-setting rx_scrq[%d]\n", i);
Nathan Fontenot57a49432017-05-26 10:31:12 -04003048 rc = reset_one_sub_crq_queue(adapter, adapter->rx_scrq[i]);
3049 if (rc)
3050 return rc;
3051 }
3052
Nathan Fontenot57a49432017-05-26 10:31:12 -04003053 return rc;
3054}
3055
Thomas Falcon032c5e82015-12-21 11:26:06 -06003056static void release_sub_crq_queue(struct ibmvnic_adapter *adapter,
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06003057 struct ibmvnic_sub_crq_queue *scrq,
3058 bool do_h_free)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003059{
3060 struct device *dev = &adapter->vdev->dev;
3061 long rc;
3062
3063 netdev_dbg(adapter->netdev, "Releasing sub-CRQ\n");
3064
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06003065 if (do_h_free) {
3066 /* Close the sub-crqs */
3067 do {
3068 rc = plpar_hcall_norets(H_FREE_SUB_CRQ,
3069 adapter->vdev->unit_address,
3070 scrq->crq_num);
3071 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
Thomas Falcon032c5e82015-12-21 11:26:06 -06003072
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06003073 if (rc) {
3074 netdev_err(adapter->netdev,
3075 "Failed to release sub-CRQ %16lx, rc = %ld\n",
3076 scrq->crq_num, rc);
3077 }
Thomas Falconffa73852017-04-19 13:44:29 -04003078 }
3079
Thomas Falconf019fb62020-11-18 19:12:17 -06003080 dma_free_coherent(dev,
3081 IBMVNIC_IND_ARR_SZ,
3082 scrq->ind_buf.indir_arr,
3083 scrq->ind_buf.indir_dma);
3084
Thomas Falcon032c5e82015-12-21 11:26:06 -06003085 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
3086 DMA_BIDIRECTIONAL);
3087 free_pages((unsigned long)scrq->msgs, 2);
3088 kfree(scrq);
3089}
3090
3091static struct ibmvnic_sub_crq_queue *init_sub_crq_queue(struct ibmvnic_adapter
3092 *adapter)
3093{
3094 struct device *dev = &adapter->vdev->dev;
3095 struct ibmvnic_sub_crq_queue *scrq;
3096 int rc;
3097
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04003098 scrq = kzalloc(sizeof(*scrq), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003099 if (!scrq)
3100 return NULL;
3101
Nathan Fontenot7f7adc52017-04-19 13:45:16 -04003102 scrq->msgs =
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04003103 (union sub_crq *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, 2);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003104 if (!scrq->msgs) {
3105 dev_warn(dev, "Couldn't allocate crq queue messages page\n");
3106 goto zero_page_failed;
3107 }
3108
3109 scrq->msg_token = dma_map_single(dev, scrq->msgs, 4 * PAGE_SIZE,
3110 DMA_BIDIRECTIONAL);
3111 if (dma_mapping_error(dev, scrq->msg_token)) {
3112 dev_warn(dev, "Couldn't map crq queue messages page\n");
3113 goto map_failed;
3114 }
3115
3116 rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
3117 4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
3118
3119 if (rc == H_RESOURCE)
3120 rc = ibmvnic_reset_crq(adapter);
3121
3122 if (rc == H_CLOSED) {
3123 dev_warn(dev, "Partner adapter not ready, waiting.\n");
3124 } else if (rc) {
3125 dev_warn(dev, "Error %d registering sub-crq\n", rc);
3126 goto reg_failed;
3127 }
3128
Thomas Falcon032c5e82015-12-21 11:26:06 -06003129 scrq->adapter = adapter;
3130 scrq->size = 4 * PAGE_SIZE / sizeof(*scrq->msgs);
Thomas Falconf019fb62020-11-18 19:12:17 -06003131 scrq->ind_buf.index = 0;
3132
3133 scrq->ind_buf.indir_arr =
3134 dma_alloc_coherent(dev,
3135 IBMVNIC_IND_ARR_SZ,
3136 &scrq->ind_buf.indir_dma,
3137 GFP_KERNEL);
3138
3139 if (!scrq->ind_buf.indir_arr)
3140 goto indir_failed;
3141
Thomas Falcon032c5e82015-12-21 11:26:06 -06003142 spin_lock_init(&scrq->lock);
3143
3144 netdev_dbg(adapter->netdev,
3145 "sub-crq initialized, num %lx, hw_irq=%lx, irq=%x\n",
3146 scrq->crq_num, scrq->hw_irq, scrq->irq);
3147
3148 return scrq;
3149
Thomas Falconf019fb62020-11-18 19:12:17 -06003150indir_failed:
3151 do {
3152 rc = plpar_hcall_norets(H_FREE_SUB_CRQ,
3153 adapter->vdev->unit_address,
3154 scrq->crq_num);
3155 } while (rc == H_BUSY || rc == H_IS_LONG_BUSY(rc));
Thomas Falcon032c5e82015-12-21 11:26:06 -06003156reg_failed:
3157 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
3158 DMA_BIDIRECTIONAL);
3159map_failed:
3160 free_pages((unsigned long)scrq->msgs, 2);
3161zero_page_failed:
3162 kfree(scrq);
3163
3164 return NULL;
3165}
3166
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06003167static void release_sub_crqs(struct ibmvnic_adapter *adapter, bool do_h_free)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003168{
3169 int i;
3170
3171 if (adapter->tx_scrq) {
Nathan Fontenot82e3be32018-02-21 21:33:56 -06003172 for (i = 0; i < adapter->num_active_tx_scrqs; i++) {
Nathan Fontenotb5108882017-03-30 02:49:18 -04003173 if (!adapter->tx_scrq[i])
3174 continue;
3175
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05003176 netdev_dbg(adapter->netdev, "Releasing tx_scrq[%d]\n",
3177 i);
Nathan Fontenotb5108882017-03-30 02:49:18 -04003178 if (adapter->tx_scrq[i]->irq) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06003179 free_irq(adapter->tx_scrq[i]->irq,
3180 adapter->tx_scrq[i]);
Thomas Falcon88eb98a2016-07-06 15:35:16 -05003181 irq_dispose_mapping(adapter->tx_scrq[i]->irq);
Nathan Fontenotb5108882017-03-30 02:49:18 -04003182 adapter->tx_scrq[i]->irq = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003183 }
Nathan Fontenotb5108882017-03-30 02:49:18 -04003184
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06003185 release_sub_crq_queue(adapter, adapter->tx_scrq[i],
3186 do_h_free);
Nathan Fontenotb5108882017-03-30 02:49:18 -04003187 }
3188
Nathan Fontenot9501df32017-03-15 23:38:07 -04003189 kfree(adapter->tx_scrq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003190 adapter->tx_scrq = NULL;
Nathan Fontenot82e3be32018-02-21 21:33:56 -06003191 adapter->num_active_tx_scrqs = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003192 }
3193
3194 if (adapter->rx_scrq) {
Nathan Fontenot82e3be32018-02-21 21:33:56 -06003195 for (i = 0; i < adapter->num_active_rx_scrqs; i++) {
Nathan Fontenotb5108882017-03-30 02:49:18 -04003196 if (!adapter->rx_scrq[i])
3197 continue;
3198
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05003199 netdev_dbg(adapter->netdev, "Releasing rx_scrq[%d]\n",
3200 i);
Nathan Fontenotb5108882017-03-30 02:49:18 -04003201 if (adapter->rx_scrq[i]->irq) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06003202 free_irq(adapter->rx_scrq[i]->irq,
3203 adapter->rx_scrq[i]);
Thomas Falcon88eb98a2016-07-06 15:35:16 -05003204 irq_dispose_mapping(adapter->rx_scrq[i]->irq);
Nathan Fontenotb5108882017-03-30 02:49:18 -04003205 adapter->rx_scrq[i]->irq = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003206 }
Nathan Fontenotb5108882017-03-30 02:49:18 -04003207
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06003208 release_sub_crq_queue(adapter, adapter->rx_scrq[i],
3209 do_h_free);
Nathan Fontenotb5108882017-03-30 02:49:18 -04003210 }
3211
Nathan Fontenot9501df32017-03-15 23:38:07 -04003212 kfree(adapter->rx_scrq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003213 adapter->rx_scrq = NULL;
Nathan Fontenot82e3be32018-02-21 21:33:56 -06003214 adapter->num_active_rx_scrqs = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003215 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06003216}
3217
3218static int disable_scrq_irq(struct ibmvnic_adapter *adapter,
3219 struct ibmvnic_sub_crq_queue *scrq)
3220{
3221 struct device *dev = &adapter->vdev->dev;
3222 unsigned long rc;
3223
3224 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
3225 H_DISABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
3226 if (rc)
3227 dev_err(dev, "Couldn't disable scrq irq 0x%lx. rc=%ld\n",
3228 scrq->hw_irq, rc);
3229 return rc;
3230}
3231
3232static int enable_scrq_irq(struct ibmvnic_adapter *adapter,
3233 struct ibmvnic_sub_crq_queue *scrq)
3234{
3235 struct device *dev = &adapter->vdev->dev;
3236 unsigned long rc;
3237
3238 if (scrq->hw_irq > 0x100000000ULL) {
3239 dev_err(dev, "bad hw_irq = %lx\n", scrq->hw_irq);
3240 return 1;
3241 }
3242
Juliet Kim7ed5b312019-09-20 16:11:23 -04003243 if (test_bit(0, &adapter->resetting) &&
Nathan Fontenot73f9d362018-05-22 11:21:10 -05003244 adapter->reset_reason == VNIC_RESET_MOBILITY) {
Juliet Kim284f87d2019-11-20 10:50:03 -05003245 u64 val = (0xff000000) | scrq->hw_irq;
Nathan Fontenot73f9d362018-05-22 11:21:10 -05003246
Juliet Kim284f87d2019-11-20 10:50:03 -05003247 rc = plpar_hcall_norets(H_EOI, val);
Juliet Kim2df5c602019-11-20 10:50:04 -05003248 /* H_EOI would fail with rc = H_FUNCTION when running
3249 * in XIVE mode which is expected, but not an error.
3250 */
Lijun Pan91dc5d22021-02-11 00:43:22 -06003251 if (rc && rc != H_FUNCTION)
Juliet Kim284f87d2019-11-20 10:50:03 -05003252 dev_err(dev, "H_EOI FAILED irq 0x%llx. rc=%ld\n",
3253 val, rc);
Nathan Fontenot73f9d362018-05-22 11:21:10 -05003254 }
Thomas Falconf23e0642018-04-15 18:53:36 -05003255
Thomas Falcon032c5e82015-12-21 11:26:06 -06003256 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
3257 H_ENABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
3258 if (rc)
3259 dev_err(dev, "Couldn't enable scrq irq 0x%lx. rc=%ld\n",
3260 scrq->hw_irq, rc);
3261 return rc;
3262}
3263
3264static int ibmvnic_complete_tx(struct ibmvnic_adapter *adapter,
3265 struct ibmvnic_sub_crq_queue *scrq)
3266{
3267 struct device *dev = &adapter->vdev->dev;
Thomas Falcon06b3e352018-03-16 20:00:28 -05003268 struct ibmvnic_tx_pool *tx_pool;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003269 struct ibmvnic_tx_buff *txbuff;
Thomas Falcon0d973382020-11-18 19:12:19 -06003270 struct netdev_queue *txq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003271 union sub_crq *next;
3272 int index;
Thomas Falconc62aa372020-11-18 19:12:20 -06003273 int i;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003274
3275restart_loop:
3276 while (pending_scrq(adapter, scrq)) {
3277 unsigned int pool = scrq->pool_index;
Thomas Falconffc385b2018-02-18 10:08:41 -06003278 int num_entries = 0;
Thomas Falcon0d973382020-11-18 19:12:19 -06003279 int total_bytes = 0;
3280 int num_packets = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003281
3282 next = ibmvnic_next_scrq(adapter, scrq);
3283 for (i = 0; i < next->tx_comp.num_comps; i++) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06003284 index = be32_to_cpu(next->tx_comp.correlators[i]);
Thomas Falcon06b3e352018-03-16 20:00:28 -05003285 if (index & IBMVNIC_TSO_POOL_MASK) {
3286 tx_pool = &adapter->tso_pool[pool];
3287 index &= ~IBMVNIC_TSO_POOL_MASK;
3288 } else {
3289 tx_pool = &adapter->tx_pool[pool];
3290 }
3291
3292 txbuff = &tx_pool->tx_buff[index];
Thomas Falcon0d973382020-11-18 19:12:19 -06003293 num_packets++;
Thomas Falconffc385b2018-02-18 10:08:41 -06003294 num_entries += txbuff->num_entries;
Thomas Falcon0d973382020-11-18 19:12:19 -06003295 if (txbuff->skb) {
3296 total_bytes += txbuff->skb->len;
Lijun Panca09bf72021-04-13 03:33:25 -05003297 if (next->tx_comp.rcs[i]) {
3298 dev_err(dev, "tx error %x\n",
3299 next->tx_comp.rcs[i]);
3300 dev_kfree_skb_irq(txbuff->skb);
3301 } else {
3302 dev_consume_skb_irq(txbuff->skb);
3303 }
Thomas Falcon0d973382020-11-18 19:12:19 -06003304 txbuff->skb = NULL;
3305 } else {
3306 netdev_warn(adapter->netdev,
3307 "TX completion received with NULL socket buffer\n");
3308 }
Thomas Falcon06b3e352018-03-16 20:00:28 -05003309 tx_pool->free_map[tx_pool->producer_index] = index;
3310 tx_pool->producer_index =
3311 (tx_pool->producer_index + 1) %
3312 tx_pool->num_buffers;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003313 }
3314 /* remove tx_comp scrq*/
3315 next->tx_comp.first = 0;
Nathan Fontenot7c3e7de2017-05-03 14:05:25 -04003316
Thomas Falcon0d973382020-11-18 19:12:19 -06003317 txq = netdev_get_tx_queue(adapter->netdev, scrq->pool_index);
3318 netdev_tx_completed_queue(txq, num_packets, total_bytes);
3319
Thomas Falconffc385b2018-02-18 10:08:41 -06003320 if (atomic_sub_return(num_entries, &scrq->used) <=
Nathan Fontenot7c3e7de2017-05-03 14:05:25 -04003321 (adapter->req_tx_entries_per_subcrq / 2) &&
3322 __netif_subqueue_stopped(adapter->netdev,
3323 scrq->pool_index)) {
3324 netif_wake_subqueue(adapter->netdev, scrq->pool_index);
Thomas Falcon0aecb132018-02-26 18:10:58 -06003325 netdev_dbg(adapter->netdev, "Started queue %d\n",
3326 scrq->pool_index);
Nathan Fontenot7c3e7de2017-05-03 14:05:25 -04003327 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06003328 }
3329
3330 enable_scrq_irq(adapter, scrq);
3331
3332 if (pending_scrq(adapter, scrq)) {
3333 disable_scrq_irq(adapter, scrq);
3334 goto restart_loop;
3335 }
3336
3337 return 0;
3338}
3339
3340static irqreturn_t ibmvnic_interrupt_tx(int irq, void *instance)
3341{
3342 struct ibmvnic_sub_crq_queue *scrq = instance;
3343 struct ibmvnic_adapter *adapter = scrq->adapter;
3344
3345 disable_scrq_irq(adapter, scrq);
3346 ibmvnic_complete_tx(adapter, scrq);
3347
3348 return IRQ_HANDLED;
3349}
3350
3351static irqreturn_t ibmvnic_interrupt_rx(int irq, void *instance)
3352{
3353 struct ibmvnic_sub_crq_queue *scrq = instance;
3354 struct ibmvnic_adapter *adapter = scrq->adapter;
3355
Nathan Fontenot09fb35e2018-01-10 10:40:09 -06003356 /* When booting a kdump kernel we can hit pending interrupts
3357 * prior to completing driver initialization.
3358 */
3359 if (unlikely(adapter->state != VNIC_OPEN))
3360 return IRQ_NONE;
3361
John Allen3d52b592017-08-02 16:44:14 -05003362 adapter->rx_stats_buffers[scrq->scrq_num].interrupts++;
3363
Thomas Falcon032c5e82015-12-21 11:26:06 -06003364 if (napi_schedule_prep(&adapter->napi[scrq->scrq_num])) {
3365 disable_scrq_irq(adapter, scrq);
3366 __napi_schedule(&adapter->napi[scrq->scrq_num]);
3367 }
3368
3369 return IRQ_HANDLED;
3370}
3371
Thomas Falconea22d512016-07-06 15:35:17 -05003372static int init_sub_crq_irqs(struct ibmvnic_adapter *adapter)
3373{
3374 struct device *dev = &adapter->vdev->dev;
3375 struct ibmvnic_sub_crq_queue *scrq;
3376 int i = 0, j = 0;
3377 int rc = 0;
3378
3379 for (i = 0; i < adapter->req_tx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05003380 netdev_dbg(adapter->netdev, "Initializing tx_scrq[%d] irq\n",
3381 i);
Thomas Falconea22d512016-07-06 15:35:17 -05003382 scrq = adapter->tx_scrq[i];
3383 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
3384
Michael Ellerman99c17902016-09-10 19:59:05 +10003385 if (!scrq->irq) {
Thomas Falconea22d512016-07-06 15:35:17 -05003386 rc = -EINVAL;
3387 dev_err(dev, "Error mapping irq\n");
3388 goto req_tx_irq_failed;
3389 }
3390
Murilo Fossa Vicentinie56e2512019-04-25 11:02:33 -03003391 snprintf(scrq->name, sizeof(scrq->name), "ibmvnic-%x-tx%d",
3392 adapter->vdev->unit_address, i);
Thomas Falconea22d512016-07-06 15:35:17 -05003393 rc = request_irq(scrq->irq, ibmvnic_interrupt_tx,
Murilo Fossa Vicentinie56e2512019-04-25 11:02:33 -03003394 0, scrq->name, scrq);
Thomas Falconea22d512016-07-06 15:35:17 -05003395
3396 if (rc) {
3397 dev_err(dev, "Couldn't register tx irq 0x%x. rc=%d\n",
3398 scrq->irq, rc);
3399 irq_dispose_mapping(scrq->irq);
Nathan Fontenotaf9090c2018-02-20 11:04:18 -06003400 goto req_tx_irq_failed;
Thomas Falconea22d512016-07-06 15:35:17 -05003401 }
3402 }
3403
3404 for (i = 0; i < adapter->req_rx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05003405 netdev_dbg(adapter->netdev, "Initializing rx_scrq[%d] irq\n",
3406 i);
Thomas Falconea22d512016-07-06 15:35:17 -05003407 scrq = adapter->rx_scrq[i];
3408 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
Michael Ellerman99c17902016-09-10 19:59:05 +10003409 if (!scrq->irq) {
Thomas Falconea22d512016-07-06 15:35:17 -05003410 rc = -EINVAL;
3411 dev_err(dev, "Error mapping irq\n");
3412 goto req_rx_irq_failed;
3413 }
Murilo Fossa Vicentinie56e2512019-04-25 11:02:33 -03003414 snprintf(scrq->name, sizeof(scrq->name), "ibmvnic-%x-rx%d",
3415 adapter->vdev->unit_address, i);
Thomas Falconea22d512016-07-06 15:35:17 -05003416 rc = request_irq(scrq->irq, ibmvnic_interrupt_rx,
Murilo Fossa Vicentinie56e2512019-04-25 11:02:33 -03003417 0, scrq->name, scrq);
Thomas Falconea22d512016-07-06 15:35:17 -05003418 if (rc) {
3419 dev_err(dev, "Couldn't register rx irq 0x%x. rc=%d\n",
3420 scrq->irq, rc);
3421 irq_dispose_mapping(scrq->irq);
3422 goto req_rx_irq_failed;
3423 }
3424 }
3425 return rc;
3426
3427req_rx_irq_failed:
Thomas Falcon8bf371e2016-10-27 12:28:52 -05003428 for (j = 0; j < i; j++) {
Thomas Falconea22d512016-07-06 15:35:17 -05003429 free_irq(adapter->rx_scrq[j]->irq, adapter->rx_scrq[j]);
3430 irq_dispose_mapping(adapter->rx_scrq[j]->irq);
Thomas Falcon8bf371e2016-10-27 12:28:52 -05003431 }
Thomas Falconea22d512016-07-06 15:35:17 -05003432 i = adapter->req_tx_queues;
3433req_tx_irq_failed:
Thomas Falcon8bf371e2016-10-27 12:28:52 -05003434 for (j = 0; j < i; j++) {
Thomas Falconea22d512016-07-06 15:35:17 -05003435 free_irq(adapter->tx_scrq[j]->irq, adapter->tx_scrq[j]);
Thomas Falcon27a21452020-07-29 16:36:32 -05003436 irq_dispose_mapping(adapter->tx_scrq[j]->irq);
Thomas Falcon8bf371e2016-10-27 12:28:52 -05003437 }
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06003438 release_sub_crqs(adapter, 1);
Thomas Falconea22d512016-07-06 15:35:17 -05003439 return rc;
3440}
3441
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003442static int init_sub_crqs(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003443{
3444 struct device *dev = &adapter->vdev->dev;
3445 struct ibmvnic_sub_crq_queue **allqueues;
3446 int registered_queues = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003447 int total_queues;
3448 int more = 0;
Thomas Falconea22d512016-07-06 15:35:17 -05003449 int i;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003450
Thomas Falcon032c5e82015-12-21 11:26:06 -06003451 total_queues = adapter->req_tx_queues + adapter->req_rx_queues;
3452
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04003453 allqueues = kcalloc(total_queues, sizeof(*allqueues), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003454 if (!allqueues)
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003455 return -1;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003456
3457 for (i = 0; i < total_queues; i++) {
3458 allqueues[i] = init_sub_crq_queue(adapter);
3459 if (!allqueues[i]) {
3460 dev_warn(dev, "Couldn't allocate all sub-crqs\n");
3461 break;
3462 }
3463 registered_queues++;
3464 }
3465
3466 /* Make sure we were able to register the minimum number of queues */
3467 if (registered_queues <
3468 adapter->min_tx_queues + adapter->min_rx_queues) {
3469 dev_err(dev, "Fatal: Couldn't init min number of sub-crqs\n");
3470 goto tx_failed;
3471 }
3472
3473 /* Distribute the failed allocated queues*/
3474 for (i = 0; i < total_queues - registered_queues + more ; i++) {
3475 netdev_dbg(adapter->netdev, "Reducing number of queues\n");
3476 switch (i % 3) {
3477 case 0:
3478 if (adapter->req_rx_queues > adapter->min_rx_queues)
3479 adapter->req_rx_queues--;
3480 else
3481 more++;
3482 break;
3483 case 1:
3484 if (adapter->req_tx_queues > adapter->min_tx_queues)
3485 adapter->req_tx_queues--;
3486 else
3487 more++;
3488 break;
3489 }
3490 }
3491
3492 adapter->tx_scrq = kcalloc(adapter->req_tx_queues,
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04003493 sizeof(*adapter->tx_scrq), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003494 if (!adapter->tx_scrq)
3495 goto tx_failed;
3496
3497 for (i = 0; i < adapter->req_tx_queues; i++) {
3498 adapter->tx_scrq[i] = allqueues[i];
3499 adapter->tx_scrq[i]->pool_index = i;
Nathan Fontenot82e3be32018-02-21 21:33:56 -06003500 adapter->num_active_tx_scrqs++;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003501 }
3502
3503 adapter->rx_scrq = kcalloc(adapter->req_rx_queues,
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04003504 sizeof(*adapter->rx_scrq), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003505 if (!adapter->rx_scrq)
3506 goto rx_failed;
3507
3508 for (i = 0; i < adapter->req_rx_queues; i++) {
3509 adapter->rx_scrq[i] = allqueues[i + adapter->req_tx_queues];
3510 adapter->rx_scrq[i]->scrq_num = i;
Nathan Fontenot82e3be32018-02-21 21:33:56 -06003511 adapter->num_active_rx_scrqs++;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003512 }
3513
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003514 kfree(allqueues);
3515 return 0;
3516
3517rx_failed:
3518 kfree(adapter->tx_scrq);
3519 adapter->tx_scrq = NULL;
3520tx_failed:
3521 for (i = 0; i < registered_queues; i++)
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06003522 release_sub_crq_queue(adapter, allqueues[i], 1);
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003523 kfree(allqueues);
3524 return -1;
3525}
3526
Lijun Pan09081b92020-09-27 20:13:27 -05003527static void send_request_cap(struct ibmvnic_adapter *adapter, int retry)
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003528{
3529 struct device *dev = &adapter->vdev->dev;
3530 union ibmvnic_crq crq;
John Allenc26eba02017-10-26 16:23:25 -05003531 int max_entries;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003532
3533 if (!retry) {
3534 /* Sub-CRQ entries are 32 byte long */
3535 int entries_page = 4 * PAGE_SIZE / (sizeof(u64) * 4);
3536
3537 if (adapter->min_tx_entries_per_subcrq > entries_page ||
3538 adapter->min_rx_add_entries_per_subcrq > entries_page) {
3539 dev_err(dev, "Fatal, invalid entries per sub-crq\n");
3540 return;
3541 }
3542
John Allenc26eba02017-10-26 16:23:25 -05003543 if (adapter->desired.mtu)
3544 adapter->req_mtu = adapter->desired.mtu;
3545 else
3546 adapter->req_mtu = adapter->netdev->mtu + ETH_HLEN;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003547
John Allenc26eba02017-10-26 16:23:25 -05003548 if (!adapter->desired.tx_entries)
3549 adapter->desired.tx_entries =
3550 adapter->max_tx_entries_per_subcrq;
3551 if (!adapter->desired.rx_entries)
3552 adapter->desired.rx_entries =
3553 adapter->max_rx_add_entries_per_subcrq;
3554
3555 max_entries = IBMVNIC_MAX_LTB_SIZE /
3556 (adapter->req_mtu + IBMVNIC_BUFFER_HLEN);
3557
3558 if ((adapter->req_mtu + IBMVNIC_BUFFER_HLEN) *
3559 adapter->desired.tx_entries > IBMVNIC_MAX_LTB_SIZE) {
3560 adapter->desired.tx_entries = max_entries;
3561 }
3562
3563 if ((adapter->req_mtu + IBMVNIC_BUFFER_HLEN) *
3564 adapter->desired.rx_entries > IBMVNIC_MAX_LTB_SIZE) {
3565 adapter->desired.rx_entries = max_entries;
3566 }
3567
3568 if (adapter->desired.tx_entries)
3569 adapter->req_tx_entries_per_subcrq =
3570 adapter->desired.tx_entries;
3571 else
3572 adapter->req_tx_entries_per_subcrq =
3573 adapter->max_tx_entries_per_subcrq;
3574
3575 if (adapter->desired.rx_entries)
3576 adapter->req_rx_add_entries_per_subcrq =
3577 adapter->desired.rx_entries;
3578 else
3579 adapter->req_rx_add_entries_per_subcrq =
3580 adapter->max_rx_add_entries_per_subcrq;
3581
3582 if (adapter->desired.tx_queues)
3583 adapter->req_tx_queues =
3584 adapter->desired.tx_queues;
3585 else
3586 adapter->req_tx_queues =
3587 adapter->opt_tx_comp_sub_queues;
3588
3589 if (adapter->desired.rx_queues)
3590 adapter->req_rx_queues =
3591 adapter->desired.rx_queues;
3592 else
3593 adapter->req_rx_queues =
3594 adapter->opt_rx_comp_queues;
3595
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003596 adapter->req_rx_add_queues = adapter->max_rx_add_queues;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003597 }
3598
Thomas Falcon032c5e82015-12-21 11:26:06 -06003599 memset(&crq, 0, sizeof(crq));
3600 crq.request_capability.first = IBMVNIC_CRQ_CMD;
3601 crq.request_capability.cmd = REQUEST_CAPABILITY;
3602
3603 crq.request_capability.capability = cpu_to_be16(REQ_TX_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06003604 crq.request_capability.number = cpu_to_be64(adapter->req_tx_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06003605 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003606 ibmvnic_send_crq(adapter, &crq);
3607
3608 crq.request_capability.capability = cpu_to_be16(REQ_RX_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06003609 crq.request_capability.number = cpu_to_be64(adapter->req_rx_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06003610 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003611 ibmvnic_send_crq(adapter, &crq);
3612
3613 crq.request_capability.capability = cpu_to_be16(REQ_RX_ADD_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06003614 crq.request_capability.number = cpu_to_be64(adapter->req_rx_add_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06003615 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003616 ibmvnic_send_crq(adapter, &crq);
3617
3618 crq.request_capability.capability =
3619 cpu_to_be16(REQ_TX_ENTRIES_PER_SUBCRQ);
3620 crq.request_capability.number =
Thomas Falconde89e852016-03-01 10:20:09 -06003621 cpu_to_be64(adapter->req_tx_entries_per_subcrq);
Thomas Falcon901e0402017-02-15 12:17:59 -06003622 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003623 ibmvnic_send_crq(adapter, &crq);
3624
3625 crq.request_capability.capability =
3626 cpu_to_be16(REQ_RX_ADD_ENTRIES_PER_SUBCRQ);
3627 crq.request_capability.number =
Thomas Falconde89e852016-03-01 10:20:09 -06003628 cpu_to_be64(adapter->req_rx_add_entries_per_subcrq);
Thomas Falcon901e0402017-02-15 12:17:59 -06003629 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003630 ibmvnic_send_crq(adapter, &crq);
3631
3632 crq.request_capability.capability = cpu_to_be16(REQ_MTU);
Thomas Falconde89e852016-03-01 10:20:09 -06003633 crq.request_capability.number = cpu_to_be64(adapter->req_mtu);
Thomas Falcon901e0402017-02-15 12:17:59 -06003634 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003635 ibmvnic_send_crq(adapter, &crq);
3636
3637 if (adapter->netdev->flags & IFF_PROMISC) {
3638 if (adapter->promisc_supported) {
3639 crq.request_capability.capability =
3640 cpu_to_be16(PROMISC_REQUESTED);
Thomas Falconde89e852016-03-01 10:20:09 -06003641 crq.request_capability.number = cpu_to_be64(1);
Thomas Falcon901e0402017-02-15 12:17:59 -06003642 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003643 ibmvnic_send_crq(adapter, &crq);
3644 }
3645 } else {
3646 crq.request_capability.capability =
3647 cpu_to_be16(PROMISC_REQUESTED);
Thomas Falconde89e852016-03-01 10:20:09 -06003648 crq.request_capability.number = cpu_to_be64(0);
Thomas Falcon901e0402017-02-15 12:17:59 -06003649 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003650 ibmvnic_send_crq(adapter, &crq);
3651 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06003652}
3653
3654static int pending_scrq(struct ibmvnic_adapter *adapter,
3655 struct ibmvnic_sub_crq_queue *scrq)
3656{
3657 union sub_crq *entry = &scrq->msgs[scrq->cur];
Lijun Pan665ab1e2021-01-29 19:19:04 -06003658 int rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003659
Lijun Pan665ab1e2021-01-29 19:19:04 -06003660 rc = !!(entry->generic.first & IBMVNIC_CRQ_CMD_RSP);
3661
3662 /* Ensure that the SCRQ valid flag is loaded prior to loading the
3663 * contents of the SCRQ descriptor
3664 */
3665 dma_rmb();
3666
3667 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003668}
3669
3670static union sub_crq *ibmvnic_next_scrq(struct ibmvnic_adapter *adapter,
3671 struct ibmvnic_sub_crq_queue *scrq)
3672{
3673 union sub_crq *entry;
3674 unsigned long flags;
3675
3676 spin_lock_irqsave(&scrq->lock, flags);
3677 entry = &scrq->msgs[scrq->cur];
3678 if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP) {
3679 if (++scrq->cur == scrq->size)
3680 scrq->cur = 0;
3681 } else {
3682 entry = NULL;
3683 }
3684 spin_unlock_irqrestore(&scrq->lock, flags);
3685
Lijun Pan665ab1e2021-01-29 19:19:04 -06003686 /* Ensure that the SCRQ valid flag is loaded prior to loading the
3687 * contents of the SCRQ descriptor
Thomas Falconb71ec952020-12-01 09:52:10 -06003688 */
3689 dma_rmb();
3690
Thomas Falcon032c5e82015-12-21 11:26:06 -06003691 return entry;
3692}
3693
3694static union ibmvnic_crq *ibmvnic_next_crq(struct ibmvnic_adapter *adapter)
3695{
3696 struct ibmvnic_crq_queue *queue = &adapter->crq;
3697 union ibmvnic_crq *crq;
3698
3699 crq = &queue->msgs[queue->cur];
3700 if (crq->generic.first & IBMVNIC_CRQ_CMD_RSP) {
3701 if (++queue->cur == queue->size)
3702 queue->cur = 0;
3703 } else {
3704 crq = NULL;
3705 }
3706
3707 return crq;
3708}
3709
Thomas Falcon2d14d372018-07-13 12:03:32 -05003710static void print_subcrq_error(struct device *dev, int rc, const char *func)
3711{
3712 switch (rc) {
3713 case H_PARAMETER:
3714 dev_warn_ratelimited(dev,
3715 "%s failed: Send request is malformed or adapter failover pending. (rc=%d)\n",
3716 func, rc);
3717 break;
3718 case H_CLOSED:
3719 dev_warn_ratelimited(dev,
3720 "%s failed: Backing queue closed. Adapter is down or failover pending. (rc=%d)\n",
3721 func, rc);
3722 break;
3723 default:
3724 dev_err_ratelimited(dev, "%s failed: (rc=%d)\n", func, rc);
3725 break;
3726 }
3727}
3728
Thomas Falconad7775d2016-04-01 17:20:34 -05003729static int send_subcrq_indirect(struct ibmvnic_adapter *adapter,
3730 u64 remote_handle, u64 ioba, u64 num_entries)
3731{
3732 unsigned int ua = adapter->vdev->unit_address;
3733 struct device *dev = &adapter->vdev->dev;
3734 int rc;
3735
3736 /* Make sure the hypervisor sees the complete request */
Lijun Pan1a421562021-02-12 20:36:46 -06003737 dma_wmb();
Thomas Falconad7775d2016-04-01 17:20:34 -05003738 rc = plpar_hcall_norets(H_SEND_SUB_CRQ_INDIRECT, ua,
3739 cpu_to_be64(remote_handle),
3740 ioba, num_entries);
3741
Thomas Falcon2d14d372018-07-13 12:03:32 -05003742 if (rc)
3743 print_subcrq_error(dev, rc, __func__);
Thomas Falconad7775d2016-04-01 17:20:34 -05003744
3745 return rc;
3746}
3747
Thomas Falcon032c5e82015-12-21 11:26:06 -06003748static int ibmvnic_send_crq(struct ibmvnic_adapter *adapter,
3749 union ibmvnic_crq *crq)
3750{
3751 unsigned int ua = adapter->vdev->unit_address;
3752 struct device *dev = &adapter->vdev->dev;
3753 u64 *u64_crq = (u64 *)crq;
3754 int rc;
3755
3756 netdev_dbg(adapter->netdev, "Sending CRQ: %016lx %016lx\n",
Lijun Pan429aa362021-02-11 00:43:18 -06003757 (unsigned long)cpu_to_be64(u64_crq[0]),
3758 (unsigned long)cpu_to_be64(u64_crq[1]));
Thomas Falcon032c5e82015-12-21 11:26:06 -06003759
Thomas Falcon51536982018-05-23 13:37:56 -05003760 if (!adapter->crq.active &&
3761 crq->generic.first != IBMVNIC_CRQ_INIT_CMD) {
3762 dev_warn(dev, "Invalid request detected while CRQ is inactive, possible device state change during reset\n");
3763 return -EINVAL;
3764 }
3765
Thomas Falcon032c5e82015-12-21 11:26:06 -06003766 /* Make sure the hypervisor sees the complete request */
Lijun Pan1a421562021-02-12 20:36:46 -06003767 dma_wmb();
Thomas Falcon032c5e82015-12-21 11:26:06 -06003768
3769 rc = plpar_hcall_norets(H_SEND_CRQ, ua,
3770 cpu_to_be64(u64_crq[0]),
3771 cpu_to_be64(u64_crq[1]));
3772
3773 if (rc) {
Nathan Fontenotec95dff2018-02-07 13:00:24 -06003774 if (rc == H_CLOSED) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06003775 dev_warn(dev, "CRQ Queue closed\n");
Lijun Panfa68bfa2020-08-19 17:52:24 -05003776 /* do not reset, report the fail, wait for passive init from server */
Nathan Fontenotec95dff2018-02-07 13:00:24 -06003777 }
3778
Thomas Falcon032c5e82015-12-21 11:26:06 -06003779 dev_warn(dev, "Send error (rc=%d)\n", rc);
3780 }
3781
3782 return rc;
3783}
3784
3785static int ibmvnic_send_crq_init(struct ibmvnic_adapter *adapter)
3786{
Thomas Falcon36a782f2020-08-31 11:59:57 -05003787 struct device *dev = &adapter->vdev->dev;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003788 union ibmvnic_crq crq;
Thomas Falcon36a782f2020-08-31 11:59:57 -05003789 int retries = 100;
3790 int rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003791
3792 memset(&crq, 0, sizeof(crq));
3793 crq.generic.first = IBMVNIC_CRQ_INIT_CMD;
3794 crq.generic.cmd = IBMVNIC_CRQ_INIT;
3795 netdev_dbg(adapter->netdev, "Sending CRQ init\n");
3796
Thomas Falcon36a782f2020-08-31 11:59:57 -05003797 do {
3798 rc = ibmvnic_send_crq(adapter, &crq);
3799 if (rc != H_CLOSED)
3800 break;
3801 retries--;
3802 msleep(50);
3803
3804 } while (retries > 0);
3805
3806 if (rc) {
3807 dev_err(dev, "Failed to send init request, rc = %d\n", rc);
3808 return rc;
3809 }
3810
3811 return 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003812}
3813
Thomas Falcon032c5e82015-12-21 11:26:06 -06003814static int send_version_xchg(struct ibmvnic_adapter *adapter)
3815{
3816 union ibmvnic_crq crq;
3817
3818 memset(&crq, 0, sizeof(crq));
3819 crq.version_exchange.first = IBMVNIC_CRQ_CMD;
3820 crq.version_exchange.cmd = VERSION_EXCHANGE;
3821 crq.version_exchange.version = cpu_to_be16(ibmvnic_version);
3822
3823 return ibmvnic_send_crq(adapter, &crq);
3824}
3825
Nathan Fontenot37798d02017-11-08 11:23:56 -06003826struct vnic_login_client_data {
3827 u8 type;
3828 __be16 len;
Kees Cook08ea5562018-04-10 15:26:43 -07003829 char name[];
Nathan Fontenot37798d02017-11-08 11:23:56 -06003830} __packed;
3831
3832static int vnic_client_data_len(struct ibmvnic_adapter *adapter)
3833{
3834 int len;
3835
3836 /* Calculate the amount of buffer space needed for the
3837 * vnic client data in the login buffer. There are four entries,
3838 * OS name, LPAR name, device name, and a null last entry.
3839 */
3840 len = 4 * sizeof(struct vnic_login_client_data);
3841 len += 6; /* "Linux" plus NULL */
3842 len += strlen(utsname()->nodename) + 1;
3843 len += strlen(adapter->netdev->name) + 1;
3844
3845 return len;
3846}
3847
3848static void vnic_add_client_data(struct ibmvnic_adapter *adapter,
3849 struct vnic_login_client_data *vlcd)
3850{
3851 const char *os_name = "Linux";
3852 int len;
3853
3854 /* Type 1 - LPAR OS */
3855 vlcd->type = 1;
3856 len = strlen(os_name) + 1;
3857 vlcd->len = cpu_to_be16(len);
Kees Cook08ea5562018-04-10 15:26:43 -07003858 strncpy(vlcd->name, os_name, len);
3859 vlcd = (struct vnic_login_client_data *)(vlcd->name + len);
Nathan Fontenot37798d02017-11-08 11:23:56 -06003860
3861 /* Type 2 - LPAR name */
3862 vlcd->type = 2;
3863 len = strlen(utsname()->nodename) + 1;
3864 vlcd->len = cpu_to_be16(len);
Kees Cook08ea5562018-04-10 15:26:43 -07003865 strncpy(vlcd->name, utsname()->nodename, len);
3866 vlcd = (struct vnic_login_client_data *)(vlcd->name + len);
Nathan Fontenot37798d02017-11-08 11:23:56 -06003867
3868 /* Type 3 - device name */
3869 vlcd->type = 3;
3870 len = strlen(adapter->netdev->name) + 1;
3871 vlcd->len = cpu_to_be16(len);
Kees Cook08ea5562018-04-10 15:26:43 -07003872 strncpy(vlcd->name, adapter->netdev->name, len);
Nathan Fontenot37798d02017-11-08 11:23:56 -06003873}
3874
Thomas Falcon20a8ab72018-02-26 18:10:59 -06003875static int send_login(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003876{
3877 struct ibmvnic_login_rsp_buffer *login_rsp_buffer;
3878 struct ibmvnic_login_buffer *login_buffer;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003879 struct device *dev = &adapter->vdev->dev;
Dany Maddenc98d9cc2020-11-25 18:04:30 -06003880 struct vnic_login_client_data *vlcd;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003881 dma_addr_t rsp_buffer_token;
3882 dma_addr_t buffer_token;
3883 size_t rsp_buffer_size;
3884 union ibmvnic_crq crq;
Dany Maddenc98d9cc2020-11-25 18:04:30 -06003885 int client_data_len;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003886 size_t buffer_size;
3887 __be64 *tx_list_p;
3888 __be64 *rx_list_p;
Dany Maddenc98d9cc2020-11-25 18:04:30 -06003889 int rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003890 int i;
3891
Thomas Falcon20a8ab72018-02-26 18:10:59 -06003892 if (!adapter->tx_scrq || !adapter->rx_scrq) {
3893 netdev_err(adapter->netdev,
3894 "RX or TX queues are not allocated, device login failed\n");
3895 return -1;
3896 }
3897
Lijun Pana0c8be52020-12-19 15:39:19 -06003898 release_login_buffer(adapter);
Thomas Falcon34f0f4e2018-02-13 18:23:40 -06003899 release_login_rsp_buffer(adapter);
Lijun Pana0c8be52020-12-19 15:39:19 -06003900
Nathan Fontenot37798d02017-11-08 11:23:56 -06003901 client_data_len = vnic_client_data_len(adapter);
3902
Thomas Falcon032c5e82015-12-21 11:26:06 -06003903 buffer_size =
3904 sizeof(struct ibmvnic_login_buffer) +
Nathan Fontenot37798d02017-11-08 11:23:56 -06003905 sizeof(u64) * (adapter->req_tx_queues + adapter->req_rx_queues) +
3906 client_data_len;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003907
Nathan Fontenot37798d02017-11-08 11:23:56 -06003908 login_buffer = kzalloc(buffer_size, GFP_ATOMIC);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003909 if (!login_buffer)
3910 goto buf_alloc_failed;
3911
3912 buffer_token = dma_map_single(dev, login_buffer, buffer_size,
3913 DMA_TO_DEVICE);
3914 if (dma_mapping_error(dev, buffer_token)) {
3915 dev_err(dev, "Couldn't map login buffer\n");
3916 goto buf_map_failed;
3917 }
3918
John Allen498cd8e2016-04-06 11:49:55 -05003919 rsp_buffer_size = sizeof(struct ibmvnic_login_rsp_buffer) +
3920 sizeof(u64) * adapter->req_tx_queues +
3921 sizeof(u64) * adapter->req_rx_queues +
3922 sizeof(u64) * adapter->req_rx_queues +
3923 sizeof(u8) * IBMVNIC_TX_DESC_VERSIONS;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003924
3925 login_rsp_buffer = kmalloc(rsp_buffer_size, GFP_ATOMIC);
3926 if (!login_rsp_buffer)
3927 goto buf_rsp_alloc_failed;
3928
3929 rsp_buffer_token = dma_map_single(dev, login_rsp_buffer,
3930 rsp_buffer_size, DMA_FROM_DEVICE);
3931 if (dma_mapping_error(dev, rsp_buffer_token)) {
3932 dev_err(dev, "Couldn't map login rsp buffer\n");
3933 goto buf_rsp_map_failed;
3934 }
Nathan Fontenot661a2622017-04-19 13:44:58 -04003935
Thomas Falcon032c5e82015-12-21 11:26:06 -06003936 adapter->login_buf = login_buffer;
3937 adapter->login_buf_token = buffer_token;
3938 adapter->login_buf_sz = buffer_size;
3939 adapter->login_rsp_buf = login_rsp_buffer;
3940 adapter->login_rsp_buf_token = rsp_buffer_token;
3941 adapter->login_rsp_buf_sz = rsp_buffer_size;
3942
3943 login_buffer->len = cpu_to_be32(buffer_size);
3944 login_buffer->version = cpu_to_be32(INITIAL_VERSION_LB);
3945 login_buffer->num_txcomp_subcrqs = cpu_to_be32(adapter->req_tx_queues);
3946 login_buffer->off_txcomp_subcrqs =
3947 cpu_to_be32(sizeof(struct ibmvnic_login_buffer));
3948 login_buffer->num_rxcomp_subcrqs = cpu_to_be32(adapter->req_rx_queues);
3949 login_buffer->off_rxcomp_subcrqs =
3950 cpu_to_be32(sizeof(struct ibmvnic_login_buffer) +
3951 sizeof(u64) * adapter->req_tx_queues);
3952 login_buffer->login_rsp_ioba = cpu_to_be32(rsp_buffer_token);
3953 login_buffer->login_rsp_len = cpu_to_be32(rsp_buffer_size);
3954
3955 tx_list_p = (__be64 *)((char *)login_buffer +
3956 sizeof(struct ibmvnic_login_buffer));
3957 rx_list_p = (__be64 *)((char *)login_buffer +
3958 sizeof(struct ibmvnic_login_buffer) +
3959 sizeof(u64) * adapter->req_tx_queues);
3960
3961 for (i = 0; i < adapter->req_tx_queues; i++) {
3962 if (adapter->tx_scrq[i]) {
Lijun Pan914789a2021-02-11 00:43:21 -06003963 tx_list_p[i] =
3964 cpu_to_be64(adapter->tx_scrq[i]->crq_num);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003965 }
3966 }
3967
3968 for (i = 0; i < adapter->req_rx_queues; i++) {
3969 if (adapter->rx_scrq[i]) {
Lijun Pan914789a2021-02-11 00:43:21 -06003970 rx_list_p[i] =
3971 cpu_to_be64(adapter->rx_scrq[i]->crq_num);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003972 }
3973 }
3974
Nathan Fontenot37798d02017-11-08 11:23:56 -06003975 /* Insert vNIC login client data */
3976 vlcd = (struct vnic_login_client_data *)
3977 ((char *)rx_list_p + (sizeof(u64) * adapter->req_rx_queues));
3978 login_buffer->client_data_offset =
3979 cpu_to_be32((char *)vlcd - (char *)login_buffer);
3980 login_buffer->client_data_len = cpu_to_be32(client_data_len);
3981
3982 vnic_add_client_data(adapter, vlcd);
3983
Thomas Falcon032c5e82015-12-21 11:26:06 -06003984 netdev_dbg(adapter->netdev, "Login Buffer:\n");
3985 for (i = 0; i < (adapter->login_buf_sz - 1) / 8 + 1; i++) {
3986 netdev_dbg(adapter->netdev, "%016lx\n",
Lijun Pan429aa362021-02-11 00:43:18 -06003987 ((unsigned long *)(adapter->login_buf))[i]);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003988 }
3989
3990 memset(&crq, 0, sizeof(crq));
3991 crq.login.first = IBMVNIC_CRQ_CMD;
3992 crq.login.cmd = LOGIN;
3993 crq.login.ioba = cpu_to_be32(buffer_token);
3994 crq.login.len = cpu_to_be32(buffer_size);
Sukadev Bhattiprolu76cdc5c2020-11-25 18:04:29 -06003995
3996 adapter->login_pending = true;
Dany Maddenc98d9cc2020-11-25 18:04:30 -06003997 rc = ibmvnic_send_crq(adapter, &crq);
3998 if (rc) {
3999 adapter->login_pending = false;
4000 netdev_err(adapter->netdev, "Failed to send login, rc=%d\n", rc);
4001 goto buf_rsp_map_failed;
4002 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06004003
Thomas Falcon20a8ab72018-02-26 18:10:59 -06004004 return 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004005
Thomas Falcon032c5e82015-12-21 11:26:06 -06004006buf_rsp_map_failed:
4007 kfree(login_rsp_buffer);
Dany Maddenc98d9cc2020-11-25 18:04:30 -06004008 adapter->login_rsp_buf = NULL;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004009buf_rsp_alloc_failed:
4010 dma_unmap_single(dev, buffer_token, buffer_size, DMA_TO_DEVICE);
4011buf_map_failed:
4012 kfree(login_buffer);
Dany Maddenc98d9cc2020-11-25 18:04:30 -06004013 adapter->login_buf = NULL;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004014buf_alloc_failed:
Thomas Falcon20a8ab72018-02-26 18:10:59 -06004015 return -1;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004016}
4017
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05004018static int send_request_map(struct ibmvnic_adapter *adapter, dma_addr_t addr,
4019 u32 len, u8 map_id)
Thomas Falcon032c5e82015-12-21 11:26:06 -06004020{
4021 union ibmvnic_crq crq;
4022
4023 memset(&crq, 0, sizeof(crq));
4024 crq.request_map.first = IBMVNIC_CRQ_CMD;
4025 crq.request_map.cmd = REQUEST_MAP;
4026 crq.request_map.map_id = map_id;
4027 crq.request_map.ioba = cpu_to_be32(addr);
4028 crq.request_map.len = cpu_to_be32(len);
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05004029 return ibmvnic_send_crq(adapter, &crq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004030}
4031
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05004032static int send_request_unmap(struct ibmvnic_adapter *adapter, u8 map_id)
Thomas Falcon032c5e82015-12-21 11:26:06 -06004033{
4034 union ibmvnic_crq crq;
4035
4036 memset(&crq, 0, sizeof(crq));
4037 crq.request_unmap.first = IBMVNIC_CRQ_CMD;
4038 crq.request_unmap.cmd = REQUEST_UNMAP;
4039 crq.request_unmap.map_id = map_id;
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05004040 return ibmvnic_send_crq(adapter, &crq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004041}
4042
Lijun Pan69980d02020-09-27 20:13:28 -05004043static void send_query_map(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06004044{
4045 union ibmvnic_crq crq;
4046
4047 memset(&crq, 0, sizeof(crq));
4048 crq.query_map.first = IBMVNIC_CRQ_CMD;
4049 crq.query_map.cmd = QUERY_MAP;
4050 ibmvnic_send_crq(adapter, &crq);
4051}
4052
4053/* Send a series of CRQs requesting various capabilities of the VNIC server */
Lijun Pan491099a2020-09-27 20:13:26 -05004054static void send_query_cap(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06004055{
4056 union ibmvnic_crq crq;
4057
Thomas Falcon901e0402017-02-15 12:17:59 -06004058 atomic_set(&adapter->running_cap_crqs, 0);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004059 memset(&crq, 0, sizeof(crq));
4060 crq.query_capability.first = IBMVNIC_CRQ_CMD;
4061 crq.query_capability.cmd = QUERY_CAPABILITY;
4062
4063 crq.query_capability.capability = cpu_to_be16(MIN_TX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06004064 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004065 ibmvnic_send_crq(adapter, &crq);
4066
4067 crq.query_capability.capability = cpu_to_be16(MIN_RX_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_ADD_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(MAX_TX_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_RX_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_ADD_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 =
4088 cpu_to_be16(MIN_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06004089 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004090 ibmvnic_send_crq(adapter, &crq);
4091
4092 crq.query_capability.capability =
4093 cpu_to_be16(MIN_RX_ADD_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06004094 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004095 ibmvnic_send_crq(adapter, &crq);
4096
4097 crq.query_capability.capability =
4098 cpu_to_be16(MAX_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06004099 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004100 ibmvnic_send_crq(adapter, &crq);
4101
4102 crq.query_capability.capability =
4103 cpu_to_be16(MAX_RX_ADD_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06004104 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004105 ibmvnic_send_crq(adapter, &crq);
4106
4107 crq.query_capability.capability = cpu_to_be16(TCP_IP_OFFLOAD);
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(PROMISC_SUPPORTED);
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(MIN_MTU);
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(MAX_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_MULTICAST_FILTERS);
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(VLAN_HEADER_INSERTION);
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
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04004131 crq.query_capability.capability = cpu_to_be16(RX_VLAN_HEADER_INSERTION);
4132 atomic_inc(&adapter->running_cap_crqs);
4133 ibmvnic_send_crq(adapter, &crq);
4134
Thomas Falcon032c5e82015-12-21 11:26:06 -06004135 crq.query_capability.capability = cpu_to_be16(MAX_TX_SG_ENTRIES);
Thomas Falcon901e0402017-02-15 12:17:59 -06004136 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004137 ibmvnic_send_crq(adapter, &crq);
4138
4139 crq.query_capability.capability = cpu_to_be16(RX_SG_SUPPORTED);
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(OPT_TX_COMP_SUB_QUEUES);
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_RX_COMP_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 =
4152 cpu_to_be16(OPT_RX_BUFADD_Q_PER_RX_COMP_Q);
Thomas Falcon901e0402017-02-15 12:17:59 -06004153 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004154 ibmvnic_send_crq(adapter, &crq);
4155
4156 crq.query_capability.capability =
4157 cpu_to_be16(OPT_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06004158 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004159 ibmvnic_send_crq(adapter, &crq);
4160
4161 crq.query_capability.capability =
4162 cpu_to_be16(OPT_RXBA_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06004163 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004164 ibmvnic_send_crq(adapter, &crq);
4165
4166 crq.query_capability.capability = cpu_to_be16(TX_RX_DESC_REQ);
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
Lijun Pan16e811f2020-09-27 20:13:29 -05004171static void send_query_ip_offload(struct ibmvnic_adapter *adapter)
4172{
4173 int buf_sz = sizeof(struct ibmvnic_query_ip_offload_buffer);
4174 struct device *dev = &adapter->vdev->dev;
4175 union ibmvnic_crq crq;
4176
4177 adapter->ip_offload_tok =
4178 dma_map_single(dev,
4179 &adapter->ip_offload_buf,
4180 buf_sz,
4181 DMA_FROM_DEVICE);
4182
4183 if (dma_mapping_error(dev, adapter->ip_offload_tok)) {
4184 if (!firmware_has_feature(FW_FEATURE_CMO))
4185 dev_err(dev, "Couldn't map offload buffer\n");
4186 return;
4187 }
4188
4189 memset(&crq, 0, sizeof(crq));
4190 crq.query_ip_offload.first = IBMVNIC_CRQ_CMD;
4191 crq.query_ip_offload.cmd = QUERY_IP_OFFLOAD;
4192 crq.query_ip_offload.len = cpu_to_be32(buf_sz);
4193 crq.query_ip_offload.ioba =
4194 cpu_to_be32(adapter->ip_offload_tok);
4195
4196 ibmvnic_send_crq(adapter, &crq);
4197}
4198
Lijun Pan46899bd2020-09-27 20:13:30 -05004199static void send_control_ip_offload(struct ibmvnic_adapter *adapter)
4200{
4201 struct ibmvnic_control_ip_offload_buffer *ctrl_buf = &adapter->ip_offload_ctrl;
4202 struct ibmvnic_query_ip_offload_buffer *buf = &adapter->ip_offload_buf;
4203 struct device *dev = &adapter->vdev->dev;
4204 netdev_features_t old_hw_features = 0;
4205 union ibmvnic_crq crq;
4206
4207 adapter->ip_offload_ctrl_tok =
4208 dma_map_single(dev,
4209 ctrl_buf,
4210 sizeof(adapter->ip_offload_ctrl),
4211 DMA_TO_DEVICE);
4212
4213 if (dma_mapping_error(dev, adapter->ip_offload_ctrl_tok)) {
4214 dev_err(dev, "Couldn't map ip offload control buffer\n");
4215 return;
4216 }
4217
4218 ctrl_buf->len = cpu_to_be32(sizeof(adapter->ip_offload_ctrl));
4219 ctrl_buf->version = cpu_to_be32(INITIAL_VERSION_IOB);
4220 ctrl_buf->ipv4_chksum = buf->ipv4_chksum;
4221 ctrl_buf->ipv6_chksum = buf->ipv6_chksum;
4222 ctrl_buf->tcp_ipv4_chksum = buf->tcp_ipv4_chksum;
4223 ctrl_buf->udp_ipv4_chksum = buf->udp_ipv4_chksum;
4224 ctrl_buf->tcp_ipv6_chksum = buf->tcp_ipv6_chksum;
4225 ctrl_buf->udp_ipv6_chksum = buf->udp_ipv6_chksum;
4226 ctrl_buf->large_tx_ipv4 = buf->large_tx_ipv4;
4227 ctrl_buf->large_tx_ipv6 = buf->large_tx_ipv6;
4228
4229 /* large_rx disabled for now, additional features needed */
4230 ctrl_buf->large_rx_ipv4 = 0;
4231 ctrl_buf->large_rx_ipv6 = 0;
4232
4233 if (adapter->state != VNIC_PROBING) {
4234 old_hw_features = adapter->netdev->hw_features;
4235 adapter->netdev->hw_features = 0;
4236 }
4237
4238 adapter->netdev->hw_features = NETIF_F_SG | NETIF_F_GSO | NETIF_F_GRO;
4239
4240 if (buf->tcp_ipv4_chksum || buf->udp_ipv4_chksum)
4241 adapter->netdev->hw_features |= NETIF_F_IP_CSUM;
4242
4243 if (buf->tcp_ipv6_chksum || buf->udp_ipv6_chksum)
4244 adapter->netdev->hw_features |= NETIF_F_IPV6_CSUM;
4245
4246 if ((adapter->netdev->features &
4247 (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)))
4248 adapter->netdev->hw_features |= NETIF_F_RXCSUM;
4249
4250 if (buf->large_tx_ipv4)
4251 adapter->netdev->hw_features |= NETIF_F_TSO;
4252 if (buf->large_tx_ipv6)
4253 adapter->netdev->hw_features |= NETIF_F_TSO6;
4254
4255 if (adapter->state == VNIC_PROBING) {
4256 adapter->netdev->features |= adapter->netdev->hw_features;
4257 } else if (old_hw_features != adapter->netdev->hw_features) {
4258 netdev_features_t tmp = 0;
4259
4260 /* disable features no longer supported */
4261 adapter->netdev->features &= adapter->netdev->hw_features;
4262 /* turn on features now supported if previously enabled */
4263 tmp = (old_hw_features ^ adapter->netdev->hw_features) &
4264 adapter->netdev->hw_features;
4265 adapter->netdev->features |=
4266 tmp & adapter->netdev->wanted_features;
4267 }
4268
4269 memset(&crq, 0, sizeof(crq));
4270 crq.control_ip_offload.first = IBMVNIC_CRQ_CMD;
4271 crq.control_ip_offload.cmd = CONTROL_IP_OFFLOAD;
4272 crq.control_ip_offload.len =
4273 cpu_to_be32(sizeof(adapter->ip_offload_ctrl));
4274 crq.control_ip_offload.ioba = cpu_to_be32(adapter->ip_offload_ctrl_tok);
4275 ibmvnic_send_crq(adapter, &crq);
4276}
4277
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02004278static void handle_vpd_size_rsp(union ibmvnic_crq *crq,
4279 struct ibmvnic_adapter *adapter)
4280{
4281 struct device *dev = &adapter->vdev->dev;
4282
4283 if (crq->get_vpd_size_rsp.rc.code) {
4284 dev_err(dev, "Error retrieving VPD size, rc=%x\n",
4285 crq->get_vpd_size_rsp.rc.code);
4286 complete(&adapter->fw_done);
4287 return;
4288 }
4289
4290 adapter->vpd->len = be64_to_cpu(crq->get_vpd_size_rsp.len);
4291 complete(&adapter->fw_done);
4292}
4293
4294static void handle_vpd_rsp(union ibmvnic_crq *crq,
4295 struct ibmvnic_adapter *adapter)
4296{
4297 struct device *dev = &adapter->vdev->dev;
Desnes Augusto Nunes do Rosario21a25452018-02-05 14:33:55 -02004298 unsigned char *substr = NULL;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02004299 u8 fw_level_len = 0;
4300
4301 memset(adapter->fw_version, 0, 32);
4302
4303 dma_unmap_single(dev, adapter->vpd->dma_addr, adapter->vpd->len,
4304 DMA_FROM_DEVICE);
4305
4306 if (crq->get_vpd_rsp.rc.code) {
4307 dev_err(dev, "Error retrieving VPD from device, rc=%x\n",
4308 crq->get_vpd_rsp.rc.code);
4309 goto complete;
4310 }
4311
4312 /* get the position of the firmware version info
4313 * located after the ASCII 'RM' substring in the buffer
4314 */
4315 substr = strnstr(adapter->vpd->buff, "RM", adapter->vpd->len);
4316 if (!substr) {
Desnes Augusto Nunes do Rosarioa1073112018-02-01 16:04:30 -02004317 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 -02004318 goto complete;
4319 }
4320
4321 /* get length of firmware level ASCII substring */
4322 if ((substr + 2) < (adapter->vpd->buff + adapter->vpd->len)) {
4323 fw_level_len = *(substr + 2);
4324 } else {
4325 dev_info(dev, "Length of FW substr extrapolated VDP buff\n");
4326 goto complete;
4327 }
4328
4329 /* copy firmware version string from vpd into adapter */
4330 if ((substr + 3 + fw_level_len) <
4331 (adapter->vpd->buff + adapter->vpd->len)) {
Desnes Augusto Nunes do Rosario21a25452018-02-05 14:33:55 -02004332 strncpy((char *)adapter->fw_version, substr + 3, fw_level_len);
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02004333 } else {
4334 dev_info(dev, "FW substr extrapolated VPD buff\n");
4335 }
4336
4337complete:
Desnes Augusto Nunes do Rosario21a25452018-02-05 14:33:55 -02004338 if (adapter->fw_version[0] == '\0')
4339 strncpy((char *)adapter->fw_version, "N/A", 3 * sizeof(char));
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02004340 complete(&adapter->fw_done);
4341}
4342
Thomas Falcon032c5e82015-12-21 11:26:06 -06004343static void handle_query_ip_offload_rsp(struct ibmvnic_adapter *adapter)
4344{
4345 struct device *dev = &adapter->vdev->dev;
4346 struct ibmvnic_query_ip_offload_buffer *buf = &adapter->ip_offload_buf;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004347 int i;
4348
4349 dma_unmap_single(dev, adapter->ip_offload_tok,
4350 sizeof(adapter->ip_offload_buf), DMA_FROM_DEVICE);
4351
4352 netdev_dbg(adapter->netdev, "Query IP Offload Buffer:\n");
4353 for (i = 0; i < (sizeof(adapter->ip_offload_buf) - 1) / 8 + 1; i++)
4354 netdev_dbg(adapter->netdev, "%016lx\n",
Lijun Pan429aa362021-02-11 00:43:18 -06004355 ((unsigned long *)(buf))[i]);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004356
4357 netdev_dbg(adapter->netdev, "ipv4_chksum = %d\n", buf->ipv4_chksum);
4358 netdev_dbg(adapter->netdev, "ipv6_chksum = %d\n", buf->ipv6_chksum);
4359 netdev_dbg(adapter->netdev, "tcp_ipv4_chksum = %d\n",
4360 buf->tcp_ipv4_chksum);
4361 netdev_dbg(adapter->netdev, "tcp_ipv6_chksum = %d\n",
4362 buf->tcp_ipv6_chksum);
4363 netdev_dbg(adapter->netdev, "udp_ipv4_chksum = %d\n",
4364 buf->udp_ipv4_chksum);
4365 netdev_dbg(adapter->netdev, "udp_ipv6_chksum = %d\n",
4366 buf->udp_ipv6_chksum);
4367 netdev_dbg(adapter->netdev, "large_tx_ipv4 = %d\n",
4368 buf->large_tx_ipv4);
4369 netdev_dbg(adapter->netdev, "large_tx_ipv6 = %d\n",
4370 buf->large_tx_ipv6);
4371 netdev_dbg(adapter->netdev, "large_rx_ipv4 = %d\n",
4372 buf->large_rx_ipv4);
4373 netdev_dbg(adapter->netdev, "large_rx_ipv6 = %d\n",
4374 buf->large_rx_ipv6);
4375 netdev_dbg(adapter->netdev, "max_ipv4_hdr_sz = %d\n",
4376 buf->max_ipv4_header_size);
4377 netdev_dbg(adapter->netdev, "max_ipv6_hdr_sz = %d\n",
4378 buf->max_ipv6_header_size);
4379 netdev_dbg(adapter->netdev, "max_tcp_hdr_size = %d\n",
4380 buf->max_tcp_header_size);
4381 netdev_dbg(adapter->netdev, "max_udp_hdr_size = %d\n",
4382 buf->max_udp_header_size);
4383 netdev_dbg(adapter->netdev, "max_large_tx_size = %d\n",
4384 buf->max_large_tx_size);
4385 netdev_dbg(adapter->netdev, "max_large_rx_size = %d\n",
4386 buf->max_large_rx_size);
4387 netdev_dbg(adapter->netdev, "ipv6_ext_hdr = %d\n",
4388 buf->ipv6_extension_header);
4389 netdev_dbg(adapter->netdev, "tcp_pseudosum_req = %d\n",
4390 buf->tcp_pseudosum_req);
4391 netdev_dbg(adapter->netdev, "num_ipv6_ext_hd = %d\n",
4392 buf->num_ipv6_ext_headers);
4393 netdev_dbg(adapter->netdev, "off_ipv6_ext_hd = %d\n",
4394 buf->off_ipv6_ext_headers);
4395
Lijun Pan46899bd2020-09-27 20:13:30 -05004396 send_control_ip_offload(adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004397}
4398
Thomas Falconc9008d32018-08-06 21:39:59 -05004399static const char *ibmvnic_fw_err_cause(u16 cause)
4400{
4401 switch (cause) {
4402 case ADAPTER_PROBLEM:
4403 return "adapter problem";
4404 case BUS_PROBLEM:
4405 return "bus problem";
4406 case FW_PROBLEM:
4407 return "firmware problem";
4408 case DD_PROBLEM:
4409 return "device driver problem";
4410 case EEH_RECOVERY:
4411 return "EEH recovery";
4412 case FW_UPDATED:
4413 return "firmware updated";
4414 case LOW_MEMORY:
4415 return "low Memory";
4416 default:
4417 return "unknown";
4418 }
4419}
4420
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04004421static void handle_error_indication(union ibmvnic_crq *crq,
4422 struct ibmvnic_adapter *adapter)
4423{
4424 struct device *dev = &adapter->vdev->dev;
Thomas Falconc9008d32018-08-06 21:39:59 -05004425 u16 cause;
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04004426
Thomas Falconc9008d32018-08-06 21:39:59 -05004427 cause = be16_to_cpu(crq->error_indication.error_cause);
4428
4429 dev_warn_ratelimited(dev,
4430 "Firmware reports %serror, cause: %s. Starting recovery...\n",
4431 crq->error_indication.flags
4432 & IBMVNIC_FATAL_ERROR ? "FATAL " : "",
4433 ibmvnic_fw_err_cause(cause));
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04004434
Nathan Fontenoted651a12017-05-03 14:04:38 -04004435 if (crq->error_indication.flags & IBMVNIC_FATAL_ERROR)
4436 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
John Allen8cb31cf2017-05-26 10:30:37 -04004437 else
4438 ibmvnic_reset(adapter, VNIC_RESET_NON_FATAL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004439}
4440
Thomas Falconf8136142018-01-29 13:45:05 -06004441static int handle_change_mac_rsp(union ibmvnic_crq *crq,
4442 struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06004443{
4444 struct net_device *netdev = adapter->netdev;
4445 struct device *dev = &adapter->vdev->dev;
4446 long rc;
4447
4448 rc = crq->change_mac_addr_rsp.rc.code;
4449 if (rc) {
4450 dev_err(dev, "Error %ld in CHANGE_MAC_ADDR_RSP\n", rc);
Thomas Falconf8136142018-01-29 13:45:05 -06004451 goto out;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004452 }
Lijun Pand9b0e592020-10-20 17:39:19 -05004453 /* crq->change_mac_addr.mac_addr is the requested one
4454 * crq->change_mac_addr_rsp.mac_addr is the returned valid one.
4455 */
Thomas Falcon62740e92019-05-09 23:13:43 -05004456 ether_addr_copy(netdev->dev_addr,
4457 &crq->change_mac_addr_rsp.mac_addr[0]);
Lijun Pand9b0e592020-10-20 17:39:19 -05004458 ether_addr_copy(adapter->mac_addr,
4459 &crq->change_mac_addr_rsp.mac_addr[0]);
Thomas Falconf8136142018-01-29 13:45:05 -06004460out:
4461 complete(&adapter->fw_done);
4462 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004463}
4464
4465static void handle_request_cap_rsp(union ibmvnic_crq *crq,
4466 struct ibmvnic_adapter *adapter)
4467{
4468 struct device *dev = &adapter->vdev->dev;
4469 u64 *req_value;
4470 char *name;
4471
Thomas Falcon901e0402017-02-15 12:17:59 -06004472 atomic_dec(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004473 switch (be16_to_cpu(crq->request_capability_rsp.capability)) {
4474 case REQ_TX_QUEUES:
4475 req_value = &adapter->req_tx_queues;
4476 name = "tx";
4477 break;
4478 case REQ_RX_QUEUES:
4479 req_value = &adapter->req_rx_queues;
4480 name = "rx";
4481 break;
4482 case REQ_RX_ADD_QUEUES:
4483 req_value = &adapter->req_rx_add_queues;
4484 name = "rx_add";
4485 break;
4486 case REQ_TX_ENTRIES_PER_SUBCRQ:
4487 req_value = &adapter->req_tx_entries_per_subcrq;
4488 name = "tx_entries_per_subcrq";
4489 break;
4490 case REQ_RX_ADD_ENTRIES_PER_SUBCRQ:
4491 req_value = &adapter->req_rx_add_entries_per_subcrq;
4492 name = "rx_add_entries_per_subcrq";
4493 break;
4494 case REQ_MTU:
4495 req_value = &adapter->req_mtu;
4496 name = "mtu";
4497 break;
4498 case PROMISC_REQUESTED:
4499 req_value = &adapter->promisc;
4500 name = "promisc";
4501 break;
4502 default:
4503 dev_err(dev, "Got invalid cap request rsp %d\n",
4504 crq->request_capability.capability);
4505 return;
4506 }
4507
4508 switch (crq->request_capability_rsp.rc.code) {
4509 case SUCCESS:
4510 break;
4511 case PARTIALSUCCESS:
4512 dev_info(dev, "req=%lld, rsp=%ld in %s queue, retrying.\n",
4513 *req_value,
Lijun Pan914789a2021-02-11 00:43:21 -06004514 (long)be64_to_cpu(crq->request_capability_rsp.number),
4515 name);
John Allene7913802018-01-18 16:27:12 -06004516
4517 if (be16_to_cpu(crq->request_capability_rsp.capability) ==
4518 REQ_MTU) {
4519 pr_err("mtu of %llu is not supported. Reverting.\n",
4520 *req_value);
4521 *req_value = adapter->fallback.mtu;
4522 } else {
4523 *req_value =
4524 be64_to_cpu(crq->request_capability_rsp.number);
4525 }
4526
Lijun Pan09081b92020-09-27 20:13:27 -05004527 send_request_cap(adapter, 1);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004528 return;
4529 default:
4530 dev_err(dev, "Error %d in request cap rsp\n",
4531 crq->request_capability_rsp.rc.code);
4532 return;
4533 }
4534
4535 /* Done receiving requested capabilities, query IP offload support */
Thomas Falcon901e0402017-02-15 12:17:59 -06004536 if (atomic_read(&adapter->running_cap_crqs) == 0) {
Thomas Falcon249168a2017-02-15 12:18:00 -06004537 adapter->wait_capability = false;
Lijun Pan16e811f2020-09-27 20:13:29 -05004538 send_query_ip_offload(adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004539 }
4540}
4541
4542static int handle_login_rsp(union ibmvnic_crq *login_rsp_crq,
4543 struct ibmvnic_adapter *adapter)
4544{
4545 struct device *dev = &adapter->vdev->dev;
John Allenc26eba02017-10-26 16:23:25 -05004546 struct net_device *netdev = adapter->netdev;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004547 struct ibmvnic_login_rsp_buffer *login_rsp = adapter->login_rsp_buf;
4548 struct ibmvnic_login_buffer *login = adapter->login_buf;
Cristobal Fornof3ae59c2020-08-19 13:16:23 -05004549 u64 *tx_handle_array;
4550 u64 *rx_handle_array;
4551 int num_tx_pools;
4552 int num_rx_pools;
Thomas Falcon507ebe62020-08-21 13:39:01 -05004553 u64 *size_array;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004554 int i;
4555
Sukadev Bhattiprolu76cdc5c2020-11-25 18:04:29 -06004556 /* CHECK: Test/set of login_pending does not need to be atomic
4557 * because only ibmvnic_tasklet tests/clears this.
4558 */
4559 if (!adapter->login_pending) {
4560 netdev_warn(netdev, "Ignoring unexpected login response\n");
4561 return 0;
4562 }
4563 adapter->login_pending = false;
4564
Thomas Falcon032c5e82015-12-21 11:26:06 -06004565 dma_unmap_single(dev, adapter->login_buf_token, adapter->login_buf_sz,
Thomas Falcon37e40fa2018-04-06 18:37:02 -05004566 DMA_TO_DEVICE);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004567 dma_unmap_single(dev, adapter->login_rsp_buf_token,
Thomas Falcon37e40fa2018-04-06 18:37:02 -05004568 adapter->login_rsp_buf_sz, DMA_FROM_DEVICE);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004569
John Allen498cd8e2016-04-06 11:49:55 -05004570 /* If the number of queues requested can't be allocated by the
4571 * server, the login response will return with code 1. We will need
4572 * to resend the login buffer with fewer queues requested.
4573 */
4574 if (login_rsp_crq->generic.rc.code) {
Nathan Fontenot64d92aa2018-04-11 10:09:32 -05004575 adapter->init_done_rc = login_rsp_crq->generic.rc.code;
John Allen498cd8e2016-04-06 11:49:55 -05004576 complete(&adapter->init_done);
4577 return 0;
4578 }
4579
John Allenc26eba02017-10-26 16:23:25 -05004580 netdev->mtu = adapter->req_mtu - ETH_HLEN;
4581
Thomas Falcon032c5e82015-12-21 11:26:06 -06004582 netdev_dbg(adapter->netdev, "Login Response Buffer:\n");
4583 for (i = 0; i < (adapter->login_rsp_buf_sz - 1) / 8 + 1; i++) {
4584 netdev_dbg(adapter->netdev, "%016lx\n",
Lijun Pan429aa362021-02-11 00:43:18 -06004585 ((unsigned long *)(adapter->login_rsp_buf))[i]);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004586 }
4587
4588 /* Sanity checks */
4589 if (login->num_txcomp_subcrqs != login_rsp->num_txsubm_subcrqs ||
4590 (be32_to_cpu(login->num_rxcomp_subcrqs) *
4591 adapter->req_rx_add_queues !=
4592 be32_to_cpu(login_rsp->num_rxadd_subcrqs))) {
4593 dev_err(dev, "FATAL: Inconsistent login and login rsp\n");
Dany Madden31d6b402020-11-25 18:04:24 -06004594 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004595 return -EIO;
4596 }
Thomas Falcon507ebe62020-08-21 13:39:01 -05004597 size_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
4598 be32_to_cpu(adapter->login_rsp_buf->off_rxadd_buff_size));
4599 /* variable buffer sizes are not supported, so just read the
4600 * first entry.
4601 */
4602 adapter->cur_rx_buf_sz = be64_to_cpu(size_array[0]);
Cristobal Fornof3ae59c2020-08-19 13:16:23 -05004603
4604 num_tx_pools = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
4605 num_rx_pools = be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
4606
4607 tx_handle_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
4608 be32_to_cpu(adapter->login_rsp_buf->off_txsubm_subcrqs));
4609 rx_handle_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
4610 be32_to_cpu(adapter->login_rsp_buf->off_rxadd_subcrqs));
4611
4612 for (i = 0; i < num_tx_pools; i++)
4613 adapter->tx_scrq[i]->handle = tx_handle_array[i];
4614
4615 for (i = 0; i < num_rx_pools; i++)
4616 adapter->rx_scrq[i]->handle = rx_handle_array[i];
4617
Thomas Falcon507ebe62020-08-21 13:39:01 -05004618 adapter->num_active_tx_scrqs = num_tx_pools;
4619 adapter->num_active_rx_scrqs = num_rx_pools;
Cristobal Fornof3ae59c2020-08-19 13:16:23 -05004620 release_login_rsp_buffer(adapter);
Thomas Falcona2c0f032018-02-21 18:18:30 -06004621 release_login_buffer(adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004622 complete(&adapter->init_done);
4623
Thomas Falcon032c5e82015-12-21 11:26:06 -06004624 return 0;
4625}
4626
Thomas Falcon032c5e82015-12-21 11:26:06 -06004627static void handle_request_unmap_rsp(union ibmvnic_crq *crq,
4628 struct ibmvnic_adapter *adapter)
4629{
4630 struct device *dev = &adapter->vdev->dev;
4631 long rc;
4632
4633 rc = crq->request_unmap_rsp.rc.code;
4634 if (rc)
4635 dev_err(dev, "Error %ld in REQUEST_UNMAP_RSP\n", rc);
4636}
4637
4638static void handle_query_map_rsp(union ibmvnic_crq *crq,
4639 struct ibmvnic_adapter *adapter)
4640{
4641 struct net_device *netdev = adapter->netdev;
4642 struct device *dev = &adapter->vdev->dev;
4643 long rc;
4644
4645 rc = crq->query_map_rsp.rc.code;
4646 if (rc) {
4647 dev_err(dev, "Error %ld in QUERY_MAP_RSP\n", rc);
4648 return;
4649 }
4650 netdev_dbg(netdev, "page_size = %d\ntot_pages = %d\nfree_pages = %d\n",
4651 crq->query_map_rsp.page_size, crq->query_map_rsp.tot_pages,
4652 crq->query_map_rsp.free_pages);
4653}
4654
4655static void handle_query_cap_rsp(union ibmvnic_crq *crq,
4656 struct ibmvnic_adapter *adapter)
4657{
4658 struct net_device *netdev = adapter->netdev;
4659 struct device *dev = &adapter->vdev->dev;
4660 long rc;
4661
Thomas Falcon901e0402017-02-15 12:17:59 -06004662 atomic_dec(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004663 netdev_dbg(netdev, "Outstanding queries: %d\n",
Thomas Falcon901e0402017-02-15 12:17:59 -06004664 atomic_read(&adapter->running_cap_crqs));
Thomas Falcon032c5e82015-12-21 11:26:06 -06004665 rc = crq->query_capability.rc.code;
4666 if (rc) {
4667 dev_err(dev, "Error %ld in QUERY_CAP_RSP\n", rc);
4668 goto out;
4669 }
4670
4671 switch (be16_to_cpu(crq->query_capability.capability)) {
4672 case MIN_TX_QUEUES:
4673 adapter->min_tx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004674 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004675 netdev_dbg(netdev, "min_tx_queues = %lld\n",
4676 adapter->min_tx_queues);
4677 break;
4678 case MIN_RX_QUEUES:
4679 adapter->min_rx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004680 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004681 netdev_dbg(netdev, "min_rx_queues = %lld\n",
4682 adapter->min_rx_queues);
4683 break;
4684 case MIN_RX_ADD_QUEUES:
4685 adapter->min_rx_add_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004686 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004687 netdev_dbg(netdev, "min_rx_add_queues = %lld\n",
4688 adapter->min_rx_add_queues);
4689 break;
4690 case MAX_TX_QUEUES:
4691 adapter->max_tx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004692 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004693 netdev_dbg(netdev, "max_tx_queues = %lld\n",
4694 adapter->max_tx_queues);
4695 break;
4696 case MAX_RX_QUEUES:
4697 adapter->max_rx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004698 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004699 netdev_dbg(netdev, "max_rx_queues = %lld\n",
4700 adapter->max_rx_queues);
4701 break;
4702 case MAX_RX_ADD_QUEUES:
4703 adapter->max_rx_add_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004704 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004705 netdev_dbg(netdev, "max_rx_add_queues = %lld\n",
4706 adapter->max_rx_add_queues);
4707 break;
4708 case MIN_TX_ENTRIES_PER_SUBCRQ:
4709 adapter->min_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06004710 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004711 netdev_dbg(netdev, "min_tx_entries_per_subcrq = %lld\n",
4712 adapter->min_tx_entries_per_subcrq);
4713 break;
4714 case MIN_RX_ADD_ENTRIES_PER_SUBCRQ:
4715 adapter->min_rx_add_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06004716 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004717 netdev_dbg(netdev, "min_rx_add_entrs_per_subcrq = %lld\n",
4718 adapter->min_rx_add_entries_per_subcrq);
4719 break;
4720 case MAX_TX_ENTRIES_PER_SUBCRQ:
4721 adapter->max_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06004722 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004723 netdev_dbg(netdev, "max_tx_entries_per_subcrq = %lld\n",
4724 adapter->max_tx_entries_per_subcrq);
4725 break;
4726 case MAX_RX_ADD_ENTRIES_PER_SUBCRQ:
4727 adapter->max_rx_add_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06004728 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004729 netdev_dbg(netdev, "max_rx_add_entrs_per_subcrq = %lld\n",
4730 adapter->max_rx_add_entries_per_subcrq);
4731 break;
4732 case TCP_IP_OFFLOAD:
4733 adapter->tcp_ip_offload =
Thomas Falconde89e852016-03-01 10:20:09 -06004734 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004735 netdev_dbg(netdev, "tcp_ip_offload = %lld\n",
4736 adapter->tcp_ip_offload);
4737 break;
4738 case PROMISC_SUPPORTED:
4739 adapter->promisc_supported =
Thomas Falconde89e852016-03-01 10:20:09 -06004740 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004741 netdev_dbg(netdev, "promisc_supported = %lld\n",
4742 adapter->promisc_supported);
4743 break;
4744 case MIN_MTU:
Thomas Falconde89e852016-03-01 10:20:09 -06004745 adapter->min_mtu = be64_to_cpu(crq->query_capability.number);
Thomas Falconf39f0d12017-02-14 10:22:59 -06004746 netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004747 netdev_dbg(netdev, "min_mtu = %lld\n", adapter->min_mtu);
4748 break;
4749 case MAX_MTU:
Thomas Falconde89e852016-03-01 10:20:09 -06004750 adapter->max_mtu = be64_to_cpu(crq->query_capability.number);
Thomas Falconf39f0d12017-02-14 10:22:59 -06004751 netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004752 netdev_dbg(netdev, "max_mtu = %lld\n", adapter->max_mtu);
4753 break;
4754 case MAX_MULTICAST_FILTERS:
4755 adapter->max_multicast_filters =
Thomas Falconde89e852016-03-01 10:20:09 -06004756 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004757 netdev_dbg(netdev, "max_multicast_filters = %lld\n",
4758 adapter->max_multicast_filters);
4759 break;
4760 case VLAN_HEADER_INSERTION:
4761 adapter->vlan_header_insertion =
Thomas Falconde89e852016-03-01 10:20:09 -06004762 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004763 if (adapter->vlan_header_insertion)
4764 netdev->features |= NETIF_F_HW_VLAN_STAG_TX;
4765 netdev_dbg(netdev, "vlan_header_insertion = %lld\n",
4766 adapter->vlan_header_insertion);
4767 break;
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04004768 case RX_VLAN_HEADER_INSERTION:
4769 adapter->rx_vlan_header_insertion =
4770 be64_to_cpu(crq->query_capability.number);
4771 netdev_dbg(netdev, "rx_vlan_header_insertion = %lld\n",
4772 adapter->rx_vlan_header_insertion);
4773 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004774 case MAX_TX_SG_ENTRIES:
4775 adapter->max_tx_sg_entries =
Thomas Falconde89e852016-03-01 10:20:09 -06004776 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004777 netdev_dbg(netdev, "max_tx_sg_entries = %lld\n",
4778 adapter->max_tx_sg_entries);
4779 break;
4780 case RX_SG_SUPPORTED:
4781 adapter->rx_sg_supported =
Thomas Falconde89e852016-03-01 10:20:09 -06004782 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004783 netdev_dbg(netdev, "rx_sg_supported = %lld\n",
4784 adapter->rx_sg_supported);
4785 break;
4786 case OPT_TX_COMP_SUB_QUEUES:
4787 adapter->opt_tx_comp_sub_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004788 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004789 netdev_dbg(netdev, "opt_tx_comp_sub_queues = %lld\n",
4790 adapter->opt_tx_comp_sub_queues);
4791 break;
4792 case OPT_RX_COMP_QUEUES:
4793 adapter->opt_rx_comp_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004794 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004795 netdev_dbg(netdev, "opt_rx_comp_queues = %lld\n",
4796 adapter->opt_rx_comp_queues);
4797 break;
4798 case OPT_RX_BUFADD_Q_PER_RX_COMP_Q:
4799 adapter->opt_rx_bufadd_q_per_rx_comp_q =
Thomas Falconde89e852016-03-01 10:20:09 -06004800 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004801 netdev_dbg(netdev, "opt_rx_bufadd_q_per_rx_comp_q = %lld\n",
4802 adapter->opt_rx_bufadd_q_per_rx_comp_q);
4803 break;
4804 case OPT_TX_ENTRIES_PER_SUBCRQ:
4805 adapter->opt_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06004806 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004807 netdev_dbg(netdev, "opt_tx_entries_per_subcrq = %lld\n",
4808 adapter->opt_tx_entries_per_subcrq);
4809 break;
4810 case OPT_RXBA_ENTRIES_PER_SUBCRQ:
4811 adapter->opt_rxba_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06004812 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004813 netdev_dbg(netdev, "opt_rxba_entries_per_subcrq = %lld\n",
4814 adapter->opt_rxba_entries_per_subcrq);
4815 break;
4816 case TX_RX_DESC_REQ:
4817 adapter->tx_rx_desc_req = crq->query_capability.number;
4818 netdev_dbg(netdev, "tx_rx_desc_req = %llx\n",
4819 adapter->tx_rx_desc_req);
4820 break;
4821
4822 default:
4823 netdev_err(netdev, "Got invalid cap rsp %d\n",
4824 crq->query_capability.capability);
4825 }
4826
4827out:
Thomas Falcon249168a2017-02-15 12:18:00 -06004828 if (atomic_read(&adapter->running_cap_crqs) == 0) {
4829 adapter->wait_capability = false;
Lijun Pan09081b92020-09-27 20:13:27 -05004830 send_request_cap(adapter, 0);
Thomas Falcon249168a2017-02-15 12:18:00 -06004831 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06004832}
4833
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03004834static int send_query_phys_parms(struct ibmvnic_adapter *adapter)
4835{
4836 union ibmvnic_crq crq;
4837 int rc;
4838
4839 memset(&crq, 0, sizeof(crq));
4840 crq.query_phys_parms.first = IBMVNIC_CRQ_CMD;
4841 crq.query_phys_parms.cmd = QUERY_PHYS_PARMS;
Thomas Falconff25dcb2019-11-25 17:12:56 -06004842
4843 mutex_lock(&adapter->fw_lock);
4844 adapter->fw_done_rc = 0;
Thomas Falcon070eca92019-11-25 17:12:53 -06004845 reinit_completion(&adapter->fw_done);
Thomas Falconff25dcb2019-11-25 17:12:56 -06004846
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03004847 rc = ibmvnic_send_crq(adapter, &crq);
Thomas Falconff25dcb2019-11-25 17:12:56 -06004848 if (rc) {
4849 mutex_unlock(&adapter->fw_lock);
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03004850 return rc;
Thomas Falconff25dcb2019-11-25 17:12:56 -06004851 }
Thomas Falcon476d96c2019-11-25 17:12:55 -06004852
4853 rc = ibmvnic_wait_for_completion(adapter, &adapter->fw_done, 10000);
Thomas Falconff25dcb2019-11-25 17:12:56 -06004854 if (rc) {
4855 mutex_unlock(&adapter->fw_lock);
Thomas Falcon476d96c2019-11-25 17:12:55 -06004856 return rc;
Thomas Falconff25dcb2019-11-25 17:12:56 -06004857 }
Thomas Falcon476d96c2019-11-25 17:12:55 -06004858
Thomas Falconff25dcb2019-11-25 17:12:56 -06004859 mutex_unlock(&adapter->fw_lock);
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03004860 return adapter->fw_done_rc ? -EIO : 0;
4861}
4862
4863static int handle_query_phys_parms_rsp(union ibmvnic_crq *crq,
4864 struct ibmvnic_adapter *adapter)
4865{
4866 struct net_device *netdev = adapter->netdev;
4867 int rc;
Murilo Fossa Vicentinidd0f9d82019-09-16 11:50:37 -03004868 __be32 rspeed = cpu_to_be32(crq->query_phys_parms_rsp.speed);
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03004869
4870 rc = crq->query_phys_parms_rsp.rc.code;
4871 if (rc) {
4872 netdev_err(netdev, "Error %d in QUERY_PHYS_PARMS\n", rc);
4873 return rc;
4874 }
Murilo Fossa Vicentinidd0f9d82019-09-16 11:50:37 -03004875 switch (rspeed) {
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03004876 case IBMVNIC_10MBPS:
4877 adapter->speed = SPEED_10;
4878 break;
4879 case IBMVNIC_100MBPS:
4880 adapter->speed = SPEED_100;
4881 break;
4882 case IBMVNIC_1GBPS:
4883 adapter->speed = SPEED_1000;
4884 break;
Lijun Panb9cd7952020-09-27 19:06:25 -05004885 case IBMVNIC_10GBPS:
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03004886 adapter->speed = SPEED_10000;
4887 break;
4888 case IBMVNIC_25GBPS:
4889 adapter->speed = SPEED_25000;
4890 break;
4891 case IBMVNIC_40GBPS:
4892 adapter->speed = SPEED_40000;
4893 break;
4894 case IBMVNIC_50GBPS:
4895 adapter->speed = SPEED_50000;
4896 break;
4897 case IBMVNIC_100GBPS:
4898 adapter->speed = SPEED_100000;
4899 break;
Lijun Panb9cd7952020-09-27 19:06:25 -05004900 case IBMVNIC_200GBPS:
4901 adapter->speed = SPEED_200000;
4902 break;
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03004903 default:
Murilo Fossa Vicentinidd0f9d82019-09-16 11:50:37 -03004904 if (netif_carrier_ok(netdev))
4905 netdev_warn(netdev, "Unknown speed 0x%08x\n", rspeed);
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03004906 adapter->speed = SPEED_UNKNOWN;
4907 }
4908 if (crq->query_phys_parms_rsp.flags1 & IBMVNIC_FULL_DUPLEX)
4909 adapter->duplex = DUPLEX_FULL;
4910 else if (crq->query_phys_parms_rsp.flags1 & IBMVNIC_HALF_DUPLEX)
4911 adapter->duplex = DUPLEX_HALF;
4912 else
4913 adapter->duplex = DUPLEX_UNKNOWN;
4914
4915 return rc;
4916}
4917
Thomas Falcon032c5e82015-12-21 11:26:06 -06004918static void ibmvnic_handle_crq(union ibmvnic_crq *crq,
4919 struct ibmvnic_adapter *adapter)
4920{
4921 struct ibmvnic_generic_crq *gen_crq = &crq->generic;
4922 struct net_device *netdev = adapter->netdev;
4923 struct device *dev = &adapter->vdev->dev;
Murilo Fossa Vicentini993a82b2017-04-19 13:44:35 -04004924 u64 *u64_crq = (u64 *)crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004925 long rc;
4926
4927 netdev_dbg(netdev, "Handling CRQ: %016lx %016lx\n",
Lijun Pan429aa362021-02-11 00:43:18 -06004928 (unsigned long)cpu_to_be64(u64_crq[0]),
4929 (unsigned long)cpu_to_be64(u64_crq[1]));
Thomas Falcon032c5e82015-12-21 11:26:06 -06004930 switch (gen_crq->first) {
4931 case IBMVNIC_CRQ_INIT_RSP:
4932 switch (gen_crq->cmd) {
4933 case IBMVNIC_CRQ_INIT:
4934 dev_info(dev, "Partner initialized\n");
John Allen017892c12017-05-26 10:30:19 -04004935 adapter->from_passive_init = true;
Sukadev Bhattiprolu76cdc5c2020-11-25 18:04:29 -06004936 /* Discard any stale login responses from prev reset.
4937 * CHECK: should we clear even on INIT_COMPLETE?
4938 */
4939 adapter->login_pending = false;
4940
Thomas Falcon17c87052018-05-23 13:37:58 -05004941 if (!completion_done(&adapter->init_done)) {
4942 complete(&adapter->init_done);
4943 adapter->init_done_rc = -EIO;
4944 }
Sukadev Bhattiproluef66a1e2021-02-02 21:08:02 -08004945 rc = ibmvnic_reset(adapter, VNIC_RESET_FAILOVER);
4946 if (rc && rc != -EBUSY) {
4947 /* We were unable to schedule the failover
4948 * reset either because the adapter was still
4949 * probing (eg: during kexec) or we could not
4950 * allocate memory. Clear the failover_pending
4951 * flag since no one else will. We ignore
4952 * EBUSY because it means either FAILOVER reset
4953 * is already scheduled or the adapter is
4954 * being removed.
4955 */
4956 netdev_err(netdev,
4957 "Error %ld scheduling failover reset\n",
4958 rc);
4959 adapter->failover_pending = false;
4960 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06004961 break;
4962 case IBMVNIC_CRQ_INIT_COMPLETE:
4963 dev_info(dev, "Partner initialization complete\n");
Thomas Falcon51536982018-05-23 13:37:56 -05004964 adapter->crq.active = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004965 send_version_xchg(adapter);
4966 break;
4967 default:
4968 dev_err(dev, "Unknown crq cmd: %d\n", gen_crq->cmd);
4969 }
4970 return;
4971 case IBMVNIC_CRQ_XPORT_EVENT:
Nathan Fontenoted651a12017-05-03 14:04:38 -04004972 netif_carrier_off(netdev);
Thomas Falcon51536982018-05-23 13:37:56 -05004973 adapter->crq.active = false;
Thomas Falcon2147e3d2019-11-25 17:12:54 -06004974 /* terminate any thread waiting for a response
4975 * from the device
4976 */
4977 if (!completion_done(&adapter->fw_done)) {
4978 adapter->fw_done_rc = -EIO;
4979 complete(&adapter->fw_done);
4980 }
4981 if (!completion_done(&adapter->stats_done))
4982 complete(&adapter->stats_done);
Juliet Kim7ed5b312019-09-20 16:11:23 -04004983 if (test_bit(0, &adapter->resetting))
Thomas Falcon2770a792018-05-23 13:38:02 -05004984 adapter->force_reset_recovery = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004985 if (gen_crq->cmd == IBMVNIC_PARTITION_MIGRATED) {
Nathan Fontenoted651a12017-05-03 14:04:38 -04004986 dev_info(dev, "Migrated, re-enabling adapter\n");
4987 ibmvnic_reset(adapter, VNIC_RESET_MOBILITY);
Thomas Falcondfad09a2016-08-18 11:37:51 -05004988 } else if (gen_crq->cmd == IBMVNIC_DEVICE_FAILOVER) {
4989 dev_info(dev, "Backing device failover detected\n");
Thomas Falcon5a18e1e2018-04-06 18:37:05 -05004990 adapter->failover_pending = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004991 } else {
4992 /* The adapter lost the connection */
4993 dev_err(dev, "Virtual Adapter failed (rc=%d)\n",
4994 gen_crq->cmd);
Nathan Fontenoted651a12017-05-03 14:04:38 -04004995 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004996 }
4997 return;
4998 case IBMVNIC_CRQ_CMD_RSP:
4999 break;
5000 default:
5001 dev_err(dev, "Got an invalid msg type 0x%02x\n",
5002 gen_crq->first);
5003 return;
5004 }
5005
5006 switch (gen_crq->cmd) {
5007 case VERSION_EXCHANGE_RSP:
5008 rc = crq->version_exchange_rsp.rc.code;
5009 if (rc) {
5010 dev_err(dev, "Error %ld in VERSION_EXCHG_RSP\n", rc);
5011 break;
5012 }
Thomas Falcon78468892020-05-28 11:19:17 -05005013 ibmvnic_version =
Thomas Falcon032c5e82015-12-21 11:26:06 -06005014 be16_to_cpu(crq->version_exchange_rsp.version);
Thomas Falcon78468892020-05-28 11:19:17 -05005015 dev_info(dev, "Partner protocol version is %d\n",
5016 ibmvnic_version);
Lijun Pan491099a2020-09-27 20:13:26 -05005017 send_query_cap(adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005018 break;
5019 case QUERY_CAPABILITY_RSP:
5020 handle_query_cap_rsp(crq, adapter);
5021 break;
5022 case QUERY_MAP_RSP:
5023 handle_query_map_rsp(crq, adapter);
5024 break;
5025 case REQUEST_MAP_RSP:
Thomas Falconf3be0cb2017-06-21 14:53:01 -05005026 adapter->fw_done_rc = crq->request_map_rsp.rc.code;
5027 complete(&adapter->fw_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005028 break;
5029 case REQUEST_UNMAP_RSP:
5030 handle_request_unmap_rsp(crq, adapter);
5031 break;
5032 case REQUEST_CAPABILITY_RSP:
5033 handle_request_cap_rsp(crq, adapter);
5034 break;
5035 case LOGIN_RSP:
5036 netdev_dbg(netdev, "Got Login Response\n");
5037 handle_login_rsp(crq, adapter);
5038 break;
5039 case LOGICAL_LINK_STATE_RSP:
Nathan Fontenot53da09e2017-04-21 15:39:04 -04005040 netdev_dbg(netdev,
5041 "Got Logical Link State Response, state: %d rc: %d\n",
5042 crq->logical_link_state_rsp.link_state,
5043 crq->logical_link_state_rsp.rc.code);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005044 adapter->logical_link_state =
5045 crq->logical_link_state_rsp.link_state;
Nathan Fontenot53da09e2017-04-21 15:39:04 -04005046 adapter->init_done_rc = crq->logical_link_state_rsp.rc.code;
5047 complete(&adapter->init_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005048 break;
5049 case LINK_STATE_INDICATION:
5050 netdev_dbg(netdev, "Got Logical Link State Indication\n");
5051 adapter->phys_link_state =
5052 crq->link_state_indication.phys_link_state;
5053 adapter->logical_link_state =
5054 crq->link_state_indication.logical_link_state;
Thomas Falcon0655f992019-05-09 23:13:44 -05005055 if (adapter->phys_link_state && adapter->logical_link_state)
5056 netif_carrier_on(netdev);
5057 else
5058 netif_carrier_off(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005059 break;
5060 case CHANGE_MAC_ADDR_RSP:
5061 netdev_dbg(netdev, "Got MAC address change Response\n");
Thomas Falconf8136142018-01-29 13:45:05 -06005062 adapter->fw_done_rc = handle_change_mac_rsp(crq, adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005063 break;
5064 case ERROR_INDICATION:
5065 netdev_dbg(netdev, "Got Error Indication\n");
5066 handle_error_indication(crq, adapter);
5067 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005068 case REQUEST_STATISTICS_RSP:
5069 netdev_dbg(netdev, "Got Statistics Response\n");
5070 complete(&adapter->stats_done);
5071 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005072 case QUERY_IP_OFFLOAD_RSP:
5073 netdev_dbg(netdev, "Got Query IP offload Response\n");
5074 handle_query_ip_offload_rsp(adapter);
5075 break;
5076 case MULTICAST_CTRL_RSP:
5077 netdev_dbg(netdev, "Got multicast control Response\n");
5078 break;
5079 case CONTROL_IP_OFFLOAD_RSP:
5080 netdev_dbg(netdev, "Got Control IP offload Response\n");
5081 dma_unmap_single(dev, adapter->ip_offload_ctrl_tok,
5082 sizeof(adapter->ip_offload_ctrl),
5083 DMA_TO_DEVICE);
John Allenbd0b6722017-03-17 17:13:40 -05005084 complete(&adapter->init_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005085 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005086 case COLLECT_FW_TRACE_RSP:
5087 netdev_dbg(netdev, "Got Collect firmware trace Response\n");
5088 complete(&adapter->fw_done);
5089 break;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02005090 case GET_VPD_SIZE_RSP:
5091 handle_vpd_size_rsp(crq, adapter);
5092 break;
5093 case GET_VPD_RSP:
5094 handle_vpd_rsp(crq, adapter);
5095 break;
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03005096 case QUERY_PHYS_PARMS_RSP:
5097 adapter->fw_done_rc = handle_query_phys_parms_rsp(crq, adapter);
5098 complete(&adapter->fw_done);
5099 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005100 default:
5101 netdev_err(netdev, "Got an invalid cmd type 0x%02x\n",
5102 gen_crq->cmd);
5103 }
5104}
5105
5106static irqreturn_t ibmvnic_interrupt(int irq, void *instance)
5107{
5108 struct ibmvnic_adapter *adapter = instance;
Thomas Falcon6c267b32017-02-15 12:17:58 -06005109
Thomas Falcon6c267b32017-02-15 12:17:58 -06005110 tasklet_schedule(&adapter->tasklet);
Thomas Falcon6c267b32017-02-15 12:17:58 -06005111 return IRQ_HANDLED;
5112}
5113
Allen Paisaa7c3fe2020-09-14 12:59:29 +05305114static void ibmvnic_tasklet(struct tasklet_struct *t)
Thomas Falcon6c267b32017-02-15 12:17:58 -06005115{
Allen Paisaa7c3fe2020-09-14 12:59:29 +05305116 struct ibmvnic_adapter *adapter = from_tasklet(adapter, t, tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005117 struct ibmvnic_crq_queue *queue = &adapter->crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005118 union ibmvnic_crq *crq;
5119 unsigned long flags;
5120 bool done = false;
5121
5122 spin_lock_irqsave(&queue->lock, flags);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005123 while (!done) {
5124 /* Pull all the valid messages off the CRQ */
5125 while ((crq = ibmvnic_next_crq(adapter)) != NULL) {
Lijun Pane41aec72021-01-27 19:34:42 -06005126 /* This barrier makes sure ibmvnic_next_crq()'s
5127 * crq->generic.first & IBMVNIC_CRQ_CMD_RSP is loaded
5128 * before ibmvnic_handle_crq()'s
5129 * switch(gen_crq->first) and switch(gen_crq->cmd).
5130 */
5131 dma_rmb();
Thomas Falcon032c5e82015-12-21 11:26:06 -06005132 ibmvnic_handle_crq(crq, adapter);
5133 crq->generic.first = 0;
5134 }
Brian Kinged7ecbf2017-04-19 13:44:53 -04005135
5136 /* remain in tasklet until all
5137 * capabilities responses are received
5138 */
5139 if (!adapter->wait_capability)
5140 done = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005141 }
Thomas Falcon249168a2017-02-15 12:18:00 -06005142 /* if capabilities CRQ's were sent in this tasklet, the following
5143 * tasklet must wait until all responses are received
5144 */
5145 if (atomic_read(&adapter->running_cap_crqs) != 0)
5146 adapter->wait_capability = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005147 spin_unlock_irqrestore(&queue->lock, flags);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005148}
5149
5150static int ibmvnic_reenable_crq_queue(struct ibmvnic_adapter *adapter)
5151{
5152 struct vio_dev *vdev = adapter->vdev;
5153 int rc;
5154
5155 do {
5156 rc = plpar_hcall_norets(H_ENABLE_CRQ, vdev->unit_address);
5157 } while (rc == H_IN_PROGRESS || rc == H_BUSY || H_IS_LONG_BUSY(rc));
5158
5159 if (rc)
5160 dev_err(&vdev->dev, "Error enabling adapter (rc=%d)\n", rc);
5161
5162 return rc;
5163}
5164
5165static int ibmvnic_reset_crq(struct ibmvnic_adapter *adapter)
5166{
5167 struct ibmvnic_crq_queue *crq = &adapter->crq;
5168 struct device *dev = &adapter->vdev->dev;
5169 struct vio_dev *vdev = adapter->vdev;
5170 int rc;
5171
5172 /* Close the CRQ */
5173 do {
5174 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
5175 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
5176
5177 /* Clean out the queue */
Lijun Pan0e435be2020-11-23 13:35:46 -06005178 if (!crq->msgs)
5179 return -EINVAL;
5180
Thomas Falcon032c5e82015-12-21 11:26:06 -06005181 memset(crq->msgs, 0, PAGE_SIZE);
5182 crq->cur = 0;
Thomas Falcon51536982018-05-23 13:37:56 -05005183 crq->active = false;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005184
5185 /* And re-open it again */
5186 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
5187 crq->msg_token, PAGE_SIZE);
5188
5189 if (rc == H_CLOSED)
5190 /* Adapter is good, but other end is not ready */
5191 dev_warn(dev, "Partner adapter not ready\n");
5192 else if (rc != 0)
5193 dev_warn(dev, "Couldn't register crq (rc=%d)\n", rc);
5194
5195 return rc;
5196}
5197
Nathan Fontenotf9928872017-03-30 02:48:54 -04005198static void release_crq_queue(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06005199{
5200 struct ibmvnic_crq_queue *crq = &adapter->crq;
5201 struct vio_dev *vdev = adapter->vdev;
5202 long rc;
5203
Nathan Fontenotf9928872017-03-30 02:48:54 -04005204 if (!crq->msgs)
5205 return;
5206
Thomas Falcon032c5e82015-12-21 11:26:06 -06005207 netdev_dbg(adapter->netdev, "Releasing CRQ\n");
5208 free_irq(vdev->irq, adapter);
Thomas Falcon6c267b32017-02-15 12:17:58 -06005209 tasklet_kill(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005210 do {
5211 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
5212 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
5213
5214 dma_unmap_single(&vdev->dev, crq->msg_token, PAGE_SIZE,
5215 DMA_BIDIRECTIONAL);
5216 free_page((unsigned long)crq->msgs);
Nathan Fontenotf9928872017-03-30 02:48:54 -04005217 crq->msgs = NULL;
Thomas Falcon51536982018-05-23 13:37:56 -05005218 crq->active = false;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005219}
5220
Nathan Fontenotf9928872017-03-30 02:48:54 -04005221static int init_crq_queue(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06005222{
5223 struct ibmvnic_crq_queue *crq = &adapter->crq;
5224 struct device *dev = &adapter->vdev->dev;
5225 struct vio_dev *vdev = adapter->vdev;
5226 int rc, retrc = -ENOMEM;
5227
Nathan Fontenotf9928872017-03-30 02:48:54 -04005228 if (crq->msgs)
5229 return 0;
5230
Thomas Falcon032c5e82015-12-21 11:26:06 -06005231 crq->msgs = (union ibmvnic_crq *)get_zeroed_page(GFP_KERNEL);
5232 /* Should we allocate more than one page? */
5233
5234 if (!crq->msgs)
5235 return -ENOMEM;
5236
5237 crq->size = PAGE_SIZE / sizeof(*crq->msgs);
5238 crq->msg_token = dma_map_single(dev, crq->msgs, PAGE_SIZE,
5239 DMA_BIDIRECTIONAL);
5240 if (dma_mapping_error(dev, crq->msg_token))
5241 goto map_failed;
5242
5243 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
5244 crq->msg_token, PAGE_SIZE);
5245
5246 if (rc == H_RESOURCE)
5247 /* maybe kexecing and resource is busy. try a reset */
5248 rc = ibmvnic_reset_crq(adapter);
5249 retrc = rc;
5250
5251 if (rc == H_CLOSED) {
5252 dev_warn(dev, "Partner adapter not ready\n");
5253 } else if (rc) {
5254 dev_warn(dev, "Error %d opening adapter\n", rc);
5255 goto reg_crq_failed;
5256 }
5257
5258 retrc = 0;
5259
Allen Paisaa7c3fe2020-09-14 12:59:29 +05305260 tasklet_setup(&adapter->tasklet, (void *)ibmvnic_tasklet);
Thomas Falcon6c267b32017-02-15 12:17:58 -06005261
Thomas Falcon032c5e82015-12-21 11:26:06 -06005262 netdev_dbg(adapter->netdev, "registering irq 0x%x\n", vdev->irq);
Murilo Fossa Vicentinie56e2512019-04-25 11:02:33 -03005263 snprintf(crq->name, sizeof(crq->name), "ibmvnic-%x",
5264 adapter->vdev->unit_address);
5265 rc = request_irq(vdev->irq, ibmvnic_interrupt, 0, crq->name, adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005266 if (rc) {
5267 dev_err(dev, "Couldn't register irq 0x%x. rc=%d\n",
5268 vdev->irq, rc);
5269 goto req_irq_failed;
5270 }
5271
5272 rc = vio_enable_interrupts(vdev);
5273 if (rc) {
5274 dev_err(dev, "Error %d enabling interrupts\n", rc);
5275 goto req_irq_failed;
5276 }
5277
5278 crq->cur = 0;
5279 spin_lock_init(&crq->lock);
5280
5281 return retrc;
5282
5283req_irq_failed:
Thomas Falcon6c267b32017-02-15 12:17:58 -06005284 tasklet_kill(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005285 do {
5286 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
5287 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
5288reg_crq_failed:
5289 dma_unmap_single(dev, crq->msg_token, PAGE_SIZE, DMA_BIDIRECTIONAL);
5290map_failed:
5291 free_page((unsigned long)crq->msgs);
Nathan Fontenotf9928872017-03-30 02:48:54 -04005292 crq->msgs = NULL;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005293 return retrc;
5294}
5295
Lijun Pan635e4422020-08-19 17:52:26 -05005296static int ibmvnic_reset_init(struct ibmvnic_adapter *adapter, bool reset)
John Allenf6ef6402017-03-17 17:13:42 -05005297{
5298 struct device *dev = &adapter->vdev->dev;
Dany Madden98c41f02020-11-25 18:04:32 -06005299 unsigned long timeout = msecs_to_jiffies(20000);
Michal Suchanek6881b072021-03-02 20:47:47 +01005300 u64 old_num_rx_queues = adapter->req_rx_queues;
5301 u64 old_num_tx_queues = adapter->req_tx_queues;
John Allenf6ef6402017-03-17 17:13:42 -05005302 int rc;
5303
John Allen017892c12017-05-26 10:30:19 -04005304 adapter->from_passive_init = false;
5305
Michal Suchanek6881b072021-03-02 20:47:47 +01005306 if (reset)
Lijun Pan635e4422020-08-19 17:52:26 -05005307 reinit_completion(&adapter->init_done);
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06005308
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04005309 adapter->init_done_rc = 0;
Lijun Panfa68bfa2020-08-19 17:52:24 -05005310 rc = ibmvnic_send_crq_init(adapter);
5311 if (rc) {
5312 dev_err(dev, "Send crq init failed with error %d\n", rc);
5313 return rc;
5314 }
5315
John Allenf6ef6402017-03-17 17:13:42 -05005316 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
5317 dev_err(dev, "Initialization sequence timed out\n");
John Allen017892c12017-05-26 10:30:19 -04005318 return -1;
5319 }
5320
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04005321 if (adapter->init_done_rc) {
5322 release_crq_queue(adapter);
5323 return adapter->init_done_rc;
5324 }
5325
Lijun Pan785a2b12020-09-17 21:12:46 -05005326 if (adapter->from_passive_init) {
5327 adapter->state = VNIC_OPEN;
5328 adapter->from_passive_init = false;
5329 return -1;
5330 }
5331
Lijun Pan635e4422020-08-19 17:52:26 -05005332 if (reset &&
5333 test_bit(0, &adapter->resetting) && !adapter->wait_for_reset &&
Nathan Fontenot30f79622018-04-06 18:37:06 -05005334 adapter->reset_reason != VNIC_RESET_MOBILITY) {
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06005335 if (adapter->req_rx_queues != old_num_rx_queues ||
5336 adapter->req_tx_queues != old_num_tx_queues) {
5337 release_sub_crqs(adapter, 0);
5338 rc = init_sub_crqs(adapter);
5339 } else {
5340 rc = reset_sub_crq_queues(adapter);
5341 }
5342 } else {
Nathan Fontenot57a49432017-05-26 10:31:12 -04005343 rc = init_sub_crqs(adapter);
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06005344 }
5345
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04005346 if (rc) {
5347 dev_err(dev, "Initialization of sub crqs failed\n");
5348 release_crq_queue(adapter);
Thomas Falcon5df969c2017-06-28 19:55:54 -05005349 return rc;
5350 }
5351
5352 rc = init_sub_crq_irqs(adapter);
5353 if (rc) {
5354 dev_err(dev, "Failed to initialize sub crq irqs\n");
5355 release_crq_queue(adapter);
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04005356 }
5357
5358 return rc;
John Allenf6ef6402017-03-17 17:13:42 -05005359}
5360
Thomas Falcon40c9db82017-06-12 12:35:04 -05005361static struct device_attribute dev_attr_failover;
5362
Thomas Falcon032c5e82015-12-21 11:26:06 -06005363static int ibmvnic_probe(struct vio_dev *dev, const struct vio_device_id *id)
5364{
5365 struct ibmvnic_adapter *adapter;
5366 struct net_device *netdev;
5367 unsigned char *mac_addr_p;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005368 int rc;
5369
5370 dev_dbg(&dev->dev, "entering ibmvnic_probe for UA 0x%x\n",
5371 dev->unit_address);
5372
5373 mac_addr_p = (unsigned char *)vio_get_attribute(dev,
5374 VETH_MAC_ADDR, NULL);
5375 if (!mac_addr_p) {
5376 dev_err(&dev->dev,
5377 "(%s:%3.3d) ERROR: Can't find MAC_ADDR attribute\n",
5378 __FILE__, __LINE__);
5379 return 0;
5380 }
5381
5382 netdev = alloc_etherdev_mq(sizeof(struct ibmvnic_adapter),
Thomas Falcond45cc3a2017-12-18 12:52:11 -06005383 IBMVNIC_MAX_QUEUES);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005384 if (!netdev)
5385 return -ENOMEM;
5386
5387 adapter = netdev_priv(netdev);
Nathan Fontenot90c80142017-05-03 14:04:32 -04005388 adapter->state = VNIC_PROBING;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005389 dev_set_drvdata(&dev->dev, netdev);
5390 adapter->vdev = dev;
5391 adapter->netdev = netdev;
Sukadev Bhattiprolu76cdc5c2020-11-25 18:04:29 -06005392 adapter->login_pending = false;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005393
5394 ether_addr_copy(adapter->mac_addr, mac_addr_p);
5395 ether_addr_copy(netdev->dev_addr, adapter->mac_addr);
5396 netdev->irq = dev->irq;
5397 netdev->netdev_ops = &ibmvnic_netdev_ops;
5398 netdev->ethtool_ops = &ibmvnic_ethtool_ops;
5399 SET_NETDEV_DEV(netdev, &dev->dev);
5400
Nathan Fontenoted651a12017-05-03 14:04:38 -04005401 INIT_WORK(&adapter->ibmvnic_reset, __ibmvnic_reset);
Juliet Kim7ed5b312019-09-20 16:11:23 -04005402 INIT_DELAYED_WORK(&adapter->ibmvnic_delayed_reset,
5403 __ibmvnic_delayed_reset);
Nathan Fontenoted651a12017-05-03 14:04:38 -04005404 INIT_LIST_HEAD(&adapter->rwi_list);
Thomas Falcon6c5c7482018-12-10 15:22:22 -06005405 spin_lock_init(&adapter->rwi_lock);
Juliet Kim7d7195a2020-03-10 09:23:58 -05005406 spin_lock_init(&adapter->state_lock);
Thomas Falconff25dcb2019-11-25 17:12:56 -06005407 mutex_init(&adapter->fw_lock);
Thomas Falconbbd669a2019-04-04 18:58:26 -05005408 init_completion(&adapter->init_done);
Thomas Falcon070eca92019-11-25 17:12:53 -06005409 init_completion(&adapter->fw_done);
5410 init_completion(&adapter->reset_done);
5411 init_completion(&adapter->stats_done);
Juliet Kim7ed5b312019-09-20 16:11:23 -04005412 clear_bit(0, &adapter->resetting);
Nathan Fontenoted651a12017-05-03 14:04:38 -04005413
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04005414 do {
Nathan Fontenot30f79622018-04-06 18:37:06 -05005415 rc = init_crq_queue(adapter);
5416 if (rc) {
5417 dev_err(&dev->dev, "Couldn't initialize crq. rc=%d\n",
5418 rc);
5419 goto ibmvnic_init_fail;
5420 }
5421
Lijun Pan635e4422020-08-19 17:52:26 -05005422 rc = ibmvnic_reset_init(adapter, false);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05005423 if (rc && rc != EAGAIN)
5424 goto ibmvnic_init_fail;
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04005425 } while (rc == EAGAIN);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005426
Thomas Falcon07184212018-05-16 15:49:05 -05005427 rc = init_stats_buffers(adapter);
5428 if (rc)
5429 goto ibmvnic_init_fail;
5430
5431 rc = init_stats_token(adapter);
5432 if (rc)
5433 goto ibmvnic_stats_fail;
5434
Thomas Falconf39f0d12017-02-14 10:22:59 -06005435 netdev->mtu = adapter->req_mtu - ETH_HLEN;
John Allenc26eba02017-10-26 16:23:25 -05005436 netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
5437 netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005438
Thomas Falcon40c9db82017-06-12 12:35:04 -05005439 rc = device_create_file(&dev->dev, &dev_attr_failover);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05005440 if (rc)
Thomas Falcon07184212018-05-16 15:49:05 -05005441 goto ibmvnic_dev_file_err;
Thomas Falcon40c9db82017-06-12 12:35:04 -05005442
Mick Tarsele876a8a2017-09-28 13:53:18 -07005443 netif_carrier_off(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005444 rc = register_netdev(netdev);
5445 if (rc) {
5446 dev_err(&dev->dev, "failed to register netdev rc=%d\n", rc);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05005447 goto ibmvnic_register_fail;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005448 }
5449 dev_info(&dev->dev, "ibmvnic registered\n");
5450
Nathan Fontenot90c80142017-05-03 14:04:32 -04005451 adapter->state = VNIC_PROBED;
John Allenc26eba02017-10-26 16:23:25 -05005452
5453 adapter->wait_for_reset = false;
Dany Maddena86d5c62020-11-25 18:04:31 -06005454 adapter->last_reset_time = jiffies;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005455 return 0;
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05005456
5457ibmvnic_register_fail:
5458 device_remove_file(&dev->dev, &dev_attr_failover);
5459
Thomas Falcon07184212018-05-16 15:49:05 -05005460ibmvnic_dev_file_err:
5461 release_stats_token(adapter);
5462
5463ibmvnic_stats_fail:
5464 release_stats_buffers(adapter);
5465
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05005466ibmvnic_init_fail:
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06005467 release_sub_crqs(adapter, 1);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05005468 release_crq_queue(adapter);
Thomas Falconff25dcb2019-11-25 17:12:56 -06005469 mutex_destroy(&adapter->fw_lock);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05005470 free_netdev(netdev);
5471
5472 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005473}
5474
Uwe Kleine-König386a9662021-02-25 23:18:34 +01005475static void ibmvnic_remove(struct vio_dev *dev)
Thomas Falcon032c5e82015-12-21 11:26:06 -06005476{
5477 struct net_device *netdev = dev_get_drvdata(&dev->dev);
Nathan Fontenot37489052017-04-19 13:45:04 -04005478 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Juliet Kim7d7195a2020-03-10 09:23:58 -05005479 unsigned long flags;
5480
5481 spin_lock_irqsave(&adapter->state_lock, flags);
Sukadev Bhattiprolu4a41c422021-02-12 20:42:50 -08005482
5483 /* If ibmvnic_reset() is scheduling a reset, wait for it to
5484 * finish. Then, set the state to REMOVING to prevent it from
5485 * scheduling any more work and to have reset functions ignore
5486 * any resets that have already been scheduled. Drop the lock
5487 * after setting state, so __ibmvnic_reset() which is called
5488 * from the flush_work() below, can make progress.
5489 */
Junlin Yang69cdb792021-03-05 16:48:39 +08005490 spin_lock(&adapter->rwi_lock);
Nathan Fontenot90c80142017-05-03 14:04:32 -04005491 adapter->state = VNIC_REMOVING;
Junlin Yang69cdb792021-03-05 16:48:39 +08005492 spin_unlock(&adapter->rwi_lock);
Sukadev Bhattiprolu4a41c422021-02-12 20:42:50 -08005493
Juliet Kim7d7195a2020-03-10 09:23:58 -05005494 spin_unlock_irqrestore(&adapter->state_lock, flags);
5495
Thomas Falcon6954a9e2020-06-12 13:34:41 -05005496 flush_work(&adapter->ibmvnic_reset);
5497 flush_delayed_work(&adapter->ibmvnic_delayed_reset);
5498
Juliet Kima5681e22018-11-19 15:59:22 -06005499 rtnl_lock();
5500 unregister_netdevice(netdev);
Nathan Fontenot37489052017-04-19 13:45:04 -04005501
5502 release_resources(adapter);
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06005503 release_sub_crqs(adapter, 1);
Nathan Fontenot37489052017-04-19 13:45:04 -04005504 release_crq_queue(adapter);
5505
Thomas Falcon53cc7722018-02-26 18:10:56 -06005506 release_stats_token(adapter);
5507 release_stats_buffers(adapter);
5508
Nathan Fontenot90c80142017-05-03 14:04:32 -04005509 adapter->state = VNIC_REMOVED;
5510
Juliet Kima5681e22018-11-19 15:59:22 -06005511 rtnl_unlock();
Thomas Falconff25dcb2019-11-25 17:12:56 -06005512 mutex_destroy(&adapter->fw_lock);
Thomas Falcon40c9db82017-06-12 12:35:04 -05005513 device_remove_file(&dev->dev, &dev_attr_failover);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005514 free_netdev(netdev);
5515 dev_set_drvdata(&dev->dev, NULL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005516}
5517
Thomas Falcon40c9db82017-06-12 12:35:04 -05005518static ssize_t failover_store(struct device *dev, struct device_attribute *attr,
5519 const char *buf, size_t count)
5520{
5521 struct net_device *netdev = dev_get_drvdata(dev);
5522 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
5523 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
5524 __be64 session_token;
5525 long rc;
5526
5527 if (!sysfs_streq(buf, "1"))
5528 return -EINVAL;
5529
5530 rc = plpar_hcall(H_VIOCTL, retbuf, adapter->vdev->unit_address,
5531 H_GET_SESSION_TOKEN, 0, 0, 0);
5532 if (rc) {
5533 netdev_err(netdev, "Couldn't retrieve session token, rc %ld\n",
5534 rc);
Lijun Pan334c4242021-04-13 03:31:44 -05005535 goto last_resort;
Thomas Falcon40c9db82017-06-12 12:35:04 -05005536 }
5537
5538 session_token = (__be64)retbuf[0];
5539 netdev_dbg(netdev, "Initiating client failover, session id %llx\n",
5540 be64_to_cpu(session_token));
5541 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
5542 H_SESSION_ERR_DETECTED, session_token, 0, 0);
Lijun Pan334c4242021-04-13 03:31:44 -05005543 if (rc)
5544 netdev_err(netdev,
5545 "H_VIOCTL initiated failover failed, rc %ld\n",
Thomas Falcon40c9db82017-06-12 12:35:04 -05005546 rc);
Lijun Pan334c4242021-04-13 03:31:44 -05005547
5548last_resort:
5549 netdev_dbg(netdev, "Trying to send CRQ_CMD, the last resort\n");
5550 ibmvnic_reset(adapter, VNIC_RESET_FAILOVER);
Thomas Falcon40c9db82017-06-12 12:35:04 -05005551
5552 return count;
5553}
Joe Perches6cbaefb2017-12-19 10:15:09 -08005554static DEVICE_ATTR_WO(failover);
Thomas Falcon40c9db82017-06-12 12:35:04 -05005555
Thomas Falcon032c5e82015-12-21 11:26:06 -06005556static unsigned long ibmvnic_get_desired_dma(struct vio_dev *vdev)
5557{
5558 struct net_device *netdev = dev_get_drvdata(&vdev->dev);
5559 struct ibmvnic_adapter *adapter;
5560 struct iommu_table *tbl;
5561 unsigned long ret = 0;
5562 int i;
5563
5564 tbl = get_iommu_table_base(&vdev->dev);
5565
5566 /* netdev inits at probe time along with the structures we need below*/
5567 if (!netdev)
5568 return IOMMU_PAGE_ALIGN(IBMVNIC_IO_ENTITLEMENT_DEFAULT, tbl);
5569
5570 adapter = netdev_priv(netdev);
5571
5572 ret += PAGE_SIZE; /* the crq message queue */
Thomas Falcon032c5e82015-12-21 11:26:06 -06005573 ret += IOMMU_PAGE_ALIGN(sizeof(struct ibmvnic_statistics), tbl);
5574
5575 for (i = 0; i < adapter->req_tx_queues + adapter->req_rx_queues; i++)
5576 ret += 4 * PAGE_SIZE; /* the scrq message queue */
5577
Thomas Falcon507ebe62020-08-21 13:39:01 -05005578 for (i = 0; i < adapter->num_active_rx_pools; i++)
Thomas Falcon032c5e82015-12-21 11:26:06 -06005579 ret += adapter->rx_pool[i].size *
5580 IOMMU_PAGE_ALIGN(adapter->rx_pool[i].buff_size, tbl);
5581
5582 return ret;
5583}
5584
5585static int ibmvnic_resume(struct device *dev)
5586{
5587 struct net_device *netdev = dev_get_drvdata(dev);
5588 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005589
John Allencb89ba22017-06-19 11:27:53 -05005590 if (adapter->state != VNIC_OPEN)
5591 return 0;
5592
John Allena2488782017-07-24 13:26:06 -05005593 tasklet_schedule(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005594
5595 return 0;
5596}
5597
Arvind Yadav8c37bc62017-08-17 18:52:54 +05305598static const struct vio_device_id ibmvnic_device_table[] = {
Thomas Falcon032c5e82015-12-21 11:26:06 -06005599 {"network", "IBM,vnic"},
5600 {"", "" }
5601};
5602MODULE_DEVICE_TABLE(vio, ibmvnic_device_table);
5603
5604static const struct dev_pm_ops ibmvnic_pm_ops = {
5605 .resume = ibmvnic_resume
5606};
5607
5608static struct vio_driver ibmvnic_driver = {
5609 .id_table = ibmvnic_device_table,
5610 .probe = ibmvnic_probe,
5611 .remove = ibmvnic_remove,
5612 .get_desired_dma = ibmvnic_get_desired_dma,
5613 .name = ibmvnic_driver_name,
5614 .pm = &ibmvnic_pm_ops,
5615};
5616
5617/* module functions */
5618static int __init ibmvnic_module_init(void)
5619{
5620 pr_info("%s: %s %s\n", ibmvnic_driver_name, ibmvnic_driver_string,
5621 IBMVNIC_DRIVER_VERSION);
5622
5623 return vio_register_driver(&ibmvnic_driver);
5624}
5625
5626static void __exit ibmvnic_module_exit(void)
5627{
5628 vio_unregister_driver(&ibmvnic_driver);
5629}
5630
5631module_init(ibmvnic_module_init);
5632module_exit(ibmvnic_module_exit);