blob: 17ba6db6f5f9b405cb2d8def6c2084e7166a1b80 [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 *);
87static int send_subcrq(struct ibmvnic_adapter *adapter, u64 remote_handle,
88 union sub_crq *sub_crq);
Thomas Falconad7775d2016-04-01 17:20:34 -050089static int send_subcrq_indirect(struct ibmvnic_adapter *, u64, u64, u64);
Thomas Falcon032c5e82015-12-21 11:26:06 -060090static irqreturn_t ibmvnic_interrupt_rx(int irq, void *instance);
91static int enable_scrq_irq(struct ibmvnic_adapter *,
92 struct ibmvnic_sub_crq_queue *);
93static int disable_scrq_irq(struct ibmvnic_adapter *,
94 struct ibmvnic_sub_crq_queue *);
95static int pending_scrq(struct ibmvnic_adapter *,
96 struct ibmvnic_sub_crq_queue *);
97static union sub_crq *ibmvnic_next_scrq(struct ibmvnic_adapter *,
98 struct ibmvnic_sub_crq_queue *);
99static int ibmvnic_poll(struct napi_struct *napi, int data);
Lijun Pan69980d02020-09-27 20:13:28 -0500100static void send_query_map(struct ibmvnic_adapter *adapter);
Thomas Falcon9c4eaab2018-05-23 13:37:57 -0500101static int send_request_map(struct ibmvnic_adapter *, dma_addr_t, __be32, u8);
102static int send_request_unmap(struct ibmvnic_adapter *, u8);
Thomas Falcon20a8ab72018-02-26 18:10:59 -0600103static int send_login(struct ibmvnic_adapter *adapter);
Lijun Pan491099a2020-09-27 20:13:26 -0500104static void send_query_cap(struct ibmvnic_adapter *adapter);
Thomas Falcon4d96f122017-08-01 15:04:36 -0500105static int init_sub_crqs(struct ibmvnic_adapter *);
John Allenbd0b6722017-03-17 17:13:40 -0500106static int init_sub_crq_irqs(struct ibmvnic_adapter *adapter);
Lijun Pan635e4422020-08-19 17:52:26 -0500107static int ibmvnic_reset_init(struct ibmvnic_adapter *, bool reset);
Nathan Fontenotf9928872017-03-30 02:48:54 -0400108static void release_crq_queue(struct ibmvnic_adapter *);
Thomas Falcon62740e92019-05-09 23:13:43 -0500109static int __ibmvnic_set_mac(struct net_device *, u8 *);
Nathan Fontenot30f79622018-04-06 18:37:06 -0500110static int init_crq_queue(struct ibmvnic_adapter *adapter);
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -0300111static int send_query_phys_parms(struct ibmvnic_adapter *adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600112
113struct ibmvnic_stat {
114 char name[ETH_GSTRING_LEN];
115 int offset;
116};
117
118#define IBMVNIC_STAT_OFF(stat) (offsetof(struct ibmvnic_adapter, stats) + \
119 offsetof(struct ibmvnic_statistics, stat))
120#define IBMVNIC_GET_STAT(a, off) (*((u64 *)(((unsigned long)(a)) + off)))
121
122static const struct ibmvnic_stat ibmvnic_stats[] = {
123 {"rx_packets", IBMVNIC_STAT_OFF(rx_packets)},
124 {"rx_bytes", IBMVNIC_STAT_OFF(rx_bytes)},
125 {"tx_packets", IBMVNIC_STAT_OFF(tx_packets)},
126 {"tx_bytes", IBMVNIC_STAT_OFF(tx_bytes)},
127 {"ucast_tx_packets", IBMVNIC_STAT_OFF(ucast_tx_packets)},
128 {"ucast_rx_packets", IBMVNIC_STAT_OFF(ucast_rx_packets)},
129 {"mcast_tx_packets", IBMVNIC_STAT_OFF(mcast_tx_packets)},
130 {"mcast_rx_packets", IBMVNIC_STAT_OFF(mcast_rx_packets)},
131 {"bcast_tx_packets", IBMVNIC_STAT_OFF(bcast_tx_packets)},
132 {"bcast_rx_packets", IBMVNIC_STAT_OFF(bcast_rx_packets)},
133 {"align_errors", IBMVNIC_STAT_OFF(align_errors)},
134 {"fcs_errors", IBMVNIC_STAT_OFF(fcs_errors)},
135 {"single_collision_frames", IBMVNIC_STAT_OFF(single_collision_frames)},
136 {"multi_collision_frames", IBMVNIC_STAT_OFF(multi_collision_frames)},
137 {"sqe_test_errors", IBMVNIC_STAT_OFF(sqe_test_errors)},
138 {"deferred_tx", IBMVNIC_STAT_OFF(deferred_tx)},
139 {"late_collisions", IBMVNIC_STAT_OFF(late_collisions)},
140 {"excess_collisions", IBMVNIC_STAT_OFF(excess_collisions)},
141 {"internal_mac_tx_errors", IBMVNIC_STAT_OFF(internal_mac_tx_errors)},
142 {"carrier_sense", IBMVNIC_STAT_OFF(carrier_sense)},
143 {"too_long_frames", IBMVNIC_STAT_OFF(too_long_frames)},
144 {"internal_mac_rx_errors", IBMVNIC_STAT_OFF(internal_mac_rx_errors)},
145};
146
147static long h_reg_sub_crq(unsigned long unit_address, unsigned long token,
148 unsigned long length, unsigned long *number,
149 unsigned long *irq)
150{
151 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
152 long rc;
153
154 rc = plpar_hcall(H_REG_SUB_CRQ, retbuf, unit_address, token, length);
155 *number = retbuf[0];
156 *irq = retbuf[1];
157
158 return rc;
159}
160
Thomas Falcon476d96c2019-11-25 17:12:55 -0600161/**
162 * ibmvnic_wait_for_completion - Check device state and wait for completion
163 * @adapter: private device data
164 * @comp_done: completion structure to wait for
165 * @timeout: time to wait in milliseconds
166 *
167 * Wait for a completion signal or until the timeout limit is reached
168 * while checking that the device is still active.
169 */
170static int ibmvnic_wait_for_completion(struct ibmvnic_adapter *adapter,
171 struct completion *comp_done,
172 unsigned long timeout)
173{
174 struct net_device *netdev;
175 unsigned long div_timeout;
176 u8 retry;
177
178 netdev = adapter->netdev;
179 retry = 5;
180 div_timeout = msecs_to_jiffies(timeout / retry);
181 while (true) {
182 if (!adapter->crq.active) {
183 netdev_err(netdev, "Device down!\n");
184 return -ENODEV;
185 }
Thomas Falcon8f9cc1e2019-12-11 09:38:39 -0600186 if (!retry--)
Thomas Falcon476d96c2019-11-25 17:12:55 -0600187 break;
188 if (wait_for_completion_timeout(comp_done, div_timeout))
189 return 0;
190 }
191 netdev_err(netdev, "Operation timed out.\n");
192 return -ETIMEDOUT;
193}
194
Thomas Falcon032c5e82015-12-21 11:26:06 -0600195static int alloc_long_term_buff(struct ibmvnic_adapter *adapter,
196 struct ibmvnic_long_term_buff *ltb, int size)
197{
198 struct device *dev = &adapter->vdev->dev;
Thomas Falcon9c4eaab2018-05-23 13:37:57 -0500199 int rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600200
201 ltb->size = size;
202 ltb->buff = dma_alloc_coherent(dev, ltb->size, &ltb->addr,
203 GFP_KERNEL);
204
205 if (!ltb->buff) {
206 dev_err(dev, "Couldn't alloc long term buffer\n");
207 return -ENOMEM;
208 }
209 ltb->map_id = adapter->map_id;
210 adapter->map_id++;
Nathan Fontenotdb5d0b52017-02-10 13:45:05 -0500211
Thomas Falconff25dcb2019-11-25 17:12:56 -0600212 mutex_lock(&adapter->fw_lock);
213 adapter->fw_done_rc = 0;
Thomas Falcon070eca92019-11-25 17:12:53 -0600214 reinit_completion(&adapter->fw_done);
Thomas Falcon9c4eaab2018-05-23 13:37:57 -0500215 rc = send_request_map(adapter, ltb->addr,
216 ltb->size, ltb->map_id);
217 if (rc) {
218 dma_free_coherent(dev, ltb->size, ltb->buff, ltb->addr);
Thomas Falconff25dcb2019-11-25 17:12:56 -0600219 mutex_unlock(&adapter->fw_lock);
Thomas Falcon9c4eaab2018-05-23 13:37:57 -0500220 return rc;
221 }
Thomas Falcon476d96c2019-11-25 17:12:55 -0600222
223 rc = ibmvnic_wait_for_completion(adapter, &adapter->fw_done, 10000);
224 if (rc) {
225 dev_err(dev,
226 "Long term map request aborted or timed out,rc = %d\n",
227 rc);
228 dma_free_coherent(dev, ltb->size, ltb->buff, ltb->addr);
Thomas Falconff25dcb2019-11-25 17:12:56 -0600229 mutex_unlock(&adapter->fw_lock);
Thomas Falcon476d96c2019-11-25 17:12:55 -0600230 return rc;
231 }
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500232
233 if (adapter->fw_done_rc) {
234 dev_err(dev, "Couldn't map long term buffer,rc = %d\n",
235 adapter->fw_done_rc);
Thomas Falcon4cf2ddf32018-05-16 15:49:03 -0500236 dma_free_coherent(dev, ltb->size, ltb->buff, ltb->addr);
Thomas Falconff25dcb2019-11-25 17:12:56 -0600237 mutex_unlock(&adapter->fw_lock);
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500238 return -1;
239 }
Thomas Falconff25dcb2019-11-25 17:12:56 -0600240 mutex_unlock(&adapter->fw_lock);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600241 return 0;
242}
243
244static void free_long_term_buff(struct ibmvnic_adapter *adapter,
245 struct ibmvnic_long_term_buff *ltb)
246{
247 struct device *dev = &adapter->vdev->dev;
248
Nathan Fontenotc657e322017-03-30 02:49:06 -0400249 if (!ltb->buff)
250 return;
251
Nathan Fontenoted651a12017-05-03 14:04:38 -0400252 if (adapter->reset_reason != VNIC_RESET_FAILOVER &&
253 adapter->reset_reason != VNIC_RESET_MOBILITY)
Thomas Falcondfad09a2016-08-18 11:37:51 -0500254 send_request_unmap(adapter, ltb->map_id);
Brian King59af56c2017-04-19 13:44:41 -0400255 dma_free_coherent(dev, ltb->size, ltb->buff, ltb->addr);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600256}
257
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500258static int reset_long_term_buff(struct ibmvnic_adapter *adapter,
259 struct ibmvnic_long_term_buff *ltb)
260{
Thomas Falcon476d96c2019-11-25 17:12:55 -0600261 struct device *dev = &adapter->vdev->dev;
Thomas Falcon9c4eaab2018-05-23 13:37:57 -0500262 int rc;
263
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500264 memset(ltb->buff, 0, ltb->size);
265
Thomas Falconff25dcb2019-11-25 17:12:56 -0600266 mutex_lock(&adapter->fw_lock);
267 adapter->fw_done_rc = 0;
268
Thomas Falcon070eca92019-11-25 17:12:53 -0600269 reinit_completion(&adapter->fw_done);
Thomas Falcon9c4eaab2018-05-23 13:37:57 -0500270 rc = send_request_map(adapter, ltb->addr, ltb->size, ltb->map_id);
Thomas Falconff25dcb2019-11-25 17:12:56 -0600271 if (rc) {
272 mutex_unlock(&adapter->fw_lock);
Thomas Falcon9c4eaab2018-05-23 13:37:57 -0500273 return rc;
Thomas Falconff25dcb2019-11-25 17:12:56 -0600274 }
Thomas Falcon476d96c2019-11-25 17:12:55 -0600275
276 rc = ibmvnic_wait_for_completion(adapter, &adapter->fw_done, 10000);
277 if (rc) {
278 dev_info(dev,
279 "Reset failed, long term map request timed out or aborted\n");
Thomas Falconff25dcb2019-11-25 17:12:56 -0600280 mutex_unlock(&adapter->fw_lock);
Thomas Falcon476d96c2019-11-25 17:12:55 -0600281 return rc;
282 }
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500283
284 if (adapter->fw_done_rc) {
Thomas Falcon476d96c2019-11-25 17:12:55 -0600285 dev_info(dev,
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500286 "Reset failed, attempting to free and reallocate buffer\n");
287 free_long_term_buff(adapter, ltb);
Thomas Falconff25dcb2019-11-25 17:12:56 -0600288 mutex_unlock(&adapter->fw_lock);
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500289 return alloc_long_term_buff(adapter, ltb, ltb->size);
290 }
Thomas Falconff25dcb2019-11-25 17:12:56 -0600291 mutex_unlock(&adapter->fw_lock);
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500292 return 0;
293}
294
Thomas Falconf185a492017-05-26 10:30:48 -0400295static void deactivate_rx_pools(struct ibmvnic_adapter *adapter)
296{
297 int i;
298
Thomas Falcon507ebe62020-08-21 13:39:01 -0500299 for (i = 0; i < adapter->num_active_rx_pools; i++)
Thomas Falconf185a492017-05-26 10:30:48 -0400300 adapter->rx_pool[i].active = 0;
301}
302
Thomas Falcon032c5e82015-12-21 11:26:06 -0600303static void replenish_rx_pool(struct ibmvnic_adapter *adapter,
304 struct ibmvnic_rx_pool *pool)
305{
306 int count = pool->size - atomic_read(&pool->available);
Cristobal Fornof3ae59c2020-08-19 13:16:23 -0500307 u64 handle = adapter->rx_scrq[pool->index]->handle;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600308 struct device *dev = &adapter->vdev->dev;
Thomas Falcon4f0b6812020-11-18 19:12:18 -0600309 struct ibmvnic_ind_xmit_queue *ind_bufp;
310 struct ibmvnic_sub_crq_queue *rx_scrq;
311 union sub_crq *sub_crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600312 int buffers_added = 0;
313 unsigned long lpar_rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600314 struct sk_buff *skb;
315 unsigned int offset;
316 dma_addr_t dma_addr;
317 unsigned char *dst;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600318 int shift = 0;
319 int index;
320 int i;
321
Thomas Falconf185a492017-05-26 10:30:48 -0400322 if (!pool->active)
323 return;
324
Thomas Falcon4f0b6812020-11-18 19:12:18 -0600325 rx_scrq = adapter->rx_scrq[pool->index];
326 ind_bufp = &rx_scrq->ind_buf;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600327 for (i = 0; i < count; ++i) {
328 skb = alloc_skb(pool->buff_size, GFP_ATOMIC);
329 if (!skb) {
330 dev_err(dev, "Couldn't replenish rx buff\n");
331 adapter->replenish_no_mem++;
332 break;
333 }
334
335 index = pool->free_map[pool->next_free];
336
337 if (pool->rx_buff[index].skb)
338 dev_err(dev, "Inconsistent free_map!\n");
339
340 /* Copy the skb to the long term mapped DMA buffer */
341 offset = index * pool->buff_size;
342 dst = pool->long_term_buff.buff + offset;
343 memset(dst, 0, pool->buff_size);
344 dma_addr = pool->long_term_buff.addr + offset;
345 pool->rx_buff[index].data = dst;
346
347 pool->free_map[pool->next_free] = IBMVNIC_INVALID_MAP;
348 pool->rx_buff[index].dma = dma_addr;
349 pool->rx_buff[index].skb = skb;
350 pool->rx_buff[index].pool_index = pool->index;
351 pool->rx_buff[index].size = pool->buff_size;
352
Thomas Falcon4f0b6812020-11-18 19:12:18 -0600353 sub_crq = &ind_bufp->indir_arr[ind_bufp->index++];
354 memset(sub_crq, 0, sizeof(*sub_crq));
355 sub_crq->rx_add.first = IBMVNIC_CRQ_CMD;
356 sub_crq->rx_add.correlator =
Thomas Falcon032c5e82015-12-21 11:26:06 -0600357 cpu_to_be64((u64)&pool->rx_buff[index]);
Thomas Falcon4f0b6812020-11-18 19:12:18 -0600358 sub_crq->rx_add.ioba = cpu_to_be32(dma_addr);
359 sub_crq->rx_add.map_id = pool->long_term_buff.map_id;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600360
361 /* The length field of the sCRQ is defined to be 24 bits so the
362 * buffer size needs to be left shifted by a byte before it is
363 * converted to big endian to prevent the last byte from being
364 * truncated.
365 */
366#ifdef __LITTLE_ENDIAN__
367 shift = 8;
368#endif
Thomas Falcon4f0b6812020-11-18 19:12:18 -0600369 sub_crq->rx_add.len = cpu_to_be32(pool->buff_size << shift);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600370 pool->next_free = (pool->next_free + 1) % pool->size;
Thomas Falcon4f0b6812020-11-18 19:12:18 -0600371 if (ind_bufp->index == IBMVNIC_MAX_IND_DESCS ||
372 i == count - 1) {
373 lpar_rc =
374 send_subcrq_indirect(adapter, handle,
375 (u64)ind_bufp->indir_dma,
376 (u64)ind_bufp->index);
377 if (lpar_rc != H_SUCCESS)
378 goto failure;
379 buffers_added += ind_bufp->index;
380 adapter->replenish_add_buff_success += ind_bufp->index;
381 ind_bufp->index = 0;
382 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600383 }
384 atomic_add(buffers_added, &pool->available);
385 return;
386
387failure:
Thomas Falcon2d14d372018-07-13 12:03:32 -0500388 if (lpar_rc != H_PARAMETER && lpar_rc != H_CLOSED)
389 dev_err_ratelimited(dev, "rx: replenish packet buffer failed\n");
Thomas Falcon4f0b6812020-11-18 19:12:18 -0600390 for (i = ind_bufp->index - 1; i >= 0; --i) {
391 struct ibmvnic_rx_buff *rx_buff;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600392
Thomas Falcon4f0b6812020-11-18 19:12:18 -0600393 pool->next_free = pool->next_free == 0 ?
394 pool->size - 1 : pool->next_free - 1;
395 sub_crq = &ind_bufp->indir_arr[i];
396 rx_buff = (struct ibmvnic_rx_buff *)
397 be64_to_cpu(sub_crq->rx_add.correlator);
398 index = (int)(rx_buff - pool->rx_buff);
399 pool->free_map[pool->next_free] = index;
400 dev_kfree_skb_any(pool->rx_buff[index].skb);
401 pool->rx_buff[index].skb = NULL;
402 }
403 ind_bufp->index = 0;
Thomas Falcon5a18e1e2018-04-06 18:37:05 -0500404 if (lpar_rc == H_CLOSED || adapter->failover_pending) {
Thomas Falconf185a492017-05-26 10:30:48 -0400405 /* Disable buffer pool replenishment and report carrier off if
Thomas Falcon5a18e1e2018-04-06 18:37:05 -0500406 * queue is closed or pending failover.
407 * Firmware guarantees that a signal will be sent to the
408 * driver, triggering a reset.
Thomas Falconf185a492017-05-26 10:30:48 -0400409 */
410 deactivate_rx_pools(adapter);
411 netif_carrier_off(adapter->netdev);
412 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600413}
414
415static void replenish_pools(struct ibmvnic_adapter *adapter)
416{
417 int i;
418
Thomas Falcon032c5e82015-12-21 11:26:06 -0600419 adapter->replenish_task_cycles++;
Thomas Falcon507ebe62020-08-21 13:39:01 -0500420 for (i = 0; i < adapter->num_active_rx_pools; i++) {
Thomas Falcon032c5e82015-12-21 11:26:06 -0600421 if (adapter->rx_pool[i].active)
422 replenish_rx_pool(adapter, &adapter->rx_pool[i]);
423 }
424}
425
John Allen3d52b592017-08-02 16:44:14 -0500426static void release_stats_buffers(struct ibmvnic_adapter *adapter)
427{
428 kfree(adapter->tx_stats_buffers);
429 kfree(adapter->rx_stats_buffers);
Thomas Falconb0992ec2018-02-06 17:25:23 -0600430 adapter->tx_stats_buffers = NULL;
431 adapter->rx_stats_buffers = NULL;
John Allen3d52b592017-08-02 16:44:14 -0500432}
433
434static int init_stats_buffers(struct ibmvnic_adapter *adapter)
435{
436 adapter->tx_stats_buffers =
Nathan Fontenotabcae542018-02-19 13:30:47 -0600437 kcalloc(IBMVNIC_MAX_QUEUES,
John Allen3d52b592017-08-02 16:44:14 -0500438 sizeof(struct ibmvnic_tx_queue_stats),
439 GFP_KERNEL);
440 if (!adapter->tx_stats_buffers)
441 return -ENOMEM;
442
443 adapter->rx_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_rx_queue_stats),
446 GFP_KERNEL);
447 if (!adapter->rx_stats_buffers)
448 return -ENOMEM;
449
450 return 0;
451}
452
Nathan Fontenot7bbc27a2017-03-30 02:49:23 -0400453static void release_stats_token(struct ibmvnic_adapter *adapter)
454{
455 struct device *dev = &adapter->vdev->dev;
456
457 if (!adapter->stats_token)
458 return;
459
460 dma_unmap_single(dev, adapter->stats_token,
461 sizeof(struct ibmvnic_statistics),
462 DMA_FROM_DEVICE);
463 adapter->stats_token = 0;
464}
465
466static int init_stats_token(struct ibmvnic_adapter *adapter)
467{
468 struct device *dev = &adapter->vdev->dev;
469 dma_addr_t stok;
470
471 stok = dma_map_single(dev, &adapter->stats,
472 sizeof(struct ibmvnic_statistics),
473 DMA_FROM_DEVICE);
474 if (dma_mapping_error(dev, stok)) {
475 dev_err(dev, "Couldn't map stats buffer\n");
476 return -1;
477 }
478
479 adapter->stats_token = stok;
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500480 netdev_dbg(adapter->netdev, "Stats token initialized (%llx)\n", stok);
Nathan Fontenot7bbc27a2017-03-30 02:49:23 -0400481 return 0;
482}
483
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400484static int reset_rx_pools(struct ibmvnic_adapter *adapter)
485{
486 struct ibmvnic_rx_pool *rx_pool;
Thomas Falcon507ebe62020-08-21 13:39:01 -0500487 u64 buff_size;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400488 int rx_scrqs;
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500489 int i, j, rc;
John Allen896d8692018-01-18 16:26:31 -0600490
Mingming Cao9f134572020-08-25 13:26:41 -0400491 if (!adapter->rx_pool)
492 return -1;
493
Thomas Falcon507ebe62020-08-21 13:39:01 -0500494 buff_size = adapter->cur_rx_buf_sz;
495 rx_scrqs = adapter->num_active_rx_pools;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400496 for (i = 0; i < rx_scrqs; i++) {
497 rx_pool = &adapter->rx_pool[i];
498
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500499 netdev_dbg(adapter->netdev, "Re-setting rx_pool[%d]\n", i);
500
Thomas Falcon507ebe62020-08-21 13:39:01 -0500501 if (rx_pool->buff_size != buff_size) {
John Allen896d8692018-01-18 16:26:31 -0600502 free_long_term_buff(adapter, &rx_pool->long_term_buff);
Thomas Falcon507ebe62020-08-21 13:39:01 -0500503 rx_pool->buff_size = buff_size;
Thomas Falcon7c940b12019-06-07 16:03:55 -0500504 rc = alloc_long_term_buff(adapter,
505 &rx_pool->long_term_buff,
506 rx_pool->size *
507 rx_pool->buff_size);
John Allen896d8692018-01-18 16:26:31 -0600508 } else {
509 rc = reset_long_term_buff(adapter,
510 &rx_pool->long_term_buff);
511 }
512
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500513 if (rc)
514 return rc;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400515
516 for (j = 0; j < rx_pool->size; j++)
517 rx_pool->free_map[j] = j;
518
519 memset(rx_pool->rx_buff, 0,
520 rx_pool->size * sizeof(struct ibmvnic_rx_buff));
521
522 atomic_set(&rx_pool->available, 0);
523 rx_pool->next_alloc = 0;
524 rx_pool->next_free = 0;
Thomas Falconc3e53b92017-06-14 23:50:05 -0500525 rx_pool->active = 1;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400526 }
527
528 return 0;
529}
530
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400531static void release_rx_pools(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -0600532{
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400533 struct ibmvnic_rx_pool *rx_pool;
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400534 int i, j;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600535
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400536 if (!adapter->rx_pool)
Thomas Falcon032c5e82015-12-21 11:26:06 -0600537 return;
538
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600539 for (i = 0; i < adapter->num_active_rx_pools; i++) {
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400540 rx_pool = &adapter->rx_pool[i];
541
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500542 netdev_dbg(adapter->netdev, "Releasing rx_pool[%d]\n", i);
543
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400544 kfree(rx_pool->free_map);
545 free_long_term_buff(adapter, &rx_pool->long_term_buff);
546
547 if (!rx_pool->rx_buff)
Nathan Fontenote0ebe9422017-05-03 14:04:50 -0400548 continue;
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400549
550 for (j = 0; j < rx_pool->size; j++) {
551 if (rx_pool->rx_buff[j].skb) {
Thomas Falconb7cdec32018-11-21 11:17:58 -0600552 dev_kfree_skb_any(rx_pool->rx_buff[j].skb);
553 rx_pool->rx_buff[j].skb = NULL;
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400554 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600555 }
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400556
557 kfree(rx_pool->rx_buff);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600558 }
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400559
560 kfree(adapter->rx_pool);
561 adapter->rx_pool = NULL;
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600562 adapter->num_active_rx_pools = 0;
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400563}
564
565static int init_rx_pools(struct net_device *netdev)
566{
567 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
568 struct device *dev = &adapter->vdev->dev;
569 struct ibmvnic_rx_pool *rx_pool;
570 int rxadd_subcrqs;
Thomas Falcon507ebe62020-08-21 13:39:01 -0500571 u64 buff_size;
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400572 int i, j;
573
Thomas Falcon507ebe62020-08-21 13:39:01 -0500574 rxadd_subcrqs = adapter->num_active_rx_scrqs;
575 buff_size = adapter->cur_rx_buf_sz;
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400576
577 adapter->rx_pool = kcalloc(rxadd_subcrqs,
578 sizeof(struct ibmvnic_rx_pool),
579 GFP_KERNEL);
580 if (!adapter->rx_pool) {
581 dev_err(dev, "Failed to allocate rx pools\n");
582 return -1;
583 }
584
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600585 adapter->num_active_rx_pools = rxadd_subcrqs;
586
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400587 for (i = 0; i < rxadd_subcrqs; i++) {
588 rx_pool = &adapter->rx_pool[i];
589
590 netdev_dbg(adapter->netdev,
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500591 "Initializing rx_pool[%d], %lld buffs, %lld bytes each\n",
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400592 i, adapter->req_rx_add_entries_per_subcrq,
Thomas Falcon507ebe62020-08-21 13:39:01 -0500593 buff_size);
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400594
595 rx_pool->size = adapter->req_rx_add_entries_per_subcrq;
596 rx_pool->index = i;
Thomas Falcon507ebe62020-08-21 13:39:01 -0500597 rx_pool->buff_size = buff_size;
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400598 rx_pool->active = 1;
599
600 rx_pool->free_map = kcalloc(rx_pool->size, sizeof(int),
601 GFP_KERNEL);
602 if (!rx_pool->free_map) {
603 release_rx_pools(adapter);
604 return -1;
605 }
606
607 rx_pool->rx_buff = kcalloc(rx_pool->size,
608 sizeof(struct ibmvnic_rx_buff),
609 GFP_KERNEL);
610 if (!rx_pool->rx_buff) {
611 dev_err(dev, "Couldn't alloc rx buffers\n");
612 release_rx_pools(adapter);
613 return -1;
614 }
615
616 if (alloc_long_term_buff(adapter, &rx_pool->long_term_buff,
617 rx_pool->size * rx_pool->buff_size)) {
618 release_rx_pools(adapter);
619 return -1;
620 }
621
622 for (j = 0; j < rx_pool->size; ++j)
623 rx_pool->free_map[j] = j;
624
625 atomic_set(&rx_pool->available, 0);
626 rx_pool->next_alloc = 0;
627 rx_pool->next_free = 0;
628 }
629
630 return 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600631}
632
Thomas Falcone26dc252018-03-16 20:00:25 -0500633static int reset_one_tx_pool(struct ibmvnic_adapter *adapter,
634 struct ibmvnic_tx_pool *tx_pool)
635{
636 int rc, i;
637
638 rc = reset_long_term_buff(adapter, &tx_pool->long_term_buff);
639 if (rc)
640 return rc;
641
642 memset(tx_pool->tx_buff, 0,
643 tx_pool->num_buffers *
644 sizeof(struct ibmvnic_tx_buff));
645
646 for (i = 0; i < tx_pool->num_buffers; i++)
647 tx_pool->free_map[i] = i;
648
649 tx_pool->consumer_index = 0;
650 tx_pool->producer_index = 0;
651
652 return 0;
653}
654
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400655static int reset_tx_pools(struct ibmvnic_adapter *adapter)
656{
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400657 int tx_scrqs;
Thomas Falcone26dc252018-03-16 20:00:25 -0500658 int i, rc;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400659
Mingming Cao9f134572020-08-25 13:26:41 -0400660 if (!adapter->tx_pool)
661 return -1;
662
Thomas Falcon507ebe62020-08-21 13:39:01 -0500663 tx_scrqs = adapter->num_active_tx_pools;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400664 for (i = 0; i < tx_scrqs; i++) {
Thomas Falcone26dc252018-03-16 20:00:25 -0500665 rc = reset_one_tx_pool(adapter, &adapter->tso_pool[i]);
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500666 if (rc)
667 return rc;
Thomas Falcone26dc252018-03-16 20:00:25 -0500668 rc = reset_one_tx_pool(adapter, &adapter->tx_pool[i]);
Thomas Falconfdb06102017-10-17 12:36:55 -0500669 if (rc)
670 return rc;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400671 }
672
673 return 0;
674}
675
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -0200676static void release_vpd_data(struct ibmvnic_adapter *adapter)
677{
678 if (!adapter->vpd)
679 return;
680
681 kfree(adapter->vpd->buff);
682 kfree(adapter->vpd);
Thomas Falconb0992ec2018-02-06 17:25:23 -0600683
684 adapter->vpd = NULL;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -0200685}
686
Thomas Falconfb794212018-03-16 20:00:26 -0500687static void release_one_tx_pool(struct ibmvnic_adapter *adapter,
688 struct ibmvnic_tx_pool *tx_pool)
689{
690 kfree(tx_pool->tx_buff);
691 kfree(tx_pool->free_map);
692 free_long_term_buff(adapter, &tx_pool->long_term_buff);
693}
694
Nathan Fontenotc657e322017-03-30 02:49:06 -0400695static void release_tx_pools(struct ibmvnic_adapter *adapter)
696{
John Allen896d8692018-01-18 16:26:31 -0600697 int i;
Nathan Fontenotc657e322017-03-30 02:49:06 -0400698
699 if (!adapter->tx_pool)
700 return;
701
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600702 for (i = 0; i < adapter->num_active_tx_pools; i++) {
Thomas Falconfb794212018-03-16 20:00:26 -0500703 release_one_tx_pool(adapter, &adapter->tx_pool[i]);
704 release_one_tx_pool(adapter, &adapter->tso_pool[i]);
Nathan Fontenotc657e322017-03-30 02:49:06 -0400705 }
706
707 kfree(adapter->tx_pool);
708 adapter->tx_pool = NULL;
Thomas Falconfb794212018-03-16 20:00:26 -0500709 kfree(adapter->tso_pool);
710 adapter->tso_pool = NULL;
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600711 adapter->num_active_tx_pools = 0;
Nathan Fontenotc657e322017-03-30 02:49:06 -0400712}
713
Thomas Falcon32053062018-03-16 20:00:27 -0500714static int init_one_tx_pool(struct net_device *netdev,
715 struct ibmvnic_tx_pool *tx_pool,
716 int num_entries, int buf_size)
717{
718 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
719 int i;
720
721 tx_pool->tx_buff = kcalloc(num_entries,
722 sizeof(struct ibmvnic_tx_buff),
723 GFP_KERNEL);
724 if (!tx_pool->tx_buff)
725 return -1;
726
727 if (alloc_long_term_buff(adapter, &tx_pool->long_term_buff,
728 num_entries * buf_size))
729 return -1;
730
731 tx_pool->free_map = kcalloc(num_entries, sizeof(int), GFP_KERNEL);
732 if (!tx_pool->free_map)
733 return -1;
734
735 for (i = 0; i < num_entries; i++)
736 tx_pool->free_map[i] = i;
737
738 tx_pool->consumer_index = 0;
739 tx_pool->producer_index = 0;
740 tx_pool->num_buffers = num_entries;
741 tx_pool->buf_size = buf_size;
742
743 return 0;
744}
745
Nathan Fontenotc657e322017-03-30 02:49:06 -0400746static int init_tx_pools(struct net_device *netdev)
747{
748 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Nathan Fontenotc657e322017-03-30 02:49:06 -0400749 int tx_subcrqs;
Thomas Falcon32053062018-03-16 20:00:27 -0500750 int i, rc;
Nathan Fontenotc657e322017-03-30 02:49:06 -0400751
Thomas Falcon507ebe62020-08-21 13:39:01 -0500752 tx_subcrqs = adapter->num_active_tx_scrqs;
Nathan Fontenotc657e322017-03-30 02:49:06 -0400753 adapter->tx_pool = kcalloc(tx_subcrqs,
754 sizeof(struct ibmvnic_tx_pool), GFP_KERNEL);
755 if (!adapter->tx_pool)
756 return -1;
757
Thomas Falcon32053062018-03-16 20:00:27 -0500758 adapter->tso_pool = kcalloc(tx_subcrqs,
759 sizeof(struct ibmvnic_tx_pool), GFP_KERNEL);
760 if (!adapter->tso_pool)
761 return -1;
762
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600763 adapter->num_active_tx_pools = tx_subcrqs;
764
Nathan Fontenotc657e322017-03-30 02:49:06 -0400765 for (i = 0; i < tx_subcrqs; i++) {
Thomas Falcon32053062018-03-16 20:00:27 -0500766 rc = init_one_tx_pool(netdev, &adapter->tx_pool[i],
767 adapter->req_tx_entries_per_subcrq,
768 adapter->req_mtu + VLAN_HLEN);
769 if (rc) {
Nathan Fontenotc657e322017-03-30 02:49:06 -0400770 release_tx_pools(adapter);
Thomas Falcon32053062018-03-16 20:00:27 -0500771 return rc;
Nathan Fontenotc657e322017-03-30 02:49:06 -0400772 }
773
Thomas Falcon7c940b12019-06-07 16:03:55 -0500774 rc = init_one_tx_pool(netdev, &adapter->tso_pool[i],
775 IBMVNIC_TSO_BUFS,
776 IBMVNIC_TSO_BUF_SZ);
Thomas Falcon32053062018-03-16 20:00:27 -0500777 if (rc) {
Nathan Fontenotc657e322017-03-30 02:49:06 -0400778 release_tx_pools(adapter);
Thomas Falcon32053062018-03-16 20:00:27 -0500779 return rc;
Nathan Fontenotc657e322017-03-30 02:49:06 -0400780 }
Nathan Fontenotc657e322017-03-30 02:49:06 -0400781 }
782
783 return 0;
784}
785
John Allend944c3d62017-05-26 10:30:13 -0400786static void ibmvnic_napi_enable(struct ibmvnic_adapter *adapter)
787{
788 int i;
789
790 if (adapter->napi_enabled)
791 return;
792
793 for (i = 0; i < adapter->req_rx_queues; i++)
794 napi_enable(&adapter->napi[i]);
795
796 adapter->napi_enabled = true;
797}
798
799static void ibmvnic_napi_disable(struct ibmvnic_adapter *adapter)
800{
801 int i;
802
803 if (!adapter->napi_enabled)
804 return;
805
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500806 for (i = 0; i < adapter->req_rx_queues; i++) {
807 netdev_dbg(adapter->netdev, "Disabling napi[%d]\n", i);
John Allend944c3d62017-05-26 10:30:13 -0400808 napi_disable(&adapter->napi[i]);
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500809 }
John Allend944c3d62017-05-26 10:30:13 -0400810
811 adapter->napi_enabled = false;
812}
813
Nathan Fontenot86f669b2018-02-19 13:30:39 -0600814static int init_napi(struct ibmvnic_adapter *adapter)
815{
816 int i;
817
818 adapter->napi = kcalloc(adapter->req_rx_queues,
819 sizeof(struct napi_struct), GFP_KERNEL);
820 if (!adapter->napi)
821 return -ENOMEM;
822
823 for (i = 0; i < adapter->req_rx_queues; i++) {
824 netdev_dbg(adapter->netdev, "Adding napi[%d]\n", i);
825 netif_napi_add(adapter->netdev, &adapter->napi[i],
826 ibmvnic_poll, NAPI_POLL_WEIGHT);
827 }
828
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600829 adapter->num_active_rx_napi = adapter->req_rx_queues;
Nathan Fontenot86f669b2018-02-19 13:30:39 -0600830 return 0;
831}
832
833static void release_napi(struct ibmvnic_adapter *adapter)
834{
835 int i;
836
837 if (!adapter->napi)
838 return;
839
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600840 for (i = 0; i < adapter->num_active_rx_napi; i++) {
Wen Yang390de192018-12-11 12:20:46 +0800841 netdev_dbg(adapter->netdev, "Releasing napi[%d]\n", i);
842 netif_napi_del(&adapter->napi[i]);
Nathan Fontenot86f669b2018-02-19 13:30:39 -0600843 }
844
845 kfree(adapter->napi);
846 adapter->napi = NULL;
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600847 adapter->num_active_rx_napi = 0;
Thomas Falconc3f22412018-05-23 13:37:55 -0500848 adapter->napi_enabled = false;
Nathan Fontenot86f669b2018-02-19 13:30:39 -0600849}
850
John Allena57a5d22017-03-17 17:13:41 -0500851static int ibmvnic_login(struct net_device *netdev)
Thomas Falcon032c5e82015-12-21 11:26:06 -0600852{
853 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
John Allenbd0b6722017-03-17 17:13:40 -0500854 unsigned long timeout = msecs_to_jiffies(30000);
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500855 int retry_count = 0;
Thomas Falcondff515a32020-06-15 10:29:23 -0500856 int retries = 10;
Thomas Falconeb110412018-05-24 14:37:53 -0500857 bool retry;
Thomas Falcon4d96f122017-08-01 15:04:36 -0500858 int rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600859
John Allenbd0b6722017-03-17 17:13:40 -0500860 do {
Thomas Falconeb110412018-05-24 14:37:53 -0500861 retry = false;
Thomas Falcondff515a32020-06-15 10:29:23 -0500862 if (retry_count > retries) {
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500863 netdev_warn(netdev, "Login attempts exceeded\n");
864 return -1;
865 }
866
867 adapter->init_done_rc = 0;
868 reinit_completion(&adapter->init_done);
869 rc = send_login(adapter);
870 if (rc) {
871 netdev_warn(netdev, "Unable to login\n");
872 return rc;
873 }
874
875 if (!wait_for_completion_timeout(&adapter->init_done,
876 timeout)) {
Thomas Falcondff515a32020-06-15 10:29:23 -0500877 netdev_warn(netdev, "Login timed out, retrying...\n");
878 retry = true;
879 adapter->init_done_rc = 0;
880 retry_count++;
881 continue;
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500882 }
883
Thomas Falcondff515a32020-06-15 10:29:23 -0500884 if (adapter->init_done_rc == ABORTED) {
885 netdev_warn(netdev, "Login aborted, retrying...\n");
886 retry = true;
887 adapter->init_done_rc = 0;
888 retry_count++;
889 /* FW or device may be busy, so
890 * wait a bit before retrying login
891 */
892 msleep(500);
893 } else if (adapter->init_done_rc == PARTIALSUCCESS) {
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500894 retry_count++;
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -0600895 release_sub_crqs(adapter, 1);
John Allenbd0b6722017-03-17 17:13:40 -0500896
Thomas Falconeb110412018-05-24 14:37:53 -0500897 retry = true;
898 netdev_dbg(netdev,
899 "Received partial success, retrying...\n");
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500900 adapter->init_done_rc = 0;
John Allenbd0b6722017-03-17 17:13:40 -0500901 reinit_completion(&adapter->init_done);
Lijun Pan491099a2020-09-27 20:13:26 -0500902 send_query_cap(adapter);
John Allenbd0b6722017-03-17 17:13:40 -0500903 if (!wait_for_completion_timeout(&adapter->init_done,
904 timeout)) {
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500905 netdev_warn(netdev,
906 "Capabilities query timed out\n");
John Allenbd0b6722017-03-17 17:13:40 -0500907 return -1;
908 }
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500909
Thomas Falcon4d96f122017-08-01 15:04:36 -0500910 rc = init_sub_crqs(adapter);
911 if (rc) {
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500912 netdev_warn(netdev,
913 "SCRQ initialization failed\n");
Thomas Falcon4d96f122017-08-01 15:04:36 -0500914 return -1;
915 }
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500916
Thomas Falcon4d96f122017-08-01 15:04:36 -0500917 rc = init_sub_crq_irqs(adapter);
918 if (rc) {
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500919 netdev_warn(netdev,
920 "SCRQ irq initialization failed\n");
Thomas Falcon4d96f122017-08-01 15:04:36 -0500921 return -1;
922 }
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500923 } else if (adapter->init_done_rc) {
924 netdev_warn(netdev, "Adapter login failed\n");
John Allenbd0b6722017-03-17 17:13:40 -0500925 return -1;
926 }
Thomas Falconeb110412018-05-24 14:37:53 -0500927 } while (retry);
John Allenbd0b6722017-03-17 17:13:40 -0500928
Thomas Falcon62740e92019-05-09 23:13:43 -0500929 __ibmvnic_set_mac(netdev, adapter->mac_addr);
Thomas Falcon3d166132018-01-10 19:39:52 -0600930
John Allena57a5d22017-03-17 17:13:41 -0500931 return 0;
932}
933
Thomas Falcon34f0f4e2018-02-13 18:23:40 -0600934static void release_login_buffer(struct ibmvnic_adapter *adapter)
935{
936 kfree(adapter->login_buf);
937 adapter->login_buf = NULL;
938}
939
940static void release_login_rsp_buffer(struct ibmvnic_adapter *adapter)
941{
942 kfree(adapter->login_rsp_buf);
943 adapter->login_rsp_buf = NULL;
944}
945
Nathan Fontenot1b8955e2017-03-30 02:49:29 -0400946static void release_resources(struct ibmvnic_adapter *adapter)
947{
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -0200948 release_vpd_data(adapter);
949
Nathan Fontenot1b8955e2017-03-30 02:49:29 -0400950 release_tx_pools(adapter);
951 release_rx_pools(adapter);
952
Nathan Fontenot86f669b2018-02-19 13:30:39 -0600953 release_napi(adapter);
Thomas Falcon34f0f4e2018-02-13 18:23:40 -0600954 release_login_rsp_buffer(adapter);
Nathan Fontenot1b8955e2017-03-30 02:49:29 -0400955}
956
Nathan Fontenot53da09e2017-04-21 15:39:04 -0400957static int set_link_state(struct ibmvnic_adapter *adapter, u8 link_state)
958{
959 struct net_device *netdev = adapter->netdev;
960 unsigned long timeout = msecs_to_jiffies(30000);
961 union ibmvnic_crq crq;
962 bool resend;
963 int rc;
964
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500965 netdev_dbg(netdev, "setting link state %d\n", link_state);
966
Nathan Fontenot53da09e2017-04-21 15:39:04 -0400967 memset(&crq, 0, sizeof(crq));
968 crq.logical_link_state.first = IBMVNIC_CRQ_CMD;
969 crq.logical_link_state.cmd = LOGICAL_LINK_STATE;
970 crq.logical_link_state.link_state = link_state;
971
972 do {
973 resend = false;
974
975 reinit_completion(&adapter->init_done);
976 rc = ibmvnic_send_crq(adapter, &crq);
977 if (rc) {
978 netdev_err(netdev, "Failed to set link state\n");
979 return rc;
980 }
981
982 if (!wait_for_completion_timeout(&adapter->init_done,
983 timeout)) {
984 netdev_err(netdev, "timeout setting link state\n");
985 return -1;
986 }
987
Lijun Pan4c5f6af2020-08-19 17:52:23 -0500988 if (adapter->init_done_rc == PARTIALSUCCESS) {
Nathan Fontenot53da09e2017-04-21 15:39:04 -0400989 /* Partuial success, delay and re-send */
990 mdelay(1000);
991 resend = true;
Thomas Falconab5ec332018-05-23 13:37:59 -0500992 } else if (adapter->init_done_rc) {
993 netdev_warn(netdev, "Unable to set link state, rc=%d\n",
994 adapter->init_done_rc);
995 return adapter->init_done_rc;
Nathan Fontenot53da09e2017-04-21 15:39:04 -0400996 }
997 } while (resend);
998
999 return 0;
1000}
1001
Thomas Falcon7f3c6e62017-04-21 15:38:40 -04001002static int set_real_num_queues(struct net_device *netdev)
1003{
1004 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1005 int rc;
1006
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001007 netdev_dbg(netdev, "Setting real tx/rx queues (%llx/%llx)\n",
1008 adapter->req_tx_queues, adapter->req_rx_queues);
1009
Thomas Falcon7f3c6e62017-04-21 15:38:40 -04001010 rc = netif_set_real_num_tx_queues(netdev, adapter->req_tx_queues);
1011 if (rc) {
1012 netdev_err(netdev, "failed to set the number of tx queues\n");
1013 return rc;
1014 }
1015
1016 rc = netif_set_real_num_rx_queues(netdev, adapter->req_rx_queues);
1017 if (rc)
1018 netdev_err(netdev, "failed to set the number of rx queues\n");
1019
1020 return rc;
1021}
1022
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001023static int ibmvnic_get_vpd(struct ibmvnic_adapter *adapter)
1024{
1025 struct device *dev = &adapter->vdev->dev;
1026 union ibmvnic_crq crq;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001027 int len = 0;
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05001028 int rc;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001029
1030 if (adapter->vpd->buff)
1031 len = adapter->vpd->len;
1032
Thomas Falconff25dcb2019-11-25 17:12:56 -06001033 mutex_lock(&adapter->fw_lock);
1034 adapter->fw_done_rc = 0;
Thomas Falcon070eca92019-11-25 17:12:53 -06001035 reinit_completion(&adapter->fw_done);
Thomas Falconff25dcb2019-11-25 17:12:56 -06001036
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001037 crq.get_vpd_size.first = IBMVNIC_CRQ_CMD;
1038 crq.get_vpd_size.cmd = GET_VPD_SIZE;
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05001039 rc = ibmvnic_send_crq(adapter, &crq);
Thomas Falconff25dcb2019-11-25 17:12:56 -06001040 if (rc) {
1041 mutex_unlock(&adapter->fw_lock);
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05001042 return rc;
Thomas Falconff25dcb2019-11-25 17:12:56 -06001043 }
Thomas Falcon476d96c2019-11-25 17:12:55 -06001044
1045 rc = ibmvnic_wait_for_completion(adapter, &adapter->fw_done, 10000);
1046 if (rc) {
1047 dev_err(dev, "Could not retrieve VPD size, rc = %d\n", rc);
Thomas Falconff25dcb2019-11-25 17:12:56 -06001048 mutex_unlock(&adapter->fw_lock);
Thomas Falcon476d96c2019-11-25 17:12:55 -06001049 return rc;
1050 }
Thomas Falconff25dcb2019-11-25 17:12:56 -06001051 mutex_unlock(&adapter->fw_lock);
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001052
1053 if (!adapter->vpd->len)
1054 return -ENODATA;
1055
1056 if (!adapter->vpd->buff)
1057 adapter->vpd->buff = kzalloc(adapter->vpd->len, GFP_KERNEL);
1058 else if (adapter->vpd->len != len)
1059 adapter->vpd->buff =
1060 krealloc(adapter->vpd->buff,
1061 adapter->vpd->len, GFP_KERNEL);
1062
1063 if (!adapter->vpd->buff) {
1064 dev_err(dev, "Could allocate VPD buffer\n");
1065 return -ENOMEM;
1066 }
1067
1068 adapter->vpd->dma_addr =
1069 dma_map_single(dev, adapter->vpd->buff, adapter->vpd->len,
1070 DMA_FROM_DEVICE);
Desnes Augusto Nunes do Rosariof7431062017-11-17 09:09:04 -02001071 if (dma_mapping_error(dev, adapter->vpd->dma_addr)) {
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001072 dev_err(dev, "Could not map VPD buffer\n");
1073 kfree(adapter->vpd->buff);
Thomas Falconb0992ec2018-02-06 17:25:23 -06001074 adapter->vpd->buff = NULL;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001075 return -ENOMEM;
1076 }
1077
Thomas Falconff25dcb2019-11-25 17:12:56 -06001078 mutex_lock(&adapter->fw_lock);
1079 adapter->fw_done_rc = 0;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001080 reinit_completion(&adapter->fw_done);
Thomas Falconff25dcb2019-11-25 17:12:56 -06001081
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001082 crq.get_vpd.first = IBMVNIC_CRQ_CMD;
1083 crq.get_vpd.cmd = GET_VPD;
1084 crq.get_vpd.ioba = cpu_to_be32(adapter->vpd->dma_addr);
1085 crq.get_vpd.len = cpu_to_be32((u32)adapter->vpd->len);
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05001086 rc = ibmvnic_send_crq(adapter, &crq);
1087 if (rc) {
1088 kfree(adapter->vpd->buff);
1089 adapter->vpd->buff = NULL;
Thomas Falconff25dcb2019-11-25 17:12:56 -06001090 mutex_unlock(&adapter->fw_lock);
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05001091 return rc;
1092 }
Thomas Falcon476d96c2019-11-25 17:12:55 -06001093
1094 rc = ibmvnic_wait_for_completion(adapter, &adapter->fw_done, 10000);
1095 if (rc) {
1096 dev_err(dev, "Unable to retrieve VPD, rc = %d\n", rc);
1097 kfree(adapter->vpd->buff);
1098 adapter->vpd->buff = NULL;
Thomas Falconff25dcb2019-11-25 17:12:56 -06001099 mutex_unlock(&adapter->fw_lock);
Thomas Falcon476d96c2019-11-25 17:12:55 -06001100 return rc;
1101 }
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001102
Thomas Falconff25dcb2019-11-25 17:12:56 -06001103 mutex_unlock(&adapter->fw_lock);
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001104 return 0;
1105}
1106
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001107static int init_resources(struct ibmvnic_adapter *adapter)
John Allena57a5d22017-03-17 17:13:41 -05001108{
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001109 struct net_device *netdev = adapter->netdev;
Nathan Fontenot86f669b2018-02-19 13:30:39 -06001110 int rc;
John Allena57a5d22017-03-17 17:13:41 -05001111
Thomas Falcon7f3c6e62017-04-21 15:38:40 -04001112 rc = set_real_num_queues(netdev);
1113 if (rc)
1114 return rc;
John Allenbd0b6722017-03-17 17:13:40 -05001115
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001116 adapter->vpd = kzalloc(sizeof(*adapter->vpd), GFP_KERNEL);
1117 if (!adapter->vpd)
1118 return -ENOMEM;
1119
John Allen69d08dc2018-01-18 16:27:58 -06001120 /* Vital Product Data (VPD) */
1121 rc = ibmvnic_get_vpd(adapter);
1122 if (rc) {
1123 netdev_err(netdev, "failed to initialize Vital Product Data (VPD)\n");
1124 return rc;
1125 }
1126
Thomas Falcon032c5e82015-12-21 11:26:06 -06001127 adapter->map_id = 1;
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001128
Nathan Fontenot86f669b2018-02-19 13:30:39 -06001129 rc = init_napi(adapter);
1130 if (rc)
1131 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001132
Lijun Pan69980d02020-09-27 20:13:28 -05001133 send_query_map(adapter);
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -04001134
1135 rc = init_rx_pools(netdev);
1136 if (rc)
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001137 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001138
Nathan Fontenotc657e322017-03-30 02:49:06 -04001139 rc = init_tx_pools(netdev);
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001140 return rc;
1141}
1142
Nathan Fontenoted651a12017-05-03 14:04:38 -04001143static int __ibmvnic_open(struct net_device *netdev)
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001144{
1145 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001146 enum vnic_state prev_state = adapter->state;
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001147 int i, rc;
1148
Nathan Fontenot90c80142017-05-03 14:04:32 -04001149 adapter->state = VNIC_OPENING;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001150 replenish_pools(adapter);
John Allend944c3d62017-05-26 10:30:13 -04001151 ibmvnic_napi_enable(adapter);
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001152
Thomas Falcon032c5e82015-12-21 11:26:06 -06001153 /* We're ready to receive frames, enable the sub-crq interrupts and
1154 * set the logical link state to up
1155 */
Nathan Fontenoted651a12017-05-03 14:04:38 -04001156 for (i = 0; i < adapter->req_rx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001157 netdev_dbg(netdev, "Enabling rx_scrq[%d] irq\n", i);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001158 if (prev_state == VNIC_CLOSED)
1159 enable_irq(adapter->rx_scrq[i]->irq);
Thomas Falconf23e0642018-04-15 18:53:36 -05001160 enable_scrq_irq(adapter, adapter->rx_scrq[i]);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001161 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001162
Nathan Fontenoted651a12017-05-03 14:04:38 -04001163 for (i = 0; i < adapter->req_tx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001164 netdev_dbg(netdev, "Enabling tx_scrq[%d] irq\n", i);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001165 if (prev_state == VNIC_CLOSED)
1166 enable_irq(adapter->tx_scrq[i]->irq);
Thomas Falconf23e0642018-04-15 18:53:36 -05001167 enable_scrq_irq(adapter, adapter->tx_scrq[i]);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001168 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001169
Nathan Fontenot53da09e2017-04-21 15:39:04 -04001170 rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_UP);
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001171 if (rc) {
1172 for (i = 0; i < adapter->req_rx_queues; i++)
1173 napi_disable(&adapter->napi[i]);
1174 release_resources(adapter);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001175 return rc;
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001176 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001177
Nathan Fontenoted651a12017-05-03 14:04:38 -04001178 netif_tx_start_all_queues(netdev);
1179
1180 if (prev_state == VNIC_CLOSED) {
1181 for (i = 0; i < adapter->req_rx_queues; i++)
1182 napi_schedule(&adapter->napi[i]);
1183 }
1184
1185 adapter->state = VNIC_OPEN;
1186 return rc;
1187}
1188
1189static int ibmvnic_open(struct net_device *netdev)
1190{
1191 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
John Allen69d08dc2018-01-18 16:27:58 -06001192 int rc;
Nathan Fontenoted651a12017-05-03 14:04:38 -04001193
Thomas Falcon5a18e1e2018-04-06 18:37:05 -05001194 /* If device failover is pending, just set device state and return.
1195 * Device operation will be handled by reset routine.
1196 */
1197 if (adapter->failover_pending) {
1198 adapter->state = VNIC_OPEN;
1199 return 0;
1200 }
1201
Nathan Fontenoted651a12017-05-03 14:04:38 -04001202 if (adapter->state != VNIC_CLOSED) {
1203 rc = ibmvnic_login(netdev);
Juliet Kima5681e22018-11-19 15:59:22 -06001204 if (rc)
Sukadev Bhattiprolu1d850492020-10-30 10:07:11 -07001205 goto out;
Nathan Fontenoted651a12017-05-03 14:04:38 -04001206
1207 rc = init_resources(adapter);
1208 if (rc) {
1209 netdev_err(netdev, "failed to initialize resources\n");
1210 release_resources(adapter);
Sukadev Bhattiprolu1d850492020-10-30 10:07:11 -07001211 goto out;
Nathan Fontenoted651a12017-05-03 14:04:38 -04001212 }
1213 }
1214
1215 rc = __ibmvnic_open(netdev);
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001216
Sukadev Bhattiprolu1d850492020-10-30 10:07:11 -07001217out:
1218 /*
1219 * If open fails due to a pending failover, set device state and
1220 * return. Device operation will be handled by reset routine.
1221 */
1222 if (rc && adapter->failover_pending) {
1223 adapter->state = VNIC_OPEN;
1224 rc = 0;
1225 }
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001226 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001227}
1228
Thomas Falcond0869c02018-02-13 18:23:43 -06001229static void clean_rx_pools(struct ibmvnic_adapter *adapter)
1230{
1231 struct ibmvnic_rx_pool *rx_pool;
Thomas Falcon637f81d2018-02-26 18:10:57 -06001232 struct ibmvnic_rx_buff *rx_buff;
Thomas Falcond0869c02018-02-13 18:23:43 -06001233 u64 rx_entries;
1234 int rx_scrqs;
1235 int i, j;
1236
1237 if (!adapter->rx_pool)
1238 return;
1239
Thomas Falcon660e3092018-04-20 14:25:32 -05001240 rx_scrqs = adapter->num_active_rx_pools;
Thomas Falcond0869c02018-02-13 18:23:43 -06001241 rx_entries = adapter->req_rx_add_entries_per_subcrq;
1242
1243 /* Free any remaining skbs in the rx buffer pools */
1244 for (i = 0; i < rx_scrqs; i++) {
1245 rx_pool = &adapter->rx_pool[i];
Thomas Falcon637f81d2018-02-26 18:10:57 -06001246 if (!rx_pool || !rx_pool->rx_buff)
Thomas Falcond0869c02018-02-13 18:23:43 -06001247 continue;
1248
1249 netdev_dbg(adapter->netdev, "Cleaning rx_pool[%d]\n", i);
1250 for (j = 0; j < rx_entries; j++) {
Thomas Falcon637f81d2018-02-26 18:10:57 -06001251 rx_buff = &rx_pool->rx_buff[j];
1252 if (rx_buff && rx_buff->skb) {
1253 dev_kfree_skb_any(rx_buff->skb);
1254 rx_buff->skb = NULL;
Thomas Falcond0869c02018-02-13 18:23:43 -06001255 }
1256 }
1257 }
1258}
1259
Thomas Falcone9e1e972018-03-16 20:00:30 -05001260static void clean_one_tx_pool(struct ibmvnic_adapter *adapter,
1261 struct ibmvnic_tx_pool *tx_pool)
Nathan Fontenotb41b83e2017-05-03 14:04:56 -04001262{
Thomas Falcon637f81d2018-02-26 18:10:57 -06001263 struct ibmvnic_tx_buff *tx_buff;
Nathan Fontenotb41b83e2017-05-03 14:04:56 -04001264 u64 tx_entries;
Thomas Falcone9e1e972018-03-16 20:00:30 -05001265 int i;
Nathan Fontenotb41b83e2017-05-03 14:04:56 -04001266
Dan Carpenter050e85c2018-03-23 14:36:15 +03001267 if (!tx_pool || !tx_pool->tx_buff)
Thomas Falcone9e1e972018-03-16 20:00:30 -05001268 return;
1269
1270 tx_entries = tx_pool->num_buffers;
1271
1272 for (i = 0; i < tx_entries; i++) {
1273 tx_buff = &tx_pool->tx_buff[i];
1274 if (tx_buff && tx_buff->skb) {
1275 dev_kfree_skb_any(tx_buff->skb);
1276 tx_buff->skb = NULL;
1277 }
1278 }
1279}
1280
1281static void clean_tx_pools(struct ibmvnic_adapter *adapter)
1282{
1283 int tx_scrqs;
1284 int i;
1285
1286 if (!adapter->tx_pool || !adapter->tso_pool)
Nathan Fontenotb41b83e2017-05-03 14:04:56 -04001287 return;
1288
Thomas Falcon660e3092018-04-20 14:25:32 -05001289 tx_scrqs = adapter->num_active_tx_pools;
Nathan Fontenotb41b83e2017-05-03 14:04:56 -04001290
1291 /* Free any remaining skbs in the tx buffer pools */
1292 for (i = 0; i < tx_scrqs; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001293 netdev_dbg(adapter->netdev, "Cleaning tx_pool[%d]\n", i);
Thomas Falcone9e1e972018-03-16 20:00:30 -05001294 clean_one_tx_pool(adapter, &adapter->tx_pool[i]);
1295 clean_one_tx_pool(adapter, &adapter->tso_pool[i]);
Nathan Fontenotb41b83e2017-05-03 14:04:56 -04001296 }
1297}
1298
John Allen6095e592018-03-30 13:44:21 -05001299static void ibmvnic_disable_irqs(struct ibmvnic_adapter *adapter)
John Allenea5509f2017-03-17 17:13:43 -05001300{
John Allen6095e592018-03-30 13:44:21 -05001301 struct net_device *netdev = adapter->netdev;
John Allenea5509f2017-03-17 17:13:43 -05001302 int i;
1303
Nathan Fontenot46293b92017-05-03 14:05:02 -04001304 if (adapter->tx_scrq) {
1305 for (i = 0; i < adapter->req_tx_queues; i++)
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001306 if (adapter->tx_scrq[i]->irq) {
Thomas Falconf8738662018-03-07 17:51:45 -06001307 netdev_dbg(netdev,
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001308 "Disabling tx_scrq[%d] irq\n", i);
Thomas Falconf23e0642018-04-15 18:53:36 -05001309 disable_scrq_irq(adapter, adapter->tx_scrq[i]);
Nathan Fontenot46293b92017-05-03 14:05:02 -04001310 disable_irq(adapter->tx_scrq[i]->irq);
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001311 }
Nathan Fontenot46293b92017-05-03 14:05:02 -04001312 }
1313
Nathan Fontenot46293b92017-05-03 14:05:02 -04001314 if (adapter->rx_scrq) {
1315 for (i = 0; i < adapter->req_rx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001316 if (adapter->rx_scrq[i]->irq) {
Thomas Falconf8738662018-03-07 17:51:45 -06001317 netdev_dbg(netdev,
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001318 "Disabling rx_scrq[%d] irq\n", i);
Thomas Falconf23e0642018-04-15 18:53:36 -05001319 disable_scrq_irq(adapter, adapter->rx_scrq[i]);
Nathan Fontenot46293b92017-05-03 14:05:02 -04001320 disable_irq(adapter->rx_scrq[i]->irq);
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001321 }
Nathan Fontenot46293b92017-05-03 14:05:02 -04001322 }
1323 }
John Allen6095e592018-03-30 13:44:21 -05001324}
1325
1326static void ibmvnic_cleanup(struct net_device *netdev)
1327{
1328 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1329
1330 /* ensure that transmissions are stopped if called by do_reset */
Juliet Kim7ed5b312019-09-20 16:11:23 -04001331 if (test_bit(0, &adapter->resetting))
John Allen6095e592018-03-30 13:44:21 -05001332 netif_tx_disable(netdev);
1333 else
1334 netif_tx_stop_all_queues(netdev);
1335
1336 ibmvnic_napi_disable(adapter);
1337 ibmvnic_disable_irqs(adapter);
1338
Thomas Falcond0869c02018-02-13 18:23:43 -06001339 clean_rx_pools(adapter);
Thomas Falcon10f76212017-05-26 10:30:31 -04001340 clean_tx_pools(adapter);
Thomas Falcon01d9bd72018-03-07 17:51:46 -06001341}
1342
1343static int __ibmvnic_close(struct net_device *netdev)
1344{
1345 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1346 int rc = 0;
1347
1348 adapter->state = VNIC_CLOSING;
1349 rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_DN);
1350 if (rc)
1351 return rc;
Nathan Fontenot90c80142017-05-03 14:04:32 -04001352 adapter->state = VNIC_CLOSED;
Thomas Falcon01d9bd72018-03-07 17:51:46 -06001353 return 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001354}
1355
Nathan Fontenoted651a12017-05-03 14:04:38 -04001356static int ibmvnic_close(struct net_device *netdev)
1357{
1358 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1359 int rc;
1360
Thomas Falcon5a18e1e2018-04-06 18:37:05 -05001361 /* If device failover is pending, just set device state and return.
1362 * Device operation will be handled by reset routine.
1363 */
1364 if (adapter->failover_pending) {
1365 adapter->state = VNIC_CLOSED;
1366 return 0;
1367 }
1368
Nathan Fontenoted651a12017-05-03 14:04:38 -04001369 rc = __ibmvnic_close(netdev);
Nathan Fontenot30f79622018-04-06 18:37:06 -05001370 ibmvnic_cleanup(netdev);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001371
1372 return rc;
1373}
1374
Thomas Falconad7775d2016-04-01 17:20:34 -05001375/**
1376 * build_hdr_data - creates L2/L3/L4 header data buffer
1377 * @hdr_field - bitfield determining needed headers
1378 * @skb - socket buffer
1379 * @hdr_len - array of header lengths
1380 * @tot_len - total length of data
1381 *
1382 * Reads hdr_field to determine which headers are needed by firmware.
1383 * Builds a buffer containing these headers. Saves individual header
1384 * lengths and total buffer length to be used to build descriptors.
1385 */
1386static int build_hdr_data(u8 hdr_field, struct sk_buff *skb,
1387 int *hdr_len, u8 *hdr_data)
1388{
1389 int len = 0;
1390 u8 *hdr;
1391
Thomas Falconda75e3b2018-03-12 11:51:02 -05001392 if (skb_vlan_tagged(skb) && !skb_vlan_tag_present(skb))
1393 hdr_len[0] = sizeof(struct vlan_ethhdr);
1394 else
1395 hdr_len[0] = sizeof(struct ethhdr);
Thomas Falconad7775d2016-04-01 17:20:34 -05001396
1397 if (skb->protocol == htons(ETH_P_IP)) {
1398 hdr_len[1] = ip_hdr(skb)->ihl * 4;
1399 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
1400 hdr_len[2] = tcp_hdrlen(skb);
1401 else if (ip_hdr(skb)->protocol == IPPROTO_UDP)
1402 hdr_len[2] = sizeof(struct udphdr);
1403 } else if (skb->protocol == htons(ETH_P_IPV6)) {
1404 hdr_len[1] = sizeof(struct ipv6hdr);
1405 if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
1406 hdr_len[2] = tcp_hdrlen(skb);
1407 else if (ipv6_hdr(skb)->nexthdr == IPPROTO_UDP)
1408 hdr_len[2] = sizeof(struct udphdr);
Thomas Falcon4eb50ce2017-12-18 12:52:40 -06001409 } else if (skb->protocol == htons(ETH_P_ARP)) {
1410 hdr_len[1] = arp_hdr_len(skb->dev);
1411 hdr_len[2] = 0;
Thomas Falconad7775d2016-04-01 17:20:34 -05001412 }
1413
1414 memset(hdr_data, 0, 120);
1415 if ((hdr_field >> 6) & 1) {
1416 hdr = skb_mac_header(skb);
1417 memcpy(hdr_data, hdr, hdr_len[0]);
1418 len += hdr_len[0];
1419 }
1420
1421 if ((hdr_field >> 5) & 1) {
1422 hdr = skb_network_header(skb);
1423 memcpy(hdr_data + len, hdr, hdr_len[1]);
1424 len += hdr_len[1];
1425 }
1426
1427 if ((hdr_field >> 4) & 1) {
1428 hdr = skb_transport_header(skb);
1429 memcpy(hdr_data + len, hdr, hdr_len[2]);
1430 len += hdr_len[2];
1431 }
1432 return len;
1433}
1434
1435/**
1436 * create_hdr_descs - create header and header extension descriptors
1437 * @hdr_field - bitfield determining needed headers
1438 * @data - buffer containing header data
1439 * @len - length of data buffer
1440 * @hdr_len - array of individual header lengths
1441 * @scrq_arr - descriptor array
1442 *
1443 * Creates header and, if needed, header extension descriptors and
1444 * places them in a descriptor array, scrq_arr
1445 */
1446
Thomas Falcon2de09682017-10-16 10:02:11 -05001447static int create_hdr_descs(u8 hdr_field, u8 *hdr_data, int len, int *hdr_len,
1448 union sub_crq *scrq_arr)
Thomas Falconad7775d2016-04-01 17:20:34 -05001449{
1450 union sub_crq hdr_desc;
1451 int tmp_len = len;
Thomas Falcon2de09682017-10-16 10:02:11 -05001452 int num_descs = 0;
Thomas Falconad7775d2016-04-01 17:20:34 -05001453 u8 *data, *cur;
1454 int tmp;
1455
1456 while (tmp_len > 0) {
1457 cur = hdr_data + len - tmp_len;
1458
1459 memset(&hdr_desc, 0, sizeof(hdr_desc));
1460 if (cur != hdr_data) {
1461 data = hdr_desc.hdr_ext.data;
1462 tmp = tmp_len > 29 ? 29 : tmp_len;
1463 hdr_desc.hdr_ext.first = IBMVNIC_CRQ_CMD;
1464 hdr_desc.hdr_ext.type = IBMVNIC_HDR_EXT_DESC;
1465 hdr_desc.hdr_ext.len = tmp;
1466 } else {
1467 data = hdr_desc.hdr.data;
1468 tmp = tmp_len > 24 ? 24 : tmp_len;
1469 hdr_desc.hdr.first = IBMVNIC_CRQ_CMD;
1470 hdr_desc.hdr.type = IBMVNIC_HDR_DESC;
1471 hdr_desc.hdr.len = tmp;
1472 hdr_desc.hdr.l2_len = (u8)hdr_len[0];
1473 hdr_desc.hdr.l3_len = cpu_to_be16((u16)hdr_len[1]);
1474 hdr_desc.hdr.l4_len = (u8)hdr_len[2];
1475 hdr_desc.hdr.flag = hdr_field << 1;
1476 }
1477 memcpy(data, cur, tmp);
1478 tmp_len -= tmp;
1479 *scrq_arr = hdr_desc;
1480 scrq_arr++;
Thomas Falcon2de09682017-10-16 10:02:11 -05001481 num_descs++;
Thomas Falconad7775d2016-04-01 17:20:34 -05001482 }
Thomas Falcon2de09682017-10-16 10:02:11 -05001483
1484 return num_descs;
Thomas Falconad7775d2016-04-01 17:20:34 -05001485}
1486
1487/**
1488 * build_hdr_descs_arr - build a header descriptor array
1489 * @skb - socket buffer
1490 * @num_entries - number of descriptors to be sent
1491 * @subcrq - first TX descriptor
1492 * @hdr_field - bit field determining which headers will be sent
1493 *
1494 * This function will build a TX descriptor array with applicable
1495 * L2/L3/L4 packet header descriptors to be sent by send_subcrq_indirect.
1496 */
1497
1498static void build_hdr_descs_arr(struct ibmvnic_tx_buff *txbuff,
1499 int *num_entries, u8 hdr_field)
1500{
1501 int hdr_len[3] = {0, 0, 0};
Thomas Falcon2de09682017-10-16 10:02:11 -05001502 int tot_len;
Thomas Falconad7775d2016-04-01 17:20:34 -05001503 u8 *hdr_data = txbuff->hdr_data;
1504
1505 tot_len = build_hdr_data(hdr_field, txbuff->skb, hdr_len,
1506 txbuff->hdr_data);
Thomas Falcon2de09682017-10-16 10:02:11 -05001507 *num_entries += create_hdr_descs(hdr_field, hdr_data, tot_len, hdr_len,
Thomas Falconad7775d2016-04-01 17:20:34 -05001508 txbuff->indir_arr + 1);
1509}
1510
Thomas Falcon1f247a62018-03-12 11:51:04 -05001511static int ibmvnic_xmit_workarounds(struct sk_buff *skb,
1512 struct net_device *netdev)
1513{
1514 /* For some backing devices, mishandling of small packets
1515 * can result in a loss of connection or TX stall. Device
1516 * architects recommend that no packet should be smaller
1517 * than the minimum MTU value provided to the driver, so
1518 * pad any packets to that length
1519 */
1520 if (skb->len < netdev->min_mtu)
1521 return skb_put_padto(skb, netdev->min_mtu);
Thomas Falcon7083a452018-03-12 21:05:26 -05001522
1523 return 0;
Thomas Falcon1f247a62018-03-12 11:51:04 -05001524}
1525
YueHaibing94b2bb22018-09-18 14:35:47 +08001526static netdev_tx_t ibmvnic_xmit(struct sk_buff *skb, struct net_device *netdev)
Thomas Falcon032c5e82015-12-21 11:26:06 -06001527{
1528 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1529 int queue_num = skb_get_queue_mapping(skb);
Thomas Falconad7775d2016-04-01 17:20:34 -05001530 u8 *hdrs = (u8 *)&adapter->tx_rx_desc_req;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001531 struct device *dev = &adapter->vdev->dev;
1532 struct ibmvnic_tx_buff *tx_buff = NULL;
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001533 struct ibmvnic_sub_crq_queue *tx_scrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001534 struct ibmvnic_tx_pool *tx_pool;
1535 unsigned int tx_send_failed = 0;
1536 unsigned int tx_map_failed = 0;
1537 unsigned int tx_dropped = 0;
1538 unsigned int tx_packets = 0;
1539 unsigned int tx_bytes = 0;
1540 dma_addr_t data_dma_addr;
1541 struct netdev_queue *txq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001542 unsigned long lpar_rc;
1543 union sub_crq tx_crq;
1544 unsigned int offset;
Thomas Falconad7775d2016-04-01 17:20:34 -05001545 int num_entries = 1;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001546 unsigned char *dst;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001547 int index = 0;
Thomas Falcona0dca102018-01-18 19:29:48 -06001548 u8 proto = 0;
Cristobal Fornof3ae59c2020-08-19 13:16:23 -05001549 u64 handle;
YueHaibing94b2bb22018-09-18 14:35:47 +08001550 netdev_tx_t ret = NETDEV_TX_OK;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001551
Juliet Kim7ed5b312019-09-20 16:11:23 -04001552 if (test_bit(0, &adapter->resetting)) {
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001553 if (!netif_subqueue_stopped(netdev, skb))
1554 netif_stop_subqueue(netdev, queue_num);
1555 dev_kfree_skb_any(skb);
1556
Thomas Falcon032c5e82015-12-21 11:26:06 -06001557 tx_send_failed++;
1558 tx_dropped++;
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001559 ret = NETDEV_TX_OK;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001560 goto out;
1561 }
1562
Thomas Falcon7083a452018-03-12 21:05:26 -05001563 if (ibmvnic_xmit_workarounds(skb, netdev)) {
Thomas Falcon1f247a62018-03-12 11:51:04 -05001564 tx_dropped++;
1565 tx_send_failed++;
1566 ret = NETDEV_TX_OK;
1567 goto out;
1568 }
Thomas Falcon06b3e352018-03-16 20:00:28 -05001569 if (skb_is_gso(skb))
1570 tx_pool = &adapter->tso_pool[queue_num];
1571 else
1572 tx_pool = &adapter->tx_pool[queue_num];
Thomas Falcon1f247a62018-03-12 11:51:04 -05001573
Nathan Fontenot161b8a82017-05-03 14:05:08 -04001574 tx_scrq = adapter->tx_scrq[queue_num];
1575 txq = netdev_get_tx_queue(netdev, skb_get_queue_mapping(skb));
Cristobal Fornof3ae59c2020-08-19 13:16:23 -05001576 handle = tx_scrq->handle;
Nathan Fontenot161b8a82017-05-03 14:05:08 -04001577
Thomas Falcon032c5e82015-12-21 11:26:06 -06001578 index = tx_pool->free_map[tx_pool->consumer_index];
Thomas Falconfdb06102017-10-17 12:36:55 -05001579
Thomas Falcon86b61a52018-03-16 20:00:29 -05001580 if (index == IBMVNIC_INVALID_MAP) {
1581 dev_kfree_skb_any(skb);
1582 tx_send_failed++;
1583 tx_dropped++;
1584 ret = NETDEV_TX_OK;
1585 goto out;
1586 }
1587
1588 tx_pool->free_map[tx_pool->consumer_index] = IBMVNIC_INVALID_MAP;
1589
Thomas Falcon06b3e352018-03-16 20:00:28 -05001590 offset = index * tx_pool->buf_size;
1591 dst = tx_pool->long_term_buff.buff + offset;
1592 memset(dst, 0, tx_pool->buf_size);
1593 data_dma_addr = tx_pool->long_term_buff.addr + offset;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001594
Thomas Falcon15482052017-10-17 12:36:54 -05001595 if (skb_shinfo(skb)->nr_frags) {
1596 int cur, i;
1597
1598 /* Copy the head */
1599 skb_copy_from_linear_data(skb, dst, skb_headlen(skb));
1600 cur = skb_headlen(skb);
1601
1602 /* Copy the frags */
1603 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1604 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1605
1606 memcpy(dst + cur,
1607 page_address(skb_frag_page(frag)) +
Jonathan Lemonb54c9d52019-07-30 07:40:33 -07001608 skb_frag_off(frag), skb_frag_size(frag));
Thomas Falcon15482052017-10-17 12:36:54 -05001609 cur += skb_frag_size(frag);
1610 }
1611 } else {
1612 skb_copy_from_linear_data(skb, dst, skb->len);
1613 }
1614
Thomas Falcon032c5e82015-12-21 11:26:06 -06001615 tx_pool->consumer_index =
Thomas Falcon06b3e352018-03-16 20:00:28 -05001616 (tx_pool->consumer_index + 1) % tx_pool->num_buffers;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001617
1618 tx_buff = &tx_pool->tx_buff[index];
1619 tx_buff->skb = skb;
1620 tx_buff->data_dma[0] = data_dma_addr;
1621 tx_buff->data_len[0] = skb->len;
1622 tx_buff->index = index;
1623 tx_buff->pool_index = queue_num;
1624 tx_buff->last_frag = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001625
1626 memset(&tx_crq, 0, sizeof(tx_crq));
1627 tx_crq.v1.first = IBMVNIC_CRQ_CMD;
1628 tx_crq.v1.type = IBMVNIC_TX_DESC;
1629 tx_crq.v1.n_crq_elem = 1;
1630 tx_crq.v1.n_sge = 1;
1631 tx_crq.v1.flags1 = IBMVNIC_TX_COMP_NEEDED;
Thomas Falcon06b3e352018-03-16 20:00:28 -05001632
Thomas Falconfdb06102017-10-17 12:36:55 -05001633 if (skb_is_gso(skb))
Thomas Falcon06b3e352018-03-16 20:00:28 -05001634 tx_crq.v1.correlator =
1635 cpu_to_be32(index | IBMVNIC_TSO_POOL_MASK);
Thomas Falconfdb06102017-10-17 12:36:55 -05001636 else
Thomas Falcon06b3e352018-03-16 20:00:28 -05001637 tx_crq.v1.correlator = cpu_to_be32(index);
1638 tx_crq.v1.dma_reg = cpu_to_be16(tx_pool->long_term_buff.map_id);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001639 tx_crq.v1.sge_len = cpu_to_be32(skb->len);
1640 tx_crq.v1.ioba = cpu_to_be64(data_dma_addr);
1641
Michał Mirosławe84b4792018-11-07 17:50:52 +01001642 if (adapter->vlan_header_insertion && skb_vlan_tag_present(skb)) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06001643 tx_crq.v1.flags2 |= IBMVNIC_TX_VLAN_INSERT;
1644 tx_crq.v1.vlan_id = cpu_to_be16(skb->vlan_tci);
1645 }
1646
1647 if (skb->protocol == htons(ETH_P_IP)) {
Thomas Falcona0dca102018-01-18 19:29:48 -06001648 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV4;
1649 proto = ip_hdr(skb)->protocol;
1650 } else if (skb->protocol == htons(ETH_P_IPV6)) {
1651 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV6;
1652 proto = ipv6_hdr(skb)->nexthdr;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001653 }
1654
Thomas Falcona0dca102018-01-18 19:29:48 -06001655 if (proto == IPPROTO_TCP)
1656 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_TCP;
1657 else if (proto == IPPROTO_UDP)
1658 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_UDP;
1659
Thomas Falconad7775d2016-04-01 17:20:34 -05001660 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06001661 tx_crq.v1.flags1 |= IBMVNIC_TX_CHKSUM_OFFLOAD;
Thomas Falconad7775d2016-04-01 17:20:34 -05001662 hdrs += 2;
1663 }
Thomas Falconfdb06102017-10-17 12:36:55 -05001664 if (skb_is_gso(skb)) {
1665 tx_crq.v1.flags1 |= IBMVNIC_TX_LSO;
1666 tx_crq.v1.mss = cpu_to_be16(skb_shinfo(skb)->gso_size);
1667 hdrs += 2;
1668 }
Thomas Falconad7775d2016-04-01 17:20:34 -05001669 /* determine if l2/3/4 headers are sent to firmware */
John Allen2fa56a42018-02-09 13:19:46 -06001670 if ((*hdrs >> 7) & 1) {
Thomas Falconad7775d2016-04-01 17:20:34 -05001671 build_hdr_descs_arr(tx_buff, &num_entries, *hdrs);
1672 tx_crq.v1.n_crq_elem = num_entries;
Thomas Falconecba6162018-02-26 18:10:55 -06001673 tx_buff->num_entries = num_entries;
Thomas Falconad7775d2016-04-01 17:20:34 -05001674 tx_buff->indir_arr[0] = tx_crq;
1675 tx_buff->indir_dma = dma_map_single(dev, tx_buff->indir_arr,
1676 sizeof(tx_buff->indir_arr),
1677 DMA_TO_DEVICE);
1678 if (dma_mapping_error(dev, tx_buff->indir_dma)) {
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001679 dev_kfree_skb_any(skb);
1680 tx_buff->skb = NULL;
Thomas Falconad7775d2016-04-01 17:20:34 -05001681 if (!firmware_has_feature(FW_FEATURE_CMO))
1682 dev_err(dev, "tx: unable to map descriptor array\n");
1683 tx_map_failed++;
1684 tx_dropped++;
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001685 ret = NETDEV_TX_OK;
Thomas Falcon86b61a52018-03-16 20:00:29 -05001686 goto tx_err_out;
Thomas Falconad7775d2016-04-01 17:20:34 -05001687 }
Cristobal Fornof3ae59c2020-08-19 13:16:23 -05001688 lpar_rc = send_subcrq_indirect(adapter, handle,
Thomas Falconad7775d2016-04-01 17:20:34 -05001689 (u64)tx_buff->indir_dma,
1690 (u64)num_entries);
Thomas Falcon80f0fe02019-08-14 14:57:05 -05001691 dma_unmap_single(dev, tx_buff->indir_dma,
1692 sizeof(tx_buff->indir_arr), DMA_TO_DEVICE);
Thomas Falconad7775d2016-04-01 17:20:34 -05001693 } else {
Thomas Falconecba6162018-02-26 18:10:55 -06001694 tx_buff->num_entries = num_entries;
Cristobal Fornof3ae59c2020-08-19 13:16:23 -05001695 lpar_rc = send_subcrq(adapter, handle,
John Allen498cd8e2016-04-06 11:49:55 -05001696 &tx_crq);
Thomas Falconad7775d2016-04-01 17:20:34 -05001697 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001698 if (lpar_rc != H_SUCCESS) {
Thomas Falcon2d14d372018-07-13 12:03:32 -05001699 if (lpar_rc != H_CLOSED && lpar_rc != H_PARAMETER)
1700 dev_err_ratelimited(dev, "tx: send failed\n");
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001701 dev_kfree_skb_any(skb);
1702 tx_buff->skb = NULL;
1703
Thomas Falcon5a18e1e2018-04-06 18:37:05 -05001704 if (lpar_rc == H_CLOSED || adapter->failover_pending) {
1705 /* Disable TX and report carrier off if queue is closed
1706 * or pending failover.
Thomas Falconb8c80b82017-05-26 10:30:42 -04001707 * Firmware guarantees that a signal will be sent to the
1708 * driver, triggering a reset or some other action.
1709 */
1710 netif_tx_stop_all_queues(netdev);
1711 netif_carrier_off(netdev);
1712 }
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001713
Thomas Falcon032c5e82015-12-21 11:26:06 -06001714 tx_send_failed++;
1715 tx_dropped++;
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001716 ret = NETDEV_TX_OK;
Thomas Falcon86b61a52018-03-16 20:00:29 -05001717 goto tx_err_out;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001718 }
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001719
Thomas Falconffc385b2018-02-18 10:08:41 -06001720 if (atomic_add_return(num_entries, &tx_scrq->used)
Brian King58c8c0c2017-04-19 13:44:47 -04001721 >= adapter->req_tx_entries_per_subcrq) {
Thomas Falcon0aecb132018-02-26 18:10:58 -06001722 netdev_dbg(netdev, "Stopping queue %d\n", queue_num);
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001723 netif_stop_subqueue(netdev, queue_num);
1724 }
1725
Thomas Falcon032c5e82015-12-21 11:26:06 -06001726 tx_packets++;
1727 tx_bytes += skb->len;
1728 txq->trans_start = jiffies;
1729 ret = NETDEV_TX_OK;
Thomas Falcon86b61a52018-03-16 20:00:29 -05001730 goto out;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001731
Thomas Falcon86b61a52018-03-16 20:00:29 -05001732tx_err_out:
1733 /* roll back consumer index and map array*/
1734 if (tx_pool->consumer_index == 0)
1735 tx_pool->consumer_index =
1736 tx_pool->num_buffers - 1;
1737 else
1738 tx_pool->consumer_index--;
1739 tx_pool->free_map[tx_pool->consumer_index] = index;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001740out:
1741 netdev->stats.tx_dropped += tx_dropped;
1742 netdev->stats.tx_bytes += tx_bytes;
1743 netdev->stats.tx_packets += tx_packets;
1744 adapter->tx_send_failed += tx_send_failed;
1745 adapter->tx_map_failed += tx_map_failed;
John Allen3d52b592017-08-02 16:44:14 -05001746 adapter->tx_stats_buffers[queue_num].packets += tx_packets;
1747 adapter->tx_stats_buffers[queue_num].bytes += tx_bytes;
1748 adapter->tx_stats_buffers[queue_num].dropped_packets += tx_dropped;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001749
1750 return ret;
1751}
1752
1753static void ibmvnic_set_multi(struct net_device *netdev)
1754{
1755 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1756 struct netdev_hw_addr *ha;
1757 union ibmvnic_crq crq;
1758
1759 memset(&crq, 0, sizeof(crq));
1760 crq.request_capability.first = IBMVNIC_CRQ_CMD;
1761 crq.request_capability.cmd = REQUEST_CAPABILITY;
1762
1763 if (netdev->flags & IFF_PROMISC) {
1764 if (!adapter->promisc_supported)
1765 return;
1766 } else {
1767 if (netdev->flags & IFF_ALLMULTI) {
1768 /* Accept all multicast */
1769 memset(&crq, 0, sizeof(crq));
1770 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1771 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1772 crq.multicast_ctrl.flags = IBMVNIC_ENABLE_ALL;
1773 ibmvnic_send_crq(adapter, &crq);
1774 } else if (netdev_mc_empty(netdev)) {
1775 /* Reject all multicast */
1776 memset(&crq, 0, sizeof(crq));
1777 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1778 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1779 crq.multicast_ctrl.flags = IBMVNIC_DISABLE_ALL;
1780 ibmvnic_send_crq(adapter, &crq);
1781 } else {
1782 /* Accept one or more multicast(s) */
1783 netdev_for_each_mc_addr(ha, netdev) {
1784 memset(&crq, 0, sizeof(crq));
1785 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1786 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1787 crq.multicast_ctrl.flags = IBMVNIC_ENABLE_MC;
1788 ether_addr_copy(&crq.multicast_ctrl.mac_addr[0],
1789 ha->addr);
1790 ibmvnic_send_crq(adapter, &crq);
1791 }
1792 }
1793 }
1794}
1795
Thomas Falcon62740e92019-05-09 23:13:43 -05001796static int __ibmvnic_set_mac(struct net_device *netdev, u8 *dev_addr)
Thomas Falcon032c5e82015-12-21 11:26:06 -06001797{
1798 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001799 union ibmvnic_crq crq;
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05001800 int rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001801
Thomas Falcon62740e92019-05-09 23:13:43 -05001802 if (!is_valid_ether_addr(dev_addr)) {
1803 rc = -EADDRNOTAVAIL;
1804 goto err;
1805 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001806
1807 memset(&crq, 0, sizeof(crq));
1808 crq.change_mac_addr.first = IBMVNIC_CRQ_CMD;
1809 crq.change_mac_addr.cmd = CHANGE_MAC_ADDR;
Thomas Falcon62740e92019-05-09 23:13:43 -05001810 ether_addr_copy(&crq.change_mac_addr.mac_addr[0], dev_addr);
Thomas Falconf8136142018-01-29 13:45:05 -06001811
Thomas Falconff25dcb2019-11-25 17:12:56 -06001812 mutex_lock(&adapter->fw_lock);
1813 adapter->fw_done_rc = 0;
Thomas Falcon070eca92019-11-25 17:12:53 -06001814 reinit_completion(&adapter->fw_done);
Thomas Falconff25dcb2019-11-25 17:12:56 -06001815
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05001816 rc = ibmvnic_send_crq(adapter, &crq);
Thomas Falcon62740e92019-05-09 23:13:43 -05001817 if (rc) {
1818 rc = -EIO;
Thomas Falconff25dcb2019-11-25 17:12:56 -06001819 mutex_unlock(&adapter->fw_lock);
Thomas Falcon62740e92019-05-09 23:13:43 -05001820 goto err;
1821 }
1822
Thomas Falcon476d96c2019-11-25 17:12:55 -06001823 rc = ibmvnic_wait_for_completion(adapter, &adapter->fw_done, 10000);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001824 /* netdev->dev_addr is changed in handle_change_mac_rsp function */
Thomas Falcon476d96c2019-11-25 17:12:55 -06001825 if (rc || adapter->fw_done_rc) {
Thomas Falcon62740e92019-05-09 23:13:43 -05001826 rc = -EIO;
Thomas Falconff25dcb2019-11-25 17:12:56 -06001827 mutex_unlock(&adapter->fw_lock);
Thomas Falcon62740e92019-05-09 23:13:43 -05001828 goto err;
1829 }
Thomas Falconff25dcb2019-11-25 17:12:56 -06001830 mutex_unlock(&adapter->fw_lock);
Thomas Falcon62740e92019-05-09 23:13:43 -05001831 return 0;
1832err:
1833 ether_addr_copy(adapter->mac_addr, netdev->dev_addr);
1834 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001835}
1836
John Allenc26eba02017-10-26 16:23:25 -05001837static int ibmvnic_set_mac(struct net_device *netdev, void *p)
1838{
1839 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1840 struct sockaddr *addr = p;
Thomas Falconf8136142018-01-29 13:45:05 -06001841 int rc;
John Allenc26eba02017-10-26 16:23:25 -05001842
Thomas Falcon62740e92019-05-09 23:13:43 -05001843 rc = 0;
Lijun Pan8fc36722020-10-27 17:04:56 -05001844 if (!is_valid_ether_addr(addr->sa_data))
1845 return -EADDRNOTAVAIL;
1846
1847 if (adapter->state != VNIC_PROBED) {
1848 ether_addr_copy(adapter->mac_addr, addr->sa_data);
Thomas Falcon62740e92019-05-09 23:13:43 -05001849 rc = __ibmvnic_set_mac(netdev, addr->sa_data);
Lijun Pan8fc36722020-10-27 17:04:56 -05001850 }
John Allenc26eba02017-10-26 16:23:25 -05001851
Thomas Falconf8136142018-01-29 13:45:05 -06001852 return rc;
John Allenc26eba02017-10-26 16:23:25 -05001853}
1854
Nathan Fontenoted651a12017-05-03 14:04:38 -04001855/**
Juliet Kimb27507b2019-09-20 16:11:22 -04001856 * do_change_param_reset returns zero if we are able to keep processing reset
1857 * events, or non-zero if we hit a fatal error and must halt.
1858 */
1859static int do_change_param_reset(struct ibmvnic_adapter *adapter,
1860 struct ibmvnic_rwi *rwi,
1861 u32 reset_state)
1862{
1863 struct net_device *netdev = adapter->netdev;
1864 int i, rc;
1865
1866 netdev_dbg(adapter->netdev, "Change param resetting driver (%d)\n",
1867 rwi->reset_reason);
1868
1869 netif_carrier_off(netdev);
1870 adapter->reset_reason = rwi->reset_reason;
1871
1872 ibmvnic_cleanup(netdev);
1873
1874 if (reset_state == VNIC_OPEN) {
1875 rc = __ibmvnic_close(netdev);
1876 if (rc)
1877 return rc;
1878 }
1879
1880 release_resources(adapter);
1881 release_sub_crqs(adapter, 1);
1882 release_crq_queue(adapter);
1883
1884 adapter->state = VNIC_PROBED;
1885
1886 rc = init_crq_queue(adapter);
1887
1888 if (rc) {
1889 netdev_err(adapter->netdev,
1890 "Couldn't initialize crq. rc=%d\n", rc);
1891 return rc;
1892 }
1893
Lijun Pan635e4422020-08-19 17:52:26 -05001894 rc = ibmvnic_reset_init(adapter, true);
Juliet Kimb27507b2019-09-20 16:11:22 -04001895 if (rc)
1896 return IBMVNIC_INIT_FAILED;
1897
1898 /* If the adapter was in PROBE state prior to the reset,
1899 * exit here.
1900 */
1901 if (reset_state == VNIC_PROBED)
1902 return 0;
1903
1904 rc = ibmvnic_login(netdev);
1905 if (rc) {
1906 adapter->state = reset_state;
1907 return rc;
1908 }
1909
1910 rc = init_resources(adapter);
1911 if (rc)
1912 return rc;
1913
1914 ibmvnic_disable_irqs(adapter);
1915
1916 adapter->state = VNIC_CLOSED;
1917
1918 if (reset_state == VNIC_CLOSED)
1919 return 0;
1920
1921 rc = __ibmvnic_open(netdev);
1922 if (rc)
1923 return IBMVNIC_OPEN_FAILED;
1924
1925 /* refresh device's multicast list */
1926 ibmvnic_set_multi(netdev);
1927
1928 /* kick napi */
1929 for (i = 0; i < adapter->req_rx_queues; i++)
1930 napi_schedule(&adapter->napi[i]);
1931
1932 return 0;
1933}
1934
1935/**
Nathan Fontenoted651a12017-05-03 14:04:38 -04001936 * do_reset returns zero if we are able to keep processing reset events, or
1937 * non-zero if we hit a fatal error and must halt.
1938 */
1939static int do_reset(struct ibmvnic_adapter *adapter,
1940 struct ibmvnic_rwi *rwi, u32 reset_state)
1941{
John Allen896d8692018-01-18 16:26:31 -06001942 u64 old_num_rx_queues, old_num_tx_queues;
Thomas Falcon5bf032e2018-11-21 11:17:59 -06001943 u64 old_num_rx_slots, old_num_tx_slots;
Nathan Fontenoted651a12017-05-03 14:04:38 -04001944 struct net_device *netdev = adapter->netdev;
1945 int i, rc;
1946
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001947 netdev_dbg(adapter->netdev, "Re-setting driver (%d)\n",
1948 rwi->reset_reason);
1949
Juliet Kimb27507b2019-09-20 16:11:22 -04001950 rtnl_lock();
Sukadev Bhattiprolu1d850492020-10-30 10:07:11 -07001951 /*
1952 * Now that we have the rtnl lock, clear any pending failover.
1953 * This will ensure ibmvnic_open() has either completed or will
1954 * block until failover is complete.
1955 */
1956 if (rwi->reset_reason == VNIC_RESET_FAILOVER)
1957 adapter->failover_pending = false;
Juliet Kimb27507b2019-09-20 16:11:22 -04001958
Nathan Fontenoted651a12017-05-03 14:04:38 -04001959 netif_carrier_off(netdev);
1960 adapter->reset_reason = rwi->reset_reason;
1961
John Allen896d8692018-01-18 16:26:31 -06001962 old_num_rx_queues = adapter->req_rx_queues;
1963 old_num_tx_queues = adapter->req_tx_queues;
Thomas Falcon5bf032e2018-11-21 11:17:59 -06001964 old_num_rx_slots = adapter->req_rx_add_entries_per_subcrq;
1965 old_num_tx_slots = adapter->req_tx_entries_per_subcrq;
John Allen896d8692018-01-18 16:26:31 -06001966
Nathan Fontenot30f79622018-04-06 18:37:06 -05001967 ibmvnic_cleanup(netdev);
1968
Thomas Falcon1f946082019-06-07 16:03:53 -05001969 if (reset_state == VNIC_OPEN &&
1970 adapter->reset_reason != VNIC_RESET_MOBILITY &&
Nathan Fontenot30f79622018-04-06 18:37:06 -05001971 adapter->reset_reason != VNIC_RESET_FAILOVER) {
Juliet Kimb27507b2019-09-20 16:11:22 -04001972 adapter->state = VNIC_CLOSING;
Nathan Fontenoted651a12017-05-03 14:04:38 -04001973
Juliet Kimb27507b2019-09-20 16:11:22 -04001974 /* Release the RTNL lock before link state change and
1975 * re-acquire after the link state change to allow
1976 * linkwatch_event to grab the RTNL lock and run during
1977 * a reset.
1978 */
1979 rtnl_unlock();
1980 rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_DN);
1981 rtnl_lock();
1982 if (rc)
1983 goto out;
1984
1985 if (adapter->state != VNIC_CLOSING) {
1986 rc = -1;
1987 goto out;
1988 }
1989
1990 adapter->state = VNIC_CLOSED;
John Allenc26eba02017-10-26 16:23:25 -05001991 }
1992
John Allen8cb31cf2017-05-26 10:30:37 -04001993 if (adapter->reset_reason != VNIC_RESET_NON_FATAL) {
1994 /* remove the closed state so when we call open it appears
1995 * we are coming from the probed state.
1996 */
Nathan Fontenoted651a12017-05-03 14:04:38 -04001997 adapter->state = VNIC_PROBED;
John Allen8cb31cf2017-05-26 10:30:37 -04001998
Juliet Kimb27507b2019-09-20 16:11:22 -04001999 if (adapter->reset_reason == VNIC_RESET_MOBILITY) {
Nathan Fontenot30f79622018-04-06 18:37:06 -05002000 rc = ibmvnic_reenable_crq_queue(adapter);
2001 release_sub_crqs(adapter, 1);
2002 } else {
2003 rc = ibmvnic_reset_crq(adapter);
Dany Madden8b40eb732020-06-18 15:24:13 -04002004 if (rc == H_CLOSED || rc == H_SUCCESS) {
Nathan Fontenot30f79622018-04-06 18:37:06 -05002005 rc = vio_enable_interrupts(adapter->vdev);
Dany Madden8b40eb732020-06-18 15:24:13 -04002006 if (rc)
2007 netdev_err(adapter->netdev,
2008 "Reset failed to enable interrupts. rc=%d\n",
2009 rc);
2010 }
Nathan Fontenot30f79622018-04-06 18:37:06 -05002011 }
2012
2013 if (rc) {
2014 netdev_err(adapter->netdev,
Dany Madden8b40eb732020-06-18 15:24:13 -04002015 "Reset couldn't initialize crq. rc=%d\n", rc);
Juliet Kimb27507b2019-09-20 16:11:22 -04002016 goto out;
Nathan Fontenot30f79622018-04-06 18:37:06 -05002017 }
2018
Lijun Pan635e4422020-08-19 17:52:26 -05002019 rc = ibmvnic_reset_init(adapter, true);
Juliet Kimb27507b2019-09-20 16:11:22 -04002020 if (rc) {
2021 rc = IBMVNIC_INIT_FAILED;
2022 goto out;
2023 }
John Allen8cb31cf2017-05-26 10:30:37 -04002024
2025 /* If the adapter was in PROBE state prior to the reset,
2026 * exit here.
2027 */
Juliet Kimb27507b2019-09-20 16:11:22 -04002028 if (reset_state == VNIC_PROBED) {
2029 rc = 0;
2030 goto out;
2031 }
John Allen8cb31cf2017-05-26 10:30:37 -04002032
2033 rc = ibmvnic_login(netdev);
2034 if (rc) {
John Allen3578a7e2018-07-16 10:29:30 -05002035 adapter->state = reset_state;
Juliet Kimb27507b2019-09-20 16:11:22 -04002036 goto out;
John Allen8cb31cf2017-05-26 10:30:37 -04002037 }
2038
Juliet Kimb27507b2019-09-20 16:11:22 -04002039 if (adapter->req_rx_queues != old_num_rx_queues ||
2040 adapter->req_tx_queues != old_num_tx_queues ||
2041 adapter->req_rx_add_entries_per_subcrq !=
2042 old_num_rx_slots ||
2043 adapter->req_tx_entries_per_subcrq !=
Mingming Cao9f134572020-08-25 13:26:41 -04002044 old_num_tx_slots ||
2045 !adapter->rx_pool ||
2046 !adapter->tso_pool ||
2047 !adapter->tx_pool) {
John Allen896d8692018-01-18 16:26:31 -06002048 release_rx_pools(adapter);
2049 release_tx_pools(adapter);
Juliet Kima5681e22018-11-19 15:59:22 -06002050 release_napi(adapter);
2051 release_vpd_data(adapter);
2052
2053 rc = init_resources(adapter);
Thomas Falconf611a5b2018-08-30 13:19:53 -05002054 if (rc)
Juliet Kimb27507b2019-09-20 16:11:22 -04002055 goto out;
Nathan Fontenotd9043c12018-02-19 13:30:14 -06002056
John Allenc26eba02017-10-26 16:23:25 -05002057 } else {
2058 rc = reset_tx_pools(adapter);
Jakub Kicinski8ae4dff2020-09-04 21:07:49 -07002059 if (rc) {
Mingming Cao9f134572020-08-25 13:26:41 -04002060 netdev_dbg(adapter->netdev, "reset tx pools failed (%d)\n",
2061 rc);
Juliet Kimb27507b2019-09-20 16:11:22 -04002062 goto out;
Jakub Kicinski8ae4dff2020-09-04 21:07:49 -07002063 }
Nathan Fontenot8c0543a2017-05-26 10:31:06 -04002064
John Allenc26eba02017-10-26 16:23:25 -05002065 rc = reset_rx_pools(adapter);
Jakub Kicinski8ae4dff2020-09-04 21:07:49 -07002066 if (rc) {
Mingming Cao9f134572020-08-25 13:26:41 -04002067 netdev_dbg(adapter->netdev, "reset rx pools failed (%d)\n",
2068 rc);
Juliet Kimb27507b2019-09-20 16:11:22 -04002069 goto out;
Jakub Kicinski8ae4dff2020-09-04 21:07:49 -07002070 }
John Allenc26eba02017-10-26 16:23:25 -05002071 }
Thomas Falcon134bbe72018-05-16 15:49:04 -05002072 ibmvnic_disable_irqs(adapter);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002073 }
John Allene676d812018-03-14 10:41:29 -05002074 adapter->state = VNIC_CLOSED;
2075
Juliet Kimb27507b2019-09-20 16:11:22 -04002076 if (reset_state == VNIC_CLOSED) {
2077 rc = 0;
2078 goto out;
2079 }
John Allene676d812018-03-14 10:41:29 -05002080
Nathan Fontenoted651a12017-05-03 14:04:38 -04002081 rc = __ibmvnic_open(netdev);
2082 if (rc) {
Juliet Kimb27507b2019-09-20 16:11:22 -04002083 rc = IBMVNIC_OPEN_FAILED;
2084 goto out;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002085 }
2086
Thomas Falconbe32a242019-06-07 16:03:54 -05002087 /* refresh device's multicast list */
2088 ibmvnic_set_multi(netdev);
2089
Nathan Fontenoted651a12017-05-03 14:04:38 -04002090 /* kick napi */
2091 for (i = 0; i < adapter->req_rx_queues; i++)
2092 napi_schedule(&adapter->napi[i]);
2093
Juliet Kimb27507b2019-09-20 16:11:22 -04002094 if (adapter->reset_reason != VNIC_RESET_FAILOVER)
Thomas Falcon986103e2018-11-30 10:59:08 -06002095 call_netdevice_notifiers(NETDEV_NOTIFY_PEERS, netdev);
Nathan Fontenot61d3e1d2017-06-12 20:47:45 -04002096
Juliet Kimb27507b2019-09-20 16:11:22 -04002097 rc = 0;
2098
2099out:
2100 rtnl_unlock();
2101
2102 return rc;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002103}
2104
Thomas Falcon2770a792018-05-23 13:38:02 -05002105static int do_hard_reset(struct ibmvnic_adapter *adapter,
2106 struct ibmvnic_rwi *rwi, u32 reset_state)
2107{
2108 struct net_device *netdev = adapter->netdev;
2109 int rc;
2110
2111 netdev_dbg(adapter->netdev, "Hard resetting driver (%d)\n",
2112 rwi->reset_reason);
2113
2114 netif_carrier_off(netdev);
2115 adapter->reset_reason = rwi->reset_reason;
2116
2117 ibmvnic_cleanup(netdev);
2118 release_resources(adapter);
2119 release_sub_crqs(adapter, 0);
2120 release_crq_queue(adapter);
2121
2122 /* remove the closed state so when we call open it appears
2123 * we are coming from the probed state.
2124 */
2125 adapter->state = VNIC_PROBED;
2126
Thomas Falconbbd669a2019-04-04 18:58:26 -05002127 reinit_completion(&adapter->init_done);
Thomas Falcon2770a792018-05-23 13:38:02 -05002128 rc = init_crq_queue(adapter);
2129 if (rc) {
2130 netdev_err(adapter->netdev,
2131 "Couldn't initialize crq. rc=%d\n", rc);
2132 return rc;
2133 }
2134
Lijun Pan635e4422020-08-19 17:52:26 -05002135 rc = ibmvnic_reset_init(adapter, false);
Thomas Falcon2770a792018-05-23 13:38:02 -05002136 if (rc)
2137 return rc;
2138
2139 /* If the adapter was in PROBE state prior to the reset,
2140 * exit here.
2141 */
2142 if (reset_state == VNIC_PROBED)
2143 return 0;
2144
2145 rc = ibmvnic_login(netdev);
2146 if (rc) {
2147 adapter->state = VNIC_PROBED;
2148 return 0;
2149 }
Juliet Kima5681e22018-11-19 15:59:22 -06002150
2151 rc = init_resources(adapter);
Thomas Falcon2770a792018-05-23 13:38:02 -05002152 if (rc)
2153 return rc;
2154
2155 ibmvnic_disable_irqs(adapter);
2156 adapter->state = VNIC_CLOSED;
2157
2158 if (reset_state == VNIC_CLOSED)
2159 return 0;
2160
2161 rc = __ibmvnic_open(netdev);
Juliet Kimb27507b2019-09-20 16:11:22 -04002162 if (rc)
2163 return IBMVNIC_OPEN_FAILED;
Thomas Falcon2770a792018-05-23 13:38:02 -05002164
Thomas Falcon2770a792018-05-23 13:38:02 -05002165 return 0;
2166}
2167
Nathan Fontenoted651a12017-05-03 14:04:38 -04002168static struct ibmvnic_rwi *get_next_rwi(struct ibmvnic_adapter *adapter)
2169{
2170 struct ibmvnic_rwi *rwi;
Thomas Falcon6c5c7482018-12-10 15:22:22 -06002171 unsigned long flags;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002172
Thomas Falcon6c5c7482018-12-10 15:22:22 -06002173 spin_lock_irqsave(&adapter->rwi_lock, flags);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002174
2175 if (!list_empty(&adapter->rwi_list)) {
2176 rwi = list_first_entry(&adapter->rwi_list, struct ibmvnic_rwi,
2177 list);
2178 list_del(&rwi->list);
2179 } else {
2180 rwi = NULL;
2181 }
2182
Thomas Falcon6c5c7482018-12-10 15:22:22 -06002183 spin_unlock_irqrestore(&adapter->rwi_lock, flags);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002184 return rwi;
2185}
2186
2187static void free_all_rwi(struct ibmvnic_adapter *adapter)
2188{
2189 struct ibmvnic_rwi *rwi;
2190
2191 rwi = get_next_rwi(adapter);
2192 while (rwi) {
2193 kfree(rwi);
2194 rwi = get_next_rwi(adapter);
2195 }
2196}
2197
2198static void __ibmvnic_reset(struct work_struct *work)
2199{
2200 struct ibmvnic_rwi *rwi;
2201 struct ibmvnic_adapter *adapter;
Juliet Kim7d7195a2020-03-10 09:23:58 -05002202 bool saved_state = false;
2203 unsigned long flags;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002204 u32 reset_state;
John Allenc26eba02017-10-26 16:23:25 -05002205 int rc = 0;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002206
2207 adapter = container_of(work, struct ibmvnic_adapter, ibmvnic_reset);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002208
Juliet Kim7ed5b312019-09-20 16:11:23 -04002209 if (test_and_set_bit_lock(0, &adapter->resetting)) {
2210 schedule_delayed_work(&adapter->ibmvnic_delayed_reset,
2211 IBMVNIC_RESET_DELAY);
2212 return;
2213 }
2214
Nathan Fontenoted651a12017-05-03 14:04:38 -04002215 rwi = get_next_rwi(adapter);
2216 while (rwi) {
Juliet Kim7d7195a2020-03-10 09:23:58 -05002217 spin_lock_irqsave(&adapter->state_lock, flags);
2218
Thomas Falcon36f10312019-08-27 11:10:04 -05002219 if (adapter->state == VNIC_REMOVING ||
Michal Suchanekc8dc5592019-09-09 22:44:51 +02002220 adapter->state == VNIC_REMOVED) {
Juliet Kim7d7195a2020-03-10 09:23:58 -05002221 spin_unlock_irqrestore(&adapter->state_lock, flags);
Juliet Kim1c2977c2019-09-05 17:30:01 -04002222 kfree(rwi);
2223 rc = EBUSY;
2224 break;
2225 }
Thomas Falcon36f10312019-08-27 11:10:04 -05002226
Juliet Kim7d7195a2020-03-10 09:23:58 -05002227 if (!saved_state) {
2228 reset_state = adapter->state;
2229 adapter->state = VNIC_RESETTING;
2230 saved_state = true;
2231 }
2232 spin_unlock_irqrestore(&adapter->state_lock, flags);
2233
Juliet Kimb27507b2019-09-20 16:11:22 -04002234 if (rwi->reset_reason == VNIC_RESET_CHANGE_PARAM) {
2235 /* CHANGE_PARAM requestor holds rtnl_lock */
2236 rc = do_change_param_reset(adapter, rwi, reset_state);
2237 } else if (adapter->force_reset_recovery) {
Sukadev Bhattiprolu1d850492020-10-30 10:07:11 -07002238 /*
2239 * Since we are doing a hard reset now, clear the
2240 * failover_pending flag so we don't ignore any
2241 * future MOBILITY or other resets.
2242 */
2243 adapter->failover_pending = false;
2244
Juliet Kimb27507b2019-09-20 16:11:22 -04002245 /* Transport event occurred during previous reset */
2246 if (adapter->wait_for_reset) {
2247 /* Previous was CHANGE_PARAM; caller locked */
2248 adapter->force_reset_recovery = false;
2249 rc = do_hard_reset(adapter, rwi, reset_state);
2250 } else {
2251 rtnl_lock();
2252 adapter->force_reset_recovery = false;
2253 rc = do_hard_reset(adapter, rwi, reset_state);
2254 rtnl_unlock();
2255 }
Juliet Kimf9c6cea2020-04-30 13:22:11 -05002256 } else if (!(rwi->reset_reason == VNIC_RESET_FATAL &&
2257 adapter->from_passive_init)) {
Thomas Falcon2770a792018-05-23 13:38:02 -05002258 rc = do_reset(adapter, rwi, reset_state);
2259 }
Nathan Fontenoted651a12017-05-03 14:04:38 -04002260 kfree(rwi);
Juliet Kimb27507b2019-09-20 16:11:22 -04002261 if (rc == IBMVNIC_OPEN_FAILED) {
2262 if (list_empty(&adapter->rwi_list))
2263 adapter->state = VNIC_CLOSED;
2264 else
2265 adapter->state = reset_state;
2266 rc = 0;
2267 } else if (rc && rc != IBMVNIC_INIT_FAILED &&
Thomas Falcon2770a792018-05-23 13:38:02 -05002268 !adapter->force_reset_recovery)
Nathan Fontenoted651a12017-05-03 14:04:38 -04002269 break;
2270
2271 rwi = get_next_rwi(adapter);
Juliet Kim7ed5b312019-09-20 16:11:23 -04002272
2273 if (rwi && (rwi->reset_reason == VNIC_RESET_FAILOVER ||
2274 rwi->reset_reason == VNIC_RESET_MOBILITY))
2275 adapter->force_reset_recovery = true;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002276 }
2277
John Allenc26eba02017-10-26 16:23:25 -05002278 if (adapter->wait_for_reset) {
John Allenc26eba02017-10-26 16:23:25 -05002279 adapter->reset_done_rc = rc;
2280 complete(&adapter->reset_done);
2281 }
2282
Nathan Fontenoted651a12017-05-03 14:04:38 -04002283 if (rc) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002284 netdev_dbg(adapter->netdev, "Reset failed\n");
Nathan Fontenoted651a12017-05-03 14:04:38 -04002285 free_all_rwi(adapter);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002286 }
Juliet Kim1c2977c2019-09-05 17:30:01 -04002287
Juliet Kim7ed5b312019-09-20 16:11:23 -04002288 clear_bit_unlock(0, &adapter->resetting);
2289}
2290
2291static void __ibmvnic_delayed_reset(struct work_struct *work)
2292{
2293 struct ibmvnic_adapter *adapter;
2294
2295 adapter = container_of(work, struct ibmvnic_adapter,
2296 ibmvnic_delayed_reset.work);
2297 __ibmvnic_reset(&adapter->ibmvnic_reset);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002298}
2299
Thomas Falconaf894d22018-04-06 18:37:04 -05002300static int ibmvnic_reset(struct ibmvnic_adapter *adapter,
2301 enum ibmvnic_reset_reason reason)
Nathan Fontenoted651a12017-05-03 14:04:38 -04002302{
Thomas Falcon2770a792018-05-23 13:38:02 -05002303 struct list_head *entry, *tmp_entry;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002304 struct ibmvnic_rwi *rwi, *tmp;
2305 struct net_device *netdev = adapter->netdev;
Thomas Falcon6c5c7482018-12-10 15:22:22 -06002306 unsigned long flags;
Thomas Falconaf894d22018-04-06 18:37:04 -05002307 int ret;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002308
Sukadev Bhattiprolu1d850492020-10-30 10:07:11 -07002309 /*
2310 * If failover is pending don't schedule any other reset.
2311 * Instead let the failover complete. If there is already a
2312 * a failover reset scheduled, we will detect and drop the
2313 * duplicate reset when walking the ->rwi_list below.
2314 */
Nathan Fontenoted651a12017-05-03 14:04:38 -04002315 if (adapter->state == VNIC_REMOVING ||
Thomas Falcon5a18e1e2018-04-06 18:37:05 -05002316 adapter->state == VNIC_REMOVED ||
Sukadev Bhattiprolu1d850492020-10-30 10:07:11 -07002317 (adapter->failover_pending && reason != VNIC_RESET_FAILOVER)) {
Thomas Falconaf894d22018-04-06 18:37:04 -05002318 ret = EBUSY;
Thomas Falcon5a18e1e2018-04-06 18:37:05 -05002319 netdev_dbg(netdev, "Adapter removing or pending failover, skipping reset\n");
Thomas Falconaf894d22018-04-06 18:37:04 -05002320 goto err;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002321 }
2322
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04002323 if (adapter->state == VNIC_PROBING) {
2324 netdev_warn(netdev, "Adapter reset during probe\n");
Thomas Falconaf894d22018-04-06 18:37:04 -05002325 ret = adapter->init_done_rc = EAGAIN;
2326 goto err;
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04002327 }
2328
Thomas Falcon6c5c7482018-12-10 15:22:22 -06002329 spin_lock_irqsave(&adapter->rwi_lock, flags);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002330
2331 list_for_each(entry, &adapter->rwi_list) {
2332 tmp = list_entry(entry, struct ibmvnic_rwi, list);
2333 if (tmp->reset_reason == reason) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002334 netdev_dbg(netdev, "Skipping matching reset\n");
Thomas Falcon6c5c7482018-12-10 15:22:22 -06002335 spin_unlock_irqrestore(&adapter->rwi_lock, flags);
Thomas Falconaf894d22018-04-06 18:37:04 -05002336 ret = EBUSY;
2337 goto err;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002338 }
2339 }
2340
Thomas Falcon1d1bbc32018-12-10 15:22:23 -06002341 rwi = kzalloc(sizeof(*rwi), GFP_ATOMIC);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002342 if (!rwi) {
Thomas Falcon6c5c7482018-12-10 15:22:22 -06002343 spin_unlock_irqrestore(&adapter->rwi_lock, flags);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002344 ibmvnic_close(netdev);
Thomas Falconaf894d22018-04-06 18:37:04 -05002345 ret = ENOMEM;
2346 goto err;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002347 }
Thomas Falcon2770a792018-05-23 13:38:02 -05002348 /* if we just received a transport event,
2349 * flush reset queue and process this reset
2350 */
2351 if (adapter->force_reset_recovery && !list_empty(&adapter->rwi_list)) {
2352 list_for_each_safe(entry, tmp_entry, &adapter->rwi_list)
2353 list_del(entry);
2354 }
Nathan Fontenoted651a12017-05-03 14:04:38 -04002355 rwi->reset_reason = reason;
2356 list_add_tail(&rwi->list, &adapter->rwi_list);
Thomas Falcon6c5c7482018-12-10 15:22:22 -06002357 spin_unlock_irqrestore(&adapter->rwi_lock, flags);
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002358 netdev_dbg(adapter->netdev, "Scheduling reset (reason %d)\n", reason);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002359 schedule_work(&adapter->ibmvnic_reset);
Thomas Falconaf894d22018-04-06 18:37:04 -05002360
2361 return 0;
2362err:
Thomas Falconaf894d22018-04-06 18:37:04 -05002363 return -ret;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002364}
2365
Michael S. Tsirkin0290bd22019-12-10 09:23:51 -05002366static void ibmvnic_tx_timeout(struct net_device *dev, unsigned int txqueue)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002367{
2368 struct ibmvnic_adapter *adapter = netdev_priv(dev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002369
Nathan Fontenoted651a12017-05-03 14:04:38 -04002370 ibmvnic_reset(adapter, VNIC_RESET_TIMEOUT);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002371}
2372
2373static void remove_buff_from_pool(struct ibmvnic_adapter *adapter,
2374 struct ibmvnic_rx_buff *rx_buff)
2375{
2376 struct ibmvnic_rx_pool *pool = &adapter->rx_pool[rx_buff->pool_index];
2377
2378 rx_buff->skb = NULL;
2379
2380 pool->free_map[pool->next_alloc] = (int)(rx_buff - pool->rx_buff);
2381 pool->next_alloc = (pool->next_alloc + 1) % pool->size;
2382
2383 atomic_dec(&pool->available);
2384}
2385
2386static int ibmvnic_poll(struct napi_struct *napi, int budget)
2387{
2388 struct net_device *netdev = napi->dev;
2389 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2390 int scrq_num = (int)(napi - adapter->napi);
2391 int frames_processed = 0;
Nathan Fontenot152ce472017-05-26 10:30:54 -04002392
Thomas Falcon032c5e82015-12-21 11:26:06 -06002393restart_poll:
2394 while (frames_processed < budget) {
2395 struct sk_buff *skb;
2396 struct ibmvnic_rx_buff *rx_buff;
2397 union sub_crq *next;
2398 u32 length;
2399 u16 offset;
2400 u8 flags = 0;
2401
Juliet Kim7ed5b312019-09-20 16:11:23 -04002402 if (unlikely(test_bit(0, &adapter->resetting) &&
John Allen34686562018-02-06 16:21:49 -06002403 adapter->reset_reason != VNIC_RESET_NON_FATAL)) {
Thomas Falcon21ecba62017-06-14 23:50:09 -05002404 enable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
2405 napi_complete_done(napi, frames_processed);
2406 return frames_processed;
2407 }
2408
Thomas Falcon032c5e82015-12-21 11:26:06 -06002409 if (!pending_scrq(adapter, adapter->rx_scrq[scrq_num]))
2410 break;
2411 next = ibmvnic_next_scrq(adapter, adapter->rx_scrq[scrq_num]);
2412 rx_buff =
2413 (struct ibmvnic_rx_buff *)be64_to_cpu(next->
2414 rx_comp.correlator);
2415 /* do error checking */
2416 if (next->rx_comp.rc) {
John Allene1cea2e2017-08-07 15:42:30 -05002417 netdev_dbg(netdev, "rx buffer returned with rc %x\n",
2418 be16_to_cpu(next->rx_comp.rc));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002419 /* free the entry */
2420 next->rx_comp.first = 0;
Thomas Falcon4b9b0f02018-02-13 18:23:42 -06002421 dev_kfree_skb_any(rx_buff->skb);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002422 remove_buff_from_pool(adapter, rx_buff);
Nathan Fontenotca05e312017-05-03 14:05:14 -04002423 continue;
Thomas Falconabe27a82018-02-19 20:12:57 -06002424 } else if (!rx_buff->skb) {
2425 /* free the entry */
2426 next->rx_comp.first = 0;
2427 remove_buff_from_pool(adapter, rx_buff);
2428 continue;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002429 }
2430
2431 length = be32_to_cpu(next->rx_comp.len);
2432 offset = be16_to_cpu(next->rx_comp.off_frame_data);
2433 flags = next->rx_comp.flags;
2434 skb = rx_buff->skb;
2435 skb_copy_to_linear_data(skb, rx_buff->data + offset,
2436 length);
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04002437
2438 /* VLAN Header has been stripped by the system firmware and
2439 * needs to be inserted by the driver
2440 */
2441 if (adapter->rx_vlan_header_insertion &&
2442 (flags & IBMVNIC_VLAN_STRIPPED))
2443 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
2444 ntohs(next->rx_comp.vlan_tci));
2445
Thomas Falcon032c5e82015-12-21 11:26:06 -06002446 /* free the entry */
2447 next->rx_comp.first = 0;
2448 remove_buff_from_pool(adapter, rx_buff);
2449
2450 skb_put(skb, length);
2451 skb->protocol = eth_type_trans(skb, netdev);
Thomas Falcon94ca3052017-05-03 14:05:20 -04002452 skb_record_rx_queue(skb, scrq_num);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002453
2454 if (flags & IBMVNIC_IP_CHKSUM_GOOD &&
2455 flags & IBMVNIC_TCP_UDP_CHKSUM_GOOD) {
2456 skb->ip_summed = CHECKSUM_UNNECESSARY;
2457 }
2458
2459 length = skb->len;
2460 napi_gro_receive(napi, skb); /* send it up */
2461 netdev->stats.rx_packets++;
2462 netdev->stats.rx_bytes += length;
John Allen3d52b592017-08-02 16:44:14 -05002463 adapter->rx_stats_buffers[scrq_num].packets++;
2464 adapter->rx_stats_buffers[scrq_num].bytes += length;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002465 frames_processed++;
2466 }
Nathan Fontenot152ce472017-05-26 10:30:54 -04002467
2468 if (adapter->state != VNIC_CLOSING)
2469 replenish_rx_pool(adapter, &adapter->rx_pool[scrq_num]);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002470
2471 if (frames_processed < budget) {
2472 enable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
Eric Dumazet6ad20162017-01-30 08:22:01 -08002473 napi_complete_done(napi, frames_processed);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002474 if (pending_scrq(adapter, adapter->rx_scrq[scrq_num]) &&
2475 napi_reschedule(napi)) {
2476 disable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
2477 goto restart_poll;
2478 }
2479 }
2480 return frames_processed;
2481}
2482
John Allenc26eba02017-10-26 16:23:25 -05002483static int wait_for_reset(struct ibmvnic_adapter *adapter)
2484{
Thomas Falconaf894d22018-04-06 18:37:04 -05002485 int rc, ret;
2486
John Allenc26eba02017-10-26 16:23:25 -05002487 adapter->fallback.mtu = adapter->req_mtu;
2488 adapter->fallback.rx_queues = adapter->req_rx_queues;
2489 adapter->fallback.tx_queues = adapter->req_tx_queues;
2490 adapter->fallback.rx_entries = adapter->req_rx_add_entries_per_subcrq;
2491 adapter->fallback.tx_entries = adapter->req_tx_entries_per_subcrq;
2492
Thomas Falcon070eca92019-11-25 17:12:53 -06002493 reinit_completion(&adapter->reset_done);
John Allenc26eba02017-10-26 16:23:25 -05002494 adapter->wait_for_reset = true;
Thomas Falconaf894d22018-04-06 18:37:04 -05002495 rc = ibmvnic_reset(adapter, VNIC_RESET_CHANGE_PARAM);
Thomas Falcon476d96c2019-11-25 17:12:55 -06002496
2497 if (rc) {
2498 ret = rc;
2499 goto out;
2500 }
2501 rc = ibmvnic_wait_for_completion(adapter, &adapter->reset_done, 60000);
2502 if (rc) {
2503 ret = -ENODEV;
2504 goto out;
2505 }
John Allenc26eba02017-10-26 16:23:25 -05002506
Thomas Falconaf894d22018-04-06 18:37:04 -05002507 ret = 0;
John Allenc26eba02017-10-26 16:23:25 -05002508 if (adapter->reset_done_rc) {
Thomas Falconaf894d22018-04-06 18:37:04 -05002509 ret = -EIO;
John Allenc26eba02017-10-26 16:23:25 -05002510 adapter->desired.mtu = adapter->fallback.mtu;
2511 adapter->desired.rx_queues = adapter->fallback.rx_queues;
2512 adapter->desired.tx_queues = adapter->fallback.tx_queues;
2513 adapter->desired.rx_entries = adapter->fallback.rx_entries;
2514 adapter->desired.tx_entries = adapter->fallback.tx_entries;
2515
Thomas Falcon070eca92019-11-25 17:12:53 -06002516 reinit_completion(&adapter->reset_done);
Thomas Falconaf894d22018-04-06 18:37:04 -05002517 adapter->wait_for_reset = true;
2518 rc = ibmvnic_reset(adapter, VNIC_RESET_CHANGE_PARAM);
Thomas Falcon476d96c2019-11-25 17:12:55 -06002519 if (rc) {
2520 ret = rc;
2521 goto out;
2522 }
2523 rc = ibmvnic_wait_for_completion(adapter, &adapter->reset_done,
2524 60000);
2525 if (rc) {
2526 ret = -ENODEV;
2527 goto out;
2528 }
John Allenc26eba02017-10-26 16:23:25 -05002529 }
Thomas Falcon476d96c2019-11-25 17:12:55 -06002530out:
John Allenc26eba02017-10-26 16:23:25 -05002531 adapter->wait_for_reset = false;
2532
Thomas Falconaf894d22018-04-06 18:37:04 -05002533 return ret;
John Allenc26eba02017-10-26 16:23:25 -05002534}
2535
John Allen3a807b72017-06-06 16:55:52 -05002536static int ibmvnic_change_mtu(struct net_device *netdev, int new_mtu)
2537{
John Allenc26eba02017-10-26 16:23:25 -05002538 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2539
2540 adapter->desired.mtu = new_mtu + ETH_HLEN;
2541
2542 return wait_for_reset(adapter);
John Allen3a807b72017-06-06 16:55:52 -05002543}
2544
Thomas Falconf10b09e2018-03-12 11:51:05 -05002545static netdev_features_t ibmvnic_features_check(struct sk_buff *skb,
2546 struct net_device *dev,
2547 netdev_features_t features)
2548{
2549 /* Some backing hardware adapters can not
2550 * handle packets with a MSS less than 224
2551 * or with only one segment.
2552 */
2553 if (skb_is_gso(skb)) {
2554 if (skb_shinfo(skb)->gso_size < 224 ||
2555 skb_shinfo(skb)->gso_segs == 1)
2556 features &= ~NETIF_F_GSO_MASK;
2557 }
2558
2559 return features;
2560}
2561
Thomas Falcon032c5e82015-12-21 11:26:06 -06002562static const struct net_device_ops ibmvnic_netdev_ops = {
2563 .ndo_open = ibmvnic_open,
2564 .ndo_stop = ibmvnic_close,
2565 .ndo_start_xmit = ibmvnic_xmit,
2566 .ndo_set_rx_mode = ibmvnic_set_multi,
2567 .ndo_set_mac_address = ibmvnic_set_mac,
2568 .ndo_validate_addr = eth_validate_addr,
Thomas Falcon032c5e82015-12-21 11:26:06 -06002569 .ndo_tx_timeout = ibmvnic_tx_timeout,
John Allen3a807b72017-06-06 16:55:52 -05002570 .ndo_change_mtu = ibmvnic_change_mtu,
Thomas Falconf10b09e2018-03-12 11:51:05 -05002571 .ndo_features_check = ibmvnic_features_check,
Thomas Falcon032c5e82015-12-21 11:26:06 -06002572};
2573
2574/* ethtool functions */
2575
Philippe Reynes8a433792017-01-07 22:37:29 +01002576static int ibmvnic_get_link_ksettings(struct net_device *netdev,
2577 struct ethtool_link_ksettings *cmd)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002578{
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03002579 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2580 int rc;
Philippe Reynes8a433792017-01-07 22:37:29 +01002581
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03002582 rc = send_query_phys_parms(adapter);
2583 if (rc) {
2584 adapter->speed = SPEED_UNKNOWN;
2585 adapter->duplex = DUPLEX_UNKNOWN;
2586 }
2587 cmd->base.speed = adapter->speed;
2588 cmd->base.duplex = adapter->duplex;
Philippe Reynes8a433792017-01-07 22:37:29 +01002589 cmd->base.port = PORT_FIBRE;
2590 cmd->base.phy_address = 0;
2591 cmd->base.autoneg = AUTONEG_ENABLE;
2592
Thomas Falcon032c5e82015-12-21 11:26:06 -06002593 return 0;
2594}
2595
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02002596static void ibmvnic_get_drvinfo(struct net_device *netdev,
Thomas Falcon032c5e82015-12-21 11:26:06 -06002597 struct ethtool_drvinfo *info)
2598{
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02002599 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2600
Thomas Falcon032c5e82015-12-21 11:26:06 -06002601 strlcpy(info->driver, ibmvnic_driver_name, sizeof(info->driver));
2602 strlcpy(info->version, IBMVNIC_DRIVER_VERSION, sizeof(info->version));
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02002603 strlcpy(info->fw_version, adapter->fw_version,
2604 sizeof(info->fw_version));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002605}
2606
2607static u32 ibmvnic_get_msglevel(struct net_device *netdev)
2608{
2609 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2610
2611 return adapter->msg_enable;
2612}
2613
2614static void ibmvnic_set_msglevel(struct net_device *netdev, u32 data)
2615{
2616 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2617
2618 adapter->msg_enable = data;
2619}
2620
2621static u32 ibmvnic_get_link(struct net_device *netdev)
2622{
2623 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2624
2625 /* Don't need to send a query because we request a logical link up at
2626 * init and then we wait for link state indications
2627 */
2628 return adapter->logical_link_state;
2629}
2630
2631static void ibmvnic_get_ringparam(struct net_device *netdev,
2632 struct ethtool_ringparam *ring)
2633{
John Allenbc131b32017-08-02 16:46:30 -05002634 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2635
Thomas Falcon723ad912018-09-28 18:38:26 -05002636 if (adapter->priv_flags & IBMVNIC_USE_SERVER_MAXES) {
2637 ring->rx_max_pending = adapter->max_rx_add_entries_per_subcrq;
2638 ring->tx_max_pending = adapter->max_tx_entries_per_subcrq;
2639 } else {
2640 ring->rx_max_pending = IBMVNIC_MAX_QUEUE_SZ;
2641 ring->tx_max_pending = IBMVNIC_MAX_QUEUE_SZ;
2642 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002643 ring->rx_mini_max_pending = 0;
2644 ring->rx_jumbo_max_pending = 0;
John Allenbc131b32017-08-02 16:46:30 -05002645 ring->rx_pending = adapter->req_rx_add_entries_per_subcrq;
2646 ring->tx_pending = adapter->req_tx_entries_per_subcrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002647 ring->rx_mini_pending = 0;
2648 ring->rx_jumbo_pending = 0;
2649}
2650
John Allenc26eba02017-10-26 16:23:25 -05002651static int ibmvnic_set_ringparam(struct net_device *netdev,
2652 struct ethtool_ringparam *ring)
2653{
2654 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Thomas Falcon723ad912018-09-28 18:38:26 -05002655 int ret;
John Allenc26eba02017-10-26 16:23:25 -05002656
Thomas Falcon723ad912018-09-28 18:38:26 -05002657 ret = 0;
John Allenc26eba02017-10-26 16:23:25 -05002658 adapter->desired.rx_entries = ring->rx_pending;
2659 adapter->desired.tx_entries = ring->tx_pending;
2660
Thomas Falcon723ad912018-09-28 18:38:26 -05002661 ret = wait_for_reset(adapter);
2662
2663 if (!ret &&
2664 (adapter->req_rx_add_entries_per_subcrq != ring->rx_pending ||
2665 adapter->req_tx_entries_per_subcrq != ring->tx_pending))
2666 netdev_info(netdev,
2667 "Could not match full ringsize request. Requested: RX %d, TX %d; Allowed: RX %llu, TX %llu\n",
2668 ring->rx_pending, ring->tx_pending,
2669 adapter->req_rx_add_entries_per_subcrq,
2670 adapter->req_tx_entries_per_subcrq);
2671 return ret;
John Allenc26eba02017-10-26 16:23:25 -05002672}
2673
John Allenc2dbeb62017-08-02 16:47:17 -05002674static void ibmvnic_get_channels(struct net_device *netdev,
2675 struct ethtool_channels *channels)
2676{
2677 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2678
Thomas Falcon723ad912018-09-28 18:38:26 -05002679 if (adapter->priv_flags & IBMVNIC_USE_SERVER_MAXES) {
2680 channels->max_rx = adapter->max_rx_queues;
2681 channels->max_tx = adapter->max_tx_queues;
2682 } else {
2683 channels->max_rx = IBMVNIC_MAX_QUEUES;
2684 channels->max_tx = IBMVNIC_MAX_QUEUES;
2685 }
2686
John Allenc2dbeb62017-08-02 16:47:17 -05002687 channels->max_other = 0;
2688 channels->max_combined = 0;
2689 channels->rx_count = adapter->req_rx_queues;
2690 channels->tx_count = adapter->req_tx_queues;
2691 channels->other_count = 0;
2692 channels->combined_count = 0;
2693}
2694
John Allenc26eba02017-10-26 16:23:25 -05002695static int ibmvnic_set_channels(struct net_device *netdev,
2696 struct ethtool_channels *channels)
2697{
2698 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Thomas Falcon723ad912018-09-28 18:38:26 -05002699 int ret;
John Allenc26eba02017-10-26 16:23:25 -05002700
Thomas Falcon723ad912018-09-28 18:38:26 -05002701 ret = 0;
John Allenc26eba02017-10-26 16:23:25 -05002702 adapter->desired.rx_queues = channels->rx_count;
2703 adapter->desired.tx_queues = channels->tx_count;
2704
Thomas Falcon723ad912018-09-28 18:38:26 -05002705 ret = wait_for_reset(adapter);
2706
2707 if (!ret &&
2708 (adapter->req_rx_queues != channels->rx_count ||
2709 adapter->req_tx_queues != channels->tx_count))
2710 netdev_info(netdev,
2711 "Could not match full channels request. Requested: RX %d, TX %d; Allowed: RX %llu, TX %llu\n",
2712 channels->rx_count, channels->tx_count,
2713 adapter->req_rx_queues, adapter->req_tx_queues);
2714 return ret;
2715
John Allenc26eba02017-10-26 16:23:25 -05002716}
2717
Thomas Falcon032c5e82015-12-21 11:26:06 -06002718static void ibmvnic_get_strings(struct net_device *dev, u32 stringset, u8 *data)
2719{
John Allen3d52b592017-08-02 16:44:14 -05002720 struct ibmvnic_adapter *adapter = netdev_priv(dev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002721 int i;
2722
Thomas Falcon723ad912018-09-28 18:38:26 -05002723 switch (stringset) {
2724 case ETH_SS_STATS:
2725 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats);
2726 i++, data += ETH_GSTRING_LEN)
2727 memcpy(data, ibmvnic_stats[i].name, ETH_GSTRING_LEN);
2728
2729 for (i = 0; i < adapter->req_tx_queues; i++) {
2730 snprintf(data, ETH_GSTRING_LEN, "tx%d_packets", i);
2731 data += ETH_GSTRING_LEN;
2732
2733 snprintf(data, ETH_GSTRING_LEN, "tx%d_bytes", i);
2734 data += ETH_GSTRING_LEN;
2735
2736 snprintf(data, ETH_GSTRING_LEN,
2737 "tx%d_dropped_packets", i);
2738 data += ETH_GSTRING_LEN;
2739 }
2740
2741 for (i = 0; i < adapter->req_rx_queues; i++) {
2742 snprintf(data, ETH_GSTRING_LEN, "rx%d_packets", i);
2743 data += ETH_GSTRING_LEN;
2744
2745 snprintf(data, ETH_GSTRING_LEN, "rx%d_bytes", i);
2746 data += ETH_GSTRING_LEN;
2747
2748 snprintf(data, ETH_GSTRING_LEN, "rx%d_interrupts", i);
2749 data += ETH_GSTRING_LEN;
2750 }
2751 break;
2752
2753 case ETH_SS_PRIV_FLAGS:
2754 for (i = 0; i < ARRAY_SIZE(ibmvnic_priv_flags); i++)
2755 strcpy(data + i * ETH_GSTRING_LEN,
2756 ibmvnic_priv_flags[i]);
2757 break;
2758 default:
Thomas Falcon032c5e82015-12-21 11:26:06 -06002759 return;
John Allen3d52b592017-08-02 16:44:14 -05002760 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002761}
2762
2763static int ibmvnic_get_sset_count(struct net_device *dev, int sset)
2764{
John Allen3d52b592017-08-02 16:44:14 -05002765 struct ibmvnic_adapter *adapter = netdev_priv(dev);
2766
Thomas Falcon032c5e82015-12-21 11:26:06 -06002767 switch (sset) {
2768 case ETH_SS_STATS:
John Allen3d52b592017-08-02 16:44:14 -05002769 return ARRAY_SIZE(ibmvnic_stats) +
2770 adapter->req_tx_queues * NUM_TX_STATS +
2771 adapter->req_rx_queues * NUM_RX_STATS;
Thomas Falcon723ad912018-09-28 18:38:26 -05002772 case ETH_SS_PRIV_FLAGS:
2773 return ARRAY_SIZE(ibmvnic_priv_flags);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002774 default:
2775 return -EOPNOTSUPP;
2776 }
2777}
2778
2779static void ibmvnic_get_ethtool_stats(struct net_device *dev,
2780 struct ethtool_stats *stats, u64 *data)
2781{
2782 struct ibmvnic_adapter *adapter = netdev_priv(dev);
2783 union ibmvnic_crq crq;
John Allen3d52b592017-08-02 16:44:14 -05002784 int i, j;
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05002785 int rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002786
2787 memset(&crq, 0, sizeof(crq));
2788 crq.request_statistics.first = IBMVNIC_CRQ_CMD;
2789 crq.request_statistics.cmd = REQUEST_STATISTICS;
2790 crq.request_statistics.ioba = cpu_to_be32(adapter->stats_token);
2791 crq.request_statistics.len =
2792 cpu_to_be32(sizeof(struct ibmvnic_statistics));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002793
2794 /* Wait for data to be written */
Thomas Falcon070eca92019-11-25 17:12:53 -06002795 reinit_completion(&adapter->stats_done);
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05002796 rc = ibmvnic_send_crq(adapter, &crq);
2797 if (rc)
2798 return;
Thomas Falcon476d96c2019-11-25 17:12:55 -06002799 rc = ibmvnic_wait_for_completion(adapter, &adapter->stats_done, 10000);
2800 if (rc)
2801 return;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002802
2803 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++)
John Allen52da5c12017-08-02 16:45:28 -05002804 data[i] = be64_to_cpu(IBMVNIC_GET_STAT(adapter,
2805 ibmvnic_stats[i].offset));
John Allen3d52b592017-08-02 16:44:14 -05002806
2807 for (j = 0; j < adapter->req_tx_queues; j++) {
2808 data[i] = adapter->tx_stats_buffers[j].packets;
2809 i++;
2810 data[i] = adapter->tx_stats_buffers[j].bytes;
2811 i++;
2812 data[i] = adapter->tx_stats_buffers[j].dropped_packets;
2813 i++;
2814 }
2815
2816 for (j = 0; j < adapter->req_rx_queues; j++) {
2817 data[i] = adapter->rx_stats_buffers[j].packets;
2818 i++;
2819 data[i] = adapter->rx_stats_buffers[j].bytes;
2820 i++;
2821 data[i] = adapter->rx_stats_buffers[j].interrupts;
2822 i++;
2823 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002824}
2825
Thomas Falcon723ad912018-09-28 18:38:26 -05002826static u32 ibmvnic_get_priv_flags(struct net_device *netdev)
2827{
2828 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2829
2830 return adapter->priv_flags;
2831}
2832
2833static int ibmvnic_set_priv_flags(struct net_device *netdev, u32 flags)
2834{
2835 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2836 bool which_maxes = !!(flags & IBMVNIC_USE_SERVER_MAXES);
2837
2838 if (which_maxes)
2839 adapter->priv_flags |= IBMVNIC_USE_SERVER_MAXES;
2840 else
2841 adapter->priv_flags &= ~IBMVNIC_USE_SERVER_MAXES;
2842
2843 return 0;
2844}
Thomas Falcon032c5e82015-12-21 11:26:06 -06002845static const struct ethtool_ops ibmvnic_ethtool_ops = {
Thomas Falcon032c5e82015-12-21 11:26:06 -06002846 .get_drvinfo = ibmvnic_get_drvinfo,
2847 .get_msglevel = ibmvnic_get_msglevel,
2848 .set_msglevel = ibmvnic_set_msglevel,
2849 .get_link = ibmvnic_get_link,
2850 .get_ringparam = ibmvnic_get_ringparam,
John Allenc26eba02017-10-26 16:23:25 -05002851 .set_ringparam = ibmvnic_set_ringparam,
John Allenc2dbeb62017-08-02 16:47:17 -05002852 .get_channels = ibmvnic_get_channels,
John Allenc26eba02017-10-26 16:23:25 -05002853 .set_channels = ibmvnic_set_channels,
Thomas Falcon032c5e82015-12-21 11:26:06 -06002854 .get_strings = ibmvnic_get_strings,
2855 .get_sset_count = ibmvnic_get_sset_count,
2856 .get_ethtool_stats = ibmvnic_get_ethtool_stats,
Philippe Reynes8a433792017-01-07 22:37:29 +01002857 .get_link_ksettings = ibmvnic_get_link_ksettings,
Thomas Falcon723ad912018-09-28 18:38:26 -05002858 .get_priv_flags = ibmvnic_get_priv_flags,
2859 .set_priv_flags = ibmvnic_set_priv_flags,
Thomas Falcon032c5e82015-12-21 11:26:06 -06002860};
2861
2862/* Routines for managing CRQs/sCRQs */
2863
Nathan Fontenot57a49432017-05-26 10:31:12 -04002864static int reset_one_sub_crq_queue(struct ibmvnic_adapter *adapter,
2865 struct ibmvnic_sub_crq_queue *scrq)
2866{
2867 int rc;
2868
2869 if (scrq->irq) {
2870 free_irq(scrq->irq, scrq);
2871 irq_dispose_mapping(scrq->irq);
2872 scrq->irq = 0;
2873 }
2874
Thomas Falconc8b2ad02017-06-14 23:50:07 -05002875 memset(scrq->msgs, 0, 4 * PAGE_SIZE);
Thomas Falcon41f71462018-04-06 18:37:03 -05002876 atomic_set(&scrq->used, 0);
Nathan Fontenot57a49432017-05-26 10:31:12 -04002877 scrq->cur = 0;
Thomas Falconf019fb62020-11-18 19:12:17 -06002878 scrq->ind_buf.index = 0;
Nathan Fontenot57a49432017-05-26 10:31:12 -04002879
2880 rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
2881 4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
2882 return rc;
2883}
2884
2885static int reset_sub_crq_queues(struct ibmvnic_adapter *adapter)
2886{
2887 int i, rc;
2888
2889 for (i = 0; i < adapter->req_tx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002890 netdev_dbg(adapter->netdev, "Re-setting tx_scrq[%d]\n", i);
Nathan Fontenot57a49432017-05-26 10:31:12 -04002891 rc = reset_one_sub_crq_queue(adapter, adapter->tx_scrq[i]);
2892 if (rc)
2893 return rc;
2894 }
2895
2896 for (i = 0; i < adapter->req_rx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002897 netdev_dbg(adapter->netdev, "Re-setting rx_scrq[%d]\n", i);
Nathan Fontenot57a49432017-05-26 10:31:12 -04002898 rc = reset_one_sub_crq_queue(adapter, adapter->rx_scrq[i]);
2899 if (rc)
2900 return rc;
2901 }
2902
Nathan Fontenot57a49432017-05-26 10:31:12 -04002903 return rc;
2904}
2905
Thomas Falcon032c5e82015-12-21 11:26:06 -06002906static void release_sub_crq_queue(struct ibmvnic_adapter *adapter,
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06002907 struct ibmvnic_sub_crq_queue *scrq,
2908 bool do_h_free)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002909{
2910 struct device *dev = &adapter->vdev->dev;
2911 long rc;
2912
2913 netdev_dbg(adapter->netdev, "Releasing sub-CRQ\n");
2914
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06002915 if (do_h_free) {
2916 /* Close the sub-crqs */
2917 do {
2918 rc = plpar_hcall_norets(H_FREE_SUB_CRQ,
2919 adapter->vdev->unit_address,
2920 scrq->crq_num);
2921 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002922
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06002923 if (rc) {
2924 netdev_err(adapter->netdev,
2925 "Failed to release sub-CRQ %16lx, rc = %ld\n",
2926 scrq->crq_num, rc);
2927 }
Thomas Falconffa73852017-04-19 13:44:29 -04002928 }
2929
Thomas Falconf019fb62020-11-18 19:12:17 -06002930 dma_free_coherent(dev,
2931 IBMVNIC_IND_ARR_SZ,
2932 scrq->ind_buf.indir_arr,
2933 scrq->ind_buf.indir_dma);
2934
Thomas Falcon032c5e82015-12-21 11:26:06 -06002935 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
2936 DMA_BIDIRECTIONAL);
2937 free_pages((unsigned long)scrq->msgs, 2);
2938 kfree(scrq);
2939}
2940
2941static struct ibmvnic_sub_crq_queue *init_sub_crq_queue(struct ibmvnic_adapter
2942 *adapter)
2943{
2944 struct device *dev = &adapter->vdev->dev;
2945 struct ibmvnic_sub_crq_queue *scrq;
2946 int rc;
2947
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04002948 scrq = kzalloc(sizeof(*scrq), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002949 if (!scrq)
2950 return NULL;
2951
Nathan Fontenot7f7adc52017-04-19 13:45:16 -04002952 scrq->msgs =
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04002953 (union sub_crq *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, 2);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002954 if (!scrq->msgs) {
2955 dev_warn(dev, "Couldn't allocate crq queue messages page\n");
2956 goto zero_page_failed;
2957 }
2958
2959 scrq->msg_token = dma_map_single(dev, scrq->msgs, 4 * PAGE_SIZE,
2960 DMA_BIDIRECTIONAL);
2961 if (dma_mapping_error(dev, scrq->msg_token)) {
2962 dev_warn(dev, "Couldn't map crq queue messages page\n");
2963 goto map_failed;
2964 }
2965
2966 rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
2967 4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
2968
2969 if (rc == H_RESOURCE)
2970 rc = ibmvnic_reset_crq(adapter);
2971
2972 if (rc == H_CLOSED) {
2973 dev_warn(dev, "Partner adapter not ready, waiting.\n");
2974 } else if (rc) {
2975 dev_warn(dev, "Error %d registering sub-crq\n", rc);
2976 goto reg_failed;
2977 }
2978
Thomas Falcon032c5e82015-12-21 11:26:06 -06002979 scrq->adapter = adapter;
2980 scrq->size = 4 * PAGE_SIZE / sizeof(*scrq->msgs);
Thomas Falconf019fb62020-11-18 19:12:17 -06002981 scrq->ind_buf.index = 0;
2982
2983 scrq->ind_buf.indir_arr =
2984 dma_alloc_coherent(dev,
2985 IBMVNIC_IND_ARR_SZ,
2986 &scrq->ind_buf.indir_dma,
2987 GFP_KERNEL);
2988
2989 if (!scrq->ind_buf.indir_arr)
2990 goto indir_failed;
2991
Thomas Falcon032c5e82015-12-21 11:26:06 -06002992 spin_lock_init(&scrq->lock);
2993
2994 netdev_dbg(adapter->netdev,
2995 "sub-crq initialized, num %lx, hw_irq=%lx, irq=%x\n",
2996 scrq->crq_num, scrq->hw_irq, scrq->irq);
2997
2998 return scrq;
2999
Thomas Falconf019fb62020-11-18 19:12:17 -06003000indir_failed:
3001 do {
3002 rc = plpar_hcall_norets(H_FREE_SUB_CRQ,
3003 adapter->vdev->unit_address,
3004 scrq->crq_num);
3005 } while (rc == H_BUSY || rc == H_IS_LONG_BUSY(rc));
Thomas Falcon032c5e82015-12-21 11:26:06 -06003006reg_failed:
3007 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
3008 DMA_BIDIRECTIONAL);
3009map_failed:
3010 free_pages((unsigned long)scrq->msgs, 2);
3011zero_page_failed:
3012 kfree(scrq);
3013
3014 return NULL;
3015}
3016
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06003017static void release_sub_crqs(struct ibmvnic_adapter *adapter, bool do_h_free)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003018{
3019 int i;
3020
3021 if (adapter->tx_scrq) {
Nathan Fontenot82e3be32018-02-21 21:33:56 -06003022 for (i = 0; i < adapter->num_active_tx_scrqs; i++) {
Nathan Fontenotb5108882017-03-30 02:49:18 -04003023 if (!adapter->tx_scrq[i])
3024 continue;
3025
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05003026 netdev_dbg(adapter->netdev, "Releasing tx_scrq[%d]\n",
3027 i);
Nathan Fontenotb5108882017-03-30 02:49:18 -04003028 if (adapter->tx_scrq[i]->irq) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06003029 free_irq(adapter->tx_scrq[i]->irq,
3030 adapter->tx_scrq[i]);
Thomas Falcon88eb98a2016-07-06 15:35:16 -05003031 irq_dispose_mapping(adapter->tx_scrq[i]->irq);
Nathan Fontenotb5108882017-03-30 02:49:18 -04003032 adapter->tx_scrq[i]->irq = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003033 }
Nathan Fontenotb5108882017-03-30 02:49:18 -04003034
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06003035 release_sub_crq_queue(adapter, adapter->tx_scrq[i],
3036 do_h_free);
Nathan Fontenotb5108882017-03-30 02:49:18 -04003037 }
3038
Nathan Fontenot9501df32017-03-15 23:38:07 -04003039 kfree(adapter->tx_scrq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003040 adapter->tx_scrq = NULL;
Nathan Fontenot82e3be32018-02-21 21:33:56 -06003041 adapter->num_active_tx_scrqs = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003042 }
3043
3044 if (adapter->rx_scrq) {
Nathan Fontenot82e3be32018-02-21 21:33:56 -06003045 for (i = 0; i < adapter->num_active_rx_scrqs; i++) {
Nathan Fontenotb5108882017-03-30 02:49:18 -04003046 if (!adapter->rx_scrq[i])
3047 continue;
3048
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05003049 netdev_dbg(adapter->netdev, "Releasing rx_scrq[%d]\n",
3050 i);
Nathan Fontenotb5108882017-03-30 02:49:18 -04003051 if (adapter->rx_scrq[i]->irq) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06003052 free_irq(adapter->rx_scrq[i]->irq,
3053 adapter->rx_scrq[i]);
Thomas Falcon88eb98a2016-07-06 15:35:16 -05003054 irq_dispose_mapping(adapter->rx_scrq[i]->irq);
Nathan Fontenotb5108882017-03-30 02:49:18 -04003055 adapter->rx_scrq[i]->irq = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003056 }
Nathan Fontenotb5108882017-03-30 02:49:18 -04003057
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06003058 release_sub_crq_queue(adapter, adapter->rx_scrq[i],
3059 do_h_free);
Nathan Fontenotb5108882017-03-30 02:49:18 -04003060 }
3061
Nathan Fontenot9501df32017-03-15 23:38:07 -04003062 kfree(adapter->rx_scrq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003063 adapter->rx_scrq = NULL;
Nathan Fontenot82e3be32018-02-21 21:33:56 -06003064 adapter->num_active_rx_scrqs = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003065 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06003066}
3067
3068static int disable_scrq_irq(struct ibmvnic_adapter *adapter,
3069 struct ibmvnic_sub_crq_queue *scrq)
3070{
3071 struct device *dev = &adapter->vdev->dev;
3072 unsigned long rc;
3073
3074 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
3075 H_DISABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
3076 if (rc)
3077 dev_err(dev, "Couldn't disable scrq irq 0x%lx. rc=%ld\n",
3078 scrq->hw_irq, rc);
3079 return rc;
3080}
3081
3082static int enable_scrq_irq(struct ibmvnic_adapter *adapter,
3083 struct ibmvnic_sub_crq_queue *scrq)
3084{
3085 struct device *dev = &adapter->vdev->dev;
3086 unsigned long rc;
3087
3088 if (scrq->hw_irq > 0x100000000ULL) {
3089 dev_err(dev, "bad hw_irq = %lx\n", scrq->hw_irq);
3090 return 1;
3091 }
3092
Juliet Kim7ed5b312019-09-20 16:11:23 -04003093 if (test_bit(0, &adapter->resetting) &&
Nathan Fontenot73f9d362018-05-22 11:21:10 -05003094 adapter->reset_reason == VNIC_RESET_MOBILITY) {
Juliet Kim284f87d2019-11-20 10:50:03 -05003095 u64 val = (0xff000000) | scrq->hw_irq;
Nathan Fontenot73f9d362018-05-22 11:21:10 -05003096
Juliet Kim284f87d2019-11-20 10:50:03 -05003097 rc = plpar_hcall_norets(H_EOI, val);
Juliet Kim2df5c602019-11-20 10:50:04 -05003098 /* H_EOI would fail with rc = H_FUNCTION when running
3099 * in XIVE mode which is expected, but not an error.
3100 */
3101 if (rc && (rc != H_FUNCTION))
Juliet Kim284f87d2019-11-20 10:50:03 -05003102 dev_err(dev, "H_EOI FAILED irq 0x%llx. rc=%ld\n",
3103 val, rc);
Nathan Fontenot73f9d362018-05-22 11:21:10 -05003104 }
Thomas Falconf23e0642018-04-15 18:53:36 -05003105
Thomas Falcon032c5e82015-12-21 11:26:06 -06003106 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
3107 H_ENABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
3108 if (rc)
3109 dev_err(dev, "Couldn't enable scrq irq 0x%lx. rc=%ld\n",
3110 scrq->hw_irq, rc);
3111 return rc;
3112}
3113
3114static int ibmvnic_complete_tx(struct ibmvnic_adapter *adapter,
3115 struct ibmvnic_sub_crq_queue *scrq)
3116{
3117 struct device *dev = &adapter->vdev->dev;
Thomas Falcon06b3e352018-03-16 20:00:28 -05003118 struct ibmvnic_tx_pool *tx_pool;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003119 struct ibmvnic_tx_buff *txbuff;
3120 union sub_crq *next;
3121 int index;
3122 int i, j;
3123
3124restart_loop:
3125 while (pending_scrq(adapter, scrq)) {
3126 unsigned int pool = scrq->pool_index;
Thomas Falconffc385b2018-02-18 10:08:41 -06003127 int num_entries = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003128
3129 next = ibmvnic_next_scrq(adapter, scrq);
3130 for (i = 0; i < next->tx_comp.num_comps; i++) {
3131 if (next->tx_comp.rcs[i]) {
3132 dev_err(dev, "tx error %x\n",
3133 next->tx_comp.rcs[i]);
3134 continue;
3135 }
3136 index = be32_to_cpu(next->tx_comp.correlators[i]);
Thomas Falcon06b3e352018-03-16 20:00:28 -05003137 if (index & IBMVNIC_TSO_POOL_MASK) {
3138 tx_pool = &adapter->tso_pool[pool];
3139 index &= ~IBMVNIC_TSO_POOL_MASK;
3140 } else {
3141 tx_pool = &adapter->tx_pool[pool];
3142 }
3143
3144 txbuff = &tx_pool->tx_buff[index];
Thomas Falcon032c5e82015-12-21 11:26:06 -06003145
3146 for (j = 0; j < IBMVNIC_MAX_FRAGS_PER_CRQ; j++) {
3147 if (!txbuff->data_dma[j])
3148 continue;
3149
3150 txbuff->data_dma[j] = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003151 }
3152
Thomas Falcon142c0ac2017-03-05 12:18:41 -06003153 if (txbuff->last_frag) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06003154 dev_kfree_skb_any(txbuff->skb);
Nathan Fontenot7c3e7de2017-05-03 14:05:25 -04003155 txbuff->skb = NULL;
Thomas Falcon142c0ac2017-03-05 12:18:41 -06003156 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06003157
Thomas Falconffc385b2018-02-18 10:08:41 -06003158 num_entries += txbuff->num_entries;
3159
Thomas Falcon06b3e352018-03-16 20:00:28 -05003160 tx_pool->free_map[tx_pool->producer_index] = index;
3161 tx_pool->producer_index =
3162 (tx_pool->producer_index + 1) %
3163 tx_pool->num_buffers;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003164 }
3165 /* remove tx_comp scrq*/
3166 next->tx_comp.first = 0;
Nathan Fontenot7c3e7de2017-05-03 14:05:25 -04003167
Thomas Falconffc385b2018-02-18 10:08:41 -06003168 if (atomic_sub_return(num_entries, &scrq->used) <=
Nathan Fontenot7c3e7de2017-05-03 14:05:25 -04003169 (adapter->req_tx_entries_per_subcrq / 2) &&
3170 __netif_subqueue_stopped(adapter->netdev,
3171 scrq->pool_index)) {
3172 netif_wake_subqueue(adapter->netdev, scrq->pool_index);
Thomas Falcon0aecb132018-02-26 18:10:58 -06003173 netdev_dbg(adapter->netdev, "Started queue %d\n",
3174 scrq->pool_index);
Nathan Fontenot7c3e7de2017-05-03 14:05:25 -04003175 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06003176 }
3177
3178 enable_scrq_irq(adapter, scrq);
3179
3180 if (pending_scrq(adapter, scrq)) {
3181 disable_scrq_irq(adapter, scrq);
3182 goto restart_loop;
3183 }
3184
3185 return 0;
3186}
3187
3188static irqreturn_t ibmvnic_interrupt_tx(int irq, void *instance)
3189{
3190 struct ibmvnic_sub_crq_queue *scrq = instance;
3191 struct ibmvnic_adapter *adapter = scrq->adapter;
3192
3193 disable_scrq_irq(adapter, scrq);
3194 ibmvnic_complete_tx(adapter, scrq);
3195
3196 return IRQ_HANDLED;
3197}
3198
3199static irqreturn_t ibmvnic_interrupt_rx(int irq, void *instance)
3200{
3201 struct ibmvnic_sub_crq_queue *scrq = instance;
3202 struct ibmvnic_adapter *adapter = scrq->adapter;
3203
Nathan Fontenot09fb35e2018-01-10 10:40:09 -06003204 /* When booting a kdump kernel we can hit pending interrupts
3205 * prior to completing driver initialization.
3206 */
3207 if (unlikely(adapter->state != VNIC_OPEN))
3208 return IRQ_NONE;
3209
John Allen3d52b592017-08-02 16:44:14 -05003210 adapter->rx_stats_buffers[scrq->scrq_num].interrupts++;
3211
Thomas Falcon032c5e82015-12-21 11:26:06 -06003212 if (napi_schedule_prep(&adapter->napi[scrq->scrq_num])) {
3213 disable_scrq_irq(adapter, scrq);
3214 __napi_schedule(&adapter->napi[scrq->scrq_num]);
3215 }
3216
3217 return IRQ_HANDLED;
3218}
3219
Thomas Falconea22d512016-07-06 15:35:17 -05003220static int init_sub_crq_irqs(struct ibmvnic_adapter *adapter)
3221{
3222 struct device *dev = &adapter->vdev->dev;
3223 struct ibmvnic_sub_crq_queue *scrq;
3224 int i = 0, j = 0;
3225 int rc = 0;
3226
3227 for (i = 0; i < adapter->req_tx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05003228 netdev_dbg(adapter->netdev, "Initializing tx_scrq[%d] irq\n",
3229 i);
Thomas Falconea22d512016-07-06 15:35:17 -05003230 scrq = adapter->tx_scrq[i];
3231 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
3232
Michael Ellerman99c17902016-09-10 19:59:05 +10003233 if (!scrq->irq) {
Thomas Falconea22d512016-07-06 15:35:17 -05003234 rc = -EINVAL;
3235 dev_err(dev, "Error mapping irq\n");
3236 goto req_tx_irq_failed;
3237 }
3238
Murilo Fossa Vicentinie56e2512019-04-25 11:02:33 -03003239 snprintf(scrq->name, sizeof(scrq->name), "ibmvnic-%x-tx%d",
3240 adapter->vdev->unit_address, i);
Thomas Falconea22d512016-07-06 15:35:17 -05003241 rc = request_irq(scrq->irq, ibmvnic_interrupt_tx,
Murilo Fossa Vicentinie56e2512019-04-25 11:02:33 -03003242 0, scrq->name, scrq);
Thomas Falconea22d512016-07-06 15:35:17 -05003243
3244 if (rc) {
3245 dev_err(dev, "Couldn't register tx irq 0x%x. rc=%d\n",
3246 scrq->irq, rc);
3247 irq_dispose_mapping(scrq->irq);
Nathan Fontenotaf9090c2018-02-20 11:04:18 -06003248 goto req_tx_irq_failed;
Thomas Falconea22d512016-07-06 15:35:17 -05003249 }
3250 }
3251
3252 for (i = 0; i < adapter->req_rx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05003253 netdev_dbg(adapter->netdev, "Initializing rx_scrq[%d] irq\n",
3254 i);
Thomas Falconea22d512016-07-06 15:35:17 -05003255 scrq = adapter->rx_scrq[i];
3256 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
Michael Ellerman99c17902016-09-10 19:59:05 +10003257 if (!scrq->irq) {
Thomas Falconea22d512016-07-06 15:35:17 -05003258 rc = -EINVAL;
3259 dev_err(dev, "Error mapping irq\n");
3260 goto req_rx_irq_failed;
3261 }
Murilo Fossa Vicentinie56e2512019-04-25 11:02:33 -03003262 snprintf(scrq->name, sizeof(scrq->name), "ibmvnic-%x-rx%d",
3263 adapter->vdev->unit_address, i);
Thomas Falconea22d512016-07-06 15:35:17 -05003264 rc = request_irq(scrq->irq, ibmvnic_interrupt_rx,
Murilo Fossa Vicentinie56e2512019-04-25 11:02:33 -03003265 0, scrq->name, scrq);
Thomas Falconea22d512016-07-06 15:35:17 -05003266 if (rc) {
3267 dev_err(dev, "Couldn't register rx irq 0x%x. rc=%d\n",
3268 scrq->irq, rc);
3269 irq_dispose_mapping(scrq->irq);
3270 goto req_rx_irq_failed;
3271 }
3272 }
3273 return rc;
3274
3275req_rx_irq_failed:
Thomas Falcon8bf371e2016-10-27 12:28:52 -05003276 for (j = 0; j < i; j++) {
Thomas Falconea22d512016-07-06 15:35:17 -05003277 free_irq(adapter->rx_scrq[j]->irq, adapter->rx_scrq[j]);
3278 irq_dispose_mapping(adapter->rx_scrq[j]->irq);
Thomas Falcon8bf371e2016-10-27 12:28:52 -05003279 }
Thomas Falconea22d512016-07-06 15:35:17 -05003280 i = adapter->req_tx_queues;
3281req_tx_irq_failed:
Thomas Falcon8bf371e2016-10-27 12:28:52 -05003282 for (j = 0; j < i; j++) {
Thomas Falconea22d512016-07-06 15:35:17 -05003283 free_irq(adapter->tx_scrq[j]->irq, adapter->tx_scrq[j]);
Thomas Falcon27a21452020-07-29 16:36:32 -05003284 irq_dispose_mapping(adapter->tx_scrq[j]->irq);
Thomas Falcon8bf371e2016-10-27 12:28:52 -05003285 }
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06003286 release_sub_crqs(adapter, 1);
Thomas Falconea22d512016-07-06 15:35:17 -05003287 return rc;
3288}
3289
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003290static int init_sub_crqs(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003291{
3292 struct device *dev = &adapter->vdev->dev;
3293 struct ibmvnic_sub_crq_queue **allqueues;
3294 int registered_queues = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003295 int total_queues;
3296 int more = 0;
Thomas Falconea22d512016-07-06 15:35:17 -05003297 int i;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003298
Thomas Falcon032c5e82015-12-21 11:26:06 -06003299 total_queues = adapter->req_tx_queues + adapter->req_rx_queues;
3300
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04003301 allqueues = kcalloc(total_queues, sizeof(*allqueues), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003302 if (!allqueues)
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003303 return -1;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003304
3305 for (i = 0; i < total_queues; i++) {
3306 allqueues[i] = init_sub_crq_queue(adapter);
3307 if (!allqueues[i]) {
3308 dev_warn(dev, "Couldn't allocate all sub-crqs\n");
3309 break;
3310 }
3311 registered_queues++;
3312 }
3313
3314 /* Make sure we were able to register the minimum number of queues */
3315 if (registered_queues <
3316 adapter->min_tx_queues + adapter->min_rx_queues) {
3317 dev_err(dev, "Fatal: Couldn't init min number of sub-crqs\n");
3318 goto tx_failed;
3319 }
3320
3321 /* Distribute the failed allocated queues*/
3322 for (i = 0; i < total_queues - registered_queues + more ; i++) {
3323 netdev_dbg(adapter->netdev, "Reducing number of queues\n");
3324 switch (i % 3) {
3325 case 0:
3326 if (adapter->req_rx_queues > adapter->min_rx_queues)
3327 adapter->req_rx_queues--;
3328 else
3329 more++;
3330 break;
3331 case 1:
3332 if (adapter->req_tx_queues > adapter->min_tx_queues)
3333 adapter->req_tx_queues--;
3334 else
3335 more++;
3336 break;
3337 }
3338 }
3339
3340 adapter->tx_scrq = kcalloc(adapter->req_tx_queues,
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04003341 sizeof(*adapter->tx_scrq), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003342 if (!adapter->tx_scrq)
3343 goto tx_failed;
3344
3345 for (i = 0; i < adapter->req_tx_queues; i++) {
3346 adapter->tx_scrq[i] = allqueues[i];
3347 adapter->tx_scrq[i]->pool_index = i;
Nathan Fontenot82e3be32018-02-21 21:33:56 -06003348 adapter->num_active_tx_scrqs++;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003349 }
3350
3351 adapter->rx_scrq = kcalloc(adapter->req_rx_queues,
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04003352 sizeof(*adapter->rx_scrq), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003353 if (!adapter->rx_scrq)
3354 goto rx_failed;
3355
3356 for (i = 0; i < adapter->req_rx_queues; i++) {
3357 adapter->rx_scrq[i] = allqueues[i + adapter->req_tx_queues];
3358 adapter->rx_scrq[i]->scrq_num = i;
Nathan Fontenot82e3be32018-02-21 21:33:56 -06003359 adapter->num_active_rx_scrqs++;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003360 }
3361
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003362 kfree(allqueues);
3363 return 0;
3364
3365rx_failed:
3366 kfree(adapter->tx_scrq);
3367 adapter->tx_scrq = NULL;
3368tx_failed:
3369 for (i = 0; i < registered_queues; i++)
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06003370 release_sub_crq_queue(adapter, allqueues[i], 1);
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003371 kfree(allqueues);
3372 return -1;
3373}
3374
Lijun Pan09081b92020-09-27 20:13:27 -05003375static void send_request_cap(struct ibmvnic_adapter *adapter, int retry)
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003376{
3377 struct device *dev = &adapter->vdev->dev;
3378 union ibmvnic_crq crq;
John Allenc26eba02017-10-26 16:23:25 -05003379 int max_entries;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003380
3381 if (!retry) {
3382 /* Sub-CRQ entries are 32 byte long */
3383 int entries_page = 4 * PAGE_SIZE / (sizeof(u64) * 4);
3384
3385 if (adapter->min_tx_entries_per_subcrq > entries_page ||
3386 adapter->min_rx_add_entries_per_subcrq > entries_page) {
3387 dev_err(dev, "Fatal, invalid entries per sub-crq\n");
3388 return;
3389 }
3390
John Allenc26eba02017-10-26 16:23:25 -05003391 if (adapter->desired.mtu)
3392 adapter->req_mtu = adapter->desired.mtu;
3393 else
3394 adapter->req_mtu = adapter->netdev->mtu + ETH_HLEN;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003395
John Allenc26eba02017-10-26 16:23:25 -05003396 if (!adapter->desired.tx_entries)
3397 adapter->desired.tx_entries =
3398 adapter->max_tx_entries_per_subcrq;
3399 if (!adapter->desired.rx_entries)
3400 adapter->desired.rx_entries =
3401 adapter->max_rx_add_entries_per_subcrq;
3402
3403 max_entries = IBMVNIC_MAX_LTB_SIZE /
3404 (adapter->req_mtu + IBMVNIC_BUFFER_HLEN);
3405
3406 if ((adapter->req_mtu + IBMVNIC_BUFFER_HLEN) *
3407 adapter->desired.tx_entries > IBMVNIC_MAX_LTB_SIZE) {
3408 adapter->desired.tx_entries = max_entries;
3409 }
3410
3411 if ((adapter->req_mtu + IBMVNIC_BUFFER_HLEN) *
3412 adapter->desired.rx_entries > IBMVNIC_MAX_LTB_SIZE) {
3413 adapter->desired.rx_entries = max_entries;
3414 }
3415
3416 if (adapter->desired.tx_entries)
3417 adapter->req_tx_entries_per_subcrq =
3418 adapter->desired.tx_entries;
3419 else
3420 adapter->req_tx_entries_per_subcrq =
3421 adapter->max_tx_entries_per_subcrq;
3422
3423 if (adapter->desired.rx_entries)
3424 adapter->req_rx_add_entries_per_subcrq =
3425 adapter->desired.rx_entries;
3426 else
3427 adapter->req_rx_add_entries_per_subcrq =
3428 adapter->max_rx_add_entries_per_subcrq;
3429
3430 if (adapter->desired.tx_queues)
3431 adapter->req_tx_queues =
3432 adapter->desired.tx_queues;
3433 else
3434 adapter->req_tx_queues =
3435 adapter->opt_tx_comp_sub_queues;
3436
3437 if (adapter->desired.rx_queues)
3438 adapter->req_rx_queues =
3439 adapter->desired.rx_queues;
3440 else
3441 adapter->req_rx_queues =
3442 adapter->opt_rx_comp_queues;
3443
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003444 adapter->req_rx_add_queues = adapter->max_rx_add_queues;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003445 }
3446
Thomas Falcon032c5e82015-12-21 11:26:06 -06003447 memset(&crq, 0, sizeof(crq));
3448 crq.request_capability.first = IBMVNIC_CRQ_CMD;
3449 crq.request_capability.cmd = REQUEST_CAPABILITY;
3450
3451 crq.request_capability.capability = cpu_to_be16(REQ_TX_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06003452 crq.request_capability.number = cpu_to_be64(adapter->req_tx_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06003453 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003454 ibmvnic_send_crq(adapter, &crq);
3455
3456 crq.request_capability.capability = cpu_to_be16(REQ_RX_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06003457 crq.request_capability.number = cpu_to_be64(adapter->req_rx_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06003458 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003459 ibmvnic_send_crq(adapter, &crq);
3460
3461 crq.request_capability.capability = cpu_to_be16(REQ_RX_ADD_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06003462 crq.request_capability.number = cpu_to_be64(adapter->req_rx_add_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06003463 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003464 ibmvnic_send_crq(adapter, &crq);
3465
3466 crq.request_capability.capability =
3467 cpu_to_be16(REQ_TX_ENTRIES_PER_SUBCRQ);
3468 crq.request_capability.number =
Thomas Falconde89e852016-03-01 10:20:09 -06003469 cpu_to_be64(adapter->req_tx_entries_per_subcrq);
Thomas Falcon901e0402017-02-15 12:17:59 -06003470 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003471 ibmvnic_send_crq(adapter, &crq);
3472
3473 crq.request_capability.capability =
3474 cpu_to_be16(REQ_RX_ADD_ENTRIES_PER_SUBCRQ);
3475 crq.request_capability.number =
Thomas Falconde89e852016-03-01 10:20:09 -06003476 cpu_to_be64(adapter->req_rx_add_entries_per_subcrq);
Thomas Falcon901e0402017-02-15 12:17:59 -06003477 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003478 ibmvnic_send_crq(adapter, &crq);
3479
3480 crq.request_capability.capability = cpu_to_be16(REQ_MTU);
Thomas Falconde89e852016-03-01 10:20:09 -06003481 crq.request_capability.number = cpu_to_be64(adapter->req_mtu);
Thomas Falcon901e0402017-02-15 12:17:59 -06003482 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003483 ibmvnic_send_crq(adapter, &crq);
3484
3485 if (adapter->netdev->flags & IFF_PROMISC) {
3486 if (adapter->promisc_supported) {
3487 crq.request_capability.capability =
3488 cpu_to_be16(PROMISC_REQUESTED);
Thomas Falconde89e852016-03-01 10:20:09 -06003489 crq.request_capability.number = cpu_to_be64(1);
Thomas Falcon901e0402017-02-15 12:17:59 -06003490 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003491 ibmvnic_send_crq(adapter, &crq);
3492 }
3493 } else {
3494 crq.request_capability.capability =
3495 cpu_to_be16(PROMISC_REQUESTED);
Thomas Falconde89e852016-03-01 10:20:09 -06003496 crq.request_capability.number = cpu_to_be64(0);
Thomas Falcon901e0402017-02-15 12:17:59 -06003497 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003498 ibmvnic_send_crq(adapter, &crq);
3499 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06003500}
3501
3502static int pending_scrq(struct ibmvnic_adapter *adapter,
3503 struct ibmvnic_sub_crq_queue *scrq)
3504{
3505 union sub_crq *entry = &scrq->msgs[scrq->cur];
3506
Thomas Falcon1cf9cc72017-06-14 23:50:08 -05003507 if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003508 return 1;
3509 else
3510 return 0;
3511}
3512
3513static union sub_crq *ibmvnic_next_scrq(struct ibmvnic_adapter *adapter,
3514 struct ibmvnic_sub_crq_queue *scrq)
3515{
3516 union sub_crq *entry;
3517 unsigned long flags;
3518
3519 spin_lock_irqsave(&scrq->lock, flags);
3520 entry = &scrq->msgs[scrq->cur];
3521 if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP) {
3522 if (++scrq->cur == scrq->size)
3523 scrq->cur = 0;
3524 } else {
3525 entry = NULL;
3526 }
3527 spin_unlock_irqrestore(&scrq->lock, flags);
3528
3529 return entry;
3530}
3531
3532static union ibmvnic_crq *ibmvnic_next_crq(struct ibmvnic_adapter *adapter)
3533{
3534 struct ibmvnic_crq_queue *queue = &adapter->crq;
3535 union ibmvnic_crq *crq;
3536
3537 crq = &queue->msgs[queue->cur];
3538 if (crq->generic.first & IBMVNIC_CRQ_CMD_RSP) {
3539 if (++queue->cur == queue->size)
3540 queue->cur = 0;
3541 } else {
3542 crq = NULL;
3543 }
3544
3545 return crq;
3546}
3547
Thomas Falcon2d14d372018-07-13 12:03:32 -05003548static void print_subcrq_error(struct device *dev, int rc, const char *func)
3549{
3550 switch (rc) {
3551 case H_PARAMETER:
3552 dev_warn_ratelimited(dev,
3553 "%s failed: Send request is malformed or adapter failover pending. (rc=%d)\n",
3554 func, rc);
3555 break;
3556 case H_CLOSED:
3557 dev_warn_ratelimited(dev,
3558 "%s failed: Backing queue closed. Adapter is down or failover pending. (rc=%d)\n",
3559 func, rc);
3560 break;
3561 default:
3562 dev_err_ratelimited(dev, "%s failed: (rc=%d)\n", func, rc);
3563 break;
3564 }
3565}
3566
Thomas Falcon032c5e82015-12-21 11:26:06 -06003567static int send_subcrq(struct ibmvnic_adapter *adapter, u64 remote_handle,
3568 union sub_crq *sub_crq)
3569{
3570 unsigned int ua = adapter->vdev->unit_address;
3571 struct device *dev = &adapter->vdev->dev;
3572 u64 *u64_crq = (u64 *)sub_crq;
3573 int rc;
3574
3575 netdev_dbg(adapter->netdev,
3576 "Sending sCRQ %016lx: %016lx %016lx %016lx %016lx\n",
3577 (unsigned long int)cpu_to_be64(remote_handle),
3578 (unsigned long int)cpu_to_be64(u64_crq[0]),
3579 (unsigned long int)cpu_to_be64(u64_crq[1]),
3580 (unsigned long int)cpu_to_be64(u64_crq[2]),
3581 (unsigned long int)cpu_to_be64(u64_crq[3]));
3582
3583 /* Make sure the hypervisor sees the complete request */
3584 mb();
3585
3586 rc = plpar_hcall_norets(H_SEND_SUB_CRQ, ua,
3587 cpu_to_be64(remote_handle),
3588 cpu_to_be64(u64_crq[0]),
3589 cpu_to_be64(u64_crq[1]),
3590 cpu_to_be64(u64_crq[2]),
3591 cpu_to_be64(u64_crq[3]));
3592
Thomas Falcon2d14d372018-07-13 12:03:32 -05003593 if (rc)
3594 print_subcrq_error(dev, rc, __func__);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003595
3596 return rc;
3597}
3598
Thomas Falconad7775d2016-04-01 17:20:34 -05003599static int send_subcrq_indirect(struct ibmvnic_adapter *adapter,
3600 u64 remote_handle, u64 ioba, u64 num_entries)
3601{
3602 unsigned int ua = adapter->vdev->unit_address;
3603 struct device *dev = &adapter->vdev->dev;
3604 int rc;
3605
3606 /* Make sure the hypervisor sees the complete request */
3607 mb();
3608 rc = plpar_hcall_norets(H_SEND_SUB_CRQ_INDIRECT, ua,
3609 cpu_to_be64(remote_handle),
3610 ioba, num_entries);
3611
Thomas Falcon2d14d372018-07-13 12:03:32 -05003612 if (rc)
3613 print_subcrq_error(dev, rc, __func__);
Thomas Falconad7775d2016-04-01 17:20:34 -05003614
3615 return rc;
3616}
3617
Thomas Falcon032c5e82015-12-21 11:26:06 -06003618static int ibmvnic_send_crq(struct ibmvnic_adapter *adapter,
3619 union ibmvnic_crq *crq)
3620{
3621 unsigned int ua = adapter->vdev->unit_address;
3622 struct device *dev = &adapter->vdev->dev;
3623 u64 *u64_crq = (u64 *)crq;
3624 int rc;
3625
3626 netdev_dbg(adapter->netdev, "Sending CRQ: %016lx %016lx\n",
3627 (unsigned long int)cpu_to_be64(u64_crq[0]),
3628 (unsigned long int)cpu_to_be64(u64_crq[1]));
3629
Thomas Falcon51536982018-05-23 13:37:56 -05003630 if (!adapter->crq.active &&
3631 crq->generic.first != IBMVNIC_CRQ_INIT_CMD) {
3632 dev_warn(dev, "Invalid request detected while CRQ is inactive, possible device state change during reset\n");
3633 return -EINVAL;
3634 }
3635
Thomas Falcon032c5e82015-12-21 11:26:06 -06003636 /* Make sure the hypervisor sees the complete request */
3637 mb();
3638
3639 rc = plpar_hcall_norets(H_SEND_CRQ, ua,
3640 cpu_to_be64(u64_crq[0]),
3641 cpu_to_be64(u64_crq[1]));
3642
3643 if (rc) {
Nathan Fontenotec95dff2018-02-07 13:00:24 -06003644 if (rc == H_CLOSED) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06003645 dev_warn(dev, "CRQ Queue closed\n");
Lijun Panfa68bfa2020-08-19 17:52:24 -05003646 /* do not reset, report the fail, wait for passive init from server */
Nathan Fontenotec95dff2018-02-07 13:00:24 -06003647 }
3648
Thomas Falcon032c5e82015-12-21 11:26:06 -06003649 dev_warn(dev, "Send error (rc=%d)\n", rc);
3650 }
3651
3652 return rc;
3653}
3654
3655static int ibmvnic_send_crq_init(struct ibmvnic_adapter *adapter)
3656{
Thomas Falcon36a782f2020-08-31 11:59:57 -05003657 struct device *dev = &adapter->vdev->dev;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003658 union ibmvnic_crq crq;
Thomas Falcon36a782f2020-08-31 11:59:57 -05003659 int retries = 100;
3660 int rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003661
3662 memset(&crq, 0, sizeof(crq));
3663 crq.generic.first = IBMVNIC_CRQ_INIT_CMD;
3664 crq.generic.cmd = IBMVNIC_CRQ_INIT;
3665 netdev_dbg(adapter->netdev, "Sending CRQ init\n");
3666
Thomas Falcon36a782f2020-08-31 11:59:57 -05003667 do {
3668 rc = ibmvnic_send_crq(adapter, &crq);
3669 if (rc != H_CLOSED)
3670 break;
3671 retries--;
3672 msleep(50);
3673
3674 } while (retries > 0);
3675
3676 if (rc) {
3677 dev_err(dev, "Failed to send init request, rc = %d\n", rc);
3678 return rc;
3679 }
3680
3681 return 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003682}
3683
Thomas Falcon032c5e82015-12-21 11:26:06 -06003684static int send_version_xchg(struct ibmvnic_adapter *adapter)
3685{
3686 union ibmvnic_crq crq;
3687
3688 memset(&crq, 0, sizeof(crq));
3689 crq.version_exchange.first = IBMVNIC_CRQ_CMD;
3690 crq.version_exchange.cmd = VERSION_EXCHANGE;
3691 crq.version_exchange.version = cpu_to_be16(ibmvnic_version);
3692
3693 return ibmvnic_send_crq(adapter, &crq);
3694}
3695
Nathan Fontenot37798d02017-11-08 11:23:56 -06003696struct vnic_login_client_data {
3697 u8 type;
3698 __be16 len;
Kees Cook08ea5562018-04-10 15:26:43 -07003699 char name[];
Nathan Fontenot37798d02017-11-08 11:23:56 -06003700} __packed;
3701
3702static int vnic_client_data_len(struct ibmvnic_adapter *adapter)
3703{
3704 int len;
3705
3706 /* Calculate the amount of buffer space needed for the
3707 * vnic client data in the login buffer. There are four entries,
3708 * OS name, LPAR name, device name, and a null last entry.
3709 */
3710 len = 4 * sizeof(struct vnic_login_client_data);
3711 len += 6; /* "Linux" plus NULL */
3712 len += strlen(utsname()->nodename) + 1;
3713 len += strlen(adapter->netdev->name) + 1;
3714
3715 return len;
3716}
3717
3718static void vnic_add_client_data(struct ibmvnic_adapter *adapter,
3719 struct vnic_login_client_data *vlcd)
3720{
3721 const char *os_name = "Linux";
3722 int len;
3723
3724 /* Type 1 - LPAR OS */
3725 vlcd->type = 1;
3726 len = strlen(os_name) + 1;
3727 vlcd->len = cpu_to_be16(len);
Kees Cook08ea5562018-04-10 15:26:43 -07003728 strncpy(vlcd->name, os_name, len);
3729 vlcd = (struct vnic_login_client_data *)(vlcd->name + len);
Nathan Fontenot37798d02017-11-08 11:23:56 -06003730
3731 /* Type 2 - LPAR name */
3732 vlcd->type = 2;
3733 len = strlen(utsname()->nodename) + 1;
3734 vlcd->len = cpu_to_be16(len);
Kees Cook08ea5562018-04-10 15:26:43 -07003735 strncpy(vlcd->name, utsname()->nodename, len);
3736 vlcd = (struct vnic_login_client_data *)(vlcd->name + len);
Nathan Fontenot37798d02017-11-08 11:23:56 -06003737
3738 /* Type 3 - device name */
3739 vlcd->type = 3;
3740 len = strlen(adapter->netdev->name) + 1;
3741 vlcd->len = cpu_to_be16(len);
Kees Cook08ea5562018-04-10 15:26:43 -07003742 strncpy(vlcd->name, adapter->netdev->name, len);
Nathan Fontenot37798d02017-11-08 11:23:56 -06003743}
3744
Thomas Falcon20a8ab72018-02-26 18:10:59 -06003745static int send_login(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003746{
3747 struct ibmvnic_login_rsp_buffer *login_rsp_buffer;
3748 struct ibmvnic_login_buffer *login_buffer;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003749 struct device *dev = &adapter->vdev->dev;
3750 dma_addr_t rsp_buffer_token;
3751 dma_addr_t buffer_token;
3752 size_t rsp_buffer_size;
3753 union ibmvnic_crq crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003754 size_t buffer_size;
3755 __be64 *tx_list_p;
3756 __be64 *rx_list_p;
Nathan Fontenot37798d02017-11-08 11:23:56 -06003757 int client_data_len;
3758 struct vnic_login_client_data *vlcd;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003759 int i;
3760
Thomas Falcon20a8ab72018-02-26 18:10:59 -06003761 if (!adapter->tx_scrq || !adapter->rx_scrq) {
3762 netdev_err(adapter->netdev,
3763 "RX or TX queues are not allocated, device login failed\n");
3764 return -1;
3765 }
3766
Thomas Falcon34f0f4e2018-02-13 18:23:40 -06003767 release_login_rsp_buffer(adapter);
Nathan Fontenot37798d02017-11-08 11:23:56 -06003768 client_data_len = vnic_client_data_len(adapter);
3769
Thomas Falcon032c5e82015-12-21 11:26:06 -06003770 buffer_size =
3771 sizeof(struct ibmvnic_login_buffer) +
Nathan Fontenot37798d02017-11-08 11:23:56 -06003772 sizeof(u64) * (adapter->req_tx_queues + adapter->req_rx_queues) +
3773 client_data_len;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003774
Nathan Fontenot37798d02017-11-08 11:23:56 -06003775 login_buffer = kzalloc(buffer_size, GFP_ATOMIC);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003776 if (!login_buffer)
3777 goto buf_alloc_failed;
3778
3779 buffer_token = dma_map_single(dev, login_buffer, buffer_size,
3780 DMA_TO_DEVICE);
3781 if (dma_mapping_error(dev, buffer_token)) {
3782 dev_err(dev, "Couldn't map login buffer\n");
3783 goto buf_map_failed;
3784 }
3785
John Allen498cd8e2016-04-06 11:49:55 -05003786 rsp_buffer_size = sizeof(struct ibmvnic_login_rsp_buffer) +
3787 sizeof(u64) * adapter->req_tx_queues +
3788 sizeof(u64) * adapter->req_rx_queues +
3789 sizeof(u64) * adapter->req_rx_queues +
3790 sizeof(u8) * IBMVNIC_TX_DESC_VERSIONS;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003791
3792 login_rsp_buffer = kmalloc(rsp_buffer_size, GFP_ATOMIC);
3793 if (!login_rsp_buffer)
3794 goto buf_rsp_alloc_failed;
3795
3796 rsp_buffer_token = dma_map_single(dev, login_rsp_buffer,
3797 rsp_buffer_size, DMA_FROM_DEVICE);
3798 if (dma_mapping_error(dev, rsp_buffer_token)) {
3799 dev_err(dev, "Couldn't map login rsp buffer\n");
3800 goto buf_rsp_map_failed;
3801 }
Nathan Fontenot661a2622017-04-19 13:44:58 -04003802
Thomas Falcon032c5e82015-12-21 11:26:06 -06003803 adapter->login_buf = login_buffer;
3804 adapter->login_buf_token = buffer_token;
3805 adapter->login_buf_sz = buffer_size;
3806 adapter->login_rsp_buf = login_rsp_buffer;
3807 adapter->login_rsp_buf_token = rsp_buffer_token;
3808 adapter->login_rsp_buf_sz = rsp_buffer_size;
3809
3810 login_buffer->len = cpu_to_be32(buffer_size);
3811 login_buffer->version = cpu_to_be32(INITIAL_VERSION_LB);
3812 login_buffer->num_txcomp_subcrqs = cpu_to_be32(adapter->req_tx_queues);
3813 login_buffer->off_txcomp_subcrqs =
3814 cpu_to_be32(sizeof(struct ibmvnic_login_buffer));
3815 login_buffer->num_rxcomp_subcrqs = cpu_to_be32(adapter->req_rx_queues);
3816 login_buffer->off_rxcomp_subcrqs =
3817 cpu_to_be32(sizeof(struct ibmvnic_login_buffer) +
3818 sizeof(u64) * adapter->req_tx_queues);
3819 login_buffer->login_rsp_ioba = cpu_to_be32(rsp_buffer_token);
3820 login_buffer->login_rsp_len = cpu_to_be32(rsp_buffer_size);
3821
3822 tx_list_p = (__be64 *)((char *)login_buffer +
3823 sizeof(struct ibmvnic_login_buffer));
3824 rx_list_p = (__be64 *)((char *)login_buffer +
3825 sizeof(struct ibmvnic_login_buffer) +
3826 sizeof(u64) * adapter->req_tx_queues);
3827
3828 for (i = 0; i < adapter->req_tx_queues; i++) {
3829 if (adapter->tx_scrq[i]) {
3830 tx_list_p[i] = cpu_to_be64(adapter->tx_scrq[i]->
3831 crq_num);
3832 }
3833 }
3834
3835 for (i = 0; i < adapter->req_rx_queues; i++) {
3836 if (adapter->rx_scrq[i]) {
3837 rx_list_p[i] = cpu_to_be64(adapter->rx_scrq[i]->
3838 crq_num);
3839 }
3840 }
3841
Nathan Fontenot37798d02017-11-08 11:23:56 -06003842 /* Insert vNIC login client data */
3843 vlcd = (struct vnic_login_client_data *)
3844 ((char *)rx_list_p + (sizeof(u64) * adapter->req_rx_queues));
3845 login_buffer->client_data_offset =
3846 cpu_to_be32((char *)vlcd - (char *)login_buffer);
3847 login_buffer->client_data_len = cpu_to_be32(client_data_len);
3848
3849 vnic_add_client_data(adapter, vlcd);
3850
Thomas Falcon032c5e82015-12-21 11:26:06 -06003851 netdev_dbg(adapter->netdev, "Login Buffer:\n");
3852 for (i = 0; i < (adapter->login_buf_sz - 1) / 8 + 1; i++) {
3853 netdev_dbg(adapter->netdev, "%016lx\n",
3854 ((unsigned long int *)(adapter->login_buf))[i]);
3855 }
3856
3857 memset(&crq, 0, sizeof(crq));
3858 crq.login.first = IBMVNIC_CRQ_CMD;
3859 crq.login.cmd = LOGIN;
3860 crq.login.ioba = cpu_to_be32(buffer_token);
3861 crq.login.len = cpu_to_be32(buffer_size);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003862 ibmvnic_send_crq(adapter, &crq);
3863
Thomas Falcon20a8ab72018-02-26 18:10:59 -06003864 return 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003865
Thomas Falcon032c5e82015-12-21 11:26:06 -06003866buf_rsp_map_failed:
3867 kfree(login_rsp_buffer);
3868buf_rsp_alloc_failed:
3869 dma_unmap_single(dev, buffer_token, buffer_size, DMA_TO_DEVICE);
3870buf_map_failed:
3871 kfree(login_buffer);
3872buf_alloc_failed:
Thomas Falcon20a8ab72018-02-26 18:10:59 -06003873 return -1;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003874}
3875
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05003876static int send_request_map(struct ibmvnic_adapter *adapter, dma_addr_t addr,
3877 u32 len, u8 map_id)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003878{
3879 union ibmvnic_crq crq;
3880
3881 memset(&crq, 0, sizeof(crq));
3882 crq.request_map.first = IBMVNIC_CRQ_CMD;
3883 crq.request_map.cmd = REQUEST_MAP;
3884 crq.request_map.map_id = map_id;
3885 crq.request_map.ioba = cpu_to_be32(addr);
3886 crq.request_map.len = cpu_to_be32(len);
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05003887 return ibmvnic_send_crq(adapter, &crq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003888}
3889
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05003890static int send_request_unmap(struct ibmvnic_adapter *adapter, u8 map_id)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003891{
3892 union ibmvnic_crq crq;
3893
3894 memset(&crq, 0, sizeof(crq));
3895 crq.request_unmap.first = IBMVNIC_CRQ_CMD;
3896 crq.request_unmap.cmd = REQUEST_UNMAP;
3897 crq.request_unmap.map_id = map_id;
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05003898 return ibmvnic_send_crq(adapter, &crq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003899}
3900
Lijun Pan69980d02020-09-27 20:13:28 -05003901static void send_query_map(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003902{
3903 union ibmvnic_crq crq;
3904
3905 memset(&crq, 0, sizeof(crq));
3906 crq.query_map.first = IBMVNIC_CRQ_CMD;
3907 crq.query_map.cmd = QUERY_MAP;
3908 ibmvnic_send_crq(adapter, &crq);
3909}
3910
3911/* Send a series of CRQs requesting various capabilities of the VNIC server */
Lijun Pan491099a2020-09-27 20:13:26 -05003912static void send_query_cap(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003913{
3914 union ibmvnic_crq crq;
3915
Thomas Falcon901e0402017-02-15 12:17:59 -06003916 atomic_set(&adapter->running_cap_crqs, 0);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003917 memset(&crq, 0, sizeof(crq));
3918 crq.query_capability.first = IBMVNIC_CRQ_CMD;
3919 crq.query_capability.cmd = QUERY_CAPABILITY;
3920
3921 crq.query_capability.capability = cpu_to_be16(MIN_TX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003922 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003923 ibmvnic_send_crq(adapter, &crq);
3924
3925 crq.query_capability.capability = cpu_to_be16(MIN_RX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003926 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003927 ibmvnic_send_crq(adapter, &crq);
3928
3929 crq.query_capability.capability = cpu_to_be16(MIN_RX_ADD_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003930 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003931 ibmvnic_send_crq(adapter, &crq);
3932
3933 crq.query_capability.capability = cpu_to_be16(MAX_TX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003934 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003935 ibmvnic_send_crq(adapter, &crq);
3936
3937 crq.query_capability.capability = cpu_to_be16(MAX_RX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003938 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003939 ibmvnic_send_crq(adapter, &crq);
3940
3941 crq.query_capability.capability = cpu_to_be16(MAX_RX_ADD_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003942 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003943 ibmvnic_send_crq(adapter, &crq);
3944
3945 crq.query_capability.capability =
3946 cpu_to_be16(MIN_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06003947 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003948 ibmvnic_send_crq(adapter, &crq);
3949
3950 crq.query_capability.capability =
3951 cpu_to_be16(MIN_RX_ADD_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06003952 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003953 ibmvnic_send_crq(adapter, &crq);
3954
3955 crq.query_capability.capability =
3956 cpu_to_be16(MAX_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06003957 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003958 ibmvnic_send_crq(adapter, &crq);
3959
3960 crq.query_capability.capability =
3961 cpu_to_be16(MAX_RX_ADD_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06003962 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003963 ibmvnic_send_crq(adapter, &crq);
3964
3965 crq.query_capability.capability = cpu_to_be16(TCP_IP_OFFLOAD);
Thomas Falcon901e0402017-02-15 12:17:59 -06003966 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003967 ibmvnic_send_crq(adapter, &crq);
3968
3969 crq.query_capability.capability = cpu_to_be16(PROMISC_SUPPORTED);
Thomas Falcon901e0402017-02-15 12:17:59 -06003970 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003971 ibmvnic_send_crq(adapter, &crq);
3972
3973 crq.query_capability.capability = cpu_to_be16(MIN_MTU);
Thomas Falcon901e0402017-02-15 12:17:59 -06003974 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003975 ibmvnic_send_crq(adapter, &crq);
3976
3977 crq.query_capability.capability = cpu_to_be16(MAX_MTU);
Thomas Falcon901e0402017-02-15 12:17:59 -06003978 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003979 ibmvnic_send_crq(adapter, &crq);
3980
3981 crq.query_capability.capability = cpu_to_be16(MAX_MULTICAST_FILTERS);
Thomas Falcon901e0402017-02-15 12:17:59 -06003982 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003983 ibmvnic_send_crq(adapter, &crq);
3984
3985 crq.query_capability.capability = cpu_to_be16(VLAN_HEADER_INSERTION);
Thomas Falcon901e0402017-02-15 12:17:59 -06003986 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003987 ibmvnic_send_crq(adapter, &crq);
3988
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04003989 crq.query_capability.capability = cpu_to_be16(RX_VLAN_HEADER_INSERTION);
3990 atomic_inc(&adapter->running_cap_crqs);
3991 ibmvnic_send_crq(adapter, &crq);
3992
Thomas Falcon032c5e82015-12-21 11:26:06 -06003993 crq.query_capability.capability = cpu_to_be16(MAX_TX_SG_ENTRIES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003994 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003995 ibmvnic_send_crq(adapter, &crq);
3996
3997 crq.query_capability.capability = cpu_to_be16(RX_SG_SUPPORTED);
Thomas Falcon901e0402017-02-15 12:17:59 -06003998 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003999 ibmvnic_send_crq(adapter, &crq);
4000
4001 crq.query_capability.capability = cpu_to_be16(OPT_TX_COMP_SUB_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06004002 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004003 ibmvnic_send_crq(adapter, &crq);
4004
4005 crq.query_capability.capability = cpu_to_be16(OPT_RX_COMP_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06004006 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004007 ibmvnic_send_crq(adapter, &crq);
4008
4009 crq.query_capability.capability =
4010 cpu_to_be16(OPT_RX_BUFADD_Q_PER_RX_COMP_Q);
Thomas Falcon901e0402017-02-15 12:17:59 -06004011 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004012 ibmvnic_send_crq(adapter, &crq);
4013
4014 crq.query_capability.capability =
4015 cpu_to_be16(OPT_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06004016 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004017 ibmvnic_send_crq(adapter, &crq);
4018
4019 crq.query_capability.capability =
4020 cpu_to_be16(OPT_RXBA_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06004021 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004022 ibmvnic_send_crq(adapter, &crq);
4023
4024 crq.query_capability.capability = cpu_to_be16(TX_RX_DESC_REQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06004025 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004026 ibmvnic_send_crq(adapter, &crq);
4027}
4028
Lijun Pan16e811f2020-09-27 20:13:29 -05004029static void send_query_ip_offload(struct ibmvnic_adapter *adapter)
4030{
4031 int buf_sz = sizeof(struct ibmvnic_query_ip_offload_buffer);
4032 struct device *dev = &adapter->vdev->dev;
4033 union ibmvnic_crq crq;
4034
4035 adapter->ip_offload_tok =
4036 dma_map_single(dev,
4037 &adapter->ip_offload_buf,
4038 buf_sz,
4039 DMA_FROM_DEVICE);
4040
4041 if (dma_mapping_error(dev, adapter->ip_offload_tok)) {
4042 if (!firmware_has_feature(FW_FEATURE_CMO))
4043 dev_err(dev, "Couldn't map offload buffer\n");
4044 return;
4045 }
4046
4047 memset(&crq, 0, sizeof(crq));
4048 crq.query_ip_offload.first = IBMVNIC_CRQ_CMD;
4049 crq.query_ip_offload.cmd = QUERY_IP_OFFLOAD;
4050 crq.query_ip_offload.len = cpu_to_be32(buf_sz);
4051 crq.query_ip_offload.ioba =
4052 cpu_to_be32(adapter->ip_offload_tok);
4053
4054 ibmvnic_send_crq(adapter, &crq);
4055}
4056
Lijun Pan46899bd2020-09-27 20:13:30 -05004057static void send_control_ip_offload(struct ibmvnic_adapter *adapter)
4058{
4059 struct ibmvnic_control_ip_offload_buffer *ctrl_buf = &adapter->ip_offload_ctrl;
4060 struct ibmvnic_query_ip_offload_buffer *buf = &adapter->ip_offload_buf;
4061 struct device *dev = &adapter->vdev->dev;
4062 netdev_features_t old_hw_features = 0;
4063 union ibmvnic_crq crq;
4064
4065 adapter->ip_offload_ctrl_tok =
4066 dma_map_single(dev,
4067 ctrl_buf,
4068 sizeof(adapter->ip_offload_ctrl),
4069 DMA_TO_DEVICE);
4070
4071 if (dma_mapping_error(dev, adapter->ip_offload_ctrl_tok)) {
4072 dev_err(dev, "Couldn't map ip offload control buffer\n");
4073 return;
4074 }
4075
4076 ctrl_buf->len = cpu_to_be32(sizeof(adapter->ip_offload_ctrl));
4077 ctrl_buf->version = cpu_to_be32(INITIAL_VERSION_IOB);
4078 ctrl_buf->ipv4_chksum = buf->ipv4_chksum;
4079 ctrl_buf->ipv6_chksum = buf->ipv6_chksum;
4080 ctrl_buf->tcp_ipv4_chksum = buf->tcp_ipv4_chksum;
4081 ctrl_buf->udp_ipv4_chksum = buf->udp_ipv4_chksum;
4082 ctrl_buf->tcp_ipv6_chksum = buf->tcp_ipv6_chksum;
4083 ctrl_buf->udp_ipv6_chksum = buf->udp_ipv6_chksum;
4084 ctrl_buf->large_tx_ipv4 = buf->large_tx_ipv4;
4085 ctrl_buf->large_tx_ipv6 = buf->large_tx_ipv6;
4086
4087 /* large_rx disabled for now, additional features needed */
4088 ctrl_buf->large_rx_ipv4 = 0;
4089 ctrl_buf->large_rx_ipv6 = 0;
4090
4091 if (adapter->state != VNIC_PROBING) {
4092 old_hw_features = adapter->netdev->hw_features;
4093 adapter->netdev->hw_features = 0;
4094 }
4095
4096 adapter->netdev->hw_features = NETIF_F_SG | NETIF_F_GSO | NETIF_F_GRO;
4097
4098 if (buf->tcp_ipv4_chksum || buf->udp_ipv4_chksum)
4099 adapter->netdev->hw_features |= NETIF_F_IP_CSUM;
4100
4101 if (buf->tcp_ipv6_chksum || buf->udp_ipv6_chksum)
4102 adapter->netdev->hw_features |= NETIF_F_IPV6_CSUM;
4103
4104 if ((adapter->netdev->features &
4105 (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)))
4106 adapter->netdev->hw_features |= NETIF_F_RXCSUM;
4107
4108 if (buf->large_tx_ipv4)
4109 adapter->netdev->hw_features |= NETIF_F_TSO;
4110 if (buf->large_tx_ipv6)
4111 adapter->netdev->hw_features |= NETIF_F_TSO6;
4112
4113 if (adapter->state == VNIC_PROBING) {
4114 adapter->netdev->features |= adapter->netdev->hw_features;
4115 } else if (old_hw_features != adapter->netdev->hw_features) {
4116 netdev_features_t tmp = 0;
4117
4118 /* disable features no longer supported */
4119 adapter->netdev->features &= adapter->netdev->hw_features;
4120 /* turn on features now supported if previously enabled */
4121 tmp = (old_hw_features ^ adapter->netdev->hw_features) &
4122 adapter->netdev->hw_features;
4123 adapter->netdev->features |=
4124 tmp & adapter->netdev->wanted_features;
4125 }
4126
4127 memset(&crq, 0, sizeof(crq));
4128 crq.control_ip_offload.first = IBMVNIC_CRQ_CMD;
4129 crq.control_ip_offload.cmd = CONTROL_IP_OFFLOAD;
4130 crq.control_ip_offload.len =
4131 cpu_to_be32(sizeof(adapter->ip_offload_ctrl));
4132 crq.control_ip_offload.ioba = cpu_to_be32(adapter->ip_offload_ctrl_tok);
4133 ibmvnic_send_crq(adapter, &crq);
4134}
4135
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02004136static void handle_vpd_size_rsp(union ibmvnic_crq *crq,
4137 struct ibmvnic_adapter *adapter)
4138{
4139 struct device *dev = &adapter->vdev->dev;
4140
4141 if (crq->get_vpd_size_rsp.rc.code) {
4142 dev_err(dev, "Error retrieving VPD size, rc=%x\n",
4143 crq->get_vpd_size_rsp.rc.code);
4144 complete(&adapter->fw_done);
4145 return;
4146 }
4147
4148 adapter->vpd->len = be64_to_cpu(crq->get_vpd_size_rsp.len);
4149 complete(&adapter->fw_done);
4150}
4151
4152static void handle_vpd_rsp(union ibmvnic_crq *crq,
4153 struct ibmvnic_adapter *adapter)
4154{
4155 struct device *dev = &adapter->vdev->dev;
Desnes Augusto Nunes do Rosario21a25452018-02-05 14:33:55 -02004156 unsigned char *substr = NULL;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02004157 u8 fw_level_len = 0;
4158
4159 memset(adapter->fw_version, 0, 32);
4160
4161 dma_unmap_single(dev, adapter->vpd->dma_addr, adapter->vpd->len,
4162 DMA_FROM_DEVICE);
4163
4164 if (crq->get_vpd_rsp.rc.code) {
4165 dev_err(dev, "Error retrieving VPD from device, rc=%x\n",
4166 crq->get_vpd_rsp.rc.code);
4167 goto complete;
4168 }
4169
4170 /* get the position of the firmware version info
4171 * located after the ASCII 'RM' substring in the buffer
4172 */
4173 substr = strnstr(adapter->vpd->buff, "RM", adapter->vpd->len);
4174 if (!substr) {
Desnes Augusto Nunes do Rosarioa1073112018-02-01 16:04:30 -02004175 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 -02004176 goto complete;
4177 }
4178
4179 /* get length of firmware level ASCII substring */
4180 if ((substr + 2) < (adapter->vpd->buff + adapter->vpd->len)) {
4181 fw_level_len = *(substr + 2);
4182 } else {
4183 dev_info(dev, "Length of FW substr extrapolated VDP buff\n");
4184 goto complete;
4185 }
4186
4187 /* copy firmware version string from vpd into adapter */
4188 if ((substr + 3 + fw_level_len) <
4189 (adapter->vpd->buff + adapter->vpd->len)) {
Desnes Augusto Nunes do Rosario21a25452018-02-05 14:33:55 -02004190 strncpy((char *)adapter->fw_version, substr + 3, fw_level_len);
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02004191 } else {
4192 dev_info(dev, "FW substr extrapolated VPD buff\n");
4193 }
4194
4195complete:
Desnes Augusto Nunes do Rosario21a25452018-02-05 14:33:55 -02004196 if (adapter->fw_version[0] == '\0')
4197 strncpy((char *)adapter->fw_version, "N/A", 3 * sizeof(char));
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02004198 complete(&adapter->fw_done);
4199}
4200
Thomas Falcon032c5e82015-12-21 11:26:06 -06004201static void handle_query_ip_offload_rsp(struct ibmvnic_adapter *adapter)
4202{
4203 struct device *dev = &adapter->vdev->dev;
4204 struct ibmvnic_query_ip_offload_buffer *buf = &adapter->ip_offload_buf;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004205 int i;
4206
4207 dma_unmap_single(dev, adapter->ip_offload_tok,
4208 sizeof(adapter->ip_offload_buf), DMA_FROM_DEVICE);
4209
4210 netdev_dbg(adapter->netdev, "Query IP Offload Buffer:\n");
4211 for (i = 0; i < (sizeof(adapter->ip_offload_buf) - 1) / 8 + 1; i++)
4212 netdev_dbg(adapter->netdev, "%016lx\n",
4213 ((unsigned long int *)(buf))[i]);
4214
4215 netdev_dbg(adapter->netdev, "ipv4_chksum = %d\n", buf->ipv4_chksum);
4216 netdev_dbg(adapter->netdev, "ipv6_chksum = %d\n", buf->ipv6_chksum);
4217 netdev_dbg(adapter->netdev, "tcp_ipv4_chksum = %d\n",
4218 buf->tcp_ipv4_chksum);
4219 netdev_dbg(adapter->netdev, "tcp_ipv6_chksum = %d\n",
4220 buf->tcp_ipv6_chksum);
4221 netdev_dbg(adapter->netdev, "udp_ipv4_chksum = %d\n",
4222 buf->udp_ipv4_chksum);
4223 netdev_dbg(adapter->netdev, "udp_ipv6_chksum = %d\n",
4224 buf->udp_ipv6_chksum);
4225 netdev_dbg(adapter->netdev, "large_tx_ipv4 = %d\n",
4226 buf->large_tx_ipv4);
4227 netdev_dbg(adapter->netdev, "large_tx_ipv6 = %d\n",
4228 buf->large_tx_ipv6);
4229 netdev_dbg(adapter->netdev, "large_rx_ipv4 = %d\n",
4230 buf->large_rx_ipv4);
4231 netdev_dbg(adapter->netdev, "large_rx_ipv6 = %d\n",
4232 buf->large_rx_ipv6);
4233 netdev_dbg(adapter->netdev, "max_ipv4_hdr_sz = %d\n",
4234 buf->max_ipv4_header_size);
4235 netdev_dbg(adapter->netdev, "max_ipv6_hdr_sz = %d\n",
4236 buf->max_ipv6_header_size);
4237 netdev_dbg(adapter->netdev, "max_tcp_hdr_size = %d\n",
4238 buf->max_tcp_header_size);
4239 netdev_dbg(adapter->netdev, "max_udp_hdr_size = %d\n",
4240 buf->max_udp_header_size);
4241 netdev_dbg(adapter->netdev, "max_large_tx_size = %d\n",
4242 buf->max_large_tx_size);
4243 netdev_dbg(adapter->netdev, "max_large_rx_size = %d\n",
4244 buf->max_large_rx_size);
4245 netdev_dbg(adapter->netdev, "ipv6_ext_hdr = %d\n",
4246 buf->ipv6_extension_header);
4247 netdev_dbg(adapter->netdev, "tcp_pseudosum_req = %d\n",
4248 buf->tcp_pseudosum_req);
4249 netdev_dbg(adapter->netdev, "num_ipv6_ext_hd = %d\n",
4250 buf->num_ipv6_ext_headers);
4251 netdev_dbg(adapter->netdev, "off_ipv6_ext_hd = %d\n",
4252 buf->off_ipv6_ext_headers);
4253
Lijun Pan46899bd2020-09-27 20:13:30 -05004254 send_control_ip_offload(adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004255}
4256
Thomas Falconc9008d32018-08-06 21:39:59 -05004257static const char *ibmvnic_fw_err_cause(u16 cause)
4258{
4259 switch (cause) {
4260 case ADAPTER_PROBLEM:
4261 return "adapter problem";
4262 case BUS_PROBLEM:
4263 return "bus problem";
4264 case FW_PROBLEM:
4265 return "firmware problem";
4266 case DD_PROBLEM:
4267 return "device driver problem";
4268 case EEH_RECOVERY:
4269 return "EEH recovery";
4270 case FW_UPDATED:
4271 return "firmware updated";
4272 case LOW_MEMORY:
4273 return "low Memory";
4274 default:
4275 return "unknown";
4276 }
4277}
4278
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04004279static void handle_error_indication(union ibmvnic_crq *crq,
4280 struct ibmvnic_adapter *adapter)
4281{
4282 struct device *dev = &adapter->vdev->dev;
Thomas Falconc9008d32018-08-06 21:39:59 -05004283 u16 cause;
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04004284
Thomas Falconc9008d32018-08-06 21:39:59 -05004285 cause = be16_to_cpu(crq->error_indication.error_cause);
4286
4287 dev_warn_ratelimited(dev,
4288 "Firmware reports %serror, cause: %s. Starting recovery...\n",
4289 crq->error_indication.flags
4290 & IBMVNIC_FATAL_ERROR ? "FATAL " : "",
4291 ibmvnic_fw_err_cause(cause));
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04004292
Nathan Fontenoted651a12017-05-03 14:04:38 -04004293 if (crq->error_indication.flags & IBMVNIC_FATAL_ERROR)
4294 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
John Allen8cb31cf2017-05-26 10:30:37 -04004295 else
4296 ibmvnic_reset(adapter, VNIC_RESET_NON_FATAL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004297}
4298
Thomas Falconf8136142018-01-29 13:45:05 -06004299static int handle_change_mac_rsp(union ibmvnic_crq *crq,
4300 struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06004301{
4302 struct net_device *netdev = adapter->netdev;
4303 struct device *dev = &adapter->vdev->dev;
4304 long rc;
4305
4306 rc = crq->change_mac_addr_rsp.rc.code;
4307 if (rc) {
4308 dev_err(dev, "Error %ld in CHANGE_MAC_ADDR_RSP\n", rc);
Thomas Falconf8136142018-01-29 13:45:05 -06004309 goto out;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004310 }
Lijun Pand9b0e592020-10-20 17:39:19 -05004311 /* crq->change_mac_addr.mac_addr is the requested one
4312 * crq->change_mac_addr_rsp.mac_addr is the returned valid one.
4313 */
Thomas Falcon62740e92019-05-09 23:13:43 -05004314 ether_addr_copy(netdev->dev_addr,
4315 &crq->change_mac_addr_rsp.mac_addr[0]);
Lijun Pand9b0e592020-10-20 17:39:19 -05004316 ether_addr_copy(adapter->mac_addr,
4317 &crq->change_mac_addr_rsp.mac_addr[0]);
Thomas Falconf8136142018-01-29 13:45:05 -06004318out:
4319 complete(&adapter->fw_done);
4320 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004321}
4322
4323static void handle_request_cap_rsp(union ibmvnic_crq *crq,
4324 struct ibmvnic_adapter *adapter)
4325{
4326 struct device *dev = &adapter->vdev->dev;
4327 u64 *req_value;
4328 char *name;
4329
Thomas Falcon901e0402017-02-15 12:17:59 -06004330 atomic_dec(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004331 switch (be16_to_cpu(crq->request_capability_rsp.capability)) {
4332 case REQ_TX_QUEUES:
4333 req_value = &adapter->req_tx_queues;
4334 name = "tx";
4335 break;
4336 case REQ_RX_QUEUES:
4337 req_value = &adapter->req_rx_queues;
4338 name = "rx";
4339 break;
4340 case REQ_RX_ADD_QUEUES:
4341 req_value = &adapter->req_rx_add_queues;
4342 name = "rx_add";
4343 break;
4344 case REQ_TX_ENTRIES_PER_SUBCRQ:
4345 req_value = &adapter->req_tx_entries_per_subcrq;
4346 name = "tx_entries_per_subcrq";
4347 break;
4348 case REQ_RX_ADD_ENTRIES_PER_SUBCRQ:
4349 req_value = &adapter->req_rx_add_entries_per_subcrq;
4350 name = "rx_add_entries_per_subcrq";
4351 break;
4352 case REQ_MTU:
4353 req_value = &adapter->req_mtu;
4354 name = "mtu";
4355 break;
4356 case PROMISC_REQUESTED:
4357 req_value = &adapter->promisc;
4358 name = "promisc";
4359 break;
4360 default:
4361 dev_err(dev, "Got invalid cap request rsp %d\n",
4362 crq->request_capability.capability);
4363 return;
4364 }
4365
4366 switch (crq->request_capability_rsp.rc.code) {
4367 case SUCCESS:
4368 break;
4369 case PARTIALSUCCESS:
4370 dev_info(dev, "req=%lld, rsp=%ld in %s queue, retrying.\n",
4371 *req_value,
Thomas Falcon28f4d162017-02-15 10:32:11 -06004372 (long int)be64_to_cpu(crq->request_capability_rsp.
Thomas Falcon032c5e82015-12-21 11:26:06 -06004373 number), name);
John Allene7913802018-01-18 16:27:12 -06004374
4375 if (be16_to_cpu(crq->request_capability_rsp.capability) ==
4376 REQ_MTU) {
4377 pr_err("mtu of %llu is not supported. Reverting.\n",
4378 *req_value);
4379 *req_value = adapter->fallback.mtu;
4380 } else {
4381 *req_value =
4382 be64_to_cpu(crq->request_capability_rsp.number);
4383 }
4384
Lijun Pan09081b92020-09-27 20:13:27 -05004385 send_request_cap(adapter, 1);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004386 return;
4387 default:
4388 dev_err(dev, "Error %d in request cap rsp\n",
4389 crq->request_capability_rsp.rc.code);
4390 return;
4391 }
4392
4393 /* Done receiving requested capabilities, query IP offload support */
Thomas Falcon901e0402017-02-15 12:17:59 -06004394 if (atomic_read(&adapter->running_cap_crqs) == 0) {
Thomas Falcon249168a2017-02-15 12:18:00 -06004395 adapter->wait_capability = false;
Lijun Pan16e811f2020-09-27 20:13:29 -05004396 send_query_ip_offload(adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004397 }
4398}
4399
4400static int handle_login_rsp(union ibmvnic_crq *login_rsp_crq,
4401 struct ibmvnic_adapter *adapter)
4402{
4403 struct device *dev = &adapter->vdev->dev;
John Allenc26eba02017-10-26 16:23:25 -05004404 struct net_device *netdev = adapter->netdev;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004405 struct ibmvnic_login_rsp_buffer *login_rsp = adapter->login_rsp_buf;
4406 struct ibmvnic_login_buffer *login = adapter->login_buf;
Cristobal Fornof3ae59c2020-08-19 13:16:23 -05004407 u64 *tx_handle_array;
4408 u64 *rx_handle_array;
4409 int num_tx_pools;
4410 int num_rx_pools;
Thomas Falcon507ebe62020-08-21 13:39:01 -05004411 u64 *size_array;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004412 int i;
4413
4414 dma_unmap_single(dev, adapter->login_buf_token, adapter->login_buf_sz,
Thomas Falcon37e40fa2018-04-06 18:37:02 -05004415 DMA_TO_DEVICE);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004416 dma_unmap_single(dev, adapter->login_rsp_buf_token,
Thomas Falcon37e40fa2018-04-06 18:37:02 -05004417 adapter->login_rsp_buf_sz, DMA_FROM_DEVICE);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004418
John Allen498cd8e2016-04-06 11:49:55 -05004419 /* If the number of queues requested can't be allocated by the
4420 * server, the login response will return with code 1. We will need
4421 * to resend the login buffer with fewer queues requested.
4422 */
4423 if (login_rsp_crq->generic.rc.code) {
Nathan Fontenot64d92aa2018-04-11 10:09:32 -05004424 adapter->init_done_rc = login_rsp_crq->generic.rc.code;
John Allen498cd8e2016-04-06 11:49:55 -05004425 complete(&adapter->init_done);
4426 return 0;
4427 }
4428
John Allenc26eba02017-10-26 16:23:25 -05004429 netdev->mtu = adapter->req_mtu - ETH_HLEN;
4430
Thomas Falcon032c5e82015-12-21 11:26:06 -06004431 netdev_dbg(adapter->netdev, "Login Response Buffer:\n");
4432 for (i = 0; i < (adapter->login_rsp_buf_sz - 1) / 8 + 1; i++) {
4433 netdev_dbg(adapter->netdev, "%016lx\n",
4434 ((unsigned long int *)(adapter->login_rsp_buf))[i]);
4435 }
4436
4437 /* Sanity checks */
4438 if (login->num_txcomp_subcrqs != login_rsp->num_txsubm_subcrqs ||
4439 (be32_to_cpu(login->num_rxcomp_subcrqs) *
4440 adapter->req_rx_add_queues !=
4441 be32_to_cpu(login_rsp->num_rxadd_subcrqs))) {
4442 dev_err(dev, "FATAL: Inconsistent login and login rsp\n");
4443 ibmvnic_remove(adapter->vdev);
4444 return -EIO;
4445 }
Thomas Falcon507ebe62020-08-21 13:39:01 -05004446 size_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
4447 be32_to_cpu(adapter->login_rsp_buf->off_rxadd_buff_size));
4448 /* variable buffer sizes are not supported, so just read the
4449 * first entry.
4450 */
4451 adapter->cur_rx_buf_sz = be64_to_cpu(size_array[0]);
Cristobal Fornof3ae59c2020-08-19 13:16:23 -05004452
4453 num_tx_pools = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
4454 num_rx_pools = be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
4455
4456 tx_handle_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
4457 be32_to_cpu(adapter->login_rsp_buf->off_txsubm_subcrqs));
4458 rx_handle_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
4459 be32_to_cpu(adapter->login_rsp_buf->off_rxadd_subcrqs));
4460
4461 for (i = 0; i < num_tx_pools; i++)
4462 adapter->tx_scrq[i]->handle = tx_handle_array[i];
4463
4464 for (i = 0; i < num_rx_pools; i++)
4465 adapter->rx_scrq[i]->handle = rx_handle_array[i];
4466
Thomas Falcon507ebe62020-08-21 13:39:01 -05004467 adapter->num_active_tx_scrqs = num_tx_pools;
4468 adapter->num_active_rx_scrqs = num_rx_pools;
Cristobal Fornof3ae59c2020-08-19 13:16:23 -05004469 release_login_rsp_buffer(adapter);
Thomas Falcona2c0f032018-02-21 18:18:30 -06004470 release_login_buffer(adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004471 complete(&adapter->init_done);
4472
Thomas Falcon032c5e82015-12-21 11:26:06 -06004473 return 0;
4474}
4475
Thomas Falcon032c5e82015-12-21 11:26:06 -06004476static void handle_request_unmap_rsp(union ibmvnic_crq *crq,
4477 struct ibmvnic_adapter *adapter)
4478{
4479 struct device *dev = &adapter->vdev->dev;
4480 long rc;
4481
4482 rc = crq->request_unmap_rsp.rc.code;
4483 if (rc)
4484 dev_err(dev, "Error %ld in REQUEST_UNMAP_RSP\n", rc);
4485}
4486
4487static void handle_query_map_rsp(union ibmvnic_crq *crq,
4488 struct ibmvnic_adapter *adapter)
4489{
4490 struct net_device *netdev = adapter->netdev;
4491 struct device *dev = &adapter->vdev->dev;
4492 long rc;
4493
4494 rc = crq->query_map_rsp.rc.code;
4495 if (rc) {
4496 dev_err(dev, "Error %ld in QUERY_MAP_RSP\n", rc);
4497 return;
4498 }
4499 netdev_dbg(netdev, "page_size = %d\ntot_pages = %d\nfree_pages = %d\n",
4500 crq->query_map_rsp.page_size, crq->query_map_rsp.tot_pages,
4501 crq->query_map_rsp.free_pages);
4502}
4503
4504static void handle_query_cap_rsp(union ibmvnic_crq *crq,
4505 struct ibmvnic_adapter *adapter)
4506{
4507 struct net_device *netdev = adapter->netdev;
4508 struct device *dev = &adapter->vdev->dev;
4509 long rc;
4510
Thomas Falcon901e0402017-02-15 12:17:59 -06004511 atomic_dec(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004512 netdev_dbg(netdev, "Outstanding queries: %d\n",
Thomas Falcon901e0402017-02-15 12:17:59 -06004513 atomic_read(&adapter->running_cap_crqs));
Thomas Falcon032c5e82015-12-21 11:26:06 -06004514 rc = crq->query_capability.rc.code;
4515 if (rc) {
4516 dev_err(dev, "Error %ld in QUERY_CAP_RSP\n", rc);
4517 goto out;
4518 }
4519
4520 switch (be16_to_cpu(crq->query_capability.capability)) {
4521 case MIN_TX_QUEUES:
4522 adapter->min_tx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004523 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004524 netdev_dbg(netdev, "min_tx_queues = %lld\n",
4525 adapter->min_tx_queues);
4526 break;
4527 case MIN_RX_QUEUES:
4528 adapter->min_rx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004529 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004530 netdev_dbg(netdev, "min_rx_queues = %lld\n",
4531 adapter->min_rx_queues);
4532 break;
4533 case MIN_RX_ADD_QUEUES:
4534 adapter->min_rx_add_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004535 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004536 netdev_dbg(netdev, "min_rx_add_queues = %lld\n",
4537 adapter->min_rx_add_queues);
4538 break;
4539 case MAX_TX_QUEUES:
4540 adapter->max_tx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004541 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004542 netdev_dbg(netdev, "max_tx_queues = %lld\n",
4543 adapter->max_tx_queues);
4544 break;
4545 case MAX_RX_QUEUES:
4546 adapter->max_rx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004547 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004548 netdev_dbg(netdev, "max_rx_queues = %lld\n",
4549 adapter->max_rx_queues);
4550 break;
4551 case MAX_RX_ADD_QUEUES:
4552 adapter->max_rx_add_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004553 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004554 netdev_dbg(netdev, "max_rx_add_queues = %lld\n",
4555 adapter->max_rx_add_queues);
4556 break;
4557 case MIN_TX_ENTRIES_PER_SUBCRQ:
4558 adapter->min_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06004559 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004560 netdev_dbg(netdev, "min_tx_entries_per_subcrq = %lld\n",
4561 adapter->min_tx_entries_per_subcrq);
4562 break;
4563 case MIN_RX_ADD_ENTRIES_PER_SUBCRQ:
4564 adapter->min_rx_add_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06004565 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004566 netdev_dbg(netdev, "min_rx_add_entrs_per_subcrq = %lld\n",
4567 adapter->min_rx_add_entries_per_subcrq);
4568 break;
4569 case MAX_TX_ENTRIES_PER_SUBCRQ:
4570 adapter->max_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06004571 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004572 netdev_dbg(netdev, "max_tx_entries_per_subcrq = %lld\n",
4573 adapter->max_tx_entries_per_subcrq);
4574 break;
4575 case MAX_RX_ADD_ENTRIES_PER_SUBCRQ:
4576 adapter->max_rx_add_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06004577 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004578 netdev_dbg(netdev, "max_rx_add_entrs_per_subcrq = %lld\n",
4579 adapter->max_rx_add_entries_per_subcrq);
4580 break;
4581 case TCP_IP_OFFLOAD:
4582 adapter->tcp_ip_offload =
Thomas Falconde89e852016-03-01 10:20:09 -06004583 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004584 netdev_dbg(netdev, "tcp_ip_offload = %lld\n",
4585 adapter->tcp_ip_offload);
4586 break;
4587 case PROMISC_SUPPORTED:
4588 adapter->promisc_supported =
Thomas Falconde89e852016-03-01 10:20:09 -06004589 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004590 netdev_dbg(netdev, "promisc_supported = %lld\n",
4591 adapter->promisc_supported);
4592 break;
4593 case MIN_MTU:
Thomas Falconde89e852016-03-01 10:20:09 -06004594 adapter->min_mtu = be64_to_cpu(crq->query_capability.number);
Thomas Falconf39f0d12017-02-14 10:22:59 -06004595 netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004596 netdev_dbg(netdev, "min_mtu = %lld\n", adapter->min_mtu);
4597 break;
4598 case MAX_MTU:
Thomas Falconde89e852016-03-01 10:20:09 -06004599 adapter->max_mtu = be64_to_cpu(crq->query_capability.number);
Thomas Falconf39f0d12017-02-14 10:22:59 -06004600 netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004601 netdev_dbg(netdev, "max_mtu = %lld\n", adapter->max_mtu);
4602 break;
4603 case MAX_MULTICAST_FILTERS:
4604 adapter->max_multicast_filters =
Thomas Falconde89e852016-03-01 10:20:09 -06004605 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004606 netdev_dbg(netdev, "max_multicast_filters = %lld\n",
4607 adapter->max_multicast_filters);
4608 break;
4609 case VLAN_HEADER_INSERTION:
4610 adapter->vlan_header_insertion =
Thomas Falconde89e852016-03-01 10:20:09 -06004611 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004612 if (adapter->vlan_header_insertion)
4613 netdev->features |= NETIF_F_HW_VLAN_STAG_TX;
4614 netdev_dbg(netdev, "vlan_header_insertion = %lld\n",
4615 adapter->vlan_header_insertion);
4616 break;
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04004617 case RX_VLAN_HEADER_INSERTION:
4618 adapter->rx_vlan_header_insertion =
4619 be64_to_cpu(crq->query_capability.number);
4620 netdev_dbg(netdev, "rx_vlan_header_insertion = %lld\n",
4621 adapter->rx_vlan_header_insertion);
4622 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004623 case MAX_TX_SG_ENTRIES:
4624 adapter->max_tx_sg_entries =
Thomas Falconde89e852016-03-01 10:20:09 -06004625 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004626 netdev_dbg(netdev, "max_tx_sg_entries = %lld\n",
4627 adapter->max_tx_sg_entries);
4628 break;
4629 case RX_SG_SUPPORTED:
4630 adapter->rx_sg_supported =
Thomas Falconde89e852016-03-01 10:20:09 -06004631 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004632 netdev_dbg(netdev, "rx_sg_supported = %lld\n",
4633 adapter->rx_sg_supported);
4634 break;
4635 case OPT_TX_COMP_SUB_QUEUES:
4636 adapter->opt_tx_comp_sub_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004637 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004638 netdev_dbg(netdev, "opt_tx_comp_sub_queues = %lld\n",
4639 adapter->opt_tx_comp_sub_queues);
4640 break;
4641 case OPT_RX_COMP_QUEUES:
4642 adapter->opt_rx_comp_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004643 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004644 netdev_dbg(netdev, "opt_rx_comp_queues = %lld\n",
4645 adapter->opt_rx_comp_queues);
4646 break;
4647 case OPT_RX_BUFADD_Q_PER_RX_COMP_Q:
4648 adapter->opt_rx_bufadd_q_per_rx_comp_q =
Thomas Falconde89e852016-03-01 10:20:09 -06004649 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004650 netdev_dbg(netdev, "opt_rx_bufadd_q_per_rx_comp_q = %lld\n",
4651 adapter->opt_rx_bufadd_q_per_rx_comp_q);
4652 break;
4653 case OPT_TX_ENTRIES_PER_SUBCRQ:
4654 adapter->opt_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06004655 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004656 netdev_dbg(netdev, "opt_tx_entries_per_subcrq = %lld\n",
4657 adapter->opt_tx_entries_per_subcrq);
4658 break;
4659 case OPT_RXBA_ENTRIES_PER_SUBCRQ:
4660 adapter->opt_rxba_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06004661 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004662 netdev_dbg(netdev, "opt_rxba_entries_per_subcrq = %lld\n",
4663 adapter->opt_rxba_entries_per_subcrq);
4664 break;
4665 case TX_RX_DESC_REQ:
4666 adapter->tx_rx_desc_req = crq->query_capability.number;
4667 netdev_dbg(netdev, "tx_rx_desc_req = %llx\n",
4668 adapter->tx_rx_desc_req);
4669 break;
4670
4671 default:
4672 netdev_err(netdev, "Got invalid cap rsp %d\n",
4673 crq->query_capability.capability);
4674 }
4675
4676out:
Thomas Falcon249168a2017-02-15 12:18:00 -06004677 if (atomic_read(&adapter->running_cap_crqs) == 0) {
4678 adapter->wait_capability = false;
Lijun Pan09081b92020-09-27 20:13:27 -05004679 send_request_cap(adapter, 0);
Thomas Falcon249168a2017-02-15 12:18:00 -06004680 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06004681}
4682
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03004683static int send_query_phys_parms(struct ibmvnic_adapter *adapter)
4684{
4685 union ibmvnic_crq crq;
4686 int rc;
4687
4688 memset(&crq, 0, sizeof(crq));
4689 crq.query_phys_parms.first = IBMVNIC_CRQ_CMD;
4690 crq.query_phys_parms.cmd = QUERY_PHYS_PARMS;
Thomas Falconff25dcb2019-11-25 17:12:56 -06004691
4692 mutex_lock(&adapter->fw_lock);
4693 adapter->fw_done_rc = 0;
Thomas Falcon070eca92019-11-25 17:12:53 -06004694 reinit_completion(&adapter->fw_done);
Thomas Falconff25dcb2019-11-25 17:12:56 -06004695
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03004696 rc = ibmvnic_send_crq(adapter, &crq);
Thomas Falconff25dcb2019-11-25 17:12:56 -06004697 if (rc) {
4698 mutex_unlock(&adapter->fw_lock);
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03004699 return rc;
Thomas Falconff25dcb2019-11-25 17:12:56 -06004700 }
Thomas Falcon476d96c2019-11-25 17:12:55 -06004701
4702 rc = ibmvnic_wait_for_completion(adapter, &adapter->fw_done, 10000);
Thomas Falconff25dcb2019-11-25 17:12:56 -06004703 if (rc) {
4704 mutex_unlock(&adapter->fw_lock);
Thomas Falcon476d96c2019-11-25 17:12:55 -06004705 return rc;
Thomas Falconff25dcb2019-11-25 17:12:56 -06004706 }
Thomas Falcon476d96c2019-11-25 17:12:55 -06004707
Thomas Falconff25dcb2019-11-25 17:12:56 -06004708 mutex_unlock(&adapter->fw_lock);
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03004709 return adapter->fw_done_rc ? -EIO : 0;
4710}
4711
4712static int handle_query_phys_parms_rsp(union ibmvnic_crq *crq,
4713 struct ibmvnic_adapter *adapter)
4714{
4715 struct net_device *netdev = adapter->netdev;
4716 int rc;
Murilo Fossa Vicentinidd0f9d82019-09-16 11:50:37 -03004717 __be32 rspeed = cpu_to_be32(crq->query_phys_parms_rsp.speed);
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03004718
4719 rc = crq->query_phys_parms_rsp.rc.code;
4720 if (rc) {
4721 netdev_err(netdev, "Error %d in QUERY_PHYS_PARMS\n", rc);
4722 return rc;
4723 }
Murilo Fossa Vicentinidd0f9d82019-09-16 11:50:37 -03004724 switch (rspeed) {
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03004725 case IBMVNIC_10MBPS:
4726 adapter->speed = SPEED_10;
4727 break;
4728 case IBMVNIC_100MBPS:
4729 adapter->speed = SPEED_100;
4730 break;
4731 case IBMVNIC_1GBPS:
4732 adapter->speed = SPEED_1000;
4733 break;
Lijun Panb9cd7952020-09-27 19:06:25 -05004734 case IBMVNIC_10GBPS:
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03004735 adapter->speed = SPEED_10000;
4736 break;
4737 case IBMVNIC_25GBPS:
4738 adapter->speed = SPEED_25000;
4739 break;
4740 case IBMVNIC_40GBPS:
4741 adapter->speed = SPEED_40000;
4742 break;
4743 case IBMVNIC_50GBPS:
4744 adapter->speed = SPEED_50000;
4745 break;
4746 case IBMVNIC_100GBPS:
4747 adapter->speed = SPEED_100000;
4748 break;
Lijun Panb9cd7952020-09-27 19:06:25 -05004749 case IBMVNIC_200GBPS:
4750 adapter->speed = SPEED_200000;
4751 break;
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03004752 default:
Murilo Fossa Vicentinidd0f9d82019-09-16 11:50:37 -03004753 if (netif_carrier_ok(netdev))
4754 netdev_warn(netdev, "Unknown speed 0x%08x\n", rspeed);
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03004755 adapter->speed = SPEED_UNKNOWN;
4756 }
4757 if (crq->query_phys_parms_rsp.flags1 & IBMVNIC_FULL_DUPLEX)
4758 adapter->duplex = DUPLEX_FULL;
4759 else if (crq->query_phys_parms_rsp.flags1 & IBMVNIC_HALF_DUPLEX)
4760 adapter->duplex = DUPLEX_HALF;
4761 else
4762 adapter->duplex = DUPLEX_UNKNOWN;
4763
4764 return rc;
4765}
4766
Thomas Falcon032c5e82015-12-21 11:26:06 -06004767static void ibmvnic_handle_crq(union ibmvnic_crq *crq,
4768 struct ibmvnic_adapter *adapter)
4769{
4770 struct ibmvnic_generic_crq *gen_crq = &crq->generic;
4771 struct net_device *netdev = adapter->netdev;
4772 struct device *dev = &adapter->vdev->dev;
Murilo Fossa Vicentini993a82b2017-04-19 13:44:35 -04004773 u64 *u64_crq = (u64 *)crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004774 long rc;
4775
4776 netdev_dbg(netdev, "Handling CRQ: %016lx %016lx\n",
Murilo Fossa Vicentini993a82b2017-04-19 13:44:35 -04004777 (unsigned long int)cpu_to_be64(u64_crq[0]),
4778 (unsigned long int)cpu_to_be64(u64_crq[1]));
Thomas Falcon032c5e82015-12-21 11:26:06 -06004779 switch (gen_crq->first) {
4780 case IBMVNIC_CRQ_INIT_RSP:
4781 switch (gen_crq->cmd) {
4782 case IBMVNIC_CRQ_INIT:
4783 dev_info(dev, "Partner initialized\n");
John Allen017892c12017-05-26 10:30:19 -04004784 adapter->from_passive_init = true;
Thomas Falcon17c87052018-05-23 13:37:58 -05004785 if (!completion_done(&adapter->init_done)) {
4786 complete(&adapter->init_done);
4787 adapter->init_done_rc = -EIO;
4788 }
Thomas Falcon5a18e1e2018-04-06 18:37:05 -05004789 ibmvnic_reset(adapter, VNIC_RESET_FAILOVER);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004790 break;
4791 case IBMVNIC_CRQ_INIT_COMPLETE:
4792 dev_info(dev, "Partner initialization complete\n");
Thomas Falcon51536982018-05-23 13:37:56 -05004793 adapter->crq.active = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004794 send_version_xchg(adapter);
4795 break;
4796 default:
4797 dev_err(dev, "Unknown crq cmd: %d\n", gen_crq->cmd);
4798 }
4799 return;
4800 case IBMVNIC_CRQ_XPORT_EVENT:
Nathan Fontenoted651a12017-05-03 14:04:38 -04004801 netif_carrier_off(netdev);
Thomas Falcon51536982018-05-23 13:37:56 -05004802 adapter->crq.active = false;
Thomas Falcon2147e3d2019-11-25 17:12:54 -06004803 /* terminate any thread waiting for a response
4804 * from the device
4805 */
4806 if (!completion_done(&adapter->fw_done)) {
4807 adapter->fw_done_rc = -EIO;
4808 complete(&adapter->fw_done);
4809 }
4810 if (!completion_done(&adapter->stats_done))
4811 complete(&adapter->stats_done);
Juliet Kim7ed5b312019-09-20 16:11:23 -04004812 if (test_bit(0, &adapter->resetting))
Thomas Falcon2770a792018-05-23 13:38:02 -05004813 adapter->force_reset_recovery = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004814 if (gen_crq->cmd == IBMVNIC_PARTITION_MIGRATED) {
Nathan Fontenoted651a12017-05-03 14:04:38 -04004815 dev_info(dev, "Migrated, re-enabling adapter\n");
4816 ibmvnic_reset(adapter, VNIC_RESET_MOBILITY);
Thomas Falcondfad09a2016-08-18 11:37:51 -05004817 } else if (gen_crq->cmd == IBMVNIC_DEVICE_FAILOVER) {
4818 dev_info(dev, "Backing device failover detected\n");
Thomas Falcon5a18e1e2018-04-06 18:37:05 -05004819 adapter->failover_pending = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004820 } else {
4821 /* The adapter lost the connection */
4822 dev_err(dev, "Virtual Adapter failed (rc=%d)\n",
4823 gen_crq->cmd);
Nathan Fontenoted651a12017-05-03 14:04:38 -04004824 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004825 }
4826 return;
4827 case IBMVNIC_CRQ_CMD_RSP:
4828 break;
4829 default:
4830 dev_err(dev, "Got an invalid msg type 0x%02x\n",
4831 gen_crq->first);
4832 return;
4833 }
4834
4835 switch (gen_crq->cmd) {
4836 case VERSION_EXCHANGE_RSP:
4837 rc = crq->version_exchange_rsp.rc.code;
4838 if (rc) {
4839 dev_err(dev, "Error %ld in VERSION_EXCHG_RSP\n", rc);
4840 break;
4841 }
Thomas Falcon78468892020-05-28 11:19:17 -05004842 ibmvnic_version =
Thomas Falcon032c5e82015-12-21 11:26:06 -06004843 be16_to_cpu(crq->version_exchange_rsp.version);
Thomas Falcon78468892020-05-28 11:19:17 -05004844 dev_info(dev, "Partner protocol version is %d\n",
4845 ibmvnic_version);
Lijun Pan491099a2020-09-27 20:13:26 -05004846 send_query_cap(adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004847 break;
4848 case QUERY_CAPABILITY_RSP:
4849 handle_query_cap_rsp(crq, adapter);
4850 break;
4851 case QUERY_MAP_RSP:
4852 handle_query_map_rsp(crq, adapter);
4853 break;
4854 case REQUEST_MAP_RSP:
Thomas Falconf3be0cb2017-06-21 14:53:01 -05004855 adapter->fw_done_rc = crq->request_map_rsp.rc.code;
4856 complete(&adapter->fw_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004857 break;
4858 case REQUEST_UNMAP_RSP:
4859 handle_request_unmap_rsp(crq, adapter);
4860 break;
4861 case REQUEST_CAPABILITY_RSP:
4862 handle_request_cap_rsp(crq, adapter);
4863 break;
4864 case LOGIN_RSP:
4865 netdev_dbg(netdev, "Got Login Response\n");
4866 handle_login_rsp(crq, adapter);
4867 break;
4868 case LOGICAL_LINK_STATE_RSP:
Nathan Fontenot53da09e2017-04-21 15:39:04 -04004869 netdev_dbg(netdev,
4870 "Got Logical Link State Response, state: %d rc: %d\n",
4871 crq->logical_link_state_rsp.link_state,
4872 crq->logical_link_state_rsp.rc.code);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004873 adapter->logical_link_state =
4874 crq->logical_link_state_rsp.link_state;
Nathan Fontenot53da09e2017-04-21 15:39:04 -04004875 adapter->init_done_rc = crq->logical_link_state_rsp.rc.code;
4876 complete(&adapter->init_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004877 break;
4878 case LINK_STATE_INDICATION:
4879 netdev_dbg(netdev, "Got Logical Link State Indication\n");
4880 adapter->phys_link_state =
4881 crq->link_state_indication.phys_link_state;
4882 adapter->logical_link_state =
4883 crq->link_state_indication.logical_link_state;
Thomas Falcon0655f992019-05-09 23:13:44 -05004884 if (adapter->phys_link_state && adapter->logical_link_state)
4885 netif_carrier_on(netdev);
4886 else
4887 netif_carrier_off(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004888 break;
4889 case CHANGE_MAC_ADDR_RSP:
4890 netdev_dbg(netdev, "Got MAC address change Response\n");
Thomas Falconf8136142018-01-29 13:45:05 -06004891 adapter->fw_done_rc = handle_change_mac_rsp(crq, adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004892 break;
4893 case ERROR_INDICATION:
4894 netdev_dbg(netdev, "Got Error Indication\n");
4895 handle_error_indication(crq, adapter);
4896 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004897 case REQUEST_STATISTICS_RSP:
4898 netdev_dbg(netdev, "Got Statistics Response\n");
4899 complete(&adapter->stats_done);
4900 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004901 case QUERY_IP_OFFLOAD_RSP:
4902 netdev_dbg(netdev, "Got Query IP offload Response\n");
4903 handle_query_ip_offload_rsp(adapter);
4904 break;
4905 case MULTICAST_CTRL_RSP:
4906 netdev_dbg(netdev, "Got multicast control Response\n");
4907 break;
4908 case CONTROL_IP_OFFLOAD_RSP:
4909 netdev_dbg(netdev, "Got Control IP offload Response\n");
4910 dma_unmap_single(dev, adapter->ip_offload_ctrl_tok,
4911 sizeof(adapter->ip_offload_ctrl),
4912 DMA_TO_DEVICE);
John Allenbd0b6722017-03-17 17:13:40 -05004913 complete(&adapter->init_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004914 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004915 case COLLECT_FW_TRACE_RSP:
4916 netdev_dbg(netdev, "Got Collect firmware trace Response\n");
4917 complete(&adapter->fw_done);
4918 break;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02004919 case GET_VPD_SIZE_RSP:
4920 handle_vpd_size_rsp(crq, adapter);
4921 break;
4922 case GET_VPD_RSP:
4923 handle_vpd_rsp(crq, adapter);
4924 break;
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03004925 case QUERY_PHYS_PARMS_RSP:
4926 adapter->fw_done_rc = handle_query_phys_parms_rsp(crq, adapter);
4927 complete(&adapter->fw_done);
4928 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004929 default:
4930 netdev_err(netdev, "Got an invalid cmd type 0x%02x\n",
4931 gen_crq->cmd);
4932 }
4933}
4934
4935static irqreturn_t ibmvnic_interrupt(int irq, void *instance)
4936{
4937 struct ibmvnic_adapter *adapter = instance;
Thomas Falcon6c267b32017-02-15 12:17:58 -06004938
Thomas Falcon6c267b32017-02-15 12:17:58 -06004939 tasklet_schedule(&adapter->tasklet);
Thomas Falcon6c267b32017-02-15 12:17:58 -06004940 return IRQ_HANDLED;
4941}
4942
Allen Paisaa7c3fe2020-09-14 12:59:29 +05304943static void ibmvnic_tasklet(struct tasklet_struct *t)
Thomas Falcon6c267b32017-02-15 12:17:58 -06004944{
Allen Paisaa7c3fe2020-09-14 12:59:29 +05304945 struct ibmvnic_adapter *adapter = from_tasklet(adapter, t, tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004946 struct ibmvnic_crq_queue *queue = &adapter->crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004947 union ibmvnic_crq *crq;
4948 unsigned long flags;
4949 bool done = false;
4950
4951 spin_lock_irqsave(&queue->lock, flags);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004952 while (!done) {
4953 /* Pull all the valid messages off the CRQ */
4954 while ((crq = ibmvnic_next_crq(adapter)) != NULL) {
4955 ibmvnic_handle_crq(crq, adapter);
4956 crq->generic.first = 0;
4957 }
Brian Kinged7ecbf2017-04-19 13:44:53 -04004958
4959 /* remain in tasklet until all
4960 * capabilities responses are received
4961 */
4962 if (!adapter->wait_capability)
4963 done = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004964 }
Thomas Falcon249168a2017-02-15 12:18:00 -06004965 /* if capabilities CRQ's were sent in this tasklet, the following
4966 * tasklet must wait until all responses are received
4967 */
4968 if (atomic_read(&adapter->running_cap_crqs) != 0)
4969 adapter->wait_capability = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004970 spin_unlock_irqrestore(&queue->lock, flags);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004971}
4972
4973static int ibmvnic_reenable_crq_queue(struct ibmvnic_adapter *adapter)
4974{
4975 struct vio_dev *vdev = adapter->vdev;
4976 int rc;
4977
4978 do {
4979 rc = plpar_hcall_norets(H_ENABLE_CRQ, vdev->unit_address);
4980 } while (rc == H_IN_PROGRESS || rc == H_BUSY || H_IS_LONG_BUSY(rc));
4981
4982 if (rc)
4983 dev_err(&vdev->dev, "Error enabling adapter (rc=%d)\n", rc);
4984
4985 return rc;
4986}
4987
4988static int ibmvnic_reset_crq(struct ibmvnic_adapter *adapter)
4989{
4990 struct ibmvnic_crq_queue *crq = &adapter->crq;
4991 struct device *dev = &adapter->vdev->dev;
4992 struct vio_dev *vdev = adapter->vdev;
4993 int rc;
4994
4995 /* Close the CRQ */
4996 do {
4997 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
4998 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
4999
5000 /* Clean out the queue */
5001 memset(crq->msgs, 0, PAGE_SIZE);
5002 crq->cur = 0;
Thomas Falcon51536982018-05-23 13:37:56 -05005003 crq->active = false;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005004
5005 /* And re-open it again */
5006 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
5007 crq->msg_token, PAGE_SIZE);
5008
5009 if (rc == H_CLOSED)
5010 /* Adapter is good, but other end is not ready */
5011 dev_warn(dev, "Partner adapter not ready\n");
5012 else if (rc != 0)
5013 dev_warn(dev, "Couldn't register crq (rc=%d)\n", rc);
5014
5015 return rc;
5016}
5017
Nathan Fontenotf9928872017-03-30 02:48:54 -04005018static void release_crq_queue(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06005019{
5020 struct ibmvnic_crq_queue *crq = &adapter->crq;
5021 struct vio_dev *vdev = adapter->vdev;
5022 long rc;
5023
Nathan Fontenotf9928872017-03-30 02:48:54 -04005024 if (!crq->msgs)
5025 return;
5026
Thomas Falcon032c5e82015-12-21 11:26:06 -06005027 netdev_dbg(adapter->netdev, "Releasing CRQ\n");
5028 free_irq(vdev->irq, adapter);
Thomas Falcon6c267b32017-02-15 12:17:58 -06005029 tasklet_kill(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005030 do {
5031 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
5032 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
5033
5034 dma_unmap_single(&vdev->dev, crq->msg_token, PAGE_SIZE,
5035 DMA_BIDIRECTIONAL);
5036 free_page((unsigned long)crq->msgs);
Nathan Fontenotf9928872017-03-30 02:48:54 -04005037 crq->msgs = NULL;
Thomas Falcon51536982018-05-23 13:37:56 -05005038 crq->active = false;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005039}
5040
Nathan Fontenotf9928872017-03-30 02:48:54 -04005041static int init_crq_queue(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06005042{
5043 struct ibmvnic_crq_queue *crq = &adapter->crq;
5044 struct device *dev = &adapter->vdev->dev;
5045 struct vio_dev *vdev = adapter->vdev;
5046 int rc, retrc = -ENOMEM;
5047
Nathan Fontenotf9928872017-03-30 02:48:54 -04005048 if (crq->msgs)
5049 return 0;
5050
Thomas Falcon032c5e82015-12-21 11:26:06 -06005051 crq->msgs = (union ibmvnic_crq *)get_zeroed_page(GFP_KERNEL);
5052 /* Should we allocate more than one page? */
5053
5054 if (!crq->msgs)
5055 return -ENOMEM;
5056
5057 crq->size = PAGE_SIZE / sizeof(*crq->msgs);
5058 crq->msg_token = dma_map_single(dev, crq->msgs, PAGE_SIZE,
5059 DMA_BIDIRECTIONAL);
5060 if (dma_mapping_error(dev, crq->msg_token))
5061 goto map_failed;
5062
5063 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
5064 crq->msg_token, PAGE_SIZE);
5065
5066 if (rc == H_RESOURCE)
5067 /* maybe kexecing and resource is busy. try a reset */
5068 rc = ibmvnic_reset_crq(adapter);
5069 retrc = rc;
5070
5071 if (rc == H_CLOSED) {
5072 dev_warn(dev, "Partner adapter not ready\n");
5073 } else if (rc) {
5074 dev_warn(dev, "Error %d opening adapter\n", rc);
5075 goto reg_crq_failed;
5076 }
5077
5078 retrc = 0;
5079
Allen Paisaa7c3fe2020-09-14 12:59:29 +05305080 tasklet_setup(&adapter->tasklet, (void *)ibmvnic_tasklet);
Thomas Falcon6c267b32017-02-15 12:17:58 -06005081
Thomas Falcon032c5e82015-12-21 11:26:06 -06005082 netdev_dbg(adapter->netdev, "registering irq 0x%x\n", vdev->irq);
Murilo Fossa Vicentinie56e2512019-04-25 11:02:33 -03005083 snprintf(crq->name, sizeof(crq->name), "ibmvnic-%x",
5084 adapter->vdev->unit_address);
5085 rc = request_irq(vdev->irq, ibmvnic_interrupt, 0, crq->name, adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005086 if (rc) {
5087 dev_err(dev, "Couldn't register irq 0x%x. rc=%d\n",
5088 vdev->irq, rc);
5089 goto req_irq_failed;
5090 }
5091
5092 rc = vio_enable_interrupts(vdev);
5093 if (rc) {
5094 dev_err(dev, "Error %d enabling interrupts\n", rc);
5095 goto req_irq_failed;
5096 }
5097
5098 crq->cur = 0;
5099 spin_lock_init(&crq->lock);
5100
5101 return retrc;
5102
5103req_irq_failed:
Thomas Falcon6c267b32017-02-15 12:17:58 -06005104 tasklet_kill(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005105 do {
5106 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
5107 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
5108reg_crq_failed:
5109 dma_unmap_single(dev, crq->msg_token, PAGE_SIZE, DMA_BIDIRECTIONAL);
5110map_failed:
5111 free_page((unsigned long)crq->msgs);
Nathan Fontenotf9928872017-03-30 02:48:54 -04005112 crq->msgs = NULL;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005113 return retrc;
5114}
5115
Lijun Pan635e4422020-08-19 17:52:26 -05005116static int ibmvnic_reset_init(struct ibmvnic_adapter *adapter, bool reset)
John Allenf6ef6402017-03-17 17:13:42 -05005117{
5118 struct device *dev = &adapter->vdev->dev;
5119 unsigned long timeout = msecs_to_jiffies(30000);
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06005120 u64 old_num_rx_queues, old_num_tx_queues;
John Allenf6ef6402017-03-17 17:13:42 -05005121 int rc;
5122
John Allen017892c12017-05-26 10:30:19 -04005123 adapter->from_passive_init = false;
5124
Lijun Pan635e4422020-08-19 17:52:26 -05005125 if (reset) {
5126 old_num_rx_queues = adapter->req_rx_queues;
5127 old_num_tx_queues = adapter->req_tx_queues;
5128 reinit_completion(&adapter->init_done);
5129 }
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06005130
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04005131 adapter->init_done_rc = 0;
Lijun Panfa68bfa2020-08-19 17:52:24 -05005132 rc = ibmvnic_send_crq_init(adapter);
5133 if (rc) {
5134 dev_err(dev, "Send crq init failed with error %d\n", rc);
5135 return rc;
5136 }
5137
John Allenf6ef6402017-03-17 17:13:42 -05005138 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
5139 dev_err(dev, "Initialization sequence timed out\n");
John Allen017892c12017-05-26 10:30:19 -04005140 return -1;
5141 }
5142
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04005143 if (adapter->init_done_rc) {
5144 release_crq_queue(adapter);
5145 return adapter->init_done_rc;
5146 }
5147
Lijun Pan785a2b12020-09-17 21:12:46 -05005148 if (adapter->from_passive_init) {
5149 adapter->state = VNIC_OPEN;
5150 adapter->from_passive_init = false;
5151 return -1;
5152 }
5153
Lijun Pan635e4422020-08-19 17:52:26 -05005154 if (reset &&
5155 test_bit(0, &adapter->resetting) && !adapter->wait_for_reset &&
Nathan Fontenot30f79622018-04-06 18:37:06 -05005156 adapter->reset_reason != VNIC_RESET_MOBILITY) {
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06005157 if (adapter->req_rx_queues != old_num_rx_queues ||
5158 adapter->req_tx_queues != old_num_tx_queues) {
5159 release_sub_crqs(adapter, 0);
5160 rc = init_sub_crqs(adapter);
5161 } else {
5162 rc = reset_sub_crq_queues(adapter);
5163 }
5164 } else {
Nathan Fontenot57a49432017-05-26 10:31:12 -04005165 rc = init_sub_crqs(adapter);
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06005166 }
5167
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04005168 if (rc) {
5169 dev_err(dev, "Initialization of sub crqs failed\n");
5170 release_crq_queue(adapter);
Thomas Falcon5df969c2017-06-28 19:55:54 -05005171 return rc;
5172 }
5173
5174 rc = init_sub_crq_irqs(adapter);
5175 if (rc) {
5176 dev_err(dev, "Failed to initialize sub crq irqs\n");
5177 release_crq_queue(adapter);
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04005178 }
5179
5180 return rc;
John Allenf6ef6402017-03-17 17:13:42 -05005181}
5182
Thomas Falcon40c9db82017-06-12 12:35:04 -05005183static struct device_attribute dev_attr_failover;
5184
Thomas Falcon032c5e82015-12-21 11:26:06 -06005185static int ibmvnic_probe(struct vio_dev *dev, const struct vio_device_id *id)
5186{
5187 struct ibmvnic_adapter *adapter;
5188 struct net_device *netdev;
5189 unsigned char *mac_addr_p;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005190 int rc;
5191
5192 dev_dbg(&dev->dev, "entering ibmvnic_probe for UA 0x%x\n",
5193 dev->unit_address);
5194
5195 mac_addr_p = (unsigned char *)vio_get_attribute(dev,
5196 VETH_MAC_ADDR, NULL);
5197 if (!mac_addr_p) {
5198 dev_err(&dev->dev,
5199 "(%s:%3.3d) ERROR: Can't find MAC_ADDR attribute\n",
5200 __FILE__, __LINE__);
5201 return 0;
5202 }
5203
5204 netdev = alloc_etherdev_mq(sizeof(struct ibmvnic_adapter),
Thomas Falcond45cc3a2017-12-18 12:52:11 -06005205 IBMVNIC_MAX_QUEUES);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005206 if (!netdev)
5207 return -ENOMEM;
5208
5209 adapter = netdev_priv(netdev);
Nathan Fontenot90c80142017-05-03 14:04:32 -04005210 adapter->state = VNIC_PROBING;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005211 dev_set_drvdata(&dev->dev, netdev);
5212 adapter->vdev = dev;
5213 adapter->netdev = netdev;
5214
5215 ether_addr_copy(adapter->mac_addr, mac_addr_p);
5216 ether_addr_copy(netdev->dev_addr, adapter->mac_addr);
5217 netdev->irq = dev->irq;
5218 netdev->netdev_ops = &ibmvnic_netdev_ops;
5219 netdev->ethtool_ops = &ibmvnic_ethtool_ops;
5220 SET_NETDEV_DEV(netdev, &dev->dev);
5221
5222 spin_lock_init(&adapter->stats_lock);
5223
Nathan Fontenoted651a12017-05-03 14:04:38 -04005224 INIT_WORK(&adapter->ibmvnic_reset, __ibmvnic_reset);
Juliet Kim7ed5b312019-09-20 16:11:23 -04005225 INIT_DELAYED_WORK(&adapter->ibmvnic_delayed_reset,
5226 __ibmvnic_delayed_reset);
Nathan Fontenoted651a12017-05-03 14:04:38 -04005227 INIT_LIST_HEAD(&adapter->rwi_list);
Thomas Falcon6c5c7482018-12-10 15:22:22 -06005228 spin_lock_init(&adapter->rwi_lock);
Juliet Kim7d7195a2020-03-10 09:23:58 -05005229 spin_lock_init(&adapter->state_lock);
Thomas Falconff25dcb2019-11-25 17:12:56 -06005230 mutex_init(&adapter->fw_lock);
Thomas Falconbbd669a2019-04-04 18:58:26 -05005231 init_completion(&adapter->init_done);
Thomas Falcon070eca92019-11-25 17:12:53 -06005232 init_completion(&adapter->fw_done);
5233 init_completion(&adapter->reset_done);
5234 init_completion(&adapter->stats_done);
Juliet Kim7ed5b312019-09-20 16:11:23 -04005235 clear_bit(0, &adapter->resetting);
Nathan Fontenoted651a12017-05-03 14:04:38 -04005236
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04005237 do {
Nathan Fontenot30f79622018-04-06 18:37:06 -05005238 rc = init_crq_queue(adapter);
5239 if (rc) {
5240 dev_err(&dev->dev, "Couldn't initialize crq. rc=%d\n",
5241 rc);
5242 goto ibmvnic_init_fail;
5243 }
5244
Lijun Pan635e4422020-08-19 17:52:26 -05005245 rc = ibmvnic_reset_init(adapter, false);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05005246 if (rc && rc != EAGAIN)
5247 goto ibmvnic_init_fail;
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04005248 } while (rc == EAGAIN);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005249
Thomas Falcon07184212018-05-16 15:49:05 -05005250 rc = init_stats_buffers(adapter);
5251 if (rc)
5252 goto ibmvnic_init_fail;
5253
5254 rc = init_stats_token(adapter);
5255 if (rc)
5256 goto ibmvnic_stats_fail;
5257
Thomas Falconf39f0d12017-02-14 10:22:59 -06005258 netdev->mtu = adapter->req_mtu - ETH_HLEN;
John Allenc26eba02017-10-26 16:23:25 -05005259 netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
5260 netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005261
Thomas Falcon40c9db82017-06-12 12:35:04 -05005262 rc = device_create_file(&dev->dev, &dev_attr_failover);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05005263 if (rc)
Thomas Falcon07184212018-05-16 15:49:05 -05005264 goto ibmvnic_dev_file_err;
Thomas Falcon40c9db82017-06-12 12:35:04 -05005265
Mick Tarsele876a8a2017-09-28 13:53:18 -07005266 netif_carrier_off(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005267 rc = register_netdev(netdev);
5268 if (rc) {
5269 dev_err(&dev->dev, "failed to register netdev rc=%d\n", rc);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05005270 goto ibmvnic_register_fail;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005271 }
5272 dev_info(&dev->dev, "ibmvnic registered\n");
5273
Nathan Fontenot90c80142017-05-03 14:04:32 -04005274 adapter->state = VNIC_PROBED;
John Allenc26eba02017-10-26 16:23:25 -05005275
5276 adapter->wait_for_reset = false;
5277
Thomas Falcon032c5e82015-12-21 11:26:06 -06005278 return 0;
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05005279
5280ibmvnic_register_fail:
5281 device_remove_file(&dev->dev, &dev_attr_failover);
5282
Thomas Falcon07184212018-05-16 15:49:05 -05005283ibmvnic_dev_file_err:
5284 release_stats_token(adapter);
5285
5286ibmvnic_stats_fail:
5287 release_stats_buffers(adapter);
5288
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05005289ibmvnic_init_fail:
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06005290 release_sub_crqs(adapter, 1);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05005291 release_crq_queue(adapter);
Thomas Falconff25dcb2019-11-25 17:12:56 -06005292 mutex_destroy(&adapter->fw_lock);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05005293 free_netdev(netdev);
5294
5295 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005296}
5297
5298static int ibmvnic_remove(struct vio_dev *dev)
5299{
5300 struct net_device *netdev = dev_get_drvdata(&dev->dev);
Nathan Fontenot37489052017-04-19 13:45:04 -04005301 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Juliet Kim7d7195a2020-03-10 09:23:58 -05005302 unsigned long flags;
5303
5304 spin_lock_irqsave(&adapter->state_lock, flags);
5305 if (adapter->state == VNIC_RESETTING) {
5306 spin_unlock_irqrestore(&adapter->state_lock, flags);
5307 return -EBUSY;
5308 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06005309
Nathan Fontenot90c80142017-05-03 14:04:32 -04005310 adapter->state = VNIC_REMOVING;
Juliet Kim7d7195a2020-03-10 09:23:58 -05005311 spin_unlock_irqrestore(&adapter->state_lock, flags);
5312
Thomas Falcon6954a9e2020-06-12 13:34:41 -05005313 flush_work(&adapter->ibmvnic_reset);
5314 flush_delayed_work(&adapter->ibmvnic_delayed_reset);
5315
Juliet Kima5681e22018-11-19 15:59:22 -06005316 rtnl_lock();
5317 unregister_netdevice(netdev);
Nathan Fontenot37489052017-04-19 13:45:04 -04005318
5319 release_resources(adapter);
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06005320 release_sub_crqs(adapter, 1);
Nathan Fontenot37489052017-04-19 13:45:04 -04005321 release_crq_queue(adapter);
5322
Thomas Falcon53cc7722018-02-26 18:10:56 -06005323 release_stats_token(adapter);
5324 release_stats_buffers(adapter);
5325
Nathan Fontenot90c80142017-05-03 14:04:32 -04005326 adapter->state = VNIC_REMOVED;
5327
Juliet Kima5681e22018-11-19 15:59:22 -06005328 rtnl_unlock();
Thomas Falconff25dcb2019-11-25 17:12:56 -06005329 mutex_destroy(&adapter->fw_lock);
Thomas Falcon40c9db82017-06-12 12:35:04 -05005330 device_remove_file(&dev->dev, &dev_attr_failover);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005331 free_netdev(netdev);
5332 dev_set_drvdata(&dev->dev, NULL);
5333
5334 return 0;
5335}
5336
Thomas Falcon40c9db82017-06-12 12:35:04 -05005337static ssize_t failover_store(struct device *dev, struct device_attribute *attr,
5338 const char *buf, size_t count)
5339{
5340 struct net_device *netdev = dev_get_drvdata(dev);
5341 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
5342 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
5343 __be64 session_token;
5344 long rc;
5345
5346 if (!sysfs_streq(buf, "1"))
5347 return -EINVAL;
5348
5349 rc = plpar_hcall(H_VIOCTL, retbuf, adapter->vdev->unit_address,
5350 H_GET_SESSION_TOKEN, 0, 0, 0);
5351 if (rc) {
5352 netdev_err(netdev, "Couldn't retrieve session token, rc %ld\n",
5353 rc);
5354 return -EINVAL;
5355 }
5356
5357 session_token = (__be64)retbuf[0];
5358 netdev_dbg(netdev, "Initiating client failover, session id %llx\n",
5359 be64_to_cpu(session_token));
5360 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
5361 H_SESSION_ERR_DETECTED, session_token, 0, 0);
5362 if (rc) {
5363 netdev_err(netdev, "Client initiated failover failed, rc %ld\n",
5364 rc);
5365 return -EINVAL;
5366 }
5367
5368 return count;
5369}
5370
Joe Perches6cbaefb2017-12-19 10:15:09 -08005371static DEVICE_ATTR_WO(failover);
Thomas Falcon40c9db82017-06-12 12:35:04 -05005372
Thomas Falcon032c5e82015-12-21 11:26:06 -06005373static unsigned long ibmvnic_get_desired_dma(struct vio_dev *vdev)
5374{
5375 struct net_device *netdev = dev_get_drvdata(&vdev->dev);
5376 struct ibmvnic_adapter *adapter;
5377 struct iommu_table *tbl;
5378 unsigned long ret = 0;
5379 int i;
5380
5381 tbl = get_iommu_table_base(&vdev->dev);
5382
5383 /* netdev inits at probe time along with the structures we need below*/
5384 if (!netdev)
5385 return IOMMU_PAGE_ALIGN(IBMVNIC_IO_ENTITLEMENT_DEFAULT, tbl);
5386
5387 adapter = netdev_priv(netdev);
5388
5389 ret += PAGE_SIZE; /* the crq message queue */
Thomas Falcon032c5e82015-12-21 11:26:06 -06005390 ret += IOMMU_PAGE_ALIGN(sizeof(struct ibmvnic_statistics), tbl);
5391
5392 for (i = 0; i < adapter->req_tx_queues + adapter->req_rx_queues; i++)
5393 ret += 4 * PAGE_SIZE; /* the scrq message queue */
5394
Thomas Falcon507ebe62020-08-21 13:39:01 -05005395 for (i = 0; i < adapter->num_active_rx_pools; i++)
Thomas Falcon032c5e82015-12-21 11:26:06 -06005396 ret += adapter->rx_pool[i].size *
5397 IOMMU_PAGE_ALIGN(adapter->rx_pool[i].buff_size, tbl);
5398
5399 return ret;
5400}
5401
5402static int ibmvnic_resume(struct device *dev)
5403{
5404 struct net_device *netdev = dev_get_drvdata(dev);
5405 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005406
John Allencb89ba22017-06-19 11:27:53 -05005407 if (adapter->state != VNIC_OPEN)
5408 return 0;
5409
John Allena2488782017-07-24 13:26:06 -05005410 tasklet_schedule(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005411
5412 return 0;
5413}
5414
Arvind Yadav8c37bc62017-08-17 18:52:54 +05305415static const struct vio_device_id ibmvnic_device_table[] = {
Thomas Falcon032c5e82015-12-21 11:26:06 -06005416 {"network", "IBM,vnic"},
5417 {"", "" }
5418};
5419MODULE_DEVICE_TABLE(vio, ibmvnic_device_table);
5420
5421static const struct dev_pm_ops ibmvnic_pm_ops = {
5422 .resume = ibmvnic_resume
5423};
5424
5425static struct vio_driver ibmvnic_driver = {
5426 .id_table = ibmvnic_device_table,
5427 .probe = ibmvnic_probe,
5428 .remove = ibmvnic_remove,
5429 .get_desired_dma = ibmvnic_get_desired_dma,
5430 .name = ibmvnic_driver_name,
5431 .pm = &ibmvnic_pm_ops,
5432};
5433
5434/* module functions */
5435static int __init ibmvnic_module_init(void)
5436{
5437 pr_info("%s: %s %s\n", ibmvnic_driver_name, ibmvnic_driver_string,
5438 IBMVNIC_DRIVER_VERSION);
5439
5440 return vio_register_driver(&ibmvnic_driver);
5441}
5442
5443static void __exit ibmvnic_module_exit(void)
5444{
5445 vio_unregister_driver(&ibmvnic_driver);
5446}
5447
5448module_init(ibmvnic_module_init);
5449module_exit(ibmvnic_module_exit);