blob: 5c89dd7fa3decf28d3d410339475d1de0a44dd20 [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
Lijun Pan1c7d45e2021-02-12 20:36:10 -0600260static int reset_long_term_buff(struct ibmvnic_long_term_buff *ltb)
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500261{
Lijun Pan1c7d45e2021-02-12 20:36:10 -0600262 if (!ltb->buff)
263 return -EINVAL;
Thomas Falcon9c4eaab2018-05-23 13:37:57 -0500264
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500265 memset(ltb->buff, 0, ltb->size);
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500266 return 0;
267}
268
Thomas Falconf185a492017-05-26 10:30:48 -0400269static void deactivate_rx_pools(struct ibmvnic_adapter *adapter)
270{
271 int i;
272
Thomas Falcon507ebe62020-08-21 13:39:01 -0500273 for (i = 0; i < adapter->num_active_rx_pools; i++)
Thomas Falconf185a492017-05-26 10:30:48 -0400274 adapter->rx_pool[i].active = 0;
275}
276
Thomas Falcon032c5e82015-12-21 11:26:06 -0600277static void replenish_rx_pool(struct ibmvnic_adapter *adapter,
278 struct ibmvnic_rx_pool *pool)
279{
280 int count = pool->size - atomic_read(&pool->available);
Cristobal Fornof3ae59c2020-08-19 13:16:23 -0500281 u64 handle = adapter->rx_scrq[pool->index]->handle;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600282 struct device *dev = &adapter->vdev->dev;
Thomas Falcon4f0b6812020-11-18 19:12:18 -0600283 struct ibmvnic_ind_xmit_queue *ind_bufp;
284 struct ibmvnic_sub_crq_queue *rx_scrq;
285 union sub_crq *sub_crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600286 int buffers_added = 0;
287 unsigned long lpar_rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600288 struct sk_buff *skb;
289 unsigned int offset;
290 dma_addr_t dma_addr;
291 unsigned char *dst;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600292 int shift = 0;
293 int index;
294 int i;
295
Thomas Falconf185a492017-05-26 10:30:48 -0400296 if (!pool->active)
297 return;
298
Thomas Falcon4f0b6812020-11-18 19:12:18 -0600299 rx_scrq = adapter->rx_scrq[pool->index];
300 ind_bufp = &rx_scrq->ind_buf;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600301 for (i = 0; i < count; ++i) {
Dwip N. Banerjeee552aa32020-11-18 19:12:24 -0600302 skb = netdev_alloc_skb(adapter->netdev, pool->buff_size);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600303 if (!skb) {
304 dev_err(dev, "Couldn't replenish rx buff\n");
305 adapter->replenish_no_mem++;
306 break;
307 }
308
309 index = pool->free_map[pool->next_free];
310
311 if (pool->rx_buff[index].skb)
312 dev_err(dev, "Inconsistent free_map!\n");
313
314 /* Copy the skb to the long term mapped DMA buffer */
315 offset = index * pool->buff_size;
316 dst = pool->long_term_buff.buff + offset;
317 memset(dst, 0, pool->buff_size);
318 dma_addr = pool->long_term_buff.addr + offset;
319 pool->rx_buff[index].data = dst;
320
321 pool->free_map[pool->next_free] = IBMVNIC_INVALID_MAP;
322 pool->rx_buff[index].dma = dma_addr;
323 pool->rx_buff[index].skb = skb;
324 pool->rx_buff[index].pool_index = pool->index;
325 pool->rx_buff[index].size = pool->buff_size;
326
Thomas Falcon4f0b6812020-11-18 19:12:18 -0600327 sub_crq = &ind_bufp->indir_arr[ind_bufp->index++];
328 memset(sub_crq, 0, sizeof(*sub_crq));
329 sub_crq->rx_add.first = IBMVNIC_CRQ_CMD;
330 sub_crq->rx_add.correlator =
Thomas Falcon032c5e82015-12-21 11:26:06 -0600331 cpu_to_be64((u64)&pool->rx_buff[index]);
Thomas Falcon4f0b6812020-11-18 19:12:18 -0600332 sub_crq->rx_add.ioba = cpu_to_be32(dma_addr);
333 sub_crq->rx_add.map_id = pool->long_term_buff.map_id;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600334
335 /* The length field of the sCRQ is defined to be 24 bits so the
336 * buffer size needs to be left shifted by a byte before it is
337 * converted to big endian to prevent the last byte from being
338 * truncated.
339 */
340#ifdef __LITTLE_ENDIAN__
341 shift = 8;
342#endif
Thomas Falcon4f0b6812020-11-18 19:12:18 -0600343 sub_crq->rx_add.len = cpu_to_be32(pool->buff_size << shift);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600344 pool->next_free = (pool->next_free + 1) % pool->size;
Thomas Falcon4f0b6812020-11-18 19:12:18 -0600345 if (ind_bufp->index == IBMVNIC_MAX_IND_DESCS ||
346 i == count - 1) {
347 lpar_rc =
348 send_subcrq_indirect(adapter, handle,
349 (u64)ind_bufp->indir_dma,
350 (u64)ind_bufp->index);
351 if (lpar_rc != H_SUCCESS)
352 goto failure;
353 buffers_added += ind_bufp->index;
354 adapter->replenish_add_buff_success += ind_bufp->index;
355 ind_bufp->index = 0;
356 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600357 }
358 atomic_add(buffers_added, &pool->available);
359 return;
360
361failure:
Thomas Falcon2d14d372018-07-13 12:03:32 -0500362 if (lpar_rc != H_PARAMETER && lpar_rc != H_CLOSED)
363 dev_err_ratelimited(dev, "rx: replenish packet buffer failed\n");
Thomas Falcon4f0b6812020-11-18 19:12:18 -0600364 for (i = ind_bufp->index - 1; i >= 0; --i) {
365 struct ibmvnic_rx_buff *rx_buff;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600366
Thomas Falcon4f0b6812020-11-18 19:12:18 -0600367 pool->next_free = pool->next_free == 0 ?
368 pool->size - 1 : pool->next_free - 1;
369 sub_crq = &ind_bufp->indir_arr[i];
370 rx_buff = (struct ibmvnic_rx_buff *)
371 be64_to_cpu(sub_crq->rx_add.correlator);
372 index = (int)(rx_buff - pool->rx_buff);
373 pool->free_map[pool->next_free] = index;
374 dev_kfree_skb_any(pool->rx_buff[index].skb);
375 pool->rx_buff[index].skb = NULL;
376 }
Dwip N. Banerjeec2af6222020-12-09 20:53:31 -0500377 adapter->replenish_add_buff_failure += ind_bufp->index;
378 atomic_add(buffers_added, &pool->available);
Thomas Falcon4f0b6812020-11-18 19:12:18 -0600379 ind_bufp->index = 0;
Thomas Falcon5a18e1e2018-04-06 18:37:05 -0500380 if (lpar_rc == H_CLOSED || adapter->failover_pending) {
Thomas Falconf185a492017-05-26 10:30:48 -0400381 /* Disable buffer pool replenishment and report carrier off if
Thomas Falcon5a18e1e2018-04-06 18:37:05 -0500382 * queue is closed or pending failover.
383 * Firmware guarantees that a signal will be sent to the
384 * driver, triggering a reset.
Thomas Falconf185a492017-05-26 10:30:48 -0400385 */
386 deactivate_rx_pools(adapter);
387 netif_carrier_off(adapter->netdev);
388 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600389}
390
391static void replenish_pools(struct ibmvnic_adapter *adapter)
392{
393 int i;
394
Thomas Falcon032c5e82015-12-21 11:26:06 -0600395 adapter->replenish_task_cycles++;
Thomas Falcon507ebe62020-08-21 13:39:01 -0500396 for (i = 0; i < adapter->num_active_rx_pools; i++) {
Thomas Falcon032c5e82015-12-21 11:26:06 -0600397 if (adapter->rx_pool[i].active)
398 replenish_rx_pool(adapter, &adapter->rx_pool[i]);
399 }
Sukadev Bhattiprolu38bd5ce2020-12-04 18:22:35 -0800400
401 netdev_dbg(adapter->netdev, "Replenished %d pools\n", i);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600402}
403
John Allen3d52b592017-08-02 16:44:14 -0500404static void release_stats_buffers(struct ibmvnic_adapter *adapter)
405{
406 kfree(adapter->tx_stats_buffers);
407 kfree(adapter->rx_stats_buffers);
Thomas Falconb0992ec2018-02-06 17:25:23 -0600408 adapter->tx_stats_buffers = NULL;
409 adapter->rx_stats_buffers = NULL;
John Allen3d52b592017-08-02 16:44:14 -0500410}
411
412static int init_stats_buffers(struct ibmvnic_adapter *adapter)
413{
414 adapter->tx_stats_buffers =
Nathan Fontenotabcae542018-02-19 13:30:47 -0600415 kcalloc(IBMVNIC_MAX_QUEUES,
John Allen3d52b592017-08-02 16:44:14 -0500416 sizeof(struct ibmvnic_tx_queue_stats),
417 GFP_KERNEL);
418 if (!adapter->tx_stats_buffers)
419 return -ENOMEM;
420
421 adapter->rx_stats_buffers =
Nathan Fontenotabcae542018-02-19 13:30:47 -0600422 kcalloc(IBMVNIC_MAX_QUEUES,
John Allen3d52b592017-08-02 16:44:14 -0500423 sizeof(struct ibmvnic_rx_queue_stats),
424 GFP_KERNEL);
425 if (!adapter->rx_stats_buffers)
426 return -ENOMEM;
427
428 return 0;
429}
430
Nathan Fontenot7bbc27a2017-03-30 02:49:23 -0400431static void release_stats_token(struct ibmvnic_adapter *adapter)
432{
433 struct device *dev = &adapter->vdev->dev;
434
435 if (!adapter->stats_token)
436 return;
437
438 dma_unmap_single(dev, adapter->stats_token,
439 sizeof(struct ibmvnic_statistics),
440 DMA_FROM_DEVICE);
441 adapter->stats_token = 0;
442}
443
444static int init_stats_token(struct ibmvnic_adapter *adapter)
445{
446 struct device *dev = &adapter->vdev->dev;
447 dma_addr_t stok;
448
449 stok = dma_map_single(dev, &adapter->stats,
450 sizeof(struct ibmvnic_statistics),
451 DMA_FROM_DEVICE);
452 if (dma_mapping_error(dev, stok)) {
453 dev_err(dev, "Couldn't map stats buffer\n");
454 return -1;
455 }
456
457 adapter->stats_token = stok;
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500458 netdev_dbg(adapter->netdev, "Stats token initialized (%llx)\n", stok);
Nathan Fontenot7bbc27a2017-03-30 02:49:23 -0400459 return 0;
460}
461
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400462static int reset_rx_pools(struct ibmvnic_adapter *adapter)
463{
464 struct ibmvnic_rx_pool *rx_pool;
Thomas Falcon507ebe62020-08-21 13:39:01 -0500465 u64 buff_size;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400466 int rx_scrqs;
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500467 int i, j, rc;
John Allen896d8692018-01-18 16:26:31 -0600468
Mingming Cao9f134572020-08-25 13:26:41 -0400469 if (!adapter->rx_pool)
470 return -1;
471
Thomas Falcon507ebe62020-08-21 13:39:01 -0500472 buff_size = adapter->cur_rx_buf_sz;
473 rx_scrqs = adapter->num_active_rx_pools;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400474 for (i = 0; i < rx_scrqs; i++) {
475 rx_pool = &adapter->rx_pool[i];
476
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500477 netdev_dbg(adapter->netdev, "Re-setting rx_pool[%d]\n", i);
478
Thomas Falcon507ebe62020-08-21 13:39:01 -0500479 if (rx_pool->buff_size != buff_size) {
John Allen896d8692018-01-18 16:26:31 -0600480 free_long_term_buff(adapter, &rx_pool->long_term_buff);
Dwip N. Banerjee9a87c3f2020-11-18 19:12:22 -0600481 rx_pool->buff_size = ALIGN(buff_size, L1_CACHE_BYTES);
Thomas Falcon7c940b12019-06-07 16:03:55 -0500482 rc = alloc_long_term_buff(adapter,
483 &rx_pool->long_term_buff,
484 rx_pool->size *
485 rx_pool->buff_size);
John Allen896d8692018-01-18 16:26:31 -0600486 } else {
Lijun Pan1c7d45e2021-02-12 20:36:10 -0600487 rc = reset_long_term_buff(&rx_pool->long_term_buff);
John Allen896d8692018-01-18 16:26:31 -0600488 }
489
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500490 if (rc)
491 return rc;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400492
493 for (j = 0; j < rx_pool->size; j++)
494 rx_pool->free_map[j] = j;
495
496 memset(rx_pool->rx_buff, 0,
497 rx_pool->size * sizeof(struct ibmvnic_rx_buff));
498
499 atomic_set(&rx_pool->available, 0);
500 rx_pool->next_alloc = 0;
501 rx_pool->next_free = 0;
Thomas Falconc3e53b92017-06-14 23:50:05 -0500502 rx_pool->active = 1;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400503 }
504
505 return 0;
506}
507
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400508static void release_rx_pools(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -0600509{
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400510 struct ibmvnic_rx_pool *rx_pool;
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400511 int i, j;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600512
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400513 if (!adapter->rx_pool)
Thomas Falcon032c5e82015-12-21 11:26:06 -0600514 return;
515
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600516 for (i = 0; i < adapter->num_active_rx_pools; i++) {
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400517 rx_pool = &adapter->rx_pool[i];
518
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500519 netdev_dbg(adapter->netdev, "Releasing rx_pool[%d]\n", i);
520
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400521 kfree(rx_pool->free_map);
522 free_long_term_buff(adapter, &rx_pool->long_term_buff);
523
524 if (!rx_pool->rx_buff)
Nathan Fontenote0ebe9422017-05-03 14:04:50 -0400525 continue;
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400526
527 for (j = 0; j < rx_pool->size; j++) {
528 if (rx_pool->rx_buff[j].skb) {
Thomas Falconb7cdec32018-11-21 11:17:58 -0600529 dev_kfree_skb_any(rx_pool->rx_buff[j].skb);
530 rx_pool->rx_buff[j].skb = NULL;
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400531 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600532 }
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400533
534 kfree(rx_pool->rx_buff);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600535 }
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400536
537 kfree(adapter->rx_pool);
538 adapter->rx_pool = NULL;
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600539 adapter->num_active_rx_pools = 0;
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400540}
541
542static int init_rx_pools(struct net_device *netdev)
543{
544 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
545 struct device *dev = &adapter->vdev->dev;
546 struct ibmvnic_rx_pool *rx_pool;
547 int rxadd_subcrqs;
Thomas Falcon507ebe62020-08-21 13:39:01 -0500548 u64 buff_size;
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400549 int i, j;
550
Thomas Falcon507ebe62020-08-21 13:39:01 -0500551 rxadd_subcrqs = adapter->num_active_rx_scrqs;
552 buff_size = adapter->cur_rx_buf_sz;
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400553
554 adapter->rx_pool = kcalloc(rxadd_subcrqs,
555 sizeof(struct ibmvnic_rx_pool),
556 GFP_KERNEL);
557 if (!adapter->rx_pool) {
558 dev_err(dev, "Failed to allocate rx pools\n");
559 return -1;
560 }
561
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600562 adapter->num_active_rx_pools = rxadd_subcrqs;
563
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400564 for (i = 0; i < rxadd_subcrqs; i++) {
565 rx_pool = &adapter->rx_pool[i];
566
567 netdev_dbg(adapter->netdev,
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500568 "Initializing rx_pool[%d], %lld buffs, %lld bytes each\n",
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400569 i, adapter->req_rx_add_entries_per_subcrq,
Thomas Falcon507ebe62020-08-21 13:39:01 -0500570 buff_size);
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400571
572 rx_pool->size = adapter->req_rx_add_entries_per_subcrq;
573 rx_pool->index = i;
Dwip N. Banerjee9a87c3f2020-11-18 19:12:22 -0600574 rx_pool->buff_size = ALIGN(buff_size, L1_CACHE_BYTES);
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400575 rx_pool->active = 1;
576
577 rx_pool->free_map = kcalloc(rx_pool->size, sizeof(int),
578 GFP_KERNEL);
579 if (!rx_pool->free_map) {
580 release_rx_pools(adapter);
581 return -1;
582 }
583
584 rx_pool->rx_buff = kcalloc(rx_pool->size,
585 sizeof(struct ibmvnic_rx_buff),
586 GFP_KERNEL);
587 if (!rx_pool->rx_buff) {
588 dev_err(dev, "Couldn't alloc rx buffers\n");
589 release_rx_pools(adapter);
590 return -1;
591 }
592
593 if (alloc_long_term_buff(adapter, &rx_pool->long_term_buff,
594 rx_pool->size * rx_pool->buff_size)) {
595 release_rx_pools(adapter);
596 return -1;
597 }
598
599 for (j = 0; j < rx_pool->size; ++j)
600 rx_pool->free_map[j] = j;
601
602 atomic_set(&rx_pool->available, 0);
603 rx_pool->next_alloc = 0;
604 rx_pool->next_free = 0;
605 }
606
607 return 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600608}
609
Lijun Pan1c7d45e2021-02-12 20:36:10 -0600610static int reset_one_tx_pool(struct ibmvnic_tx_pool *tx_pool)
Thomas Falcone26dc252018-03-16 20:00:25 -0500611{
612 int rc, i;
613
Lijun Pan1c7d45e2021-02-12 20:36:10 -0600614 rc = reset_long_term_buff(&tx_pool->long_term_buff);
Thomas Falcone26dc252018-03-16 20:00:25 -0500615 if (rc)
616 return rc;
617
618 memset(tx_pool->tx_buff, 0,
619 tx_pool->num_buffers *
620 sizeof(struct ibmvnic_tx_buff));
621
622 for (i = 0; i < tx_pool->num_buffers; i++)
623 tx_pool->free_map[i] = i;
624
625 tx_pool->consumer_index = 0;
626 tx_pool->producer_index = 0;
627
628 return 0;
629}
630
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400631static int reset_tx_pools(struct ibmvnic_adapter *adapter)
632{
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400633 int tx_scrqs;
Thomas Falcone26dc252018-03-16 20:00:25 -0500634 int i, rc;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400635
Mingming Cao9f134572020-08-25 13:26:41 -0400636 if (!adapter->tx_pool)
637 return -1;
638
Thomas Falcon507ebe62020-08-21 13:39:01 -0500639 tx_scrqs = adapter->num_active_tx_pools;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400640 for (i = 0; i < tx_scrqs; i++) {
Lijun Pan1c7d45e2021-02-12 20:36:10 -0600641 rc = reset_one_tx_pool(&adapter->tso_pool[i]);
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500642 if (rc)
643 return rc;
Lijun Pan1c7d45e2021-02-12 20:36:10 -0600644 rc = reset_one_tx_pool(&adapter->tx_pool[i]);
Thomas Falconfdb06102017-10-17 12:36:55 -0500645 if (rc)
646 return rc;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400647 }
648
649 return 0;
650}
651
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -0200652static void release_vpd_data(struct ibmvnic_adapter *adapter)
653{
654 if (!adapter->vpd)
655 return;
656
657 kfree(adapter->vpd->buff);
658 kfree(adapter->vpd);
Thomas Falconb0992ec2018-02-06 17:25:23 -0600659
660 adapter->vpd = NULL;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -0200661}
662
Thomas Falconfb794212018-03-16 20:00:26 -0500663static void release_one_tx_pool(struct ibmvnic_adapter *adapter,
664 struct ibmvnic_tx_pool *tx_pool)
665{
666 kfree(tx_pool->tx_buff);
667 kfree(tx_pool->free_map);
668 free_long_term_buff(adapter, &tx_pool->long_term_buff);
669}
670
Nathan Fontenotc657e322017-03-30 02:49:06 -0400671static void release_tx_pools(struct ibmvnic_adapter *adapter)
672{
John Allen896d8692018-01-18 16:26:31 -0600673 int i;
Nathan Fontenotc657e322017-03-30 02:49:06 -0400674
675 if (!adapter->tx_pool)
676 return;
677
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600678 for (i = 0; i < adapter->num_active_tx_pools; i++) {
Thomas Falconfb794212018-03-16 20:00:26 -0500679 release_one_tx_pool(adapter, &adapter->tx_pool[i]);
680 release_one_tx_pool(adapter, &adapter->tso_pool[i]);
Nathan Fontenotc657e322017-03-30 02:49:06 -0400681 }
682
683 kfree(adapter->tx_pool);
684 adapter->tx_pool = NULL;
Thomas Falconfb794212018-03-16 20:00:26 -0500685 kfree(adapter->tso_pool);
686 adapter->tso_pool = NULL;
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600687 adapter->num_active_tx_pools = 0;
Nathan Fontenotc657e322017-03-30 02:49:06 -0400688}
689
Thomas Falcon32053062018-03-16 20:00:27 -0500690static int init_one_tx_pool(struct net_device *netdev,
691 struct ibmvnic_tx_pool *tx_pool,
692 int num_entries, int buf_size)
693{
694 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
695 int i;
696
697 tx_pool->tx_buff = kcalloc(num_entries,
698 sizeof(struct ibmvnic_tx_buff),
699 GFP_KERNEL);
700 if (!tx_pool->tx_buff)
701 return -1;
702
703 if (alloc_long_term_buff(adapter, &tx_pool->long_term_buff,
704 num_entries * buf_size))
705 return -1;
706
707 tx_pool->free_map = kcalloc(num_entries, sizeof(int), GFP_KERNEL);
708 if (!tx_pool->free_map)
709 return -1;
710
711 for (i = 0; i < num_entries; i++)
712 tx_pool->free_map[i] = i;
713
714 tx_pool->consumer_index = 0;
715 tx_pool->producer_index = 0;
716 tx_pool->num_buffers = num_entries;
717 tx_pool->buf_size = buf_size;
718
719 return 0;
720}
721
Nathan Fontenotc657e322017-03-30 02:49:06 -0400722static int init_tx_pools(struct net_device *netdev)
723{
724 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Nathan Fontenotc657e322017-03-30 02:49:06 -0400725 int tx_subcrqs;
Dwip N. Banerjee9a87c3f2020-11-18 19:12:22 -0600726 u64 buff_size;
Thomas Falcon32053062018-03-16 20:00:27 -0500727 int i, rc;
Nathan Fontenotc657e322017-03-30 02:49:06 -0400728
Thomas Falcon507ebe62020-08-21 13:39:01 -0500729 tx_subcrqs = adapter->num_active_tx_scrqs;
Nathan Fontenotc657e322017-03-30 02:49:06 -0400730 adapter->tx_pool = kcalloc(tx_subcrqs,
731 sizeof(struct ibmvnic_tx_pool), GFP_KERNEL);
732 if (!adapter->tx_pool)
733 return -1;
734
Thomas Falcon32053062018-03-16 20:00:27 -0500735 adapter->tso_pool = kcalloc(tx_subcrqs,
736 sizeof(struct ibmvnic_tx_pool), GFP_KERNEL);
737 if (!adapter->tso_pool)
738 return -1;
739
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600740 adapter->num_active_tx_pools = tx_subcrqs;
741
Nathan Fontenotc657e322017-03-30 02:49:06 -0400742 for (i = 0; i < tx_subcrqs; i++) {
Dwip N. Banerjee9a87c3f2020-11-18 19:12:22 -0600743 buff_size = adapter->req_mtu + VLAN_HLEN;
744 buff_size = ALIGN(buff_size, L1_CACHE_BYTES);
Thomas Falcon32053062018-03-16 20:00:27 -0500745 rc = init_one_tx_pool(netdev, &adapter->tx_pool[i],
746 adapter->req_tx_entries_per_subcrq,
Dwip N. Banerjee9a87c3f2020-11-18 19:12:22 -0600747 buff_size);
Thomas Falcon32053062018-03-16 20:00:27 -0500748 if (rc) {
Nathan Fontenotc657e322017-03-30 02:49:06 -0400749 release_tx_pools(adapter);
Thomas Falcon32053062018-03-16 20:00:27 -0500750 return rc;
Nathan Fontenotc657e322017-03-30 02:49:06 -0400751 }
752
Thomas Falcon7c940b12019-06-07 16:03:55 -0500753 rc = init_one_tx_pool(netdev, &adapter->tso_pool[i],
754 IBMVNIC_TSO_BUFS,
755 IBMVNIC_TSO_BUF_SZ);
Thomas Falcon32053062018-03-16 20:00:27 -0500756 if (rc) {
Nathan Fontenotc657e322017-03-30 02:49:06 -0400757 release_tx_pools(adapter);
Thomas Falcon32053062018-03-16 20:00:27 -0500758 return rc;
Nathan Fontenotc657e322017-03-30 02:49:06 -0400759 }
Nathan Fontenotc657e322017-03-30 02:49:06 -0400760 }
761
762 return 0;
763}
764
John Allend944c3d62017-05-26 10:30:13 -0400765static void ibmvnic_napi_enable(struct ibmvnic_adapter *adapter)
766{
767 int i;
768
769 if (adapter->napi_enabled)
770 return;
771
772 for (i = 0; i < adapter->req_rx_queues; i++)
773 napi_enable(&adapter->napi[i]);
774
775 adapter->napi_enabled = true;
776}
777
778static void ibmvnic_napi_disable(struct ibmvnic_adapter *adapter)
779{
780 int i;
781
782 if (!adapter->napi_enabled)
783 return;
784
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500785 for (i = 0; i < adapter->req_rx_queues; i++) {
786 netdev_dbg(adapter->netdev, "Disabling napi[%d]\n", i);
John Allend944c3d62017-05-26 10:30:13 -0400787 napi_disable(&adapter->napi[i]);
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500788 }
John Allend944c3d62017-05-26 10:30:13 -0400789
790 adapter->napi_enabled = false;
791}
792
Nathan Fontenot86f669b2018-02-19 13:30:39 -0600793static int init_napi(struct ibmvnic_adapter *adapter)
794{
795 int i;
796
797 adapter->napi = kcalloc(adapter->req_rx_queues,
798 sizeof(struct napi_struct), GFP_KERNEL);
799 if (!adapter->napi)
800 return -ENOMEM;
801
802 for (i = 0; i < adapter->req_rx_queues; i++) {
803 netdev_dbg(adapter->netdev, "Adding napi[%d]\n", i);
804 netif_napi_add(adapter->netdev, &adapter->napi[i],
805 ibmvnic_poll, NAPI_POLL_WEIGHT);
806 }
807
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600808 adapter->num_active_rx_napi = adapter->req_rx_queues;
Nathan Fontenot86f669b2018-02-19 13:30:39 -0600809 return 0;
810}
811
812static void release_napi(struct ibmvnic_adapter *adapter)
813{
814 int i;
815
816 if (!adapter->napi)
817 return;
818
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600819 for (i = 0; i < adapter->num_active_rx_napi; i++) {
Wen Yang390de192018-12-11 12:20:46 +0800820 netdev_dbg(adapter->netdev, "Releasing napi[%d]\n", i);
821 netif_napi_del(&adapter->napi[i]);
Nathan Fontenot86f669b2018-02-19 13:30:39 -0600822 }
823
824 kfree(adapter->napi);
825 adapter->napi = NULL;
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600826 adapter->num_active_rx_napi = 0;
Thomas Falconc3f22412018-05-23 13:37:55 -0500827 adapter->napi_enabled = false;
Nathan Fontenot86f669b2018-02-19 13:30:39 -0600828}
829
John Allena57a5d22017-03-17 17:13:41 -0500830static int ibmvnic_login(struct net_device *netdev)
Thomas Falcon032c5e82015-12-21 11:26:06 -0600831{
832 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Dany Madden98c41f02020-11-25 18:04:32 -0600833 unsigned long timeout = msecs_to_jiffies(20000);
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500834 int retry_count = 0;
Thomas Falcondff515a32020-06-15 10:29:23 -0500835 int retries = 10;
Thomas Falconeb110412018-05-24 14:37:53 -0500836 bool retry;
Thomas Falcon4d96f122017-08-01 15:04:36 -0500837 int rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600838
John Allenbd0b6722017-03-17 17:13:40 -0500839 do {
Thomas Falconeb110412018-05-24 14:37:53 -0500840 retry = false;
Thomas Falcondff515a32020-06-15 10:29:23 -0500841 if (retry_count > retries) {
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500842 netdev_warn(netdev, "Login attempts exceeded\n");
843 return -1;
844 }
845
846 adapter->init_done_rc = 0;
847 reinit_completion(&adapter->init_done);
848 rc = send_login(adapter);
Dany Maddenc98d9cc2020-11-25 18:04:30 -0600849 if (rc)
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500850 return rc;
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500851
852 if (!wait_for_completion_timeout(&adapter->init_done,
853 timeout)) {
Thomas Falcondff515a32020-06-15 10:29:23 -0500854 netdev_warn(netdev, "Login timed out, retrying...\n");
855 retry = true;
856 adapter->init_done_rc = 0;
857 retry_count++;
858 continue;
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500859 }
860
Thomas Falcondff515a32020-06-15 10:29:23 -0500861 if (adapter->init_done_rc == ABORTED) {
862 netdev_warn(netdev, "Login aborted, retrying...\n");
863 retry = true;
864 adapter->init_done_rc = 0;
865 retry_count++;
866 /* FW or device may be busy, so
867 * wait a bit before retrying login
868 */
869 msleep(500);
870 } else if (adapter->init_done_rc == PARTIALSUCCESS) {
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500871 retry_count++;
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -0600872 release_sub_crqs(adapter, 1);
John Allenbd0b6722017-03-17 17:13:40 -0500873
Thomas Falconeb110412018-05-24 14:37:53 -0500874 retry = true;
875 netdev_dbg(netdev,
876 "Received partial success, retrying...\n");
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500877 adapter->init_done_rc = 0;
John Allenbd0b6722017-03-17 17:13:40 -0500878 reinit_completion(&adapter->init_done);
Lijun Pan491099a2020-09-27 20:13:26 -0500879 send_query_cap(adapter);
John Allenbd0b6722017-03-17 17:13:40 -0500880 if (!wait_for_completion_timeout(&adapter->init_done,
881 timeout)) {
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500882 netdev_warn(netdev,
883 "Capabilities query timed out\n");
John Allenbd0b6722017-03-17 17:13:40 -0500884 return -1;
885 }
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500886
Thomas Falcon4d96f122017-08-01 15:04:36 -0500887 rc = init_sub_crqs(adapter);
888 if (rc) {
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500889 netdev_warn(netdev,
890 "SCRQ initialization failed\n");
Thomas Falcon4d96f122017-08-01 15:04:36 -0500891 return -1;
892 }
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500893
Thomas Falcon4d96f122017-08-01 15:04:36 -0500894 rc = init_sub_crq_irqs(adapter);
895 if (rc) {
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500896 netdev_warn(netdev,
897 "SCRQ irq initialization failed\n");
Thomas Falcon4d96f122017-08-01 15:04:36 -0500898 return -1;
899 }
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500900 } else if (adapter->init_done_rc) {
901 netdev_warn(netdev, "Adapter login failed\n");
John Allenbd0b6722017-03-17 17:13:40 -0500902 return -1;
903 }
Thomas Falconeb110412018-05-24 14:37:53 -0500904 } while (retry);
John Allenbd0b6722017-03-17 17:13:40 -0500905
Thomas Falcon62740e92019-05-09 23:13:43 -0500906 __ibmvnic_set_mac(netdev, adapter->mac_addr);
Thomas Falcon3d166132018-01-10 19:39:52 -0600907
Sukadev Bhattiprolu38bd5ce2020-12-04 18:22:35 -0800908 netdev_dbg(netdev, "[S:%d] Login succeeded\n", adapter->state);
John Allena57a5d22017-03-17 17:13:41 -0500909 return 0;
910}
911
Thomas Falcon34f0f4e2018-02-13 18:23:40 -0600912static void release_login_buffer(struct ibmvnic_adapter *adapter)
913{
914 kfree(adapter->login_buf);
915 adapter->login_buf = NULL;
916}
917
918static void release_login_rsp_buffer(struct ibmvnic_adapter *adapter)
919{
920 kfree(adapter->login_rsp_buf);
921 adapter->login_rsp_buf = NULL;
922}
923
Nathan Fontenot1b8955e2017-03-30 02:49:29 -0400924static void release_resources(struct ibmvnic_adapter *adapter)
925{
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -0200926 release_vpd_data(adapter);
927
Nathan Fontenot1b8955e2017-03-30 02:49:29 -0400928 release_tx_pools(adapter);
929 release_rx_pools(adapter);
930
Nathan Fontenot86f669b2018-02-19 13:30:39 -0600931 release_napi(adapter);
Lijun Pana0c8be52020-12-19 15:39:19 -0600932 release_login_buffer(adapter);
Thomas Falcon34f0f4e2018-02-13 18:23:40 -0600933 release_login_rsp_buffer(adapter);
Nathan Fontenot1b8955e2017-03-30 02:49:29 -0400934}
935
Nathan Fontenot53da09e2017-04-21 15:39:04 -0400936static int set_link_state(struct ibmvnic_adapter *adapter, u8 link_state)
937{
938 struct net_device *netdev = adapter->netdev;
Dany Madden98c41f02020-11-25 18:04:32 -0600939 unsigned long timeout = msecs_to_jiffies(20000);
Nathan Fontenot53da09e2017-04-21 15:39:04 -0400940 union ibmvnic_crq crq;
941 bool resend;
942 int rc;
943
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500944 netdev_dbg(netdev, "setting link state %d\n", link_state);
945
Nathan Fontenot53da09e2017-04-21 15:39:04 -0400946 memset(&crq, 0, sizeof(crq));
947 crq.logical_link_state.first = IBMVNIC_CRQ_CMD;
948 crq.logical_link_state.cmd = LOGICAL_LINK_STATE;
949 crq.logical_link_state.link_state = link_state;
950
951 do {
952 resend = false;
953
954 reinit_completion(&adapter->init_done);
955 rc = ibmvnic_send_crq(adapter, &crq);
956 if (rc) {
957 netdev_err(netdev, "Failed to set link state\n");
958 return rc;
959 }
960
961 if (!wait_for_completion_timeout(&adapter->init_done,
962 timeout)) {
963 netdev_err(netdev, "timeout setting link state\n");
964 return -1;
965 }
966
Lijun Pan4c5f6af2020-08-19 17:52:23 -0500967 if (adapter->init_done_rc == PARTIALSUCCESS) {
Nathan Fontenot53da09e2017-04-21 15:39:04 -0400968 /* Partuial success, delay and re-send */
969 mdelay(1000);
970 resend = true;
Thomas Falconab5ec332018-05-23 13:37:59 -0500971 } else if (adapter->init_done_rc) {
972 netdev_warn(netdev, "Unable to set link state, rc=%d\n",
973 adapter->init_done_rc);
974 return adapter->init_done_rc;
Nathan Fontenot53da09e2017-04-21 15:39:04 -0400975 }
976 } while (resend);
977
978 return 0;
979}
980
Thomas Falcon7f3c6e62017-04-21 15:38:40 -0400981static int set_real_num_queues(struct net_device *netdev)
982{
983 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
984 int rc;
985
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500986 netdev_dbg(netdev, "Setting real tx/rx queues (%llx/%llx)\n",
987 adapter->req_tx_queues, adapter->req_rx_queues);
988
Thomas Falcon7f3c6e62017-04-21 15:38:40 -0400989 rc = netif_set_real_num_tx_queues(netdev, adapter->req_tx_queues);
990 if (rc) {
991 netdev_err(netdev, "failed to set the number of tx queues\n");
992 return rc;
993 }
994
995 rc = netif_set_real_num_rx_queues(netdev, adapter->req_rx_queues);
996 if (rc)
997 netdev_err(netdev, "failed to set the number of rx queues\n");
998
999 return rc;
1000}
1001
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001002static int ibmvnic_get_vpd(struct ibmvnic_adapter *adapter)
1003{
1004 struct device *dev = &adapter->vdev->dev;
1005 union ibmvnic_crq crq;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001006 int len = 0;
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05001007 int rc;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001008
1009 if (adapter->vpd->buff)
1010 len = adapter->vpd->len;
1011
Thomas Falconff25dcb2019-11-25 17:12:56 -06001012 mutex_lock(&adapter->fw_lock);
1013 adapter->fw_done_rc = 0;
Thomas Falcon070eca92019-11-25 17:12:53 -06001014 reinit_completion(&adapter->fw_done);
Thomas Falconff25dcb2019-11-25 17:12:56 -06001015
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001016 crq.get_vpd_size.first = IBMVNIC_CRQ_CMD;
1017 crq.get_vpd_size.cmd = GET_VPD_SIZE;
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05001018 rc = ibmvnic_send_crq(adapter, &crq);
Thomas Falconff25dcb2019-11-25 17:12:56 -06001019 if (rc) {
1020 mutex_unlock(&adapter->fw_lock);
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05001021 return rc;
Thomas Falconff25dcb2019-11-25 17:12:56 -06001022 }
Thomas Falcon476d96c2019-11-25 17:12:55 -06001023
1024 rc = ibmvnic_wait_for_completion(adapter, &adapter->fw_done, 10000);
1025 if (rc) {
1026 dev_err(dev, "Could not retrieve VPD size, rc = %d\n", rc);
Thomas Falconff25dcb2019-11-25 17:12:56 -06001027 mutex_unlock(&adapter->fw_lock);
Thomas Falcon476d96c2019-11-25 17:12:55 -06001028 return rc;
1029 }
Thomas Falconff25dcb2019-11-25 17:12:56 -06001030 mutex_unlock(&adapter->fw_lock);
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001031
1032 if (!adapter->vpd->len)
1033 return -ENODATA;
1034
1035 if (!adapter->vpd->buff)
1036 adapter->vpd->buff = kzalloc(adapter->vpd->len, GFP_KERNEL);
1037 else if (adapter->vpd->len != len)
1038 adapter->vpd->buff =
1039 krealloc(adapter->vpd->buff,
1040 adapter->vpd->len, GFP_KERNEL);
1041
1042 if (!adapter->vpd->buff) {
1043 dev_err(dev, "Could allocate VPD buffer\n");
1044 return -ENOMEM;
1045 }
1046
1047 adapter->vpd->dma_addr =
1048 dma_map_single(dev, adapter->vpd->buff, adapter->vpd->len,
1049 DMA_FROM_DEVICE);
Desnes Augusto Nunes do Rosariof7431062017-11-17 09:09:04 -02001050 if (dma_mapping_error(dev, adapter->vpd->dma_addr)) {
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001051 dev_err(dev, "Could not map VPD buffer\n");
1052 kfree(adapter->vpd->buff);
Thomas Falconb0992ec2018-02-06 17:25:23 -06001053 adapter->vpd->buff = NULL;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001054 return -ENOMEM;
1055 }
1056
Thomas Falconff25dcb2019-11-25 17:12:56 -06001057 mutex_lock(&adapter->fw_lock);
1058 adapter->fw_done_rc = 0;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001059 reinit_completion(&adapter->fw_done);
Thomas Falconff25dcb2019-11-25 17:12:56 -06001060
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001061 crq.get_vpd.first = IBMVNIC_CRQ_CMD;
1062 crq.get_vpd.cmd = GET_VPD;
1063 crq.get_vpd.ioba = cpu_to_be32(adapter->vpd->dma_addr);
1064 crq.get_vpd.len = cpu_to_be32((u32)adapter->vpd->len);
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05001065 rc = ibmvnic_send_crq(adapter, &crq);
1066 if (rc) {
1067 kfree(adapter->vpd->buff);
1068 adapter->vpd->buff = NULL;
Thomas Falconff25dcb2019-11-25 17:12:56 -06001069 mutex_unlock(&adapter->fw_lock);
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05001070 return rc;
1071 }
Thomas Falcon476d96c2019-11-25 17:12:55 -06001072
1073 rc = ibmvnic_wait_for_completion(adapter, &adapter->fw_done, 10000);
1074 if (rc) {
1075 dev_err(dev, "Unable to retrieve VPD, rc = %d\n", rc);
1076 kfree(adapter->vpd->buff);
1077 adapter->vpd->buff = NULL;
Thomas Falconff25dcb2019-11-25 17:12:56 -06001078 mutex_unlock(&adapter->fw_lock);
Thomas Falcon476d96c2019-11-25 17:12:55 -06001079 return rc;
1080 }
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001081
Thomas Falconff25dcb2019-11-25 17:12:56 -06001082 mutex_unlock(&adapter->fw_lock);
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001083 return 0;
1084}
1085
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001086static int init_resources(struct ibmvnic_adapter *adapter)
John Allena57a5d22017-03-17 17:13:41 -05001087{
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001088 struct net_device *netdev = adapter->netdev;
Nathan Fontenot86f669b2018-02-19 13:30:39 -06001089 int rc;
John Allena57a5d22017-03-17 17:13:41 -05001090
Thomas Falcon7f3c6e62017-04-21 15:38:40 -04001091 rc = set_real_num_queues(netdev);
1092 if (rc)
1093 return rc;
John Allenbd0b6722017-03-17 17:13:40 -05001094
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001095 adapter->vpd = kzalloc(sizeof(*adapter->vpd), GFP_KERNEL);
1096 if (!adapter->vpd)
1097 return -ENOMEM;
1098
John Allen69d08dc2018-01-18 16:27:58 -06001099 /* Vital Product Data (VPD) */
1100 rc = ibmvnic_get_vpd(adapter);
1101 if (rc) {
1102 netdev_err(netdev, "failed to initialize Vital Product Data (VPD)\n");
1103 return rc;
1104 }
1105
Thomas Falcon032c5e82015-12-21 11:26:06 -06001106 adapter->map_id = 1;
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001107
Nathan Fontenot86f669b2018-02-19 13:30:39 -06001108 rc = init_napi(adapter);
1109 if (rc)
1110 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001111
Lijun Pan69980d02020-09-27 20:13:28 -05001112 send_query_map(adapter);
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -04001113
1114 rc = init_rx_pools(netdev);
1115 if (rc)
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001116 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001117
Nathan Fontenotc657e322017-03-30 02:49:06 -04001118 rc = init_tx_pools(netdev);
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001119 return rc;
1120}
1121
Nathan Fontenoted651a12017-05-03 14:04:38 -04001122static int __ibmvnic_open(struct net_device *netdev)
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001123{
1124 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001125 enum vnic_state prev_state = adapter->state;
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001126 int i, rc;
1127
Nathan Fontenot90c80142017-05-03 14:04:32 -04001128 adapter->state = VNIC_OPENING;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001129 replenish_pools(adapter);
John Allend944c3d62017-05-26 10:30:13 -04001130 ibmvnic_napi_enable(adapter);
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001131
Thomas Falcon032c5e82015-12-21 11:26:06 -06001132 /* We're ready to receive frames, enable the sub-crq interrupts and
1133 * set the logical link state to up
1134 */
Nathan Fontenoted651a12017-05-03 14:04:38 -04001135 for (i = 0; i < adapter->req_rx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001136 netdev_dbg(netdev, "Enabling rx_scrq[%d] irq\n", i);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001137 if (prev_state == VNIC_CLOSED)
1138 enable_irq(adapter->rx_scrq[i]->irq);
Thomas Falconf23e0642018-04-15 18:53:36 -05001139 enable_scrq_irq(adapter, adapter->rx_scrq[i]);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001140 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001141
Nathan Fontenoted651a12017-05-03 14:04:38 -04001142 for (i = 0; i < adapter->req_tx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001143 netdev_dbg(netdev, "Enabling tx_scrq[%d] irq\n", i);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001144 if (prev_state == VNIC_CLOSED)
1145 enable_irq(adapter->tx_scrq[i]->irq);
Thomas Falconf23e0642018-04-15 18:53:36 -05001146 enable_scrq_irq(adapter, adapter->tx_scrq[i]);
Thomas Falcon0d973382020-11-18 19:12:19 -06001147 netdev_tx_reset_queue(netdev_get_tx_queue(netdev, i));
Nathan Fontenoted651a12017-05-03 14:04:38 -04001148 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001149
Nathan Fontenot53da09e2017-04-21 15:39:04 -04001150 rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_UP);
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001151 if (rc) {
1152 for (i = 0; i < adapter->req_rx_queues; i++)
1153 napi_disable(&adapter->napi[i]);
1154 release_resources(adapter);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001155 return rc;
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001156 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001157
Nathan Fontenoted651a12017-05-03 14:04:38 -04001158 netif_tx_start_all_queues(netdev);
1159
1160 if (prev_state == VNIC_CLOSED) {
1161 for (i = 0; i < adapter->req_rx_queues; i++)
1162 napi_schedule(&adapter->napi[i]);
1163 }
1164
1165 adapter->state = VNIC_OPEN;
1166 return rc;
1167}
1168
1169static int ibmvnic_open(struct net_device *netdev)
1170{
1171 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
John Allen69d08dc2018-01-18 16:27:58 -06001172 int rc;
Nathan Fontenoted651a12017-05-03 14:04:38 -04001173
Sukadev Bhattiprolu8f1c0fd2021-02-23 21:02:29 -08001174 ASSERT_RTNL();
1175
1176 /* If device failover is pending or we are about to reset, just set
1177 * device state and return. Device operation will be handled by reset
1178 * routine.
1179 *
1180 * It should be safe to overwrite the adapter->state here. Since
1181 * we hold the rtnl, either the reset has not actually started or
1182 * the rtnl got dropped during the set_link_state() in do_reset().
1183 * In the former case, no one else is changing the state (again we
1184 * have the rtnl) and in the latter case, do_reset() will detect and
1185 * honor our setting below.
Thomas Falcon5a18e1e2018-04-06 18:37:05 -05001186 */
Sukadev Bhattiprolu8f1c0fd2021-02-23 21:02:29 -08001187 if (adapter->failover_pending || (test_bit(0, &adapter->resetting))) {
1188 netdev_dbg(netdev, "[S:%d FOP:%d] Resetting, deferring open\n",
1189 adapter->state, adapter->failover_pending);
Thomas Falcon5a18e1e2018-04-06 18:37:05 -05001190 adapter->state = VNIC_OPEN;
Sukadev Bhattiprolu8f1c0fd2021-02-23 21:02:29 -08001191 rc = 0;
1192 goto out;
Thomas Falcon5a18e1e2018-04-06 18:37:05 -05001193 }
1194
Nathan Fontenoted651a12017-05-03 14:04:38 -04001195 if (adapter->state != VNIC_CLOSED) {
1196 rc = ibmvnic_login(netdev);
Juliet Kima5681e22018-11-19 15:59:22 -06001197 if (rc)
Sukadev Bhattiprolu1d850492020-10-30 10:07:11 -07001198 goto out;
Nathan Fontenoted651a12017-05-03 14:04:38 -04001199
1200 rc = init_resources(adapter);
1201 if (rc) {
1202 netdev_err(netdev, "failed to initialize resources\n");
1203 release_resources(adapter);
Sukadev Bhattiprolu1d850492020-10-30 10:07:11 -07001204 goto out;
Nathan Fontenoted651a12017-05-03 14:04:38 -04001205 }
1206 }
1207
1208 rc = __ibmvnic_open(netdev);
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001209
Sukadev Bhattiprolu1d850492020-10-30 10:07:11 -07001210out:
Sukadev Bhattiprolu8f1c0fd2021-02-23 21:02:29 -08001211 /* If open failed and there is a pending failover or in-progress reset,
1212 * set device state and return. Device operation will be handled by
1213 * reset routine. See also comments above regarding rtnl.
Sukadev Bhattiprolu1d850492020-10-30 10:07:11 -07001214 */
Sukadev Bhattiprolu8f1c0fd2021-02-23 21:02:29 -08001215 if (rc &&
1216 (adapter->failover_pending || (test_bit(0, &adapter->resetting)))) {
Sukadev Bhattiprolu1d850492020-10-30 10:07:11 -07001217 adapter->state = VNIC_OPEN;
1218 rc = 0;
1219 }
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001220 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001221}
1222
Thomas Falcond0869c02018-02-13 18:23:43 -06001223static void clean_rx_pools(struct ibmvnic_adapter *adapter)
1224{
1225 struct ibmvnic_rx_pool *rx_pool;
Thomas Falcon637f81d2018-02-26 18:10:57 -06001226 struct ibmvnic_rx_buff *rx_buff;
Thomas Falcond0869c02018-02-13 18:23:43 -06001227 u64 rx_entries;
1228 int rx_scrqs;
1229 int i, j;
1230
1231 if (!adapter->rx_pool)
1232 return;
1233
Thomas Falcon660e3092018-04-20 14:25:32 -05001234 rx_scrqs = adapter->num_active_rx_pools;
Thomas Falcond0869c02018-02-13 18:23:43 -06001235 rx_entries = adapter->req_rx_add_entries_per_subcrq;
1236
1237 /* Free any remaining skbs in the rx buffer pools */
1238 for (i = 0; i < rx_scrqs; i++) {
1239 rx_pool = &adapter->rx_pool[i];
Thomas Falcon637f81d2018-02-26 18:10:57 -06001240 if (!rx_pool || !rx_pool->rx_buff)
Thomas Falcond0869c02018-02-13 18:23:43 -06001241 continue;
1242
1243 netdev_dbg(adapter->netdev, "Cleaning rx_pool[%d]\n", i);
1244 for (j = 0; j < rx_entries; j++) {
Thomas Falcon637f81d2018-02-26 18:10:57 -06001245 rx_buff = &rx_pool->rx_buff[j];
1246 if (rx_buff && rx_buff->skb) {
1247 dev_kfree_skb_any(rx_buff->skb);
1248 rx_buff->skb = NULL;
Thomas Falcond0869c02018-02-13 18:23:43 -06001249 }
1250 }
1251 }
1252}
1253
Thomas Falcone9e1e972018-03-16 20:00:30 -05001254static void clean_one_tx_pool(struct ibmvnic_adapter *adapter,
1255 struct ibmvnic_tx_pool *tx_pool)
Nathan Fontenotb41b83e2017-05-03 14:04:56 -04001256{
Thomas Falcon637f81d2018-02-26 18:10:57 -06001257 struct ibmvnic_tx_buff *tx_buff;
Nathan Fontenotb41b83e2017-05-03 14:04:56 -04001258 u64 tx_entries;
Thomas Falcone9e1e972018-03-16 20:00:30 -05001259 int i;
Nathan Fontenotb41b83e2017-05-03 14:04:56 -04001260
Dan Carpenter050e85c2018-03-23 14:36:15 +03001261 if (!tx_pool || !tx_pool->tx_buff)
Thomas Falcone9e1e972018-03-16 20:00:30 -05001262 return;
1263
1264 tx_entries = tx_pool->num_buffers;
1265
1266 for (i = 0; i < tx_entries; i++) {
1267 tx_buff = &tx_pool->tx_buff[i];
1268 if (tx_buff && tx_buff->skb) {
1269 dev_kfree_skb_any(tx_buff->skb);
1270 tx_buff->skb = NULL;
1271 }
1272 }
1273}
1274
1275static void clean_tx_pools(struct ibmvnic_adapter *adapter)
1276{
1277 int tx_scrqs;
1278 int i;
1279
1280 if (!adapter->tx_pool || !adapter->tso_pool)
Nathan Fontenotb41b83e2017-05-03 14:04:56 -04001281 return;
1282
Thomas Falcon660e3092018-04-20 14:25:32 -05001283 tx_scrqs = adapter->num_active_tx_pools;
Nathan Fontenotb41b83e2017-05-03 14:04:56 -04001284
1285 /* Free any remaining skbs in the tx buffer pools */
1286 for (i = 0; i < tx_scrqs; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001287 netdev_dbg(adapter->netdev, "Cleaning tx_pool[%d]\n", i);
Thomas Falcone9e1e972018-03-16 20:00:30 -05001288 clean_one_tx_pool(adapter, &adapter->tx_pool[i]);
1289 clean_one_tx_pool(adapter, &adapter->tso_pool[i]);
Nathan Fontenotb41b83e2017-05-03 14:04:56 -04001290 }
1291}
1292
John Allen6095e592018-03-30 13:44:21 -05001293static void ibmvnic_disable_irqs(struct ibmvnic_adapter *adapter)
John Allenea5509f2017-03-17 17:13:43 -05001294{
John Allen6095e592018-03-30 13:44:21 -05001295 struct net_device *netdev = adapter->netdev;
John Allenea5509f2017-03-17 17:13:43 -05001296 int i;
1297
Nathan Fontenot46293b92017-05-03 14:05:02 -04001298 if (adapter->tx_scrq) {
1299 for (i = 0; i < adapter->req_tx_queues; i++)
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001300 if (adapter->tx_scrq[i]->irq) {
Thomas Falconf8738662018-03-07 17:51:45 -06001301 netdev_dbg(netdev,
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001302 "Disabling tx_scrq[%d] irq\n", i);
Thomas Falconf23e0642018-04-15 18:53:36 -05001303 disable_scrq_irq(adapter, adapter->tx_scrq[i]);
Nathan Fontenot46293b92017-05-03 14:05:02 -04001304 disable_irq(adapter->tx_scrq[i]->irq);
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001305 }
Nathan Fontenot46293b92017-05-03 14:05:02 -04001306 }
1307
Nathan Fontenot46293b92017-05-03 14:05:02 -04001308 if (adapter->rx_scrq) {
1309 for (i = 0; i < adapter->req_rx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001310 if (adapter->rx_scrq[i]->irq) {
Thomas Falconf8738662018-03-07 17:51:45 -06001311 netdev_dbg(netdev,
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001312 "Disabling rx_scrq[%d] irq\n", i);
Thomas Falconf23e0642018-04-15 18:53:36 -05001313 disable_scrq_irq(adapter, adapter->rx_scrq[i]);
Nathan Fontenot46293b92017-05-03 14:05:02 -04001314 disable_irq(adapter->rx_scrq[i]->irq);
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001315 }
Nathan Fontenot46293b92017-05-03 14:05:02 -04001316 }
1317 }
John Allen6095e592018-03-30 13:44:21 -05001318}
1319
1320static void ibmvnic_cleanup(struct net_device *netdev)
1321{
1322 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1323
1324 /* ensure that transmissions are stopped if called by do_reset */
Juliet Kim7ed5b312019-09-20 16:11:23 -04001325 if (test_bit(0, &adapter->resetting))
John Allen6095e592018-03-30 13:44:21 -05001326 netif_tx_disable(netdev);
1327 else
1328 netif_tx_stop_all_queues(netdev);
1329
1330 ibmvnic_napi_disable(adapter);
1331 ibmvnic_disable_irqs(adapter);
1332
Thomas Falcond0869c02018-02-13 18:23:43 -06001333 clean_rx_pools(adapter);
Thomas Falcon10f76212017-05-26 10:30:31 -04001334 clean_tx_pools(adapter);
Thomas Falcon01d9bd72018-03-07 17:51:46 -06001335}
1336
1337static int __ibmvnic_close(struct net_device *netdev)
1338{
1339 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1340 int rc = 0;
1341
1342 adapter->state = VNIC_CLOSING;
1343 rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_DN);
Nathan Fontenot90c80142017-05-03 14:04:32 -04001344 adapter->state = VNIC_CLOSED;
Sukadev Bhattiprolud4083d32021-02-10 17:41:43 -08001345 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001346}
1347
Nathan Fontenoted651a12017-05-03 14:04:38 -04001348static int ibmvnic_close(struct net_device *netdev)
1349{
1350 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1351 int rc;
1352
Sukadev Bhattiprolu38bd5ce2020-12-04 18:22:35 -08001353 netdev_dbg(netdev, "[S:%d FOP:%d FRR:%d] Closing\n",
1354 adapter->state, adapter->failover_pending,
1355 adapter->force_reset_recovery);
1356
Thomas Falcon5a18e1e2018-04-06 18:37:05 -05001357 /* If device failover is pending, just set device state and return.
1358 * Device operation will be handled by reset routine.
1359 */
1360 if (adapter->failover_pending) {
1361 adapter->state = VNIC_CLOSED;
1362 return 0;
1363 }
1364
Nathan Fontenoted651a12017-05-03 14:04:38 -04001365 rc = __ibmvnic_close(netdev);
Nathan Fontenot30f79622018-04-06 18:37:06 -05001366 ibmvnic_cleanup(netdev);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001367
1368 return rc;
1369}
1370
Thomas Falconad7775d2016-04-01 17:20:34 -05001371/**
1372 * build_hdr_data - creates L2/L3/L4 header data buffer
Lee Jones80708602021-01-15 20:09:03 +00001373 * @hdr_field: bitfield determining needed headers
1374 * @skb: socket buffer
1375 * @hdr_len: array of header lengths
1376 * @hdr_data: buffer to write the header to
Thomas Falconad7775d2016-04-01 17:20:34 -05001377 *
1378 * Reads hdr_field to determine which headers are needed by firmware.
1379 * Builds a buffer containing these headers. Saves individual header
1380 * lengths and total buffer length to be used to build descriptors.
1381 */
1382static int build_hdr_data(u8 hdr_field, struct sk_buff *skb,
1383 int *hdr_len, u8 *hdr_data)
1384{
1385 int len = 0;
1386 u8 *hdr;
1387
Thomas Falconda75e3b2018-03-12 11:51:02 -05001388 if (skb_vlan_tagged(skb) && !skb_vlan_tag_present(skb))
1389 hdr_len[0] = sizeof(struct vlan_ethhdr);
1390 else
1391 hdr_len[0] = sizeof(struct ethhdr);
Thomas Falconad7775d2016-04-01 17:20:34 -05001392
1393 if (skb->protocol == htons(ETH_P_IP)) {
1394 hdr_len[1] = ip_hdr(skb)->ihl * 4;
1395 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
1396 hdr_len[2] = tcp_hdrlen(skb);
1397 else if (ip_hdr(skb)->protocol == IPPROTO_UDP)
1398 hdr_len[2] = sizeof(struct udphdr);
1399 } else if (skb->protocol == htons(ETH_P_IPV6)) {
1400 hdr_len[1] = sizeof(struct ipv6hdr);
1401 if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
1402 hdr_len[2] = tcp_hdrlen(skb);
1403 else if (ipv6_hdr(skb)->nexthdr == IPPROTO_UDP)
1404 hdr_len[2] = sizeof(struct udphdr);
Thomas Falcon4eb50ce2017-12-18 12:52:40 -06001405 } else if (skb->protocol == htons(ETH_P_ARP)) {
1406 hdr_len[1] = arp_hdr_len(skb->dev);
1407 hdr_len[2] = 0;
Thomas Falconad7775d2016-04-01 17:20:34 -05001408 }
1409
1410 memset(hdr_data, 0, 120);
1411 if ((hdr_field >> 6) & 1) {
1412 hdr = skb_mac_header(skb);
1413 memcpy(hdr_data, hdr, hdr_len[0]);
1414 len += hdr_len[0];
1415 }
1416
1417 if ((hdr_field >> 5) & 1) {
1418 hdr = skb_network_header(skb);
1419 memcpy(hdr_data + len, hdr, hdr_len[1]);
1420 len += hdr_len[1];
1421 }
1422
1423 if ((hdr_field >> 4) & 1) {
1424 hdr = skb_transport_header(skb);
1425 memcpy(hdr_data + len, hdr, hdr_len[2]);
1426 len += hdr_len[2];
1427 }
1428 return len;
1429}
1430
1431/**
1432 * create_hdr_descs - create header and header extension descriptors
Lee Jones80708602021-01-15 20:09:03 +00001433 * @hdr_field: bitfield determining needed headers
1434 * @hdr_data: buffer containing header data
1435 * @len: length of data buffer
1436 * @hdr_len: array of individual header lengths
1437 * @scrq_arr: descriptor array
Thomas Falconad7775d2016-04-01 17:20:34 -05001438 *
1439 * Creates header and, if needed, header extension descriptors and
1440 * places them in a descriptor array, scrq_arr
1441 */
1442
Thomas Falcon2de09682017-10-16 10:02:11 -05001443static int create_hdr_descs(u8 hdr_field, u8 *hdr_data, int len, int *hdr_len,
1444 union sub_crq *scrq_arr)
Thomas Falconad7775d2016-04-01 17:20:34 -05001445{
1446 union sub_crq hdr_desc;
1447 int tmp_len = len;
Thomas Falcon2de09682017-10-16 10:02:11 -05001448 int num_descs = 0;
Thomas Falconad7775d2016-04-01 17:20:34 -05001449 u8 *data, *cur;
1450 int tmp;
1451
1452 while (tmp_len > 0) {
1453 cur = hdr_data + len - tmp_len;
1454
1455 memset(&hdr_desc, 0, sizeof(hdr_desc));
1456 if (cur != hdr_data) {
1457 data = hdr_desc.hdr_ext.data;
1458 tmp = tmp_len > 29 ? 29 : tmp_len;
1459 hdr_desc.hdr_ext.first = IBMVNIC_CRQ_CMD;
1460 hdr_desc.hdr_ext.type = IBMVNIC_HDR_EXT_DESC;
1461 hdr_desc.hdr_ext.len = tmp;
1462 } else {
1463 data = hdr_desc.hdr.data;
1464 tmp = tmp_len > 24 ? 24 : tmp_len;
1465 hdr_desc.hdr.first = IBMVNIC_CRQ_CMD;
1466 hdr_desc.hdr.type = IBMVNIC_HDR_DESC;
1467 hdr_desc.hdr.len = tmp;
1468 hdr_desc.hdr.l2_len = (u8)hdr_len[0];
1469 hdr_desc.hdr.l3_len = cpu_to_be16((u16)hdr_len[1]);
1470 hdr_desc.hdr.l4_len = (u8)hdr_len[2];
1471 hdr_desc.hdr.flag = hdr_field << 1;
1472 }
1473 memcpy(data, cur, tmp);
1474 tmp_len -= tmp;
1475 *scrq_arr = hdr_desc;
1476 scrq_arr++;
Thomas Falcon2de09682017-10-16 10:02:11 -05001477 num_descs++;
Thomas Falconad7775d2016-04-01 17:20:34 -05001478 }
Thomas Falcon2de09682017-10-16 10:02:11 -05001479
1480 return num_descs;
Thomas Falconad7775d2016-04-01 17:20:34 -05001481}
1482
1483/**
1484 * build_hdr_descs_arr - build a header descriptor array
Lee Jones80708602021-01-15 20:09:03 +00001485 * @txbuff: tx buffer
1486 * @num_entries: number of descriptors to be sent
1487 * @hdr_field: bit field determining which headers will be sent
Thomas Falconad7775d2016-04-01 17:20:34 -05001488 *
1489 * This function will build a TX descriptor array with applicable
1490 * L2/L3/L4 packet header descriptors to be sent by send_subcrq_indirect.
1491 */
1492
Thomas Falconc62aa372020-11-18 19:12:20 -06001493static void build_hdr_descs_arr(struct sk_buff *skb,
1494 union sub_crq *indir_arr,
Thomas Falconad7775d2016-04-01 17:20:34 -05001495 int *num_entries, u8 hdr_field)
1496{
1497 int hdr_len[3] = {0, 0, 0};
Thomas Falconc62aa372020-11-18 19:12:20 -06001498 u8 hdr_data[140] = {0};
Thomas Falcon2de09682017-10-16 10:02:11 -05001499 int tot_len;
Thomas Falconad7775d2016-04-01 17:20:34 -05001500
Thomas Falconc62aa372020-11-18 19:12:20 -06001501 tot_len = build_hdr_data(hdr_field, skb, hdr_len,
1502 hdr_data);
Thomas Falcon2de09682017-10-16 10:02:11 -05001503 *num_entries += create_hdr_descs(hdr_field, hdr_data, tot_len, hdr_len,
Thomas Falconc62aa372020-11-18 19:12:20 -06001504 indir_arr + 1);
Thomas Falconad7775d2016-04-01 17:20:34 -05001505}
1506
Thomas Falcon1f247a62018-03-12 11:51:04 -05001507static int ibmvnic_xmit_workarounds(struct sk_buff *skb,
1508 struct net_device *netdev)
1509{
1510 /* For some backing devices, mishandling of small packets
1511 * can result in a loss of connection or TX stall. Device
1512 * architects recommend that no packet should be smaller
1513 * than the minimum MTU value provided to the driver, so
1514 * pad any packets to that length
1515 */
1516 if (skb->len < netdev->min_mtu)
1517 return skb_put_padto(skb, netdev->min_mtu);
Thomas Falcon7083a452018-03-12 21:05:26 -05001518
1519 return 0;
Thomas Falcon1f247a62018-03-12 11:51:04 -05001520}
1521
Thomas Falcon0d973382020-11-18 19:12:19 -06001522static void ibmvnic_tx_scrq_clean_buffer(struct ibmvnic_adapter *adapter,
1523 struct ibmvnic_sub_crq_queue *tx_scrq)
1524{
1525 struct ibmvnic_ind_xmit_queue *ind_bufp;
1526 struct ibmvnic_tx_buff *tx_buff;
1527 struct ibmvnic_tx_pool *tx_pool;
1528 union sub_crq tx_scrq_entry;
1529 int queue_num;
1530 int entries;
1531 int index;
1532 int i;
1533
1534 ind_bufp = &tx_scrq->ind_buf;
1535 entries = (u64)ind_bufp->index;
1536 queue_num = tx_scrq->pool_index;
1537
1538 for (i = entries - 1; i >= 0; --i) {
1539 tx_scrq_entry = ind_bufp->indir_arr[i];
1540 if (tx_scrq_entry.v1.type != IBMVNIC_TX_DESC)
1541 continue;
1542 index = be32_to_cpu(tx_scrq_entry.v1.correlator);
1543 if (index & IBMVNIC_TSO_POOL_MASK) {
1544 tx_pool = &adapter->tso_pool[queue_num];
1545 index &= ~IBMVNIC_TSO_POOL_MASK;
1546 } else {
1547 tx_pool = &adapter->tx_pool[queue_num];
1548 }
1549 tx_pool->free_map[tx_pool->consumer_index] = index;
1550 tx_pool->consumer_index = tx_pool->consumer_index == 0 ?
1551 tx_pool->num_buffers - 1 :
1552 tx_pool->consumer_index - 1;
1553 tx_buff = &tx_pool->tx_buff[index];
1554 adapter->netdev->stats.tx_packets--;
1555 adapter->netdev->stats.tx_bytes -= tx_buff->skb->len;
1556 adapter->tx_stats_buffers[queue_num].packets--;
1557 adapter->tx_stats_buffers[queue_num].bytes -=
1558 tx_buff->skb->len;
1559 dev_kfree_skb_any(tx_buff->skb);
1560 tx_buff->skb = NULL;
1561 adapter->netdev->stats.tx_dropped++;
1562 }
1563 ind_bufp->index = 0;
1564 if (atomic_sub_return(entries, &tx_scrq->used) <=
1565 (adapter->req_tx_entries_per_subcrq / 2) &&
1566 __netif_subqueue_stopped(adapter->netdev, queue_num)) {
1567 netif_wake_subqueue(adapter->netdev, queue_num);
1568 netdev_dbg(adapter->netdev, "Started queue %d\n",
1569 queue_num);
1570 }
1571}
1572
1573static int ibmvnic_tx_scrq_flush(struct ibmvnic_adapter *adapter,
1574 struct ibmvnic_sub_crq_queue *tx_scrq)
1575{
1576 struct ibmvnic_ind_xmit_queue *ind_bufp;
1577 u64 dma_addr;
1578 u64 entries;
1579 u64 handle;
1580 int rc;
1581
1582 ind_bufp = &tx_scrq->ind_buf;
1583 dma_addr = (u64)ind_bufp->indir_dma;
1584 entries = (u64)ind_bufp->index;
1585 handle = tx_scrq->handle;
1586
1587 if (!entries)
1588 return 0;
1589 rc = send_subcrq_indirect(adapter, handle, dma_addr, entries);
1590 if (rc)
1591 ibmvnic_tx_scrq_clean_buffer(adapter, tx_scrq);
1592 else
1593 ind_bufp->index = 0;
1594 return 0;
1595}
1596
YueHaibing94b2bb22018-09-18 14:35:47 +08001597static netdev_tx_t ibmvnic_xmit(struct sk_buff *skb, struct net_device *netdev)
Thomas Falcon032c5e82015-12-21 11:26:06 -06001598{
1599 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1600 int queue_num = skb_get_queue_mapping(skb);
Thomas Falconad7775d2016-04-01 17:20:34 -05001601 u8 *hdrs = (u8 *)&adapter->tx_rx_desc_req;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001602 struct device *dev = &adapter->vdev->dev;
Thomas Falcon0d973382020-11-18 19:12:19 -06001603 struct ibmvnic_ind_xmit_queue *ind_bufp;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001604 struct ibmvnic_tx_buff *tx_buff = NULL;
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001605 struct ibmvnic_sub_crq_queue *tx_scrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001606 struct ibmvnic_tx_pool *tx_pool;
1607 unsigned int tx_send_failed = 0;
Thomas Falcon0d973382020-11-18 19:12:19 -06001608 netdev_tx_t ret = NETDEV_TX_OK;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001609 unsigned int tx_map_failed = 0;
Thomas Falconc62aa372020-11-18 19:12:20 -06001610 union sub_crq indir_arr[16];
Thomas Falcon032c5e82015-12-21 11:26:06 -06001611 unsigned int tx_dropped = 0;
1612 unsigned int tx_packets = 0;
1613 unsigned int tx_bytes = 0;
1614 dma_addr_t data_dma_addr;
1615 struct netdev_queue *txq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001616 unsigned long lpar_rc;
1617 union sub_crq tx_crq;
1618 unsigned int offset;
Thomas Falconad7775d2016-04-01 17:20:34 -05001619 int num_entries = 1;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001620 unsigned char *dst;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001621 int index = 0;
Thomas Falcona0dca102018-01-18 19:29:48 -06001622 u8 proto = 0;
Thomas Falcon0d973382020-11-18 19:12:19 -06001623
1624 tx_scrq = adapter->tx_scrq[queue_num];
1625 txq = netdev_get_tx_queue(netdev, queue_num);
1626 ind_bufp = &tx_scrq->ind_buf;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001627
Juliet Kim7ed5b312019-09-20 16:11:23 -04001628 if (test_bit(0, &adapter->resetting)) {
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001629 if (!netif_subqueue_stopped(netdev, skb))
1630 netif_stop_subqueue(netdev, queue_num);
1631 dev_kfree_skb_any(skb);
1632
Thomas Falcon032c5e82015-12-21 11:26:06 -06001633 tx_send_failed++;
1634 tx_dropped++;
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001635 ret = NETDEV_TX_OK;
Thomas Falcon0d973382020-11-18 19:12:19 -06001636 ibmvnic_tx_scrq_flush(adapter, tx_scrq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001637 goto out;
1638 }
1639
Thomas Falcon7083a452018-03-12 21:05:26 -05001640 if (ibmvnic_xmit_workarounds(skb, netdev)) {
Thomas Falcon1f247a62018-03-12 11:51:04 -05001641 tx_dropped++;
1642 tx_send_failed++;
1643 ret = NETDEV_TX_OK;
Thomas Falcon0d973382020-11-18 19:12:19 -06001644 ibmvnic_tx_scrq_flush(adapter, tx_scrq);
Thomas Falcon1f247a62018-03-12 11:51:04 -05001645 goto out;
1646 }
Thomas Falcon06b3e352018-03-16 20:00:28 -05001647 if (skb_is_gso(skb))
1648 tx_pool = &adapter->tso_pool[queue_num];
1649 else
1650 tx_pool = &adapter->tx_pool[queue_num];
Thomas Falcon1f247a62018-03-12 11:51:04 -05001651
Thomas Falcon032c5e82015-12-21 11:26:06 -06001652 index = tx_pool->free_map[tx_pool->consumer_index];
Thomas Falconfdb06102017-10-17 12:36:55 -05001653
Thomas Falcon86b61a52018-03-16 20:00:29 -05001654 if (index == IBMVNIC_INVALID_MAP) {
1655 dev_kfree_skb_any(skb);
1656 tx_send_failed++;
1657 tx_dropped++;
1658 ret = NETDEV_TX_OK;
Thomas Falcon0d973382020-11-18 19:12:19 -06001659 ibmvnic_tx_scrq_flush(adapter, tx_scrq);
Thomas Falcon86b61a52018-03-16 20:00:29 -05001660 goto out;
1661 }
1662
1663 tx_pool->free_map[tx_pool->consumer_index] = IBMVNIC_INVALID_MAP;
1664
Thomas Falcon06b3e352018-03-16 20:00:28 -05001665 offset = index * tx_pool->buf_size;
1666 dst = tx_pool->long_term_buff.buff + offset;
1667 memset(dst, 0, tx_pool->buf_size);
1668 data_dma_addr = tx_pool->long_term_buff.addr + offset;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001669
Thomas Falcon15482052017-10-17 12:36:54 -05001670 if (skb_shinfo(skb)->nr_frags) {
1671 int cur, i;
1672
1673 /* Copy the head */
1674 skb_copy_from_linear_data(skb, dst, skb_headlen(skb));
1675 cur = skb_headlen(skb);
1676
1677 /* Copy the frags */
1678 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1679 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1680
Christophe JAILLETc3105f82021-04-04 10:54:37 +02001681 memcpy(dst + cur, skb_frag_address(frag),
1682 skb_frag_size(frag));
Thomas Falcon15482052017-10-17 12:36:54 -05001683 cur += skb_frag_size(frag);
1684 }
1685 } else {
1686 skb_copy_from_linear_data(skb, dst, skb->len);
1687 }
1688
Lijun Pan42557da2021-02-12 20:48:40 -06001689 /* post changes to long_term_buff *dst before VIOS accessing it */
1690 dma_wmb();
1691
Thomas Falcon032c5e82015-12-21 11:26:06 -06001692 tx_pool->consumer_index =
Thomas Falcon06b3e352018-03-16 20:00:28 -05001693 (tx_pool->consumer_index + 1) % tx_pool->num_buffers;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001694
1695 tx_buff = &tx_pool->tx_buff[index];
1696 tx_buff->skb = skb;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001697 tx_buff->index = index;
1698 tx_buff->pool_index = queue_num;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001699
1700 memset(&tx_crq, 0, sizeof(tx_crq));
1701 tx_crq.v1.first = IBMVNIC_CRQ_CMD;
1702 tx_crq.v1.type = IBMVNIC_TX_DESC;
1703 tx_crq.v1.n_crq_elem = 1;
1704 tx_crq.v1.n_sge = 1;
1705 tx_crq.v1.flags1 = IBMVNIC_TX_COMP_NEEDED;
Thomas Falcon06b3e352018-03-16 20:00:28 -05001706
Thomas Falconfdb06102017-10-17 12:36:55 -05001707 if (skb_is_gso(skb))
Thomas Falcon06b3e352018-03-16 20:00:28 -05001708 tx_crq.v1.correlator =
1709 cpu_to_be32(index | IBMVNIC_TSO_POOL_MASK);
Thomas Falconfdb06102017-10-17 12:36:55 -05001710 else
Thomas Falcon06b3e352018-03-16 20:00:28 -05001711 tx_crq.v1.correlator = cpu_to_be32(index);
1712 tx_crq.v1.dma_reg = cpu_to_be16(tx_pool->long_term_buff.map_id);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001713 tx_crq.v1.sge_len = cpu_to_be32(skb->len);
1714 tx_crq.v1.ioba = cpu_to_be64(data_dma_addr);
1715
Michał Mirosławe84b4792018-11-07 17:50:52 +01001716 if (adapter->vlan_header_insertion && skb_vlan_tag_present(skb)) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06001717 tx_crq.v1.flags2 |= IBMVNIC_TX_VLAN_INSERT;
1718 tx_crq.v1.vlan_id = cpu_to_be16(skb->vlan_tci);
1719 }
1720
1721 if (skb->protocol == htons(ETH_P_IP)) {
Thomas Falcona0dca102018-01-18 19:29:48 -06001722 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV4;
1723 proto = ip_hdr(skb)->protocol;
1724 } else if (skb->protocol == htons(ETH_P_IPV6)) {
1725 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV6;
1726 proto = ipv6_hdr(skb)->nexthdr;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001727 }
1728
Thomas Falcona0dca102018-01-18 19:29:48 -06001729 if (proto == IPPROTO_TCP)
1730 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_TCP;
1731 else if (proto == IPPROTO_UDP)
1732 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_UDP;
1733
Thomas Falconad7775d2016-04-01 17:20:34 -05001734 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06001735 tx_crq.v1.flags1 |= IBMVNIC_TX_CHKSUM_OFFLOAD;
Thomas Falconad7775d2016-04-01 17:20:34 -05001736 hdrs += 2;
1737 }
Thomas Falconfdb06102017-10-17 12:36:55 -05001738 if (skb_is_gso(skb)) {
1739 tx_crq.v1.flags1 |= IBMVNIC_TX_LSO;
1740 tx_crq.v1.mss = cpu_to_be16(skb_shinfo(skb)->gso_size);
1741 hdrs += 2;
1742 }
Thomas Falcon0d973382020-11-18 19:12:19 -06001743
1744 if ((*hdrs >> 7) & 1)
Thomas Falconc62aa372020-11-18 19:12:20 -06001745 build_hdr_descs_arr(skb, indir_arr, &num_entries, *hdrs);
Thomas Falcon0d973382020-11-18 19:12:19 -06001746
1747 tx_crq.v1.n_crq_elem = num_entries;
1748 tx_buff->num_entries = num_entries;
1749 /* flush buffer if current entry can not fit */
1750 if (num_entries + ind_bufp->index > IBMVNIC_MAX_IND_DESCS) {
1751 lpar_rc = ibmvnic_tx_scrq_flush(adapter, tx_scrq);
1752 if (lpar_rc != H_SUCCESS)
1753 goto tx_flush_err;
Thomas Falconad7775d2016-04-01 17:20:34 -05001754 }
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001755
Thomas Falconc62aa372020-11-18 19:12:20 -06001756 indir_arr[0] = tx_crq;
1757 memcpy(&ind_bufp->indir_arr[ind_bufp->index], &indir_arr[0],
Thomas Falcon0d973382020-11-18 19:12:19 -06001758 num_entries * sizeof(struct ibmvnic_generic_scrq));
1759 ind_bufp->index += num_entries;
1760 if (__netdev_tx_sent_queue(txq, skb->len,
1761 netdev_xmit_more() &&
1762 ind_bufp->index < IBMVNIC_MAX_IND_DESCS)) {
1763 lpar_rc = ibmvnic_tx_scrq_flush(adapter, tx_scrq);
1764 if (lpar_rc != H_SUCCESS)
1765 goto tx_err;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001766 }
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001767
Thomas Falconffc385b2018-02-18 10:08:41 -06001768 if (atomic_add_return(num_entries, &tx_scrq->used)
Brian King58c8c0c2017-04-19 13:44:47 -04001769 >= adapter->req_tx_entries_per_subcrq) {
Thomas Falcon0aecb132018-02-26 18:10:58 -06001770 netdev_dbg(netdev, "Stopping queue %d\n", queue_num);
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001771 netif_stop_subqueue(netdev, queue_num);
1772 }
1773
Thomas Falcon032c5e82015-12-21 11:26:06 -06001774 tx_packets++;
1775 tx_bytes += skb->len;
1776 txq->trans_start = jiffies;
1777 ret = NETDEV_TX_OK;
Thomas Falcon86b61a52018-03-16 20:00:29 -05001778 goto out;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001779
Thomas Falcon0d973382020-11-18 19:12:19 -06001780tx_flush_err:
1781 dev_kfree_skb_any(skb);
1782 tx_buff->skb = NULL;
1783 tx_pool->consumer_index = tx_pool->consumer_index == 0 ?
1784 tx_pool->num_buffers - 1 :
1785 tx_pool->consumer_index - 1;
1786 tx_dropped++;
1787tx_err:
1788 if (lpar_rc != H_CLOSED && lpar_rc != H_PARAMETER)
1789 dev_err_ratelimited(dev, "tx: send failed\n");
1790
1791 if (lpar_rc == H_CLOSED || adapter->failover_pending) {
1792 /* Disable TX and report carrier off if queue is closed
1793 * or pending failover.
1794 * Firmware guarantees that a signal will be sent to the
1795 * driver, triggering a reset or some other action.
1796 */
1797 netif_tx_stop_all_queues(netdev);
1798 netif_carrier_off(netdev);
1799 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001800out:
1801 netdev->stats.tx_dropped += tx_dropped;
1802 netdev->stats.tx_bytes += tx_bytes;
1803 netdev->stats.tx_packets += tx_packets;
1804 adapter->tx_send_failed += tx_send_failed;
1805 adapter->tx_map_failed += tx_map_failed;
John Allen3d52b592017-08-02 16:44:14 -05001806 adapter->tx_stats_buffers[queue_num].packets += tx_packets;
1807 adapter->tx_stats_buffers[queue_num].bytes += tx_bytes;
1808 adapter->tx_stats_buffers[queue_num].dropped_packets += tx_dropped;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001809
1810 return ret;
1811}
1812
1813static void ibmvnic_set_multi(struct net_device *netdev)
1814{
1815 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1816 struct netdev_hw_addr *ha;
1817 union ibmvnic_crq crq;
1818
1819 memset(&crq, 0, sizeof(crq));
1820 crq.request_capability.first = IBMVNIC_CRQ_CMD;
1821 crq.request_capability.cmd = REQUEST_CAPABILITY;
1822
1823 if (netdev->flags & IFF_PROMISC) {
1824 if (!adapter->promisc_supported)
1825 return;
1826 } else {
1827 if (netdev->flags & IFF_ALLMULTI) {
1828 /* Accept all multicast */
1829 memset(&crq, 0, sizeof(crq));
1830 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1831 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1832 crq.multicast_ctrl.flags = IBMVNIC_ENABLE_ALL;
1833 ibmvnic_send_crq(adapter, &crq);
1834 } else if (netdev_mc_empty(netdev)) {
1835 /* Reject all multicast */
1836 memset(&crq, 0, sizeof(crq));
1837 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1838 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1839 crq.multicast_ctrl.flags = IBMVNIC_DISABLE_ALL;
1840 ibmvnic_send_crq(adapter, &crq);
1841 } else {
1842 /* Accept one or more multicast(s) */
1843 netdev_for_each_mc_addr(ha, netdev) {
1844 memset(&crq, 0, sizeof(crq));
1845 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1846 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1847 crq.multicast_ctrl.flags = IBMVNIC_ENABLE_MC;
1848 ether_addr_copy(&crq.multicast_ctrl.mac_addr[0],
1849 ha->addr);
1850 ibmvnic_send_crq(adapter, &crq);
1851 }
1852 }
1853 }
1854}
1855
Thomas Falcon62740e92019-05-09 23:13:43 -05001856static int __ibmvnic_set_mac(struct net_device *netdev, u8 *dev_addr)
Thomas Falcon032c5e82015-12-21 11:26:06 -06001857{
1858 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001859 union ibmvnic_crq crq;
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05001860 int rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001861
Thomas Falcon62740e92019-05-09 23:13:43 -05001862 if (!is_valid_ether_addr(dev_addr)) {
1863 rc = -EADDRNOTAVAIL;
1864 goto err;
1865 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001866
1867 memset(&crq, 0, sizeof(crq));
1868 crq.change_mac_addr.first = IBMVNIC_CRQ_CMD;
1869 crq.change_mac_addr.cmd = CHANGE_MAC_ADDR;
Thomas Falcon62740e92019-05-09 23:13:43 -05001870 ether_addr_copy(&crq.change_mac_addr.mac_addr[0], dev_addr);
Thomas Falconf8136142018-01-29 13:45:05 -06001871
Thomas Falconff25dcb2019-11-25 17:12:56 -06001872 mutex_lock(&adapter->fw_lock);
1873 adapter->fw_done_rc = 0;
Thomas Falcon070eca92019-11-25 17:12:53 -06001874 reinit_completion(&adapter->fw_done);
Thomas Falconff25dcb2019-11-25 17:12:56 -06001875
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05001876 rc = ibmvnic_send_crq(adapter, &crq);
Thomas Falcon62740e92019-05-09 23:13:43 -05001877 if (rc) {
1878 rc = -EIO;
Thomas Falconff25dcb2019-11-25 17:12:56 -06001879 mutex_unlock(&adapter->fw_lock);
Thomas Falcon62740e92019-05-09 23:13:43 -05001880 goto err;
1881 }
1882
Thomas Falcon476d96c2019-11-25 17:12:55 -06001883 rc = ibmvnic_wait_for_completion(adapter, &adapter->fw_done, 10000);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001884 /* netdev->dev_addr is changed in handle_change_mac_rsp function */
Thomas Falcon476d96c2019-11-25 17:12:55 -06001885 if (rc || adapter->fw_done_rc) {
Thomas Falcon62740e92019-05-09 23:13:43 -05001886 rc = -EIO;
Thomas Falconff25dcb2019-11-25 17:12:56 -06001887 mutex_unlock(&adapter->fw_lock);
Thomas Falcon62740e92019-05-09 23:13:43 -05001888 goto err;
1889 }
Thomas Falconff25dcb2019-11-25 17:12:56 -06001890 mutex_unlock(&adapter->fw_lock);
Thomas Falcon62740e92019-05-09 23:13:43 -05001891 return 0;
1892err:
1893 ether_addr_copy(adapter->mac_addr, netdev->dev_addr);
1894 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001895}
1896
John Allenc26eba02017-10-26 16:23:25 -05001897static int ibmvnic_set_mac(struct net_device *netdev, void *p)
1898{
1899 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1900 struct sockaddr *addr = p;
Thomas Falconf8136142018-01-29 13:45:05 -06001901 int rc;
John Allenc26eba02017-10-26 16:23:25 -05001902
Thomas Falcon62740e92019-05-09 23:13:43 -05001903 rc = 0;
Lijun Pan8fc36722020-10-27 17:04:56 -05001904 if (!is_valid_ether_addr(addr->sa_data))
1905 return -EADDRNOTAVAIL;
1906
Jiri Wiesner67eb2112021-03-04 17:18:28 +01001907 ether_addr_copy(adapter->mac_addr, addr->sa_data);
1908 if (adapter->state != VNIC_PROBED)
Thomas Falcon62740e92019-05-09 23:13:43 -05001909 rc = __ibmvnic_set_mac(netdev, addr->sa_data);
John Allenc26eba02017-10-26 16:23:25 -05001910
Thomas Falconf8136142018-01-29 13:45:05 -06001911 return rc;
John Allenc26eba02017-10-26 16:23:25 -05001912}
1913
Lijun Pancaee7bf2021-04-12 02:41:27 -05001914static const char *reset_reason_to_string(enum ibmvnic_reset_reason reason)
1915{
1916 switch (reason) {
1917 case VNIC_RESET_FAILOVER:
1918 return "FAILOVER";
1919 case VNIC_RESET_MOBILITY:
1920 return "MOBILITY";
1921 case VNIC_RESET_FATAL:
1922 return "FATAL";
1923 case VNIC_RESET_NON_FATAL:
1924 return "NON_FATAL";
1925 case VNIC_RESET_TIMEOUT:
1926 return "TIMEOUT";
1927 case VNIC_RESET_CHANGE_PARAM:
1928 return "CHANGE_PARAM";
1929 default:
1930 return "UNKNOWN";
1931 }
1932}
1933
Lee Jones80708602021-01-15 20:09:03 +00001934/*
Nathan Fontenoted651a12017-05-03 14:04:38 -04001935 * do_reset returns zero if we are able to keep processing reset events, or
1936 * non-zero if we hit a fatal error and must halt.
1937 */
1938static int do_reset(struct ibmvnic_adapter *adapter,
1939 struct ibmvnic_rwi *rwi, u32 reset_state)
1940{
John Allen896d8692018-01-18 16:26:31 -06001941 u64 old_num_rx_queues, old_num_tx_queues;
Thomas Falcon5bf032e2018-11-21 11:17:59 -06001942 u64 old_num_rx_slots, old_num_tx_slots;
Nathan Fontenoted651a12017-05-03 14:04:38 -04001943 struct net_device *netdev = adapter->netdev;
1944 int i, rc;
1945
Sukadev Bhattiprolu38bd5ce2020-12-04 18:22:35 -08001946 netdev_dbg(adapter->netdev,
Lijun Pancaee7bf2021-04-12 02:41:27 -05001947 "[S:%d FOP:%d] Reset reason: %s, reset_state %d\n",
Sukadev Bhattiprolu38bd5ce2020-12-04 18:22:35 -08001948 adapter->state, adapter->failover_pending,
Lijun Pancaee7bf2021-04-12 02:41:27 -05001949 reset_reason_to_string(rwi->reset_reason), reset_state);
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001950
Lijun Pan3f5ec372021-01-06 15:35:14 -06001951 adapter->reset_reason = rwi->reset_reason;
1952 /* requestor of VNIC_RESET_CHANGE_PARAM already has the rtnl lock */
1953 if (!(adapter->reset_reason == VNIC_RESET_CHANGE_PARAM))
1954 rtnl_lock();
1955
Lijun Panbab08be2021-02-11 00:43:19 -06001956 /* Now that we have the rtnl lock, clear any pending failover.
Sukadev Bhattiprolu1d850492020-10-30 10:07:11 -07001957 * This will ensure ibmvnic_open() has either completed or will
1958 * block until failover is complete.
1959 */
1960 if (rwi->reset_reason == VNIC_RESET_FAILOVER)
1961 adapter->failover_pending = false;
Juliet Kimb27507b2019-09-20 16:11:22 -04001962
Sukadev Bhattiprolu8f1c0fd2021-02-23 21:02:29 -08001963 /* read the state and check (again) after getting rtnl */
1964 reset_state = adapter->state;
1965
1966 if (reset_state == VNIC_REMOVING || reset_state == VNIC_REMOVED) {
1967 rc = -EBUSY;
1968 goto out;
1969 }
1970
Nathan Fontenoted651a12017-05-03 14:04:38 -04001971 netif_carrier_off(netdev);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001972
John Allen896d8692018-01-18 16:26:31 -06001973 old_num_rx_queues = adapter->req_rx_queues;
1974 old_num_tx_queues = adapter->req_tx_queues;
Thomas Falcon5bf032e2018-11-21 11:17:59 -06001975 old_num_rx_slots = adapter->req_rx_add_entries_per_subcrq;
1976 old_num_tx_slots = adapter->req_tx_entries_per_subcrq;
John Allen896d8692018-01-18 16:26:31 -06001977
Nathan Fontenot30f79622018-04-06 18:37:06 -05001978 ibmvnic_cleanup(netdev);
1979
Thomas Falcon1f946082019-06-07 16:03:53 -05001980 if (reset_state == VNIC_OPEN &&
1981 adapter->reset_reason != VNIC_RESET_MOBILITY &&
Nathan Fontenot30f79622018-04-06 18:37:06 -05001982 adapter->reset_reason != VNIC_RESET_FAILOVER) {
Lijun Pan3f5ec372021-01-06 15:35:14 -06001983 if (adapter->reset_reason == VNIC_RESET_CHANGE_PARAM) {
1984 rc = __ibmvnic_close(netdev);
1985 if (rc)
1986 goto out;
1987 } else {
1988 adapter->state = VNIC_CLOSING;
Nathan Fontenoted651a12017-05-03 14:04:38 -04001989
Lijun Pan3f5ec372021-01-06 15:35:14 -06001990 /* Release the RTNL lock before link state change and
1991 * re-acquire after the link state change to allow
1992 * linkwatch_event to grab the RTNL lock and run during
1993 * a reset.
1994 */
1995 rtnl_unlock();
1996 rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_DN);
1997 rtnl_lock();
1998 if (rc)
1999 goto out;
Juliet Kimb27507b2019-09-20 16:11:22 -04002000
Sukadev Bhattiprolu8f1c0fd2021-02-23 21:02:29 -08002001 if (adapter->state == VNIC_OPEN) {
2002 /* When we dropped rtnl, ibmvnic_open() got
2003 * it and noticed that we are resetting and
2004 * set the adapter state to OPEN. Update our
2005 * new "target" state, and resume the reset
2006 * from VNIC_CLOSING state.
2007 */
2008 netdev_dbg(netdev,
2009 "Open changed state from %d, updating.\n",
2010 reset_state);
2011 reset_state = VNIC_OPEN;
2012 adapter->state = VNIC_CLOSING;
2013 }
2014
Lijun Pan3f5ec372021-01-06 15:35:14 -06002015 if (adapter->state != VNIC_CLOSING) {
Sukadev Bhattiprolu8f1c0fd2021-02-23 21:02:29 -08002016 /* If someone else changed the adapter state
2017 * when we dropped the rtnl, fail the reset
2018 */
Lijun Pan3f5ec372021-01-06 15:35:14 -06002019 rc = -1;
2020 goto out;
2021 }
Lijun Pan3f5ec372021-01-06 15:35:14 -06002022 adapter->state = VNIC_CLOSED;
Juliet Kimb27507b2019-09-20 16:11:22 -04002023 }
Lijun Pan3f5ec372021-01-06 15:35:14 -06002024 }
Juliet Kimb27507b2019-09-20 16:11:22 -04002025
Lijun Pan3f5ec372021-01-06 15:35:14 -06002026 if (adapter->reset_reason == VNIC_RESET_CHANGE_PARAM) {
2027 release_resources(adapter);
2028 release_sub_crqs(adapter, 1);
2029 release_crq_queue(adapter);
John Allenc26eba02017-10-26 16:23:25 -05002030 }
2031
John Allen8cb31cf2017-05-26 10:30:37 -04002032 if (adapter->reset_reason != VNIC_RESET_NON_FATAL) {
2033 /* remove the closed state so when we call open it appears
2034 * we are coming from the probed state.
2035 */
Nathan Fontenoted651a12017-05-03 14:04:38 -04002036 adapter->state = VNIC_PROBED;
John Allen8cb31cf2017-05-26 10:30:37 -04002037
Lijun Pan3f5ec372021-01-06 15:35:14 -06002038 if (adapter->reset_reason == VNIC_RESET_CHANGE_PARAM) {
2039 rc = init_crq_queue(adapter);
2040 } else if (adapter->reset_reason == VNIC_RESET_MOBILITY) {
Nathan Fontenot30f79622018-04-06 18:37:06 -05002041 rc = ibmvnic_reenable_crq_queue(adapter);
2042 release_sub_crqs(adapter, 1);
2043 } else {
2044 rc = ibmvnic_reset_crq(adapter);
Dany Madden8b40eb732020-06-18 15:24:13 -04002045 if (rc == H_CLOSED || rc == H_SUCCESS) {
Nathan Fontenot30f79622018-04-06 18:37:06 -05002046 rc = vio_enable_interrupts(adapter->vdev);
Dany Madden8b40eb732020-06-18 15:24:13 -04002047 if (rc)
2048 netdev_err(adapter->netdev,
2049 "Reset failed to enable interrupts. rc=%d\n",
2050 rc);
2051 }
Nathan Fontenot30f79622018-04-06 18:37:06 -05002052 }
2053
2054 if (rc) {
2055 netdev_err(adapter->netdev,
Dany Madden8b40eb732020-06-18 15:24:13 -04002056 "Reset couldn't initialize crq. rc=%d\n", rc);
Juliet Kimb27507b2019-09-20 16:11:22 -04002057 goto out;
Nathan Fontenot30f79622018-04-06 18:37:06 -05002058 }
2059
Lijun Pan635e4422020-08-19 17:52:26 -05002060 rc = ibmvnic_reset_init(adapter, true);
Juliet Kimb27507b2019-09-20 16:11:22 -04002061 if (rc) {
2062 rc = IBMVNIC_INIT_FAILED;
2063 goto out;
2064 }
John Allen8cb31cf2017-05-26 10:30:37 -04002065
2066 /* If the adapter was in PROBE state prior to the reset,
2067 * exit here.
2068 */
Juliet Kimb27507b2019-09-20 16:11:22 -04002069 if (reset_state == VNIC_PROBED) {
2070 rc = 0;
2071 goto out;
2072 }
John Allen8cb31cf2017-05-26 10:30:37 -04002073
2074 rc = ibmvnic_login(netdev);
Lijun Panf78afaa2021-02-11 00:43:20 -06002075 if (rc)
Juliet Kimb27507b2019-09-20 16:11:22 -04002076 goto out;
John Allen8cb31cf2017-05-26 10:30:37 -04002077
Lijun Pan3f5ec372021-01-06 15:35:14 -06002078 if (adapter->reset_reason == VNIC_RESET_CHANGE_PARAM) {
2079 rc = init_resources(adapter);
2080 if (rc)
2081 goto out;
2082 } else if (adapter->req_rx_queues != old_num_rx_queues ||
Juliet Kimb27507b2019-09-20 16:11:22 -04002083 adapter->req_tx_queues != old_num_tx_queues ||
2084 adapter->req_rx_add_entries_per_subcrq !=
2085 old_num_rx_slots ||
2086 adapter->req_tx_entries_per_subcrq !=
Mingming Cao9f134572020-08-25 13:26:41 -04002087 old_num_tx_slots ||
2088 !adapter->rx_pool ||
2089 !adapter->tso_pool ||
2090 !adapter->tx_pool) {
John Allen896d8692018-01-18 16:26:31 -06002091 release_rx_pools(adapter);
2092 release_tx_pools(adapter);
Juliet Kima5681e22018-11-19 15:59:22 -06002093 release_napi(adapter);
2094 release_vpd_data(adapter);
2095
2096 rc = init_resources(adapter);
Thomas Falconf611a5b2018-08-30 13:19:53 -05002097 if (rc)
Juliet Kimb27507b2019-09-20 16:11:22 -04002098 goto out;
Nathan Fontenotd9043c12018-02-19 13:30:14 -06002099
John Allenc26eba02017-10-26 16:23:25 -05002100 } else {
2101 rc = reset_tx_pools(adapter);
Jakub Kicinski8ae4dff2020-09-04 21:07:49 -07002102 if (rc) {
Mingming Cao9f134572020-08-25 13:26:41 -04002103 netdev_dbg(adapter->netdev, "reset tx pools failed (%d)\n",
Lijun Pan91dc5d22021-02-11 00:43:22 -06002104 rc);
Juliet Kimb27507b2019-09-20 16:11:22 -04002105 goto out;
Jakub Kicinski8ae4dff2020-09-04 21:07:49 -07002106 }
Nathan Fontenot8c0543a2017-05-26 10:31:06 -04002107
John Allenc26eba02017-10-26 16:23:25 -05002108 rc = reset_rx_pools(adapter);
Jakub Kicinski8ae4dff2020-09-04 21:07:49 -07002109 if (rc) {
Mingming Cao9f134572020-08-25 13:26:41 -04002110 netdev_dbg(adapter->netdev, "reset rx pools failed (%d)\n",
Lijun Pan91dc5d22021-02-11 00:43:22 -06002111 rc);
Juliet Kimb27507b2019-09-20 16:11:22 -04002112 goto out;
Jakub Kicinski8ae4dff2020-09-04 21:07:49 -07002113 }
John Allenc26eba02017-10-26 16:23:25 -05002114 }
Thomas Falcon134bbe72018-05-16 15:49:04 -05002115 ibmvnic_disable_irqs(adapter);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002116 }
John Allene676d812018-03-14 10:41:29 -05002117 adapter->state = VNIC_CLOSED;
2118
Juliet Kimb27507b2019-09-20 16:11:22 -04002119 if (reset_state == VNIC_CLOSED) {
2120 rc = 0;
2121 goto out;
2122 }
John Allene676d812018-03-14 10:41:29 -05002123
Nathan Fontenoted651a12017-05-03 14:04:38 -04002124 rc = __ibmvnic_open(netdev);
2125 if (rc) {
Juliet Kimb27507b2019-09-20 16:11:22 -04002126 rc = IBMVNIC_OPEN_FAILED;
2127 goto out;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002128 }
2129
Thomas Falconbe32a242019-06-07 16:03:54 -05002130 /* refresh device's multicast list */
2131 ibmvnic_set_multi(netdev);
2132
Nathan Fontenoted651a12017-05-03 14:04:38 -04002133 /* kick napi */
2134 for (i = 0; i < adapter->req_rx_queues; i++)
2135 napi_schedule(&adapter->napi[i]);
2136
Lijun Pan98025bc2020-11-20 16:40:12 -06002137 if (adapter->reset_reason == VNIC_RESET_FAILOVER ||
Lijun Pan6be46662020-12-14 15:19:29 -06002138 adapter->reset_reason == VNIC_RESET_MOBILITY)
2139 __netdev_notify_peers(netdev);
Nathan Fontenot61d3e1d2017-06-12 20:47:45 -04002140
Juliet Kimb27507b2019-09-20 16:11:22 -04002141 rc = 0;
2142
2143out:
Dany Madden0cb4bc62020-11-25 18:04:27 -06002144 /* restore the adapter state if reset failed */
2145 if (rc)
2146 adapter->state = reset_state;
Lijun Pan3f5ec372021-01-06 15:35:14 -06002147 /* requestor of VNIC_RESET_CHANGE_PARAM should still hold the rtnl lock */
2148 if (!(adapter->reset_reason == VNIC_RESET_CHANGE_PARAM))
2149 rtnl_unlock();
Juliet Kimb27507b2019-09-20 16:11:22 -04002150
Sukadev Bhattiprolu38bd5ce2020-12-04 18:22:35 -08002151 netdev_dbg(adapter->netdev, "[S:%d FOP:%d] Reset done, rc %d\n",
2152 adapter->state, adapter->failover_pending, rc);
Juliet Kimb27507b2019-09-20 16:11:22 -04002153 return rc;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002154}
2155
Thomas Falcon2770a792018-05-23 13:38:02 -05002156static int do_hard_reset(struct ibmvnic_adapter *adapter,
2157 struct ibmvnic_rwi *rwi, u32 reset_state)
2158{
2159 struct net_device *netdev = adapter->netdev;
2160 int rc;
2161
Lijun Pancaee7bf2021-04-12 02:41:27 -05002162 netdev_dbg(adapter->netdev, "Hard resetting driver (%s)\n",
2163 reset_reason_to_string(rwi->reset_reason));
Thomas Falcon2770a792018-05-23 13:38:02 -05002164
Sukadev Bhattiprolu8f1c0fd2021-02-23 21:02:29 -08002165 /* read the state and check (again) after getting rtnl */
2166 reset_state = adapter->state;
2167
2168 if (reset_state == VNIC_REMOVING || reset_state == VNIC_REMOVED) {
2169 rc = -EBUSY;
2170 goto out;
2171 }
2172
Thomas Falcon2770a792018-05-23 13:38:02 -05002173 netif_carrier_off(netdev);
2174 adapter->reset_reason = rwi->reset_reason;
2175
2176 ibmvnic_cleanup(netdev);
2177 release_resources(adapter);
2178 release_sub_crqs(adapter, 0);
2179 release_crq_queue(adapter);
2180
2181 /* remove the closed state so when we call open it appears
2182 * we are coming from the probed state.
2183 */
2184 adapter->state = VNIC_PROBED;
2185
Thomas Falconbbd669a2019-04-04 18:58:26 -05002186 reinit_completion(&adapter->init_done);
Thomas Falcon2770a792018-05-23 13:38:02 -05002187 rc = init_crq_queue(adapter);
2188 if (rc) {
2189 netdev_err(adapter->netdev,
2190 "Couldn't initialize crq. rc=%d\n", rc);
Dany Madden0cb4bc62020-11-25 18:04:27 -06002191 goto out;
Thomas Falcon2770a792018-05-23 13:38:02 -05002192 }
2193
Lijun Pan635e4422020-08-19 17:52:26 -05002194 rc = ibmvnic_reset_init(adapter, false);
Thomas Falcon2770a792018-05-23 13:38:02 -05002195 if (rc)
Dany Madden0cb4bc62020-11-25 18:04:27 -06002196 goto out;
Thomas Falcon2770a792018-05-23 13:38:02 -05002197
2198 /* If the adapter was in PROBE state prior to the reset,
2199 * exit here.
2200 */
2201 if (reset_state == VNIC_PROBED)
Dany Madden0cb4bc62020-11-25 18:04:27 -06002202 goto out;
Thomas Falcon2770a792018-05-23 13:38:02 -05002203
2204 rc = ibmvnic_login(netdev);
Dany Madden0cb4bc62020-11-25 18:04:27 -06002205 if (rc)
2206 goto out;
Juliet Kima5681e22018-11-19 15:59:22 -06002207
2208 rc = init_resources(adapter);
Thomas Falcon2770a792018-05-23 13:38:02 -05002209 if (rc)
Dany Madden0cb4bc62020-11-25 18:04:27 -06002210 goto out;
Thomas Falcon2770a792018-05-23 13:38:02 -05002211
2212 ibmvnic_disable_irqs(adapter);
2213 adapter->state = VNIC_CLOSED;
2214
2215 if (reset_state == VNIC_CLOSED)
Dany Madden0cb4bc62020-11-25 18:04:27 -06002216 goto out;
Thomas Falcon2770a792018-05-23 13:38:02 -05002217
2218 rc = __ibmvnic_open(netdev);
Dany Madden0cb4bc62020-11-25 18:04:27 -06002219 if (rc) {
2220 rc = IBMVNIC_OPEN_FAILED;
2221 goto out;
2222 }
Thomas Falcon2770a792018-05-23 13:38:02 -05002223
Lijun Pan6be46662020-12-14 15:19:29 -06002224 __netdev_notify_peers(netdev);
Dany Madden0cb4bc62020-11-25 18:04:27 -06002225out:
2226 /* restore adapter state if reset failed */
2227 if (rc)
2228 adapter->state = reset_state;
Sukadev Bhattiprolu38bd5ce2020-12-04 18:22:35 -08002229 netdev_dbg(adapter->netdev, "[S:%d FOP:%d] Hard reset done, rc %d\n",
2230 adapter->state, adapter->failover_pending, rc);
Dany Madden0cb4bc62020-11-25 18:04:27 -06002231 return rc;
Thomas Falcon2770a792018-05-23 13:38:02 -05002232}
2233
Nathan Fontenoted651a12017-05-03 14:04:38 -04002234static struct ibmvnic_rwi *get_next_rwi(struct ibmvnic_adapter *adapter)
2235{
2236 struct ibmvnic_rwi *rwi;
Thomas Falcon6c5c7482018-12-10 15:22:22 -06002237 unsigned long flags;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002238
Thomas Falcon6c5c7482018-12-10 15:22:22 -06002239 spin_lock_irqsave(&adapter->rwi_lock, flags);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002240
2241 if (!list_empty(&adapter->rwi_list)) {
2242 rwi = list_first_entry(&adapter->rwi_list, struct ibmvnic_rwi,
2243 list);
2244 list_del(&rwi->list);
2245 } else {
2246 rwi = NULL;
2247 }
2248
Thomas Falcon6c5c7482018-12-10 15:22:22 -06002249 spin_unlock_irqrestore(&adapter->rwi_lock, flags);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002250 return rwi;
2251}
2252
Nathan Fontenoted651a12017-05-03 14:04:38 -04002253static void __ibmvnic_reset(struct work_struct *work)
2254{
2255 struct ibmvnic_rwi *rwi;
2256 struct ibmvnic_adapter *adapter;
Juliet Kim7d7195a2020-03-10 09:23:58 -05002257 bool saved_state = false;
2258 unsigned long flags;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002259 u32 reset_state;
John Allenc26eba02017-10-26 16:23:25 -05002260 int rc = 0;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002261
2262 adapter = container_of(work, struct ibmvnic_adapter, ibmvnic_reset);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002263
Juliet Kim7ed5b312019-09-20 16:11:23 -04002264 if (test_and_set_bit_lock(0, &adapter->resetting)) {
2265 schedule_delayed_work(&adapter->ibmvnic_delayed_reset,
2266 IBMVNIC_RESET_DELAY);
2267 return;
2268 }
2269
Nathan Fontenoted651a12017-05-03 14:04:38 -04002270 rwi = get_next_rwi(adapter);
2271 while (rwi) {
Juliet Kim7d7195a2020-03-10 09:23:58 -05002272 spin_lock_irqsave(&adapter->state_lock, flags);
2273
Thomas Falcon36f10312019-08-27 11:10:04 -05002274 if (adapter->state == VNIC_REMOVING ||
Michal Suchanekc8dc5592019-09-09 22:44:51 +02002275 adapter->state == VNIC_REMOVED) {
Juliet Kim7d7195a2020-03-10 09:23:58 -05002276 spin_unlock_irqrestore(&adapter->state_lock, flags);
Juliet Kim1c2977c2019-09-05 17:30:01 -04002277 kfree(rwi);
2278 rc = EBUSY;
2279 break;
2280 }
Thomas Falcon36f10312019-08-27 11:10:04 -05002281
Juliet Kim7d7195a2020-03-10 09:23:58 -05002282 if (!saved_state) {
2283 reset_state = adapter->state;
Juliet Kim7d7195a2020-03-10 09:23:58 -05002284 saved_state = true;
2285 }
2286 spin_unlock_irqrestore(&adapter->state_lock, flags);
2287
Lijun Pan3f5ec372021-01-06 15:35:14 -06002288 if (adapter->force_reset_recovery) {
Lijun Panbab08be2021-02-11 00:43:19 -06002289 /* Since we are doing a hard reset now, clear the
Sukadev Bhattiprolu1d850492020-10-30 10:07:11 -07002290 * failover_pending flag so we don't ignore any
2291 * future MOBILITY or other resets.
2292 */
2293 adapter->failover_pending = false;
2294
Juliet Kimb27507b2019-09-20 16:11:22 -04002295 /* Transport event occurred during previous reset */
2296 if (adapter->wait_for_reset) {
2297 /* Previous was CHANGE_PARAM; caller locked */
2298 adapter->force_reset_recovery = false;
2299 rc = do_hard_reset(adapter, rwi, reset_state);
2300 } else {
2301 rtnl_lock();
2302 adapter->force_reset_recovery = false;
2303 rc = do_hard_reset(adapter, rwi, reset_state);
2304 rtnl_unlock();
2305 }
Sukadev Bhattiproluf15fde92020-11-25 18:04:28 -06002306 if (rc) {
2307 /* give backing device time to settle down */
2308 netdev_dbg(adapter->netdev,
2309 "[S:%d] Hard reset failed, waiting 60 secs\n",
2310 adapter->state);
2311 set_current_state(TASK_UNINTERRUPTIBLE);
2312 schedule_timeout(60 * HZ);
2313 }
Lijun Pan1f45dc22020-12-23 14:49:04 -06002314 } else {
Thomas Falcon2770a792018-05-23 13:38:02 -05002315 rc = do_reset(adapter, rwi, reset_state);
2316 }
Nathan Fontenoted651a12017-05-03 14:04:38 -04002317 kfree(rwi);
Dany Maddena86d5c62020-11-25 18:04:31 -06002318 adapter->last_reset_time = jiffies;
Dany Madden0cb4bc62020-11-25 18:04:27 -06002319
Dany Madden18f141b2020-11-25 18:04:25 -06002320 if (rc)
2321 netdev_dbg(adapter->netdev, "Reset failed, rc=%d\n", rc);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002322
2323 rwi = get_next_rwi(adapter);
Juliet Kim7ed5b312019-09-20 16:11:23 -04002324
2325 if (rwi && (rwi->reset_reason == VNIC_RESET_FAILOVER ||
2326 rwi->reset_reason == VNIC_RESET_MOBILITY))
2327 adapter->force_reset_recovery = true;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002328 }
2329
John Allenc26eba02017-10-26 16:23:25 -05002330 if (adapter->wait_for_reset) {
John Allenc26eba02017-10-26 16:23:25 -05002331 adapter->reset_done_rc = rc;
2332 complete(&adapter->reset_done);
2333 }
2334
Juliet Kim7ed5b312019-09-20 16:11:23 -04002335 clear_bit_unlock(0, &adapter->resetting);
Sukadev Bhattiprolu38bd5ce2020-12-04 18:22:35 -08002336
2337 netdev_dbg(adapter->netdev,
2338 "[S:%d FRR:%d WFR:%d] Done processing resets\n",
2339 adapter->state, adapter->force_reset_recovery,
2340 adapter->wait_for_reset);
Juliet Kim7ed5b312019-09-20 16:11:23 -04002341}
2342
2343static void __ibmvnic_delayed_reset(struct work_struct *work)
2344{
2345 struct ibmvnic_adapter *adapter;
2346
2347 adapter = container_of(work, struct ibmvnic_adapter,
2348 ibmvnic_delayed_reset.work);
2349 __ibmvnic_reset(&adapter->ibmvnic_reset);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002350}
2351
Thomas Falconaf894d22018-04-06 18:37:04 -05002352static int ibmvnic_reset(struct ibmvnic_adapter *adapter,
2353 enum ibmvnic_reset_reason reason)
Nathan Fontenoted651a12017-05-03 14:04:38 -04002354{
Thomas Falcon2770a792018-05-23 13:38:02 -05002355 struct list_head *entry, *tmp_entry;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002356 struct ibmvnic_rwi *rwi, *tmp;
2357 struct net_device *netdev = adapter->netdev;
Thomas Falcon6c5c7482018-12-10 15:22:22 -06002358 unsigned long flags;
Thomas Falconaf894d22018-04-06 18:37:04 -05002359 int ret;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002360
Jakub Kicinskib646acd52021-02-16 22:58:44 -08002361 spin_lock_irqsave(&adapter->rwi_lock, flags);
2362
2363 /* If failover is pending don't schedule any other reset.
Sukadev Bhattiprolu1d850492020-10-30 10:07:11 -07002364 * Instead let the failover complete. If there is already a
2365 * a failover reset scheduled, we will detect and drop the
2366 * duplicate reset when walking the ->rwi_list below.
2367 */
Nathan Fontenoted651a12017-05-03 14:04:38 -04002368 if (adapter->state == VNIC_REMOVING ||
Thomas Falcon5a18e1e2018-04-06 18:37:05 -05002369 adapter->state == VNIC_REMOVED ||
Sukadev Bhattiprolu1d850492020-10-30 10:07:11 -07002370 (adapter->failover_pending && reason != VNIC_RESET_FAILOVER)) {
Thomas Falconaf894d22018-04-06 18:37:04 -05002371 ret = EBUSY;
Thomas Falcon5a18e1e2018-04-06 18:37:05 -05002372 netdev_dbg(netdev, "Adapter removing or pending failover, skipping reset\n");
Thomas Falconaf894d22018-04-06 18:37:04 -05002373 goto err;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002374 }
2375
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04002376 if (adapter->state == VNIC_PROBING) {
2377 netdev_warn(netdev, "Adapter reset during probe\n");
Lijun Pan91dc5d22021-02-11 00:43:22 -06002378 adapter->init_done_rc = EAGAIN;
2379 ret = EAGAIN;
Thomas Falconaf894d22018-04-06 18:37:04 -05002380 goto err;
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04002381 }
2382
Nathan Fontenoted651a12017-05-03 14:04:38 -04002383 list_for_each(entry, &adapter->rwi_list) {
2384 tmp = list_entry(entry, struct ibmvnic_rwi, list);
2385 if (tmp->reset_reason == reason) {
Lijun Pancaee7bf2021-04-12 02:41:27 -05002386 netdev_dbg(netdev, "Skipping matching reset, reason=%s\n",
2387 reset_reason_to_string(reason));
Thomas Falconaf894d22018-04-06 18:37:04 -05002388 ret = EBUSY;
2389 goto err;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002390 }
2391 }
2392
Thomas Falcon1d1bbc32018-12-10 15:22:23 -06002393 rwi = kzalloc(sizeof(*rwi), GFP_ATOMIC);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002394 if (!rwi) {
Thomas Falconaf894d22018-04-06 18:37:04 -05002395 ret = ENOMEM;
2396 goto err;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002397 }
Thomas Falcon2770a792018-05-23 13:38:02 -05002398 /* if we just received a transport event,
2399 * flush reset queue and process this reset
2400 */
2401 if (adapter->force_reset_recovery && !list_empty(&adapter->rwi_list)) {
2402 list_for_each_safe(entry, tmp_entry, &adapter->rwi_list)
2403 list_del(entry);
2404 }
Nathan Fontenoted651a12017-05-03 14:04:38 -04002405 rwi->reset_reason = reason;
2406 list_add_tail(&rwi->list, &adapter->rwi_list);
Lijun Pancaee7bf2021-04-12 02:41:27 -05002407 netdev_dbg(adapter->netdev, "Scheduling reset (reason %s)\n",
2408 reset_reason_to_string(reason));
Nathan Fontenoted651a12017-05-03 14:04:38 -04002409 schedule_work(&adapter->ibmvnic_reset);
Thomas Falconaf894d22018-04-06 18:37:04 -05002410
Sukadev Bhattiprolu4a41c422021-02-12 20:42:50 -08002411 ret = 0;
Thomas Falconaf894d22018-04-06 18:37:04 -05002412err:
Sukadev Bhattiprolu4a41c422021-02-12 20:42:50 -08002413 /* ibmvnic_close() below can block, so drop the lock first */
2414 spin_unlock_irqrestore(&adapter->rwi_lock, flags);
2415
2416 if (ret == ENOMEM)
2417 ibmvnic_close(netdev);
2418
Thomas Falconaf894d22018-04-06 18:37:04 -05002419 return -ret;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002420}
2421
Michael S. Tsirkin0290bd22019-12-10 09:23:51 -05002422static void ibmvnic_tx_timeout(struct net_device *dev, unsigned int txqueue)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002423{
2424 struct ibmvnic_adapter *adapter = netdev_priv(dev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002425
Lijun Pan855a6312020-11-20 16:40:13 -06002426 if (test_bit(0, &adapter->resetting)) {
2427 netdev_err(adapter->netdev,
2428 "Adapter is resetting, skip timeout reset\n");
2429 return;
2430 }
Dany Maddena86d5c62020-11-25 18:04:31 -06002431 /* No queuing up reset until at least 5 seconds (default watchdog val)
2432 * after last reset
2433 */
2434 if (time_before(jiffies, (adapter->last_reset_time + dev->watchdog_timeo))) {
2435 netdev_dbg(dev, "Not yet time to tx timeout.\n");
2436 return;
2437 }
Nathan Fontenoted651a12017-05-03 14:04:38 -04002438 ibmvnic_reset(adapter, VNIC_RESET_TIMEOUT);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002439}
2440
2441static void remove_buff_from_pool(struct ibmvnic_adapter *adapter,
2442 struct ibmvnic_rx_buff *rx_buff)
2443{
2444 struct ibmvnic_rx_pool *pool = &adapter->rx_pool[rx_buff->pool_index];
2445
2446 rx_buff->skb = NULL;
2447
2448 pool->free_map[pool->next_alloc] = (int)(rx_buff - pool->rx_buff);
2449 pool->next_alloc = (pool->next_alloc + 1) % pool->size;
2450
2451 atomic_dec(&pool->available);
2452}
2453
2454static int ibmvnic_poll(struct napi_struct *napi, int budget)
2455{
Dwip N. Banerjeeec20f362020-11-18 19:12:23 -06002456 struct ibmvnic_sub_crq_queue *rx_scrq;
2457 struct ibmvnic_adapter *adapter;
2458 struct net_device *netdev;
2459 int frames_processed;
2460 int scrq_num;
2461
2462 netdev = napi->dev;
2463 adapter = netdev_priv(netdev);
2464 scrq_num = (int)(napi - adapter->napi);
2465 frames_processed = 0;
2466 rx_scrq = adapter->rx_scrq[scrq_num];
Nathan Fontenot152ce472017-05-26 10:30:54 -04002467
Thomas Falcon032c5e82015-12-21 11:26:06 -06002468restart_poll:
2469 while (frames_processed < budget) {
2470 struct sk_buff *skb;
2471 struct ibmvnic_rx_buff *rx_buff;
2472 union sub_crq *next;
2473 u32 length;
2474 u16 offset;
2475 u8 flags = 0;
2476
Juliet Kim7ed5b312019-09-20 16:11:23 -04002477 if (unlikely(test_bit(0, &adapter->resetting) &&
John Allen34686562018-02-06 16:21:49 -06002478 adapter->reset_reason != VNIC_RESET_NON_FATAL)) {
Dwip N. Banerjeeec20f362020-11-18 19:12:23 -06002479 enable_scrq_irq(adapter, rx_scrq);
Thomas Falcon21ecba62017-06-14 23:50:09 -05002480 napi_complete_done(napi, frames_processed);
2481 return frames_processed;
2482 }
2483
Dwip N. Banerjeeec20f362020-11-18 19:12:23 -06002484 if (!pending_scrq(adapter, rx_scrq))
Thomas Falcon032c5e82015-12-21 11:26:06 -06002485 break;
Dwip N. Banerjeeec20f362020-11-18 19:12:23 -06002486 next = ibmvnic_next_scrq(adapter, rx_scrq);
Lijun Pan914789a2021-02-11 00:43:21 -06002487 rx_buff = (struct ibmvnic_rx_buff *)
2488 be64_to_cpu(next->rx_comp.correlator);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002489 /* do error checking */
2490 if (next->rx_comp.rc) {
John Allene1cea2e2017-08-07 15:42:30 -05002491 netdev_dbg(netdev, "rx buffer returned with rc %x\n",
2492 be16_to_cpu(next->rx_comp.rc));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002493 /* free the entry */
2494 next->rx_comp.first = 0;
Thomas Falcon4b9b0f02018-02-13 18:23:42 -06002495 dev_kfree_skb_any(rx_buff->skb);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002496 remove_buff_from_pool(adapter, rx_buff);
Nathan Fontenotca05e312017-05-03 14:05:14 -04002497 continue;
Thomas Falconabe27a82018-02-19 20:12:57 -06002498 } else if (!rx_buff->skb) {
2499 /* free the entry */
2500 next->rx_comp.first = 0;
2501 remove_buff_from_pool(adapter, rx_buff);
2502 continue;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002503 }
2504
2505 length = be32_to_cpu(next->rx_comp.len);
2506 offset = be16_to_cpu(next->rx_comp.off_frame_data);
2507 flags = next->rx_comp.flags;
2508 skb = rx_buff->skb;
Lijun Pan42557da2021-02-12 20:48:40 -06002509 /* load long_term_buff before copying to skb */
2510 dma_rmb();
Thomas Falcon032c5e82015-12-21 11:26:06 -06002511 skb_copy_to_linear_data(skb, rx_buff->data + offset,
2512 length);
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04002513
2514 /* VLAN Header has been stripped by the system firmware and
2515 * needs to be inserted by the driver
2516 */
2517 if (adapter->rx_vlan_header_insertion &&
2518 (flags & IBMVNIC_VLAN_STRIPPED))
2519 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
2520 ntohs(next->rx_comp.vlan_tci));
2521
Thomas Falcon032c5e82015-12-21 11:26:06 -06002522 /* free the entry */
2523 next->rx_comp.first = 0;
2524 remove_buff_from_pool(adapter, rx_buff);
2525
2526 skb_put(skb, length);
2527 skb->protocol = eth_type_trans(skb, netdev);
Thomas Falcon94ca3052017-05-03 14:05:20 -04002528 skb_record_rx_queue(skb, scrq_num);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002529
2530 if (flags & IBMVNIC_IP_CHKSUM_GOOD &&
2531 flags & IBMVNIC_TCP_UDP_CHKSUM_GOOD) {
2532 skb->ip_summed = CHECKSUM_UNNECESSARY;
2533 }
2534
2535 length = skb->len;
2536 napi_gro_receive(napi, skb); /* send it up */
2537 netdev->stats.rx_packets++;
2538 netdev->stats.rx_bytes += length;
John Allen3d52b592017-08-02 16:44:14 -05002539 adapter->rx_stats_buffers[scrq_num].packets++;
2540 adapter->rx_stats_buffers[scrq_num].bytes += length;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002541 frames_processed++;
2542 }
Nathan Fontenot152ce472017-05-26 10:30:54 -04002543
Dwip N. Banerjee41ed0a02020-11-18 19:12:25 -06002544 if (adapter->state != VNIC_CLOSING &&
2545 ((atomic_read(&adapter->rx_pool[scrq_num].available) <
2546 adapter->req_rx_add_entries_per_subcrq / 2) ||
2547 frames_processed < budget))
Nathan Fontenot152ce472017-05-26 10:30:54 -04002548 replenish_rx_pool(adapter, &adapter->rx_pool[scrq_num]);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002549 if (frames_processed < budget) {
Dwip N. Banerjeeec20f362020-11-18 19:12:23 -06002550 if (napi_complete_done(napi, frames_processed)) {
2551 enable_scrq_irq(adapter, rx_scrq);
2552 if (pending_scrq(adapter, rx_scrq)) {
Dwip N. Banerjeeec20f362020-11-18 19:12:23 -06002553 if (napi_reschedule(napi)) {
2554 disable_scrq_irq(adapter, rx_scrq);
2555 goto restart_poll;
2556 }
2557 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002558 }
2559 }
2560 return frames_processed;
2561}
2562
John Allenc26eba02017-10-26 16:23:25 -05002563static int wait_for_reset(struct ibmvnic_adapter *adapter)
2564{
Thomas Falconaf894d22018-04-06 18:37:04 -05002565 int rc, ret;
2566
John Allenc26eba02017-10-26 16:23:25 -05002567 adapter->fallback.mtu = adapter->req_mtu;
2568 adapter->fallback.rx_queues = adapter->req_rx_queues;
2569 adapter->fallback.tx_queues = adapter->req_tx_queues;
2570 adapter->fallback.rx_entries = adapter->req_rx_add_entries_per_subcrq;
2571 adapter->fallback.tx_entries = adapter->req_tx_entries_per_subcrq;
2572
Thomas Falcon070eca92019-11-25 17:12:53 -06002573 reinit_completion(&adapter->reset_done);
John Allenc26eba02017-10-26 16:23:25 -05002574 adapter->wait_for_reset = true;
Thomas Falconaf894d22018-04-06 18:37:04 -05002575 rc = ibmvnic_reset(adapter, VNIC_RESET_CHANGE_PARAM);
Thomas Falcon476d96c2019-11-25 17:12:55 -06002576
2577 if (rc) {
2578 ret = rc;
2579 goto out;
2580 }
2581 rc = ibmvnic_wait_for_completion(adapter, &adapter->reset_done, 60000);
2582 if (rc) {
2583 ret = -ENODEV;
2584 goto out;
2585 }
John Allenc26eba02017-10-26 16:23:25 -05002586
Thomas Falconaf894d22018-04-06 18:37:04 -05002587 ret = 0;
John Allenc26eba02017-10-26 16:23:25 -05002588 if (adapter->reset_done_rc) {
Thomas Falconaf894d22018-04-06 18:37:04 -05002589 ret = -EIO;
John Allenc26eba02017-10-26 16:23:25 -05002590 adapter->desired.mtu = adapter->fallback.mtu;
2591 adapter->desired.rx_queues = adapter->fallback.rx_queues;
2592 adapter->desired.tx_queues = adapter->fallback.tx_queues;
2593 adapter->desired.rx_entries = adapter->fallback.rx_entries;
2594 adapter->desired.tx_entries = adapter->fallback.tx_entries;
2595
Thomas Falcon070eca92019-11-25 17:12:53 -06002596 reinit_completion(&adapter->reset_done);
Thomas Falconaf894d22018-04-06 18:37:04 -05002597 adapter->wait_for_reset = true;
2598 rc = ibmvnic_reset(adapter, VNIC_RESET_CHANGE_PARAM);
Thomas Falcon476d96c2019-11-25 17:12:55 -06002599 if (rc) {
2600 ret = rc;
2601 goto out;
2602 }
2603 rc = ibmvnic_wait_for_completion(adapter, &adapter->reset_done,
2604 60000);
2605 if (rc) {
2606 ret = -ENODEV;
2607 goto out;
2608 }
John Allenc26eba02017-10-26 16:23:25 -05002609 }
Thomas Falcon476d96c2019-11-25 17:12:55 -06002610out:
John Allenc26eba02017-10-26 16:23:25 -05002611 adapter->wait_for_reset = false;
2612
Thomas Falconaf894d22018-04-06 18:37:04 -05002613 return ret;
John Allenc26eba02017-10-26 16:23:25 -05002614}
2615
John Allen3a807b72017-06-06 16:55:52 -05002616static int ibmvnic_change_mtu(struct net_device *netdev, int new_mtu)
2617{
John Allenc26eba02017-10-26 16:23:25 -05002618 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2619
2620 adapter->desired.mtu = new_mtu + ETH_HLEN;
2621
2622 return wait_for_reset(adapter);
John Allen3a807b72017-06-06 16:55:52 -05002623}
2624
Thomas Falconf10b09e2018-03-12 11:51:05 -05002625static netdev_features_t ibmvnic_features_check(struct sk_buff *skb,
2626 struct net_device *dev,
2627 netdev_features_t features)
2628{
2629 /* Some backing hardware adapters can not
2630 * handle packets with a MSS less than 224
2631 * or with only one segment.
2632 */
2633 if (skb_is_gso(skb)) {
2634 if (skb_shinfo(skb)->gso_size < 224 ||
2635 skb_shinfo(skb)->gso_segs == 1)
2636 features &= ~NETIF_F_GSO_MASK;
2637 }
2638
2639 return features;
2640}
2641
Thomas Falcon032c5e82015-12-21 11:26:06 -06002642static const struct net_device_ops ibmvnic_netdev_ops = {
2643 .ndo_open = ibmvnic_open,
2644 .ndo_stop = ibmvnic_close,
2645 .ndo_start_xmit = ibmvnic_xmit,
2646 .ndo_set_rx_mode = ibmvnic_set_multi,
2647 .ndo_set_mac_address = ibmvnic_set_mac,
2648 .ndo_validate_addr = eth_validate_addr,
Thomas Falcon032c5e82015-12-21 11:26:06 -06002649 .ndo_tx_timeout = ibmvnic_tx_timeout,
John Allen3a807b72017-06-06 16:55:52 -05002650 .ndo_change_mtu = ibmvnic_change_mtu,
Thomas Falconf10b09e2018-03-12 11:51:05 -05002651 .ndo_features_check = ibmvnic_features_check,
Thomas Falcon032c5e82015-12-21 11:26:06 -06002652};
2653
2654/* ethtool functions */
2655
Philippe Reynes8a433792017-01-07 22:37:29 +01002656static int ibmvnic_get_link_ksettings(struct net_device *netdev,
2657 struct ethtool_link_ksettings *cmd)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002658{
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03002659 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2660 int rc;
Philippe Reynes8a433792017-01-07 22:37:29 +01002661
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03002662 rc = send_query_phys_parms(adapter);
2663 if (rc) {
2664 adapter->speed = SPEED_UNKNOWN;
2665 adapter->duplex = DUPLEX_UNKNOWN;
2666 }
2667 cmd->base.speed = adapter->speed;
2668 cmd->base.duplex = adapter->duplex;
Philippe Reynes8a433792017-01-07 22:37:29 +01002669 cmd->base.port = PORT_FIBRE;
2670 cmd->base.phy_address = 0;
2671 cmd->base.autoneg = AUTONEG_ENABLE;
2672
Thomas Falcon032c5e82015-12-21 11:26:06 -06002673 return 0;
2674}
2675
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02002676static void ibmvnic_get_drvinfo(struct net_device *netdev,
Thomas Falcon032c5e82015-12-21 11:26:06 -06002677 struct ethtool_drvinfo *info)
2678{
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02002679 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2680
Lijun Pan8a96c802021-02-11 00:43:25 -06002681 strscpy(info->driver, ibmvnic_driver_name, sizeof(info->driver));
2682 strscpy(info->version, IBMVNIC_DRIVER_VERSION, sizeof(info->version));
2683 strscpy(info->fw_version, adapter->fw_version,
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02002684 sizeof(info->fw_version));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002685}
2686
2687static u32 ibmvnic_get_msglevel(struct net_device *netdev)
2688{
2689 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2690
2691 return adapter->msg_enable;
2692}
2693
2694static void ibmvnic_set_msglevel(struct net_device *netdev, u32 data)
2695{
2696 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2697
2698 adapter->msg_enable = data;
2699}
2700
2701static u32 ibmvnic_get_link(struct net_device *netdev)
2702{
2703 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2704
2705 /* Don't need to send a query because we request a logical link up at
2706 * init and then we wait for link state indications
2707 */
2708 return adapter->logical_link_state;
2709}
2710
2711static void ibmvnic_get_ringparam(struct net_device *netdev,
2712 struct ethtool_ringparam *ring)
2713{
John Allenbc131b32017-08-02 16:46:30 -05002714 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2715
Thomas Falcon723ad912018-09-28 18:38:26 -05002716 if (adapter->priv_flags & IBMVNIC_USE_SERVER_MAXES) {
2717 ring->rx_max_pending = adapter->max_rx_add_entries_per_subcrq;
2718 ring->tx_max_pending = adapter->max_tx_entries_per_subcrq;
2719 } else {
2720 ring->rx_max_pending = IBMVNIC_MAX_QUEUE_SZ;
2721 ring->tx_max_pending = IBMVNIC_MAX_QUEUE_SZ;
2722 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002723 ring->rx_mini_max_pending = 0;
2724 ring->rx_jumbo_max_pending = 0;
John Allenbc131b32017-08-02 16:46:30 -05002725 ring->rx_pending = adapter->req_rx_add_entries_per_subcrq;
2726 ring->tx_pending = adapter->req_tx_entries_per_subcrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002727 ring->rx_mini_pending = 0;
2728 ring->rx_jumbo_pending = 0;
2729}
2730
John Allenc26eba02017-10-26 16:23:25 -05002731static int ibmvnic_set_ringparam(struct net_device *netdev,
2732 struct ethtool_ringparam *ring)
2733{
2734 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Thomas Falcon723ad912018-09-28 18:38:26 -05002735 int ret;
John Allenc26eba02017-10-26 16:23:25 -05002736
Thomas Falcon723ad912018-09-28 18:38:26 -05002737 ret = 0;
John Allenc26eba02017-10-26 16:23:25 -05002738 adapter->desired.rx_entries = ring->rx_pending;
2739 adapter->desired.tx_entries = ring->tx_pending;
2740
Thomas Falcon723ad912018-09-28 18:38:26 -05002741 ret = wait_for_reset(adapter);
2742
2743 if (!ret &&
2744 (adapter->req_rx_add_entries_per_subcrq != ring->rx_pending ||
2745 adapter->req_tx_entries_per_subcrq != ring->tx_pending))
2746 netdev_info(netdev,
2747 "Could not match full ringsize request. Requested: RX %d, TX %d; Allowed: RX %llu, TX %llu\n",
2748 ring->rx_pending, ring->tx_pending,
2749 adapter->req_rx_add_entries_per_subcrq,
2750 adapter->req_tx_entries_per_subcrq);
2751 return ret;
John Allenc26eba02017-10-26 16:23:25 -05002752}
2753
John Allenc2dbeb62017-08-02 16:47:17 -05002754static void ibmvnic_get_channels(struct net_device *netdev,
2755 struct ethtool_channels *channels)
2756{
2757 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2758
Thomas Falcon723ad912018-09-28 18:38:26 -05002759 if (adapter->priv_flags & IBMVNIC_USE_SERVER_MAXES) {
2760 channels->max_rx = adapter->max_rx_queues;
2761 channels->max_tx = adapter->max_tx_queues;
2762 } else {
2763 channels->max_rx = IBMVNIC_MAX_QUEUES;
2764 channels->max_tx = IBMVNIC_MAX_QUEUES;
2765 }
2766
John Allenc2dbeb62017-08-02 16:47:17 -05002767 channels->max_other = 0;
2768 channels->max_combined = 0;
2769 channels->rx_count = adapter->req_rx_queues;
2770 channels->tx_count = adapter->req_tx_queues;
2771 channels->other_count = 0;
2772 channels->combined_count = 0;
2773}
2774
John Allenc26eba02017-10-26 16:23:25 -05002775static int ibmvnic_set_channels(struct net_device *netdev,
2776 struct ethtool_channels *channels)
2777{
2778 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Thomas Falcon723ad912018-09-28 18:38:26 -05002779 int ret;
John Allenc26eba02017-10-26 16:23:25 -05002780
Thomas Falcon723ad912018-09-28 18:38:26 -05002781 ret = 0;
John Allenc26eba02017-10-26 16:23:25 -05002782 adapter->desired.rx_queues = channels->rx_count;
2783 adapter->desired.tx_queues = channels->tx_count;
2784
Thomas Falcon723ad912018-09-28 18:38:26 -05002785 ret = wait_for_reset(adapter);
2786
2787 if (!ret &&
2788 (adapter->req_rx_queues != channels->rx_count ||
2789 adapter->req_tx_queues != channels->tx_count))
2790 netdev_info(netdev,
2791 "Could not match full channels request. Requested: RX %d, TX %d; Allowed: RX %llu, TX %llu\n",
2792 channels->rx_count, channels->tx_count,
2793 adapter->req_rx_queues, adapter->req_tx_queues);
2794 return ret;
John Allenc26eba02017-10-26 16:23:25 -05002795}
2796
Thomas Falcon032c5e82015-12-21 11:26:06 -06002797static void ibmvnic_get_strings(struct net_device *dev, u32 stringset, u8 *data)
2798{
John Allen3d52b592017-08-02 16:44:14 -05002799 struct ibmvnic_adapter *adapter = netdev_priv(dev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002800 int i;
2801
Thomas Falcon723ad912018-09-28 18:38:26 -05002802 switch (stringset) {
2803 case ETH_SS_STATS:
2804 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats);
2805 i++, data += ETH_GSTRING_LEN)
2806 memcpy(data, ibmvnic_stats[i].name, ETH_GSTRING_LEN);
2807
2808 for (i = 0; i < adapter->req_tx_queues; i++) {
2809 snprintf(data, ETH_GSTRING_LEN, "tx%d_packets", i);
2810 data += ETH_GSTRING_LEN;
2811
2812 snprintf(data, ETH_GSTRING_LEN, "tx%d_bytes", i);
2813 data += ETH_GSTRING_LEN;
2814
2815 snprintf(data, ETH_GSTRING_LEN,
2816 "tx%d_dropped_packets", i);
2817 data += ETH_GSTRING_LEN;
2818 }
2819
2820 for (i = 0; i < adapter->req_rx_queues; i++) {
2821 snprintf(data, ETH_GSTRING_LEN, "rx%d_packets", i);
2822 data += ETH_GSTRING_LEN;
2823
2824 snprintf(data, ETH_GSTRING_LEN, "rx%d_bytes", i);
2825 data += ETH_GSTRING_LEN;
2826
2827 snprintf(data, ETH_GSTRING_LEN, "rx%d_interrupts", i);
2828 data += ETH_GSTRING_LEN;
2829 }
2830 break;
2831
2832 case ETH_SS_PRIV_FLAGS:
2833 for (i = 0; i < ARRAY_SIZE(ibmvnic_priv_flags); i++)
2834 strcpy(data + i * ETH_GSTRING_LEN,
2835 ibmvnic_priv_flags[i]);
2836 break;
2837 default:
Thomas Falcon032c5e82015-12-21 11:26:06 -06002838 return;
John Allen3d52b592017-08-02 16:44:14 -05002839 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002840}
2841
2842static int ibmvnic_get_sset_count(struct net_device *dev, int sset)
2843{
John Allen3d52b592017-08-02 16:44:14 -05002844 struct ibmvnic_adapter *adapter = netdev_priv(dev);
2845
Thomas Falcon032c5e82015-12-21 11:26:06 -06002846 switch (sset) {
2847 case ETH_SS_STATS:
John Allen3d52b592017-08-02 16:44:14 -05002848 return ARRAY_SIZE(ibmvnic_stats) +
2849 adapter->req_tx_queues * NUM_TX_STATS +
2850 adapter->req_rx_queues * NUM_RX_STATS;
Thomas Falcon723ad912018-09-28 18:38:26 -05002851 case ETH_SS_PRIV_FLAGS:
2852 return ARRAY_SIZE(ibmvnic_priv_flags);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002853 default:
2854 return -EOPNOTSUPP;
2855 }
2856}
2857
2858static void ibmvnic_get_ethtool_stats(struct net_device *dev,
2859 struct ethtool_stats *stats, u64 *data)
2860{
2861 struct ibmvnic_adapter *adapter = netdev_priv(dev);
2862 union ibmvnic_crq crq;
John Allen3d52b592017-08-02 16:44:14 -05002863 int i, j;
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05002864 int rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002865
2866 memset(&crq, 0, sizeof(crq));
2867 crq.request_statistics.first = IBMVNIC_CRQ_CMD;
2868 crq.request_statistics.cmd = REQUEST_STATISTICS;
2869 crq.request_statistics.ioba = cpu_to_be32(adapter->stats_token);
2870 crq.request_statistics.len =
2871 cpu_to_be32(sizeof(struct ibmvnic_statistics));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002872
2873 /* Wait for data to be written */
Thomas Falcon070eca92019-11-25 17:12:53 -06002874 reinit_completion(&adapter->stats_done);
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05002875 rc = ibmvnic_send_crq(adapter, &crq);
2876 if (rc)
2877 return;
Thomas Falcon476d96c2019-11-25 17:12:55 -06002878 rc = ibmvnic_wait_for_completion(adapter, &adapter->stats_done, 10000);
2879 if (rc)
2880 return;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002881
2882 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++)
Lijun Pan91dc5d22021-02-11 00:43:22 -06002883 data[i] = be64_to_cpu(IBMVNIC_GET_STAT
2884 (adapter, ibmvnic_stats[i].offset));
John Allen3d52b592017-08-02 16:44:14 -05002885
2886 for (j = 0; j < adapter->req_tx_queues; j++) {
2887 data[i] = adapter->tx_stats_buffers[j].packets;
2888 i++;
2889 data[i] = adapter->tx_stats_buffers[j].bytes;
2890 i++;
2891 data[i] = adapter->tx_stats_buffers[j].dropped_packets;
2892 i++;
2893 }
2894
2895 for (j = 0; j < adapter->req_rx_queues; j++) {
2896 data[i] = adapter->rx_stats_buffers[j].packets;
2897 i++;
2898 data[i] = adapter->rx_stats_buffers[j].bytes;
2899 i++;
2900 data[i] = adapter->rx_stats_buffers[j].interrupts;
2901 i++;
2902 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002903}
2904
Thomas Falcon723ad912018-09-28 18:38:26 -05002905static u32 ibmvnic_get_priv_flags(struct net_device *netdev)
2906{
2907 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2908
2909 return adapter->priv_flags;
2910}
2911
2912static int ibmvnic_set_priv_flags(struct net_device *netdev, u32 flags)
2913{
2914 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2915 bool which_maxes = !!(flags & IBMVNIC_USE_SERVER_MAXES);
2916
2917 if (which_maxes)
2918 adapter->priv_flags |= IBMVNIC_USE_SERVER_MAXES;
2919 else
2920 adapter->priv_flags &= ~IBMVNIC_USE_SERVER_MAXES;
2921
2922 return 0;
2923}
Lijun Pan91dc5d22021-02-11 00:43:22 -06002924
Thomas Falcon032c5e82015-12-21 11:26:06 -06002925static const struct ethtool_ops ibmvnic_ethtool_ops = {
Thomas Falcon032c5e82015-12-21 11:26:06 -06002926 .get_drvinfo = ibmvnic_get_drvinfo,
2927 .get_msglevel = ibmvnic_get_msglevel,
2928 .set_msglevel = ibmvnic_set_msglevel,
2929 .get_link = ibmvnic_get_link,
2930 .get_ringparam = ibmvnic_get_ringparam,
John Allenc26eba02017-10-26 16:23:25 -05002931 .set_ringparam = ibmvnic_set_ringparam,
John Allenc2dbeb62017-08-02 16:47:17 -05002932 .get_channels = ibmvnic_get_channels,
John Allenc26eba02017-10-26 16:23:25 -05002933 .set_channels = ibmvnic_set_channels,
Thomas Falcon032c5e82015-12-21 11:26:06 -06002934 .get_strings = ibmvnic_get_strings,
2935 .get_sset_count = ibmvnic_get_sset_count,
2936 .get_ethtool_stats = ibmvnic_get_ethtool_stats,
Philippe Reynes8a433792017-01-07 22:37:29 +01002937 .get_link_ksettings = ibmvnic_get_link_ksettings,
Thomas Falcon723ad912018-09-28 18:38:26 -05002938 .get_priv_flags = ibmvnic_get_priv_flags,
2939 .set_priv_flags = ibmvnic_set_priv_flags,
Thomas Falcon032c5e82015-12-21 11:26:06 -06002940};
2941
2942/* Routines for managing CRQs/sCRQs */
2943
Nathan Fontenot57a49432017-05-26 10:31:12 -04002944static int reset_one_sub_crq_queue(struct ibmvnic_adapter *adapter,
2945 struct ibmvnic_sub_crq_queue *scrq)
2946{
2947 int rc;
2948
Dany Madden9281cf22020-11-25 18:04:26 -06002949 if (!scrq) {
YANG LI862aecb2020-12-30 15:23:14 +08002950 netdev_dbg(adapter->netdev, "Invalid scrq reset.\n");
Dany Madden9281cf22020-11-25 18:04:26 -06002951 return -EINVAL;
2952 }
2953
Nathan Fontenot57a49432017-05-26 10:31:12 -04002954 if (scrq->irq) {
2955 free_irq(scrq->irq, scrq);
2956 irq_dispose_mapping(scrq->irq);
2957 scrq->irq = 0;
2958 }
2959
Dany Madden9281cf22020-11-25 18:04:26 -06002960 if (scrq->msgs) {
2961 memset(scrq->msgs, 0, 4 * PAGE_SIZE);
2962 atomic_set(&scrq->used, 0);
2963 scrq->cur = 0;
Jakub Kicinski55fd59b2020-12-03 15:42:13 -08002964 scrq->ind_buf.index = 0;
Dany Madden9281cf22020-11-25 18:04:26 -06002965 } else {
2966 netdev_dbg(adapter->netdev, "Invalid scrq reset\n");
2967 return -EINVAL;
2968 }
Nathan Fontenot57a49432017-05-26 10:31:12 -04002969
2970 rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
2971 4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
2972 return rc;
2973}
2974
2975static int reset_sub_crq_queues(struct ibmvnic_adapter *adapter)
2976{
2977 int i, rc;
2978
Lijun Pana0faaa22020-11-23 13:35:45 -06002979 if (!adapter->tx_scrq || !adapter->rx_scrq)
2980 return -EINVAL;
2981
Nathan Fontenot57a49432017-05-26 10:31:12 -04002982 for (i = 0; i < adapter->req_tx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002983 netdev_dbg(adapter->netdev, "Re-setting tx_scrq[%d]\n", i);
Nathan Fontenot57a49432017-05-26 10:31:12 -04002984 rc = reset_one_sub_crq_queue(adapter, adapter->tx_scrq[i]);
2985 if (rc)
2986 return rc;
2987 }
2988
2989 for (i = 0; i < adapter->req_rx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002990 netdev_dbg(adapter->netdev, "Re-setting rx_scrq[%d]\n", i);
Nathan Fontenot57a49432017-05-26 10:31:12 -04002991 rc = reset_one_sub_crq_queue(adapter, adapter->rx_scrq[i]);
2992 if (rc)
2993 return rc;
2994 }
2995
Nathan Fontenot57a49432017-05-26 10:31:12 -04002996 return rc;
2997}
2998
Thomas Falcon032c5e82015-12-21 11:26:06 -06002999static void release_sub_crq_queue(struct ibmvnic_adapter *adapter,
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06003000 struct ibmvnic_sub_crq_queue *scrq,
3001 bool do_h_free)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003002{
3003 struct device *dev = &adapter->vdev->dev;
3004 long rc;
3005
3006 netdev_dbg(adapter->netdev, "Releasing sub-CRQ\n");
3007
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06003008 if (do_h_free) {
3009 /* Close the sub-crqs */
3010 do {
3011 rc = plpar_hcall_norets(H_FREE_SUB_CRQ,
3012 adapter->vdev->unit_address,
3013 scrq->crq_num);
3014 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
Thomas Falcon032c5e82015-12-21 11:26:06 -06003015
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06003016 if (rc) {
3017 netdev_err(adapter->netdev,
3018 "Failed to release sub-CRQ %16lx, rc = %ld\n",
3019 scrq->crq_num, rc);
3020 }
Thomas Falconffa73852017-04-19 13:44:29 -04003021 }
3022
Thomas Falconf019fb62020-11-18 19:12:17 -06003023 dma_free_coherent(dev,
3024 IBMVNIC_IND_ARR_SZ,
3025 scrq->ind_buf.indir_arr,
3026 scrq->ind_buf.indir_dma);
3027
Thomas Falcon032c5e82015-12-21 11:26:06 -06003028 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
3029 DMA_BIDIRECTIONAL);
3030 free_pages((unsigned long)scrq->msgs, 2);
3031 kfree(scrq);
3032}
3033
3034static struct ibmvnic_sub_crq_queue *init_sub_crq_queue(struct ibmvnic_adapter
3035 *adapter)
3036{
3037 struct device *dev = &adapter->vdev->dev;
3038 struct ibmvnic_sub_crq_queue *scrq;
3039 int rc;
3040
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04003041 scrq = kzalloc(sizeof(*scrq), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003042 if (!scrq)
3043 return NULL;
3044
Nathan Fontenot7f7adc52017-04-19 13:45:16 -04003045 scrq->msgs =
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04003046 (union sub_crq *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, 2);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003047 if (!scrq->msgs) {
3048 dev_warn(dev, "Couldn't allocate crq queue messages page\n");
3049 goto zero_page_failed;
3050 }
3051
3052 scrq->msg_token = dma_map_single(dev, scrq->msgs, 4 * PAGE_SIZE,
3053 DMA_BIDIRECTIONAL);
3054 if (dma_mapping_error(dev, scrq->msg_token)) {
3055 dev_warn(dev, "Couldn't map crq queue messages page\n");
3056 goto map_failed;
3057 }
3058
3059 rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
3060 4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
3061
3062 if (rc == H_RESOURCE)
3063 rc = ibmvnic_reset_crq(adapter);
3064
3065 if (rc == H_CLOSED) {
3066 dev_warn(dev, "Partner adapter not ready, waiting.\n");
3067 } else if (rc) {
3068 dev_warn(dev, "Error %d registering sub-crq\n", rc);
3069 goto reg_failed;
3070 }
3071
Thomas Falcon032c5e82015-12-21 11:26:06 -06003072 scrq->adapter = adapter;
3073 scrq->size = 4 * PAGE_SIZE / sizeof(*scrq->msgs);
Thomas Falconf019fb62020-11-18 19:12:17 -06003074 scrq->ind_buf.index = 0;
3075
3076 scrq->ind_buf.indir_arr =
3077 dma_alloc_coherent(dev,
3078 IBMVNIC_IND_ARR_SZ,
3079 &scrq->ind_buf.indir_dma,
3080 GFP_KERNEL);
3081
3082 if (!scrq->ind_buf.indir_arr)
3083 goto indir_failed;
3084
Thomas Falcon032c5e82015-12-21 11:26:06 -06003085 spin_lock_init(&scrq->lock);
3086
3087 netdev_dbg(adapter->netdev,
3088 "sub-crq initialized, num %lx, hw_irq=%lx, irq=%x\n",
3089 scrq->crq_num, scrq->hw_irq, scrq->irq);
3090
3091 return scrq;
3092
Thomas Falconf019fb62020-11-18 19:12:17 -06003093indir_failed:
3094 do {
3095 rc = plpar_hcall_norets(H_FREE_SUB_CRQ,
3096 adapter->vdev->unit_address,
3097 scrq->crq_num);
3098 } while (rc == H_BUSY || rc == H_IS_LONG_BUSY(rc));
Thomas Falcon032c5e82015-12-21 11:26:06 -06003099reg_failed:
3100 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
3101 DMA_BIDIRECTIONAL);
3102map_failed:
3103 free_pages((unsigned long)scrq->msgs, 2);
3104zero_page_failed:
3105 kfree(scrq);
3106
3107 return NULL;
3108}
3109
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06003110static void release_sub_crqs(struct ibmvnic_adapter *adapter, bool do_h_free)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003111{
3112 int i;
3113
3114 if (adapter->tx_scrq) {
Nathan Fontenot82e3be32018-02-21 21:33:56 -06003115 for (i = 0; i < adapter->num_active_tx_scrqs; i++) {
Nathan Fontenotb5108882017-03-30 02:49:18 -04003116 if (!adapter->tx_scrq[i])
3117 continue;
3118
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05003119 netdev_dbg(adapter->netdev, "Releasing tx_scrq[%d]\n",
3120 i);
Nathan Fontenotb5108882017-03-30 02:49:18 -04003121 if (adapter->tx_scrq[i]->irq) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06003122 free_irq(adapter->tx_scrq[i]->irq,
3123 adapter->tx_scrq[i]);
Thomas Falcon88eb98a2016-07-06 15:35:16 -05003124 irq_dispose_mapping(adapter->tx_scrq[i]->irq);
Nathan Fontenotb5108882017-03-30 02:49:18 -04003125 adapter->tx_scrq[i]->irq = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003126 }
Nathan Fontenotb5108882017-03-30 02:49:18 -04003127
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06003128 release_sub_crq_queue(adapter, adapter->tx_scrq[i],
3129 do_h_free);
Nathan Fontenotb5108882017-03-30 02:49:18 -04003130 }
3131
Nathan Fontenot9501df32017-03-15 23:38:07 -04003132 kfree(adapter->tx_scrq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003133 adapter->tx_scrq = NULL;
Nathan Fontenot82e3be32018-02-21 21:33:56 -06003134 adapter->num_active_tx_scrqs = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003135 }
3136
3137 if (adapter->rx_scrq) {
Nathan Fontenot82e3be32018-02-21 21:33:56 -06003138 for (i = 0; i < adapter->num_active_rx_scrqs; i++) {
Nathan Fontenotb5108882017-03-30 02:49:18 -04003139 if (!adapter->rx_scrq[i])
3140 continue;
3141
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05003142 netdev_dbg(adapter->netdev, "Releasing rx_scrq[%d]\n",
3143 i);
Nathan Fontenotb5108882017-03-30 02:49:18 -04003144 if (adapter->rx_scrq[i]->irq) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06003145 free_irq(adapter->rx_scrq[i]->irq,
3146 adapter->rx_scrq[i]);
Thomas Falcon88eb98a2016-07-06 15:35:16 -05003147 irq_dispose_mapping(adapter->rx_scrq[i]->irq);
Nathan Fontenotb5108882017-03-30 02:49:18 -04003148 adapter->rx_scrq[i]->irq = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003149 }
Nathan Fontenotb5108882017-03-30 02:49:18 -04003150
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06003151 release_sub_crq_queue(adapter, adapter->rx_scrq[i],
3152 do_h_free);
Nathan Fontenotb5108882017-03-30 02:49:18 -04003153 }
3154
Nathan Fontenot9501df32017-03-15 23:38:07 -04003155 kfree(adapter->rx_scrq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003156 adapter->rx_scrq = NULL;
Nathan Fontenot82e3be32018-02-21 21:33:56 -06003157 adapter->num_active_rx_scrqs = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003158 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06003159}
3160
3161static int disable_scrq_irq(struct ibmvnic_adapter *adapter,
3162 struct ibmvnic_sub_crq_queue *scrq)
3163{
3164 struct device *dev = &adapter->vdev->dev;
3165 unsigned long rc;
3166
3167 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
3168 H_DISABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
3169 if (rc)
3170 dev_err(dev, "Couldn't disable scrq irq 0x%lx. rc=%ld\n",
3171 scrq->hw_irq, rc);
3172 return rc;
3173}
3174
3175static int enable_scrq_irq(struct ibmvnic_adapter *adapter,
3176 struct ibmvnic_sub_crq_queue *scrq)
3177{
3178 struct device *dev = &adapter->vdev->dev;
3179 unsigned long rc;
3180
3181 if (scrq->hw_irq > 0x100000000ULL) {
3182 dev_err(dev, "bad hw_irq = %lx\n", scrq->hw_irq);
3183 return 1;
3184 }
3185
Juliet Kim7ed5b312019-09-20 16:11:23 -04003186 if (test_bit(0, &adapter->resetting) &&
Nathan Fontenot73f9d362018-05-22 11:21:10 -05003187 adapter->reset_reason == VNIC_RESET_MOBILITY) {
Juliet Kim284f87d2019-11-20 10:50:03 -05003188 u64 val = (0xff000000) | scrq->hw_irq;
Nathan Fontenot73f9d362018-05-22 11:21:10 -05003189
Juliet Kim284f87d2019-11-20 10:50:03 -05003190 rc = plpar_hcall_norets(H_EOI, val);
Juliet Kim2df5c602019-11-20 10:50:04 -05003191 /* H_EOI would fail with rc = H_FUNCTION when running
3192 * in XIVE mode which is expected, but not an error.
3193 */
Lijun Pan91dc5d22021-02-11 00:43:22 -06003194 if (rc && rc != H_FUNCTION)
Juliet Kim284f87d2019-11-20 10:50:03 -05003195 dev_err(dev, "H_EOI FAILED irq 0x%llx. rc=%ld\n",
3196 val, rc);
Nathan Fontenot73f9d362018-05-22 11:21:10 -05003197 }
Thomas Falconf23e0642018-04-15 18:53:36 -05003198
Thomas Falcon032c5e82015-12-21 11:26:06 -06003199 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
3200 H_ENABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
3201 if (rc)
3202 dev_err(dev, "Couldn't enable scrq irq 0x%lx. rc=%ld\n",
3203 scrq->hw_irq, rc);
3204 return rc;
3205}
3206
3207static int ibmvnic_complete_tx(struct ibmvnic_adapter *adapter,
3208 struct ibmvnic_sub_crq_queue *scrq)
3209{
3210 struct device *dev = &adapter->vdev->dev;
Thomas Falcon06b3e352018-03-16 20:00:28 -05003211 struct ibmvnic_tx_pool *tx_pool;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003212 struct ibmvnic_tx_buff *txbuff;
Thomas Falcon0d973382020-11-18 19:12:19 -06003213 struct netdev_queue *txq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003214 union sub_crq *next;
3215 int index;
Thomas Falconc62aa372020-11-18 19:12:20 -06003216 int i;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003217
3218restart_loop:
3219 while (pending_scrq(adapter, scrq)) {
3220 unsigned int pool = scrq->pool_index;
Thomas Falconffc385b2018-02-18 10:08:41 -06003221 int num_entries = 0;
Thomas Falcon0d973382020-11-18 19:12:19 -06003222 int total_bytes = 0;
3223 int num_packets = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003224
3225 next = ibmvnic_next_scrq(adapter, scrq);
3226 for (i = 0; i < next->tx_comp.num_comps; i++) {
Thomas Falconba246c12020-12-01 09:52:11 -06003227 if (next->tx_comp.rcs[i])
Thomas Falcon032c5e82015-12-21 11:26:06 -06003228 dev_err(dev, "tx error %x\n",
3229 next->tx_comp.rcs[i]);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003230 index = be32_to_cpu(next->tx_comp.correlators[i]);
Thomas Falcon06b3e352018-03-16 20:00:28 -05003231 if (index & IBMVNIC_TSO_POOL_MASK) {
3232 tx_pool = &adapter->tso_pool[pool];
3233 index &= ~IBMVNIC_TSO_POOL_MASK;
3234 } else {
3235 tx_pool = &adapter->tx_pool[pool];
3236 }
3237
3238 txbuff = &tx_pool->tx_buff[index];
Thomas Falcon0d973382020-11-18 19:12:19 -06003239 num_packets++;
Thomas Falconffc385b2018-02-18 10:08:41 -06003240 num_entries += txbuff->num_entries;
Thomas Falcon0d973382020-11-18 19:12:19 -06003241 if (txbuff->skb) {
3242 total_bytes += txbuff->skb->len;
3243 dev_consume_skb_irq(txbuff->skb);
3244 txbuff->skb = NULL;
3245 } else {
3246 netdev_warn(adapter->netdev,
3247 "TX completion received with NULL socket buffer\n");
3248 }
Thomas Falcon06b3e352018-03-16 20:00:28 -05003249 tx_pool->free_map[tx_pool->producer_index] = index;
3250 tx_pool->producer_index =
3251 (tx_pool->producer_index + 1) %
3252 tx_pool->num_buffers;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003253 }
3254 /* remove tx_comp scrq*/
3255 next->tx_comp.first = 0;
Nathan Fontenot7c3e7de2017-05-03 14:05:25 -04003256
Thomas Falcon0d973382020-11-18 19:12:19 -06003257 txq = netdev_get_tx_queue(adapter->netdev, scrq->pool_index);
3258 netdev_tx_completed_queue(txq, num_packets, total_bytes);
3259
Thomas Falconffc385b2018-02-18 10:08:41 -06003260 if (atomic_sub_return(num_entries, &scrq->used) <=
Nathan Fontenot7c3e7de2017-05-03 14:05:25 -04003261 (adapter->req_tx_entries_per_subcrq / 2) &&
3262 __netif_subqueue_stopped(adapter->netdev,
3263 scrq->pool_index)) {
3264 netif_wake_subqueue(adapter->netdev, scrq->pool_index);
Thomas Falcon0aecb132018-02-26 18:10:58 -06003265 netdev_dbg(adapter->netdev, "Started queue %d\n",
3266 scrq->pool_index);
Nathan Fontenot7c3e7de2017-05-03 14:05:25 -04003267 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06003268 }
3269
3270 enable_scrq_irq(adapter, scrq);
3271
3272 if (pending_scrq(adapter, scrq)) {
3273 disable_scrq_irq(adapter, scrq);
3274 goto restart_loop;
3275 }
3276
3277 return 0;
3278}
3279
3280static irqreturn_t ibmvnic_interrupt_tx(int irq, void *instance)
3281{
3282 struct ibmvnic_sub_crq_queue *scrq = instance;
3283 struct ibmvnic_adapter *adapter = scrq->adapter;
3284
3285 disable_scrq_irq(adapter, scrq);
3286 ibmvnic_complete_tx(adapter, scrq);
3287
3288 return IRQ_HANDLED;
3289}
3290
3291static irqreturn_t ibmvnic_interrupt_rx(int irq, void *instance)
3292{
3293 struct ibmvnic_sub_crq_queue *scrq = instance;
3294 struct ibmvnic_adapter *adapter = scrq->adapter;
3295
Nathan Fontenot09fb35e2018-01-10 10:40:09 -06003296 /* When booting a kdump kernel we can hit pending interrupts
3297 * prior to completing driver initialization.
3298 */
3299 if (unlikely(adapter->state != VNIC_OPEN))
3300 return IRQ_NONE;
3301
John Allen3d52b592017-08-02 16:44:14 -05003302 adapter->rx_stats_buffers[scrq->scrq_num].interrupts++;
3303
Thomas Falcon032c5e82015-12-21 11:26:06 -06003304 if (napi_schedule_prep(&adapter->napi[scrq->scrq_num])) {
3305 disable_scrq_irq(adapter, scrq);
3306 __napi_schedule(&adapter->napi[scrq->scrq_num]);
3307 }
3308
3309 return IRQ_HANDLED;
3310}
3311
Thomas Falconea22d512016-07-06 15:35:17 -05003312static int init_sub_crq_irqs(struct ibmvnic_adapter *adapter)
3313{
3314 struct device *dev = &adapter->vdev->dev;
3315 struct ibmvnic_sub_crq_queue *scrq;
3316 int i = 0, j = 0;
3317 int rc = 0;
3318
3319 for (i = 0; i < adapter->req_tx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05003320 netdev_dbg(adapter->netdev, "Initializing tx_scrq[%d] irq\n",
3321 i);
Thomas Falconea22d512016-07-06 15:35:17 -05003322 scrq = adapter->tx_scrq[i];
3323 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
3324
Michael Ellerman99c17902016-09-10 19:59:05 +10003325 if (!scrq->irq) {
Thomas Falconea22d512016-07-06 15:35:17 -05003326 rc = -EINVAL;
3327 dev_err(dev, "Error mapping irq\n");
3328 goto req_tx_irq_failed;
3329 }
3330
Murilo Fossa Vicentinie56e2512019-04-25 11:02:33 -03003331 snprintf(scrq->name, sizeof(scrq->name), "ibmvnic-%x-tx%d",
3332 adapter->vdev->unit_address, i);
Thomas Falconea22d512016-07-06 15:35:17 -05003333 rc = request_irq(scrq->irq, ibmvnic_interrupt_tx,
Murilo Fossa Vicentinie56e2512019-04-25 11:02:33 -03003334 0, scrq->name, scrq);
Thomas Falconea22d512016-07-06 15:35:17 -05003335
3336 if (rc) {
3337 dev_err(dev, "Couldn't register tx irq 0x%x. rc=%d\n",
3338 scrq->irq, rc);
3339 irq_dispose_mapping(scrq->irq);
Nathan Fontenotaf9090c2018-02-20 11:04:18 -06003340 goto req_tx_irq_failed;
Thomas Falconea22d512016-07-06 15:35:17 -05003341 }
3342 }
3343
3344 for (i = 0; i < adapter->req_rx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05003345 netdev_dbg(adapter->netdev, "Initializing rx_scrq[%d] irq\n",
3346 i);
Thomas Falconea22d512016-07-06 15:35:17 -05003347 scrq = adapter->rx_scrq[i];
3348 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
Michael Ellerman99c17902016-09-10 19:59:05 +10003349 if (!scrq->irq) {
Thomas Falconea22d512016-07-06 15:35:17 -05003350 rc = -EINVAL;
3351 dev_err(dev, "Error mapping irq\n");
3352 goto req_rx_irq_failed;
3353 }
Murilo Fossa Vicentinie56e2512019-04-25 11:02:33 -03003354 snprintf(scrq->name, sizeof(scrq->name), "ibmvnic-%x-rx%d",
3355 adapter->vdev->unit_address, i);
Thomas Falconea22d512016-07-06 15:35:17 -05003356 rc = request_irq(scrq->irq, ibmvnic_interrupt_rx,
Murilo Fossa Vicentinie56e2512019-04-25 11:02:33 -03003357 0, scrq->name, scrq);
Thomas Falconea22d512016-07-06 15:35:17 -05003358 if (rc) {
3359 dev_err(dev, "Couldn't register rx irq 0x%x. rc=%d\n",
3360 scrq->irq, rc);
3361 irq_dispose_mapping(scrq->irq);
3362 goto req_rx_irq_failed;
3363 }
3364 }
3365 return rc;
3366
3367req_rx_irq_failed:
Thomas Falcon8bf371e2016-10-27 12:28:52 -05003368 for (j = 0; j < i; j++) {
Thomas Falconea22d512016-07-06 15:35:17 -05003369 free_irq(adapter->rx_scrq[j]->irq, adapter->rx_scrq[j]);
3370 irq_dispose_mapping(adapter->rx_scrq[j]->irq);
Thomas Falcon8bf371e2016-10-27 12:28:52 -05003371 }
Thomas Falconea22d512016-07-06 15:35:17 -05003372 i = adapter->req_tx_queues;
3373req_tx_irq_failed:
Thomas Falcon8bf371e2016-10-27 12:28:52 -05003374 for (j = 0; j < i; j++) {
Thomas Falconea22d512016-07-06 15:35:17 -05003375 free_irq(adapter->tx_scrq[j]->irq, adapter->tx_scrq[j]);
Thomas Falcon27a21452020-07-29 16:36:32 -05003376 irq_dispose_mapping(adapter->tx_scrq[j]->irq);
Thomas Falcon8bf371e2016-10-27 12:28:52 -05003377 }
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06003378 release_sub_crqs(adapter, 1);
Thomas Falconea22d512016-07-06 15:35:17 -05003379 return rc;
3380}
3381
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003382static int init_sub_crqs(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003383{
3384 struct device *dev = &adapter->vdev->dev;
3385 struct ibmvnic_sub_crq_queue **allqueues;
3386 int registered_queues = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003387 int total_queues;
3388 int more = 0;
Thomas Falconea22d512016-07-06 15:35:17 -05003389 int i;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003390
Thomas Falcon032c5e82015-12-21 11:26:06 -06003391 total_queues = adapter->req_tx_queues + adapter->req_rx_queues;
3392
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04003393 allqueues = kcalloc(total_queues, sizeof(*allqueues), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003394 if (!allqueues)
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003395 return -1;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003396
3397 for (i = 0; i < total_queues; i++) {
3398 allqueues[i] = init_sub_crq_queue(adapter);
3399 if (!allqueues[i]) {
3400 dev_warn(dev, "Couldn't allocate all sub-crqs\n");
3401 break;
3402 }
3403 registered_queues++;
3404 }
3405
3406 /* Make sure we were able to register the minimum number of queues */
3407 if (registered_queues <
3408 adapter->min_tx_queues + adapter->min_rx_queues) {
3409 dev_err(dev, "Fatal: Couldn't init min number of sub-crqs\n");
3410 goto tx_failed;
3411 }
3412
3413 /* Distribute the failed allocated queues*/
3414 for (i = 0; i < total_queues - registered_queues + more ; i++) {
3415 netdev_dbg(adapter->netdev, "Reducing number of queues\n");
3416 switch (i % 3) {
3417 case 0:
3418 if (adapter->req_rx_queues > adapter->min_rx_queues)
3419 adapter->req_rx_queues--;
3420 else
3421 more++;
3422 break;
3423 case 1:
3424 if (adapter->req_tx_queues > adapter->min_tx_queues)
3425 adapter->req_tx_queues--;
3426 else
3427 more++;
3428 break;
3429 }
3430 }
3431
3432 adapter->tx_scrq = kcalloc(adapter->req_tx_queues,
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04003433 sizeof(*adapter->tx_scrq), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003434 if (!adapter->tx_scrq)
3435 goto tx_failed;
3436
3437 for (i = 0; i < adapter->req_tx_queues; i++) {
3438 adapter->tx_scrq[i] = allqueues[i];
3439 adapter->tx_scrq[i]->pool_index = i;
Nathan Fontenot82e3be32018-02-21 21:33:56 -06003440 adapter->num_active_tx_scrqs++;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003441 }
3442
3443 adapter->rx_scrq = kcalloc(adapter->req_rx_queues,
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04003444 sizeof(*adapter->rx_scrq), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003445 if (!adapter->rx_scrq)
3446 goto rx_failed;
3447
3448 for (i = 0; i < adapter->req_rx_queues; i++) {
3449 adapter->rx_scrq[i] = allqueues[i + adapter->req_tx_queues];
3450 adapter->rx_scrq[i]->scrq_num = i;
Nathan Fontenot82e3be32018-02-21 21:33:56 -06003451 adapter->num_active_rx_scrqs++;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003452 }
3453
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003454 kfree(allqueues);
3455 return 0;
3456
3457rx_failed:
3458 kfree(adapter->tx_scrq);
3459 adapter->tx_scrq = NULL;
3460tx_failed:
3461 for (i = 0; i < registered_queues; i++)
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06003462 release_sub_crq_queue(adapter, allqueues[i], 1);
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003463 kfree(allqueues);
3464 return -1;
3465}
3466
Lijun Pan09081b92020-09-27 20:13:27 -05003467static void send_request_cap(struct ibmvnic_adapter *adapter, int retry)
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003468{
3469 struct device *dev = &adapter->vdev->dev;
3470 union ibmvnic_crq crq;
John Allenc26eba02017-10-26 16:23:25 -05003471 int max_entries;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003472
3473 if (!retry) {
3474 /* Sub-CRQ entries are 32 byte long */
3475 int entries_page = 4 * PAGE_SIZE / (sizeof(u64) * 4);
3476
3477 if (adapter->min_tx_entries_per_subcrq > entries_page ||
3478 adapter->min_rx_add_entries_per_subcrq > entries_page) {
3479 dev_err(dev, "Fatal, invalid entries per sub-crq\n");
3480 return;
3481 }
3482
John Allenc26eba02017-10-26 16:23:25 -05003483 if (adapter->desired.mtu)
3484 adapter->req_mtu = adapter->desired.mtu;
3485 else
3486 adapter->req_mtu = adapter->netdev->mtu + ETH_HLEN;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003487
John Allenc26eba02017-10-26 16:23:25 -05003488 if (!adapter->desired.tx_entries)
3489 adapter->desired.tx_entries =
3490 adapter->max_tx_entries_per_subcrq;
3491 if (!adapter->desired.rx_entries)
3492 adapter->desired.rx_entries =
3493 adapter->max_rx_add_entries_per_subcrq;
3494
3495 max_entries = IBMVNIC_MAX_LTB_SIZE /
3496 (adapter->req_mtu + IBMVNIC_BUFFER_HLEN);
3497
3498 if ((adapter->req_mtu + IBMVNIC_BUFFER_HLEN) *
3499 adapter->desired.tx_entries > IBMVNIC_MAX_LTB_SIZE) {
3500 adapter->desired.tx_entries = max_entries;
3501 }
3502
3503 if ((adapter->req_mtu + IBMVNIC_BUFFER_HLEN) *
3504 adapter->desired.rx_entries > IBMVNIC_MAX_LTB_SIZE) {
3505 adapter->desired.rx_entries = max_entries;
3506 }
3507
3508 if (adapter->desired.tx_entries)
3509 adapter->req_tx_entries_per_subcrq =
3510 adapter->desired.tx_entries;
3511 else
3512 adapter->req_tx_entries_per_subcrq =
3513 adapter->max_tx_entries_per_subcrq;
3514
3515 if (adapter->desired.rx_entries)
3516 adapter->req_rx_add_entries_per_subcrq =
3517 adapter->desired.rx_entries;
3518 else
3519 adapter->req_rx_add_entries_per_subcrq =
3520 adapter->max_rx_add_entries_per_subcrq;
3521
3522 if (adapter->desired.tx_queues)
3523 adapter->req_tx_queues =
3524 adapter->desired.tx_queues;
3525 else
3526 adapter->req_tx_queues =
3527 adapter->opt_tx_comp_sub_queues;
3528
3529 if (adapter->desired.rx_queues)
3530 adapter->req_rx_queues =
3531 adapter->desired.rx_queues;
3532 else
3533 adapter->req_rx_queues =
3534 adapter->opt_rx_comp_queues;
3535
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003536 adapter->req_rx_add_queues = adapter->max_rx_add_queues;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003537 }
3538
Thomas Falcon032c5e82015-12-21 11:26:06 -06003539 memset(&crq, 0, sizeof(crq));
3540 crq.request_capability.first = IBMVNIC_CRQ_CMD;
3541 crq.request_capability.cmd = REQUEST_CAPABILITY;
3542
3543 crq.request_capability.capability = cpu_to_be16(REQ_TX_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06003544 crq.request_capability.number = cpu_to_be64(adapter->req_tx_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06003545 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003546 ibmvnic_send_crq(adapter, &crq);
3547
3548 crq.request_capability.capability = cpu_to_be16(REQ_RX_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06003549 crq.request_capability.number = cpu_to_be64(adapter->req_rx_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06003550 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003551 ibmvnic_send_crq(adapter, &crq);
3552
3553 crq.request_capability.capability = cpu_to_be16(REQ_RX_ADD_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06003554 crq.request_capability.number = cpu_to_be64(adapter->req_rx_add_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06003555 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003556 ibmvnic_send_crq(adapter, &crq);
3557
3558 crq.request_capability.capability =
3559 cpu_to_be16(REQ_TX_ENTRIES_PER_SUBCRQ);
3560 crq.request_capability.number =
Thomas Falconde89e852016-03-01 10:20:09 -06003561 cpu_to_be64(adapter->req_tx_entries_per_subcrq);
Thomas Falcon901e0402017-02-15 12:17:59 -06003562 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003563 ibmvnic_send_crq(adapter, &crq);
3564
3565 crq.request_capability.capability =
3566 cpu_to_be16(REQ_RX_ADD_ENTRIES_PER_SUBCRQ);
3567 crq.request_capability.number =
Thomas Falconde89e852016-03-01 10:20:09 -06003568 cpu_to_be64(adapter->req_rx_add_entries_per_subcrq);
Thomas Falcon901e0402017-02-15 12:17:59 -06003569 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003570 ibmvnic_send_crq(adapter, &crq);
3571
3572 crq.request_capability.capability = cpu_to_be16(REQ_MTU);
Thomas Falconde89e852016-03-01 10:20:09 -06003573 crq.request_capability.number = cpu_to_be64(adapter->req_mtu);
Thomas Falcon901e0402017-02-15 12:17:59 -06003574 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003575 ibmvnic_send_crq(adapter, &crq);
3576
3577 if (adapter->netdev->flags & IFF_PROMISC) {
3578 if (adapter->promisc_supported) {
3579 crq.request_capability.capability =
3580 cpu_to_be16(PROMISC_REQUESTED);
Thomas Falconde89e852016-03-01 10:20:09 -06003581 crq.request_capability.number = cpu_to_be64(1);
Thomas Falcon901e0402017-02-15 12:17:59 -06003582 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003583 ibmvnic_send_crq(adapter, &crq);
3584 }
3585 } else {
3586 crq.request_capability.capability =
3587 cpu_to_be16(PROMISC_REQUESTED);
Thomas Falconde89e852016-03-01 10:20:09 -06003588 crq.request_capability.number = cpu_to_be64(0);
Thomas Falcon901e0402017-02-15 12:17:59 -06003589 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003590 ibmvnic_send_crq(adapter, &crq);
3591 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06003592}
3593
3594static int pending_scrq(struct ibmvnic_adapter *adapter,
3595 struct ibmvnic_sub_crq_queue *scrq)
3596{
3597 union sub_crq *entry = &scrq->msgs[scrq->cur];
Lijun Pan665ab1e2021-01-29 19:19:04 -06003598 int rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003599
Lijun Pan665ab1e2021-01-29 19:19:04 -06003600 rc = !!(entry->generic.first & IBMVNIC_CRQ_CMD_RSP);
3601
3602 /* Ensure that the SCRQ valid flag is loaded prior to loading the
3603 * contents of the SCRQ descriptor
3604 */
3605 dma_rmb();
3606
3607 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003608}
3609
3610static union sub_crq *ibmvnic_next_scrq(struct ibmvnic_adapter *adapter,
3611 struct ibmvnic_sub_crq_queue *scrq)
3612{
3613 union sub_crq *entry;
3614 unsigned long flags;
3615
3616 spin_lock_irqsave(&scrq->lock, flags);
3617 entry = &scrq->msgs[scrq->cur];
3618 if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP) {
3619 if (++scrq->cur == scrq->size)
3620 scrq->cur = 0;
3621 } else {
3622 entry = NULL;
3623 }
3624 spin_unlock_irqrestore(&scrq->lock, flags);
3625
Lijun Pan665ab1e2021-01-29 19:19:04 -06003626 /* Ensure that the SCRQ valid flag is loaded prior to loading the
3627 * contents of the SCRQ descriptor
Thomas Falconb71ec952020-12-01 09:52:10 -06003628 */
3629 dma_rmb();
3630
Thomas Falcon032c5e82015-12-21 11:26:06 -06003631 return entry;
3632}
3633
3634static union ibmvnic_crq *ibmvnic_next_crq(struct ibmvnic_adapter *adapter)
3635{
3636 struct ibmvnic_crq_queue *queue = &adapter->crq;
3637 union ibmvnic_crq *crq;
3638
3639 crq = &queue->msgs[queue->cur];
3640 if (crq->generic.first & IBMVNIC_CRQ_CMD_RSP) {
3641 if (++queue->cur == queue->size)
3642 queue->cur = 0;
3643 } else {
3644 crq = NULL;
3645 }
3646
3647 return crq;
3648}
3649
Thomas Falcon2d14d372018-07-13 12:03:32 -05003650static void print_subcrq_error(struct device *dev, int rc, const char *func)
3651{
3652 switch (rc) {
3653 case H_PARAMETER:
3654 dev_warn_ratelimited(dev,
3655 "%s failed: Send request is malformed or adapter failover pending. (rc=%d)\n",
3656 func, rc);
3657 break;
3658 case H_CLOSED:
3659 dev_warn_ratelimited(dev,
3660 "%s failed: Backing queue closed. Adapter is down or failover pending. (rc=%d)\n",
3661 func, rc);
3662 break;
3663 default:
3664 dev_err_ratelimited(dev, "%s failed: (rc=%d)\n", func, rc);
3665 break;
3666 }
3667}
3668
Thomas Falconad7775d2016-04-01 17:20:34 -05003669static int send_subcrq_indirect(struct ibmvnic_adapter *adapter,
3670 u64 remote_handle, u64 ioba, u64 num_entries)
3671{
3672 unsigned int ua = adapter->vdev->unit_address;
3673 struct device *dev = &adapter->vdev->dev;
3674 int rc;
3675
3676 /* Make sure the hypervisor sees the complete request */
Lijun Pan1a421562021-02-12 20:36:46 -06003677 dma_wmb();
Thomas Falconad7775d2016-04-01 17:20:34 -05003678 rc = plpar_hcall_norets(H_SEND_SUB_CRQ_INDIRECT, ua,
3679 cpu_to_be64(remote_handle),
3680 ioba, num_entries);
3681
Thomas Falcon2d14d372018-07-13 12:03:32 -05003682 if (rc)
3683 print_subcrq_error(dev, rc, __func__);
Thomas Falconad7775d2016-04-01 17:20:34 -05003684
3685 return rc;
3686}
3687
Thomas Falcon032c5e82015-12-21 11:26:06 -06003688static int ibmvnic_send_crq(struct ibmvnic_adapter *adapter,
3689 union ibmvnic_crq *crq)
3690{
3691 unsigned int ua = adapter->vdev->unit_address;
3692 struct device *dev = &adapter->vdev->dev;
3693 u64 *u64_crq = (u64 *)crq;
3694 int rc;
3695
3696 netdev_dbg(adapter->netdev, "Sending CRQ: %016lx %016lx\n",
Lijun Pan429aa362021-02-11 00:43:18 -06003697 (unsigned long)cpu_to_be64(u64_crq[0]),
3698 (unsigned long)cpu_to_be64(u64_crq[1]));
Thomas Falcon032c5e82015-12-21 11:26:06 -06003699
Thomas Falcon51536982018-05-23 13:37:56 -05003700 if (!adapter->crq.active &&
3701 crq->generic.first != IBMVNIC_CRQ_INIT_CMD) {
3702 dev_warn(dev, "Invalid request detected while CRQ is inactive, possible device state change during reset\n");
3703 return -EINVAL;
3704 }
3705
Thomas Falcon032c5e82015-12-21 11:26:06 -06003706 /* Make sure the hypervisor sees the complete request */
Lijun Pan1a421562021-02-12 20:36:46 -06003707 dma_wmb();
Thomas Falcon032c5e82015-12-21 11:26:06 -06003708
3709 rc = plpar_hcall_norets(H_SEND_CRQ, ua,
3710 cpu_to_be64(u64_crq[0]),
3711 cpu_to_be64(u64_crq[1]));
3712
3713 if (rc) {
Nathan Fontenotec95dff2018-02-07 13:00:24 -06003714 if (rc == H_CLOSED) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06003715 dev_warn(dev, "CRQ Queue closed\n");
Lijun Panfa68bfa2020-08-19 17:52:24 -05003716 /* do not reset, report the fail, wait for passive init from server */
Nathan Fontenotec95dff2018-02-07 13:00:24 -06003717 }
3718
Thomas Falcon032c5e82015-12-21 11:26:06 -06003719 dev_warn(dev, "Send error (rc=%d)\n", rc);
3720 }
3721
3722 return rc;
3723}
3724
3725static int ibmvnic_send_crq_init(struct ibmvnic_adapter *adapter)
3726{
Thomas Falcon36a782f2020-08-31 11:59:57 -05003727 struct device *dev = &adapter->vdev->dev;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003728 union ibmvnic_crq crq;
Thomas Falcon36a782f2020-08-31 11:59:57 -05003729 int retries = 100;
3730 int rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003731
3732 memset(&crq, 0, sizeof(crq));
3733 crq.generic.first = IBMVNIC_CRQ_INIT_CMD;
3734 crq.generic.cmd = IBMVNIC_CRQ_INIT;
3735 netdev_dbg(adapter->netdev, "Sending CRQ init\n");
3736
Thomas Falcon36a782f2020-08-31 11:59:57 -05003737 do {
3738 rc = ibmvnic_send_crq(adapter, &crq);
3739 if (rc != H_CLOSED)
3740 break;
3741 retries--;
3742 msleep(50);
3743
3744 } while (retries > 0);
3745
3746 if (rc) {
3747 dev_err(dev, "Failed to send init request, rc = %d\n", rc);
3748 return rc;
3749 }
3750
3751 return 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003752}
3753
Thomas Falcon032c5e82015-12-21 11:26:06 -06003754static int send_version_xchg(struct ibmvnic_adapter *adapter)
3755{
3756 union ibmvnic_crq crq;
3757
3758 memset(&crq, 0, sizeof(crq));
3759 crq.version_exchange.first = IBMVNIC_CRQ_CMD;
3760 crq.version_exchange.cmd = VERSION_EXCHANGE;
3761 crq.version_exchange.version = cpu_to_be16(ibmvnic_version);
3762
3763 return ibmvnic_send_crq(adapter, &crq);
3764}
3765
Nathan Fontenot37798d02017-11-08 11:23:56 -06003766struct vnic_login_client_data {
3767 u8 type;
3768 __be16 len;
Kees Cook08ea5562018-04-10 15:26:43 -07003769 char name[];
Nathan Fontenot37798d02017-11-08 11:23:56 -06003770} __packed;
3771
3772static int vnic_client_data_len(struct ibmvnic_adapter *adapter)
3773{
3774 int len;
3775
3776 /* Calculate the amount of buffer space needed for the
3777 * vnic client data in the login buffer. There are four entries,
3778 * OS name, LPAR name, device name, and a null last entry.
3779 */
3780 len = 4 * sizeof(struct vnic_login_client_data);
3781 len += 6; /* "Linux" plus NULL */
3782 len += strlen(utsname()->nodename) + 1;
3783 len += strlen(adapter->netdev->name) + 1;
3784
3785 return len;
3786}
3787
3788static void vnic_add_client_data(struct ibmvnic_adapter *adapter,
3789 struct vnic_login_client_data *vlcd)
3790{
3791 const char *os_name = "Linux";
3792 int len;
3793
3794 /* Type 1 - LPAR OS */
3795 vlcd->type = 1;
3796 len = strlen(os_name) + 1;
3797 vlcd->len = cpu_to_be16(len);
Kees Cook08ea5562018-04-10 15:26:43 -07003798 strncpy(vlcd->name, os_name, len);
3799 vlcd = (struct vnic_login_client_data *)(vlcd->name + len);
Nathan Fontenot37798d02017-11-08 11:23:56 -06003800
3801 /* Type 2 - LPAR name */
3802 vlcd->type = 2;
3803 len = strlen(utsname()->nodename) + 1;
3804 vlcd->len = cpu_to_be16(len);
Kees Cook08ea5562018-04-10 15:26:43 -07003805 strncpy(vlcd->name, utsname()->nodename, len);
3806 vlcd = (struct vnic_login_client_data *)(vlcd->name + len);
Nathan Fontenot37798d02017-11-08 11:23:56 -06003807
3808 /* Type 3 - device name */
3809 vlcd->type = 3;
3810 len = strlen(adapter->netdev->name) + 1;
3811 vlcd->len = cpu_to_be16(len);
Kees Cook08ea5562018-04-10 15:26:43 -07003812 strncpy(vlcd->name, adapter->netdev->name, len);
Nathan Fontenot37798d02017-11-08 11:23:56 -06003813}
3814
Thomas Falcon20a8ab72018-02-26 18:10:59 -06003815static int send_login(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003816{
3817 struct ibmvnic_login_rsp_buffer *login_rsp_buffer;
3818 struct ibmvnic_login_buffer *login_buffer;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003819 struct device *dev = &adapter->vdev->dev;
Dany Maddenc98d9cc2020-11-25 18:04:30 -06003820 struct vnic_login_client_data *vlcd;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003821 dma_addr_t rsp_buffer_token;
3822 dma_addr_t buffer_token;
3823 size_t rsp_buffer_size;
3824 union ibmvnic_crq crq;
Dany Maddenc98d9cc2020-11-25 18:04:30 -06003825 int client_data_len;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003826 size_t buffer_size;
3827 __be64 *tx_list_p;
3828 __be64 *rx_list_p;
Dany Maddenc98d9cc2020-11-25 18:04:30 -06003829 int rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003830 int i;
3831
Thomas Falcon20a8ab72018-02-26 18:10:59 -06003832 if (!adapter->tx_scrq || !adapter->rx_scrq) {
3833 netdev_err(adapter->netdev,
3834 "RX or TX queues are not allocated, device login failed\n");
3835 return -1;
3836 }
3837
Lijun Pana0c8be52020-12-19 15:39:19 -06003838 release_login_buffer(adapter);
Thomas Falcon34f0f4e2018-02-13 18:23:40 -06003839 release_login_rsp_buffer(adapter);
Lijun Pana0c8be52020-12-19 15:39:19 -06003840
Nathan Fontenot37798d02017-11-08 11:23:56 -06003841 client_data_len = vnic_client_data_len(adapter);
3842
Thomas Falcon032c5e82015-12-21 11:26:06 -06003843 buffer_size =
3844 sizeof(struct ibmvnic_login_buffer) +
Nathan Fontenot37798d02017-11-08 11:23:56 -06003845 sizeof(u64) * (adapter->req_tx_queues + adapter->req_rx_queues) +
3846 client_data_len;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003847
Nathan Fontenot37798d02017-11-08 11:23:56 -06003848 login_buffer = kzalloc(buffer_size, GFP_ATOMIC);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003849 if (!login_buffer)
3850 goto buf_alloc_failed;
3851
3852 buffer_token = dma_map_single(dev, login_buffer, buffer_size,
3853 DMA_TO_DEVICE);
3854 if (dma_mapping_error(dev, buffer_token)) {
3855 dev_err(dev, "Couldn't map login buffer\n");
3856 goto buf_map_failed;
3857 }
3858
John Allen498cd8e2016-04-06 11:49:55 -05003859 rsp_buffer_size = sizeof(struct ibmvnic_login_rsp_buffer) +
3860 sizeof(u64) * adapter->req_tx_queues +
3861 sizeof(u64) * adapter->req_rx_queues +
3862 sizeof(u64) * adapter->req_rx_queues +
3863 sizeof(u8) * IBMVNIC_TX_DESC_VERSIONS;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003864
3865 login_rsp_buffer = kmalloc(rsp_buffer_size, GFP_ATOMIC);
3866 if (!login_rsp_buffer)
3867 goto buf_rsp_alloc_failed;
3868
3869 rsp_buffer_token = dma_map_single(dev, login_rsp_buffer,
3870 rsp_buffer_size, DMA_FROM_DEVICE);
3871 if (dma_mapping_error(dev, rsp_buffer_token)) {
3872 dev_err(dev, "Couldn't map login rsp buffer\n");
3873 goto buf_rsp_map_failed;
3874 }
Nathan Fontenot661a2622017-04-19 13:44:58 -04003875
Thomas Falcon032c5e82015-12-21 11:26:06 -06003876 adapter->login_buf = login_buffer;
3877 adapter->login_buf_token = buffer_token;
3878 adapter->login_buf_sz = buffer_size;
3879 adapter->login_rsp_buf = login_rsp_buffer;
3880 adapter->login_rsp_buf_token = rsp_buffer_token;
3881 adapter->login_rsp_buf_sz = rsp_buffer_size;
3882
3883 login_buffer->len = cpu_to_be32(buffer_size);
3884 login_buffer->version = cpu_to_be32(INITIAL_VERSION_LB);
3885 login_buffer->num_txcomp_subcrqs = cpu_to_be32(adapter->req_tx_queues);
3886 login_buffer->off_txcomp_subcrqs =
3887 cpu_to_be32(sizeof(struct ibmvnic_login_buffer));
3888 login_buffer->num_rxcomp_subcrqs = cpu_to_be32(adapter->req_rx_queues);
3889 login_buffer->off_rxcomp_subcrqs =
3890 cpu_to_be32(sizeof(struct ibmvnic_login_buffer) +
3891 sizeof(u64) * adapter->req_tx_queues);
3892 login_buffer->login_rsp_ioba = cpu_to_be32(rsp_buffer_token);
3893 login_buffer->login_rsp_len = cpu_to_be32(rsp_buffer_size);
3894
3895 tx_list_p = (__be64 *)((char *)login_buffer +
3896 sizeof(struct ibmvnic_login_buffer));
3897 rx_list_p = (__be64 *)((char *)login_buffer +
3898 sizeof(struct ibmvnic_login_buffer) +
3899 sizeof(u64) * adapter->req_tx_queues);
3900
3901 for (i = 0; i < adapter->req_tx_queues; i++) {
3902 if (adapter->tx_scrq[i]) {
Lijun Pan914789a2021-02-11 00:43:21 -06003903 tx_list_p[i] =
3904 cpu_to_be64(adapter->tx_scrq[i]->crq_num);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003905 }
3906 }
3907
3908 for (i = 0; i < adapter->req_rx_queues; i++) {
3909 if (adapter->rx_scrq[i]) {
Lijun Pan914789a2021-02-11 00:43:21 -06003910 rx_list_p[i] =
3911 cpu_to_be64(adapter->rx_scrq[i]->crq_num);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003912 }
3913 }
3914
Nathan Fontenot37798d02017-11-08 11:23:56 -06003915 /* Insert vNIC login client data */
3916 vlcd = (struct vnic_login_client_data *)
3917 ((char *)rx_list_p + (sizeof(u64) * adapter->req_rx_queues));
3918 login_buffer->client_data_offset =
3919 cpu_to_be32((char *)vlcd - (char *)login_buffer);
3920 login_buffer->client_data_len = cpu_to_be32(client_data_len);
3921
3922 vnic_add_client_data(adapter, vlcd);
3923
Thomas Falcon032c5e82015-12-21 11:26:06 -06003924 netdev_dbg(adapter->netdev, "Login Buffer:\n");
3925 for (i = 0; i < (adapter->login_buf_sz - 1) / 8 + 1; i++) {
3926 netdev_dbg(adapter->netdev, "%016lx\n",
Lijun Pan429aa362021-02-11 00:43:18 -06003927 ((unsigned long *)(adapter->login_buf))[i]);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003928 }
3929
3930 memset(&crq, 0, sizeof(crq));
3931 crq.login.first = IBMVNIC_CRQ_CMD;
3932 crq.login.cmd = LOGIN;
3933 crq.login.ioba = cpu_to_be32(buffer_token);
3934 crq.login.len = cpu_to_be32(buffer_size);
Sukadev Bhattiprolu76cdc5c2020-11-25 18:04:29 -06003935
3936 adapter->login_pending = true;
Dany Maddenc98d9cc2020-11-25 18:04:30 -06003937 rc = ibmvnic_send_crq(adapter, &crq);
3938 if (rc) {
3939 adapter->login_pending = false;
3940 netdev_err(adapter->netdev, "Failed to send login, rc=%d\n", rc);
3941 goto buf_rsp_map_failed;
3942 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06003943
Thomas Falcon20a8ab72018-02-26 18:10:59 -06003944 return 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003945
Thomas Falcon032c5e82015-12-21 11:26:06 -06003946buf_rsp_map_failed:
3947 kfree(login_rsp_buffer);
Dany Maddenc98d9cc2020-11-25 18:04:30 -06003948 adapter->login_rsp_buf = NULL;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003949buf_rsp_alloc_failed:
3950 dma_unmap_single(dev, buffer_token, buffer_size, DMA_TO_DEVICE);
3951buf_map_failed:
3952 kfree(login_buffer);
Dany Maddenc98d9cc2020-11-25 18:04:30 -06003953 adapter->login_buf = NULL;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003954buf_alloc_failed:
Thomas Falcon20a8ab72018-02-26 18:10:59 -06003955 return -1;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003956}
3957
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05003958static int send_request_map(struct ibmvnic_adapter *adapter, dma_addr_t addr,
3959 u32 len, u8 map_id)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003960{
3961 union ibmvnic_crq crq;
3962
3963 memset(&crq, 0, sizeof(crq));
3964 crq.request_map.first = IBMVNIC_CRQ_CMD;
3965 crq.request_map.cmd = REQUEST_MAP;
3966 crq.request_map.map_id = map_id;
3967 crq.request_map.ioba = cpu_to_be32(addr);
3968 crq.request_map.len = cpu_to_be32(len);
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05003969 return ibmvnic_send_crq(adapter, &crq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003970}
3971
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05003972static int send_request_unmap(struct ibmvnic_adapter *adapter, u8 map_id)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003973{
3974 union ibmvnic_crq crq;
3975
3976 memset(&crq, 0, sizeof(crq));
3977 crq.request_unmap.first = IBMVNIC_CRQ_CMD;
3978 crq.request_unmap.cmd = REQUEST_UNMAP;
3979 crq.request_unmap.map_id = map_id;
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05003980 return ibmvnic_send_crq(adapter, &crq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003981}
3982
Lijun Pan69980d02020-09-27 20:13:28 -05003983static void send_query_map(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003984{
3985 union ibmvnic_crq crq;
3986
3987 memset(&crq, 0, sizeof(crq));
3988 crq.query_map.first = IBMVNIC_CRQ_CMD;
3989 crq.query_map.cmd = QUERY_MAP;
3990 ibmvnic_send_crq(adapter, &crq);
3991}
3992
3993/* Send a series of CRQs requesting various capabilities of the VNIC server */
Lijun Pan491099a2020-09-27 20:13:26 -05003994static void send_query_cap(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003995{
3996 union ibmvnic_crq crq;
3997
Thomas Falcon901e0402017-02-15 12:17:59 -06003998 atomic_set(&adapter->running_cap_crqs, 0);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003999 memset(&crq, 0, sizeof(crq));
4000 crq.query_capability.first = IBMVNIC_CRQ_CMD;
4001 crq.query_capability.cmd = QUERY_CAPABILITY;
4002
4003 crq.query_capability.capability = cpu_to_be16(MIN_TX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06004004 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004005 ibmvnic_send_crq(adapter, &crq);
4006
4007 crq.query_capability.capability = cpu_to_be16(MIN_RX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06004008 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004009 ibmvnic_send_crq(adapter, &crq);
4010
4011 crq.query_capability.capability = cpu_to_be16(MIN_RX_ADD_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06004012 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004013 ibmvnic_send_crq(adapter, &crq);
4014
4015 crq.query_capability.capability = cpu_to_be16(MAX_TX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06004016 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004017 ibmvnic_send_crq(adapter, &crq);
4018
4019 crq.query_capability.capability = cpu_to_be16(MAX_RX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06004020 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004021 ibmvnic_send_crq(adapter, &crq);
4022
4023 crq.query_capability.capability = cpu_to_be16(MAX_RX_ADD_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06004024 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004025 ibmvnic_send_crq(adapter, &crq);
4026
4027 crq.query_capability.capability =
4028 cpu_to_be16(MIN_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06004029 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004030 ibmvnic_send_crq(adapter, &crq);
4031
4032 crq.query_capability.capability =
4033 cpu_to_be16(MIN_RX_ADD_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06004034 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004035 ibmvnic_send_crq(adapter, &crq);
4036
4037 crq.query_capability.capability =
4038 cpu_to_be16(MAX_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06004039 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004040 ibmvnic_send_crq(adapter, &crq);
4041
4042 crq.query_capability.capability =
4043 cpu_to_be16(MAX_RX_ADD_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06004044 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004045 ibmvnic_send_crq(adapter, &crq);
4046
4047 crq.query_capability.capability = cpu_to_be16(TCP_IP_OFFLOAD);
Thomas Falcon901e0402017-02-15 12:17:59 -06004048 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004049 ibmvnic_send_crq(adapter, &crq);
4050
4051 crq.query_capability.capability = cpu_to_be16(PROMISC_SUPPORTED);
Thomas Falcon901e0402017-02-15 12:17:59 -06004052 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004053 ibmvnic_send_crq(adapter, &crq);
4054
4055 crq.query_capability.capability = cpu_to_be16(MIN_MTU);
Thomas Falcon901e0402017-02-15 12:17:59 -06004056 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004057 ibmvnic_send_crq(adapter, &crq);
4058
4059 crq.query_capability.capability = cpu_to_be16(MAX_MTU);
Thomas Falcon901e0402017-02-15 12:17:59 -06004060 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004061 ibmvnic_send_crq(adapter, &crq);
4062
4063 crq.query_capability.capability = cpu_to_be16(MAX_MULTICAST_FILTERS);
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(VLAN_HEADER_INSERTION);
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
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04004071 crq.query_capability.capability = cpu_to_be16(RX_VLAN_HEADER_INSERTION);
4072 atomic_inc(&adapter->running_cap_crqs);
4073 ibmvnic_send_crq(adapter, &crq);
4074
Thomas Falcon032c5e82015-12-21 11:26:06 -06004075 crq.query_capability.capability = cpu_to_be16(MAX_TX_SG_ENTRIES);
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(RX_SG_SUPPORTED);
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(OPT_TX_COMP_SUB_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06004084 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004085 ibmvnic_send_crq(adapter, &crq);
4086
4087 crq.query_capability.capability = cpu_to_be16(OPT_RX_COMP_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06004088 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004089 ibmvnic_send_crq(adapter, &crq);
4090
4091 crq.query_capability.capability =
4092 cpu_to_be16(OPT_RX_BUFADD_Q_PER_RX_COMP_Q);
Thomas Falcon901e0402017-02-15 12:17:59 -06004093 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004094 ibmvnic_send_crq(adapter, &crq);
4095
4096 crq.query_capability.capability =
4097 cpu_to_be16(OPT_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06004098 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004099 ibmvnic_send_crq(adapter, &crq);
4100
4101 crq.query_capability.capability =
4102 cpu_to_be16(OPT_RXBA_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06004103 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004104 ibmvnic_send_crq(adapter, &crq);
4105
4106 crq.query_capability.capability = cpu_to_be16(TX_RX_DESC_REQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06004107 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004108 ibmvnic_send_crq(adapter, &crq);
4109}
4110
Lijun Pan16e811f2020-09-27 20:13:29 -05004111static void send_query_ip_offload(struct ibmvnic_adapter *adapter)
4112{
4113 int buf_sz = sizeof(struct ibmvnic_query_ip_offload_buffer);
4114 struct device *dev = &adapter->vdev->dev;
4115 union ibmvnic_crq crq;
4116
4117 adapter->ip_offload_tok =
4118 dma_map_single(dev,
4119 &adapter->ip_offload_buf,
4120 buf_sz,
4121 DMA_FROM_DEVICE);
4122
4123 if (dma_mapping_error(dev, adapter->ip_offload_tok)) {
4124 if (!firmware_has_feature(FW_FEATURE_CMO))
4125 dev_err(dev, "Couldn't map offload buffer\n");
4126 return;
4127 }
4128
4129 memset(&crq, 0, sizeof(crq));
4130 crq.query_ip_offload.first = IBMVNIC_CRQ_CMD;
4131 crq.query_ip_offload.cmd = QUERY_IP_OFFLOAD;
4132 crq.query_ip_offload.len = cpu_to_be32(buf_sz);
4133 crq.query_ip_offload.ioba =
4134 cpu_to_be32(adapter->ip_offload_tok);
4135
4136 ibmvnic_send_crq(adapter, &crq);
4137}
4138
Lijun Pan46899bd2020-09-27 20:13:30 -05004139static void send_control_ip_offload(struct ibmvnic_adapter *adapter)
4140{
4141 struct ibmvnic_control_ip_offload_buffer *ctrl_buf = &adapter->ip_offload_ctrl;
4142 struct ibmvnic_query_ip_offload_buffer *buf = &adapter->ip_offload_buf;
4143 struct device *dev = &adapter->vdev->dev;
4144 netdev_features_t old_hw_features = 0;
4145 union ibmvnic_crq crq;
4146
4147 adapter->ip_offload_ctrl_tok =
4148 dma_map_single(dev,
4149 ctrl_buf,
4150 sizeof(adapter->ip_offload_ctrl),
4151 DMA_TO_DEVICE);
4152
4153 if (dma_mapping_error(dev, adapter->ip_offload_ctrl_tok)) {
4154 dev_err(dev, "Couldn't map ip offload control buffer\n");
4155 return;
4156 }
4157
4158 ctrl_buf->len = cpu_to_be32(sizeof(adapter->ip_offload_ctrl));
4159 ctrl_buf->version = cpu_to_be32(INITIAL_VERSION_IOB);
4160 ctrl_buf->ipv4_chksum = buf->ipv4_chksum;
4161 ctrl_buf->ipv6_chksum = buf->ipv6_chksum;
4162 ctrl_buf->tcp_ipv4_chksum = buf->tcp_ipv4_chksum;
4163 ctrl_buf->udp_ipv4_chksum = buf->udp_ipv4_chksum;
4164 ctrl_buf->tcp_ipv6_chksum = buf->tcp_ipv6_chksum;
4165 ctrl_buf->udp_ipv6_chksum = buf->udp_ipv6_chksum;
4166 ctrl_buf->large_tx_ipv4 = buf->large_tx_ipv4;
4167 ctrl_buf->large_tx_ipv6 = buf->large_tx_ipv6;
4168
4169 /* large_rx disabled for now, additional features needed */
4170 ctrl_buf->large_rx_ipv4 = 0;
4171 ctrl_buf->large_rx_ipv6 = 0;
4172
4173 if (adapter->state != VNIC_PROBING) {
4174 old_hw_features = adapter->netdev->hw_features;
4175 adapter->netdev->hw_features = 0;
4176 }
4177
4178 adapter->netdev->hw_features = NETIF_F_SG | NETIF_F_GSO | NETIF_F_GRO;
4179
4180 if (buf->tcp_ipv4_chksum || buf->udp_ipv4_chksum)
4181 adapter->netdev->hw_features |= NETIF_F_IP_CSUM;
4182
4183 if (buf->tcp_ipv6_chksum || buf->udp_ipv6_chksum)
4184 adapter->netdev->hw_features |= NETIF_F_IPV6_CSUM;
4185
4186 if ((adapter->netdev->features &
4187 (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)))
4188 adapter->netdev->hw_features |= NETIF_F_RXCSUM;
4189
4190 if (buf->large_tx_ipv4)
4191 adapter->netdev->hw_features |= NETIF_F_TSO;
4192 if (buf->large_tx_ipv6)
4193 adapter->netdev->hw_features |= NETIF_F_TSO6;
4194
4195 if (adapter->state == VNIC_PROBING) {
4196 adapter->netdev->features |= adapter->netdev->hw_features;
4197 } else if (old_hw_features != adapter->netdev->hw_features) {
4198 netdev_features_t tmp = 0;
4199
4200 /* disable features no longer supported */
4201 adapter->netdev->features &= adapter->netdev->hw_features;
4202 /* turn on features now supported if previously enabled */
4203 tmp = (old_hw_features ^ adapter->netdev->hw_features) &
4204 adapter->netdev->hw_features;
4205 adapter->netdev->features |=
4206 tmp & adapter->netdev->wanted_features;
4207 }
4208
4209 memset(&crq, 0, sizeof(crq));
4210 crq.control_ip_offload.first = IBMVNIC_CRQ_CMD;
4211 crq.control_ip_offload.cmd = CONTROL_IP_OFFLOAD;
4212 crq.control_ip_offload.len =
4213 cpu_to_be32(sizeof(adapter->ip_offload_ctrl));
4214 crq.control_ip_offload.ioba = cpu_to_be32(adapter->ip_offload_ctrl_tok);
4215 ibmvnic_send_crq(adapter, &crq);
4216}
4217
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02004218static void handle_vpd_size_rsp(union ibmvnic_crq *crq,
4219 struct ibmvnic_adapter *adapter)
4220{
4221 struct device *dev = &adapter->vdev->dev;
4222
4223 if (crq->get_vpd_size_rsp.rc.code) {
4224 dev_err(dev, "Error retrieving VPD size, rc=%x\n",
4225 crq->get_vpd_size_rsp.rc.code);
4226 complete(&adapter->fw_done);
4227 return;
4228 }
4229
4230 adapter->vpd->len = be64_to_cpu(crq->get_vpd_size_rsp.len);
4231 complete(&adapter->fw_done);
4232}
4233
4234static void handle_vpd_rsp(union ibmvnic_crq *crq,
4235 struct ibmvnic_adapter *adapter)
4236{
4237 struct device *dev = &adapter->vdev->dev;
Desnes Augusto Nunes do Rosario21a25452018-02-05 14:33:55 -02004238 unsigned char *substr = NULL;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02004239 u8 fw_level_len = 0;
4240
4241 memset(adapter->fw_version, 0, 32);
4242
4243 dma_unmap_single(dev, adapter->vpd->dma_addr, adapter->vpd->len,
4244 DMA_FROM_DEVICE);
4245
4246 if (crq->get_vpd_rsp.rc.code) {
4247 dev_err(dev, "Error retrieving VPD from device, rc=%x\n",
4248 crq->get_vpd_rsp.rc.code);
4249 goto complete;
4250 }
4251
4252 /* get the position of the firmware version info
4253 * located after the ASCII 'RM' substring in the buffer
4254 */
4255 substr = strnstr(adapter->vpd->buff, "RM", adapter->vpd->len);
4256 if (!substr) {
Desnes Augusto Nunes do Rosarioa1073112018-02-01 16:04:30 -02004257 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 -02004258 goto complete;
4259 }
4260
4261 /* get length of firmware level ASCII substring */
4262 if ((substr + 2) < (adapter->vpd->buff + adapter->vpd->len)) {
4263 fw_level_len = *(substr + 2);
4264 } else {
4265 dev_info(dev, "Length of FW substr extrapolated VDP buff\n");
4266 goto complete;
4267 }
4268
4269 /* copy firmware version string from vpd into adapter */
4270 if ((substr + 3 + fw_level_len) <
4271 (adapter->vpd->buff + adapter->vpd->len)) {
Desnes Augusto Nunes do Rosario21a25452018-02-05 14:33:55 -02004272 strncpy((char *)adapter->fw_version, substr + 3, fw_level_len);
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02004273 } else {
4274 dev_info(dev, "FW substr extrapolated VPD buff\n");
4275 }
4276
4277complete:
Desnes Augusto Nunes do Rosario21a25452018-02-05 14:33:55 -02004278 if (adapter->fw_version[0] == '\0')
4279 strncpy((char *)adapter->fw_version, "N/A", 3 * sizeof(char));
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02004280 complete(&adapter->fw_done);
4281}
4282
Thomas Falcon032c5e82015-12-21 11:26:06 -06004283static void handle_query_ip_offload_rsp(struct ibmvnic_adapter *adapter)
4284{
4285 struct device *dev = &adapter->vdev->dev;
4286 struct ibmvnic_query_ip_offload_buffer *buf = &adapter->ip_offload_buf;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004287 int i;
4288
4289 dma_unmap_single(dev, adapter->ip_offload_tok,
4290 sizeof(adapter->ip_offload_buf), DMA_FROM_DEVICE);
4291
4292 netdev_dbg(adapter->netdev, "Query IP Offload Buffer:\n");
4293 for (i = 0; i < (sizeof(adapter->ip_offload_buf) - 1) / 8 + 1; i++)
4294 netdev_dbg(adapter->netdev, "%016lx\n",
Lijun Pan429aa362021-02-11 00:43:18 -06004295 ((unsigned long *)(buf))[i]);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004296
4297 netdev_dbg(adapter->netdev, "ipv4_chksum = %d\n", buf->ipv4_chksum);
4298 netdev_dbg(adapter->netdev, "ipv6_chksum = %d\n", buf->ipv6_chksum);
4299 netdev_dbg(adapter->netdev, "tcp_ipv4_chksum = %d\n",
4300 buf->tcp_ipv4_chksum);
4301 netdev_dbg(adapter->netdev, "tcp_ipv6_chksum = %d\n",
4302 buf->tcp_ipv6_chksum);
4303 netdev_dbg(adapter->netdev, "udp_ipv4_chksum = %d\n",
4304 buf->udp_ipv4_chksum);
4305 netdev_dbg(adapter->netdev, "udp_ipv6_chksum = %d\n",
4306 buf->udp_ipv6_chksum);
4307 netdev_dbg(adapter->netdev, "large_tx_ipv4 = %d\n",
4308 buf->large_tx_ipv4);
4309 netdev_dbg(adapter->netdev, "large_tx_ipv6 = %d\n",
4310 buf->large_tx_ipv6);
4311 netdev_dbg(adapter->netdev, "large_rx_ipv4 = %d\n",
4312 buf->large_rx_ipv4);
4313 netdev_dbg(adapter->netdev, "large_rx_ipv6 = %d\n",
4314 buf->large_rx_ipv6);
4315 netdev_dbg(adapter->netdev, "max_ipv4_hdr_sz = %d\n",
4316 buf->max_ipv4_header_size);
4317 netdev_dbg(adapter->netdev, "max_ipv6_hdr_sz = %d\n",
4318 buf->max_ipv6_header_size);
4319 netdev_dbg(adapter->netdev, "max_tcp_hdr_size = %d\n",
4320 buf->max_tcp_header_size);
4321 netdev_dbg(adapter->netdev, "max_udp_hdr_size = %d\n",
4322 buf->max_udp_header_size);
4323 netdev_dbg(adapter->netdev, "max_large_tx_size = %d\n",
4324 buf->max_large_tx_size);
4325 netdev_dbg(adapter->netdev, "max_large_rx_size = %d\n",
4326 buf->max_large_rx_size);
4327 netdev_dbg(adapter->netdev, "ipv6_ext_hdr = %d\n",
4328 buf->ipv6_extension_header);
4329 netdev_dbg(adapter->netdev, "tcp_pseudosum_req = %d\n",
4330 buf->tcp_pseudosum_req);
4331 netdev_dbg(adapter->netdev, "num_ipv6_ext_hd = %d\n",
4332 buf->num_ipv6_ext_headers);
4333 netdev_dbg(adapter->netdev, "off_ipv6_ext_hd = %d\n",
4334 buf->off_ipv6_ext_headers);
4335
Lijun Pan46899bd2020-09-27 20:13:30 -05004336 send_control_ip_offload(adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004337}
4338
Thomas Falconc9008d32018-08-06 21:39:59 -05004339static const char *ibmvnic_fw_err_cause(u16 cause)
4340{
4341 switch (cause) {
4342 case ADAPTER_PROBLEM:
4343 return "adapter problem";
4344 case BUS_PROBLEM:
4345 return "bus problem";
4346 case FW_PROBLEM:
4347 return "firmware problem";
4348 case DD_PROBLEM:
4349 return "device driver problem";
4350 case EEH_RECOVERY:
4351 return "EEH recovery";
4352 case FW_UPDATED:
4353 return "firmware updated";
4354 case LOW_MEMORY:
4355 return "low Memory";
4356 default:
4357 return "unknown";
4358 }
4359}
4360
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04004361static void handle_error_indication(union ibmvnic_crq *crq,
4362 struct ibmvnic_adapter *adapter)
4363{
4364 struct device *dev = &adapter->vdev->dev;
Thomas Falconc9008d32018-08-06 21:39:59 -05004365 u16 cause;
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04004366
Thomas Falconc9008d32018-08-06 21:39:59 -05004367 cause = be16_to_cpu(crq->error_indication.error_cause);
4368
4369 dev_warn_ratelimited(dev,
4370 "Firmware reports %serror, cause: %s. Starting recovery...\n",
4371 crq->error_indication.flags
4372 & IBMVNIC_FATAL_ERROR ? "FATAL " : "",
4373 ibmvnic_fw_err_cause(cause));
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04004374
Nathan Fontenoted651a12017-05-03 14:04:38 -04004375 if (crq->error_indication.flags & IBMVNIC_FATAL_ERROR)
4376 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
John Allen8cb31cf2017-05-26 10:30:37 -04004377 else
4378 ibmvnic_reset(adapter, VNIC_RESET_NON_FATAL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004379}
4380
Thomas Falconf8136142018-01-29 13:45:05 -06004381static int handle_change_mac_rsp(union ibmvnic_crq *crq,
4382 struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06004383{
4384 struct net_device *netdev = adapter->netdev;
4385 struct device *dev = &adapter->vdev->dev;
4386 long rc;
4387
4388 rc = crq->change_mac_addr_rsp.rc.code;
4389 if (rc) {
4390 dev_err(dev, "Error %ld in CHANGE_MAC_ADDR_RSP\n", rc);
Thomas Falconf8136142018-01-29 13:45:05 -06004391 goto out;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004392 }
Lijun Pand9b0e592020-10-20 17:39:19 -05004393 /* crq->change_mac_addr.mac_addr is the requested one
4394 * crq->change_mac_addr_rsp.mac_addr is the returned valid one.
4395 */
Thomas Falcon62740e92019-05-09 23:13:43 -05004396 ether_addr_copy(netdev->dev_addr,
4397 &crq->change_mac_addr_rsp.mac_addr[0]);
Lijun Pand9b0e592020-10-20 17:39:19 -05004398 ether_addr_copy(adapter->mac_addr,
4399 &crq->change_mac_addr_rsp.mac_addr[0]);
Thomas Falconf8136142018-01-29 13:45:05 -06004400out:
4401 complete(&adapter->fw_done);
4402 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004403}
4404
4405static void handle_request_cap_rsp(union ibmvnic_crq *crq,
4406 struct ibmvnic_adapter *adapter)
4407{
4408 struct device *dev = &adapter->vdev->dev;
4409 u64 *req_value;
4410 char *name;
4411
Thomas Falcon901e0402017-02-15 12:17:59 -06004412 atomic_dec(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004413 switch (be16_to_cpu(crq->request_capability_rsp.capability)) {
4414 case REQ_TX_QUEUES:
4415 req_value = &adapter->req_tx_queues;
4416 name = "tx";
4417 break;
4418 case REQ_RX_QUEUES:
4419 req_value = &adapter->req_rx_queues;
4420 name = "rx";
4421 break;
4422 case REQ_RX_ADD_QUEUES:
4423 req_value = &adapter->req_rx_add_queues;
4424 name = "rx_add";
4425 break;
4426 case REQ_TX_ENTRIES_PER_SUBCRQ:
4427 req_value = &adapter->req_tx_entries_per_subcrq;
4428 name = "tx_entries_per_subcrq";
4429 break;
4430 case REQ_RX_ADD_ENTRIES_PER_SUBCRQ:
4431 req_value = &adapter->req_rx_add_entries_per_subcrq;
4432 name = "rx_add_entries_per_subcrq";
4433 break;
4434 case REQ_MTU:
4435 req_value = &adapter->req_mtu;
4436 name = "mtu";
4437 break;
4438 case PROMISC_REQUESTED:
4439 req_value = &adapter->promisc;
4440 name = "promisc";
4441 break;
4442 default:
4443 dev_err(dev, "Got invalid cap request rsp %d\n",
4444 crq->request_capability.capability);
4445 return;
4446 }
4447
4448 switch (crq->request_capability_rsp.rc.code) {
4449 case SUCCESS:
4450 break;
4451 case PARTIALSUCCESS:
4452 dev_info(dev, "req=%lld, rsp=%ld in %s queue, retrying.\n",
4453 *req_value,
Lijun Pan914789a2021-02-11 00:43:21 -06004454 (long)be64_to_cpu(crq->request_capability_rsp.number),
4455 name);
John Allene7913802018-01-18 16:27:12 -06004456
4457 if (be16_to_cpu(crq->request_capability_rsp.capability) ==
4458 REQ_MTU) {
4459 pr_err("mtu of %llu is not supported. Reverting.\n",
4460 *req_value);
4461 *req_value = adapter->fallback.mtu;
4462 } else {
4463 *req_value =
4464 be64_to_cpu(crq->request_capability_rsp.number);
4465 }
4466
Lijun Pan09081b92020-09-27 20:13:27 -05004467 send_request_cap(adapter, 1);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004468 return;
4469 default:
4470 dev_err(dev, "Error %d in request cap rsp\n",
4471 crq->request_capability_rsp.rc.code);
4472 return;
4473 }
4474
4475 /* Done receiving requested capabilities, query IP offload support */
Thomas Falcon901e0402017-02-15 12:17:59 -06004476 if (atomic_read(&adapter->running_cap_crqs) == 0) {
Thomas Falcon249168a2017-02-15 12:18:00 -06004477 adapter->wait_capability = false;
Lijun Pan16e811f2020-09-27 20:13:29 -05004478 send_query_ip_offload(adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004479 }
4480}
4481
4482static int handle_login_rsp(union ibmvnic_crq *login_rsp_crq,
4483 struct ibmvnic_adapter *adapter)
4484{
4485 struct device *dev = &adapter->vdev->dev;
John Allenc26eba02017-10-26 16:23:25 -05004486 struct net_device *netdev = adapter->netdev;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004487 struct ibmvnic_login_rsp_buffer *login_rsp = adapter->login_rsp_buf;
4488 struct ibmvnic_login_buffer *login = adapter->login_buf;
Cristobal Fornof3ae59c2020-08-19 13:16:23 -05004489 u64 *tx_handle_array;
4490 u64 *rx_handle_array;
4491 int num_tx_pools;
4492 int num_rx_pools;
Thomas Falcon507ebe62020-08-21 13:39:01 -05004493 u64 *size_array;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004494 int i;
4495
Sukadev Bhattiprolu76cdc5c2020-11-25 18:04:29 -06004496 /* CHECK: Test/set of login_pending does not need to be atomic
4497 * because only ibmvnic_tasklet tests/clears this.
4498 */
4499 if (!adapter->login_pending) {
4500 netdev_warn(netdev, "Ignoring unexpected login response\n");
4501 return 0;
4502 }
4503 adapter->login_pending = false;
4504
Thomas Falcon032c5e82015-12-21 11:26:06 -06004505 dma_unmap_single(dev, adapter->login_buf_token, adapter->login_buf_sz,
Thomas Falcon37e40fa2018-04-06 18:37:02 -05004506 DMA_TO_DEVICE);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004507 dma_unmap_single(dev, adapter->login_rsp_buf_token,
Thomas Falcon37e40fa2018-04-06 18:37:02 -05004508 adapter->login_rsp_buf_sz, DMA_FROM_DEVICE);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004509
John Allen498cd8e2016-04-06 11:49:55 -05004510 /* If the number of queues requested can't be allocated by the
4511 * server, the login response will return with code 1. We will need
4512 * to resend the login buffer with fewer queues requested.
4513 */
4514 if (login_rsp_crq->generic.rc.code) {
Nathan Fontenot64d92aa2018-04-11 10:09:32 -05004515 adapter->init_done_rc = login_rsp_crq->generic.rc.code;
John Allen498cd8e2016-04-06 11:49:55 -05004516 complete(&adapter->init_done);
4517 return 0;
4518 }
4519
John Allenc26eba02017-10-26 16:23:25 -05004520 netdev->mtu = adapter->req_mtu - ETH_HLEN;
4521
Thomas Falcon032c5e82015-12-21 11:26:06 -06004522 netdev_dbg(adapter->netdev, "Login Response Buffer:\n");
4523 for (i = 0; i < (adapter->login_rsp_buf_sz - 1) / 8 + 1; i++) {
4524 netdev_dbg(adapter->netdev, "%016lx\n",
Lijun Pan429aa362021-02-11 00:43:18 -06004525 ((unsigned long *)(adapter->login_rsp_buf))[i]);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004526 }
4527
4528 /* Sanity checks */
4529 if (login->num_txcomp_subcrqs != login_rsp->num_txsubm_subcrqs ||
4530 (be32_to_cpu(login->num_rxcomp_subcrqs) *
4531 adapter->req_rx_add_queues !=
4532 be32_to_cpu(login_rsp->num_rxadd_subcrqs))) {
4533 dev_err(dev, "FATAL: Inconsistent login and login rsp\n");
Dany Madden31d6b402020-11-25 18:04:24 -06004534 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004535 return -EIO;
4536 }
Thomas Falcon507ebe62020-08-21 13:39:01 -05004537 size_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
4538 be32_to_cpu(adapter->login_rsp_buf->off_rxadd_buff_size));
4539 /* variable buffer sizes are not supported, so just read the
4540 * first entry.
4541 */
4542 adapter->cur_rx_buf_sz = be64_to_cpu(size_array[0]);
Cristobal Fornof3ae59c2020-08-19 13:16:23 -05004543
4544 num_tx_pools = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
4545 num_rx_pools = be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
4546
4547 tx_handle_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
4548 be32_to_cpu(adapter->login_rsp_buf->off_txsubm_subcrqs));
4549 rx_handle_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
4550 be32_to_cpu(adapter->login_rsp_buf->off_rxadd_subcrqs));
4551
4552 for (i = 0; i < num_tx_pools; i++)
4553 adapter->tx_scrq[i]->handle = tx_handle_array[i];
4554
4555 for (i = 0; i < num_rx_pools; i++)
4556 adapter->rx_scrq[i]->handle = rx_handle_array[i];
4557
Thomas Falcon507ebe62020-08-21 13:39:01 -05004558 adapter->num_active_tx_scrqs = num_tx_pools;
4559 adapter->num_active_rx_scrqs = num_rx_pools;
Cristobal Fornof3ae59c2020-08-19 13:16:23 -05004560 release_login_rsp_buffer(adapter);
Thomas Falcona2c0f032018-02-21 18:18:30 -06004561 release_login_buffer(adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004562 complete(&adapter->init_done);
4563
Thomas Falcon032c5e82015-12-21 11:26:06 -06004564 return 0;
4565}
4566
Thomas Falcon032c5e82015-12-21 11:26:06 -06004567static void handle_request_unmap_rsp(union ibmvnic_crq *crq,
4568 struct ibmvnic_adapter *adapter)
4569{
4570 struct device *dev = &adapter->vdev->dev;
4571 long rc;
4572
4573 rc = crq->request_unmap_rsp.rc.code;
4574 if (rc)
4575 dev_err(dev, "Error %ld in REQUEST_UNMAP_RSP\n", rc);
4576}
4577
4578static void handle_query_map_rsp(union ibmvnic_crq *crq,
4579 struct ibmvnic_adapter *adapter)
4580{
4581 struct net_device *netdev = adapter->netdev;
4582 struct device *dev = &adapter->vdev->dev;
4583 long rc;
4584
4585 rc = crq->query_map_rsp.rc.code;
4586 if (rc) {
4587 dev_err(dev, "Error %ld in QUERY_MAP_RSP\n", rc);
4588 return;
4589 }
4590 netdev_dbg(netdev, "page_size = %d\ntot_pages = %d\nfree_pages = %d\n",
4591 crq->query_map_rsp.page_size, crq->query_map_rsp.tot_pages,
4592 crq->query_map_rsp.free_pages);
4593}
4594
4595static void handle_query_cap_rsp(union ibmvnic_crq *crq,
4596 struct ibmvnic_adapter *adapter)
4597{
4598 struct net_device *netdev = adapter->netdev;
4599 struct device *dev = &adapter->vdev->dev;
4600 long rc;
4601
Thomas Falcon901e0402017-02-15 12:17:59 -06004602 atomic_dec(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004603 netdev_dbg(netdev, "Outstanding queries: %d\n",
Thomas Falcon901e0402017-02-15 12:17:59 -06004604 atomic_read(&adapter->running_cap_crqs));
Thomas Falcon032c5e82015-12-21 11:26:06 -06004605 rc = crq->query_capability.rc.code;
4606 if (rc) {
4607 dev_err(dev, "Error %ld in QUERY_CAP_RSP\n", rc);
4608 goto out;
4609 }
4610
4611 switch (be16_to_cpu(crq->query_capability.capability)) {
4612 case MIN_TX_QUEUES:
4613 adapter->min_tx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004614 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004615 netdev_dbg(netdev, "min_tx_queues = %lld\n",
4616 adapter->min_tx_queues);
4617 break;
4618 case MIN_RX_QUEUES:
4619 adapter->min_rx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004620 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004621 netdev_dbg(netdev, "min_rx_queues = %lld\n",
4622 adapter->min_rx_queues);
4623 break;
4624 case MIN_RX_ADD_QUEUES:
4625 adapter->min_rx_add_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004626 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004627 netdev_dbg(netdev, "min_rx_add_queues = %lld\n",
4628 adapter->min_rx_add_queues);
4629 break;
4630 case MAX_TX_QUEUES:
4631 adapter->max_tx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004632 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004633 netdev_dbg(netdev, "max_tx_queues = %lld\n",
4634 adapter->max_tx_queues);
4635 break;
4636 case MAX_RX_QUEUES:
4637 adapter->max_rx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004638 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004639 netdev_dbg(netdev, "max_rx_queues = %lld\n",
4640 adapter->max_rx_queues);
4641 break;
4642 case MAX_RX_ADD_QUEUES:
4643 adapter->max_rx_add_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004644 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004645 netdev_dbg(netdev, "max_rx_add_queues = %lld\n",
4646 adapter->max_rx_add_queues);
4647 break;
4648 case MIN_TX_ENTRIES_PER_SUBCRQ:
4649 adapter->min_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06004650 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004651 netdev_dbg(netdev, "min_tx_entries_per_subcrq = %lld\n",
4652 adapter->min_tx_entries_per_subcrq);
4653 break;
4654 case MIN_RX_ADD_ENTRIES_PER_SUBCRQ:
4655 adapter->min_rx_add_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06004656 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004657 netdev_dbg(netdev, "min_rx_add_entrs_per_subcrq = %lld\n",
4658 adapter->min_rx_add_entries_per_subcrq);
4659 break;
4660 case MAX_TX_ENTRIES_PER_SUBCRQ:
4661 adapter->max_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06004662 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004663 netdev_dbg(netdev, "max_tx_entries_per_subcrq = %lld\n",
4664 adapter->max_tx_entries_per_subcrq);
4665 break;
4666 case MAX_RX_ADD_ENTRIES_PER_SUBCRQ:
4667 adapter->max_rx_add_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06004668 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004669 netdev_dbg(netdev, "max_rx_add_entrs_per_subcrq = %lld\n",
4670 adapter->max_rx_add_entries_per_subcrq);
4671 break;
4672 case TCP_IP_OFFLOAD:
4673 adapter->tcp_ip_offload =
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, "tcp_ip_offload = %lld\n",
4676 adapter->tcp_ip_offload);
4677 break;
4678 case PROMISC_SUPPORTED:
4679 adapter->promisc_supported =
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, "promisc_supported = %lld\n",
4682 adapter->promisc_supported);
4683 break;
4684 case MIN_MTU:
Thomas Falconde89e852016-03-01 10:20:09 -06004685 adapter->min_mtu = be64_to_cpu(crq->query_capability.number);
Thomas Falconf39f0d12017-02-14 10:22:59 -06004686 netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004687 netdev_dbg(netdev, "min_mtu = %lld\n", adapter->min_mtu);
4688 break;
4689 case MAX_MTU:
Thomas Falconde89e852016-03-01 10:20:09 -06004690 adapter->max_mtu = be64_to_cpu(crq->query_capability.number);
Thomas Falconf39f0d12017-02-14 10:22:59 -06004691 netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004692 netdev_dbg(netdev, "max_mtu = %lld\n", adapter->max_mtu);
4693 break;
4694 case MAX_MULTICAST_FILTERS:
4695 adapter->max_multicast_filters =
Thomas Falconde89e852016-03-01 10:20:09 -06004696 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004697 netdev_dbg(netdev, "max_multicast_filters = %lld\n",
4698 adapter->max_multicast_filters);
4699 break;
4700 case VLAN_HEADER_INSERTION:
4701 adapter->vlan_header_insertion =
Thomas Falconde89e852016-03-01 10:20:09 -06004702 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004703 if (adapter->vlan_header_insertion)
4704 netdev->features |= NETIF_F_HW_VLAN_STAG_TX;
4705 netdev_dbg(netdev, "vlan_header_insertion = %lld\n",
4706 adapter->vlan_header_insertion);
4707 break;
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04004708 case RX_VLAN_HEADER_INSERTION:
4709 adapter->rx_vlan_header_insertion =
4710 be64_to_cpu(crq->query_capability.number);
4711 netdev_dbg(netdev, "rx_vlan_header_insertion = %lld\n",
4712 adapter->rx_vlan_header_insertion);
4713 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004714 case MAX_TX_SG_ENTRIES:
4715 adapter->max_tx_sg_entries =
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, "max_tx_sg_entries = %lld\n",
4718 adapter->max_tx_sg_entries);
4719 break;
4720 case RX_SG_SUPPORTED:
4721 adapter->rx_sg_supported =
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, "rx_sg_supported = %lld\n",
4724 adapter->rx_sg_supported);
4725 break;
4726 case OPT_TX_COMP_SUB_QUEUES:
4727 adapter->opt_tx_comp_sub_queues =
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, "opt_tx_comp_sub_queues = %lld\n",
4730 adapter->opt_tx_comp_sub_queues);
4731 break;
4732 case OPT_RX_COMP_QUEUES:
4733 adapter->opt_rx_comp_queues =
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, "opt_rx_comp_queues = %lld\n",
4736 adapter->opt_rx_comp_queues);
4737 break;
4738 case OPT_RX_BUFADD_Q_PER_RX_COMP_Q:
4739 adapter->opt_rx_bufadd_q_per_rx_comp_q =
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, "opt_rx_bufadd_q_per_rx_comp_q = %lld\n",
4742 adapter->opt_rx_bufadd_q_per_rx_comp_q);
4743 break;
4744 case OPT_TX_ENTRIES_PER_SUBCRQ:
4745 adapter->opt_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06004746 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004747 netdev_dbg(netdev, "opt_tx_entries_per_subcrq = %lld\n",
4748 adapter->opt_tx_entries_per_subcrq);
4749 break;
4750 case OPT_RXBA_ENTRIES_PER_SUBCRQ:
4751 adapter->opt_rxba_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06004752 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004753 netdev_dbg(netdev, "opt_rxba_entries_per_subcrq = %lld\n",
4754 adapter->opt_rxba_entries_per_subcrq);
4755 break;
4756 case TX_RX_DESC_REQ:
4757 adapter->tx_rx_desc_req = crq->query_capability.number;
4758 netdev_dbg(netdev, "tx_rx_desc_req = %llx\n",
4759 adapter->tx_rx_desc_req);
4760 break;
4761
4762 default:
4763 netdev_err(netdev, "Got invalid cap rsp %d\n",
4764 crq->query_capability.capability);
4765 }
4766
4767out:
Thomas Falcon249168a2017-02-15 12:18:00 -06004768 if (atomic_read(&adapter->running_cap_crqs) == 0) {
4769 adapter->wait_capability = false;
Lijun Pan09081b92020-09-27 20:13:27 -05004770 send_request_cap(adapter, 0);
Thomas Falcon249168a2017-02-15 12:18:00 -06004771 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06004772}
4773
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03004774static int send_query_phys_parms(struct ibmvnic_adapter *adapter)
4775{
4776 union ibmvnic_crq crq;
4777 int rc;
4778
4779 memset(&crq, 0, sizeof(crq));
4780 crq.query_phys_parms.first = IBMVNIC_CRQ_CMD;
4781 crq.query_phys_parms.cmd = QUERY_PHYS_PARMS;
Thomas Falconff25dcb2019-11-25 17:12:56 -06004782
4783 mutex_lock(&adapter->fw_lock);
4784 adapter->fw_done_rc = 0;
Thomas Falcon070eca92019-11-25 17:12:53 -06004785 reinit_completion(&adapter->fw_done);
Thomas Falconff25dcb2019-11-25 17:12:56 -06004786
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03004787 rc = ibmvnic_send_crq(adapter, &crq);
Thomas Falconff25dcb2019-11-25 17:12:56 -06004788 if (rc) {
4789 mutex_unlock(&adapter->fw_lock);
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03004790 return rc;
Thomas Falconff25dcb2019-11-25 17:12:56 -06004791 }
Thomas Falcon476d96c2019-11-25 17:12:55 -06004792
4793 rc = ibmvnic_wait_for_completion(adapter, &adapter->fw_done, 10000);
Thomas Falconff25dcb2019-11-25 17:12:56 -06004794 if (rc) {
4795 mutex_unlock(&adapter->fw_lock);
Thomas Falcon476d96c2019-11-25 17:12:55 -06004796 return rc;
Thomas Falconff25dcb2019-11-25 17:12:56 -06004797 }
Thomas Falcon476d96c2019-11-25 17:12:55 -06004798
Thomas Falconff25dcb2019-11-25 17:12:56 -06004799 mutex_unlock(&adapter->fw_lock);
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03004800 return adapter->fw_done_rc ? -EIO : 0;
4801}
4802
4803static int handle_query_phys_parms_rsp(union ibmvnic_crq *crq,
4804 struct ibmvnic_adapter *adapter)
4805{
4806 struct net_device *netdev = adapter->netdev;
4807 int rc;
Murilo Fossa Vicentinidd0f9d82019-09-16 11:50:37 -03004808 __be32 rspeed = cpu_to_be32(crq->query_phys_parms_rsp.speed);
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03004809
4810 rc = crq->query_phys_parms_rsp.rc.code;
4811 if (rc) {
4812 netdev_err(netdev, "Error %d in QUERY_PHYS_PARMS\n", rc);
4813 return rc;
4814 }
Murilo Fossa Vicentinidd0f9d82019-09-16 11:50:37 -03004815 switch (rspeed) {
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03004816 case IBMVNIC_10MBPS:
4817 adapter->speed = SPEED_10;
4818 break;
4819 case IBMVNIC_100MBPS:
4820 adapter->speed = SPEED_100;
4821 break;
4822 case IBMVNIC_1GBPS:
4823 adapter->speed = SPEED_1000;
4824 break;
Lijun Panb9cd7952020-09-27 19:06:25 -05004825 case IBMVNIC_10GBPS:
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03004826 adapter->speed = SPEED_10000;
4827 break;
4828 case IBMVNIC_25GBPS:
4829 adapter->speed = SPEED_25000;
4830 break;
4831 case IBMVNIC_40GBPS:
4832 adapter->speed = SPEED_40000;
4833 break;
4834 case IBMVNIC_50GBPS:
4835 adapter->speed = SPEED_50000;
4836 break;
4837 case IBMVNIC_100GBPS:
4838 adapter->speed = SPEED_100000;
4839 break;
Lijun Panb9cd7952020-09-27 19:06:25 -05004840 case IBMVNIC_200GBPS:
4841 adapter->speed = SPEED_200000;
4842 break;
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03004843 default:
Murilo Fossa Vicentinidd0f9d82019-09-16 11:50:37 -03004844 if (netif_carrier_ok(netdev))
4845 netdev_warn(netdev, "Unknown speed 0x%08x\n", rspeed);
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03004846 adapter->speed = SPEED_UNKNOWN;
4847 }
4848 if (crq->query_phys_parms_rsp.flags1 & IBMVNIC_FULL_DUPLEX)
4849 adapter->duplex = DUPLEX_FULL;
4850 else if (crq->query_phys_parms_rsp.flags1 & IBMVNIC_HALF_DUPLEX)
4851 adapter->duplex = DUPLEX_HALF;
4852 else
4853 adapter->duplex = DUPLEX_UNKNOWN;
4854
4855 return rc;
4856}
4857
Thomas Falcon032c5e82015-12-21 11:26:06 -06004858static void ibmvnic_handle_crq(union ibmvnic_crq *crq,
4859 struct ibmvnic_adapter *adapter)
4860{
4861 struct ibmvnic_generic_crq *gen_crq = &crq->generic;
4862 struct net_device *netdev = adapter->netdev;
4863 struct device *dev = &adapter->vdev->dev;
Murilo Fossa Vicentini993a82b2017-04-19 13:44:35 -04004864 u64 *u64_crq = (u64 *)crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004865 long rc;
4866
4867 netdev_dbg(netdev, "Handling CRQ: %016lx %016lx\n",
Lijun Pan429aa362021-02-11 00:43:18 -06004868 (unsigned long)cpu_to_be64(u64_crq[0]),
4869 (unsigned long)cpu_to_be64(u64_crq[1]));
Thomas Falcon032c5e82015-12-21 11:26:06 -06004870 switch (gen_crq->first) {
4871 case IBMVNIC_CRQ_INIT_RSP:
4872 switch (gen_crq->cmd) {
4873 case IBMVNIC_CRQ_INIT:
4874 dev_info(dev, "Partner initialized\n");
John Allen017892c12017-05-26 10:30:19 -04004875 adapter->from_passive_init = true;
Sukadev Bhattiprolu76cdc5c2020-11-25 18:04:29 -06004876 /* Discard any stale login responses from prev reset.
4877 * CHECK: should we clear even on INIT_COMPLETE?
4878 */
4879 adapter->login_pending = false;
4880
Thomas Falcon17c87052018-05-23 13:37:58 -05004881 if (!completion_done(&adapter->init_done)) {
4882 complete(&adapter->init_done);
4883 adapter->init_done_rc = -EIO;
4884 }
Sukadev Bhattiproluef66a1e2021-02-02 21:08:02 -08004885 rc = ibmvnic_reset(adapter, VNIC_RESET_FAILOVER);
4886 if (rc && rc != -EBUSY) {
4887 /* We were unable to schedule the failover
4888 * reset either because the adapter was still
4889 * probing (eg: during kexec) or we could not
4890 * allocate memory. Clear the failover_pending
4891 * flag since no one else will. We ignore
4892 * EBUSY because it means either FAILOVER reset
4893 * is already scheduled or the adapter is
4894 * being removed.
4895 */
4896 netdev_err(netdev,
4897 "Error %ld scheduling failover reset\n",
4898 rc);
4899 adapter->failover_pending = false;
4900 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06004901 break;
4902 case IBMVNIC_CRQ_INIT_COMPLETE:
4903 dev_info(dev, "Partner initialization complete\n");
Thomas Falcon51536982018-05-23 13:37:56 -05004904 adapter->crq.active = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004905 send_version_xchg(adapter);
4906 break;
4907 default:
4908 dev_err(dev, "Unknown crq cmd: %d\n", gen_crq->cmd);
4909 }
4910 return;
4911 case IBMVNIC_CRQ_XPORT_EVENT:
Nathan Fontenoted651a12017-05-03 14:04:38 -04004912 netif_carrier_off(netdev);
Thomas Falcon51536982018-05-23 13:37:56 -05004913 adapter->crq.active = false;
Thomas Falcon2147e3d2019-11-25 17:12:54 -06004914 /* terminate any thread waiting for a response
4915 * from the device
4916 */
4917 if (!completion_done(&adapter->fw_done)) {
4918 adapter->fw_done_rc = -EIO;
4919 complete(&adapter->fw_done);
4920 }
4921 if (!completion_done(&adapter->stats_done))
4922 complete(&adapter->stats_done);
Juliet Kim7ed5b312019-09-20 16:11:23 -04004923 if (test_bit(0, &adapter->resetting))
Thomas Falcon2770a792018-05-23 13:38:02 -05004924 adapter->force_reset_recovery = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004925 if (gen_crq->cmd == IBMVNIC_PARTITION_MIGRATED) {
Nathan Fontenoted651a12017-05-03 14:04:38 -04004926 dev_info(dev, "Migrated, re-enabling adapter\n");
4927 ibmvnic_reset(adapter, VNIC_RESET_MOBILITY);
Thomas Falcondfad09a2016-08-18 11:37:51 -05004928 } else if (gen_crq->cmd == IBMVNIC_DEVICE_FAILOVER) {
4929 dev_info(dev, "Backing device failover detected\n");
Thomas Falcon5a18e1e2018-04-06 18:37:05 -05004930 adapter->failover_pending = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004931 } else {
4932 /* The adapter lost the connection */
4933 dev_err(dev, "Virtual Adapter failed (rc=%d)\n",
4934 gen_crq->cmd);
Nathan Fontenoted651a12017-05-03 14:04:38 -04004935 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004936 }
4937 return;
4938 case IBMVNIC_CRQ_CMD_RSP:
4939 break;
4940 default:
4941 dev_err(dev, "Got an invalid msg type 0x%02x\n",
4942 gen_crq->first);
4943 return;
4944 }
4945
4946 switch (gen_crq->cmd) {
4947 case VERSION_EXCHANGE_RSP:
4948 rc = crq->version_exchange_rsp.rc.code;
4949 if (rc) {
4950 dev_err(dev, "Error %ld in VERSION_EXCHG_RSP\n", rc);
4951 break;
4952 }
Thomas Falcon78468892020-05-28 11:19:17 -05004953 ibmvnic_version =
Thomas Falcon032c5e82015-12-21 11:26:06 -06004954 be16_to_cpu(crq->version_exchange_rsp.version);
Thomas Falcon78468892020-05-28 11:19:17 -05004955 dev_info(dev, "Partner protocol version is %d\n",
4956 ibmvnic_version);
Lijun Pan491099a2020-09-27 20:13:26 -05004957 send_query_cap(adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004958 break;
4959 case QUERY_CAPABILITY_RSP:
4960 handle_query_cap_rsp(crq, adapter);
4961 break;
4962 case QUERY_MAP_RSP:
4963 handle_query_map_rsp(crq, adapter);
4964 break;
4965 case REQUEST_MAP_RSP:
Thomas Falconf3be0cb2017-06-21 14:53:01 -05004966 adapter->fw_done_rc = crq->request_map_rsp.rc.code;
4967 complete(&adapter->fw_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004968 break;
4969 case REQUEST_UNMAP_RSP:
4970 handle_request_unmap_rsp(crq, adapter);
4971 break;
4972 case REQUEST_CAPABILITY_RSP:
4973 handle_request_cap_rsp(crq, adapter);
4974 break;
4975 case LOGIN_RSP:
4976 netdev_dbg(netdev, "Got Login Response\n");
4977 handle_login_rsp(crq, adapter);
4978 break;
4979 case LOGICAL_LINK_STATE_RSP:
Nathan Fontenot53da09e2017-04-21 15:39:04 -04004980 netdev_dbg(netdev,
4981 "Got Logical Link State Response, state: %d rc: %d\n",
4982 crq->logical_link_state_rsp.link_state,
4983 crq->logical_link_state_rsp.rc.code);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004984 adapter->logical_link_state =
4985 crq->logical_link_state_rsp.link_state;
Nathan Fontenot53da09e2017-04-21 15:39:04 -04004986 adapter->init_done_rc = crq->logical_link_state_rsp.rc.code;
4987 complete(&adapter->init_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004988 break;
4989 case LINK_STATE_INDICATION:
4990 netdev_dbg(netdev, "Got Logical Link State Indication\n");
4991 adapter->phys_link_state =
4992 crq->link_state_indication.phys_link_state;
4993 adapter->logical_link_state =
4994 crq->link_state_indication.logical_link_state;
Thomas Falcon0655f992019-05-09 23:13:44 -05004995 if (adapter->phys_link_state && adapter->logical_link_state)
4996 netif_carrier_on(netdev);
4997 else
4998 netif_carrier_off(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004999 break;
5000 case CHANGE_MAC_ADDR_RSP:
5001 netdev_dbg(netdev, "Got MAC address change Response\n");
Thomas Falconf8136142018-01-29 13:45:05 -06005002 adapter->fw_done_rc = handle_change_mac_rsp(crq, adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005003 break;
5004 case ERROR_INDICATION:
5005 netdev_dbg(netdev, "Got Error Indication\n");
5006 handle_error_indication(crq, adapter);
5007 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005008 case REQUEST_STATISTICS_RSP:
5009 netdev_dbg(netdev, "Got Statistics Response\n");
5010 complete(&adapter->stats_done);
5011 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005012 case QUERY_IP_OFFLOAD_RSP:
5013 netdev_dbg(netdev, "Got Query IP offload Response\n");
5014 handle_query_ip_offload_rsp(adapter);
5015 break;
5016 case MULTICAST_CTRL_RSP:
5017 netdev_dbg(netdev, "Got multicast control Response\n");
5018 break;
5019 case CONTROL_IP_OFFLOAD_RSP:
5020 netdev_dbg(netdev, "Got Control IP offload Response\n");
5021 dma_unmap_single(dev, adapter->ip_offload_ctrl_tok,
5022 sizeof(adapter->ip_offload_ctrl),
5023 DMA_TO_DEVICE);
John Allenbd0b6722017-03-17 17:13:40 -05005024 complete(&adapter->init_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005025 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005026 case COLLECT_FW_TRACE_RSP:
5027 netdev_dbg(netdev, "Got Collect firmware trace Response\n");
5028 complete(&adapter->fw_done);
5029 break;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02005030 case GET_VPD_SIZE_RSP:
5031 handle_vpd_size_rsp(crq, adapter);
5032 break;
5033 case GET_VPD_RSP:
5034 handle_vpd_rsp(crq, adapter);
5035 break;
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03005036 case QUERY_PHYS_PARMS_RSP:
5037 adapter->fw_done_rc = handle_query_phys_parms_rsp(crq, adapter);
5038 complete(&adapter->fw_done);
5039 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005040 default:
5041 netdev_err(netdev, "Got an invalid cmd type 0x%02x\n",
5042 gen_crq->cmd);
5043 }
5044}
5045
5046static irqreturn_t ibmvnic_interrupt(int irq, void *instance)
5047{
5048 struct ibmvnic_adapter *adapter = instance;
Thomas Falcon6c267b32017-02-15 12:17:58 -06005049
Thomas Falcon6c267b32017-02-15 12:17:58 -06005050 tasklet_schedule(&adapter->tasklet);
Thomas Falcon6c267b32017-02-15 12:17:58 -06005051 return IRQ_HANDLED;
5052}
5053
Allen Paisaa7c3fe2020-09-14 12:59:29 +05305054static void ibmvnic_tasklet(struct tasklet_struct *t)
Thomas Falcon6c267b32017-02-15 12:17:58 -06005055{
Allen Paisaa7c3fe2020-09-14 12:59:29 +05305056 struct ibmvnic_adapter *adapter = from_tasklet(adapter, t, tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005057 struct ibmvnic_crq_queue *queue = &adapter->crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005058 union ibmvnic_crq *crq;
5059 unsigned long flags;
5060 bool done = false;
5061
5062 spin_lock_irqsave(&queue->lock, flags);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005063 while (!done) {
5064 /* Pull all the valid messages off the CRQ */
5065 while ((crq = ibmvnic_next_crq(adapter)) != NULL) {
Lijun Pane41aec72021-01-27 19:34:42 -06005066 /* This barrier makes sure ibmvnic_next_crq()'s
5067 * crq->generic.first & IBMVNIC_CRQ_CMD_RSP is loaded
5068 * before ibmvnic_handle_crq()'s
5069 * switch(gen_crq->first) and switch(gen_crq->cmd).
5070 */
5071 dma_rmb();
Thomas Falcon032c5e82015-12-21 11:26:06 -06005072 ibmvnic_handle_crq(crq, adapter);
5073 crq->generic.first = 0;
5074 }
Brian Kinged7ecbf2017-04-19 13:44:53 -04005075
5076 /* remain in tasklet until all
5077 * capabilities responses are received
5078 */
5079 if (!adapter->wait_capability)
5080 done = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005081 }
Thomas Falcon249168a2017-02-15 12:18:00 -06005082 /* if capabilities CRQ's were sent in this tasklet, the following
5083 * tasklet must wait until all responses are received
5084 */
5085 if (atomic_read(&adapter->running_cap_crqs) != 0)
5086 adapter->wait_capability = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005087 spin_unlock_irqrestore(&queue->lock, flags);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005088}
5089
5090static int ibmvnic_reenable_crq_queue(struct ibmvnic_adapter *adapter)
5091{
5092 struct vio_dev *vdev = adapter->vdev;
5093 int rc;
5094
5095 do {
5096 rc = plpar_hcall_norets(H_ENABLE_CRQ, vdev->unit_address);
5097 } while (rc == H_IN_PROGRESS || rc == H_BUSY || H_IS_LONG_BUSY(rc));
5098
5099 if (rc)
5100 dev_err(&vdev->dev, "Error enabling adapter (rc=%d)\n", rc);
5101
5102 return rc;
5103}
5104
5105static int ibmvnic_reset_crq(struct ibmvnic_adapter *adapter)
5106{
5107 struct ibmvnic_crq_queue *crq = &adapter->crq;
5108 struct device *dev = &adapter->vdev->dev;
5109 struct vio_dev *vdev = adapter->vdev;
5110 int rc;
5111
5112 /* Close the CRQ */
5113 do {
5114 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
5115 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
5116
5117 /* Clean out the queue */
Lijun Pan0e435be2020-11-23 13:35:46 -06005118 if (!crq->msgs)
5119 return -EINVAL;
5120
Thomas Falcon032c5e82015-12-21 11:26:06 -06005121 memset(crq->msgs, 0, PAGE_SIZE);
5122 crq->cur = 0;
Thomas Falcon51536982018-05-23 13:37:56 -05005123 crq->active = false;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005124
5125 /* And re-open it again */
5126 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
5127 crq->msg_token, PAGE_SIZE);
5128
5129 if (rc == H_CLOSED)
5130 /* Adapter is good, but other end is not ready */
5131 dev_warn(dev, "Partner adapter not ready\n");
5132 else if (rc != 0)
5133 dev_warn(dev, "Couldn't register crq (rc=%d)\n", rc);
5134
5135 return rc;
5136}
5137
Nathan Fontenotf9928872017-03-30 02:48:54 -04005138static void release_crq_queue(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06005139{
5140 struct ibmvnic_crq_queue *crq = &adapter->crq;
5141 struct vio_dev *vdev = adapter->vdev;
5142 long rc;
5143
Nathan Fontenotf9928872017-03-30 02:48:54 -04005144 if (!crq->msgs)
5145 return;
5146
Thomas Falcon032c5e82015-12-21 11:26:06 -06005147 netdev_dbg(adapter->netdev, "Releasing CRQ\n");
5148 free_irq(vdev->irq, adapter);
Thomas Falcon6c267b32017-02-15 12:17:58 -06005149 tasklet_kill(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005150 do {
5151 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
5152 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
5153
5154 dma_unmap_single(&vdev->dev, crq->msg_token, PAGE_SIZE,
5155 DMA_BIDIRECTIONAL);
5156 free_page((unsigned long)crq->msgs);
Nathan Fontenotf9928872017-03-30 02:48:54 -04005157 crq->msgs = NULL;
Thomas Falcon51536982018-05-23 13:37:56 -05005158 crq->active = false;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005159}
5160
Nathan Fontenotf9928872017-03-30 02:48:54 -04005161static int init_crq_queue(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06005162{
5163 struct ibmvnic_crq_queue *crq = &adapter->crq;
5164 struct device *dev = &adapter->vdev->dev;
5165 struct vio_dev *vdev = adapter->vdev;
5166 int rc, retrc = -ENOMEM;
5167
Nathan Fontenotf9928872017-03-30 02:48:54 -04005168 if (crq->msgs)
5169 return 0;
5170
Thomas Falcon032c5e82015-12-21 11:26:06 -06005171 crq->msgs = (union ibmvnic_crq *)get_zeroed_page(GFP_KERNEL);
5172 /* Should we allocate more than one page? */
5173
5174 if (!crq->msgs)
5175 return -ENOMEM;
5176
5177 crq->size = PAGE_SIZE / sizeof(*crq->msgs);
5178 crq->msg_token = dma_map_single(dev, crq->msgs, PAGE_SIZE,
5179 DMA_BIDIRECTIONAL);
5180 if (dma_mapping_error(dev, crq->msg_token))
5181 goto map_failed;
5182
5183 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
5184 crq->msg_token, PAGE_SIZE);
5185
5186 if (rc == H_RESOURCE)
5187 /* maybe kexecing and resource is busy. try a reset */
5188 rc = ibmvnic_reset_crq(adapter);
5189 retrc = rc;
5190
5191 if (rc == H_CLOSED) {
5192 dev_warn(dev, "Partner adapter not ready\n");
5193 } else if (rc) {
5194 dev_warn(dev, "Error %d opening adapter\n", rc);
5195 goto reg_crq_failed;
5196 }
5197
5198 retrc = 0;
5199
Allen Paisaa7c3fe2020-09-14 12:59:29 +05305200 tasklet_setup(&adapter->tasklet, (void *)ibmvnic_tasklet);
Thomas Falcon6c267b32017-02-15 12:17:58 -06005201
Thomas Falcon032c5e82015-12-21 11:26:06 -06005202 netdev_dbg(adapter->netdev, "registering irq 0x%x\n", vdev->irq);
Murilo Fossa Vicentinie56e2512019-04-25 11:02:33 -03005203 snprintf(crq->name, sizeof(crq->name), "ibmvnic-%x",
5204 adapter->vdev->unit_address);
5205 rc = request_irq(vdev->irq, ibmvnic_interrupt, 0, crq->name, adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005206 if (rc) {
5207 dev_err(dev, "Couldn't register irq 0x%x. rc=%d\n",
5208 vdev->irq, rc);
5209 goto req_irq_failed;
5210 }
5211
5212 rc = vio_enable_interrupts(vdev);
5213 if (rc) {
5214 dev_err(dev, "Error %d enabling interrupts\n", rc);
5215 goto req_irq_failed;
5216 }
5217
5218 crq->cur = 0;
5219 spin_lock_init(&crq->lock);
5220
5221 return retrc;
5222
5223req_irq_failed:
Thomas Falcon6c267b32017-02-15 12:17:58 -06005224 tasklet_kill(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005225 do {
5226 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
5227 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
5228reg_crq_failed:
5229 dma_unmap_single(dev, crq->msg_token, PAGE_SIZE, DMA_BIDIRECTIONAL);
5230map_failed:
5231 free_page((unsigned long)crq->msgs);
Nathan Fontenotf9928872017-03-30 02:48:54 -04005232 crq->msgs = NULL;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005233 return retrc;
5234}
5235
Lijun Pan635e4422020-08-19 17:52:26 -05005236static int ibmvnic_reset_init(struct ibmvnic_adapter *adapter, bool reset)
John Allenf6ef6402017-03-17 17:13:42 -05005237{
5238 struct device *dev = &adapter->vdev->dev;
Dany Madden98c41f02020-11-25 18:04:32 -06005239 unsigned long timeout = msecs_to_jiffies(20000);
Michal Suchanek6881b072021-03-02 20:47:47 +01005240 u64 old_num_rx_queues = adapter->req_rx_queues;
5241 u64 old_num_tx_queues = adapter->req_tx_queues;
John Allenf6ef6402017-03-17 17:13:42 -05005242 int rc;
5243
John Allen017892c12017-05-26 10:30:19 -04005244 adapter->from_passive_init = false;
5245
Michal Suchanek6881b072021-03-02 20:47:47 +01005246 if (reset)
Lijun Pan635e4422020-08-19 17:52:26 -05005247 reinit_completion(&adapter->init_done);
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06005248
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04005249 adapter->init_done_rc = 0;
Lijun Panfa68bfa2020-08-19 17:52:24 -05005250 rc = ibmvnic_send_crq_init(adapter);
5251 if (rc) {
5252 dev_err(dev, "Send crq init failed with error %d\n", rc);
5253 return rc;
5254 }
5255
John Allenf6ef6402017-03-17 17:13:42 -05005256 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
5257 dev_err(dev, "Initialization sequence timed out\n");
John Allen017892c12017-05-26 10:30:19 -04005258 return -1;
5259 }
5260
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04005261 if (adapter->init_done_rc) {
5262 release_crq_queue(adapter);
5263 return adapter->init_done_rc;
5264 }
5265
Lijun Pan785a2b12020-09-17 21:12:46 -05005266 if (adapter->from_passive_init) {
5267 adapter->state = VNIC_OPEN;
5268 adapter->from_passive_init = false;
5269 return -1;
5270 }
5271
Lijun Pan635e4422020-08-19 17:52:26 -05005272 if (reset &&
5273 test_bit(0, &adapter->resetting) && !adapter->wait_for_reset &&
Nathan Fontenot30f79622018-04-06 18:37:06 -05005274 adapter->reset_reason != VNIC_RESET_MOBILITY) {
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06005275 if (adapter->req_rx_queues != old_num_rx_queues ||
5276 adapter->req_tx_queues != old_num_tx_queues) {
5277 release_sub_crqs(adapter, 0);
5278 rc = init_sub_crqs(adapter);
5279 } else {
5280 rc = reset_sub_crq_queues(adapter);
5281 }
5282 } else {
Nathan Fontenot57a49432017-05-26 10:31:12 -04005283 rc = init_sub_crqs(adapter);
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06005284 }
5285
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04005286 if (rc) {
5287 dev_err(dev, "Initialization of sub crqs failed\n");
5288 release_crq_queue(adapter);
Thomas Falcon5df969c2017-06-28 19:55:54 -05005289 return rc;
5290 }
5291
5292 rc = init_sub_crq_irqs(adapter);
5293 if (rc) {
5294 dev_err(dev, "Failed to initialize sub crq irqs\n");
5295 release_crq_queue(adapter);
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04005296 }
5297
5298 return rc;
John Allenf6ef6402017-03-17 17:13:42 -05005299}
5300
Thomas Falcon40c9db82017-06-12 12:35:04 -05005301static struct device_attribute dev_attr_failover;
5302
Thomas Falcon032c5e82015-12-21 11:26:06 -06005303static int ibmvnic_probe(struct vio_dev *dev, const struct vio_device_id *id)
5304{
5305 struct ibmvnic_adapter *adapter;
5306 struct net_device *netdev;
5307 unsigned char *mac_addr_p;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005308 int rc;
5309
5310 dev_dbg(&dev->dev, "entering ibmvnic_probe for UA 0x%x\n",
5311 dev->unit_address);
5312
5313 mac_addr_p = (unsigned char *)vio_get_attribute(dev,
5314 VETH_MAC_ADDR, NULL);
5315 if (!mac_addr_p) {
5316 dev_err(&dev->dev,
5317 "(%s:%3.3d) ERROR: Can't find MAC_ADDR attribute\n",
5318 __FILE__, __LINE__);
5319 return 0;
5320 }
5321
5322 netdev = alloc_etherdev_mq(sizeof(struct ibmvnic_adapter),
Thomas Falcond45cc3a2017-12-18 12:52:11 -06005323 IBMVNIC_MAX_QUEUES);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005324 if (!netdev)
5325 return -ENOMEM;
5326
5327 adapter = netdev_priv(netdev);
Nathan Fontenot90c80142017-05-03 14:04:32 -04005328 adapter->state = VNIC_PROBING;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005329 dev_set_drvdata(&dev->dev, netdev);
5330 adapter->vdev = dev;
5331 adapter->netdev = netdev;
Sukadev Bhattiprolu76cdc5c2020-11-25 18:04:29 -06005332 adapter->login_pending = false;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005333
5334 ether_addr_copy(adapter->mac_addr, mac_addr_p);
5335 ether_addr_copy(netdev->dev_addr, adapter->mac_addr);
5336 netdev->irq = dev->irq;
5337 netdev->netdev_ops = &ibmvnic_netdev_ops;
5338 netdev->ethtool_ops = &ibmvnic_ethtool_ops;
5339 SET_NETDEV_DEV(netdev, &dev->dev);
5340
Nathan Fontenoted651a12017-05-03 14:04:38 -04005341 INIT_WORK(&adapter->ibmvnic_reset, __ibmvnic_reset);
Juliet Kim7ed5b312019-09-20 16:11:23 -04005342 INIT_DELAYED_WORK(&adapter->ibmvnic_delayed_reset,
5343 __ibmvnic_delayed_reset);
Nathan Fontenoted651a12017-05-03 14:04:38 -04005344 INIT_LIST_HEAD(&adapter->rwi_list);
Thomas Falcon6c5c7482018-12-10 15:22:22 -06005345 spin_lock_init(&adapter->rwi_lock);
Juliet Kim7d7195a2020-03-10 09:23:58 -05005346 spin_lock_init(&adapter->state_lock);
Thomas Falconff25dcb2019-11-25 17:12:56 -06005347 mutex_init(&adapter->fw_lock);
Thomas Falconbbd669a2019-04-04 18:58:26 -05005348 init_completion(&adapter->init_done);
Thomas Falcon070eca92019-11-25 17:12:53 -06005349 init_completion(&adapter->fw_done);
5350 init_completion(&adapter->reset_done);
5351 init_completion(&adapter->stats_done);
Juliet Kim7ed5b312019-09-20 16:11:23 -04005352 clear_bit(0, &adapter->resetting);
Nathan Fontenoted651a12017-05-03 14:04:38 -04005353
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04005354 do {
Nathan Fontenot30f79622018-04-06 18:37:06 -05005355 rc = init_crq_queue(adapter);
5356 if (rc) {
5357 dev_err(&dev->dev, "Couldn't initialize crq. rc=%d\n",
5358 rc);
5359 goto ibmvnic_init_fail;
5360 }
5361
Lijun Pan635e4422020-08-19 17:52:26 -05005362 rc = ibmvnic_reset_init(adapter, false);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05005363 if (rc && rc != EAGAIN)
5364 goto ibmvnic_init_fail;
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04005365 } while (rc == EAGAIN);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005366
Thomas Falcon07184212018-05-16 15:49:05 -05005367 rc = init_stats_buffers(adapter);
5368 if (rc)
5369 goto ibmvnic_init_fail;
5370
5371 rc = init_stats_token(adapter);
5372 if (rc)
5373 goto ibmvnic_stats_fail;
5374
Thomas Falconf39f0d12017-02-14 10:22:59 -06005375 netdev->mtu = adapter->req_mtu - ETH_HLEN;
John Allenc26eba02017-10-26 16:23:25 -05005376 netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
5377 netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005378
Thomas Falcon40c9db82017-06-12 12:35:04 -05005379 rc = device_create_file(&dev->dev, &dev_attr_failover);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05005380 if (rc)
Thomas Falcon07184212018-05-16 15:49:05 -05005381 goto ibmvnic_dev_file_err;
Thomas Falcon40c9db82017-06-12 12:35:04 -05005382
Mick Tarsele876a8a2017-09-28 13:53:18 -07005383 netif_carrier_off(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005384 rc = register_netdev(netdev);
5385 if (rc) {
5386 dev_err(&dev->dev, "failed to register netdev rc=%d\n", rc);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05005387 goto ibmvnic_register_fail;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005388 }
5389 dev_info(&dev->dev, "ibmvnic registered\n");
5390
Nathan Fontenot90c80142017-05-03 14:04:32 -04005391 adapter->state = VNIC_PROBED;
John Allenc26eba02017-10-26 16:23:25 -05005392
5393 adapter->wait_for_reset = false;
Dany Maddena86d5c62020-11-25 18:04:31 -06005394 adapter->last_reset_time = jiffies;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005395 return 0;
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05005396
5397ibmvnic_register_fail:
5398 device_remove_file(&dev->dev, &dev_attr_failover);
5399
Thomas Falcon07184212018-05-16 15:49:05 -05005400ibmvnic_dev_file_err:
5401 release_stats_token(adapter);
5402
5403ibmvnic_stats_fail:
5404 release_stats_buffers(adapter);
5405
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05005406ibmvnic_init_fail:
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06005407 release_sub_crqs(adapter, 1);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05005408 release_crq_queue(adapter);
Thomas Falconff25dcb2019-11-25 17:12:56 -06005409 mutex_destroy(&adapter->fw_lock);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05005410 free_netdev(netdev);
5411
5412 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005413}
5414
Uwe Kleine-König386a9662021-02-25 23:18:34 +01005415static void ibmvnic_remove(struct vio_dev *dev)
Thomas Falcon032c5e82015-12-21 11:26:06 -06005416{
5417 struct net_device *netdev = dev_get_drvdata(&dev->dev);
Nathan Fontenot37489052017-04-19 13:45:04 -04005418 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Juliet Kim7d7195a2020-03-10 09:23:58 -05005419 unsigned long flags;
5420
5421 spin_lock_irqsave(&adapter->state_lock, flags);
Sukadev Bhattiprolu4a41c422021-02-12 20:42:50 -08005422
5423 /* If ibmvnic_reset() is scheduling a reset, wait for it to
5424 * finish. Then, set the state to REMOVING to prevent it from
5425 * scheduling any more work and to have reset functions ignore
5426 * any resets that have already been scheduled. Drop the lock
5427 * after setting state, so __ibmvnic_reset() which is called
5428 * from the flush_work() below, can make progress.
5429 */
Junlin Yang69cdb792021-03-05 16:48:39 +08005430 spin_lock(&adapter->rwi_lock);
Nathan Fontenot90c80142017-05-03 14:04:32 -04005431 adapter->state = VNIC_REMOVING;
Junlin Yang69cdb792021-03-05 16:48:39 +08005432 spin_unlock(&adapter->rwi_lock);
Sukadev Bhattiprolu4a41c422021-02-12 20:42:50 -08005433
Juliet Kim7d7195a2020-03-10 09:23:58 -05005434 spin_unlock_irqrestore(&adapter->state_lock, flags);
5435
Thomas Falcon6954a9e2020-06-12 13:34:41 -05005436 flush_work(&adapter->ibmvnic_reset);
5437 flush_delayed_work(&adapter->ibmvnic_delayed_reset);
5438
Juliet Kima5681e22018-11-19 15:59:22 -06005439 rtnl_lock();
5440 unregister_netdevice(netdev);
Nathan Fontenot37489052017-04-19 13:45:04 -04005441
5442 release_resources(adapter);
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06005443 release_sub_crqs(adapter, 1);
Nathan Fontenot37489052017-04-19 13:45:04 -04005444 release_crq_queue(adapter);
5445
Thomas Falcon53cc7722018-02-26 18:10:56 -06005446 release_stats_token(adapter);
5447 release_stats_buffers(adapter);
5448
Nathan Fontenot90c80142017-05-03 14:04:32 -04005449 adapter->state = VNIC_REMOVED;
5450
Juliet Kima5681e22018-11-19 15:59:22 -06005451 rtnl_unlock();
Thomas Falconff25dcb2019-11-25 17:12:56 -06005452 mutex_destroy(&adapter->fw_lock);
Thomas Falcon40c9db82017-06-12 12:35:04 -05005453 device_remove_file(&dev->dev, &dev_attr_failover);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005454 free_netdev(netdev);
5455 dev_set_drvdata(&dev->dev, NULL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005456}
5457
Thomas Falcon40c9db82017-06-12 12:35:04 -05005458static ssize_t failover_store(struct device *dev, struct device_attribute *attr,
5459 const char *buf, size_t count)
5460{
5461 struct net_device *netdev = dev_get_drvdata(dev);
5462 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
5463 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
5464 __be64 session_token;
5465 long rc;
5466
5467 if (!sysfs_streq(buf, "1"))
5468 return -EINVAL;
5469
5470 rc = plpar_hcall(H_VIOCTL, retbuf, adapter->vdev->unit_address,
5471 H_GET_SESSION_TOKEN, 0, 0, 0);
5472 if (rc) {
5473 netdev_err(netdev, "Couldn't retrieve session token, rc %ld\n",
5474 rc);
5475 return -EINVAL;
5476 }
5477
5478 session_token = (__be64)retbuf[0];
5479 netdev_dbg(netdev, "Initiating client failover, session id %llx\n",
5480 be64_to_cpu(session_token));
5481 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
5482 H_SESSION_ERR_DETECTED, session_token, 0, 0);
5483 if (rc) {
5484 netdev_err(netdev, "Client initiated failover failed, rc %ld\n",
5485 rc);
5486 return -EINVAL;
5487 }
5488
5489 return count;
5490}
5491
Joe Perches6cbaefb2017-12-19 10:15:09 -08005492static DEVICE_ATTR_WO(failover);
Thomas Falcon40c9db82017-06-12 12:35:04 -05005493
Thomas Falcon032c5e82015-12-21 11:26:06 -06005494static unsigned long ibmvnic_get_desired_dma(struct vio_dev *vdev)
5495{
5496 struct net_device *netdev = dev_get_drvdata(&vdev->dev);
5497 struct ibmvnic_adapter *adapter;
5498 struct iommu_table *tbl;
5499 unsigned long ret = 0;
5500 int i;
5501
5502 tbl = get_iommu_table_base(&vdev->dev);
5503
5504 /* netdev inits at probe time along with the structures we need below*/
5505 if (!netdev)
5506 return IOMMU_PAGE_ALIGN(IBMVNIC_IO_ENTITLEMENT_DEFAULT, tbl);
5507
5508 adapter = netdev_priv(netdev);
5509
5510 ret += PAGE_SIZE; /* the crq message queue */
Thomas Falcon032c5e82015-12-21 11:26:06 -06005511 ret += IOMMU_PAGE_ALIGN(sizeof(struct ibmvnic_statistics), tbl);
5512
5513 for (i = 0; i < adapter->req_tx_queues + adapter->req_rx_queues; i++)
5514 ret += 4 * PAGE_SIZE; /* the scrq message queue */
5515
Thomas Falcon507ebe62020-08-21 13:39:01 -05005516 for (i = 0; i < adapter->num_active_rx_pools; i++)
Thomas Falcon032c5e82015-12-21 11:26:06 -06005517 ret += adapter->rx_pool[i].size *
5518 IOMMU_PAGE_ALIGN(adapter->rx_pool[i].buff_size, tbl);
5519
5520 return ret;
5521}
5522
5523static int ibmvnic_resume(struct device *dev)
5524{
5525 struct net_device *netdev = dev_get_drvdata(dev);
5526 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005527
John Allencb89ba22017-06-19 11:27:53 -05005528 if (adapter->state != VNIC_OPEN)
5529 return 0;
5530
John Allena2488782017-07-24 13:26:06 -05005531 tasklet_schedule(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005532
5533 return 0;
5534}
5535
Arvind Yadav8c37bc62017-08-17 18:52:54 +05305536static const struct vio_device_id ibmvnic_device_table[] = {
Thomas Falcon032c5e82015-12-21 11:26:06 -06005537 {"network", "IBM,vnic"},
5538 {"", "" }
5539};
5540MODULE_DEVICE_TABLE(vio, ibmvnic_device_table);
5541
5542static const struct dev_pm_ops ibmvnic_pm_ops = {
5543 .resume = ibmvnic_resume
5544};
5545
5546static struct vio_driver ibmvnic_driver = {
5547 .id_table = ibmvnic_device_table,
5548 .probe = ibmvnic_probe,
5549 .remove = ibmvnic_remove,
5550 .get_desired_dma = ibmvnic_get_desired_dma,
5551 .name = ibmvnic_driver_name,
5552 .pm = &ibmvnic_pm_ops,
5553};
5554
5555/* module functions */
5556static int __init ibmvnic_module_init(void)
5557{
5558 pr_info("%s: %s %s\n", ibmvnic_driver_name, ibmvnic_driver_string,
5559 IBMVNIC_DRIVER_VERSION);
5560
5561 return vio_register_driver(&ibmvnic_driver);
5562}
5563
5564static void __exit ibmvnic_module_exit(void)
5565{
5566 vio_unregister_driver(&ibmvnic_driver);
5567}
5568
5569module_init(ibmvnic_module_init);
5570module_exit(ibmvnic_module_exit);