blob: cd201f89ce6c009534b40fc7d6e72ca11ab35a06 [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;
81static int ibmvnic_remove(struct vio_dev *);
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -060082static void release_sub_crqs(struct ibmvnic_adapter *, bool);
Thomas Falcon032c5e82015-12-21 11:26:06 -060083static int ibmvnic_reset_crq(struct ibmvnic_adapter *);
84static int ibmvnic_send_crq_init(struct ibmvnic_adapter *);
85static int ibmvnic_reenable_crq_queue(struct ibmvnic_adapter *);
86static int ibmvnic_send_crq(struct ibmvnic_adapter *, union ibmvnic_crq *);
Thomas Falconad7775d2016-04-01 17:20:34 -050087static int send_subcrq_indirect(struct ibmvnic_adapter *, u64, u64, u64);
Thomas Falcon032c5e82015-12-21 11:26:06 -060088static irqreturn_t ibmvnic_interrupt_rx(int irq, void *instance);
89static int enable_scrq_irq(struct ibmvnic_adapter *,
90 struct ibmvnic_sub_crq_queue *);
91static int disable_scrq_irq(struct ibmvnic_adapter *,
92 struct ibmvnic_sub_crq_queue *);
93static int pending_scrq(struct ibmvnic_adapter *,
94 struct ibmvnic_sub_crq_queue *);
95static union sub_crq *ibmvnic_next_scrq(struct ibmvnic_adapter *,
96 struct ibmvnic_sub_crq_queue *);
97static int ibmvnic_poll(struct napi_struct *napi, int data);
Lijun Pan69980d02020-09-27 20:13:28 -050098static void send_query_map(struct ibmvnic_adapter *adapter);
Thomas Falcon9c4eaab2018-05-23 13:37:57 -050099static int send_request_map(struct ibmvnic_adapter *, dma_addr_t, __be32, u8);
100static int send_request_unmap(struct ibmvnic_adapter *, u8);
Thomas Falcon20a8ab72018-02-26 18:10:59 -0600101static int send_login(struct ibmvnic_adapter *adapter);
Lijun Pan491099a2020-09-27 20:13:26 -0500102static void send_query_cap(struct ibmvnic_adapter *adapter);
Thomas Falcon4d96f122017-08-01 15:04:36 -0500103static int init_sub_crqs(struct ibmvnic_adapter *);
John Allenbd0b6722017-03-17 17:13:40 -0500104static int init_sub_crq_irqs(struct ibmvnic_adapter *adapter);
Lijun Pan635e4422020-08-19 17:52:26 -0500105static int ibmvnic_reset_init(struct ibmvnic_adapter *, bool reset);
Nathan Fontenotf9928872017-03-30 02:48:54 -0400106static void release_crq_queue(struct ibmvnic_adapter *);
Thomas Falcon62740e92019-05-09 23:13:43 -0500107static int __ibmvnic_set_mac(struct net_device *, u8 *);
Nathan Fontenot30f79622018-04-06 18:37:06 -0500108static int init_crq_queue(struct ibmvnic_adapter *adapter);
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -0300109static int send_query_phys_parms(struct ibmvnic_adapter *adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600110
111struct ibmvnic_stat {
112 char name[ETH_GSTRING_LEN];
113 int offset;
114};
115
116#define IBMVNIC_STAT_OFF(stat) (offsetof(struct ibmvnic_adapter, stats) + \
117 offsetof(struct ibmvnic_statistics, stat))
118#define IBMVNIC_GET_STAT(a, off) (*((u64 *)(((unsigned long)(a)) + off)))
119
120static const struct ibmvnic_stat ibmvnic_stats[] = {
121 {"rx_packets", IBMVNIC_STAT_OFF(rx_packets)},
122 {"rx_bytes", IBMVNIC_STAT_OFF(rx_bytes)},
123 {"tx_packets", IBMVNIC_STAT_OFF(tx_packets)},
124 {"tx_bytes", IBMVNIC_STAT_OFF(tx_bytes)},
125 {"ucast_tx_packets", IBMVNIC_STAT_OFF(ucast_tx_packets)},
126 {"ucast_rx_packets", IBMVNIC_STAT_OFF(ucast_rx_packets)},
127 {"mcast_tx_packets", IBMVNIC_STAT_OFF(mcast_tx_packets)},
128 {"mcast_rx_packets", IBMVNIC_STAT_OFF(mcast_rx_packets)},
129 {"bcast_tx_packets", IBMVNIC_STAT_OFF(bcast_tx_packets)},
130 {"bcast_rx_packets", IBMVNIC_STAT_OFF(bcast_rx_packets)},
131 {"align_errors", IBMVNIC_STAT_OFF(align_errors)},
132 {"fcs_errors", IBMVNIC_STAT_OFF(fcs_errors)},
133 {"single_collision_frames", IBMVNIC_STAT_OFF(single_collision_frames)},
134 {"multi_collision_frames", IBMVNIC_STAT_OFF(multi_collision_frames)},
135 {"sqe_test_errors", IBMVNIC_STAT_OFF(sqe_test_errors)},
136 {"deferred_tx", IBMVNIC_STAT_OFF(deferred_tx)},
137 {"late_collisions", IBMVNIC_STAT_OFF(late_collisions)},
138 {"excess_collisions", IBMVNIC_STAT_OFF(excess_collisions)},
139 {"internal_mac_tx_errors", IBMVNIC_STAT_OFF(internal_mac_tx_errors)},
140 {"carrier_sense", IBMVNIC_STAT_OFF(carrier_sense)},
141 {"too_long_frames", IBMVNIC_STAT_OFF(too_long_frames)},
142 {"internal_mac_rx_errors", IBMVNIC_STAT_OFF(internal_mac_rx_errors)},
143};
144
145static long h_reg_sub_crq(unsigned long unit_address, unsigned long token,
146 unsigned long length, unsigned long *number,
147 unsigned long *irq)
148{
149 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
150 long rc;
151
152 rc = plpar_hcall(H_REG_SUB_CRQ, retbuf, unit_address, token, length);
153 *number = retbuf[0];
154 *irq = retbuf[1];
155
156 return rc;
157}
158
Thomas Falcon476d96c2019-11-25 17:12:55 -0600159/**
160 * ibmvnic_wait_for_completion - Check device state and wait for completion
161 * @adapter: private device data
162 * @comp_done: completion structure to wait for
163 * @timeout: time to wait in milliseconds
164 *
165 * Wait for a completion signal or until the timeout limit is reached
166 * while checking that the device is still active.
167 */
168static int ibmvnic_wait_for_completion(struct ibmvnic_adapter *adapter,
169 struct completion *comp_done,
170 unsigned long timeout)
171{
172 struct net_device *netdev;
173 unsigned long div_timeout;
174 u8 retry;
175
176 netdev = adapter->netdev;
177 retry = 5;
178 div_timeout = msecs_to_jiffies(timeout / retry);
179 while (true) {
180 if (!adapter->crq.active) {
181 netdev_err(netdev, "Device down!\n");
182 return -ENODEV;
183 }
Thomas Falcon8f9cc1e2019-12-11 09:38:39 -0600184 if (!retry--)
Thomas Falcon476d96c2019-11-25 17:12:55 -0600185 break;
186 if (wait_for_completion_timeout(comp_done, div_timeout))
187 return 0;
188 }
189 netdev_err(netdev, "Operation timed out.\n");
190 return -ETIMEDOUT;
191}
192
Thomas Falcon032c5e82015-12-21 11:26:06 -0600193static int alloc_long_term_buff(struct ibmvnic_adapter *adapter,
194 struct ibmvnic_long_term_buff *ltb, int size)
195{
196 struct device *dev = &adapter->vdev->dev;
Thomas Falcon9c4eaab2018-05-23 13:37:57 -0500197 int rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600198
199 ltb->size = size;
200 ltb->buff = dma_alloc_coherent(dev, ltb->size, &ltb->addr,
201 GFP_KERNEL);
202
203 if (!ltb->buff) {
204 dev_err(dev, "Couldn't alloc long term buffer\n");
205 return -ENOMEM;
206 }
207 ltb->map_id = adapter->map_id;
208 adapter->map_id++;
Nathan Fontenotdb5d0b52017-02-10 13:45:05 -0500209
Thomas Falconff25dcb2019-11-25 17:12:56 -0600210 mutex_lock(&adapter->fw_lock);
211 adapter->fw_done_rc = 0;
Thomas Falcon070eca92019-11-25 17:12:53 -0600212 reinit_completion(&adapter->fw_done);
Thomas Falcon9c4eaab2018-05-23 13:37:57 -0500213 rc = send_request_map(adapter, ltb->addr,
214 ltb->size, ltb->map_id);
215 if (rc) {
216 dma_free_coherent(dev, ltb->size, ltb->buff, ltb->addr);
Thomas Falconff25dcb2019-11-25 17:12:56 -0600217 mutex_unlock(&adapter->fw_lock);
Thomas Falcon9c4eaab2018-05-23 13:37:57 -0500218 return rc;
219 }
Thomas Falcon476d96c2019-11-25 17:12:55 -0600220
221 rc = ibmvnic_wait_for_completion(adapter, &adapter->fw_done, 10000);
222 if (rc) {
223 dev_err(dev,
224 "Long term map request aborted or timed out,rc = %d\n",
225 rc);
226 dma_free_coherent(dev, ltb->size, ltb->buff, ltb->addr);
Thomas Falconff25dcb2019-11-25 17:12:56 -0600227 mutex_unlock(&adapter->fw_lock);
Thomas Falcon476d96c2019-11-25 17:12:55 -0600228 return rc;
229 }
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500230
231 if (adapter->fw_done_rc) {
232 dev_err(dev, "Couldn't map long term buffer,rc = %d\n",
233 adapter->fw_done_rc);
Thomas Falcon4cf2ddf32018-05-16 15:49:03 -0500234 dma_free_coherent(dev, ltb->size, ltb->buff, ltb->addr);
Thomas Falconff25dcb2019-11-25 17:12:56 -0600235 mutex_unlock(&adapter->fw_lock);
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500236 return -1;
237 }
Thomas Falconff25dcb2019-11-25 17:12:56 -0600238 mutex_unlock(&adapter->fw_lock);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600239 return 0;
240}
241
242static void free_long_term_buff(struct ibmvnic_adapter *adapter,
243 struct ibmvnic_long_term_buff *ltb)
244{
245 struct device *dev = &adapter->vdev->dev;
246
Nathan Fontenotc657e322017-03-30 02:49:06 -0400247 if (!ltb->buff)
248 return;
249
Lijun Pan7d3a7b92021-02-12 20:49:00 -0600250 /* VIOS automatically unmaps the long term buffer at remote
251 * end for the following resets:
252 * FAILOVER, MOBILITY, TIMEOUT.
253 */
Nathan Fontenoted651a12017-05-03 14:04:38 -0400254 if (adapter->reset_reason != VNIC_RESET_FAILOVER &&
Lijun Pan7d3a7b92021-02-12 20:49:00 -0600255 adapter->reset_reason != VNIC_RESET_MOBILITY &&
256 adapter->reset_reason != VNIC_RESET_TIMEOUT)
Thomas Falcondfad09a2016-08-18 11:37:51 -0500257 send_request_unmap(adapter, ltb->map_id);
Brian King59af56c2017-04-19 13:44:41 -0400258 dma_free_coherent(dev, ltb->size, ltb->buff, ltb->addr);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600259}
260
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500261static int reset_long_term_buff(struct ibmvnic_adapter *adapter,
262 struct ibmvnic_long_term_buff *ltb)
263{
Thomas Falcon476d96c2019-11-25 17:12:55 -0600264 struct device *dev = &adapter->vdev->dev;
Thomas Falcon9c4eaab2018-05-23 13:37:57 -0500265 int rc;
266
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500267 memset(ltb->buff, 0, ltb->size);
268
Thomas Falconff25dcb2019-11-25 17:12:56 -0600269 mutex_lock(&adapter->fw_lock);
270 adapter->fw_done_rc = 0;
271
Thomas Falcon070eca92019-11-25 17:12:53 -0600272 reinit_completion(&adapter->fw_done);
Thomas Falcon9c4eaab2018-05-23 13:37:57 -0500273 rc = send_request_map(adapter, ltb->addr, ltb->size, ltb->map_id);
Thomas Falconff25dcb2019-11-25 17:12:56 -0600274 if (rc) {
275 mutex_unlock(&adapter->fw_lock);
Thomas Falcon9c4eaab2018-05-23 13:37:57 -0500276 return rc;
Thomas Falconff25dcb2019-11-25 17:12:56 -0600277 }
Thomas Falcon476d96c2019-11-25 17:12:55 -0600278
279 rc = ibmvnic_wait_for_completion(adapter, &adapter->fw_done, 10000);
280 if (rc) {
281 dev_info(dev,
282 "Reset failed, long term map request timed out or aborted\n");
Thomas Falconff25dcb2019-11-25 17:12:56 -0600283 mutex_unlock(&adapter->fw_lock);
Thomas Falcon476d96c2019-11-25 17:12:55 -0600284 return rc;
285 }
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500286
287 if (adapter->fw_done_rc) {
Thomas Falcon476d96c2019-11-25 17:12:55 -0600288 dev_info(dev,
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500289 "Reset failed, attempting to free and reallocate buffer\n");
290 free_long_term_buff(adapter, ltb);
Thomas Falconff25dcb2019-11-25 17:12:56 -0600291 mutex_unlock(&adapter->fw_lock);
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500292 return alloc_long_term_buff(adapter, ltb, ltb->size);
293 }
Thomas Falconff25dcb2019-11-25 17:12:56 -0600294 mutex_unlock(&adapter->fw_lock);
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500295 return 0;
296}
297
Thomas Falconf185a492017-05-26 10:30:48 -0400298static void deactivate_rx_pools(struct ibmvnic_adapter *adapter)
299{
300 int i;
301
Thomas Falcon507ebe62020-08-21 13:39:01 -0500302 for (i = 0; i < adapter->num_active_rx_pools; i++)
Thomas Falconf185a492017-05-26 10:30:48 -0400303 adapter->rx_pool[i].active = 0;
304}
305
Thomas Falcon032c5e82015-12-21 11:26:06 -0600306static void replenish_rx_pool(struct ibmvnic_adapter *adapter,
307 struct ibmvnic_rx_pool *pool)
308{
309 int count = pool->size - atomic_read(&pool->available);
Cristobal Fornof3ae59c2020-08-19 13:16:23 -0500310 u64 handle = adapter->rx_scrq[pool->index]->handle;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600311 struct device *dev = &adapter->vdev->dev;
Thomas Falcon4f0b6812020-11-18 19:12:18 -0600312 struct ibmvnic_ind_xmit_queue *ind_bufp;
313 struct ibmvnic_sub_crq_queue *rx_scrq;
314 union sub_crq *sub_crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600315 int buffers_added = 0;
316 unsigned long lpar_rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600317 struct sk_buff *skb;
318 unsigned int offset;
319 dma_addr_t dma_addr;
320 unsigned char *dst;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600321 int shift = 0;
322 int index;
323 int i;
324
Thomas Falconf185a492017-05-26 10:30:48 -0400325 if (!pool->active)
326 return;
327
Thomas Falcon4f0b6812020-11-18 19:12:18 -0600328 rx_scrq = adapter->rx_scrq[pool->index];
329 ind_bufp = &rx_scrq->ind_buf;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600330 for (i = 0; i < count; ++i) {
Dwip N. Banerjeee552aa32020-11-18 19:12:24 -0600331 skb = netdev_alloc_skb(adapter->netdev, pool->buff_size);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600332 if (!skb) {
333 dev_err(dev, "Couldn't replenish rx buff\n");
334 adapter->replenish_no_mem++;
335 break;
336 }
337
338 index = pool->free_map[pool->next_free];
339
340 if (pool->rx_buff[index].skb)
341 dev_err(dev, "Inconsistent free_map!\n");
342
343 /* Copy the skb to the long term mapped DMA buffer */
344 offset = index * pool->buff_size;
345 dst = pool->long_term_buff.buff + offset;
346 memset(dst, 0, pool->buff_size);
347 dma_addr = pool->long_term_buff.addr + offset;
348 pool->rx_buff[index].data = dst;
349
350 pool->free_map[pool->next_free] = IBMVNIC_INVALID_MAP;
351 pool->rx_buff[index].dma = dma_addr;
352 pool->rx_buff[index].skb = skb;
353 pool->rx_buff[index].pool_index = pool->index;
354 pool->rx_buff[index].size = pool->buff_size;
355
Thomas Falcon4f0b6812020-11-18 19:12:18 -0600356 sub_crq = &ind_bufp->indir_arr[ind_bufp->index++];
357 memset(sub_crq, 0, sizeof(*sub_crq));
358 sub_crq->rx_add.first = IBMVNIC_CRQ_CMD;
359 sub_crq->rx_add.correlator =
Thomas Falcon032c5e82015-12-21 11:26:06 -0600360 cpu_to_be64((u64)&pool->rx_buff[index]);
Thomas Falcon4f0b6812020-11-18 19:12:18 -0600361 sub_crq->rx_add.ioba = cpu_to_be32(dma_addr);
362 sub_crq->rx_add.map_id = pool->long_term_buff.map_id;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600363
364 /* The length field of the sCRQ is defined to be 24 bits so the
365 * buffer size needs to be left shifted by a byte before it is
366 * converted to big endian to prevent the last byte from being
367 * truncated.
368 */
369#ifdef __LITTLE_ENDIAN__
370 shift = 8;
371#endif
Thomas Falcon4f0b6812020-11-18 19:12:18 -0600372 sub_crq->rx_add.len = cpu_to_be32(pool->buff_size << shift);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600373 pool->next_free = (pool->next_free + 1) % pool->size;
Thomas Falcon4f0b6812020-11-18 19:12:18 -0600374 if (ind_bufp->index == IBMVNIC_MAX_IND_DESCS ||
375 i == count - 1) {
376 lpar_rc =
377 send_subcrq_indirect(adapter, handle,
378 (u64)ind_bufp->indir_dma,
379 (u64)ind_bufp->index);
380 if (lpar_rc != H_SUCCESS)
381 goto failure;
382 buffers_added += ind_bufp->index;
383 adapter->replenish_add_buff_success += ind_bufp->index;
384 ind_bufp->index = 0;
385 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600386 }
387 atomic_add(buffers_added, &pool->available);
388 return;
389
390failure:
Thomas Falcon2d14d372018-07-13 12:03:32 -0500391 if (lpar_rc != H_PARAMETER && lpar_rc != H_CLOSED)
392 dev_err_ratelimited(dev, "rx: replenish packet buffer failed\n");
Thomas Falcon4f0b6812020-11-18 19:12:18 -0600393 for (i = ind_bufp->index - 1; i >= 0; --i) {
394 struct ibmvnic_rx_buff *rx_buff;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600395
Thomas Falcon4f0b6812020-11-18 19:12:18 -0600396 pool->next_free = pool->next_free == 0 ?
397 pool->size - 1 : pool->next_free - 1;
398 sub_crq = &ind_bufp->indir_arr[i];
399 rx_buff = (struct ibmvnic_rx_buff *)
400 be64_to_cpu(sub_crq->rx_add.correlator);
401 index = (int)(rx_buff - pool->rx_buff);
402 pool->free_map[pool->next_free] = index;
403 dev_kfree_skb_any(pool->rx_buff[index].skb);
404 pool->rx_buff[index].skb = NULL;
405 }
Dwip N. Banerjeec2af6222020-12-09 20:53:31 -0500406 adapter->replenish_add_buff_failure += ind_bufp->index;
407 atomic_add(buffers_added, &pool->available);
Thomas Falcon4f0b6812020-11-18 19:12:18 -0600408 ind_bufp->index = 0;
Thomas Falcon5a18e1e2018-04-06 18:37:05 -0500409 if (lpar_rc == H_CLOSED || adapter->failover_pending) {
Thomas Falconf185a492017-05-26 10:30:48 -0400410 /* Disable buffer pool replenishment and report carrier off if
Thomas Falcon5a18e1e2018-04-06 18:37:05 -0500411 * queue is closed or pending failover.
412 * Firmware guarantees that a signal will be sent to the
413 * driver, triggering a reset.
Thomas Falconf185a492017-05-26 10:30:48 -0400414 */
415 deactivate_rx_pools(adapter);
416 netif_carrier_off(adapter->netdev);
417 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600418}
419
420static void replenish_pools(struct ibmvnic_adapter *adapter)
421{
422 int i;
423
Thomas Falcon032c5e82015-12-21 11:26:06 -0600424 adapter->replenish_task_cycles++;
Thomas Falcon507ebe62020-08-21 13:39:01 -0500425 for (i = 0; i < adapter->num_active_rx_pools; i++) {
Thomas Falcon032c5e82015-12-21 11:26:06 -0600426 if (adapter->rx_pool[i].active)
427 replenish_rx_pool(adapter, &adapter->rx_pool[i]);
428 }
Sukadev Bhattiprolu38bd5ce2020-12-04 18:22:35 -0800429
430 netdev_dbg(adapter->netdev, "Replenished %d pools\n", i);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600431}
432
John Allen3d52b592017-08-02 16:44:14 -0500433static void release_stats_buffers(struct ibmvnic_adapter *adapter)
434{
435 kfree(adapter->tx_stats_buffers);
436 kfree(adapter->rx_stats_buffers);
Thomas Falconb0992ec2018-02-06 17:25:23 -0600437 adapter->tx_stats_buffers = NULL;
438 adapter->rx_stats_buffers = NULL;
John Allen3d52b592017-08-02 16:44:14 -0500439}
440
441static int init_stats_buffers(struct ibmvnic_adapter *adapter)
442{
443 adapter->tx_stats_buffers =
Nathan Fontenotabcae542018-02-19 13:30:47 -0600444 kcalloc(IBMVNIC_MAX_QUEUES,
John Allen3d52b592017-08-02 16:44:14 -0500445 sizeof(struct ibmvnic_tx_queue_stats),
446 GFP_KERNEL);
447 if (!adapter->tx_stats_buffers)
448 return -ENOMEM;
449
450 adapter->rx_stats_buffers =
Nathan Fontenotabcae542018-02-19 13:30:47 -0600451 kcalloc(IBMVNIC_MAX_QUEUES,
John Allen3d52b592017-08-02 16:44:14 -0500452 sizeof(struct ibmvnic_rx_queue_stats),
453 GFP_KERNEL);
454 if (!adapter->rx_stats_buffers)
455 return -ENOMEM;
456
457 return 0;
458}
459
Nathan Fontenot7bbc27a2017-03-30 02:49:23 -0400460static void release_stats_token(struct ibmvnic_adapter *adapter)
461{
462 struct device *dev = &adapter->vdev->dev;
463
464 if (!adapter->stats_token)
465 return;
466
467 dma_unmap_single(dev, adapter->stats_token,
468 sizeof(struct ibmvnic_statistics),
469 DMA_FROM_DEVICE);
470 adapter->stats_token = 0;
471}
472
473static int init_stats_token(struct ibmvnic_adapter *adapter)
474{
475 struct device *dev = &adapter->vdev->dev;
476 dma_addr_t stok;
477
478 stok = dma_map_single(dev, &adapter->stats,
479 sizeof(struct ibmvnic_statistics),
480 DMA_FROM_DEVICE);
481 if (dma_mapping_error(dev, stok)) {
482 dev_err(dev, "Couldn't map stats buffer\n");
483 return -1;
484 }
485
486 adapter->stats_token = stok;
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500487 netdev_dbg(adapter->netdev, "Stats token initialized (%llx)\n", stok);
Nathan Fontenot7bbc27a2017-03-30 02:49:23 -0400488 return 0;
489}
490
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400491static int reset_rx_pools(struct ibmvnic_adapter *adapter)
492{
493 struct ibmvnic_rx_pool *rx_pool;
Thomas Falcon507ebe62020-08-21 13:39:01 -0500494 u64 buff_size;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400495 int rx_scrqs;
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500496 int i, j, rc;
John Allen896d8692018-01-18 16:26:31 -0600497
Mingming Cao9f134572020-08-25 13:26:41 -0400498 if (!adapter->rx_pool)
499 return -1;
500
Thomas Falcon507ebe62020-08-21 13:39:01 -0500501 buff_size = adapter->cur_rx_buf_sz;
502 rx_scrqs = adapter->num_active_rx_pools;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400503 for (i = 0; i < rx_scrqs; i++) {
504 rx_pool = &adapter->rx_pool[i];
505
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500506 netdev_dbg(adapter->netdev, "Re-setting rx_pool[%d]\n", i);
507
Thomas Falcon507ebe62020-08-21 13:39:01 -0500508 if (rx_pool->buff_size != buff_size) {
John Allen896d8692018-01-18 16:26:31 -0600509 free_long_term_buff(adapter, &rx_pool->long_term_buff);
Dwip N. Banerjee9a87c3f2020-11-18 19:12:22 -0600510 rx_pool->buff_size = ALIGN(buff_size, L1_CACHE_BYTES);
Thomas Falcon7c940b12019-06-07 16:03:55 -0500511 rc = alloc_long_term_buff(adapter,
512 &rx_pool->long_term_buff,
513 rx_pool->size *
514 rx_pool->buff_size);
John Allen896d8692018-01-18 16:26:31 -0600515 } else {
516 rc = reset_long_term_buff(adapter,
517 &rx_pool->long_term_buff);
518 }
519
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500520 if (rc)
521 return rc;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400522
523 for (j = 0; j < rx_pool->size; j++)
524 rx_pool->free_map[j] = j;
525
526 memset(rx_pool->rx_buff, 0,
527 rx_pool->size * sizeof(struct ibmvnic_rx_buff));
528
529 atomic_set(&rx_pool->available, 0);
530 rx_pool->next_alloc = 0;
531 rx_pool->next_free = 0;
Thomas Falconc3e53b92017-06-14 23:50:05 -0500532 rx_pool->active = 1;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400533 }
534
535 return 0;
536}
537
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400538static void release_rx_pools(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -0600539{
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400540 struct ibmvnic_rx_pool *rx_pool;
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400541 int i, j;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600542
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400543 if (!adapter->rx_pool)
Thomas Falcon032c5e82015-12-21 11:26:06 -0600544 return;
545
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600546 for (i = 0; i < adapter->num_active_rx_pools; i++) {
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400547 rx_pool = &adapter->rx_pool[i];
548
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500549 netdev_dbg(adapter->netdev, "Releasing rx_pool[%d]\n", i);
550
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400551 kfree(rx_pool->free_map);
552 free_long_term_buff(adapter, &rx_pool->long_term_buff);
553
554 if (!rx_pool->rx_buff)
Nathan Fontenote0ebe9422017-05-03 14:04:50 -0400555 continue;
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400556
557 for (j = 0; j < rx_pool->size; j++) {
558 if (rx_pool->rx_buff[j].skb) {
Thomas Falconb7cdec32018-11-21 11:17:58 -0600559 dev_kfree_skb_any(rx_pool->rx_buff[j].skb);
560 rx_pool->rx_buff[j].skb = NULL;
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400561 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600562 }
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400563
564 kfree(rx_pool->rx_buff);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600565 }
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400566
567 kfree(adapter->rx_pool);
568 adapter->rx_pool = NULL;
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600569 adapter->num_active_rx_pools = 0;
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400570}
571
572static int init_rx_pools(struct net_device *netdev)
573{
574 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
575 struct device *dev = &adapter->vdev->dev;
576 struct ibmvnic_rx_pool *rx_pool;
577 int rxadd_subcrqs;
Thomas Falcon507ebe62020-08-21 13:39:01 -0500578 u64 buff_size;
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400579 int i, j;
580
Thomas Falcon507ebe62020-08-21 13:39:01 -0500581 rxadd_subcrqs = adapter->num_active_rx_scrqs;
582 buff_size = adapter->cur_rx_buf_sz;
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400583
584 adapter->rx_pool = kcalloc(rxadd_subcrqs,
585 sizeof(struct ibmvnic_rx_pool),
586 GFP_KERNEL);
587 if (!adapter->rx_pool) {
588 dev_err(dev, "Failed to allocate rx pools\n");
589 return -1;
590 }
591
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600592 adapter->num_active_rx_pools = rxadd_subcrqs;
593
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400594 for (i = 0; i < rxadd_subcrqs; i++) {
595 rx_pool = &adapter->rx_pool[i];
596
597 netdev_dbg(adapter->netdev,
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500598 "Initializing rx_pool[%d], %lld buffs, %lld bytes each\n",
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400599 i, adapter->req_rx_add_entries_per_subcrq,
Thomas Falcon507ebe62020-08-21 13:39:01 -0500600 buff_size);
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400601
602 rx_pool->size = adapter->req_rx_add_entries_per_subcrq;
603 rx_pool->index = i;
Dwip N. Banerjee9a87c3f2020-11-18 19:12:22 -0600604 rx_pool->buff_size = ALIGN(buff_size, L1_CACHE_BYTES);
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400605 rx_pool->active = 1;
606
607 rx_pool->free_map = kcalloc(rx_pool->size, sizeof(int),
608 GFP_KERNEL);
609 if (!rx_pool->free_map) {
610 release_rx_pools(adapter);
611 return -1;
612 }
613
614 rx_pool->rx_buff = kcalloc(rx_pool->size,
615 sizeof(struct ibmvnic_rx_buff),
616 GFP_KERNEL);
617 if (!rx_pool->rx_buff) {
618 dev_err(dev, "Couldn't alloc rx buffers\n");
619 release_rx_pools(adapter);
620 return -1;
621 }
622
623 if (alloc_long_term_buff(adapter, &rx_pool->long_term_buff,
624 rx_pool->size * rx_pool->buff_size)) {
625 release_rx_pools(adapter);
626 return -1;
627 }
628
629 for (j = 0; j < rx_pool->size; ++j)
630 rx_pool->free_map[j] = j;
631
632 atomic_set(&rx_pool->available, 0);
633 rx_pool->next_alloc = 0;
634 rx_pool->next_free = 0;
635 }
636
637 return 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600638}
639
Thomas Falcone26dc252018-03-16 20:00:25 -0500640static int reset_one_tx_pool(struct ibmvnic_adapter *adapter,
641 struct ibmvnic_tx_pool *tx_pool)
642{
643 int rc, i;
644
645 rc = reset_long_term_buff(adapter, &tx_pool->long_term_buff);
646 if (rc)
647 return rc;
648
649 memset(tx_pool->tx_buff, 0,
650 tx_pool->num_buffers *
651 sizeof(struct ibmvnic_tx_buff));
652
653 for (i = 0; i < tx_pool->num_buffers; i++)
654 tx_pool->free_map[i] = i;
655
656 tx_pool->consumer_index = 0;
657 tx_pool->producer_index = 0;
658
659 return 0;
660}
661
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400662static int reset_tx_pools(struct ibmvnic_adapter *adapter)
663{
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400664 int tx_scrqs;
Thomas Falcone26dc252018-03-16 20:00:25 -0500665 int i, rc;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400666
Mingming Cao9f134572020-08-25 13:26:41 -0400667 if (!adapter->tx_pool)
668 return -1;
669
Thomas Falcon507ebe62020-08-21 13:39:01 -0500670 tx_scrqs = adapter->num_active_tx_pools;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400671 for (i = 0; i < tx_scrqs; i++) {
Thomas Falcone26dc252018-03-16 20:00:25 -0500672 rc = reset_one_tx_pool(adapter, &adapter->tso_pool[i]);
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500673 if (rc)
674 return rc;
Thomas Falcone26dc252018-03-16 20:00:25 -0500675 rc = reset_one_tx_pool(adapter, &adapter->tx_pool[i]);
Thomas Falconfdb06102017-10-17 12:36:55 -0500676 if (rc)
677 return rc;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400678 }
679
680 return 0;
681}
682
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -0200683static void release_vpd_data(struct ibmvnic_adapter *adapter)
684{
685 if (!adapter->vpd)
686 return;
687
688 kfree(adapter->vpd->buff);
689 kfree(adapter->vpd);
Thomas Falconb0992ec2018-02-06 17:25:23 -0600690
691 adapter->vpd = NULL;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -0200692}
693
Thomas Falconfb794212018-03-16 20:00:26 -0500694static void release_one_tx_pool(struct ibmvnic_adapter *adapter,
695 struct ibmvnic_tx_pool *tx_pool)
696{
697 kfree(tx_pool->tx_buff);
698 kfree(tx_pool->free_map);
699 free_long_term_buff(adapter, &tx_pool->long_term_buff);
700}
701
Nathan Fontenotc657e322017-03-30 02:49:06 -0400702static void release_tx_pools(struct ibmvnic_adapter *adapter)
703{
John Allen896d8692018-01-18 16:26:31 -0600704 int i;
Nathan Fontenotc657e322017-03-30 02:49:06 -0400705
706 if (!adapter->tx_pool)
707 return;
708
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600709 for (i = 0; i < adapter->num_active_tx_pools; i++) {
Thomas Falconfb794212018-03-16 20:00:26 -0500710 release_one_tx_pool(adapter, &adapter->tx_pool[i]);
711 release_one_tx_pool(adapter, &adapter->tso_pool[i]);
Nathan Fontenotc657e322017-03-30 02:49:06 -0400712 }
713
714 kfree(adapter->tx_pool);
715 adapter->tx_pool = NULL;
Thomas Falconfb794212018-03-16 20:00:26 -0500716 kfree(adapter->tso_pool);
717 adapter->tso_pool = NULL;
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600718 adapter->num_active_tx_pools = 0;
Nathan Fontenotc657e322017-03-30 02:49:06 -0400719}
720
Thomas Falcon32053062018-03-16 20:00:27 -0500721static int init_one_tx_pool(struct net_device *netdev,
722 struct ibmvnic_tx_pool *tx_pool,
723 int num_entries, int buf_size)
724{
725 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
726 int i;
727
728 tx_pool->tx_buff = kcalloc(num_entries,
729 sizeof(struct ibmvnic_tx_buff),
730 GFP_KERNEL);
731 if (!tx_pool->tx_buff)
732 return -1;
733
734 if (alloc_long_term_buff(adapter, &tx_pool->long_term_buff,
735 num_entries * buf_size))
736 return -1;
737
738 tx_pool->free_map = kcalloc(num_entries, sizeof(int), GFP_KERNEL);
739 if (!tx_pool->free_map)
740 return -1;
741
742 for (i = 0; i < num_entries; i++)
743 tx_pool->free_map[i] = i;
744
745 tx_pool->consumer_index = 0;
746 tx_pool->producer_index = 0;
747 tx_pool->num_buffers = num_entries;
748 tx_pool->buf_size = buf_size;
749
750 return 0;
751}
752
Nathan Fontenotc657e322017-03-30 02:49:06 -0400753static int init_tx_pools(struct net_device *netdev)
754{
755 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Nathan Fontenotc657e322017-03-30 02:49:06 -0400756 int tx_subcrqs;
Dwip N. Banerjee9a87c3f2020-11-18 19:12:22 -0600757 u64 buff_size;
Thomas Falcon32053062018-03-16 20:00:27 -0500758 int i, rc;
Nathan Fontenotc657e322017-03-30 02:49:06 -0400759
Thomas Falcon507ebe62020-08-21 13:39:01 -0500760 tx_subcrqs = adapter->num_active_tx_scrqs;
Nathan Fontenotc657e322017-03-30 02:49:06 -0400761 adapter->tx_pool = kcalloc(tx_subcrqs,
762 sizeof(struct ibmvnic_tx_pool), GFP_KERNEL);
763 if (!adapter->tx_pool)
764 return -1;
765
Thomas Falcon32053062018-03-16 20:00:27 -0500766 adapter->tso_pool = kcalloc(tx_subcrqs,
767 sizeof(struct ibmvnic_tx_pool), GFP_KERNEL);
768 if (!adapter->tso_pool)
769 return -1;
770
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600771 adapter->num_active_tx_pools = tx_subcrqs;
772
Nathan Fontenotc657e322017-03-30 02:49:06 -0400773 for (i = 0; i < tx_subcrqs; i++) {
Dwip N. Banerjee9a87c3f2020-11-18 19:12:22 -0600774 buff_size = adapter->req_mtu + VLAN_HLEN;
775 buff_size = ALIGN(buff_size, L1_CACHE_BYTES);
Thomas Falcon32053062018-03-16 20:00:27 -0500776 rc = init_one_tx_pool(netdev, &adapter->tx_pool[i],
777 adapter->req_tx_entries_per_subcrq,
Dwip N. Banerjee9a87c3f2020-11-18 19:12:22 -0600778 buff_size);
Thomas Falcon32053062018-03-16 20:00:27 -0500779 if (rc) {
Nathan Fontenotc657e322017-03-30 02:49:06 -0400780 release_tx_pools(adapter);
Thomas Falcon32053062018-03-16 20:00:27 -0500781 return rc;
Nathan Fontenotc657e322017-03-30 02:49:06 -0400782 }
783
Thomas Falcon7c940b12019-06-07 16:03:55 -0500784 rc = init_one_tx_pool(netdev, &adapter->tso_pool[i],
785 IBMVNIC_TSO_BUFS,
786 IBMVNIC_TSO_BUF_SZ);
Thomas Falcon32053062018-03-16 20:00:27 -0500787 if (rc) {
Nathan Fontenotc657e322017-03-30 02:49:06 -0400788 release_tx_pools(adapter);
Thomas Falcon32053062018-03-16 20:00:27 -0500789 return rc;
Nathan Fontenotc657e322017-03-30 02:49:06 -0400790 }
Nathan Fontenotc657e322017-03-30 02:49:06 -0400791 }
792
793 return 0;
794}
795
John Allend944c3d62017-05-26 10:30:13 -0400796static void ibmvnic_napi_enable(struct ibmvnic_adapter *adapter)
797{
798 int i;
799
800 if (adapter->napi_enabled)
801 return;
802
803 for (i = 0; i < adapter->req_rx_queues; i++)
804 napi_enable(&adapter->napi[i]);
805
806 adapter->napi_enabled = true;
807}
808
809static void ibmvnic_napi_disable(struct ibmvnic_adapter *adapter)
810{
811 int i;
812
813 if (!adapter->napi_enabled)
814 return;
815
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500816 for (i = 0; i < adapter->req_rx_queues; i++) {
817 netdev_dbg(adapter->netdev, "Disabling napi[%d]\n", i);
John Allend944c3d62017-05-26 10:30:13 -0400818 napi_disable(&adapter->napi[i]);
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500819 }
John Allend944c3d62017-05-26 10:30:13 -0400820
821 adapter->napi_enabled = false;
822}
823
Nathan Fontenot86f669b2018-02-19 13:30:39 -0600824static int init_napi(struct ibmvnic_adapter *adapter)
825{
826 int i;
827
828 adapter->napi = kcalloc(adapter->req_rx_queues,
829 sizeof(struct napi_struct), GFP_KERNEL);
830 if (!adapter->napi)
831 return -ENOMEM;
832
833 for (i = 0; i < adapter->req_rx_queues; i++) {
834 netdev_dbg(adapter->netdev, "Adding napi[%d]\n", i);
835 netif_napi_add(adapter->netdev, &adapter->napi[i],
836 ibmvnic_poll, NAPI_POLL_WEIGHT);
837 }
838
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600839 adapter->num_active_rx_napi = adapter->req_rx_queues;
Nathan Fontenot86f669b2018-02-19 13:30:39 -0600840 return 0;
841}
842
843static void release_napi(struct ibmvnic_adapter *adapter)
844{
845 int i;
846
847 if (!adapter->napi)
848 return;
849
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600850 for (i = 0; i < adapter->num_active_rx_napi; i++) {
Wen Yang390de192018-12-11 12:20:46 +0800851 netdev_dbg(adapter->netdev, "Releasing napi[%d]\n", i);
852 netif_napi_del(&adapter->napi[i]);
Nathan Fontenot86f669b2018-02-19 13:30:39 -0600853 }
854
855 kfree(adapter->napi);
856 adapter->napi = NULL;
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600857 adapter->num_active_rx_napi = 0;
Thomas Falconc3f22412018-05-23 13:37:55 -0500858 adapter->napi_enabled = false;
Nathan Fontenot86f669b2018-02-19 13:30:39 -0600859}
860
John Allena57a5d22017-03-17 17:13:41 -0500861static int ibmvnic_login(struct net_device *netdev)
Thomas Falcon032c5e82015-12-21 11:26:06 -0600862{
863 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Dany Madden98c41f02020-11-25 18:04:32 -0600864 unsigned long timeout = msecs_to_jiffies(20000);
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500865 int retry_count = 0;
Thomas Falcondff515a32020-06-15 10:29:23 -0500866 int retries = 10;
Thomas Falconeb110412018-05-24 14:37:53 -0500867 bool retry;
Thomas Falcon4d96f122017-08-01 15:04:36 -0500868 int rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600869
John Allenbd0b6722017-03-17 17:13:40 -0500870 do {
Thomas Falconeb110412018-05-24 14:37:53 -0500871 retry = false;
Thomas Falcondff515a32020-06-15 10:29:23 -0500872 if (retry_count > retries) {
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500873 netdev_warn(netdev, "Login attempts exceeded\n");
874 return -1;
875 }
876
877 adapter->init_done_rc = 0;
878 reinit_completion(&adapter->init_done);
879 rc = send_login(adapter);
Dany Maddenc98d9cc2020-11-25 18:04:30 -0600880 if (rc)
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500881 return rc;
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500882
883 if (!wait_for_completion_timeout(&adapter->init_done,
884 timeout)) {
Thomas Falcondff515a32020-06-15 10:29:23 -0500885 netdev_warn(netdev, "Login timed out, retrying...\n");
886 retry = true;
887 adapter->init_done_rc = 0;
888 retry_count++;
889 continue;
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500890 }
891
Thomas Falcondff515a32020-06-15 10:29:23 -0500892 if (adapter->init_done_rc == ABORTED) {
893 netdev_warn(netdev, "Login aborted, retrying...\n");
894 retry = true;
895 adapter->init_done_rc = 0;
896 retry_count++;
897 /* FW or device may be busy, so
898 * wait a bit before retrying login
899 */
900 msleep(500);
901 } else if (adapter->init_done_rc == PARTIALSUCCESS) {
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500902 retry_count++;
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -0600903 release_sub_crqs(adapter, 1);
John Allenbd0b6722017-03-17 17:13:40 -0500904
Thomas Falconeb110412018-05-24 14:37:53 -0500905 retry = true;
906 netdev_dbg(netdev,
907 "Received partial success, retrying...\n");
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500908 adapter->init_done_rc = 0;
John Allenbd0b6722017-03-17 17:13:40 -0500909 reinit_completion(&adapter->init_done);
Lijun Pan491099a2020-09-27 20:13:26 -0500910 send_query_cap(adapter);
John Allenbd0b6722017-03-17 17:13:40 -0500911 if (!wait_for_completion_timeout(&adapter->init_done,
912 timeout)) {
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500913 netdev_warn(netdev,
914 "Capabilities query timed out\n");
John Allenbd0b6722017-03-17 17:13:40 -0500915 return -1;
916 }
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500917
Thomas Falcon4d96f122017-08-01 15:04:36 -0500918 rc = init_sub_crqs(adapter);
919 if (rc) {
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500920 netdev_warn(netdev,
921 "SCRQ initialization failed\n");
Thomas Falcon4d96f122017-08-01 15:04:36 -0500922 return -1;
923 }
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500924
Thomas Falcon4d96f122017-08-01 15:04:36 -0500925 rc = init_sub_crq_irqs(adapter);
926 if (rc) {
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500927 netdev_warn(netdev,
928 "SCRQ irq initialization failed\n");
Thomas Falcon4d96f122017-08-01 15:04:36 -0500929 return -1;
930 }
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500931 } else if (adapter->init_done_rc) {
932 netdev_warn(netdev, "Adapter login failed\n");
John Allenbd0b6722017-03-17 17:13:40 -0500933 return -1;
934 }
Thomas Falconeb110412018-05-24 14:37:53 -0500935 } while (retry);
John Allenbd0b6722017-03-17 17:13:40 -0500936
Thomas Falcon62740e92019-05-09 23:13:43 -0500937 __ibmvnic_set_mac(netdev, adapter->mac_addr);
Thomas Falcon3d166132018-01-10 19:39:52 -0600938
Sukadev Bhattiprolu38bd5ce2020-12-04 18:22:35 -0800939 netdev_dbg(netdev, "[S:%d] Login succeeded\n", adapter->state);
John Allena57a5d22017-03-17 17:13:41 -0500940 return 0;
941}
942
Thomas Falcon34f0f4e2018-02-13 18:23:40 -0600943static void release_login_buffer(struct ibmvnic_adapter *adapter)
944{
945 kfree(adapter->login_buf);
946 adapter->login_buf = NULL;
947}
948
949static void release_login_rsp_buffer(struct ibmvnic_adapter *adapter)
950{
951 kfree(adapter->login_rsp_buf);
952 adapter->login_rsp_buf = NULL;
953}
954
Nathan Fontenot1b8955e2017-03-30 02:49:29 -0400955static void release_resources(struct ibmvnic_adapter *adapter)
956{
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -0200957 release_vpd_data(adapter);
958
Nathan Fontenot1b8955e2017-03-30 02:49:29 -0400959 release_tx_pools(adapter);
960 release_rx_pools(adapter);
961
Nathan Fontenot86f669b2018-02-19 13:30:39 -0600962 release_napi(adapter);
Lijun Pana0c8be52020-12-19 15:39:19 -0600963 release_login_buffer(adapter);
Thomas Falcon34f0f4e2018-02-13 18:23:40 -0600964 release_login_rsp_buffer(adapter);
Nathan Fontenot1b8955e2017-03-30 02:49:29 -0400965}
966
Nathan Fontenot53da09e2017-04-21 15:39:04 -0400967static int set_link_state(struct ibmvnic_adapter *adapter, u8 link_state)
968{
969 struct net_device *netdev = adapter->netdev;
Dany Madden98c41f02020-11-25 18:04:32 -0600970 unsigned long timeout = msecs_to_jiffies(20000);
Nathan Fontenot53da09e2017-04-21 15:39:04 -0400971 union ibmvnic_crq crq;
972 bool resend;
973 int rc;
974
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500975 netdev_dbg(netdev, "setting link state %d\n", link_state);
976
Nathan Fontenot53da09e2017-04-21 15:39:04 -0400977 memset(&crq, 0, sizeof(crq));
978 crq.logical_link_state.first = IBMVNIC_CRQ_CMD;
979 crq.logical_link_state.cmd = LOGICAL_LINK_STATE;
980 crq.logical_link_state.link_state = link_state;
981
982 do {
983 resend = false;
984
985 reinit_completion(&adapter->init_done);
986 rc = ibmvnic_send_crq(adapter, &crq);
987 if (rc) {
988 netdev_err(netdev, "Failed to set link state\n");
989 return rc;
990 }
991
992 if (!wait_for_completion_timeout(&adapter->init_done,
993 timeout)) {
994 netdev_err(netdev, "timeout setting link state\n");
995 return -1;
996 }
997
Lijun Pan4c5f6af2020-08-19 17:52:23 -0500998 if (adapter->init_done_rc == PARTIALSUCCESS) {
Nathan Fontenot53da09e2017-04-21 15:39:04 -0400999 /* Partuial success, delay and re-send */
1000 mdelay(1000);
1001 resend = true;
Thomas Falconab5ec332018-05-23 13:37:59 -05001002 } else if (adapter->init_done_rc) {
1003 netdev_warn(netdev, "Unable to set link state, rc=%d\n",
1004 adapter->init_done_rc);
1005 return adapter->init_done_rc;
Nathan Fontenot53da09e2017-04-21 15:39:04 -04001006 }
1007 } while (resend);
1008
1009 return 0;
1010}
1011
Thomas Falcon7f3c6e62017-04-21 15:38:40 -04001012static int set_real_num_queues(struct net_device *netdev)
1013{
1014 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1015 int rc;
1016
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001017 netdev_dbg(netdev, "Setting real tx/rx queues (%llx/%llx)\n",
1018 adapter->req_tx_queues, adapter->req_rx_queues);
1019
Thomas Falcon7f3c6e62017-04-21 15:38:40 -04001020 rc = netif_set_real_num_tx_queues(netdev, adapter->req_tx_queues);
1021 if (rc) {
1022 netdev_err(netdev, "failed to set the number of tx queues\n");
1023 return rc;
1024 }
1025
1026 rc = netif_set_real_num_rx_queues(netdev, adapter->req_rx_queues);
1027 if (rc)
1028 netdev_err(netdev, "failed to set the number of rx queues\n");
1029
1030 return rc;
1031}
1032
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001033static int ibmvnic_get_vpd(struct ibmvnic_adapter *adapter)
1034{
1035 struct device *dev = &adapter->vdev->dev;
1036 union ibmvnic_crq crq;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001037 int len = 0;
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05001038 int rc;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001039
1040 if (adapter->vpd->buff)
1041 len = adapter->vpd->len;
1042
Thomas Falconff25dcb2019-11-25 17:12:56 -06001043 mutex_lock(&adapter->fw_lock);
1044 adapter->fw_done_rc = 0;
Thomas Falcon070eca92019-11-25 17:12:53 -06001045 reinit_completion(&adapter->fw_done);
Thomas Falconff25dcb2019-11-25 17:12:56 -06001046
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001047 crq.get_vpd_size.first = IBMVNIC_CRQ_CMD;
1048 crq.get_vpd_size.cmd = GET_VPD_SIZE;
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05001049 rc = ibmvnic_send_crq(adapter, &crq);
Thomas Falconff25dcb2019-11-25 17:12:56 -06001050 if (rc) {
1051 mutex_unlock(&adapter->fw_lock);
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05001052 return rc;
Thomas Falconff25dcb2019-11-25 17:12:56 -06001053 }
Thomas Falcon476d96c2019-11-25 17:12:55 -06001054
1055 rc = ibmvnic_wait_for_completion(adapter, &adapter->fw_done, 10000);
1056 if (rc) {
1057 dev_err(dev, "Could not retrieve VPD size, rc = %d\n", rc);
Thomas Falconff25dcb2019-11-25 17:12:56 -06001058 mutex_unlock(&adapter->fw_lock);
Thomas Falcon476d96c2019-11-25 17:12:55 -06001059 return rc;
1060 }
Thomas Falconff25dcb2019-11-25 17:12:56 -06001061 mutex_unlock(&adapter->fw_lock);
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001062
1063 if (!adapter->vpd->len)
1064 return -ENODATA;
1065
1066 if (!adapter->vpd->buff)
1067 adapter->vpd->buff = kzalloc(adapter->vpd->len, GFP_KERNEL);
1068 else if (adapter->vpd->len != len)
1069 adapter->vpd->buff =
1070 krealloc(adapter->vpd->buff,
1071 adapter->vpd->len, GFP_KERNEL);
1072
1073 if (!adapter->vpd->buff) {
1074 dev_err(dev, "Could allocate VPD buffer\n");
1075 return -ENOMEM;
1076 }
1077
1078 adapter->vpd->dma_addr =
1079 dma_map_single(dev, adapter->vpd->buff, adapter->vpd->len,
1080 DMA_FROM_DEVICE);
Desnes Augusto Nunes do Rosariof7431062017-11-17 09:09:04 -02001081 if (dma_mapping_error(dev, adapter->vpd->dma_addr)) {
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001082 dev_err(dev, "Could not map VPD buffer\n");
1083 kfree(adapter->vpd->buff);
Thomas Falconb0992ec2018-02-06 17:25:23 -06001084 adapter->vpd->buff = NULL;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001085 return -ENOMEM;
1086 }
1087
Thomas Falconff25dcb2019-11-25 17:12:56 -06001088 mutex_lock(&adapter->fw_lock);
1089 adapter->fw_done_rc = 0;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001090 reinit_completion(&adapter->fw_done);
Thomas Falconff25dcb2019-11-25 17:12:56 -06001091
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001092 crq.get_vpd.first = IBMVNIC_CRQ_CMD;
1093 crq.get_vpd.cmd = GET_VPD;
1094 crq.get_vpd.ioba = cpu_to_be32(adapter->vpd->dma_addr);
1095 crq.get_vpd.len = cpu_to_be32((u32)adapter->vpd->len);
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05001096 rc = ibmvnic_send_crq(adapter, &crq);
1097 if (rc) {
1098 kfree(adapter->vpd->buff);
1099 adapter->vpd->buff = NULL;
Thomas Falconff25dcb2019-11-25 17:12:56 -06001100 mutex_unlock(&adapter->fw_lock);
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05001101 return rc;
1102 }
Thomas Falcon476d96c2019-11-25 17:12:55 -06001103
1104 rc = ibmvnic_wait_for_completion(adapter, &adapter->fw_done, 10000);
1105 if (rc) {
1106 dev_err(dev, "Unable to retrieve VPD, rc = %d\n", rc);
1107 kfree(adapter->vpd->buff);
1108 adapter->vpd->buff = NULL;
Thomas Falconff25dcb2019-11-25 17:12:56 -06001109 mutex_unlock(&adapter->fw_lock);
Thomas Falcon476d96c2019-11-25 17:12:55 -06001110 return rc;
1111 }
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001112
Thomas Falconff25dcb2019-11-25 17:12:56 -06001113 mutex_unlock(&adapter->fw_lock);
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001114 return 0;
1115}
1116
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001117static int init_resources(struct ibmvnic_adapter *adapter)
John Allena57a5d22017-03-17 17:13:41 -05001118{
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001119 struct net_device *netdev = adapter->netdev;
Nathan Fontenot86f669b2018-02-19 13:30:39 -06001120 int rc;
John Allena57a5d22017-03-17 17:13:41 -05001121
Thomas Falcon7f3c6e62017-04-21 15:38:40 -04001122 rc = set_real_num_queues(netdev);
1123 if (rc)
1124 return rc;
John Allenbd0b6722017-03-17 17:13:40 -05001125
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001126 adapter->vpd = kzalloc(sizeof(*adapter->vpd), GFP_KERNEL);
1127 if (!adapter->vpd)
1128 return -ENOMEM;
1129
John Allen69d08dc2018-01-18 16:27:58 -06001130 /* Vital Product Data (VPD) */
1131 rc = ibmvnic_get_vpd(adapter);
1132 if (rc) {
1133 netdev_err(netdev, "failed to initialize Vital Product Data (VPD)\n");
1134 return rc;
1135 }
1136
Thomas Falcon032c5e82015-12-21 11:26:06 -06001137 adapter->map_id = 1;
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001138
Nathan Fontenot86f669b2018-02-19 13:30:39 -06001139 rc = init_napi(adapter);
1140 if (rc)
1141 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001142
Lijun Pan69980d02020-09-27 20:13:28 -05001143 send_query_map(adapter);
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -04001144
1145 rc = init_rx_pools(netdev);
1146 if (rc)
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001147 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001148
Nathan Fontenotc657e322017-03-30 02:49:06 -04001149 rc = init_tx_pools(netdev);
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001150 return rc;
1151}
1152
Nathan Fontenoted651a12017-05-03 14:04:38 -04001153static int __ibmvnic_open(struct net_device *netdev)
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001154{
1155 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001156 enum vnic_state prev_state = adapter->state;
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001157 int i, rc;
1158
Nathan Fontenot90c80142017-05-03 14:04:32 -04001159 adapter->state = VNIC_OPENING;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001160 replenish_pools(adapter);
John Allend944c3d62017-05-26 10:30:13 -04001161 ibmvnic_napi_enable(adapter);
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001162
Thomas Falcon032c5e82015-12-21 11:26:06 -06001163 /* We're ready to receive frames, enable the sub-crq interrupts and
1164 * set the logical link state to up
1165 */
Nathan Fontenoted651a12017-05-03 14:04:38 -04001166 for (i = 0; i < adapter->req_rx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001167 netdev_dbg(netdev, "Enabling rx_scrq[%d] irq\n", i);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001168 if (prev_state == VNIC_CLOSED)
1169 enable_irq(adapter->rx_scrq[i]->irq);
Thomas Falconf23e0642018-04-15 18:53:36 -05001170 enable_scrq_irq(adapter, adapter->rx_scrq[i]);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001171 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001172
Nathan Fontenoted651a12017-05-03 14:04:38 -04001173 for (i = 0; i < adapter->req_tx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001174 netdev_dbg(netdev, "Enabling tx_scrq[%d] irq\n", i);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001175 if (prev_state == VNIC_CLOSED)
1176 enable_irq(adapter->tx_scrq[i]->irq);
Thomas Falconf23e0642018-04-15 18:53:36 -05001177 enable_scrq_irq(adapter, adapter->tx_scrq[i]);
Thomas Falcon0d973382020-11-18 19:12:19 -06001178 netdev_tx_reset_queue(netdev_get_tx_queue(netdev, i));
Nathan Fontenoted651a12017-05-03 14:04:38 -04001179 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001180
Nathan Fontenot53da09e2017-04-21 15:39:04 -04001181 rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_UP);
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001182 if (rc) {
1183 for (i = 0; i < adapter->req_rx_queues; i++)
1184 napi_disable(&adapter->napi[i]);
1185 release_resources(adapter);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001186 return rc;
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001187 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001188
Nathan Fontenoted651a12017-05-03 14:04:38 -04001189 netif_tx_start_all_queues(netdev);
1190
1191 if (prev_state == VNIC_CLOSED) {
1192 for (i = 0; i < adapter->req_rx_queues; i++)
1193 napi_schedule(&adapter->napi[i]);
1194 }
1195
1196 adapter->state = VNIC_OPEN;
1197 return rc;
1198}
1199
1200static int ibmvnic_open(struct net_device *netdev)
1201{
1202 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
John Allen69d08dc2018-01-18 16:27:58 -06001203 int rc;
Nathan Fontenoted651a12017-05-03 14:04:38 -04001204
Thomas Falcon5a18e1e2018-04-06 18:37:05 -05001205 /* If device failover is pending, just set device state and return.
1206 * Device operation will be handled by reset routine.
1207 */
1208 if (adapter->failover_pending) {
1209 adapter->state = VNIC_OPEN;
1210 return 0;
1211 }
1212
Nathan Fontenoted651a12017-05-03 14:04:38 -04001213 if (adapter->state != VNIC_CLOSED) {
1214 rc = ibmvnic_login(netdev);
Juliet Kima5681e22018-11-19 15:59:22 -06001215 if (rc)
Sukadev Bhattiprolu1d850492020-10-30 10:07:11 -07001216 goto out;
Nathan Fontenoted651a12017-05-03 14:04:38 -04001217
1218 rc = init_resources(adapter);
1219 if (rc) {
1220 netdev_err(netdev, "failed to initialize resources\n");
1221 release_resources(adapter);
Sukadev Bhattiprolu1d850492020-10-30 10:07:11 -07001222 goto out;
Nathan Fontenoted651a12017-05-03 14:04:38 -04001223 }
1224 }
1225
1226 rc = __ibmvnic_open(netdev);
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001227
Sukadev Bhattiprolu1d850492020-10-30 10:07:11 -07001228out:
1229 /*
1230 * If open fails due to a pending failover, set device state and
1231 * return. Device operation will be handled by reset routine.
1232 */
1233 if (rc && adapter->failover_pending) {
1234 adapter->state = VNIC_OPEN;
1235 rc = 0;
1236 }
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001237 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001238}
1239
Thomas Falcond0869c02018-02-13 18:23:43 -06001240static void clean_rx_pools(struct ibmvnic_adapter *adapter)
1241{
1242 struct ibmvnic_rx_pool *rx_pool;
Thomas Falcon637f81d2018-02-26 18:10:57 -06001243 struct ibmvnic_rx_buff *rx_buff;
Thomas Falcond0869c02018-02-13 18:23:43 -06001244 u64 rx_entries;
1245 int rx_scrqs;
1246 int i, j;
1247
1248 if (!adapter->rx_pool)
1249 return;
1250
Thomas Falcon660e3092018-04-20 14:25:32 -05001251 rx_scrqs = adapter->num_active_rx_pools;
Thomas Falcond0869c02018-02-13 18:23:43 -06001252 rx_entries = adapter->req_rx_add_entries_per_subcrq;
1253
1254 /* Free any remaining skbs in the rx buffer pools */
1255 for (i = 0; i < rx_scrqs; i++) {
1256 rx_pool = &adapter->rx_pool[i];
Thomas Falcon637f81d2018-02-26 18:10:57 -06001257 if (!rx_pool || !rx_pool->rx_buff)
Thomas Falcond0869c02018-02-13 18:23:43 -06001258 continue;
1259
1260 netdev_dbg(adapter->netdev, "Cleaning rx_pool[%d]\n", i);
1261 for (j = 0; j < rx_entries; j++) {
Thomas Falcon637f81d2018-02-26 18:10:57 -06001262 rx_buff = &rx_pool->rx_buff[j];
1263 if (rx_buff && rx_buff->skb) {
1264 dev_kfree_skb_any(rx_buff->skb);
1265 rx_buff->skb = NULL;
Thomas Falcond0869c02018-02-13 18:23:43 -06001266 }
1267 }
1268 }
1269}
1270
Thomas Falcone9e1e972018-03-16 20:00:30 -05001271static void clean_one_tx_pool(struct ibmvnic_adapter *adapter,
1272 struct ibmvnic_tx_pool *tx_pool)
Nathan Fontenotb41b83e2017-05-03 14:04:56 -04001273{
Thomas Falcon637f81d2018-02-26 18:10:57 -06001274 struct ibmvnic_tx_buff *tx_buff;
Nathan Fontenotb41b83e2017-05-03 14:04:56 -04001275 u64 tx_entries;
Thomas Falcone9e1e972018-03-16 20:00:30 -05001276 int i;
Nathan Fontenotb41b83e2017-05-03 14:04:56 -04001277
Dan Carpenter050e85c2018-03-23 14:36:15 +03001278 if (!tx_pool || !tx_pool->tx_buff)
Thomas Falcone9e1e972018-03-16 20:00:30 -05001279 return;
1280
1281 tx_entries = tx_pool->num_buffers;
1282
1283 for (i = 0; i < tx_entries; i++) {
1284 tx_buff = &tx_pool->tx_buff[i];
1285 if (tx_buff && tx_buff->skb) {
1286 dev_kfree_skb_any(tx_buff->skb);
1287 tx_buff->skb = NULL;
1288 }
1289 }
1290}
1291
1292static void clean_tx_pools(struct ibmvnic_adapter *adapter)
1293{
1294 int tx_scrqs;
1295 int i;
1296
1297 if (!adapter->tx_pool || !adapter->tso_pool)
Nathan Fontenotb41b83e2017-05-03 14:04:56 -04001298 return;
1299
Thomas Falcon660e3092018-04-20 14:25:32 -05001300 tx_scrqs = adapter->num_active_tx_pools;
Nathan Fontenotb41b83e2017-05-03 14:04:56 -04001301
1302 /* Free any remaining skbs in the tx buffer pools */
1303 for (i = 0; i < tx_scrqs; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001304 netdev_dbg(adapter->netdev, "Cleaning tx_pool[%d]\n", i);
Thomas Falcone9e1e972018-03-16 20:00:30 -05001305 clean_one_tx_pool(adapter, &adapter->tx_pool[i]);
1306 clean_one_tx_pool(adapter, &adapter->tso_pool[i]);
Nathan Fontenotb41b83e2017-05-03 14:04:56 -04001307 }
1308}
1309
John Allen6095e592018-03-30 13:44:21 -05001310static void ibmvnic_disable_irqs(struct ibmvnic_adapter *adapter)
John Allenea5509f2017-03-17 17:13:43 -05001311{
John Allen6095e592018-03-30 13:44:21 -05001312 struct net_device *netdev = adapter->netdev;
John Allenea5509f2017-03-17 17:13:43 -05001313 int i;
1314
Nathan Fontenot46293b92017-05-03 14:05:02 -04001315 if (adapter->tx_scrq) {
1316 for (i = 0; i < adapter->req_tx_queues; i++)
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001317 if (adapter->tx_scrq[i]->irq) {
Thomas Falconf8738662018-03-07 17:51:45 -06001318 netdev_dbg(netdev,
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001319 "Disabling tx_scrq[%d] irq\n", i);
Thomas Falconf23e0642018-04-15 18:53:36 -05001320 disable_scrq_irq(adapter, adapter->tx_scrq[i]);
Nathan Fontenot46293b92017-05-03 14:05:02 -04001321 disable_irq(adapter->tx_scrq[i]->irq);
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001322 }
Nathan Fontenot46293b92017-05-03 14:05:02 -04001323 }
1324
Nathan Fontenot46293b92017-05-03 14:05:02 -04001325 if (adapter->rx_scrq) {
1326 for (i = 0; i < adapter->req_rx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001327 if (adapter->rx_scrq[i]->irq) {
Thomas Falconf8738662018-03-07 17:51:45 -06001328 netdev_dbg(netdev,
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001329 "Disabling rx_scrq[%d] irq\n", i);
Thomas Falconf23e0642018-04-15 18:53:36 -05001330 disable_scrq_irq(adapter, adapter->rx_scrq[i]);
Nathan Fontenot46293b92017-05-03 14:05:02 -04001331 disable_irq(adapter->rx_scrq[i]->irq);
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001332 }
Nathan Fontenot46293b92017-05-03 14:05:02 -04001333 }
1334 }
John Allen6095e592018-03-30 13:44:21 -05001335}
1336
1337static void ibmvnic_cleanup(struct net_device *netdev)
1338{
1339 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1340
1341 /* ensure that transmissions are stopped if called by do_reset */
Juliet Kim7ed5b312019-09-20 16:11:23 -04001342 if (test_bit(0, &adapter->resetting))
John Allen6095e592018-03-30 13:44:21 -05001343 netif_tx_disable(netdev);
1344 else
1345 netif_tx_stop_all_queues(netdev);
1346
1347 ibmvnic_napi_disable(adapter);
1348 ibmvnic_disable_irqs(adapter);
1349
Thomas Falcond0869c02018-02-13 18:23:43 -06001350 clean_rx_pools(adapter);
Thomas Falcon10f76212017-05-26 10:30:31 -04001351 clean_tx_pools(adapter);
Thomas Falcon01d9bd72018-03-07 17:51:46 -06001352}
1353
1354static int __ibmvnic_close(struct net_device *netdev)
1355{
1356 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1357 int rc = 0;
1358
1359 adapter->state = VNIC_CLOSING;
1360 rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_DN);
Nathan Fontenot90c80142017-05-03 14:04:32 -04001361 adapter->state = VNIC_CLOSED;
Sukadev Bhattiprolud4083d32021-02-10 17:41:43 -08001362 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001363}
1364
Nathan Fontenoted651a12017-05-03 14:04:38 -04001365static int ibmvnic_close(struct net_device *netdev)
1366{
1367 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1368 int rc;
1369
Sukadev Bhattiprolu38bd5ce2020-12-04 18:22:35 -08001370 netdev_dbg(netdev, "[S:%d FOP:%d FRR:%d] Closing\n",
1371 adapter->state, adapter->failover_pending,
1372 adapter->force_reset_recovery);
1373
Thomas Falcon5a18e1e2018-04-06 18:37:05 -05001374 /* If device failover is pending, just set device state and return.
1375 * Device operation will be handled by reset routine.
1376 */
1377 if (adapter->failover_pending) {
1378 adapter->state = VNIC_CLOSED;
1379 return 0;
1380 }
1381
Nathan Fontenoted651a12017-05-03 14:04:38 -04001382 rc = __ibmvnic_close(netdev);
Nathan Fontenot30f79622018-04-06 18:37:06 -05001383 ibmvnic_cleanup(netdev);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001384
1385 return rc;
1386}
1387
Thomas Falconad7775d2016-04-01 17:20:34 -05001388/**
1389 * build_hdr_data - creates L2/L3/L4 header data buffer
1390 * @hdr_field - bitfield determining needed headers
1391 * @skb - socket buffer
1392 * @hdr_len - array of header lengths
1393 * @tot_len - total length of data
1394 *
1395 * Reads hdr_field to determine which headers are needed by firmware.
1396 * Builds a buffer containing these headers. Saves individual header
1397 * lengths and total buffer length to be used to build descriptors.
1398 */
1399static int build_hdr_data(u8 hdr_field, struct sk_buff *skb,
1400 int *hdr_len, u8 *hdr_data)
1401{
1402 int len = 0;
1403 u8 *hdr;
1404
Thomas Falconda75e3b2018-03-12 11:51:02 -05001405 if (skb_vlan_tagged(skb) && !skb_vlan_tag_present(skb))
1406 hdr_len[0] = sizeof(struct vlan_ethhdr);
1407 else
1408 hdr_len[0] = sizeof(struct ethhdr);
Thomas Falconad7775d2016-04-01 17:20:34 -05001409
1410 if (skb->protocol == htons(ETH_P_IP)) {
1411 hdr_len[1] = ip_hdr(skb)->ihl * 4;
1412 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
1413 hdr_len[2] = tcp_hdrlen(skb);
1414 else if (ip_hdr(skb)->protocol == IPPROTO_UDP)
1415 hdr_len[2] = sizeof(struct udphdr);
1416 } else if (skb->protocol == htons(ETH_P_IPV6)) {
1417 hdr_len[1] = sizeof(struct ipv6hdr);
1418 if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
1419 hdr_len[2] = tcp_hdrlen(skb);
1420 else if (ipv6_hdr(skb)->nexthdr == IPPROTO_UDP)
1421 hdr_len[2] = sizeof(struct udphdr);
Thomas Falcon4eb50ce2017-12-18 12:52:40 -06001422 } else if (skb->protocol == htons(ETH_P_ARP)) {
1423 hdr_len[1] = arp_hdr_len(skb->dev);
1424 hdr_len[2] = 0;
Thomas Falconad7775d2016-04-01 17:20:34 -05001425 }
1426
1427 memset(hdr_data, 0, 120);
1428 if ((hdr_field >> 6) & 1) {
1429 hdr = skb_mac_header(skb);
1430 memcpy(hdr_data, hdr, hdr_len[0]);
1431 len += hdr_len[0];
1432 }
1433
1434 if ((hdr_field >> 5) & 1) {
1435 hdr = skb_network_header(skb);
1436 memcpy(hdr_data + len, hdr, hdr_len[1]);
1437 len += hdr_len[1];
1438 }
1439
1440 if ((hdr_field >> 4) & 1) {
1441 hdr = skb_transport_header(skb);
1442 memcpy(hdr_data + len, hdr, hdr_len[2]);
1443 len += hdr_len[2];
1444 }
1445 return len;
1446}
1447
1448/**
1449 * create_hdr_descs - create header and header extension descriptors
1450 * @hdr_field - bitfield determining needed headers
1451 * @data - buffer containing header data
1452 * @len - length of data buffer
1453 * @hdr_len - array of individual header lengths
1454 * @scrq_arr - descriptor array
1455 *
1456 * Creates header and, if needed, header extension descriptors and
1457 * places them in a descriptor array, scrq_arr
1458 */
1459
Thomas Falcon2de09682017-10-16 10:02:11 -05001460static int create_hdr_descs(u8 hdr_field, u8 *hdr_data, int len, int *hdr_len,
1461 union sub_crq *scrq_arr)
Thomas Falconad7775d2016-04-01 17:20:34 -05001462{
1463 union sub_crq hdr_desc;
1464 int tmp_len = len;
Thomas Falcon2de09682017-10-16 10:02:11 -05001465 int num_descs = 0;
Thomas Falconad7775d2016-04-01 17:20:34 -05001466 u8 *data, *cur;
1467 int tmp;
1468
1469 while (tmp_len > 0) {
1470 cur = hdr_data + len - tmp_len;
1471
1472 memset(&hdr_desc, 0, sizeof(hdr_desc));
1473 if (cur != hdr_data) {
1474 data = hdr_desc.hdr_ext.data;
1475 tmp = tmp_len > 29 ? 29 : tmp_len;
1476 hdr_desc.hdr_ext.first = IBMVNIC_CRQ_CMD;
1477 hdr_desc.hdr_ext.type = IBMVNIC_HDR_EXT_DESC;
1478 hdr_desc.hdr_ext.len = tmp;
1479 } else {
1480 data = hdr_desc.hdr.data;
1481 tmp = tmp_len > 24 ? 24 : tmp_len;
1482 hdr_desc.hdr.first = IBMVNIC_CRQ_CMD;
1483 hdr_desc.hdr.type = IBMVNIC_HDR_DESC;
1484 hdr_desc.hdr.len = tmp;
1485 hdr_desc.hdr.l2_len = (u8)hdr_len[0];
1486 hdr_desc.hdr.l3_len = cpu_to_be16((u16)hdr_len[1]);
1487 hdr_desc.hdr.l4_len = (u8)hdr_len[2];
1488 hdr_desc.hdr.flag = hdr_field << 1;
1489 }
1490 memcpy(data, cur, tmp);
1491 tmp_len -= tmp;
1492 *scrq_arr = hdr_desc;
1493 scrq_arr++;
Thomas Falcon2de09682017-10-16 10:02:11 -05001494 num_descs++;
Thomas Falconad7775d2016-04-01 17:20:34 -05001495 }
Thomas Falcon2de09682017-10-16 10:02:11 -05001496
1497 return num_descs;
Thomas Falconad7775d2016-04-01 17:20:34 -05001498}
1499
1500/**
1501 * build_hdr_descs_arr - build a header descriptor array
1502 * @skb - socket buffer
1503 * @num_entries - number of descriptors to be sent
1504 * @subcrq - first TX descriptor
1505 * @hdr_field - bit field determining which headers will be sent
1506 *
1507 * This function will build a TX descriptor array with applicable
1508 * L2/L3/L4 packet header descriptors to be sent by send_subcrq_indirect.
1509 */
1510
Thomas Falconc62aa372020-11-18 19:12:20 -06001511static void build_hdr_descs_arr(struct sk_buff *skb,
1512 union sub_crq *indir_arr,
Thomas Falconad7775d2016-04-01 17:20:34 -05001513 int *num_entries, u8 hdr_field)
1514{
1515 int hdr_len[3] = {0, 0, 0};
Thomas Falconc62aa372020-11-18 19:12:20 -06001516 u8 hdr_data[140] = {0};
Thomas Falcon2de09682017-10-16 10:02:11 -05001517 int tot_len;
Thomas Falconad7775d2016-04-01 17:20:34 -05001518
Thomas Falconc62aa372020-11-18 19:12:20 -06001519 tot_len = build_hdr_data(hdr_field, skb, hdr_len,
1520 hdr_data);
Thomas Falcon2de09682017-10-16 10:02:11 -05001521 *num_entries += create_hdr_descs(hdr_field, hdr_data, tot_len, hdr_len,
Thomas Falconc62aa372020-11-18 19:12:20 -06001522 indir_arr + 1);
Thomas Falconad7775d2016-04-01 17:20:34 -05001523}
1524
Thomas Falcon1f247a62018-03-12 11:51:04 -05001525static int ibmvnic_xmit_workarounds(struct sk_buff *skb,
1526 struct net_device *netdev)
1527{
1528 /* For some backing devices, mishandling of small packets
1529 * can result in a loss of connection or TX stall. Device
1530 * architects recommend that no packet should be smaller
1531 * than the minimum MTU value provided to the driver, so
1532 * pad any packets to that length
1533 */
1534 if (skb->len < netdev->min_mtu)
1535 return skb_put_padto(skb, netdev->min_mtu);
Thomas Falcon7083a452018-03-12 21:05:26 -05001536
1537 return 0;
Thomas Falcon1f247a62018-03-12 11:51:04 -05001538}
1539
Thomas Falcon0d973382020-11-18 19:12:19 -06001540static void ibmvnic_tx_scrq_clean_buffer(struct ibmvnic_adapter *adapter,
1541 struct ibmvnic_sub_crq_queue *tx_scrq)
1542{
1543 struct ibmvnic_ind_xmit_queue *ind_bufp;
1544 struct ibmvnic_tx_buff *tx_buff;
1545 struct ibmvnic_tx_pool *tx_pool;
1546 union sub_crq tx_scrq_entry;
1547 int queue_num;
1548 int entries;
1549 int index;
1550 int i;
1551
1552 ind_bufp = &tx_scrq->ind_buf;
1553 entries = (u64)ind_bufp->index;
1554 queue_num = tx_scrq->pool_index;
1555
1556 for (i = entries - 1; i >= 0; --i) {
1557 tx_scrq_entry = ind_bufp->indir_arr[i];
1558 if (tx_scrq_entry.v1.type != IBMVNIC_TX_DESC)
1559 continue;
1560 index = be32_to_cpu(tx_scrq_entry.v1.correlator);
1561 if (index & IBMVNIC_TSO_POOL_MASK) {
1562 tx_pool = &adapter->tso_pool[queue_num];
1563 index &= ~IBMVNIC_TSO_POOL_MASK;
1564 } else {
1565 tx_pool = &adapter->tx_pool[queue_num];
1566 }
1567 tx_pool->free_map[tx_pool->consumer_index] = index;
1568 tx_pool->consumer_index = tx_pool->consumer_index == 0 ?
1569 tx_pool->num_buffers - 1 :
1570 tx_pool->consumer_index - 1;
1571 tx_buff = &tx_pool->tx_buff[index];
1572 adapter->netdev->stats.tx_packets--;
1573 adapter->netdev->stats.tx_bytes -= tx_buff->skb->len;
1574 adapter->tx_stats_buffers[queue_num].packets--;
1575 adapter->tx_stats_buffers[queue_num].bytes -=
1576 tx_buff->skb->len;
1577 dev_kfree_skb_any(tx_buff->skb);
1578 tx_buff->skb = NULL;
1579 adapter->netdev->stats.tx_dropped++;
1580 }
1581 ind_bufp->index = 0;
1582 if (atomic_sub_return(entries, &tx_scrq->used) <=
1583 (adapter->req_tx_entries_per_subcrq / 2) &&
1584 __netif_subqueue_stopped(adapter->netdev, queue_num)) {
1585 netif_wake_subqueue(adapter->netdev, queue_num);
1586 netdev_dbg(adapter->netdev, "Started queue %d\n",
1587 queue_num);
1588 }
1589}
1590
1591static int ibmvnic_tx_scrq_flush(struct ibmvnic_adapter *adapter,
1592 struct ibmvnic_sub_crq_queue *tx_scrq)
1593{
1594 struct ibmvnic_ind_xmit_queue *ind_bufp;
1595 u64 dma_addr;
1596 u64 entries;
1597 u64 handle;
1598 int rc;
1599
1600 ind_bufp = &tx_scrq->ind_buf;
1601 dma_addr = (u64)ind_bufp->indir_dma;
1602 entries = (u64)ind_bufp->index;
1603 handle = tx_scrq->handle;
1604
1605 if (!entries)
1606 return 0;
1607 rc = send_subcrq_indirect(adapter, handle, dma_addr, entries);
1608 if (rc)
1609 ibmvnic_tx_scrq_clean_buffer(adapter, tx_scrq);
1610 else
1611 ind_bufp->index = 0;
1612 return 0;
1613}
1614
YueHaibing94b2bb22018-09-18 14:35:47 +08001615static netdev_tx_t ibmvnic_xmit(struct sk_buff *skb, struct net_device *netdev)
Thomas Falcon032c5e82015-12-21 11:26:06 -06001616{
1617 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1618 int queue_num = skb_get_queue_mapping(skb);
Thomas Falconad7775d2016-04-01 17:20:34 -05001619 u8 *hdrs = (u8 *)&adapter->tx_rx_desc_req;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001620 struct device *dev = &adapter->vdev->dev;
Thomas Falcon0d973382020-11-18 19:12:19 -06001621 struct ibmvnic_ind_xmit_queue *ind_bufp;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001622 struct ibmvnic_tx_buff *tx_buff = NULL;
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001623 struct ibmvnic_sub_crq_queue *tx_scrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001624 struct ibmvnic_tx_pool *tx_pool;
1625 unsigned int tx_send_failed = 0;
Thomas Falcon0d973382020-11-18 19:12:19 -06001626 netdev_tx_t ret = NETDEV_TX_OK;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001627 unsigned int tx_map_failed = 0;
Thomas Falconc62aa372020-11-18 19:12:20 -06001628 union sub_crq indir_arr[16];
Thomas Falcon032c5e82015-12-21 11:26:06 -06001629 unsigned int tx_dropped = 0;
1630 unsigned int tx_packets = 0;
1631 unsigned int tx_bytes = 0;
1632 dma_addr_t data_dma_addr;
1633 struct netdev_queue *txq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001634 unsigned long lpar_rc;
1635 union sub_crq tx_crq;
1636 unsigned int offset;
Thomas Falconad7775d2016-04-01 17:20:34 -05001637 int num_entries = 1;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001638 unsigned char *dst;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001639 int index = 0;
Thomas Falcona0dca102018-01-18 19:29:48 -06001640 u8 proto = 0;
Thomas Falcon0d973382020-11-18 19:12:19 -06001641
1642 tx_scrq = adapter->tx_scrq[queue_num];
1643 txq = netdev_get_tx_queue(netdev, queue_num);
1644 ind_bufp = &tx_scrq->ind_buf;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001645
Juliet Kim7ed5b312019-09-20 16:11:23 -04001646 if (test_bit(0, &adapter->resetting)) {
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001647 if (!netif_subqueue_stopped(netdev, skb))
1648 netif_stop_subqueue(netdev, queue_num);
1649 dev_kfree_skb_any(skb);
1650
Thomas Falcon032c5e82015-12-21 11:26:06 -06001651 tx_send_failed++;
1652 tx_dropped++;
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001653 ret = NETDEV_TX_OK;
Thomas Falcon0d973382020-11-18 19:12:19 -06001654 ibmvnic_tx_scrq_flush(adapter, tx_scrq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001655 goto out;
1656 }
1657
Thomas Falcon7083a452018-03-12 21:05:26 -05001658 if (ibmvnic_xmit_workarounds(skb, netdev)) {
Thomas Falcon1f247a62018-03-12 11:51:04 -05001659 tx_dropped++;
1660 tx_send_failed++;
1661 ret = NETDEV_TX_OK;
Thomas Falcon0d973382020-11-18 19:12:19 -06001662 ibmvnic_tx_scrq_flush(adapter, tx_scrq);
Thomas Falcon1f247a62018-03-12 11:51:04 -05001663 goto out;
1664 }
Thomas Falcon06b3e352018-03-16 20:00:28 -05001665 if (skb_is_gso(skb))
1666 tx_pool = &adapter->tso_pool[queue_num];
1667 else
1668 tx_pool = &adapter->tx_pool[queue_num];
Thomas Falcon1f247a62018-03-12 11:51:04 -05001669
Thomas Falcon032c5e82015-12-21 11:26:06 -06001670 index = tx_pool->free_map[tx_pool->consumer_index];
Thomas Falconfdb06102017-10-17 12:36:55 -05001671
Thomas Falcon86b61a52018-03-16 20:00:29 -05001672 if (index == IBMVNIC_INVALID_MAP) {
1673 dev_kfree_skb_any(skb);
1674 tx_send_failed++;
1675 tx_dropped++;
1676 ret = NETDEV_TX_OK;
Thomas Falcon0d973382020-11-18 19:12:19 -06001677 ibmvnic_tx_scrq_flush(adapter, tx_scrq);
Thomas Falcon86b61a52018-03-16 20:00:29 -05001678 goto out;
1679 }
1680
1681 tx_pool->free_map[tx_pool->consumer_index] = IBMVNIC_INVALID_MAP;
1682
Thomas Falcon06b3e352018-03-16 20:00:28 -05001683 offset = index * tx_pool->buf_size;
1684 dst = tx_pool->long_term_buff.buff + offset;
1685 memset(dst, 0, tx_pool->buf_size);
1686 data_dma_addr = tx_pool->long_term_buff.addr + offset;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001687
Thomas Falcon15482052017-10-17 12:36:54 -05001688 if (skb_shinfo(skb)->nr_frags) {
1689 int cur, i;
1690
1691 /* Copy the head */
1692 skb_copy_from_linear_data(skb, dst, skb_headlen(skb));
1693 cur = skb_headlen(skb);
1694
1695 /* Copy the frags */
1696 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1697 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1698
1699 memcpy(dst + cur,
1700 page_address(skb_frag_page(frag)) +
Jonathan Lemonb54c9d52019-07-30 07:40:33 -07001701 skb_frag_off(frag), skb_frag_size(frag));
Thomas Falcon15482052017-10-17 12:36:54 -05001702 cur += skb_frag_size(frag);
1703 }
1704 } else {
1705 skb_copy_from_linear_data(skb, dst, skb->len);
1706 }
1707
Lijun Pan42557da2021-02-12 20:48:40 -06001708 /* post changes to long_term_buff *dst before VIOS accessing it */
1709 dma_wmb();
1710
Thomas Falcon032c5e82015-12-21 11:26:06 -06001711 tx_pool->consumer_index =
Thomas Falcon06b3e352018-03-16 20:00:28 -05001712 (tx_pool->consumer_index + 1) % tx_pool->num_buffers;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001713
1714 tx_buff = &tx_pool->tx_buff[index];
1715 tx_buff->skb = skb;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001716 tx_buff->index = index;
1717 tx_buff->pool_index = queue_num;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001718
1719 memset(&tx_crq, 0, sizeof(tx_crq));
1720 tx_crq.v1.first = IBMVNIC_CRQ_CMD;
1721 tx_crq.v1.type = IBMVNIC_TX_DESC;
1722 tx_crq.v1.n_crq_elem = 1;
1723 tx_crq.v1.n_sge = 1;
1724 tx_crq.v1.flags1 = IBMVNIC_TX_COMP_NEEDED;
Thomas Falcon06b3e352018-03-16 20:00:28 -05001725
Thomas Falconfdb06102017-10-17 12:36:55 -05001726 if (skb_is_gso(skb))
Thomas Falcon06b3e352018-03-16 20:00:28 -05001727 tx_crq.v1.correlator =
1728 cpu_to_be32(index | IBMVNIC_TSO_POOL_MASK);
Thomas Falconfdb06102017-10-17 12:36:55 -05001729 else
Thomas Falcon06b3e352018-03-16 20:00:28 -05001730 tx_crq.v1.correlator = cpu_to_be32(index);
1731 tx_crq.v1.dma_reg = cpu_to_be16(tx_pool->long_term_buff.map_id);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001732 tx_crq.v1.sge_len = cpu_to_be32(skb->len);
1733 tx_crq.v1.ioba = cpu_to_be64(data_dma_addr);
1734
Michał Mirosławe84b4792018-11-07 17:50:52 +01001735 if (adapter->vlan_header_insertion && skb_vlan_tag_present(skb)) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06001736 tx_crq.v1.flags2 |= IBMVNIC_TX_VLAN_INSERT;
1737 tx_crq.v1.vlan_id = cpu_to_be16(skb->vlan_tci);
1738 }
1739
1740 if (skb->protocol == htons(ETH_P_IP)) {
Thomas Falcona0dca102018-01-18 19:29:48 -06001741 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV4;
1742 proto = ip_hdr(skb)->protocol;
1743 } else if (skb->protocol == htons(ETH_P_IPV6)) {
1744 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV6;
1745 proto = ipv6_hdr(skb)->nexthdr;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001746 }
1747
Thomas Falcona0dca102018-01-18 19:29:48 -06001748 if (proto == IPPROTO_TCP)
1749 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_TCP;
1750 else if (proto == IPPROTO_UDP)
1751 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_UDP;
1752
Thomas Falconad7775d2016-04-01 17:20:34 -05001753 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06001754 tx_crq.v1.flags1 |= IBMVNIC_TX_CHKSUM_OFFLOAD;
Thomas Falconad7775d2016-04-01 17:20:34 -05001755 hdrs += 2;
1756 }
Thomas Falconfdb06102017-10-17 12:36:55 -05001757 if (skb_is_gso(skb)) {
1758 tx_crq.v1.flags1 |= IBMVNIC_TX_LSO;
1759 tx_crq.v1.mss = cpu_to_be16(skb_shinfo(skb)->gso_size);
1760 hdrs += 2;
1761 }
Thomas Falcon0d973382020-11-18 19:12:19 -06001762
1763 if ((*hdrs >> 7) & 1)
Thomas Falconc62aa372020-11-18 19:12:20 -06001764 build_hdr_descs_arr(skb, indir_arr, &num_entries, *hdrs);
Thomas Falcon0d973382020-11-18 19:12:19 -06001765
1766 tx_crq.v1.n_crq_elem = num_entries;
1767 tx_buff->num_entries = num_entries;
1768 /* flush buffer if current entry can not fit */
1769 if (num_entries + ind_bufp->index > IBMVNIC_MAX_IND_DESCS) {
1770 lpar_rc = ibmvnic_tx_scrq_flush(adapter, tx_scrq);
1771 if (lpar_rc != H_SUCCESS)
1772 goto tx_flush_err;
Thomas Falconad7775d2016-04-01 17:20:34 -05001773 }
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001774
Thomas Falconc62aa372020-11-18 19:12:20 -06001775 indir_arr[0] = tx_crq;
1776 memcpy(&ind_bufp->indir_arr[ind_bufp->index], &indir_arr[0],
Thomas Falcon0d973382020-11-18 19:12:19 -06001777 num_entries * sizeof(struct ibmvnic_generic_scrq));
1778 ind_bufp->index += num_entries;
1779 if (__netdev_tx_sent_queue(txq, skb->len,
1780 netdev_xmit_more() &&
1781 ind_bufp->index < IBMVNIC_MAX_IND_DESCS)) {
1782 lpar_rc = ibmvnic_tx_scrq_flush(adapter, tx_scrq);
1783 if (lpar_rc != H_SUCCESS)
1784 goto tx_err;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001785 }
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001786
Thomas Falconffc385b2018-02-18 10:08:41 -06001787 if (atomic_add_return(num_entries, &tx_scrq->used)
Brian King58c8c0c2017-04-19 13:44:47 -04001788 >= adapter->req_tx_entries_per_subcrq) {
Thomas Falcon0aecb132018-02-26 18:10:58 -06001789 netdev_dbg(netdev, "Stopping queue %d\n", queue_num);
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001790 netif_stop_subqueue(netdev, queue_num);
1791 }
1792
Thomas Falcon032c5e82015-12-21 11:26:06 -06001793 tx_packets++;
1794 tx_bytes += skb->len;
1795 txq->trans_start = jiffies;
1796 ret = NETDEV_TX_OK;
Thomas Falcon86b61a52018-03-16 20:00:29 -05001797 goto out;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001798
Thomas Falcon0d973382020-11-18 19:12:19 -06001799tx_flush_err:
1800 dev_kfree_skb_any(skb);
1801 tx_buff->skb = NULL;
1802 tx_pool->consumer_index = tx_pool->consumer_index == 0 ?
1803 tx_pool->num_buffers - 1 :
1804 tx_pool->consumer_index - 1;
1805 tx_dropped++;
1806tx_err:
1807 if (lpar_rc != H_CLOSED && lpar_rc != H_PARAMETER)
1808 dev_err_ratelimited(dev, "tx: send failed\n");
1809
1810 if (lpar_rc == H_CLOSED || adapter->failover_pending) {
1811 /* Disable TX and report carrier off if queue is closed
1812 * or pending failover.
1813 * Firmware guarantees that a signal will be sent to the
1814 * driver, triggering a reset or some other action.
1815 */
1816 netif_tx_stop_all_queues(netdev);
1817 netif_carrier_off(netdev);
1818 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001819out:
1820 netdev->stats.tx_dropped += tx_dropped;
1821 netdev->stats.tx_bytes += tx_bytes;
1822 netdev->stats.tx_packets += tx_packets;
1823 adapter->tx_send_failed += tx_send_failed;
1824 adapter->tx_map_failed += tx_map_failed;
John Allen3d52b592017-08-02 16:44:14 -05001825 adapter->tx_stats_buffers[queue_num].packets += tx_packets;
1826 adapter->tx_stats_buffers[queue_num].bytes += tx_bytes;
1827 adapter->tx_stats_buffers[queue_num].dropped_packets += tx_dropped;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001828
1829 return ret;
1830}
1831
1832static void ibmvnic_set_multi(struct net_device *netdev)
1833{
1834 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1835 struct netdev_hw_addr *ha;
1836 union ibmvnic_crq crq;
1837
1838 memset(&crq, 0, sizeof(crq));
1839 crq.request_capability.first = IBMVNIC_CRQ_CMD;
1840 crq.request_capability.cmd = REQUEST_CAPABILITY;
1841
1842 if (netdev->flags & IFF_PROMISC) {
1843 if (!adapter->promisc_supported)
1844 return;
1845 } else {
1846 if (netdev->flags & IFF_ALLMULTI) {
1847 /* Accept all multicast */
1848 memset(&crq, 0, sizeof(crq));
1849 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1850 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1851 crq.multicast_ctrl.flags = IBMVNIC_ENABLE_ALL;
1852 ibmvnic_send_crq(adapter, &crq);
1853 } else if (netdev_mc_empty(netdev)) {
1854 /* Reject all multicast */
1855 memset(&crq, 0, sizeof(crq));
1856 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1857 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1858 crq.multicast_ctrl.flags = IBMVNIC_DISABLE_ALL;
1859 ibmvnic_send_crq(adapter, &crq);
1860 } else {
1861 /* Accept one or more multicast(s) */
1862 netdev_for_each_mc_addr(ha, netdev) {
1863 memset(&crq, 0, sizeof(crq));
1864 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1865 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1866 crq.multicast_ctrl.flags = IBMVNIC_ENABLE_MC;
1867 ether_addr_copy(&crq.multicast_ctrl.mac_addr[0],
1868 ha->addr);
1869 ibmvnic_send_crq(adapter, &crq);
1870 }
1871 }
1872 }
1873}
1874
Thomas Falcon62740e92019-05-09 23:13:43 -05001875static int __ibmvnic_set_mac(struct net_device *netdev, u8 *dev_addr)
Thomas Falcon032c5e82015-12-21 11:26:06 -06001876{
1877 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001878 union ibmvnic_crq crq;
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05001879 int rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001880
Thomas Falcon62740e92019-05-09 23:13:43 -05001881 if (!is_valid_ether_addr(dev_addr)) {
1882 rc = -EADDRNOTAVAIL;
1883 goto err;
1884 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001885
1886 memset(&crq, 0, sizeof(crq));
1887 crq.change_mac_addr.first = IBMVNIC_CRQ_CMD;
1888 crq.change_mac_addr.cmd = CHANGE_MAC_ADDR;
Thomas Falcon62740e92019-05-09 23:13:43 -05001889 ether_addr_copy(&crq.change_mac_addr.mac_addr[0], dev_addr);
Thomas Falconf8136142018-01-29 13:45:05 -06001890
Thomas Falconff25dcb2019-11-25 17:12:56 -06001891 mutex_lock(&adapter->fw_lock);
1892 adapter->fw_done_rc = 0;
Thomas Falcon070eca92019-11-25 17:12:53 -06001893 reinit_completion(&adapter->fw_done);
Thomas Falconff25dcb2019-11-25 17:12:56 -06001894
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05001895 rc = ibmvnic_send_crq(adapter, &crq);
Thomas Falcon62740e92019-05-09 23:13:43 -05001896 if (rc) {
1897 rc = -EIO;
Thomas Falconff25dcb2019-11-25 17:12:56 -06001898 mutex_unlock(&adapter->fw_lock);
Thomas Falcon62740e92019-05-09 23:13:43 -05001899 goto err;
1900 }
1901
Thomas Falcon476d96c2019-11-25 17:12:55 -06001902 rc = ibmvnic_wait_for_completion(adapter, &adapter->fw_done, 10000);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001903 /* netdev->dev_addr is changed in handle_change_mac_rsp function */
Thomas Falcon476d96c2019-11-25 17:12:55 -06001904 if (rc || adapter->fw_done_rc) {
Thomas Falcon62740e92019-05-09 23:13:43 -05001905 rc = -EIO;
Thomas Falconff25dcb2019-11-25 17:12:56 -06001906 mutex_unlock(&adapter->fw_lock);
Thomas Falcon62740e92019-05-09 23:13:43 -05001907 goto err;
1908 }
Thomas Falconff25dcb2019-11-25 17:12:56 -06001909 mutex_unlock(&adapter->fw_lock);
Thomas Falcon62740e92019-05-09 23:13:43 -05001910 return 0;
1911err:
1912 ether_addr_copy(adapter->mac_addr, netdev->dev_addr);
1913 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001914}
1915
John Allenc26eba02017-10-26 16:23:25 -05001916static int ibmvnic_set_mac(struct net_device *netdev, void *p)
1917{
1918 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1919 struct sockaddr *addr = p;
Thomas Falconf8136142018-01-29 13:45:05 -06001920 int rc;
John Allenc26eba02017-10-26 16:23:25 -05001921
Thomas Falcon62740e92019-05-09 23:13:43 -05001922 rc = 0;
Lijun Pan8fc36722020-10-27 17:04:56 -05001923 if (!is_valid_ether_addr(addr->sa_data))
1924 return -EADDRNOTAVAIL;
1925
1926 if (adapter->state != VNIC_PROBED) {
1927 ether_addr_copy(adapter->mac_addr, addr->sa_data);
Thomas Falcon62740e92019-05-09 23:13:43 -05001928 rc = __ibmvnic_set_mac(netdev, addr->sa_data);
Lijun Pan8fc36722020-10-27 17:04:56 -05001929 }
John Allenc26eba02017-10-26 16:23:25 -05001930
Thomas Falconf8136142018-01-29 13:45:05 -06001931 return rc;
John Allenc26eba02017-10-26 16:23:25 -05001932}
1933
Nathan Fontenoted651a12017-05-03 14:04:38 -04001934/**
Juliet Kimb27507b2019-09-20 16:11:22 -04001935 * do_change_param_reset returns zero if we are able to keep processing reset
1936 * events, or non-zero if we hit a fatal error and must halt.
1937 */
1938static int do_change_param_reset(struct ibmvnic_adapter *adapter,
1939 struct ibmvnic_rwi *rwi,
1940 u32 reset_state)
1941{
1942 struct net_device *netdev = adapter->netdev;
1943 int i, rc;
1944
1945 netdev_dbg(adapter->netdev, "Change param resetting driver (%d)\n",
1946 rwi->reset_reason);
1947
1948 netif_carrier_off(netdev);
1949 adapter->reset_reason = rwi->reset_reason;
1950
1951 ibmvnic_cleanup(netdev);
1952
1953 if (reset_state == VNIC_OPEN) {
1954 rc = __ibmvnic_close(netdev);
1955 if (rc)
Dany Madden0cb4bc62020-11-25 18:04:27 -06001956 goto out;
Juliet Kimb27507b2019-09-20 16:11:22 -04001957 }
1958
1959 release_resources(adapter);
1960 release_sub_crqs(adapter, 1);
1961 release_crq_queue(adapter);
1962
1963 adapter->state = VNIC_PROBED;
1964
1965 rc = init_crq_queue(adapter);
1966
1967 if (rc) {
1968 netdev_err(adapter->netdev,
1969 "Couldn't initialize crq. rc=%d\n", rc);
1970 return rc;
1971 }
1972
Lijun Pan635e4422020-08-19 17:52:26 -05001973 rc = ibmvnic_reset_init(adapter, true);
Dany Madden0cb4bc62020-11-25 18:04:27 -06001974 if (rc) {
1975 rc = IBMVNIC_INIT_FAILED;
1976 goto out;
1977 }
Juliet Kimb27507b2019-09-20 16:11:22 -04001978
1979 /* If the adapter was in PROBE state prior to the reset,
1980 * exit here.
1981 */
1982 if (reset_state == VNIC_PROBED)
Dany Madden0cb4bc62020-11-25 18:04:27 -06001983 goto out;
Juliet Kimb27507b2019-09-20 16:11:22 -04001984
1985 rc = ibmvnic_login(netdev);
1986 if (rc) {
Dany Madden0cb4bc62020-11-25 18:04:27 -06001987 goto out;
Juliet Kimb27507b2019-09-20 16:11:22 -04001988 }
1989
1990 rc = init_resources(adapter);
1991 if (rc)
Dany Madden0cb4bc62020-11-25 18:04:27 -06001992 goto out;
Juliet Kimb27507b2019-09-20 16:11:22 -04001993
1994 ibmvnic_disable_irqs(adapter);
1995
1996 adapter->state = VNIC_CLOSED;
1997
1998 if (reset_state == VNIC_CLOSED)
1999 return 0;
2000
2001 rc = __ibmvnic_open(netdev);
Dany Madden0cb4bc62020-11-25 18:04:27 -06002002 if (rc) {
2003 rc = IBMVNIC_OPEN_FAILED;
2004 goto out;
2005 }
Juliet Kimb27507b2019-09-20 16:11:22 -04002006
2007 /* refresh device's multicast list */
2008 ibmvnic_set_multi(netdev);
2009
2010 /* kick napi */
2011 for (i = 0; i < adapter->req_rx_queues; i++)
2012 napi_schedule(&adapter->napi[i]);
2013
Dany Madden0cb4bc62020-11-25 18:04:27 -06002014out:
2015 if (rc)
2016 adapter->state = reset_state;
2017 return rc;
Juliet Kimb27507b2019-09-20 16:11:22 -04002018}
2019
2020/**
Nathan Fontenoted651a12017-05-03 14:04:38 -04002021 * do_reset returns zero if we are able to keep processing reset events, or
2022 * non-zero if we hit a fatal error and must halt.
2023 */
2024static int do_reset(struct ibmvnic_adapter *adapter,
2025 struct ibmvnic_rwi *rwi, u32 reset_state)
2026{
John Allen896d8692018-01-18 16:26:31 -06002027 u64 old_num_rx_queues, old_num_tx_queues;
Thomas Falcon5bf032e2018-11-21 11:17:59 -06002028 u64 old_num_rx_slots, old_num_tx_slots;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002029 struct net_device *netdev = adapter->netdev;
2030 int i, rc;
2031
Sukadev Bhattiprolu38bd5ce2020-12-04 18:22:35 -08002032 netdev_dbg(adapter->netdev,
2033 "[S:%d FOP:%d] Reset reason %d, reset_state %d\n",
2034 adapter->state, adapter->failover_pending,
2035 rwi->reset_reason, reset_state);
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002036
Juliet Kimb27507b2019-09-20 16:11:22 -04002037 rtnl_lock();
Sukadev Bhattiprolu1d850492020-10-30 10:07:11 -07002038 /*
2039 * Now that we have the rtnl lock, clear any pending failover.
2040 * This will ensure ibmvnic_open() has either completed or will
2041 * block until failover is complete.
2042 */
2043 if (rwi->reset_reason == VNIC_RESET_FAILOVER)
2044 adapter->failover_pending = false;
Juliet Kimb27507b2019-09-20 16:11:22 -04002045
Nathan Fontenoted651a12017-05-03 14:04:38 -04002046 netif_carrier_off(netdev);
2047 adapter->reset_reason = rwi->reset_reason;
2048
John Allen896d8692018-01-18 16:26:31 -06002049 old_num_rx_queues = adapter->req_rx_queues;
2050 old_num_tx_queues = adapter->req_tx_queues;
Thomas Falcon5bf032e2018-11-21 11:17:59 -06002051 old_num_rx_slots = adapter->req_rx_add_entries_per_subcrq;
2052 old_num_tx_slots = adapter->req_tx_entries_per_subcrq;
John Allen896d8692018-01-18 16:26:31 -06002053
Nathan Fontenot30f79622018-04-06 18:37:06 -05002054 ibmvnic_cleanup(netdev);
2055
Thomas Falcon1f946082019-06-07 16:03:53 -05002056 if (reset_state == VNIC_OPEN &&
2057 adapter->reset_reason != VNIC_RESET_MOBILITY &&
Nathan Fontenot30f79622018-04-06 18:37:06 -05002058 adapter->reset_reason != VNIC_RESET_FAILOVER) {
Juliet Kimb27507b2019-09-20 16:11:22 -04002059 adapter->state = VNIC_CLOSING;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002060
Juliet Kimb27507b2019-09-20 16:11:22 -04002061 /* Release the RTNL lock before link state change and
2062 * re-acquire after the link state change to allow
2063 * linkwatch_event to grab the RTNL lock and run during
2064 * a reset.
2065 */
2066 rtnl_unlock();
2067 rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_DN);
2068 rtnl_lock();
2069 if (rc)
2070 goto out;
2071
2072 if (adapter->state != VNIC_CLOSING) {
2073 rc = -1;
2074 goto out;
2075 }
2076
2077 adapter->state = VNIC_CLOSED;
John Allenc26eba02017-10-26 16:23:25 -05002078 }
2079
John Allen8cb31cf2017-05-26 10:30:37 -04002080 if (adapter->reset_reason != VNIC_RESET_NON_FATAL) {
2081 /* remove the closed state so when we call open it appears
2082 * we are coming from the probed state.
2083 */
Nathan Fontenoted651a12017-05-03 14:04:38 -04002084 adapter->state = VNIC_PROBED;
John Allen8cb31cf2017-05-26 10:30:37 -04002085
Juliet Kimb27507b2019-09-20 16:11:22 -04002086 if (adapter->reset_reason == VNIC_RESET_MOBILITY) {
Nathan Fontenot30f79622018-04-06 18:37:06 -05002087 rc = ibmvnic_reenable_crq_queue(adapter);
2088 release_sub_crqs(adapter, 1);
2089 } else {
2090 rc = ibmvnic_reset_crq(adapter);
Dany Madden8b40eb732020-06-18 15:24:13 -04002091 if (rc == H_CLOSED || rc == H_SUCCESS) {
Nathan Fontenot30f79622018-04-06 18:37:06 -05002092 rc = vio_enable_interrupts(adapter->vdev);
Dany Madden8b40eb732020-06-18 15:24:13 -04002093 if (rc)
2094 netdev_err(adapter->netdev,
2095 "Reset failed to enable interrupts. rc=%d\n",
2096 rc);
2097 }
Nathan Fontenot30f79622018-04-06 18:37:06 -05002098 }
2099
2100 if (rc) {
2101 netdev_err(adapter->netdev,
Dany Madden8b40eb732020-06-18 15:24:13 -04002102 "Reset couldn't initialize crq. rc=%d\n", rc);
Juliet Kimb27507b2019-09-20 16:11:22 -04002103 goto out;
Nathan Fontenot30f79622018-04-06 18:37:06 -05002104 }
2105
Lijun Pan635e4422020-08-19 17:52:26 -05002106 rc = ibmvnic_reset_init(adapter, true);
Juliet Kimb27507b2019-09-20 16:11:22 -04002107 if (rc) {
2108 rc = IBMVNIC_INIT_FAILED;
2109 goto out;
2110 }
John Allen8cb31cf2017-05-26 10:30:37 -04002111
2112 /* If the adapter was in PROBE state prior to the reset,
2113 * exit here.
2114 */
Juliet Kimb27507b2019-09-20 16:11:22 -04002115 if (reset_state == VNIC_PROBED) {
2116 rc = 0;
2117 goto out;
2118 }
John Allen8cb31cf2017-05-26 10:30:37 -04002119
2120 rc = ibmvnic_login(netdev);
2121 if (rc) {
Juliet Kimb27507b2019-09-20 16:11:22 -04002122 goto out;
John Allen8cb31cf2017-05-26 10:30:37 -04002123 }
2124
Juliet Kimb27507b2019-09-20 16:11:22 -04002125 if (adapter->req_rx_queues != old_num_rx_queues ||
2126 adapter->req_tx_queues != old_num_tx_queues ||
2127 adapter->req_rx_add_entries_per_subcrq !=
2128 old_num_rx_slots ||
2129 adapter->req_tx_entries_per_subcrq !=
Mingming Cao9f134572020-08-25 13:26:41 -04002130 old_num_tx_slots ||
2131 !adapter->rx_pool ||
2132 !adapter->tso_pool ||
2133 !adapter->tx_pool) {
John Allen896d8692018-01-18 16:26:31 -06002134 release_rx_pools(adapter);
2135 release_tx_pools(adapter);
Juliet Kima5681e22018-11-19 15:59:22 -06002136 release_napi(adapter);
2137 release_vpd_data(adapter);
2138
2139 rc = init_resources(adapter);
Thomas Falconf611a5b2018-08-30 13:19:53 -05002140 if (rc)
Juliet Kimb27507b2019-09-20 16:11:22 -04002141 goto out;
Nathan Fontenotd9043c12018-02-19 13:30:14 -06002142
John Allenc26eba02017-10-26 16:23:25 -05002143 } else {
2144 rc = reset_tx_pools(adapter);
Jakub Kicinski8ae4dff2020-09-04 21:07:49 -07002145 if (rc) {
Mingming Cao9f134572020-08-25 13:26:41 -04002146 netdev_dbg(adapter->netdev, "reset tx pools failed (%d)\n",
2147 rc);
Juliet Kimb27507b2019-09-20 16:11:22 -04002148 goto out;
Jakub Kicinski8ae4dff2020-09-04 21:07:49 -07002149 }
Nathan Fontenot8c0543a2017-05-26 10:31:06 -04002150
John Allenc26eba02017-10-26 16:23:25 -05002151 rc = reset_rx_pools(adapter);
Jakub Kicinski8ae4dff2020-09-04 21:07:49 -07002152 if (rc) {
Mingming Cao9f134572020-08-25 13:26:41 -04002153 netdev_dbg(adapter->netdev, "reset rx pools failed (%d)\n",
2154 rc);
Juliet Kimb27507b2019-09-20 16:11:22 -04002155 goto out;
Jakub Kicinski8ae4dff2020-09-04 21:07:49 -07002156 }
John Allenc26eba02017-10-26 16:23:25 -05002157 }
Thomas Falcon134bbe72018-05-16 15:49:04 -05002158 ibmvnic_disable_irqs(adapter);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002159 }
John Allene676d812018-03-14 10:41:29 -05002160 adapter->state = VNIC_CLOSED;
2161
Juliet Kimb27507b2019-09-20 16:11:22 -04002162 if (reset_state == VNIC_CLOSED) {
2163 rc = 0;
2164 goto out;
2165 }
John Allene676d812018-03-14 10:41:29 -05002166
Nathan Fontenoted651a12017-05-03 14:04:38 -04002167 rc = __ibmvnic_open(netdev);
2168 if (rc) {
Juliet Kimb27507b2019-09-20 16:11:22 -04002169 rc = IBMVNIC_OPEN_FAILED;
2170 goto out;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002171 }
2172
Thomas Falconbe32a242019-06-07 16:03:54 -05002173 /* refresh device's multicast list */
2174 ibmvnic_set_multi(netdev);
2175
Nathan Fontenoted651a12017-05-03 14:04:38 -04002176 /* kick napi */
2177 for (i = 0; i < adapter->req_rx_queues; i++)
2178 napi_schedule(&adapter->napi[i]);
2179
Lijun Pan98025bc2020-11-20 16:40:12 -06002180 if (adapter->reset_reason == VNIC_RESET_FAILOVER ||
Lijun Pan6be46662020-12-14 15:19:29 -06002181 adapter->reset_reason == VNIC_RESET_MOBILITY)
2182 __netdev_notify_peers(netdev);
Nathan Fontenot61d3e1d2017-06-12 20:47:45 -04002183
Juliet Kimb27507b2019-09-20 16:11:22 -04002184 rc = 0;
2185
2186out:
Dany Madden0cb4bc62020-11-25 18:04:27 -06002187 /* restore the adapter state if reset failed */
2188 if (rc)
2189 adapter->state = reset_state;
Juliet Kimb27507b2019-09-20 16:11:22 -04002190 rtnl_unlock();
2191
Sukadev Bhattiprolu38bd5ce2020-12-04 18:22:35 -08002192 netdev_dbg(adapter->netdev, "[S:%d FOP:%d] Reset done, rc %d\n",
2193 adapter->state, adapter->failover_pending, rc);
Juliet Kimb27507b2019-09-20 16:11:22 -04002194 return rc;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002195}
2196
Thomas Falcon2770a792018-05-23 13:38:02 -05002197static int do_hard_reset(struct ibmvnic_adapter *adapter,
2198 struct ibmvnic_rwi *rwi, u32 reset_state)
2199{
2200 struct net_device *netdev = adapter->netdev;
2201 int rc;
2202
2203 netdev_dbg(adapter->netdev, "Hard resetting driver (%d)\n",
2204 rwi->reset_reason);
2205
2206 netif_carrier_off(netdev);
2207 adapter->reset_reason = rwi->reset_reason;
2208
2209 ibmvnic_cleanup(netdev);
2210 release_resources(adapter);
2211 release_sub_crqs(adapter, 0);
2212 release_crq_queue(adapter);
2213
2214 /* remove the closed state so when we call open it appears
2215 * we are coming from the probed state.
2216 */
2217 adapter->state = VNIC_PROBED;
2218
Thomas Falconbbd669a2019-04-04 18:58:26 -05002219 reinit_completion(&adapter->init_done);
Thomas Falcon2770a792018-05-23 13:38:02 -05002220 rc = init_crq_queue(adapter);
2221 if (rc) {
2222 netdev_err(adapter->netdev,
2223 "Couldn't initialize crq. rc=%d\n", rc);
Dany Madden0cb4bc62020-11-25 18:04:27 -06002224 goto out;
Thomas Falcon2770a792018-05-23 13:38:02 -05002225 }
2226
Lijun Pan635e4422020-08-19 17:52:26 -05002227 rc = ibmvnic_reset_init(adapter, false);
Thomas Falcon2770a792018-05-23 13:38:02 -05002228 if (rc)
Dany Madden0cb4bc62020-11-25 18:04:27 -06002229 goto out;
Thomas Falcon2770a792018-05-23 13:38:02 -05002230
2231 /* If the adapter was in PROBE state prior to the reset,
2232 * exit here.
2233 */
2234 if (reset_state == VNIC_PROBED)
Dany Madden0cb4bc62020-11-25 18:04:27 -06002235 goto out;
Thomas Falcon2770a792018-05-23 13:38:02 -05002236
2237 rc = ibmvnic_login(netdev);
Dany Madden0cb4bc62020-11-25 18:04:27 -06002238 if (rc)
2239 goto out;
Juliet Kima5681e22018-11-19 15:59:22 -06002240
2241 rc = init_resources(adapter);
Thomas Falcon2770a792018-05-23 13:38:02 -05002242 if (rc)
Dany Madden0cb4bc62020-11-25 18:04:27 -06002243 goto out;
Thomas Falcon2770a792018-05-23 13:38:02 -05002244
2245 ibmvnic_disable_irqs(adapter);
2246 adapter->state = VNIC_CLOSED;
2247
2248 if (reset_state == VNIC_CLOSED)
Dany Madden0cb4bc62020-11-25 18:04:27 -06002249 goto out;
Thomas Falcon2770a792018-05-23 13:38:02 -05002250
2251 rc = __ibmvnic_open(netdev);
Dany Madden0cb4bc62020-11-25 18:04:27 -06002252 if (rc) {
2253 rc = IBMVNIC_OPEN_FAILED;
2254 goto out;
2255 }
Thomas Falcon2770a792018-05-23 13:38:02 -05002256
Lijun Pan6be46662020-12-14 15:19:29 -06002257 __netdev_notify_peers(netdev);
Dany Madden0cb4bc62020-11-25 18:04:27 -06002258out:
2259 /* restore adapter state if reset failed */
2260 if (rc)
2261 adapter->state = reset_state;
Sukadev Bhattiprolu38bd5ce2020-12-04 18:22:35 -08002262 netdev_dbg(adapter->netdev, "[S:%d FOP:%d] Hard reset done, rc %d\n",
2263 adapter->state, adapter->failover_pending, rc);
Dany Madden0cb4bc62020-11-25 18:04:27 -06002264 return rc;
Thomas Falcon2770a792018-05-23 13:38:02 -05002265}
2266
Nathan Fontenoted651a12017-05-03 14:04:38 -04002267static struct ibmvnic_rwi *get_next_rwi(struct ibmvnic_adapter *adapter)
2268{
2269 struct ibmvnic_rwi *rwi;
Thomas Falcon6c5c7482018-12-10 15:22:22 -06002270 unsigned long flags;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002271
Thomas Falcon6c5c7482018-12-10 15:22:22 -06002272 spin_lock_irqsave(&adapter->rwi_lock, flags);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002273
2274 if (!list_empty(&adapter->rwi_list)) {
2275 rwi = list_first_entry(&adapter->rwi_list, struct ibmvnic_rwi,
2276 list);
2277 list_del(&rwi->list);
2278 } else {
2279 rwi = NULL;
2280 }
2281
Thomas Falcon6c5c7482018-12-10 15:22:22 -06002282 spin_unlock_irqrestore(&adapter->rwi_lock, flags);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002283 return rwi;
2284}
2285
Nathan Fontenoted651a12017-05-03 14:04:38 -04002286static void __ibmvnic_reset(struct work_struct *work)
2287{
2288 struct ibmvnic_rwi *rwi;
2289 struct ibmvnic_adapter *adapter;
Juliet Kim7d7195a2020-03-10 09:23:58 -05002290 bool saved_state = false;
2291 unsigned long flags;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002292 u32 reset_state;
John Allenc26eba02017-10-26 16:23:25 -05002293 int rc = 0;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002294
2295 adapter = container_of(work, struct ibmvnic_adapter, ibmvnic_reset);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002296
Juliet Kim7ed5b312019-09-20 16:11:23 -04002297 if (test_and_set_bit_lock(0, &adapter->resetting)) {
2298 schedule_delayed_work(&adapter->ibmvnic_delayed_reset,
2299 IBMVNIC_RESET_DELAY);
2300 return;
2301 }
2302
Nathan Fontenoted651a12017-05-03 14:04:38 -04002303 rwi = get_next_rwi(adapter);
2304 while (rwi) {
Juliet Kim7d7195a2020-03-10 09:23:58 -05002305 spin_lock_irqsave(&adapter->state_lock, flags);
2306
Thomas Falcon36f10312019-08-27 11:10:04 -05002307 if (adapter->state == VNIC_REMOVING ||
Michal Suchanekc8dc5592019-09-09 22:44:51 +02002308 adapter->state == VNIC_REMOVED) {
Juliet Kim7d7195a2020-03-10 09:23:58 -05002309 spin_unlock_irqrestore(&adapter->state_lock, flags);
Juliet Kim1c2977c2019-09-05 17:30:01 -04002310 kfree(rwi);
2311 rc = EBUSY;
2312 break;
2313 }
Thomas Falcon36f10312019-08-27 11:10:04 -05002314
Juliet Kim7d7195a2020-03-10 09:23:58 -05002315 if (!saved_state) {
2316 reset_state = adapter->state;
Juliet Kim7d7195a2020-03-10 09:23:58 -05002317 saved_state = true;
2318 }
2319 spin_unlock_irqrestore(&adapter->state_lock, flags);
2320
Juliet Kimb27507b2019-09-20 16:11:22 -04002321 if (rwi->reset_reason == VNIC_RESET_CHANGE_PARAM) {
2322 /* CHANGE_PARAM requestor holds rtnl_lock */
2323 rc = do_change_param_reset(adapter, rwi, reset_state);
2324 } else if (adapter->force_reset_recovery) {
Sukadev Bhattiprolu1d850492020-10-30 10:07:11 -07002325 /*
2326 * Since we are doing a hard reset now, clear the
2327 * failover_pending flag so we don't ignore any
2328 * future MOBILITY or other resets.
2329 */
2330 adapter->failover_pending = false;
2331
Juliet Kimb27507b2019-09-20 16:11:22 -04002332 /* Transport event occurred during previous reset */
2333 if (adapter->wait_for_reset) {
2334 /* Previous was CHANGE_PARAM; caller locked */
2335 adapter->force_reset_recovery = false;
2336 rc = do_hard_reset(adapter, rwi, reset_state);
2337 } else {
2338 rtnl_lock();
2339 adapter->force_reset_recovery = false;
2340 rc = do_hard_reset(adapter, rwi, reset_state);
2341 rtnl_unlock();
2342 }
Sukadev Bhattiproluf15fde92020-11-25 18:04:28 -06002343 if (rc) {
2344 /* give backing device time to settle down */
2345 netdev_dbg(adapter->netdev,
2346 "[S:%d] Hard reset failed, waiting 60 secs\n",
2347 adapter->state);
2348 set_current_state(TASK_UNINTERRUPTIBLE);
2349 schedule_timeout(60 * HZ);
2350 }
Lijun Pan1f45dc22020-12-23 14:49:04 -06002351 } else {
Thomas Falcon2770a792018-05-23 13:38:02 -05002352 rc = do_reset(adapter, rwi, reset_state);
2353 }
Nathan Fontenoted651a12017-05-03 14:04:38 -04002354 kfree(rwi);
Dany Maddena86d5c62020-11-25 18:04:31 -06002355 adapter->last_reset_time = jiffies;
Dany Madden0cb4bc62020-11-25 18:04:27 -06002356
Dany Madden18f141b2020-11-25 18:04:25 -06002357 if (rc)
2358 netdev_dbg(adapter->netdev, "Reset failed, rc=%d\n", rc);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002359
2360 rwi = get_next_rwi(adapter);
Juliet Kim7ed5b312019-09-20 16:11:23 -04002361
2362 if (rwi && (rwi->reset_reason == VNIC_RESET_FAILOVER ||
2363 rwi->reset_reason == VNIC_RESET_MOBILITY))
2364 adapter->force_reset_recovery = true;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002365 }
2366
John Allenc26eba02017-10-26 16:23:25 -05002367 if (adapter->wait_for_reset) {
John Allenc26eba02017-10-26 16:23:25 -05002368 adapter->reset_done_rc = rc;
2369 complete(&adapter->reset_done);
2370 }
2371
Juliet Kim7ed5b312019-09-20 16:11:23 -04002372 clear_bit_unlock(0, &adapter->resetting);
Sukadev Bhattiprolu38bd5ce2020-12-04 18:22:35 -08002373
2374 netdev_dbg(adapter->netdev,
2375 "[S:%d FRR:%d WFR:%d] Done processing resets\n",
2376 adapter->state, adapter->force_reset_recovery,
2377 adapter->wait_for_reset);
Juliet Kim7ed5b312019-09-20 16:11:23 -04002378}
2379
2380static void __ibmvnic_delayed_reset(struct work_struct *work)
2381{
2382 struct ibmvnic_adapter *adapter;
2383
2384 adapter = container_of(work, struct ibmvnic_adapter,
2385 ibmvnic_delayed_reset.work);
2386 __ibmvnic_reset(&adapter->ibmvnic_reset);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002387}
2388
Thomas Falconaf894d22018-04-06 18:37:04 -05002389static int ibmvnic_reset(struct ibmvnic_adapter *adapter,
2390 enum ibmvnic_reset_reason reason)
Nathan Fontenoted651a12017-05-03 14:04:38 -04002391{
Thomas Falcon2770a792018-05-23 13:38:02 -05002392 struct list_head *entry, *tmp_entry;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002393 struct ibmvnic_rwi *rwi, *tmp;
2394 struct net_device *netdev = adapter->netdev;
Thomas Falcon6c5c7482018-12-10 15:22:22 -06002395 unsigned long flags;
Thomas Falconaf894d22018-04-06 18:37:04 -05002396 int ret;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002397
Sukadev Bhattiprolu1d850492020-10-30 10:07:11 -07002398 /*
2399 * If failover is pending don't schedule any other reset.
2400 * Instead let the failover complete. If there is already a
2401 * a failover reset scheduled, we will detect and drop the
2402 * duplicate reset when walking the ->rwi_list below.
2403 */
Nathan Fontenoted651a12017-05-03 14:04:38 -04002404 if (adapter->state == VNIC_REMOVING ||
Thomas Falcon5a18e1e2018-04-06 18:37:05 -05002405 adapter->state == VNIC_REMOVED ||
Sukadev Bhattiprolu1d850492020-10-30 10:07:11 -07002406 (adapter->failover_pending && reason != VNIC_RESET_FAILOVER)) {
Thomas Falconaf894d22018-04-06 18:37:04 -05002407 ret = EBUSY;
Thomas Falcon5a18e1e2018-04-06 18:37:05 -05002408 netdev_dbg(netdev, "Adapter removing or pending failover, skipping reset\n");
Thomas Falconaf894d22018-04-06 18:37:04 -05002409 goto err;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002410 }
2411
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04002412 if (adapter->state == VNIC_PROBING) {
2413 netdev_warn(netdev, "Adapter reset during probe\n");
Thomas Falconaf894d22018-04-06 18:37:04 -05002414 ret = adapter->init_done_rc = EAGAIN;
2415 goto err;
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04002416 }
2417
Thomas Falcon6c5c7482018-12-10 15:22:22 -06002418 spin_lock_irqsave(&adapter->rwi_lock, flags);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002419
2420 list_for_each(entry, &adapter->rwi_list) {
2421 tmp = list_entry(entry, struct ibmvnic_rwi, list);
2422 if (tmp->reset_reason == reason) {
Sukadev Bhattiprolu38bd5ce2020-12-04 18:22:35 -08002423 netdev_dbg(netdev, "Skipping matching reset, reason=%d\n",
2424 reason);
Thomas Falcon6c5c7482018-12-10 15:22:22 -06002425 spin_unlock_irqrestore(&adapter->rwi_lock, flags);
Thomas Falconaf894d22018-04-06 18:37:04 -05002426 ret = EBUSY;
2427 goto err;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002428 }
2429 }
2430
Thomas Falcon1d1bbc32018-12-10 15:22:23 -06002431 rwi = kzalloc(sizeof(*rwi), GFP_ATOMIC);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002432 if (!rwi) {
Thomas Falcon6c5c7482018-12-10 15:22:22 -06002433 spin_unlock_irqrestore(&adapter->rwi_lock, flags);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002434 ibmvnic_close(netdev);
Thomas Falconaf894d22018-04-06 18:37:04 -05002435 ret = ENOMEM;
2436 goto err;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002437 }
Thomas Falcon2770a792018-05-23 13:38:02 -05002438 /* if we just received a transport event,
2439 * flush reset queue and process this reset
2440 */
2441 if (adapter->force_reset_recovery && !list_empty(&adapter->rwi_list)) {
2442 list_for_each_safe(entry, tmp_entry, &adapter->rwi_list)
2443 list_del(entry);
2444 }
Nathan Fontenoted651a12017-05-03 14:04:38 -04002445 rwi->reset_reason = reason;
2446 list_add_tail(&rwi->list, &adapter->rwi_list);
Thomas Falcon6c5c7482018-12-10 15:22:22 -06002447 spin_unlock_irqrestore(&adapter->rwi_lock, flags);
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002448 netdev_dbg(adapter->netdev, "Scheduling reset (reason %d)\n", reason);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002449 schedule_work(&adapter->ibmvnic_reset);
Thomas Falconaf894d22018-04-06 18:37:04 -05002450
2451 return 0;
2452err:
Thomas Falconaf894d22018-04-06 18:37:04 -05002453 return -ret;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002454}
2455
Michael S. Tsirkin0290bd22019-12-10 09:23:51 -05002456static void ibmvnic_tx_timeout(struct net_device *dev, unsigned int txqueue)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002457{
2458 struct ibmvnic_adapter *adapter = netdev_priv(dev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002459
Lijun Pan855a6312020-11-20 16:40:13 -06002460 if (test_bit(0, &adapter->resetting)) {
2461 netdev_err(adapter->netdev,
2462 "Adapter is resetting, skip timeout reset\n");
2463 return;
2464 }
Dany Maddena86d5c62020-11-25 18:04:31 -06002465 /* No queuing up reset until at least 5 seconds (default watchdog val)
2466 * after last reset
2467 */
2468 if (time_before(jiffies, (adapter->last_reset_time + dev->watchdog_timeo))) {
2469 netdev_dbg(dev, "Not yet time to tx timeout.\n");
2470 return;
2471 }
Nathan Fontenoted651a12017-05-03 14:04:38 -04002472 ibmvnic_reset(adapter, VNIC_RESET_TIMEOUT);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002473}
2474
2475static void remove_buff_from_pool(struct ibmvnic_adapter *adapter,
2476 struct ibmvnic_rx_buff *rx_buff)
2477{
2478 struct ibmvnic_rx_pool *pool = &adapter->rx_pool[rx_buff->pool_index];
2479
2480 rx_buff->skb = NULL;
2481
2482 pool->free_map[pool->next_alloc] = (int)(rx_buff - pool->rx_buff);
2483 pool->next_alloc = (pool->next_alloc + 1) % pool->size;
2484
2485 atomic_dec(&pool->available);
2486}
2487
2488static int ibmvnic_poll(struct napi_struct *napi, int budget)
2489{
Dwip N. Banerjeeec20f362020-11-18 19:12:23 -06002490 struct ibmvnic_sub_crq_queue *rx_scrq;
2491 struct ibmvnic_adapter *adapter;
2492 struct net_device *netdev;
2493 int frames_processed;
2494 int scrq_num;
2495
2496 netdev = napi->dev;
2497 adapter = netdev_priv(netdev);
2498 scrq_num = (int)(napi - adapter->napi);
2499 frames_processed = 0;
2500 rx_scrq = adapter->rx_scrq[scrq_num];
Nathan Fontenot152ce472017-05-26 10:30:54 -04002501
Thomas Falcon032c5e82015-12-21 11:26:06 -06002502restart_poll:
2503 while (frames_processed < budget) {
2504 struct sk_buff *skb;
2505 struct ibmvnic_rx_buff *rx_buff;
2506 union sub_crq *next;
2507 u32 length;
2508 u16 offset;
2509 u8 flags = 0;
2510
Juliet Kim7ed5b312019-09-20 16:11:23 -04002511 if (unlikely(test_bit(0, &adapter->resetting) &&
John Allen34686562018-02-06 16:21:49 -06002512 adapter->reset_reason != VNIC_RESET_NON_FATAL)) {
Dwip N. Banerjeeec20f362020-11-18 19:12:23 -06002513 enable_scrq_irq(adapter, rx_scrq);
Thomas Falcon21ecba62017-06-14 23:50:09 -05002514 napi_complete_done(napi, frames_processed);
2515 return frames_processed;
2516 }
2517
Dwip N. Banerjeeec20f362020-11-18 19:12:23 -06002518 if (!pending_scrq(adapter, rx_scrq))
Thomas Falcon032c5e82015-12-21 11:26:06 -06002519 break;
Thomas Falconb71ec952020-12-01 09:52:10 -06002520 /* The queue entry at the current index is peeked at above
2521 * to determine that there is a valid descriptor awaiting
2522 * processing. We want to be sure that the current slot
2523 * holds a valid descriptor before reading its contents.
2524 */
2525 dma_rmb();
Dwip N. Banerjeeec20f362020-11-18 19:12:23 -06002526 next = ibmvnic_next_scrq(adapter, rx_scrq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002527 rx_buff =
2528 (struct ibmvnic_rx_buff *)be64_to_cpu(next->
2529 rx_comp.correlator);
2530 /* do error checking */
2531 if (next->rx_comp.rc) {
John Allene1cea2e2017-08-07 15:42:30 -05002532 netdev_dbg(netdev, "rx buffer returned with rc %x\n",
2533 be16_to_cpu(next->rx_comp.rc));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002534 /* free the entry */
2535 next->rx_comp.first = 0;
Thomas Falcon4b9b0f02018-02-13 18:23:42 -06002536 dev_kfree_skb_any(rx_buff->skb);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002537 remove_buff_from_pool(adapter, rx_buff);
Nathan Fontenotca05e312017-05-03 14:05:14 -04002538 continue;
Thomas Falconabe27a82018-02-19 20:12:57 -06002539 } else if (!rx_buff->skb) {
2540 /* free the entry */
2541 next->rx_comp.first = 0;
2542 remove_buff_from_pool(adapter, rx_buff);
2543 continue;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002544 }
2545
2546 length = be32_to_cpu(next->rx_comp.len);
2547 offset = be16_to_cpu(next->rx_comp.off_frame_data);
2548 flags = next->rx_comp.flags;
2549 skb = rx_buff->skb;
Lijun Pan42557da2021-02-12 20:48:40 -06002550 /* load long_term_buff before copying to skb */
2551 dma_rmb();
Thomas Falcon032c5e82015-12-21 11:26:06 -06002552 skb_copy_to_linear_data(skb, rx_buff->data + offset,
2553 length);
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04002554
2555 /* VLAN Header has been stripped by the system firmware and
2556 * needs to be inserted by the driver
2557 */
2558 if (adapter->rx_vlan_header_insertion &&
2559 (flags & IBMVNIC_VLAN_STRIPPED))
2560 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
2561 ntohs(next->rx_comp.vlan_tci));
2562
Thomas Falcon032c5e82015-12-21 11:26:06 -06002563 /* free the entry */
2564 next->rx_comp.first = 0;
2565 remove_buff_from_pool(adapter, rx_buff);
2566
2567 skb_put(skb, length);
2568 skb->protocol = eth_type_trans(skb, netdev);
Thomas Falcon94ca3052017-05-03 14:05:20 -04002569 skb_record_rx_queue(skb, scrq_num);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002570
2571 if (flags & IBMVNIC_IP_CHKSUM_GOOD &&
2572 flags & IBMVNIC_TCP_UDP_CHKSUM_GOOD) {
2573 skb->ip_summed = CHECKSUM_UNNECESSARY;
2574 }
2575
2576 length = skb->len;
2577 napi_gro_receive(napi, skb); /* send it up */
2578 netdev->stats.rx_packets++;
2579 netdev->stats.rx_bytes += length;
John Allen3d52b592017-08-02 16:44:14 -05002580 adapter->rx_stats_buffers[scrq_num].packets++;
2581 adapter->rx_stats_buffers[scrq_num].bytes += length;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002582 frames_processed++;
2583 }
Nathan Fontenot152ce472017-05-26 10:30:54 -04002584
Dwip N. Banerjee41ed0a02020-11-18 19:12:25 -06002585 if (adapter->state != VNIC_CLOSING &&
2586 ((atomic_read(&adapter->rx_pool[scrq_num].available) <
2587 adapter->req_rx_add_entries_per_subcrq / 2) ||
2588 frames_processed < budget))
Nathan Fontenot152ce472017-05-26 10:30:54 -04002589 replenish_rx_pool(adapter, &adapter->rx_pool[scrq_num]);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002590 if (frames_processed < budget) {
Dwip N. Banerjeeec20f362020-11-18 19:12:23 -06002591 if (napi_complete_done(napi, frames_processed)) {
2592 enable_scrq_irq(adapter, rx_scrq);
2593 if (pending_scrq(adapter, rx_scrq)) {
2594 rmb();
2595 if (napi_reschedule(napi)) {
2596 disable_scrq_irq(adapter, rx_scrq);
2597 goto restart_poll;
2598 }
2599 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002600 }
2601 }
2602 return frames_processed;
2603}
2604
John Allenc26eba02017-10-26 16:23:25 -05002605static int wait_for_reset(struct ibmvnic_adapter *adapter)
2606{
Thomas Falconaf894d22018-04-06 18:37:04 -05002607 int rc, ret;
2608
John Allenc26eba02017-10-26 16:23:25 -05002609 adapter->fallback.mtu = adapter->req_mtu;
2610 adapter->fallback.rx_queues = adapter->req_rx_queues;
2611 adapter->fallback.tx_queues = adapter->req_tx_queues;
2612 adapter->fallback.rx_entries = adapter->req_rx_add_entries_per_subcrq;
2613 adapter->fallback.tx_entries = adapter->req_tx_entries_per_subcrq;
2614
Thomas Falcon070eca92019-11-25 17:12:53 -06002615 reinit_completion(&adapter->reset_done);
John Allenc26eba02017-10-26 16:23:25 -05002616 adapter->wait_for_reset = true;
Thomas Falconaf894d22018-04-06 18:37:04 -05002617 rc = ibmvnic_reset(adapter, VNIC_RESET_CHANGE_PARAM);
Thomas Falcon476d96c2019-11-25 17:12:55 -06002618
2619 if (rc) {
2620 ret = rc;
2621 goto out;
2622 }
2623 rc = ibmvnic_wait_for_completion(adapter, &adapter->reset_done, 60000);
2624 if (rc) {
2625 ret = -ENODEV;
2626 goto out;
2627 }
John Allenc26eba02017-10-26 16:23:25 -05002628
Thomas Falconaf894d22018-04-06 18:37:04 -05002629 ret = 0;
John Allenc26eba02017-10-26 16:23:25 -05002630 if (adapter->reset_done_rc) {
Thomas Falconaf894d22018-04-06 18:37:04 -05002631 ret = -EIO;
John Allenc26eba02017-10-26 16:23:25 -05002632 adapter->desired.mtu = adapter->fallback.mtu;
2633 adapter->desired.rx_queues = adapter->fallback.rx_queues;
2634 adapter->desired.tx_queues = adapter->fallback.tx_queues;
2635 adapter->desired.rx_entries = adapter->fallback.rx_entries;
2636 adapter->desired.tx_entries = adapter->fallback.tx_entries;
2637
Thomas Falcon070eca92019-11-25 17:12:53 -06002638 reinit_completion(&adapter->reset_done);
Thomas Falconaf894d22018-04-06 18:37:04 -05002639 adapter->wait_for_reset = true;
2640 rc = ibmvnic_reset(adapter, VNIC_RESET_CHANGE_PARAM);
Thomas Falcon476d96c2019-11-25 17:12:55 -06002641 if (rc) {
2642 ret = rc;
2643 goto out;
2644 }
2645 rc = ibmvnic_wait_for_completion(adapter, &adapter->reset_done,
2646 60000);
2647 if (rc) {
2648 ret = -ENODEV;
2649 goto out;
2650 }
John Allenc26eba02017-10-26 16:23:25 -05002651 }
Thomas Falcon476d96c2019-11-25 17:12:55 -06002652out:
John Allenc26eba02017-10-26 16:23:25 -05002653 adapter->wait_for_reset = false;
2654
Thomas Falconaf894d22018-04-06 18:37:04 -05002655 return ret;
John Allenc26eba02017-10-26 16:23:25 -05002656}
2657
John Allen3a807b72017-06-06 16:55:52 -05002658static int ibmvnic_change_mtu(struct net_device *netdev, int new_mtu)
2659{
John Allenc26eba02017-10-26 16:23:25 -05002660 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2661
2662 adapter->desired.mtu = new_mtu + ETH_HLEN;
2663
2664 return wait_for_reset(adapter);
John Allen3a807b72017-06-06 16:55:52 -05002665}
2666
Thomas Falconf10b09e2018-03-12 11:51:05 -05002667static netdev_features_t ibmvnic_features_check(struct sk_buff *skb,
2668 struct net_device *dev,
2669 netdev_features_t features)
2670{
2671 /* Some backing hardware adapters can not
2672 * handle packets with a MSS less than 224
2673 * or with only one segment.
2674 */
2675 if (skb_is_gso(skb)) {
2676 if (skb_shinfo(skb)->gso_size < 224 ||
2677 skb_shinfo(skb)->gso_segs == 1)
2678 features &= ~NETIF_F_GSO_MASK;
2679 }
2680
2681 return features;
2682}
2683
Thomas Falcon032c5e82015-12-21 11:26:06 -06002684static const struct net_device_ops ibmvnic_netdev_ops = {
2685 .ndo_open = ibmvnic_open,
2686 .ndo_stop = ibmvnic_close,
2687 .ndo_start_xmit = ibmvnic_xmit,
2688 .ndo_set_rx_mode = ibmvnic_set_multi,
2689 .ndo_set_mac_address = ibmvnic_set_mac,
2690 .ndo_validate_addr = eth_validate_addr,
Thomas Falcon032c5e82015-12-21 11:26:06 -06002691 .ndo_tx_timeout = ibmvnic_tx_timeout,
John Allen3a807b72017-06-06 16:55:52 -05002692 .ndo_change_mtu = ibmvnic_change_mtu,
Thomas Falconf10b09e2018-03-12 11:51:05 -05002693 .ndo_features_check = ibmvnic_features_check,
Thomas Falcon032c5e82015-12-21 11:26:06 -06002694};
2695
2696/* ethtool functions */
2697
Philippe Reynes8a433792017-01-07 22:37:29 +01002698static int ibmvnic_get_link_ksettings(struct net_device *netdev,
2699 struct ethtool_link_ksettings *cmd)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002700{
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03002701 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2702 int rc;
Philippe Reynes8a433792017-01-07 22:37:29 +01002703
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03002704 rc = send_query_phys_parms(adapter);
2705 if (rc) {
2706 adapter->speed = SPEED_UNKNOWN;
2707 adapter->duplex = DUPLEX_UNKNOWN;
2708 }
2709 cmd->base.speed = adapter->speed;
2710 cmd->base.duplex = adapter->duplex;
Philippe Reynes8a433792017-01-07 22:37:29 +01002711 cmd->base.port = PORT_FIBRE;
2712 cmd->base.phy_address = 0;
2713 cmd->base.autoneg = AUTONEG_ENABLE;
2714
Thomas Falcon032c5e82015-12-21 11:26:06 -06002715 return 0;
2716}
2717
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02002718static void ibmvnic_get_drvinfo(struct net_device *netdev,
Thomas Falcon032c5e82015-12-21 11:26:06 -06002719 struct ethtool_drvinfo *info)
2720{
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02002721 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2722
Thomas Falcon032c5e82015-12-21 11:26:06 -06002723 strlcpy(info->driver, ibmvnic_driver_name, sizeof(info->driver));
2724 strlcpy(info->version, IBMVNIC_DRIVER_VERSION, sizeof(info->version));
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02002725 strlcpy(info->fw_version, adapter->fw_version,
2726 sizeof(info->fw_version));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002727}
2728
2729static u32 ibmvnic_get_msglevel(struct net_device *netdev)
2730{
2731 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2732
2733 return adapter->msg_enable;
2734}
2735
2736static void ibmvnic_set_msglevel(struct net_device *netdev, u32 data)
2737{
2738 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2739
2740 adapter->msg_enable = data;
2741}
2742
2743static u32 ibmvnic_get_link(struct net_device *netdev)
2744{
2745 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2746
2747 /* Don't need to send a query because we request a logical link up at
2748 * init and then we wait for link state indications
2749 */
2750 return adapter->logical_link_state;
2751}
2752
2753static void ibmvnic_get_ringparam(struct net_device *netdev,
2754 struct ethtool_ringparam *ring)
2755{
John Allenbc131b32017-08-02 16:46:30 -05002756 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2757
Thomas Falcon723ad912018-09-28 18:38:26 -05002758 if (adapter->priv_flags & IBMVNIC_USE_SERVER_MAXES) {
2759 ring->rx_max_pending = adapter->max_rx_add_entries_per_subcrq;
2760 ring->tx_max_pending = adapter->max_tx_entries_per_subcrq;
2761 } else {
2762 ring->rx_max_pending = IBMVNIC_MAX_QUEUE_SZ;
2763 ring->tx_max_pending = IBMVNIC_MAX_QUEUE_SZ;
2764 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002765 ring->rx_mini_max_pending = 0;
2766 ring->rx_jumbo_max_pending = 0;
John Allenbc131b32017-08-02 16:46:30 -05002767 ring->rx_pending = adapter->req_rx_add_entries_per_subcrq;
2768 ring->tx_pending = adapter->req_tx_entries_per_subcrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002769 ring->rx_mini_pending = 0;
2770 ring->rx_jumbo_pending = 0;
2771}
2772
John Allenc26eba02017-10-26 16:23:25 -05002773static int ibmvnic_set_ringparam(struct net_device *netdev,
2774 struct ethtool_ringparam *ring)
2775{
2776 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Thomas Falcon723ad912018-09-28 18:38:26 -05002777 int ret;
John Allenc26eba02017-10-26 16:23:25 -05002778
Thomas Falcon723ad912018-09-28 18:38:26 -05002779 ret = 0;
John Allenc26eba02017-10-26 16:23:25 -05002780 adapter->desired.rx_entries = ring->rx_pending;
2781 adapter->desired.tx_entries = ring->tx_pending;
2782
Thomas Falcon723ad912018-09-28 18:38:26 -05002783 ret = wait_for_reset(adapter);
2784
2785 if (!ret &&
2786 (adapter->req_rx_add_entries_per_subcrq != ring->rx_pending ||
2787 adapter->req_tx_entries_per_subcrq != ring->tx_pending))
2788 netdev_info(netdev,
2789 "Could not match full ringsize request. Requested: RX %d, TX %d; Allowed: RX %llu, TX %llu\n",
2790 ring->rx_pending, ring->tx_pending,
2791 adapter->req_rx_add_entries_per_subcrq,
2792 adapter->req_tx_entries_per_subcrq);
2793 return ret;
John Allenc26eba02017-10-26 16:23:25 -05002794}
2795
John Allenc2dbeb62017-08-02 16:47:17 -05002796static void ibmvnic_get_channels(struct net_device *netdev,
2797 struct ethtool_channels *channels)
2798{
2799 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2800
Thomas Falcon723ad912018-09-28 18:38:26 -05002801 if (adapter->priv_flags & IBMVNIC_USE_SERVER_MAXES) {
2802 channels->max_rx = adapter->max_rx_queues;
2803 channels->max_tx = adapter->max_tx_queues;
2804 } else {
2805 channels->max_rx = IBMVNIC_MAX_QUEUES;
2806 channels->max_tx = IBMVNIC_MAX_QUEUES;
2807 }
2808
John Allenc2dbeb62017-08-02 16:47:17 -05002809 channels->max_other = 0;
2810 channels->max_combined = 0;
2811 channels->rx_count = adapter->req_rx_queues;
2812 channels->tx_count = adapter->req_tx_queues;
2813 channels->other_count = 0;
2814 channels->combined_count = 0;
2815}
2816
John Allenc26eba02017-10-26 16:23:25 -05002817static int ibmvnic_set_channels(struct net_device *netdev,
2818 struct ethtool_channels *channels)
2819{
2820 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Thomas Falcon723ad912018-09-28 18:38:26 -05002821 int ret;
John Allenc26eba02017-10-26 16:23:25 -05002822
Thomas Falcon723ad912018-09-28 18:38:26 -05002823 ret = 0;
John Allenc26eba02017-10-26 16:23:25 -05002824 adapter->desired.rx_queues = channels->rx_count;
2825 adapter->desired.tx_queues = channels->tx_count;
2826
Thomas Falcon723ad912018-09-28 18:38:26 -05002827 ret = wait_for_reset(adapter);
2828
2829 if (!ret &&
2830 (adapter->req_rx_queues != channels->rx_count ||
2831 adapter->req_tx_queues != channels->tx_count))
2832 netdev_info(netdev,
2833 "Could not match full channels request. Requested: RX %d, TX %d; Allowed: RX %llu, TX %llu\n",
2834 channels->rx_count, channels->tx_count,
2835 adapter->req_rx_queues, adapter->req_tx_queues);
2836 return ret;
2837
John Allenc26eba02017-10-26 16:23:25 -05002838}
2839
Thomas Falcon032c5e82015-12-21 11:26:06 -06002840static void ibmvnic_get_strings(struct net_device *dev, u32 stringset, u8 *data)
2841{
John Allen3d52b592017-08-02 16:44:14 -05002842 struct ibmvnic_adapter *adapter = netdev_priv(dev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002843 int i;
2844
Thomas Falcon723ad912018-09-28 18:38:26 -05002845 switch (stringset) {
2846 case ETH_SS_STATS:
2847 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats);
2848 i++, data += ETH_GSTRING_LEN)
2849 memcpy(data, ibmvnic_stats[i].name, ETH_GSTRING_LEN);
2850
2851 for (i = 0; i < adapter->req_tx_queues; i++) {
2852 snprintf(data, ETH_GSTRING_LEN, "tx%d_packets", i);
2853 data += ETH_GSTRING_LEN;
2854
2855 snprintf(data, ETH_GSTRING_LEN, "tx%d_bytes", i);
2856 data += ETH_GSTRING_LEN;
2857
2858 snprintf(data, ETH_GSTRING_LEN,
2859 "tx%d_dropped_packets", i);
2860 data += ETH_GSTRING_LEN;
2861 }
2862
2863 for (i = 0; i < adapter->req_rx_queues; i++) {
2864 snprintf(data, ETH_GSTRING_LEN, "rx%d_packets", i);
2865 data += ETH_GSTRING_LEN;
2866
2867 snprintf(data, ETH_GSTRING_LEN, "rx%d_bytes", i);
2868 data += ETH_GSTRING_LEN;
2869
2870 snprintf(data, ETH_GSTRING_LEN, "rx%d_interrupts", i);
2871 data += ETH_GSTRING_LEN;
2872 }
2873 break;
2874
2875 case ETH_SS_PRIV_FLAGS:
2876 for (i = 0; i < ARRAY_SIZE(ibmvnic_priv_flags); i++)
2877 strcpy(data + i * ETH_GSTRING_LEN,
2878 ibmvnic_priv_flags[i]);
2879 break;
2880 default:
Thomas Falcon032c5e82015-12-21 11:26:06 -06002881 return;
John Allen3d52b592017-08-02 16:44:14 -05002882 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002883}
2884
2885static int ibmvnic_get_sset_count(struct net_device *dev, int sset)
2886{
John Allen3d52b592017-08-02 16:44:14 -05002887 struct ibmvnic_adapter *adapter = netdev_priv(dev);
2888
Thomas Falcon032c5e82015-12-21 11:26:06 -06002889 switch (sset) {
2890 case ETH_SS_STATS:
John Allen3d52b592017-08-02 16:44:14 -05002891 return ARRAY_SIZE(ibmvnic_stats) +
2892 adapter->req_tx_queues * NUM_TX_STATS +
2893 adapter->req_rx_queues * NUM_RX_STATS;
Thomas Falcon723ad912018-09-28 18:38:26 -05002894 case ETH_SS_PRIV_FLAGS:
2895 return ARRAY_SIZE(ibmvnic_priv_flags);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002896 default:
2897 return -EOPNOTSUPP;
2898 }
2899}
2900
2901static void ibmvnic_get_ethtool_stats(struct net_device *dev,
2902 struct ethtool_stats *stats, u64 *data)
2903{
2904 struct ibmvnic_adapter *adapter = netdev_priv(dev);
2905 union ibmvnic_crq crq;
John Allen3d52b592017-08-02 16:44:14 -05002906 int i, j;
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05002907 int rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002908
2909 memset(&crq, 0, sizeof(crq));
2910 crq.request_statistics.first = IBMVNIC_CRQ_CMD;
2911 crq.request_statistics.cmd = REQUEST_STATISTICS;
2912 crq.request_statistics.ioba = cpu_to_be32(adapter->stats_token);
2913 crq.request_statistics.len =
2914 cpu_to_be32(sizeof(struct ibmvnic_statistics));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002915
2916 /* Wait for data to be written */
Thomas Falcon070eca92019-11-25 17:12:53 -06002917 reinit_completion(&adapter->stats_done);
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05002918 rc = ibmvnic_send_crq(adapter, &crq);
2919 if (rc)
2920 return;
Thomas Falcon476d96c2019-11-25 17:12:55 -06002921 rc = ibmvnic_wait_for_completion(adapter, &adapter->stats_done, 10000);
2922 if (rc)
2923 return;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002924
2925 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++)
John Allen52da5c12017-08-02 16:45:28 -05002926 data[i] = be64_to_cpu(IBMVNIC_GET_STAT(adapter,
2927 ibmvnic_stats[i].offset));
John Allen3d52b592017-08-02 16:44:14 -05002928
2929 for (j = 0; j < adapter->req_tx_queues; j++) {
2930 data[i] = adapter->tx_stats_buffers[j].packets;
2931 i++;
2932 data[i] = adapter->tx_stats_buffers[j].bytes;
2933 i++;
2934 data[i] = adapter->tx_stats_buffers[j].dropped_packets;
2935 i++;
2936 }
2937
2938 for (j = 0; j < adapter->req_rx_queues; j++) {
2939 data[i] = adapter->rx_stats_buffers[j].packets;
2940 i++;
2941 data[i] = adapter->rx_stats_buffers[j].bytes;
2942 i++;
2943 data[i] = adapter->rx_stats_buffers[j].interrupts;
2944 i++;
2945 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002946}
2947
Thomas Falcon723ad912018-09-28 18:38:26 -05002948static u32 ibmvnic_get_priv_flags(struct net_device *netdev)
2949{
2950 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2951
2952 return adapter->priv_flags;
2953}
2954
2955static int ibmvnic_set_priv_flags(struct net_device *netdev, u32 flags)
2956{
2957 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2958 bool which_maxes = !!(flags & IBMVNIC_USE_SERVER_MAXES);
2959
2960 if (which_maxes)
2961 adapter->priv_flags |= IBMVNIC_USE_SERVER_MAXES;
2962 else
2963 adapter->priv_flags &= ~IBMVNIC_USE_SERVER_MAXES;
2964
2965 return 0;
2966}
Thomas Falcon032c5e82015-12-21 11:26:06 -06002967static const struct ethtool_ops ibmvnic_ethtool_ops = {
Thomas Falcon032c5e82015-12-21 11:26:06 -06002968 .get_drvinfo = ibmvnic_get_drvinfo,
2969 .get_msglevel = ibmvnic_get_msglevel,
2970 .set_msglevel = ibmvnic_set_msglevel,
2971 .get_link = ibmvnic_get_link,
2972 .get_ringparam = ibmvnic_get_ringparam,
John Allenc26eba02017-10-26 16:23:25 -05002973 .set_ringparam = ibmvnic_set_ringparam,
John Allenc2dbeb62017-08-02 16:47:17 -05002974 .get_channels = ibmvnic_get_channels,
John Allenc26eba02017-10-26 16:23:25 -05002975 .set_channels = ibmvnic_set_channels,
Thomas Falcon032c5e82015-12-21 11:26:06 -06002976 .get_strings = ibmvnic_get_strings,
2977 .get_sset_count = ibmvnic_get_sset_count,
2978 .get_ethtool_stats = ibmvnic_get_ethtool_stats,
Philippe Reynes8a433792017-01-07 22:37:29 +01002979 .get_link_ksettings = ibmvnic_get_link_ksettings,
Thomas Falcon723ad912018-09-28 18:38:26 -05002980 .get_priv_flags = ibmvnic_get_priv_flags,
2981 .set_priv_flags = ibmvnic_set_priv_flags,
Thomas Falcon032c5e82015-12-21 11:26:06 -06002982};
2983
2984/* Routines for managing CRQs/sCRQs */
2985
Nathan Fontenot57a49432017-05-26 10:31:12 -04002986static int reset_one_sub_crq_queue(struct ibmvnic_adapter *adapter,
2987 struct ibmvnic_sub_crq_queue *scrq)
2988{
2989 int rc;
2990
Dany Madden9281cf22020-11-25 18:04:26 -06002991 if (!scrq) {
YANG LI862aecb2020-12-30 15:23:14 +08002992 netdev_dbg(adapter->netdev, "Invalid scrq reset.\n");
Dany Madden9281cf22020-11-25 18:04:26 -06002993 return -EINVAL;
2994 }
2995
Nathan Fontenot57a49432017-05-26 10:31:12 -04002996 if (scrq->irq) {
2997 free_irq(scrq->irq, scrq);
2998 irq_dispose_mapping(scrq->irq);
2999 scrq->irq = 0;
3000 }
3001
Dany Madden9281cf22020-11-25 18:04:26 -06003002 if (scrq->msgs) {
3003 memset(scrq->msgs, 0, 4 * PAGE_SIZE);
3004 atomic_set(&scrq->used, 0);
3005 scrq->cur = 0;
Jakub Kicinski55fd59b2020-12-03 15:42:13 -08003006 scrq->ind_buf.index = 0;
Dany Madden9281cf22020-11-25 18:04:26 -06003007 } else {
3008 netdev_dbg(adapter->netdev, "Invalid scrq reset\n");
3009 return -EINVAL;
3010 }
Nathan Fontenot57a49432017-05-26 10:31:12 -04003011
3012 rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
3013 4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
3014 return rc;
3015}
3016
3017static int reset_sub_crq_queues(struct ibmvnic_adapter *adapter)
3018{
3019 int i, rc;
3020
Lijun Pana0faaa22020-11-23 13:35:45 -06003021 if (!adapter->tx_scrq || !adapter->rx_scrq)
3022 return -EINVAL;
3023
Nathan Fontenot57a49432017-05-26 10:31:12 -04003024 for (i = 0; i < adapter->req_tx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05003025 netdev_dbg(adapter->netdev, "Re-setting tx_scrq[%d]\n", i);
Nathan Fontenot57a49432017-05-26 10:31:12 -04003026 rc = reset_one_sub_crq_queue(adapter, adapter->tx_scrq[i]);
3027 if (rc)
3028 return rc;
3029 }
3030
3031 for (i = 0; i < adapter->req_rx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05003032 netdev_dbg(adapter->netdev, "Re-setting rx_scrq[%d]\n", i);
Nathan Fontenot57a49432017-05-26 10:31:12 -04003033 rc = reset_one_sub_crq_queue(adapter, adapter->rx_scrq[i]);
3034 if (rc)
3035 return rc;
3036 }
3037
Nathan Fontenot57a49432017-05-26 10:31:12 -04003038 return rc;
3039}
3040
Thomas Falcon032c5e82015-12-21 11:26:06 -06003041static void release_sub_crq_queue(struct ibmvnic_adapter *adapter,
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06003042 struct ibmvnic_sub_crq_queue *scrq,
3043 bool do_h_free)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003044{
3045 struct device *dev = &adapter->vdev->dev;
3046 long rc;
3047
3048 netdev_dbg(adapter->netdev, "Releasing sub-CRQ\n");
3049
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06003050 if (do_h_free) {
3051 /* Close the sub-crqs */
3052 do {
3053 rc = plpar_hcall_norets(H_FREE_SUB_CRQ,
3054 adapter->vdev->unit_address,
3055 scrq->crq_num);
3056 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
Thomas Falcon032c5e82015-12-21 11:26:06 -06003057
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06003058 if (rc) {
3059 netdev_err(adapter->netdev,
3060 "Failed to release sub-CRQ %16lx, rc = %ld\n",
3061 scrq->crq_num, rc);
3062 }
Thomas Falconffa73852017-04-19 13:44:29 -04003063 }
3064
Thomas Falconf019fb62020-11-18 19:12:17 -06003065 dma_free_coherent(dev,
3066 IBMVNIC_IND_ARR_SZ,
3067 scrq->ind_buf.indir_arr,
3068 scrq->ind_buf.indir_dma);
3069
Thomas Falcon032c5e82015-12-21 11:26:06 -06003070 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
3071 DMA_BIDIRECTIONAL);
3072 free_pages((unsigned long)scrq->msgs, 2);
3073 kfree(scrq);
3074}
3075
3076static struct ibmvnic_sub_crq_queue *init_sub_crq_queue(struct ibmvnic_adapter
3077 *adapter)
3078{
3079 struct device *dev = &adapter->vdev->dev;
3080 struct ibmvnic_sub_crq_queue *scrq;
3081 int rc;
3082
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04003083 scrq = kzalloc(sizeof(*scrq), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003084 if (!scrq)
3085 return NULL;
3086
Nathan Fontenot7f7adc52017-04-19 13:45:16 -04003087 scrq->msgs =
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04003088 (union sub_crq *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, 2);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003089 if (!scrq->msgs) {
3090 dev_warn(dev, "Couldn't allocate crq queue messages page\n");
3091 goto zero_page_failed;
3092 }
3093
3094 scrq->msg_token = dma_map_single(dev, scrq->msgs, 4 * PAGE_SIZE,
3095 DMA_BIDIRECTIONAL);
3096 if (dma_mapping_error(dev, scrq->msg_token)) {
3097 dev_warn(dev, "Couldn't map crq queue messages page\n");
3098 goto map_failed;
3099 }
3100
3101 rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
3102 4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
3103
3104 if (rc == H_RESOURCE)
3105 rc = ibmvnic_reset_crq(adapter);
3106
3107 if (rc == H_CLOSED) {
3108 dev_warn(dev, "Partner adapter not ready, waiting.\n");
3109 } else if (rc) {
3110 dev_warn(dev, "Error %d registering sub-crq\n", rc);
3111 goto reg_failed;
3112 }
3113
Thomas Falcon032c5e82015-12-21 11:26:06 -06003114 scrq->adapter = adapter;
3115 scrq->size = 4 * PAGE_SIZE / sizeof(*scrq->msgs);
Thomas Falconf019fb62020-11-18 19:12:17 -06003116 scrq->ind_buf.index = 0;
3117
3118 scrq->ind_buf.indir_arr =
3119 dma_alloc_coherent(dev,
3120 IBMVNIC_IND_ARR_SZ,
3121 &scrq->ind_buf.indir_dma,
3122 GFP_KERNEL);
3123
3124 if (!scrq->ind_buf.indir_arr)
3125 goto indir_failed;
3126
Thomas Falcon032c5e82015-12-21 11:26:06 -06003127 spin_lock_init(&scrq->lock);
3128
3129 netdev_dbg(adapter->netdev,
3130 "sub-crq initialized, num %lx, hw_irq=%lx, irq=%x\n",
3131 scrq->crq_num, scrq->hw_irq, scrq->irq);
3132
3133 return scrq;
3134
Thomas Falconf019fb62020-11-18 19:12:17 -06003135indir_failed:
3136 do {
3137 rc = plpar_hcall_norets(H_FREE_SUB_CRQ,
3138 adapter->vdev->unit_address,
3139 scrq->crq_num);
3140 } while (rc == H_BUSY || rc == H_IS_LONG_BUSY(rc));
Thomas Falcon032c5e82015-12-21 11:26:06 -06003141reg_failed:
3142 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
3143 DMA_BIDIRECTIONAL);
3144map_failed:
3145 free_pages((unsigned long)scrq->msgs, 2);
3146zero_page_failed:
3147 kfree(scrq);
3148
3149 return NULL;
3150}
3151
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06003152static void release_sub_crqs(struct ibmvnic_adapter *adapter, bool do_h_free)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003153{
3154 int i;
3155
3156 if (adapter->tx_scrq) {
Nathan Fontenot82e3be32018-02-21 21:33:56 -06003157 for (i = 0; i < adapter->num_active_tx_scrqs; i++) {
Nathan Fontenotb5108882017-03-30 02:49:18 -04003158 if (!adapter->tx_scrq[i])
3159 continue;
3160
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05003161 netdev_dbg(adapter->netdev, "Releasing tx_scrq[%d]\n",
3162 i);
Nathan Fontenotb5108882017-03-30 02:49:18 -04003163 if (adapter->tx_scrq[i]->irq) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06003164 free_irq(adapter->tx_scrq[i]->irq,
3165 adapter->tx_scrq[i]);
Thomas Falcon88eb98a2016-07-06 15:35:16 -05003166 irq_dispose_mapping(adapter->tx_scrq[i]->irq);
Nathan Fontenotb5108882017-03-30 02:49:18 -04003167 adapter->tx_scrq[i]->irq = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003168 }
Nathan Fontenotb5108882017-03-30 02:49:18 -04003169
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06003170 release_sub_crq_queue(adapter, adapter->tx_scrq[i],
3171 do_h_free);
Nathan Fontenotb5108882017-03-30 02:49:18 -04003172 }
3173
Nathan Fontenot9501df32017-03-15 23:38:07 -04003174 kfree(adapter->tx_scrq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003175 adapter->tx_scrq = NULL;
Nathan Fontenot82e3be32018-02-21 21:33:56 -06003176 adapter->num_active_tx_scrqs = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003177 }
3178
3179 if (adapter->rx_scrq) {
Nathan Fontenot82e3be32018-02-21 21:33:56 -06003180 for (i = 0; i < adapter->num_active_rx_scrqs; i++) {
Nathan Fontenotb5108882017-03-30 02:49:18 -04003181 if (!adapter->rx_scrq[i])
3182 continue;
3183
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05003184 netdev_dbg(adapter->netdev, "Releasing rx_scrq[%d]\n",
3185 i);
Nathan Fontenotb5108882017-03-30 02:49:18 -04003186 if (adapter->rx_scrq[i]->irq) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06003187 free_irq(adapter->rx_scrq[i]->irq,
3188 adapter->rx_scrq[i]);
Thomas Falcon88eb98a2016-07-06 15:35:16 -05003189 irq_dispose_mapping(adapter->rx_scrq[i]->irq);
Nathan Fontenotb5108882017-03-30 02:49:18 -04003190 adapter->rx_scrq[i]->irq = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003191 }
Nathan Fontenotb5108882017-03-30 02:49:18 -04003192
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06003193 release_sub_crq_queue(adapter, adapter->rx_scrq[i],
3194 do_h_free);
Nathan Fontenotb5108882017-03-30 02:49:18 -04003195 }
3196
Nathan Fontenot9501df32017-03-15 23:38:07 -04003197 kfree(adapter->rx_scrq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003198 adapter->rx_scrq = NULL;
Nathan Fontenot82e3be32018-02-21 21:33:56 -06003199 adapter->num_active_rx_scrqs = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003200 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06003201}
3202
3203static int disable_scrq_irq(struct ibmvnic_adapter *adapter,
3204 struct ibmvnic_sub_crq_queue *scrq)
3205{
3206 struct device *dev = &adapter->vdev->dev;
3207 unsigned long rc;
3208
3209 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
3210 H_DISABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
3211 if (rc)
3212 dev_err(dev, "Couldn't disable scrq irq 0x%lx. rc=%ld\n",
3213 scrq->hw_irq, rc);
3214 return rc;
3215}
3216
3217static int enable_scrq_irq(struct ibmvnic_adapter *adapter,
3218 struct ibmvnic_sub_crq_queue *scrq)
3219{
3220 struct device *dev = &adapter->vdev->dev;
3221 unsigned long rc;
3222
3223 if (scrq->hw_irq > 0x100000000ULL) {
3224 dev_err(dev, "bad hw_irq = %lx\n", scrq->hw_irq);
3225 return 1;
3226 }
3227
Juliet Kim7ed5b312019-09-20 16:11:23 -04003228 if (test_bit(0, &adapter->resetting) &&
Nathan Fontenot73f9d362018-05-22 11:21:10 -05003229 adapter->reset_reason == VNIC_RESET_MOBILITY) {
Juliet Kim284f87d2019-11-20 10:50:03 -05003230 u64 val = (0xff000000) | scrq->hw_irq;
Nathan Fontenot73f9d362018-05-22 11:21:10 -05003231
Juliet Kim284f87d2019-11-20 10:50:03 -05003232 rc = plpar_hcall_norets(H_EOI, val);
Juliet Kim2df5c602019-11-20 10:50:04 -05003233 /* H_EOI would fail with rc = H_FUNCTION when running
3234 * in XIVE mode which is expected, but not an error.
3235 */
3236 if (rc && (rc != H_FUNCTION))
Juliet Kim284f87d2019-11-20 10:50:03 -05003237 dev_err(dev, "H_EOI FAILED irq 0x%llx. rc=%ld\n",
3238 val, rc);
Nathan Fontenot73f9d362018-05-22 11:21:10 -05003239 }
Thomas Falconf23e0642018-04-15 18:53:36 -05003240
Thomas Falcon032c5e82015-12-21 11:26:06 -06003241 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
3242 H_ENABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
3243 if (rc)
3244 dev_err(dev, "Couldn't enable scrq irq 0x%lx. rc=%ld\n",
3245 scrq->hw_irq, rc);
3246 return rc;
3247}
3248
3249static int ibmvnic_complete_tx(struct ibmvnic_adapter *adapter,
3250 struct ibmvnic_sub_crq_queue *scrq)
3251{
3252 struct device *dev = &adapter->vdev->dev;
Thomas Falcon06b3e352018-03-16 20:00:28 -05003253 struct ibmvnic_tx_pool *tx_pool;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003254 struct ibmvnic_tx_buff *txbuff;
Thomas Falcon0d973382020-11-18 19:12:19 -06003255 struct netdev_queue *txq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003256 union sub_crq *next;
3257 int index;
Thomas Falconc62aa372020-11-18 19:12:20 -06003258 int i;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003259
3260restart_loop:
3261 while (pending_scrq(adapter, scrq)) {
3262 unsigned int pool = scrq->pool_index;
Thomas Falconffc385b2018-02-18 10:08:41 -06003263 int num_entries = 0;
Thomas Falcon0d973382020-11-18 19:12:19 -06003264 int total_bytes = 0;
3265 int num_packets = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003266
Thomas Falconb71ec952020-12-01 09:52:10 -06003267 /* The queue entry at the current index is peeked at above
3268 * to determine that there is a valid descriptor awaiting
3269 * processing. We want to be sure that the current slot
3270 * holds a valid descriptor before reading its contents.
3271 */
3272 dma_rmb();
3273
Thomas Falcon032c5e82015-12-21 11:26:06 -06003274 next = ibmvnic_next_scrq(adapter, scrq);
3275 for (i = 0; i < next->tx_comp.num_comps; i++) {
Thomas Falconba246c12020-12-01 09:52:11 -06003276 if (next->tx_comp.rcs[i])
Thomas Falcon032c5e82015-12-21 11:26:06 -06003277 dev_err(dev, "tx error %x\n",
3278 next->tx_comp.rcs[i]);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003279 index = be32_to_cpu(next->tx_comp.correlators[i]);
Thomas Falcon06b3e352018-03-16 20:00:28 -05003280 if (index & IBMVNIC_TSO_POOL_MASK) {
3281 tx_pool = &adapter->tso_pool[pool];
3282 index &= ~IBMVNIC_TSO_POOL_MASK;
3283 } else {
3284 tx_pool = &adapter->tx_pool[pool];
3285 }
3286
3287 txbuff = &tx_pool->tx_buff[index];
Thomas Falcon0d973382020-11-18 19:12:19 -06003288 num_packets++;
Thomas Falconffc385b2018-02-18 10:08:41 -06003289 num_entries += txbuff->num_entries;
Thomas Falcon0d973382020-11-18 19:12:19 -06003290 if (txbuff->skb) {
3291 total_bytes += txbuff->skb->len;
3292 dev_consume_skb_irq(txbuff->skb);
3293 txbuff->skb = NULL;
3294 } else {
3295 netdev_warn(adapter->netdev,
3296 "TX completion received with NULL socket buffer\n");
3297 }
Thomas Falcon06b3e352018-03-16 20:00:28 -05003298 tx_pool->free_map[tx_pool->producer_index] = index;
3299 tx_pool->producer_index =
3300 (tx_pool->producer_index + 1) %
3301 tx_pool->num_buffers;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003302 }
3303 /* remove tx_comp scrq*/
3304 next->tx_comp.first = 0;
Nathan Fontenot7c3e7de2017-05-03 14:05:25 -04003305
Thomas Falcon0d973382020-11-18 19:12:19 -06003306 txq = netdev_get_tx_queue(adapter->netdev, scrq->pool_index);
3307 netdev_tx_completed_queue(txq, num_packets, total_bytes);
3308
Thomas Falconffc385b2018-02-18 10:08:41 -06003309 if (atomic_sub_return(num_entries, &scrq->used) <=
Nathan Fontenot7c3e7de2017-05-03 14:05:25 -04003310 (adapter->req_tx_entries_per_subcrq / 2) &&
3311 __netif_subqueue_stopped(adapter->netdev,
3312 scrq->pool_index)) {
3313 netif_wake_subqueue(adapter->netdev, scrq->pool_index);
Thomas Falcon0aecb132018-02-26 18:10:58 -06003314 netdev_dbg(adapter->netdev, "Started queue %d\n",
3315 scrq->pool_index);
Nathan Fontenot7c3e7de2017-05-03 14:05:25 -04003316 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06003317 }
3318
3319 enable_scrq_irq(adapter, scrq);
3320
3321 if (pending_scrq(adapter, scrq)) {
3322 disable_scrq_irq(adapter, scrq);
3323 goto restart_loop;
3324 }
3325
3326 return 0;
3327}
3328
3329static irqreturn_t ibmvnic_interrupt_tx(int irq, void *instance)
3330{
3331 struct ibmvnic_sub_crq_queue *scrq = instance;
3332 struct ibmvnic_adapter *adapter = scrq->adapter;
3333
3334 disable_scrq_irq(adapter, scrq);
3335 ibmvnic_complete_tx(adapter, scrq);
3336
3337 return IRQ_HANDLED;
3338}
3339
3340static irqreturn_t ibmvnic_interrupt_rx(int irq, void *instance)
3341{
3342 struct ibmvnic_sub_crq_queue *scrq = instance;
3343 struct ibmvnic_adapter *adapter = scrq->adapter;
3344
Nathan Fontenot09fb35e2018-01-10 10:40:09 -06003345 /* When booting a kdump kernel we can hit pending interrupts
3346 * prior to completing driver initialization.
3347 */
3348 if (unlikely(adapter->state != VNIC_OPEN))
3349 return IRQ_NONE;
3350
John Allen3d52b592017-08-02 16:44:14 -05003351 adapter->rx_stats_buffers[scrq->scrq_num].interrupts++;
3352
Thomas Falcon032c5e82015-12-21 11:26:06 -06003353 if (napi_schedule_prep(&adapter->napi[scrq->scrq_num])) {
3354 disable_scrq_irq(adapter, scrq);
3355 __napi_schedule(&adapter->napi[scrq->scrq_num]);
3356 }
3357
3358 return IRQ_HANDLED;
3359}
3360
Thomas Falconea22d512016-07-06 15:35:17 -05003361static int init_sub_crq_irqs(struct ibmvnic_adapter *adapter)
3362{
3363 struct device *dev = &adapter->vdev->dev;
3364 struct ibmvnic_sub_crq_queue *scrq;
3365 int i = 0, j = 0;
3366 int rc = 0;
3367
3368 for (i = 0; i < adapter->req_tx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05003369 netdev_dbg(adapter->netdev, "Initializing tx_scrq[%d] irq\n",
3370 i);
Thomas Falconea22d512016-07-06 15:35:17 -05003371 scrq = adapter->tx_scrq[i];
3372 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
3373
Michael Ellerman99c17902016-09-10 19:59:05 +10003374 if (!scrq->irq) {
Thomas Falconea22d512016-07-06 15:35:17 -05003375 rc = -EINVAL;
3376 dev_err(dev, "Error mapping irq\n");
3377 goto req_tx_irq_failed;
3378 }
3379
Murilo Fossa Vicentinie56e2512019-04-25 11:02:33 -03003380 snprintf(scrq->name, sizeof(scrq->name), "ibmvnic-%x-tx%d",
3381 adapter->vdev->unit_address, i);
Thomas Falconea22d512016-07-06 15:35:17 -05003382 rc = request_irq(scrq->irq, ibmvnic_interrupt_tx,
Murilo Fossa Vicentinie56e2512019-04-25 11:02:33 -03003383 0, scrq->name, scrq);
Thomas Falconea22d512016-07-06 15:35:17 -05003384
3385 if (rc) {
3386 dev_err(dev, "Couldn't register tx irq 0x%x. rc=%d\n",
3387 scrq->irq, rc);
3388 irq_dispose_mapping(scrq->irq);
Nathan Fontenotaf9090c2018-02-20 11:04:18 -06003389 goto req_tx_irq_failed;
Thomas Falconea22d512016-07-06 15:35:17 -05003390 }
3391 }
3392
3393 for (i = 0; i < adapter->req_rx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05003394 netdev_dbg(adapter->netdev, "Initializing rx_scrq[%d] irq\n",
3395 i);
Thomas Falconea22d512016-07-06 15:35:17 -05003396 scrq = adapter->rx_scrq[i];
3397 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
Michael Ellerman99c17902016-09-10 19:59:05 +10003398 if (!scrq->irq) {
Thomas Falconea22d512016-07-06 15:35:17 -05003399 rc = -EINVAL;
3400 dev_err(dev, "Error mapping irq\n");
3401 goto req_rx_irq_failed;
3402 }
Murilo Fossa Vicentinie56e2512019-04-25 11:02:33 -03003403 snprintf(scrq->name, sizeof(scrq->name), "ibmvnic-%x-rx%d",
3404 adapter->vdev->unit_address, i);
Thomas Falconea22d512016-07-06 15:35:17 -05003405 rc = request_irq(scrq->irq, ibmvnic_interrupt_rx,
Murilo Fossa Vicentinie56e2512019-04-25 11:02:33 -03003406 0, scrq->name, scrq);
Thomas Falconea22d512016-07-06 15:35:17 -05003407 if (rc) {
3408 dev_err(dev, "Couldn't register rx irq 0x%x. rc=%d\n",
3409 scrq->irq, rc);
3410 irq_dispose_mapping(scrq->irq);
3411 goto req_rx_irq_failed;
3412 }
3413 }
3414 return rc;
3415
3416req_rx_irq_failed:
Thomas Falcon8bf371e2016-10-27 12:28:52 -05003417 for (j = 0; j < i; j++) {
Thomas Falconea22d512016-07-06 15:35:17 -05003418 free_irq(adapter->rx_scrq[j]->irq, adapter->rx_scrq[j]);
3419 irq_dispose_mapping(adapter->rx_scrq[j]->irq);
Thomas Falcon8bf371e2016-10-27 12:28:52 -05003420 }
Thomas Falconea22d512016-07-06 15:35:17 -05003421 i = adapter->req_tx_queues;
3422req_tx_irq_failed:
Thomas Falcon8bf371e2016-10-27 12:28:52 -05003423 for (j = 0; j < i; j++) {
Thomas Falconea22d512016-07-06 15:35:17 -05003424 free_irq(adapter->tx_scrq[j]->irq, adapter->tx_scrq[j]);
Thomas Falcon27a21452020-07-29 16:36:32 -05003425 irq_dispose_mapping(adapter->tx_scrq[j]->irq);
Thomas Falcon8bf371e2016-10-27 12:28:52 -05003426 }
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06003427 release_sub_crqs(adapter, 1);
Thomas Falconea22d512016-07-06 15:35:17 -05003428 return rc;
3429}
3430
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003431static int init_sub_crqs(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003432{
3433 struct device *dev = &adapter->vdev->dev;
3434 struct ibmvnic_sub_crq_queue **allqueues;
3435 int registered_queues = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003436 int total_queues;
3437 int more = 0;
Thomas Falconea22d512016-07-06 15:35:17 -05003438 int i;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003439
Thomas Falcon032c5e82015-12-21 11:26:06 -06003440 total_queues = adapter->req_tx_queues + adapter->req_rx_queues;
3441
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04003442 allqueues = kcalloc(total_queues, sizeof(*allqueues), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003443 if (!allqueues)
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003444 return -1;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003445
3446 for (i = 0; i < total_queues; i++) {
3447 allqueues[i] = init_sub_crq_queue(adapter);
3448 if (!allqueues[i]) {
3449 dev_warn(dev, "Couldn't allocate all sub-crqs\n");
3450 break;
3451 }
3452 registered_queues++;
3453 }
3454
3455 /* Make sure we were able to register the minimum number of queues */
3456 if (registered_queues <
3457 adapter->min_tx_queues + adapter->min_rx_queues) {
3458 dev_err(dev, "Fatal: Couldn't init min number of sub-crqs\n");
3459 goto tx_failed;
3460 }
3461
3462 /* Distribute the failed allocated queues*/
3463 for (i = 0; i < total_queues - registered_queues + more ; i++) {
3464 netdev_dbg(adapter->netdev, "Reducing number of queues\n");
3465 switch (i % 3) {
3466 case 0:
3467 if (adapter->req_rx_queues > adapter->min_rx_queues)
3468 adapter->req_rx_queues--;
3469 else
3470 more++;
3471 break;
3472 case 1:
3473 if (adapter->req_tx_queues > adapter->min_tx_queues)
3474 adapter->req_tx_queues--;
3475 else
3476 more++;
3477 break;
3478 }
3479 }
3480
3481 adapter->tx_scrq = kcalloc(adapter->req_tx_queues,
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04003482 sizeof(*adapter->tx_scrq), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003483 if (!adapter->tx_scrq)
3484 goto tx_failed;
3485
3486 for (i = 0; i < adapter->req_tx_queues; i++) {
3487 adapter->tx_scrq[i] = allqueues[i];
3488 adapter->tx_scrq[i]->pool_index = i;
Nathan Fontenot82e3be32018-02-21 21:33:56 -06003489 adapter->num_active_tx_scrqs++;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003490 }
3491
3492 adapter->rx_scrq = kcalloc(adapter->req_rx_queues,
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04003493 sizeof(*adapter->rx_scrq), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003494 if (!adapter->rx_scrq)
3495 goto rx_failed;
3496
3497 for (i = 0; i < adapter->req_rx_queues; i++) {
3498 adapter->rx_scrq[i] = allqueues[i + adapter->req_tx_queues];
3499 adapter->rx_scrq[i]->scrq_num = i;
Nathan Fontenot82e3be32018-02-21 21:33:56 -06003500 adapter->num_active_rx_scrqs++;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003501 }
3502
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003503 kfree(allqueues);
3504 return 0;
3505
3506rx_failed:
3507 kfree(adapter->tx_scrq);
3508 adapter->tx_scrq = NULL;
3509tx_failed:
3510 for (i = 0; i < registered_queues; i++)
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06003511 release_sub_crq_queue(adapter, allqueues[i], 1);
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003512 kfree(allqueues);
3513 return -1;
3514}
3515
Lijun Pan09081b92020-09-27 20:13:27 -05003516static void send_request_cap(struct ibmvnic_adapter *adapter, int retry)
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003517{
3518 struct device *dev = &adapter->vdev->dev;
3519 union ibmvnic_crq crq;
John Allenc26eba02017-10-26 16:23:25 -05003520 int max_entries;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003521
3522 if (!retry) {
3523 /* Sub-CRQ entries are 32 byte long */
3524 int entries_page = 4 * PAGE_SIZE / (sizeof(u64) * 4);
3525
3526 if (adapter->min_tx_entries_per_subcrq > entries_page ||
3527 adapter->min_rx_add_entries_per_subcrq > entries_page) {
3528 dev_err(dev, "Fatal, invalid entries per sub-crq\n");
3529 return;
3530 }
3531
John Allenc26eba02017-10-26 16:23:25 -05003532 if (adapter->desired.mtu)
3533 adapter->req_mtu = adapter->desired.mtu;
3534 else
3535 adapter->req_mtu = adapter->netdev->mtu + ETH_HLEN;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003536
John Allenc26eba02017-10-26 16:23:25 -05003537 if (!adapter->desired.tx_entries)
3538 adapter->desired.tx_entries =
3539 adapter->max_tx_entries_per_subcrq;
3540 if (!adapter->desired.rx_entries)
3541 adapter->desired.rx_entries =
3542 adapter->max_rx_add_entries_per_subcrq;
3543
3544 max_entries = IBMVNIC_MAX_LTB_SIZE /
3545 (adapter->req_mtu + IBMVNIC_BUFFER_HLEN);
3546
3547 if ((adapter->req_mtu + IBMVNIC_BUFFER_HLEN) *
3548 adapter->desired.tx_entries > IBMVNIC_MAX_LTB_SIZE) {
3549 adapter->desired.tx_entries = max_entries;
3550 }
3551
3552 if ((adapter->req_mtu + IBMVNIC_BUFFER_HLEN) *
3553 adapter->desired.rx_entries > IBMVNIC_MAX_LTB_SIZE) {
3554 adapter->desired.rx_entries = max_entries;
3555 }
3556
3557 if (adapter->desired.tx_entries)
3558 adapter->req_tx_entries_per_subcrq =
3559 adapter->desired.tx_entries;
3560 else
3561 adapter->req_tx_entries_per_subcrq =
3562 adapter->max_tx_entries_per_subcrq;
3563
3564 if (adapter->desired.rx_entries)
3565 adapter->req_rx_add_entries_per_subcrq =
3566 adapter->desired.rx_entries;
3567 else
3568 adapter->req_rx_add_entries_per_subcrq =
3569 adapter->max_rx_add_entries_per_subcrq;
3570
3571 if (adapter->desired.tx_queues)
3572 adapter->req_tx_queues =
3573 adapter->desired.tx_queues;
3574 else
3575 adapter->req_tx_queues =
3576 adapter->opt_tx_comp_sub_queues;
3577
3578 if (adapter->desired.rx_queues)
3579 adapter->req_rx_queues =
3580 adapter->desired.rx_queues;
3581 else
3582 adapter->req_rx_queues =
3583 adapter->opt_rx_comp_queues;
3584
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003585 adapter->req_rx_add_queues = adapter->max_rx_add_queues;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003586 }
3587
Thomas Falcon032c5e82015-12-21 11:26:06 -06003588 memset(&crq, 0, sizeof(crq));
3589 crq.request_capability.first = IBMVNIC_CRQ_CMD;
3590 crq.request_capability.cmd = REQUEST_CAPABILITY;
3591
3592 crq.request_capability.capability = cpu_to_be16(REQ_TX_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06003593 crq.request_capability.number = cpu_to_be64(adapter->req_tx_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06003594 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003595 ibmvnic_send_crq(adapter, &crq);
3596
3597 crq.request_capability.capability = cpu_to_be16(REQ_RX_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06003598 crq.request_capability.number = cpu_to_be64(adapter->req_rx_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06003599 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003600 ibmvnic_send_crq(adapter, &crq);
3601
3602 crq.request_capability.capability = cpu_to_be16(REQ_RX_ADD_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06003603 crq.request_capability.number = cpu_to_be64(adapter->req_rx_add_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06003604 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003605 ibmvnic_send_crq(adapter, &crq);
3606
3607 crq.request_capability.capability =
3608 cpu_to_be16(REQ_TX_ENTRIES_PER_SUBCRQ);
3609 crq.request_capability.number =
Thomas Falconde89e852016-03-01 10:20:09 -06003610 cpu_to_be64(adapter->req_tx_entries_per_subcrq);
Thomas Falcon901e0402017-02-15 12:17:59 -06003611 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003612 ibmvnic_send_crq(adapter, &crq);
3613
3614 crq.request_capability.capability =
3615 cpu_to_be16(REQ_RX_ADD_ENTRIES_PER_SUBCRQ);
3616 crq.request_capability.number =
Thomas Falconde89e852016-03-01 10:20:09 -06003617 cpu_to_be64(adapter->req_rx_add_entries_per_subcrq);
Thomas Falcon901e0402017-02-15 12:17:59 -06003618 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003619 ibmvnic_send_crq(adapter, &crq);
3620
3621 crq.request_capability.capability = cpu_to_be16(REQ_MTU);
Thomas Falconde89e852016-03-01 10:20:09 -06003622 crq.request_capability.number = cpu_to_be64(adapter->req_mtu);
Thomas Falcon901e0402017-02-15 12:17:59 -06003623 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003624 ibmvnic_send_crq(adapter, &crq);
3625
3626 if (adapter->netdev->flags & IFF_PROMISC) {
3627 if (adapter->promisc_supported) {
3628 crq.request_capability.capability =
3629 cpu_to_be16(PROMISC_REQUESTED);
Thomas Falconde89e852016-03-01 10:20:09 -06003630 crq.request_capability.number = cpu_to_be64(1);
Thomas Falcon901e0402017-02-15 12:17:59 -06003631 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003632 ibmvnic_send_crq(adapter, &crq);
3633 }
3634 } else {
3635 crq.request_capability.capability =
3636 cpu_to_be16(PROMISC_REQUESTED);
Thomas Falconde89e852016-03-01 10:20:09 -06003637 crq.request_capability.number = cpu_to_be64(0);
Thomas Falcon901e0402017-02-15 12:17:59 -06003638 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003639 ibmvnic_send_crq(adapter, &crq);
3640 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06003641}
3642
3643static int pending_scrq(struct ibmvnic_adapter *adapter,
3644 struct ibmvnic_sub_crq_queue *scrq)
3645{
3646 union sub_crq *entry = &scrq->msgs[scrq->cur];
3647
Thomas Falcon1cf9cc72017-06-14 23:50:08 -05003648 if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003649 return 1;
3650 else
3651 return 0;
3652}
3653
3654static union sub_crq *ibmvnic_next_scrq(struct ibmvnic_adapter *adapter,
3655 struct ibmvnic_sub_crq_queue *scrq)
3656{
3657 union sub_crq *entry;
3658 unsigned long flags;
3659
3660 spin_lock_irqsave(&scrq->lock, flags);
3661 entry = &scrq->msgs[scrq->cur];
3662 if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP) {
3663 if (++scrq->cur == scrq->size)
3664 scrq->cur = 0;
3665 } else {
3666 entry = NULL;
3667 }
3668 spin_unlock_irqrestore(&scrq->lock, flags);
3669
Thomas Falconb71ec952020-12-01 09:52:10 -06003670 /* Ensure that the entire buffer descriptor has been
3671 * loaded before reading its contents
3672 */
3673 dma_rmb();
3674
Thomas Falcon032c5e82015-12-21 11:26:06 -06003675 return entry;
3676}
3677
3678static union ibmvnic_crq *ibmvnic_next_crq(struct ibmvnic_adapter *adapter)
3679{
3680 struct ibmvnic_crq_queue *queue = &adapter->crq;
3681 union ibmvnic_crq *crq;
3682
3683 crq = &queue->msgs[queue->cur];
3684 if (crq->generic.first & IBMVNIC_CRQ_CMD_RSP) {
3685 if (++queue->cur == queue->size)
3686 queue->cur = 0;
3687 } else {
3688 crq = NULL;
3689 }
3690
3691 return crq;
3692}
3693
Thomas Falcon2d14d372018-07-13 12:03:32 -05003694static void print_subcrq_error(struct device *dev, int rc, const char *func)
3695{
3696 switch (rc) {
3697 case H_PARAMETER:
3698 dev_warn_ratelimited(dev,
3699 "%s failed: Send request is malformed or adapter failover pending. (rc=%d)\n",
3700 func, rc);
3701 break;
3702 case H_CLOSED:
3703 dev_warn_ratelimited(dev,
3704 "%s failed: Backing queue closed. Adapter is down or failover pending. (rc=%d)\n",
3705 func, rc);
3706 break;
3707 default:
3708 dev_err_ratelimited(dev, "%s failed: (rc=%d)\n", func, rc);
3709 break;
3710 }
3711}
3712
Thomas Falconad7775d2016-04-01 17:20:34 -05003713static int send_subcrq_indirect(struct ibmvnic_adapter *adapter,
3714 u64 remote_handle, u64 ioba, u64 num_entries)
3715{
3716 unsigned int ua = adapter->vdev->unit_address;
3717 struct device *dev = &adapter->vdev->dev;
3718 int rc;
3719
3720 /* Make sure the hypervisor sees the complete request */
3721 mb();
3722 rc = plpar_hcall_norets(H_SEND_SUB_CRQ_INDIRECT, ua,
3723 cpu_to_be64(remote_handle),
3724 ioba, num_entries);
3725
Thomas Falcon2d14d372018-07-13 12:03:32 -05003726 if (rc)
3727 print_subcrq_error(dev, rc, __func__);
Thomas Falconad7775d2016-04-01 17:20:34 -05003728
3729 return rc;
3730}
3731
Thomas Falcon032c5e82015-12-21 11:26:06 -06003732static int ibmvnic_send_crq(struct ibmvnic_adapter *adapter,
3733 union ibmvnic_crq *crq)
3734{
3735 unsigned int ua = adapter->vdev->unit_address;
3736 struct device *dev = &adapter->vdev->dev;
3737 u64 *u64_crq = (u64 *)crq;
3738 int rc;
3739
3740 netdev_dbg(adapter->netdev, "Sending CRQ: %016lx %016lx\n",
3741 (unsigned long int)cpu_to_be64(u64_crq[0]),
3742 (unsigned long int)cpu_to_be64(u64_crq[1]));
3743
Thomas Falcon51536982018-05-23 13:37:56 -05003744 if (!adapter->crq.active &&
3745 crq->generic.first != IBMVNIC_CRQ_INIT_CMD) {
3746 dev_warn(dev, "Invalid request detected while CRQ is inactive, possible device state change during reset\n");
3747 return -EINVAL;
3748 }
3749
Thomas Falcon032c5e82015-12-21 11:26:06 -06003750 /* Make sure the hypervisor sees the complete request */
3751 mb();
3752
3753 rc = plpar_hcall_norets(H_SEND_CRQ, ua,
3754 cpu_to_be64(u64_crq[0]),
3755 cpu_to_be64(u64_crq[1]));
3756
3757 if (rc) {
Nathan Fontenotec95dff2018-02-07 13:00:24 -06003758 if (rc == H_CLOSED) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06003759 dev_warn(dev, "CRQ Queue closed\n");
Lijun Panfa68bfa2020-08-19 17:52:24 -05003760 /* do not reset, report the fail, wait for passive init from server */
Nathan Fontenotec95dff2018-02-07 13:00:24 -06003761 }
3762
Thomas Falcon032c5e82015-12-21 11:26:06 -06003763 dev_warn(dev, "Send error (rc=%d)\n", rc);
3764 }
3765
3766 return rc;
3767}
3768
3769static int ibmvnic_send_crq_init(struct ibmvnic_adapter *adapter)
3770{
Thomas Falcon36a782f2020-08-31 11:59:57 -05003771 struct device *dev = &adapter->vdev->dev;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003772 union ibmvnic_crq crq;
Thomas Falcon36a782f2020-08-31 11:59:57 -05003773 int retries = 100;
3774 int rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003775
3776 memset(&crq, 0, sizeof(crq));
3777 crq.generic.first = IBMVNIC_CRQ_INIT_CMD;
3778 crq.generic.cmd = IBMVNIC_CRQ_INIT;
3779 netdev_dbg(adapter->netdev, "Sending CRQ init\n");
3780
Thomas Falcon36a782f2020-08-31 11:59:57 -05003781 do {
3782 rc = ibmvnic_send_crq(adapter, &crq);
3783 if (rc != H_CLOSED)
3784 break;
3785 retries--;
3786 msleep(50);
3787
3788 } while (retries > 0);
3789
3790 if (rc) {
3791 dev_err(dev, "Failed to send init request, rc = %d\n", rc);
3792 return rc;
3793 }
3794
3795 return 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003796}
3797
Thomas Falcon032c5e82015-12-21 11:26:06 -06003798static int send_version_xchg(struct ibmvnic_adapter *adapter)
3799{
3800 union ibmvnic_crq crq;
3801
3802 memset(&crq, 0, sizeof(crq));
3803 crq.version_exchange.first = IBMVNIC_CRQ_CMD;
3804 crq.version_exchange.cmd = VERSION_EXCHANGE;
3805 crq.version_exchange.version = cpu_to_be16(ibmvnic_version);
3806
3807 return ibmvnic_send_crq(adapter, &crq);
3808}
3809
Nathan Fontenot37798d02017-11-08 11:23:56 -06003810struct vnic_login_client_data {
3811 u8 type;
3812 __be16 len;
Kees Cook08ea5562018-04-10 15:26:43 -07003813 char name[];
Nathan Fontenot37798d02017-11-08 11:23:56 -06003814} __packed;
3815
3816static int vnic_client_data_len(struct ibmvnic_adapter *adapter)
3817{
3818 int len;
3819
3820 /* Calculate the amount of buffer space needed for the
3821 * vnic client data in the login buffer. There are four entries,
3822 * OS name, LPAR name, device name, and a null last entry.
3823 */
3824 len = 4 * sizeof(struct vnic_login_client_data);
3825 len += 6; /* "Linux" plus NULL */
3826 len += strlen(utsname()->nodename) + 1;
3827 len += strlen(adapter->netdev->name) + 1;
3828
3829 return len;
3830}
3831
3832static void vnic_add_client_data(struct ibmvnic_adapter *adapter,
3833 struct vnic_login_client_data *vlcd)
3834{
3835 const char *os_name = "Linux";
3836 int len;
3837
3838 /* Type 1 - LPAR OS */
3839 vlcd->type = 1;
3840 len = strlen(os_name) + 1;
3841 vlcd->len = cpu_to_be16(len);
Kees Cook08ea5562018-04-10 15:26:43 -07003842 strncpy(vlcd->name, os_name, len);
3843 vlcd = (struct vnic_login_client_data *)(vlcd->name + len);
Nathan Fontenot37798d02017-11-08 11:23:56 -06003844
3845 /* Type 2 - LPAR name */
3846 vlcd->type = 2;
3847 len = strlen(utsname()->nodename) + 1;
3848 vlcd->len = cpu_to_be16(len);
Kees Cook08ea5562018-04-10 15:26:43 -07003849 strncpy(vlcd->name, utsname()->nodename, len);
3850 vlcd = (struct vnic_login_client_data *)(vlcd->name + len);
Nathan Fontenot37798d02017-11-08 11:23:56 -06003851
3852 /* Type 3 - device name */
3853 vlcd->type = 3;
3854 len = strlen(adapter->netdev->name) + 1;
3855 vlcd->len = cpu_to_be16(len);
Kees Cook08ea5562018-04-10 15:26:43 -07003856 strncpy(vlcd->name, adapter->netdev->name, len);
Nathan Fontenot37798d02017-11-08 11:23:56 -06003857}
3858
Thomas Falcon20a8ab72018-02-26 18:10:59 -06003859static int send_login(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003860{
3861 struct ibmvnic_login_rsp_buffer *login_rsp_buffer;
3862 struct ibmvnic_login_buffer *login_buffer;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003863 struct device *dev = &adapter->vdev->dev;
Dany Maddenc98d9cc2020-11-25 18:04:30 -06003864 struct vnic_login_client_data *vlcd;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003865 dma_addr_t rsp_buffer_token;
3866 dma_addr_t buffer_token;
3867 size_t rsp_buffer_size;
3868 union ibmvnic_crq crq;
Dany Maddenc98d9cc2020-11-25 18:04:30 -06003869 int client_data_len;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003870 size_t buffer_size;
3871 __be64 *tx_list_p;
3872 __be64 *rx_list_p;
Dany Maddenc98d9cc2020-11-25 18:04:30 -06003873 int rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003874 int i;
3875
Thomas Falcon20a8ab72018-02-26 18:10:59 -06003876 if (!adapter->tx_scrq || !adapter->rx_scrq) {
3877 netdev_err(adapter->netdev,
3878 "RX or TX queues are not allocated, device login failed\n");
3879 return -1;
3880 }
3881
Lijun Pana0c8be52020-12-19 15:39:19 -06003882 release_login_buffer(adapter);
Thomas Falcon34f0f4e2018-02-13 18:23:40 -06003883 release_login_rsp_buffer(adapter);
Lijun Pana0c8be52020-12-19 15:39:19 -06003884
Nathan Fontenot37798d02017-11-08 11:23:56 -06003885 client_data_len = vnic_client_data_len(adapter);
3886
Thomas Falcon032c5e82015-12-21 11:26:06 -06003887 buffer_size =
3888 sizeof(struct ibmvnic_login_buffer) +
Nathan Fontenot37798d02017-11-08 11:23:56 -06003889 sizeof(u64) * (adapter->req_tx_queues + adapter->req_rx_queues) +
3890 client_data_len;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003891
Nathan Fontenot37798d02017-11-08 11:23:56 -06003892 login_buffer = kzalloc(buffer_size, GFP_ATOMIC);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003893 if (!login_buffer)
3894 goto buf_alloc_failed;
3895
3896 buffer_token = dma_map_single(dev, login_buffer, buffer_size,
3897 DMA_TO_DEVICE);
3898 if (dma_mapping_error(dev, buffer_token)) {
3899 dev_err(dev, "Couldn't map login buffer\n");
3900 goto buf_map_failed;
3901 }
3902
John Allen498cd8e2016-04-06 11:49:55 -05003903 rsp_buffer_size = sizeof(struct ibmvnic_login_rsp_buffer) +
3904 sizeof(u64) * adapter->req_tx_queues +
3905 sizeof(u64) * adapter->req_rx_queues +
3906 sizeof(u64) * adapter->req_rx_queues +
3907 sizeof(u8) * IBMVNIC_TX_DESC_VERSIONS;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003908
3909 login_rsp_buffer = kmalloc(rsp_buffer_size, GFP_ATOMIC);
3910 if (!login_rsp_buffer)
3911 goto buf_rsp_alloc_failed;
3912
3913 rsp_buffer_token = dma_map_single(dev, login_rsp_buffer,
3914 rsp_buffer_size, DMA_FROM_DEVICE);
3915 if (dma_mapping_error(dev, rsp_buffer_token)) {
3916 dev_err(dev, "Couldn't map login rsp buffer\n");
3917 goto buf_rsp_map_failed;
3918 }
Nathan Fontenot661a2622017-04-19 13:44:58 -04003919
Thomas Falcon032c5e82015-12-21 11:26:06 -06003920 adapter->login_buf = login_buffer;
3921 adapter->login_buf_token = buffer_token;
3922 adapter->login_buf_sz = buffer_size;
3923 adapter->login_rsp_buf = login_rsp_buffer;
3924 adapter->login_rsp_buf_token = rsp_buffer_token;
3925 adapter->login_rsp_buf_sz = rsp_buffer_size;
3926
3927 login_buffer->len = cpu_to_be32(buffer_size);
3928 login_buffer->version = cpu_to_be32(INITIAL_VERSION_LB);
3929 login_buffer->num_txcomp_subcrqs = cpu_to_be32(adapter->req_tx_queues);
3930 login_buffer->off_txcomp_subcrqs =
3931 cpu_to_be32(sizeof(struct ibmvnic_login_buffer));
3932 login_buffer->num_rxcomp_subcrqs = cpu_to_be32(adapter->req_rx_queues);
3933 login_buffer->off_rxcomp_subcrqs =
3934 cpu_to_be32(sizeof(struct ibmvnic_login_buffer) +
3935 sizeof(u64) * adapter->req_tx_queues);
3936 login_buffer->login_rsp_ioba = cpu_to_be32(rsp_buffer_token);
3937 login_buffer->login_rsp_len = cpu_to_be32(rsp_buffer_size);
3938
3939 tx_list_p = (__be64 *)((char *)login_buffer +
3940 sizeof(struct ibmvnic_login_buffer));
3941 rx_list_p = (__be64 *)((char *)login_buffer +
3942 sizeof(struct ibmvnic_login_buffer) +
3943 sizeof(u64) * adapter->req_tx_queues);
3944
3945 for (i = 0; i < adapter->req_tx_queues; i++) {
3946 if (adapter->tx_scrq[i]) {
3947 tx_list_p[i] = cpu_to_be64(adapter->tx_scrq[i]->
3948 crq_num);
3949 }
3950 }
3951
3952 for (i = 0; i < adapter->req_rx_queues; i++) {
3953 if (adapter->rx_scrq[i]) {
3954 rx_list_p[i] = cpu_to_be64(adapter->rx_scrq[i]->
3955 crq_num);
3956 }
3957 }
3958
Nathan Fontenot37798d02017-11-08 11:23:56 -06003959 /* Insert vNIC login client data */
3960 vlcd = (struct vnic_login_client_data *)
3961 ((char *)rx_list_p + (sizeof(u64) * adapter->req_rx_queues));
3962 login_buffer->client_data_offset =
3963 cpu_to_be32((char *)vlcd - (char *)login_buffer);
3964 login_buffer->client_data_len = cpu_to_be32(client_data_len);
3965
3966 vnic_add_client_data(adapter, vlcd);
3967
Thomas Falcon032c5e82015-12-21 11:26:06 -06003968 netdev_dbg(adapter->netdev, "Login Buffer:\n");
3969 for (i = 0; i < (adapter->login_buf_sz - 1) / 8 + 1; i++) {
3970 netdev_dbg(adapter->netdev, "%016lx\n",
3971 ((unsigned long int *)(adapter->login_buf))[i]);
3972 }
3973
3974 memset(&crq, 0, sizeof(crq));
3975 crq.login.first = IBMVNIC_CRQ_CMD;
3976 crq.login.cmd = LOGIN;
3977 crq.login.ioba = cpu_to_be32(buffer_token);
3978 crq.login.len = cpu_to_be32(buffer_size);
Sukadev Bhattiprolu76cdc5c2020-11-25 18:04:29 -06003979
3980 adapter->login_pending = true;
Dany Maddenc98d9cc2020-11-25 18:04:30 -06003981 rc = ibmvnic_send_crq(adapter, &crq);
3982 if (rc) {
3983 adapter->login_pending = false;
3984 netdev_err(adapter->netdev, "Failed to send login, rc=%d\n", rc);
3985 goto buf_rsp_map_failed;
3986 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06003987
Thomas Falcon20a8ab72018-02-26 18:10:59 -06003988 return 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003989
Thomas Falcon032c5e82015-12-21 11:26:06 -06003990buf_rsp_map_failed:
3991 kfree(login_rsp_buffer);
Dany Maddenc98d9cc2020-11-25 18:04:30 -06003992 adapter->login_rsp_buf = NULL;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003993buf_rsp_alloc_failed:
3994 dma_unmap_single(dev, buffer_token, buffer_size, DMA_TO_DEVICE);
3995buf_map_failed:
3996 kfree(login_buffer);
Dany Maddenc98d9cc2020-11-25 18:04:30 -06003997 adapter->login_buf = NULL;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003998buf_alloc_failed:
Thomas Falcon20a8ab72018-02-26 18:10:59 -06003999 return -1;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004000}
4001
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05004002static int send_request_map(struct ibmvnic_adapter *adapter, dma_addr_t addr,
4003 u32 len, u8 map_id)
Thomas Falcon032c5e82015-12-21 11:26:06 -06004004{
4005 union ibmvnic_crq crq;
4006
4007 memset(&crq, 0, sizeof(crq));
4008 crq.request_map.first = IBMVNIC_CRQ_CMD;
4009 crq.request_map.cmd = REQUEST_MAP;
4010 crq.request_map.map_id = map_id;
4011 crq.request_map.ioba = cpu_to_be32(addr);
4012 crq.request_map.len = cpu_to_be32(len);
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05004013 return ibmvnic_send_crq(adapter, &crq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004014}
4015
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05004016static int send_request_unmap(struct ibmvnic_adapter *adapter, u8 map_id)
Thomas Falcon032c5e82015-12-21 11:26:06 -06004017{
4018 union ibmvnic_crq crq;
4019
4020 memset(&crq, 0, sizeof(crq));
4021 crq.request_unmap.first = IBMVNIC_CRQ_CMD;
4022 crq.request_unmap.cmd = REQUEST_UNMAP;
4023 crq.request_unmap.map_id = map_id;
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05004024 return ibmvnic_send_crq(adapter, &crq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004025}
4026
Lijun Pan69980d02020-09-27 20:13:28 -05004027static void send_query_map(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06004028{
4029 union ibmvnic_crq crq;
4030
4031 memset(&crq, 0, sizeof(crq));
4032 crq.query_map.first = IBMVNIC_CRQ_CMD;
4033 crq.query_map.cmd = QUERY_MAP;
4034 ibmvnic_send_crq(adapter, &crq);
4035}
4036
4037/* Send a series of CRQs requesting various capabilities of the VNIC server */
Lijun Pan491099a2020-09-27 20:13:26 -05004038static void send_query_cap(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06004039{
4040 union ibmvnic_crq crq;
4041
Thomas Falcon901e0402017-02-15 12:17:59 -06004042 atomic_set(&adapter->running_cap_crqs, 0);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004043 memset(&crq, 0, sizeof(crq));
4044 crq.query_capability.first = IBMVNIC_CRQ_CMD;
4045 crq.query_capability.cmd = QUERY_CAPABILITY;
4046
4047 crq.query_capability.capability = cpu_to_be16(MIN_TX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06004048 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004049 ibmvnic_send_crq(adapter, &crq);
4050
4051 crq.query_capability.capability = cpu_to_be16(MIN_RX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06004052 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004053 ibmvnic_send_crq(adapter, &crq);
4054
4055 crq.query_capability.capability = cpu_to_be16(MIN_RX_ADD_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06004056 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004057 ibmvnic_send_crq(adapter, &crq);
4058
4059 crq.query_capability.capability = cpu_to_be16(MAX_TX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06004060 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004061 ibmvnic_send_crq(adapter, &crq);
4062
4063 crq.query_capability.capability = cpu_to_be16(MAX_RX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06004064 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004065 ibmvnic_send_crq(adapter, &crq);
4066
4067 crq.query_capability.capability = cpu_to_be16(MAX_RX_ADD_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06004068 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004069 ibmvnic_send_crq(adapter, &crq);
4070
4071 crq.query_capability.capability =
4072 cpu_to_be16(MIN_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06004073 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004074 ibmvnic_send_crq(adapter, &crq);
4075
4076 crq.query_capability.capability =
4077 cpu_to_be16(MIN_RX_ADD_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06004078 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004079 ibmvnic_send_crq(adapter, &crq);
4080
4081 crq.query_capability.capability =
4082 cpu_to_be16(MAX_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06004083 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004084 ibmvnic_send_crq(adapter, &crq);
4085
4086 crq.query_capability.capability =
4087 cpu_to_be16(MAX_RX_ADD_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06004088 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004089 ibmvnic_send_crq(adapter, &crq);
4090
4091 crq.query_capability.capability = cpu_to_be16(TCP_IP_OFFLOAD);
Thomas Falcon901e0402017-02-15 12:17:59 -06004092 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004093 ibmvnic_send_crq(adapter, &crq);
4094
4095 crq.query_capability.capability = cpu_to_be16(PROMISC_SUPPORTED);
Thomas Falcon901e0402017-02-15 12:17:59 -06004096 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004097 ibmvnic_send_crq(adapter, &crq);
4098
4099 crq.query_capability.capability = cpu_to_be16(MIN_MTU);
Thomas Falcon901e0402017-02-15 12:17:59 -06004100 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004101 ibmvnic_send_crq(adapter, &crq);
4102
4103 crq.query_capability.capability = cpu_to_be16(MAX_MTU);
Thomas Falcon901e0402017-02-15 12:17:59 -06004104 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004105 ibmvnic_send_crq(adapter, &crq);
4106
4107 crq.query_capability.capability = cpu_to_be16(MAX_MULTICAST_FILTERS);
Thomas Falcon901e0402017-02-15 12:17:59 -06004108 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004109 ibmvnic_send_crq(adapter, &crq);
4110
4111 crq.query_capability.capability = cpu_to_be16(VLAN_HEADER_INSERTION);
Thomas Falcon901e0402017-02-15 12:17:59 -06004112 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004113 ibmvnic_send_crq(adapter, &crq);
4114
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04004115 crq.query_capability.capability = cpu_to_be16(RX_VLAN_HEADER_INSERTION);
4116 atomic_inc(&adapter->running_cap_crqs);
4117 ibmvnic_send_crq(adapter, &crq);
4118
Thomas Falcon032c5e82015-12-21 11:26:06 -06004119 crq.query_capability.capability = cpu_to_be16(MAX_TX_SG_ENTRIES);
Thomas Falcon901e0402017-02-15 12:17:59 -06004120 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004121 ibmvnic_send_crq(adapter, &crq);
4122
4123 crq.query_capability.capability = cpu_to_be16(RX_SG_SUPPORTED);
Thomas Falcon901e0402017-02-15 12:17:59 -06004124 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004125 ibmvnic_send_crq(adapter, &crq);
4126
4127 crq.query_capability.capability = cpu_to_be16(OPT_TX_COMP_SUB_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06004128 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004129 ibmvnic_send_crq(adapter, &crq);
4130
4131 crq.query_capability.capability = cpu_to_be16(OPT_RX_COMP_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06004132 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004133 ibmvnic_send_crq(adapter, &crq);
4134
4135 crq.query_capability.capability =
4136 cpu_to_be16(OPT_RX_BUFADD_Q_PER_RX_COMP_Q);
Thomas Falcon901e0402017-02-15 12:17:59 -06004137 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004138 ibmvnic_send_crq(adapter, &crq);
4139
4140 crq.query_capability.capability =
4141 cpu_to_be16(OPT_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06004142 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004143 ibmvnic_send_crq(adapter, &crq);
4144
4145 crq.query_capability.capability =
4146 cpu_to_be16(OPT_RXBA_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06004147 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004148 ibmvnic_send_crq(adapter, &crq);
4149
4150 crq.query_capability.capability = cpu_to_be16(TX_RX_DESC_REQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06004151 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004152 ibmvnic_send_crq(adapter, &crq);
4153}
4154
Lijun Pan16e811f2020-09-27 20:13:29 -05004155static void send_query_ip_offload(struct ibmvnic_adapter *adapter)
4156{
4157 int buf_sz = sizeof(struct ibmvnic_query_ip_offload_buffer);
4158 struct device *dev = &adapter->vdev->dev;
4159 union ibmvnic_crq crq;
4160
4161 adapter->ip_offload_tok =
4162 dma_map_single(dev,
4163 &adapter->ip_offload_buf,
4164 buf_sz,
4165 DMA_FROM_DEVICE);
4166
4167 if (dma_mapping_error(dev, adapter->ip_offload_tok)) {
4168 if (!firmware_has_feature(FW_FEATURE_CMO))
4169 dev_err(dev, "Couldn't map offload buffer\n");
4170 return;
4171 }
4172
4173 memset(&crq, 0, sizeof(crq));
4174 crq.query_ip_offload.first = IBMVNIC_CRQ_CMD;
4175 crq.query_ip_offload.cmd = QUERY_IP_OFFLOAD;
4176 crq.query_ip_offload.len = cpu_to_be32(buf_sz);
4177 crq.query_ip_offload.ioba =
4178 cpu_to_be32(adapter->ip_offload_tok);
4179
4180 ibmvnic_send_crq(adapter, &crq);
4181}
4182
Lijun Pan46899bd2020-09-27 20:13:30 -05004183static void send_control_ip_offload(struct ibmvnic_adapter *adapter)
4184{
4185 struct ibmvnic_control_ip_offload_buffer *ctrl_buf = &adapter->ip_offload_ctrl;
4186 struct ibmvnic_query_ip_offload_buffer *buf = &adapter->ip_offload_buf;
4187 struct device *dev = &adapter->vdev->dev;
4188 netdev_features_t old_hw_features = 0;
4189 union ibmvnic_crq crq;
4190
4191 adapter->ip_offload_ctrl_tok =
4192 dma_map_single(dev,
4193 ctrl_buf,
4194 sizeof(adapter->ip_offload_ctrl),
4195 DMA_TO_DEVICE);
4196
4197 if (dma_mapping_error(dev, adapter->ip_offload_ctrl_tok)) {
4198 dev_err(dev, "Couldn't map ip offload control buffer\n");
4199 return;
4200 }
4201
4202 ctrl_buf->len = cpu_to_be32(sizeof(adapter->ip_offload_ctrl));
4203 ctrl_buf->version = cpu_to_be32(INITIAL_VERSION_IOB);
4204 ctrl_buf->ipv4_chksum = buf->ipv4_chksum;
4205 ctrl_buf->ipv6_chksum = buf->ipv6_chksum;
4206 ctrl_buf->tcp_ipv4_chksum = buf->tcp_ipv4_chksum;
4207 ctrl_buf->udp_ipv4_chksum = buf->udp_ipv4_chksum;
4208 ctrl_buf->tcp_ipv6_chksum = buf->tcp_ipv6_chksum;
4209 ctrl_buf->udp_ipv6_chksum = buf->udp_ipv6_chksum;
4210 ctrl_buf->large_tx_ipv4 = buf->large_tx_ipv4;
4211 ctrl_buf->large_tx_ipv6 = buf->large_tx_ipv6;
4212
4213 /* large_rx disabled for now, additional features needed */
4214 ctrl_buf->large_rx_ipv4 = 0;
4215 ctrl_buf->large_rx_ipv6 = 0;
4216
4217 if (adapter->state != VNIC_PROBING) {
4218 old_hw_features = adapter->netdev->hw_features;
4219 adapter->netdev->hw_features = 0;
4220 }
4221
4222 adapter->netdev->hw_features = NETIF_F_SG | NETIF_F_GSO | NETIF_F_GRO;
4223
4224 if (buf->tcp_ipv4_chksum || buf->udp_ipv4_chksum)
4225 adapter->netdev->hw_features |= NETIF_F_IP_CSUM;
4226
4227 if (buf->tcp_ipv6_chksum || buf->udp_ipv6_chksum)
4228 adapter->netdev->hw_features |= NETIF_F_IPV6_CSUM;
4229
4230 if ((adapter->netdev->features &
4231 (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)))
4232 adapter->netdev->hw_features |= NETIF_F_RXCSUM;
4233
4234 if (buf->large_tx_ipv4)
4235 adapter->netdev->hw_features |= NETIF_F_TSO;
4236 if (buf->large_tx_ipv6)
4237 adapter->netdev->hw_features |= NETIF_F_TSO6;
4238
4239 if (adapter->state == VNIC_PROBING) {
4240 adapter->netdev->features |= adapter->netdev->hw_features;
4241 } else if (old_hw_features != adapter->netdev->hw_features) {
4242 netdev_features_t tmp = 0;
4243
4244 /* disable features no longer supported */
4245 adapter->netdev->features &= adapter->netdev->hw_features;
4246 /* turn on features now supported if previously enabled */
4247 tmp = (old_hw_features ^ adapter->netdev->hw_features) &
4248 adapter->netdev->hw_features;
4249 adapter->netdev->features |=
4250 tmp & adapter->netdev->wanted_features;
4251 }
4252
4253 memset(&crq, 0, sizeof(crq));
4254 crq.control_ip_offload.first = IBMVNIC_CRQ_CMD;
4255 crq.control_ip_offload.cmd = CONTROL_IP_OFFLOAD;
4256 crq.control_ip_offload.len =
4257 cpu_to_be32(sizeof(adapter->ip_offload_ctrl));
4258 crq.control_ip_offload.ioba = cpu_to_be32(adapter->ip_offload_ctrl_tok);
4259 ibmvnic_send_crq(adapter, &crq);
4260}
4261
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02004262static void handle_vpd_size_rsp(union ibmvnic_crq *crq,
4263 struct ibmvnic_adapter *adapter)
4264{
4265 struct device *dev = &adapter->vdev->dev;
4266
4267 if (crq->get_vpd_size_rsp.rc.code) {
4268 dev_err(dev, "Error retrieving VPD size, rc=%x\n",
4269 crq->get_vpd_size_rsp.rc.code);
4270 complete(&adapter->fw_done);
4271 return;
4272 }
4273
4274 adapter->vpd->len = be64_to_cpu(crq->get_vpd_size_rsp.len);
4275 complete(&adapter->fw_done);
4276}
4277
4278static void handle_vpd_rsp(union ibmvnic_crq *crq,
4279 struct ibmvnic_adapter *adapter)
4280{
4281 struct device *dev = &adapter->vdev->dev;
Desnes Augusto Nunes do Rosario21a25452018-02-05 14:33:55 -02004282 unsigned char *substr = NULL;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02004283 u8 fw_level_len = 0;
4284
4285 memset(adapter->fw_version, 0, 32);
4286
4287 dma_unmap_single(dev, adapter->vpd->dma_addr, adapter->vpd->len,
4288 DMA_FROM_DEVICE);
4289
4290 if (crq->get_vpd_rsp.rc.code) {
4291 dev_err(dev, "Error retrieving VPD from device, rc=%x\n",
4292 crq->get_vpd_rsp.rc.code);
4293 goto complete;
4294 }
4295
4296 /* get the position of the firmware version info
4297 * located after the ASCII 'RM' substring in the buffer
4298 */
4299 substr = strnstr(adapter->vpd->buff, "RM", adapter->vpd->len);
4300 if (!substr) {
Desnes Augusto Nunes do Rosarioa1073112018-02-01 16:04:30 -02004301 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 -02004302 goto complete;
4303 }
4304
4305 /* get length of firmware level ASCII substring */
4306 if ((substr + 2) < (adapter->vpd->buff + adapter->vpd->len)) {
4307 fw_level_len = *(substr + 2);
4308 } else {
4309 dev_info(dev, "Length of FW substr extrapolated VDP buff\n");
4310 goto complete;
4311 }
4312
4313 /* copy firmware version string from vpd into adapter */
4314 if ((substr + 3 + fw_level_len) <
4315 (adapter->vpd->buff + adapter->vpd->len)) {
Desnes Augusto Nunes do Rosario21a25452018-02-05 14:33:55 -02004316 strncpy((char *)adapter->fw_version, substr + 3, fw_level_len);
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02004317 } else {
4318 dev_info(dev, "FW substr extrapolated VPD buff\n");
4319 }
4320
4321complete:
Desnes Augusto Nunes do Rosario21a25452018-02-05 14:33:55 -02004322 if (adapter->fw_version[0] == '\0')
4323 strncpy((char *)adapter->fw_version, "N/A", 3 * sizeof(char));
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02004324 complete(&adapter->fw_done);
4325}
4326
Thomas Falcon032c5e82015-12-21 11:26:06 -06004327static void handle_query_ip_offload_rsp(struct ibmvnic_adapter *adapter)
4328{
4329 struct device *dev = &adapter->vdev->dev;
4330 struct ibmvnic_query_ip_offload_buffer *buf = &adapter->ip_offload_buf;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004331 int i;
4332
4333 dma_unmap_single(dev, adapter->ip_offload_tok,
4334 sizeof(adapter->ip_offload_buf), DMA_FROM_DEVICE);
4335
4336 netdev_dbg(adapter->netdev, "Query IP Offload Buffer:\n");
4337 for (i = 0; i < (sizeof(adapter->ip_offload_buf) - 1) / 8 + 1; i++)
4338 netdev_dbg(adapter->netdev, "%016lx\n",
4339 ((unsigned long int *)(buf))[i]);
4340
4341 netdev_dbg(adapter->netdev, "ipv4_chksum = %d\n", buf->ipv4_chksum);
4342 netdev_dbg(adapter->netdev, "ipv6_chksum = %d\n", buf->ipv6_chksum);
4343 netdev_dbg(adapter->netdev, "tcp_ipv4_chksum = %d\n",
4344 buf->tcp_ipv4_chksum);
4345 netdev_dbg(adapter->netdev, "tcp_ipv6_chksum = %d\n",
4346 buf->tcp_ipv6_chksum);
4347 netdev_dbg(adapter->netdev, "udp_ipv4_chksum = %d\n",
4348 buf->udp_ipv4_chksum);
4349 netdev_dbg(adapter->netdev, "udp_ipv6_chksum = %d\n",
4350 buf->udp_ipv6_chksum);
4351 netdev_dbg(adapter->netdev, "large_tx_ipv4 = %d\n",
4352 buf->large_tx_ipv4);
4353 netdev_dbg(adapter->netdev, "large_tx_ipv6 = %d\n",
4354 buf->large_tx_ipv6);
4355 netdev_dbg(adapter->netdev, "large_rx_ipv4 = %d\n",
4356 buf->large_rx_ipv4);
4357 netdev_dbg(adapter->netdev, "large_rx_ipv6 = %d\n",
4358 buf->large_rx_ipv6);
4359 netdev_dbg(adapter->netdev, "max_ipv4_hdr_sz = %d\n",
4360 buf->max_ipv4_header_size);
4361 netdev_dbg(adapter->netdev, "max_ipv6_hdr_sz = %d\n",
4362 buf->max_ipv6_header_size);
4363 netdev_dbg(adapter->netdev, "max_tcp_hdr_size = %d\n",
4364 buf->max_tcp_header_size);
4365 netdev_dbg(adapter->netdev, "max_udp_hdr_size = %d\n",
4366 buf->max_udp_header_size);
4367 netdev_dbg(adapter->netdev, "max_large_tx_size = %d\n",
4368 buf->max_large_tx_size);
4369 netdev_dbg(adapter->netdev, "max_large_rx_size = %d\n",
4370 buf->max_large_rx_size);
4371 netdev_dbg(adapter->netdev, "ipv6_ext_hdr = %d\n",
4372 buf->ipv6_extension_header);
4373 netdev_dbg(adapter->netdev, "tcp_pseudosum_req = %d\n",
4374 buf->tcp_pseudosum_req);
4375 netdev_dbg(adapter->netdev, "num_ipv6_ext_hd = %d\n",
4376 buf->num_ipv6_ext_headers);
4377 netdev_dbg(adapter->netdev, "off_ipv6_ext_hd = %d\n",
4378 buf->off_ipv6_ext_headers);
4379
Lijun Pan46899bd2020-09-27 20:13:30 -05004380 send_control_ip_offload(adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004381}
4382
Thomas Falconc9008d32018-08-06 21:39:59 -05004383static const char *ibmvnic_fw_err_cause(u16 cause)
4384{
4385 switch (cause) {
4386 case ADAPTER_PROBLEM:
4387 return "adapter problem";
4388 case BUS_PROBLEM:
4389 return "bus problem";
4390 case FW_PROBLEM:
4391 return "firmware problem";
4392 case DD_PROBLEM:
4393 return "device driver problem";
4394 case EEH_RECOVERY:
4395 return "EEH recovery";
4396 case FW_UPDATED:
4397 return "firmware updated";
4398 case LOW_MEMORY:
4399 return "low Memory";
4400 default:
4401 return "unknown";
4402 }
4403}
4404
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04004405static void handle_error_indication(union ibmvnic_crq *crq,
4406 struct ibmvnic_adapter *adapter)
4407{
4408 struct device *dev = &adapter->vdev->dev;
Thomas Falconc9008d32018-08-06 21:39:59 -05004409 u16 cause;
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04004410
Thomas Falconc9008d32018-08-06 21:39:59 -05004411 cause = be16_to_cpu(crq->error_indication.error_cause);
4412
4413 dev_warn_ratelimited(dev,
4414 "Firmware reports %serror, cause: %s. Starting recovery...\n",
4415 crq->error_indication.flags
4416 & IBMVNIC_FATAL_ERROR ? "FATAL " : "",
4417 ibmvnic_fw_err_cause(cause));
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04004418
Nathan Fontenoted651a12017-05-03 14:04:38 -04004419 if (crq->error_indication.flags & IBMVNIC_FATAL_ERROR)
4420 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
John Allen8cb31cf2017-05-26 10:30:37 -04004421 else
4422 ibmvnic_reset(adapter, VNIC_RESET_NON_FATAL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004423}
4424
Thomas Falconf8136142018-01-29 13:45:05 -06004425static int handle_change_mac_rsp(union ibmvnic_crq *crq,
4426 struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06004427{
4428 struct net_device *netdev = adapter->netdev;
4429 struct device *dev = &adapter->vdev->dev;
4430 long rc;
4431
4432 rc = crq->change_mac_addr_rsp.rc.code;
4433 if (rc) {
4434 dev_err(dev, "Error %ld in CHANGE_MAC_ADDR_RSP\n", rc);
Thomas Falconf8136142018-01-29 13:45:05 -06004435 goto out;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004436 }
Lijun Pand9b0e592020-10-20 17:39:19 -05004437 /* crq->change_mac_addr.mac_addr is the requested one
4438 * crq->change_mac_addr_rsp.mac_addr is the returned valid one.
4439 */
Thomas Falcon62740e92019-05-09 23:13:43 -05004440 ether_addr_copy(netdev->dev_addr,
4441 &crq->change_mac_addr_rsp.mac_addr[0]);
Lijun Pand9b0e592020-10-20 17:39:19 -05004442 ether_addr_copy(adapter->mac_addr,
4443 &crq->change_mac_addr_rsp.mac_addr[0]);
Thomas Falconf8136142018-01-29 13:45:05 -06004444out:
4445 complete(&adapter->fw_done);
4446 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004447}
4448
4449static void handle_request_cap_rsp(union ibmvnic_crq *crq,
4450 struct ibmvnic_adapter *adapter)
4451{
4452 struct device *dev = &adapter->vdev->dev;
4453 u64 *req_value;
4454 char *name;
4455
Thomas Falcon901e0402017-02-15 12:17:59 -06004456 atomic_dec(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004457 switch (be16_to_cpu(crq->request_capability_rsp.capability)) {
4458 case REQ_TX_QUEUES:
4459 req_value = &adapter->req_tx_queues;
4460 name = "tx";
4461 break;
4462 case REQ_RX_QUEUES:
4463 req_value = &adapter->req_rx_queues;
4464 name = "rx";
4465 break;
4466 case REQ_RX_ADD_QUEUES:
4467 req_value = &adapter->req_rx_add_queues;
4468 name = "rx_add";
4469 break;
4470 case REQ_TX_ENTRIES_PER_SUBCRQ:
4471 req_value = &adapter->req_tx_entries_per_subcrq;
4472 name = "tx_entries_per_subcrq";
4473 break;
4474 case REQ_RX_ADD_ENTRIES_PER_SUBCRQ:
4475 req_value = &adapter->req_rx_add_entries_per_subcrq;
4476 name = "rx_add_entries_per_subcrq";
4477 break;
4478 case REQ_MTU:
4479 req_value = &adapter->req_mtu;
4480 name = "mtu";
4481 break;
4482 case PROMISC_REQUESTED:
4483 req_value = &adapter->promisc;
4484 name = "promisc";
4485 break;
4486 default:
4487 dev_err(dev, "Got invalid cap request rsp %d\n",
4488 crq->request_capability.capability);
4489 return;
4490 }
4491
4492 switch (crq->request_capability_rsp.rc.code) {
4493 case SUCCESS:
4494 break;
4495 case PARTIALSUCCESS:
4496 dev_info(dev, "req=%lld, rsp=%ld in %s queue, retrying.\n",
4497 *req_value,
Thomas Falcon28f4d162017-02-15 10:32:11 -06004498 (long int)be64_to_cpu(crq->request_capability_rsp.
Thomas Falcon032c5e82015-12-21 11:26:06 -06004499 number), name);
John Allene7913802018-01-18 16:27:12 -06004500
4501 if (be16_to_cpu(crq->request_capability_rsp.capability) ==
4502 REQ_MTU) {
4503 pr_err("mtu of %llu is not supported. Reverting.\n",
4504 *req_value);
4505 *req_value = adapter->fallback.mtu;
4506 } else {
4507 *req_value =
4508 be64_to_cpu(crq->request_capability_rsp.number);
4509 }
4510
Lijun Pan09081b92020-09-27 20:13:27 -05004511 send_request_cap(adapter, 1);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004512 return;
4513 default:
4514 dev_err(dev, "Error %d in request cap rsp\n",
4515 crq->request_capability_rsp.rc.code);
4516 return;
4517 }
4518
4519 /* Done receiving requested capabilities, query IP offload support */
Thomas Falcon901e0402017-02-15 12:17:59 -06004520 if (atomic_read(&adapter->running_cap_crqs) == 0) {
Thomas Falcon249168a2017-02-15 12:18:00 -06004521 adapter->wait_capability = false;
Lijun Pan16e811f2020-09-27 20:13:29 -05004522 send_query_ip_offload(adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004523 }
4524}
4525
4526static int handle_login_rsp(union ibmvnic_crq *login_rsp_crq,
4527 struct ibmvnic_adapter *adapter)
4528{
4529 struct device *dev = &adapter->vdev->dev;
John Allenc26eba02017-10-26 16:23:25 -05004530 struct net_device *netdev = adapter->netdev;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004531 struct ibmvnic_login_rsp_buffer *login_rsp = adapter->login_rsp_buf;
4532 struct ibmvnic_login_buffer *login = adapter->login_buf;
Cristobal Fornof3ae59c2020-08-19 13:16:23 -05004533 u64 *tx_handle_array;
4534 u64 *rx_handle_array;
4535 int num_tx_pools;
4536 int num_rx_pools;
Thomas Falcon507ebe62020-08-21 13:39:01 -05004537 u64 *size_array;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004538 int i;
4539
Sukadev Bhattiprolu76cdc5c2020-11-25 18:04:29 -06004540 /* CHECK: Test/set of login_pending does not need to be atomic
4541 * because only ibmvnic_tasklet tests/clears this.
4542 */
4543 if (!adapter->login_pending) {
4544 netdev_warn(netdev, "Ignoring unexpected login response\n");
4545 return 0;
4546 }
4547 adapter->login_pending = false;
4548
Thomas Falcon032c5e82015-12-21 11:26:06 -06004549 dma_unmap_single(dev, adapter->login_buf_token, adapter->login_buf_sz,
Thomas Falcon37e40fa2018-04-06 18:37:02 -05004550 DMA_TO_DEVICE);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004551 dma_unmap_single(dev, adapter->login_rsp_buf_token,
Thomas Falcon37e40fa2018-04-06 18:37:02 -05004552 adapter->login_rsp_buf_sz, DMA_FROM_DEVICE);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004553
John Allen498cd8e2016-04-06 11:49:55 -05004554 /* If the number of queues requested can't be allocated by the
4555 * server, the login response will return with code 1. We will need
4556 * to resend the login buffer with fewer queues requested.
4557 */
4558 if (login_rsp_crq->generic.rc.code) {
Nathan Fontenot64d92aa2018-04-11 10:09:32 -05004559 adapter->init_done_rc = login_rsp_crq->generic.rc.code;
John Allen498cd8e2016-04-06 11:49:55 -05004560 complete(&adapter->init_done);
4561 return 0;
4562 }
4563
John Allenc26eba02017-10-26 16:23:25 -05004564 netdev->mtu = adapter->req_mtu - ETH_HLEN;
4565
Thomas Falcon032c5e82015-12-21 11:26:06 -06004566 netdev_dbg(adapter->netdev, "Login Response Buffer:\n");
4567 for (i = 0; i < (adapter->login_rsp_buf_sz - 1) / 8 + 1; i++) {
4568 netdev_dbg(adapter->netdev, "%016lx\n",
4569 ((unsigned long int *)(adapter->login_rsp_buf))[i]);
4570 }
4571
4572 /* Sanity checks */
4573 if (login->num_txcomp_subcrqs != login_rsp->num_txsubm_subcrqs ||
4574 (be32_to_cpu(login->num_rxcomp_subcrqs) *
4575 adapter->req_rx_add_queues !=
4576 be32_to_cpu(login_rsp->num_rxadd_subcrqs))) {
4577 dev_err(dev, "FATAL: Inconsistent login and login rsp\n");
Dany Madden31d6b402020-11-25 18:04:24 -06004578 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004579 return -EIO;
4580 }
Thomas Falcon507ebe62020-08-21 13:39:01 -05004581 size_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
4582 be32_to_cpu(adapter->login_rsp_buf->off_rxadd_buff_size));
4583 /* variable buffer sizes are not supported, so just read the
4584 * first entry.
4585 */
4586 adapter->cur_rx_buf_sz = be64_to_cpu(size_array[0]);
Cristobal Fornof3ae59c2020-08-19 13:16:23 -05004587
4588 num_tx_pools = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
4589 num_rx_pools = be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
4590
4591 tx_handle_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
4592 be32_to_cpu(adapter->login_rsp_buf->off_txsubm_subcrqs));
4593 rx_handle_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
4594 be32_to_cpu(adapter->login_rsp_buf->off_rxadd_subcrqs));
4595
4596 for (i = 0; i < num_tx_pools; i++)
4597 adapter->tx_scrq[i]->handle = tx_handle_array[i];
4598
4599 for (i = 0; i < num_rx_pools; i++)
4600 adapter->rx_scrq[i]->handle = rx_handle_array[i];
4601
Thomas Falcon507ebe62020-08-21 13:39:01 -05004602 adapter->num_active_tx_scrqs = num_tx_pools;
4603 adapter->num_active_rx_scrqs = num_rx_pools;
Cristobal Fornof3ae59c2020-08-19 13:16:23 -05004604 release_login_rsp_buffer(adapter);
Thomas Falcona2c0f032018-02-21 18:18:30 -06004605 release_login_buffer(adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004606 complete(&adapter->init_done);
4607
Thomas Falcon032c5e82015-12-21 11:26:06 -06004608 return 0;
4609}
4610
Thomas Falcon032c5e82015-12-21 11:26:06 -06004611static void handle_request_unmap_rsp(union ibmvnic_crq *crq,
4612 struct ibmvnic_adapter *adapter)
4613{
4614 struct device *dev = &adapter->vdev->dev;
4615 long rc;
4616
4617 rc = crq->request_unmap_rsp.rc.code;
4618 if (rc)
4619 dev_err(dev, "Error %ld in REQUEST_UNMAP_RSP\n", rc);
4620}
4621
4622static void handle_query_map_rsp(union ibmvnic_crq *crq,
4623 struct ibmvnic_adapter *adapter)
4624{
4625 struct net_device *netdev = adapter->netdev;
4626 struct device *dev = &adapter->vdev->dev;
4627 long rc;
4628
4629 rc = crq->query_map_rsp.rc.code;
4630 if (rc) {
4631 dev_err(dev, "Error %ld in QUERY_MAP_RSP\n", rc);
4632 return;
4633 }
4634 netdev_dbg(netdev, "page_size = %d\ntot_pages = %d\nfree_pages = %d\n",
4635 crq->query_map_rsp.page_size, crq->query_map_rsp.tot_pages,
4636 crq->query_map_rsp.free_pages);
4637}
4638
4639static void handle_query_cap_rsp(union ibmvnic_crq *crq,
4640 struct ibmvnic_adapter *adapter)
4641{
4642 struct net_device *netdev = adapter->netdev;
4643 struct device *dev = &adapter->vdev->dev;
4644 long rc;
4645
Thomas Falcon901e0402017-02-15 12:17:59 -06004646 atomic_dec(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004647 netdev_dbg(netdev, "Outstanding queries: %d\n",
Thomas Falcon901e0402017-02-15 12:17:59 -06004648 atomic_read(&adapter->running_cap_crqs));
Thomas Falcon032c5e82015-12-21 11:26:06 -06004649 rc = crq->query_capability.rc.code;
4650 if (rc) {
4651 dev_err(dev, "Error %ld in QUERY_CAP_RSP\n", rc);
4652 goto out;
4653 }
4654
4655 switch (be16_to_cpu(crq->query_capability.capability)) {
4656 case MIN_TX_QUEUES:
4657 adapter->min_tx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004658 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004659 netdev_dbg(netdev, "min_tx_queues = %lld\n",
4660 adapter->min_tx_queues);
4661 break;
4662 case MIN_RX_QUEUES:
4663 adapter->min_rx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004664 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004665 netdev_dbg(netdev, "min_rx_queues = %lld\n",
4666 adapter->min_rx_queues);
4667 break;
4668 case MIN_RX_ADD_QUEUES:
4669 adapter->min_rx_add_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004670 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004671 netdev_dbg(netdev, "min_rx_add_queues = %lld\n",
4672 adapter->min_rx_add_queues);
4673 break;
4674 case MAX_TX_QUEUES:
4675 adapter->max_tx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004676 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004677 netdev_dbg(netdev, "max_tx_queues = %lld\n",
4678 adapter->max_tx_queues);
4679 break;
4680 case MAX_RX_QUEUES:
4681 adapter->max_rx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004682 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004683 netdev_dbg(netdev, "max_rx_queues = %lld\n",
4684 adapter->max_rx_queues);
4685 break;
4686 case MAX_RX_ADD_QUEUES:
4687 adapter->max_rx_add_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004688 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004689 netdev_dbg(netdev, "max_rx_add_queues = %lld\n",
4690 adapter->max_rx_add_queues);
4691 break;
4692 case MIN_TX_ENTRIES_PER_SUBCRQ:
4693 adapter->min_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06004694 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004695 netdev_dbg(netdev, "min_tx_entries_per_subcrq = %lld\n",
4696 adapter->min_tx_entries_per_subcrq);
4697 break;
4698 case MIN_RX_ADD_ENTRIES_PER_SUBCRQ:
4699 adapter->min_rx_add_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06004700 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004701 netdev_dbg(netdev, "min_rx_add_entrs_per_subcrq = %lld\n",
4702 adapter->min_rx_add_entries_per_subcrq);
4703 break;
4704 case MAX_TX_ENTRIES_PER_SUBCRQ:
4705 adapter->max_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06004706 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004707 netdev_dbg(netdev, "max_tx_entries_per_subcrq = %lld\n",
4708 adapter->max_tx_entries_per_subcrq);
4709 break;
4710 case MAX_RX_ADD_ENTRIES_PER_SUBCRQ:
4711 adapter->max_rx_add_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06004712 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004713 netdev_dbg(netdev, "max_rx_add_entrs_per_subcrq = %lld\n",
4714 adapter->max_rx_add_entries_per_subcrq);
4715 break;
4716 case TCP_IP_OFFLOAD:
4717 adapter->tcp_ip_offload =
Thomas Falconde89e852016-03-01 10:20:09 -06004718 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004719 netdev_dbg(netdev, "tcp_ip_offload = %lld\n",
4720 adapter->tcp_ip_offload);
4721 break;
4722 case PROMISC_SUPPORTED:
4723 adapter->promisc_supported =
Thomas Falconde89e852016-03-01 10:20:09 -06004724 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004725 netdev_dbg(netdev, "promisc_supported = %lld\n",
4726 adapter->promisc_supported);
4727 break;
4728 case MIN_MTU:
Thomas Falconde89e852016-03-01 10:20:09 -06004729 adapter->min_mtu = be64_to_cpu(crq->query_capability.number);
Thomas Falconf39f0d12017-02-14 10:22:59 -06004730 netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004731 netdev_dbg(netdev, "min_mtu = %lld\n", adapter->min_mtu);
4732 break;
4733 case MAX_MTU:
Thomas Falconde89e852016-03-01 10:20:09 -06004734 adapter->max_mtu = be64_to_cpu(crq->query_capability.number);
Thomas Falconf39f0d12017-02-14 10:22:59 -06004735 netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004736 netdev_dbg(netdev, "max_mtu = %lld\n", adapter->max_mtu);
4737 break;
4738 case MAX_MULTICAST_FILTERS:
4739 adapter->max_multicast_filters =
Thomas Falconde89e852016-03-01 10:20:09 -06004740 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004741 netdev_dbg(netdev, "max_multicast_filters = %lld\n",
4742 adapter->max_multicast_filters);
4743 break;
4744 case VLAN_HEADER_INSERTION:
4745 adapter->vlan_header_insertion =
Thomas Falconde89e852016-03-01 10:20:09 -06004746 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004747 if (adapter->vlan_header_insertion)
4748 netdev->features |= NETIF_F_HW_VLAN_STAG_TX;
4749 netdev_dbg(netdev, "vlan_header_insertion = %lld\n",
4750 adapter->vlan_header_insertion);
4751 break;
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04004752 case RX_VLAN_HEADER_INSERTION:
4753 adapter->rx_vlan_header_insertion =
4754 be64_to_cpu(crq->query_capability.number);
4755 netdev_dbg(netdev, "rx_vlan_header_insertion = %lld\n",
4756 adapter->rx_vlan_header_insertion);
4757 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004758 case MAX_TX_SG_ENTRIES:
4759 adapter->max_tx_sg_entries =
Thomas Falconde89e852016-03-01 10:20:09 -06004760 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004761 netdev_dbg(netdev, "max_tx_sg_entries = %lld\n",
4762 adapter->max_tx_sg_entries);
4763 break;
4764 case RX_SG_SUPPORTED:
4765 adapter->rx_sg_supported =
Thomas Falconde89e852016-03-01 10:20:09 -06004766 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004767 netdev_dbg(netdev, "rx_sg_supported = %lld\n",
4768 adapter->rx_sg_supported);
4769 break;
4770 case OPT_TX_COMP_SUB_QUEUES:
4771 adapter->opt_tx_comp_sub_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004772 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004773 netdev_dbg(netdev, "opt_tx_comp_sub_queues = %lld\n",
4774 adapter->opt_tx_comp_sub_queues);
4775 break;
4776 case OPT_RX_COMP_QUEUES:
4777 adapter->opt_rx_comp_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004778 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004779 netdev_dbg(netdev, "opt_rx_comp_queues = %lld\n",
4780 adapter->opt_rx_comp_queues);
4781 break;
4782 case OPT_RX_BUFADD_Q_PER_RX_COMP_Q:
4783 adapter->opt_rx_bufadd_q_per_rx_comp_q =
Thomas Falconde89e852016-03-01 10:20:09 -06004784 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004785 netdev_dbg(netdev, "opt_rx_bufadd_q_per_rx_comp_q = %lld\n",
4786 adapter->opt_rx_bufadd_q_per_rx_comp_q);
4787 break;
4788 case OPT_TX_ENTRIES_PER_SUBCRQ:
4789 adapter->opt_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06004790 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004791 netdev_dbg(netdev, "opt_tx_entries_per_subcrq = %lld\n",
4792 adapter->opt_tx_entries_per_subcrq);
4793 break;
4794 case OPT_RXBA_ENTRIES_PER_SUBCRQ:
4795 adapter->opt_rxba_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06004796 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004797 netdev_dbg(netdev, "opt_rxba_entries_per_subcrq = %lld\n",
4798 adapter->opt_rxba_entries_per_subcrq);
4799 break;
4800 case TX_RX_DESC_REQ:
4801 adapter->tx_rx_desc_req = crq->query_capability.number;
4802 netdev_dbg(netdev, "tx_rx_desc_req = %llx\n",
4803 adapter->tx_rx_desc_req);
4804 break;
4805
4806 default:
4807 netdev_err(netdev, "Got invalid cap rsp %d\n",
4808 crq->query_capability.capability);
4809 }
4810
4811out:
Thomas Falcon249168a2017-02-15 12:18:00 -06004812 if (atomic_read(&adapter->running_cap_crqs) == 0) {
4813 adapter->wait_capability = false;
Lijun Pan09081b92020-09-27 20:13:27 -05004814 send_request_cap(adapter, 0);
Thomas Falcon249168a2017-02-15 12:18:00 -06004815 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06004816}
4817
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03004818static int send_query_phys_parms(struct ibmvnic_adapter *adapter)
4819{
4820 union ibmvnic_crq crq;
4821 int rc;
4822
4823 memset(&crq, 0, sizeof(crq));
4824 crq.query_phys_parms.first = IBMVNIC_CRQ_CMD;
4825 crq.query_phys_parms.cmd = QUERY_PHYS_PARMS;
Thomas Falconff25dcb2019-11-25 17:12:56 -06004826
4827 mutex_lock(&adapter->fw_lock);
4828 adapter->fw_done_rc = 0;
Thomas Falcon070eca92019-11-25 17:12:53 -06004829 reinit_completion(&adapter->fw_done);
Thomas Falconff25dcb2019-11-25 17:12:56 -06004830
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03004831 rc = ibmvnic_send_crq(adapter, &crq);
Thomas Falconff25dcb2019-11-25 17:12:56 -06004832 if (rc) {
4833 mutex_unlock(&adapter->fw_lock);
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03004834 return rc;
Thomas Falconff25dcb2019-11-25 17:12:56 -06004835 }
Thomas Falcon476d96c2019-11-25 17:12:55 -06004836
4837 rc = ibmvnic_wait_for_completion(adapter, &adapter->fw_done, 10000);
Thomas Falconff25dcb2019-11-25 17:12:56 -06004838 if (rc) {
4839 mutex_unlock(&adapter->fw_lock);
Thomas Falcon476d96c2019-11-25 17:12:55 -06004840 return rc;
Thomas Falconff25dcb2019-11-25 17:12:56 -06004841 }
Thomas Falcon476d96c2019-11-25 17:12:55 -06004842
Thomas Falconff25dcb2019-11-25 17:12:56 -06004843 mutex_unlock(&adapter->fw_lock);
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03004844 return adapter->fw_done_rc ? -EIO : 0;
4845}
4846
4847static int handle_query_phys_parms_rsp(union ibmvnic_crq *crq,
4848 struct ibmvnic_adapter *adapter)
4849{
4850 struct net_device *netdev = adapter->netdev;
4851 int rc;
Murilo Fossa Vicentinidd0f9d82019-09-16 11:50:37 -03004852 __be32 rspeed = cpu_to_be32(crq->query_phys_parms_rsp.speed);
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03004853
4854 rc = crq->query_phys_parms_rsp.rc.code;
4855 if (rc) {
4856 netdev_err(netdev, "Error %d in QUERY_PHYS_PARMS\n", rc);
4857 return rc;
4858 }
Murilo Fossa Vicentinidd0f9d82019-09-16 11:50:37 -03004859 switch (rspeed) {
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03004860 case IBMVNIC_10MBPS:
4861 adapter->speed = SPEED_10;
4862 break;
4863 case IBMVNIC_100MBPS:
4864 adapter->speed = SPEED_100;
4865 break;
4866 case IBMVNIC_1GBPS:
4867 adapter->speed = SPEED_1000;
4868 break;
Lijun Panb9cd7952020-09-27 19:06:25 -05004869 case IBMVNIC_10GBPS:
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03004870 adapter->speed = SPEED_10000;
4871 break;
4872 case IBMVNIC_25GBPS:
4873 adapter->speed = SPEED_25000;
4874 break;
4875 case IBMVNIC_40GBPS:
4876 adapter->speed = SPEED_40000;
4877 break;
4878 case IBMVNIC_50GBPS:
4879 adapter->speed = SPEED_50000;
4880 break;
4881 case IBMVNIC_100GBPS:
4882 adapter->speed = SPEED_100000;
4883 break;
Lijun Panb9cd7952020-09-27 19:06:25 -05004884 case IBMVNIC_200GBPS:
4885 adapter->speed = SPEED_200000;
4886 break;
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03004887 default:
Murilo Fossa Vicentinidd0f9d82019-09-16 11:50:37 -03004888 if (netif_carrier_ok(netdev))
4889 netdev_warn(netdev, "Unknown speed 0x%08x\n", rspeed);
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03004890 adapter->speed = SPEED_UNKNOWN;
4891 }
4892 if (crq->query_phys_parms_rsp.flags1 & IBMVNIC_FULL_DUPLEX)
4893 adapter->duplex = DUPLEX_FULL;
4894 else if (crq->query_phys_parms_rsp.flags1 & IBMVNIC_HALF_DUPLEX)
4895 adapter->duplex = DUPLEX_HALF;
4896 else
4897 adapter->duplex = DUPLEX_UNKNOWN;
4898
4899 return rc;
4900}
4901
Thomas Falcon032c5e82015-12-21 11:26:06 -06004902static void ibmvnic_handle_crq(union ibmvnic_crq *crq,
4903 struct ibmvnic_adapter *adapter)
4904{
4905 struct ibmvnic_generic_crq *gen_crq = &crq->generic;
4906 struct net_device *netdev = adapter->netdev;
4907 struct device *dev = &adapter->vdev->dev;
Murilo Fossa Vicentini993a82b2017-04-19 13:44:35 -04004908 u64 *u64_crq = (u64 *)crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004909 long rc;
4910
4911 netdev_dbg(netdev, "Handling CRQ: %016lx %016lx\n",
Murilo Fossa Vicentini993a82b2017-04-19 13:44:35 -04004912 (unsigned long int)cpu_to_be64(u64_crq[0]),
4913 (unsigned long int)cpu_to_be64(u64_crq[1]));
Thomas Falcon032c5e82015-12-21 11:26:06 -06004914 switch (gen_crq->first) {
4915 case IBMVNIC_CRQ_INIT_RSP:
4916 switch (gen_crq->cmd) {
4917 case IBMVNIC_CRQ_INIT:
4918 dev_info(dev, "Partner initialized\n");
John Allen017892c12017-05-26 10:30:19 -04004919 adapter->from_passive_init = true;
Sukadev Bhattiprolu76cdc5c2020-11-25 18:04:29 -06004920 /* Discard any stale login responses from prev reset.
4921 * CHECK: should we clear even on INIT_COMPLETE?
4922 */
4923 adapter->login_pending = false;
4924
Thomas Falcon17c87052018-05-23 13:37:58 -05004925 if (!completion_done(&adapter->init_done)) {
4926 complete(&adapter->init_done);
4927 adapter->init_done_rc = -EIO;
4928 }
Sukadev Bhattiproluef66a1e2021-02-02 21:08:02 -08004929 rc = ibmvnic_reset(adapter, VNIC_RESET_FAILOVER);
4930 if (rc && rc != -EBUSY) {
4931 /* We were unable to schedule the failover
4932 * reset either because the adapter was still
4933 * probing (eg: during kexec) or we could not
4934 * allocate memory. Clear the failover_pending
4935 * flag since no one else will. We ignore
4936 * EBUSY because it means either FAILOVER reset
4937 * is already scheduled or the adapter is
4938 * being removed.
4939 */
4940 netdev_err(netdev,
4941 "Error %ld scheduling failover reset\n",
4942 rc);
4943 adapter->failover_pending = false;
4944 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06004945 break;
4946 case IBMVNIC_CRQ_INIT_COMPLETE:
4947 dev_info(dev, "Partner initialization complete\n");
Thomas Falcon51536982018-05-23 13:37:56 -05004948 adapter->crq.active = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004949 send_version_xchg(adapter);
4950 break;
4951 default:
4952 dev_err(dev, "Unknown crq cmd: %d\n", gen_crq->cmd);
4953 }
4954 return;
4955 case IBMVNIC_CRQ_XPORT_EVENT:
Nathan Fontenoted651a12017-05-03 14:04:38 -04004956 netif_carrier_off(netdev);
Thomas Falcon51536982018-05-23 13:37:56 -05004957 adapter->crq.active = false;
Thomas Falcon2147e3d2019-11-25 17:12:54 -06004958 /* terminate any thread waiting for a response
4959 * from the device
4960 */
4961 if (!completion_done(&adapter->fw_done)) {
4962 adapter->fw_done_rc = -EIO;
4963 complete(&adapter->fw_done);
4964 }
4965 if (!completion_done(&adapter->stats_done))
4966 complete(&adapter->stats_done);
Juliet Kim7ed5b312019-09-20 16:11:23 -04004967 if (test_bit(0, &adapter->resetting))
Thomas Falcon2770a792018-05-23 13:38:02 -05004968 adapter->force_reset_recovery = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004969 if (gen_crq->cmd == IBMVNIC_PARTITION_MIGRATED) {
Nathan Fontenoted651a12017-05-03 14:04:38 -04004970 dev_info(dev, "Migrated, re-enabling adapter\n");
4971 ibmvnic_reset(adapter, VNIC_RESET_MOBILITY);
Thomas Falcondfad09a2016-08-18 11:37:51 -05004972 } else if (gen_crq->cmd == IBMVNIC_DEVICE_FAILOVER) {
4973 dev_info(dev, "Backing device failover detected\n");
Thomas Falcon5a18e1e2018-04-06 18:37:05 -05004974 adapter->failover_pending = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004975 } else {
4976 /* The adapter lost the connection */
4977 dev_err(dev, "Virtual Adapter failed (rc=%d)\n",
4978 gen_crq->cmd);
Nathan Fontenoted651a12017-05-03 14:04:38 -04004979 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004980 }
4981 return;
4982 case IBMVNIC_CRQ_CMD_RSP:
4983 break;
4984 default:
4985 dev_err(dev, "Got an invalid msg type 0x%02x\n",
4986 gen_crq->first);
4987 return;
4988 }
4989
4990 switch (gen_crq->cmd) {
4991 case VERSION_EXCHANGE_RSP:
4992 rc = crq->version_exchange_rsp.rc.code;
4993 if (rc) {
4994 dev_err(dev, "Error %ld in VERSION_EXCHG_RSP\n", rc);
4995 break;
4996 }
Thomas Falcon78468892020-05-28 11:19:17 -05004997 ibmvnic_version =
Thomas Falcon032c5e82015-12-21 11:26:06 -06004998 be16_to_cpu(crq->version_exchange_rsp.version);
Thomas Falcon78468892020-05-28 11:19:17 -05004999 dev_info(dev, "Partner protocol version is %d\n",
5000 ibmvnic_version);
Lijun Pan491099a2020-09-27 20:13:26 -05005001 send_query_cap(adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005002 break;
5003 case QUERY_CAPABILITY_RSP:
5004 handle_query_cap_rsp(crq, adapter);
5005 break;
5006 case QUERY_MAP_RSP:
5007 handle_query_map_rsp(crq, adapter);
5008 break;
5009 case REQUEST_MAP_RSP:
Thomas Falconf3be0cb2017-06-21 14:53:01 -05005010 adapter->fw_done_rc = crq->request_map_rsp.rc.code;
5011 complete(&adapter->fw_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005012 break;
5013 case REQUEST_UNMAP_RSP:
5014 handle_request_unmap_rsp(crq, adapter);
5015 break;
5016 case REQUEST_CAPABILITY_RSP:
5017 handle_request_cap_rsp(crq, adapter);
5018 break;
5019 case LOGIN_RSP:
5020 netdev_dbg(netdev, "Got Login Response\n");
5021 handle_login_rsp(crq, adapter);
5022 break;
5023 case LOGICAL_LINK_STATE_RSP:
Nathan Fontenot53da09e2017-04-21 15:39:04 -04005024 netdev_dbg(netdev,
5025 "Got Logical Link State Response, state: %d rc: %d\n",
5026 crq->logical_link_state_rsp.link_state,
5027 crq->logical_link_state_rsp.rc.code);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005028 adapter->logical_link_state =
5029 crq->logical_link_state_rsp.link_state;
Nathan Fontenot53da09e2017-04-21 15:39:04 -04005030 adapter->init_done_rc = crq->logical_link_state_rsp.rc.code;
5031 complete(&adapter->init_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005032 break;
5033 case LINK_STATE_INDICATION:
5034 netdev_dbg(netdev, "Got Logical Link State Indication\n");
5035 adapter->phys_link_state =
5036 crq->link_state_indication.phys_link_state;
5037 adapter->logical_link_state =
5038 crq->link_state_indication.logical_link_state;
Thomas Falcon0655f992019-05-09 23:13:44 -05005039 if (adapter->phys_link_state && adapter->logical_link_state)
5040 netif_carrier_on(netdev);
5041 else
5042 netif_carrier_off(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005043 break;
5044 case CHANGE_MAC_ADDR_RSP:
5045 netdev_dbg(netdev, "Got MAC address change Response\n");
Thomas Falconf8136142018-01-29 13:45:05 -06005046 adapter->fw_done_rc = handle_change_mac_rsp(crq, adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005047 break;
5048 case ERROR_INDICATION:
5049 netdev_dbg(netdev, "Got Error Indication\n");
5050 handle_error_indication(crq, adapter);
5051 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005052 case REQUEST_STATISTICS_RSP:
5053 netdev_dbg(netdev, "Got Statistics Response\n");
5054 complete(&adapter->stats_done);
5055 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005056 case QUERY_IP_OFFLOAD_RSP:
5057 netdev_dbg(netdev, "Got Query IP offload Response\n");
5058 handle_query_ip_offload_rsp(adapter);
5059 break;
5060 case MULTICAST_CTRL_RSP:
5061 netdev_dbg(netdev, "Got multicast control Response\n");
5062 break;
5063 case CONTROL_IP_OFFLOAD_RSP:
5064 netdev_dbg(netdev, "Got Control IP offload Response\n");
5065 dma_unmap_single(dev, adapter->ip_offload_ctrl_tok,
5066 sizeof(adapter->ip_offload_ctrl),
5067 DMA_TO_DEVICE);
John Allenbd0b6722017-03-17 17:13:40 -05005068 complete(&adapter->init_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005069 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005070 case COLLECT_FW_TRACE_RSP:
5071 netdev_dbg(netdev, "Got Collect firmware trace Response\n");
5072 complete(&adapter->fw_done);
5073 break;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02005074 case GET_VPD_SIZE_RSP:
5075 handle_vpd_size_rsp(crq, adapter);
5076 break;
5077 case GET_VPD_RSP:
5078 handle_vpd_rsp(crq, adapter);
5079 break;
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03005080 case QUERY_PHYS_PARMS_RSP:
5081 adapter->fw_done_rc = handle_query_phys_parms_rsp(crq, adapter);
5082 complete(&adapter->fw_done);
5083 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005084 default:
5085 netdev_err(netdev, "Got an invalid cmd type 0x%02x\n",
5086 gen_crq->cmd);
5087 }
5088}
5089
5090static irqreturn_t ibmvnic_interrupt(int irq, void *instance)
5091{
5092 struct ibmvnic_adapter *adapter = instance;
Thomas Falcon6c267b32017-02-15 12:17:58 -06005093
Thomas Falcon6c267b32017-02-15 12:17:58 -06005094 tasklet_schedule(&adapter->tasklet);
Thomas Falcon6c267b32017-02-15 12:17:58 -06005095 return IRQ_HANDLED;
5096}
5097
Allen Paisaa7c3fe2020-09-14 12:59:29 +05305098static void ibmvnic_tasklet(struct tasklet_struct *t)
Thomas Falcon6c267b32017-02-15 12:17:58 -06005099{
Allen Paisaa7c3fe2020-09-14 12:59:29 +05305100 struct ibmvnic_adapter *adapter = from_tasklet(adapter, t, tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005101 struct ibmvnic_crq_queue *queue = &adapter->crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005102 union ibmvnic_crq *crq;
5103 unsigned long flags;
5104 bool done = false;
5105
5106 spin_lock_irqsave(&queue->lock, flags);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005107 while (!done) {
5108 /* Pull all the valid messages off the CRQ */
5109 while ((crq = ibmvnic_next_crq(adapter)) != NULL) {
Lijun Pane41aec72021-01-27 19:34:42 -06005110 /* This barrier makes sure ibmvnic_next_crq()'s
5111 * crq->generic.first & IBMVNIC_CRQ_CMD_RSP is loaded
5112 * before ibmvnic_handle_crq()'s
5113 * switch(gen_crq->first) and switch(gen_crq->cmd).
5114 */
5115 dma_rmb();
Thomas Falcon032c5e82015-12-21 11:26:06 -06005116 ibmvnic_handle_crq(crq, adapter);
5117 crq->generic.first = 0;
5118 }
Brian Kinged7ecbf2017-04-19 13:44:53 -04005119
5120 /* remain in tasklet until all
5121 * capabilities responses are received
5122 */
5123 if (!adapter->wait_capability)
5124 done = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005125 }
Thomas Falcon249168a2017-02-15 12:18:00 -06005126 /* if capabilities CRQ's were sent in this tasklet, the following
5127 * tasklet must wait until all responses are received
5128 */
5129 if (atomic_read(&adapter->running_cap_crqs) != 0)
5130 adapter->wait_capability = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005131 spin_unlock_irqrestore(&queue->lock, flags);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005132}
5133
5134static int ibmvnic_reenable_crq_queue(struct ibmvnic_adapter *adapter)
5135{
5136 struct vio_dev *vdev = adapter->vdev;
5137 int rc;
5138
5139 do {
5140 rc = plpar_hcall_norets(H_ENABLE_CRQ, vdev->unit_address);
5141 } while (rc == H_IN_PROGRESS || rc == H_BUSY || H_IS_LONG_BUSY(rc));
5142
5143 if (rc)
5144 dev_err(&vdev->dev, "Error enabling adapter (rc=%d)\n", rc);
5145
5146 return rc;
5147}
5148
5149static int ibmvnic_reset_crq(struct ibmvnic_adapter *adapter)
5150{
5151 struct ibmvnic_crq_queue *crq = &adapter->crq;
5152 struct device *dev = &adapter->vdev->dev;
5153 struct vio_dev *vdev = adapter->vdev;
5154 int rc;
5155
5156 /* Close the CRQ */
5157 do {
5158 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
5159 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
5160
5161 /* Clean out the queue */
Lijun Pan0e435be2020-11-23 13:35:46 -06005162 if (!crq->msgs)
5163 return -EINVAL;
5164
Thomas Falcon032c5e82015-12-21 11:26:06 -06005165 memset(crq->msgs, 0, PAGE_SIZE);
5166 crq->cur = 0;
Thomas Falcon51536982018-05-23 13:37:56 -05005167 crq->active = false;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005168
5169 /* And re-open it again */
5170 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
5171 crq->msg_token, PAGE_SIZE);
5172
5173 if (rc == H_CLOSED)
5174 /* Adapter is good, but other end is not ready */
5175 dev_warn(dev, "Partner adapter not ready\n");
5176 else if (rc != 0)
5177 dev_warn(dev, "Couldn't register crq (rc=%d)\n", rc);
5178
5179 return rc;
5180}
5181
Nathan Fontenotf9928872017-03-30 02:48:54 -04005182static void release_crq_queue(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06005183{
5184 struct ibmvnic_crq_queue *crq = &adapter->crq;
5185 struct vio_dev *vdev = adapter->vdev;
5186 long rc;
5187
Nathan Fontenotf9928872017-03-30 02:48:54 -04005188 if (!crq->msgs)
5189 return;
5190
Thomas Falcon032c5e82015-12-21 11:26:06 -06005191 netdev_dbg(adapter->netdev, "Releasing CRQ\n");
5192 free_irq(vdev->irq, adapter);
Thomas Falcon6c267b32017-02-15 12:17:58 -06005193 tasklet_kill(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005194 do {
5195 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
5196 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
5197
5198 dma_unmap_single(&vdev->dev, crq->msg_token, PAGE_SIZE,
5199 DMA_BIDIRECTIONAL);
5200 free_page((unsigned long)crq->msgs);
Nathan Fontenotf9928872017-03-30 02:48:54 -04005201 crq->msgs = NULL;
Thomas Falcon51536982018-05-23 13:37:56 -05005202 crq->active = false;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005203}
5204
Nathan Fontenotf9928872017-03-30 02:48:54 -04005205static int init_crq_queue(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06005206{
5207 struct ibmvnic_crq_queue *crq = &adapter->crq;
5208 struct device *dev = &adapter->vdev->dev;
5209 struct vio_dev *vdev = adapter->vdev;
5210 int rc, retrc = -ENOMEM;
5211
Nathan Fontenotf9928872017-03-30 02:48:54 -04005212 if (crq->msgs)
5213 return 0;
5214
Thomas Falcon032c5e82015-12-21 11:26:06 -06005215 crq->msgs = (union ibmvnic_crq *)get_zeroed_page(GFP_KERNEL);
5216 /* Should we allocate more than one page? */
5217
5218 if (!crq->msgs)
5219 return -ENOMEM;
5220
5221 crq->size = PAGE_SIZE / sizeof(*crq->msgs);
5222 crq->msg_token = dma_map_single(dev, crq->msgs, PAGE_SIZE,
5223 DMA_BIDIRECTIONAL);
5224 if (dma_mapping_error(dev, crq->msg_token))
5225 goto map_failed;
5226
5227 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
5228 crq->msg_token, PAGE_SIZE);
5229
5230 if (rc == H_RESOURCE)
5231 /* maybe kexecing and resource is busy. try a reset */
5232 rc = ibmvnic_reset_crq(adapter);
5233 retrc = rc;
5234
5235 if (rc == H_CLOSED) {
5236 dev_warn(dev, "Partner adapter not ready\n");
5237 } else if (rc) {
5238 dev_warn(dev, "Error %d opening adapter\n", rc);
5239 goto reg_crq_failed;
5240 }
5241
5242 retrc = 0;
5243
Allen Paisaa7c3fe2020-09-14 12:59:29 +05305244 tasklet_setup(&adapter->tasklet, (void *)ibmvnic_tasklet);
Thomas Falcon6c267b32017-02-15 12:17:58 -06005245
Thomas Falcon032c5e82015-12-21 11:26:06 -06005246 netdev_dbg(adapter->netdev, "registering irq 0x%x\n", vdev->irq);
Murilo Fossa Vicentinie56e2512019-04-25 11:02:33 -03005247 snprintf(crq->name, sizeof(crq->name), "ibmvnic-%x",
5248 adapter->vdev->unit_address);
5249 rc = request_irq(vdev->irq, ibmvnic_interrupt, 0, crq->name, adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005250 if (rc) {
5251 dev_err(dev, "Couldn't register irq 0x%x. rc=%d\n",
5252 vdev->irq, rc);
5253 goto req_irq_failed;
5254 }
5255
5256 rc = vio_enable_interrupts(vdev);
5257 if (rc) {
5258 dev_err(dev, "Error %d enabling interrupts\n", rc);
5259 goto req_irq_failed;
5260 }
5261
5262 crq->cur = 0;
5263 spin_lock_init(&crq->lock);
5264
5265 return retrc;
5266
5267req_irq_failed:
Thomas Falcon6c267b32017-02-15 12:17:58 -06005268 tasklet_kill(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005269 do {
5270 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
5271 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
5272reg_crq_failed:
5273 dma_unmap_single(dev, crq->msg_token, PAGE_SIZE, DMA_BIDIRECTIONAL);
5274map_failed:
5275 free_page((unsigned long)crq->msgs);
Nathan Fontenotf9928872017-03-30 02:48:54 -04005276 crq->msgs = NULL;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005277 return retrc;
5278}
5279
Lijun Pan635e4422020-08-19 17:52:26 -05005280static int ibmvnic_reset_init(struct ibmvnic_adapter *adapter, bool reset)
John Allenf6ef6402017-03-17 17:13:42 -05005281{
5282 struct device *dev = &adapter->vdev->dev;
Dany Madden98c41f02020-11-25 18:04:32 -06005283 unsigned long timeout = msecs_to_jiffies(20000);
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06005284 u64 old_num_rx_queues, old_num_tx_queues;
John Allenf6ef6402017-03-17 17:13:42 -05005285 int rc;
5286
John Allen017892c12017-05-26 10:30:19 -04005287 adapter->from_passive_init = false;
5288
Lijun Pan635e4422020-08-19 17:52:26 -05005289 if (reset) {
5290 old_num_rx_queues = adapter->req_rx_queues;
5291 old_num_tx_queues = adapter->req_tx_queues;
5292 reinit_completion(&adapter->init_done);
5293 }
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06005294
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04005295 adapter->init_done_rc = 0;
Lijun Panfa68bfa2020-08-19 17:52:24 -05005296 rc = ibmvnic_send_crq_init(adapter);
5297 if (rc) {
5298 dev_err(dev, "Send crq init failed with error %d\n", rc);
5299 return rc;
5300 }
5301
John Allenf6ef6402017-03-17 17:13:42 -05005302 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
5303 dev_err(dev, "Initialization sequence timed out\n");
John Allen017892c12017-05-26 10:30:19 -04005304 return -1;
5305 }
5306
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04005307 if (adapter->init_done_rc) {
5308 release_crq_queue(adapter);
5309 return adapter->init_done_rc;
5310 }
5311
Lijun Pan785a2b12020-09-17 21:12:46 -05005312 if (adapter->from_passive_init) {
5313 adapter->state = VNIC_OPEN;
5314 adapter->from_passive_init = false;
5315 return -1;
5316 }
5317
Lijun Pan635e4422020-08-19 17:52:26 -05005318 if (reset &&
5319 test_bit(0, &adapter->resetting) && !adapter->wait_for_reset &&
Nathan Fontenot30f79622018-04-06 18:37:06 -05005320 adapter->reset_reason != VNIC_RESET_MOBILITY) {
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06005321 if (adapter->req_rx_queues != old_num_rx_queues ||
5322 adapter->req_tx_queues != old_num_tx_queues) {
5323 release_sub_crqs(adapter, 0);
5324 rc = init_sub_crqs(adapter);
5325 } else {
5326 rc = reset_sub_crq_queues(adapter);
5327 }
5328 } else {
Nathan Fontenot57a49432017-05-26 10:31:12 -04005329 rc = init_sub_crqs(adapter);
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06005330 }
5331
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04005332 if (rc) {
5333 dev_err(dev, "Initialization of sub crqs failed\n");
5334 release_crq_queue(adapter);
Thomas Falcon5df969c2017-06-28 19:55:54 -05005335 return rc;
5336 }
5337
5338 rc = init_sub_crq_irqs(adapter);
5339 if (rc) {
5340 dev_err(dev, "Failed to initialize sub crq irqs\n");
5341 release_crq_queue(adapter);
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04005342 }
5343
5344 return rc;
John Allenf6ef6402017-03-17 17:13:42 -05005345}
5346
Thomas Falcon40c9db82017-06-12 12:35:04 -05005347static struct device_attribute dev_attr_failover;
5348
Thomas Falcon032c5e82015-12-21 11:26:06 -06005349static int ibmvnic_probe(struct vio_dev *dev, const struct vio_device_id *id)
5350{
5351 struct ibmvnic_adapter *adapter;
5352 struct net_device *netdev;
5353 unsigned char *mac_addr_p;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005354 int rc;
5355
5356 dev_dbg(&dev->dev, "entering ibmvnic_probe for UA 0x%x\n",
5357 dev->unit_address);
5358
5359 mac_addr_p = (unsigned char *)vio_get_attribute(dev,
5360 VETH_MAC_ADDR, NULL);
5361 if (!mac_addr_p) {
5362 dev_err(&dev->dev,
5363 "(%s:%3.3d) ERROR: Can't find MAC_ADDR attribute\n",
5364 __FILE__, __LINE__);
5365 return 0;
5366 }
5367
5368 netdev = alloc_etherdev_mq(sizeof(struct ibmvnic_adapter),
Thomas Falcond45cc3a2017-12-18 12:52:11 -06005369 IBMVNIC_MAX_QUEUES);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005370 if (!netdev)
5371 return -ENOMEM;
5372
5373 adapter = netdev_priv(netdev);
Nathan Fontenot90c80142017-05-03 14:04:32 -04005374 adapter->state = VNIC_PROBING;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005375 dev_set_drvdata(&dev->dev, netdev);
5376 adapter->vdev = dev;
5377 adapter->netdev = netdev;
Sukadev Bhattiprolu76cdc5c2020-11-25 18:04:29 -06005378 adapter->login_pending = false;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005379
5380 ether_addr_copy(adapter->mac_addr, mac_addr_p);
5381 ether_addr_copy(netdev->dev_addr, adapter->mac_addr);
5382 netdev->irq = dev->irq;
5383 netdev->netdev_ops = &ibmvnic_netdev_ops;
5384 netdev->ethtool_ops = &ibmvnic_ethtool_ops;
5385 SET_NETDEV_DEV(netdev, &dev->dev);
5386
5387 spin_lock_init(&adapter->stats_lock);
5388
Nathan Fontenoted651a12017-05-03 14:04:38 -04005389 INIT_WORK(&adapter->ibmvnic_reset, __ibmvnic_reset);
Juliet Kim7ed5b312019-09-20 16:11:23 -04005390 INIT_DELAYED_WORK(&adapter->ibmvnic_delayed_reset,
5391 __ibmvnic_delayed_reset);
Nathan Fontenoted651a12017-05-03 14:04:38 -04005392 INIT_LIST_HEAD(&adapter->rwi_list);
Thomas Falcon6c5c7482018-12-10 15:22:22 -06005393 spin_lock_init(&adapter->rwi_lock);
Juliet Kim7d7195a2020-03-10 09:23:58 -05005394 spin_lock_init(&adapter->state_lock);
Thomas Falconff25dcb2019-11-25 17:12:56 -06005395 mutex_init(&adapter->fw_lock);
Thomas Falconbbd669a2019-04-04 18:58:26 -05005396 init_completion(&adapter->init_done);
Thomas Falcon070eca92019-11-25 17:12:53 -06005397 init_completion(&adapter->fw_done);
5398 init_completion(&adapter->reset_done);
5399 init_completion(&adapter->stats_done);
Juliet Kim7ed5b312019-09-20 16:11:23 -04005400 clear_bit(0, &adapter->resetting);
Nathan Fontenoted651a12017-05-03 14:04:38 -04005401
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04005402 do {
Nathan Fontenot30f79622018-04-06 18:37:06 -05005403 rc = init_crq_queue(adapter);
5404 if (rc) {
5405 dev_err(&dev->dev, "Couldn't initialize crq. rc=%d\n",
5406 rc);
5407 goto ibmvnic_init_fail;
5408 }
5409
Lijun Pan635e4422020-08-19 17:52:26 -05005410 rc = ibmvnic_reset_init(adapter, false);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05005411 if (rc && rc != EAGAIN)
5412 goto ibmvnic_init_fail;
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04005413 } while (rc == EAGAIN);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005414
Thomas Falcon07184212018-05-16 15:49:05 -05005415 rc = init_stats_buffers(adapter);
5416 if (rc)
5417 goto ibmvnic_init_fail;
5418
5419 rc = init_stats_token(adapter);
5420 if (rc)
5421 goto ibmvnic_stats_fail;
5422
Thomas Falconf39f0d12017-02-14 10:22:59 -06005423 netdev->mtu = adapter->req_mtu - ETH_HLEN;
John Allenc26eba02017-10-26 16:23:25 -05005424 netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
5425 netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005426
Thomas Falcon40c9db82017-06-12 12:35:04 -05005427 rc = device_create_file(&dev->dev, &dev_attr_failover);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05005428 if (rc)
Thomas Falcon07184212018-05-16 15:49:05 -05005429 goto ibmvnic_dev_file_err;
Thomas Falcon40c9db82017-06-12 12:35:04 -05005430
Mick Tarsele876a8a2017-09-28 13:53:18 -07005431 netif_carrier_off(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005432 rc = register_netdev(netdev);
5433 if (rc) {
5434 dev_err(&dev->dev, "failed to register netdev rc=%d\n", rc);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05005435 goto ibmvnic_register_fail;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005436 }
5437 dev_info(&dev->dev, "ibmvnic registered\n");
5438
Nathan Fontenot90c80142017-05-03 14:04:32 -04005439 adapter->state = VNIC_PROBED;
John Allenc26eba02017-10-26 16:23:25 -05005440
5441 adapter->wait_for_reset = false;
Dany Maddena86d5c62020-11-25 18:04:31 -06005442 adapter->last_reset_time = jiffies;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005443 return 0;
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05005444
5445ibmvnic_register_fail:
5446 device_remove_file(&dev->dev, &dev_attr_failover);
5447
Thomas Falcon07184212018-05-16 15:49:05 -05005448ibmvnic_dev_file_err:
5449 release_stats_token(adapter);
5450
5451ibmvnic_stats_fail:
5452 release_stats_buffers(adapter);
5453
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05005454ibmvnic_init_fail:
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06005455 release_sub_crqs(adapter, 1);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05005456 release_crq_queue(adapter);
Thomas Falconff25dcb2019-11-25 17:12:56 -06005457 mutex_destroy(&adapter->fw_lock);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05005458 free_netdev(netdev);
5459
5460 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005461}
5462
5463static int ibmvnic_remove(struct vio_dev *dev)
5464{
5465 struct net_device *netdev = dev_get_drvdata(&dev->dev);
Nathan Fontenot37489052017-04-19 13:45:04 -04005466 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Juliet Kim7d7195a2020-03-10 09:23:58 -05005467 unsigned long flags;
5468
5469 spin_lock_irqsave(&adapter->state_lock, flags);
Nathan Fontenot90c80142017-05-03 14:04:32 -04005470 adapter->state = VNIC_REMOVING;
Juliet Kim7d7195a2020-03-10 09:23:58 -05005471 spin_unlock_irqrestore(&adapter->state_lock, flags);
5472
Thomas Falcon6954a9e2020-06-12 13:34:41 -05005473 flush_work(&adapter->ibmvnic_reset);
5474 flush_delayed_work(&adapter->ibmvnic_delayed_reset);
5475
Juliet Kima5681e22018-11-19 15:59:22 -06005476 rtnl_lock();
5477 unregister_netdevice(netdev);
Nathan Fontenot37489052017-04-19 13:45:04 -04005478
5479 release_resources(adapter);
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06005480 release_sub_crqs(adapter, 1);
Nathan Fontenot37489052017-04-19 13:45:04 -04005481 release_crq_queue(adapter);
5482
Thomas Falcon53cc7722018-02-26 18:10:56 -06005483 release_stats_token(adapter);
5484 release_stats_buffers(adapter);
5485
Nathan Fontenot90c80142017-05-03 14:04:32 -04005486 adapter->state = VNIC_REMOVED;
5487
Juliet Kima5681e22018-11-19 15:59:22 -06005488 rtnl_unlock();
Thomas Falconff25dcb2019-11-25 17:12:56 -06005489 mutex_destroy(&adapter->fw_lock);
Thomas Falcon40c9db82017-06-12 12:35:04 -05005490 device_remove_file(&dev->dev, &dev_attr_failover);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005491 free_netdev(netdev);
5492 dev_set_drvdata(&dev->dev, NULL);
5493
5494 return 0;
5495}
5496
Thomas Falcon40c9db82017-06-12 12:35:04 -05005497static ssize_t failover_store(struct device *dev, struct device_attribute *attr,
5498 const char *buf, size_t count)
5499{
5500 struct net_device *netdev = dev_get_drvdata(dev);
5501 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
5502 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
5503 __be64 session_token;
5504 long rc;
5505
5506 if (!sysfs_streq(buf, "1"))
5507 return -EINVAL;
5508
5509 rc = plpar_hcall(H_VIOCTL, retbuf, adapter->vdev->unit_address,
5510 H_GET_SESSION_TOKEN, 0, 0, 0);
5511 if (rc) {
5512 netdev_err(netdev, "Couldn't retrieve session token, rc %ld\n",
5513 rc);
5514 return -EINVAL;
5515 }
5516
5517 session_token = (__be64)retbuf[0];
5518 netdev_dbg(netdev, "Initiating client failover, session id %llx\n",
5519 be64_to_cpu(session_token));
5520 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
5521 H_SESSION_ERR_DETECTED, session_token, 0, 0);
5522 if (rc) {
5523 netdev_err(netdev, "Client initiated failover failed, rc %ld\n",
5524 rc);
5525 return -EINVAL;
5526 }
5527
5528 return count;
5529}
5530
Joe Perches6cbaefb2017-12-19 10:15:09 -08005531static DEVICE_ATTR_WO(failover);
Thomas Falcon40c9db82017-06-12 12:35:04 -05005532
Thomas Falcon032c5e82015-12-21 11:26:06 -06005533static unsigned long ibmvnic_get_desired_dma(struct vio_dev *vdev)
5534{
5535 struct net_device *netdev = dev_get_drvdata(&vdev->dev);
5536 struct ibmvnic_adapter *adapter;
5537 struct iommu_table *tbl;
5538 unsigned long ret = 0;
5539 int i;
5540
5541 tbl = get_iommu_table_base(&vdev->dev);
5542
5543 /* netdev inits at probe time along with the structures we need below*/
5544 if (!netdev)
5545 return IOMMU_PAGE_ALIGN(IBMVNIC_IO_ENTITLEMENT_DEFAULT, tbl);
5546
5547 adapter = netdev_priv(netdev);
5548
5549 ret += PAGE_SIZE; /* the crq message queue */
Thomas Falcon032c5e82015-12-21 11:26:06 -06005550 ret += IOMMU_PAGE_ALIGN(sizeof(struct ibmvnic_statistics), tbl);
5551
5552 for (i = 0; i < adapter->req_tx_queues + adapter->req_rx_queues; i++)
5553 ret += 4 * PAGE_SIZE; /* the scrq message queue */
5554
Thomas Falcon507ebe62020-08-21 13:39:01 -05005555 for (i = 0; i < adapter->num_active_rx_pools; i++)
Thomas Falcon032c5e82015-12-21 11:26:06 -06005556 ret += adapter->rx_pool[i].size *
5557 IOMMU_PAGE_ALIGN(adapter->rx_pool[i].buff_size, tbl);
5558
5559 return ret;
5560}
5561
5562static int ibmvnic_resume(struct device *dev)
5563{
5564 struct net_device *netdev = dev_get_drvdata(dev);
5565 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005566
John Allencb89ba22017-06-19 11:27:53 -05005567 if (adapter->state != VNIC_OPEN)
5568 return 0;
5569
John Allena2488782017-07-24 13:26:06 -05005570 tasklet_schedule(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005571
5572 return 0;
5573}
5574
Arvind Yadav8c37bc62017-08-17 18:52:54 +05305575static const struct vio_device_id ibmvnic_device_table[] = {
Thomas Falcon032c5e82015-12-21 11:26:06 -06005576 {"network", "IBM,vnic"},
5577 {"", "" }
5578};
5579MODULE_DEVICE_TABLE(vio, ibmvnic_device_table);
5580
5581static const struct dev_pm_ops ibmvnic_pm_ops = {
5582 .resume = ibmvnic_resume
5583};
5584
5585static struct vio_driver ibmvnic_driver = {
5586 .id_table = ibmvnic_device_table,
5587 .probe = ibmvnic_probe,
5588 .remove = ibmvnic_remove,
5589 .get_desired_dma = ibmvnic_get_desired_dma,
5590 .name = ibmvnic_driver_name,
5591 .pm = &ibmvnic_pm_ops,
5592};
5593
5594/* module functions */
5595static int __init ibmvnic_module_init(void)
5596{
5597 pr_info("%s: %s %s\n", ibmvnic_driver_name, ibmvnic_driver_string,
5598 IBMVNIC_DRIVER_VERSION);
5599
5600 return vio_register_driver(&ibmvnic_driver);
5601}
5602
5603static void __exit ibmvnic_module_exit(void)
5604{
5605 vio_unregister_driver(&ibmvnic_driver);
5606}
5607
5608module_init(ibmvnic_module_init);
5609module_exit(ibmvnic_module_exit);