blob: 110a0d0eaabb5166238b5da4b8bafd4185e072b8 [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
1681 memcpy(dst + cur,
1682 page_address(skb_frag_page(frag)) +
Jonathan Lemonb54c9d52019-07-30 07:40:33 -07001683 skb_frag_off(frag), skb_frag_size(frag));
Thomas Falcon15482052017-10-17 12:36:54 -05001684 cur += skb_frag_size(frag);
1685 }
1686 } else {
1687 skb_copy_from_linear_data(skb, dst, skb->len);
1688 }
1689
Lijun Pan42557da2021-02-12 20:48:40 -06001690 /* post changes to long_term_buff *dst before VIOS accessing it */
1691 dma_wmb();
1692
Thomas Falcon032c5e82015-12-21 11:26:06 -06001693 tx_pool->consumer_index =
Thomas Falcon06b3e352018-03-16 20:00:28 -05001694 (tx_pool->consumer_index + 1) % tx_pool->num_buffers;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001695
1696 tx_buff = &tx_pool->tx_buff[index];
1697 tx_buff->skb = skb;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001698 tx_buff->index = index;
1699 tx_buff->pool_index = queue_num;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001700
1701 memset(&tx_crq, 0, sizeof(tx_crq));
1702 tx_crq.v1.first = IBMVNIC_CRQ_CMD;
1703 tx_crq.v1.type = IBMVNIC_TX_DESC;
1704 tx_crq.v1.n_crq_elem = 1;
1705 tx_crq.v1.n_sge = 1;
1706 tx_crq.v1.flags1 = IBMVNIC_TX_COMP_NEEDED;
Thomas Falcon06b3e352018-03-16 20:00:28 -05001707
Thomas Falconfdb06102017-10-17 12:36:55 -05001708 if (skb_is_gso(skb))
Thomas Falcon06b3e352018-03-16 20:00:28 -05001709 tx_crq.v1.correlator =
1710 cpu_to_be32(index | IBMVNIC_TSO_POOL_MASK);
Thomas Falconfdb06102017-10-17 12:36:55 -05001711 else
Thomas Falcon06b3e352018-03-16 20:00:28 -05001712 tx_crq.v1.correlator = cpu_to_be32(index);
1713 tx_crq.v1.dma_reg = cpu_to_be16(tx_pool->long_term_buff.map_id);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001714 tx_crq.v1.sge_len = cpu_to_be32(skb->len);
1715 tx_crq.v1.ioba = cpu_to_be64(data_dma_addr);
1716
Michał Mirosławe84b4792018-11-07 17:50:52 +01001717 if (adapter->vlan_header_insertion && skb_vlan_tag_present(skb)) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06001718 tx_crq.v1.flags2 |= IBMVNIC_TX_VLAN_INSERT;
1719 tx_crq.v1.vlan_id = cpu_to_be16(skb->vlan_tci);
1720 }
1721
1722 if (skb->protocol == htons(ETH_P_IP)) {
Thomas Falcona0dca102018-01-18 19:29:48 -06001723 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV4;
1724 proto = ip_hdr(skb)->protocol;
1725 } else if (skb->protocol == htons(ETH_P_IPV6)) {
1726 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV6;
1727 proto = ipv6_hdr(skb)->nexthdr;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001728 }
1729
Thomas Falcona0dca102018-01-18 19:29:48 -06001730 if (proto == IPPROTO_TCP)
1731 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_TCP;
1732 else if (proto == IPPROTO_UDP)
1733 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_UDP;
1734
Thomas Falconad7775d2016-04-01 17:20:34 -05001735 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06001736 tx_crq.v1.flags1 |= IBMVNIC_TX_CHKSUM_OFFLOAD;
Thomas Falconad7775d2016-04-01 17:20:34 -05001737 hdrs += 2;
1738 }
Thomas Falconfdb06102017-10-17 12:36:55 -05001739 if (skb_is_gso(skb)) {
1740 tx_crq.v1.flags1 |= IBMVNIC_TX_LSO;
1741 tx_crq.v1.mss = cpu_to_be16(skb_shinfo(skb)->gso_size);
1742 hdrs += 2;
1743 }
Thomas Falcon0d973382020-11-18 19:12:19 -06001744
1745 if ((*hdrs >> 7) & 1)
Thomas Falconc62aa372020-11-18 19:12:20 -06001746 build_hdr_descs_arr(skb, indir_arr, &num_entries, *hdrs);
Thomas Falcon0d973382020-11-18 19:12:19 -06001747
1748 tx_crq.v1.n_crq_elem = num_entries;
1749 tx_buff->num_entries = num_entries;
1750 /* flush buffer if current entry can not fit */
1751 if (num_entries + ind_bufp->index > IBMVNIC_MAX_IND_DESCS) {
1752 lpar_rc = ibmvnic_tx_scrq_flush(adapter, tx_scrq);
1753 if (lpar_rc != H_SUCCESS)
1754 goto tx_flush_err;
Thomas Falconad7775d2016-04-01 17:20:34 -05001755 }
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001756
Thomas Falconc62aa372020-11-18 19:12:20 -06001757 indir_arr[0] = tx_crq;
1758 memcpy(&ind_bufp->indir_arr[ind_bufp->index], &indir_arr[0],
Thomas Falcon0d973382020-11-18 19:12:19 -06001759 num_entries * sizeof(struct ibmvnic_generic_scrq));
1760 ind_bufp->index += num_entries;
1761 if (__netdev_tx_sent_queue(txq, skb->len,
1762 netdev_xmit_more() &&
1763 ind_bufp->index < IBMVNIC_MAX_IND_DESCS)) {
1764 lpar_rc = ibmvnic_tx_scrq_flush(adapter, tx_scrq);
1765 if (lpar_rc != H_SUCCESS)
1766 goto tx_err;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001767 }
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001768
Thomas Falconffc385b2018-02-18 10:08:41 -06001769 if (atomic_add_return(num_entries, &tx_scrq->used)
Brian King58c8c0c2017-04-19 13:44:47 -04001770 >= adapter->req_tx_entries_per_subcrq) {
Thomas Falcon0aecb132018-02-26 18:10:58 -06001771 netdev_dbg(netdev, "Stopping queue %d\n", queue_num);
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001772 netif_stop_subqueue(netdev, queue_num);
1773 }
1774
Thomas Falcon032c5e82015-12-21 11:26:06 -06001775 tx_packets++;
1776 tx_bytes += skb->len;
1777 txq->trans_start = jiffies;
1778 ret = NETDEV_TX_OK;
Thomas Falcon86b61a52018-03-16 20:00:29 -05001779 goto out;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001780
Thomas Falcon0d973382020-11-18 19:12:19 -06001781tx_flush_err:
1782 dev_kfree_skb_any(skb);
1783 tx_buff->skb = NULL;
1784 tx_pool->consumer_index = tx_pool->consumer_index == 0 ?
1785 tx_pool->num_buffers - 1 :
1786 tx_pool->consumer_index - 1;
1787 tx_dropped++;
1788tx_err:
1789 if (lpar_rc != H_CLOSED && lpar_rc != H_PARAMETER)
1790 dev_err_ratelimited(dev, "tx: send failed\n");
1791
1792 if (lpar_rc == H_CLOSED || adapter->failover_pending) {
1793 /* Disable TX and report carrier off if queue is closed
1794 * or pending failover.
1795 * Firmware guarantees that a signal will be sent to the
1796 * driver, triggering a reset or some other action.
1797 */
1798 netif_tx_stop_all_queues(netdev);
1799 netif_carrier_off(netdev);
1800 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001801out:
1802 netdev->stats.tx_dropped += tx_dropped;
1803 netdev->stats.tx_bytes += tx_bytes;
1804 netdev->stats.tx_packets += tx_packets;
1805 adapter->tx_send_failed += tx_send_failed;
1806 adapter->tx_map_failed += tx_map_failed;
John Allen3d52b592017-08-02 16:44:14 -05001807 adapter->tx_stats_buffers[queue_num].packets += tx_packets;
1808 adapter->tx_stats_buffers[queue_num].bytes += tx_bytes;
1809 adapter->tx_stats_buffers[queue_num].dropped_packets += tx_dropped;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001810
1811 return ret;
1812}
1813
1814static void ibmvnic_set_multi(struct net_device *netdev)
1815{
1816 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1817 struct netdev_hw_addr *ha;
1818 union ibmvnic_crq crq;
1819
1820 memset(&crq, 0, sizeof(crq));
1821 crq.request_capability.first = IBMVNIC_CRQ_CMD;
1822 crq.request_capability.cmd = REQUEST_CAPABILITY;
1823
1824 if (netdev->flags & IFF_PROMISC) {
1825 if (!adapter->promisc_supported)
1826 return;
1827 } else {
1828 if (netdev->flags & IFF_ALLMULTI) {
1829 /* Accept all multicast */
1830 memset(&crq, 0, sizeof(crq));
1831 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1832 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1833 crq.multicast_ctrl.flags = IBMVNIC_ENABLE_ALL;
1834 ibmvnic_send_crq(adapter, &crq);
1835 } else if (netdev_mc_empty(netdev)) {
1836 /* Reject all multicast */
1837 memset(&crq, 0, sizeof(crq));
1838 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1839 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1840 crq.multicast_ctrl.flags = IBMVNIC_DISABLE_ALL;
1841 ibmvnic_send_crq(adapter, &crq);
1842 } else {
1843 /* Accept one or more multicast(s) */
1844 netdev_for_each_mc_addr(ha, netdev) {
1845 memset(&crq, 0, sizeof(crq));
1846 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1847 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1848 crq.multicast_ctrl.flags = IBMVNIC_ENABLE_MC;
1849 ether_addr_copy(&crq.multicast_ctrl.mac_addr[0],
1850 ha->addr);
1851 ibmvnic_send_crq(adapter, &crq);
1852 }
1853 }
1854 }
1855}
1856
Thomas Falcon62740e92019-05-09 23:13:43 -05001857static int __ibmvnic_set_mac(struct net_device *netdev, u8 *dev_addr)
Thomas Falcon032c5e82015-12-21 11:26:06 -06001858{
1859 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001860 union ibmvnic_crq crq;
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05001861 int rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001862
Thomas Falcon62740e92019-05-09 23:13:43 -05001863 if (!is_valid_ether_addr(dev_addr)) {
1864 rc = -EADDRNOTAVAIL;
1865 goto err;
1866 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001867
1868 memset(&crq, 0, sizeof(crq));
1869 crq.change_mac_addr.first = IBMVNIC_CRQ_CMD;
1870 crq.change_mac_addr.cmd = CHANGE_MAC_ADDR;
Thomas Falcon62740e92019-05-09 23:13:43 -05001871 ether_addr_copy(&crq.change_mac_addr.mac_addr[0], dev_addr);
Thomas Falconf8136142018-01-29 13:45:05 -06001872
Thomas Falconff25dcb2019-11-25 17:12:56 -06001873 mutex_lock(&adapter->fw_lock);
1874 adapter->fw_done_rc = 0;
Thomas Falcon070eca92019-11-25 17:12:53 -06001875 reinit_completion(&adapter->fw_done);
Thomas Falconff25dcb2019-11-25 17:12:56 -06001876
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05001877 rc = ibmvnic_send_crq(adapter, &crq);
Thomas Falcon62740e92019-05-09 23:13:43 -05001878 if (rc) {
1879 rc = -EIO;
Thomas Falconff25dcb2019-11-25 17:12:56 -06001880 mutex_unlock(&adapter->fw_lock);
Thomas Falcon62740e92019-05-09 23:13:43 -05001881 goto err;
1882 }
1883
Thomas Falcon476d96c2019-11-25 17:12:55 -06001884 rc = ibmvnic_wait_for_completion(adapter, &adapter->fw_done, 10000);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001885 /* netdev->dev_addr is changed in handle_change_mac_rsp function */
Thomas Falcon476d96c2019-11-25 17:12:55 -06001886 if (rc || adapter->fw_done_rc) {
Thomas Falcon62740e92019-05-09 23:13:43 -05001887 rc = -EIO;
Thomas Falconff25dcb2019-11-25 17:12:56 -06001888 mutex_unlock(&adapter->fw_lock);
Thomas Falcon62740e92019-05-09 23:13:43 -05001889 goto err;
1890 }
Thomas Falconff25dcb2019-11-25 17:12:56 -06001891 mutex_unlock(&adapter->fw_lock);
Thomas Falcon62740e92019-05-09 23:13:43 -05001892 return 0;
1893err:
1894 ether_addr_copy(adapter->mac_addr, netdev->dev_addr);
1895 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001896}
1897
John Allenc26eba02017-10-26 16:23:25 -05001898static int ibmvnic_set_mac(struct net_device *netdev, void *p)
1899{
1900 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1901 struct sockaddr *addr = p;
Thomas Falconf8136142018-01-29 13:45:05 -06001902 int rc;
John Allenc26eba02017-10-26 16:23:25 -05001903
Thomas Falcon62740e92019-05-09 23:13:43 -05001904 rc = 0;
Lijun Pan8fc36722020-10-27 17:04:56 -05001905 if (!is_valid_ether_addr(addr->sa_data))
1906 return -EADDRNOTAVAIL;
1907
Jiri Wiesner67eb2112021-03-04 17:18:28 +01001908 ether_addr_copy(adapter->mac_addr, addr->sa_data);
1909 if (adapter->state != VNIC_PROBED)
Thomas Falcon62740e92019-05-09 23:13:43 -05001910 rc = __ibmvnic_set_mac(netdev, addr->sa_data);
John Allenc26eba02017-10-26 16:23:25 -05001911
Thomas Falconf8136142018-01-29 13:45:05 -06001912 return rc;
John Allenc26eba02017-10-26 16:23:25 -05001913}
1914
Lee Jones80708602021-01-15 20:09:03 +00001915/*
Nathan Fontenoted651a12017-05-03 14:04:38 -04001916 * do_reset returns zero if we are able to keep processing reset events, or
1917 * non-zero if we hit a fatal error and must halt.
1918 */
1919static int do_reset(struct ibmvnic_adapter *adapter,
1920 struct ibmvnic_rwi *rwi, u32 reset_state)
1921{
John Allen896d8692018-01-18 16:26:31 -06001922 u64 old_num_rx_queues, old_num_tx_queues;
Thomas Falcon5bf032e2018-11-21 11:17:59 -06001923 u64 old_num_rx_slots, old_num_tx_slots;
Nathan Fontenoted651a12017-05-03 14:04:38 -04001924 struct net_device *netdev = adapter->netdev;
1925 int i, rc;
1926
Sukadev Bhattiprolu38bd5ce2020-12-04 18:22:35 -08001927 netdev_dbg(adapter->netdev,
1928 "[S:%d FOP:%d] Reset reason %d, reset_state %d\n",
1929 adapter->state, adapter->failover_pending,
1930 rwi->reset_reason, reset_state);
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001931
Lijun Pan3f5ec372021-01-06 15:35:14 -06001932 adapter->reset_reason = rwi->reset_reason;
1933 /* requestor of VNIC_RESET_CHANGE_PARAM already has the rtnl lock */
1934 if (!(adapter->reset_reason == VNIC_RESET_CHANGE_PARAM))
1935 rtnl_lock();
1936
Lijun Panbab08be2021-02-11 00:43:19 -06001937 /* Now that we have the rtnl lock, clear any pending failover.
Sukadev Bhattiprolu1d850492020-10-30 10:07:11 -07001938 * This will ensure ibmvnic_open() has either completed or will
1939 * block until failover is complete.
1940 */
1941 if (rwi->reset_reason == VNIC_RESET_FAILOVER)
1942 adapter->failover_pending = false;
Juliet Kimb27507b2019-09-20 16:11:22 -04001943
Sukadev Bhattiprolu8f1c0fd2021-02-23 21:02:29 -08001944 /* read the state and check (again) after getting rtnl */
1945 reset_state = adapter->state;
1946
1947 if (reset_state == VNIC_REMOVING || reset_state == VNIC_REMOVED) {
1948 rc = -EBUSY;
1949 goto out;
1950 }
1951
Nathan Fontenoted651a12017-05-03 14:04:38 -04001952 netif_carrier_off(netdev);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001953
John Allen896d8692018-01-18 16:26:31 -06001954 old_num_rx_queues = adapter->req_rx_queues;
1955 old_num_tx_queues = adapter->req_tx_queues;
Thomas Falcon5bf032e2018-11-21 11:17:59 -06001956 old_num_rx_slots = adapter->req_rx_add_entries_per_subcrq;
1957 old_num_tx_slots = adapter->req_tx_entries_per_subcrq;
John Allen896d8692018-01-18 16:26:31 -06001958
Nathan Fontenot30f79622018-04-06 18:37:06 -05001959 ibmvnic_cleanup(netdev);
1960
Thomas Falcon1f946082019-06-07 16:03:53 -05001961 if (reset_state == VNIC_OPEN &&
1962 adapter->reset_reason != VNIC_RESET_MOBILITY &&
Nathan Fontenot30f79622018-04-06 18:37:06 -05001963 adapter->reset_reason != VNIC_RESET_FAILOVER) {
Lijun Pan3f5ec372021-01-06 15:35:14 -06001964 if (adapter->reset_reason == VNIC_RESET_CHANGE_PARAM) {
1965 rc = __ibmvnic_close(netdev);
1966 if (rc)
1967 goto out;
1968 } else {
1969 adapter->state = VNIC_CLOSING;
Nathan Fontenoted651a12017-05-03 14:04:38 -04001970
Lijun Pan3f5ec372021-01-06 15:35:14 -06001971 /* Release the RTNL lock before link state change and
1972 * re-acquire after the link state change to allow
1973 * linkwatch_event to grab the RTNL lock and run during
1974 * a reset.
1975 */
1976 rtnl_unlock();
1977 rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_DN);
1978 rtnl_lock();
1979 if (rc)
1980 goto out;
Juliet Kimb27507b2019-09-20 16:11:22 -04001981
Sukadev Bhattiprolu8f1c0fd2021-02-23 21:02:29 -08001982 if (adapter->state == VNIC_OPEN) {
1983 /* When we dropped rtnl, ibmvnic_open() got
1984 * it and noticed that we are resetting and
1985 * set the adapter state to OPEN. Update our
1986 * new "target" state, and resume the reset
1987 * from VNIC_CLOSING state.
1988 */
1989 netdev_dbg(netdev,
1990 "Open changed state from %d, updating.\n",
1991 reset_state);
1992 reset_state = VNIC_OPEN;
1993 adapter->state = VNIC_CLOSING;
1994 }
1995
Lijun Pan3f5ec372021-01-06 15:35:14 -06001996 if (adapter->state != VNIC_CLOSING) {
Sukadev Bhattiprolu8f1c0fd2021-02-23 21:02:29 -08001997 /* If someone else changed the adapter state
1998 * when we dropped the rtnl, fail the reset
1999 */
Lijun Pan3f5ec372021-01-06 15:35:14 -06002000 rc = -1;
2001 goto out;
2002 }
Lijun Pan3f5ec372021-01-06 15:35:14 -06002003 adapter->state = VNIC_CLOSED;
Juliet Kimb27507b2019-09-20 16:11:22 -04002004 }
Lijun Pan3f5ec372021-01-06 15:35:14 -06002005 }
Juliet Kimb27507b2019-09-20 16:11:22 -04002006
Lijun Pan3f5ec372021-01-06 15:35:14 -06002007 if (adapter->reset_reason == VNIC_RESET_CHANGE_PARAM) {
2008 release_resources(adapter);
2009 release_sub_crqs(adapter, 1);
2010 release_crq_queue(adapter);
John Allenc26eba02017-10-26 16:23:25 -05002011 }
2012
John Allen8cb31cf2017-05-26 10:30:37 -04002013 if (adapter->reset_reason != VNIC_RESET_NON_FATAL) {
2014 /* remove the closed state so when we call open it appears
2015 * we are coming from the probed state.
2016 */
Nathan Fontenoted651a12017-05-03 14:04:38 -04002017 adapter->state = VNIC_PROBED;
John Allen8cb31cf2017-05-26 10:30:37 -04002018
Lijun Pan3f5ec372021-01-06 15:35:14 -06002019 if (adapter->reset_reason == VNIC_RESET_CHANGE_PARAM) {
2020 rc = init_crq_queue(adapter);
2021 } else if (adapter->reset_reason == VNIC_RESET_MOBILITY) {
Nathan Fontenot30f79622018-04-06 18:37:06 -05002022 rc = ibmvnic_reenable_crq_queue(adapter);
2023 release_sub_crqs(adapter, 1);
2024 } else {
2025 rc = ibmvnic_reset_crq(adapter);
Dany Madden8b40eb732020-06-18 15:24:13 -04002026 if (rc == H_CLOSED || rc == H_SUCCESS) {
Nathan Fontenot30f79622018-04-06 18:37:06 -05002027 rc = vio_enable_interrupts(adapter->vdev);
Dany Madden8b40eb732020-06-18 15:24:13 -04002028 if (rc)
2029 netdev_err(adapter->netdev,
2030 "Reset failed to enable interrupts. rc=%d\n",
2031 rc);
2032 }
Nathan Fontenot30f79622018-04-06 18:37:06 -05002033 }
2034
2035 if (rc) {
2036 netdev_err(adapter->netdev,
Dany Madden8b40eb732020-06-18 15:24:13 -04002037 "Reset couldn't initialize crq. rc=%d\n", rc);
Juliet Kimb27507b2019-09-20 16:11:22 -04002038 goto out;
Nathan Fontenot30f79622018-04-06 18:37:06 -05002039 }
2040
Lijun Pan635e4422020-08-19 17:52:26 -05002041 rc = ibmvnic_reset_init(adapter, true);
Juliet Kimb27507b2019-09-20 16:11:22 -04002042 if (rc) {
2043 rc = IBMVNIC_INIT_FAILED;
2044 goto out;
2045 }
John Allen8cb31cf2017-05-26 10:30:37 -04002046
2047 /* If the adapter was in PROBE state prior to the reset,
2048 * exit here.
2049 */
Juliet Kimb27507b2019-09-20 16:11:22 -04002050 if (reset_state == VNIC_PROBED) {
2051 rc = 0;
2052 goto out;
2053 }
John Allen8cb31cf2017-05-26 10:30:37 -04002054
2055 rc = ibmvnic_login(netdev);
Lijun Panf78afaa2021-02-11 00:43:20 -06002056 if (rc)
Juliet Kimb27507b2019-09-20 16:11:22 -04002057 goto out;
John Allen8cb31cf2017-05-26 10:30:37 -04002058
Lijun Pan3f5ec372021-01-06 15:35:14 -06002059 if (adapter->reset_reason == VNIC_RESET_CHANGE_PARAM) {
2060 rc = init_resources(adapter);
2061 if (rc)
2062 goto out;
2063 } else if (adapter->req_rx_queues != old_num_rx_queues ||
Juliet Kimb27507b2019-09-20 16:11:22 -04002064 adapter->req_tx_queues != old_num_tx_queues ||
2065 adapter->req_rx_add_entries_per_subcrq !=
2066 old_num_rx_slots ||
2067 adapter->req_tx_entries_per_subcrq !=
Mingming Cao9f134572020-08-25 13:26:41 -04002068 old_num_tx_slots ||
2069 !adapter->rx_pool ||
2070 !adapter->tso_pool ||
2071 !adapter->tx_pool) {
John Allen896d8692018-01-18 16:26:31 -06002072 release_rx_pools(adapter);
2073 release_tx_pools(adapter);
Juliet Kima5681e22018-11-19 15:59:22 -06002074 release_napi(adapter);
2075 release_vpd_data(adapter);
2076
2077 rc = init_resources(adapter);
Thomas Falconf611a5b2018-08-30 13:19:53 -05002078 if (rc)
Juliet Kimb27507b2019-09-20 16:11:22 -04002079 goto out;
Nathan Fontenotd9043c12018-02-19 13:30:14 -06002080
John Allenc26eba02017-10-26 16:23:25 -05002081 } else {
2082 rc = reset_tx_pools(adapter);
Jakub Kicinski8ae4dff2020-09-04 21:07:49 -07002083 if (rc) {
Mingming Cao9f134572020-08-25 13:26:41 -04002084 netdev_dbg(adapter->netdev, "reset tx pools failed (%d)\n",
Lijun Pan91dc5d22021-02-11 00:43:22 -06002085 rc);
Juliet Kimb27507b2019-09-20 16:11:22 -04002086 goto out;
Jakub Kicinski8ae4dff2020-09-04 21:07:49 -07002087 }
Nathan Fontenot8c0543a2017-05-26 10:31:06 -04002088
John Allenc26eba02017-10-26 16:23:25 -05002089 rc = reset_rx_pools(adapter);
Jakub Kicinski8ae4dff2020-09-04 21:07:49 -07002090 if (rc) {
Mingming Cao9f134572020-08-25 13:26:41 -04002091 netdev_dbg(adapter->netdev, "reset rx pools failed (%d)\n",
Lijun Pan91dc5d22021-02-11 00:43:22 -06002092 rc);
Juliet Kimb27507b2019-09-20 16:11:22 -04002093 goto out;
Jakub Kicinski8ae4dff2020-09-04 21:07:49 -07002094 }
John Allenc26eba02017-10-26 16:23:25 -05002095 }
Thomas Falcon134bbe72018-05-16 15:49:04 -05002096 ibmvnic_disable_irqs(adapter);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002097 }
John Allene676d812018-03-14 10:41:29 -05002098 adapter->state = VNIC_CLOSED;
2099
Juliet Kimb27507b2019-09-20 16:11:22 -04002100 if (reset_state == VNIC_CLOSED) {
2101 rc = 0;
2102 goto out;
2103 }
John Allene676d812018-03-14 10:41:29 -05002104
Nathan Fontenoted651a12017-05-03 14:04:38 -04002105 rc = __ibmvnic_open(netdev);
2106 if (rc) {
Juliet Kimb27507b2019-09-20 16:11:22 -04002107 rc = IBMVNIC_OPEN_FAILED;
2108 goto out;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002109 }
2110
Thomas Falconbe32a242019-06-07 16:03:54 -05002111 /* refresh device's multicast list */
2112 ibmvnic_set_multi(netdev);
2113
Nathan Fontenoted651a12017-05-03 14:04:38 -04002114 /* kick napi */
2115 for (i = 0; i < adapter->req_rx_queues; i++)
2116 napi_schedule(&adapter->napi[i]);
2117
Lijun Pan98025bc2020-11-20 16:40:12 -06002118 if (adapter->reset_reason == VNIC_RESET_FAILOVER ||
Lijun Pan6be46662020-12-14 15:19:29 -06002119 adapter->reset_reason == VNIC_RESET_MOBILITY)
2120 __netdev_notify_peers(netdev);
Nathan Fontenot61d3e1d2017-06-12 20:47:45 -04002121
Juliet Kimb27507b2019-09-20 16:11:22 -04002122 rc = 0;
2123
2124out:
Dany Madden0cb4bc62020-11-25 18:04:27 -06002125 /* restore the adapter state if reset failed */
2126 if (rc)
2127 adapter->state = reset_state;
Lijun Pan3f5ec372021-01-06 15:35:14 -06002128 /* requestor of VNIC_RESET_CHANGE_PARAM should still hold the rtnl lock */
2129 if (!(adapter->reset_reason == VNIC_RESET_CHANGE_PARAM))
2130 rtnl_unlock();
Juliet Kimb27507b2019-09-20 16:11:22 -04002131
Sukadev Bhattiprolu38bd5ce2020-12-04 18:22:35 -08002132 netdev_dbg(adapter->netdev, "[S:%d FOP:%d] Reset done, rc %d\n",
2133 adapter->state, adapter->failover_pending, rc);
Juliet Kimb27507b2019-09-20 16:11:22 -04002134 return rc;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002135}
2136
Thomas Falcon2770a792018-05-23 13:38:02 -05002137static int do_hard_reset(struct ibmvnic_adapter *adapter,
2138 struct ibmvnic_rwi *rwi, u32 reset_state)
2139{
2140 struct net_device *netdev = adapter->netdev;
2141 int rc;
2142
2143 netdev_dbg(adapter->netdev, "Hard resetting driver (%d)\n",
2144 rwi->reset_reason);
2145
Sukadev Bhattiprolu8f1c0fd2021-02-23 21:02:29 -08002146 /* read the state and check (again) after getting rtnl */
2147 reset_state = adapter->state;
2148
2149 if (reset_state == VNIC_REMOVING || reset_state == VNIC_REMOVED) {
2150 rc = -EBUSY;
2151 goto out;
2152 }
2153
Thomas Falcon2770a792018-05-23 13:38:02 -05002154 netif_carrier_off(netdev);
2155 adapter->reset_reason = rwi->reset_reason;
2156
2157 ibmvnic_cleanup(netdev);
2158 release_resources(adapter);
2159 release_sub_crqs(adapter, 0);
2160 release_crq_queue(adapter);
2161
2162 /* remove the closed state so when we call open it appears
2163 * we are coming from the probed state.
2164 */
2165 adapter->state = VNIC_PROBED;
2166
Thomas Falconbbd669a2019-04-04 18:58:26 -05002167 reinit_completion(&adapter->init_done);
Thomas Falcon2770a792018-05-23 13:38:02 -05002168 rc = init_crq_queue(adapter);
2169 if (rc) {
2170 netdev_err(adapter->netdev,
2171 "Couldn't initialize crq. rc=%d\n", rc);
Dany Madden0cb4bc62020-11-25 18:04:27 -06002172 goto out;
Thomas Falcon2770a792018-05-23 13:38:02 -05002173 }
2174
Lijun Pan635e4422020-08-19 17:52:26 -05002175 rc = ibmvnic_reset_init(adapter, false);
Thomas Falcon2770a792018-05-23 13:38:02 -05002176 if (rc)
Dany Madden0cb4bc62020-11-25 18:04:27 -06002177 goto out;
Thomas Falcon2770a792018-05-23 13:38:02 -05002178
2179 /* If the adapter was in PROBE state prior to the reset,
2180 * exit here.
2181 */
2182 if (reset_state == VNIC_PROBED)
Dany Madden0cb4bc62020-11-25 18:04:27 -06002183 goto out;
Thomas Falcon2770a792018-05-23 13:38:02 -05002184
2185 rc = ibmvnic_login(netdev);
Dany Madden0cb4bc62020-11-25 18:04:27 -06002186 if (rc)
2187 goto out;
Juliet Kima5681e22018-11-19 15:59:22 -06002188
2189 rc = init_resources(adapter);
Thomas Falcon2770a792018-05-23 13:38:02 -05002190 if (rc)
Dany Madden0cb4bc62020-11-25 18:04:27 -06002191 goto out;
Thomas Falcon2770a792018-05-23 13:38:02 -05002192
2193 ibmvnic_disable_irqs(adapter);
2194 adapter->state = VNIC_CLOSED;
2195
2196 if (reset_state == VNIC_CLOSED)
Dany Madden0cb4bc62020-11-25 18:04:27 -06002197 goto out;
Thomas Falcon2770a792018-05-23 13:38:02 -05002198
2199 rc = __ibmvnic_open(netdev);
Dany Madden0cb4bc62020-11-25 18:04:27 -06002200 if (rc) {
2201 rc = IBMVNIC_OPEN_FAILED;
2202 goto out;
2203 }
Thomas Falcon2770a792018-05-23 13:38:02 -05002204
Lijun Pan6be46662020-12-14 15:19:29 -06002205 __netdev_notify_peers(netdev);
Dany Madden0cb4bc62020-11-25 18:04:27 -06002206out:
2207 /* restore adapter state if reset failed */
2208 if (rc)
2209 adapter->state = reset_state;
Sukadev Bhattiprolu38bd5ce2020-12-04 18:22:35 -08002210 netdev_dbg(adapter->netdev, "[S:%d FOP:%d] Hard reset done, rc %d\n",
2211 adapter->state, adapter->failover_pending, rc);
Dany Madden0cb4bc62020-11-25 18:04:27 -06002212 return rc;
Thomas Falcon2770a792018-05-23 13:38:02 -05002213}
2214
Nathan Fontenoted651a12017-05-03 14:04:38 -04002215static struct ibmvnic_rwi *get_next_rwi(struct ibmvnic_adapter *adapter)
2216{
2217 struct ibmvnic_rwi *rwi;
Thomas Falcon6c5c7482018-12-10 15:22:22 -06002218 unsigned long flags;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002219
Thomas Falcon6c5c7482018-12-10 15:22:22 -06002220 spin_lock_irqsave(&adapter->rwi_lock, flags);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002221
2222 if (!list_empty(&adapter->rwi_list)) {
2223 rwi = list_first_entry(&adapter->rwi_list, struct ibmvnic_rwi,
2224 list);
2225 list_del(&rwi->list);
2226 } else {
2227 rwi = NULL;
2228 }
2229
Thomas Falcon6c5c7482018-12-10 15:22:22 -06002230 spin_unlock_irqrestore(&adapter->rwi_lock, flags);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002231 return rwi;
2232}
2233
Nathan Fontenoted651a12017-05-03 14:04:38 -04002234static void __ibmvnic_reset(struct work_struct *work)
2235{
2236 struct ibmvnic_rwi *rwi;
2237 struct ibmvnic_adapter *adapter;
Juliet Kim7d7195a2020-03-10 09:23:58 -05002238 bool saved_state = false;
2239 unsigned long flags;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002240 u32 reset_state;
John Allenc26eba02017-10-26 16:23:25 -05002241 int rc = 0;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002242
2243 adapter = container_of(work, struct ibmvnic_adapter, ibmvnic_reset);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002244
Juliet Kim7ed5b312019-09-20 16:11:23 -04002245 if (test_and_set_bit_lock(0, &adapter->resetting)) {
2246 schedule_delayed_work(&adapter->ibmvnic_delayed_reset,
2247 IBMVNIC_RESET_DELAY);
2248 return;
2249 }
2250
Nathan Fontenoted651a12017-05-03 14:04:38 -04002251 rwi = get_next_rwi(adapter);
2252 while (rwi) {
Juliet Kim7d7195a2020-03-10 09:23:58 -05002253 spin_lock_irqsave(&adapter->state_lock, flags);
2254
Thomas Falcon36f10312019-08-27 11:10:04 -05002255 if (adapter->state == VNIC_REMOVING ||
Michal Suchanekc8dc5592019-09-09 22:44:51 +02002256 adapter->state == VNIC_REMOVED) {
Juliet Kim7d7195a2020-03-10 09:23:58 -05002257 spin_unlock_irqrestore(&adapter->state_lock, flags);
Juliet Kim1c2977c2019-09-05 17:30:01 -04002258 kfree(rwi);
2259 rc = EBUSY;
2260 break;
2261 }
Thomas Falcon36f10312019-08-27 11:10:04 -05002262
Juliet Kim7d7195a2020-03-10 09:23:58 -05002263 if (!saved_state) {
2264 reset_state = adapter->state;
Juliet Kim7d7195a2020-03-10 09:23:58 -05002265 saved_state = true;
2266 }
2267 spin_unlock_irqrestore(&adapter->state_lock, flags);
2268
Lijun Pan3f5ec372021-01-06 15:35:14 -06002269 if (adapter->force_reset_recovery) {
Lijun Panbab08be2021-02-11 00:43:19 -06002270 /* Since we are doing a hard reset now, clear the
Sukadev Bhattiprolu1d850492020-10-30 10:07:11 -07002271 * failover_pending flag so we don't ignore any
2272 * future MOBILITY or other resets.
2273 */
2274 adapter->failover_pending = false;
2275
Juliet Kimb27507b2019-09-20 16:11:22 -04002276 /* Transport event occurred during previous reset */
2277 if (adapter->wait_for_reset) {
2278 /* Previous was CHANGE_PARAM; caller locked */
2279 adapter->force_reset_recovery = false;
2280 rc = do_hard_reset(adapter, rwi, reset_state);
2281 } else {
2282 rtnl_lock();
2283 adapter->force_reset_recovery = false;
2284 rc = do_hard_reset(adapter, rwi, reset_state);
2285 rtnl_unlock();
2286 }
Sukadev Bhattiproluf15fde92020-11-25 18:04:28 -06002287 if (rc) {
2288 /* give backing device time to settle down */
2289 netdev_dbg(adapter->netdev,
2290 "[S:%d] Hard reset failed, waiting 60 secs\n",
2291 adapter->state);
2292 set_current_state(TASK_UNINTERRUPTIBLE);
2293 schedule_timeout(60 * HZ);
2294 }
Lijun Pan1f45dc22020-12-23 14:49:04 -06002295 } else {
Thomas Falcon2770a792018-05-23 13:38:02 -05002296 rc = do_reset(adapter, rwi, reset_state);
2297 }
Nathan Fontenoted651a12017-05-03 14:04:38 -04002298 kfree(rwi);
Dany Maddena86d5c62020-11-25 18:04:31 -06002299 adapter->last_reset_time = jiffies;
Dany Madden0cb4bc62020-11-25 18:04:27 -06002300
Dany Madden18f141b2020-11-25 18:04:25 -06002301 if (rc)
2302 netdev_dbg(adapter->netdev, "Reset failed, rc=%d\n", rc);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002303
2304 rwi = get_next_rwi(adapter);
Juliet Kim7ed5b312019-09-20 16:11:23 -04002305
2306 if (rwi && (rwi->reset_reason == VNIC_RESET_FAILOVER ||
2307 rwi->reset_reason == VNIC_RESET_MOBILITY))
2308 adapter->force_reset_recovery = true;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002309 }
2310
John Allenc26eba02017-10-26 16:23:25 -05002311 if (adapter->wait_for_reset) {
John Allenc26eba02017-10-26 16:23:25 -05002312 adapter->reset_done_rc = rc;
2313 complete(&adapter->reset_done);
2314 }
2315
Juliet Kim7ed5b312019-09-20 16:11:23 -04002316 clear_bit_unlock(0, &adapter->resetting);
Sukadev Bhattiprolu38bd5ce2020-12-04 18:22:35 -08002317
2318 netdev_dbg(adapter->netdev,
2319 "[S:%d FRR:%d WFR:%d] Done processing resets\n",
2320 adapter->state, adapter->force_reset_recovery,
2321 adapter->wait_for_reset);
Juliet Kim7ed5b312019-09-20 16:11:23 -04002322}
2323
2324static void __ibmvnic_delayed_reset(struct work_struct *work)
2325{
2326 struct ibmvnic_adapter *adapter;
2327
2328 adapter = container_of(work, struct ibmvnic_adapter,
2329 ibmvnic_delayed_reset.work);
2330 __ibmvnic_reset(&adapter->ibmvnic_reset);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002331}
2332
Thomas Falconaf894d22018-04-06 18:37:04 -05002333static int ibmvnic_reset(struct ibmvnic_adapter *adapter,
2334 enum ibmvnic_reset_reason reason)
Nathan Fontenoted651a12017-05-03 14:04:38 -04002335{
Thomas Falcon2770a792018-05-23 13:38:02 -05002336 struct list_head *entry, *tmp_entry;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002337 struct ibmvnic_rwi *rwi, *tmp;
2338 struct net_device *netdev = adapter->netdev;
Thomas Falcon6c5c7482018-12-10 15:22:22 -06002339 unsigned long flags;
Thomas Falconaf894d22018-04-06 18:37:04 -05002340 int ret;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002341
Jakub Kicinskib646acd52021-02-16 22:58:44 -08002342 spin_lock_irqsave(&adapter->rwi_lock, flags);
2343
2344 /* If failover is pending don't schedule any other reset.
Sukadev Bhattiprolu1d850492020-10-30 10:07:11 -07002345 * Instead let the failover complete. If there is already a
2346 * a failover reset scheduled, we will detect and drop the
2347 * duplicate reset when walking the ->rwi_list below.
2348 */
Nathan Fontenoted651a12017-05-03 14:04:38 -04002349 if (adapter->state == VNIC_REMOVING ||
Thomas Falcon5a18e1e2018-04-06 18:37:05 -05002350 adapter->state == VNIC_REMOVED ||
Sukadev Bhattiprolu1d850492020-10-30 10:07:11 -07002351 (adapter->failover_pending && reason != VNIC_RESET_FAILOVER)) {
Thomas Falconaf894d22018-04-06 18:37:04 -05002352 ret = EBUSY;
Thomas Falcon5a18e1e2018-04-06 18:37:05 -05002353 netdev_dbg(netdev, "Adapter removing or pending failover, skipping reset\n");
Thomas Falconaf894d22018-04-06 18:37:04 -05002354 goto err;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002355 }
2356
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04002357 if (adapter->state == VNIC_PROBING) {
2358 netdev_warn(netdev, "Adapter reset during probe\n");
Lijun Pan91dc5d22021-02-11 00:43:22 -06002359 adapter->init_done_rc = EAGAIN;
2360 ret = EAGAIN;
Thomas Falconaf894d22018-04-06 18:37:04 -05002361 goto err;
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04002362 }
2363
Nathan Fontenoted651a12017-05-03 14:04:38 -04002364 list_for_each(entry, &adapter->rwi_list) {
2365 tmp = list_entry(entry, struct ibmvnic_rwi, list);
2366 if (tmp->reset_reason == reason) {
Sukadev Bhattiprolu38bd5ce2020-12-04 18:22:35 -08002367 netdev_dbg(netdev, "Skipping matching reset, reason=%d\n",
2368 reason);
Thomas Falconaf894d22018-04-06 18:37:04 -05002369 ret = EBUSY;
2370 goto err;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002371 }
2372 }
2373
Thomas Falcon1d1bbc32018-12-10 15:22:23 -06002374 rwi = kzalloc(sizeof(*rwi), GFP_ATOMIC);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002375 if (!rwi) {
Thomas Falconaf894d22018-04-06 18:37:04 -05002376 ret = ENOMEM;
2377 goto err;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002378 }
Thomas Falcon2770a792018-05-23 13:38:02 -05002379 /* if we just received a transport event,
2380 * flush reset queue and process this reset
2381 */
2382 if (adapter->force_reset_recovery && !list_empty(&adapter->rwi_list)) {
2383 list_for_each_safe(entry, tmp_entry, &adapter->rwi_list)
2384 list_del(entry);
2385 }
Nathan Fontenoted651a12017-05-03 14:04:38 -04002386 rwi->reset_reason = reason;
2387 list_add_tail(&rwi->list, &adapter->rwi_list);
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002388 netdev_dbg(adapter->netdev, "Scheduling reset (reason %d)\n", reason);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002389 schedule_work(&adapter->ibmvnic_reset);
Thomas Falconaf894d22018-04-06 18:37:04 -05002390
Sukadev Bhattiprolu4a41c422021-02-12 20:42:50 -08002391 ret = 0;
Thomas Falconaf894d22018-04-06 18:37:04 -05002392err:
Sukadev Bhattiprolu4a41c422021-02-12 20:42:50 -08002393 /* ibmvnic_close() below can block, so drop the lock first */
2394 spin_unlock_irqrestore(&adapter->rwi_lock, flags);
2395
2396 if (ret == ENOMEM)
2397 ibmvnic_close(netdev);
2398
Thomas Falconaf894d22018-04-06 18:37:04 -05002399 return -ret;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002400}
2401
Michael S. Tsirkin0290bd22019-12-10 09:23:51 -05002402static void ibmvnic_tx_timeout(struct net_device *dev, unsigned int txqueue)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002403{
2404 struct ibmvnic_adapter *adapter = netdev_priv(dev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002405
Lijun Pan855a6312020-11-20 16:40:13 -06002406 if (test_bit(0, &adapter->resetting)) {
2407 netdev_err(adapter->netdev,
2408 "Adapter is resetting, skip timeout reset\n");
2409 return;
2410 }
Dany Maddena86d5c62020-11-25 18:04:31 -06002411 /* No queuing up reset until at least 5 seconds (default watchdog val)
2412 * after last reset
2413 */
2414 if (time_before(jiffies, (adapter->last_reset_time + dev->watchdog_timeo))) {
2415 netdev_dbg(dev, "Not yet time to tx timeout.\n");
2416 return;
2417 }
Nathan Fontenoted651a12017-05-03 14:04:38 -04002418 ibmvnic_reset(adapter, VNIC_RESET_TIMEOUT);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002419}
2420
2421static void remove_buff_from_pool(struct ibmvnic_adapter *adapter,
2422 struct ibmvnic_rx_buff *rx_buff)
2423{
2424 struct ibmvnic_rx_pool *pool = &adapter->rx_pool[rx_buff->pool_index];
2425
2426 rx_buff->skb = NULL;
2427
2428 pool->free_map[pool->next_alloc] = (int)(rx_buff - pool->rx_buff);
2429 pool->next_alloc = (pool->next_alloc + 1) % pool->size;
2430
2431 atomic_dec(&pool->available);
2432}
2433
2434static int ibmvnic_poll(struct napi_struct *napi, int budget)
2435{
Dwip N. Banerjeeec20f362020-11-18 19:12:23 -06002436 struct ibmvnic_sub_crq_queue *rx_scrq;
2437 struct ibmvnic_adapter *adapter;
2438 struct net_device *netdev;
2439 int frames_processed;
2440 int scrq_num;
2441
2442 netdev = napi->dev;
2443 adapter = netdev_priv(netdev);
2444 scrq_num = (int)(napi - adapter->napi);
2445 frames_processed = 0;
2446 rx_scrq = adapter->rx_scrq[scrq_num];
Nathan Fontenot152ce472017-05-26 10:30:54 -04002447
Thomas Falcon032c5e82015-12-21 11:26:06 -06002448restart_poll:
2449 while (frames_processed < budget) {
2450 struct sk_buff *skb;
2451 struct ibmvnic_rx_buff *rx_buff;
2452 union sub_crq *next;
2453 u32 length;
2454 u16 offset;
2455 u8 flags = 0;
2456
Juliet Kim7ed5b312019-09-20 16:11:23 -04002457 if (unlikely(test_bit(0, &adapter->resetting) &&
John Allen34686562018-02-06 16:21:49 -06002458 adapter->reset_reason != VNIC_RESET_NON_FATAL)) {
Dwip N. Banerjeeec20f362020-11-18 19:12:23 -06002459 enable_scrq_irq(adapter, rx_scrq);
Thomas Falcon21ecba62017-06-14 23:50:09 -05002460 napi_complete_done(napi, frames_processed);
2461 return frames_processed;
2462 }
2463
Dwip N. Banerjeeec20f362020-11-18 19:12:23 -06002464 if (!pending_scrq(adapter, rx_scrq))
Thomas Falcon032c5e82015-12-21 11:26:06 -06002465 break;
Dwip N. Banerjeeec20f362020-11-18 19:12:23 -06002466 next = ibmvnic_next_scrq(adapter, rx_scrq);
Lijun Pan914789a2021-02-11 00:43:21 -06002467 rx_buff = (struct ibmvnic_rx_buff *)
2468 be64_to_cpu(next->rx_comp.correlator);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002469 /* do error checking */
2470 if (next->rx_comp.rc) {
John Allene1cea2e2017-08-07 15:42:30 -05002471 netdev_dbg(netdev, "rx buffer returned with rc %x\n",
2472 be16_to_cpu(next->rx_comp.rc));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002473 /* free the entry */
2474 next->rx_comp.first = 0;
Thomas Falcon4b9b0f02018-02-13 18:23:42 -06002475 dev_kfree_skb_any(rx_buff->skb);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002476 remove_buff_from_pool(adapter, rx_buff);
Nathan Fontenotca05e312017-05-03 14:05:14 -04002477 continue;
Thomas Falconabe27a82018-02-19 20:12:57 -06002478 } else if (!rx_buff->skb) {
2479 /* free the entry */
2480 next->rx_comp.first = 0;
2481 remove_buff_from_pool(adapter, rx_buff);
2482 continue;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002483 }
2484
2485 length = be32_to_cpu(next->rx_comp.len);
2486 offset = be16_to_cpu(next->rx_comp.off_frame_data);
2487 flags = next->rx_comp.flags;
2488 skb = rx_buff->skb;
Lijun Pan42557da2021-02-12 20:48:40 -06002489 /* load long_term_buff before copying to skb */
2490 dma_rmb();
Thomas Falcon032c5e82015-12-21 11:26:06 -06002491 skb_copy_to_linear_data(skb, rx_buff->data + offset,
2492 length);
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04002493
2494 /* VLAN Header has been stripped by the system firmware and
2495 * needs to be inserted by the driver
2496 */
2497 if (adapter->rx_vlan_header_insertion &&
2498 (flags & IBMVNIC_VLAN_STRIPPED))
2499 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
2500 ntohs(next->rx_comp.vlan_tci));
2501
Thomas Falcon032c5e82015-12-21 11:26:06 -06002502 /* free the entry */
2503 next->rx_comp.first = 0;
2504 remove_buff_from_pool(adapter, rx_buff);
2505
2506 skb_put(skb, length);
2507 skb->protocol = eth_type_trans(skb, netdev);
Thomas Falcon94ca3052017-05-03 14:05:20 -04002508 skb_record_rx_queue(skb, scrq_num);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002509
2510 if (flags & IBMVNIC_IP_CHKSUM_GOOD &&
2511 flags & IBMVNIC_TCP_UDP_CHKSUM_GOOD) {
2512 skb->ip_summed = CHECKSUM_UNNECESSARY;
2513 }
2514
2515 length = skb->len;
2516 napi_gro_receive(napi, skb); /* send it up */
2517 netdev->stats.rx_packets++;
2518 netdev->stats.rx_bytes += length;
John Allen3d52b592017-08-02 16:44:14 -05002519 adapter->rx_stats_buffers[scrq_num].packets++;
2520 adapter->rx_stats_buffers[scrq_num].bytes += length;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002521 frames_processed++;
2522 }
Nathan Fontenot152ce472017-05-26 10:30:54 -04002523
Dwip N. Banerjee41ed0a02020-11-18 19:12:25 -06002524 if (adapter->state != VNIC_CLOSING &&
2525 ((atomic_read(&adapter->rx_pool[scrq_num].available) <
2526 adapter->req_rx_add_entries_per_subcrq / 2) ||
2527 frames_processed < budget))
Nathan Fontenot152ce472017-05-26 10:30:54 -04002528 replenish_rx_pool(adapter, &adapter->rx_pool[scrq_num]);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002529 if (frames_processed < budget) {
Dwip N. Banerjeeec20f362020-11-18 19:12:23 -06002530 if (napi_complete_done(napi, frames_processed)) {
2531 enable_scrq_irq(adapter, rx_scrq);
2532 if (pending_scrq(adapter, rx_scrq)) {
Dwip N. Banerjeeec20f362020-11-18 19:12:23 -06002533 if (napi_reschedule(napi)) {
2534 disable_scrq_irq(adapter, rx_scrq);
2535 goto restart_poll;
2536 }
2537 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002538 }
2539 }
2540 return frames_processed;
2541}
2542
John Allenc26eba02017-10-26 16:23:25 -05002543static int wait_for_reset(struct ibmvnic_adapter *adapter)
2544{
Thomas Falconaf894d22018-04-06 18:37:04 -05002545 int rc, ret;
2546
John Allenc26eba02017-10-26 16:23:25 -05002547 adapter->fallback.mtu = adapter->req_mtu;
2548 adapter->fallback.rx_queues = adapter->req_rx_queues;
2549 adapter->fallback.tx_queues = adapter->req_tx_queues;
2550 adapter->fallback.rx_entries = adapter->req_rx_add_entries_per_subcrq;
2551 adapter->fallback.tx_entries = adapter->req_tx_entries_per_subcrq;
2552
Thomas Falcon070eca92019-11-25 17:12:53 -06002553 reinit_completion(&adapter->reset_done);
John Allenc26eba02017-10-26 16:23:25 -05002554 adapter->wait_for_reset = true;
Thomas Falconaf894d22018-04-06 18:37:04 -05002555 rc = ibmvnic_reset(adapter, VNIC_RESET_CHANGE_PARAM);
Thomas Falcon476d96c2019-11-25 17:12:55 -06002556
2557 if (rc) {
2558 ret = rc;
2559 goto out;
2560 }
2561 rc = ibmvnic_wait_for_completion(adapter, &adapter->reset_done, 60000);
2562 if (rc) {
2563 ret = -ENODEV;
2564 goto out;
2565 }
John Allenc26eba02017-10-26 16:23:25 -05002566
Thomas Falconaf894d22018-04-06 18:37:04 -05002567 ret = 0;
John Allenc26eba02017-10-26 16:23:25 -05002568 if (adapter->reset_done_rc) {
Thomas Falconaf894d22018-04-06 18:37:04 -05002569 ret = -EIO;
John Allenc26eba02017-10-26 16:23:25 -05002570 adapter->desired.mtu = adapter->fallback.mtu;
2571 adapter->desired.rx_queues = adapter->fallback.rx_queues;
2572 adapter->desired.tx_queues = adapter->fallback.tx_queues;
2573 adapter->desired.rx_entries = adapter->fallback.rx_entries;
2574 adapter->desired.tx_entries = adapter->fallback.tx_entries;
2575
Thomas Falcon070eca92019-11-25 17:12:53 -06002576 reinit_completion(&adapter->reset_done);
Thomas Falconaf894d22018-04-06 18:37:04 -05002577 adapter->wait_for_reset = true;
2578 rc = ibmvnic_reset(adapter, VNIC_RESET_CHANGE_PARAM);
Thomas Falcon476d96c2019-11-25 17:12:55 -06002579 if (rc) {
2580 ret = rc;
2581 goto out;
2582 }
2583 rc = ibmvnic_wait_for_completion(adapter, &adapter->reset_done,
2584 60000);
2585 if (rc) {
2586 ret = -ENODEV;
2587 goto out;
2588 }
John Allenc26eba02017-10-26 16:23:25 -05002589 }
Thomas Falcon476d96c2019-11-25 17:12:55 -06002590out:
John Allenc26eba02017-10-26 16:23:25 -05002591 adapter->wait_for_reset = false;
2592
Thomas Falconaf894d22018-04-06 18:37:04 -05002593 return ret;
John Allenc26eba02017-10-26 16:23:25 -05002594}
2595
John Allen3a807b72017-06-06 16:55:52 -05002596static int ibmvnic_change_mtu(struct net_device *netdev, int new_mtu)
2597{
John Allenc26eba02017-10-26 16:23:25 -05002598 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2599
2600 adapter->desired.mtu = new_mtu + ETH_HLEN;
2601
2602 return wait_for_reset(adapter);
John Allen3a807b72017-06-06 16:55:52 -05002603}
2604
Thomas Falconf10b09e2018-03-12 11:51:05 -05002605static netdev_features_t ibmvnic_features_check(struct sk_buff *skb,
2606 struct net_device *dev,
2607 netdev_features_t features)
2608{
2609 /* Some backing hardware adapters can not
2610 * handle packets with a MSS less than 224
2611 * or with only one segment.
2612 */
2613 if (skb_is_gso(skb)) {
2614 if (skb_shinfo(skb)->gso_size < 224 ||
2615 skb_shinfo(skb)->gso_segs == 1)
2616 features &= ~NETIF_F_GSO_MASK;
2617 }
2618
2619 return features;
2620}
2621
Thomas Falcon032c5e82015-12-21 11:26:06 -06002622static const struct net_device_ops ibmvnic_netdev_ops = {
2623 .ndo_open = ibmvnic_open,
2624 .ndo_stop = ibmvnic_close,
2625 .ndo_start_xmit = ibmvnic_xmit,
2626 .ndo_set_rx_mode = ibmvnic_set_multi,
2627 .ndo_set_mac_address = ibmvnic_set_mac,
2628 .ndo_validate_addr = eth_validate_addr,
Thomas Falcon032c5e82015-12-21 11:26:06 -06002629 .ndo_tx_timeout = ibmvnic_tx_timeout,
John Allen3a807b72017-06-06 16:55:52 -05002630 .ndo_change_mtu = ibmvnic_change_mtu,
Thomas Falconf10b09e2018-03-12 11:51:05 -05002631 .ndo_features_check = ibmvnic_features_check,
Thomas Falcon032c5e82015-12-21 11:26:06 -06002632};
2633
2634/* ethtool functions */
2635
Philippe Reynes8a433792017-01-07 22:37:29 +01002636static int ibmvnic_get_link_ksettings(struct net_device *netdev,
2637 struct ethtool_link_ksettings *cmd)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002638{
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03002639 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2640 int rc;
Philippe Reynes8a433792017-01-07 22:37:29 +01002641
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03002642 rc = send_query_phys_parms(adapter);
2643 if (rc) {
2644 adapter->speed = SPEED_UNKNOWN;
2645 adapter->duplex = DUPLEX_UNKNOWN;
2646 }
2647 cmd->base.speed = adapter->speed;
2648 cmd->base.duplex = adapter->duplex;
Philippe Reynes8a433792017-01-07 22:37:29 +01002649 cmd->base.port = PORT_FIBRE;
2650 cmd->base.phy_address = 0;
2651 cmd->base.autoneg = AUTONEG_ENABLE;
2652
Thomas Falcon032c5e82015-12-21 11:26:06 -06002653 return 0;
2654}
2655
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02002656static void ibmvnic_get_drvinfo(struct net_device *netdev,
Thomas Falcon032c5e82015-12-21 11:26:06 -06002657 struct ethtool_drvinfo *info)
2658{
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02002659 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2660
Lijun Pan8a96c802021-02-11 00:43:25 -06002661 strscpy(info->driver, ibmvnic_driver_name, sizeof(info->driver));
2662 strscpy(info->version, IBMVNIC_DRIVER_VERSION, sizeof(info->version));
2663 strscpy(info->fw_version, adapter->fw_version,
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02002664 sizeof(info->fw_version));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002665}
2666
2667static u32 ibmvnic_get_msglevel(struct net_device *netdev)
2668{
2669 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2670
2671 return adapter->msg_enable;
2672}
2673
2674static void ibmvnic_set_msglevel(struct net_device *netdev, u32 data)
2675{
2676 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2677
2678 adapter->msg_enable = data;
2679}
2680
2681static u32 ibmvnic_get_link(struct net_device *netdev)
2682{
2683 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2684
2685 /* Don't need to send a query because we request a logical link up at
2686 * init and then we wait for link state indications
2687 */
2688 return adapter->logical_link_state;
2689}
2690
2691static void ibmvnic_get_ringparam(struct net_device *netdev,
2692 struct ethtool_ringparam *ring)
2693{
John Allenbc131b32017-08-02 16:46:30 -05002694 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2695
Thomas Falcon723ad912018-09-28 18:38:26 -05002696 if (adapter->priv_flags & IBMVNIC_USE_SERVER_MAXES) {
2697 ring->rx_max_pending = adapter->max_rx_add_entries_per_subcrq;
2698 ring->tx_max_pending = adapter->max_tx_entries_per_subcrq;
2699 } else {
2700 ring->rx_max_pending = IBMVNIC_MAX_QUEUE_SZ;
2701 ring->tx_max_pending = IBMVNIC_MAX_QUEUE_SZ;
2702 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002703 ring->rx_mini_max_pending = 0;
2704 ring->rx_jumbo_max_pending = 0;
John Allenbc131b32017-08-02 16:46:30 -05002705 ring->rx_pending = adapter->req_rx_add_entries_per_subcrq;
2706 ring->tx_pending = adapter->req_tx_entries_per_subcrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002707 ring->rx_mini_pending = 0;
2708 ring->rx_jumbo_pending = 0;
2709}
2710
John Allenc26eba02017-10-26 16:23:25 -05002711static int ibmvnic_set_ringparam(struct net_device *netdev,
2712 struct ethtool_ringparam *ring)
2713{
2714 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Thomas Falcon723ad912018-09-28 18:38:26 -05002715 int ret;
John Allenc26eba02017-10-26 16:23:25 -05002716
Thomas Falcon723ad912018-09-28 18:38:26 -05002717 ret = 0;
John Allenc26eba02017-10-26 16:23:25 -05002718 adapter->desired.rx_entries = ring->rx_pending;
2719 adapter->desired.tx_entries = ring->tx_pending;
2720
Thomas Falcon723ad912018-09-28 18:38:26 -05002721 ret = wait_for_reset(adapter);
2722
2723 if (!ret &&
2724 (adapter->req_rx_add_entries_per_subcrq != ring->rx_pending ||
2725 adapter->req_tx_entries_per_subcrq != ring->tx_pending))
2726 netdev_info(netdev,
2727 "Could not match full ringsize request. Requested: RX %d, TX %d; Allowed: RX %llu, TX %llu\n",
2728 ring->rx_pending, ring->tx_pending,
2729 adapter->req_rx_add_entries_per_subcrq,
2730 adapter->req_tx_entries_per_subcrq);
2731 return ret;
John Allenc26eba02017-10-26 16:23:25 -05002732}
2733
John Allenc2dbeb62017-08-02 16:47:17 -05002734static void ibmvnic_get_channels(struct net_device *netdev,
2735 struct ethtool_channels *channels)
2736{
2737 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2738
Thomas Falcon723ad912018-09-28 18:38:26 -05002739 if (adapter->priv_flags & IBMVNIC_USE_SERVER_MAXES) {
2740 channels->max_rx = adapter->max_rx_queues;
2741 channels->max_tx = adapter->max_tx_queues;
2742 } else {
2743 channels->max_rx = IBMVNIC_MAX_QUEUES;
2744 channels->max_tx = IBMVNIC_MAX_QUEUES;
2745 }
2746
John Allenc2dbeb62017-08-02 16:47:17 -05002747 channels->max_other = 0;
2748 channels->max_combined = 0;
2749 channels->rx_count = adapter->req_rx_queues;
2750 channels->tx_count = adapter->req_tx_queues;
2751 channels->other_count = 0;
2752 channels->combined_count = 0;
2753}
2754
John Allenc26eba02017-10-26 16:23:25 -05002755static int ibmvnic_set_channels(struct net_device *netdev,
2756 struct ethtool_channels *channels)
2757{
2758 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Thomas Falcon723ad912018-09-28 18:38:26 -05002759 int ret;
John Allenc26eba02017-10-26 16:23:25 -05002760
Thomas Falcon723ad912018-09-28 18:38:26 -05002761 ret = 0;
John Allenc26eba02017-10-26 16:23:25 -05002762 adapter->desired.rx_queues = channels->rx_count;
2763 adapter->desired.tx_queues = channels->tx_count;
2764
Thomas Falcon723ad912018-09-28 18:38:26 -05002765 ret = wait_for_reset(adapter);
2766
2767 if (!ret &&
2768 (adapter->req_rx_queues != channels->rx_count ||
2769 adapter->req_tx_queues != channels->tx_count))
2770 netdev_info(netdev,
2771 "Could not match full channels request. Requested: RX %d, TX %d; Allowed: RX %llu, TX %llu\n",
2772 channels->rx_count, channels->tx_count,
2773 adapter->req_rx_queues, adapter->req_tx_queues);
2774 return ret;
John Allenc26eba02017-10-26 16:23:25 -05002775}
2776
Thomas Falcon032c5e82015-12-21 11:26:06 -06002777static void ibmvnic_get_strings(struct net_device *dev, u32 stringset, u8 *data)
2778{
John Allen3d52b592017-08-02 16:44:14 -05002779 struct ibmvnic_adapter *adapter = netdev_priv(dev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002780 int i;
2781
Thomas Falcon723ad912018-09-28 18:38:26 -05002782 switch (stringset) {
2783 case ETH_SS_STATS:
2784 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats);
2785 i++, data += ETH_GSTRING_LEN)
2786 memcpy(data, ibmvnic_stats[i].name, ETH_GSTRING_LEN);
2787
2788 for (i = 0; i < adapter->req_tx_queues; i++) {
2789 snprintf(data, ETH_GSTRING_LEN, "tx%d_packets", i);
2790 data += ETH_GSTRING_LEN;
2791
2792 snprintf(data, ETH_GSTRING_LEN, "tx%d_bytes", i);
2793 data += ETH_GSTRING_LEN;
2794
2795 snprintf(data, ETH_GSTRING_LEN,
2796 "tx%d_dropped_packets", i);
2797 data += ETH_GSTRING_LEN;
2798 }
2799
2800 for (i = 0; i < adapter->req_rx_queues; i++) {
2801 snprintf(data, ETH_GSTRING_LEN, "rx%d_packets", i);
2802 data += ETH_GSTRING_LEN;
2803
2804 snprintf(data, ETH_GSTRING_LEN, "rx%d_bytes", i);
2805 data += ETH_GSTRING_LEN;
2806
2807 snprintf(data, ETH_GSTRING_LEN, "rx%d_interrupts", i);
2808 data += ETH_GSTRING_LEN;
2809 }
2810 break;
2811
2812 case ETH_SS_PRIV_FLAGS:
2813 for (i = 0; i < ARRAY_SIZE(ibmvnic_priv_flags); i++)
2814 strcpy(data + i * ETH_GSTRING_LEN,
2815 ibmvnic_priv_flags[i]);
2816 break;
2817 default:
Thomas Falcon032c5e82015-12-21 11:26:06 -06002818 return;
John Allen3d52b592017-08-02 16:44:14 -05002819 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002820}
2821
2822static int ibmvnic_get_sset_count(struct net_device *dev, int sset)
2823{
John Allen3d52b592017-08-02 16:44:14 -05002824 struct ibmvnic_adapter *adapter = netdev_priv(dev);
2825
Thomas Falcon032c5e82015-12-21 11:26:06 -06002826 switch (sset) {
2827 case ETH_SS_STATS:
John Allen3d52b592017-08-02 16:44:14 -05002828 return ARRAY_SIZE(ibmvnic_stats) +
2829 adapter->req_tx_queues * NUM_TX_STATS +
2830 adapter->req_rx_queues * NUM_RX_STATS;
Thomas Falcon723ad912018-09-28 18:38:26 -05002831 case ETH_SS_PRIV_FLAGS:
2832 return ARRAY_SIZE(ibmvnic_priv_flags);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002833 default:
2834 return -EOPNOTSUPP;
2835 }
2836}
2837
2838static void ibmvnic_get_ethtool_stats(struct net_device *dev,
2839 struct ethtool_stats *stats, u64 *data)
2840{
2841 struct ibmvnic_adapter *adapter = netdev_priv(dev);
2842 union ibmvnic_crq crq;
John Allen3d52b592017-08-02 16:44:14 -05002843 int i, j;
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05002844 int rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002845
2846 memset(&crq, 0, sizeof(crq));
2847 crq.request_statistics.first = IBMVNIC_CRQ_CMD;
2848 crq.request_statistics.cmd = REQUEST_STATISTICS;
2849 crq.request_statistics.ioba = cpu_to_be32(adapter->stats_token);
2850 crq.request_statistics.len =
2851 cpu_to_be32(sizeof(struct ibmvnic_statistics));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002852
2853 /* Wait for data to be written */
Thomas Falcon070eca92019-11-25 17:12:53 -06002854 reinit_completion(&adapter->stats_done);
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05002855 rc = ibmvnic_send_crq(adapter, &crq);
2856 if (rc)
2857 return;
Thomas Falcon476d96c2019-11-25 17:12:55 -06002858 rc = ibmvnic_wait_for_completion(adapter, &adapter->stats_done, 10000);
2859 if (rc)
2860 return;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002861
2862 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++)
Lijun Pan91dc5d22021-02-11 00:43:22 -06002863 data[i] = be64_to_cpu(IBMVNIC_GET_STAT
2864 (adapter, ibmvnic_stats[i].offset));
John Allen3d52b592017-08-02 16:44:14 -05002865
2866 for (j = 0; j < adapter->req_tx_queues; j++) {
2867 data[i] = adapter->tx_stats_buffers[j].packets;
2868 i++;
2869 data[i] = adapter->tx_stats_buffers[j].bytes;
2870 i++;
2871 data[i] = adapter->tx_stats_buffers[j].dropped_packets;
2872 i++;
2873 }
2874
2875 for (j = 0; j < adapter->req_rx_queues; j++) {
2876 data[i] = adapter->rx_stats_buffers[j].packets;
2877 i++;
2878 data[i] = adapter->rx_stats_buffers[j].bytes;
2879 i++;
2880 data[i] = adapter->rx_stats_buffers[j].interrupts;
2881 i++;
2882 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002883}
2884
Thomas Falcon723ad912018-09-28 18:38:26 -05002885static u32 ibmvnic_get_priv_flags(struct net_device *netdev)
2886{
2887 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2888
2889 return adapter->priv_flags;
2890}
2891
2892static int ibmvnic_set_priv_flags(struct net_device *netdev, u32 flags)
2893{
2894 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2895 bool which_maxes = !!(flags & IBMVNIC_USE_SERVER_MAXES);
2896
2897 if (which_maxes)
2898 adapter->priv_flags |= IBMVNIC_USE_SERVER_MAXES;
2899 else
2900 adapter->priv_flags &= ~IBMVNIC_USE_SERVER_MAXES;
2901
2902 return 0;
2903}
Lijun Pan91dc5d22021-02-11 00:43:22 -06002904
Thomas Falcon032c5e82015-12-21 11:26:06 -06002905static const struct ethtool_ops ibmvnic_ethtool_ops = {
Thomas Falcon032c5e82015-12-21 11:26:06 -06002906 .get_drvinfo = ibmvnic_get_drvinfo,
2907 .get_msglevel = ibmvnic_get_msglevel,
2908 .set_msglevel = ibmvnic_set_msglevel,
2909 .get_link = ibmvnic_get_link,
2910 .get_ringparam = ibmvnic_get_ringparam,
John Allenc26eba02017-10-26 16:23:25 -05002911 .set_ringparam = ibmvnic_set_ringparam,
John Allenc2dbeb62017-08-02 16:47:17 -05002912 .get_channels = ibmvnic_get_channels,
John Allenc26eba02017-10-26 16:23:25 -05002913 .set_channels = ibmvnic_set_channels,
Thomas Falcon032c5e82015-12-21 11:26:06 -06002914 .get_strings = ibmvnic_get_strings,
2915 .get_sset_count = ibmvnic_get_sset_count,
2916 .get_ethtool_stats = ibmvnic_get_ethtool_stats,
Philippe Reynes8a433792017-01-07 22:37:29 +01002917 .get_link_ksettings = ibmvnic_get_link_ksettings,
Thomas Falcon723ad912018-09-28 18:38:26 -05002918 .get_priv_flags = ibmvnic_get_priv_flags,
2919 .set_priv_flags = ibmvnic_set_priv_flags,
Thomas Falcon032c5e82015-12-21 11:26:06 -06002920};
2921
2922/* Routines for managing CRQs/sCRQs */
2923
Nathan Fontenot57a49432017-05-26 10:31:12 -04002924static int reset_one_sub_crq_queue(struct ibmvnic_adapter *adapter,
2925 struct ibmvnic_sub_crq_queue *scrq)
2926{
2927 int rc;
2928
Dany Madden9281cf22020-11-25 18:04:26 -06002929 if (!scrq) {
YANG LI862aecb2020-12-30 15:23:14 +08002930 netdev_dbg(adapter->netdev, "Invalid scrq reset.\n");
Dany Madden9281cf22020-11-25 18:04:26 -06002931 return -EINVAL;
2932 }
2933
Nathan Fontenot57a49432017-05-26 10:31:12 -04002934 if (scrq->irq) {
2935 free_irq(scrq->irq, scrq);
2936 irq_dispose_mapping(scrq->irq);
2937 scrq->irq = 0;
2938 }
2939
Dany Madden9281cf22020-11-25 18:04:26 -06002940 if (scrq->msgs) {
2941 memset(scrq->msgs, 0, 4 * PAGE_SIZE);
2942 atomic_set(&scrq->used, 0);
2943 scrq->cur = 0;
Jakub Kicinski55fd59b2020-12-03 15:42:13 -08002944 scrq->ind_buf.index = 0;
Dany Madden9281cf22020-11-25 18:04:26 -06002945 } else {
2946 netdev_dbg(adapter->netdev, "Invalid scrq reset\n");
2947 return -EINVAL;
2948 }
Nathan Fontenot57a49432017-05-26 10:31:12 -04002949
2950 rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
2951 4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
2952 return rc;
2953}
2954
2955static int reset_sub_crq_queues(struct ibmvnic_adapter *adapter)
2956{
2957 int i, rc;
2958
Lijun Pana0faaa22020-11-23 13:35:45 -06002959 if (!adapter->tx_scrq || !adapter->rx_scrq)
2960 return -EINVAL;
2961
Nathan Fontenot57a49432017-05-26 10:31:12 -04002962 for (i = 0; i < adapter->req_tx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002963 netdev_dbg(adapter->netdev, "Re-setting tx_scrq[%d]\n", i);
Nathan Fontenot57a49432017-05-26 10:31:12 -04002964 rc = reset_one_sub_crq_queue(adapter, adapter->tx_scrq[i]);
2965 if (rc)
2966 return rc;
2967 }
2968
2969 for (i = 0; i < adapter->req_rx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002970 netdev_dbg(adapter->netdev, "Re-setting rx_scrq[%d]\n", i);
Nathan Fontenot57a49432017-05-26 10:31:12 -04002971 rc = reset_one_sub_crq_queue(adapter, adapter->rx_scrq[i]);
2972 if (rc)
2973 return rc;
2974 }
2975
Nathan Fontenot57a49432017-05-26 10:31:12 -04002976 return rc;
2977}
2978
Thomas Falcon032c5e82015-12-21 11:26:06 -06002979static void release_sub_crq_queue(struct ibmvnic_adapter *adapter,
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06002980 struct ibmvnic_sub_crq_queue *scrq,
2981 bool do_h_free)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002982{
2983 struct device *dev = &adapter->vdev->dev;
2984 long rc;
2985
2986 netdev_dbg(adapter->netdev, "Releasing sub-CRQ\n");
2987
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06002988 if (do_h_free) {
2989 /* Close the sub-crqs */
2990 do {
2991 rc = plpar_hcall_norets(H_FREE_SUB_CRQ,
2992 adapter->vdev->unit_address,
2993 scrq->crq_num);
2994 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002995
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06002996 if (rc) {
2997 netdev_err(adapter->netdev,
2998 "Failed to release sub-CRQ %16lx, rc = %ld\n",
2999 scrq->crq_num, rc);
3000 }
Thomas Falconffa73852017-04-19 13:44:29 -04003001 }
3002
Thomas Falconf019fb62020-11-18 19:12:17 -06003003 dma_free_coherent(dev,
3004 IBMVNIC_IND_ARR_SZ,
3005 scrq->ind_buf.indir_arr,
3006 scrq->ind_buf.indir_dma);
3007
Thomas Falcon032c5e82015-12-21 11:26:06 -06003008 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
3009 DMA_BIDIRECTIONAL);
3010 free_pages((unsigned long)scrq->msgs, 2);
3011 kfree(scrq);
3012}
3013
3014static struct ibmvnic_sub_crq_queue *init_sub_crq_queue(struct ibmvnic_adapter
3015 *adapter)
3016{
3017 struct device *dev = &adapter->vdev->dev;
3018 struct ibmvnic_sub_crq_queue *scrq;
3019 int rc;
3020
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04003021 scrq = kzalloc(sizeof(*scrq), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003022 if (!scrq)
3023 return NULL;
3024
Nathan Fontenot7f7adc52017-04-19 13:45:16 -04003025 scrq->msgs =
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04003026 (union sub_crq *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, 2);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003027 if (!scrq->msgs) {
3028 dev_warn(dev, "Couldn't allocate crq queue messages page\n");
3029 goto zero_page_failed;
3030 }
3031
3032 scrq->msg_token = dma_map_single(dev, scrq->msgs, 4 * PAGE_SIZE,
3033 DMA_BIDIRECTIONAL);
3034 if (dma_mapping_error(dev, scrq->msg_token)) {
3035 dev_warn(dev, "Couldn't map crq queue messages page\n");
3036 goto map_failed;
3037 }
3038
3039 rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
3040 4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
3041
3042 if (rc == H_RESOURCE)
3043 rc = ibmvnic_reset_crq(adapter);
3044
3045 if (rc == H_CLOSED) {
3046 dev_warn(dev, "Partner adapter not ready, waiting.\n");
3047 } else if (rc) {
3048 dev_warn(dev, "Error %d registering sub-crq\n", rc);
3049 goto reg_failed;
3050 }
3051
Thomas Falcon032c5e82015-12-21 11:26:06 -06003052 scrq->adapter = adapter;
3053 scrq->size = 4 * PAGE_SIZE / sizeof(*scrq->msgs);
Thomas Falconf019fb62020-11-18 19:12:17 -06003054 scrq->ind_buf.index = 0;
3055
3056 scrq->ind_buf.indir_arr =
3057 dma_alloc_coherent(dev,
3058 IBMVNIC_IND_ARR_SZ,
3059 &scrq->ind_buf.indir_dma,
3060 GFP_KERNEL);
3061
3062 if (!scrq->ind_buf.indir_arr)
3063 goto indir_failed;
3064
Thomas Falcon032c5e82015-12-21 11:26:06 -06003065 spin_lock_init(&scrq->lock);
3066
3067 netdev_dbg(adapter->netdev,
3068 "sub-crq initialized, num %lx, hw_irq=%lx, irq=%x\n",
3069 scrq->crq_num, scrq->hw_irq, scrq->irq);
3070
3071 return scrq;
3072
Thomas Falconf019fb62020-11-18 19:12:17 -06003073indir_failed:
3074 do {
3075 rc = plpar_hcall_norets(H_FREE_SUB_CRQ,
3076 adapter->vdev->unit_address,
3077 scrq->crq_num);
3078 } while (rc == H_BUSY || rc == H_IS_LONG_BUSY(rc));
Thomas Falcon032c5e82015-12-21 11:26:06 -06003079reg_failed:
3080 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
3081 DMA_BIDIRECTIONAL);
3082map_failed:
3083 free_pages((unsigned long)scrq->msgs, 2);
3084zero_page_failed:
3085 kfree(scrq);
3086
3087 return NULL;
3088}
3089
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06003090static void release_sub_crqs(struct ibmvnic_adapter *adapter, bool do_h_free)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003091{
3092 int i;
3093
3094 if (adapter->tx_scrq) {
Nathan Fontenot82e3be32018-02-21 21:33:56 -06003095 for (i = 0; i < adapter->num_active_tx_scrqs; i++) {
Nathan Fontenotb5108882017-03-30 02:49:18 -04003096 if (!adapter->tx_scrq[i])
3097 continue;
3098
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05003099 netdev_dbg(adapter->netdev, "Releasing tx_scrq[%d]\n",
3100 i);
Nathan Fontenotb5108882017-03-30 02:49:18 -04003101 if (adapter->tx_scrq[i]->irq) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06003102 free_irq(adapter->tx_scrq[i]->irq,
3103 adapter->tx_scrq[i]);
Thomas Falcon88eb98a2016-07-06 15:35:16 -05003104 irq_dispose_mapping(adapter->tx_scrq[i]->irq);
Nathan Fontenotb5108882017-03-30 02:49:18 -04003105 adapter->tx_scrq[i]->irq = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003106 }
Nathan Fontenotb5108882017-03-30 02:49:18 -04003107
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06003108 release_sub_crq_queue(adapter, adapter->tx_scrq[i],
3109 do_h_free);
Nathan Fontenotb5108882017-03-30 02:49:18 -04003110 }
3111
Nathan Fontenot9501df32017-03-15 23:38:07 -04003112 kfree(adapter->tx_scrq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003113 adapter->tx_scrq = NULL;
Nathan Fontenot82e3be32018-02-21 21:33:56 -06003114 adapter->num_active_tx_scrqs = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003115 }
3116
3117 if (adapter->rx_scrq) {
Nathan Fontenot82e3be32018-02-21 21:33:56 -06003118 for (i = 0; i < adapter->num_active_rx_scrqs; i++) {
Nathan Fontenotb5108882017-03-30 02:49:18 -04003119 if (!adapter->rx_scrq[i])
3120 continue;
3121
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05003122 netdev_dbg(adapter->netdev, "Releasing rx_scrq[%d]\n",
3123 i);
Nathan Fontenotb5108882017-03-30 02:49:18 -04003124 if (adapter->rx_scrq[i]->irq) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06003125 free_irq(adapter->rx_scrq[i]->irq,
3126 adapter->rx_scrq[i]);
Thomas Falcon88eb98a2016-07-06 15:35:16 -05003127 irq_dispose_mapping(adapter->rx_scrq[i]->irq);
Nathan Fontenotb5108882017-03-30 02:49:18 -04003128 adapter->rx_scrq[i]->irq = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003129 }
Nathan Fontenotb5108882017-03-30 02:49:18 -04003130
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06003131 release_sub_crq_queue(adapter, adapter->rx_scrq[i],
3132 do_h_free);
Nathan Fontenotb5108882017-03-30 02:49:18 -04003133 }
3134
Nathan Fontenot9501df32017-03-15 23:38:07 -04003135 kfree(adapter->rx_scrq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003136 adapter->rx_scrq = NULL;
Nathan Fontenot82e3be32018-02-21 21:33:56 -06003137 adapter->num_active_rx_scrqs = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003138 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06003139}
3140
3141static int disable_scrq_irq(struct ibmvnic_adapter *adapter,
3142 struct ibmvnic_sub_crq_queue *scrq)
3143{
3144 struct device *dev = &adapter->vdev->dev;
3145 unsigned long rc;
3146
3147 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
3148 H_DISABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
3149 if (rc)
3150 dev_err(dev, "Couldn't disable scrq irq 0x%lx. rc=%ld\n",
3151 scrq->hw_irq, rc);
3152 return rc;
3153}
3154
3155static int enable_scrq_irq(struct ibmvnic_adapter *adapter,
3156 struct ibmvnic_sub_crq_queue *scrq)
3157{
3158 struct device *dev = &adapter->vdev->dev;
3159 unsigned long rc;
3160
3161 if (scrq->hw_irq > 0x100000000ULL) {
3162 dev_err(dev, "bad hw_irq = %lx\n", scrq->hw_irq);
3163 return 1;
3164 }
3165
Juliet Kim7ed5b312019-09-20 16:11:23 -04003166 if (test_bit(0, &adapter->resetting) &&
Nathan Fontenot73f9d362018-05-22 11:21:10 -05003167 adapter->reset_reason == VNIC_RESET_MOBILITY) {
Juliet Kim284f87d2019-11-20 10:50:03 -05003168 u64 val = (0xff000000) | scrq->hw_irq;
Nathan Fontenot73f9d362018-05-22 11:21:10 -05003169
Juliet Kim284f87d2019-11-20 10:50:03 -05003170 rc = plpar_hcall_norets(H_EOI, val);
Juliet Kim2df5c602019-11-20 10:50:04 -05003171 /* H_EOI would fail with rc = H_FUNCTION when running
3172 * in XIVE mode which is expected, but not an error.
3173 */
Lijun Pan91dc5d22021-02-11 00:43:22 -06003174 if (rc && rc != H_FUNCTION)
Juliet Kim284f87d2019-11-20 10:50:03 -05003175 dev_err(dev, "H_EOI FAILED irq 0x%llx. rc=%ld\n",
3176 val, rc);
Nathan Fontenot73f9d362018-05-22 11:21:10 -05003177 }
Thomas Falconf23e0642018-04-15 18:53:36 -05003178
Thomas Falcon032c5e82015-12-21 11:26:06 -06003179 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
3180 H_ENABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
3181 if (rc)
3182 dev_err(dev, "Couldn't enable scrq irq 0x%lx. rc=%ld\n",
3183 scrq->hw_irq, rc);
3184 return rc;
3185}
3186
3187static int ibmvnic_complete_tx(struct ibmvnic_adapter *adapter,
3188 struct ibmvnic_sub_crq_queue *scrq)
3189{
3190 struct device *dev = &adapter->vdev->dev;
Thomas Falcon06b3e352018-03-16 20:00:28 -05003191 struct ibmvnic_tx_pool *tx_pool;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003192 struct ibmvnic_tx_buff *txbuff;
Thomas Falcon0d973382020-11-18 19:12:19 -06003193 struct netdev_queue *txq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003194 union sub_crq *next;
3195 int index;
Thomas Falconc62aa372020-11-18 19:12:20 -06003196 int i;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003197
3198restart_loop:
3199 while (pending_scrq(adapter, scrq)) {
3200 unsigned int pool = scrq->pool_index;
Thomas Falconffc385b2018-02-18 10:08:41 -06003201 int num_entries = 0;
Thomas Falcon0d973382020-11-18 19:12:19 -06003202 int total_bytes = 0;
3203 int num_packets = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003204
3205 next = ibmvnic_next_scrq(adapter, scrq);
3206 for (i = 0; i < next->tx_comp.num_comps; i++) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06003207 index = be32_to_cpu(next->tx_comp.correlators[i]);
Thomas Falcon06b3e352018-03-16 20:00:28 -05003208 if (index & IBMVNIC_TSO_POOL_MASK) {
3209 tx_pool = &adapter->tso_pool[pool];
3210 index &= ~IBMVNIC_TSO_POOL_MASK;
3211 } else {
3212 tx_pool = &adapter->tx_pool[pool];
3213 }
3214
3215 txbuff = &tx_pool->tx_buff[index];
Thomas Falcon0d973382020-11-18 19:12:19 -06003216 num_packets++;
Thomas Falconffc385b2018-02-18 10:08:41 -06003217 num_entries += txbuff->num_entries;
Thomas Falcon0d973382020-11-18 19:12:19 -06003218 if (txbuff->skb) {
3219 total_bytes += txbuff->skb->len;
Lijun Panca09bf72021-04-13 03:33:25 -05003220 if (next->tx_comp.rcs[i]) {
3221 dev_err(dev, "tx error %x\n",
3222 next->tx_comp.rcs[i]);
3223 dev_kfree_skb_irq(txbuff->skb);
3224 } else {
3225 dev_consume_skb_irq(txbuff->skb);
3226 }
Thomas Falcon0d973382020-11-18 19:12:19 -06003227 txbuff->skb = NULL;
3228 } else {
3229 netdev_warn(adapter->netdev,
3230 "TX completion received with NULL socket buffer\n");
3231 }
Thomas Falcon06b3e352018-03-16 20:00:28 -05003232 tx_pool->free_map[tx_pool->producer_index] = index;
3233 tx_pool->producer_index =
3234 (tx_pool->producer_index + 1) %
3235 tx_pool->num_buffers;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003236 }
3237 /* remove tx_comp scrq*/
3238 next->tx_comp.first = 0;
Nathan Fontenot7c3e7de2017-05-03 14:05:25 -04003239
Thomas Falcon0d973382020-11-18 19:12:19 -06003240 txq = netdev_get_tx_queue(adapter->netdev, scrq->pool_index);
3241 netdev_tx_completed_queue(txq, num_packets, total_bytes);
3242
Thomas Falconffc385b2018-02-18 10:08:41 -06003243 if (atomic_sub_return(num_entries, &scrq->used) <=
Nathan Fontenot7c3e7de2017-05-03 14:05:25 -04003244 (adapter->req_tx_entries_per_subcrq / 2) &&
3245 __netif_subqueue_stopped(adapter->netdev,
3246 scrq->pool_index)) {
3247 netif_wake_subqueue(adapter->netdev, scrq->pool_index);
Thomas Falcon0aecb132018-02-26 18:10:58 -06003248 netdev_dbg(adapter->netdev, "Started queue %d\n",
3249 scrq->pool_index);
Nathan Fontenot7c3e7de2017-05-03 14:05:25 -04003250 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06003251 }
3252
3253 enable_scrq_irq(adapter, scrq);
3254
3255 if (pending_scrq(adapter, scrq)) {
3256 disable_scrq_irq(adapter, scrq);
3257 goto restart_loop;
3258 }
3259
3260 return 0;
3261}
3262
3263static irqreturn_t ibmvnic_interrupt_tx(int irq, void *instance)
3264{
3265 struct ibmvnic_sub_crq_queue *scrq = instance;
3266 struct ibmvnic_adapter *adapter = scrq->adapter;
3267
3268 disable_scrq_irq(adapter, scrq);
3269 ibmvnic_complete_tx(adapter, scrq);
3270
3271 return IRQ_HANDLED;
3272}
3273
3274static irqreturn_t ibmvnic_interrupt_rx(int irq, void *instance)
3275{
3276 struct ibmvnic_sub_crq_queue *scrq = instance;
3277 struct ibmvnic_adapter *adapter = scrq->adapter;
3278
Nathan Fontenot09fb35e2018-01-10 10:40:09 -06003279 /* When booting a kdump kernel we can hit pending interrupts
3280 * prior to completing driver initialization.
3281 */
3282 if (unlikely(adapter->state != VNIC_OPEN))
3283 return IRQ_NONE;
3284
John Allen3d52b592017-08-02 16:44:14 -05003285 adapter->rx_stats_buffers[scrq->scrq_num].interrupts++;
3286
Thomas Falcon032c5e82015-12-21 11:26:06 -06003287 if (napi_schedule_prep(&adapter->napi[scrq->scrq_num])) {
3288 disable_scrq_irq(adapter, scrq);
3289 __napi_schedule(&adapter->napi[scrq->scrq_num]);
3290 }
3291
3292 return IRQ_HANDLED;
3293}
3294
Thomas Falconea22d512016-07-06 15:35:17 -05003295static int init_sub_crq_irqs(struct ibmvnic_adapter *adapter)
3296{
3297 struct device *dev = &adapter->vdev->dev;
3298 struct ibmvnic_sub_crq_queue *scrq;
3299 int i = 0, j = 0;
3300 int rc = 0;
3301
3302 for (i = 0; i < adapter->req_tx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05003303 netdev_dbg(adapter->netdev, "Initializing tx_scrq[%d] irq\n",
3304 i);
Thomas Falconea22d512016-07-06 15:35:17 -05003305 scrq = adapter->tx_scrq[i];
3306 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
3307
Michael Ellerman99c17902016-09-10 19:59:05 +10003308 if (!scrq->irq) {
Thomas Falconea22d512016-07-06 15:35:17 -05003309 rc = -EINVAL;
3310 dev_err(dev, "Error mapping irq\n");
3311 goto req_tx_irq_failed;
3312 }
3313
Murilo Fossa Vicentinie56e2512019-04-25 11:02:33 -03003314 snprintf(scrq->name, sizeof(scrq->name), "ibmvnic-%x-tx%d",
3315 adapter->vdev->unit_address, i);
Thomas Falconea22d512016-07-06 15:35:17 -05003316 rc = request_irq(scrq->irq, ibmvnic_interrupt_tx,
Murilo Fossa Vicentinie56e2512019-04-25 11:02:33 -03003317 0, scrq->name, scrq);
Thomas Falconea22d512016-07-06 15:35:17 -05003318
3319 if (rc) {
3320 dev_err(dev, "Couldn't register tx irq 0x%x. rc=%d\n",
3321 scrq->irq, rc);
3322 irq_dispose_mapping(scrq->irq);
Nathan Fontenotaf9090c2018-02-20 11:04:18 -06003323 goto req_tx_irq_failed;
Thomas Falconea22d512016-07-06 15:35:17 -05003324 }
3325 }
3326
3327 for (i = 0; i < adapter->req_rx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05003328 netdev_dbg(adapter->netdev, "Initializing rx_scrq[%d] irq\n",
3329 i);
Thomas Falconea22d512016-07-06 15:35:17 -05003330 scrq = adapter->rx_scrq[i];
3331 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
Michael Ellerman99c17902016-09-10 19:59:05 +10003332 if (!scrq->irq) {
Thomas Falconea22d512016-07-06 15:35:17 -05003333 rc = -EINVAL;
3334 dev_err(dev, "Error mapping irq\n");
3335 goto req_rx_irq_failed;
3336 }
Murilo Fossa Vicentinie56e2512019-04-25 11:02:33 -03003337 snprintf(scrq->name, sizeof(scrq->name), "ibmvnic-%x-rx%d",
3338 adapter->vdev->unit_address, i);
Thomas Falconea22d512016-07-06 15:35:17 -05003339 rc = request_irq(scrq->irq, ibmvnic_interrupt_rx,
Murilo Fossa Vicentinie56e2512019-04-25 11:02:33 -03003340 0, scrq->name, scrq);
Thomas Falconea22d512016-07-06 15:35:17 -05003341 if (rc) {
3342 dev_err(dev, "Couldn't register rx irq 0x%x. rc=%d\n",
3343 scrq->irq, rc);
3344 irq_dispose_mapping(scrq->irq);
3345 goto req_rx_irq_failed;
3346 }
3347 }
3348 return rc;
3349
3350req_rx_irq_failed:
Thomas Falcon8bf371e2016-10-27 12:28:52 -05003351 for (j = 0; j < i; j++) {
Thomas Falconea22d512016-07-06 15:35:17 -05003352 free_irq(adapter->rx_scrq[j]->irq, adapter->rx_scrq[j]);
3353 irq_dispose_mapping(adapter->rx_scrq[j]->irq);
Thomas Falcon8bf371e2016-10-27 12:28:52 -05003354 }
Thomas Falconea22d512016-07-06 15:35:17 -05003355 i = adapter->req_tx_queues;
3356req_tx_irq_failed:
Thomas Falcon8bf371e2016-10-27 12:28:52 -05003357 for (j = 0; j < i; j++) {
Thomas Falconea22d512016-07-06 15:35:17 -05003358 free_irq(adapter->tx_scrq[j]->irq, adapter->tx_scrq[j]);
Thomas Falcon27a21452020-07-29 16:36:32 -05003359 irq_dispose_mapping(adapter->tx_scrq[j]->irq);
Thomas Falcon8bf371e2016-10-27 12:28:52 -05003360 }
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06003361 release_sub_crqs(adapter, 1);
Thomas Falconea22d512016-07-06 15:35:17 -05003362 return rc;
3363}
3364
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003365static int init_sub_crqs(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003366{
3367 struct device *dev = &adapter->vdev->dev;
3368 struct ibmvnic_sub_crq_queue **allqueues;
3369 int registered_queues = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003370 int total_queues;
3371 int more = 0;
Thomas Falconea22d512016-07-06 15:35:17 -05003372 int i;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003373
Thomas Falcon032c5e82015-12-21 11:26:06 -06003374 total_queues = adapter->req_tx_queues + adapter->req_rx_queues;
3375
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04003376 allqueues = kcalloc(total_queues, sizeof(*allqueues), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003377 if (!allqueues)
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003378 return -1;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003379
3380 for (i = 0; i < total_queues; i++) {
3381 allqueues[i] = init_sub_crq_queue(adapter);
3382 if (!allqueues[i]) {
3383 dev_warn(dev, "Couldn't allocate all sub-crqs\n");
3384 break;
3385 }
3386 registered_queues++;
3387 }
3388
3389 /* Make sure we were able to register the minimum number of queues */
3390 if (registered_queues <
3391 adapter->min_tx_queues + adapter->min_rx_queues) {
3392 dev_err(dev, "Fatal: Couldn't init min number of sub-crqs\n");
3393 goto tx_failed;
3394 }
3395
3396 /* Distribute the failed allocated queues*/
3397 for (i = 0; i < total_queues - registered_queues + more ; i++) {
3398 netdev_dbg(adapter->netdev, "Reducing number of queues\n");
3399 switch (i % 3) {
3400 case 0:
3401 if (adapter->req_rx_queues > adapter->min_rx_queues)
3402 adapter->req_rx_queues--;
3403 else
3404 more++;
3405 break;
3406 case 1:
3407 if (adapter->req_tx_queues > adapter->min_tx_queues)
3408 adapter->req_tx_queues--;
3409 else
3410 more++;
3411 break;
3412 }
3413 }
3414
3415 adapter->tx_scrq = kcalloc(adapter->req_tx_queues,
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04003416 sizeof(*adapter->tx_scrq), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003417 if (!adapter->tx_scrq)
3418 goto tx_failed;
3419
3420 for (i = 0; i < adapter->req_tx_queues; i++) {
3421 adapter->tx_scrq[i] = allqueues[i];
3422 adapter->tx_scrq[i]->pool_index = i;
Nathan Fontenot82e3be32018-02-21 21:33:56 -06003423 adapter->num_active_tx_scrqs++;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003424 }
3425
3426 adapter->rx_scrq = kcalloc(adapter->req_rx_queues,
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04003427 sizeof(*adapter->rx_scrq), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003428 if (!adapter->rx_scrq)
3429 goto rx_failed;
3430
3431 for (i = 0; i < adapter->req_rx_queues; i++) {
3432 adapter->rx_scrq[i] = allqueues[i + adapter->req_tx_queues];
3433 adapter->rx_scrq[i]->scrq_num = i;
Nathan Fontenot82e3be32018-02-21 21:33:56 -06003434 adapter->num_active_rx_scrqs++;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003435 }
3436
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003437 kfree(allqueues);
3438 return 0;
3439
3440rx_failed:
3441 kfree(adapter->tx_scrq);
3442 adapter->tx_scrq = NULL;
3443tx_failed:
3444 for (i = 0; i < registered_queues; i++)
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06003445 release_sub_crq_queue(adapter, allqueues[i], 1);
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003446 kfree(allqueues);
3447 return -1;
3448}
3449
Lijun Pan09081b92020-09-27 20:13:27 -05003450static void send_request_cap(struct ibmvnic_adapter *adapter, int retry)
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003451{
3452 struct device *dev = &adapter->vdev->dev;
3453 union ibmvnic_crq crq;
John Allenc26eba02017-10-26 16:23:25 -05003454 int max_entries;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003455
3456 if (!retry) {
3457 /* Sub-CRQ entries are 32 byte long */
3458 int entries_page = 4 * PAGE_SIZE / (sizeof(u64) * 4);
3459
3460 if (adapter->min_tx_entries_per_subcrq > entries_page ||
3461 adapter->min_rx_add_entries_per_subcrq > entries_page) {
3462 dev_err(dev, "Fatal, invalid entries per sub-crq\n");
3463 return;
3464 }
3465
John Allenc26eba02017-10-26 16:23:25 -05003466 if (adapter->desired.mtu)
3467 adapter->req_mtu = adapter->desired.mtu;
3468 else
3469 adapter->req_mtu = adapter->netdev->mtu + ETH_HLEN;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003470
John Allenc26eba02017-10-26 16:23:25 -05003471 if (!adapter->desired.tx_entries)
3472 adapter->desired.tx_entries =
3473 adapter->max_tx_entries_per_subcrq;
3474 if (!adapter->desired.rx_entries)
3475 adapter->desired.rx_entries =
3476 adapter->max_rx_add_entries_per_subcrq;
3477
3478 max_entries = IBMVNIC_MAX_LTB_SIZE /
3479 (adapter->req_mtu + IBMVNIC_BUFFER_HLEN);
3480
3481 if ((adapter->req_mtu + IBMVNIC_BUFFER_HLEN) *
3482 adapter->desired.tx_entries > IBMVNIC_MAX_LTB_SIZE) {
3483 adapter->desired.tx_entries = max_entries;
3484 }
3485
3486 if ((adapter->req_mtu + IBMVNIC_BUFFER_HLEN) *
3487 adapter->desired.rx_entries > IBMVNIC_MAX_LTB_SIZE) {
3488 adapter->desired.rx_entries = max_entries;
3489 }
3490
3491 if (adapter->desired.tx_entries)
3492 adapter->req_tx_entries_per_subcrq =
3493 adapter->desired.tx_entries;
3494 else
3495 adapter->req_tx_entries_per_subcrq =
3496 adapter->max_tx_entries_per_subcrq;
3497
3498 if (adapter->desired.rx_entries)
3499 adapter->req_rx_add_entries_per_subcrq =
3500 adapter->desired.rx_entries;
3501 else
3502 adapter->req_rx_add_entries_per_subcrq =
3503 adapter->max_rx_add_entries_per_subcrq;
3504
3505 if (adapter->desired.tx_queues)
3506 adapter->req_tx_queues =
3507 adapter->desired.tx_queues;
3508 else
3509 adapter->req_tx_queues =
3510 adapter->opt_tx_comp_sub_queues;
3511
3512 if (adapter->desired.rx_queues)
3513 adapter->req_rx_queues =
3514 adapter->desired.rx_queues;
3515 else
3516 adapter->req_rx_queues =
3517 adapter->opt_rx_comp_queues;
3518
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003519 adapter->req_rx_add_queues = adapter->max_rx_add_queues;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003520 }
3521
Thomas Falcon032c5e82015-12-21 11:26:06 -06003522 memset(&crq, 0, sizeof(crq));
3523 crq.request_capability.first = IBMVNIC_CRQ_CMD;
3524 crq.request_capability.cmd = REQUEST_CAPABILITY;
3525
3526 crq.request_capability.capability = cpu_to_be16(REQ_TX_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06003527 crq.request_capability.number = cpu_to_be64(adapter->req_tx_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06003528 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003529 ibmvnic_send_crq(adapter, &crq);
3530
3531 crq.request_capability.capability = cpu_to_be16(REQ_RX_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06003532 crq.request_capability.number = cpu_to_be64(adapter->req_rx_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06003533 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003534 ibmvnic_send_crq(adapter, &crq);
3535
3536 crq.request_capability.capability = cpu_to_be16(REQ_RX_ADD_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06003537 crq.request_capability.number = cpu_to_be64(adapter->req_rx_add_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06003538 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003539 ibmvnic_send_crq(adapter, &crq);
3540
3541 crq.request_capability.capability =
3542 cpu_to_be16(REQ_TX_ENTRIES_PER_SUBCRQ);
3543 crq.request_capability.number =
Thomas Falconde89e852016-03-01 10:20:09 -06003544 cpu_to_be64(adapter->req_tx_entries_per_subcrq);
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 =
3549 cpu_to_be16(REQ_RX_ADD_ENTRIES_PER_SUBCRQ);
3550 crq.request_capability.number =
Thomas Falconde89e852016-03-01 10:20:09 -06003551 cpu_to_be64(adapter->req_rx_add_entries_per_subcrq);
Thomas Falcon901e0402017-02-15 12:17:59 -06003552 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003553 ibmvnic_send_crq(adapter, &crq);
3554
3555 crq.request_capability.capability = cpu_to_be16(REQ_MTU);
Thomas Falconde89e852016-03-01 10:20:09 -06003556 crq.request_capability.number = cpu_to_be64(adapter->req_mtu);
Thomas Falcon901e0402017-02-15 12:17:59 -06003557 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003558 ibmvnic_send_crq(adapter, &crq);
3559
3560 if (adapter->netdev->flags & IFF_PROMISC) {
3561 if (adapter->promisc_supported) {
3562 crq.request_capability.capability =
3563 cpu_to_be16(PROMISC_REQUESTED);
Thomas Falconde89e852016-03-01 10:20:09 -06003564 crq.request_capability.number = cpu_to_be64(1);
Thomas Falcon901e0402017-02-15 12:17:59 -06003565 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003566 ibmvnic_send_crq(adapter, &crq);
3567 }
3568 } else {
3569 crq.request_capability.capability =
3570 cpu_to_be16(PROMISC_REQUESTED);
Thomas Falconde89e852016-03-01 10:20:09 -06003571 crq.request_capability.number = cpu_to_be64(0);
Thomas Falcon901e0402017-02-15 12:17:59 -06003572 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003573 ibmvnic_send_crq(adapter, &crq);
3574 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06003575}
3576
3577static int pending_scrq(struct ibmvnic_adapter *adapter,
3578 struct ibmvnic_sub_crq_queue *scrq)
3579{
3580 union sub_crq *entry = &scrq->msgs[scrq->cur];
Lijun Pan665ab1e2021-01-29 19:19:04 -06003581 int rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003582
Lijun Pan665ab1e2021-01-29 19:19:04 -06003583 rc = !!(entry->generic.first & IBMVNIC_CRQ_CMD_RSP);
3584
3585 /* Ensure that the SCRQ valid flag is loaded prior to loading the
3586 * contents of the SCRQ descriptor
3587 */
3588 dma_rmb();
3589
3590 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003591}
3592
3593static union sub_crq *ibmvnic_next_scrq(struct ibmvnic_adapter *adapter,
3594 struct ibmvnic_sub_crq_queue *scrq)
3595{
3596 union sub_crq *entry;
3597 unsigned long flags;
3598
3599 spin_lock_irqsave(&scrq->lock, flags);
3600 entry = &scrq->msgs[scrq->cur];
3601 if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP) {
3602 if (++scrq->cur == scrq->size)
3603 scrq->cur = 0;
3604 } else {
3605 entry = NULL;
3606 }
3607 spin_unlock_irqrestore(&scrq->lock, flags);
3608
Lijun Pan665ab1e2021-01-29 19:19:04 -06003609 /* Ensure that the SCRQ valid flag is loaded prior to loading the
3610 * contents of the SCRQ descriptor
Thomas Falconb71ec952020-12-01 09:52:10 -06003611 */
3612 dma_rmb();
3613
Thomas Falcon032c5e82015-12-21 11:26:06 -06003614 return entry;
3615}
3616
3617static union ibmvnic_crq *ibmvnic_next_crq(struct ibmvnic_adapter *adapter)
3618{
3619 struct ibmvnic_crq_queue *queue = &adapter->crq;
3620 union ibmvnic_crq *crq;
3621
3622 crq = &queue->msgs[queue->cur];
3623 if (crq->generic.first & IBMVNIC_CRQ_CMD_RSP) {
3624 if (++queue->cur == queue->size)
3625 queue->cur = 0;
3626 } else {
3627 crq = NULL;
3628 }
3629
3630 return crq;
3631}
3632
Thomas Falcon2d14d372018-07-13 12:03:32 -05003633static void print_subcrq_error(struct device *dev, int rc, const char *func)
3634{
3635 switch (rc) {
3636 case H_PARAMETER:
3637 dev_warn_ratelimited(dev,
3638 "%s failed: Send request is malformed or adapter failover pending. (rc=%d)\n",
3639 func, rc);
3640 break;
3641 case H_CLOSED:
3642 dev_warn_ratelimited(dev,
3643 "%s failed: Backing queue closed. Adapter is down or failover pending. (rc=%d)\n",
3644 func, rc);
3645 break;
3646 default:
3647 dev_err_ratelimited(dev, "%s failed: (rc=%d)\n", func, rc);
3648 break;
3649 }
3650}
3651
Thomas Falconad7775d2016-04-01 17:20:34 -05003652static int send_subcrq_indirect(struct ibmvnic_adapter *adapter,
3653 u64 remote_handle, u64 ioba, u64 num_entries)
3654{
3655 unsigned int ua = adapter->vdev->unit_address;
3656 struct device *dev = &adapter->vdev->dev;
3657 int rc;
3658
3659 /* Make sure the hypervisor sees the complete request */
Lijun Pan1a421562021-02-12 20:36:46 -06003660 dma_wmb();
Thomas Falconad7775d2016-04-01 17:20:34 -05003661 rc = plpar_hcall_norets(H_SEND_SUB_CRQ_INDIRECT, ua,
3662 cpu_to_be64(remote_handle),
3663 ioba, num_entries);
3664
Thomas Falcon2d14d372018-07-13 12:03:32 -05003665 if (rc)
3666 print_subcrq_error(dev, rc, __func__);
Thomas Falconad7775d2016-04-01 17:20:34 -05003667
3668 return rc;
3669}
3670
Thomas Falcon032c5e82015-12-21 11:26:06 -06003671static int ibmvnic_send_crq(struct ibmvnic_adapter *adapter,
3672 union ibmvnic_crq *crq)
3673{
3674 unsigned int ua = adapter->vdev->unit_address;
3675 struct device *dev = &adapter->vdev->dev;
3676 u64 *u64_crq = (u64 *)crq;
3677 int rc;
3678
3679 netdev_dbg(adapter->netdev, "Sending CRQ: %016lx %016lx\n",
Lijun Pan429aa362021-02-11 00:43:18 -06003680 (unsigned long)cpu_to_be64(u64_crq[0]),
3681 (unsigned long)cpu_to_be64(u64_crq[1]));
Thomas Falcon032c5e82015-12-21 11:26:06 -06003682
Thomas Falcon51536982018-05-23 13:37:56 -05003683 if (!adapter->crq.active &&
3684 crq->generic.first != IBMVNIC_CRQ_INIT_CMD) {
3685 dev_warn(dev, "Invalid request detected while CRQ is inactive, possible device state change during reset\n");
3686 return -EINVAL;
3687 }
3688
Thomas Falcon032c5e82015-12-21 11:26:06 -06003689 /* Make sure the hypervisor sees the complete request */
Lijun Pan1a421562021-02-12 20:36:46 -06003690 dma_wmb();
Thomas Falcon032c5e82015-12-21 11:26:06 -06003691
3692 rc = plpar_hcall_norets(H_SEND_CRQ, ua,
3693 cpu_to_be64(u64_crq[0]),
3694 cpu_to_be64(u64_crq[1]));
3695
3696 if (rc) {
Nathan Fontenotec95dff2018-02-07 13:00:24 -06003697 if (rc == H_CLOSED) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06003698 dev_warn(dev, "CRQ Queue closed\n");
Lijun Panfa68bfa2020-08-19 17:52:24 -05003699 /* do not reset, report the fail, wait for passive init from server */
Nathan Fontenotec95dff2018-02-07 13:00:24 -06003700 }
3701
Thomas Falcon032c5e82015-12-21 11:26:06 -06003702 dev_warn(dev, "Send error (rc=%d)\n", rc);
3703 }
3704
3705 return rc;
3706}
3707
3708static int ibmvnic_send_crq_init(struct ibmvnic_adapter *adapter)
3709{
Thomas Falcon36a782f2020-08-31 11:59:57 -05003710 struct device *dev = &adapter->vdev->dev;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003711 union ibmvnic_crq crq;
Thomas Falcon36a782f2020-08-31 11:59:57 -05003712 int retries = 100;
3713 int rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003714
3715 memset(&crq, 0, sizeof(crq));
3716 crq.generic.first = IBMVNIC_CRQ_INIT_CMD;
3717 crq.generic.cmd = IBMVNIC_CRQ_INIT;
3718 netdev_dbg(adapter->netdev, "Sending CRQ init\n");
3719
Thomas Falcon36a782f2020-08-31 11:59:57 -05003720 do {
3721 rc = ibmvnic_send_crq(adapter, &crq);
3722 if (rc != H_CLOSED)
3723 break;
3724 retries--;
3725 msleep(50);
3726
3727 } while (retries > 0);
3728
3729 if (rc) {
3730 dev_err(dev, "Failed to send init request, rc = %d\n", rc);
3731 return rc;
3732 }
3733
3734 return 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003735}
3736
Thomas Falcon032c5e82015-12-21 11:26:06 -06003737static int send_version_xchg(struct ibmvnic_adapter *adapter)
3738{
3739 union ibmvnic_crq crq;
3740
3741 memset(&crq, 0, sizeof(crq));
3742 crq.version_exchange.first = IBMVNIC_CRQ_CMD;
3743 crq.version_exchange.cmd = VERSION_EXCHANGE;
3744 crq.version_exchange.version = cpu_to_be16(ibmvnic_version);
3745
3746 return ibmvnic_send_crq(adapter, &crq);
3747}
3748
Nathan Fontenot37798d02017-11-08 11:23:56 -06003749struct vnic_login_client_data {
3750 u8 type;
3751 __be16 len;
Kees Cook08ea5562018-04-10 15:26:43 -07003752 char name[];
Nathan Fontenot37798d02017-11-08 11:23:56 -06003753} __packed;
3754
3755static int vnic_client_data_len(struct ibmvnic_adapter *adapter)
3756{
3757 int len;
3758
3759 /* Calculate the amount of buffer space needed for the
3760 * vnic client data in the login buffer. There are four entries,
3761 * OS name, LPAR name, device name, and a null last entry.
3762 */
3763 len = 4 * sizeof(struct vnic_login_client_data);
3764 len += 6; /* "Linux" plus NULL */
3765 len += strlen(utsname()->nodename) + 1;
3766 len += strlen(adapter->netdev->name) + 1;
3767
3768 return len;
3769}
3770
3771static void vnic_add_client_data(struct ibmvnic_adapter *adapter,
3772 struct vnic_login_client_data *vlcd)
3773{
3774 const char *os_name = "Linux";
3775 int len;
3776
3777 /* Type 1 - LPAR OS */
3778 vlcd->type = 1;
3779 len = strlen(os_name) + 1;
3780 vlcd->len = cpu_to_be16(len);
Kees Cook08ea5562018-04-10 15:26:43 -07003781 strncpy(vlcd->name, os_name, len);
3782 vlcd = (struct vnic_login_client_data *)(vlcd->name + len);
Nathan Fontenot37798d02017-11-08 11:23:56 -06003783
3784 /* Type 2 - LPAR name */
3785 vlcd->type = 2;
3786 len = strlen(utsname()->nodename) + 1;
3787 vlcd->len = cpu_to_be16(len);
Kees Cook08ea5562018-04-10 15:26:43 -07003788 strncpy(vlcd->name, utsname()->nodename, len);
3789 vlcd = (struct vnic_login_client_data *)(vlcd->name + len);
Nathan Fontenot37798d02017-11-08 11:23:56 -06003790
3791 /* Type 3 - device name */
3792 vlcd->type = 3;
3793 len = strlen(adapter->netdev->name) + 1;
3794 vlcd->len = cpu_to_be16(len);
Kees Cook08ea5562018-04-10 15:26:43 -07003795 strncpy(vlcd->name, adapter->netdev->name, len);
Nathan Fontenot37798d02017-11-08 11:23:56 -06003796}
3797
Thomas Falcon20a8ab72018-02-26 18:10:59 -06003798static int send_login(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003799{
3800 struct ibmvnic_login_rsp_buffer *login_rsp_buffer;
3801 struct ibmvnic_login_buffer *login_buffer;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003802 struct device *dev = &adapter->vdev->dev;
Dany Maddenc98d9cc2020-11-25 18:04:30 -06003803 struct vnic_login_client_data *vlcd;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003804 dma_addr_t rsp_buffer_token;
3805 dma_addr_t buffer_token;
3806 size_t rsp_buffer_size;
3807 union ibmvnic_crq crq;
Dany Maddenc98d9cc2020-11-25 18:04:30 -06003808 int client_data_len;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003809 size_t buffer_size;
3810 __be64 *tx_list_p;
3811 __be64 *rx_list_p;
Dany Maddenc98d9cc2020-11-25 18:04:30 -06003812 int rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003813 int i;
3814
Thomas Falcon20a8ab72018-02-26 18:10:59 -06003815 if (!adapter->tx_scrq || !adapter->rx_scrq) {
3816 netdev_err(adapter->netdev,
3817 "RX or TX queues are not allocated, device login failed\n");
3818 return -1;
3819 }
3820
Lijun Pana0c8be52020-12-19 15:39:19 -06003821 release_login_buffer(adapter);
Thomas Falcon34f0f4e2018-02-13 18:23:40 -06003822 release_login_rsp_buffer(adapter);
Lijun Pana0c8be52020-12-19 15:39:19 -06003823
Nathan Fontenot37798d02017-11-08 11:23:56 -06003824 client_data_len = vnic_client_data_len(adapter);
3825
Thomas Falcon032c5e82015-12-21 11:26:06 -06003826 buffer_size =
3827 sizeof(struct ibmvnic_login_buffer) +
Nathan Fontenot37798d02017-11-08 11:23:56 -06003828 sizeof(u64) * (adapter->req_tx_queues + adapter->req_rx_queues) +
3829 client_data_len;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003830
Nathan Fontenot37798d02017-11-08 11:23:56 -06003831 login_buffer = kzalloc(buffer_size, GFP_ATOMIC);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003832 if (!login_buffer)
3833 goto buf_alloc_failed;
3834
3835 buffer_token = dma_map_single(dev, login_buffer, buffer_size,
3836 DMA_TO_DEVICE);
3837 if (dma_mapping_error(dev, buffer_token)) {
3838 dev_err(dev, "Couldn't map login buffer\n");
3839 goto buf_map_failed;
3840 }
3841
John Allen498cd8e2016-04-06 11:49:55 -05003842 rsp_buffer_size = sizeof(struct ibmvnic_login_rsp_buffer) +
3843 sizeof(u64) * adapter->req_tx_queues +
3844 sizeof(u64) * adapter->req_rx_queues +
3845 sizeof(u64) * adapter->req_rx_queues +
3846 sizeof(u8) * IBMVNIC_TX_DESC_VERSIONS;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003847
3848 login_rsp_buffer = kmalloc(rsp_buffer_size, GFP_ATOMIC);
3849 if (!login_rsp_buffer)
3850 goto buf_rsp_alloc_failed;
3851
3852 rsp_buffer_token = dma_map_single(dev, login_rsp_buffer,
3853 rsp_buffer_size, DMA_FROM_DEVICE);
3854 if (dma_mapping_error(dev, rsp_buffer_token)) {
3855 dev_err(dev, "Couldn't map login rsp buffer\n");
3856 goto buf_rsp_map_failed;
3857 }
Nathan Fontenot661a2622017-04-19 13:44:58 -04003858
Thomas Falcon032c5e82015-12-21 11:26:06 -06003859 adapter->login_buf = login_buffer;
3860 adapter->login_buf_token = buffer_token;
3861 adapter->login_buf_sz = buffer_size;
3862 adapter->login_rsp_buf = login_rsp_buffer;
3863 adapter->login_rsp_buf_token = rsp_buffer_token;
3864 adapter->login_rsp_buf_sz = rsp_buffer_size;
3865
3866 login_buffer->len = cpu_to_be32(buffer_size);
3867 login_buffer->version = cpu_to_be32(INITIAL_VERSION_LB);
3868 login_buffer->num_txcomp_subcrqs = cpu_to_be32(adapter->req_tx_queues);
3869 login_buffer->off_txcomp_subcrqs =
3870 cpu_to_be32(sizeof(struct ibmvnic_login_buffer));
3871 login_buffer->num_rxcomp_subcrqs = cpu_to_be32(adapter->req_rx_queues);
3872 login_buffer->off_rxcomp_subcrqs =
3873 cpu_to_be32(sizeof(struct ibmvnic_login_buffer) +
3874 sizeof(u64) * adapter->req_tx_queues);
3875 login_buffer->login_rsp_ioba = cpu_to_be32(rsp_buffer_token);
3876 login_buffer->login_rsp_len = cpu_to_be32(rsp_buffer_size);
3877
3878 tx_list_p = (__be64 *)((char *)login_buffer +
3879 sizeof(struct ibmvnic_login_buffer));
3880 rx_list_p = (__be64 *)((char *)login_buffer +
3881 sizeof(struct ibmvnic_login_buffer) +
3882 sizeof(u64) * adapter->req_tx_queues);
3883
3884 for (i = 0; i < adapter->req_tx_queues; i++) {
3885 if (adapter->tx_scrq[i]) {
Lijun Pan914789a2021-02-11 00:43:21 -06003886 tx_list_p[i] =
3887 cpu_to_be64(adapter->tx_scrq[i]->crq_num);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003888 }
3889 }
3890
3891 for (i = 0; i < adapter->req_rx_queues; i++) {
3892 if (adapter->rx_scrq[i]) {
Lijun Pan914789a2021-02-11 00:43:21 -06003893 rx_list_p[i] =
3894 cpu_to_be64(adapter->rx_scrq[i]->crq_num);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003895 }
3896 }
3897
Nathan Fontenot37798d02017-11-08 11:23:56 -06003898 /* Insert vNIC login client data */
3899 vlcd = (struct vnic_login_client_data *)
3900 ((char *)rx_list_p + (sizeof(u64) * adapter->req_rx_queues));
3901 login_buffer->client_data_offset =
3902 cpu_to_be32((char *)vlcd - (char *)login_buffer);
3903 login_buffer->client_data_len = cpu_to_be32(client_data_len);
3904
3905 vnic_add_client_data(adapter, vlcd);
3906
Thomas Falcon032c5e82015-12-21 11:26:06 -06003907 netdev_dbg(adapter->netdev, "Login Buffer:\n");
3908 for (i = 0; i < (adapter->login_buf_sz - 1) / 8 + 1; i++) {
3909 netdev_dbg(adapter->netdev, "%016lx\n",
Lijun Pan429aa362021-02-11 00:43:18 -06003910 ((unsigned long *)(adapter->login_buf))[i]);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003911 }
3912
3913 memset(&crq, 0, sizeof(crq));
3914 crq.login.first = IBMVNIC_CRQ_CMD;
3915 crq.login.cmd = LOGIN;
3916 crq.login.ioba = cpu_to_be32(buffer_token);
3917 crq.login.len = cpu_to_be32(buffer_size);
Sukadev Bhattiprolu76cdc5c2020-11-25 18:04:29 -06003918
3919 adapter->login_pending = true;
Dany Maddenc98d9cc2020-11-25 18:04:30 -06003920 rc = ibmvnic_send_crq(adapter, &crq);
3921 if (rc) {
3922 adapter->login_pending = false;
3923 netdev_err(adapter->netdev, "Failed to send login, rc=%d\n", rc);
3924 goto buf_rsp_map_failed;
3925 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06003926
Thomas Falcon20a8ab72018-02-26 18:10:59 -06003927 return 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003928
Thomas Falcon032c5e82015-12-21 11:26:06 -06003929buf_rsp_map_failed:
3930 kfree(login_rsp_buffer);
Dany Maddenc98d9cc2020-11-25 18:04:30 -06003931 adapter->login_rsp_buf = NULL;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003932buf_rsp_alloc_failed:
3933 dma_unmap_single(dev, buffer_token, buffer_size, DMA_TO_DEVICE);
3934buf_map_failed:
3935 kfree(login_buffer);
Dany Maddenc98d9cc2020-11-25 18:04:30 -06003936 adapter->login_buf = NULL;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003937buf_alloc_failed:
Thomas Falcon20a8ab72018-02-26 18:10:59 -06003938 return -1;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003939}
3940
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05003941static int send_request_map(struct ibmvnic_adapter *adapter, dma_addr_t addr,
3942 u32 len, u8 map_id)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003943{
3944 union ibmvnic_crq crq;
3945
3946 memset(&crq, 0, sizeof(crq));
3947 crq.request_map.first = IBMVNIC_CRQ_CMD;
3948 crq.request_map.cmd = REQUEST_MAP;
3949 crq.request_map.map_id = map_id;
3950 crq.request_map.ioba = cpu_to_be32(addr);
3951 crq.request_map.len = cpu_to_be32(len);
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05003952 return ibmvnic_send_crq(adapter, &crq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003953}
3954
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05003955static int send_request_unmap(struct ibmvnic_adapter *adapter, u8 map_id)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003956{
3957 union ibmvnic_crq crq;
3958
3959 memset(&crq, 0, sizeof(crq));
3960 crq.request_unmap.first = IBMVNIC_CRQ_CMD;
3961 crq.request_unmap.cmd = REQUEST_UNMAP;
3962 crq.request_unmap.map_id = map_id;
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05003963 return ibmvnic_send_crq(adapter, &crq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003964}
3965
Lijun Pan69980d02020-09-27 20:13:28 -05003966static void send_query_map(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003967{
3968 union ibmvnic_crq crq;
3969
3970 memset(&crq, 0, sizeof(crq));
3971 crq.query_map.first = IBMVNIC_CRQ_CMD;
3972 crq.query_map.cmd = QUERY_MAP;
3973 ibmvnic_send_crq(adapter, &crq);
3974}
3975
3976/* Send a series of CRQs requesting various capabilities of the VNIC server */
Lijun Pan491099a2020-09-27 20:13:26 -05003977static void send_query_cap(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003978{
3979 union ibmvnic_crq crq;
3980
Thomas Falcon901e0402017-02-15 12:17:59 -06003981 atomic_set(&adapter->running_cap_crqs, 0);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003982 memset(&crq, 0, sizeof(crq));
3983 crq.query_capability.first = IBMVNIC_CRQ_CMD;
3984 crq.query_capability.cmd = QUERY_CAPABILITY;
3985
3986 crq.query_capability.capability = cpu_to_be16(MIN_TX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003987 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003988 ibmvnic_send_crq(adapter, &crq);
3989
3990 crq.query_capability.capability = cpu_to_be16(MIN_RX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003991 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003992 ibmvnic_send_crq(adapter, &crq);
3993
3994 crq.query_capability.capability = cpu_to_be16(MIN_RX_ADD_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003995 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003996 ibmvnic_send_crq(adapter, &crq);
3997
3998 crq.query_capability.capability = cpu_to_be16(MAX_TX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003999 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004000 ibmvnic_send_crq(adapter, &crq);
4001
4002 crq.query_capability.capability = cpu_to_be16(MAX_RX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06004003 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004004 ibmvnic_send_crq(adapter, &crq);
4005
4006 crq.query_capability.capability = cpu_to_be16(MAX_RX_ADD_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06004007 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004008 ibmvnic_send_crq(adapter, &crq);
4009
4010 crq.query_capability.capability =
4011 cpu_to_be16(MIN_TX_ENTRIES_PER_SUBCRQ);
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 =
4016 cpu_to_be16(MIN_RX_ADD_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06004017 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004018 ibmvnic_send_crq(adapter, &crq);
4019
4020 crq.query_capability.capability =
4021 cpu_to_be16(MAX_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06004022 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004023 ibmvnic_send_crq(adapter, &crq);
4024
4025 crq.query_capability.capability =
4026 cpu_to_be16(MAX_RX_ADD_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06004027 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004028 ibmvnic_send_crq(adapter, &crq);
4029
4030 crq.query_capability.capability = cpu_to_be16(TCP_IP_OFFLOAD);
Thomas Falcon901e0402017-02-15 12:17:59 -06004031 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004032 ibmvnic_send_crq(adapter, &crq);
4033
4034 crq.query_capability.capability = cpu_to_be16(PROMISC_SUPPORTED);
Thomas Falcon901e0402017-02-15 12:17:59 -06004035 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004036 ibmvnic_send_crq(adapter, &crq);
4037
4038 crq.query_capability.capability = cpu_to_be16(MIN_MTU);
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 = cpu_to_be16(MAX_MTU);
Thomas Falcon901e0402017-02-15 12:17:59 -06004043 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004044 ibmvnic_send_crq(adapter, &crq);
4045
4046 crq.query_capability.capability = cpu_to_be16(MAX_MULTICAST_FILTERS);
Thomas Falcon901e0402017-02-15 12:17:59 -06004047 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004048 ibmvnic_send_crq(adapter, &crq);
4049
4050 crq.query_capability.capability = cpu_to_be16(VLAN_HEADER_INSERTION);
Thomas Falcon901e0402017-02-15 12:17:59 -06004051 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004052 ibmvnic_send_crq(adapter, &crq);
4053
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04004054 crq.query_capability.capability = cpu_to_be16(RX_VLAN_HEADER_INSERTION);
4055 atomic_inc(&adapter->running_cap_crqs);
4056 ibmvnic_send_crq(adapter, &crq);
4057
Thomas Falcon032c5e82015-12-21 11:26:06 -06004058 crq.query_capability.capability = cpu_to_be16(MAX_TX_SG_ENTRIES);
Thomas Falcon901e0402017-02-15 12:17:59 -06004059 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004060 ibmvnic_send_crq(adapter, &crq);
4061
4062 crq.query_capability.capability = cpu_to_be16(RX_SG_SUPPORTED);
Thomas Falcon901e0402017-02-15 12:17:59 -06004063 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004064 ibmvnic_send_crq(adapter, &crq);
4065
4066 crq.query_capability.capability = cpu_to_be16(OPT_TX_COMP_SUB_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06004067 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004068 ibmvnic_send_crq(adapter, &crq);
4069
4070 crq.query_capability.capability = cpu_to_be16(OPT_RX_COMP_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06004071 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004072 ibmvnic_send_crq(adapter, &crq);
4073
4074 crq.query_capability.capability =
4075 cpu_to_be16(OPT_RX_BUFADD_Q_PER_RX_COMP_Q);
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 =
4080 cpu_to_be16(OPT_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06004081 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004082 ibmvnic_send_crq(adapter, &crq);
4083
4084 crq.query_capability.capability =
4085 cpu_to_be16(OPT_RXBA_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06004086 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004087 ibmvnic_send_crq(adapter, &crq);
4088
4089 crq.query_capability.capability = cpu_to_be16(TX_RX_DESC_REQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06004090 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004091 ibmvnic_send_crq(adapter, &crq);
4092}
4093
Lijun Pan16e811f2020-09-27 20:13:29 -05004094static void send_query_ip_offload(struct ibmvnic_adapter *adapter)
4095{
4096 int buf_sz = sizeof(struct ibmvnic_query_ip_offload_buffer);
4097 struct device *dev = &adapter->vdev->dev;
4098 union ibmvnic_crq crq;
4099
4100 adapter->ip_offload_tok =
4101 dma_map_single(dev,
4102 &adapter->ip_offload_buf,
4103 buf_sz,
4104 DMA_FROM_DEVICE);
4105
4106 if (dma_mapping_error(dev, adapter->ip_offload_tok)) {
4107 if (!firmware_has_feature(FW_FEATURE_CMO))
4108 dev_err(dev, "Couldn't map offload buffer\n");
4109 return;
4110 }
4111
4112 memset(&crq, 0, sizeof(crq));
4113 crq.query_ip_offload.first = IBMVNIC_CRQ_CMD;
4114 crq.query_ip_offload.cmd = QUERY_IP_OFFLOAD;
4115 crq.query_ip_offload.len = cpu_to_be32(buf_sz);
4116 crq.query_ip_offload.ioba =
4117 cpu_to_be32(adapter->ip_offload_tok);
4118
4119 ibmvnic_send_crq(adapter, &crq);
4120}
4121
Lijun Pan46899bd2020-09-27 20:13:30 -05004122static void send_control_ip_offload(struct ibmvnic_adapter *adapter)
4123{
4124 struct ibmvnic_control_ip_offload_buffer *ctrl_buf = &adapter->ip_offload_ctrl;
4125 struct ibmvnic_query_ip_offload_buffer *buf = &adapter->ip_offload_buf;
4126 struct device *dev = &adapter->vdev->dev;
4127 netdev_features_t old_hw_features = 0;
4128 union ibmvnic_crq crq;
4129
4130 adapter->ip_offload_ctrl_tok =
4131 dma_map_single(dev,
4132 ctrl_buf,
4133 sizeof(adapter->ip_offload_ctrl),
4134 DMA_TO_DEVICE);
4135
4136 if (dma_mapping_error(dev, adapter->ip_offload_ctrl_tok)) {
4137 dev_err(dev, "Couldn't map ip offload control buffer\n");
4138 return;
4139 }
4140
4141 ctrl_buf->len = cpu_to_be32(sizeof(adapter->ip_offload_ctrl));
4142 ctrl_buf->version = cpu_to_be32(INITIAL_VERSION_IOB);
4143 ctrl_buf->ipv4_chksum = buf->ipv4_chksum;
4144 ctrl_buf->ipv6_chksum = buf->ipv6_chksum;
4145 ctrl_buf->tcp_ipv4_chksum = buf->tcp_ipv4_chksum;
4146 ctrl_buf->udp_ipv4_chksum = buf->udp_ipv4_chksum;
4147 ctrl_buf->tcp_ipv6_chksum = buf->tcp_ipv6_chksum;
4148 ctrl_buf->udp_ipv6_chksum = buf->udp_ipv6_chksum;
4149 ctrl_buf->large_tx_ipv4 = buf->large_tx_ipv4;
4150 ctrl_buf->large_tx_ipv6 = buf->large_tx_ipv6;
4151
4152 /* large_rx disabled for now, additional features needed */
4153 ctrl_buf->large_rx_ipv4 = 0;
4154 ctrl_buf->large_rx_ipv6 = 0;
4155
4156 if (adapter->state != VNIC_PROBING) {
4157 old_hw_features = adapter->netdev->hw_features;
4158 adapter->netdev->hw_features = 0;
4159 }
4160
4161 adapter->netdev->hw_features = NETIF_F_SG | NETIF_F_GSO | NETIF_F_GRO;
4162
4163 if (buf->tcp_ipv4_chksum || buf->udp_ipv4_chksum)
4164 adapter->netdev->hw_features |= NETIF_F_IP_CSUM;
4165
4166 if (buf->tcp_ipv6_chksum || buf->udp_ipv6_chksum)
4167 adapter->netdev->hw_features |= NETIF_F_IPV6_CSUM;
4168
4169 if ((adapter->netdev->features &
4170 (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)))
4171 adapter->netdev->hw_features |= NETIF_F_RXCSUM;
4172
4173 if (buf->large_tx_ipv4)
4174 adapter->netdev->hw_features |= NETIF_F_TSO;
4175 if (buf->large_tx_ipv6)
4176 adapter->netdev->hw_features |= NETIF_F_TSO6;
4177
4178 if (adapter->state == VNIC_PROBING) {
4179 adapter->netdev->features |= adapter->netdev->hw_features;
4180 } else if (old_hw_features != adapter->netdev->hw_features) {
4181 netdev_features_t tmp = 0;
4182
4183 /* disable features no longer supported */
4184 adapter->netdev->features &= adapter->netdev->hw_features;
4185 /* turn on features now supported if previously enabled */
4186 tmp = (old_hw_features ^ adapter->netdev->hw_features) &
4187 adapter->netdev->hw_features;
4188 adapter->netdev->features |=
4189 tmp & adapter->netdev->wanted_features;
4190 }
4191
4192 memset(&crq, 0, sizeof(crq));
4193 crq.control_ip_offload.first = IBMVNIC_CRQ_CMD;
4194 crq.control_ip_offload.cmd = CONTROL_IP_OFFLOAD;
4195 crq.control_ip_offload.len =
4196 cpu_to_be32(sizeof(adapter->ip_offload_ctrl));
4197 crq.control_ip_offload.ioba = cpu_to_be32(adapter->ip_offload_ctrl_tok);
4198 ibmvnic_send_crq(adapter, &crq);
4199}
4200
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02004201static void handle_vpd_size_rsp(union ibmvnic_crq *crq,
4202 struct ibmvnic_adapter *adapter)
4203{
4204 struct device *dev = &adapter->vdev->dev;
4205
4206 if (crq->get_vpd_size_rsp.rc.code) {
4207 dev_err(dev, "Error retrieving VPD size, rc=%x\n",
4208 crq->get_vpd_size_rsp.rc.code);
4209 complete(&adapter->fw_done);
4210 return;
4211 }
4212
4213 adapter->vpd->len = be64_to_cpu(crq->get_vpd_size_rsp.len);
4214 complete(&adapter->fw_done);
4215}
4216
4217static void handle_vpd_rsp(union ibmvnic_crq *crq,
4218 struct ibmvnic_adapter *adapter)
4219{
4220 struct device *dev = &adapter->vdev->dev;
Desnes Augusto Nunes do Rosario21a25452018-02-05 14:33:55 -02004221 unsigned char *substr = NULL;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02004222 u8 fw_level_len = 0;
4223
4224 memset(adapter->fw_version, 0, 32);
4225
4226 dma_unmap_single(dev, adapter->vpd->dma_addr, adapter->vpd->len,
4227 DMA_FROM_DEVICE);
4228
4229 if (crq->get_vpd_rsp.rc.code) {
4230 dev_err(dev, "Error retrieving VPD from device, rc=%x\n",
4231 crq->get_vpd_rsp.rc.code);
4232 goto complete;
4233 }
4234
4235 /* get the position of the firmware version info
4236 * located after the ASCII 'RM' substring in the buffer
4237 */
4238 substr = strnstr(adapter->vpd->buff, "RM", adapter->vpd->len);
4239 if (!substr) {
Desnes Augusto Nunes do Rosarioa1073112018-02-01 16:04:30 -02004240 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 -02004241 goto complete;
4242 }
4243
4244 /* get length of firmware level ASCII substring */
4245 if ((substr + 2) < (adapter->vpd->buff + adapter->vpd->len)) {
4246 fw_level_len = *(substr + 2);
4247 } else {
4248 dev_info(dev, "Length of FW substr extrapolated VDP buff\n");
4249 goto complete;
4250 }
4251
4252 /* copy firmware version string from vpd into adapter */
4253 if ((substr + 3 + fw_level_len) <
4254 (adapter->vpd->buff + adapter->vpd->len)) {
Desnes Augusto Nunes do Rosario21a25452018-02-05 14:33:55 -02004255 strncpy((char *)adapter->fw_version, substr + 3, fw_level_len);
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02004256 } else {
4257 dev_info(dev, "FW substr extrapolated VPD buff\n");
4258 }
4259
4260complete:
Desnes Augusto Nunes do Rosario21a25452018-02-05 14:33:55 -02004261 if (adapter->fw_version[0] == '\0')
4262 strncpy((char *)adapter->fw_version, "N/A", 3 * sizeof(char));
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02004263 complete(&adapter->fw_done);
4264}
4265
Thomas Falcon032c5e82015-12-21 11:26:06 -06004266static void handle_query_ip_offload_rsp(struct ibmvnic_adapter *adapter)
4267{
4268 struct device *dev = &adapter->vdev->dev;
4269 struct ibmvnic_query_ip_offload_buffer *buf = &adapter->ip_offload_buf;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004270 int i;
4271
4272 dma_unmap_single(dev, adapter->ip_offload_tok,
4273 sizeof(adapter->ip_offload_buf), DMA_FROM_DEVICE);
4274
4275 netdev_dbg(adapter->netdev, "Query IP Offload Buffer:\n");
4276 for (i = 0; i < (sizeof(adapter->ip_offload_buf) - 1) / 8 + 1; i++)
4277 netdev_dbg(adapter->netdev, "%016lx\n",
Lijun Pan429aa362021-02-11 00:43:18 -06004278 ((unsigned long *)(buf))[i]);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004279
4280 netdev_dbg(adapter->netdev, "ipv4_chksum = %d\n", buf->ipv4_chksum);
4281 netdev_dbg(adapter->netdev, "ipv6_chksum = %d\n", buf->ipv6_chksum);
4282 netdev_dbg(adapter->netdev, "tcp_ipv4_chksum = %d\n",
4283 buf->tcp_ipv4_chksum);
4284 netdev_dbg(adapter->netdev, "tcp_ipv6_chksum = %d\n",
4285 buf->tcp_ipv6_chksum);
4286 netdev_dbg(adapter->netdev, "udp_ipv4_chksum = %d\n",
4287 buf->udp_ipv4_chksum);
4288 netdev_dbg(adapter->netdev, "udp_ipv6_chksum = %d\n",
4289 buf->udp_ipv6_chksum);
4290 netdev_dbg(adapter->netdev, "large_tx_ipv4 = %d\n",
4291 buf->large_tx_ipv4);
4292 netdev_dbg(adapter->netdev, "large_tx_ipv6 = %d\n",
4293 buf->large_tx_ipv6);
4294 netdev_dbg(adapter->netdev, "large_rx_ipv4 = %d\n",
4295 buf->large_rx_ipv4);
4296 netdev_dbg(adapter->netdev, "large_rx_ipv6 = %d\n",
4297 buf->large_rx_ipv6);
4298 netdev_dbg(adapter->netdev, "max_ipv4_hdr_sz = %d\n",
4299 buf->max_ipv4_header_size);
4300 netdev_dbg(adapter->netdev, "max_ipv6_hdr_sz = %d\n",
4301 buf->max_ipv6_header_size);
4302 netdev_dbg(adapter->netdev, "max_tcp_hdr_size = %d\n",
4303 buf->max_tcp_header_size);
4304 netdev_dbg(adapter->netdev, "max_udp_hdr_size = %d\n",
4305 buf->max_udp_header_size);
4306 netdev_dbg(adapter->netdev, "max_large_tx_size = %d\n",
4307 buf->max_large_tx_size);
4308 netdev_dbg(adapter->netdev, "max_large_rx_size = %d\n",
4309 buf->max_large_rx_size);
4310 netdev_dbg(adapter->netdev, "ipv6_ext_hdr = %d\n",
4311 buf->ipv6_extension_header);
4312 netdev_dbg(adapter->netdev, "tcp_pseudosum_req = %d\n",
4313 buf->tcp_pseudosum_req);
4314 netdev_dbg(adapter->netdev, "num_ipv6_ext_hd = %d\n",
4315 buf->num_ipv6_ext_headers);
4316 netdev_dbg(adapter->netdev, "off_ipv6_ext_hd = %d\n",
4317 buf->off_ipv6_ext_headers);
4318
Lijun Pan46899bd2020-09-27 20:13:30 -05004319 send_control_ip_offload(adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004320}
4321
Thomas Falconc9008d32018-08-06 21:39:59 -05004322static const char *ibmvnic_fw_err_cause(u16 cause)
4323{
4324 switch (cause) {
4325 case ADAPTER_PROBLEM:
4326 return "adapter problem";
4327 case BUS_PROBLEM:
4328 return "bus problem";
4329 case FW_PROBLEM:
4330 return "firmware problem";
4331 case DD_PROBLEM:
4332 return "device driver problem";
4333 case EEH_RECOVERY:
4334 return "EEH recovery";
4335 case FW_UPDATED:
4336 return "firmware updated";
4337 case LOW_MEMORY:
4338 return "low Memory";
4339 default:
4340 return "unknown";
4341 }
4342}
4343
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04004344static void handle_error_indication(union ibmvnic_crq *crq,
4345 struct ibmvnic_adapter *adapter)
4346{
4347 struct device *dev = &adapter->vdev->dev;
Thomas Falconc9008d32018-08-06 21:39:59 -05004348 u16 cause;
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04004349
Thomas Falconc9008d32018-08-06 21:39:59 -05004350 cause = be16_to_cpu(crq->error_indication.error_cause);
4351
4352 dev_warn_ratelimited(dev,
4353 "Firmware reports %serror, cause: %s. Starting recovery...\n",
4354 crq->error_indication.flags
4355 & IBMVNIC_FATAL_ERROR ? "FATAL " : "",
4356 ibmvnic_fw_err_cause(cause));
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04004357
Nathan Fontenoted651a12017-05-03 14:04:38 -04004358 if (crq->error_indication.flags & IBMVNIC_FATAL_ERROR)
4359 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
John Allen8cb31cf2017-05-26 10:30:37 -04004360 else
4361 ibmvnic_reset(adapter, VNIC_RESET_NON_FATAL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004362}
4363
Thomas Falconf8136142018-01-29 13:45:05 -06004364static int handle_change_mac_rsp(union ibmvnic_crq *crq,
4365 struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06004366{
4367 struct net_device *netdev = adapter->netdev;
4368 struct device *dev = &adapter->vdev->dev;
4369 long rc;
4370
4371 rc = crq->change_mac_addr_rsp.rc.code;
4372 if (rc) {
4373 dev_err(dev, "Error %ld in CHANGE_MAC_ADDR_RSP\n", rc);
Thomas Falconf8136142018-01-29 13:45:05 -06004374 goto out;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004375 }
Lijun Pand9b0e592020-10-20 17:39:19 -05004376 /* crq->change_mac_addr.mac_addr is the requested one
4377 * crq->change_mac_addr_rsp.mac_addr is the returned valid one.
4378 */
Thomas Falcon62740e92019-05-09 23:13:43 -05004379 ether_addr_copy(netdev->dev_addr,
4380 &crq->change_mac_addr_rsp.mac_addr[0]);
Lijun Pand9b0e592020-10-20 17:39:19 -05004381 ether_addr_copy(adapter->mac_addr,
4382 &crq->change_mac_addr_rsp.mac_addr[0]);
Thomas Falconf8136142018-01-29 13:45:05 -06004383out:
4384 complete(&adapter->fw_done);
4385 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004386}
4387
4388static void handle_request_cap_rsp(union ibmvnic_crq *crq,
4389 struct ibmvnic_adapter *adapter)
4390{
4391 struct device *dev = &adapter->vdev->dev;
4392 u64 *req_value;
4393 char *name;
4394
Thomas Falcon901e0402017-02-15 12:17:59 -06004395 atomic_dec(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004396 switch (be16_to_cpu(crq->request_capability_rsp.capability)) {
4397 case REQ_TX_QUEUES:
4398 req_value = &adapter->req_tx_queues;
4399 name = "tx";
4400 break;
4401 case REQ_RX_QUEUES:
4402 req_value = &adapter->req_rx_queues;
4403 name = "rx";
4404 break;
4405 case REQ_RX_ADD_QUEUES:
4406 req_value = &adapter->req_rx_add_queues;
4407 name = "rx_add";
4408 break;
4409 case REQ_TX_ENTRIES_PER_SUBCRQ:
4410 req_value = &adapter->req_tx_entries_per_subcrq;
4411 name = "tx_entries_per_subcrq";
4412 break;
4413 case REQ_RX_ADD_ENTRIES_PER_SUBCRQ:
4414 req_value = &adapter->req_rx_add_entries_per_subcrq;
4415 name = "rx_add_entries_per_subcrq";
4416 break;
4417 case REQ_MTU:
4418 req_value = &adapter->req_mtu;
4419 name = "mtu";
4420 break;
4421 case PROMISC_REQUESTED:
4422 req_value = &adapter->promisc;
4423 name = "promisc";
4424 break;
4425 default:
4426 dev_err(dev, "Got invalid cap request rsp %d\n",
4427 crq->request_capability.capability);
4428 return;
4429 }
4430
4431 switch (crq->request_capability_rsp.rc.code) {
4432 case SUCCESS:
4433 break;
4434 case PARTIALSUCCESS:
4435 dev_info(dev, "req=%lld, rsp=%ld in %s queue, retrying.\n",
4436 *req_value,
Lijun Pan914789a2021-02-11 00:43:21 -06004437 (long)be64_to_cpu(crq->request_capability_rsp.number),
4438 name);
John Allene7913802018-01-18 16:27:12 -06004439
4440 if (be16_to_cpu(crq->request_capability_rsp.capability) ==
4441 REQ_MTU) {
4442 pr_err("mtu of %llu is not supported. Reverting.\n",
4443 *req_value);
4444 *req_value = adapter->fallback.mtu;
4445 } else {
4446 *req_value =
4447 be64_to_cpu(crq->request_capability_rsp.number);
4448 }
4449
Lijun Pan09081b92020-09-27 20:13:27 -05004450 send_request_cap(adapter, 1);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004451 return;
4452 default:
4453 dev_err(dev, "Error %d in request cap rsp\n",
4454 crq->request_capability_rsp.rc.code);
4455 return;
4456 }
4457
4458 /* Done receiving requested capabilities, query IP offload support */
Thomas Falcon901e0402017-02-15 12:17:59 -06004459 if (atomic_read(&adapter->running_cap_crqs) == 0) {
Thomas Falcon249168a2017-02-15 12:18:00 -06004460 adapter->wait_capability = false;
Lijun Pan16e811f2020-09-27 20:13:29 -05004461 send_query_ip_offload(adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004462 }
4463}
4464
4465static int handle_login_rsp(union ibmvnic_crq *login_rsp_crq,
4466 struct ibmvnic_adapter *adapter)
4467{
4468 struct device *dev = &adapter->vdev->dev;
John Allenc26eba02017-10-26 16:23:25 -05004469 struct net_device *netdev = adapter->netdev;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004470 struct ibmvnic_login_rsp_buffer *login_rsp = adapter->login_rsp_buf;
4471 struct ibmvnic_login_buffer *login = adapter->login_buf;
Cristobal Fornof3ae59c2020-08-19 13:16:23 -05004472 u64 *tx_handle_array;
4473 u64 *rx_handle_array;
4474 int num_tx_pools;
4475 int num_rx_pools;
Thomas Falcon507ebe62020-08-21 13:39:01 -05004476 u64 *size_array;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004477 int i;
4478
Sukadev Bhattiprolu76cdc5c2020-11-25 18:04:29 -06004479 /* CHECK: Test/set of login_pending does not need to be atomic
4480 * because only ibmvnic_tasklet tests/clears this.
4481 */
4482 if (!adapter->login_pending) {
4483 netdev_warn(netdev, "Ignoring unexpected login response\n");
4484 return 0;
4485 }
4486 adapter->login_pending = false;
4487
Thomas Falcon032c5e82015-12-21 11:26:06 -06004488 dma_unmap_single(dev, adapter->login_buf_token, adapter->login_buf_sz,
Thomas Falcon37e40fa2018-04-06 18:37:02 -05004489 DMA_TO_DEVICE);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004490 dma_unmap_single(dev, adapter->login_rsp_buf_token,
Thomas Falcon37e40fa2018-04-06 18:37:02 -05004491 adapter->login_rsp_buf_sz, DMA_FROM_DEVICE);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004492
John Allen498cd8e2016-04-06 11:49:55 -05004493 /* If the number of queues requested can't be allocated by the
4494 * server, the login response will return with code 1. We will need
4495 * to resend the login buffer with fewer queues requested.
4496 */
4497 if (login_rsp_crq->generic.rc.code) {
Nathan Fontenot64d92aa2018-04-11 10:09:32 -05004498 adapter->init_done_rc = login_rsp_crq->generic.rc.code;
John Allen498cd8e2016-04-06 11:49:55 -05004499 complete(&adapter->init_done);
4500 return 0;
4501 }
4502
John Allenc26eba02017-10-26 16:23:25 -05004503 netdev->mtu = adapter->req_mtu - ETH_HLEN;
4504
Thomas Falcon032c5e82015-12-21 11:26:06 -06004505 netdev_dbg(adapter->netdev, "Login Response Buffer:\n");
4506 for (i = 0; i < (adapter->login_rsp_buf_sz - 1) / 8 + 1; i++) {
4507 netdev_dbg(adapter->netdev, "%016lx\n",
Lijun Pan429aa362021-02-11 00:43:18 -06004508 ((unsigned long *)(adapter->login_rsp_buf))[i]);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004509 }
4510
4511 /* Sanity checks */
4512 if (login->num_txcomp_subcrqs != login_rsp->num_txsubm_subcrqs ||
4513 (be32_to_cpu(login->num_rxcomp_subcrqs) *
4514 adapter->req_rx_add_queues !=
4515 be32_to_cpu(login_rsp->num_rxadd_subcrqs))) {
4516 dev_err(dev, "FATAL: Inconsistent login and login rsp\n");
Dany Madden31d6b402020-11-25 18:04:24 -06004517 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004518 return -EIO;
4519 }
Thomas Falcon507ebe62020-08-21 13:39:01 -05004520 size_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
4521 be32_to_cpu(adapter->login_rsp_buf->off_rxadd_buff_size));
4522 /* variable buffer sizes are not supported, so just read the
4523 * first entry.
4524 */
4525 adapter->cur_rx_buf_sz = be64_to_cpu(size_array[0]);
Cristobal Fornof3ae59c2020-08-19 13:16:23 -05004526
4527 num_tx_pools = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
4528 num_rx_pools = be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
4529
4530 tx_handle_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
4531 be32_to_cpu(adapter->login_rsp_buf->off_txsubm_subcrqs));
4532 rx_handle_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
4533 be32_to_cpu(adapter->login_rsp_buf->off_rxadd_subcrqs));
4534
4535 for (i = 0; i < num_tx_pools; i++)
4536 adapter->tx_scrq[i]->handle = tx_handle_array[i];
4537
4538 for (i = 0; i < num_rx_pools; i++)
4539 adapter->rx_scrq[i]->handle = rx_handle_array[i];
4540
Thomas Falcon507ebe62020-08-21 13:39:01 -05004541 adapter->num_active_tx_scrqs = num_tx_pools;
4542 adapter->num_active_rx_scrqs = num_rx_pools;
Cristobal Fornof3ae59c2020-08-19 13:16:23 -05004543 release_login_rsp_buffer(adapter);
Thomas Falcona2c0f032018-02-21 18:18:30 -06004544 release_login_buffer(adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004545 complete(&adapter->init_done);
4546
Thomas Falcon032c5e82015-12-21 11:26:06 -06004547 return 0;
4548}
4549
Thomas Falcon032c5e82015-12-21 11:26:06 -06004550static void handle_request_unmap_rsp(union ibmvnic_crq *crq,
4551 struct ibmvnic_adapter *adapter)
4552{
4553 struct device *dev = &adapter->vdev->dev;
4554 long rc;
4555
4556 rc = crq->request_unmap_rsp.rc.code;
4557 if (rc)
4558 dev_err(dev, "Error %ld in REQUEST_UNMAP_RSP\n", rc);
4559}
4560
4561static void handle_query_map_rsp(union ibmvnic_crq *crq,
4562 struct ibmvnic_adapter *adapter)
4563{
4564 struct net_device *netdev = adapter->netdev;
4565 struct device *dev = &adapter->vdev->dev;
4566 long rc;
4567
4568 rc = crq->query_map_rsp.rc.code;
4569 if (rc) {
4570 dev_err(dev, "Error %ld in QUERY_MAP_RSP\n", rc);
4571 return;
4572 }
4573 netdev_dbg(netdev, "page_size = %d\ntot_pages = %d\nfree_pages = %d\n",
4574 crq->query_map_rsp.page_size, crq->query_map_rsp.tot_pages,
4575 crq->query_map_rsp.free_pages);
4576}
4577
4578static void handle_query_cap_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
Thomas Falcon901e0402017-02-15 12:17:59 -06004585 atomic_dec(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004586 netdev_dbg(netdev, "Outstanding queries: %d\n",
Thomas Falcon901e0402017-02-15 12:17:59 -06004587 atomic_read(&adapter->running_cap_crqs));
Thomas Falcon032c5e82015-12-21 11:26:06 -06004588 rc = crq->query_capability.rc.code;
4589 if (rc) {
4590 dev_err(dev, "Error %ld in QUERY_CAP_RSP\n", rc);
4591 goto out;
4592 }
4593
4594 switch (be16_to_cpu(crq->query_capability.capability)) {
4595 case MIN_TX_QUEUES:
4596 adapter->min_tx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004597 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004598 netdev_dbg(netdev, "min_tx_queues = %lld\n",
4599 adapter->min_tx_queues);
4600 break;
4601 case MIN_RX_QUEUES:
4602 adapter->min_rx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004603 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004604 netdev_dbg(netdev, "min_rx_queues = %lld\n",
4605 adapter->min_rx_queues);
4606 break;
4607 case MIN_RX_ADD_QUEUES:
4608 adapter->min_rx_add_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004609 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004610 netdev_dbg(netdev, "min_rx_add_queues = %lld\n",
4611 adapter->min_rx_add_queues);
4612 break;
4613 case MAX_TX_QUEUES:
4614 adapter->max_tx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004615 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004616 netdev_dbg(netdev, "max_tx_queues = %lld\n",
4617 adapter->max_tx_queues);
4618 break;
4619 case MAX_RX_QUEUES:
4620 adapter->max_rx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004621 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004622 netdev_dbg(netdev, "max_rx_queues = %lld\n",
4623 adapter->max_rx_queues);
4624 break;
4625 case MAX_RX_ADD_QUEUES:
4626 adapter->max_rx_add_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004627 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004628 netdev_dbg(netdev, "max_rx_add_queues = %lld\n",
4629 adapter->max_rx_add_queues);
4630 break;
4631 case MIN_TX_ENTRIES_PER_SUBCRQ:
4632 adapter->min_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06004633 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004634 netdev_dbg(netdev, "min_tx_entries_per_subcrq = %lld\n",
4635 adapter->min_tx_entries_per_subcrq);
4636 break;
4637 case MIN_RX_ADD_ENTRIES_PER_SUBCRQ:
4638 adapter->min_rx_add_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06004639 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004640 netdev_dbg(netdev, "min_rx_add_entrs_per_subcrq = %lld\n",
4641 adapter->min_rx_add_entries_per_subcrq);
4642 break;
4643 case MAX_TX_ENTRIES_PER_SUBCRQ:
4644 adapter->max_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06004645 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004646 netdev_dbg(netdev, "max_tx_entries_per_subcrq = %lld\n",
4647 adapter->max_tx_entries_per_subcrq);
4648 break;
4649 case MAX_RX_ADD_ENTRIES_PER_SUBCRQ:
4650 adapter->max_rx_add_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06004651 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004652 netdev_dbg(netdev, "max_rx_add_entrs_per_subcrq = %lld\n",
4653 adapter->max_rx_add_entries_per_subcrq);
4654 break;
4655 case TCP_IP_OFFLOAD:
4656 adapter->tcp_ip_offload =
Thomas Falconde89e852016-03-01 10:20:09 -06004657 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004658 netdev_dbg(netdev, "tcp_ip_offload = %lld\n",
4659 adapter->tcp_ip_offload);
4660 break;
4661 case PROMISC_SUPPORTED:
4662 adapter->promisc_supported =
Thomas Falconde89e852016-03-01 10:20:09 -06004663 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004664 netdev_dbg(netdev, "promisc_supported = %lld\n",
4665 adapter->promisc_supported);
4666 break;
4667 case MIN_MTU:
Thomas Falconde89e852016-03-01 10:20:09 -06004668 adapter->min_mtu = be64_to_cpu(crq->query_capability.number);
Thomas Falconf39f0d12017-02-14 10:22:59 -06004669 netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004670 netdev_dbg(netdev, "min_mtu = %lld\n", adapter->min_mtu);
4671 break;
4672 case MAX_MTU:
Thomas Falconde89e852016-03-01 10:20:09 -06004673 adapter->max_mtu = be64_to_cpu(crq->query_capability.number);
Thomas Falconf39f0d12017-02-14 10:22:59 -06004674 netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004675 netdev_dbg(netdev, "max_mtu = %lld\n", adapter->max_mtu);
4676 break;
4677 case MAX_MULTICAST_FILTERS:
4678 adapter->max_multicast_filters =
Thomas Falconde89e852016-03-01 10:20:09 -06004679 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004680 netdev_dbg(netdev, "max_multicast_filters = %lld\n",
4681 adapter->max_multicast_filters);
4682 break;
4683 case VLAN_HEADER_INSERTION:
4684 adapter->vlan_header_insertion =
Thomas Falconde89e852016-03-01 10:20:09 -06004685 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004686 if (adapter->vlan_header_insertion)
4687 netdev->features |= NETIF_F_HW_VLAN_STAG_TX;
4688 netdev_dbg(netdev, "vlan_header_insertion = %lld\n",
4689 adapter->vlan_header_insertion);
4690 break;
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04004691 case RX_VLAN_HEADER_INSERTION:
4692 adapter->rx_vlan_header_insertion =
4693 be64_to_cpu(crq->query_capability.number);
4694 netdev_dbg(netdev, "rx_vlan_header_insertion = %lld\n",
4695 adapter->rx_vlan_header_insertion);
4696 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004697 case MAX_TX_SG_ENTRIES:
4698 adapter->max_tx_sg_entries =
Thomas Falconde89e852016-03-01 10:20:09 -06004699 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004700 netdev_dbg(netdev, "max_tx_sg_entries = %lld\n",
4701 adapter->max_tx_sg_entries);
4702 break;
4703 case RX_SG_SUPPORTED:
4704 adapter->rx_sg_supported =
Thomas Falconde89e852016-03-01 10:20:09 -06004705 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004706 netdev_dbg(netdev, "rx_sg_supported = %lld\n",
4707 adapter->rx_sg_supported);
4708 break;
4709 case OPT_TX_COMP_SUB_QUEUES:
4710 adapter->opt_tx_comp_sub_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004711 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004712 netdev_dbg(netdev, "opt_tx_comp_sub_queues = %lld\n",
4713 adapter->opt_tx_comp_sub_queues);
4714 break;
4715 case OPT_RX_COMP_QUEUES:
4716 adapter->opt_rx_comp_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004717 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004718 netdev_dbg(netdev, "opt_rx_comp_queues = %lld\n",
4719 adapter->opt_rx_comp_queues);
4720 break;
4721 case OPT_RX_BUFADD_Q_PER_RX_COMP_Q:
4722 adapter->opt_rx_bufadd_q_per_rx_comp_q =
Thomas Falconde89e852016-03-01 10:20:09 -06004723 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004724 netdev_dbg(netdev, "opt_rx_bufadd_q_per_rx_comp_q = %lld\n",
4725 adapter->opt_rx_bufadd_q_per_rx_comp_q);
4726 break;
4727 case OPT_TX_ENTRIES_PER_SUBCRQ:
4728 adapter->opt_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06004729 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004730 netdev_dbg(netdev, "opt_tx_entries_per_subcrq = %lld\n",
4731 adapter->opt_tx_entries_per_subcrq);
4732 break;
4733 case OPT_RXBA_ENTRIES_PER_SUBCRQ:
4734 adapter->opt_rxba_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06004735 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004736 netdev_dbg(netdev, "opt_rxba_entries_per_subcrq = %lld\n",
4737 adapter->opt_rxba_entries_per_subcrq);
4738 break;
4739 case TX_RX_DESC_REQ:
4740 adapter->tx_rx_desc_req = crq->query_capability.number;
4741 netdev_dbg(netdev, "tx_rx_desc_req = %llx\n",
4742 adapter->tx_rx_desc_req);
4743 break;
4744
4745 default:
4746 netdev_err(netdev, "Got invalid cap rsp %d\n",
4747 crq->query_capability.capability);
4748 }
4749
4750out:
Thomas Falcon249168a2017-02-15 12:18:00 -06004751 if (atomic_read(&adapter->running_cap_crqs) == 0) {
4752 adapter->wait_capability = false;
Lijun Pan09081b92020-09-27 20:13:27 -05004753 send_request_cap(adapter, 0);
Thomas Falcon249168a2017-02-15 12:18:00 -06004754 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06004755}
4756
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03004757static int send_query_phys_parms(struct ibmvnic_adapter *adapter)
4758{
4759 union ibmvnic_crq crq;
4760 int rc;
4761
4762 memset(&crq, 0, sizeof(crq));
4763 crq.query_phys_parms.first = IBMVNIC_CRQ_CMD;
4764 crq.query_phys_parms.cmd = QUERY_PHYS_PARMS;
Thomas Falconff25dcb2019-11-25 17:12:56 -06004765
4766 mutex_lock(&adapter->fw_lock);
4767 adapter->fw_done_rc = 0;
Thomas Falcon070eca92019-11-25 17:12:53 -06004768 reinit_completion(&adapter->fw_done);
Thomas Falconff25dcb2019-11-25 17:12:56 -06004769
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03004770 rc = ibmvnic_send_crq(adapter, &crq);
Thomas Falconff25dcb2019-11-25 17:12:56 -06004771 if (rc) {
4772 mutex_unlock(&adapter->fw_lock);
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03004773 return rc;
Thomas Falconff25dcb2019-11-25 17:12:56 -06004774 }
Thomas Falcon476d96c2019-11-25 17:12:55 -06004775
4776 rc = ibmvnic_wait_for_completion(adapter, &adapter->fw_done, 10000);
Thomas Falconff25dcb2019-11-25 17:12:56 -06004777 if (rc) {
4778 mutex_unlock(&adapter->fw_lock);
Thomas Falcon476d96c2019-11-25 17:12:55 -06004779 return rc;
Thomas Falconff25dcb2019-11-25 17:12:56 -06004780 }
Thomas Falcon476d96c2019-11-25 17:12:55 -06004781
Thomas Falconff25dcb2019-11-25 17:12:56 -06004782 mutex_unlock(&adapter->fw_lock);
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03004783 return adapter->fw_done_rc ? -EIO : 0;
4784}
4785
4786static int handle_query_phys_parms_rsp(union ibmvnic_crq *crq,
4787 struct ibmvnic_adapter *adapter)
4788{
4789 struct net_device *netdev = adapter->netdev;
4790 int rc;
Murilo Fossa Vicentinidd0f9d82019-09-16 11:50:37 -03004791 __be32 rspeed = cpu_to_be32(crq->query_phys_parms_rsp.speed);
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03004792
4793 rc = crq->query_phys_parms_rsp.rc.code;
4794 if (rc) {
4795 netdev_err(netdev, "Error %d in QUERY_PHYS_PARMS\n", rc);
4796 return rc;
4797 }
Murilo Fossa Vicentinidd0f9d82019-09-16 11:50:37 -03004798 switch (rspeed) {
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03004799 case IBMVNIC_10MBPS:
4800 adapter->speed = SPEED_10;
4801 break;
4802 case IBMVNIC_100MBPS:
4803 adapter->speed = SPEED_100;
4804 break;
4805 case IBMVNIC_1GBPS:
4806 adapter->speed = SPEED_1000;
4807 break;
Lijun Panb9cd7952020-09-27 19:06:25 -05004808 case IBMVNIC_10GBPS:
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03004809 adapter->speed = SPEED_10000;
4810 break;
4811 case IBMVNIC_25GBPS:
4812 adapter->speed = SPEED_25000;
4813 break;
4814 case IBMVNIC_40GBPS:
4815 adapter->speed = SPEED_40000;
4816 break;
4817 case IBMVNIC_50GBPS:
4818 adapter->speed = SPEED_50000;
4819 break;
4820 case IBMVNIC_100GBPS:
4821 adapter->speed = SPEED_100000;
4822 break;
Lijun Panb9cd7952020-09-27 19:06:25 -05004823 case IBMVNIC_200GBPS:
4824 adapter->speed = SPEED_200000;
4825 break;
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03004826 default:
Murilo Fossa Vicentinidd0f9d82019-09-16 11:50:37 -03004827 if (netif_carrier_ok(netdev))
4828 netdev_warn(netdev, "Unknown speed 0x%08x\n", rspeed);
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03004829 adapter->speed = SPEED_UNKNOWN;
4830 }
4831 if (crq->query_phys_parms_rsp.flags1 & IBMVNIC_FULL_DUPLEX)
4832 adapter->duplex = DUPLEX_FULL;
4833 else if (crq->query_phys_parms_rsp.flags1 & IBMVNIC_HALF_DUPLEX)
4834 adapter->duplex = DUPLEX_HALF;
4835 else
4836 adapter->duplex = DUPLEX_UNKNOWN;
4837
4838 return rc;
4839}
4840
Thomas Falcon032c5e82015-12-21 11:26:06 -06004841static void ibmvnic_handle_crq(union ibmvnic_crq *crq,
4842 struct ibmvnic_adapter *adapter)
4843{
4844 struct ibmvnic_generic_crq *gen_crq = &crq->generic;
4845 struct net_device *netdev = adapter->netdev;
4846 struct device *dev = &adapter->vdev->dev;
Murilo Fossa Vicentini993a82b2017-04-19 13:44:35 -04004847 u64 *u64_crq = (u64 *)crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004848 long rc;
4849
4850 netdev_dbg(netdev, "Handling CRQ: %016lx %016lx\n",
Lijun Pan429aa362021-02-11 00:43:18 -06004851 (unsigned long)cpu_to_be64(u64_crq[0]),
4852 (unsigned long)cpu_to_be64(u64_crq[1]));
Thomas Falcon032c5e82015-12-21 11:26:06 -06004853 switch (gen_crq->first) {
4854 case IBMVNIC_CRQ_INIT_RSP:
4855 switch (gen_crq->cmd) {
4856 case IBMVNIC_CRQ_INIT:
4857 dev_info(dev, "Partner initialized\n");
John Allen017892c12017-05-26 10:30:19 -04004858 adapter->from_passive_init = true;
Sukadev Bhattiprolu76cdc5c2020-11-25 18:04:29 -06004859 /* Discard any stale login responses from prev reset.
4860 * CHECK: should we clear even on INIT_COMPLETE?
4861 */
4862 adapter->login_pending = false;
4863
Thomas Falcon17c87052018-05-23 13:37:58 -05004864 if (!completion_done(&adapter->init_done)) {
4865 complete(&adapter->init_done);
4866 adapter->init_done_rc = -EIO;
4867 }
Sukadev Bhattiproluef66a1e2021-02-02 21:08:02 -08004868 rc = ibmvnic_reset(adapter, VNIC_RESET_FAILOVER);
4869 if (rc && rc != -EBUSY) {
4870 /* We were unable to schedule the failover
4871 * reset either because the adapter was still
4872 * probing (eg: during kexec) or we could not
4873 * allocate memory. Clear the failover_pending
4874 * flag since no one else will. We ignore
4875 * EBUSY because it means either FAILOVER reset
4876 * is already scheduled or the adapter is
4877 * being removed.
4878 */
4879 netdev_err(netdev,
4880 "Error %ld scheduling failover reset\n",
4881 rc);
4882 adapter->failover_pending = false;
4883 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06004884 break;
4885 case IBMVNIC_CRQ_INIT_COMPLETE:
4886 dev_info(dev, "Partner initialization complete\n");
Thomas Falcon51536982018-05-23 13:37:56 -05004887 adapter->crq.active = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004888 send_version_xchg(adapter);
4889 break;
4890 default:
4891 dev_err(dev, "Unknown crq cmd: %d\n", gen_crq->cmd);
4892 }
4893 return;
4894 case IBMVNIC_CRQ_XPORT_EVENT:
Nathan Fontenoted651a12017-05-03 14:04:38 -04004895 netif_carrier_off(netdev);
Thomas Falcon51536982018-05-23 13:37:56 -05004896 adapter->crq.active = false;
Thomas Falcon2147e3d2019-11-25 17:12:54 -06004897 /* terminate any thread waiting for a response
4898 * from the device
4899 */
4900 if (!completion_done(&adapter->fw_done)) {
4901 adapter->fw_done_rc = -EIO;
4902 complete(&adapter->fw_done);
4903 }
4904 if (!completion_done(&adapter->stats_done))
4905 complete(&adapter->stats_done);
Juliet Kim7ed5b312019-09-20 16:11:23 -04004906 if (test_bit(0, &adapter->resetting))
Thomas Falcon2770a792018-05-23 13:38:02 -05004907 adapter->force_reset_recovery = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004908 if (gen_crq->cmd == IBMVNIC_PARTITION_MIGRATED) {
Nathan Fontenoted651a12017-05-03 14:04:38 -04004909 dev_info(dev, "Migrated, re-enabling adapter\n");
4910 ibmvnic_reset(adapter, VNIC_RESET_MOBILITY);
Thomas Falcondfad09a2016-08-18 11:37:51 -05004911 } else if (gen_crq->cmd == IBMVNIC_DEVICE_FAILOVER) {
4912 dev_info(dev, "Backing device failover detected\n");
Thomas Falcon5a18e1e2018-04-06 18:37:05 -05004913 adapter->failover_pending = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004914 } else {
4915 /* The adapter lost the connection */
4916 dev_err(dev, "Virtual Adapter failed (rc=%d)\n",
4917 gen_crq->cmd);
Nathan Fontenoted651a12017-05-03 14:04:38 -04004918 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004919 }
4920 return;
4921 case IBMVNIC_CRQ_CMD_RSP:
4922 break;
4923 default:
4924 dev_err(dev, "Got an invalid msg type 0x%02x\n",
4925 gen_crq->first);
4926 return;
4927 }
4928
4929 switch (gen_crq->cmd) {
4930 case VERSION_EXCHANGE_RSP:
4931 rc = crq->version_exchange_rsp.rc.code;
4932 if (rc) {
4933 dev_err(dev, "Error %ld in VERSION_EXCHG_RSP\n", rc);
4934 break;
4935 }
Thomas Falcon78468892020-05-28 11:19:17 -05004936 ibmvnic_version =
Thomas Falcon032c5e82015-12-21 11:26:06 -06004937 be16_to_cpu(crq->version_exchange_rsp.version);
Thomas Falcon78468892020-05-28 11:19:17 -05004938 dev_info(dev, "Partner protocol version is %d\n",
4939 ibmvnic_version);
Lijun Pan491099a2020-09-27 20:13:26 -05004940 send_query_cap(adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004941 break;
4942 case QUERY_CAPABILITY_RSP:
4943 handle_query_cap_rsp(crq, adapter);
4944 break;
4945 case QUERY_MAP_RSP:
4946 handle_query_map_rsp(crq, adapter);
4947 break;
4948 case REQUEST_MAP_RSP:
Thomas Falconf3be0cb2017-06-21 14:53:01 -05004949 adapter->fw_done_rc = crq->request_map_rsp.rc.code;
4950 complete(&adapter->fw_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004951 break;
4952 case REQUEST_UNMAP_RSP:
4953 handle_request_unmap_rsp(crq, adapter);
4954 break;
4955 case REQUEST_CAPABILITY_RSP:
4956 handle_request_cap_rsp(crq, adapter);
4957 break;
4958 case LOGIN_RSP:
4959 netdev_dbg(netdev, "Got Login Response\n");
4960 handle_login_rsp(crq, adapter);
4961 break;
4962 case LOGICAL_LINK_STATE_RSP:
Nathan Fontenot53da09e2017-04-21 15:39:04 -04004963 netdev_dbg(netdev,
4964 "Got Logical Link State Response, state: %d rc: %d\n",
4965 crq->logical_link_state_rsp.link_state,
4966 crq->logical_link_state_rsp.rc.code);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004967 adapter->logical_link_state =
4968 crq->logical_link_state_rsp.link_state;
Nathan Fontenot53da09e2017-04-21 15:39:04 -04004969 adapter->init_done_rc = crq->logical_link_state_rsp.rc.code;
4970 complete(&adapter->init_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004971 break;
4972 case LINK_STATE_INDICATION:
4973 netdev_dbg(netdev, "Got Logical Link State Indication\n");
4974 adapter->phys_link_state =
4975 crq->link_state_indication.phys_link_state;
4976 adapter->logical_link_state =
4977 crq->link_state_indication.logical_link_state;
Thomas Falcon0655f992019-05-09 23:13:44 -05004978 if (adapter->phys_link_state && adapter->logical_link_state)
4979 netif_carrier_on(netdev);
4980 else
4981 netif_carrier_off(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004982 break;
4983 case CHANGE_MAC_ADDR_RSP:
4984 netdev_dbg(netdev, "Got MAC address change Response\n");
Thomas Falconf8136142018-01-29 13:45:05 -06004985 adapter->fw_done_rc = handle_change_mac_rsp(crq, adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004986 break;
4987 case ERROR_INDICATION:
4988 netdev_dbg(netdev, "Got Error Indication\n");
4989 handle_error_indication(crq, adapter);
4990 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004991 case REQUEST_STATISTICS_RSP:
4992 netdev_dbg(netdev, "Got Statistics Response\n");
4993 complete(&adapter->stats_done);
4994 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004995 case QUERY_IP_OFFLOAD_RSP:
4996 netdev_dbg(netdev, "Got Query IP offload Response\n");
4997 handle_query_ip_offload_rsp(adapter);
4998 break;
4999 case MULTICAST_CTRL_RSP:
5000 netdev_dbg(netdev, "Got multicast control Response\n");
5001 break;
5002 case CONTROL_IP_OFFLOAD_RSP:
5003 netdev_dbg(netdev, "Got Control IP offload Response\n");
5004 dma_unmap_single(dev, adapter->ip_offload_ctrl_tok,
5005 sizeof(adapter->ip_offload_ctrl),
5006 DMA_TO_DEVICE);
John Allenbd0b6722017-03-17 17:13:40 -05005007 complete(&adapter->init_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005008 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005009 case COLLECT_FW_TRACE_RSP:
5010 netdev_dbg(netdev, "Got Collect firmware trace Response\n");
5011 complete(&adapter->fw_done);
5012 break;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02005013 case GET_VPD_SIZE_RSP:
5014 handle_vpd_size_rsp(crq, adapter);
5015 break;
5016 case GET_VPD_RSP:
5017 handle_vpd_rsp(crq, adapter);
5018 break;
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03005019 case QUERY_PHYS_PARMS_RSP:
5020 adapter->fw_done_rc = handle_query_phys_parms_rsp(crq, adapter);
5021 complete(&adapter->fw_done);
5022 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005023 default:
5024 netdev_err(netdev, "Got an invalid cmd type 0x%02x\n",
5025 gen_crq->cmd);
5026 }
5027}
5028
5029static irqreturn_t ibmvnic_interrupt(int irq, void *instance)
5030{
5031 struct ibmvnic_adapter *adapter = instance;
Thomas Falcon6c267b32017-02-15 12:17:58 -06005032
Thomas Falcon6c267b32017-02-15 12:17:58 -06005033 tasklet_schedule(&adapter->tasklet);
Thomas Falcon6c267b32017-02-15 12:17:58 -06005034 return IRQ_HANDLED;
5035}
5036
Allen Paisaa7c3fe2020-09-14 12:59:29 +05305037static void ibmvnic_tasklet(struct tasklet_struct *t)
Thomas Falcon6c267b32017-02-15 12:17:58 -06005038{
Allen Paisaa7c3fe2020-09-14 12:59:29 +05305039 struct ibmvnic_adapter *adapter = from_tasklet(adapter, t, tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005040 struct ibmvnic_crq_queue *queue = &adapter->crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005041 union ibmvnic_crq *crq;
5042 unsigned long flags;
5043 bool done = false;
5044
5045 spin_lock_irqsave(&queue->lock, flags);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005046 while (!done) {
5047 /* Pull all the valid messages off the CRQ */
5048 while ((crq = ibmvnic_next_crq(adapter)) != NULL) {
Lijun Pane41aec72021-01-27 19:34:42 -06005049 /* This barrier makes sure ibmvnic_next_crq()'s
5050 * crq->generic.first & IBMVNIC_CRQ_CMD_RSP is loaded
5051 * before ibmvnic_handle_crq()'s
5052 * switch(gen_crq->first) and switch(gen_crq->cmd).
5053 */
5054 dma_rmb();
Thomas Falcon032c5e82015-12-21 11:26:06 -06005055 ibmvnic_handle_crq(crq, adapter);
5056 crq->generic.first = 0;
5057 }
Brian Kinged7ecbf2017-04-19 13:44:53 -04005058
5059 /* remain in tasklet until all
5060 * capabilities responses are received
5061 */
5062 if (!adapter->wait_capability)
5063 done = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005064 }
Thomas Falcon249168a2017-02-15 12:18:00 -06005065 /* if capabilities CRQ's were sent in this tasklet, the following
5066 * tasklet must wait until all responses are received
5067 */
5068 if (atomic_read(&adapter->running_cap_crqs) != 0)
5069 adapter->wait_capability = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005070 spin_unlock_irqrestore(&queue->lock, flags);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005071}
5072
5073static int ibmvnic_reenable_crq_queue(struct ibmvnic_adapter *adapter)
5074{
5075 struct vio_dev *vdev = adapter->vdev;
5076 int rc;
5077
5078 do {
5079 rc = plpar_hcall_norets(H_ENABLE_CRQ, vdev->unit_address);
5080 } while (rc == H_IN_PROGRESS || rc == H_BUSY || H_IS_LONG_BUSY(rc));
5081
5082 if (rc)
5083 dev_err(&vdev->dev, "Error enabling adapter (rc=%d)\n", rc);
5084
5085 return rc;
5086}
5087
5088static int ibmvnic_reset_crq(struct ibmvnic_adapter *adapter)
5089{
5090 struct ibmvnic_crq_queue *crq = &adapter->crq;
5091 struct device *dev = &adapter->vdev->dev;
5092 struct vio_dev *vdev = adapter->vdev;
5093 int rc;
5094
5095 /* Close the CRQ */
5096 do {
5097 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
5098 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
5099
5100 /* Clean out the queue */
Lijun Pan0e435be2020-11-23 13:35:46 -06005101 if (!crq->msgs)
5102 return -EINVAL;
5103
Thomas Falcon032c5e82015-12-21 11:26:06 -06005104 memset(crq->msgs, 0, PAGE_SIZE);
5105 crq->cur = 0;
Thomas Falcon51536982018-05-23 13:37:56 -05005106 crq->active = false;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005107
5108 /* And re-open it again */
5109 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
5110 crq->msg_token, PAGE_SIZE);
5111
5112 if (rc == H_CLOSED)
5113 /* Adapter is good, but other end is not ready */
5114 dev_warn(dev, "Partner adapter not ready\n");
5115 else if (rc != 0)
5116 dev_warn(dev, "Couldn't register crq (rc=%d)\n", rc);
5117
5118 return rc;
5119}
5120
Nathan Fontenotf9928872017-03-30 02:48:54 -04005121static void release_crq_queue(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06005122{
5123 struct ibmvnic_crq_queue *crq = &adapter->crq;
5124 struct vio_dev *vdev = adapter->vdev;
5125 long rc;
5126
Nathan Fontenotf9928872017-03-30 02:48:54 -04005127 if (!crq->msgs)
5128 return;
5129
Thomas Falcon032c5e82015-12-21 11:26:06 -06005130 netdev_dbg(adapter->netdev, "Releasing CRQ\n");
5131 free_irq(vdev->irq, adapter);
Thomas Falcon6c267b32017-02-15 12:17:58 -06005132 tasklet_kill(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005133 do {
5134 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
5135 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
5136
5137 dma_unmap_single(&vdev->dev, crq->msg_token, PAGE_SIZE,
5138 DMA_BIDIRECTIONAL);
5139 free_page((unsigned long)crq->msgs);
Nathan Fontenotf9928872017-03-30 02:48:54 -04005140 crq->msgs = NULL;
Thomas Falcon51536982018-05-23 13:37:56 -05005141 crq->active = false;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005142}
5143
Nathan Fontenotf9928872017-03-30 02:48:54 -04005144static int init_crq_queue(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06005145{
5146 struct ibmvnic_crq_queue *crq = &adapter->crq;
5147 struct device *dev = &adapter->vdev->dev;
5148 struct vio_dev *vdev = adapter->vdev;
5149 int rc, retrc = -ENOMEM;
5150
Nathan Fontenotf9928872017-03-30 02:48:54 -04005151 if (crq->msgs)
5152 return 0;
5153
Thomas Falcon032c5e82015-12-21 11:26:06 -06005154 crq->msgs = (union ibmvnic_crq *)get_zeroed_page(GFP_KERNEL);
5155 /* Should we allocate more than one page? */
5156
5157 if (!crq->msgs)
5158 return -ENOMEM;
5159
5160 crq->size = PAGE_SIZE / sizeof(*crq->msgs);
5161 crq->msg_token = dma_map_single(dev, crq->msgs, PAGE_SIZE,
5162 DMA_BIDIRECTIONAL);
5163 if (dma_mapping_error(dev, crq->msg_token))
5164 goto map_failed;
5165
5166 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
5167 crq->msg_token, PAGE_SIZE);
5168
5169 if (rc == H_RESOURCE)
5170 /* maybe kexecing and resource is busy. try a reset */
5171 rc = ibmvnic_reset_crq(adapter);
5172 retrc = rc;
5173
5174 if (rc == H_CLOSED) {
5175 dev_warn(dev, "Partner adapter not ready\n");
5176 } else if (rc) {
5177 dev_warn(dev, "Error %d opening adapter\n", rc);
5178 goto reg_crq_failed;
5179 }
5180
5181 retrc = 0;
5182
Allen Paisaa7c3fe2020-09-14 12:59:29 +05305183 tasklet_setup(&adapter->tasklet, (void *)ibmvnic_tasklet);
Thomas Falcon6c267b32017-02-15 12:17:58 -06005184
Thomas Falcon032c5e82015-12-21 11:26:06 -06005185 netdev_dbg(adapter->netdev, "registering irq 0x%x\n", vdev->irq);
Murilo Fossa Vicentinie56e2512019-04-25 11:02:33 -03005186 snprintf(crq->name, sizeof(crq->name), "ibmvnic-%x",
5187 adapter->vdev->unit_address);
5188 rc = request_irq(vdev->irq, ibmvnic_interrupt, 0, crq->name, adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005189 if (rc) {
5190 dev_err(dev, "Couldn't register irq 0x%x. rc=%d\n",
5191 vdev->irq, rc);
5192 goto req_irq_failed;
5193 }
5194
5195 rc = vio_enable_interrupts(vdev);
5196 if (rc) {
5197 dev_err(dev, "Error %d enabling interrupts\n", rc);
5198 goto req_irq_failed;
5199 }
5200
5201 crq->cur = 0;
5202 spin_lock_init(&crq->lock);
5203
5204 return retrc;
5205
5206req_irq_failed:
Thomas Falcon6c267b32017-02-15 12:17:58 -06005207 tasklet_kill(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005208 do {
5209 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
5210 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
5211reg_crq_failed:
5212 dma_unmap_single(dev, crq->msg_token, PAGE_SIZE, DMA_BIDIRECTIONAL);
5213map_failed:
5214 free_page((unsigned long)crq->msgs);
Nathan Fontenotf9928872017-03-30 02:48:54 -04005215 crq->msgs = NULL;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005216 return retrc;
5217}
5218
Lijun Pan635e4422020-08-19 17:52:26 -05005219static int ibmvnic_reset_init(struct ibmvnic_adapter *adapter, bool reset)
John Allenf6ef6402017-03-17 17:13:42 -05005220{
5221 struct device *dev = &adapter->vdev->dev;
Dany Madden98c41f02020-11-25 18:04:32 -06005222 unsigned long timeout = msecs_to_jiffies(20000);
Michal Suchanek6881b072021-03-02 20:47:47 +01005223 u64 old_num_rx_queues = adapter->req_rx_queues;
5224 u64 old_num_tx_queues = adapter->req_tx_queues;
John Allenf6ef6402017-03-17 17:13:42 -05005225 int rc;
5226
John Allen017892c12017-05-26 10:30:19 -04005227 adapter->from_passive_init = false;
5228
Michal Suchanek6881b072021-03-02 20:47:47 +01005229 if (reset)
Lijun Pan635e4422020-08-19 17:52:26 -05005230 reinit_completion(&adapter->init_done);
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06005231
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04005232 adapter->init_done_rc = 0;
Lijun Panfa68bfa2020-08-19 17:52:24 -05005233 rc = ibmvnic_send_crq_init(adapter);
5234 if (rc) {
5235 dev_err(dev, "Send crq init failed with error %d\n", rc);
5236 return rc;
5237 }
5238
John Allenf6ef6402017-03-17 17:13:42 -05005239 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
5240 dev_err(dev, "Initialization sequence timed out\n");
John Allen017892c12017-05-26 10:30:19 -04005241 return -1;
5242 }
5243
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04005244 if (adapter->init_done_rc) {
5245 release_crq_queue(adapter);
5246 return adapter->init_done_rc;
5247 }
5248
Lijun Pan785a2b12020-09-17 21:12:46 -05005249 if (adapter->from_passive_init) {
5250 adapter->state = VNIC_OPEN;
5251 adapter->from_passive_init = false;
5252 return -1;
5253 }
5254
Lijun Pan635e4422020-08-19 17:52:26 -05005255 if (reset &&
5256 test_bit(0, &adapter->resetting) && !adapter->wait_for_reset &&
Nathan Fontenot30f79622018-04-06 18:37:06 -05005257 adapter->reset_reason != VNIC_RESET_MOBILITY) {
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06005258 if (adapter->req_rx_queues != old_num_rx_queues ||
5259 adapter->req_tx_queues != old_num_tx_queues) {
5260 release_sub_crqs(adapter, 0);
5261 rc = init_sub_crqs(adapter);
5262 } else {
5263 rc = reset_sub_crq_queues(adapter);
5264 }
5265 } else {
Nathan Fontenot57a49432017-05-26 10:31:12 -04005266 rc = init_sub_crqs(adapter);
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06005267 }
5268
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04005269 if (rc) {
5270 dev_err(dev, "Initialization of sub crqs failed\n");
5271 release_crq_queue(adapter);
Thomas Falcon5df969c2017-06-28 19:55:54 -05005272 return rc;
5273 }
5274
5275 rc = init_sub_crq_irqs(adapter);
5276 if (rc) {
5277 dev_err(dev, "Failed to initialize sub crq irqs\n");
5278 release_crq_queue(adapter);
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04005279 }
5280
5281 return rc;
John Allenf6ef6402017-03-17 17:13:42 -05005282}
5283
Thomas Falcon40c9db82017-06-12 12:35:04 -05005284static struct device_attribute dev_attr_failover;
5285
Thomas Falcon032c5e82015-12-21 11:26:06 -06005286static int ibmvnic_probe(struct vio_dev *dev, const struct vio_device_id *id)
5287{
5288 struct ibmvnic_adapter *adapter;
5289 struct net_device *netdev;
5290 unsigned char *mac_addr_p;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005291 int rc;
5292
5293 dev_dbg(&dev->dev, "entering ibmvnic_probe for UA 0x%x\n",
5294 dev->unit_address);
5295
5296 mac_addr_p = (unsigned char *)vio_get_attribute(dev,
5297 VETH_MAC_ADDR, NULL);
5298 if (!mac_addr_p) {
5299 dev_err(&dev->dev,
5300 "(%s:%3.3d) ERROR: Can't find MAC_ADDR attribute\n",
5301 __FILE__, __LINE__);
5302 return 0;
5303 }
5304
5305 netdev = alloc_etherdev_mq(sizeof(struct ibmvnic_adapter),
Thomas Falcond45cc3a2017-12-18 12:52:11 -06005306 IBMVNIC_MAX_QUEUES);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005307 if (!netdev)
5308 return -ENOMEM;
5309
5310 adapter = netdev_priv(netdev);
Nathan Fontenot90c80142017-05-03 14:04:32 -04005311 adapter->state = VNIC_PROBING;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005312 dev_set_drvdata(&dev->dev, netdev);
5313 adapter->vdev = dev;
5314 adapter->netdev = netdev;
Sukadev Bhattiprolu76cdc5c2020-11-25 18:04:29 -06005315 adapter->login_pending = false;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005316
5317 ether_addr_copy(adapter->mac_addr, mac_addr_p);
5318 ether_addr_copy(netdev->dev_addr, adapter->mac_addr);
5319 netdev->irq = dev->irq;
5320 netdev->netdev_ops = &ibmvnic_netdev_ops;
5321 netdev->ethtool_ops = &ibmvnic_ethtool_ops;
5322 SET_NETDEV_DEV(netdev, &dev->dev);
5323
Nathan Fontenoted651a12017-05-03 14:04:38 -04005324 INIT_WORK(&adapter->ibmvnic_reset, __ibmvnic_reset);
Juliet Kim7ed5b312019-09-20 16:11:23 -04005325 INIT_DELAYED_WORK(&adapter->ibmvnic_delayed_reset,
5326 __ibmvnic_delayed_reset);
Nathan Fontenoted651a12017-05-03 14:04:38 -04005327 INIT_LIST_HEAD(&adapter->rwi_list);
Thomas Falcon6c5c7482018-12-10 15:22:22 -06005328 spin_lock_init(&adapter->rwi_lock);
Juliet Kim7d7195a2020-03-10 09:23:58 -05005329 spin_lock_init(&adapter->state_lock);
Thomas Falconff25dcb2019-11-25 17:12:56 -06005330 mutex_init(&adapter->fw_lock);
Thomas Falconbbd669a2019-04-04 18:58:26 -05005331 init_completion(&adapter->init_done);
Thomas Falcon070eca92019-11-25 17:12:53 -06005332 init_completion(&adapter->fw_done);
5333 init_completion(&adapter->reset_done);
5334 init_completion(&adapter->stats_done);
Juliet Kim7ed5b312019-09-20 16:11:23 -04005335 clear_bit(0, &adapter->resetting);
Nathan Fontenoted651a12017-05-03 14:04:38 -04005336
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04005337 do {
Nathan Fontenot30f79622018-04-06 18:37:06 -05005338 rc = init_crq_queue(adapter);
5339 if (rc) {
5340 dev_err(&dev->dev, "Couldn't initialize crq. rc=%d\n",
5341 rc);
5342 goto ibmvnic_init_fail;
5343 }
5344
Lijun Pan635e4422020-08-19 17:52:26 -05005345 rc = ibmvnic_reset_init(adapter, false);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05005346 if (rc && rc != EAGAIN)
5347 goto ibmvnic_init_fail;
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04005348 } while (rc == EAGAIN);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005349
Thomas Falcon07184212018-05-16 15:49:05 -05005350 rc = init_stats_buffers(adapter);
5351 if (rc)
5352 goto ibmvnic_init_fail;
5353
5354 rc = init_stats_token(adapter);
5355 if (rc)
5356 goto ibmvnic_stats_fail;
5357
Thomas Falconf39f0d12017-02-14 10:22:59 -06005358 netdev->mtu = adapter->req_mtu - ETH_HLEN;
John Allenc26eba02017-10-26 16:23:25 -05005359 netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
5360 netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005361
Thomas Falcon40c9db82017-06-12 12:35:04 -05005362 rc = device_create_file(&dev->dev, &dev_attr_failover);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05005363 if (rc)
Thomas Falcon07184212018-05-16 15:49:05 -05005364 goto ibmvnic_dev_file_err;
Thomas Falcon40c9db82017-06-12 12:35:04 -05005365
Mick Tarsele876a8a2017-09-28 13:53:18 -07005366 netif_carrier_off(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005367 rc = register_netdev(netdev);
5368 if (rc) {
5369 dev_err(&dev->dev, "failed to register netdev rc=%d\n", rc);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05005370 goto ibmvnic_register_fail;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005371 }
5372 dev_info(&dev->dev, "ibmvnic registered\n");
5373
Nathan Fontenot90c80142017-05-03 14:04:32 -04005374 adapter->state = VNIC_PROBED;
John Allenc26eba02017-10-26 16:23:25 -05005375
5376 adapter->wait_for_reset = false;
Dany Maddena86d5c62020-11-25 18:04:31 -06005377 adapter->last_reset_time = jiffies;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005378 return 0;
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05005379
5380ibmvnic_register_fail:
5381 device_remove_file(&dev->dev, &dev_attr_failover);
5382
Thomas Falcon07184212018-05-16 15:49:05 -05005383ibmvnic_dev_file_err:
5384 release_stats_token(adapter);
5385
5386ibmvnic_stats_fail:
5387 release_stats_buffers(adapter);
5388
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05005389ibmvnic_init_fail:
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06005390 release_sub_crqs(adapter, 1);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05005391 release_crq_queue(adapter);
Thomas Falconff25dcb2019-11-25 17:12:56 -06005392 mutex_destroy(&adapter->fw_lock);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05005393 free_netdev(netdev);
5394
5395 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005396}
5397
Uwe Kleine-König386a9662021-02-25 23:18:34 +01005398static void ibmvnic_remove(struct vio_dev *dev)
Thomas Falcon032c5e82015-12-21 11:26:06 -06005399{
5400 struct net_device *netdev = dev_get_drvdata(&dev->dev);
Nathan Fontenot37489052017-04-19 13:45:04 -04005401 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Juliet Kim7d7195a2020-03-10 09:23:58 -05005402 unsigned long flags;
5403
5404 spin_lock_irqsave(&adapter->state_lock, flags);
Sukadev Bhattiprolu4a41c422021-02-12 20:42:50 -08005405
5406 /* If ibmvnic_reset() is scheduling a reset, wait for it to
5407 * finish. Then, set the state to REMOVING to prevent it from
5408 * scheduling any more work and to have reset functions ignore
5409 * any resets that have already been scheduled. Drop the lock
5410 * after setting state, so __ibmvnic_reset() which is called
5411 * from the flush_work() below, can make progress.
5412 */
Junlin Yang69cdb792021-03-05 16:48:39 +08005413 spin_lock(&adapter->rwi_lock);
Nathan Fontenot90c80142017-05-03 14:04:32 -04005414 adapter->state = VNIC_REMOVING;
Junlin Yang69cdb792021-03-05 16:48:39 +08005415 spin_unlock(&adapter->rwi_lock);
Sukadev Bhattiprolu4a41c422021-02-12 20:42:50 -08005416
Juliet Kim7d7195a2020-03-10 09:23:58 -05005417 spin_unlock_irqrestore(&adapter->state_lock, flags);
5418
Thomas Falcon6954a9e2020-06-12 13:34:41 -05005419 flush_work(&adapter->ibmvnic_reset);
5420 flush_delayed_work(&adapter->ibmvnic_delayed_reset);
5421
Juliet Kima5681e22018-11-19 15:59:22 -06005422 rtnl_lock();
5423 unregister_netdevice(netdev);
Nathan Fontenot37489052017-04-19 13:45:04 -04005424
5425 release_resources(adapter);
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06005426 release_sub_crqs(adapter, 1);
Nathan Fontenot37489052017-04-19 13:45:04 -04005427 release_crq_queue(adapter);
5428
Thomas Falcon53cc7722018-02-26 18:10:56 -06005429 release_stats_token(adapter);
5430 release_stats_buffers(adapter);
5431
Nathan Fontenot90c80142017-05-03 14:04:32 -04005432 adapter->state = VNIC_REMOVED;
5433
Juliet Kima5681e22018-11-19 15:59:22 -06005434 rtnl_unlock();
Thomas Falconff25dcb2019-11-25 17:12:56 -06005435 mutex_destroy(&adapter->fw_lock);
Thomas Falcon40c9db82017-06-12 12:35:04 -05005436 device_remove_file(&dev->dev, &dev_attr_failover);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005437 free_netdev(netdev);
5438 dev_set_drvdata(&dev->dev, NULL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005439}
5440
Thomas Falcon40c9db82017-06-12 12:35:04 -05005441static ssize_t failover_store(struct device *dev, struct device_attribute *attr,
5442 const char *buf, size_t count)
5443{
5444 struct net_device *netdev = dev_get_drvdata(dev);
5445 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
5446 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
5447 __be64 session_token;
5448 long rc;
5449
5450 if (!sysfs_streq(buf, "1"))
5451 return -EINVAL;
5452
5453 rc = plpar_hcall(H_VIOCTL, retbuf, adapter->vdev->unit_address,
5454 H_GET_SESSION_TOKEN, 0, 0, 0);
5455 if (rc) {
5456 netdev_err(netdev, "Couldn't retrieve session token, rc %ld\n",
5457 rc);
5458 return -EINVAL;
5459 }
5460
5461 session_token = (__be64)retbuf[0];
5462 netdev_dbg(netdev, "Initiating client failover, session id %llx\n",
5463 be64_to_cpu(session_token));
5464 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
5465 H_SESSION_ERR_DETECTED, session_token, 0, 0);
5466 if (rc) {
5467 netdev_err(netdev, "Client initiated failover failed, rc %ld\n",
5468 rc);
5469 return -EINVAL;
5470 }
5471
5472 return count;
5473}
5474
Joe Perches6cbaefb2017-12-19 10:15:09 -08005475static DEVICE_ATTR_WO(failover);
Thomas Falcon40c9db82017-06-12 12:35:04 -05005476
Thomas Falcon032c5e82015-12-21 11:26:06 -06005477static unsigned long ibmvnic_get_desired_dma(struct vio_dev *vdev)
5478{
5479 struct net_device *netdev = dev_get_drvdata(&vdev->dev);
5480 struct ibmvnic_adapter *adapter;
5481 struct iommu_table *tbl;
5482 unsigned long ret = 0;
5483 int i;
5484
5485 tbl = get_iommu_table_base(&vdev->dev);
5486
5487 /* netdev inits at probe time along with the structures we need below*/
5488 if (!netdev)
5489 return IOMMU_PAGE_ALIGN(IBMVNIC_IO_ENTITLEMENT_DEFAULT, tbl);
5490
5491 adapter = netdev_priv(netdev);
5492
5493 ret += PAGE_SIZE; /* the crq message queue */
Thomas Falcon032c5e82015-12-21 11:26:06 -06005494 ret += IOMMU_PAGE_ALIGN(sizeof(struct ibmvnic_statistics), tbl);
5495
5496 for (i = 0; i < adapter->req_tx_queues + adapter->req_rx_queues; i++)
5497 ret += 4 * PAGE_SIZE; /* the scrq message queue */
5498
Thomas Falcon507ebe62020-08-21 13:39:01 -05005499 for (i = 0; i < adapter->num_active_rx_pools; i++)
Thomas Falcon032c5e82015-12-21 11:26:06 -06005500 ret += adapter->rx_pool[i].size *
5501 IOMMU_PAGE_ALIGN(adapter->rx_pool[i].buff_size, tbl);
5502
5503 return ret;
5504}
5505
5506static int ibmvnic_resume(struct device *dev)
5507{
5508 struct net_device *netdev = dev_get_drvdata(dev);
5509 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005510
John Allencb89ba22017-06-19 11:27:53 -05005511 if (adapter->state != VNIC_OPEN)
5512 return 0;
5513
John Allena2488782017-07-24 13:26:06 -05005514 tasklet_schedule(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005515
5516 return 0;
5517}
5518
Arvind Yadav8c37bc62017-08-17 18:52:54 +05305519static const struct vio_device_id ibmvnic_device_table[] = {
Thomas Falcon032c5e82015-12-21 11:26:06 -06005520 {"network", "IBM,vnic"},
5521 {"", "" }
5522};
5523MODULE_DEVICE_TABLE(vio, ibmvnic_device_table);
5524
5525static const struct dev_pm_ops ibmvnic_pm_ops = {
5526 .resume = ibmvnic_resume
5527};
5528
5529static struct vio_driver ibmvnic_driver = {
5530 .id_table = ibmvnic_device_table,
5531 .probe = ibmvnic_probe,
5532 .remove = ibmvnic_remove,
5533 .get_desired_dma = ibmvnic_get_desired_dma,
5534 .name = ibmvnic_driver_name,
5535 .pm = &ibmvnic_pm_ops,
5536};
5537
5538/* module functions */
5539static int __init ibmvnic_module_init(void)
5540{
5541 pr_info("%s: %s %s\n", ibmvnic_driver_name, ibmvnic_driver_string,
5542 IBMVNIC_DRIVER_VERSION);
5543
5544 return vio_register_driver(&ibmvnic_driver);
5545}
5546
5547static void __exit ibmvnic_module_exit(void)
5548{
5549 vio_unregister_driver(&ibmvnic_driver);
5550}
5551
5552module_init(ibmvnic_module_init);
5553module_exit(ibmvnic_module_exit);