blob: fe3201ba20341341dbd5754e9b186d7916929443 [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
1908 if (adapter->state != VNIC_PROBED) {
1909 ether_addr_copy(adapter->mac_addr, addr->sa_data);
Thomas Falcon62740e92019-05-09 23:13:43 -05001910 rc = __ibmvnic_set_mac(netdev, addr->sa_data);
Lijun Pan8fc36722020-10-27 17:04:56 -05001911 }
John Allenc26eba02017-10-26 16:23:25 -05001912
Thomas Falconf8136142018-01-29 13:45:05 -06001913 return rc;
John Allenc26eba02017-10-26 16:23:25 -05001914}
1915
Lee Jones80708602021-01-15 20:09:03 +00001916/*
Nathan Fontenoted651a12017-05-03 14:04:38 -04001917 * do_reset returns zero if we are able to keep processing reset events, or
1918 * non-zero if we hit a fatal error and must halt.
1919 */
1920static int do_reset(struct ibmvnic_adapter *adapter,
1921 struct ibmvnic_rwi *rwi, u32 reset_state)
1922{
John Allen896d8692018-01-18 16:26:31 -06001923 u64 old_num_rx_queues, old_num_tx_queues;
Thomas Falcon5bf032e2018-11-21 11:17:59 -06001924 u64 old_num_rx_slots, old_num_tx_slots;
Nathan Fontenoted651a12017-05-03 14:04:38 -04001925 struct net_device *netdev = adapter->netdev;
1926 int i, rc;
1927
Sukadev Bhattiprolu38bd5ce2020-12-04 18:22:35 -08001928 netdev_dbg(adapter->netdev,
1929 "[S:%d FOP:%d] Reset reason %d, reset_state %d\n",
1930 adapter->state, adapter->failover_pending,
1931 rwi->reset_reason, reset_state);
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001932
Lijun Pan3f5ec372021-01-06 15:35:14 -06001933 adapter->reset_reason = rwi->reset_reason;
1934 /* requestor of VNIC_RESET_CHANGE_PARAM already has the rtnl lock */
1935 if (!(adapter->reset_reason == VNIC_RESET_CHANGE_PARAM))
1936 rtnl_lock();
1937
Lijun Panbab08be2021-02-11 00:43:19 -06001938 /* Now that we have the rtnl lock, clear any pending failover.
Sukadev Bhattiprolu1d850492020-10-30 10:07:11 -07001939 * This will ensure ibmvnic_open() has either completed or will
1940 * block until failover is complete.
1941 */
1942 if (rwi->reset_reason == VNIC_RESET_FAILOVER)
1943 adapter->failover_pending = false;
Juliet Kimb27507b2019-09-20 16:11:22 -04001944
Sukadev Bhattiprolu8f1c0fd2021-02-23 21:02:29 -08001945 /* read the state and check (again) after getting rtnl */
1946 reset_state = adapter->state;
1947
1948 if (reset_state == VNIC_REMOVING || reset_state == VNIC_REMOVED) {
1949 rc = -EBUSY;
1950 goto out;
1951 }
1952
Nathan Fontenoted651a12017-05-03 14:04:38 -04001953 netif_carrier_off(netdev);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001954
John Allen896d8692018-01-18 16:26:31 -06001955 old_num_rx_queues = adapter->req_rx_queues;
1956 old_num_tx_queues = adapter->req_tx_queues;
Thomas Falcon5bf032e2018-11-21 11:17:59 -06001957 old_num_rx_slots = adapter->req_rx_add_entries_per_subcrq;
1958 old_num_tx_slots = adapter->req_tx_entries_per_subcrq;
John Allen896d8692018-01-18 16:26:31 -06001959
Nathan Fontenot30f79622018-04-06 18:37:06 -05001960 ibmvnic_cleanup(netdev);
1961
Thomas Falcon1f946082019-06-07 16:03:53 -05001962 if (reset_state == VNIC_OPEN &&
1963 adapter->reset_reason != VNIC_RESET_MOBILITY &&
Nathan Fontenot30f79622018-04-06 18:37:06 -05001964 adapter->reset_reason != VNIC_RESET_FAILOVER) {
Lijun Pan3f5ec372021-01-06 15:35:14 -06001965 if (adapter->reset_reason == VNIC_RESET_CHANGE_PARAM) {
1966 rc = __ibmvnic_close(netdev);
1967 if (rc)
1968 goto out;
1969 } else {
1970 adapter->state = VNIC_CLOSING;
Nathan Fontenoted651a12017-05-03 14:04:38 -04001971
Lijun Pan3f5ec372021-01-06 15:35:14 -06001972 /* Release the RTNL lock before link state change and
1973 * re-acquire after the link state change to allow
1974 * linkwatch_event to grab the RTNL lock and run during
1975 * a reset.
1976 */
1977 rtnl_unlock();
1978 rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_DN);
1979 rtnl_lock();
1980 if (rc)
1981 goto out;
Juliet Kimb27507b2019-09-20 16:11:22 -04001982
Sukadev Bhattiprolu8f1c0fd2021-02-23 21:02:29 -08001983 if (adapter->state == VNIC_OPEN) {
1984 /* When we dropped rtnl, ibmvnic_open() got
1985 * it and noticed that we are resetting and
1986 * set the adapter state to OPEN. Update our
1987 * new "target" state, and resume the reset
1988 * from VNIC_CLOSING state.
1989 */
1990 netdev_dbg(netdev,
1991 "Open changed state from %d, updating.\n",
1992 reset_state);
1993 reset_state = VNIC_OPEN;
1994 adapter->state = VNIC_CLOSING;
1995 }
1996
Lijun Pan3f5ec372021-01-06 15:35:14 -06001997 if (adapter->state != VNIC_CLOSING) {
Sukadev Bhattiprolu8f1c0fd2021-02-23 21:02:29 -08001998 /* If someone else changed the adapter state
1999 * when we dropped the rtnl, fail the reset
2000 */
Lijun Pan3f5ec372021-01-06 15:35:14 -06002001 rc = -1;
2002 goto out;
2003 }
Lijun Pan3f5ec372021-01-06 15:35:14 -06002004 adapter->state = VNIC_CLOSED;
Juliet Kimb27507b2019-09-20 16:11:22 -04002005 }
Lijun Pan3f5ec372021-01-06 15:35:14 -06002006 }
Juliet Kimb27507b2019-09-20 16:11:22 -04002007
Lijun Pan3f5ec372021-01-06 15:35:14 -06002008 if (adapter->reset_reason == VNIC_RESET_CHANGE_PARAM) {
2009 release_resources(adapter);
2010 release_sub_crqs(adapter, 1);
2011 release_crq_queue(adapter);
John Allenc26eba02017-10-26 16:23:25 -05002012 }
2013
John Allen8cb31cf2017-05-26 10:30:37 -04002014 if (adapter->reset_reason != VNIC_RESET_NON_FATAL) {
2015 /* remove the closed state so when we call open it appears
2016 * we are coming from the probed state.
2017 */
Nathan Fontenoted651a12017-05-03 14:04:38 -04002018 adapter->state = VNIC_PROBED;
John Allen8cb31cf2017-05-26 10:30:37 -04002019
Lijun Pan3f5ec372021-01-06 15:35:14 -06002020 if (adapter->reset_reason == VNIC_RESET_CHANGE_PARAM) {
2021 rc = init_crq_queue(adapter);
2022 } else if (adapter->reset_reason == VNIC_RESET_MOBILITY) {
Nathan Fontenot30f79622018-04-06 18:37:06 -05002023 rc = ibmvnic_reenable_crq_queue(adapter);
2024 release_sub_crqs(adapter, 1);
2025 } else {
2026 rc = ibmvnic_reset_crq(adapter);
Dany Madden8b40eb732020-06-18 15:24:13 -04002027 if (rc == H_CLOSED || rc == H_SUCCESS) {
Nathan Fontenot30f79622018-04-06 18:37:06 -05002028 rc = vio_enable_interrupts(adapter->vdev);
Dany Madden8b40eb732020-06-18 15:24:13 -04002029 if (rc)
2030 netdev_err(adapter->netdev,
2031 "Reset failed to enable interrupts. rc=%d\n",
2032 rc);
2033 }
Nathan Fontenot30f79622018-04-06 18:37:06 -05002034 }
2035
2036 if (rc) {
2037 netdev_err(adapter->netdev,
Dany Madden8b40eb732020-06-18 15:24:13 -04002038 "Reset couldn't initialize crq. rc=%d\n", rc);
Juliet Kimb27507b2019-09-20 16:11:22 -04002039 goto out;
Nathan Fontenot30f79622018-04-06 18:37:06 -05002040 }
2041
Lijun Pan635e4422020-08-19 17:52:26 -05002042 rc = ibmvnic_reset_init(adapter, true);
Juliet Kimb27507b2019-09-20 16:11:22 -04002043 if (rc) {
2044 rc = IBMVNIC_INIT_FAILED;
2045 goto out;
2046 }
John Allen8cb31cf2017-05-26 10:30:37 -04002047
2048 /* If the adapter was in PROBE state prior to the reset,
2049 * exit here.
2050 */
Juliet Kimb27507b2019-09-20 16:11:22 -04002051 if (reset_state == VNIC_PROBED) {
2052 rc = 0;
2053 goto out;
2054 }
John Allen8cb31cf2017-05-26 10:30:37 -04002055
2056 rc = ibmvnic_login(netdev);
Lijun Panf78afaa2021-02-11 00:43:20 -06002057 if (rc)
Juliet Kimb27507b2019-09-20 16:11:22 -04002058 goto out;
John Allen8cb31cf2017-05-26 10:30:37 -04002059
Lijun Pan3f5ec372021-01-06 15:35:14 -06002060 if (adapter->reset_reason == VNIC_RESET_CHANGE_PARAM) {
2061 rc = init_resources(adapter);
2062 if (rc)
2063 goto out;
2064 } else if (adapter->req_rx_queues != old_num_rx_queues ||
Juliet Kimb27507b2019-09-20 16:11:22 -04002065 adapter->req_tx_queues != old_num_tx_queues ||
2066 adapter->req_rx_add_entries_per_subcrq !=
2067 old_num_rx_slots ||
2068 adapter->req_tx_entries_per_subcrq !=
Mingming Cao9f134572020-08-25 13:26:41 -04002069 old_num_tx_slots ||
2070 !adapter->rx_pool ||
2071 !adapter->tso_pool ||
2072 !adapter->tx_pool) {
John Allen896d8692018-01-18 16:26:31 -06002073 release_rx_pools(adapter);
2074 release_tx_pools(adapter);
Juliet Kima5681e22018-11-19 15:59:22 -06002075 release_napi(adapter);
2076 release_vpd_data(adapter);
2077
2078 rc = init_resources(adapter);
Thomas Falconf611a5b2018-08-30 13:19:53 -05002079 if (rc)
Juliet Kimb27507b2019-09-20 16:11:22 -04002080 goto out;
Nathan Fontenotd9043c12018-02-19 13:30:14 -06002081
John Allenc26eba02017-10-26 16:23:25 -05002082 } else {
2083 rc = reset_tx_pools(adapter);
Jakub Kicinski8ae4dff2020-09-04 21:07:49 -07002084 if (rc) {
Mingming Cao9f134572020-08-25 13:26:41 -04002085 netdev_dbg(adapter->netdev, "reset tx pools failed (%d)\n",
Lijun Pan91dc5d22021-02-11 00:43:22 -06002086 rc);
Juliet Kimb27507b2019-09-20 16:11:22 -04002087 goto out;
Jakub Kicinski8ae4dff2020-09-04 21:07:49 -07002088 }
Nathan Fontenot8c0543a2017-05-26 10:31:06 -04002089
John Allenc26eba02017-10-26 16:23:25 -05002090 rc = reset_rx_pools(adapter);
Jakub Kicinski8ae4dff2020-09-04 21:07:49 -07002091 if (rc) {
Mingming Cao9f134572020-08-25 13:26:41 -04002092 netdev_dbg(adapter->netdev, "reset rx pools failed (%d)\n",
Lijun Pan91dc5d22021-02-11 00:43:22 -06002093 rc);
Juliet Kimb27507b2019-09-20 16:11:22 -04002094 goto out;
Jakub Kicinski8ae4dff2020-09-04 21:07:49 -07002095 }
John Allenc26eba02017-10-26 16:23:25 -05002096 }
Thomas Falcon134bbe72018-05-16 15:49:04 -05002097 ibmvnic_disable_irqs(adapter);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002098 }
John Allene676d812018-03-14 10:41:29 -05002099 adapter->state = VNIC_CLOSED;
2100
Juliet Kimb27507b2019-09-20 16:11:22 -04002101 if (reset_state == VNIC_CLOSED) {
2102 rc = 0;
2103 goto out;
2104 }
John Allene676d812018-03-14 10:41:29 -05002105
Nathan Fontenoted651a12017-05-03 14:04:38 -04002106 rc = __ibmvnic_open(netdev);
2107 if (rc) {
Juliet Kimb27507b2019-09-20 16:11:22 -04002108 rc = IBMVNIC_OPEN_FAILED;
2109 goto out;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002110 }
2111
Thomas Falconbe32a242019-06-07 16:03:54 -05002112 /* refresh device's multicast list */
2113 ibmvnic_set_multi(netdev);
2114
Nathan Fontenoted651a12017-05-03 14:04:38 -04002115 /* kick napi */
2116 for (i = 0; i < adapter->req_rx_queues; i++)
2117 napi_schedule(&adapter->napi[i]);
2118
Lijun Pan98025bc2020-11-20 16:40:12 -06002119 if (adapter->reset_reason == VNIC_RESET_FAILOVER ||
Lijun Pan6be46662020-12-14 15:19:29 -06002120 adapter->reset_reason == VNIC_RESET_MOBILITY)
2121 __netdev_notify_peers(netdev);
Nathan Fontenot61d3e1d2017-06-12 20:47:45 -04002122
Juliet Kimb27507b2019-09-20 16:11:22 -04002123 rc = 0;
2124
2125out:
Dany Madden0cb4bc62020-11-25 18:04:27 -06002126 /* restore the adapter state if reset failed */
2127 if (rc)
2128 adapter->state = reset_state;
Lijun Pan3f5ec372021-01-06 15:35:14 -06002129 /* requestor of VNIC_RESET_CHANGE_PARAM should still hold the rtnl lock */
2130 if (!(adapter->reset_reason == VNIC_RESET_CHANGE_PARAM))
2131 rtnl_unlock();
Juliet Kimb27507b2019-09-20 16:11:22 -04002132
Sukadev Bhattiprolu38bd5ce2020-12-04 18:22:35 -08002133 netdev_dbg(adapter->netdev, "[S:%d FOP:%d] Reset done, rc %d\n",
2134 adapter->state, adapter->failover_pending, rc);
Juliet Kimb27507b2019-09-20 16:11:22 -04002135 return rc;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002136}
2137
Thomas Falcon2770a792018-05-23 13:38:02 -05002138static int do_hard_reset(struct ibmvnic_adapter *adapter,
2139 struct ibmvnic_rwi *rwi, u32 reset_state)
2140{
2141 struct net_device *netdev = adapter->netdev;
2142 int rc;
2143
2144 netdev_dbg(adapter->netdev, "Hard resetting driver (%d)\n",
2145 rwi->reset_reason);
2146
Sukadev Bhattiprolu8f1c0fd2021-02-23 21:02:29 -08002147 /* read the state and check (again) after getting rtnl */
2148 reset_state = adapter->state;
2149
2150 if (reset_state == VNIC_REMOVING || reset_state == VNIC_REMOVED) {
2151 rc = -EBUSY;
2152 goto out;
2153 }
2154
Thomas Falcon2770a792018-05-23 13:38:02 -05002155 netif_carrier_off(netdev);
2156 adapter->reset_reason = rwi->reset_reason;
2157
2158 ibmvnic_cleanup(netdev);
2159 release_resources(adapter);
2160 release_sub_crqs(adapter, 0);
2161 release_crq_queue(adapter);
2162
2163 /* remove the closed state so when we call open it appears
2164 * we are coming from the probed state.
2165 */
2166 adapter->state = VNIC_PROBED;
2167
Thomas Falconbbd669a2019-04-04 18:58:26 -05002168 reinit_completion(&adapter->init_done);
Thomas Falcon2770a792018-05-23 13:38:02 -05002169 rc = init_crq_queue(adapter);
2170 if (rc) {
2171 netdev_err(adapter->netdev,
2172 "Couldn't initialize crq. rc=%d\n", rc);
Dany Madden0cb4bc62020-11-25 18:04:27 -06002173 goto out;
Thomas Falcon2770a792018-05-23 13:38:02 -05002174 }
2175
Lijun Pan635e4422020-08-19 17:52:26 -05002176 rc = ibmvnic_reset_init(adapter, false);
Thomas Falcon2770a792018-05-23 13:38:02 -05002177 if (rc)
Dany Madden0cb4bc62020-11-25 18:04:27 -06002178 goto out;
Thomas Falcon2770a792018-05-23 13:38:02 -05002179
2180 /* If the adapter was in PROBE state prior to the reset,
2181 * exit here.
2182 */
2183 if (reset_state == VNIC_PROBED)
Dany Madden0cb4bc62020-11-25 18:04:27 -06002184 goto out;
Thomas Falcon2770a792018-05-23 13:38:02 -05002185
2186 rc = ibmvnic_login(netdev);
Dany Madden0cb4bc62020-11-25 18:04:27 -06002187 if (rc)
2188 goto out;
Juliet Kima5681e22018-11-19 15:59:22 -06002189
2190 rc = init_resources(adapter);
Thomas Falcon2770a792018-05-23 13:38:02 -05002191 if (rc)
Dany Madden0cb4bc62020-11-25 18:04:27 -06002192 goto out;
Thomas Falcon2770a792018-05-23 13:38:02 -05002193
2194 ibmvnic_disable_irqs(adapter);
2195 adapter->state = VNIC_CLOSED;
2196
2197 if (reset_state == VNIC_CLOSED)
Dany Madden0cb4bc62020-11-25 18:04:27 -06002198 goto out;
Thomas Falcon2770a792018-05-23 13:38:02 -05002199
2200 rc = __ibmvnic_open(netdev);
Dany Madden0cb4bc62020-11-25 18:04:27 -06002201 if (rc) {
2202 rc = IBMVNIC_OPEN_FAILED;
2203 goto out;
2204 }
Thomas Falcon2770a792018-05-23 13:38:02 -05002205
Lijun Pan6be46662020-12-14 15:19:29 -06002206 __netdev_notify_peers(netdev);
Dany Madden0cb4bc62020-11-25 18:04:27 -06002207out:
2208 /* restore adapter state if reset failed */
2209 if (rc)
2210 adapter->state = reset_state;
Sukadev Bhattiprolu38bd5ce2020-12-04 18:22:35 -08002211 netdev_dbg(adapter->netdev, "[S:%d FOP:%d] Hard reset done, rc %d\n",
2212 adapter->state, adapter->failover_pending, rc);
Dany Madden0cb4bc62020-11-25 18:04:27 -06002213 return rc;
Thomas Falcon2770a792018-05-23 13:38:02 -05002214}
2215
Nathan Fontenoted651a12017-05-03 14:04:38 -04002216static struct ibmvnic_rwi *get_next_rwi(struct ibmvnic_adapter *adapter)
2217{
2218 struct ibmvnic_rwi *rwi;
Thomas Falcon6c5c7482018-12-10 15:22:22 -06002219 unsigned long flags;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002220
Thomas Falcon6c5c7482018-12-10 15:22:22 -06002221 spin_lock_irqsave(&adapter->rwi_lock, flags);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002222
2223 if (!list_empty(&adapter->rwi_list)) {
2224 rwi = list_first_entry(&adapter->rwi_list, struct ibmvnic_rwi,
2225 list);
2226 list_del(&rwi->list);
2227 } else {
2228 rwi = NULL;
2229 }
2230
Thomas Falcon6c5c7482018-12-10 15:22:22 -06002231 spin_unlock_irqrestore(&adapter->rwi_lock, flags);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002232 return rwi;
2233}
2234
Nathan Fontenoted651a12017-05-03 14:04:38 -04002235static void __ibmvnic_reset(struct work_struct *work)
2236{
2237 struct ibmvnic_rwi *rwi;
2238 struct ibmvnic_adapter *adapter;
Juliet Kim7d7195a2020-03-10 09:23:58 -05002239 bool saved_state = false;
2240 unsigned long flags;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002241 u32 reset_state;
John Allenc26eba02017-10-26 16:23:25 -05002242 int rc = 0;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002243
2244 adapter = container_of(work, struct ibmvnic_adapter, ibmvnic_reset);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002245
Juliet Kim7ed5b312019-09-20 16:11:23 -04002246 if (test_and_set_bit_lock(0, &adapter->resetting)) {
2247 schedule_delayed_work(&adapter->ibmvnic_delayed_reset,
2248 IBMVNIC_RESET_DELAY);
2249 return;
2250 }
2251
Nathan Fontenoted651a12017-05-03 14:04:38 -04002252 rwi = get_next_rwi(adapter);
2253 while (rwi) {
Juliet Kim7d7195a2020-03-10 09:23:58 -05002254 spin_lock_irqsave(&adapter->state_lock, flags);
2255
Thomas Falcon36f10312019-08-27 11:10:04 -05002256 if (adapter->state == VNIC_REMOVING ||
Michal Suchanekc8dc5592019-09-09 22:44:51 +02002257 adapter->state == VNIC_REMOVED) {
Juliet Kim7d7195a2020-03-10 09:23:58 -05002258 spin_unlock_irqrestore(&adapter->state_lock, flags);
Juliet Kim1c2977c2019-09-05 17:30:01 -04002259 kfree(rwi);
2260 rc = EBUSY;
2261 break;
2262 }
Thomas Falcon36f10312019-08-27 11:10:04 -05002263
Juliet Kim7d7195a2020-03-10 09:23:58 -05002264 if (!saved_state) {
2265 reset_state = adapter->state;
Juliet Kim7d7195a2020-03-10 09:23:58 -05002266 saved_state = true;
2267 }
2268 spin_unlock_irqrestore(&adapter->state_lock, flags);
2269
Lijun Pan3f5ec372021-01-06 15:35:14 -06002270 if (adapter->force_reset_recovery) {
Lijun Panbab08be2021-02-11 00:43:19 -06002271 /* Since we are doing a hard reset now, clear the
Sukadev Bhattiprolu1d850492020-10-30 10:07:11 -07002272 * failover_pending flag so we don't ignore any
2273 * future MOBILITY or other resets.
2274 */
2275 adapter->failover_pending = false;
2276
Juliet Kimb27507b2019-09-20 16:11:22 -04002277 /* Transport event occurred during previous reset */
2278 if (adapter->wait_for_reset) {
2279 /* Previous was CHANGE_PARAM; caller locked */
2280 adapter->force_reset_recovery = false;
2281 rc = do_hard_reset(adapter, rwi, reset_state);
2282 } else {
2283 rtnl_lock();
2284 adapter->force_reset_recovery = false;
2285 rc = do_hard_reset(adapter, rwi, reset_state);
2286 rtnl_unlock();
2287 }
Sukadev Bhattiproluf15fde92020-11-25 18:04:28 -06002288 if (rc) {
2289 /* give backing device time to settle down */
2290 netdev_dbg(adapter->netdev,
2291 "[S:%d] Hard reset failed, waiting 60 secs\n",
2292 adapter->state);
2293 set_current_state(TASK_UNINTERRUPTIBLE);
2294 schedule_timeout(60 * HZ);
2295 }
Lijun Pan1f45dc22020-12-23 14:49:04 -06002296 } else {
Thomas Falcon2770a792018-05-23 13:38:02 -05002297 rc = do_reset(adapter, rwi, reset_state);
2298 }
Nathan Fontenoted651a12017-05-03 14:04:38 -04002299 kfree(rwi);
Dany Maddena86d5c62020-11-25 18:04:31 -06002300 adapter->last_reset_time = jiffies;
Dany Madden0cb4bc62020-11-25 18:04:27 -06002301
Dany Madden18f141b2020-11-25 18:04:25 -06002302 if (rc)
2303 netdev_dbg(adapter->netdev, "Reset failed, rc=%d\n", rc);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002304
2305 rwi = get_next_rwi(adapter);
Juliet Kim7ed5b312019-09-20 16:11:23 -04002306
2307 if (rwi && (rwi->reset_reason == VNIC_RESET_FAILOVER ||
2308 rwi->reset_reason == VNIC_RESET_MOBILITY))
2309 adapter->force_reset_recovery = true;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002310 }
2311
John Allenc26eba02017-10-26 16:23:25 -05002312 if (adapter->wait_for_reset) {
John Allenc26eba02017-10-26 16:23:25 -05002313 adapter->reset_done_rc = rc;
2314 complete(&adapter->reset_done);
2315 }
2316
Juliet Kim7ed5b312019-09-20 16:11:23 -04002317 clear_bit_unlock(0, &adapter->resetting);
Sukadev Bhattiprolu38bd5ce2020-12-04 18:22:35 -08002318
2319 netdev_dbg(adapter->netdev,
2320 "[S:%d FRR:%d WFR:%d] Done processing resets\n",
2321 adapter->state, adapter->force_reset_recovery,
2322 adapter->wait_for_reset);
Juliet Kim7ed5b312019-09-20 16:11:23 -04002323}
2324
2325static void __ibmvnic_delayed_reset(struct work_struct *work)
2326{
2327 struct ibmvnic_adapter *adapter;
2328
2329 adapter = container_of(work, struct ibmvnic_adapter,
2330 ibmvnic_delayed_reset.work);
2331 __ibmvnic_reset(&adapter->ibmvnic_reset);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002332}
2333
Thomas Falconaf894d22018-04-06 18:37:04 -05002334static int ibmvnic_reset(struct ibmvnic_adapter *adapter,
2335 enum ibmvnic_reset_reason reason)
Nathan Fontenoted651a12017-05-03 14:04:38 -04002336{
Thomas Falcon2770a792018-05-23 13:38:02 -05002337 struct list_head *entry, *tmp_entry;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002338 struct ibmvnic_rwi *rwi, *tmp;
2339 struct net_device *netdev = adapter->netdev;
Thomas Falcon6c5c7482018-12-10 15:22:22 -06002340 unsigned long flags;
Thomas Falconaf894d22018-04-06 18:37:04 -05002341 int ret;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002342
Jakub Kicinskib646acd52021-02-16 22:58:44 -08002343 spin_lock_irqsave(&adapter->rwi_lock, flags);
2344
2345 /* If failover is pending don't schedule any other reset.
Sukadev Bhattiprolu1d850492020-10-30 10:07:11 -07002346 * Instead let the failover complete. If there is already a
2347 * a failover reset scheduled, we will detect and drop the
2348 * duplicate reset when walking the ->rwi_list below.
2349 */
Nathan Fontenoted651a12017-05-03 14:04:38 -04002350 if (adapter->state == VNIC_REMOVING ||
Thomas Falcon5a18e1e2018-04-06 18:37:05 -05002351 adapter->state == VNIC_REMOVED ||
Sukadev Bhattiprolu1d850492020-10-30 10:07:11 -07002352 (adapter->failover_pending && reason != VNIC_RESET_FAILOVER)) {
Thomas Falconaf894d22018-04-06 18:37:04 -05002353 ret = EBUSY;
Thomas Falcon5a18e1e2018-04-06 18:37:05 -05002354 netdev_dbg(netdev, "Adapter removing or pending failover, skipping reset\n");
Thomas Falconaf894d22018-04-06 18:37:04 -05002355 goto err;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002356 }
2357
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04002358 if (adapter->state == VNIC_PROBING) {
2359 netdev_warn(netdev, "Adapter reset during probe\n");
Lijun Pan91dc5d22021-02-11 00:43:22 -06002360 adapter->init_done_rc = EAGAIN;
2361 ret = EAGAIN;
Thomas Falconaf894d22018-04-06 18:37:04 -05002362 goto err;
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04002363 }
2364
Nathan Fontenoted651a12017-05-03 14:04:38 -04002365 list_for_each(entry, &adapter->rwi_list) {
2366 tmp = list_entry(entry, struct ibmvnic_rwi, list);
2367 if (tmp->reset_reason == reason) {
Sukadev Bhattiprolu38bd5ce2020-12-04 18:22:35 -08002368 netdev_dbg(netdev, "Skipping matching reset, reason=%d\n",
2369 reason);
Thomas Falconaf894d22018-04-06 18:37:04 -05002370 ret = EBUSY;
2371 goto err;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002372 }
2373 }
2374
Thomas Falcon1d1bbc32018-12-10 15:22:23 -06002375 rwi = kzalloc(sizeof(*rwi), GFP_ATOMIC);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002376 if (!rwi) {
Thomas Falconaf894d22018-04-06 18:37:04 -05002377 ret = ENOMEM;
2378 goto err;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002379 }
Thomas Falcon2770a792018-05-23 13:38:02 -05002380 /* if we just received a transport event,
2381 * flush reset queue and process this reset
2382 */
2383 if (adapter->force_reset_recovery && !list_empty(&adapter->rwi_list)) {
2384 list_for_each_safe(entry, tmp_entry, &adapter->rwi_list)
2385 list_del(entry);
2386 }
Nathan Fontenoted651a12017-05-03 14:04:38 -04002387 rwi->reset_reason = reason;
2388 list_add_tail(&rwi->list, &adapter->rwi_list);
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002389 netdev_dbg(adapter->netdev, "Scheduling reset (reason %d)\n", reason);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002390 schedule_work(&adapter->ibmvnic_reset);
Thomas Falconaf894d22018-04-06 18:37:04 -05002391
Sukadev Bhattiprolu4a41c422021-02-12 20:42:50 -08002392 ret = 0;
Thomas Falconaf894d22018-04-06 18:37:04 -05002393err:
Sukadev Bhattiprolu4a41c422021-02-12 20:42:50 -08002394 /* ibmvnic_close() below can block, so drop the lock first */
2395 spin_unlock_irqrestore(&adapter->rwi_lock, flags);
2396
2397 if (ret == ENOMEM)
2398 ibmvnic_close(netdev);
2399
Thomas Falconaf894d22018-04-06 18:37:04 -05002400 return -ret;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002401}
2402
Michael S. Tsirkin0290bd22019-12-10 09:23:51 -05002403static void ibmvnic_tx_timeout(struct net_device *dev, unsigned int txqueue)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002404{
2405 struct ibmvnic_adapter *adapter = netdev_priv(dev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002406
Lijun Pan855a6312020-11-20 16:40:13 -06002407 if (test_bit(0, &adapter->resetting)) {
2408 netdev_err(adapter->netdev,
2409 "Adapter is resetting, skip timeout reset\n");
2410 return;
2411 }
Dany Maddena86d5c62020-11-25 18:04:31 -06002412 /* No queuing up reset until at least 5 seconds (default watchdog val)
2413 * after last reset
2414 */
2415 if (time_before(jiffies, (adapter->last_reset_time + dev->watchdog_timeo))) {
2416 netdev_dbg(dev, "Not yet time to tx timeout.\n");
2417 return;
2418 }
Nathan Fontenoted651a12017-05-03 14:04:38 -04002419 ibmvnic_reset(adapter, VNIC_RESET_TIMEOUT);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002420}
2421
2422static void remove_buff_from_pool(struct ibmvnic_adapter *adapter,
2423 struct ibmvnic_rx_buff *rx_buff)
2424{
2425 struct ibmvnic_rx_pool *pool = &adapter->rx_pool[rx_buff->pool_index];
2426
2427 rx_buff->skb = NULL;
2428
2429 pool->free_map[pool->next_alloc] = (int)(rx_buff - pool->rx_buff);
2430 pool->next_alloc = (pool->next_alloc + 1) % pool->size;
2431
2432 atomic_dec(&pool->available);
2433}
2434
2435static int ibmvnic_poll(struct napi_struct *napi, int budget)
2436{
Dwip N. Banerjeeec20f362020-11-18 19:12:23 -06002437 struct ibmvnic_sub_crq_queue *rx_scrq;
2438 struct ibmvnic_adapter *adapter;
2439 struct net_device *netdev;
2440 int frames_processed;
2441 int scrq_num;
2442
2443 netdev = napi->dev;
2444 adapter = netdev_priv(netdev);
2445 scrq_num = (int)(napi - adapter->napi);
2446 frames_processed = 0;
2447 rx_scrq = adapter->rx_scrq[scrq_num];
Nathan Fontenot152ce472017-05-26 10:30:54 -04002448
Thomas Falcon032c5e82015-12-21 11:26:06 -06002449restart_poll:
2450 while (frames_processed < budget) {
2451 struct sk_buff *skb;
2452 struct ibmvnic_rx_buff *rx_buff;
2453 union sub_crq *next;
2454 u32 length;
2455 u16 offset;
2456 u8 flags = 0;
2457
Juliet Kim7ed5b312019-09-20 16:11:23 -04002458 if (unlikely(test_bit(0, &adapter->resetting) &&
John Allen34686562018-02-06 16:21:49 -06002459 adapter->reset_reason != VNIC_RESET_NON_FATAL)) {
Dwip N. Banerjeeec20f362020-11-18 19:12:23 -06002460 enable_scrq_irq(adapter, rx_scrq);
Thomas Falcon21ecba62017-06-14 23:50:09 -05002461 napi_complete_done(napi, frames_processed);
2462 return frames_processed;
2463 }
2464
Dwip N. Banerjeeec20f362020-11-18 19:12:23 -06002465 if (!pending_scrq(adapter, rx_scrq))
Thomas Falcon032c5e82015-12-21 11:26:06 -06002466 break;
Dwip N. Banerjeeec20f362020-11-18 19:12:23 -06002467 next = ibmvnic_next_scrq(adapter, rx_scrq);
Lijun Pan914789a2021-02-11 00:43:21 -06002468 rx_buff = (struct ibmvnic_rx_buff *)
2469 be64_to_cpu(next->rx_comp.correlator);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002470 /* do error checking */
2471 if (next->rx_comp.rc) {
John Allene1cea2e2017-08-07 15:42:30 -05002472 netdev_dbg(netdev, "rx buffer returned with rc %x\n",
2473 be16_to_cpu(next->rx_comp.rc));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002474 /* free the entry */
2475 next->rx_comp.first = 0;
Thomas Falcon4b9b0f02018-02-13 18:23:42 -06002476 dev_kfree_skb_any(rx_buff->skb);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002477 remove_buff_from_pool(adapter, rx_buff);
Nathan Fontenotca05e312017-05-03 14:05:14 -04002478 continue;
Thomas Falconabe27a82018-02-19 20:12:57 -06002479 } else if (!rx_buff->skb) {
2480 /* free the entry */
2481 next->rx_comp.first = 0;
2482 remove_buff_from_pool(adapter, rx_buff);
2483 continue;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002484 }
2485
2486 length = be32_to_cpu(next->rx_comp.len);
2487 offset = be16_to_cpu(next->rx_comp.off_frame_data);
2488 flags = next->rx_comp.flags;
2489 skb = rx_buff->skb;
Lijun Pan42557da2021-02-12 20:48:40 -06002490 /* load long_term_buff before copying to skb */
2491 dma_rmb();
Thomas Falcon032c5e82015-12-21 11:26:06 -06002492 skb_copy_to_linear_data(skb, rx_buff->data + offset,
2493 length);
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04002494
2495 /* VLAN Header has been stripped by the system firmware and
2496 * needs to be inserted by the driver
2497 */
2498 if (adapter->rx_vlan_header_insertion &&
2499 (flags & IBMVNIC_VLAN_STRIPPED))
2500 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
2501 ntohs(next->rx_comp.vlan_tci));
2502
Thomas Falcon032c5e82015-12-21 11:26:06 -06002503 /* free the entry */
2504 next->rx_comp.first = 0;
2505 remove_buff_from_pool(adapter, rx_buff);
2506
2507 skb_put(skb, length);
2508 skb->protocol = eth_type_trans(skb, netdev);
Thomas Falcon94ca3052017-05-03 14:05:20 -04002509 skb_record_rx_queue(skb, scrq_num);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002510
2511 if (flags & IBMVNIC_IP_CHKSUM_GOOD &&
2512 flags & IBMVNIC_TCP_UDP_CHKSUM_GOOD) {
2513 skb->ip_summed = CHECKSUM_UNNECESSARY;
2514 }
2515
2516 length = skb->len;
2517 napi_gro_receive(napi, skb); /* send it up */
2518 netdev->stats.rx_packets++;
2519 netdev->stats.rx_bytes += length;
John Allen3d52b592017-08-02 16:44:14 -05002520 adapter->rx_stats_buffers[scrq_num].packets++;
2521 adapter->rx_stats_buffers[scrq_num].bytes += length;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002522 frames_processed++;
2523 }
Nathan Fontenot152ce472017-05-26 10:30:54 -04002524
Dwip N. Banerjee41ed0a02020-11-18 19:12:25 -06002525 if (adapter->state != VNIC_CLOSING &&
2526 ((atomic_read(&adapter->rx_pool[scrq_num].available) <
2527 adapter->req_rx_add_entries_per_subcrq / 2) ||
2528 frames_processed < budget))
Nathan Fontenot152ce472017-05-26 10:30:54 -04002529 replenish_rx_pool(adapter, &adapter->rx_pool[scrq_num]);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002530 if (frames_processed < budget) {
Dwip N. Banerjeeec20f362020-11-18 19:12:23 -06002531 if (napi_complete_done(napi, frames_processed)) {
2532 enable_scrq_irq(adapter, rx_scrq);
2533 if (pending_scrq(adapter, rx_scrq)) {
Dwip N. Banerjeeec20f362020-11-18 19:12:23 -06002534 if (napi_reschedule(napi)) {
2535 disable_scrq_irq(adapter, rx_scrq);
2536 goto restart_poll;
2537 }
2538 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002539 }
2540 }
2541 return frames_processed;
2542}
2543
John Allenc26eba02017-10-26 16:23:25 -05002544static int wait_for_reset(struct ibmvnic_adapter *adapter)
2545{
Thomas Falconaf894d22018-04-06 18:37:04 -05002546 int rc, ret;
2547
John Allenc26eba02017-10-26 16:23:25 -05002548 adapter->fallback.mtu = adapter->req_mtu;
2549 adapter->fallback.rx_queues = adapter->req_rx_queues;
2550 adapter->fallback.tx_queues = adapter->req_tx_queues;
2551 adapter->fallback.rx_entries = adapter->req_rx_add_entries_per_subcrq;
2552 adapter->fallback.tx_entries = adapter->req_tx_entries_per_subcrq;
2553
Thomas Falcon070eca92019-11-25 17:12:53 -06002554 reinit_completion(&adapter->reset_done);
John Allenc26eba02017-10-26 16:23:25 -05002555 adapter->wait_for_reset = true;
Thomas Falconaf894d22018-04-06 18:37:04 -05002556 rc = ibmvnic_reset(adapter, VNIC_RESET_CHANGE_PARAM);
Thomas Falcon476d96c2019-11-25 17:12:55 -06002557
2558 if (rc) {
2559 ret = rc;
2560 goto out;
2561 }
2562 rc = ibmvnic_wait_for_completion(adapter, &adapter->reset_done, 60000);
2563 if (rc) {
2564 ret = -ENODEV;
2565 goto out;
2566 }
John Allenc26eba02017-10-26 16:23:25 -05002567
Thomas Falconaf894d22018-04-06 18:37:04 -05002568 ret = 0;
John Allenc26eba02017-10-26 16:23:25 -05002569 if (adapter->reset_done_rc) {
Thomas Falconaf894d22018-04-06 18:37:04 -05002570 ret = -EIO;
John Allenc26eba02017-10-26 16:23:25 -05002571 adapter->desired.mtu = adapter->fallback.mtu;
2572 adapter->desired.rx_queues = adapter->fallback.rx_queues;
2573 adapter->desired.tx_queues = adapter->fallback.tx_queues;
2574 adapter->desired.rx_entries = adapter->fallback.rx_entries;
2575 adapter->desired.tx_entries = adapter->fallback.tx_entries;
2576
Thomas Falcon070eca92019-11-25 17:12:53 -06002577 reinit_completion(&adapter->reset_done);
Thomas Falconaf894d22018-04-06 18:37:04 -05002578 adapter->wait_for_reset = true;
2579 rc = ibmvnic_reset(adapter, VNIC_RESET_CHANGE_PARAM);
Thomas Falcon476d96c2019-11-25 17:12:55 -06002580 if (rc) {
2581 ret = rc;
2582 goto out;
2583 }
2584 rc = ibmvnic_wait_for_completion(adapter, &adapter->reset_done,
2585 60000);
2586 if (rc) {
2587 ret = -ENODEV;
2588 goto out;
2589 }
John Allenc26eba02017-10-26 16:23:25 -05002590 }
Thomas Falcon476d96c2019-11-25 17:12:55 -06002591out:
John Allenc26eba02017-10-26 16:23:25 -05002592 adapter->wait_for_reset = false;
2593
Thomas Falconaf894d22018-04-06 18:37:04 -05002594 return ret;
John Allenc26eba02017-10-26 16:23:25 -05002595}
2596
John Allen3a807b72017-06-06 16:55:52 -05002597static int ibmvnic_change_mtu(struct net_device *netdev, int new_mtu)
2598{
John Allenc26eba02017-10-26 16:23:25 -05002599 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2600
2601 adapter->desired.mtu = new_mtu + ETH_HLEN;
2602
2603 return wait_for_reset(adapter);
John Allen3a807b72017-06-06 16:55:52 -05002604}
2605
Thomas Falconf10b09e2018-03-12 11:51:05 -05002606static netdev_features_t ibmvnic_features_check(struct sk_buff *skb,
2607 struct net_device *dev,
2608 netdev_features_t features)
2609{
2610 /* Some backing hardware adapters can not
2611 * handle packets with a MSS less than 224
2612 * or with only one segment.
2613 */
2614 if (skb_is_gso(skb)) {
2615 if (skb_shinfo(skb)->gso_size < 224 ||
2616 skb_shinfo(skb)->gso_segs == 1)
2617 features &= ~NETIF_F_GSO_MASK;
2618 }
2619
2620 return features;
2621}
2622
Thomas Falcon032c5e82015-12-21 11:26:06 -06002623static const struct net_device_ops ibmvnic_netdev_ops = {
2624 .ndo_open = ibmvnic_open,
2625 .ndo_stop = ibmvnic_close,
2626 .ndo_start_xmit = ibmvnic_xmit,
2627 .ndo_set_rx_mode = ibmvnic_set_multi,
2628 .ndo_set_mac_address = ibmvnic_set_mac,
2629 .ndo_validate_addr = eth_validate_addr,
Thomas Falcon032c5e82015-12-21 11:26:06 -06002630 .ndo_tx_timeout = ibmvnic_tx_timeout,
John Allen3a807b72017-06-06 16:55:52 -05002631 .ndo_change_mtu = ibmvnic_change_mtu,
Thomas Falconf10b09e2018-03-12 11:51:05 -05002632 .ndo_features_check = ibmvnic_features_check,
Thomas Falcon032c5e82015-12-21 11:26:06 -06002633};
2634
2635/* ethtool functions */
2636
Philippe Reynes8a433792017-01-07 22:37:29 +01002637static int ibmvnic_get_link_ksettings(struct net_device *netdev,
2638 struct ethtool_link_ksettings *cmd)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002639{
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03002640 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2641 int rc;
Philippe Reynes8a433792017-01-07 22:37:29 +01002642
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03002643 rc = send_query_phys_parms(adapter);
2644 if (rc) {
2645 adapter->speed = SPEED_UNKNOWN;
2646 adapter->duplex = DUPLEX_UNKNOWN;
2647 }
2648 cmd->base.speed = adapter->speed;
2649 cmd->base.duplex = adapter->duplex;
Philippe Reynes8a433792017-01-07 22:37:29 +01002650 cmd->base.port = PORT_FIBRE;
2651 cmd->base.phy_address = 0;
2652 cmd->base.autoneg = AUTONEG_ENABLE;
2653
Thomas Falcon032c5e82015-12-21 11:26:06 -06002654 return 0;
2655}
2656
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02002657static void ibmvnic_get_drvinfo(struct net_device *netdev,
Thomas Falcon032c5e82015-12-21 11:26:06 -06002658 struct ethtool_drvinfo *info)
2659{
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02002660 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2661
Lijun Pan8a96c802021-02-11 00:43:25 -06002662 strscpy(info->driver, ibmvnic_driver_name, sizeof(info->driver));
2663 strscpy(info->version, IBMVNIC_DRIVER_VERSION, sizeof(info->version));
2664 strscpy(info->fw_version, adapter->fw_version,
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02002665 sizeof(info->fw_version));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002666}
2667
2668static u32 ibmvnic_get_msglevel(struct net_device *netdev)
2669{
2670 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2671
2672 return adapter->msg_enable;
2673}
2674
2675static void ibmvnic_set_msglevel(struct net_device *netdev, u32 data)
2676{
2677 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2678
2679 adapter->msg_enable = data;
2680}
2681
2682static u32 ibmvnic_get_link(struct net_device *netdev)
2683{
2684 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2685
2686 /* Don't need to send a query because we request a logical link up at
2687 * init and then we wait for link state indications
2688 */
2689 return adapter->logical_link_state;
2690}
2691
2692static void ibmvnic_get_ringparam(struct net_device *netdev,
2693 struct ethtool_ringparam *ring)
2694{
John Allenbc131b32017-08-02 16:46:30 -05002695 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2696
Thomas Falcon723ad912018-09-28 18:38:26 -05002697 if (adapter->priv_flags & IBMVNIC_USE_SERVER_MAXES) {
2698 ring->rx_max_pending = adapter->max_rx_add_entries_per_subcrq;
2699 ring->tx_max_pending = adapter->max_tx_entries_per_subcrq;
2700 } else {
2701 ring->rx_max_pending = IBMVNIC_MAX_QUEUE_SZ;
2702 ring->tx_max_pending = IBMVNIC_MAX_QUEUE_SZ;
2703 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002704 ring->rx_mini_max_pending = 0;
2705 ring->rx_jumbo_max_pending = 0;
John Allenbc131b32017-08-02 16:46:30 -05002706 ring->rx_pending = adapter->req_rx_add_entries_per_subcrq;
2707 ring->tx_pending = adapter->req_tx_entries_per_subcrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002708 ring->rx_mini_pending = 0;
2709 ring->rx_jumbo_pending = 0;
2710}
2711
John Allenc26eba02017-10-26 16:23:25 -05002712static int ibmvnic_set_ringparam(struct net_device *netdev,
2713 struct ethtool_ringparam *ring)
2714{
2715 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Thomas Falcon723ad912018-09-28 18:38:26 -05002716 int ret;
John Allenc26eba02017-10-26 16:23:25 -05002717
Thomas Falcon723ad912018-09-28 18:38:26 -05002718 ret = 0;
John Allenc26eba02017-10-26 16:23:25 -05002719 adapter->desired.rx_entries = ring->rx_pending;
2720 adapter->desired.tx_entries = ring->tx_pending;
2721
Thomas Falcon723ad912018-09-28 18:38:26 -05002722 ret = wait_for_reset(adapter);
2723
2724 if (!ret &&
2725 (adapter->req_rx_add_entries_per_subcrq != ring->rx_pending ||
2726 adapter->req_tx_entries_per_subcrq != ring->tx_pending))
2727 netdev_info(netdev,
2728 "Could not match full ringsize request. Requested: RX %d, TX %d; Allowed: RX %llu, TX %llu\n",
2729 ring->rx_pending, ring->tx_pending,
2730 adapter->req_rx_add_entries_per_subcrq,
2731 adapter->req_tx_entries_per_subcrq);
2732 return ret;
John Allenc26eba02017-10-26 16:23:25 -05002733}
2734
John Allenc2dbeb62017-08-02 16:47:17 -05002735static void ibmvnic_get_channels(struct net_device *netdev,
2736 struct ethtool_channels *channels)
2737{
2738 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2739
Thomas Falcon723ad912018-09-28 18:38:26 -05002740 if (adapter->priv_flags & IBMVNIC_USE_SERVER_MAXES) {
2741 channels->max_rx = adapter->max_rx_queues;
2742 channels->max_tx = adapter->max_tx_queues;
2743 } else {
2744 channels->max_rx = IBMVNIC_MAX_QUEUES;
2745 channels->max_tx = IBMVNIC_MAX_QUEUES;
2746 }
2747
John Allenc2dbeb62017-08-02 16:47:17 -05002748 channels->max_other = 0;
2749 channels->max_combined = 0;
2750 channels->rx_count = adapter->req_rx_queues;
2751 channels->tx_count = adapter->req_tx_queues;
2752 channels->other_count = 0;
2753 channels->combined_count = 0;
2754}
2755
John Allenc26eba02017-10-26 16:23:25 -05002756static int ibmvnic_set_channels(struct net_device *netdev,
2757 struct ethtool_channels *channels)
2758{
2759 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Thomas Falcon723ad912018-09-28 18:38:26 -05002760 int ret;
John Allenc26eba02017-10-26 16:23:25 -05002761
Thomas Falcon723ad912018-09-28 18:38:26 -05002762 ret = 0;
John Allenc26eba02017-10-26 16:23:25 -05002763 adapter->desired.rx_queues = channels->rx_count;
2764 adapter->desired.tx_queues = channels->tx_count;
2765
Thomas Falcon723ad912018-09-28 18:38:26 -05002766 ret = wait_for_reset(adapter);
2767
2768 if (!ret &&
2769 (adapter->req_rx_queues != channels->rx_count ||
2770 adapter->req_tx_queues != channels->tx_count))
2771 netdev_info(netdev,
2772 "Could not match full channels request. Requested: RX %d, TX %d; Allowed: RX %llu, TX %llu\n",
2773 channels->rx_count, channels->tx_count,
2774 adapter->req_rx_queues, adapter->req_tx_queues);
2775 return ret;
John Allenc26eba02017-10-26 16:23:25 -05002776}
2777
Thomas Falcon032c5e82015-12-21 11:26:06 -06002778static void ibmvnic_get_strings(struct net_device *dev, u32 stringset, u8 *data)
2779{
John Allen3d52b592017-08-02 16:44:14 -05002780 struct ibmvnic_adapter *adapter = netdev_priv(dev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002781 int i;
2782
Thomas Falcon723ad912018-09-28 18:38:26 -05002783 switch (stringset) {
2784 case ETH_SS_STATS:
2785 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats);
2786 i++, data += ETH_GSTRING_LEN)
2787 memcpy(data, ibmvnic_stats[i].name, ETH_GSTRING_LEN);
2788
2789 for (i = 0; i < adapter->req_tx_queues; i++) {
2790 snprintf(data, ETH_GSTRING_LEN, "tx%d_packets", i);
2791 data += ETH_GSTRING_LEN;
2792
2793 snprintf(data, ETH_GSTRING_LEN, "tx%d_bytes", i);
2794 data += ETH_GSTRING_LEN;
2795
2796 snprintf(data, ETH_GSTRING_LEN,
2797 "tx%d_dropped_packets", i);
2798 data += ETH_GSTRING_LEN;
2799 }
2800
2801 for (i = 0; i < adapter->req_rx_queues; i++) {
2802 snprintf(data, ETH_GSTRING_LEN, "rx%d_packets", i);
2803 data += ETH_GSTRING_LEN;
2804
2805 snprintf(data, ETH_GSTRING_LEN, "rx%d_bytes", i);
2806 data += ETH_GSTRING_LEN;
2807
2808 snprintf(data, ETH_GSTRING_LEN, "rx%d_interrupts", i);
2809 data += ETH_GSTRING_LEN;
2810 }
2811 break;
2812
2813 case ETH_SS_PRIV_FLAGS:
2814 for (i = 0; i < ARRAY_SIZE(ibmvnic_priv_flags); i++)
2815 strcpy(data + i * ETH_GSTRING_LEN,
2816 ibmvnic_priv_flags[i]);
2817 break;
2818 default:
Thomas Falcon032c5e82015-12-21 11:26:06 -06002819 return;
John Allen3d52b592017-08-02 16:44:14 -05002820 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002821}
2822
2823static int ibmvnic_get_sset_count(struct net_device *dev, int sset)
2824{
John Allen3d52b592017-08-02 16:44:14 -05002825 struct ibmvnic_adapter *adapter = netdev_priv(dev);
2826
Thomas Falcon032c5e82015-12-21 11:26:06 -06002827 switch (sset) {
2828 case ETH_SS_STATS:
John Allen3d52b592017-08-02 16:44:14 -05002829 return ARRAY_SIZE(ibmvnic_stats) +
2830 adapter->req_tx_queues * NUM_TX_STATS +
2831 adapter->req_rx_queues * NUM_RX_STATS;
Thomas Falcon723ad912018-09-28 18:38:26 -05002832 case ETH_SS_PRIV_FLAGS:
2833 return ARRAY_SIZE(ibmvnic_priv_flags);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002834 default:
2835 return -EOPNOTSUPP;
2836 }
2837}
2838
2839static void ibmvnic_get_ethtool_stats(struct net_device *dev,
2840 struct ethtool_stats *stats, u64 *data)
2841{
2842 struct ibmvnic_adapter *adapter = netdev_priv(dev);
2843 union ibmvnic_crq crq;
John Allen3d52b592017-08-02 16:44:14 -05002844 int i, j;
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05002845 int rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002846
2847 memset(&crq, 0, sizeof(crq));
2848 crq.request_statistics.first = IBMVNIC_CRQ_CMD;
2849 crq.request_statistics.cmd = REQUEST_STATISTICS;
2850 crq.request_statistics.ioba = cpu_to_be32(adapter->stats_token);
2851 crq.request_statistics.len =
2852 cpu_to_be32(sizeof(struct ibmvnic_statistics));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002853
2854 /* Wait for data to be written */
Thomas Falcon070eca92019-11-25 17:12:53 -06002855 reinit_completion(&adapter->stats_done);
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05002856 rc = ibmvnic_send_crq(adapter, &crq);
2857 if (rc)
2858 return;
Thomas Falcon476d96c2019-11-25 17:12:55 -06002859 rc = ibmvnic_wait_for_completion(adapter, &adapter->stats_done, 10000);
2860 if (rc)
2861 return;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002862
2863 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++)
Lijun Pan91dc5d22021-02-11 00:43:22 -06002864 data[i] = be64_to_cpu(IBMVNIC_GET_STAT
2865 (adapter, ibmvnic_stats[i].offset));
John Allen3d52b592017-08-02 16:44:14 -05002866
2867 for (j = 0; j < adapter->req_tx_queues; j++) {
2868 data[i] = adapter->tx_stats_buffers[j].packets;
2869 i++;
2870 data[i] = adapter->tx_stats_buffers[j].bytes;
2871 i++;
2872 data[i] = adapter->tx_stats_buffers[j].dropped_packets;
2873 i++;
2874 }
2875
2876 for (j = 0; j < adapter->req_rx_queues; j++) {
2877 data[i] = adapter->rx_stats_buffers[j].packets;
2878 i++;
2879 data[i] = adapter->rx_stats_buffers[j].bytes;
2880 i++;
2881 data[i] = adapter->rx_stats_buffers[j].interrupts;
2882 i++;
2883 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002884}
2885
Thomas Falcon723ad912018-09-28 18:38:26 -05002886static u32 ibmvnic_get_priv_flags(struct net_device *netdev)
2887{
2888 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2889
2890 return adapter->priv_flags;
2891}
2892
2893static int ibmvnic_set_priv_flags(struct net_device *netdev, u32 flags)
2894{
2895 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2896 bool which_maxes = !!(flags & IBMVNIC_USE_SERVER_MAXES);
2897
2898 if (which_maxes)
2899 adapter->priv_flags |= IBMVNIC_USE_SERVER_MAXES;
2900 else
2901 adapter->priv_flags &= ~IBMVNIC_USE_SERVER_MAXES;
2902
2903 return 0;
2904}
Lijun Pan91dc5d22021-02-11 00:43:22 -06002905
Thomas Falcon032c5e82015-12-21 11:26:06 -06002906static const struct ethtool_ops ibmvnic_ethtool_ops = {
Thomas Falcon032c5e82015-12-21 11:26:06 -06002907 .get_drvinfo = ibmvnic_get_drvinfo,
2908 .get_msglevel = ibmvnic_get_msglevel,
2909 .set_msglevel = ibmvnic_set_msglevel,
2910 .get_link = ibmvnic_get_link,
2911 .get_ringparam = ibmvnic_get_ringparam,
John Allenc26eba02017-10-26 16:23:25 -05002912 .set_ringparam = ibmvnic_set_ringparam,
John Allenc2dbeb62017-08-02 16:47:17 -05002913 .get_channels = ibmvnic_get_channels,
John Allenc26eba02017-10-26 16:23:25 -05002914 .set_channels = ibmvnic_set_channels,
Thomas Falcon032c5e82015-12-21 11:26:06 -06002915 .get_strings = ibmvnic_get_strings,
2916 .get_sset_count = ibmvnic_get_sset_count,
2917 .get_ethtool_stats = ibmvnic_get_ethtool_stats,
Philippe Reynes8a433792017-01-07 22:37:29 +01002918 .get_link_ksettings = ibmvnic_get_link_ksettings,
Thomas Falcon723ad912018-09-28 18:38:26 -05002919 .get_priv_flags = ibmvnic_get_priv_flags,
2920 .set_priv_flags = ibmvnic_set_priv_flags,
Thomas Falcon032c5e82015-12-21 11:26:06 -06002921};
2922
2923/* Routines for managing CRQs/sCRQs */
2924
Nathan Fontenot57a49432017-05-26 10:31:12 -04002925static int reset_one_sub_crq_queue(struct ibmvnic_adapter *adapter,
2926 struct ibmvnic_sub_crq_queue *scrq)
2927{
2928 int rc;
2929
Dany Madden9281cf22020-11-25 18:04:26 -06002930 if (!scrq) {
YANG LI862aecb2020-12-30 15:23:14 +08002931 netdev_dbg(adapter->netdev, "Invalid scrq reset.\n");
Dany Madden9281cf22020-11-25 18:04:26 -06002932 return -EINVAL;
2933 }
2934
Nathan Fontenot57a49432017-05-26 10:31:12 -04002935 if (scrq->irq) {
2936 free_irq(scrq->irq, scrq);
2937 irq_dispose_mapping(scrq->irq);
2938 scrq->irq = 0;
2939 }
2940
Dany Madden9281cf22020-11-25 18:04:26 -06002941 if (scrq->msgs) {
2942 memset(scrq->msgs, 0, 4 * PAGE_SIZE);
2943 atomic_set(&scrq->used, 0);
2944 scrq->cur = 0;
Jakub Kicinski55fd59b2020-12-03 15:42:13 -08002945 scrq->ind_buf.index = 0;
Dany Madden9281cf22020-11-25 18:04:26 -06002946 } else {
2947 netdev_dbg(adapter->netdev, "Invalid scrq reset\n");
2948 return -EINVAL;
2949 }
Nathan Fontenot57a49432017-05-26 10:31:12 -04002950
2951 rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
2952 4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
2953 return rc;
2954}
2955
2956static int reset_sub_crq_queues(struct ibmvnic_adapter *adapter)
2957{
2958 int i, rc;
2959
Lijun Pana0faaa22020-11-23 13:35:45 -06002960 if (!adapter->tx_scrq || !adapter->rx_scrq)
2961 return -EINVAL;
2962
Nathan Fontenot57a49432017-05-26 10:31:12 -04002963 for (i = 0; i < adapter->req_tx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002964 netdev_dbg(adapter->netdev, "Re-setting tx_scrq[%d]\n", i);
Nathan Fontenot57a49432017-05-26 10:31:12 -04002965 rc = reset_one_sub_crq_queue(adapter, adapter->tx_scrq[i]);
2966 if (rc)
2967 return rc;
2968 }
2969
2970 for (i = 0; i < adapter->req_rx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002971 netdev_dbg(adapter->netdev, "Re-setting rx_scrq[%d]\n", i);
Nathan Fontenot57a49432017-05-26 10:31:12 -04002972 rc = reset_one_sub_crq_queue(adapter, adapter->rx_scrq[i]);
2973 if (rc)
2974 return rc;
2975 }
2976
Nathan Fontenot57a49432017-05-26 10:31:12 -04002977 return rc;
2978}
2979
Thomas Falcon032c5e82015-12-21 11:26:06 -06002980static void release_sub_crq_queue(struct ibmvnic_adapter *adapter,
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06002981 struct ibmvnic_sub_crq_queue *scrq,
2982 bool do_h_free)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002983{
2984 struct device *dev = &adapter->vdev->dev;
2985 long rc;
2986
2987 netdev_dbg(adapter->netdev, "Releasing sub-CRQ\n");
2988
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06002989 if (do_h_free) {
2990 /* Close the sub-crqs */
2991 do {
2992 rc = plpar_hcall_norets(H_FREE_SUB_CRQ,
2993 adapter->vdev->unit_address,
2994 scrq->crq_num);
2995 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002996
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06002997 if (rc) {
2998 netdev_err(adapter->netdev,
2999 "Failed to release sub-CRQ %16lx, rc = %ld\n",
3000 scrq->crq_num, rc);
3001 }
Thomas Falconffa73852017-04-19 13:44:29 -04003002 }
3003
Thomas Falconf019fb62020-11-18 19:12:17 -06003004 dma_free_coherent(dev,
3005 IBMVNIC_IND_ARR_SZ,
3006 scrq->ind_buf.indir_arr,
3007 scrq->ind_buf.indir_dma);
3008
Thomas Falcon032c5e82015-12-21 11:26:06 -06003009 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
3010 DMA_BIDIRECTIONAL);
3011 free_pages((unsigned long)scrq->msgs, 2);
3012 kfree(scrq);
3013}
3014
3015static struct ibmvnic_sub_crq_queue *init_sub_crq_queue(struct ibmvnic_adapter
3016 *adapter)
3017{
3018 struct device *dev = &adapter->vdev->dev;
3019 struct ibmvnic_sub_crq_queue *scrq;
3020 int rc;
3021
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04003022 scrq = kzalloc(sizeof(*scrq), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003023 if (!scrq)
3024 return NULL;
3025
Nathan Fontenot7f7adc52017-04-19 13:45:16 -04003026 scrq->msgs =
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04003027 (union sub_crq *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, 2);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003028 if (!scrq->msgs) {
3029 dev_warn(dev, "Couldn't allocate crq queue messages page\n");
3030 goto zero_page_failed;
3031 }
3032
3033 scrq->msg_token = dma_map_single(dev, scrq->msgs, 4 * PAGE_SIZE,
3034 DMA_BIDIRECTIONAL);
3035 if (dma_mapping_error(dev, scrq->msg_token)) {
3036 dev_warn(dev, "Couldn't map crq queue messages page\n");
3037 goto map_failed;
3038 }
3039
3040 rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
3041 4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
3042
3043 if (rc == H_RESOURCE)
3044 rc = ibmvnic_reset_crq(adapter);
3045
3046 if (rc == H_CLOSED) {
3047 dev_warn(dev, "Partner adapter not ready, waiting.\n");
3048 } else if (rc) {
3049 dev_warn(dev, "Error %d registering sub-crq\n", rc);
3050 goto reg_failed;
3051 }
3052
Thomas Falcon032c5e82015-12-21 11:26:06 -06003053 scrq->adapter = adapter;
3054 scrq->size = 4 * PAGE_SIZE / sizeof(*scrq->msgs);
Thomas Falconf019fb62020-11-18 19:12:17 -06003055 scrq->ind_buf.index = 0;
3056
3057 scrq->ind_buf.indir_arr =
3058 dma_alloc_coherent(dev,
3059 IBMVNIC_IND_ARR_SZ,
3060 &scrq->ind_buf.indir_dma,
3061 GFP_KERNEL);
3062
3063 if (!scrq->ind_buf.indir_arr)
3064 goto indir_failed;
3065
Thomas Falcon032c5e82015-12-21 11:26:06 -06003066 spin_lock_init(&scrq->lock);
3067
3068 netdev_dbg(adapter->netdev,
3069 "sub-crq initialized, num %lx, hw_irq=%lx, irq=%x\n",
3070 scrq->crq_num, scrq->hw_irq, scrq->irq);
3071
3072 return scrq;
3073
Thomas Falconf019fb62020-11-18 19:12:17 -06003074indir_failed:
3075 do {
3076 rc = plpar_hcall_norets(H_FREE_SUB_CRQ,
3077 adapter->vdev->unit_address,
3078 scrq->crq_num);
3079 } while (rc == H_BUSY || rc == H_IS_LONG_BUSY(rc));
Thomas Falcon032c5e82015-12-21 11:26:06 -06003080reg_failed:
3081 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
3082 DMA_BIDIRECTIONAL);
3083map_failed:
3084 free_pages((unsigned long)scrq->msgs, 2);
3085zero_page_failed:
3086 kfree(scrq);
3087
3088 return NULL;
3089}
3090
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06003091static void release_sub_crqs(struct ibmvnic_adapter *adapter, bool do_h_free)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003092{
3093 int i;
3094
3095 if (adapter->tx_scrq) {
Nathan Fontenot82e3be32018-02-21 21:33:56 -06003096 for (i = 0; i < adapter->num_active_tx_scrqs; i++) {
Nathan Fontenotb5108882017-03-30 02:49:18 -04003097 if (!adapter->tx_scrq[i])
3098 continue;
3099
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05003100 netdev_dbg(adapter->netdev, "Releasing tx_scrq[%d]\n",
3101 i);
Nathan Fontenotb5108882017-03-30 02:49:18 -04003102 if (adapter->tx_scrq[i]->irq) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06003103 free_irq(adapter->tx_scrq[i]->irq,
3104 adapter->tx_scrq[i]);
Thomas Falcon88eb98a2016-07-06 15:35:16 -05003105 irq_dispose_mapping(adapter->tx_scrq[i]->irq);
Nathan Fontenotb5108882017-03-30 02:49:18 -04003106 adapter->tx_scrq[i]->irq = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003107 }
Nathan Fontenotb5108882017-03-30 02:49:18 -04003108
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06003109 release_sub_crq_queue(adapter, adapter->tx_scrq[i],
3110 do_h_free);
Nathan Fontenotb5108882017-03-30 02:49:18 -04003111 }
3112
Nathan Fontenot9501df32017-03-15 23:38:07 -04003113 kfree(adapter->tx_scrq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003114 adapter->tx_scrq = NULL;
Nathan Fontenot82e3be32018-02-21 21:33:56 -06003115 adapter->num_active_tx_scrqs = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003116 }
3117
3118 if (adapter->rx_scrq) {
Nathan Fontenot82e3be32018-02-21 21:33:56 -06003119 for (i = 0; i < adapter->num_active_rx_scrqs; i++) {
Nathan Fontenotb5108882017-03-30 02:49:18 -04003120 if (!adapter->rx_scrq[i])
3121 continue;
3122
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05003123 netdev_dbg(adapter->netdev, "Releasing rx_scrq[%d]\n",
3124 i);
Nathan Fontenotb5108882017-03-30 02:49:18 -04003125 if (adapter->rx_scrq[i]->irq) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06003126 free_irq(adapter->rx_scrq[i]->irq,
3127 adapter->rx_scrq[i]);
Thomas Falcon88eb98a2016-07-06 15:35:16 -05003128 irq_dispose_mapping(adapter->rx_scrq[i]->irq);
Nathan Fontenotb5108882017-03-30 02:49:18 -04003129 adapter->rx_scrq[i]->irq = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003130 }
Nathan Fontenotb5108882017-03-30 02:49:18 -04003131
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06003132 release_sub_crq_queue(adapter, adapter->rx_scrq[i],
3133 do_h_free);
Nathan Fontenotb5108882017-03-30 02:49:18 -04003134 }
3135
Nathan Fontenot9501df32017-03-15 23:38:07 -04003136 kfree(adapter->rx_scrq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003137 adapter->rx_scrq = NULL;
Nathan Fontenot82e3be32018-02-21 21:33:56 -06003138 adapter->num_active_rx_scrqs = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003139 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06003140}
3141
3142static int disable_scrq_irq(struct ibmvnic_adapter *adapter,
3143 struct ibmvnic_sub_crq_queue *scrq)
3144{
3145 struct device *dev = &adapter->vdev->dev;
3146 unsigned long rc;
3147
3148 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
3149 H_DISABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
3150 if (rc)
3151 dev_err(dev, "Couldn't disable scrq irq 0x%lx. rc=%ld\n",
3152 scrq->hw_irq, rc);
3153 return rc;
3154}
3155
3156static int enable_scrq_irq(struct ibmvnic_adapter *adapter,
3157 struct ibmvnic_sub_crq_queue *scrq)
3158{
3159 struct device *dev = &adapter->vdev->dev;
3160 unsigned long rc;
3161
3162 if (scrq->hw_irq > 0x100000000ULL) {
3163 dev_err(dev, "bad hw_irq = %lx\n", scrq->hw_irq);
3164 return 1;
3165 }
3166
Juliet Kim7ed5b312019-09-20 16:11:23 -04003167 if (test_bit(0, &adapter->resetting) &&
Nathan Fontenot73f9d362018-05-22 11:21:10 -05003168 adapter->reset_reason == VNIC_RESET_MOBILITY) {
Juliet Kim284f87d2019-11-20 10:50:03 -05003169 u64 val = (0xff000000) | scrq->hw_irq;
Nathan Fontenot73f9d362018-05-22 11:21:10 -05003170
Juliet Kim284f87d2019-11-20 10:50:03 -05003171 rc = plpar_hcall_norets(H_EOI, val);
Juliet Kim2df5c602019-11-20 10:50:04 -05003172 /* H_EOI would fail with rc = H_FUNCTION when running
3173 * in XIVE mode which is expected, but not an error.
3174 */
Lijun Pan91dc5d22021-02-11 00:43:22 -06003175 if (rc && rc != H_FUNCTION)
Juliet Kim284f87d2019-11-20 10:50:03 -05003176 dev_err(dev, "H_EOI FAILED irq 0x%llx. rc=%ld\n",
3177 val, rc);
Nathan Fontenot73f9d362018-05-22 11:21:10 -05003178 }
Thomas Falconf23e0642018-04-15 18:53:36 -05003179
Thomas Falcon032c5e82015-12-21 11:26:06 -06003180 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
3181 H_ENABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
3182 if (rc)
3183 dev_err(dev, "Couldn't enable scrq irq 0x%lx. rc=%ld\n",
3184 scrq->hw_irq, rc);
3185 return rc;
3186}
3187
3188static int ibmvnic_complete_tx(struct ibmvnic_adapter *adapter,
3189 struct ibmvnic_sub_crq_queue *scrq)
3190{
3191 struct device *dev = &adapter->vdev->dev;
Thomas Falcon06b3e352018-03-16 20:00:28 -05003192 struct ibmvnic_tx_pool *tx_pool;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003193 struct ibmvnic_tx_buff *txbuff;
Thomas Falcon0d973382020-11-18 19:12:19 -06003194 struct netdev_queue *txq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003195 union sub_crq *next;
3196 int index;
Thomas Falconc62aa372020-11-18 19:12:20 -06003197 int i;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003198
3199restart_loop:
3200 while (pending_scrq(adapter, scrq)) {
3201 unsigned int pool = scrq->pool_index;
Thomas Falconffc385b2018-02-18 10:08:41 -06003202 int num_entries = 0;
Thomas Falcon0d973382020-11-18 19:12:19 -06003203 int total_bytes = 0;
3204 int num_packets = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003205
3206 next = ibmvnic_next_scrq(adapter, scrq);
3207 for (i = 0; i < next->tx_comp.num_comps; i++) {
Thomas Falconba246c12020-12-01 09:52:11 -06003208 if (next->tx_comp.rcs[i])
Thomas Falcon032c5e82015-12-21 11:26:06 -06003209 dev_err(dev, "tx error %x\n",
3210 next->tx_comp.rcs[i]);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003211 index = be32_to_cpu(next->tx_comp.correlators[i]);
Thomas Falcon06b3e352018-03-16 20:00:28 -05003212 if (index & IBMVNIC_TSO_POOL_MASK) {
3213 tx_pool = &adapter->tso_pool[pool];
3214 index &= ~IBMVNIC_TSO_POOL_MASK;
3215 } else {
3216 tx_pool = &adapter->tx_pool[pool];
3217 }
3218
3219 txbuff = &tx_pool->tx_buff[index];
Thomas Falcon0d973382020-11-18 19:12:19 -06003220 num_packets++;
Thomas Falconffc385b2018-02-18 10:08:41 -06003221 num_entries += txbuff->num_entries;
Thomas Falcon0d973382020-11-18 19:12:19 -06003222 if (txbuff->skb) {
3223 total_bytes += txbuff->skb->len;
3224 dev_consume_skb_irq(txbuff->skb);
3225 txbuff->skb = NULL;
3226 } else {
3227 netdev_warn(adapter->netdev,
3228 "TX completion received with NULL socket buffer\n");
3229 }
Thomas Falcon06b3e352018-03-16 20:00:28 -05003230 tx_pool->free_map[tx_pool->producer_index] = index;
3231 tx_pool->producer_index =
3232 (tx_pool->producer_index + 1) %
3233 tx_pool->num_buffers;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003234 }
3235 /* remove tx_comp scrq*/
3236 next->tx_comp.first = 0;
Nathan Fontenot7c3e7de2017-05-03 14:05:25 -04003237
Thomas Falcon0d973382020-11-18 19:12:19 -06003238 txq = netdev_get_tx_queue(adapter->netdev, scrq->pool_index);
3239 netdev_tx_completed_queue(txq, num_packets, total_bytes);
3240
Thomas Falconffc385b2018-02-18 10:08:41 -06003241 if (atomic_sub_return(num_entries, &scrq->used) <=
Nathan Fontenot7c3e7de2017-05-03 14:05:25 -04003242 (adapter->req_tx_entries_per_subcrq / 2) &&
3243 __netif_subqueue_stopped(adapter->netdev,
3244 scrq->pool_index)) {
3245 netif_wake_subqueue(adapter->netdev, scrq->pool_index);
Thomas Falcon0aecb132018-02-26 18:10:58 -06003246 netdev_dbg(adapter->netdev, "Started queue %d\n",
3247 scrq->pool_index);
Nathan Fontenot7c3e7de2017-05-03 14:05:25 -04003248 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06003249 }
3250
3251 enable_scrq_irq(adapter, scrq);
3252
3253 if (pending_scrq(adapter, scrq)) {
3254 disable_scrq_irq(adapter, scrq);
3255 goto restart_loop;
3256 }
3257
3258 return 0;
3259}
3260
3261static irqreturn_t ibmvnic_interrupt_tx(int irq, void *instance)
3262{
3263 struct ibmvnic_sub_crq_queue *scrq = instance;
3264 struct ibmvnic_adapter *adapter = scrq->adapter;
3265
3266 disable_scrq_irq(adapter, scrq);
3267 ibmvnic_complete_tx(adapter, scrq);
3268
3269 return IRQ_HANDLED;
3270}
3271
3272static irqreturn_t ibmvnic_interrupt_rx(int irq, void *instance)
3273{
3274 struct ibmvnic_sub_crq_queue *scrq = instance;
3275 struct ibmvnic_adapter *adapter = scrq->adapter;
3276
Nathan Fontenot09fb35e2018-01-10 10:40:09 -06003277 /* When booting a kdump kernel we can hit pending interrupts
3278 * prior to completing driver initialization.
3279 */
3280 if (unlikely(adapter->state != VNIC_OPEN))
3281 return IRQ_NONE;
3282
John Allen3d52b592017-08-02 16:44:14 -05003283 adapter->rx_stats_buffers[scrq->scrq_num].interrupts++;
3284
Thomas Falcon032c5e82015-12-21 11:26:06 -06003285 if (napi_schedule_prep(&adapter->napi[scrq->scrq_num])) {
3286 disable_scrq_irq(adapter, scrq);
3287 __napi_schedule(&adapter->napi[scrq->scrq_num]);
3288 }
3289
3290 return IRQ_HANDLED;
3291}
3292
Thomas Falconea22d512016-07-06 15:35:17 -05003293static int init_sub_crq_irqs(struct ibmvnic_adapter *adapter)
3294{
3295 struct device *dev = &adapter->vdev->dev;
3296 struct ibmvnic_sub_crq_queue *scrq;
3297 int i = 0, j = 0;
3298 int rc = 0;
3299
3300 for (i = 0; i < adapter->req_tx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05003301 netdev_dbg(adapter->netdev, "Initializing tx_scrq[%d] irq\n",
3302 i);
Thomas Falconea22d512016-07-06 15:35:17 -05003303 scrq = adapter->tx_scrq[i];
3304 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
3305
Michael Ellerman99c17902016-09-10 19:59:05 +10003306 if (!scrq->irq) {
Thomas Falconea22d512016-07-06 15:35:17 -05003307 rc = -EINVAL;
3308 dev_err(dev, "Error mapping irq\n");
3309 goto req_tx_irq_failed;
3310 }
3311
Murilo Fossa Vicentinie56e2512019-04-25 11:02:33 -03003312 snprintf(scrq->name, sizeof(scrq->name), "ibmvnic-%x-tx%d",
3313 adapter->vdev->unit_address, i);
Thomas Falconea22d512016-07-06 15:35:17 -05003314 rc = request_irq(scrq->irq, ibmvnic_interrupt_tx,
Murilo Fossa Vicentinie56e2512019-04-25 11:02:33 -03003315 0, scrq->name, scrq);
Thomas Falconea22d512016-07-06 15:35:17 -05003316
3317 if (rc) {
3318 dev_err(dev, "Couldn't register tx irq 0x%x. rc=%d\n",
3319 scrq->irq, rc);
3320 irq_dispose_mapping(scrq->irq);
Nathan Fontenotaf9090c2018-02-20 11:04:18 -06003321 goto req_tx_irq_failed;
Thomas Falconea22d512016-07-06 15:35:17 -05003322 }
3323 }
3324
3325 for (i = 0; i < adapter->req_rx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05003326 netdev_dbg(adapter->netdev, "Initializing rx_scrq[%d] irq\n",
3327 i);
Thomas Falconea22d512016-07-06 15:35:17 -05003328 scrq = adapter->rx_scrq[i];
3329 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
Michael Ellerman99c17902016-09-10 19:59:05 +10003330 if (!scrq->irq) {
Thomas Falconea22d512016-07-06 15:35:17 -05003331 rc = -EINVAL;
3332 dev_err(dev, "Error mapping irq\n");
3333 goto req_rx_irq_failed;
3334 }
Murilo Fossa Vicentinie56e2512019-04-25 11:02:33 -03003335 snprintf(scrq->name, sizeof(scrq->name), "ibmvnic-%x-rx%d",
3336 adapter->vdev->unit_address, i);
Thomas Falconea22d512016-07-06 15:35:17 -05003337 rc = request_irq(scrq->irq, ibmvnic_interrupt_rx,
Murilo Fossa Vicentinie56e2512019-04-25 11:02:33 -03003338 0, scrq->name, scrq);
Thomas Falconea22d512016-07-06 15:35:17 -05003339 if (rc) {
3340 dev_err(dev, "Couldn't register rx irq 0x%x. rc=%d\n",
3341 scrq->irq, rc);
3342 irq_dispose_mapping(scrq->irq);
3343 goto req_rx_irq_failed;
3344 }
3345 }
3346 return rc;
3347
3348req_rx_irq_failed:
Thomas Falcon8bf371e2016-10-27 12:28:52 -05003349 for (j = 0; j < i; j++) {
Thomas Falconea22d512016-07-06 15:35:17 -05003350 free_irq(adapter->rx_scrq[j]->irq, adapter->rx_scrq[j]);
3351 irq_dispose_mapping(adapter->rx_scrq[j]->irq);
Thomas Falcon8bf371e2016-10-27 12:28:52 -05003352 }
Thomas Falconea22d512016-07-06 15:35:17 -05003353 i = adapter->req_tx_queues;
3354req_tx_irq_failed:
Thomas Falcon8bf371e2016-10-27 12:28:52 -05003355 for (j = 0; j < i; j++) {
Thomas Falconea22d512016-07-06 15:35:17 -05003356 free_irq(adapter->tx_scrq[j]->irq, adapter->tx_scrq[j]);
Thomas Falcon27a21452020-07-29 16:36:32 -05003357 irq_dispose_mapping(adapter->tx_scrq[j]->irq);
Thomas Falcon8bf371e2016-10-27 12:28:52 -05003358 }
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06003359 release_sub_crqs(adapter, 1);
Thomas Falconea22d512016-07-06 15:35:17 -05003360 return rc;
3361}
3362
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003363static int init_sub_crqs(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003364{
3365 struct device *dev = &adapter->vdev->dev;
3366 struct ibmvnic_sub_crq_queue **allqueues;
3367 int registered_queues = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003368 int total_queues;
3369 int more = 0;
Thomas Falconea22d512016-07-06 15:35:17 -05003370 int i;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003371
Thomas Falcon032c5e82015-12-21 11:26:06 -06003372 total_queues = adapter->req_tx_queues + adapter->req_rx_queues;
3373
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04003374 allqueues = kcalloc(total_queues, sizeof(*allqueues), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003375 if (!allqueues)
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003376 return -1;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003377
3378 for (i = 0; i < total_queues; i++) {
3379 allqueues[i] = init_sub_crq_queue(adapter);
3380 if (!allqueues[i]) {
3381 dev_warn(dev, "Couldn't allocate all sub-crqs\n");
3382 break;
3383 }
3384 registered_queues++;
3385 }
3386
3387 /* Make sure we were able to register the minimum number of queues */
3388 if (registered_queues <
3389 adapter->min_tx_queues + adapter->min_rx_queues) {
3390 dev_err(dev, "Fatal: Couldn't init min number of sub-crqs\n");
3391 goto tx_failed;
3392 }
3393
3394 /* Distribute the failed allocated queues*/
3395 for (i = 0; i < total_queues - registered_queues + more ; i++) {
3396 netdev_dbg(adapter->netdev, "Reducing number of queues\n");
3397 switch (i % 3) {
3398 case 0:
3399 if (adapter->req_rx_queues > adapter->min_rx_queues)
3400 adapter->req_rx_queues--;
3401 else
3402 more++;
3403 break;
3404 case 1:
3405 if (adapter->req_tx_queues > adapter->min_tx_queues)
3406 adapter->req_tx_queues--;
3407 else
3408 more++;
3409 break;
3410 }
3411 }
3412
3413 adapter->tx_scrq = kcalloc(adapter->req_tx_queues,
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04003414 sizeof(*adapter->tx_scrq), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003415 if (!adapter->tx_scrq)
3416 goto tx_failed;
3417
3418 for (i = 0; i < adapter->req_tx_queues; i++) {
3419 adapter->tx_scrq[i] = allqueues[i];
3420 adapter->tx_scrq[i]->pool_index = i;
Nathan Fontenot82e3be32018-02-21 21:33:56 -06003421 adapter->num_active_tx_scrqs++;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003422 }
3423
3424 adapter->rx_scrq = kcalloc(adapter->req_rx_queues,
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04003425 sizeof(*adapter->rx_scrq), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003426 if (!adapter->rx_scrq)
3427 goto rx_failed;
3428
3429 for (i = 0; i < adapter->req_rx_queues; i++) {
3430 adapter->rx_scrq[i] = allqueues[i + adapter->req_tx_queues];
3431 adapter->rx_scrq[i]->scrq_num = i;
Nathan Fontenot82e3be32018-02-21 21:33:56 -06003432 adapter->num_active_rx_scrqs++;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003433 }
3434
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003435 kfree(allqueues);
3436 return 0;
3437
3438rx_failed:
3439 kfree(adapter->tx_scrq);
3440 adapter->tx_scrq = NULL;
3441tx_failed:
3442 for (i = 0; i < registered_queues; i++)
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06003443 release_sub_crq_queue(adapter, allqueues[i], 1);
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003444 kfree(allqueues);
3445 return -1;
3446}
3447
Lijun Pan09081b92020-09-27 20:13:27 -05003448static void send_request_cap(struct ibmvnic_adapter *adapter, int retry)
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003449{
3450 struct device *dev = &adapter->vdev->dev;
3451 union ibmvnic_crq crq;
John Allenc26eba02017-10-26 16:23:25 -05003452 int max_entries;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003453
3454 if (!retry) {
3455 /* Sub-CRQ entries are 32 byte long */
3456 int entries_page = 4 * PAGE_SIZE / (sizeof(u64) * 4);
3457
3458 if (adapter->min_tx_entries_per_subcrq > entries_page ||
3459 adapter->min_rx_add_entries_per_subcrq > entries_page) {
3460 dev_err(dev, "Fatal, invalid entries per sub-crq\n");
3461 return;
3462 }
3463
John Allenc26eba02017-10-26 16:23:25 -05003464 if (adapter->desired.mtu)
3465 adapter->req_mtu = adapter->desired.mtu;
3466 else
3467 adapter->req_mtu = adapter->netdev->mtu + ETH_HLEN;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003468
John Allenc26eba02017-10-26 16:23:25 -05003469 if (!adapter->desired.tx_entries)
3470 adapter->desired.tx_entries =
3471 adapter->max_tx_entries_per_subcrq;
3472 if (!adapter->desired.rx_entries)
3473 adapter->desired.rx_entries =
3474 adapter->max_rx_add_entries_per_subcrq;
3475
3476 max_entries = IBMVNIC_MAX_LTB_SIZE /
3477 (adapter->req_mtu + IBMVNIC_BUFFER_HLEN);
3478
3479 if ((adapter->req_mtu + IBMVNIC_BUFFER_HLEN) *
3480 adapter->desired.tx_entries > IBMVNIC_MAX_LTB_SIZE) {
3481 adapter->desired.tx_entries = max_entries;
3482 }
3483
3484 if ((adapter->req_mtu + IBMVNIC_BUFFER_HLEN) *
3485 adapter->desired.rx_entries > IBMVNIC_MAX_LTB_SIZE) {
3486 adapter->desired.rx_entries = max_entries;
3487 }
3488
3489 if (adapter->desired.tx_entries)
3490 adapter->req_tx_entries_per_subcrq =
3491 adapter->desired.tx_entries;
3492 else
3493 adapter->req_tx_entries_per_subcrq =
3494 adapter->max_tx_entries_per_subcrq;
3495
3496 if (adapter->desired.rx_entries)
3497 adapter->req_rx_add_entries_per_subcrq =
3498 adapter->desired.rx_entries;
3499 else
3500 adapter->req_rx_add_entries_per_subcrq =
3501 adapter->max_rx_add_entries_per_subcrq;
3502
3503 if (adapter->desired.tx_queues)
3504 adapter->req_tx_queues =
3505 adapter->desired.tx_queues;
3506 else
3507 adapter->req_tx_queues =
3508 adapter->opt_tx_comp_sub_queues;
3509
3510 if (adapter->desired.rx_queues)
3511 adapter->req_rx_queues =
3512 adapter->desired.rx_queues;
3513 else
3514 adapter->req_rx_queues =
3515 adapter->opt_rx_comp_queues;
3516
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003517 adapter->req_rx_add_queues = adapter->max_rx_add_queues;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003518 }
3519
Thomas Falcon032c5e82015-12-21 11:26:06 -06003520 memset(&crq, 0, sizeof(crq));
3521 crq.request_capability.first = IBMVNIC_CRQ_CMD;
3522 crq.request_capability.cmd = REQUEST_CAPABILITY;
3523
3524 crq.request_capability.capability = cpu_to_be16(REQ_TX_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06003525 crq.request_capability.number = cpu_to_be64(adapter->req_tx_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06003526 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003527 ibmvnic_send_crq(adapter, &crq);
3528
3529 crq.request_capability.capability = cpu_to_be16(REQ_RX_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06003530 crq.request_capability.number = cpu_to_be64(adapter->req_rx_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06003531 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003532 ibmvnic_send_crq(adapter, &crq);
3533
3534 crq.request_capability.capability = cpu_to_be16(REQ_RX_ADD_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06003535 crq.request_capability.number = cpu_to_be64(adapter->req_rx_add_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06003536 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003537 ibmvnic_send_crq(adapter, &crq);
3538
3539 crq.request_capability.capability =
3540 cpu_to_be16(REQ_TX_ENTRIES_PER_SUBCRQ);
3541 crq.request_capability.number =
Thomas Falconde89e852016-03-01 10:20:09 -06003542 cpu_to_be64(adapter->req_tx_entries_per_subcrq);
Thomas Falcon901e0402017-02-15 12:17:59 -06003543 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003544 ibmvnic_send_crq(adapter, &crq);
3545
3546 crq.request_capability.capability =
3547 cpu_to_be16(REQ_RX_ADD_ENTRIES_PER_SUBCRQ);
3548 crq.request_capability.number =
Thomas Falconde89e852016-03-01 10:20:09 -06003549 cpu_to_be64(adapter->req_rx_add_entries_per_subcrq);
Thomas Falcon901e0402017-02-15 12:17:59 -06003550 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003551 ibmvnic_send_crq(adapter, &crq);
3552
3553 crq.request_capability.capability = cpu_to_be16(REQ_MTU);
Thomas Falconde89e852016-03-01 10:20:09 -06003554 crq.request_capability.number = cpu_to_be64(adapter->req_mtu);
Thomas Falcon901e0402017-02-15 12:17:59 -06003555 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003556 ibmvnic_send_crq(adapter, &crq);
3557
3558 if (adapter->netdev->flags & IFF_PROMISC) {
3559 if (adapter->promisc_supported) {
3560 crq.request_capability.capability =
3561 cpu_to_be16(PROMISC_REQUESTED);
Thomas Falconde89e852016-03-01 10:20:09 -06003562 crq.request_capability.number = cpu_to_be64(1);
Thomas Falcon901e0402017-02-15 12:17:59 -06003563 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003564 ibmvnic_send_crq(adapter, &crq);
3565 }
3566 } else {
3567 crq.request_capability.capability =
3568 cpu_to_be16(PROMISC_REQUESTED);
Thomas Falconde89e852016-03-01 10:20:09 -06003569 crq.request_capability.number = cpu_to_be64(0);
Thomas Falcon901e0402017-02-15 12:17:59 -06003570 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003571 ibmvnic_send_crq(adapter, &crq);
3572 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06003573}
3574
3575static int pending_scrq(struct ibmvnic_adapter *adapter,
3576 struct ibmvnic_sub_crq_queue *scrq)
3577{
3578 union sub_crq *entry = &scrq->msgs[scrq->cur];
Lijun Pan665ab1e2021-01-29 19:19:04 -06003579 int rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003580
Lijun Pan665ab1e2021-01-29 19:19:04 -06003581 rc = !!(entry->generic.first & IBMVNIC_CRQ_CMD_RSP);
3582
3583 /* Ensure that the SCRQ valid flag is loaded prior to loading the
3584 * contents of the SCRQ descriptor
3585 */
3586 dma_rmb();
3587
3588 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003589}
3590
3591static union sub_crq *ibmvnic_next_scrq(struct ibmvnic_adapter *adapter,
3592 struct ibmvnic_sub_crq_queue *scrq)
3593{
3594 union sub_crq *entry;
3595 unsigned long flags;
3596
3597 spin_lock_irqsave(&scrq->lock, flags);
3598 entry = &scrq->msgs[scrq->cur];
3599 if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP) {
3600 if (++scrq->cur == scrq->size)
3601 scrq->cur = 0;
3602 } else {
3603 entry = NULL;
3604 }
3605 spin_unlock_irqrestore(&scrq->lock, flags);
3606
Lijun Pan665ab1e2021-01-29 19:19:04 -06003607 /* Ensure that the SCRQ valid flag is loaded prior to loading the
3608 * contents of the SCRQ descriptor
Thomas Falconb71ec952020-12-01 09:52:10 -06003609 */
3610 dma_rmb();
3611
Thomas Falcon032c5e82015-12-21 11:26:06 -06003612 return entry;
3613}
3614
3615static union ibmvnic_crq *ibmvnic_next_crq(struct ibmvnic_adapter *adapter)
3616{
3617 struct ibmvnic_crq_queue *queue = &adapter->crq;
3618 union ibmvnic_crq *crq;
3619
3620 crq = &queue->msgs[queue->cur];
3621 if (crq->generic.first & IBMVNIC_CRQ_CMD_RSP) {
3622 if (++queue->cur == queue->size)
3623 queue->cur = 0;
3624 } else {
3625 crq = NULL;
3626 }
3627
3628 return crq;
3629}
3630
Thomas Falcon2d14d372018-07-13 12:03:32 -05003631static void print_subcrq_error(struct device *dev, int rc, const char *func)
3632{
3633 switch (rc) {
3634 case H_PARAMETER:
3635 dev_warn_ratelimited(dev,
3636 "%s failed: Send request is malformed or adapter failover pending. (rc=%d)\n",
3637 func, rc);
3638 break;
3639 case H_CLOSED:
3640 dev_warn_ratelimited(dev,
3641 "%s failed: Backing queue closed. Adapter is down or failover pending. (rc=%d)\n",
3642 func, rc);
3643 break;
3644 default:
3645 dev_err_ratelimited(dev, "%s failed: (rc=%d)\n", func, rc);
3646 break;
3647 }
3648}
3649
Thomas Falconad7775d2016-04-01 17:20:34 -05003650static int send_subcrq_indirect(struct ibmvnic_adapter *adapter,
3651 u64 remote_handle, u64 ioba, u64 num_entries)
3652{
3653 unsigned int ua = adapter->vdev->unit_address;
3654 struct device *dev = &adapter->vdev->dev;
3655 int rc;
3656
3657 /* Make sure the hypervisor sees the complete request */
Lijun Pan1a421562021-02-12 20:36:46 -06003658 dma_wmb();
Thomas Falconad7775d2016-04-01 17:20:34 -05003659 rc = plpar_hcall_norets(H_SEND_SUB_CRQ_INDIRECT, ua,
3660 cpu_to_be64(remote_handle),
3661 ioba, num_entries);
3662
Thomas Falcon2d14d372018-07-13 12:03:32 -05003663 if (rc)
3664 print_subcrq_error(dev, rc, __func__);
Thomas Falconad7775d2016-04-01 17:20:34 -05003665
3666 return rc;
3667}
3668
Thomas Falcon032c5e82015-12-21 11:26:06 -06003669static int ibmvnic_send_crq(struct ibmvnic_adapter *adapter,
3670 union ibmvnic_crq *crq)
3671{
3672 unsigned int ua = adapter->vdev->unit_address;
3673 struct device *dev = &adapter->vdev->dev;
3674 u64 *u64_crq = (u64 *)crq;
3675 int rc;
3676
3677 netdev_dbg(adapter->netdev, "Sending CRQ: %016lx %016lx\n",
Lijun Pan429aa362021-02-11 00:43:18 -06003678 (unsigned long)cpu_to_be64(u64_crq[0]),
3679 (unsigned long)cpu_to_be64(u64_crq[1]));
Thomas Falcon032c5e82015-12-21 11:26:06 -06003680
Thomas Falcon51536982018-05-23 13:37:56 -05003681 if (!adapter->crq.active &&
3682 crq->generic.first != IBMVNIC_CRQ_INIT_CMD) {
3683 dev_warn(dev, "Invalid request detected while CRQ is inactive, possible device state change during reset\n");
3684 return -EINVAL;
3685 }
3686
Thomas Falcon032c5e82015-12-21 11:26:06 -06003687 /* Make sure the hypervisor sees the complete request */
Lijun Pan1a421562021-02-12 20:36:46 -06003688 dma_wmb();
Thomas Falcon032c5e82015-12-21 11:26:06 -06003689
3690 rc = plpar_hcall_norets(H_SEND_CRQ, ua,
3691 cpu_to_be64(u64_crq[0]),
3692 cpu_to_be64(u64_crq[1]));
3693
3694 if (rc) {
Nathan Fontenotec95dff2018-02-07 13:00:24 -06003695 if (rc == H_CLOSED) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06003696 dev_warn(dev, "CRQ Queue closed\n");
Lijun Panfa68bfa2020-08-19 17:52:24 -05003697 /* do not reset, report the fail, wait for passive init from server */
Nathan Fontenotec95dff2018-02-07 13:00:24 -06003698 }
3699
Thomas Falcon032c5e82015-12-21 11:26:06 -06003700 dev_warn(dev, "Send error (rc=%d)\n", rc);
3701 }
3702
3703 return rc;
3704}
3705
3706static int ibmvnic_send_crq_init(struct ibmvnic_adapter *adapter)
3707{
Thomas Falcon36a782f2020-08-31 11:59:57 -05003708 struct device *dev = &adapter->vdev->dev;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003709 union ibmvnic_crq crq;
Thomas Falcon36a782f2020-08-31 11:59:57 -05003710 int retries = 100;
3711 int rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003712
3713 memset(&crq, 0, sizeof(crq));
3714 crq.generic.first = IBMVNIC_CRQ_INIT_CMD;
3715 crq.generic.cmd = IBMVNIC_CRQ_INIT;
3716 netdev_dbg(adapter->netdev, "Sending CRQ init\n");
3717
Thomas Falcon36a782f2020-08-31 11:59:57 -05003718 do {
3719 rc = ibmvnic_send_crq(adapter, &crq);
3720 if (rc != H_CLOSED)
3721 break;
3722 retries--;
3723 msleep(50);
3724
3725 } while (retries > 0);
3726
3727 if (rc) {
3728 dev_err(dev, "Failed to send init request, rc = %d\n", rc);
3729 return rc;
3730 }
3731
3732 return 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003733}
3734
Thomas Falcon032c5e82015-12-21 11:26:06 -06003735static int send_version_xchg(struct ibmvnic_adapter *adapter)
3736{
3737 union ibmvnic_crq crq;
3738
3739 memset(&crq, 0, sizeof(crq));
3740 crq.version_exchange.first = IBMVNIC_CRQ_CMD;
3741 crq.version_exchange.cmd = VERSION_EXCHANGE;
3742 crq.version_exchange.version = cpu_to_be16(ibmvnic_version);
3743
3744 return ibmvnic_send_crq(adapter, &crq);
3745}
3746
Nathan Fontenot37798d02017-11-08 11:23:56 -06003747struct vnic_login_client_data {
3748 u8 type;
3749 __be16 len;
Kees Cook08ea5562018-04-10 15:26:43 -07003750 char name[];
Nathan Fontenot37798d02017-11-08 11:23:56 -06003751} __packed;
3752
3753static int vnic_client_data_len(struct ibmvnic_adapter *adapter)
3754{
3755 int len;
3756
3757 /* Calculate the amount of buffer space needed for the
3758 * vnic client data in the login buffer. There are four entries,
3759 * OS name, LPAR name, device name, and a null last entry.
3760 */
3761 len = 4 * sizeof(struct vnic_login_client_data);
3762 len += 6; /* "Linux" plus NULL */
3763 len += strlen(utsname()->nodename) + 1;
3764 len += strlen(adapter->netdev->name) + 1;
3765
3766 return len;
3767}
3768
3769static void vnic_add_client_data(struct ibmvnic_adapter *adapter,
3770 struct vnic_login_client_data *vlcd)
3771{
3772 const char *os_name = "Linux";
3773 int len;
3774
3775 /* Type 1 - LPAR OS */
3776 vlcd->type = 1;
3777 len = strlen(os_name) + 1;
3778 vlcd->len = cpu_to_be16(len);
Kees Cook08ea5562018-04-10 15:26:43 -07003779 strncpy(vlcd->name, os_name, len);
3780 vlcd = (struct vnic_login_client_data *)(vlcd->name + len);
Nathan Fontenot37798d02017-11-08 11:23:56 -06003781
3782 /* Type 2 - LPAR name */
3783 vlcd->type = 2;
3784 len = strlen(utsname()->nodename) + 1;
3785 vlcd->len = cpu_to_be16(len);
Kees Cook08ea5562018-04-10 15:26:43 -07003786 strncpy(vlcd->name, utsname()->nodename, len);
3787 vlcd = (struct vnic_login_client_data *)(vlcd->name + len);
Nathan Fontenot37798d02017-11-08 11:23:56 -06003788
3789 /* Type 3 - device name */
3790 vlcd->type = 3;
3791 len = strlen(adapter->netdev->name) + 1;
3792 vlcd->len = cpu_to_be16(len);
Kees Cook08ea5562018-04-10 15:26:43 -07003793 strncpy(vlcd->name, adapter->netdev->name, len);
Nathan Fontenot37798d02017-11-08 11:23:56 -06003794}
3795
Thomas Falcon20a8ab72018-02-26 18:10:59 -06003796static int send_login(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003797{
3798 struct ibmvnic_login_rsp_buffer *login_rsp_buffer;
3799 struct ibmvnic_login_buffer *login_buffer;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003800 struct device *dev = &adapter->vdev->dev;
Dany Maddenc98d9cc2020-11-25 18:04:30 -06003801 struct vnic_login_client_data *vlcd;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003802 dma_addr_t rsp_buffer_token;
3803 dma_addr_t buffer_token;
3804 size_t rsp_buffer_size;
3805 union ibmvnic_crq crq;
Dany Maddenc98d9cc2020-11-25 18:04:30 -06003806 int client_data_len;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003807 size_t buffer_size;
3808 __be64 *tx_list_p;
3809 __be64 *rx_list_p;
Dany Maddenc98d9cc2020-11-25 18:04:30 -06003810 int rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003811 int i;
3812
Thomas Falcon20a8ab72018-02-26 18:10:59 -06003813 if (!adapter->tx_scrq || !adapter->rx_scrq) {
3814 netdev_err(adapter->netdev,
3815 "RX or TX queues are not allocated, device login failed\n");
3816 return -1;
3817 }
3818
Lijun Pana0c8be52020-12-19 15:39:19 -06003819 release_login_buffer(adapter);
Thomas Falcon34f0f4e2018-02-13 18:23:40 -06003820 release_login_rsp_buffer(adapter);
Lijun Pana0c8be52020-12-19 15:39:19 -06003821
Nathan Fontenot37798d02017-11-08 11:23:56 -06003822 client_data_len = vnic_client_data_len(adapter);
3823
Thomas Falcon032c5e82015-12-21 11:26:06 -06003824 buffer_size =
3825 sizeof(struct ibmvnic_login_buffer) +
Nathan Fontenot37798d02017-11-08 11:23:56 -06003826 sizeof(u64) * (adapter->req_tx_queues + adapter->req_rx_queues) +
3827 client_data_len;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003828
Nathan Fontenot37798d02017-11-08 11:23:56 -06003829 login_buffer = kzalloc(buffer_size, GFP_ATOMIC);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003830 if (!login_buffer)
3831 goto buf_alloc_failed;
3832
3833 buffer_token = dma_map_single(dev, login_buffer, buffer_size,
3834 DMA_TO_DEVICE);
3835 if (dma_mapping_error(dev, buffer_token)) {
3836 dev_err(dev, "Couldn't map login buffer\n");
3837 goto buf_map_failed;
3838 }
3839
John Allen498cd8e2016-04-06 11:49:55 -05003840 rsp_buffer_size = sizeof(struct ibmvnic_login_rsp_buffer) +
3841 sizeof(u64) * adapter->req_tx_queues +
3842 sizeof(u64) * adapter->req_rx_queues +
3843 sizeof(u64) * adapter->req_rx_queues +
3844 sizeof(u8) * IBMVNIC_TX_DESC_VERSIONS;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003845
3846 login_rsp_buffer = kmalloc(rsp_buffer_size, GFP_ATOMIC);
3847 if (!login_rsp_buffer)
3848 goto buf_rsp_alloc_failed;
3849
3850 rsp_buffer_token = dma_map_single(dev, login_rsp_buffer,
3851 rsp_buffer_size, DMA_FROM_DEVICE);
3852 if (dma_mapping_error(dev, rsp_buffer_token)) {
3853 dev_err(dev, "Couldn't map login rsp buffer\n");
3854 goto buf_rsp_map_failed;
3855 }
Nathan Fontenot661a2622017-04-19 13:44:58 -04003856
Thomas Falcon032c5e82015-12-21 11:26:06 -06003857 adapter->login_buf = login_buffer;
3858 adapter->login_buf_token = buffer_token;
3859 adapter->login_buf_sz = buffer_size;
3860 adapter->login_rsp_buf = login_rsp_buffer;
3861 adapter->login_rsp_buf_token = rsp_buffer_token;
3862 adapter->login_rsp_buf_sz = rsp_buffer_size;
3863
3864 login_buffer->len = cpu_to_be32(buffer_size);
3865 login_buffer->version = cpu_to_be32(INITIAL_VERSION_LB);
3866 login_buffer->num_txcomp_subcrqs = cpu_to_be32(adapter->req_tx_queues);
3867 login_buffer->off_txcomp_subcrqs =
3868 cpu_to_be32(sizeof(struct ibmvnic_login_buffer));
3869 login_buffer->num_rxcomp_subcrqs = cpu_to_be32(adapter->req_rx_queues);
3870 login_buffer->off_rxcomp_subcrqs =
3871 cpu_to_be32(sizeof(struct ibmvnic_login_buffer) +
3872 sizeof(u64) * adapter->req_tx_queues);
3873 login_buffer->login_rsp_ioba = cpu_to_be32(rsp_buffer_token);
3874 login_buffer->login_rsp_len = cpu_to_be32(rsp_buffer_size);
3875
3876 tx_list_p = (__be64 *)((char *)login_buffer +
3877 sizeof(struct ibmvnic_login_buffer));
3878 rx_list_p = (__be64 *)((char *)login_buffer +
3879 sizeof(struct ibmvnic_login_buffer) +
3880 sizeof(u64) * adapter->req_tx_queues);
3881
3882 for (i = 0; i < adapter->req_tx_queues; i++) {
3883 if (adapter->tx_scrq[i]) {
Lijun Pan914789a2021-02-11 00:43:21 -06003884 tx_list_p[i] =
3885 cpu_to_be64(adapter->tx_scrq[i]->crq_num);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003886 }
3887 }
3888
3889 for (i = 0; i < adapter->req_rx_queues; i++) {
3890 if (adapter->rx_scrq[i]) {
Lijun Pan914789a2021-02-11 00:43:21 -06003891 rx_list_p[i] =
3892 cpu_to_be64(adapter->rx_scrq[i]->crq_num);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003893 }
3894 }
3895
Nathan Fontenot37798d02017-11-08 11:23:56 -06003896 /* Insert vNIC login client data */
3897 vlcd = (struct vnic_login_client_data *)
3898 ((char *)rx_list_p + (sizeof(u64) * adapter->req_rx_queues));
3899 login_buffer->client_data_offset =
3900 cpu_to_be32((char *)vlcd - (char *)login_buffer);
3901 login_buffer->client_data_len = cpu_to_be32(client_data_len);
3902
3903 vnic_add_client_data(adapter, vlcd);
3904
Thomas Falcon032c5e82015-12-21 11:26:06 -06003905 netdev_dbg(adapter->netdev, "Login Buffer:\n");
3906 for (i = 0; i < (adapter->login_buf_sz - 1) / 8 + 1; i++) {
3907 netdev_dbg(adapter->netdev, "%016lx\n",
Lijun Pan429aa362021-02-11 00:43:18 -06003908 ((unsigned long *)(adapter->login_buf))[i]);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003909 }
3910
3911 memset(&crq, 0, sizeof(crq));
3912 crq.login.first = IBMVNIC_CRQ_CMD;
3913 crq.login.cmd = LOGIN;
3914 crq.login.ioba = cpu_to_be32(buffer_token);
3915 crq.login.len = cpu_to_be32(buffer_size);
Sukadev Bhattiprolu76cdc5c2020-11-25 18:04:29 -06003916
3917 adapter->login_pending = true;
Dany Maddenc98d9cc2020-11-25 18:04:30 -06003918 rc = ibmvnic_send_crq(adapter, &crq);
3919 if (rc) {
3920 adapter->login_pending = false;
3921 netdev_err(adapter->netdev, "Failed to send login, rc=%d\n", rc);
3922 goto buf_rsp_map_failed;
3923 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06003924
Thomas Falcon20a8ab72018-02-26 18:10:59 -06003925 return 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003926
Thomas Falcon032c5e82015-12-21 11:26:06 -06003927buf_rsp_map_failed:
3928 kfree(login_rsp_buffer);
Dany Maddenc98d9cc2020-11-25 18:04:30 -06003929 adapter->login_rsp_buf = NULL;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003930buf_rsp_alloc_failed:
3931 dma_unmap_single(dev, buffer_token, buffer_size, DMA_TO_DEVICE);
3932buf_map_failed:
3933 kfree(login_buffer);
Dany Maddenc98d9cc2020-11-25 18:04:30 -06003934 adapter->login_buf = NULL;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003935buf_alloc_failed:
Thomas Falcon20a8ab72018-02-26 18:10:59 -06003936 return -1;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003937}
3938
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05003939static int send_request_map(struct ibmvnic_adapter *adapter, dma_addr_t addr,
3940 u32 len, u8 map_id)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003941{
3942 union ibmvnic_crq crq;
3943
3944 memset(&crq, 0, sizeof(crq));
3945 crq.request_map.first = IBMVNIC_CRQ_CMD;
3946 crq.request_map.cmd = REQUEST_MAP;
3947 crq.request_map.map_id = map_id;
3948 crq.request_map.ioba = cpu_to_be32(addr);
3949 crq.request_map.len = cpu_to_be32(len);
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05003950 return ibmvnic_send_crq(adapter, &crq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003951}
3952
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05003953static int send_request_unmap(struct ibmvnic_adapter *adapter, u8 map_id)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003954{
3955 union ibmvnic_crq crq;
3956
3957 memset(&crq, 0, sizeof(crq));
3958 crq.request_unmap.first = IBMVNIC_CRQ_CMD;
3959 crq.request_unmap.cmd = REQUEST_UNMAP;
3960 crq.request_unmap.map_id = map_id;
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05003961 return ibmvnic_send_crq(adapter, &crq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003962}
3963
Lijun Pan69980d02020-09-27 20:13:28 -05003964static void send_query_map(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003965{
3966 union ibmvnic_crq crq;
3967
3968 memset(&crq, 0, sizeof(crq));
3969 crq.query_map.first = IBMVNIC_CRQ_CMD;
3970 crq.query_map.cmd = QUERY_MAP;
3971 ibmvnic_send_crq(adapter, &crq);
3972}
3973
3974/* Send a series of CRQs requesting various capabilities of the VNIC server */
Lijun Pan491099a2020-09-27 20:13:26 -05003975static void send_query_cap(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003976{
3977 union ibmvnic_crq crq;
3978
Thomas Falcon901e0402017-02-15 12:17:59 -06003979 atomic_set(&adapter->running_cap_crqs, 0);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003980 memset(&crq, 0, sizeof(crq));
3981 crq.query_capability.first = IBMVNIC_CRQ_CMD;
3982 crq.query_capability.cmd = QUERY_CAPABILITY;
3983
3984 crq.query_capability.capability = cpu_to_be16(MIN_TX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003985 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003986 ibmvnic_send_crq(adapter, &crq);
3987
3988 crq.query_capability.capability = cpu_to_be16(MIN_RX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003989 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003990 ibmvnic_send_crq(adapter, &crq);
3991
3992 crq.query_capability.capability = cpu_to_be16(MIN_RX_ADD_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003993 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003994 ibmvnic_send_crq(adapter, &crq);
3995
3996 crq.query_capability.capability = cpu_to_be16(MAX_TX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003997 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003998 ibmvnic_send_crq(adapter, &crq);
3999
4000 crq.query_capability.capability = cpu_to_be16(MAX_RX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06004001 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004002 ibmvnic_send_crq(adapter, &crq);
4003
4004 crq.query_capability.capability = cpu_to_be16(MAX_RX_ADD_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06004005 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004006 ibmvnic_send_crq(adapter, &crq);
4007
4008 crq.query_capability.capability =
4009 cpu_to_be16(MIN_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06004010 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004011 ibmvnic_send_crq(adapter, &crq);
4012
4013 crq.query_capability.capability =
4014 cpu_to_be16(MIN_RX_ADD_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06004015 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004016 ibmvnic_send_crq(adapter, &crq);
4017
4018 crq.query_capability.capability =
4019 cpu_to_be16(MAX_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06004020 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004021 ibmvnic_send_crq(adapter, &crq);
4022
4023 crq.query_capability.capability =
4024 cpu_to_be16(MAX_RX_ADD_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06004025 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004026 ibmvnic_send_crq(adapter, &crq);
4027
4028 crq.query_capability.capability = cpu_to_be16(TCP_IP_OFFLOAD);
Thomas Falcon901e0402017-02-15 12:17:59 -06004029 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004030 ibmvnic_send_crq(adapter, &crq);
4031
4032 crq.query_capability.capability = cpu_to_be16(PROMISC_SUPPORTED);
Thomas Falcon901e0402017-02-15 12:17:59 -06004033 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004034 ibmvnic_send_crq(adapter, &crq);
4035
4036 crq.query_capability.capability = cpu_to_be16(MIN_MTU);
Thomas Falcon901e0402017-02-15 12:17:59 -06004037 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004038 ibmvnic_send_crq(adapter, &crq);
4039
4040 crq.query_capability.capability = cpu_to_be16(MAX_MTU);
Thomas Falcon901e0402017-02-15 12:17:59 -06004041 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004042 ibmvnic_send_crq(adapter, &crq);
4043
4044 crq.query_capability.capability = cpu_to_be16(MAX_MULTICAST_FILTERS);
Thomas Falcon901e0402017-02-15 12:17:59 -06004045 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004046 ibmvnic_send_crq(adapter, &crq);
4047
4048 crq.query_capability.capability = cpu_to_be16(VLAN_HEADER_INSERTION);
Thomas Falcon901e0402017-02-15 12:17:59 -06004049 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004050 ibmvnic_send_crq(adapter, &crq);
4051
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04004052 crq.query_capability.capability = cpu_to_be16(RX_VLAN_HEADER_INSERTION);
4053 atomic_inc(&adapter->running_cap_crqs);
4054 ibmvnic_send_crq(adapter, &crq);
4055
Thomas Falcon032c5e82015-12-21 11:26:06 -06004056 crq.query_capability.capability = cpu_to_be16(MAX_TX_SG_ENTRIES);
Thomas Falcon901e0402017-02-15 12:17:59 -06004057 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004058 ibmvnic_send_crq(adapter, &crq);
4059
4060 crq.query_capability.capability = cpu_to_be16(RX_SG_SUPPORTED);
Thomas Falcon901e0402017-02-15 12:17:59 -06004061 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004062 ibmvnic_send_crq(adapter, &crq);
4063
4064 crq.query_capability.capability = cpu_to_be16(OPT_TX_COMP_SUB_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06004065 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004066 ibmvnic_send_crq(adapter, &crq);
4067
4068 crq.query_capability.capability = cpu_to_be16(OPT_RX_COMP_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06004069 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004070 ibmvnic_send_crq(adapter, &crq);
4071
4072 crq.query_capability.capability =
4073 cpu_to_be16(OPT_RX_BUFADD_Q_PER_RX_COMP_Q);
Thomas Falcon901e0402017-02-15 12:17:59 -06004074 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004075 ibmvnic_send_crq(adapter, &crq);
4076
4077 crq.query_capability.capability =
4078 cpu_to_be16(OPT_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06004079 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004080 ibmvnic_send_crq(adapter, &crq);
4081
4082 crq.query_capability.capability =
4083 cpu_to_be16(OPT_RXBA_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06004084 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004085 ibmvnic_send_crq(adapter, &crq);
4086
4087 crq.query_capability.capability = cpu_to_be16(TX_RX_DESC_REQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06004088 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004089 ibmvnic_send_crq(adapter, &crq);
4090}
4091
Lijun Pan16e811f2020-09-27 20:13:29 -05004092static void send_query_ip_offload(struct ibmvnic_adapter *adapter)
4093{
4094 int buf_sz = sizeof(struct ibmvnic_query_ip_offload_buffer);
4095 struct device *dev = &adapter->vdev->dev;
4096 union ibmvnic_crq crq;
4097
4098 adapter->ip_offload_tok =
4099 dma_map_single(dev,
4100 &adapter->ip_offload_buf,
4101 buf_sz,
4102 DMA_FROM_DEVICE);
4103
4104 if (dma_mapping_error(dev, adapter->ip_offload_tok)) {
4105 if (!firmware_has_feature(FW_FEATURE_CMO))
4106 dev_err(dev, "Couldn't map offload buffer\n");
4107 return;
4108 }
4109
4110 memset(&crq, 0, sizeof(crq));
4111 crq.query_ip_offload.first = IBMVNIC_CRQ_CMD;
4112 crq.query_ip_offload.cmd = QUERY_IP_OFFLOAD;
4113 crq.query_ip_offload.len = cpu_to_be32(buf_sz);
4114 crq.query_ip_offload.ioba =
4115 cpu_to_be32(adapter->ip_offload_tok);
4116
4117 ibmvnic_send_crq(adapter, &crq);
4118}
4119
Lijun Pan46899bd2020-09-27 20:13:30 -05004120static void send_control_ip_offload(struct ibmvnic_adapter *adapter)
4121{
4122 struct ibmvnic_control_ip_offload_buffer *ctrl_buf = &adapter->ip_offload_ctrl;
4123 struct ibmvnic_query_ip_offload_buffer *buf = &adapter->ip_offload_buf;
4124 struct device *dev = &adapter->vdev->dev;
4125 netdev_features_t old_hw_features = 0;
4126 union ibmvnic_crq crq;
4127
4128 adapter->ip_offload_ctrl_tok =
4129 dma_map_single(dev,
4130 ctrl_buf,
4131 sizeof(adapter->ip_offload_ctrl),
4132 DMA_TO_DEVICE);
4133
4134 if (dma_mapping_error(dev, adapter->ip_offload_ctrl_tok)) {
4135 dev_err(dev, "Couldn't map ip offload control buffer\n");
4136 return;
4137 }
4138
4139 ctrl_buf->len = cpu_to_be32(sizeof(adapter->ip_offload_ctrl));
4140 ctrl_buf->version = cpu_to_be32(INITIAL_VERSION_IOB);
4141 ctrl_buf->ipv4_chksum = buf->ipv4_chksum;
4142 ctrl_buf->ipv6_chksum = buf->ipv6_chksum;
4143 ctrl_buf->tcp_ipv4_chksum = buf->tcp_ipv4_chksum;
4144 ctrl_buf->udp_ipv4_chksum = buf->udp_ipv4_chksum;
4145 ctrl_buf->tcp_ipv6_chksum = buf->tcp_ipv6_chksum;
4146 ctrl_buf->udp_ipv6_chksum = buf->udp_ipv6_chksum;
4147 ctrl_buf->large_tx_ipv4 = buf->large_tx_ipv4;
4148 ctrl_buf->large_tx_ipv6 = buf->large_tx_ipv6;
4149
4150 /* large_rx disabled for now, additional features needed */
4151 ctrl_buf->large_rx_ipv4 = 0;
4152 ctrl_buf->large_rx_ipv6 = 0;
4153
4154 if (adapter->state != VNIC_PROBING) {
4155 old_hw_features = adapter->netdev->hw_features;
4156 adapter->netdev->hw_features = 0;
4157 }
4158
4159 adapter->netdev->hw_features = NETIF_F_SG | NETIF_F_GSO | NETIF_F_GRO;
4160
4161 if (buf->tcp_ipv4_chksum || buf->udp_ipv4_chksum)
4162 adapter->netdev->hw_features |= NETIF_F_IP_CSUM;
4163
4164 if (buf->tcp_ipv6_chksum || buf->udp_ipv6_chksum)
4165 adapter->netdev->hw_features |= NETIF_F_IPV6_CSUM;
4166
4167 if ((adapter->netdev->features &
4168 (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)))
4169 adapter->netdev->hw_features |= NETIF_F_RXCSUM;
4170
4171 if (buf->large_tx_ipv4)
4172 adapter->netdev->hw_features |= NETIF_F_TSO;
4173 if (buf->large_tx_ipv6)
4174 adapter->netdev->hw_features |= NETIF_F_TSO6;
4175
4176 if (adapter->state == VNIC_PROBING) {
4177 adapter->netdev->features |= adapter->netdev->hw_features;
4178 } else if (old_hw_features != adapter->netdev->hw_features) {
4179 netdev_features_t tmp = 0;
4180
4181 /* disable features no longer supported */
4182 adapter->netdev->features &= adapter->netdev->hw_features;
4183 /* turn on features now supported if previously enabled */
4184 tmp = (old_hw_features ^ adapter->netdev->hw_features) &
4185 adapter->netdev->hw_features;
4186 adapter->netdev->features |=
4187 tmp & adapter->netdev->wanted_features;
4188 }
4189
4190 memset(&crq, 0, sizeof(crq));
4191 crq.control_ip_offload.first = IBMVNIC_CRQ_CMD;
4192 crq.control_ip_offload.cmd = CONTROL_IP_OFFLOAD;
4193 crq.control_ip_offload.len =
4194 cpu_to_be32(sizeof(adapter->ip_offload_ctrl));
4195 crq.control_ip_offload.ioba = cpu_to_be32(adapter->ip_offload_ctrl_tok);
4196 ibmvnic_send_crq(adapter, &crq);
4197}
4198
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02004199static void handle_vpd_size_rsp(union ibmvnic_crq *crq,
4200 struct ibmvnic_adapter *adapter)
4201{
4202 struct device *dev = &adapter->vdev->dev;
4203
4204 if (crq->get_vpd_size_rsp.rc.code) {
4205 dev_err(dev, "Error retrieving VPD size, rc=%x\n",
4206 crq->get_vpd_size_rsp.rc.code);
4207 complete(&adapter->fw_done);
4208 return;
4209 }
4210
4211 adapter->vpd->len = be64_to_cpu(crq->get_vpd_size_rsp.len);
4212 complete(&adapter->fw_done);
4213}
4214
4215static void handle_vpd_rsp(union ibmvnic_crq *crq,
4216 struct ibmvnic_adapter *adapter)
4217{
4218 struct device *dev = &adapter->vdev->dev;
Desnes Augusto Nunes do Rosario21a25452018-02-05 14:33:55 -02004219 unsigned char *substr = NULL;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02004220 u8 fw_level_len = 0;
4221
4222 memset(adapter->fw_version, 0, 32);
4223
4224 dma_unmap_single(dev, adapter->vpd->dma_addr, adapter->vpd->len,
4225 DMA_FROM_DEVICE);
4226
4227 if (crq->get_vpd_rsp.rc.code) {
4228 dev_err(dev, "Error retrieving VPD from device, rc=%x\n",
4229 crq->get_vpd_rsp.rc.code);
4230 goto complete;
4231 }
4232
4233 /* get the position of the firmware version info
4234 * located after the ASCII 'RM' substring in the buffer
4235 */
4236 substr = strnstr(adapter->vpd->buff, "RM", adapter->vpd->len);
4237 if (!substr) {
Desnes Augusto Nunes do Rosarioa1073112018-02-01 16:04:30 -02004238 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 -02004239 goto complete;
4240 }
4241
4242 /* get length of firmware level ASCII substring */
4243 if ((substr + 2) < (adapter->vpd->buff + adapter->vpd->len)) {
4244 fw_level_len = *(substr + 2);
4245 } else {
4246 dev_info(dev, "Length of FW substr extrapolated VDP buff\n");
4247 goto complete;
4248 }
4249
4250 /* copy firmware version string from vpd into adapter */
4251 if ((substr + 3 + fw_level_len) <
4252 (adapter->vpd->buff + adapter->vpd->len)) {
Desnes Augusto Nunes do Rosario21a25452018-02-05 14:33:55 -02004253 strncpy((char *)adapter->fw_version, substr + 3, fw_level_len);
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02004254 } else {
4255 dev_info(dev, "FW substr extrapolated VPD buff\n");
4256 }
4257
4258complete:
Desnes Augusto Nunes do Rosario21a25452018-02-05 14:33:55 -02004259 if (adapter->fw_version[0] == '\0')
4260 strncpy((char *)adapter->fw_version, "N/A", 3 * sizeof(char));
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02004261 complete(&adapter->fw_done);
4262}
4263
Thomas Falcon032c5e82015-12-21 11:26:06 -06004264static void handle_query_ip_offload_rsp(struct ibmvnic_adapter *adapter)
4265{
4266 struct device *dev = &adapter->vdev->dev;
4267 struct ibmvnic_query_ip_offload_buffer *buf = &adapter->ip_offload_buf;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004268 int i;
4269
4270 dma_unmap_single(dev, adapter->ip_offload_tok,
4271 sizeof(adapter->ip_offload_buf), DMA_FROM_DEVICE);
4272
4273 netdev_dbg(adapter->netdev, "Query IP Offload Buffer:\n");
4274 for (i = 0; i < (sizeof(adapter->ip_offload_buf) - 1) / 8 + 1; i++)
4275 netdev_dbg(adapter->netdev, "%016lx\n",
Lijun Pan429aa362021-02-11 00:43:18 -06004276 ((unsigned long *)(buf))[i]);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004277
4278 netdev_dbg(adapter->netdev, "ipv4_chksum = %d\n", buf->ipv4_chksum);
4279 netdev_dbg(adapter->netdev, "ipv6_chksum = %d\n", buf->ipv6_chksum);
4280 netdev_dbg(adapter->netdev, "tcp_ipv4_chksum = %d\n",
4281 buf->tcp_ipv4_chksum);
4282 netdev_dbg(adapter->netdev, "tcp_ipv6_chksum = %d\n",
4283 buf->tcp_ipv6_chksum);
4284 netdev_dbg(adapter->netdev, "udp_ipv4_chksum = %d\n",
4285 buf->udp_ipv4_chksum);
4286 netdev_dbg(adapter->netdev, "udp_ipv6_chksum = %d\n",
4287 buf->udp_ipv6_chksum);
4288 netdev_dbg(adapter->netdev, "large_tx_ipv4 = %d\n",
4289 buf->large_tx_ipv4);
4290 netdev_dbg(adapter->netdev, "large_tx_ipv6 = %d\n",
4291 buf->large_tx_ipv6);
4292 netdev_dbg(adapter->netdev, "large_rx_ipv4 = %d\n",
4293 buf->large_rx_ipv4);
4294 netdev_dbg(adapter->netdev, "large_rx_ipv6 = %d\n",
4295 buf->large_rx_ipv6);
4296 netdev_dbg(adapter->netdev, "max_ipv4_hdr_sz = %d\n",
4297 buf->max_ipv4_header_size);
4298 netdev_dbg(adapter->netdev, "max_ipv6_hdr_sz = %d\n",
4299 buf->max_ipv6_header_size);
4300 netdev_dbg(adapter->netdev, "max_tcp_hdr_size = %d\n",
4301 buf->max_tcp_header_size);
4302 netdev_dbg(adapter->netdev, "max_udp_hdr_size = %d\n",
4303 buf->max_udp_header_size);
4304 netdev_dbg(adapter->netdev, "max_large_tx_size = %d\n",
4305 buf->max_large_tx_size);
4306 netdev_dbg(adapter->netdev, "max_large_rx_size = %d\n",
4307 buf->max_large_rx_size);
4308 netdev_dbg(adapter->netdev, "ipv6_ext_hdr = %d\n",
4309 buf->ipv6_extension_header);
4310 netdev_dbg(adapter->netdev, "tcp_pseudosum_req = %d\n",
4311 buf->tcp_pseudosum_req);
4312 netdev_dbg(adapter->netdev, "num_ipv6_ext_hd = %d\n",
4313 buf->num_ipv6_ext_headers);
4314 netdev_dbg(adapter->netdev, "off_ipv6_ext_hd = %d\n",
4315 buf->off_ipv6_ext_headers);
4316
Lijun Pan46899bd2020-09-27 20:13:30 -05004317 send_control_ip_offload(adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004318}
4319
Thomas Falconc9008d32018-08-06 21:39:59 -05004320static const char *ibmvnic_fw_err_cause(u16 cause)
4321{
4322 switch (cause) {
4323 case ADAPTER_PROBLEM:
4324 return "adapter problem";
4325 case BUS_PROBLEM:
4326 return "bus problem";
4327 case FW_PROBLEM:
4328 return "firmware problem";
4329 case DD_PROBLEM:
4330 return "device driver problem";
4331 case EEH_RECOVERY:
4332 return "EEH recovery";
4333 case FW_UPDATED:
4334 return "firmware updated";
4335 case LOW_MEMORY:
4336 return "low Memory";
4337 default:
4338 return "unknown";
4339 }
4340}
4341
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04004342static void handle_error_indication(union ibmvnic_crq *crq,
4343 struct ibmvnic_adapter *adapter)
4344{
4345 struct device *dev = &adapter->vdev->dev;
Thomas Falconc9008d32018-08-06 21:39:59 -05004346 u16 cause;
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04004347
Thomas Falconc9008d32018-08-06 21:39:59 -05004348 cause = be16_to_cpu(crq->error_indication.error_cause);
4349
4350 dev_warn_ratelimited(dev,
4351 "Firmware reports %serror, cause: %s. Starting recovery...\n",
4352 crq->error_indication.flags
4353 & IBMVNIC_FATAL_ERROR ? "FATAL " : "",
4354 ibmvnic_fw_err_cause(cause));
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04004355
Nathan Fontenoted651a12017-05-03 14:04:38 -04004356 if (crq->error_indication.flags & IBMVNIC_FATAL_ERROR)
4357 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
John Allen8cb31cf2017-05-26 10:30:37 -04004358 else
4359 ibmvnic_reset(adapter, VNIC_RESET_NON_FATAL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004360}
4361
Thomas Falconf8136142018-01-29 13:45:05 -06004362static int handle_change_mac_rsp(union ibmvnic_crq *crq,
4363 struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06004364{
4365 struct net_device *netdev = adapter->netdev;
4366 struct device *dev = &adapter->vdev->dev;
4367 long rc;
4368
4369 rc = crq->change_mac_addr_rsp.rc.code;
4370 if (rc) {
4371 dev_err(dev, "Error %ld in CHANGE_MAC_ADDR_RSP\n", rc);
Thomas Falconf8136142018-01-29 13:45:05 -06004372 goto out;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004373 }
Lijun Pand9b0e592020-10-20 17:39:19 -05004374 /* crq->change_mac_addr.mac_addr is the requested one
4375 * crq->change_mac_addr_rsp.mac_addr is the returned valid one.
4376 */
Thomas Falcon62740e92019-05-09 23:13:43 -05004377 ether_addr_copy(netdev->dev_addr,
4378 &crq->change_mac_addr_rsp.mac_addr[0]);
Lijun Pand9b0e592020-10-20 17:39:19 -05004379 ether_addr_copy(adapter->mac_addr,
4380 &crq->change_mac_addr_rsp.mac_addr[0]);
Thomas Falconf8136142018-01-29 13:45:05 -06004381out:
4382 complete(&adapter->fw_done);
4383 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004384}
4385
4386static void handle_request_cap_rsp(union ibmvnic_crq *crq,
4387 struct ibmvnic_adapter *adapter)
4388{
4389 struct device *dev = &adapter->vdev->dev;
4390 u64 *req_value;
4391 char *name;
4392
Thomas Falcon901e0402017-02-15 12:17:59 -06004393 atomic_dec(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004394 switch (be16_to_cpu(crq->request_capability_rsp.capability)) {
4395 case REQ_TX_QUEUES:
4396 req_value = &adapter->req_tx_queues;
4397 name = "tx";
4398 break;
4399 case REQ_RX_QUEUES:
4400 req_value = &adapter->req_rx_queues;
4401 name = "rx";
4402 break;
4403 case REQ_RX_ADD_QUEUES:
4404 req_value = &adapter->req_rx_add_queues;
4405 name = "rx_add";
4406 break;
4407 case REQ_TX_ENTRIES_PER_SUBCRQ:
4408 req_value = &adapter->req_tx_entries_per_subcrq;
4409 name = "tx_entries_per_subcrq";
4410 break;
4411 case REQ_RX_ADD_ENTRIES_PER_SUBCRQ:
4412 req_value = &adapter->req_rx_add_entries_per_subcrq;
4413 name = "rx_add_entries_per_subcrq";
4414 break;
4415 case REQ_MTU:
4416 req_value = &adapter->req_mtu;
4417 name = "mtu";
4418 break;
4419 case PROMISC_REQUESTED:
4420 req_value = &adapter->promisc;
4421 name = "promisc";
4422 break;
4423 default:
4424 dev_err(dev, "Got invalid cap request rsp %d\n",
4425 crq->request_capability.capability);
4426 return;
4427 }
4428
4429 switch (crq->request_capability_rsp.rc.code) {
4430 case SUCCESS:
4431 break;
4432 case PARTIALSUCCESS:
4433 dev_info(dev, "req=%lld, rsp=%ld in %s queue, retrying.\n",
4434 *req_value,
Lijun Pan914789a2021-02-11 00:43:21 -06004435 (long)be64_to_cpu(crq->request_capability_rsp.number),
4436 name);
John Allene7913802018-01-18 16:27:12 -06004437
4438 if (be16_to_cpu(crq->request_capability_rsp.capability) ==
4439 REQ_MTU) {
4440 pr_err("mtu of %llu is not supported. Reverting.\n",
4441 *req_value);
4442 *req_value = adapter->fallback.mtu;
4443 } else {
4444 *req_value =
4445 be64_to_cpu(crq->request_capability_rsp.number);
4446 }
4447
Lijun Pan09081b92020-09-27 20:13:27 -05004448 send_request_cap(adapter, 1);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004449 return;
4450 default:
4451 dev_err(dev, "Error %d in request cap rsp\n",
4452 crq->request_capability_rsp.rc.code);
4453 return;
4454 }
4455
4456 /* Done receiving requested capabilities, query IP offload support */
Thomas Falcon901e0402017-02-15 12:17:59 -06004457 if (atomic_read(&adapter->running_cap_crqs) == 0) {
Thomas Falcon249168a2017-02-15 12:18:00 -06004458 adapter->wait_capability = false;
Lijun Pan16e811f2020-09-27 20:13:29 -05004459 send_query_ip_offload(adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004460 }
4461}
4462
4463static int handle_login_rsp(union ibmvnic_crq *login_rsp_crq,
4464 struct ibmvnic_adapter *adapter)
4465{
4466 struct device *dev = &adapter->vdev->dev;
John Allenc26eba02017-10-26 16:23:25 -05004467 struct net_device *netdev = adapter->netdev;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004468 struct ibmvnic_login_rsp_buffer *login_rsp = adapter->login_rsp_buf;
4469 struct ibmvnic_login_buffer *login = adapter->login_buf;
Cristobal Fornof3ae59c2020-08-19 13:16:23 -05004470 u64 *tx_handle_array;
4471 u64 *rx_handle_array;
4472 int num_tx_pools;
4473 int num_rx_pools;
Thomas Falcon507ebe62020-08-21 13:39:01 -05004474 u64 *size_array;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004475 int i;
4476
Sukadev Bhattiprolu76cdc5c2020-11-25 18:04:29 -06004477 /* CHECK: Test/set of login_pending does not need to be atomic
4478 * because only ibmvnic_tasklet tests/clears this.
4479 */
4480 if (!adapter->login_pending) {
4481 netdev_warn(netdev, "Ignoring unexpected login response\n");
4482 return 0;
4483 }
4484 adapter->login_pending = false;
4485
Thomas Falcon032c5e82015-12-21 11:26:06 -06004486 dma_unmap_single(dev, adapter->login_buf_token, adapter->login_buf_sz,
Thomas Falcon37e40fa2018-04-06 18:37:02 -05004487 DMA_TO_DEVICE);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004488 dma_unmap_single(dev, adapter->login_rsp_buf_token,
Thomas Falcon37e40fa2018-04-06 18:37:02 -05004489 adapter->login_rsp_buf_sz, DMA_FROM_DEVICE);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004490
John Allen498cd8e2016-04-06 11:49:55 -05004491 /* If the number of queues requested can't be allocated by the
4492 * server, the login response will return with code 1. We will need
4493 * to resend the login buffer with fewer queues requested.
4494 */
4495 if (login_rsp_crq->generic.rc.code) {
Nathan Fontenot64d92aa2018-04-11 10:09:32 -05004496 adapter->init_done_rc = login_rsp_crq->generic.rc.code;
John Allen498cd8e2016-04-06 11:49:55 -05004497 complete(&adapter->init_done);
4498 return 0;
4499 }
4500
John Allenc26eba02017-10-26 16:23:25 -05004501 netdev->mtu = adapter->req_mtu - ETH_HLEN;
4502
Thomas Falcon032c5e82015-12-21 11:26:06 -06004503 netdev_dbg(adapter->netdev, "Login Response Buffer:\n");
4504 for (i = 0; i < (adapter->login_rsp_buf_sz - 1) / 8 + 1; i++) {
4505 netdev_dbg(adapter->netdev, "%016lx\n",
Lijun Pan429aa362021-02-11 00:43:18 -06004506 ((unsigned long *)(adapter->login_rsp_buf))[i]);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004507 }
4508
4509 /* Sanity checks */
4510 if (login->num_txcomp_subcrqs != login_rsp->num_txsubm_subcrqs ||
4511 (be32_to_cpu(login->num_rxcomp_subcrqs) *
4512 adapter->req_rx_add_queues !=
4513 be32_to_cpu(login_rsp->num_rxadd_subcrqs))) {
4514 dev_err(dev, "FATAL: Inconsistent login and login rsp\n");
Dany Madden31d6b402020-11-25 18:04:24 -06004515 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004516 return -EIO;
4517 }
Thomas Falcon507ebe62020-08-21 13:39:01 -05004518 size_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
4519 be32_to_cpu(adapter->login_rsp_buf->off_rxadd_buff_size));
4520 /* variable buffer sizes are not supported, so just read the
4521 * first entry.
4522 */
4523 adapter->cur_rx_buf_sz = be64_to_cpu(size_array[0]);
Cristobal Fornof3ae59c2020-08-19 13:16:23 -05004524
4525 num_tx_pools = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
4526 num_rx_pools = be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
4527
4528 tx_handle_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
4529 be32_to_cpu(adapter->login_rsp_buf->off_txsubm_subcrqs));
4530 rx_handle_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
4531 be32_to_cpu(adapter->login_rsp_buf->off_rxadd_subcrqs));
4532
4533 for (i = 0; i < num_tx_pools; i++)
4534 adapter->tx_scrq[i]->handle = tx_handle_array[i];
4535
4536 for (i = 0; i < num_rx_pools; i++)
4537 adapter->rx_scrq[i]->handle = rx_handle_array[i];
4538
Thomas Falcon507ebe62020-08-21 13:39:01 -05004539 adapter->num_active_tx_scrqs = num_tx_pools;
4540 adapter->num_active_rx_scrqs = num_rx_pools;
Cristobal Fornof3ae59c2020-08-19 13:16:23 -05004541 release_login_rsp_buffer(adapter);
Thomas Falcona2c0f032018-02-21 18:18:30 -06004542 release_login_buffer(adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004543 complete(&adapter->init_done);
4544
Thomas Falcon032c5e82015-12-21 11:26:06 -06004545 return 0;
4546}
4547
Thomas Falcon032c5e82015-12-21 11:26:06 -06004548static void handle_request_unmap_rsp(union ibmvnic_crq *crq,
4549 struct ibmvnic_adapter *adapter)
4550{
4551 struct device *dev = &adapter->vdev->dev;
4552 long rc;
4553
4554 rc = crq->request_unmap_rsp.rc.code;
4555 if (rc)
4556 dev_err(dev, "Error %ld in REQUEST_UNMAP_RSP\n", rc);
4557}
4558
4559static void handle_query_map_rsp(union ibmvnic_crq *crq,
4560 struct ibmvnic_adapter *adapter)
4561{
4562 struct net_device *netdev = adapter->netdev;
4563 struct device *dev = &adapter->vdev->dev;
4564 long rc;
4565
4566 rc = crq->query_map_rsp.rc.code;
4567 if (rc) {
4568 dev_err(dev, "Error %ld in QUERY_MAP_RSP\n", rc);
4569 return;
4570 }
4571 netdev_dbg(netdev, "page_size = %d\ntot_pages = %d\nfree_pages = %d\n",
4572 crq->query_map_rsp.page_size, crq->query_map_rsp.tot_pages,
4573 crq->query_map_rsp.free_pages);
4574}
4575
4576static void handle_query_cap_rsp(union ibmvnic_crq *crq,
4577 struct ibmvnic_adapter *adapter)
4578{
4579 struct net_device *netdev = adapter->netdev;
4580 struct device *dev = &adapter->vdev->dev;
4581 long rc;
4582
Thomas Falcon901e0402017-02-15 12:17:59 -06004583 atomic_dec(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004584 netdev_dbg(netdev, "Outstanding queries: %d\n",
Thomas Falcon901e0402017-02-15 12:17:59 -06004585 atomic_read(&adapter->running_cap_crqs));
Thomas Falcon032c5e82015-12-21 11:26:06 -06004586 rc = crq->query_capability.rc.code;
4587 if (rc) {
4588 dev_err(dev, "Error %ld in QUERY_CAP_RSP\n", rc);
4589 goto out;
4590 }
4591
4592 switch (be16_to_cpu(crq->query_capability.capability)) {
4593 case MIN_TX_QUEUES:
4594 adapter->min_tx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004595 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004596 netdev_dbg(netdev, "min_tx_queues = %lld\n",
4597 adapter->min_tx_queues);
4598 break;
4599 case MIN_RX_QUEUES:
4600 adapter->min_rx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004601 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004602 netdev_dbg(netdev, "min_rx_queues = %lld\n",
4603 adapter->min_rx_queues);
4604 break;
4605 case MIN_RX_ADD_QUEUES:
4606 adapter->min_rx_add_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004607 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004608 netdev_dbg(netdev, "min_rx_add_queues = %lld\n",
4609 adapter->min_rx_add_queues);
4610 break;
4611 case MAX_TX_QUEUES:
4612 adapter->max_tx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004613 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004614 netdev_dbg(netdev, "max_tx_queues = %lld\n",
4615 adapter->max_tx_queues);
4616 break;
4617 case MAX_RX_QUEUES:
4618 adapter->max_rx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004619 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004620 netdev_dbg(netdev, "max_rx_queues = %lld\n",
4621 adapter->max_rx_queues);
4622 break;
4623 case MAX_RX_ADD_QUEUES:
4624 adapter->max_rx_add_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004625 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004626 netdev_dbg(netdev, "max_rx_add_queues = %lld\n",
4627 adapter->max_rx_add_queues);
4628 break;
4629 case MIN_TX_ENTRIES_PER_SUBCRQ:
4630 adapter->min_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06004631 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004632 netdev_dbg(netdev, "min_tx_entries_per_subcrq = %lld\n",
4633 adapter->min_tx_entries_per_subcrq);
4634 break;
4635 case MIN_RX_ADD_ENTRIES_PER_SUBCRQ:
4636 adapter->min_rx_add_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06004637 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004638 netdev_dbg(netdev, "min_rx_add_entrs_per_subcrq = %lld\n",
4639 adapter->min_rx_add_entries_per_subcrq);
4640 break;
4641 case MAX_TX_ENTRIES_PER_SUBCRQ:
4642 adapter->max_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06004643 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004644 netdev_dbg(netdev, "max_tx_entries_per_subcrq = %lld\n",
4645 adapter->max_tx_entries_per_subcrq);
4646 break;
4647 case MAX_RX_ADD_ENTRIES_PER_SUBCRQ:
4648 adapter->max_rx_add_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06004649 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004650 netdev_dbg(netdev, "max_rx_add_entrs_per_subcrq = %lld\n",
4651 adapter->max_rx_add_entries_per_subcrq);
4652 break;
4653 case TCP_IP_OFFLOAD:
4654 adapter->tcp_ip_offload =
Thomas Falconde89e852016-03-01 10:20:09 -06004655 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004656 netdev_dbg(netdev, "tcp_ip_offload = %lld\n",
4657 adapter->tcp_ip_offload);
4658 break;
4659 case PROMISC_SUPPORTED:
4660 adapter->promisc_supported =
Thomas Falconde89e852016-03-01 10:20:09 -06004661 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004662 netdev_dbg(netdev, "promisc_supported = %lld\n",
4663 adapter->promisc_supported);
4664 break;
4665 case MIN_MTU:
Thomas Falconde89e852016-03-01 10:20:09 -06004666 adapter->min_mtu = be64_to_cpu(crq->query_capability.number);
Thomas Falconf39f0d12017-02-14 10:22:59 -06004667 netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004668 netdev_dbg(netdev, "min_mtu = %lld\n", adapter->min_mtu);
4669 break;
4670 case MAX_MTU:
Thomas Falconde89e852016-03-01 10:20:09 -06004671 adapter->max_mtu = be64_to_cpu(crq->query_capability.number);
Thomas Falconf39f0d12017-02-14 10:22:59 -06004672 netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004673 netdev_dbg(netdev, "max_mtu = %lld\n", adapter->max_mtu);
4674 break;
4675 case MAX_MULTICAST_FILTERS:
4676 adapter->max_multicast_filters =
Thomas Falconde89e852016-03-01 10:20:09 -06004677 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004678 netdev_dbg(netdev, "max_multicast_filters = %lld\n",
4679 adapter->max_multicast_filters);
4680 break;
4681 case VLAN_HEADER_INSERTION:
4682 adapter->vlan_header_insertion =
Thomas Falconde89e852016-03-01 10:20:09 -06004683 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004684 if (adapter->vlan_header_insertion)
4685 netdev->features |= NETIF_F_HW_VLAN_STAG_TX;
4686 netdev_dbg(netdev, "vlan_header_insertion = %lld\n",
4687 adapter->vlan_header_insertion);
4688 break;
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04004689 case RX_VLAN_HEADER_INSERTION:
4690 adapter->rx_vlan_header_insertion =
4691 be64_to_cpu(crq->query_capability.number);
4692 netdev_dbg(netdev, "rx_vlan_header_insertion = %lld\n",
4693 adapter->rx_vlan_header_insertion);
4694 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004695 case MAX_TX_SG_ENTRIES:
4696 adapter->max_tx_sg_entries =
Thomas Falconde89e852016-03-01 10:20:09 -06004697 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004698 netdev_dbg(netdev, "max_tx_sg_entries = %lld\n",
4699 adapter->max_tx_sg_entries);
4700 break;
4701 case RX_SG_SUPPORTED:
4702 adapter->rx_sg_supported =
Thomas Falconde89e852016-03-01 10:20:09 -06004703 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004704 netdev_dbg(netdev, "rx_sg_supported = %lld\n",
4705 adapter->rx_sg_supported);
4706 break;
4707 case OPT_TX_COMP_SUB_QUEUES:
4708 adapter->opt_tx_comp_sub_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004709 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004710 netdev_dbg(netdev, "opt_tx_comp_sub_queues = %lld\n",
4711 adapter->opt_tx_comp_sub_queues);
4712 break;
4713 case OPT_RX_COMP_QUEUES:
4714 adapter->opt_rx_comp_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004715 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004716 netdev_dbg(netdev, "opt_rx_comp_queues = %lld\n",
4717 adapter->opt_rx_comp_queues);
4718 break;
4719 case OPT_RX_BUFADD_Q_PER_RX_COMP_Q:
4720 adapter->opt_rx_bufadd_q_per_rx_comp_q =
Thomas Falconde89e852016-03-01 10:20:09 -06004721 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004722 netdev_dbg(netdev, "opt_rx_bufadd_q_per_rx_comp_q = %lld\n",
4723 adapter->opt_rx_bufadd_q_per_rx_comp_q);
4724 break;
4725 case OPT_TX_ENTRIES_PER_SUBCRQ:
4726 adapter->opt_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06004727 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004728 netdev_dbg(netdev, "opt_tx_entries_per_subcrq = %lld\n",
4729 adapter->opt_tx_entries_per_subcrq);
4730 break;
4731 case OPT_RXBA_ENTRIES_PER_SUBCRQ:
4732 adapter->opt_rxba_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06004733 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004734 netdev_dbg(netdev, "opt_rxba_entries_per_subcrq = %lld\n",
4735 adapter->opt_rxba_entries_per_subcrq);
4736 break;
4737 case TX_RX_DESC_REQ:
4738 adapter->tx_rx_desc_req = crq->query_capability.number;
4739 netdev_dbg(netdev, "tx_rx_desc_req = %llx\n",
4740 adapter->tx_rx_desc_req);
4741 break;
4742
4743 default:
4744 netdev_err(netdev, "Got invalid cap rsp %d\n",
4745 crq->query_capability.capability);
4746 }
4747
4748out:
Thomas Falcon249168a2017-02-15 12:18:00 -06004749 if (atomic_read(&adapter->running_cap_crqs) == 0) {
4750 adapter->wait_capability = false;
Lijun Pan09081b92020-09-27 20:13:27 -05004751 send_request_cap(adapter, 0);
Thomas Falcon249168a2017-02-15 12:18:00 -06004752 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06004753}
4754
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03004755static int send_query_phys_parms(struct ibmvnic_adapter *adapter)
4756{
4757 union ibmvnic_crq crq;
4758 int rc;
4759
4760 memset(&crq, 0, sizeof(crq));
4761 crq.query_phys_parms.first = IBMVNIC_CRQ_CMD;
4762 crq.query_phys_parms.cmd = QUERY_PHYS_PARMS;
Thomas Falconff25dcb2019-11-25 17:12:56 -06004763
4764 mutex_lock(&adapter->fw_lock);
4765 adapter->fw_done_rc = 0;
Thomas Falcon070eca92019-11-25 17:12:53 -06004766 reinit_completion(&adapter->fw_done);
Thomas Falconff25dcb2019-11-25 17:12:56 -06004767
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03004768 rc = ibmvnic_send_crq(adapter, &crq);
Thomas Falconff25dcb2019-11-25 17:12:56 -06004769 if (rc) {
4770 mutex_unlock(&adapter->fw_lock);
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03004771 return rc;
Thomas Falconff25dcb2019-11-25 17:12:56 -06004772 }
Thomas Falcon476d96c2019-11-25 17:12:55 -06004773
4774 rc = ibmvnic_wait_for_completion(adapter, &adapter->fw_done, 10000);
Thomas Falconff25dcb2019-11-25 17:12:56 -06004775 if (rc) {
4776 mutex_unlock(&adapter->fw_lock);
Thomas Falcon476d96c2019-11-25 17:12:55 -06004777 return rc;
Thomas Falconff25dcb2019-11-25 17:12:56 -06004778 }
Thomas Falcon476d96c2019-11-25 17:12:55 -06004779
Thomas Falconff25dcb2019-11-25 17:12:56 -06004780 mutex_unlock(&adapter->fw_lock);
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03004781 return adapter->fw_done_rc ? -EIO : 0;
4782}
4783
4784static int handle_query_phys_parms_rsp(union ibmvnic_crq *crq,
4785 struct ibmvnic_adapter *adapter)
4786{
4787 struct net_device *netdev = adapter->netdev;
4788 int rc;
Murilo Fossa Vicentinidd0f9d82019-09-16 11:50:37 -03004789 __be32 rspeed = cpu_to_be32(crq->query_phys_parms_rsp.speed);
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03004790
4791 rc = crq->query_phys_parms_rsp.rc.code;
4792 if (rc) {
4793 netdev_err(netdev, "Error %d in QUERY_PHYS_PARMS\n", rc);
4794 return rc;
4795 }
Murilo Fossa Vicentinidd0f9d82019-09-16 11:50:37 -03004796 switch (rspeed) {
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03004797 case IBMVNIC_10MBPS:
4798 adapter->speed = SPEED_10;
4799 break;
4800 case IBMVNIC_100MBPS:
4801 adapter->speed = SPEED_100;
4802 break;
4803 case IBMVNIC_1GBPS:
4804 adapter->speed = SPEED_1000;
4805 break;
Lijun Panb9cd7952020-09-27 19:06:25 -05004806 case IBMVNIC_10GBPS:
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03004807 adapter->speed = SPEED_10000;
4808 break;
4809 case IBMVNIC_25GBPS:
4810 adapter->speed = SPEED_25000;
4811 break;
4812 case IBMVNIC_40GBPS:
4813 adapter->speed = SPEED_40000;
4814 break;
4815 case IBMVNIC_50GBPS:
4816 adapter->speed = SPEED_50000;
4817 break;
4818 case IBMVNIC_100GBPS:
4819 adapter->speed = SPEED_100000;
4820 break;
Lijun Panb9cd7952020-09-27 19:06:25 -05004821 case IBMVNIC_200GBPS:
4822 adapter->speed = SPEED_200000;
4823 break;
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03004824 default:
Murilo Fossa Vicentinidd0f9d82019-09-16 11:50:37 -03004825 if (netif_carrier_ok(netdev))
4826 netdev_warn(netdev, "Unknown speed 0x%08x\n", rspeed);
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03004827 adapter->speed = SPEED_UNKNOWN;
4828 }
4829 if (crq->query_phys_parms_rsp.flags1 & IBMVNIC_FULL_DUPLEX)
4830 adapter->duplex = DUPLEX_FULL;
4831 else if (crq->query_phys_parms_rsp.flags1 & IBMVNIC_HALF_DUPLEX)
4832 adapter->duplex = DUPLEX_HALF;
4833 else
4834 adapter->duplex = DUPLEX_UNKNOWN;
4835
4836 return rc;
4837}
4838
Thomas Falcon032c5e82015-12-21 11:26:06 -06004839static void ibmvnic_handle_crq(union ibmvnic_crq *crq,
4840 struct ibmvnic_adapter *adapter)
4841{
4842 struct ibmvnic_generic_crq *gen_crq = &crq->generic;
4843 struct net_device *netdev = adapter->netdev;
4844 struct device *dev = &adapter->vdev->dev;
Murilo Fossa Vicentini993a82b2017-04-19 13:44:35 -04004845 u64 *u64_crq = (u64 *)crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004846 long rc;
4847
4848 netdev_dbg(netdev, "Handling CRQ: %016lx %016lx\n",
Lijun Pan429aa362021-02-11 00:43:18 -06004849 (unsigned long)cpu_to_be64(u64_crq[0]),
4850 (unsigned long)cpu_to_be64(u64_crq[1]));
Thomas Falcon032c5e82015-12-21 11:26:06 -06004851 switch (gen_crq->first) {
4852 case IBMVNIC_CRQ_INIT_RSP:
4853 switch (gen_crq->cmd) {
4854 case IBMVNIC_CRQ_INIT:
4855 dev_info(dev, "Partner initialized\n");
John Allen017892c12017-05-26 10:30:19 -04004856 adapter->from_passive_init = true;
Sukadev Bhattiprolu76cdc5c2020-11-25 18:04:29 -06004857 /* Discard any stale login responses from prev reset.
4858 * CHECK: should we clear even on INIT_COMPLETE?
4859 */
4860 adapter->login_pending = false;
4861
Thomas Falcon17c87052018-05-23 13:37:58 -05004862 if (!completion_done(&adapter->init_done)) {
4863 complete(&adapter->init_done);
4864 adapter->init_done_rc = -EIO;
4865 }
Sukadev Bhattiproluef66a1e2021-02-02 21:08:02 -08004866 rc = ibmvnic_reset(adapter, VNIC_RESET_FAILOVER);
4867 if (rc && rc != -EBUSY) {
4868 /* We were unable to schedule the failover
4869 * reset either because the adapter was still
4870 * probing (eg: during kexec) or we could not
4871 * allocate memory. Clear the failover_pending
4872 * flag since no one else will. We ignore
4873 * EBUSY because it means either FAILOVER reset
4874 * is already scheduled or the adapter is
4875 * being removed.
4876 */
4877 netdev_err(netdev,
4878 "Error %ld scheduling failover reset\n",
4879 rc);
4880 adapter->failover_pending = false;
4881 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06004882 break;
4883 case IBMVNIC_CRQ_INIT_COMPLETE:
4884 dev_info(dev, "Partner initialization complete\n");
Thomas Falcon51536982018-05-23 13:37:56 -05004885 adapter->crq.active = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004886 send_version_xchg(adapter);
4887 break;
4888 default:
4889 dev_err(dev, "Unknown crq cmd: %d\n", gen_crq->cmd);
4890 }
4891 return;
4892 case IBMVNIC_CRQ_XPORT_EVENT:
Nathan Fontenoted651a12017-05-03 14:04:38 -04004893 netif_carrier_off(netdev);
Thomas Falcon51536982018-05-23 13:37:56 -05004894 adapter->crq.active = false;
Thomas Falcon2147e3d2019-11-25 17:12:54 -06004895 /* terminate any thread waiting for a response
4896 * from the device
4897 */
4898 if (!completion_done(&adapter->fw_done)) {
4899 adapter->fw_done_rc = -EIO;
4900 complete(&adapter->fw_done);
4901 }
4902 if (!completion_done(&adapter->stats_done))
4903 complete(&adapter->stats_done);
Juliet Kim7ed5b312019-09-20 16:11:23 -04004904 if (test_bit(0, &adapter->resetting))
Thomas Falcon2770a792018-05-23 13:38:02 -05004905 adapter->force_reset_recovery = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004906 if (gen_crq->cmd == IBMVNIC_PARTITION_MIGRATED) {
Nathan Fontenoted651a12017-05-03 14:04:38 -04004907 dev_info(dev, "Migrated, re-enabling adapter\n");
4908 ibmvnic_reset(adapter, VNIC_RESET_MOBILITY);
Thomas Falcondfad09a2016-08-18 11:37:51 -05004909 } else if (gen_crq->cmd == IBMVNIC_DEVICE_FAILOVER) {
4910 dev_info(dev, "Backing device failover detected\n");
Thomas Falcon5a18e1e2018-04-06 18:37:05 -05004911 adapter->failover_pending = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004912 } else {
4913 /* The adapter lost the connection */
4914 dev_err(dev, "Virtual Adapter failed (rc=%d)\n",
4915 gen_crq->cmd);
Nathan Fontenoted651a12017-05-03 14:04:38 -04004916 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004917 }
4918 return;
4919 case IBMVNIC_CRQ_CMD_RSP:
4920 break;
4921 default:
4922 dev_err(dev, "Got an invalid msg type 0x%02x\n",
4923 gen_crq->first);
4924 return;
4925 }
4926
4927 switch (gen_crq->cmd) {
4928 case VERSION_EXCHANGE_RSP:
4929 rc = crq->version_exchange_rsp.rc.code;
4930 if (rc) {
4931 dev_err(dev, "Error %ld in VERSION_EXCHG_RSP\n", rc);
4932 break;
4933 }
Thomas Falcon78468892020-05-28 11:19:17 -05004934 ibmvnic_version =
Thomas Falcon032c5e82015-12-21 11:26:06 -06004935 be16_to_cpu(crq->version_exchange_rsp.version);
Thomas Falcon78468892020-05-28 11:19:17 -05004936 dev_info(dev, "Partner protocol version is %d\n",
4937 ibmvnic_version);
Lijun Pan491099a2020-09-27 20:13:26 -05004938 send_query_cap(adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004939 break;
4940 case QUERY_CAPABILITY_RSP:
4941 handle_query_cap_rsp(crq, adapter);
4942 break;
4943 case QUERY_MAP_RSP:
4944 handle_query_map_rsp(crq, adapter);
4945 break;
4946 case REQUEST_MAP_RSP:
Thomas Falconf3be0cb2017-06-21 14:53:01 -05004947 adapter->fw_done_rc = crq->request_map_rsp.rc.code;
4948 complete(&adapter->fw_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004949 break;
4950 case REQUEST_UNMAP_RSP:
4951 handle_request_unmap_rsp(crq, adapter);
4952 break;
4953 case REQUEST_CAPABILITY_RSP:
4954 handle_request_cap_rsp(crq, adapter);
4955 break;
4956 case LOGIN_RSP:
4957 netdev_dbg(netdev, "Got Login Response\n");
4958 handle_login_rsp(crq, adapter);
4959 break;
4960 case LOGICAL_LINK_STATE_RSP:
Nathan Fontenot53da09e2017-04-21 15:39:04 -04004961 netdev_dbg(netdev,
4962 "Got Logical Link State Response, state: %d rc: %d\n",
4963 crq->logical_link_state_rsp.link_state,
4964 crq->logical_link_state_rsp.rc.code);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004965 adapter->logical_link_state =
4966 crq->logical_link_state_rsp.link_state;
Nathan Fontenot53da09e2017-04-21 15:39:04 -04004967 adapter->init_done_rc = crq->logical_link_state_rsp.rc.code;
4968 complete(&adapter->init_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004969 break;
4970 case LINK_STATE_INDICATION:
4971 netdev_dbg(netdev, "Got Logical Link State Indication\n");
4972 adapter->phys_link_state =
4973 crq->link_state_indication.phys_link_state;
4974 adapter->logical_link_state =
4975 crq->link_state_indication.logical_link_state;
Thomas Falcon0655f992019-05-09 23:13:44 -05004976 if (adapter->phys_link_state && adapter->logical_link_state)
4977 netif_carrier_on(netdev);
4978 else
4979 netif_carrier_off(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004980 break;
4981 case CHANGE_MAC_ADDR_RSP:
4982 netdev_dbg(netdev, "Got MAC address change Response\n");
Thomas Falconf8136142018-01-29 13:45:05 -06004983 adapter->fw_done_rc = handle_change_mac_rsp(crq, adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004984 break;
4985 case ERROR_INDICATION:
4986 netdev_dbg(netdev, "Got Error Indication\n");
4987 handle_error_indication(crq, adapter);
4988 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004989 case REQUEST_STATISTICS_RSP:
4990 netdev_dbg(netdev, "Got Statistics Response\n");
4991 complete(&adapter->stats_done);
4992 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004993 case QUERY_IP_OFFLOAD_RSP:
4994 netdev_dbg(netdev, "Got Query IP offload Response\n");
4995 handle_query_ip_offload_rsp(adapter);
4996 break;
4997 case MULTICAST_CTRL_RSP:
4998 netdev_dbg(netdev, "Got multicast control Response\n");
4999 break;
5000 case CONTROL_IP_OFFLOAD_RSP:
5001 netdev_dbg(netdev, "Got Control IP offload Response\n");
5002 dma_unmap_single(dev, adapter->ip_offload_ctrl_tok,
5003 sizeof(adapter->ip_offload_ctrl),
5004 DMA_TO_DEVICE);
John Allenbd0b6722017-03-17 17:13:40 -05005005 complete(&adapter->init_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005006 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005007 case COLLECT_FW_TRACE_RSP:
5008 netdev_dbg(netdev, "Got Collect firmware trace Response\n");
5009 complete(&adapter->fw_done);
5010 break;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02005011 case GET_VPD_SIZE_RSP:
5012 handle_vpd_size_rsp(crq, adapter);
5013 break;
5014 case GET_VPD_RSP:
5015 handle_vpd_rsp(crq, adapter);
5016 break;
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03005017 case QUERY_PHYS_PARMS_RSP:
5018 adapter->fw_done_rc = handle_query_phys_parms_rsp(crq, adapter);
5019 complete(&adapter->fw_done);
5020 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005021 default:
5022 netdev_err(netdev, "Got an invalid cmd type 0x%02x\n",
5023 gen_crq->cmd);
5024 }
5025}
5026
5027static irqreturn_t ibmvnic_interrupt(int irq, void *instance)
5028{
5029 struct ibmvnic_adapter *adapter = instance;
Thomas Falcon6c267b32017-02-15 12:17:58 -06005030
Thomas Falcon6c267b32017-02-15 12:17:58 -06005031 tasklet_schedule(&adapter->tasklet);
Thomas Falcon6c267b32017-02-15 12:17:58 -06005032 return IRQ_HANDLED;
5033}
5034
Allen Paisaa7c3fe2020-09-14 12:59:29 +05305035static void ibmvnic_tasklet(struct tasklet_struct *t)
Thomas Falcon6c267b32017-02-15 12:17:58 -06005036{
Allen Paisaa7c3fe2020-09-14 12:59:29 +05305037 struct ibmvnic_adapter *adapter = from_tasklet(adapter, t, tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005038 struct ibmvnic_crq_queue *queue = &adapter->crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005039 union ibmvnic_crq *crq;
5040 unsigned long flags;
5041 bool done = false;
5042
5043 spin_lock_irqsave(&queue->lock, flags);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005044 while (!done) {
5045 /* Pull all the valid messages off the CRQ */
5046 while ((crq = ibmvnic_next_crq(adapter)) != NULL) {
Lijun Pane41aec72021-01-27 19:34:42 -06005047 /* This barrier makes sure ibmvnic_next_crq()'s
5048 * crq->generic.first & IBMVNIC_CRQ_CMD_RSP is loaded
5049 * before ibmvnic_handle_crq()'s
5050 * switch(gen_crq->first) and switch(gen_crq->cmd).
5051 */
5052 dma_rmb();
Thomas Falcon032c5e82015-12-21 11:26:06 -06005053 ibmvnic_handle_crq(crq, adapter);
5054 crq->generic.first = 0;
5055 }
Brian Kinged7ecbf2017-04-19 13:44:53 -04005056
5057 /* remain in tasklet until all
5058 * capabilities responses are received
5059 */
5060 if (!adapter->wait_capability)
5061 done = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005062 }
Thomas Falcon249168a2017-02-15 12:18:00 -06005063 /* if capabilities CRQ's were sent in this tasklet, the following
5064 * tasklet must wait until all responses are received
5065 */
5066 if (atomic_read(&adapter->running_cap_crqs) != 0)
5067 adapter->wait_capability = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005068 spin_unlock_irqrestore(&queue->lock, flags);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005069}
5070
5071static int ibmvnic_reenable_crq_queue(struct ibmvnic_adapter *adapter)
5072{
5073 struct vio_dev *vdev = adapter->vdev;
5074 int rc;
5075
5076 do {
5077 rc = plpar_hcall_norets(H_ENABLE_CRQ, vdev->unit_address);
5078 } while (rc == H_IN_PROGRESS || rc == H_BUSY || H_IS_LONG_BUSY(rc));
5079
5080 if (rc)
5081 dev_err(&vdev->dev, "Error enabling adapter (rc=%d)\n", rc);
5082
5083 return rc;
5084}
5085
5086static int ibmvnic_reset_crq(struct ibmvnic_adapter *adapter)
5087{
5088 struct ibmvnic_crq_queue *crq = &adapter->crq;
5089 struct device *dev = &adapter->vdev->dev;
5090 struct vio_dev *vdev = adapter->vdev;
5091 int rc;
5092
5093 /* Close the CRQ */
5094 do {
5095 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
5096 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
5097
5098 /* Clean out the queue */
Lijun Pan0e435be2020-11-23 13:35:46 -06005099 if (!crq->msgs)
5100 return -EINVAL;
5101
Thomas Falcon032c5e82015-12-21 11:26:06 -06005102 memset(crq->msgs, 0, PAGE_SIZE);
5103 crq->cur = 0;
Thomas Falcon51536982018-05-23 13:37:56 -05005104 crq->active = false;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005105
5106 /* And re-open it again */
5107 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
5108 crq->msg_token, PAGE_SIZE);
5109
5110 if (rc == H_CLOSED)
5111 /* Adapter is good, but other end is not ready */
5112 dev_warn(dev, "Partner adapter not ready\n");
5113 else if (rc != 0)
5114 dev_warn(dev, "Couldn't register crq (rc=%d)\n", rc);
5115
5116 return rc;
5117}
5118
Nathan Fontenotf9928872017-03-30 02:48:54 -04005119static void release_crq_queue(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06005120{
5121 struct ibmvnic_crq_queue *crq = &adapter->crq;
5122 struct vio_dev *vdev = adapter->vdev;
5123 long rc;
5124
Nathan Fontenotf9928872017-03-30 02:48:54 -04005125 if (!crq->msgs)
5126 return;
5127
Thomas Falcon032c5e82015-12-21 11:26:06 -06005128 netdev_dbg(adapter->netdev, "Releasing CRQ\n");
5129 free_irq(vdev->irq, adapter);
Thomas Falcon6c267b32017-02-15 12:17:58 -06005130 tasklet_kill(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005131 do {
5132 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
5133 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
5134
5135 dma_unmap_single(&vdev->dev, crq->msg_token, PAGE_SIZE,
5136 DMA_BIDIRECTIONAL);
5137 free_page((unsigned long)crq->msgs);
Nathan Fontenotf9928872017-03-30 02:48:54 -04005138 crq->msgs = NULL;
Thomas Falcon51536982018-05-23 13:37:56 -05005139 crq->active = false;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005140}
5141
Nathan Fontenotf9928872017-03-30 02:48:54 -04005142static int init_crq_queue(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06005143{
5144 struct ibmvnic_crq_queue *crq = &adapter->crq;
5145 struct device *dev = &adapter->vdev->dev;
5146 struct vio_dev *vdev = adapter->vdev;
5147 int rc, retrc = -ENOMEM;
5148
Nathan Fontenotf9928872017-03-30 02:48:54 -04005149 if (crq->msgs)
5150 return 0;
5151
Thomas Falcon032c5e82015-12-21 11:26:06 -06005152 crq->msgs = (union ibmvnic_crq *)get_zeroed_page(GFP_KERNEL);
5153 /* Should we allocate more than one page? */
5154
5155 if (!crq->msgs)
5156 return -ENOMEM;
5157
5158 crq->size = PAGE_SIZE / sizeof(*crq->msgs);
5159 crq->msg_token = dma_map_single(dev, crq->msgs, PAGE_SIZE,
5160 DMA_BIDIRECTIONAL);
5161 if (dma_mapping_error(dev, crq->msg_token))
5162 goto map_failed;
5163
5164 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
5165 crq->msg_token, PAGE_SIZE);
5166
5167 if (rc == H_RESOURCE)
5168 /* maybe kexecing and resource is busy. try a reset */
5169 rc = ibmvnic_reset_crq(adapter);
5170 retrc = rc;
5171
5172 if (rc == H_CLOSED) {
5173 dev_warn(dev, "Partner adapter not ready\n");
5174 } else if (rc) {
5175 dev_warn(dev, "Error %d opening adapter\n", rc);
5176 goto reg_crq_failed;
5177 }
5178
5179 retrc = 0;
5180
Allen Paisaa7c3fe2020-09-14 12:59:29 +05305181 tasklet_setup(&adapter->tasklet, (void *)ibmvnic_tasklet);
Thomas Falcon6c267b32017-02-15 12:17:58 -06005182
Thomas Falcon032c5e82015-12-21 11:26:06 -06005183 netdev_dbg(adapter->netdev, "registering irq 0x%x\n", vdev->irq);
Murilo Fossa Vicentinie56e2512019-04-25 11:02:33 -03005184 snprintf(crq->name, sizeof(crq->name), "ibmvnic-%x",
5185 adapter->vdev->unit_address);
5186 rc = request_irq(vdev->irq, ibmvnic_interrupt, 0, crq->name, adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005187 if (rc) {
5188 dev_err(dev, "Couldn't register irq 0x%x. rc=%d\n",
5189 vdev->irq, rc);
5190 goto req_irq_failed;
5191 }
5192
5193 rc = vio_enable_interrupts(vdev);
5194 if (rc) {
5195 dev_err(dev, "Error %d enabling interrupts\n", rc);
5196 goto req_irq_failed;
5197 }
5198
5199 crq->cur = 0;
5200 spin_lock_init(&crq->lock);
5201
5202 return retrc;
5203
5204req_irq_failed:
Thomas Falcon6c267b32017-02-15 12:17:58 -06005205 tasklet_kill(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005206 do {
5207 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
5208 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
5209reg_crq_failed:
5210 dma_unmap_single(dev, crq->msg_token, PAGE_SIZE, DMA_BIDIRECTIONAL);
5211map_failed:
5212 free_page((unsigned long)crq->msgs);
Nathan Fontenotf9928872017-03-30 02:48:54 -04005213 crq->msgs = NULL;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005214 return retrc;
5215}
5216
Lijun Pan635e4422020-08-19 17:52:26 -05005217static int ibmvnic_reset_init(struct ibmvnic_adapter *adapter, bool reset)
John Allenf6ef6402017-03-17 17:13:42 -05005218{
5219 struct device *dev = &adapter->vdev->dev;
Dany Madden98c41f02020-11-25 18:04:32 -06005220 unsigned long timeout = msecs_to_jiffies(20000);
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06005221 u64 old_num_rx_queues, old_num_tx_queues;
John Allenf6ef6402017-03-17 17:13:42 -05005222 int rc;
5223
John Allen017892c12017-05-26 10:30:19 -04005224 adapter->from_passive_init = false;
5225
Lijun Pan635e4422020-08-19 17:52:26 -05005226 if (reset) {
5227 old_num_rx_queues = adapter->req_rx_queues;
5228 old_num_tx_queues = adapter->req_tx_queues;
5229 reinit_completion(&adapter->init_done);
5230 }
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 */
5413 spin_lock_irqsave(&adapter->rwi_lock, flags);
Nathan Fontenot90c80142017-05-03 14:04:32 -04005414 adapter->state = VNIC_REMOVING;
Sukadev Bhattiprolu4a41c422021-02-12 20:42:50 -08005415 spin_unlock_irqrestore(&adapter->rwi_lock, flags);
5416
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);