blob: e2a3c4bf00c98f9e5fddf359814840ed24b7442a [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);
100static void send_map_query(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);
John Allenbd0b6722017-03-17 17:13:40 -0500104static void send_cap_queries(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;
309 int buffers_added = 0;
310 unsigned long lpar_rc;
311 union sub_crq sub_crq;
312 struct sk_buff *skb;
313 unsigned int offset;
314 dma_addr_t dma_addr;
315 unsigned char *dst;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600316 int shift = 0;
317 int index;
318 int i;
319
Thomas Falconf185a492017-05-26 10:30:48 -0400320 if (!pool->active)
321 return;
322
Thomas Falcon032c5e82015-12-21 11:26:06 -0600323 for (i = 0; i < count; ++i) {
324 skb = alloc_skb(pool->buff_size, GFP_ATOMIC);
325 if (!skb) {
326 dev_err(dev, "Couldn't replenish rx buff\n");
327 adapter->replenish_no_mem++;
328 break;
329 }
330
331 index = pool->free_map[pool->next_free];
332
333 if (pool->rx_buff[index].skb)
334 dev_err(dev, "Inconsistent free_map!\n");
335
336 /* Copy the skb to the long term mapped DMA buffer */
337 offset = index * pool->buff_size;
338 dst = pool->long_term_buff.buff + offset;
339 memset(dst, 0, pool->buff_size);
340 dma_addr = pool->long_term_buff.addr + offset;
341 pool->rx_buff[index].data = dst;
342
343 pool->free_map[pool->next_free] = IBMVNIC_INVALID_MAP;
344 pool->rx_buff[index].dma = dma_addr;
345 pool->rx_buff[index].skb = skb;
346 pool->rx_buff[index].pool_index = pool->index;
347 pool->rx_buff[index].size = pool->buff_size;
348
349 memset(&sub_crq, 0, sizeof(sub_crq));
350 sub_crq.rx_add.first = IBMVNIC_CRQ_CMD;
351 sub_crq.rx_add.correlator =
352 cpu_to_be64((u64)&pool->rx_buff[index]);
353 sub_crq.rx_add.ioba = cpu_to_be32(dma_addr);
354 sub_crq.rx_add.map_id = pool->long_term_buff.map_id;
355
356 /* The length field of the sCRQ is defined to be 24 bits so the
357 * buffer size needs to be left shifted by a byte before it is
358 * converted to big endian to prevent the last byte from being
359 * truncated.
360 */
361#ifdef __LITTLE_ENDIAN__
362 shift = 8;
363#endif
364 sub_crq.rx_add.len = cpu_to_be32(pool->buff_size << shift);
365
Cristobal Fornof3ae59c2020-08-19 13:16:23 -0500366 lpar_rc = send_subcrq(adapter, handle, &sub_crq);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600367 if (lpar_rc != H_SUCCESS)
368 goto failure;
369
370 buffers_added++;
371 adapter->replenish_add_buff_success++;
372 pool->next_free = (pool->next_free + 1) % pool->size;
373 }
374 atomic_add(buffers_added, &pool->available);
375 return;
376
377failure:
Thomas Falcon2d14d372018-07-13 12:03:32 -0500378 if (lpar_rc != H_PARAMETER && lpar_rc != H_CLOSED)
379 dev_err_ratelimited(dev, "rx: replenish packet buffer failed\n");
Thomas Falcon032c5e82015-12-21 11:26:06 -0600380 pool->free_map[pool->next_free] = index;
381 pool->rx_buff[index].skb = NULL;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600382
383 dev_kfree_skb_any(skb);
384 adapter->replenish_add_buff_failure++;
385 atomic_add(buffers_added, &pool->available);
Thomas Falconf185a492017-05-26 10:30:48 -0400386
Thomas Falcon5a18e1e2018-04-06 18:37:05 -0500387 if (lpar_rc == H_CLOSED || adapter->failover_pending) {
Thomas Falconf185a492017-05-26 10:30:48 -0400388 /* Disable buffer pool replenishment and report carrier off if
Thomas Falcon5a18e1e2018-04-06 18:37:05 -0500389 * queue is closed or pending failover.
390 * Firmware guarantees that a signal will be sent to the
391 * driver, triggering a reset.
Thomas Falconf185a492017-05-26 10:30:48 -0400392 */
393 deactivate_rx_pools(adapter);
394 netif_carrier_off(adapter->netdev);
395 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600396}
397
398static void replenish_pools(struct ibmvnic_adapter *adapter)
399{
400 int i;
401
Thomas Falcon032c5e82015-12-21 11:26:06 -0600402 adapter->replenish_task_cycles++;
Thomas Falcon507ebe62020-08-21 13:39:01 -0500403 for (i = 0; i < adapter->num_active_rx_pools; i++) {
Thomas Falcon032c5e82015-12-21 11:26:06 -0600404 if (adapter->rx_pool[i].active)
405 replenish_rx_pool(adapter, &adapter->rx_pool[i]);
406 }
407}
408
John Allen3d52b592017-08-02 16:44:14 -0500409static void release_stats_buffers(struct ibmvnic_adapter *adapter)
410{
411 kfree(adapter->tx_stats_buffers);
412 kfree(adapter->rx_stats_buffers);
Thomas Falconb0992ec2018-02-06 17:25:23 -0600413 adapter->tx_stats_buffers = NULL;
414 adapter->rx_stats_buffers = NULL;
John Allen3d52b592017-08-02 16:44:14 -0500415}
416
417static int init_stats_buffers(struct ibmvnic_adapter *adapter)
418{
419 adapter->tx_stats_buffers =
Nathan Fontenotabcae542018-02-19 13:30:47 -0600420 kcalloc(IBMVNIC_MAX_QUEUES,
John Allen3d52b592017-08-02 16:44:14 -0500421 sizeof(struct ibmvnic_tx_queue_stats),
422 GFP_KERNEL);
423 if (!adapter->tx_stats_buffers)
424 return -ENOMEM;
425
426 adapter->rx_stats_buffers =
Nathan Fontenotabcae542018-02-19 13:30:47 -0600427 kcalloc(IBMVNIC_MAX_QUEUES,
John Allen3d52b592017-08-02 16:44:14 -0500428 sizeof(struct ibmvnic_rx_queue_stats),
429 GFP_KERNEL);
430 if (!adapter->rx_stats_buffers)
431 return -ENOMEM;
432
433 return 0;
434}
435
Nathan Fontenot7bbc27a2017-03-30 02:49:23 -0400436static void release_stats_token(struct ibmvnic_adapter *adapter)
437{
438 struct device *dev = &adapter->vdev->dev;
439
440 if (!adapter->stats_token)
441 return;
442
443 dma_unmap_single(dev, adapter->stats_token,
444 sizeof(struct ibmvnic_statistics),
445 DMA_FROM_DEVICE);
446 adapter->stats_token = 0;
447}
448
449static int init_stats_token(struct ibmvnic_adapter *adapter)
450{
451 struct device *dev = &adapter->vdev->dev;
452 dma_addr_t stok;
453
454 stok = dma_map_single(dev, &adapter->stats,
455 sizeof(struct ibmvnic_statistics),
456 DMA_FROM_DEVICE);
457 if (dma_mapping_error(dev, stok)) {
458 dev_err(dev, "Couldn't map stats buffer\n");
459 return -1;
460 }
461
462 adapter->stats_token = stok;
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500463 netdev_dbg(adapter->netdev, "Stats token initialized (%llx)\n", stok);
Nathan Fontenot7bbc27a2017-03-30 02:49:23 -0400464 return 0;
465}
466
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400467static int reset_rx_pools(struct ibmvnic_adapter *adapter)
468{
469 struct ibmvnic_rx_pool *rx_pool;
Thomas Falcon507ebe62020-08-21 13:39:01 -0500470 u64 buff_size;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400471 int rx_scrqs;
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500472 int i, j, rc;
John Allen896d8692018-01-18 16:26:31 -0600473
Mingming Cao9f134572020-08-25 13:26:41 -0400474 if (!adapter->rx_pool)
475 return -1;
476
Thomas Falcon507ebe62020-08-21 13:39:01 -0500477 buff_size = adapter->cur_rx_buf_sz;
478 rx_scrqs = adapter->num_active_rx_pools;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400479 for (i = 0; i < rx_scrqs; i++) {
480 rx_pool = &adapter->rx_pool[i];
481
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500482 netdev_dbg(adapter->netdev, "Re-setting rx_pool[%d]\n", i);
483
Thomas Falcon507ebe62020-08-21 13:39:01 -0500484 if (rx_pool->buff_size != buff_size) {
John Allen896d8692018-01-18 16:26:31 -0600485 free_long_term_buff(adapter, &rx_pool->long_term_buff);
Thomas Falcon507ebe62020-08-21 13:39:01 -0500486 rx_pool->buff_size = buff_size;
Thomas Falcon7c940b12019-06-07 16:03:55 -0500487 rc = alloc_long_term_buff(adapter,
488 &rx_pool->long_term_buff,
489 rx_pool->size *
490 rx_pool->buff_size);
John Allen896d8692018-01-18 16:26:31 -0600491 } else {
492 rc = reset_long_term_buff(adapter,
493 &rx_pool->long_term_buff);
494 }
495
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500496 if (rc)
497 return rc;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400498
499 for (j = 0; j < rx_pool->size; j++)
500 rx_pool->free_map[j] = j;
501
502 memset(rx_pool->rx_buff, 0,
503 rx_pool->size * sizeof(struct ibmvnic_rx_buff));
504
505 atomic_set(&rx_pool->available, 0);
506 rx_pool->next_alloc = 0;
507 rx_pool->next_free = 0;
Thomas Falconc3e53b92017-06-14 23:50:05 -0500508 rx_pool->active = 1;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400509 }
510
511 return 0;
512}
513
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400514static void release_rx_pools(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -0600515{
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400516 struct ibmvnic_rx_pool *rx_pool;
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400517 int i, j;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600518
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400519 if (!adapter->rx_pool)
Thomas Falcon032c5e82015-12-21 11:26:06 -0600520 return;
521
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600522 for (i = 0; i < adapter->num_active_rx_pools; i++) {
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400523 rx_pool = &adapter->rx_pool[i];
524
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500525 netdev_dbg(adapter->netdev, "Releasing rx_pool[%d]\n", i);
526
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400527 kfree(rx_pool->free_map);
528 free_long_term_buff(adapter, &rx_pool->long_term_buff);
529
530 if (!rx_pool->rx_buff)
Nathan Fontenote0ebe9422017-05-03 14:04:50 -0400531 continue;
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400532
533 for (j = 0; j < rx_pool->size; j++) {
534 if (rx_pool->rx_buff[j].skb) {
Thomas Falconb7cdec32018-11-21 11:17:58 -0600535 dev_kfree_skb_any(rx_pool->rx_buff[j].skb);
536 rx_pool->rx_buff[j].skb = NULL;
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400537 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600538 }
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400539
540 kfree(rx_pool->rx_buff);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600541 }
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400542
543 kfree(adapter->rx_pool);
544 adapter->rx_pool = NULL;
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600545 adapter->num_active_rx_pools = 0;
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400546}
547
548static int init_rx_pools(struct net_device *netdev)
549{
550 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
551 struct device *dev = &adapter->vdev->dev;
552 struct ibmvnic_rx_pool *rx_pool;
553 int rxadd_subcrqs;
Thomas Falcon507ebe62020-08-21 13:39:01 -0500554 u64 buff_size;
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400555 int i, j;
556
Thomas Falcon507ebe62020-08-21 13:39:01 -0500557 rxadd_subcrqs = adapter->num_active_rx_scrqs;
558 buff_size = adapter->cur_rx_buf_sz;
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400559
560 adapter->rx_pool = kcalloc(rxadd_subcrqs,
561 sizeof(struct ibmvnic_rx_pool),
562 GFP_KERNEL);
563 if (!adapter->rx_pool) {
564 dev_err(dev, "Failed to allocate rx pools\n");
565 return -1;
566 }
567
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600568 adapter->num_active_rx_pools = rxadd_subcrqs;
569
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400570 for (i = 0; i < rxadd_subcrqs; i++) {
571 rx_pool = &adapter->rx_pool[i];
572
573 netdev_dbg(adapter->netdev,
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500574 "Initializing rx_pool[%d], %lld buffs, %lld bytes each\n",
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400575 i, adapter->req_rx_add_entries_per_subcrq,
Thomas Falcon507ebe62020-08-21 13:39:01 -0500576 buff_size);
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400577
578 rx_pool->size = adapter->req_rx_add_entries_per_subcrq;
579 rx_pool->index = i;
Thomas Falcon507ebe62020-08-21 13:39:01 -0500580 rx_pool->buff_size = buff_size;
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400581 rx_pool->active = 1;
582
583 rx_pool->free_map = kcalloc(rx_pool->size, sizeof(int),
584 GFP_KERNEL);
585 if (!rx_pool->free_map) {
586 release_rx_pools(adapter);
587 return -1;
588 }
589
590 rx_pool->rx_buff = kcalloc(rx_pool->size,
591 sizeof(struct ibmvnic_rx_buff),
592 GFP_KERNEL);
593 if (!rx_pool->rx_buff) {
594 dev_err(dev, "Couldn't alloc rx buffers\n");
595 release_rx_pools(adapter);
596 return -1;
597 }
598
599 if (alloc_long_term_buff(adapter, &rx_pool->long_term_buff,
600 rx_pool->size * rx_pool->buff_size)) {
601 release_rx_pools(adapter);
602 return -1;
603 }
604
605 for (j = 0; j < rx_pool->size; ++j)
606 rx_pool->free_map[j] = j;
607
608 atomic_set(&rx_pool->available, 0);
609 rx_pool->next_alloc = 0;
610 rx_pool->next_free = 0;
611 }
612
613 return 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600614}
615
Thomas Falcone26dc252018-03-16 20:00:25 -0500616static int reset_one_tx_pool(struct ibmvnic_adapter *adapter,
617 struct ibmvnic_tx_pool *tx_pool)
618{
619 int rc, i;
620
621 rc = reset_long_term_buff(adapter, &tx_pool->long_term_buff);
622 if (rc)
623 return rc;
624
625 memset(tx_pool->tx_buff, 0,
626 tx_pool->num_buffers *
627 sizeof(struct ibmvnic_tx_buff));
628
629 for (i = 0; i < tx_pool->num_buffers; i++)
630 tx_pool->free_map[i] = i;
631
632 tx_pool->consumer_index = 0;
633 tx_pool->producer_index = 0;
634
635 return 0;
636}
637
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400638static int reset_tx_pools(struct ibmvnic_adapter *adapter)
639{
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400640 int tx_scrqs;
Thomas Falcone26dc252018-03-16 20:00:25 -0500641 int i, rc;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400642
Mingming Cao9f134572020-08-25 13:26:41 -0400643 if (!adapter->tx_pool)
644 return -1;
645
Thomas Falcon507ebe62020-08-21 13:39:01 -0500646 tx_scrqs = adapter->num_active_tx_pools;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400647 for (i = 0; i < tx_scrqs; i++) {
Thomas Falcone26dc252018-03-16 20:00:25 -0500648 rc = reset_one_tx_pool(adapter, &adapter->tso_pool[i]);
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500649 if (rc)
650 return rc;
Thomas Falcone26dc252018-03-16 20:00:25 -0500651 rc = reset_one_tx_pool(adapter, &adapter->tx_pool[i]);
Thomas Falconfdb06102017-10-17 12:36:55 -0500652 if (rc)
653 return rc;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400654 }
655
656 return 0;
657}
658
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -0200659static void release_vpd_data(struct ibmvnic_adapter *adapter)
660{
661 if (!adapter->vpd)
662 return;
663
664 kfree(adapter->vpd->buff);
665 kfree(adapter->vpd);
Thomas Falconb0992ec2018-02-06 17:25:23 -0600666
667 adapter->vpd = NULL;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -0200668}
669
Thomas Falconfb794212018-03-16 20:00:26 -0500670static void release_one_tx_pool(struct ibmvnic_adapter *adapter,
671 struct ibmvnic_tx_pool *tx_pool)
672{
673 kfree(tx_pool->tx_buff);
674 kfree(tx_pool->free_map);
675 free_long_term_buff(adapter, &tx_pool->long_term_buff);
676}
677
Nathan Fontenotc657e322017-03-30 02:49:06 -0400678static void release_tx_pools(struct ibmvnic_adapter *adapter)
679{
John Allen896d8692018-01-18 16:26:31 -0600680 int i;
Nathan Fontenotc657e322017-03-30 02:49:06 -0400681
682 if (!adapter->tx_pool)
683 return;
684
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600685 for (i = 0; i < adapter->num_active_tx_pools; i++) {
Thomas Falconfb794212018-03-16 20:00:26 -0500686 release_one_tx_pool(adapter, &adapter->tx_pool[i]);
687 release_one_tx_pool(adapter, &adapter->tso_pool[i]);
Nathan Fontenotc657e322017-03-30 02:49:06 -0400688 }
689
690 kfree(adapter->tx_pool);
691 adapter->tx_pool = NULL;
Thomas Falconfb794212018-03-16 20:00:26 -0500692 kfree(adapter->tso_pool);
693 adapter->tso_pool = NULL;
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600694 adapter->num_active_tx_pools = 0;
Nathan Fontenotc657e322017-03-30 02:49:06 -0400695}
696
Thomas Falcon32053062018-03-16 20:00:27 -0500697static int init_one_tx_pool(struct net_device *netdev,
698 struct ibmvnic_tx_pool *tx_pool,
699 int num_entries, int buf_size)
700{
701 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
702 int i;
703
704 tx_pool->tx_buff = kcalloc(num_entries,
705 sizeof(struct ibmvnic_tx_buff),
706 GFP_KERNEL);
707 if (!tx_pool->tx_buff)
708 return -1;
709
710 if (alloc_long_term_buff(adapter, &tx_pool->long_term_buff,
711 num_entries * buf_size))
712 return -1;
713
714 tx_pool->free_map = kcalloc(num_entries, sizeof(int), GFP_KERNEL);
715 if (!tx_pool->free_map)
716 return -1;
717
718 for (i = 0; i < num_entries; i++)
719 tx_pool->free_map[i] = i;
720
721 tx_pool->consumer_index = 0;
722 tx_pool->producer_index = 0;
723 tx_pool->num_buffers = num_entries;
724 tx_pool->buf_size = buf_size;
725
726 return 0;
727}
728
Nathan Fontenotc657e322017-03-30 02:49:06 -0400729static int init_tx_pools(struct net_device *netdev)
730{
731 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Nathan Fontenotc657e322017-03-30 02:49:06 -0400732 int tx_subcrqs;
Thomas Falcon32053062018-03-16 20:00:27 -0500733 int i, rc;
Nathan Fontenotc657e322017-03-30 02:49:06 -0400734
Thomas Falcon507ebe62020-08-21 13:39:01 -0500735 tx_subcrqs = adapter->num_active_tx_scrqs;
Nathan Fontenotc657e322017-03-30 02:49:06 -0400736 adapter->tx_pool = kcalloc(tx_subcrqs,
737 sizeof(struct ibmvnic_tx_pool), GFP_KERNEL);
738 if (!adapter->tx_pool)
739 return -1;
740
Thomas Falcon32053062018-03-16 20:00:27 -0500741 adapter->tso_pool = kcalloc(tx_subcrqs,
742 sizeof(struct ibmvnic_tx_pool), GFP_KERNEL);
743 if (!adapter->tso_pool)
744 return -1;
745
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600746 adapter->num_active_tx_pools = tx_subcrqs;
747
Nathan Fontenotc657e322017-03-30 02:49:06 -0400748 for (i = 0; i < tx_subcrqs; i++) {
Thomas Falcon32053062018-03-16 20:00:27 -0500749 rc = init_one_tx_pool(netdev, &adapter->tx_pool[i],
750 adapter->req_tx_entries_per_subcrq,
751 adapter->req_mtu + VLAN_HLEN);
752 if (rc) {
Nathan Fontenotc657e322017-03-30 02:49:06 -0400753 release_tx_pools(adapter);
Thomas Falcon32053062018-03-16 20:00:27 -0500754 return rc;
Nathan Fontenotc657e322017-03-30 02:49:06 -0400755 }
756
Thomas Falcon7c940b12019-06-07 16:03:55 -0500757 rc = init_one_tx_pool(netdev, &adapter->tso_pool[i],
758 IBMVNIC_TSO_BUFS,
759 IBMVNIC_TSO_BUF_SZ);
Thomas Falcon32053062018-03-16 20:00:27 -0500760 if (rc) {
Nathan Fontenotc657e322017-03-30 02:49:06 -0400761 release_tx_pools(adapter);
Thomas Falcon32053062018-03-16 20:00:27 -0500762 return rc;
Nathan Fontenotc657e322017-03-30 02:49:06 -0400763 }
Nathan Fontenotc657e322017-03-30 02:49:06 -0400764 }
765
766 return 0;
767}
768
John Allend944c3d62017-05-26 10:30:13 -0400769static void ibmvnic_napi_enable(struct ibmvnic_adapter *adapter)
770{
771 int i;
772
773 if (adapter->napi_enabled)
774 return;
775
776 for (i = 0; i < adapter->req_rx_queues; i++)
777 napi_enable(&adapter->napi[i]);
778
779 adapter->napi_enabled = true;
780}
781
782static void ibmvnic_napi_disable(struct ibmvnic_adapter *adapter)
783{
784 int i;
785
786 if (!adapter->napi_enabled)
787 return;
788
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500789 for (i = 0; i < adapter->req_rx_queues; i++) {
790 netdev_dbg(adapter->netdev, "Disabling napi[%d]\n", i);
John Allend944c3d62017-05-26 10:30:13 -0400791 napi_disable(&adapter->napi[i]);
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500792 }
John Allend944c3d62017-05-26 10:30:13 -0400793
794 adapter->napi_enabled = false;
795}
796
Nathan Fontenot86f669b2018-02-19 13:30:39 -0600797static int init_napi(struct ibmvnic_adapter *adapter)
798{
799 int i;
800
801 adapter->napi = kcalloc(adapter->req_rx_queues,
802 sizeof(struct napi_struct), GFP_KERNEL);
803 if (!adapter->napi)
804 return -ENOMEM;
805
806 for (i = 0; i < adapter->req_rx_queues; i++) {
807 netdev_dbg(adapter->netdev, "Adding napi[%d]\n", i);
808 netif_napi_add(adapter->netdev, &adapter->napi[i],
809 ibmvnic_poll, NAPI_POLL_WEIGHT);
810 }
811
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600812 adapter->num_active_rx_napi = adapter->req_rx_queues;
Nathan Fontenot86f669b2018-02-19 13:30:39 -0600813 return 0;
814}
815
816static void release_napi(struct ibmvnic_adapter *adapter)
817{
818 int i;
819
820 if (!adapter->napi)
821 return;
822
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600823 for (i = 0; i < adapter->num_active_rx_napi; i++) {
Wen Yang390de192018-12-11 12:20:46 +0800824 netdev_dbg(adapter->netdev, "Releasing napi[%d]\n", i);
825 netif_napi_del(&adapter->napi[i]);
Nathan Fontenot86f669b2018-02-19 13:30:39 -0600826 }
827
828 kfree(adapter->napi);
829 adapter->napi = NULL;
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600830 adapter->num_active_rx_napi = 0;
Thomas Falconc3f22412018-05-23 13:37:55 -0500831 adapter->napi_enabled = false;
Nathan Fontenot86f669b2018-02-19 13:30:39 -0600832}
833
John Allena57a5d22017-03-17 17:13:41 -0500834static int ibmvnic_login(struct net_device *netdev)
Thomas Falcon032c5e82015-12-21 11:26:06 -0600835{
836 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
John Allenbd0b6722017-03-17 17:13:40 -0500837 unsigned long timeout = msecs_to_jiffies(30000);
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500838 int retry_count = 0;
Thomas Falcondff515a32020-06-15 10:29:23 -0500839 int retries = 10;
Thomas Falconeb110412018-05-24 14:37:53 -0500840 bool retry;
Thomas Falcon4d96f122017-08-01 15:04:36 -0500841 int rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600842
John Allenbd0b6722017-03-17 17:13:40 -0500843 do {
Thomas Falconeb110412018-05-24 14:37:53 -0500844 retry = false;
Thomas Falcondff515a32020-06-15 10:29:23 -0500845 if (retry_count > retries) {
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500846 netdev_warn(netdev, "Login attempts exceeded\n");
847 return -1;
848 }
849
850 adapter->init_done_rc = 0;
851 reinit_completion(&adapter->init_done);
852 rc = send_login(adapter);
853 if (rc) {
854 netdev_warn(netdev, "Unable to login\n");
855 return rc;
856 }
857
858 if (!wait_for_completion_timeout(&adapter->init_done,
859 timeout)) {
Thomas Falcondff515a32020-06-15 10:29:23 -0500860 netdev_warn(netdev, "Login timed out, retrying...\n");
861 retry = true;
862 adapter->init_done_rc = 0;
863 retry_count++;
864 continue;
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500865 }
866
Thomas Falcondff515a32020-06-15 10:29:23 -0500867 if (adapter->init_done_rc == ABORTED) {
868 netdev_warn(netdev, "Login aborted, retrying...\n");
869 retry = true;
870 adapter->init_done_rc = 0;
871 retry_count++;
872 /* FW or device may be busy, so
873 * wait a bit before retrying login
874 */
875 msleep(500);
876 } else if (adapter->init_done_rc == PARTIALSUCCESS) {
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500877 retry_count++;
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -0600878 release_sub_crqs(adapter, 1);
John Allenbd0b6722017-03-17 17:13:40 -0500879
Thomas Falconeb110412018-05-24 14:37:53 -0500880 retry = true;
881 netdev_dbg(netdev,
882 "Received partial success, retrying...\n");
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500883 adapter->init_done_rc = 0;
John Allenbd0b6722017-03-17 17:13:40 -0500884 reinit_completion(&adapter->init_done);
885 send_cap_queries(adapter);
886 if (!wait_for_completion_timeout(&adapter->init_done,
887 timeout)) {
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500888 netdev_warn(netdev,
889 "Capabilities query timed out\n");
John Allenbd0b6722017-03-17 17:13:40 -0500890 return -1;
891 }
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500892
Thomas Falcon4d96f122017-08-01 15:04:36 -0500893 rc = init_sub_crqs(adapter);
894 if (rc) {
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500895 netdev_warn(netdev,
896 "SCRQ initialization failed\n");
Thomas Falcon4d96f122017-08-01 15:04:36 -0500897 return -1;
898 }
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500899
Thomas Falcon4d96f122017-08-01 15:04:36 -0500900 rc = init_sub_crq_irqs(adapter);
901 if (rc) {
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500902 netdev_warn(netdev,
903 "SCRQ irq initialization failed\n");
Thomas Falcon4d96f122017-08-01 15:04:36 -0500904 return -1;
905 }
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500906 } else if (adapter->init_done_rc) {
907 netdev_warn(netdev, "Adapter login failed\n");
John Allenbd0b6722017-03-17 17:13:40 -0500908 return -1;
909 }
Thomas Falconeb110412018-05-24 14:37:53 -0500910 } while (retry);
John Allenbd0b6722017-03-17 17:13:40 -0500911
Thomas Falcon62740e92019-05-09 23:13:43 -0500912 __ibmvnic_set_mac(netdev, adapter->mac_addr);
Thomas Falcon3d166132018-01-10 19:39:52 -0600913
John Allena57a5d22017-03-17 17:13:41 -0500914 return 0;
915}
916
Thomas Falcon34f0f4e2018-02-13 18:23:40 -0600917static void release_login_buffer(struct ibmvnic_adapter *adapter)
918{
919 kfree(adapter->login_buf);
920 adapter->login_buf = NULL;
921}
922
923static void release_login_rsp_buffer(struct ibmvnic_adapter *adapter)
924{
925 kfree(adapter->login_rsp_buf);
926 adapter->login_rsp_buf = NULL;
927}
928
Nathan Fontenot1b8955e2017-03-30 02:49:29 -0400929static void release_resources(struct ibmvnic_adapter *adapter)
930{
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -0200931 release_vpd_data(adapter);
932
Nathan Fontenot1b8955e2017-03-30 02:49:29 -0400933 release_tx_pools(adapter);
934 release_rx_pools(adapter);
935
Nathan Fontenot86f669b2018-02-19 13:30:39 -0600936 release_napi(adapter);
Thomas Falcon34f0f4e2018-02-13 18:23:40 -0600937 release_login_rsp_buffer(adapter);
Nathan Fontenot1b8955e2017-03-30 02:49:29 -0400938}
939
Nathan Fontenot53da09e2017-04-21 15:39:04 -0400940static int set_link_state(struct ibmvnic_adapter *adapter, u8 link_state)
941{
942 struct net_device *netdev = adapter->netdev;
943 unsigned long timeout = msecs_to_jiffies(30000);
944 union ibmvnic_crq crq;
945 bool resend;
946 int rc;
947
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500948 netdev_dbg(netdev, "setting link state %d\n", link_state);
949
Nathan Fontenot53da09e2017-04-21 15:39:04 -0400950 memset(&crq, 0, sizeof(crq));
951 crq.logical_link_state.first = IBMVNIC_CRQ_CMD;
952 crq.logical_link_state.cmd = LOGICAL_LINK_STATE;
953 crq.logical_link_state.link_state = link_state;
954
955 do {
956 resend = false;
957
958 reinit_completion(&adapter->init_done);
959 rc = ibmvnic_send_crq(adapter, &crq);
960 if (rc) {
961 netdev_err(netdev, "Failed to set link state\n");
962 return rc;
963 }
964
965 if (!wait_for_completion_timeout(&adapter->init_done,
966 timeout)) {
967 netdev_err(netdev, "timeout setting link state\n");
968 return -1;
969 }
970
Lijun Pan4c5f6af2020-08-19 17:52:23 -0500971 if (adapter->init_done_rc == PARTIALSUCCESS) {
Nathan Fontenot53da09e2017-04-21 15:39:04 -0400972 /* Partuial success, delay and re-send */
973 mdelay(1000);
974 resend = true;
Thomas Falconab5ec332018-05-23 13:37:59 -0500975 } else if (adapter->init_done_rc) {
976 netdev_warn(netdev, "Unable to set link state, rc=%d\n",
977 adapter->init_done_rc);
978 return adapter->init_done_rc;
Nathan Fontenot53da09e2017-04-21 15:39:04 -0400979 }
980 } while (resend);
981
982 return 0;
983}
984
Thomas Falcon7f3c6e62017-04-21 15:38:40 -0400985static int set_real_num_queues(struct net_device *netdev)
986{
987 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
988 int rc;
989
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500990 netdev_dbg(netdev, "Setting real tx/rx queues (%llx/%llx)\n",
991 adapter->req_tx_queues, adapter->req_rx_queues);
992
Thomas Falcon7f3c6e62017-04-21 15:38:40 -0400993 rc = netif_set_real_num_tx_queues(netdev, adapter->req_tx_queues);
994 if (rc) {
995 netdev_err(netdev, "failed to set the number of tx queues\n");
996 return rc;
997 }
998
999 rc = netif_set_real_num_rx_queues(netdev, adapter->req_rx_queues);
1000 if (rc)
1001 netdev_err(netdev, "failed to set the number of rx queues\n");
1002
1003 return rc;
1004}
1005
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001006static int ibmvnic_get_vpd(struct ibmvnic_adapter *adapter)
1007{
1008 struct device *dev = &adapter->vdev->dev;
1009 union ibmvnic_crq crq;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001010 int len = 0;
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05001011 int rc;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001012
1013 if (adapter->vpd->buff)
1014 len = adapter->vpd->len;
1015
Thomas Falconff25dcb2019-11-25 17:12:56 -06001016 mutex_lock(&adapter->fw_lock);
1017 adapter->fw_done_rc = 0;
Thomas Falcon070eca92019-11-25 17:12:53 -06001018 reinit_completion(&adapter->fw_done);
Thomas Falconff25dcb2019-11-25 17:12:56 -06001019
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001020 crq.get_vpd_size.first = IBMVNIC_CRQ_CMD;
1021 crq.get_vpd_size.cmd = GET_VPD_SIZE;
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05001022 rc = ibmvnic_send_crq(adapter, &crq);
Thomas Falconff25dcb2019-11-25 17:12:56 -06001023 if (rc) {
1024 mutex_unlock(&adapter->fw_lock);
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05001025 return rc;
Thomas Falconff25dcb2019-11-25 17:12:56 -06001026 }
Thomas Falcon476d96c2019-11-25 17:12:55 -06001027
1028 rc = ibmvnic_wait_for_completion(adapter, &adapter->fw_done, 10000);
1029 if (rc) {
1030 dev_err(dev, "Could not retrieve VPD size, rc = %d\n", rc);
Thomas Falconff25dcb2019-11-25 17:12:56 -06001031 mutex_unlock(&adapter->fw_lock);
Thomas Falcon476d96c2019-11-25 17:12:55 -06001032 return rc;
1033 }
Thomas Falconff25dcb2019-11-25 17:12:56 -06001034 mutex_unlock(&adapter->fw_lock);
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001035
1036 if (!adapter->vpd->len)
1037 return -ENODATA;
1038
1039 if (!adapter->vpd->buff)
1040 adapter->vpd->buff = kzalloc(adapter->vpd->len, GFP_KERNEL);
1041 else if (adapter->vpd->len != len)
1042 adapter->vpd->buff =
1043 krealloc(adapter->vpd->buff,
1044 adapter->vpd->len, GFP_KERNEL);
1045
1046 if (!adapter->vpd->buff) {
1047 dev_err(dev, "Could allocate VPD buffer\n");
1048 return -ENOMEM;
1049 }
1050
1051 adapter->vpd->dma_addr =
1052 dma_map_single(dev, adapter->vpd->buff, adapter->vpd->len,
1053 DMA_FROM_DEVICE);
Desnes Augusto Nunes do Rosariof7431062017-11-17 09:09:04 -02001054 if (dma_mapping_error(dev, adapter->vpd->dma_addr)) {
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001055 dev_err(dev, "Could not map VPD buffer\n");
1056 kfree(adapter->vpd->buff);
Thomas Falconb0992ec2018-02-06 17:25:23 -06001057 adapter->vpd->buff = NULL;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001058 return -ENOMEM;
1059 }
1060
Thomas Falconff25dcb2019-11-25 17:12:56 -06001061 mutex_lock(&adapter->fw_lock);
1062 adapter->fw_done_rc = 0;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001063 reinit_completion(&adapter->fw_done);
Thomas Falconff25dcb2019-11-25 17:12:56 -06001064
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001065 crq.get_vpd.first = IBMVNIC_CRQ_CMD;
1066 crq.get_vpd.cmd = GET_VPD;
1067 crq.get_vpd.ioba = cpu_to_be32(adapter->vpd->dma_addr);
1068 crq.get_vpd.len = cpu_to_be32((u32)adapter->vpd->len);
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05001069 rc = ibmvnic_send_crq(adapter, &crq);
1070 if (rc) {
1071 kfree(adapter->vpd->buff);
1072 adapter->vpd->buff = NULL;
Thomas Falconff25dcb2019-11-25 17:12:56 -06001073 mutex_unlock(&adapter->fw_lock);
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05001074 return rc;
1075 }
Thomas Falcon476d96c2019-11-25 17:12:55 -06001076
1077 rc = ibmvnic_wait_for_completion(adapter, &adapter->fw_done, 10000);
1078 if (rc) {
1079 dev_err(dev, "Unable to retrieve VPD, rc = %d\n", rc);
1080 kfree(adapter->vpd->buff);
1081 adapter->vpd->buff = NULL;
Thomas Falconff25dcb2019-11-25 17:12:56 -06001082 mutex_unlock(&adapter->fw_lock);
Thomas Falcon476d96c2019-11-25 17:12:55 -06001083 return rc;
1084 }
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001085
Thomas Falconff25dcb2019-11-25 17:12:56 -06001086 mutex_unlock(&adapter->fw_lock);
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001087 return 0;
1088}
1089
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001090static int init_resources(struct ibmvnic_adapter *adapter)
John Allena57a5d22017-03-17 17:13:41 -05001091{
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001092 struct net_device *netdev = adapter->netdev;
Nathan Fontenot86f669b2018-02-19 13:30:39 -06001093 int rc;
John Allena57a5d22017-03-17 17:13:41 -05001094
Thomas Falcon7f3c6e62017-04-21 15:38:40 -04001095 rc = set_real_num_queues(netdev);
1096 if (rc)
1097 return rc;
John Allenbd0b6722017-03-17 17:13:40 -05001098
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001099 adapter->vpd = kzalloc(sizeof(*adapter->vpd), GFP_KERNEL);
1100 if (!adapter->vpd)
1101 return -ENOMEM;
1102
John Allen69d08dc2018-01-18 16:27:58 -06001103 /* Vital Product Data (VPD) */
1104 rc = ibmvnic_get_vpd(adapter);
1105 if (rc) {
1106 netdev_err(netdev, "failed to initialize Vital Product Data (VPD)\n");
1107 return rc;
1108 }
1109
Thomas Falcon032c5e82015-12-21 11:26:06 -06001110 adapter->map_id = 1;
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001111
Nathan Fontenot86f669b2018-02-19 13:30:39 -06001112 rc = init_napi(adapter);
1113 if (rc)
1114 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001115
Thomas Falcon032c5e82015-12-21 11:26:06 -06001116 send_map_query(adapter);
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -04001117
1118 rc = init_rx_pools(netdev);
1119 if (rc)
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001120 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001121
Nathan Fontenotc657e322017-03-30 02:49:06 -04001122 rc = init_tx_pools(netdev);
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001123 return rc;
1124}
1125
Nathan Fontenoted651a12017-05-03 14:04:38 -04001126static int __ibmvnic_open(struct net_device *netdev)
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001127{
1128 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001129 enum vnic_state prev_state = adapter->state;
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001130 int i, rc;
1131
Nathan Fontenot90c80142017-05-03 14:04:32 -04001132 adapter->state = VNIC_OPENING;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001133 replenish_pools(adapter);
John Allend944c3d62017-05-26 10:30:13 -04001134 ibmvnic_napi_enable(adapter);
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001135
Thomas Falcon032c5e82015-12-21 11:26:06 -06001136 /* We're ready to receive frames, enable the sub-crq interrupts and
1137 * set the logical link state to up
1138 */
Nathan Fontenoted651a12017-05-03 14:04:38 -04001139 for (i = 0; i < adapter->req_rx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001140 netdev_dbg(netdev, "Enabling rx_scrq[%d] irq\n", i);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001141 if (prev_state == VNIC_CLOSED)
1142 enable_irq(adapter->rx_scrq[i]->irq);
Thomas Falconf23e0642018-04-15 18:53:36 -05001143 enable_scrq_irq(adapter, adapter->rx_scrq[i]);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001144 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001145
Nathan Fontenoted651a12017-05-03 14:04:38 -04001146 for (i = 0; i < adapter->req_tx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001147 netdev_dbg(netdev, "Enabling tx_scrq[%d] irq\n", i);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001148 if (prev_state == VNIC_CLOSED)
1149 enable_irq(adapter->tx_scrq[i]->irq);
Thomas Falconf23e0642018-04-15 18:53:36 -05001150 enable_scrq_irq(adapter, adapter->tx_scrq[i]);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001151 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001152
Nathan Fontenot53da09e2017-04-21 15:39:04 -04001153 rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_UP);
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001154 if (rc) {
1155 for (i = 0; i < adapter->req_rx_queues; i++)
1156 napi_disable(&adapter->napi[i]);
1157 release_resources(adapter);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001158 return rc;
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001159 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001160
Nathan Fontenoted651a12017-05-03 14:04:38 -04001161 netif_tx_start_all_queues(netdev);
1162
1163 if (prev_state == VNIC_CLOSED) {
1164 for (i = 0; i < adapter->req_rx_queues; i++)
1165 napi_schedule(&adapter->napi[i]);
1166 }
1167
1168 adapter->state = VNIC_OPEN;
1169 return rc;
1170}
1171
1172static int ibmvnic_open(struct net_device *netdev)
1173{
1174 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
John Allen69d08dc2018-01-18 16:27:58 -06001175 int rc;
Nathan Fontenoted651a12017-05-03 14:04:38 -04001176
Thomas Falcon5a18e1e2018-04-06 18:37:05 -05001177 /* If device failover is pending, just set device state and return.
1178 * Device operation will be handled by reset routine.
1179 */
1180 if (adapter->failover_pending) {
1181 adapter->state = VNIC_OPEN;
1182 return 0;
1183 }
1184
Nathan Fontenoted651a12017-05-03 14:04:38 -04001185 if (adapter->state != VNIC_CLOSED) {
1186 rc = ibmvnic_login(netdev);
Juliet Kima5681e22018-11-19 15:59:22 -06001187 if (rc)
Nathan Fontenoted651a12017-05-03 14:04:38 -04001188 return rc;
Nathan Fontenoted651a12017-05-03 14:04:38 -04001189
1190 rc = init_resources(adapter);
1191 if (rc) {
1192 netdev_err(netdev, "failed to initialize resources\n");
1193 release_resources(adapter);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001194 return rc;
1195 }
1196 }
1197
1198 rc = __ibmvnic_open(netdev);
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001199
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001200 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001201}
1202
Thomas Falcond0869c02018-02-13 18:23:43 -06001203static void clean_rx_pools(struct ibmvnic_adapter *adapter)
1204{
1205 struct ibmvnic_rx_pool *rx_pool;
Thomas Falcon637f81d2018-02-26 18:10:57 -06001206 struct ibmvnic_rx_buff *rx_buff;
Thomas Falcond0869c02018-02-13 18:23:43 -06001207 u64 rx_entries;
1208 int rx_scrqs;
1209 int i, j;
1210
1211 if (!adapter->rx_pool)
1212 return;
1213
Thomas Falcon660e3092018-04-20 14:25:32 -05001214 rx_scrqs = adapter->num_active_rx_pools;
Thomas Falcond0869c02018-02-13 18:23:43 -06001215 rx_entries = adapter->req_rx_add_entries_per_subcrq;
1216
1217 /* Free any remaining skbs in the rx buffer pools */
1218 for (i = 0; i < rx_scrqs; i++) {
1219 rx_pool = &adapter->rx_pool[i];
Thomas Falcon637f81d2018-02-26 18:10:57 -06001220 if (!rx_pool || !rx_pool->rx_buff)
Thomas Falcond0869c02018-02-13 18:23:43 -06001221 continue;
1222
1223 netdev_dbg(adapter->netdev, "Cleaning rx_pool[%d]\n", i);
1224 for (j = 0; j < rx_entries; j++) {
Thomas Falcon637f81d2018-02-26 18:10:57 -06001225 rx_buff = &rx_pool->rx_buff[j];
1226 if (rx_buff && rx_buff->skb) {
1227 dev_kfree_skb_any(rx_buff->skb);
1228 rx_buff->skb = NULL;
Thomas Falcond0869c02018-02-13 18:23:43 -06001229 }
1230 }
1231 }
1232}
1233
Thomas Falcone9e1e972018-03-16 20:00:30 -05001234static void clean_one_tx_pool(struct ibmvnic_adapter *adapter,
1235 struct ibmvnic_tx_pool *tx_pool)
Nathan Fontenotb41b83e2017-05-03 14:04:56 -04001236{
Thomas Falcon637f81d2018-02-26 18:10:57 -06001237 struct ibmvnic_tx_buff *tx_buff;
Nathan Fontenotb41b83e2017-05-03 14:04:56 -04001238 u64 tx_entries;
Thomas Falcone9e1e972018-03-16 20:00:30 -05001239 int i;
Nathan Fontenotb41b83e2017-05-03 14:04:56 -04001240
Dan Carpenter050e85c2018-03-23 14:36:15 +03001241 if (!tx_pool || !tx_pool->tx_buff)
Thomas Falcone9e1e972018-03-16 20:00:30 -05001242 return;
1243
1244 tx_entries = tx_pool->num_buffers;
1245
1246 for (i = 0; i < tx_entries; i++) {
1247 tx_buff = &tx_pool->tx_buff[i];
1248 if (tx_buff && tx_buff->skb) {
1249 dev_kfree_skb_any(tx_buff->skb);
1250 tx_buff->skb = NULL;
1251 }
1252 }
1253}
1254
1255static void clean_tx_pools(struct ibmvnic_adapter *adapter)
1256{
1257 int tx_scrqs;
1258 int i;
1259
1260 if (!adapter->tx_pool || !adapter->tso_pool)
Nathan Fontenotb41b83e2017-05-03 14:04:56 -04001261 return;
1262
Thomas Falcon660e3092018-04-20 14:25:32 -05001263 tx_scrqs = adapter->num_active_tx_pools;
Nathan Fontenotb41b83e2017-05-03 14:04:56 -04001264
1265 /* Free any remaining skbs in the tx buffer pools */
1266 for (i = 0; i < tx_scrqs; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001267 netdev_dbg(adapter->netdev, "Cleaning tx_pool[%d]\n", i);
Thomas Falcone9e1e972018-03-16 20:00:30 -05001268 clean_one_tx_pool(adapter, &adapter->tx_pool[i]);
1269 clean_one_tx_pool(adapter, &adapter->tso_pool[i]);
Nathan Fontenotb41b83e2017-05-03 14:04:56 -04001270 }
1271}
1272
John Allen6095e592018-03-30 13:44:21 -05001273static void ibmvnic_disable_irqs(struct ibmvnic_adapter *adapter)
John Allenea5509f2017-03-17 17:13:43 -05001274{
John Allen6095e592018-03-30 13:44:21 -05001275 struct net_device *netdev = adapter->netdev;
John Allenea5509f2017-03-17 17:13:43 -05001276 int i;
1277
Nathan Fontenot46293b92017-05-03 14:05:02 -04001278 if (adapter->tx_scrq) {
1279 for (i = 0; i < adapter->req_tx_queues; i++)
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001280 if (adapter->tx_scrq[i]->irq) {
Thomas Falconf8738662018-03-07 17:51:45 -06001281 netdev_dbg(netdev,
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001282 "Disabling tx_scrq[%d] irq\n", i);
Thomas Falconf23e0642018-04-15 18:53:36 -05001283 disable_scrq_irq(adapter, adapter->tx_scrq[i]);
Nathan Fontenot46293b92017-05-03 14:05:02 -04001284 disable_irq(adapter->tx_scrq[i]->irq);
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001285 }
Nathan Fontenot46293b92017-05-03 14:05:02 -04001286 }
1287
Nathan Fontenot46293b92017-05-03 14:05:02 -04001288 if (adapter->rx_scrq) {
1289 for (i = 0; i < adapter->req_rx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001290 if (adapter->rx_scrq[i]->irq) {
Thomas Falconf8738662018-03-07 17:51:45 -06001291 netdev_dbg(netdev,
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001292 "Disabling rx_scrq[%d] irq\n", i);
Thomas Falconf23e0642018-04-15 18:53:36 -05001293 disable_scrq_irq(adapter, adapter->rx_scrq[i]);
Nathan Fontenot46293b92017-05-03 14:05:02 -04001294 disable_irq(adapter->rx_scrq[i]->irq);
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001295 }
Nathan Fontenot46293b92017-05-03 14:05:02 -04001296 }
1297 }
John Allen6095e592018-03-30 13:44:21 -05001298}
1299
1300static void ibmvnic_cleanup(struct net_device *netdev)
1301{
1302 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1303
1304 /* ensure that transmissions are stopped if called by do_reset */
Juliet Kim7ed5b312019-09-20 16:11:23 -04001305 if (test_bit(0, &adapter->resetting))
John Allen6095e592018-03-30 13:44:21 -05001306 netif_tx_disable(netdev);
1307 else
1308 netif_tx_stop_all_queues(netdev);
1309
1310 ibmvnic_napi_disable(adapter);
1311 ibmvnic_disable_irqs(adapter);
1312
Thomas Falcond0869c02018-02-13 18:23:43 -06001313 clean_rx_pools(adapter);
Thomas Falcon10f76212017-05-26 10:30:31 -04001314 clean_tx_pools(adapter);
Thomas Falcon01d9bd72018-03-07 17:51:46 -06001315}
1316
1317static int __ibmvnic_close(struct net_device *netdev)
1318{
1319 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1320 int rc = 0;
1321
1322 adapter->state = VNIC_CLOSING;
1323 rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_DN);
1324 if (rc)
1325 return rc;
Nathan Fontenot90c80142017-05-03 14:04:32 -04001326 adapter->state = VNIC_CLOSED;
Thomas Falcon01d9bd72018-03-07 17:51:46 -06001327 return 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001328}
1329
Nathan Fontenoted651a12017-05-03 14:04:38 -04001330static int ibmvnic_close(struct net_device *netdev)
1331{
1332 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1333 int rc;
1334
Thomas Falcon5a18e1e2018-04-06 18:37:05 -05001335 /* If device failover is pending, just set device state and return.
1336 * Device operation will be handled by reset routine.
1337 */
1338 if (adapter->failover_pending) {
1339 adapter->state = VNIC_CLOSED;
1340 return 0;
1341 }
1342
Nathan Fontenoted651a12017-05-03 14:04:38 -04001343 rc = __ibmvnic_close(netdev);
Nathan Fontenot30f79622018-04-06 18:37:06 -05001344 ibmvnic_cleanup(netdev);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001345
1346 return rc;
1347}
1348
Thomas Falconad7775d2016-04-01 17:20:34 -05001349/**
1350 * build_hdr_data - creates L2/L3/L4 header data buffer
1351 * @hdr_field - bitfield determining needed headers
1352 * @skb - socket buffer
1353 * @hdr_len - array of header lengths
1354 * @tot_len - total length of data
1355 *
1356 * Reads hdr_field to determine which headers are needed by firmware.
1357 * Builds a buffer containing these headers. Saves individual header
1358 * lengths and total buffer length to be used to build descriptors.
1359 */
1360static int build_hdr_data(u8 hdr_field, struct sk_buff *skb,
1361 int *hdr_len, u8 *hdr_data)
1362{
1363 int len = 0;
1364 u8 *hdr;
1365
Thomas Falconda75e3b2018-03-12 11:51:02 -05001366 if (skb_vlan_tagged(skb) && !skb_vlan_tag_present(skb))
1367 hdr_len[0] = sizeof(struct vlan_ethhdr);
1368 else
1369 hdr_len[0] = sizeof(struct ethhdr);
Thomas Falconad7775d2016-04-01 17:20:34 -05001370
1371 if (skb->protocol == htons(ETH_P_IP)) {
1372 hdr_len[1] = ip_hdr(skb)->ihl * 4;
1373 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
1374 hdr_len[2] = tcp_hdrlen(skb);
1375 else if (ip_hdr(skb)->protocol == IPPROTO_UDP)
1376 hdr_len[2] = sizeof(struct udphdr);
1377 } else if (skb->protocol == htons(ETH_P_IPV6)) {
1378 hdr_len[1] = sizeof(struct ipv6hdr);
1379 if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
1380 hdr_len[2] = tcp_hdrlen(skb);
1381 else if (ipv6_hdr(skb)->nexthdr == IPPROTO_UDP)
1382 hdr_len[2] = sizeof(struct udphdr);
Thomas Falcon4eb50ce2017-12-18 12:52:40 -06001383 } else if (skb->protocol == htons(ETH_P_ARP)) {
1384 hdr_len[1] = arp_hdr_len(skb->dev);
1385 hdr_len[2] = 0;
Thomas Falconad7775d2016-04-01 17:20:34 -05001386 }
1387
1388 memset(hdr_data, 0, 120);
1389 if ((hdr_field >> 6) & 1) {
1390 hdr = skb_mac_header(skb);
1391 memcpy(hdr_data, hdr, hdr_len[0]);
1392 len += hdr_len[0];
1393 }
1394
1395 if ((hdr_field >> 5) & 1) {
1396 hdr = skb_network_header(skb);
1397 memcpy(hdr_data + len, hdr, hdr_len[1]);
1398 len += hdr_len[1];
1399 }
1400
1401 if ((hdr_field >> 4) & 1) {
1402 hdr = skb_transport_header(skb);
1403 memcpy(hdr_data + len, hdr, hdr_len[2]);
1404 len += hdr_len[2];
1405 }
1406 return len;
1407}
1408
1409/**
1410 * create_hdr_descs - create header and header extension descriptors
1411 * @hdr_field - bitfield determining needed headers
1412 * @data - buffer containing header data
1413 * @len - length of data buffer
1414 * @hdr_len - array of individual header lengths
1415 * @scrq_arr - descriptor array
1416 *
1417 * Creates header and, if needed, header extension descriptors and
1418 * places them in a descriptor array, scrq_arr
1419 */
1420
Thomas Falcon2de09682017-10-16 10:02:11 -05001421static int create_hdr_descs(u8 hdr_field, u8 *hdr_data, int len, int *hdr_len,
1422 union sub_crq *scrq_arr)
Thomas Falconad7775d2016-04-01 17:20:34 -05001423{
1424 union sub_crq hdr_desc;
1425 int tmp_len = len;
Thomas Falcon2de09682017-10-16 10:02:11 -05001426 int num_descs = 0;
Thomas Falconad7775d2016-04-01 17:20:34 -05001427 u8 *data, *cur;
1428 int tmp;
1429
1430 while (tmp_len > 0) {
1431 cur = hdr_data + len - tmp_len;
1432
1433 memset(&hdr_desc, 0, sizeof(hdr_desc));
1434 if (cur != hdr_data) {
1435 data = hdr_desc.hdr_ext.data;
1436 tmp = tmp_len > 29 ? 29 : tmp_len;
1437 hdr_desc.hdr_ext.first = IBMVNIC_CRQ_CMD;
1438 hdr_desc.hdr_ext.type = IBMVNIC_HDR_EXT_DESC;
1439 hdr_desc.hdr_ext.len = tmp;
1440 } else {
1441 data = hdr_desc.hdr.data;
1442 tmp = tmp_len > 24 ? 24 : tmp_len;
1443 hdr_desc.hdr.first = IBMVNIC_CRQ_CMD;
1444 hdr_desc.hdr.type = IBMVNIC_HDR_DESC;
1445 hdr_desc.hdr.len = tmp;
1446 hdr_desc.hdr.l2_len = (u8)hdr_len[0];
1447 hdr_desc.hdr.l3_len = cpu_to_be16((u16)hdr_len[1]);
1448 hdr_desc.hdr.l4_len = (u8)hdr_len[2];
1449 hdr_desc.hdr.flag = hdr_field << 1;
1450 }
1451 memcpy(data, cur, tmp);
1452 tmp_len -= tmp;
1453 *scrq_arr = hdr_desc;
1454 scrq_arr++;
Thomas Falcon2de09682017-10-16 10:02:11 -05001455 num_descs++;
Thomas Falconad7775d2016-04-01 17:20:34 -05001456 }
Thomas Falcon2de09682017-10-16 10:02:11 -05001457
1458 return num_descs;
Thomas Falconad7775d2016-04-01 17:20:34 -05001459}
1460
1461/**
1462 * build_hdr_descs_arr - build a header descriptor array
1463 * @skb - socket buffer
1464 * @num_entries - number of descriptors to be sent
1465 * @subcrq - first TX descriptor
1466 * @hdr_field - bit field determining which headers will be sent
1467 *
1468 * This function will build a TX descriptor array with applicable
1469 * L2/L3/L4 packet header descriptors to be sent by send_subcrq_indirect.
1470 */
1471
1472static void build_hdr_descs_arr(struct ibmvnic_tx_buff *txbuff,
1473 int *num_entries, u8 hdr_field)
1474{
1475 int hdr_len[3] = {0, 0, 0};
Thomas Falcon2de09682017-10-16 10:02:11 -05001476 int tot_len;
Thomas Falconad7775d2016-04-01 17:20:34 -05001477 u8 *hdr_data = txbuff->hdr_data;
1478
1479 tot_len = build_hdr_data(hdr_field, txbuff->skb, hdr_len,
1480 txbuff->hdr_data);
Thomas Falcon2de09682017-10-16 10:02:11 -05001481 *num_entries += create_hdr_descs(hdr_field, hdr_data, tot_len, hdr_len,
Thomas Falconad7775d2016-04-01 17:20:34 -05001482 txbuff->indir_arr + 1);
1483}
1484
Thomas Falcon1f247a62018-03-12 11:51:04 -05001485static int ibmvnic_xmit_workarounds(struct sk_buff *skb,
1486 struct net_device *netdev)
1487{
1488 /* For some backing devices, mishandling of small packets
1489 * can result in a loss of connection or TX stall. Device
1490 * architects recommend that no packet should be smaller
1491 * than the minimum MTU value provided to the driver, so
1492 * pad any packets to that length
1493 */
1494 if (skb->len < netdev->min_mtu)
1495 return skb_put_padto(skb, netdev->min_mtu);
Thomas Falcon7083a452018-03-12 21:05:26 -05001496
1497 return 0;
Thomas Falcon1f247a62018-03-12 11:51:04 -05001498}
1499
YueHaibing94b2bb22018-09-18 14:35:47 +08001500static netdev_tx_t ibmvnic_xmit(struct sk_buff *skb, struct net_device *netdev)
Thomas Falcon032c5e82015-12-21 11:26:06 -06001501{
1502 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1503 int queue_num = skb_get_queue_mapping(skb);
Thomas Falconad7775d2016-04-01 17:20:34 -05001504 u8 *hdrs = (u8 *)&adapter->tx_rx_desc_req;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001505 struct device *dev = &adapter->vdev->dev;
1506 struct ibmvnic_tx_buff *tx_buff = NULL;
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001507 struct ibmvnic_sub_crq_queue *tx_scrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001508 struct ibmvnic_tx_pool *tx_pool;
1509 unsigned int tx_send_failed = 0;
1510 unsigned int tx_map_failed = 0;
1511 unsigned int tx_dropped = 0;
1512 unsigned int tx_packets = 0;
1513 unsigned int tx_bytes = 0;
1514 dma_addr_t data_dma_addr;
1515 struct netdev_queue *txq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001516 unsigned long lpar_rc;
1517 union sub_crq tx_crq;
1518 unsigned int offset;
Thomas Falconad7775d2016-04-01 17:20:34 -05001519 int num_entries = 1;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001520 unsigned char *dst;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001521 int index = 0;
Thomas Falcona0dca102018-01-18 19:29:48 -06001522 u8 proto = 0;
Cristobal Fornof3ae59c2020-08-19 13:16:23 -05001523 u64 handle;
YueHaibing94b2bb22018-09-18 14:35:47 +08001524 netdev_tx_t ret = NETDEV_TX_OK;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001525
Juliet Kim7ed5b312019-09-20 16:11:23 -04001526 if (test_bit(0, &adapter->resetting)) {
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001527 if (!netif_subqueue_stopped(netdev, skb))
1528 netif_stop_subqueue(netdev, queue_num);
1529 dev_kfree_skb_any(skb);
1530
Thomas Falcon032c5e82015-12-21 11:26:06 -06001531 tx_send_failed++;
1532 tx_dropped++;
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001533 ret = NETDEV_TX_OK;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001534 goto out;
1535 }
1536
Thomas Falcon7083a452018-03-12 21:05:26 -05001537 if (ibmvnic_xmit_workarounds(skb, netdev)) {
Thomas Falcon1f247a62018-03-12 11:51:04 -05001538 tx_dropped++;
1539 tx_send_failed++;
1540 ret = NETDEV_TX_OK;
1541 goto out;
1542 }
Thomas Falcon06b3e352018-03-16 20:00:28 -05001543 if (skb_is_gso(skb))
1544 tx_pool = &adapter->tso_pool[queue_num];
1545 else
1546 tx_pool = &adapter->tx_pool[queue_num];
Thomas Falcon1f247a62018-03-12 11:51:04 -05001547
Nathan Fontenot161b8a82017-05-03 14:05:08 -04001548 tx_scrq = adapter->tx_scrq[queue_num];
1549 txq = netdev_get_tx_queue(netdev, skb_get_queue_mapping(skb));
Cristobal Fornof3ae59c2020-08-19 13:16:23 -05001550 handle = tx_scrq->handle;
Nathan Fontenot161b8a82017-05-03 14:05:08 -04001551
Thomas Falcon032c5e82015-12-21 11:26:06 -06001552 index = tx_pool->free_map[tx_pool->consumer_index];
Thomas Falconfdb06102017-10-17 12:36:55 -05001553
Thomas Falcon86b61a52018-03-16 20:00:29 -05001554 if (index == IBMVNIC_INVALID_MAP) {
1555 dev_kfree_skb_any(skb);
1556 tx_send_failed++;
1557 tx_dropped++;
1558 ret = NETDEV_TX_OK;
1559 goto out;
1560 }
1561
1562 tx_pool->free_map[tx_pool->consumer_index] = IBMVNIC_INVALID_MAP;
1563
Thomas Falcon06b3e352018-03-16 20:00:28 -05001564 offset = index * tx_pool->buf_size;
1565 dst = tx_pool->long_term_buff.buff + offset;
1566 memset(dst, 0, tx_pool->buf_size);
1567 data_dma_addr = tx_pool->long_term_buff.addr + offset;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001568
Thomas Falcon15482052017-10-17 12:36:54 -05001569 if (skb_shinfo(skb)->nr_frags) {
1570 int cur, i;
1571
1572 /* Copy the head */
1573 skb_copy_from_linear_data(skb, dst, skb_headlen(skb));
1574 cur = skb_headlen(skb);
1575
1576 /* Copy the frags */
1577 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1578 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1579
1580 memcpy(dst + cur,
1581 page_address(skb_frag_page(frag)) +
Jonathan Lemonb54c9d52019-07-30 07:40:33 -07001582 skb_frag_off(frag), skb_frag_size(frag));
Thomas Falcon15482052017-10-17 12:36:54 -05001583 cur += skb_frag_size(frag);
1584 }
1585 } else {
1586 skb_copy_from_linear_data(skb, dst, skb->len);
1587 }
1588
Thomas Falcon032c5e82015-12-21 11:26:06 -06001589 tx_pool->consumer_index =
Thomas Falcon06b3e352018-03-16 20:00:28 -05001590 (tx_pool->consumer_index + 1) % tx_pool->num_buffers;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001591
1592 tx_buff = &tx_pool->tx_buff[index];
1593 tx_buff->skb = skb;
1594 tx_buff->data_dma[0] = data_dma_addr;
1595 tx_buff->data_len[0] = skb->len;
1596 tx_buff->index = index;
1597 tx_buff->pool_index = queue_num;
1598 tx_buff->last_frag = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001599
1600 memset(&tx_crq, 0, sizeof(tx_crq));
1601 tx_crq.v1.first = IBMVNIC_CRQ_CMD;
1602 tx_crq.v1.type = IBMVNIC_TX_DESC;
1603 tx_crq.v1.n_crq_elem = 1;
1604 tx_crq.v1.n_sge = 1;
1605 tx_crq.v1.flags1 = IBMVNIC_TX_COMP_NEEDED;
Thomas Falcon06b3e352018-03-16 20:00:28 -05001606
Thomas Falconfdb06102017-10-17 12:36:55 -05001607 if (skb_is_gso(skb))
Thomas Falcon06b3e352018-03-16 20:00:28 -05001608 tx_crq.v1.correlator =
1609 cpu_to_be32(index | IBMVNIC_TSO_POOL_MASK);
Thomas Falconfdb06102017-10-17 12:36:55 -05001610 else
Thomas Falcon06b3e352018-03-16 20:00:28 -05001611 tx_crq.v1.correlator = cpu_to_be32(index);
1612 tx_crq.v1.dma_reg = cpu_to_be16(tx_pool->long_term_buff.map_id);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001613 tx_crq.v1.sge_len = cpu_to_be32(skb->len);
1614 tx_crq.v1.ioba = cpu_to_be64(data_dma_addr);
1615
Michał Mirosławe84b4792018-11-07 17:50:52 +01001616 if (adapter->vlan_header_insertion && skb_vlan_tag_present(skb)) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06001617 tx_crq.v1.flags2 |= IBMVNIC_TX_VLAN_INSERT;
1618 tx_crq.v1.vlan_id = cpu_to_be16(skb->vlan_tci);
1619 }
1620
1621 if (skb->protocol == htons(ETH_P_IP)) {
Thomas Falcona0dca102018-01-18 19:29:48 -06001622 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV4;
1623 proto = ip_hdr(skb)->protocol;
1624 } else if (skb->protocol == htons(ETH_P_IPV6)) {
1625 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV6;
1626 proto = ipv6_hdr(skb)->nexthdr;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001627 }
1628
Thomas Falcona0dca102018-01-18 19:29:48 -06001629 if (proto == IPPROTO_TCP)
1630 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_TCP;
1631 else if (proto == IPPROTO_UDP)
1632 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_UDP;
1633
Thomas Falconad7775d2016-04-01 17:20:34 -05001634 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06001635 tx_crq.v1.flags1 |= IBMVNIC_TX_CHKSUM_OFFLOAD;
Thomas Falconad7775d2016-04-01 17:20:34 -05001636 hdrs += 2;
1637 }
Thomas Falconfdb06102017-10-17 12:36:55 -05001638 if (skb_is_gso(skb)) {
1639 tx_crq.v1.flags1 |= IBMVNIC_TX_LSO;
1640 tx_crq.v1.mss = cpu_to_be16(skb_shinfo(skb)->gso_size);
1641 hdrs += 2;
1642 }
Thomas Falconad7775d2016-04-01 17:20:34 -05001643 /* determine if l2/3/4 headers are sent to firmware */
John Allen2fa56a42018-02-09 13:19:46 -06001644 if ((*hdrs >> 7) & 1) {
Thomas Falconad7775d2016-04-01 17:20:34 -05001645 build_hdr_descs_arr(tx_buff, &num_entries, *hdrs);
1646 tx_crq.v1.n_crq_elem = num_entries;
Thomas Falconecba6162018-02-26 18:10:55 -06001647 tx_buff->num_entries = num_entries;
Thomas Falconad7775d2016-04-01 17:20:34 -05001648 tx_buff->indir_arr[0] = tx_crq;
1649 tx_buff->indir_dma = dma_map_single(dev, tx_buff->indir_arr,
1650 sizeof(tx_buff->indir_arr),
1651 DMA_TO_DEVICE);
1652 if (dma_mapping_error(dev, tx_buff->indir_dma)) {
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001653 dev_kfree_skb_any(skb);
1654 tx_buff->skb = NULL;
Thomas Falconad7775d2016-04-01 17:20:34 -05001655 if (!firmware_has_feature(FW_FEATURE_CMO))
1656 dev_err(dev, "tx: unable to map descriptor array\n");
1657 tx_map_failed++;
1658 tx_dropped++;
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001659 ret = NETDEV_TX_OK;
Thomas Falcon86b61a52018-03-16 20:00:29 -05001660 goto tx_err_out;
Thomas Falconad7775d2016-04-01 17:20:34 -05001661 }
Cristobal Fornof3ae59c2020-08-19 13:16:23 -05001662 lpar_rc = send_subcrq_indirect(adapter, handle,
Thomas Falconad7775d2016-04-01 17:20:34 -05001663 (u64)tx_buff->indir_dma,
1664 (u64)num_entries);
Thomas Falcon80f0fe02019-08-14 14:57:05 -05001665 dma_unmap_single(dev, tx_buff->indir_dma,
1666 sizeof(tx_buff->indir_arr), DMA_TO_DEVICE);
Thomas Falconad7775d2016-04-01 17:20:34 -05001667 } else {
Thomas Falconecba6162018-02-26 18:10:55 -06001668 tx_buff->num_entries = num_entries;
Cristobal Fornof3ae59c2020-08-19 13:16:23 -05001669 lpar_rc = send_subcrq(adapter, handle,
John Allen498cd8e2016-04-06 11:49:55 -05001670 &tx_crq);
Thomas Falconad7775d2016-04-01 17:20:34 -05001671 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001672 if (lpar_rc != H_SUCCESS) {
Thomas Falcon2d14d372018-07-13 12:03:32 -05001673 if (lpar_rc != H_CLOSED && lpar_rc != H_PARAMETER)
1674 dev_err_ratelimited(dev, "tx: send failed\n");
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001675 dev_kfree_skb_any(skb);
1676 tx_buff->skb = NULL;
1677
Thomas Falcon5a18e1e2018-04-06 18:37:05 -05001678 if (lpar_rc == H_CLOSED || adapter->failover_pending) {
1679 /* Disable TX and report carrier off if queue is closed
1680 * or pending failover.
Thomas Falconb8c80b82017-05-26 10:30:42 -04001681 * Firmware guarantees that a signal will be sent to the
1682 * driver, triggering a reset or some other action.
1683 */
1684 netif_tx_stop_all_queues(netdev);
1685 netif_carrier_off(netdev);
1686 }
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001687
Thomas Falcon032c5e82015-12-21 11:26:06 -06001688 tx_send_failed++;
1689 tx_dropped++;
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001690 ret = NETDEV_TX_OK;
Thomas Falcon86b61a52018-03-16 20:00:29 -05001691 goto tx_err_out;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001692 }
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001693
Thomas Falconffc385b2018-02-18 10:08:41 -06001694 if (atomic_add_return(num_entries, &tx_scrq->used)
Brian King58c8c0c2017-04-19 13:44:47 -04001695 >= adapter->req_tx_entries_per_subcrq) {
Thomas Falcon0aecb132018-02-26 18:10:58 -06001696 netdev_dbg(netdev, "Stopping queue %d\n", queue_num);
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001697 netif_stop_subqueue(netdev, queue_num);
1698 }
1699
Thomas Falcon032c5e82015-12-21 11:26:06 -06001700 tx_packets++;
1701 tx_bytes += skb->len;
1702 txq->trans_start = jiffies;
1703 ret = NETDEV_TX_OK;
Thomas Falcon86b61a52018-03-16 20:00:29 -05001704 goto out;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001705
Thomas Falcon86b61a52018-03-16 20:00:29 -05001706tx_err_out:
1707 /* roll back consumer index and map array*/
1708 if (tx_pool->consumer_index == 0)
1709 tx_pool->consumer_index =
1710 tx_pool->num_buffers - 1;
1711 else
1712 tx_pool->consumer_index--;
1713 tx_pool->free_map[tx_pool->consumer_index] = index;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001714out:
1715 netdev->stats.tx_dropped += tx_dropped;
1716 netdev->stats.tx_bytes += tx_bytes;
1717 netdev->stats.tx_packets += tx_packets;
1718 adapter->tx_send_failed += tx_send_failed;
1719 adapter->tx_map_failed += tx_map_failed;
John Allen3d52b592017-08-02 16:44:14 -05001720 adapter->tx_stats_buffers[queue_num].packets += tx_packets;
1721 adapter->tx_stats_buffers[queue_num].bytes += tx_bytes;
1722 adapter->tx_stats_buffers[queue_num].dropped_packets += tx_dropped;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001723
1724 return ret;
1725}
1726
1727static void ibmvnic_set_multi(struct net_device *netdev)
1728{
1729 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1730 struct netdev_hw_addr *ha;
1731 union ibmvnic_crq crq;
1732
1733 memset(&crq, 0, sizeof(crq));
1734 crq.request_capability.first = IBMVNIC_CRQ_CMD;
1735 crq.request_capability.cmd = REQUEST_CAPABILITY;
1736
1737 if (netdev->flags & IFF_PROMISC) {
1738 if (!adapter->promisc_supported)
1739 return;
1740 } else {
1741 if (netdev->flags & IFF_ALLMULTI) {
1742 /* Accept all multicast */
1743 memset(&crq, 0, sizeof(crq));
1744 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1745 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1746 crq.multicast_ctrl.flags = IBMVNIC_ENABLE_ALL;
1747 ibmvnic_send_crq(adapter, &crq);
1748 } else if (netdev_mc_empty(netdev)) {
1749 /* Reject all multicast */
1750 memset(&crq, 0, sizeof(crq));
1751 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1752 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1753 crq.multicast_ctrl.flags = IBMVNIC_DISABLE_ALL;
1754 ibmvnic_send_crq(adapter, &crq);
1755 } else {
1756 /* Accept one or more multicast(s) */
1757 netdev_for_each_mc_addr(ha, netdev) {
1758 memset(&crq, 0, sizeof(crq));
1759 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1760 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1761 crq.multicast_ctrl.flags = IBMVNIC_ENABLE_MC;
1762 ether_addr_copy(&crq.multicast_ctrl.mac_addr[0],
1763 ha->addr);
1764 ibmvnic_send_crq(adapter, &crq);
1765 }
1766 }
1767 }
1768}
1769
Thomas Falcon62740e92019-05-09 23:13:43 -05001770static int __ibmvnic_set_mac(struct net_device *netdev, u8 *dev_addr)
Thomas Falcon032c5e82015-12-21 11:26:06 -06001771{
1772 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001773 union ibmvnic_crq crq;
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05001774 int rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001775
Thomas Falcon62740e92019-05-09 23:13:43 -05001776 if (!is_valid_ether_addr(dev_addr)) {
1777 rc = -EADDRNOTAVAIL;
1778 goto err;
1779 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001780
1781 memset(&crq, 0, sizeof(crq));
1782 crq.change_mac_addr.first = IBMVNIC_CRQ_CMD;
1783 crq.change_mac_addr.cmd = CHANGE_MAC_ADDR;
Thomas Falcon62740e92019-05-09 23:13:43 -05001784 ether_addr_copy(&crq.change_mac_addr.mac_addr[0], dev_addr);
Thomas Falconf8136142018-01-29 13:45:05 -06001785
Thomas Falconff25dcb2019-11-25 17:12:56 -06001786 mutex_lock(&adapter->fw_lock);
1787 adapter->fw_done_rc = 0;
Thomas Falcon070eca92019-11-25 17:12:53 -06001788 reinit_completion(&adapter->fw_done);
Thomas Falconff25dcb2019-11-25 17:12:56 -06001789
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05001790 rc = ibmvnic_send_crq(adapter, &crq);
Thomas Falcon62740e92019-05-09 23:13:43 -05001791 if (rc) {
1792 rc = -EIO;
Thomas Falconff25dcb2019-11-25 17:12:56 -06001793 mutex_unlock(&adapter->fw_lock);
Thomas Falcon62740e92019-05-09 23:13:43 -05001794 goto err;
1795 }
1796
Thomas Falcon476d96c2019-11-25 17:12:55 -06001797 rc = ibmvnic_wait_for_completion(adapter, &adapter->fw_done, 10000);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001798 /* netdev->dev_addr is changed in handle_change_mac_rsp function */
Thomas Falcon476d96c2019-11-25 17:12:55 -06001799 if (rc || adapter->fw_done_rc) {
Thomas Falcon62740e92019-05-09 23:13:43 -05001800 rc = -EIO;
Thomas Falconff25dcb2019-11-25 17:12:56 -06001801 mutex_unlock(&adapter->fw_lock);
Thomas Falcon62740e92019-05-09 23:13:43 -05001802 goto err;
1803 }
Thomas Falconff25dcb2019-11-25 17:12:56 -06001804 mutex_unlock(&adapter->fw_lock);
Thomas Falcon62740e92019-05-09 23:13:43 -05001805 return 0;
1806err:
1807 ether_addr_copy(adapter->mac_addr, netdev->dev_addr);
1808 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001809}
1810
John Allenc26eba02017-10-26 16:23:25 -05001811static int ibmvnic_set_mac(struct net_device *netdev, void *p)
1812{
1813 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1814 struct sockaddr *addr = p;
Thomas Falconf8136142018-01-29 13:45:05 -06001815 int rc;
John Allenc26eba02017-10-26 16:23:25 -05001816
Thomas Falcon62740e92019-05-09 23:13:43 -05001817 rc = 0;
1818 ether_addr_copy(adapter->mac_addr, addr->sa_data);
1819 if (adapter->state != VNIC_PROBED)
1820 rc = __ibmvnic_set_mac(netdev, addr->sa_data);
John Allenc26eba02017-10-26 16:23:25 -05001821
Thomas Falconf8136142018-01-29 13:45:05 -06001822 return rc;
John Allenc26eba02017-10-26 16:23:25 -05001823}
1824
Nathan Fontenoted651a12017-05-03 14:04:38 -04001825/**
Juliet Kimb27507b2019-09-20 16:11:22 -04001826 * do_change_param_reset returns zero if we are able to keep processing reset
1827 * events, or non-zero if we hit a fatal error and must halt.
1828 */
1829static int do_change_param_reset(struct ibmvnic_adapter *adapter,
1830 struct ibmvnic_rwi *rwi,
1831 u32 reset_state)
1832{
1833 struct net_device *netdev = adapter->netdev;
1834 int i, rc;
1835
1836 netdev_dbg(adapter->netdev, "Change param resetting driver (%d)\n",
1837 rwi->reset_reason);
1838
1839 netif_carrier_off(netdev);
1840 adapter->reset_reason = rwi->reset_reason;
1841
1842 ibmvnic_cleanup(netdev);
1843
1844 if (reset_state == VNIC_OPEN) {
1845 rc = __ibmvnic_close(netdev);
1846 if (rc)
1847 return rc;
1848 }
1849
1850 release_resources(adapter);
1851 release_sub_crqs(adapter, 1);
1852 release_crq_queue(adapter);
1853
1854 adapter->state = VNIC_PROBED;
1855
1856 rc = init_crq_queue(adapter);
1857
1858 if (rc) {
1859 netdev_err(adapter->netdev,
1860 "Couldn't initialize crq. rc=%d\n", rc);
1861 return rc;
1862 }
1863
Lijun Pan635e4422020-08-19 17:52:26 -05001864 rc = ibmvnic_reset_init(adapter, true);
Juliet Kimb27507b2019-09-20 16:11:22 -04001865 if (rc)
1866 return IBMVNIC_INIT_FAILED;
1867
1868 /* If the adapter was in PROBE state prior to the reset,
1869 * exit here.
1870 */
1871 if (reset_state == VNIC_PROBED)
1872 return 0;
1873
1874 rc = ibmvnic_login(netdev);
1875 if (rc) {
1876 adapter->state = reset_state;
1877 return rc;
1878 }
1879
1880 rc = init_resources(adapter);
1881 if (rc)
1882 return rc;
1883
1884 ibmvnic_disable_irqs(adapter);
1885
1886 adapter->state = VNIC_CLOSED;
1887
1888 if (reset_state == VNIC_CLOSED)
1889 return 0;
1890
1891 rc = __ibmvnic_open(netdev);
1892 if (rc)
1893 return IBMVNIC_OPEN_FAILED;
1894
1895 /* refresh device's multicast list */
1896 ibmvnic_set_multi(netdev);
1897
1898 /* kick napi */
1899 for (i = 0; i < adapter->req_rx_queues; i++)
1900 napi_schedule(&adapter->napi[i]);
1901
1902 return 0;
1903}
1904
1905/**
Nathan Fontenoted651a12017-05-03 14:04:38 -04001906 * do_reset returns zero if we are able to keep processing reset events, or
1907 * non-zero if we hit a fatal error and must halt.
1908 */
1909static int do_reset(struct ibmvnic_adapter *adapter,
1910 struct ibmvnic_rwi *rwi, u32 reset_state)
1911{
John Allen896d8692018-01-18 16:26:31 -06001912 u64 old_num_rx_queues, old_num_tx_queues;
Thomas Falcon5bf032e2018-11-21 11:17:59 -06001913 u64 old_num_rx_slots, old_num_tx_slots;
Nathan Fontenoted651a12017-05-03 14:04:38 -04001914 struct net_device *netdev = adapter->netdev;
1915 int i, rc;
1916
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001917 netdev_dbg(adapter->netdev, "Re-setting driver (%d)\n",
1918 rwi->reset_reason);
1919
Juliet Kimb27507b2019-09-20 16:11:22 -04001920 rtnl_lock();
1921
Nathan Fontenoted651a12017-05-03 14:04:38 -04001922 netif_carrier_off(netdev);
1923 adapter->reset_reason = rwi->reset_reason;
1924
John Allen896d8692018-01-18 16:26:31 -06001925 old_num_rx_queues = adapter->req_rx_queues;
1926 old_num_tx_queues = adapter->req_tx_queues;
Thomas Falcon5bf032e2018-11-21 11:17:59 -06001927 old_num_rx_slots = adapter->req_rx_add_entries_per_subcrq;
1928 old_num_tx_slots = adapter->req_tx_entries_per_subcrq;
John Allen896d8692018-01-18 16:26:31 -06001929
Nathan Fontenot30f79622018-04-06 18:37:06 -05001930 ibmvnic_cleanup(netdev);
1931
Thomas Falcon1f946082019-06-07 16:03:53 -05001932 if (reset_state == VNIC_OPEN &&
1933 adapter->reset_reason != VNIC_RESET_MOBILITY &&
Nathan Fontenot30f79622018-04-06 18:37:06 -05001934 adapter->reset_reason != VNIC_RESET_FAILOVER) {
Juliet Kimb27507b2019-09-20 16:11:22 -04001935 adapter->state = VNIC_CLOSING;
Nathan Fontenoted651a12017-05-03 14:04:38 -04001936
Juliet Kimb27507b2019-09-20 16:11:22 -04001937 /* Release the RTNL lock before link state change and
1938 * re-acquire after the link state change to allow
1939 * linkwatch_event to grab the RTNL lock and run during
1940 * a reset.
1941 */
1942 rtnl_unlock();
1943 rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_DN);
1944 rtnl_lock();
1945 if (rc)
1946 goto out;
1947
1948 if (adapter->state != VNIC_CLOSING) {
1949 rc = -1;
1950 goto out;
1951 }
1952
1953 adapter->state = VNIC_CLOSED;
John Allenc26eba02017-10-26 16:23:25 -05001954 }
1955
John Allen8cb31cf2017-05-26 10:30:37 -04001956 if (adapter->reset_reason != VNIC_RESET_NON_FATAL) {
1957 /* remove the closed state so when we call open it appears
1958 * we are coming from the probed state.
1959 */
Nathan Fontenoted651a12017-05-03 14:04:38 -04001960 adapter->state = VNIC_PROBED;
John Allen8cb31cf2017-05-26 10:30:37 -04001961
Juliet Kimb27507b2019-09-20 16:11:22 -04001962 if (adapter->reset_reason == VNIC_RESET_MOBILITY) {
Nathan Fontenot30f79622018-04-06 18:37:06 -05001963 rc = ibmvnic_reenable_crq_queue(adapter);
1964 release_sub_crqs(adapter, 1);
1965 } else {
1966 rc = ibmvnic_reset_crq(adapter);
Dany Madden8b40eb732020-06-18 15:24:13 -04001967 if (rc == H_CLOSED || rc == H_SUCCESS) {
Nathan Fontenot30f79622018-04-06 18:37:06 -05001968 rc = vio_enable_interrupts(adapter->vdev);
Dany Madden8b40eb732020-06-18 15:24:13 -04001969 if (rc)
1970 netdev_err(adapter->netdev,
1971 "Reset failed to enable interrupts. rc=%d\n",
1972 rc);
1973 }
Nathan Fontenot30f79622018-04-06 18:37:06 -05001974 }
1975
1976 if (rc) {
1977 netdev_err(adapter->netdev,
Dany Madden8b40eb732020-06-18 15:24:13 -04001978 "Reset couldn't initialize crq. rc=%d\n", rc);
Juliet Kimb27507b2019-09-20 16:11:22 -04001979 goto out;
Nathan Fontenot30f79622018-04-06 18:37:06 -05001980 }
1981
Lijun Pan635e4422020-08-19 17:52:26 -05001982 rc = ibmvnic_reset_init(adapter, true);
Juliet Kimb27507b2019-09-20 16:11:22 -04001983 if (rc) {
1984 rc = IBMVNIC_INIT_FAILED;
1985 goto out;
1986 }
John Allen8cb31cf2017-05-26 10:30:37 -04001987
1988 /* If the adapter was in PROBE state prior to the reset,
1989 * exit here.
1990 */
Juliet Kimb27507b2019-09-20 16:11:22 -04001991 if (reset_state == VNIC_PROBED) {
1992 rc = 0;
1993 goto out;
1994 }
John Allen8cb31cf2017-05-26 10:30:37 -04001995
1996 rc = ibmvnic_login(netdev);
1997 if (rc) {
John Allen3578a7e2018-07-16 10:29:30 -05001998 adapter->state = reset_state;
Juliet Kimb27507b2019-09-20 16:11:22 -04001999 goto out;
John Allen8cb31cf2017-05-26 10:30:37 -04002000 }
2001
Juliet Kimb27507b2019-09-20 16:11:22 -04002002 if (adapter->req_rx_queues != old_num_rx_queues ||
2003 adapter->req_tx_queues != old_num_tx_queues ||
2004 adapter->req_rx_add_entries_per_subcrq !=
2005 old_num_rx_slots ||
2006 adapter->req_tx_entries_per_subcrq !=
Mingming Cao9f134572020-08-25 13:26:41 -04002007 old_num_tx_slots ||
2008 !adapter->rx_pool ||
2009 !adapter->tso_pool ||
2010 !adapter->tx_pool) {
John Allen896d8692018-01-18 16:26:31 -06002011 release_rx_pools(adapter);
2012 release_tx_pools(adapter);
Juliet Kima5681e22018-11-19 15:59:22 -06002013 release_napi(adapter);
2014 release_vpd_data(adapter);
2015
2016 rc = init_resources(adapter);
Thomas Falconf611a5b2018-08-30 13:19:53 -05002017 if (rc)
Juliet Kimb27507b2019-09-20 16:11:22 -04002018 goto out;
Nathan Fontenotd9043c12018-02-19 13:30:14 -06002019
John Allenc26eba02017-10-26 16:23:25 -05002020 } else {
2021 rc = reset_tx_pools(adapter);
2022 if (rc)
Mingming Cao9f134572020-08-25 13:26:41 -04002023 netdev_dbg(adapter->netdev, "reset tx pools failed (%d)\n",
2024 rc);
Juliet Kimb27507b2019-09-20 16:11:22 -04002025 goto out;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -04002026
John Allenc26eba02017-10-26 16:23:25 -05002027 rc = reset_rx_pools(adapter);
2028 if (rc)
Mingming Cao9f134572020-08-25 13:26:41 -04002029 netdev_dbg(adapter->netdev, "reset rx pools failed (%d)\n",
2030 rc);
Juliet Kimb27507b2019-09-20 16:11:22 -04002031 goto out;
John Allenc26eba02017-10-26 16:23:25 -05002032 }
Thomas Falcon134bbe72018-05-16 15:49:04 -05002033 ibmvnic_disable_irqs(adapter);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002034 }
John Allene676d812018-03-14 10:41:29 -05002035 adapter->state = VNIC_CLOSED;
2036
Juliet Kimb27507b2019-09-20 16:11:22 -04002037 if (reset_state == VNIC_CLOSED) {
2038 rc = 0;
2039 goto out;
2040 }
John Allene676d812018-03-14 10:41:29 -05002041
Nathan Fontenoted651a12017-05-03 14:04:38 -04002042 rc = __ibmvnic_open(netdev);
2043 if (rc) {
Juliet Kimb27507b2019-09-20 16:11:22 -04002044 rc = IBMVNIC_OPEN_FAILED;
2045 goto out;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002046 }
2047
Thomas Falconbe32a242019-06-07 16:03:54 -05002048 /* refresh device's multicast list */
2049 ibmvnic_set_multi(netdev);
2050
Nathan Fontenoted651a12017-05-03 14:04:38 -04002051 /* kick napi */
2052 for (i = 0; i < adapter->req_rx_queues; i++)
2053 napi_schedule(&adapter->napi[i]);
2054
Juliet Kimb27507b2019-09-20 16:11:22 -04002055 if (adapter->reset_reason != VNIC_RESET_FAILOVER)
Thomas Falcon986103e2018-11-30 10:59:08 -06002056 call_netdevice_notifiers(NETDEV_NOTIFY_PEERS, netdev);
Nathan Fontenot61d3e1d2017-06-12 20:47:45 -04002057
Juliet Kimb27507b2019-09-20 16:11:22 -04002058 rc = 0;
2059
2060out:
2061 rtnl_unlock();
2062
2063 return rc;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002064}
2065
Thomas Falcon2770a792018-05-23 13:38:02 -05002066static int do_hard_reset(struct ibmvnic_adapter *adapter,
2067 struct ibmvnic_rwi *rwi, u32 reset_state)
2068{
2069 struct net_device *netdev = adapter->netdev;
2070 int rc;
2071
2072 netdev_dbg(adapter->netdev, "Hard resetting driver (%d)\n",
2073 rwi->reset_reason);
2074
2075 netif_carrier_off(netdev);
2076 adapter->reset_reason = rwi->reset_reason;
2077
2078 ibmvnic_cleanup(netdev);
2079 release_resources(adapter);
2080 release_sub_crqs(adapter, 0);
2081 release_crq_queue(adapter);
2082
2083 /* remove the closed state so when we call open it appears
2084 * we are coming from the probed state.
2085 */
2086 adapter->state = VNIC_PROBED;
2087
Thomas Falconbbd669a2019-04-04 18:58:26 -05002088 reinit_completion(&adapter->init_done);
Thomas Falcon2770a792018-05-23 13:38:02 -05002089 rc = init_crq_queue(adapter);
2090 if (rc) {
2091 netdev_err(adapter->netdev,
2092 "Couldn't initialize crq. rc=%d\n", rc);
2093 return rc;
2094 }
2095
Lijun Pan635e4422020-08-19 17:52:26 -05002096 rc = ibmvnic_reset_init(adapter, false);
Thomas Falcon2770a792018-05-23 13:38:02 -05002097 if (rc)
2098 return rc;
2099
2100 /* If the adapter was in PROBE state prior to the reset,
2101 * exit here.
2102 */
2103 if (reset_state == VNIC_PROBED)
2104 return 0;
2105
2106 rc = ibmvnic_login(netdev);
2107 if (rc) {
2108 adapter->state = VNIC_PROBED;
2109 return 0;
2110 }
Juliet Kima5681e22018-11-19 15:59:22 -06002111
2112 rc = init_resources(adapter);
Thomas Falcon2770a792018-05-23 13:38:02 -05002113 if (rc)
2114 return rc;
2115
2116 ibmvnic_disable_irqs(adapter);
2117 adapter->state = VNIC_CLOSED;
2118
2119 if (reset_state == VNIC_CLOSED)
2120 return 0;
2121
2122 rc = __ibmvnic_open(netdev);
Juliet Kimb27507b2019-09-20 16:11:22 -04002123 if (rc)
2124 return IBMVNIC_OPEN_FAILED;
Thomas Falcon2770a792018-05-23 13:38:02 -05002125
Thomas Falcon2770a792018-05-23 13:38:02 -05002126 return 0;
2127}
2128
Nathan Fontenoted651a12017-05-03 14:04:38 -04002129static struct ibmvnic_rwi *get_next_rwi(struct ibmvnic_adapter *adapter)
2130{
2131 struct ibmvnic_rwi *rwi;
Thomas Falcon6c5c7482018-12-10 15:22:22 -06002132 unsigned long flags;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002133
Thomas Falcon6c5c7482018-12-10 15:22:22 -06002134 spin_lock_irqsave(&adapter->rwi_lock, flags);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002135
2136 if (!list_empty(&adapter->rwi_list)) {
2137 rwi = list_first_entry(&adapter->rwi_list, struct ibmvnic_rwi,
2138 list);
2139 list_del(&rwi->list);
2140 } else {
2141 rwi = NULL;
2142 }
2143
Thomas Falcon6c5c7482018-12-10 15:22:22 -06002144 spin_unlock_irqrestore(&adapter->rwi_lock, flags);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002145 return rwi;
2146}
2147
2148static void free_all_rwi(struct ibmvnic_adapter *adapter)
2149{
2150 struct ibmvnic_rwi *rwi;
2151
2152 rwi = get_next_rwi(adapter);
2153 while (rwi) {
2154 kfree(rwi);
2155 rwi = get_next_rwi(adapter);
2156 }
2157}
2158
2159static void __ibmvnic_reset(struct work_struct *work)
2160{
2161 struct ibmvnic_rwi *rwi;
2162 struct ibmvnic_adapter *adapter;
Juliet Kim7d7195a2020-03-10 09:23:58 -05002163 bool saved_state = false;
2164 unsigned long flags;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002165 u32 reset_state;
John Allenc26eba02017-10-26 16:23:25 -05002166 int rc = 0;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002167
2168 adapter = container_of(work, struct ibmvnic_adapter, ibmvnic_reset);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002169
Juliet Kim7ed5b312019-09-20 16:11:23 -04002170 if (test_and_set_bit_lock(0, &adapter->resetting)) {
2171 schedule_delayed_work(&adapter->ibmvnic_delayed_reset,
2172 IBMVNIC_RESET_DELAY);
2173 return;
2174 }
2175
Nathan Fontenoted651a12017-05-03 14:04:38 -04002176 rwi = get_next_rwi(adapter);
2177 while (rwi) {
Juliet Kim7d7195a2020-03-10 09:23:58 -05002178 spin_lock_irqsave(&adapter->state_lock, flags);
2179
Thomas Falcon36f10312019-08-27 11:10:04 -05002180 if (adapter->state == VNIC_REMOVING ||
Michal Suchanekc8dc5592019-09-09 22:44:51 +02002181 adapter->state == VNIC_REMOVED) {
Juliet Kim7d7195a2020-03-10 09:23:58 -05002182 spin_unlock_irqrestore(&adapter->state_lock, flags);
Juliet Kim1c2977c2019-09-05 17:30:01 -04002183 kfree(rwi);
2184 rc = EBUSY;
2185 break;
2186 }
Thomas Falcon36f10312019-08-27 11:10:04 -05002187
Juliet Kim7d7195a2020-03-10 09:23:58 -05002188 if (!saved_state) {
2189 reset_state = adapter->state;
2190 adapter->state = VNIC_RESETTING;
2191 saved_state = true;
2192 }
2193 spin_unlock_irqrestore(&adapter->state_lock, flags);
2194
Juliet Kimb27507b2019-09-20 16:11:22 -04002195 if (rwi->reset_reason == VNIC_RESET_CHANGE_PARAM) {
2196 /* CHANGE_PARAM requestor holds rtnl_lock */
2197 rc = do_change_param_reset(adapter, rwi, reset_state);
2198 } else if (adapter->force_reset_recovery) {
2199 /* Transport event occurred during previous reset */
2200 if (adapter->wait_for_reset) {
2201 /* Previous was CHANGE_PARAM; caller locked */
2202 adapter->force_reset_recovery = false;
2203 rc = do_hard_reset(adapter, rwi, reset_state);
2204 } else {
2205 rtnl_lock();
2206 adapter->force_reset_recovery = false;
2207 rc = do_hard_reset(adapter, rwi, reset_state);
2208 rtnl_unlock();
2209 }
Juliet Kimf9c6cea2020-04-30 13:22:11 -05002210 } else if (!(rwi->reset_reason == VNIC_RESET_FATAL &&
2211 adapter->from_passive_init)) {
Thomas Falcon2770a792018-05-23 13:38:02 -05002212 rc = do_reset(adapter, rwi, reset_state);
2213 }
Nathan Fontenoted651a12017-05-03 14:04:38 -04002214 kfree(rwi);
Juliet Kimb27507b2019-09-20 16:11:22 -04002215 if (rc == IBMVNIC_OPEN_FAILED) {
2216 if (list_empty(&adapter->rwi_list))
2217 adapter->state = VNIC_CLOSED;
2218 else
2219 adapter->state = reset_state;
2220 rc = 0;
2221 } else if (rc && rc != IBMVNIC_INIT_FAILED &&
Thomas Falcon2770a792018-05-23 13:38:02 -05002222 !adapter->force_reset_recovery)
Nathan Fontenoted651a12017-05-03 14:04:38 -04002223 break;
2224
2225 rwi = get_next_rwi(adapter);
Juliet Kim7ed5b312019-09-20 16:11:23 -04002226
2227 if (rwi && (rwi->reset_reason == VNIC_RESET_FAILOVER ||
2228 rwi->reset_reason == VNIC_RESET_MOBILITY))
2229 adapter->force_reset_recovery = true;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002230 }
2231
John Allenc26eba02017-10-26 16:23:25 -05002232 if (adapter->wait_for_reset) {
John Allenc26eba02017-10-26 16:23:25 -05002233 adapter->reset_done_rc = rc;
2234 complete(&adapter->reset_done);
2235 }
2236
Nathan Fontenoted651a12017-05-03 14:04:38 -04002237 if (rc) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002238 netdev_dbg(adapter->netdev, "Reset failed\n");
Nathan Fontenoted651a12017-05-03 14:04:38 -04002239 free_all_rwi(adapter);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002240 }
Juliet Kim1c2977c2019-09-05 17:30:01 -04002241
Juliet Kim7ed5b312019-09-20 16:11:23 -04002242 clear_bit_unlock(0, &adapter->resetting);
2243}
2244
2245static void __ibmvnic_delayed_reset(struct work_struct *work)
2246{
2247 struct ibmvnic_adapter *adapter;
2248
2249 adapter = container_of(work, struct ibmvnic_adapter,
2250 ibmvnic_delayed_reset.work);
2251 __ibmvnic_reset(&adapter->ibmvnic_reset);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002252}
2253
Thomas Falconaf894d22018-04-06 18:37:04 -05002254static int ibmvnic_reset(struct ibmvnic_adapter *adapter,
2255 enum ibmvnic_reset_reason reason)
Nathan Fontenoted651a12017-05-03 14:04:38 -04002256{
Thomas Falcon2770a792018-05-23 13:38:02 -05002257 struct list_head *entry, *tmp_entry;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002258 struct ibmvnic_rwi *rwi, *tmp;
2259 struct net_device *netdev = adapter->netdev;
Thomas Falcon6c5c7482018-12-10 15:22:22 -06002260 unsigned long flags;
Thomas Falconaf894d22018-04-06 18:37:04 -05002261 int ret;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002262
2263 if (adapter->state == VNIC_REMOVING ||
Thomas Falcon5a18e1e2018-04-06 18:37:05 -05002264 adapter->state == VNIC_REMOVED ||
2265 adapter->failover_pending) {
Thomas Falconaf894d22018-04-06 18:37:04 -05002266 ret = EBUSY;
Thomas Falcon5a18e1e2018-04-06 18:37:05 -05002267 netdev_dbg(netdev, "Adapter removing or pending failover, skipping reset\n");
Thomas Falconaf894d22018-04-06 18:37:04 -05002268 goto err;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002269 }
2270
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04002271 if (adapter->state == VNIC_PROBING) {
2272 netdev_warn(netdev, "Adapter reset during probe\n");
Thomas Falconaf894d22018-04-06 18:37:04 -05002273 ret = adapter->init_done_rc = EAGAIN;
2274 goto err;
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04002275 }
2276
Thomas Falcon6c5c7482018-12-10 15:22:22 -06002277 spin_lock_irqsave(&adapter->rwi_lock, flags);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002278
2279 list_for_each(entry, &adapter->rwi_list) {
2280 tmp = list_entry(entry, struct ibmvnic_rwi, list);
2281 if (tmp->reset_reason == reason) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002282 netdev_dbg(netdev, "Skipping matching reset\n");
Thomas Falcon6c5c7482018-12-10 15:22:22 -06002283 spin_unlock_irqrestore(&adapter->rwi_lock, flags);
Thomas Falconaf894d22018-04-06 18:37:04 -05002284 ret = EBUSY;
2285 goto err;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002286 }
2287 }
2288
Thomas Falcon1d1bbc32018-12-10 15:22:23 -06002289 rwi = kzalloc(sizeof(*rwi), GFP_ATOMIC);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002290 if (!rwi) {
Thomas Falcon6c5c7482018-12-10 15:22:22 -06002291 spin_unlock_irqrestore(&adapter->rwi_lock, flags);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002292 ibmvnic_close(netdev);
Thomas Falconaf894d22018-04-06 18:37:04 -05002293 ret = ENOMEM;
2294 goto err;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002295 }
Thomas Falcon2770a792018-05-23 13:38:02 -05002296 /* if we just received a transport event,
2297 * flush reset queue and process this reset
2298 */
2299 if (adapter->force_reset_recovery && !list_empty(&adapter->rwi_list)) {
2300 list_for_each_safe(entry, tmp_entry, &adapter->rwi_list)
2301 list_del(entry);
2302 }
Nathan Fontenoted651a12017-05-03 14:04:38 -04002303 rwi->reset_reason = reason;
2304 list_add_tail(&rwi->list, &adapter->rwi_list);
Thomas Falcon6c5c7482018-12-10 15:22:22 -06002305 spin_unlock_irqrestore(&adapter->rwi_lock, flags);
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002306 netdev_dbg(adapter->netdev, "Scheduling reset (reason %d)\n", reason);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002307 schedule_work(&adapter->ibmvnic_reset);
Thomas Falconaf894d22018-04-06 18:37:04 -05002308
2309 return 0;
2310err:
Thomas Falconaf894d22018-04-06 18:37:04 -05002311 return -ret;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002312}
2313
Michael S. Tsirkin0290bd22019-12-10 09:23:51 -05002314static void ibmvnic_tx_timeout(struct net_device *dev, unsigned int txqueue)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002315{
2316 struct ibmvnic_adapter *adapter = netdev_priv(dev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002317
Nathan Fontenoted651a12017-05-03 14:04:38 -04002318 ibmvnic_reset(adapter, VNIC_RESET_TIMEOUT);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002319}
2320
2321static void remove_buff_from_pool(struct ibmvnic_adapter *adapter,
2322 struct ibmvnic_rx_buff *rx_buff)
2323{
2324 struct ibmvnic_rx_pool *pool = &adapter->rx_pool[rx_buff->pool_index];
2325
2326 rx_buff->skb = NULL;
2327
2328 pool->free_map[pool->next_alloc] = (int)(rx_buff - pool->rx_buff);
2329 pool->next_alloc = (pool->next_alloc + 1) % pool->size;
2330
2331 atomic_dec(&pool->available);
2332}
2333
2334static int ibmvnic_poll(struct napi_struct *napi, int budget)
2335{
2336 struct net_device *netdev = napi->dev;
2337 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2338 int scrq_num = (int)(napi - adapter->napi);
2339 int frames_processed = 0;
Nathan Fontenot152ce472017-05-26 10:30:54 -04002340
Thomas Falcon032c5e82015-12-21 11:26:06 -06002341restart_poll:
2342 while (frames_processed < budget) {
2343 struct sk_buff *skb;
2344 struct ibmvnic_rx_buff *rx_buff;
2345 union sub_crq *next;
2346 u32 length;
2347 u16 offset;
2348 u8 flags = 0;
2349
Juliet Kim7ed5b312019-09-20 16:11:23 -04002350 if (unlikely(test_bit(0, &adapter->resetting) &&
John Allen34686562018-02-06 16:21:49 -06002351 adapter->reset_reason != VNIC_RESET_NON_FATAL)) {
Thomas Falcon21ecba62017-06-14 23:50:09 -05002352 enable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
2353 napi_complete_done(napi, frames_processed);
2354 return frames_processed;
2355 }
2356
Thomas Falcon032c5e82015-12-21 11:26:06 -06002357 if (!pending_scrq(adapter, adapter->rx_scrq[scrq_num]))
2358 break;
2359 next = ibmvnic_next_scrq(adapter, adapter->rx_scrq[scrq_num]);
2360 rx_buff =
2361 (struct ibmvnic_rx_buff *)be64_to_cpu(next->
2362 rx_comp.correlator);
2363 /* do error checking */
2364 if (next->rx_comp.rc) {
John Allene1cea2e2017-08-07 15:42:30 -05002365 netdev_dbg(netdev, "rx buffer returned with rc %x\n",
2366 be16_to_cpu(next->rx_comp.rc));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002367 /* free the entry */
2368 next->rx_comp.first = 0;
Thomas Falcon4b9b0f02018-02-13 18:23:42 -06002369 dev_kfree_skb_any(rx_buff->skb);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002370 remove_buff_from_pool(adapter, rx_buff);
Nathan Fontenotca05e312017-05-03 14:05:14 -04002371 continue;
Thomas Falconabe27a82018-02-19 20:12:57 -06002372 } else if (!rx_buff->skb) {
2373 /* free the entry */
2374 next->rx_comp.first = 0;
2375 remove_buff_from_pool(adapter, rx_buff);
2376 continue;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002377 }
2378
2379 length = be32_to_cpu(next->rx_comp.len);
2380 offset = be16_to_cpu(next->rx_comp.off_frame_data);
2381 flags = next->rx_comp.flags;
2382 skb = rx_buff->skb;
2383 skb_copy_to_linear_data(skb, rx_buff->data + offset,
2384 length);
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04002385
2386 /* VLAN Header has been stripped by the system firmware and
2387 * needs to be inserted by the driver
2388 */
2389 if (adapter->rx_vlan_header_insertion &&
2390 (flags & IBMVNIC_VLAN_STRIPPED))
2391 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
2392 ntohs(next->rx_comp.vlan_tci));
2393
Thomas Falcon032c5e82015-12-21 11:26:06 -06002394 /* free the entry */
2395 next->rx_comp.first = 0;
2396 remove_buff_from_pool(adapter, rx_buff);
2397
2398 skb_put(skb, length);
2399 skb->protocol = eth_type_trans(skb, netdev);
Thomas Falcon94ca3052017-05-03 14:05:20 -04002400 skb_record_rx_queue(skb, scrq_num);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002401
2402 if (flags & IBMVNIC_IP_CHKSUM_GOOD &&
2403 flags & IBMVNIC_TCP_UDP_CHKSUM_GOOD) {
2404 skb->ip_summed = CHECKSUM_UNNECESSARY;
2405 }
2406
2407 length = skb->len;
2408 napi_gro_receive(napi, skb); /* send it up */
2409 netdev->stats.rx_packets++;
2410 netdev->stats.rx_bytes += length;
John Allen3d52b592017-08-02 16:44:14 -05002411 adapter->rx_stats_buffers[scrq_num].packets++;
2412 adapter->rx_stats_buffers[scrq_num].bytes += length;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002413 frames_processed++;
2414 }
Nathan Fontenot152ce472017-05-26 10:30:54 -04002415
2416 if (adapter->state != VNIC_CLOSING)
2417 replenish_rx_pool(adapter, &adapter->rx_pool[scrq_num]);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002418
2419 if (frames_processed < budget) {
2420 enable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
Eric Dumazet6ad20162017-01-30 08:22:01 -08002421 napi_complete_done(napi, frames_processed);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002422 if (pending_scrq(adapter, adapter->rx_scrq[scrq_num]) &&
2423 napi_reschedule(napi)) {
2424 disable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
2425 goto restart_poll;
2426 }
2427 }
2428 return frames_processed;
2429}
2430
John Allenc26eba02017-10-26 16:23:25 -05002431static int wait_for_reset(struct ibmvnic_adapter *adapter)
2432{
Thomas Falconaf894d22018-04-06 18:37:04 -05002433 int rc, ret;
2434
John Allenc26eba02017-10-26 16:23:25 -05002435 adapter->fallback.mtu = adapter->req_mtu;
2436 adapter->fallback.rx_queues = adapter->req_rx_queues;
2437 adapter->fallback.tx_queues = adapter->req_tx_queues;
2438 adapter->fallback.rx_entries = adapter->req_rx_add_entries_per_subcrq;
2439 adapter->fallback.tx_entries = adapter->req_tx_entries_per_subcrq;
2440
Thomas Falcon070eca92019-11-25 17:12:53 -06002441 reinit_completion(&adapter->reset_done);
John Allenc26eba02017-10-26 16:23:25 -05002442 adapter->wait_for_reset = true;
Thomas Falconaf894d22018-04-06 18:37:04 -05002443 rc = ibmvnic_reset(adapter, VNIC_RESET_CHANGE_PARAM);
Thomas Falcon476d96c2019-11-25 17:12:55 -06002444
2445 if (rc) {
2446 ret = rc;
2447 goto out;
2448 }
2449 rc = ibmvnic_wait_for_completion(adapter, &adapter->reset_done, 60000);
2450 if (rc) {
2451 ret = -ENODEV;
2452 goto out;
2453 }
John Allenc26eba02017-10-26 16:23:25 -05002454
Thomas Falconaf894d22018-04-06 18:37:04 -05002455 ret = 0;
John Allenc26eba02017-10-26 16:23:25 -05002456 if (adapter->reset_done_rc) {
Thomas Falconaf894d22018-04-06 18:37:04 -05002457 ret = -EIO;
John Allenc26eba02017-10-26 16:23:25 -05002458 adapter->desired.mtu = adapter->fallback.mtu;
2459 adapter->desired.rx_queues = adapter->fallback.rx_queues;
2460 adapter->desired.tx_queues = adapter->fallback.tx_queues;
2461 adapter->desired.rx_entries = adapter->fallback.rx_entries;
2462 adapter->desired.tx_entries = adapter->fallback.tx_entries;
2463
Thomas Falcon070eca92019-11-25 17:12:53 -06002464 reinit_completion(&adapter->reset_done);
Thomas Falconaf894d22018-04-06 18:37:04 -05002465 adapter->wait_for_reset = true;
2466 rc = ibmvnic_reset(adapter, VNIC_RESET_CHANGE_PARAM);
Thomas Falcon476d96c2019-11-25 17:12:55 -06002467 if (rc) {
2468 ret = rc;
2469 goto out;
2470 }
2471 rc = ibmvnic_wait_for_completion(adapter, &adapter->reset_done,
2472 60000);
2473 if (rc) {
2474 ret = -ENODEV;
2475 goto out;
2476 }
John Allenc26eba02017-10-26 16:23:25 -05002477 }
Thomas Falcon476d96c2019-11-25 17:12:55 -06002478out:
John Allenc26eba02017-10-26 16:23:25 -05002479 adapter->wait_for_reset = false;
2480
Thomas Falconaf894d22018-04-06 18:37:04 -05002481 return ret;
John Allenc26eba02017-10-26 16:23:25 -05002482}
2483
John Allen3a807b72017-06-06 16:55:52 -05002484static int ibmvnic_change_mtu(struct net_device *netdev, int new_mtu)
2485{
John Allenc26eba02017-10-26 16:23:25 -05002486 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2487
2488 adapter->desired.mtu = new_mtu + ETH_HLEN;
2489
2490 return wait_for_reset(adapter);
John Allen3a807b72017-06-06 16:55:52 -05002491}
2492
Thomas Falconf10b09e2018-03-12 11:51:05 -05002493static netdev_features_t ibmvnic_features_check(struct sk_buff *skb,
2494 struct net_device *dev,
2495 netdev_features_t features)
2496{
2497 /* Some backing hardware adapters can not
2498 * handle packets with a MSS less than 224
2499 * or with only one segment.
2500 */
2501 if (skb_is_gso(skb)) {
2502 if (skb_shinfo(skb)->gso_size < 224 ||
2503 skb_shinfo(skb)->gso_segs == 1)
2504 features &= ~NETIF_F_GSO_MASK;
2505 }
2506
2507 return features;
2508}
2509
Thomas Falcon032c5e82015-12-21 11:26:06 -06002510static const struct net_device_ops ibmvnic_netdev_ops = {
2511 .ndo_open = ibmvnic_open,
2512 .ndo_stop = ibmvnic_close,
2513 .ndo_start_xmit = ibmvnic_xmit,
2514 .ndo_set_rx_mode = ibmvnic_set_multi,
2515 .ndo_set_mac_address = ibmvnic_set_mac,
2516 .ndo_validate_addr = eth_validate_addr,
Thomas Falcon032c5e82015-12-21 11:26:06 -06002517 .ndo_tx_timeout = ibmvnic_tx_timeout,
John Allen3a807b72017-06-06 16:55:52 -05002518 .ndo_change_mtu = ibmvnic_change_mtu,
Thomas Falconf10b09e2018-03-12 11:51:05 -05002519 .ndo_features_check = ibmvnic_features_check,
Thomas Falcon032c5e82015-12-21 11:26:06 -06002520};
2521
2522/* ethtool functions */
2523
Philippe Reynes8a433792017-01-07 22:37:29 +01002524static int ibmvnic_get_link_ksettings(struct net_device *netdev,
2525 struct ethtool_link_ksettings *cmd)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002526{
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03002527 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2528 int rc;
Philippe Reynes8a433792017-01-07 22:37:29 +01002529
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03002530 rc = send_query_phys_parms(adapter);
2531 if (rc) {
2532 adapter->speed = SPEED_UNKNOWN;
2533 adapter->duplex = DUPLEX_UNKNOWN;
2534 }
2535 cmd->base.speed = adapter->speed;
2536 cmd->base.duplex = adapter->duplex;
Philippe Reynes8a433792017-01-07 22:37:29 +01002537 cmd->base.port = PORT_FIBRE;
2538 cmd->base.phy_address = 0;
2539 cmd->base.autoneg = AUTONEG_ENABLE;
2540
Thomas Falcon032c5e82015-12-21 11:26:06 -06002541 return 0;
2542}
2543
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02002544static void ibmvnic_get_drvinfo(struct net_device *netdev,
Thomas Falcon032c5e82015-12-21 11:26:06 -06002545 struct ethtool_drvinfo *info)
2546{
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02002547 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2548
Thomas Falcon032c5e82015-12-21 11:26:06 -06002549 strlcpy(info->driver, ibmvnic_driver_name, sizeof(info->driver));
2550 strlcpy(info->version, IBMVNIC_DRIVER_VERSION, sizeof(info->version));
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02002551 strlcpy(info->fw_version, adapter->fw_version,
2552 sizeof(info->fw_version));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002553}
2554
2555static u32 ibmvnic_get_msglevel(struct net_device *netdev)
2556{
2557 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2558
2559 return adapter->msg_enable;
2560}
2561
2562static void ibmvnic_set_msglevel(struct net_device *netdev, u32 data)
2563{
2564 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2565
2566 adapter->msg_enable = data;
2567}
2568
2569static u32 ibmvnic_get_link(struct net_device *netdev)
2570{
2571 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2572
2573 /* Don't need to send a query because we request a logical link up at
2574 * init and then we wait for link state indications
2575 */
2576 return adapter->logical_link_state;
2577}
2578
2579static void ibmvnic_get_ringparam(struct net_device *netdev,
2580 struct ethtool_ringparam *ring)
2581{
John Allenbc131b32017-08-02 16:46:30 -05002582 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2583
Thomas Falcon723ad912018-09-28 18:38:26 -05002584 if (adapter->priv_flags & IBMVNIC_USE_SERVER_MAXES) {
2585 ring->rx_max_pending = adapter->max_rx_add_entries_per_subcrq;
2586 ring->tx_max_pending = adapter->max_tx_entries_per_subcrq;
2587 } else {
2588 ring->rx_max_pending = IBMVNIC_MAX_QUEUE_SZ;
2589 ring->tx_max_pending = IBMVNIC_MAX_QUEUE_SZ;
2590 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002591 ring->rx_mini_max_pending = 0;
2592 ring->rx_jumbo_max_pending = 0;
John Allenbc131b32017-08-02 16:46:30 -05002593 ring->rx_pending = adapter->req_rx_add_entries_per_subcrq;
2594 ring->tx_pending = adapter->req_tx_entries_per_subcrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002595 ring->rx_mini_pending = 0;
2596 ring->rx_jumbo_pending = 0;
2597}
2598
John Allenc26eba02017-10-26 16:23:25 -05002599static int ibmvnic_set_ringparam(struct net_device *netdev,
2600 struct ethtool_ringparam *ring)
2601{
2602 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Thomas Falcon723ad912018-09-28 18:38:26 -05002603 int ret;
John Allenc26eba02017-10-26 16:23:25 -05002604
Thomas Falcon723ad912018-09-28 18:38:26 -05002605 ret = 0;
John Allenc26eba02017-10-26 16:23:25 -05002606 adapter->desired.rx_entries = ring->rx_pending;
2607 adapter->desired.tx_entries = ring->tx_pending;
2608
Thomas Falcon723ad912018-09-28 18:38:26 -05002609 ret = wait_for_reset(adapter);
2610
2611 if (!ret &&
2612 (adapter->req_rx_add_entries_per_subcrq != ring->rx_pending ||
2613 adapter->req_tx_entries_per_subcrq != ring->tx_pending))
2614 netdev_info(netdev,
2615 "Could not match full ringsize request. Requested: RX %d, TX %d; Allowed: RX %llu, TX %llu\n",
2616 ring->rx_pending, ring->tx_pending,
2617 adapter->req_rx_add_entries_per_subcrq,
2618 adapter->req_tx_entries_per_subcrq);
2619 return ret;
John Allenc26eba02017-10-26 16:23:25 -05002620}
2621
John Allenc2dbeb62017-08-02 16:47:17 -05002622static void ibmvnic_get_channels(struct net_device *netdev,
2623 struct ethtool_channels *channels)
2624{
2625 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2626
Thomas Falcon723ad912018-09-28 18:38:26 -05002627 if (adapter->priv_flags & IBMVNIC_USE_SERVER_MAXES) {
2628 channels->max_rx = adapter->max_rx_queues;
2629 channels->max_tx = adapter->max_tx_queues;
2630 } else {
2631 channels->max_rx = IBMVNIC_MAX_QUEUES;
2632 channels->max_tx = IBMVNIC_MAX_QUEUES;
2633 }
2634
John Allenc2dbeb62017-08-02 16:47:17 -05002635 channels->max_other = 0;
2636 channels->max_combined = 0;
2637 channels->rx_count = adapter->req_rx_queues;
2638 channels->tx_count = adapter->req_tx_queues;
2639 channels->other_count = 0;
2640 channels->combined_count = 0;
2641}
2642
John Allenc26eba02017-10-26 16:23:25 -05002643static int ibmvnic_set_channels(struct net_device *netdev,
2644 struct ethtool_channels *channels)
2645{
2646 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Thomas Falcon723ad912018-09-28 18:38:26 -05002647 int ret;
John Allenc26eba02017-10-26 16:23:25 -05002648
Thomas Falcon723ad912018-09-28 18:38:26 -05002649 ret = 0;
John Allenc26eba02017-10-26 16:23:25 -05002650 adapter->desired.rx_queues = channels->rx_count;
2651 adapter->desired.tx_queues = channels->tx_count;
2652
Thomas Falcon723ad912018-09-28 18:38:26 -05002653 ret = wait_for_reset(adapter);
2654
2655 if (!ret &&
2656 (adapter->req_rx_queues != channels->rx_count ||
2657 adapter->req_tx_queues != channels->tx_count))
2658 netdev_info(netdev,
2659 "Could not match full channels request. Requested: RX %d, TX %d; Allowed: RX %llu, TX %llu\n",
2660 channels->rx_count, channels->tx_count,
2661 adapter->req_rx_queues, adapter->req_tx_queues);
2662 return ret;
2663
John Allenc26eba02017-10-26 16:23:25 -05002664}
2665
Thomas Falcon032c5e82015-12-21 11:26:06 -06002666static void ibmvnic_get_strings(struct net_device *dev, u32 stringset, u8 *data)
2667{
John Allen3d52b592017-08-02 16:44:14 -05002668 struct ibmvnic_adapter *adapter = netdev_priv(dev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002669 int i;
2670
Thomas Falcon723ad912018-09-28 18:38:26 -05002671 switch (stringset) {
2672 case ETH_SS_STATS:
2673 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats);
2674 i++, data += ETH_GSTRING_LEN)
2675 memcpy(data, ibmvnic_stats[i].name, ETH_GSTRING_LEN);
2676
2677 for (i = 0; i < adapter->req_tx_queues; i++) {
2678 snprintf(data, ETH_GSTRING_LEN, "tx%d_packets", i);
2679 data += ETH_GSTRING_LEN;
2680
2681 snprintf(data, ETH_GSTRING_LEN, "tx%d_bytes", i);
2682 data += ETH_GSTRING_LEN;
2683
2684 snprintf(data, ETH_GSTRING_LEN,
2685 "tx%d_dropped_packets", i);
2686 data += ETH_GSTRING_LEN;
2687 }
2688
2689 for (i = 0; i < adapter->req_rx_queues; i++) {
2690 snprintf(data, ETH_GSTRING_LEN, "rx%d_packets", i);
2691 data += ETH_GSTRING_LEN;
2692
2693 snprintf(data, ETH_GSTRING_LEN, "rx%d_bytes", i);
2694 data += ETH_GSTRING_LEN;
2695
2696 snprintf(data, ETH_GSTRING_LEN, "rx%d_interrupts", i);
2697 data += ETH_GSTRING_LEN;
2698 }
2699 break;
2700
2701 case ETH_SS_PRIV_FLAGS:
2702 for (i = 0; i < ARRAY_SIZE(ibmvnic_priv_flags); i++)
2703 strcpy(data + i * ETH_GSTRING_LEN,
2704 ibmvnic_priv_flags[i]);
2705 break;
2706 default:
Thomas Falcon032c5e82015-12-21 11:26:06 -06002707 return;
John Allen3d52b592017-08-02 16:44:14 -05002708 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002709}
2710
2711static int ibmvnic_get_sset_count(struct net_device *dev, int sset)
2712{
John Allen3d52b592017-08-02 16:44:14 -05002713 struct ibmvnic_adapter *adapter = netdev_priv(dev);
2714
Thomas Falcon032c5e82015-12-21 11:26:06 -06002715 switch (sset) {
2716 case ETH_SS_STATS:
John Allen3d52b592017-08-02 16:44:14 -05002717 return ARRAY_SIZE(ibmvnic_stats) +
2718 adapter->req_tx_queues * NUM_TX_STATS +
2719 adapter->req_rx_queues * NUM_RX_STATS;
Thomas Falcon723ad912018-09-28 18:38:26 -05002720 case ETH_SS_PRIV_FLAGS:
2721 return ARRAY_SIZE(ibmvnic_priv_flags);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002722 default:
2723 return -EOPNOTSUPP;
2724 }
2725}
2726
2727static void ibmvnic_get_ethtool_stats(struct net_device *dev,
2728 struct ethtool_stats *stats, u64 *data)
2729{
2730 struct ibmvnic_adapter *adapter = netdev_priv(dev);
2731 union ibmvnic_crq crq;
John Allen3d52b592017-08-02 16:44:14 -05002732 int i, j;
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05002733 int rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002734
2735 memset(&crq, 0, sizeof(crq));
2736 crq.request_statistics.first = IBMVNIC_CRQ_CMD;
2737 crq.request_statistics.cmd = REQUEST_STATISTICS;
2738 crq.request_statistics.ioba = cpu_to_be32(adapter->stats_token);
2739 crq.request_statistics.len =
2740 cpu_to_be32(sizeof(struct ibmvnic_statistics));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002741
2742 /* Wait for data to be written */
Thomas Falcon070eca92019-11-25 17:12:53 -06002743 reinit_completion(&adapter->stats_done);
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05002744 rc = ibmvnic_send_crq(adapter, &crq);
2745 if (rc)
2746 return;
Thomas Falcon476d96c2019-11-25 17:12:55 -06002747 rc = ibmvnic_wait_for_completion(adapter, &adapter->stats_done, 10000);
2748 if (rc)
2749 return;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002750
2751 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++)
John Allen52da5c12017-08-02 16:45:28 -05002752 data[i] = be64_to_cpu(IBMVNIC_GET_STAT(adapter,
2753 ibmvnic_stats[i].offset));
John Allen3d52b592017-08-02 16:44:14 -05002754
2755 for (j = 0; j < adapter->req_tx_queues; j++) {
2756 data[i] = adapter->tx_stats_buffers[j].packets;
2757 i++;
2758 data[i] = adapter->tx_stats_buffers[j].bytes;
2759 i++;
2760 data[i] = adapter->tx_stats_buffers[j].dropped_packets;
2761 i++;
2762 }
2763
2764 for (j = 0; j < adapter->req_rx_queues; j++) {
2765 data[i] = adapter->rx_stats_buffers[j].packets;
2766 i++;
2767 data[i] = adapter->rx_stats_buffers[j].bytes;
2768 i++;
2769 data[i] = adapter->rx_stats_buffers[j].interrupts;
2770 i++;
2771 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002772}
2773
Thomas Falcon723ad912018-09-28 18:38:26 -05002774static u32 ibmvnic_get_priv_flags(struct net_device *netdev)
2775{
2776 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2777
2778 return adapter->priv_flags;
2779}
2780
2781static int ibmvnic_set_priv_flags(struct net_device *netdev, u32 flags)
2782{
2783 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2784 bool which_maxes = !!(flags & IBMVNIC_USE_SERVER_MAXES);
2785
2786 if (which_maxes)
2787 adapter->priv_flags |= IBMVNIC_USE_SERVER_MAXES;
2788 else
2789 adapter->priv_flags &= ~IBMVNIC_USE_SERVER_MAXES;
2790
2791 return 0;
2792}
Thomas Falcon032c5e82015-12-21 11:26:06 -06002793static const struct ethtool_ops ibmvnic_ethtool_ops = {
Thomas Falcon032c5e82015-12-21 11:26:06 -06002794 .get_drvinfo = ibmvnic_get_drvinfo,
2795 .get_msglevel = ibmvnic_get_msglevel,
2796 .set_msglevel = ibmvnic_set_msglevel,
2797 .get_link = ibmvnic_get_link,
2798 .get_ringparam = ibmvnic_get_ringparam,
John Allenc26eba02017-10-26 16:23:25 -05002799 .set_ringparam = ibmvnic_set_ringparam,
John Allenc2dbeb62017-08-02 16:47:17 -05002800 .get_channels = ibmvnic_get_channels,
John Allenc26eba02017-10-26 16:23:25 -05002801 .set_channels = ibmvnic_set_channels,
Thomas Falcon032c5e82015-12-21 11:26:06 -06002802 .get_strings = ibmvnic_get_strings,
2803 .get_sset_count = ibmvnic_get_sset_count,
2804 .get_ethtool_stats = ibmvnic_get_ethtool_stats,
Philippe Reynes8a433792017-01-07 22:37:29 +01002805 .get_link_ksettings = ibmvnic_get_link_ksettings,
Thomas Falcon723ad912018-09-28 18:38:26 -05002806 .get_priv_flags = ibmvnic_get_priv_flags,
2807 .set_priv_flags = ibmvnic_set_priv_flags,
Thomas Falcon032c5e82015-12-21 11:26:06 -06002808};
2809
2810/* Routines for managing CRQs/sCRQs */
2811
Nathan Fontenot57a49432017-05-26 10:31:12 -04002812static int reset_one_sub_crq_queue(struct ibmvnic_adapter *adapter,
2813 struct ibmvnic_sub_crq_queue *scrq)
2814{
2815 int rc;
2816
2817 if (scrq->irq) {
2818 free_irq(scrq->irq, scrq);
2819 irq_dispose_mapping(scrq->irq);
2820 scrq->irq = 0;
2821 }
2822
Thomas Falconc8b2ad02017-06-14 23:50:07 -05002823 memset(scrq->msgs, 0, 4 * PAGE_SIZE);
Thomas Falcon41f71462018-04-06 18:37:03 -05002824 atomic_set(&scrq->used, 0);
Nathan Fontenot57a49432017-05-26 10:31:12 -04002825 scrq->cur = 0;
2826
2827 rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
2828 4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
2829 return rc;
2830}
2831
2832static int reset_sub_crq_queues(struct ibmvnic_adapter *adapter)
2833{
2834 int i, rc;
2835
2836 for (i = 0; i < adapter->req_tx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002837 netdev_dbg(adapter->netdev, "Re-setting tx_scrq[%d]\n", i);
Nathan Fontenot57a49432017-05-26 10:31:12 -04002838 rc = reset_one_sub_crq_queue(adapter, adapter->tx_scrq[i]);
2839 if (rc)
2840 return rc;
2841 }
2842
2843 for (i = 0; i < adapter->req_rx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002844 netdev_dbg(adapter->netdev, "Re-setting rx_scrq[%d]\n", i);
Nathan Fontenot57a49432017-05-26 10:31:12 -04002845 rc = reset_one_sub_crq_queue(adapter, adapter->rx_scrq[i]);
2846 if (rc)
2847 return rc;
2848 }
2849
Nathan Fontenot57a49432017-05-26 10:31:12 -04002850 return rc;
2851}
2852
Thomas Falcon032c5e82015-12-21 11:26:06 -06002853static void release_sub_crq_queue(struct ibmvnic_adapter *adapter,
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06002854 struct ibmvnic_sub_crq_queue *scrq,
2855 bool do_h_free)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002856{
2857 struct device *dev = &adapter->vdev->dev;
2858 long rc;
2859
2860 netdev_dbg(adapter->netdev, "Releasing sub-CRQ\n");
2861
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06002862 if (do_h_free) {
2863 /* Close the sub-crqs */
2864 do {
2865 rc = plpar_hcall_norets(H_FREE_SUB_CRQ,
2866 adapter->vdev->unit_address,
2867 scrq->crq_num);
2868 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002869
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06002870 if (rc) {
2871 netdev_err(adapter->netdev,
2872 "Failed to release sub-CRQ %16lx, rc = %ld\n",
2873 scrq->crq_num, rc);
2874 }
Thomas Falconffa73852017-04-19 13:44:29 -04002875 }
2876
Thomas Falcon032c5e82015-12-21 11:26:06 -06002877 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
2878 DMA_BIDIRECTIONAL);
2879 free_pages((unsigned long)scrq->msgs, 2);
2880 kfree(scrq);
2881}
2882
2883static struct ibmvnic_sub_crq_queue *init_sub_crq_queue(struct ibmvnic_adapter
2884 *adapter)
2885{
2886 struct device *dev = &adapter->vdev->dev;
2887 struct ibmvnic_sub_crq_queue *scrq;
2888 int rc;
2889
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04002890 scrq = kzalloc(sizeof(*scrq), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002891 if (!scrq)
2892 return NULL;
2893
Nathan Fontenot7f7adc52017-04-19 13:45:16 -04002894 scrq->msgs =
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04002895 (union sub_crq *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, 2);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002896 if (!scrq->msgs) {
2897 dev_warn(dev, "Couldn't allocate crq queue messages page\n");
2898 goto zero_page_failed;
2899 }
2900
2901 scrq->msg_token = dma_map_single(dev, scrq->msgs, 4 * PAGE_SIZE,
2902 DMA_BIDIRECTIONAL);
2903 if (dma_mapping_error(dev, scrq->msg_token)) {
2904 dev_warn(dev, "Couldn't map crq queue messages page\n");
2905 goto map_failed;
2906 }
2907
2908 rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
2909 4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
2910
2911 if (rc == H_RESOURCE)
2912 rc = ibmvnic_reset_crq(adapter);
2913
2914 if (rc == H_CLOSED) {
2915 dev_warn(dev, "Partner adapter not ready, waiting.\n");
2916 } else if (rc) {
2917 dev_warn(dev, "Error %d registering sub-crq\n", rc);
2918 goto reg_failed;
2919 }
2920
Thomas Falcon032c5e82015-12-21 11:26:06 -06002921 scrq->adapter = adapter;
2922 scrq->size = 4 * PAGE_SIZE / sizeof(*scrq->msgs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002923 spin_lock_init(&scrq->lock);
2924
2925 netdev_dbg(adapter->netdev,
2926 "sub-crq initialized, num %lx, hw_irq=%lx, irq=%x\n",
2927 scrq->crq_num, scrq->hw_irq, scrq->irq);
2928
2929 return scrq;
2930
Thomas Falcon032c5e82015-12-21 11:26:06 -06002931reg_failed:
2932 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
2933 DMA_BIDIRECTIONAL);
2934map_failed:
2935 free_pages((unsigned long)scrq->msgs, 2);
2936zero_page_failed:
2937 kfree(scrq);
2938
2939 return NULL;
2940}
2941
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06002942static void release_sub_crqs(struct ibmvnic_adapter *adapter, bool do_h_free)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002943{
2944 int i;
2945
2946 if (adapter->tx_scrq) {
Nathan Fontenot82e3be32018-02-21 21:33:56 -06002947 for (i = 0; i < adapter->num_active_tx_scrqs; i++) {
Nathan Fontenotb5108882017-03-30 02:49:18 -04002948 if (!adapter->tx_scrq[i])
2949 continue;
2950
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002951 netdev_dbg(adapter->netdev, "Releasing tx_scrq[%d]\n",
2952 i);
Nathan Fontenotb5108882017-03-30 02:49:18 -04002953 if (adapter->tx_scrq[i]->irq) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06002954 free_irq(adapter->tx_scrq[i]->irq,
2955 adapter->tx_scrq[i]);
Thomas Falcon88eb98a2016-07-06 15:35:16 -05002956 irq_dispose_mapping(adapter->tx_scrq[i]->irq);
Nathan Fontenotb5108882017-03-30 02:49:18 -04002957 adapter->tx_scrq[i]->irq = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002958 }
Nathan Fontenotb5108882017-03-30 02:49:18 -04002959
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06002960 release_sub_crq_queue(adapter, adapter->tx_scrq[i],
2961 do_h_free);
Nathan Fontenotb5108882017-03-30 02:49:18 -04002962 }
2963
Nathan Fontenot9501df32017-03-15 23:38:07 -04002964 kfree(adapter->tx_scrq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002965 adapter->tx_scrq = NULL;
Nathan Fontenot82e3be32018-02-21 21:33:56 -06002966 adapter->num_active_tx_scrqs = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002967 }
2968
2969 if (adapter->rx_scrq) {
Nathan Fontenot82e3be32018-02-21 21:33:56 -06002970 for (i = 0; i < adapter->num_active_rx_scrqs; i++) {
Nathan Fontenotb5108882017-03-30 02:49:18 -04002971 if (!adapter->rx_scrq[i])
2972 continue;
2973
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002974 netdev_dbg(adapter->netdev, "Releasing rx_scrq[%d]\n",
2975 i);
Nathan Fontenotb5108882017-03-30 02:49:18 -04002976 if (adapter->rx_scrq[i]->irq) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06002977 free_irq(adapter->rx_scrq[i]->irq,
2978 adapter->rx_scrq[i]);
Thomas Falcon88eb98a2016-07-06 15:35:16 -05002979 irq_dispose_mapping(adapter->rx_scrq[i]->irq);
Nathan Fontenotb5108882017-03-30 02:49:18 -04002980 adapter->rx_scrq[i]->irq = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002981 }
Nathan Fontenotb5108882017-03-30 02:49:18 -04002982
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06002983 release_sub_crq_queue(adapter, adapter->rx_scrq[i],
2984 do_h_free);
Nathan Fontenotb5108882017-03-30 02:49:18 -04002985 }
2986
Nathan Fontenot9501df32017-03-15 23:38:07 -04002987 kfree(adapter->rx_scrq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002988 adapter->rx_scrq = NULL;
Nathan Fontenot82e3be32018-02-21 21:33:56 -06002989 adapter->num_active_rx_scrqs = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002990 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002991}
2992
2993static int disable_scrq_irq(struct ibmvnic_adapter *adapter,
2994 struct ibmvnic_sub_crq_queue *scrq)
2995{
2996 struct device *dev = &adapter->vdev->dev;
2997 unsigned long rc;
2998
2999 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
3000 H_DISABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
3001 if (rc)
3002 dev_err(dev, "Couldn't disable scrq irq 0x%lx. rc=%ld\n",
3003 scrq->hw_irq, rc);
3004 return rc;
3005}
3006
3007static int enable_scrq_irq(struct ibmvnic_adapter *adapter,
3008 struct ibmvnic_sub_crq_queue *scrq)
3009{
3010 struct device *dev = &adapter->vdev->dev;
3011 unsigned long rc;
3012
3013 if (scrq->hw_irq > 0x100000000ULL) {
3014 dev_err(dev, "bad hw_irq = %lx\n", scrq->hw_irq);
3015 return 1;
3016 }
3017
Juliet Kim7ed5b312019-09-20 16:11:23 -04003018 if (test_bit(0, &adapter->resetting) &&
Nathan Fontenot73f9d362018-05-22 11:21:10 -05003019 adapter->reset_reason == VNIC_RESET_MOBILITY) {
Juliet Kim284f87d2019-11-20 10:50:03 -05003020 u64 val = (0xff000000) | scrq->hw_irq;
Nathan Fontenot73f9d362018-05-22 11:21:10 -05003021
Juliet Kim284f87d2019-11-20 10:50:03 -05003022 rc = plpar_hcall_norets(H_EOI, val);
Juliet Kim2df5c602019-11-20 10:50:04 -05003023 /* H_EOI would fail with rc = H_FUNCTION when running
3024 * in XIVE mode which is expected, but not an error.
3025 */
3026 if (rc && (rc != H_FUNCTION))
Juliet Kim284f87d2019-11-20 10:50:03 -05003027 dev_err(dev, "H_EOI FAILED irq 0x%llx. rc=%ld\n",
3028 val, rc);
Nathan Fontenot73f9d362018-05-22 11:21:10 -05003029 }
Thomas Falconf23e0642018-04-15 18:53:36 -05003030
Thomas Falcon032c5e82015-12-21 11:26:06 -06003031 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
3032 H_ENABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
3033 if (rc)
3034 dev_err(dev, "Couldn't enable scrq irq 0x%lx. rc=%ld\n",
3035 scrq->hw_irq, rc);
3036 return rc;
3037}
3038
3039static int ibmvnic_complete_tx(struct ibmvnic_adapter *adapter,
3040 struct ibmvnic_sub_crq_queue *scrq)
3041{
3042 struct device *dev = &adapter->vdev->dev;
Thomas Falcon06b3e352018-03-16 20:00:28 -05003043 struct ibmvnic_tx_pool *tx_pool;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003044 struct ibmvnic_tx_buff *txbuff;
3045 union sub_crq *next;
3046 int index;
3047 int i, j;
3048
3049restart_loop:
3050 while (pending_scrq(adapter, scrq)) {
3051 unsigned int pool = scrq->pool_index;
Thomas Falconffc385b2018-02-18 10:08:41 -06003052 int num_entries = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003053
3054 next = ibmvnic_next_scrq(adapter, scrq);
3055 for (i = 0; i < next->tx_comp.num_comps; i++) {
3056 if (next->tx_comp.rcs[i]) {
3057 dev_err(dev, "tx error %x\n",
3058 next->tx_comp.rcs[i]);
3059 continue;
3060 }
3061 index = be32_to_cpu(next->tx_comp.correlators[i]);
Thomas Falcon06b3e352018-03-16 20:00:28 -05003062 if (index & IBMVNIC_TSO_POOL_MASK) {
3063 tx_pool = &adapter->tso_pool[pool];
3064 index &= ~IBMVNIC_TSO_POOL_MASK;
3065 } else {
3066 tx_pool = &adapter->tx_pool[pool];
3067 }
3068
3069 txbuff = &tx_pool->tx_buff[index];
Thomas Falcon032c5e82015-12-21 11:26:06 -06003070
3071 for (j = 0; j < IBMVNIC_MAX_FRAGS_PER_CRQ; j++) {
3072 if (!txbuff->data_dma[j])
3073 continue;
3074
3075 txbuff->data_dma[j] = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003076 }
3077
Thomas Falcon142c0ac2017-03-05 12:18:41 -06003078 if (txbuff->last_frag) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06003079 dev_kfree_skb_any(txbuff->skb);
Nathan Fontenot7c3e7de2017-05-03 14:05:25 -04003080 txbuff->skb = NULL;
Thomas Falcon142c0ac2017-03-05 12:18:41 -06003081 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06003082
Thomas Falconffc385b2018-02-18 10:08:41 -06003083 num_entries += txbuff->num_entries;
3084
Thomas Falcon06b3e352018-03-16 20:00:28 -05003085 tx_pool->free_map[tx_pool->producer_index] = index;
3086 tx_pool->producer_index =
3087 (tx_pool->producer_index + 1) %
3088 tx_pool->num_buffers;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003089 }
3090 /* remove tx_comp scrq*/
3091 next->tx_comp.first = 0;
Nathan Fontenot7c3e7de2017-05-03 14:05:25 -04003092
Thomas Falconffc385b2018-02-18 10:08:41 -06003093 if (atomic_sub_return(num_entries, &scrq->used) <=
Nathan Fontenot7c3e7de2017-05-03 14:05:25 -04003094 (adapter->req_tx_entries_per_subcrq / 2) &&
3095 __netif_subqueue_stopped(adapter->netdev,
3096 scrq->pool_index)) {
3097 netif_wake_subqueue(adapter->netdev, scrq->pool_index);
Thomas Falcon0aecb132018-02-26 18:10:58 -06003098 netdev_dbg(adapter->netdev, "Started queue %d\n",
3099 scrq->pool_index);
Nathan Fontenot7c3e7de2017-05-03 14:05:25 -04003100 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06003101 }
3102
3103 enable_scrq_irq(adapter, scrq);
3104
3105 if (pending_scrq(adapter, scrq)) {
3106 disable_scrq_irq(adapter, scrq);
3107 goto restart_loop;
3108 }
3109
3110 return 0;
3111}
3112
3113static irqreturn_t ibmvnic_interrupt_tx(int irq, void *instance)
3114{
3115 struct ibmvnic_sub_crq_queue *scrq = instance;
3116 struct ibmvnic_adapter *adapter = scrq->adapter;
3117
3118 disable_scrq_irq(adapter, scrq);
3119 ibmvnic_complete_tx(adapter, scrq);
3120
3121 return IRQ_HANDLED;
3122}
3123
3124static irqreturn_t ibmvnic_interrupt_rx(int irq, void *instance)
3125{
3126 struct ibmvnic_sub_crq_queue *scrq = instance;
3127 struct ibmvnic_adapter *adapter = scrq->adapter;
3128
Nathan Fontenot09fb35e2018-01-10 10:40:09 -06003129 /* When booting a kdump kernel we can hit pending interrupts
3130 * prior to completing driver initialization.
3131 */
3132 if (unlikely(adapter->state != VNIC_OPEN))
3133 return IRQ_NONE;
3134
John Allen3d52b592017-08-02 16:44:14 -05003135 adapter->rx_stats_buffers[scrq->scrq_num].interrupts++;
3136
Thomas Falcon032c5e82015-12-21 11:26:06 -06003137 if (napi_schedule_prep(&adapter->napi[scrq->scrq_num])) {
3138 disable_scrq_irq(adapter, scrq);
3139 __napi_schedule(&adapter->napi[scrq->scrq_num]);
3140 }
3141
3142 return IRQ_HANDLED;
3143}
3144
Thomas Falconea22d512016-07-06 15:35:17 -05003145static int init_sub_crq_irqs(struct ibmvnic_adapter *adapter)
3146{
3147 struct device *dev = &adapter->vdev->dev;
3148 struct ibmvnic_sub_crq_queue *scrq;
3149 int i = 0, j = 0;
3150 int rc = 0;
3151
3152 for (i = 0; i < adapter->req_tx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05003153 netdev_dbg(adapter->netdev, "Initializing tx_scrq[%d] irq\n",
3154 i);
Thomas Falconea22d512016-07-06 15:35:17 -05003155 scrq = adapter->tx_scrq[i];
3156 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
3157
Michael Ellerman99c17902016-09-10 19:59:05 +10003158 if (!scrq->irq) {
Thomas Falconea22d512016-07-06 15:35:17 -05003159 rc = -EINVAL;
3160 dev_err(dev, "Error mapping irq\n");
3161 goto req_tx_irq_failed;
3162 }
3163
Murilo Fossa Vicentinie56e2512019-04-25 11:02:33 -03003164 snprintf(scrq->name, sizeof(scrq->name), "ibmvnic-%x-tx%d",
3165 adapter->vdev->unit_address, i);
Thomas Falconea22d512016-07-06 15:35:17 -05003166 rc = request_irq(scrq->irq, ibmvnic_interrupt_tx,
Murilo Fossa Vicentinie56e2512019-04-25 11:02:33 -03003167 0, scrq->name, scrq);
Thomas Falconea22d512016-07-06 15:35:17 -05003168
3169 if (rc) {
3170 dev_err(dev, "Couldn't register tx irq 0x%x. rc=%d\n",
3171 scrq->irq, rc);
3172 irq_dispose_mapping(scrq->irq);
Nathan Fontenotaf9090c2018-02-20 11:04:18 -06003173 goto req_tx_irq_failed;
Thomas Falconea22d512016-07-06 15:35:17 -05003174 }
3175 }
3176
3177 for (i = 0; i < adapter->req_rx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05003178 netdev_dbg(adapter->netdev, "Initializing rx_scrq[%d] irq\n",
3179 i);
Thomas Falconea22d512016-07-06 15:35:17 -05003180 scrq = adapter->rx_scrq[i];
3181 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
Michael Ellerman99c17902016-09-10 19:59:05 +10003182 if (!scrq->irq) {
Thomas Falconea22d512016-07-06 15:35:17 -05003183 rc = -EINVAL;
3184 dev_err(dev, "Error mapping irq\n");
3185 goto req_rx_irq_failed;
3186 }
Murilo Fossa Vicentinie56e2512019-04-25 11:02:33 -03003187 snprintf(scrq->name, sizeof(scrq->name), "ibmvnic-%x-rx%d",
3188 adapter->vdev->unit_address, i);
Thomas Falconea22d512016-07-06 15:35:17 -05003189 rc = request_irq(scrq->irq, ibmvnic_interrupt_rx,
Murilo Fossa Vicentinie56e2512019-04-25 11:02:33 -03003190 0, scrq->name, scrq);
Thomas Falconea22d512016-07-06 15:35:17 -05003191 if (rc) {
3192 dev_err(dev, "Couldn't register rx irq 0x%x. rc=%d\n",
3193 scrq->irq, rc);
3194 irq_dispose_mapping(scrq->irq);
3195 goto req_rx_irq_failed;
3196 }
3197 }
3198 return rc;
3199
3200req_rx_irq_failed:
Thomas Falcon8bf371e2016-10-27 12:28:52 -05003201 for (j = 0; j < i; j++) {
Thomas Falconea22d512016-07-06 15:35:17 -05003202 free_irq(adapter->rx_scrq[j]->irq, adapter->rx_scrq[j]);
3203 irq_dispose_mapping(adapter->rx_scrq[j]->irq);
Thomas Falcon8bf371e2016-10-27 12:28:52 -05003204 }
Thomas Falconea22d512016-07-06 15:35:17 -05003205 i = adapter->req_tx_queues;
3206req_tx_irq_failed:
Thomas Falcon8bf371e2016-10-27 12:28:52 -05003207 for (j = 0; j < i; j++) {
Thomas Falconea22d512016-07-06 15:35:17 -05003208 free_irq(adapter->tx_scrq[j]->irq, adapter->tx_scrq[j]);
Thomas Falcon27a21452020-07-29 16:36:32 -05003209 irq_dispose_mapping(adapter->tx_scrq[j]->irq);
Thomas Falcon8bf371e2016-10-27 12:28:52 -05003210 }
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06003211 release_sub_crqs(adapter, 1);
Thomas Falconea22d512016-07-06 15:35:17 -05003212 return rc;
3213}
3214
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003215static int init_sub_crqs(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003216{
3217 struct device *dev = &adapter->vdev->dev;
3218 struct ibmvnic_sub_crq_queue **allqueues;
3219 int registered_queues = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003220 int total_queues;
3221 int more = 0;
Thomas Falconea22d512016-07-06 15:35:17 -05003222 int i;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003223
Thomas Falcon032c5e82015-12-21 11:26:06 -06003224 total_queues = adapter->req_tx_queues + adapter->req_rx_queues;
3225
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04003226 allqueues = kcalloc(total_queues, sizeof(*allqueues), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003227 if (!allqueues)
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003228 return -1;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003229
3230 for (i = 0; i < total_queues; i++) {
3231 allqueues[i] = init_sub_crq_queue(adapter);
3232 if (!allqueues[i]) {
3233 dev_warn(dev, "Couldn't allocate all sub-crqs\n");
3234 break;
3235 }
3236 registered_queues++;
3237 }
3238
3239 /* Make sure we were able to register the minimum number of queues */
3240 if (registered_queues <
3241 adapter->min_tx_queues + adapter->min_rx_queues) {
3242 dev_err(dev, "Fatal: Couldn't init min number of sub-crqs\n");
3243 goto tx_failed;
3244 }
3245
3246 /* Distribute the failed allocated queues*/
3247 for (i = 0; i < total_queues - registered_queues + more ; i++) {
3248 netdev_dbg(adapter->netdev, "Reducing number of queues\n");
3249 switch (i % 3) {
3250 case 0:
3251 if (adapter->req_rx_queues > adapter->min_rx_queues)
3252 adapter->req_rx_queues--;
3253 else
3254 more++;
3255 break;
3256 case 1:
3257 if (adapter->req_tx_queues > adapter->min_tx_queues)
3258 adapter->req_tx_queues--;
3259 else
3260 more++;
3261 break;
3262 }
3263 }
3264
3265 adapter->tx_scrq = kcalloc(adapter->req_tx_queues,
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04003266 sizeof(*adapter->tx_scrq), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003267 if (!adapter->tx_scrq)
3268 goto tx_failed;
3269
3270 for (i = 0; i < adapter->req_tx_queues; i++) {
3271 adapter->tx_scrq[i] = allqueues[i];
3272 adapter->tx_scrq[i]->pool_index = i;
Nathan Fontenot82e3be32018-02-21 21:33:56 -06003273 adapter->num_active_tx_scrqs++;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003274 }
3275
3276 adapter->rx_scrq = kcalloc(adapter->req_rx_queues,
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04003277 sizeof(*adapter->rx_scrq), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003278 if (!adapter->rx_scrq)
3279 goto rx_failed;
3280
3281 for (i = 0; i < adapter->req_rx_queues; i++) {
3282 adapter->rx_scrq[i] = allqueues[i + adapter->req_tx_queues];
3283 adapter->rx_scrq[i]->scrq_num = i;
Nathan Fontenot82e3be32018-02-21 21:33:56 -06003284 adapter->num_active_rx_scrqs++;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003285 }
3286
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003287 kfree(allqueues);
3288 return 0;
3289
3290rx_failed:
3291 kfree(adapter->tx_scrq);
3292 adapter->tx_scrq = NULL;
3293tx_failed:
3294 for (i = 0; i < registered_queues; i++)
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06003295 release_sub_crq_queue(adapter, allqueues[i], 1);
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003296 kfree(allqueues);
3297 return -1;
3298}
3299
3300static void ibmvnic_send_req_caps(struct ibmvnic_adapter *adapter, int retry)
3301{
3302 struct device *dev = &adapter->vdev->dev;
3303 union ibmvnic_crq crq;
John Allenc26eba02017-10-26 16:23:25 -05003304 int max_entries;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003305
3306 if (!retry) {
3307 /* Sub-CRQ entries are 32 byte long */
3308 int entries_page = 4 * PAGE_SIZE / (sizeof(u64) * 4);
3309
3310 if (adapter->min_tx_entries_per_subcrq > entries_page ||
3311 adapter->min_rx_add_entries_per_subcrq > entries_page) {
3312 dev_err(dev, "Fatal, invalid entries per sub-crq\n");
3313 return;
3314 }
3315
John Allenc26eba02017-10-26 16:23:25 -05003316 if (adapter->desired.mtu)
3317 adapter->req_mtu = adapter->desired.mtu;
3318 else
3319 adapter->req_mtu = adapter->netdev->mtu + ETH_HLEN;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003320
John Allenc26eba02017-10-26 16:23:25 -05003321 if (!adapter->desired.tx_entries)
3322 adapter->desired.tx_entries =
3323 adapter->max_tx_entries_per_subcrq;
3324 if (!adapter->desired.rx_entries)
3325 adapter->desired.rx_entries =
3326 adapter->max_rx_add_entries_per_subcrq;
3327
3328 max_entries = IBMVNIC_MAX_LTB_SIZE /
3329 (adapter->req_mtu + IBMVNIC_BUFFER_HLEN);
3330
3331 if ((adapter->req_mtu + IBMVNIC_BUFFER_HLEN) *
3332 adapter->desired.tx_entries > IBMVNIC_MAX_LTB_SIZE) {
3333 adapter->desired.tx_entries = max_entries;
3334 }
3335
3336 if ((adapter->req_mtu + IBMVNIC_BUFFER_HLEN) *
3337 adapter->desired.rx_entries > IBMVNIC_MAX_LTB_SIZE) {
3338 adapter->desired.rx_entries = max_entries;
3339 }
3340
3341 if (adapter->desired.tx_entries)
3342 adapter->req_tx_entries_per_subcrq =
3343 adapter->desired.tx_entries;
3344 else
3345 adapter->req_tx_entries_per_subcrq =
3346 adapter->max_tx_entries_per_subcrq;
3347
3348 if (adapter->desired.rx_entries)
3349 adapter->req_rx_add_entries_per_subcrq =
3350 adapter->desired.rx_entries;
3351 else
3352 adapter->req_rx_add_entries_per_subcrq =
3353 adapter->max_rx_add_entries_per_subcrq;
3354
3355 if (adapter->desired.tx_queues)
3356 adapter->req_tx_queues =
3357 adapter->desired.tx_queues;
3358 else
3359 adapter->req_tx_queues =
3360 adapter->opt_tx_comp_sub_queues;
3361
3362 if (adapter->desired.rx_queues)
3363 adapter->req_rx_queues =
3364 adapter->desired.rx_queues;
3365 else
3366 adapter->req_rx_queues =
3367 adapter->opt_rx_comp_queues;
3368
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003369 adapter->req_rx_add_queues = adapter->max_rx_add_queues;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003370 }
3371
Thomas Falcon032c5e82015-12-21 11:26:06 -06003372 memset(&crq, 0, sizeof(crq));
3373 crq.request_capability.first = IBMVNIC_CRQ_CMD;
3374 crq.request_capability.cmd = REQUEST_CAPABILITY;
3375
3376 crq.request_capability.capability = cpu_to_be16(REQ_TX_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06003377 crq.request_capability.number = cpu_to_be64(adapter->req_tx_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06003378 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003379 ibmvnic_send_crq(adapter, &crq);
3380
3381 crq.request_capability.capability = cpu_to_be16(REQ_RX_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06003382 crq.request_capability.number = cpu_to_be64(adapter->req_rx_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06003383 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003384 ibmvnic_send_crq(adapter, &crq);
3385
3386 crq.request_capability.capability = cpu_to_be16(REQ_RX_ADD_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06003387 crq.request_capability.number = cpu_to_be64(adapter->req_rx_add_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06003388 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003389 ibmvnic_send_crq(adapter, &crq);
3390
3391 crq.request_capability.capability =
3392 cpu_to_be16(REQ_TX_ENTRIES_PER_SUBCRQ);
3393 crq.request_capability.number =
Thomas Falconde89e852016-03-01 10:20:09 -06003394 cpu_to_be64(adapter->req_tx_entries_per_subcrq);
Thomas Falcon901e0402017-02-15 12:17:59 -06003395 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003396 ibmvnic_send_crq(adapter, &crq);
3397
3398 crq.request_capability.capability =
3399 cpu_to_be16(REQ_RX_ADD_ENTRIES_PER_SUBCRQ);
3400 crq.request_capability.number =
Thomas Falconde89e852016-03-01 10:20:09 -06003401 cpu_to_be64(adapter->req_rx_add_entries_per_subcrq);
Thomas Falcon901e0402017-02-15 12:17:59 -06003402 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003403 ibmvnic_send_crq(adapter, &crq);
3404
3405 crq.request_capability.capability = cpu_to_be16(REQ_MTU);
Thomas Falconde89e852016-03-01 10:20:09 -06003406 crq.request_capability.number = cpu_to_be64(adapter->req_mtu);
Thomas Falcon901e0402017-02-15 12:17:59 -06003407 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003408 ibmvnic_send_crq(adapter, &crq);
3409
3410 if (adapter->netdev->flags & IFF_PROMISC) {
3411 if (adapter->promisc_supported) {
3412 crq.request_capability.capability =
3413 cpu_to_be16(PROMISC_REQUESTED);
Thomas Falconde89e852016-03-01 10:20:09 -06003414 crq.request_capability.number = cpu_to_be64(1);
Thomas Falcon901e0402017-02-15 12:17:59 -06003415 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003416 ibmvnic_send_crq(adapter, &crq);
3417 }
3418 } else {
3419 crq.request_capability.capability =
3420 cpu_to_be16(PROMISC_REQUESTED);
Thomas Falconde89e852016-03-01 10:20:09 -06003421 crq.request_capability.number = cpu_to_be64(0);
Thomas Falcon901e0402017-02-15 12:17:59 -06003422 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003423 ibmvnic_send_crq(adapter, &crq);
3424 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06003425}
3426
3427static int pending_scrq(struct ibmvnic_adapter *adapter,
3428 struct ibmvnic_sub_crq_queue *scrq)
3429{
3430 union sub_crq *entry = &scrq->msgs[scrq->cur];
3431
Thomas Falcon1cf9cc72017-06-14 23:50:08 -05003432 if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003433 return 1;
3434 else
3435 return 0;
3436}
3437
3438static union sub_crq *ibmvnic_next_scrq(struct ibmvnic_adapter *adapter,
3439 struct ibmvnic_sub_crq_queue *scrq)
3440{
3441 union sub_crq *entry;
3442 unsigned long flags;
3443
3444 spin_lock_irqsave(&scrq->lock, flags);
3445 entry = &scrq->msgs[scrq->cur];
3446 if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP) {
3447 if (++scrq->cur == scrq->size)
3448 scrq->cur = 0;
3449 } else {
3450 entry = NULL;
3451 }
3452 spin_unlock_irqrestore(&scrq->lock, flags);
3453
3454 return entry;
3455}
3456
3457static union ibmvnic_crq *ibmvnic_next_crq(struct ibmvnic_adapter *adapter)
3458{
3459 struct ibmvnic_crq_queue *queue = &adapter->crq;
3460 union ibmvnic_crq *crq;
3461
3462 crq = &queue->msgs[queue->cur];
3463 if (crq->generic.first & IBMVNIC_CRQ_CMD_RSP) {
3464 if (++queue->cur == queue->size)
3465 queue->cur = 0;
3466 } else {
3467 crq = NULL;
3468 }
3469
3470 return crq;
3471}
3472
Thomas Falcon2d14d372018-07-13 12:03:32 -05003473static void print_subcrq_error(struct device *dev, int rc, const char *func)
3474{
3475 switch (rc) {
3476 case H_PARAMETER:
3477 dev_warn_ratelimited(dev,
3478 "%s failed: Send request is malformed or adapter failover pending. (rc=%d)\n",
3479 func, rc);
3480 break;
3481 case H_CLOSED:
3482 dev_warn_ratelimited(dev,
3483 "%s failed: Backing queue closed. Adapter is down or failover pending. (rc=%d)\n",
3484 func, rc);
3485 break;
3486 default:
3487 dev_err_ratelimited(dev, "%s failed: (rc=%d)\n", func, rc);
3488 break;
3489 }
3490}
3491
Thomas Falcon032c5e82015-12-21 11:26:06 -06003492static int send_subcrq(struct ibmvnic_adapter *adapter, u64 remote_handle,
3493 union sub_crq *sub_crq)
3494{
3495 unsigned int ua = adapter->vdev->unit_address;
3496 struct device *dev = &adapter->vdev->dev;
3497 u64 *u64_crq = (u64 *)sub_crq;
3498 int rc;
3499
3500 netdev_dbg(adapter->netdev,
3501 "Sending sCRQ %016lx: %016lx %016lx %016lx %016lx\n",
3502 (unsigned long int)cpu_to_be64(remote_handle),
3503 (unsigned long int)cpu_to_be64(u64_crq[0]),
3504 (unsigned long int)cpu_to_be64(u64_crq[1]),
3505 (unsigned long int)cpu_to_be64(u64_crq[2]),
3506 (unsigned long int)cpu_to_be64(u64_crq[3]));
3507
3508 /* Make sure the hypervisor sees the complete request */
3509 mb();
3510
3511 rc = plpar_hcall_norets(H_SEND_SUB_CRQ, ua,
3512 cpu_to_be64(remote_handle),
3513 cpu_to_be64(u64_crq[0]),
3514 cpu_to_be64(u64_crq[1]),
3515 cpu_to_be64(u64_crq[2]),
3516 cpu_to_be64(u64_crq[3]));
3517
Thomas Falcon2d14d372018-07-13 12:03:32 -05003518 if (rc)
3519 print_subcrq_error(dev, rc, __func__);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003520
3521 return rc;
3522}
3523
Thomas Falconad7775d2016-04-01 17:20:34 -05003524static int send_subcrq_indirect(struct ibmvnic_adapter *adapter,
3525 u64 remote_handle, u64 ioba, u64 num_entries)
3526{
3527 unsigned int ua = adapter->vdev->unit_address;
3528 struct device *dev = &adapter->vdev->dev;
3529 int rc;
3530
3531 /* Make sure the hypervisor sees the complete request */
3532 mb();
3533 rc = plpar_hcall_norets(H_SEND_SUB_CRQ_INDIRECT, ua,
3534 cpu_to_be64(remote_handle),
3535 ioba, num_entries);
3536
Thomas Falcon2d14d372018-07-13 12:03:32 -05003537 if (rc)
3538 print_subcrq_error(dev, rc, __func__);
Thomas Falconad7775d2016-04-01 17:20:34 -05003539
3540 return rc;
3541}
3542
Thomas Falcon032c5e82015-12-21 11:26:06 -06003543static int ibmvnic_send_crq(struct ibmvnic_adapter *adapter,
3544 union ibmvnic_crq *crq)
3545{
3546 unsigned int ua = adapter->vdev->unit_address;
3547 struct device *dev = &adapter->vdev->dev;
3548 u64 *u64_crq = (u64 *)crq;
3549 int rc;
3550
3551 netdev_dbg(adapter->netdev, "Sending CRQ: %016lx %016lx\n",
3552 (unsigned long int)cpu_to_be64(u64_crq[0]),
3553 (unsigned long int)cpu_to_be64(u64_crq[1]));
3554
Thomas Falcon51536982018-05-23 13:37:56 -05003555 if (!adapter->crq.active &&
3556 crq->generic.first != IBMVNIC_CRQ_INIT_CMD) {
3557 dev_warn(dev, "Invalid request detected while CRQ is inactive, possible device state change during reset\n");
3558 return -EINVAL;
3559 }
3560
Thomas Falcon032c5e82015-12-21 11:26:06 -06003561 /* Make sure the hypervisor sees the complete request */
3562 mb();
3563
3564 rc = plpar_hcall_norets(H_SEND_CRQ, ua,
3565 cpu_to_be64(u64_crq[0]),
3566 cpu_to_be64(u64_crq[1]));
3567
3568 if (rc) {
Nathan Fontenotec95dff2018-02-07 13:00:24 -06003569 if (rc == H_CLOSED) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06003570 dev_warn(dev, "CRQ Queue closed\n");
Lijun Panfa68bfa2020-08-19 17:52:24 -05003571 /* do not reset, report the fail, wait for passive init from server */
Nathan Fontenotec95dff2018-02-07 13:00:24 -06003572 }
3573
Thomas Falcon032c5e82015-12-21 11:26:06 -06003574 dev_warn(dev, "Send error (rc=%d)\n", rc);
3575 }
3576
3577 return rc;
3578}
3579
3580static int ibmvnic_send_crq_init(struct ibmvnic_adapter *adapter)
3581{
Thomas Falcon36a782f2020-08-31 11:59:57 -05003582 struct device *dev = &adapter->vdev->dev;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003583 union ibmvnic_crq crq;
Thomas Falcon36a782f2020-08-31 11:59:57 -05003584 int retries = 100;
3585 int rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003586
3587 memset(&crq, 0, sizeof(crq));
3588 crq.generic.first = IBMVNIC_CRQ_INIT_CMD;
3589 crq.generic.cmd = IBMVNIC_CRQ_INIT;
3590 netdev_dbg(adapter->netdev, "Sending CRQ init\n");
3591
Thomas Falcon36a782f2020-08-31 11:59:57 -05003592 do {
3593 rc = ibmvnic_send_crq(adapter, &crq);
3594 if (rc != H_CLOSED)
3595 break;
3596 retries--;
3597 msleep(50);
3598
3599 } while (retries > 0);
3600
3601 if (rc) {
3602 dev_err(dev, "Failed to send init request, rc = %d\n", rc);
3603 return rc;
3604 }
3605
3606 return 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003607}
3608
Thomas Falcon032c5e82015-12-21 11:26:06 -06003609static int send_version_xchg(struct ibmvnic_adapter *adapter)
3610{
3611 union ibmvnic_crq crq;
3612
3613 memset(&crq, 0, sizeof(crq));
3614 crq.version_exchange.first = IBMVNIC_CRQ_CMD;
3615 crq.version_exchange.cmd = VERSION_EXCHANGE;
3616 crq.version_exchange.version = cpu_to_be16(ibmvnic_version);
3617
3618 return ibmvnic_send_crq(adapter, &crq);
3619}
3620
Nathan Fontenot37798d02017-11-08 11:23:56 -06003621struct vnic_login_client_data {
3622 u8 type;
3623 __be16 len;
Kees Cook08ea5562018-04-10 15:26:43 -07003624 char name[];
Nathan Fontenot37798d02017-11-08 11:23:56 -06003625} __packed;
3626
3627static int vnic_client_data_len(struct ibmvnic_adapter *adapter)
3628{
3629 int len;
3630
3631 /* Calculate the amount of buffer space needed for the
3632 * vnic client data in the login buffer. There are four entries,
3633 * OS name, LPAR name, device name, and a null last entry.
3634 */
3635 len = 4 * sizeof(struct vnic_login_client_data);
3636 len += 6; /* "Linux" plus NULL */
3637 len += strlen(utsname()->nodename) + 1;
3638 len += strlen(adapter->netdev->name) + 1;
3639
3640 return len;
3641}
3642
3643static void vnic_add_client_data(struct ibmvnic_adapter *adapter,
3644 struct vnic_login_client_data *vlcd)
3645{
3646 const char *os_name = "Linux";
3647 int len;
3648
3649 /* Type 1 - LPAR OS */
3650 vlcd->type = 1;
3651 len = strlen(os_name) + 1;
3652 vlcd->len = cpu_to_be16(len);
Kees Cook08ea5562018-04-10 15:26:43 -07003653 strncpy(vlcd->name, os_name, len);
3654 vlcd = (struct vnic_login_client_data *)(vlcd->name + len);
Nathan Fontenot37798d02017-11-08 11:23:56 -06003655
3656 /* Type 2 - LPAR name */
3657 vlcd->type = 2;
3658 len = strlen(utsname()->nodename) + 1;
3659 vlcd->len = cpu_to_be16(len);
Kees Cook08ea5562018-04-10 15:26:43 -07003660 strncpy(vlcd->name, utsname()->nodename, len);
3661 vlcd = (struct vnic_login_client_data *)(vlcd->name + len);
Nathan Fontenot37798d02017-11-08 11:23:56 -06003662
3663 /* Type 3 - device name */
3664 vlcd->type = 3;
3665 len = strlen(adapter->netdev->name) + 1;
3666 vlcd->len = cpu_to_be16(len);
Kees Cook08ea5562018-04-10 15:26:43 -07003667 strncpy(vlcd->name, adapter->netdev->name, len);
Nathan Fontenot37798d02017-11-08 11:23:56 -06003668}
3669
Thomas Falcon20a8ab72018-02-26 18:10:59 -06003670static int send_login(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003671{
3672 struct ibmvnic_login_rsp_buffer *login_rsp_buffer;
3673 struct ibmvnic_login_buffer *login_buffer;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003674 struct device *dev = &adapter->vdev->dev;
3675 dma_addr_t rsp_buffer_token;
3676 dma_addr_t buffer_token;
3677 size_t rsp_buffer_size;
3678 union ibmvnic_crq crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003679 size_t buffer_size;
3680 __be64 *tx_list_p;
3681 __be64 *rx_list_p;
Nathan Fontenot37798d02017-11-08 11:23:56 -06003682 int client_data_len;
3683 struct vnic_login_client_data *vlcd;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003684 int i;
3685
Thomas Falcon20a8ab72018-02-26 18:10:59 -06003686 if (!adapter->tx_scrq || !adapter->rx_scrq) {
3687 netdev_err(adapter->netdev,
3688 "RX or TX queues are not allocated, device login failed\n");
3689 return -1;
3690 }
3691
Thomas Falcon34f0f4e2018-02-13 18:23:40 -06003692 release_login_rsp_buffer(adapter);
Nathan Fontenot37798d02017-11-08 11:23:56 -06003693 client_data_len = vnic_client_data_len(adapter);
3694
Thomas Falcon032c5e82015-12-21 11:26:06 -06003695 buffer_size =
3696 sizeof(struct ibmvnic_login_buffer) +
Nathan Fontenot37798d02017-11-08 11:23:56 -06003697 sizeof(u64) * (adapter->req_tx_queues + adapter->req_rx_queues) +
3698 client_data_len;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003699
Nathan Fontenot37798d02017-11-08 11:23:56 -06003700 login_buffer = kzalloc(buffer_size, GFP_ATOMIC);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003701 if (!login_buffer)
3702 goto buf_alloc_failed;
3703
3704 buffer_token = dma_map_single(dev, login_buffer, buffer_size,
3705 DMA_TO_DEVICE);
3706 if (dma_mapping_error(dev, buffer_token)) {
3707 dev_err(dev, "Couldn't map login buffer\n");
3708 goto buf_map_failed;
3709 }
3710
John Allen498cd8e2016-04-06 11:49:55 -05003711 rsp_buffer_size = sizeof(struct ibmvnic_login_rsp_buffer) +
3712 sizeof(u64) * adapter->req_tx_queues +
3713 sizeof(u64) * adapter->req_rx_queues +
3714 sizeof(u64) * adapter->req_rx_queues +
3715 sizeof(u8) * IBMVNIC_TX_DESC_VERSIONS;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003716
3717 login_rsp_buffer = kmalloc(rsp_buffer_size, GFP_ATOMIC);
3718 if (!login_rsp_buffer)
3719 goto buf_rsp_alloc_failed;
3720
3721 rsp_buffer_token = dma_map_single(dev, login_rsp_buffer,
3722 rsp_buffer_size, DMA_FROM_DEVICE);
3723 if (dma_mapping_error(dev, rsp_buffer_token)) {
3724 dev_err(dev, "Couldn't map login rsp buffer\n");
3725 goto buf_rsp_map_failed;
3726 }
Nathan Fontenot661a2622017-04-19 13:44:58 -04003727
Thomas Falcon032c5e82015-12-21 11:26:06 -06003728 adapter->login_buf = login_buffer;
3729 adapter->login_buf_token = buffer_token;
3730 adapter->login_buf_sz = buffer_size;
3731 adapter->login_rsp_buf = login_rsp_buffer;
3732 adapter->login_rsp_buf_token = rsp_buffer_token;
3733 adapter->login_rsp_buf_sz = rsp_buffer_size;
3734
3735 login_buffer->len = cpu_to_be32(buffer_size);
3736 login_buffer->version = cpu_to_be32(INITIAL_VERSION_LB);
3737 login_buffer->num_txcomp_subcrqs = cpu_to_be32(adapter->req_tx_queues);
3738 login_buffer->off_txcomp_subcrqs =
3739 cpu_to_be32(sizeof(struct ibmvnic_login_buffer));
3740 login_buffer->num_rxcomp_subcrqs = cpu_to_be32(adapter->req_rx_queues);
3741 login_buffer->off_rxcomp_subcrqs =
3742 cpu_to_be32(sizeof(struct ibmvnic_login_buffer) +
3743 sizeof(u64) * adapter->req_tx_queues);
3744 login_buffer->login_rsp_ioba = cpu_to_be32(rsp_buffer_token);
3745 login_buffer->login_rsp_len = cpu_to_be32(rsp_buffer_size);
3746
3747 tx_list_p = (__be64 *)((char *)login_buffer +
3748 sizeof(struct ibmvnic_login_buffer));
3749 rx_list_p = (__be64 *)((char *)login_buffer +
3750 sizeof(struct ibmvnic_login_buffer) +
3751 sizeof(u64) * adapter->req_tx_queues);
3752
3753 for (i = 0; i < adapter->req_tx_queues; i++) {
3754 if (adapter->tx_scrq[i]) {
3755 tx_list_p[i] = cpu_to_be64(adapter->tx_scrq[i]->
3756 crq_num);
3757 }
3758 }
3759
3760 for (i = 0; i < adapter->req_rx_queues; i++) {
3761 if (adapter->rx_scrq[i]) {
3762 rx_list_p[i] = cpu_to_be64(adapter->rx_scrq[i]->
3763 crq_num);
3764 }
3765 }
3766
Nathan Fontenot37798d02017-11-08 11:23:56 -06003767 /* Insert vNIC login client data */
3768 vlcd = (struct vnic_login_client_data *)
3769 ((char *)rx_list_p + (sizeof(u64) * adapter->req_rx_queues));
3770 login_buffer->client_data_offset =
3771 cpu_to_be32((char *)vlcd - (char *)login_buffer);
3772 login_buffer->client_data_len = cpu_to_be32(client_data_len);
3773
3774 vnic_add_client_data(adapter, vlcd);
3775
Thomas Falcon032c5e82015-12-21 11:26:06 -06003776 netdev_dbg(adapter->netdev, "Login Buffer:\n");
3777 for (i = 0; i < (adapter->login_buf_sz - 1) / 8 + 1; i++) {
3778 netdev_dbg(adapter->netdev, "%016lx\n",
3779 ((unsigned long int *)(adapter->login_buf))[i]);
3780 }
3781
3782 memset(&crq, 0, sizeof(crq));
3783 crq.login.first = IBMVNIC_CRQ_CMD;
3784 crq.login.cmd = LOGIN;
3785 crq.login.ioba = cpu_to_be32(buffer_token);
3786 crq.login.len = cpu_to_be32(buffer_size);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003787 ibmvnic_send_crq(adapter, &crq);
3788
Thomas Falcon20a8ab72018-02-26 18:10:59 -06003789 return 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003790
Thomas Falcon032c5e82015-12-21 11:26:06 -06003791buf_rsp_map_failed:
3792 kfree(login_rsp_buffer);
3793buf_rsp_alloc_failed:
3794 dma_unmap_single(dev, buffer_token, buffer_size, DMA_TO_DEVICE);
3795buf_map_failed:
3796 kfree(login_buffer);
3797buf_alloc_failed:
Thomas Falcon20a8ab72018-02-26 18:10:59 -06003798 return -1;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003799}
3800
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05003801static int send_request_map(struct ibmvnic_adapter *adapter, dma_addr_t addr,
3802 u32 len, u8 map_id)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003803{
3804 union ibmvnic_crq crq;
3805
3806 memset(&crq, 0, sizeof(crq));
3807 crq.request_map.first = IBMVNIC_CRQ_CMD;
3808 crq.request_map.cmd = REQUEST_MAP;
3809 crq.request_map.map_id = map_id;
3810 crq.request_map.ioba = cpu_to_be32(addr);
3811 crq.request_map.len = cpu_to_be32(len);
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05003812 return ibmvnic_send_crq(adapter, &crq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003813}
3814
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05003815static int send_request_unmap(struct ibmvnic_adapter *adapter, u8 map_id)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003816{
3817 union ibmvnic_crq crq;
3818
3819 memset(&crq, 0, sizeof(crq));
3820 crq.request_unmap.first = IBMVNIC_CRQ_CMD;
3821 crq.request_unmap.cmd = REQUEST_UNMAP;
3822 crq.request_unmap.map_id = map_id;
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05003823 return ibmvnic_send_crq(adapter, &crq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003824}
3825
3826static void send_map_query(struct ibmvnic_adapter *adapter)
3827{
3828 union ibmvnic_crq crq;
3829
3830 memset(&crq, 0, sizeof(crq));
3831 crq.query_map.first = IBMVNIC_CRQ_CMD;
3832 crq.query_map.cmd = QUERY_MAP;
3833 ibmvnic_send_crq(adapter, &crq);
3834}
3835
3836/* Send a series of CRQs requesting various capabilities of the VNIC server */
3837static void send_cap_queries(struct ibmvnic_adapter *adapter)
3838{
3839 union ibmvnic_crq crq;
3840
Thomas Falcon901e0402017-02-15 12:17:59 -06003841 atomic_set(&adapter->running_cap_crqs, 0);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003842 memset(&crq, 0, sizeof(crq));
3843 crq.query_capability.first = IBMVNIC_CRQ_CMD;
3844 crq.query_capability.cmd = QUERY_CAPABILITY;
3845
3846 crq.query_capability.capability = cpu_to_be16(MIN_TX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003847 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003848 ibmvnic_send_crq(adapter, &crq);
3849
3850 crq.query_capability.capability = cpu_to_be16(MIN_RX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003851 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003852 ibmvnic_send_crq(adapter, &crq);
3853
3854 crq.query_capability.capability = cpu_to_be16(MIN_RX_ADD_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003855 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003856 ibmvnic_send_crq(adapter, &crq);
3857
3858 crq.query_capability.capability = cpu_to_be16(MAX_TX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003859 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003860 ibmvnic_send_crq(adapter, &crq);
3861
3862 crq.query_capability.capability = cpu_to_be16(MAX_RX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003863 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003864 ibmvnic_send_crq(adapter, &crq);
3865
3866 crq.query_capability.capability = cpu_to_be16(MAX_RX_ADD_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003867 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003868 ibmvnic_send_crq(adapter, &crq);
3869
3870 crq.query_capability.capability =
3871 cpu_to_be16(MIN_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06003872 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003873 ibmvnic_send_crq(adapter, &crq);
3874
3875 crq.query_capability.capability =
3876 cpu_to_be16(MIN_RX_ADD_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06003877 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003878 ibmvnic_send_crq(adapter, &crq);
3879
3880 crq.query_capability.capability =
3881 cpu_to_be16(MAX_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06003882 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003883 ibmvnic_send_crq(adapter, &crq);
3884
3885 crq.query_capability.capability =
3886 cpu_to_be16(MAX_RX_ADD_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06003887 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003888 ibmvnic_send_crq(adapter, &crq);
3889
3890 crq.query_capability.capability = cpu_to_be16(TCP_IP_OFFLOAD);
Thomas Falcon901e0402017-02-15 12:17:59 -06003891 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003892 ibmvnic_send_crq(adapter, &crq);
3893
3894 crq.query_capability.capability = cpu_to_be16(PROMISC_SUPPORTED);
Thomas Falcon901e0402017-02-15 12:17:59 -06003895 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003896 ibmvnic_send_crq(adapter, &crq);
3897
3898 crq.query_capability.capability = cpu_to_be16(MIN_MTU);
Thomas Falcon901e0402017-02-15 12:17:59 -06003899 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003900 ibmvnic_send_crq(adapter, &crq);
3901
3902 crq.query_capability.capability = cpu_to_be16(MAX_MTU);
Thomas Falcon901e0402017-02-15 12:17:59 -06003903 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003904 ibmvnic_send_crq(adapter, &crq);
3905
3906 crq.query_capability.capability = cpu_to_be16(MAX_MULTICAST_FILTERS);
Thomas Falcon901e0402017-02-15 12:17:59 -06003907 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003908 ibmvnic_send_crq(adapter, &crq);
3909
3910 crq.query_capability.capability = cpu_to_be16(VLAN_HEADER_INSERTION);
Thomas Falcon901e0402017-02-15 12:17:59 -06003911 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003912 ibmvnic_send_crq(adapter, &crq);
3913
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04003914 crq.query_capability.capability = cpu_to_be16(RX_VLAN_HEADER_INSERTION);
3915 atomic_inc(&adapter->running_cap_crqs);
3916 ibmvnic_send_crq(adapter, &crq);
3917
Thomas Falcon032c5e82015-12-21 11:26:06 -06003918 crq.query_capability.capability = cpu_to_be16(MAX_TX_SG_ENTRIES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003919 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003920 ibmvnic_send_crq(adapter, &crq);
3921
3922 crq.query_capability.capability = cpu_to_be16(RX_SG_SUPPORTED);
Thomas Falcon901e0402017-02-15 12:17:59 -06003923 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003924 ibmvnic_send_crq(adapter, &crq);
3925
3926 crq.query_capability.capability = cpu_to_be16(OPT_TX_COMP_SUB_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003927 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003928 ibmvnic_send_crq(adapter, &crq);
3929
3930 crq.query_capability.capability = cpu_to_be16(OPT_RX_COMP_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003931 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003932 ibmvnic_send_crq(adapter, &crq);
3933
3934 crq.query_capability.capability =
3935 cpu_to_be16(OPT_RX_BUFADD_Q_PER_RX_COMP_Q);
Thomas Falcon901e0402017-02-15 12:17:59 -06003936 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003937 ibmvnic_send_crq(adapter, &crq);
3938
3939 crq.query_capability.capability =
3940 cpu_to_be16(OPT_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06003941 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003942 ibmvnic_send_crq(adapter, &crq);
3943
3944 crq.query_capability.capability =
3945 cpu_to_be16(OPT_RXBA_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06003946 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003947 ibmvnic_send_crq(adapter, &crq);
3948
3949 crq.query_capability.capability = cpu_to_be16(TX_RX_DESC_REQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06003950 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003951 ibmvnic_send_crq(adapter, &crq);
3952}
3953
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02003954static void handle_vpd_size_rsp(union ibmvnic_crq *crq,
3955 struct ibmvnic_adapter *adapter)
3956{
3957 struct device *dev = &adapter->vdev->dev;
3958
3959 if (crq->get_vpd_size_rsp.rc.code) {
3960 dev_err(dev, "Error retrieving VPD size, rc=%x\n",
3961 crq->get_vpd_size_rsp.rc.code);
3962 complete(&adapter->fw_done);
3963 return;
3964 }
3965
3966 adapter->vpd->len = be64_to_cpu(crq->get_vpd_size_rsp.len);
3967 complete(&adapter->fw_done);
3968}
3969
3970static void handle_vpd_rsp(union ibmvnic_crq *crq,
3971 struct ibmvnic_adapter *adapter)
3972{
3973 struct device *dev = &adapter->vdev->dev;
Desnes Augusto Nunes do Rosario21a25452018-02-05 14:33:55 -02003974 unsigned char *substr = NULL;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02003975 u8 fw_level_len = 0;
3976
3977 memset(adapter->fw_version, 0, 32);
3978
3979 dma_unmap_single(dev, adapter->vpd->dma_addr, adapter->vpd->len,
3980 DMA_FROM_DEVICE);
3981
3982 if (crq->get_vpd_rsp.rc.code) {
3983 dev_err(dev, "Error retrieving VPD from device, rc=%x\n",
3984 crq->get_vpd_rsp.rc.code);
3985 goto complete;
3986 }
3987
3988 /* get the position of the firmware version info
3989 * located after the ASCII 'RM' substring in the buffer
3990 */
3991 substr = strnstr(adapter->vpd->buff, "RM", adapter->vpd->len);
3992 if (!substr) {
Desnes Augusto Nunes do Rosarioa1073112018-02-01 16:04:30 -02003993 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 -02003994 goto complete;
3995 }
3996
3997 /* get length of firmware level ASCII substring */
3998 if ((substr + 2) < (adapter->vpd->buff + adapter->vpd->len)) {
3999 fw_level_len = *(substr + 2);
4000 } else {
4001 dev_info(dev, "Length of FW substr extrapolated VDP buff\n");
4002 goto complete;
4003 }
4004
4005 /* copy firmware version string from vpd into adapter */
4006 if ((substr + 3 + fw_level_len) <
4007 (adapter->vpd->buff + adapter->vpd->len)) {
Desnes Augusto Nunes do Rosario21a25452018-02-05 14:33:55 -02004008 strncpy((char *)adapter->fw_version, substr + 3, fw_level_len);
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02004009 } else {
4010 dev_info(dev, "FW substr extrapolated VPD buff\n");
4011 }
4012
4013complete:
Desnes Augusto Nunes do Rosario21a25452018-02-05 14:33:55 -02004014 if (adapter->fw_version[0] == '\0')
4015 strncpy((char *)adapter->fw_version, "N/A", 3 * sizeof(char));
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02004016 complete(&adapter->fw_done);
4017}
4018
Thomas Falcon032c5e82015-12-21 11:26:06 -06004019static void handle_query_ip_offload_rsp(struct ibmvnic_adapter *adapter)
4020{
4021 struct device *dev = &adapter->vdev->dev;
4022 struct ibmvnic_query_ip_offload_buffer *buf = &adapter->ip_offload_buf;
Thomas Falcondde746a2019-04-10 11:07:00 -05004023 netdev_features_t old_hw_features = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004024 union ibmvnic_crq crq;
4025 int i;
4026
4027 dma_unmap_single(dev, adapter->ip_offload_tok,
4028 sizeof(adapter->ip_offload_buf), DMA_FROM_DEVICE);
4029
4030 netdev_dbg(adapter->netdev, "Query IP Offload Buffer:\n");
4031 for (i = 0; i < (sizeof(adapter->ip_offload_buf) - 1) / 8 + 1; i++)
4032 netdev_dbg(adapter->netdev, "%016lx\n",
4033 ((unsigned long int *)(buf))[i]);
4034
4035 netdev_dbg(adapter->netdev, "ipv4_chksum = %d\n", buf->ipv4_chksum);
4036 netdev_dbg(adapter->netdev, "ipv6_chksum = %d\n", buf->ipv6_chksum);
4037 netdev_dbg(adapter->netdev, "tcp_ipv4_chksum = %d\n",
4038 buf->tcp_ipv4_chksum);
4039 netdev_dbg(adapter->netdev, "tcp_ipv6_chksum = %d\n",
4040 buf->tcp_ipv6_chksum);
4041 netdev_dbg(adapter->netdev, "udp_ipv4_chksum = %d\n",
4042 buf->udp_ipv4_chksum);
4043 netdev_dbg(adapter->netdev, "udp_ipv6_chksum = %d\n",
4044 buf->udp_ipv6_chksum);
4045 netdev_dbg(adapter->netdev, "large_tx_ipv4 = %d\n",
4046 buf->large_tx_ipv4);
4047 netdev_dbg(adapter->netdev, "large_tx_ipv6 = %d\n",
4048 buf->large_tx_ipv6);
4049 netdev_dbg(adapter->netdev, "large_rx_ipv4 = %d\n",
4050 buf->large_rx_ipv4);
4051 netdev_dbg(adapter->netdev, "large_rx_ipv6 = %d\n",
4052 buf->large_rx_ipv6);
4053 netdev_dbg(adapter->netdev, "max_ipv4_hdr_sz = %d\n",
4054 buf->max_ipv4_header_size);
4055 netdev_dbg(adapter->netdev, "max_ipv6_hdr_sz = %d\n",
4056 buf->max_ipv6_header_size);
4057 netdev_dbg(adapter->netdev, "max_tcp_hdr_size = %d\n",
4058 buf->max_tcp_header_size);
4059 netdev_dbg(adapter->netdev, "max_udp_hdr_size = %d\n",
4060 buf->max_udp_header_size);
4061 netdev_dbg(adapter->netdev, "max_large_tx_size = %d\n",
4062 buf->max_large_tx_size);
4063 netdev_dbg(adapter->netdev, "max_large_rx_size = %d\n",
4064 buf->max_large_rx_size);
4065 netdev_dbg(adapter->netdev, "ipv6_ext_hdr = %d\n",
4066 buf->ipv6_extension_header);
4067 netdev_dbg(adapter->netdev, "tcp_pseudosum_req = %d\n",
4068 buf->tcp_pseudosum_req);
4069 netdev_dbg(adapter->netdev, "num_ipv6_ext_hd = %d\n",
4070 buf->num_ipv6_ext_headers);
4071 netdev_dbg(adapter->netdev, "off_ipv6_ext_hd = %d\n",
4072 buf->off_ipv6_ext_headers);
4073
4074 adapter->ip_offload_ctrl_tok =
4075 dma_map_single(dev, &adapter->ip_offload_ctrl,
4076 sizeof(adapter->ip_offload_ctrl), DMA_TO_DEVICE);
4077
4078 if (dma_mapping_error(dev, adapter->ip_offload_ctrl_tok)) {
4079 dev_err(dev, "Couldn't map ip offload control buffer\n");
4080 return;
4081 }
4082
Thomas Falconf6897942018-01-18 19:05:01 -06004083 adapter->ip_offload_ctrl.len =
4084 cpu_to_be32(sizeof(adapter->ip_offload_ctrl));
Thomas Falcon032c5e82015-12-21 11:26:06 -06004085 adapter->ip_offload_ctrl.version = cpu_to_be32(INITIAL_VERSION_IOB);
Thomas Falconf6897942018-01-18 19:05:01 -06004086 adapter->ip_offload_ctrl.ipv4_chksum = buf->ipv4_chksum;
4087 adapter->ip_offload_ctrl.ipv6_chksum = buf->ipv6_chksum;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004088 adapter->ip_offload_ctrl.tcp_ipv4_chksum = buf->tcp_ipv4_chksum;
4089 adapter->ip_offload_ctrl.udp_ipv4_chksum = buf->udp_ipv4_chksum;
4090 adapter->ip_offload_ctrl.tcp_ipv6_chksum = buf->tcp_ipv6_chksum;
4091 adapter->ip_offload_ctrl.udp_ipv6_chksum = buf->udp_ipv6_chksum;
Thomas Falconfdb06102017-10-17 12:36:55 -05004092 adapter->ip_offload_ctrl.large_tx_ipv4 = buf->large_tx_ipv4;
4093 adapter->ip_offload_ctrl.large_tx_ipv6 = buf->large_tx_ipv6;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004094
Thomas Falconfdb06102017-10-17 12:36:55 -05004095 /* large_rx disabled for now, additional features needed */
Thomas Falcon032c5e82015-12-21 11:26:06 -06004096 adapter->ip_offload_ctrl.large_rx_ipv4 = 0;
4097 adapter->ip_offload_ctrl.large_rx_ipv6 = 0;
4098
Thomas Falcondde746a2019-04-10 11:07:00 -05004099 if (adapter->state != VNIC_PROBING) {
4100 old_hw_features = adapter->netdev->hw_features;
4101 adapter->netdev->hw_features = 0;
4102 }
4103
Thomas Falconb66b7bd2019-04-10 11:06:59 -05004104 adapter->netdev->hw_features = NETIF_F_SG | NETIF_F_GSO | NETIF_F_GRO;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004105
4106 if (buf->tcp_ipv4_chksum || buf->udp_ipv4_chksum)
Thomas Falcondde746a2019-04-10 11:07:00 -05004107 adapter->netdev->hw_features |= NETIF_F_IP_CSUM;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004108
4109 if (buf->tcp_ipv6_chksum || buf->udp_ipv6_chksum)
Thomas Falcondde746a2019-04-10 11:07:00 -05004110 adapter->netdev->hw_features |= NETIF_F_IPV6_CSUM;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004111
Thomas Falcon9be02cd2016-04-01 17:20:35 -05004112 if ((adapter->netdev->features &
4113 (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)))
Thomas Falcondde746a2019-04-10 11:07:00 -05004114 adapter->netdev->hw_features |= NETIF_F_RXCSUM;
Thomas Falcon9be02cd2016-04-01 17:20:35 -05004115
Thomas Falconfdb06102017-10-17 12:36:55 -05004116 if (buf->large_tx_ipv4)
Thomas Falcondde746a2019-04-10 11:07:00 -05004117 adapter->netdev->hw_features |= NETIF_F_TSO;
Thomas Falconfdb06102017-10-17 12:36:55 -05004118 if (buf->large_tx_ipv6)
Thomas Falcondde746a2019-04-10 11:07:00 -05004119 adapter->netdev->hw_features |= NETIF_F_TSO6;
Thomas Falconfdb06102017-10-17 12:36:55 -05004120
Thomas Falcondde746a2019-04-10 11:07:00 -05004121 if (adapter->state == VNIC_PROBING) {
4122 adapter->netdev->features |= adapter->netdev->hw_features;
4123 } else if (old_hw_features != adapter->netdev->hw_features) {
4124 netdev_features_t tmp = 0;
4125
4126 /* disable features no longer supported */
4127 adapter->netdev->features &= adapter->netdev->hw_features;
4128 /* turn on features now supported if previously enabled */
4129 tmp = (old_hw_features ^ adapter->netdev->hw_features) &
4130 adapter->netdev->hw_features;
4131 adapter->netdev->features |=
4132 tmp & adapter->netdev->wanted_features;
4133 }
Thomas Falconaa0bf852017-10-17 12:36:56 -05004134
Thomas Falcon032c5e82015-12-21 11:26:06 -06004135 memset(&crq, 0, sizeof(crq));
4136 crq.control_ip_offload.first = IBMVNIC_CRQ_CMD;
4137 crq.control_ip_offload.cmd = CONTROL_IP_OFFLOAD;
4138 crq.control_ip_offload.len =
4139 cpu_to_be32(sizeof(adapter->ip_offload_ctrl));
4140 crq.control_ip_offload.ioba = cpu_to_be32(adapter->ip_offload_ctrl_tok);
4141 ibmvnic_send_crq(adapter, &crq);
4142}
4143
Thomas Falconc9008d32018-08-06 21:39:59 -05004144static const char *ibmvnic_fw_err_cause(u16 cause)
4145{
4146 switch (cause) {
4147 case ADAPTER_PROBLEM:
4148 return "adapter problem";
4149 case BUS_PROBLEM:
4150 return "bus problem";
4151 case FW_PROBLEM:
4152 return "firmware problem";
4153 case DD_PROBLEM:
4154 return "device driver problem";
4155 case EEH_RECOVERY:
4156 return "EEH recovery";
4157 case FW_UPDATED:
4158 return "firmware updated";
4159 case LOW_MEMORY:
4160 return "low Memory";
4161 default:
4162 return "unknown";
4163 }
4164}
4165
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04004166static void handle_error_indication(union ibmvnic_crq *crq,
4167 struct ibmvnic_adapter *adapter)
4168{
4169 struct device *dev = &adapter->vdev->dev;
Thomas Falconc9008d32018-08-06 21:39:59 -05004170 u16 cause;
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04004171
Thomas Falconc9008d32018-08-06 21:39:59 -05004172 cause = be16_to_cpu(crq->error_indication.error_cause);
4173
4174 dev_warn_ratelimited(dev,
4175 "Firmware reports %serror, cause: %s. Starting recovery...\n",
4176 crq->error_indication.flags
4177 & IBMVNIC_FATAL_ERROR ? "FATAL " : "",
4178 ibmvnic_fw_err_cause(cause));
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04004179
Nathan Fontenoted651a12017-05-03 14:04:38 -04004180 if (crq->error_indication.flags & IBMVNIC_FATAL_ERROR)
4181 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
John Allen8cb31cf2017-05-26 10:30:37 -04004182 else
4183 ibmvnic_reset(adapter, VNIC_RESET_NON_FATAL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004184}
4185
Thomas Falconf8136142018-01-29 13:45:05 -06004186static int handle_change_mac_rsp(union ibmvnic_crq *crq,
4187 struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06004188{
4189 struct net_device *netdev = adapter->netdev;
4190 struct device *dev = &adapter->vdev->dev;
4191 long rc;
4192
4193 rc = crq->change_mac_addr_rsp.rc.code;
4194 if (rc) {
4195 dev_err(dev, "Error %ld in CHANGE_MAC_ADDR_RSP\n", rc);
Thomas Falconf8136142018-01-29 13:45:05 -06004196 goto out;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004197 }
Thomas Falcon62740e92019-05-09 23:13:43 -05004198 ether_addr_copy(netdev->dev_addr,
4199 &crq->change_mac_addr_rsp.mac_addr[0]);
Thomas Falconf8136142018-01-29 13:45:05 -06004200out:
4201 complete(&adapter->fw_done);
4202 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004203}
4204
4205static void handle_request_cap_rsp(union ibmvnic_crq *crq,
4206 struct ibmvnic_adapter *adapter)
4207{
4208 struct device *dev = &adapter->vdev->dev;
4209 u64 *req_value;
4210 char *name;
4211
Thomas Falcon901e0402017-02-15 12:17:59 -06004212 atomic_dec(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004213 switch (be16_to_cpu(crq->request_capability_rsp.capability)) {
4214 case REQ_TX_QUEUES:
4215 req_value = &adapter->req_tx_queues;
4216 name = "tx";
4217 break;
4218 case REQ_RX_QUEUES:
4219 req_value = &adapter->req_rx_queues;
4220 name = "rx";
4221 break;
4222 case REQ_RX_ADD_QUEUES:
4223 req_value = &adapter->req_rx_add_queues;
4224 name = "rx_add";
4225 break;
4226 case REQ_TX_ENTRIES_PER_SUBCRQ:
4227 req_value = &adapter->req_tx_entries_per_subcrq;
4228 name = "tx_entries_per_subcrq";
4229 break;
4230 case REQ_RX_ADD_ENTRIES_PER_SUBCRQ:
4231 req_value = &adapter->req_rx_add_entries_per_subcrq;
4232 name = "rx_add_entries_per_subcrq";
4233 break;
4234 case REQ_MTU:
4235 req_value = &adapter->req_mtu;
4236 name = "mtu";
4237 break;
4238 case PROMISC_REQUESTED:
4239 req_value = &adapter->promisc;
4240 name = "promisc";
4241 break;
4242 default:
4243 dev_err(dev, "Got invalid cap request rsp %d\n",
4244 crq->request_capability.capability);
4245 return;
4246 }
4247
4248 switch (crq->request_capability_rsp.rc.code) {
4249 case SUCCESS:
4250 break;
4251 case PARTIALSUCCESS:
4252 dev_info(dev, "req=%lld, rsp=%ld in %s queue, retrying.\n",
4253 *req_value,
Thomas Falcon28f4d162017-02-15 10:32:11 -06004254 (long int)be64_to_cpu(crq->request_capability_rsp.
Thomas Falcon032c5e82015-12-21 11:26:06 -06004255 number), name);
John Allene7913802018-01-18 16:27:12 -06004256
4257 if (be16_to_cpu(crq->request_capability_rsp.capability) ==
4258 REQ_MTU) {
4259 pr_err("mtu of %llu is not supported. Reverting.\n",
4260 *req_value);
4261 *req_value = adapter->fallback.mtu;
4262 } else {
4263 *req_value =
4264 be64_to_cpu(crq->request_capability_rsp.number);
4265 }
4266
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04004267 ibmvnic_send_req_caps(adapter, 1);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004268 return;
4269 default:
4270 dev_err(dev, "Error %d in request cap rsp\n",
4271 crq->request_capability_rsp.rc.code);
4272 return;
4273 }
4274
4275 /* Done receiving requested capabilities, query IP offload support */
Thomas Falcon901e0402017-02-15 12:17:59 -06004276 if (atomic_read(&adapter->running_cap_crqs) == 0) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06004277 union ibmvnic_crq newcrq;
4278 int buf_sz = sizeof(struct ibmvnic_query_ip_offload_buffer);
4279 struct ibmvnic_query_ip_offload_buffer *ip_offload_buf =
4280 &adapter->ip_offload_buf;
4281
Thomas Falcon249168a2017-02-15 12:18:00 -06004282 adapter->wait_capability = false;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004283 adapter->ip_offload_tok = dma_map_single(dev, ip_offload_buf,
4284 buf_sz,
4285 DMA_FROM_DEVICE);
4286
4287 if (dma_mapping_error(dev, adapter->ip_offload_tok)) {
4288 if (!firmware_has_feature(FW_FEATURE_CMO))
4289 dev_err(dev, "Couldn't map offload buffer\n");
4290 return;
4291 }
4292
4293 memset(&newcrq, 0, sizeof(newcrq));
4294 newcrq.query_ip_offload.first = IBMVNIC_CRQ_CMD;
4295 newcrq.query_ip_offload.cmd = QUERY_IP_OFFLOAD;
4296 newcrq.query_ip_offload.len = cpu_to_be32(buf_sz);
4297 newcrq.query_ip_offload.ioba =
4298 cpu_to_be32(adapter->ip_offload_tok);
4299
4300 ibmvnic_send_crq(adapter, &newcrq);
4301 }
4302}
4303
4304static int handle_login_rsp(union ibmvnic_crq *login_rsp_crq,
4305 struct ibmvnic_adapter *adapter)
4306{
4307 struct device *dev = &adapter->vdev->dev;
John Allenc26eba02017-10-26 16:23:25 -05004308 struct net_device *netdev = adapter->netdev;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004309 struct ibmvnic_login_rsp_buffer *login_rsp = adapter->login_rsp_buf;
4310 struct ibmvnic_login_buffer *login = adapter->login_buf;
Cristobal Fornof3ae59c2020-08-19 13:16:23 -05004311 u64 *tx_handle_array;
4312 u64 *rx_handle_array;
4313 int num_tx_pools;
4314 int num_rx_pools;
Thomas Falcon507ebe62020-08-21 13:39:01 -05004315 u64 *size_array;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004316 int i;
4317
4318 dma_unmap_single(dev, adapter->login_buf_token, adapter->login_buf_sz,
Thomas Falcon37e40fa2018-04-06 18:37:02 -05004319 DMA_TO_DEVICE);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004320 dma_unmap_single(dev, adapter->login_rsp_buf_token,
Thomas Falcon37e40fa2018-04-06 18:37:02 -05004321 adapter->login_rsp_buf_sz, DMA_FROM_DEVICE);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004322
John Allen498cd8e2016-04-06 11:49:55 -05004323 /* If the number of queues requested can't be allocated by the
4324 * server, the login response will return with code 1. We will need
4325 * to resend the login buffer with fewer queues requested.
4326 */
4327 if (login_rsp_crq->generic.rc.code) {
Nathan Fontenot64d92aa2018-04-11 10:09:32 -05004328 adapter->init_done_rc = login_rsp_crq->generic.rc.code;
John Allen498cd8e2016-04-06 11:49:55 -05004329 complete(&adapter->init_done);
4330 return 0;
4331 }
4332
John Allenc26eba02017-10-26 16:23:25 -05004333 netdev->mtu = adapter->req_mtu - ETH_HLEN;
4334
Thomas Falcon032c5e82015-12-21 11:26:06 -06004335 netdev_dbg(adapter->netdev, "Login Response Buffer:\n");
4336 for (i = 0; i < (adapter->login_rsp_buf_sz - 1) / 8 + 1; i++) {
4337 netdev_dbg(adapter->netdev, "%016lx\n",
4338 ((unsigned long int *)(adapter->login_rsp_buf))[i]);
4339 }
4340
4341 /* Sanity checks */
4342 if (login->num_txcomp_subcrqs != login_rsp->num_txsubm_subcrqs ||
4343 (be32_to_cpu(login->num_rxcomp_subcrqs) *
4344 adapter->req_rx_add_queues !=
4345 be32_to_cpu(login_rsp->num_rxadd_subcrqs))) {
4346 dev_err(dev, "FATAL: Inconsistent login and login rsp\n");
4347 ibmvnic_remove(adapter->vdev);
4348 return -EIO;
4349 }
Thomas Falcon507ebe62020-08-21 13:39:01 -05004350 size_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
4351 be32_to_cpu(adapter->login_rsp_buf->off_rxadd_buff_size));
4352 /* variable buffer sizes are not supported, so just read the
4353 * first entry.
4354 */
4355 adapter->cur_rx_buf_sz = be64_to_cpu(size_array[0]);
Cristobal Fornof3ae59c2020-08-19 13:16:23 -05004356
4357 num_tx_pools = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
4358 num_rx_pools = be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
4359
4360 tx_handle_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
4361 be32_to_cpu(adapter->login_rsp_buf->off_txsubm_subcrqs));
4362 rx_handle_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
4363 be32_to_cpu(adapter->login_rsp_buf->off_rxadd_subcrqs));
4364
4365 for (i = 0; i < num_tx_pools; i++)
4366 adapter->tx_scrq[i]->handle = tx_handle_array[i];
4367
4368 for (i = 0; i < num_rx_pools; i++)
4369 adapter->rx_scrq[i]->handle = rx_handle_array[i];
4370
Thomas Falcon507ebe62020-08-21 13:39:01 -05004371 adapter->num_active_tx_scrqs = num_tx_pools;
4372 adapter->num_active_rx_scrqs = num_rx_pools;
Cristobal Fornof3ae59c2020-08-19 13:16:23 -05004373 release_login_rsp_buffer(adapter);
Thomas Falcona2c0f032018-02-21 18:18:30 -06004374 release_login_buffer(adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004375 complete(&adapter->init_done);
4376
Thomas Falcon032c5e82015-12-21 11:26:06 -06004377 return 0;
4378}
4379
Thomas Falcon032c5e82015-12-21 11:26:06 -06004380static void handle_request_unmap_rsp(union ibmvnic_crq *crq,
4381 struct ibmvnic_adapter *adapter)
4382{
4383 struct device *dev = &adapter->vdev->dev;
4384 long rc;
4385
4386 rc = crq->request_unmap_rsp.rc.code;
4387 if (rc)
4388 dev_err(dev, "Error %ld in REQUEST_UNMAP_RSP\n", rc);
4389}
4390
4391static void handle_query_map_rsp(union ibmvnic_crq *crq,
4392 struct ibmvnic_adapter *adapter)
4393{
4394 struct net_device *netdev = adapter->netdev;
4395 struct device *dev = &adapter->vdev->dev;
4396 long rc;
4397
4398 rc = crq->query_map_rsp.rc.code;
4399 if (rc) {
4400 dev_err(dev, "Error %ld in QUERY_MAP_RSP\n", rc);
4401 return;
4402 }
4403 netdev_dbg(netdev, "page_size = %d\ntot_pages = %d\nfree_pages = %d\n",
4404 crq->query_map_rsp.page_size, crq->query_map_rsp.tot_pages,
4405 crq->query_map_rsp.free_pages);
4406}
4407
4408static void handle_query_cap_rsp(union ibmvnic_crq *crq,
4409 struct ibmvnic_adapter *adapter)
4410{
4411 struct net_device *netdev = adapter->netdev;
4412 struct device *dev = &adapter->vdev->dev;
4413 long rc;
4414
Thomas Falcon901e0402017-02-15 12:17:59 -06004415 atomic_dec(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004416 netdev_dbg(netdev, "Outstanding queries: %d\n",
Thomas Falcon901e0402017-02-15 12:17:59 -06004417 atomic_read(&adapter->running_cap_crqs));
Thomas Falcon032c5e82015-12-21 11:26:06 -06004418 rc = crq->query_capability.rc.code;
4419 if (rc) {
4420 dev_err(dev, "Error %ld in QUERY_CAP_RSP\n", rc);
4421 goto out;
4422 }
4423
4424 switch (be16_to_cpu(crq->query_capability.capability)) {
4425 case MIN_TX_QUEUES:
4426 adapter->min_tx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004427 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004428 netdev_dbg(netdev, "min_tx_queues = %lld\n",
4429 adapter->min_tx_queues);
4430 break;
4431 case MIN_RX_QUEUES:
4432 adapter->min_rx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004433 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004434 netdev_dbg(netdev, "min_rx_queues = %lld\n",
4435 adapter->min_rx_queues);
4436 break;
4437 case MIN_RX_ADD_QUEUES:
4438 adapter->min_rx_add_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004439 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004440 netdev_dbg(netdev, "min_rx_add_queues = %lld\n",
4441 adapter->min_rx_add_queues);
4442 break;
4443 case MAX_TX_QUEUES:
4444 adapter->max_tx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004445 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004446 netdev_dbg(netdev, "max_tx_queues = %lld\n",
4447 adapter->max_tx_queues);
4448 break;
4449 case MAX_RX_QUEUES:
4450 adapter->max_rx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004451 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004452 netdev_dbg(netdev, "max_rx_queues = %lld\n",
4453 adapter->max_rx_queues);
4454 break;
4455 case MAX_RX_ADD_QUEUES:
4456 adapter->max_rx_add_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004457 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004458 netdev_dbg(netdev, "max_rx_add_queues = %lld\n",
4459 adapter->max_rx_add_queues);
4460 break;
4461 case MIN_TX_ENTRIES_PER_SUBCRQ:
4462 adapter->min_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06004463 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004464 netdev_dbg(netdev, "min_tx_entries_per_subcrq = %lld\n",
4465 adapter->min_tx_entries_per_subcrq);
4466 break;
4467 case MIN_RX_ADD_ENTRIES_PER_SUBCRQ:
4468 adapter->min_rx_add_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06004469 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004470 netdev_dbg(netdev, "min_rx_add_entrs_per_subcrq = %lld\n",
4471 adapter->min_rx_add_entries_per_subcrq);
4472 break;
4473 case MAX_TX_ENTRIES_PER_SUBCRQ:
4474 adapter->max_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06004475 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004476 netdev_dbg(netdev, "max_tx_entries_per_subcrq = %lld\n",
4477 adapter->max_tx_entries_per_subcrq);
4478 break;
4479 case MAX_RX_ADD_ENTRIES_PER_SUBCRQ:
4480 adapter->max_rx_add_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06004481 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004482 netdev_dbg(netdev, "max_rx_add_entrs_per_subcrq = %lld\n",
4483 adapter->max_rx_add_entries_per_subcrq);
4484 break;
4485 case TCP_IP_OFFLOAD:
4486 adapter->tcp_ip_offload =
Thomas Falconde89e852016-03-01 10:20:09 -06004487 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004488 netdev_dbg(netdev, "tcp_ip_offload = %lld\n",
4489 adapter->tcp_ip_offload);
4490 break;
4491 case PROMISC_SUPPORTED:
4492 adapter->promisc_supported =
Thomas Falconde89e852016-03-01 10:20:09 -06004493 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004494 netdev_dbg(netdev, "promisc_supported = %lld\n",
4495 adapter->promisc_supported);
4496 break;
4497 case MIN_MTU:
Thomas Falconde89e852016-03-01 10:20:09 -06004498 adapter->min_mtu = be64_to_cpu(crq->query_capability.number);
Thomas Falconf39f0d12017-02-14 10:22:59 -06004499 netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004500 netdev_dbg(netdev, "min_mtu = %lld\n", adapter->min_mtu);
4501 break;
4502 case MAX_MTU:
Thomas Falconde89e852016-03-01 10:20:09 -06004503 adapter->max_mtu = be64_to_cpu(crq->query_capability.number);
Thomas Falconf39f0d12017-02-14 10:22:59 -06004504 netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004505 netdev_dbg(netdev, "max_mtu = %lld\n", adapter->max_mtu);
4506 break;
4507 case MAX_MULTICAST_FILTERS:
4508 adapter->max_multicast_filters =
Thomas Falconde89e852016-03-01 10:20:09 -06004509 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004510 netdev_dbg(netdev, "max_multicast_filters = %lld\n",
4511 adapter->max_multicast_filters);
4512 break;
4513 case VLAN_HEADER_INSERTION:
4514 adapter->vlan_header_insertion =
Thomas Falconde89e852016-03-01 10:20:09 -06004515 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004516 if (adapter->vlan_header_insertion)
4517 netdev->features |= NETIF_F_HW_VLAN_STAG_TX;
4518 netdev_dbg(netdev, "vlan_header_insertion = %lld\n",
4519 adapter->vlan_header_insertion);
4520 break;
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04004521 case RX_VLAN_HEADER_INSERTION:
4522 adapter->rx_vlan_header_insertion =
4523 be64_to_cpu(crq->query_capability.number);
4524 netdev_dbg(netdev, "rx_vlan_header_insertion = %lld\n",
4525 adapter->rx_vlan_header_insertion);
4526 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004527 case MAX_TX_SG_ENTRIES:
4528 adapter->max_tx_sg_entries =
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, "max_tx_sg_entries = %lld\n",
4531 adapter->max_tx_sg_entries);
4532 break;
4533 case RX_SG_SUPPORTED:
4534 adapter->rx_sg_supported =
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, "rx_sg_supported = %lld\n",
4537 adapter->rx_sg_supported);
4538 break;
4539 case OPT_TX_COMP_SUB_QUEUES:
4540 adapter->opt_tx_comp_sub_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, "opt_tx_comp_sub_queues = %lld\n",
4543 adapter->opt_tx_comp_sub_queues);
4544 break;
4545 case OPT_RX_COMP_QUEUES:
4546 adapter->opt_rx_comp_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, "opt_rx_comp_queues = %lld\n",
4549 adapter->opt_rx_comp_queues);
4550 break;
4551 case OPT_RX_BUFADD_Q_PER_RX_COMP_Q:
4552 adapter->opt_rx_bufadd_q_per_rx_comp_q =
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, "opt_rx_bufadd_q_per_rx_comp_q = %lld\n",
4555 adapter->opt_rx_bufadd_q_per_rx_comp_q);
4556 break;
4557 case OPT_TX_ENTRIES_PER_SUBCRQ:
4558 adapter->opt_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, "opt_tx_entries_per_subcrq = %lld\n",
4561 adapter->opt_tx_entries_per_subcrq);
4562 break;
4563 case OPT_RXBA_ENTRIES_PER_SUBCRQ:
4564 adapter->opt_rxba_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, "opt_rxba_entries_per_subcrq = %lld\n",
4567 adapter->opt_rxba_entries_per_subcrq);
4568 break;
4569 case TX_RX_DESC_REQ:
4570 adapter->tx_rx_desc_req = crq->query_capability.number;
4571 netdev_dbg(netdev, "tx_rx_desc_req = %llx\n",
4572 adapter->tx_rx_desc_req);
4573 break;
4574
4575 default:
4576 netdev_err(netdev, "Got invalid cap rsp %d\n",
4577 crq->query_capability.capability);
4578 }
4579
4580out:
Thomas Falcon249168a2017-02-15 12:18:00 -06004581 if (atomic_read(&adapter->running_cap_crqs) == 0) {
4582 adapter->wait_capability = false;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04004583 ibmvnic_send_req_caps(adapter, 0);
Thomas Falcon249168a2017-02-15 12:18:00 -06004584 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06004585}
4586
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03004587static int send_query_phys_parms(struct ibmvnic_adapter *adapter)
4588{
4589 union ibmvnic_crq crq;
4590 int rc;
4591
4592 memset(&crq, 0, sizeof(crq));
4593 crq.query_phys_parms.first = IBMVNIC_CRQ_CMD;
4594 crq.query_phys_parms.cmd = QUERY_PHYS_PARMS;
Thomas Falconff25dcb2019-11-25 17:12:56 -06004595
4596 mutex_lock(&adapter->fw_lock);
4597 adapter->fw_done_rc = 0;
Thomas Falcon070eca92019-11-25 17:12:53 -06004598 reinit_completion(&adapter->fw_done);
Thomas Falconff25dcb2019-11-25 17:12:56 -06004599
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03004600 rc = ibmvnic_send_crq(adapter, &crq);
Thomas Falconff25dcb2019-11-25 17:12:56 -06004601 if (rc) {
4602 mutex_unlock(&adapter->fw_lock);
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03004603 return rc;
Thomas Falconff25dcb2019-11-25 17:12:56 -06004604 }
Thomas Falcon476d96c2019-11-25 17:12:55 -06004605
4606 rc = ibmvnic_wait_for_completion(adapter, &adapter->fw_done, 10000);
Thomas Falconff25dcb2019-11-25 17:12:56 -06004607 if (rc) {
4608 mutex_unlock(&adapter->fw_lock);
Thomas Falcon476d96c2019-11-25 17:12:55 -06004609 return rc;
Thomas Falconff25dcb2019-11-25 17:12:56 -06004610 }
Thomas Falcon476d96c2019-11-25 17:12:55 -06004611
Thomas Falconff25dcb2019-11-25 17:12:56 -06004612 mutex_unlock(&adapter->fw_lock);
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03004613 return adapter->fw_done_rc ? -EIO : 0;
4614}
4615
4616static int handle_query_phys_parms_rsp(union ibmvnic_crq *crq,
4617 struct ibmvnic_adapter *adapter)
4618{
4619 struct net_device *netdev = adapter->netdev;
4620 int rc;
Murilo Fossa Vicentinidd0f9d82019-09-16 11:50:37 -03004621 __be32 rspeed = cpu_to_be32(crq->query_phys_parms_rsp.speed);
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03004622
4623 rc = crq->query_phys_parms_rsp.rc.code;
4624 if (rc) {
4625 netdev_err(netdev, "Error %d in QUERY_PHYS_PARMS\n", rc);
4626 return rc;
4627 }
Murilo Fossa Vicentinidd0f9d82019-09-16 11:50:37 -03004628 switch (rspeed) {
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03004629 case IBMVNIC_10MBPS:
4630 adapter->speed = SPEED_10;
4631 break;
4632 case IBMVNIC_100MBPS:
4633 adapter->speed = SPEED_100;
4634 break;
4635 case IBMVNIC_1GBPS:
4636 adapter->speed = SPEED_1000;
4637 break;
4638 case IBMVNIC_10GBP:
4639 adapter->speed = SPEED_10000;
4640 break;
4641 case IBMVNIC_25GBPS:
4642 adapter->speed = SPEED_25000;
4643 break;
4644 case IBMVNIC_40GBPS:
4645 adapter->speed = SPEED_40000;
4646 break;
4647 case IBMVNIC_50GBPS:
4648 adapter->speed = SPEED_50000;
4649 break;
4650 case IBMVNIC_100GBPS:
4651 adapter->speed = SPEED_100000;
4652 break;
4653 default:
Murilo Fossa Vicentinidd0f9d82019-09-16 11:50:37 -03004654 if (netif_carrier_ok(netdev))
4655 netdev_warn(netdev, "Unknown speed 0x%08x\n", rspeed);
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03004656 adapter->speed = SPEED_UNKNOWN;
4657 }
4658 if (crq->query_phys_parms_rsp.flags1 & IBMVNIC_FULL_DUPLEX)
4659 adapter->duplex = DUPLEX_FULL;
4660 else if (crq->query_phys_parms_rsp.flags1 & IBMVNIC_HALF_DUPLEX)
4661 adapter->duplex = DUPLEX_HALF;
4662 else
4663 adapter->duplex = DUPLEX_UNKNOWN;
4664
4665 return rc;
4666}
4667
Thomas Falcon032c5e82015-12-21 11:26:06 -06004668static void ibmvnic_handle_crq(union ibmvnic_crq *crq,
4669 struct ibmvnic_adapter *adapter)
4670{
4671 struct ibmvnic_generic_crq *gen_crq = &crq->generic;
4672 struct net_device *netdev = adapter->netdev;
4673 struct device *dev = &adapter->vdev->dev;
Murilo Fossa Vicentini993a82b2017-04-19 13:44:35 -04004674 u64 *u64_crq = (u64 *)crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004675 long rc;
4676
4677 netdev_dbg(netdev, "Handling CRQ: %016lx %016lx\n",
Murilo Fossa Vicentini993a82b2017-04-19 13:44:35 -04004678 (unsigned long int)cpu_to_be64(u64_crq[0]),
4679 (unsigned long int)cpu_to_be64(u64_crq[1]));
Thomas Falcon032c5e82015-12-21 11:26:06 -06004680 switch (gen_crq->first) {
4681 case IBMVNIC_CRQ_INIT_RSP:
4682 switch (gen_crq->cmd) {
4683 case IBMVNIC_CRQ_INIT:
4684 dev_info(dev, "Partner initialized\n");
John Allen017892c12017-05-26 10:30:19 -04004685 adapter->from_passive_init = true;
Thomas Falcon5a18e1e2018-04-06 18:37:05 -05004686 adapter->failover_pending = false;
Thomas Falcon17c87052018-05-23 13:37:58 -05004687 if (!completion_done(&adapter->init_done)) {
4688 complete(&adapter->init_done);
4689 adapter->init_done_rc = -EIO;
4690 }
Thomas Falcon5a18e1e2018-04-06 18:37:05 -05004691 ibmvnic_reset(adapter, VNIC_RESET_FAILOVER);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004692 break;
4693 case IBMVNIC_CRQ_INIT_COMPLETE:
4694 dev_info(dev, "Partner initialization complete\n");
Thomas Falcon51536982018-05-23 13:37:56 -05004695 adapter->crq.active = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004696 send_version_xchg(adapter);
4697 break;
4698 default:
4699 dev_err(dev, "Unknown crq cmd: %d\n", gen_crq->cmd);
4700 }
4701 return;
4702 case IBMVNIC_CRQ_XPORT_EVENT:
Nathan Fontenoted651a12017-05-03 14:04:38 -04004703 netif_carrier_off(netdev);
Thomas Falcon51536982018-05-23 13:37:56 -05004704 adapter->crq.active = false;
Thomas Falcon2147e3d2019-11-25 17:12:54 -06004705 /* terminate any thread waiting for a response
4706 * from the device
4707 */
4708 if (!completion_done(&adapter->fw_done)) {
4709 adapter->fw_done_rc = -EIO;
4710 complete(&adapter->fw_done);
4711 }
4712 if (!completion_done(&adapter->stats_done))
4713 complete(&adapter->stats_done);
Juliet Kim7ed5b312019-09-20 16:11:23 -04004714 if (test_bit(0, &adapter->resetting))
Thomas Falcon2770a792018-05-23 13:38:02 -05004715 adapter->force_reset_recovery = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004716 if (gen_crq->cmd == IBMVNIC_PARTITION_MIGRATED) {
Nathan Fontenoted651a12017-05-03 14:04:38 -04004717 dev_info(dev, "Migrated, re-enabling adapter\n");
4718 ibmvnic_reset(adapter, VNIC_RESET_MOBILITY);
Thomas Falcondfad09a2016-08-18 11:37:51 -05004719 } else if (gen_crq->cmd == IBMVNIC_DEVICE_FAILOVER) {
4720 dev_info(dev, "Backing device failover detected\n");
Thomas Falcon5a18e1e2018-04-06 18:37:05 -05004721 adapter->failover_pending = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004722 } else {
4723 /* The adapter lost the connection */
4724 dev_err(dev, "Virtual Adapter failed (rc=%d)\n",
4725 gen_crq->cmd);
Nathan Fontenoted651a12017-05-03 14:04:38 -04004726 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004727 }
4728 return;
4729 case IBMVNIC_CRQ_CMD_RSP:
4730 break;
4731 default:
4732 dev_err(dev, "Got an invalid msg type 0x%02x\n",
4733 gen_crq->first);
4734 return;
4735 }
4736
4737 switch (gen_crq->cmd) {
4738 case VERSION_EXCHANGE_RSP:
4739 rc = crq->version_exchange_rsp.rc.code;
4740 if (rc) {
4741 dev_err(dev, "Error %ld in VERSION_EXCHG_RSP\n", rc);
4742 break;
4743 }
Thomas Falcon78468892020-05-28 11:19:17 -05004744 ibmvnic_version =
Thomas Falcon032c5e82015-12-21 11:26:06 -06004745 be16_to_cpu(crq->version_exchange_rsp.version);
Thomas Falcon78468892020-05-28 11:19:17 -05004746 dev_info(dev, "Partner protocol version is %d\n",
4747 ibmvnic_version);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004748 send_cap_queries(adapter);
4749 break;
4750 case QUERY_CAPABILITY_RSP:
4751 handle_query_cap_rsp(crq, adapter);
4752 break;
4753 case QUERY_MAP_RSP:
4754 handle_query_map_rsp(crq, adapter);
4755 break;
4756 case REQUEST_MAP_RSP:
Thomas Falconf3be0cb2017-06-21 14:53:01 -05004757 adapter->fw_done_rc = crq->request_map_rsp.rc.code;
4758 complete(&adapter->fw_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004759 break;
4760 case REQUEST_UNMAP_RSP:
4761 handle_request_unmap_rsp(crq, adapter);
4762 break;
4763 case REQUEST_CAPABILITY_RSP:
4764 handle_request_cap_rsp(crq, adapter);
4765 break;
4766 case LOGIN_RSP:
4767 netdev_dbg(netdev, "Got Login Response\n");
4768 handle_login_rsp(crq, adapter);
4769 break;
4770 case LOGICAL_LINK_STATE_RSP:
Nathan Fontenot53da09e2017-04-21 15:39:04 -04004771 netdev_dbg(netdev,
4772 "Got Logical Link State Response, state: %d rc: %d\n",
4773 crq->logical_link_state_rsp.link_state,
4774 crq->logical_link_state_rsp.rc.code);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004775 adapter->logical_link_state =
4776 crq->logical_link_state_rsp.link_state;
Nathan Fontenot53da09e2017-04-21 15:39:04 -04004777 adapter->init_done_rc = crq->logical_link_state_rsp.rc.code;
4778 complete(&adapter->init_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004779 break;
4780 case LINK_STATE_INDICATION:
4781 netdev_dbg(netdev, "Got Logical Link State Indication\n");
4782 adapter->phys_link_state =
4783 crq->link_state_indication.phys_link_state;
4784 adapter->logical_link_state =
4785 crq->link_state_indication.logical_link_state;
Thomas Falcon0655f992019-05-09 23:13:44 -05004786 if (adapter->phys_link_state && adapter->logical_link_state)
4787 netif_carrier_on(netdev);
4788 else
4789 netif_carrier_off(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004790 break;
4791 case CHANGE_MAC_ADDR_RSP:
4792 netdev_dbg(netdev, "Got MAC address change Response\n");
Thomas Falconf8136142018-01-29 13:45:05 -06004793 adapter->fw_done_rc = handle_change_mac_rsp(crq, adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004794 break;
4795 case ERROR_INDICATION:
4796 netdev_dbg(netdev, "Got Error Indication\n");
4797 handle_error_indication(crq, adapter);
4798 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004799 case REQUEST_STATISTICS_RSP:
4800 netdev_dbg(netdev, "Got Statistics Response\n");
4801 complete(&adapter->stats_done);
4802 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004803 case QUERY_IP_OFFLOAD_RSP:
4804 netdev_dbg(netdev, "Got Query IP offload Response\n");
4805 handle_query_ip_offload_rsp(adapter);
4806 break;
4807 case MULTICAST_CTRL_RSP:
4808 netdev_dbg(netdev, "Got multicast control Response\n");
4809 break;
4810 case CONTROL_IP_OFFLOAD_RSP:
4811 netdev_dbg(netdev, "Got Control IP offload Response\n");
4812 dma_unmap_single(dev, adapter->ip_offload_ctrl_tok,
4813 sizeof(adapter->ip_offload_ctrl),
4814 DMA_TO_DEVICE);
John Allenbd0b6722017-03-17 17:13:40 -05004815 complete(&adapter->init_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004816 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004817 case COLLECT_FW_TRACE_RSP:
4818 netdev_dbg(netdev, "Got Collect firmware trace Response\n");
4819 complete(&adapter->fw_done);
4820 break;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02004821 case GET_VPD_SIZE_RSP:
4822 handle_vpd_size_rsp(crq, adapter);
4823 break;
4824 case GET_VPD_RSP:
4825 handle_vpd_rsp(crq, adapter);
4826 break;
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03004827 case QUERY_PHYS_PARMS_RSP:
4828 adapter->fw_done_rc = handle_query_phys_parms_rsp(crq, adapter);
4829 complete(&adapter->fw_done);
4830 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004831 default:
4832 netdev_err(netdev, "Got an invalid cmd type 0x%02x\n",
4833 gen_crq->cmd);
4834 }
4835}
4836
4837static irqreturn_t ibmvnic_interrupt(int irq, void *instance)
4838{
4839 struct ibmvnic_adapter *adapter = instance;
Thomas Falcon6c267b32017-02-15 12:17:58 -06004840
Thomas Falcon6c267b32017-02-15 12:17:58 -06004841 tasklet_schedule(&adapter->tasklet);
Thomas Falcon6c267b32017-02-15 12:17:58 -06004842 return IRQ_HANDLED;
4843}
4844
Allen Paisaa7c3fe2020-09-14 12:59:29 +05304845static void ibmvnic_tasklet(struct tasklet_struct *t)
Thomas Falcon6c267b32017-02-15 12:17:58 -06004846{
Allen Paisaa7c3fe2020-09-14 12:59:29 +05304847 struct ibmvnic_adapter *adapter = from_tasklet(adapter, t, tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004848 struct ibmvnic_crq_queue *queue = &adapter->crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004849 union ibmvnic_crq *crq;
4850 unsigned long flags;
4851 bool done = false;
4852
4853 spin_lock_irqsave(&queue->lock, flags);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004854 while (!done) {
4855 /* Pull all the valid messages off the CRQ */
4856 while ((crq = ibmvnic_next_crq(adapter)) != NULL) {
4857 ibmvnic_handle_crq(crq, adapter);
4858 crq->generic.first = 0;
4859 }
Brian Kinged7ecbf2017-04-19 13:44:53 -04004860
4861 /* remain in tasklet until all
4862 * capabilities responses are received
4863 */
4864 if (!adapter->wait_capability)
4865 done = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004866 }
Thomas Falcon249168a2017-02-15 12:18:00 -06004867 /* if capabilities CRQ's were sent in this tasklet, the following
4868 * tasklet must wait until all responses are received
4869 */
4870 if (atomic_read(&adapter->running_cap_crqs) != 0)
4871 adapter->wait_capability = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004872 spin_unlock_irqrestore(&queue->lock, flags);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004873}
4874
4875static int ibmvnic_reenable_crq_queue(struct ibmvnic_adapter *adapter)
4876{
4877 struct vio_dev *vdev = adapter->vdev;
4878 int rc;
4879
4880 do {
4881 rc = plpar_hcall_norets(H_ENABLE_CRQ, vdev->unit_address);
4882 } while (rc == H_IN_PROGRESS || rc == H_BUSY || H_IS_LONG_BUSY(rc));
4883
4884 if (rc)
4885 dev_err(&vdev->dev, "Error enabling adapter (rc=%d)\n", rc);
4886
4887 return rc;
4888}
4889
4890static int ibmvnic_reset_crq(struct ibmvnic_adapter *adapter)
4891{
4892 struct ibmvnic_crq_queue *crq = &adapter->crq;
4893 struct device *dev = &adapter->vdev->dev;
4894 struct vio_dev *vdev = adapter->vdev;
4895 int rc;
4896
4897 /* Close the CRQ */
4898 do {
4899 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
4900 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
4901
4902 /* Clean out the queue */
4903 memset(crq->msgs, 0, PAGE_SIZE);
4904 crq->cur = 0;
Thomas Falcon51536982018-05-23 13:37:56 -05004905 crq->active = false;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004906
4907 /* And re-open it again */
4908 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
4909 crq->msg_token, PAGE_SIZE);
4910
4911 if (rc == H_CLOSED)
4912 /* Adapter is good, but other end is not ready */
4913 dev_warn(dev, "Partner adapter not ready\n");
4914 else if (rc != 0)
4915 dev_warn(dev, "Couldn't register crq (rc=%d)\n", rc);
4916
4917 return rc;
4918}
4919
Nathan Fontenotf9928872017-03-30 02:48:54 -04004920static void release_crq_queue(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06004921{
4922 struct ibmvnic_crq_queue *crq = &adapter->crq;
4923 struct vio_dev *vdev = adapter->vdev;
4924 long rc;
4925
Nathan Fontenotf9928872017-03-30 02:48:54 -04004926 if (!crq->msgs)
4927 return;
4928
Thomas Falcon032c5e82015-12-21 11:26:06 -06004929 netdev_dbg(adapter->netdev, "Releasing CRQ\n");
4930 free_irq(vdev->irq, adapter);
Thomas Falcon6c267b32017-02-15 12:17:58 -06004931 tasklet_kill(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004932 do {
4933 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
4934 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
4935
4936 dma_unmap_single(&vdev->dev, crq->msg_token, PAGE_SIZE,
4937 DMA_BIDIRECTIONAL);
4938 free_page((unsigned long)crq->msgs);
Nathan Fontenotf9928872017-03-30 02:48:54 -04004939 crq->msgs = NULL;
Thomas Falcon51536982018-05-23 13:37:56 -05004940 crq->active = false;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004941}
4942
Nathan Fontenotf9928872017-03-30 02:48:54 -04004943static int init_crq_queue(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06004944{
4945 struct ibmvnic_crq_queue *crq = &adapter->crq;
4946 struct device *dev = &adapter->vdev->dev;
4947 struct vio_dev *vdev = adapter->vdev;
4948 int rc, retrc = -ENOMEM;
4949
Nathan Fontenotf9928872017-03-30 02:48:54 -04004950 if (crq->msgs)
4951 return 0;
4952
Thomas Falcon032c5e82015-12-21 11:26:06 -06004953 crq->msgs = (union ibmvnic_crq *)get_zeroed_page(GFP_KERNEL);
4954 /* Should we allocate more than one page? */
4955
4956 if (!crq->msgs)
4957 return -ENOMEM;
4958
4959 crq->size = PAGE_SIZE / sizeof(*crq->msgs);
4960 crq->msg_token = dma_map_single(dev, crq->msgs, PAGE_SIZE,
4961 DMA_BIDIRECTIONAL);
4962 if (dma_mapping_error(dev, crq->msg_token))
4963 goto map_failed;
4964
4965 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
4966 crq->msg_token, PAGE_SIZE);
4967
4968 if (rc == H_RESOURCE)
4969 /* maybe kexecing and resource is busy. try a reset */
4970 rc = ibmvnic_reset_crq(adapter);
4971 retrc = rc;
4972
4973 if (rc == H_CLOSED) {
4974 dev_warn(dev, "Partner adapter not ready\n");
4975 } else if (rc) {
4976 dev_warn(dev, "Error %d opening adapter\n", rc);
4977 goto reg_crq_failed;
4978 }
4979
4980 retrc = 0;
4981
Allen Paisaa7c3fe2020-09-14 12:59:29 +05304982 tasklet_setup(&adapter->tasklet, (void *)ibmvnic_tasklet);
Thomas Falcon6c267b32017-02-15 12:17:58 -06004983
Thomas Falcon032c5e82015-12-21 11:26:06 -06004984 netdev_dbg(adapter->netdev, "registering irq 0x%x\n", vdev->irq);
Murilo Fossa Vicentinie56e2512019-04-25 11:02:33 -03004985 snprintf(crq->name, sizeof(crq->name), "ibmvnic-%x",
4986 adapter->vdev->unit_address);
4987 rc = request_irq(vdev->irq, ibmvnic_interrupt, 0, crq->name, adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004988 if (rc) {
4989 dev_err(dev, "Couldn't register irq 0x%x. rc=%d\n",
4990 vdev->irq, rc);
4991 goto req_irq_failed;
4992 }
4993
4994 rc = vio_enable_interrupts(vdev);
4995 if (rc) {
4996 dev_err(dev, "Error %d enabling interrupts\n", rc);
4997 goto req_irq_failed;
4998 }
4999
5000 crq->cur = 0;
5001 spin_lock_init(&crq->lock);
5002
5003 return retrc;
5004
5005req_irq_failed:
Thomas Falcon6c267b32017-02-15 12:17:58 -06005006 tasklet_kill(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005007 do {
5008 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
5009 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
5010reg_crq_failed:
5011 dma_unmap_single(dev, crq->msg_token, PAGE_SIZE, DMA_BIDIRECTIONAL);
5012map_failed:
5013 free_page((unsigned long)crq->msgs);
Nathan Fontenotf9928872017-03-30 02:48:54 -04005014 crq->msgs = NULL;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005015 return retrc;
5016}
5017
Lijun Pan635e4422020-08-19 17:52:26 -05005018static int ibmvnic_reset_init(struct ibmvnic_adapter *adapter, bool reset)
John Allenf6ef6402017-03-17 17:13:42 -05005019{
5020 struct device *dev = &adapter->vdev->dev;
5021 unsigned long timeout = msecs_to_jiffies(30000);
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06005022 u64 old_num_rx_queues, old_num_tx_queues;
John Allenf6ef6402017-03-17 17:13:42 -05005023 int rc;
5024
John Allen017892c12017-05-26 10:30:19 -04005025 adapter->from_passive_init = false;
5026
Lijun Pan635e4422020-08-19 17:52:26 -05005027 if (reset) {
5028 old_num_rx_queues = adapter->req_rx_queues;
5029 old_num_tx_queues = adapter->req_tx_queues;
5030 reinit_completion(&adapter->init_done);
5031 }
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06005032
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04005033 adapter->init_done_rc = 0;
Lijun Panfa68bfa2020-08-19 17:52:24 -05005034 rc = ibmvnic_send_crq_init(adapter);
5035 if (rc) {
5036 dev_err(dev, "Send crq init failed with error %d\n", rc);
5037 return rc;
5038 }
5039
John Allenf6ef6402017-03-17 17:13:42 -05005040 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
5041 dev_err(dev, "Initialization sequence timed out\n");
John Allen017892c12017-05-26 10:30:19 -04005042 return -1;
5043 }
5044
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04005045 if (adapter->init_done_rc) {
5046 release_crq_queue(adapter);
5047 return adapter->init_done_rc;
5048 }
5049
Lijun Pan635e4422020-08-19 17:52:26 -05005050 if (reset &&
5051 test_bit(0, &adapter->resetting) && !adapter->wait_for_reset &&
Nathan Fontenot30f79622018-04-06 18:37:06 -05005052 adapter->reset_reason != VNIC_RESET_MOBILITY) {
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06005053 if (adapter->req_rx_queues != old_num_rx_queues ||
5054 adapter->req_tx_queues != old_num_tx_queues) {
5055 release_sub_crqs(adapter, 0);
5056 rc = init_sub_crqs(adapter);
5057 } else {
5058 rc = reset_sub_crq_queues(adapter);
5059 }
5060 } else {
Nathan Fontenot57a49432017-05-26 10:31:12 -04005061 rc = init_sub_crqs(adapter);
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06005062 }
5063
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04005064 if (rc) {
5065 dev_err(dev, "Initialization of sub crqs failed\n");
5066 release_crq_queue(adapter);
Thomas Falcon5df969c2017-06-28 19:55:54 -05005067 return rc;
5068 }
5069
5070 rc = init_sub_crq_irqs(adapter);
5071 if (rc) {
5072 dev_err(dev, "Failed to initialize sub crq irqs\n");
5073 release_crq_queue(adapter);
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04005074 }
5075
5076 return rc;
John Allenf6ef6402017-03-17 17:13:42 -05005077}
5078
Thomas Falcon40c9db82017-06-12 12:35:04 -05005079static struct device_attribute dev_attr_failover;
5080
Thomas Falcon032c5e82015-12-21 11:26:06 -06005081static int ibmvnic_probe(struct vio_dev *dev, const struct vio_device_id *id)
5082{
5083 struct ibmvnic_adapter *adapter;
5084 struct net_device *netdev;
5085 unsigned char *mac_addr_p;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005086 int rc;
5087
5088 dev_dbg(&dev->dev, "entering ibmvnic_probe for UA 0x%x\n",
5089 dev->unit_address);
5090
5091 mac_addr_p = (unsigned char *)vio_get_attribute(dev,
5092 VETH_MAC_ADDR, NULL);
5093 if (!mac_addr_p) {
5094 dev_err(&dev->dev,
5095 "(%s:%3.3d) ERROR: Can't find MAC_ADDR attribute\n",
5096 __FILE__, __LINE__);
5097 return 0;
5098 }
5099
5100 netdev = alloc_etherdev_mq(sizeof(struct ibmvnic_adapter),
Thomas Falcond45cc3a2017-12-18 12:52:11 -06005101 IBMVNIC_MAX_QUEUES);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005102 if (!netdev)
5103 return -ENOMEM;
5104
5105 adapter = netdev_priv(netdev);
Nathan Fontenot90c80142017-05-03 14:04:32 -04005106 adapter->state = VNIC_PROBING;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005107 dev_set_drvdata(&dev->dev, netdev);
5108 adapter->vdev = dev;
5109 adapter->netdev = netdev;
5110
5111 ether_addr_copy(adapter->mac_addr, mac_addr_p);
5112 ether_addr_copy(netdev->dev_addr, adapter->mac_addr);
5113 netdev->irq = dev->irq;
5114 netdev->netdev_ops = &ibmvnic_netdev_ops;
5115 netdev->ethtool_ops = &ibmvnic_ethtool_ops;
5116 SET_NETDEV_DEV(netdev, &dev->dev);
5117
5118 spin_lock_init(&adapter->stats_lock);
5119
Nathan Fontenoted651a12017-05-03 14:04:38 -04005120 INIT_WORK(&adapter->ibmvnic_reset, __ibmvnic_reset);
Juliet Kim7ed5b312019-09-20 16:11:23 -04005121 INIT_DELAYED_WORK(&adapter->ibmvnic_delayed_reset,
5122 __ibmvnic_delayed_reset);
Nathan Fontenoted651a12017-05-03 14:04:38 -04005123 INIT_LIST_HEAD(&adapter->rwi_list);
Thomas Falcon6c5c7482018-12-10 15:22:22 -06005124 spin_lock_init(&adapter->rwi_lock);
Juliet Kim7d7195a2020-03-10 09:23:58 -05005125 spin_lock_init(&adapter->state_lock);
Thomas Falconff25dcb2019-11-25 17:12:56 -06005126 mutex_init(&adapter->fw_lock);
Thomas Falconbbd669a2019-04-04 18:58:26 -05005127 init_completion(&adapter->init_done);
Thomas Falcon070eca92019-11-25 17:12:53 -06005128 init_completion(&adapter->fw_done);
5129 init_completion(&adapter->reset_done);
5130 init_completion(&adapter->stats_done);
Juliet Kim7ed5b312019-09-20 16:11:23 -04005131 clear_bit(0, &adapter->resetting);
Nathan Fontenoted651a12017-05-03 14:04:38 -04005132
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04005133 do {
Nathan Fontenot30f79622018-04-06 18:37:06 -05005134 rc = init_crq_queue(adapter);
5135 if (rc) {
5136 dev_err(&dev->dev, "Couldn't initialize crq. rc=%d\n",
5137 rc);
5138 goto ibmvnic_init_fail;
5139 }
5140
Lijun Pan635e4422020-08-19 17:52:26 -05005141 rc = ibmvnic_reset_init(adapter, false);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05005142 if (rc && rc != EAGAIN)
5143 goto ibmvnic_init_fail;
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04005144 } while (rc == EAGAIN);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005145
Thomas Falcon07184212018-05-16 15:49:05 -05005146 rc = init_stats_buffers(adapter);
5147 if (rc)
5148 goto ibmvnic_init_fail;
5149
5150 rc = init_stats_token(adapter);
5151 if (rc)
5152 goto ibmvnic_stats_fail;
5153
Thomas Falconf39f0d12017-02-14 10:22:59 -06005154 netdev->mtu = adapter->req_mtu - ETH_HLEN;
John Allenc26eba02017-10-26 16:23:25 -05005155 netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
5156 netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005157
Thomas Falcon40c9db82017-06-12 12:35:04 -05005158 rc = device_create_file(&dev->dev, &dev_attr_failover);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05005159 if (rc)
Thomas Falcon07184212018-05-16 15:49:05 -05005160 goto ibmvnic_dev_file_err;
Thomas Falcon40c9db82017-06-12 12:35:04 -05005161
Mick Tarsele876a8a2017-09-28 13:53:18 -07005162 netif_carrier_off(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005163 rc = register_netdev(netdev);
5164 if (rc) {
5165 dev_err(&dev->dev, "failed to register netdev rc=%d\n", rc);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05005166 goto ibmvnic_register_fail;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005167 }
5168 dev_info(&dev->dev, "ibmvnic registered\n");
5169
Nathan Fontenot90c80142017-05-03 14:04:32 -04005170 adapter->state = VNIC_PROBED;
John Allenc26eba02017-10-26 16:23:25 -05005171
5172 adapter->wait_for_reset = false;
5173
Thomas Falcon032c5e82015-12-21 11:26:06 -06005174 return 0;
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05005175
5176ibmvnic_register_fail:
5177 device_remove_file(&dev->dev, &dev_attr_failover);
5178
Thomas Falcon07184212018-05-16 15:49:05 -05005179ibmvnic_dev_file_err:
5180 release_stats_token(adapter);
5181
5182ibmvnic_stats_fail:
5183 release_stats_buffers(adapter);
5184
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05005185ibmvnic_init_fail:
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06005186 release_sub_crqs(adapter, 1);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05005187 release_crq_queue(adapter);
Thomas Falconff25dcb2019-11-25 17:12:56 -06005188 mutex_destroy(&adapter->fw_lock);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05005189 free_netdev(netdev);
5190
5191 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005192}
5193
5194static int ibmvnic_remove(struct vio_dev *dev)
5195{
5196 struct net_device *netdev = dev_get_drvdata(&dev->dev);
Nathan Fontenot37489052017-04-19 13:45:04 -04005197 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Juliet Kim7d7195a2020-03-10 09:23:58 -05005198 unsigned long flags;
5199
5200 spin_lock_irqsave(&adapter->state_lock, flags);
5201 if (adapter->state == VNIC_RESETTING) {
5202 spin_unlock_irqrestore(&adapter->state_lock, flags);
5203 return -EBUSY;
5204 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06005205
Nathan Fontenot90c80142017-05-03 14:04:32 -04005206 adapter->state = VNIC_REMOVING;
Juliet Kim7d7195a2020-03-10 09:23:58 -05005207 spin_unlock_irqrestore(&adapter->state_lock, flags);
5208
Thomas Falcon6954a9e2020-06-12 13:34:41 -05005209 flush_work(&adapter->ibmvnic_reset);
5210 flush_delayed_work(&adapter->ibmvnic_delayed_reset);
5211
Juliet Kima5681e22018-11-19 15:59:22 -06005212 rtnl_lock();
5213 unregister_netdevice(netdev);
Nathan Fontenot37489052017-04-19 13:45:04 -04005214
5215 release_resources(adapter);
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06005216 release_sub_crqs(adapter, 1);
Nathan Fontenot37489052017-04-19 13:45:04 -04005217 release_crq_queue(adapter);
5218
Thomas Falcon53cc7722018-02-26 18:10:56 -06005219 release_stats_token(adapter);
5220 release_stats_buffers(adapter);
5221
Nathan Fontenot90c80142017-05-03 14:04:32 -04005222 adapter->state = VNIC_REMOVED;
5223
Juliet Kima5681e22018-11-19 15:59:22 -06005224 rtnl_unlock();
Thomas Falconff25dcb2019-11-25 17:12:56 -06005225 mutex_destroy(&adapter->fw_lock);
Thomas Falcon40c9db82017-06-12 12:35:04 -05005226 device_remove_file(&dev->dev, &dev_attr_failover);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005227 free_netdev(netdev);
5228 dev_set_drvdata(&dev->dev, NULL);
5229
5230 return 0;
5231}
5232
Thomas Falcon40c9db82017-06-12 12:35:04 -05005233static ssize_t failover_store(struct device *dev, struct device_attribute *attr,
5234 const char *buf, size_t count)
5235{
5236 struct net_device *netdev = dev_get_drvdata(dev);
5237 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
5238 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
5239 __be64 session_token;
5240 long rc;
5241
5242 if (!sysfs_streq(buf, "1"))
5243 return -EINVAL;
5244
5245 rc = plpar_hcall(H_VIOCTL, retbuf, adapter->vdev->unit_address,
5246 H_GET_SESSION_TOKEN, 0, 0, 0);
5247 if (rc) {
5248 netdev_err(netdev, "Couldn't retrieve session token, rc %ld\n",
5249 rc);
5250 return -EINVAL;
5251 }
5252
5253 session_token = (__be64)retbuf[0];
5254 netdev_dbg(netdev, "Initiating client failover, session id %llx\n",
5255 be64_to_cpu(session_token));
5256 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
5257 H_SESSION_ERR_DETECTED, session_token, 0, 0);
5258 if (rc) {
5259 netdev_err(netdev, "Client initiated failover failed, rc %ld\n",
5260 rc);
5261 return -EINVAL;
5262 }
5263
5264 return count;
5265}
5266
Joe Perches6cbaefb2017-12-19 10:15:09 -08005267static DEVICE_ATTR_WO(failover);
Thomas Falcon40c9db82017-06-12 12:35:04 -05005268
Thomas Falcon032c5e82015-12-21 11:26:06 -06005269static unsigned long ibmvnic_get_desired_dma(struct vio_dev *vdev)
5270{
5271 struct net_device *netdev = dev_get_drvdata(&vdev->dev);
5272 struct ibmvnic_adapter *adapter;
5273 struct iommu_table *tbl;
5274 unsigned long ret = 0;
5275 int i;
5276
5277 tbl = get_iommu_table_base(&vdev->dev);
5278
5279 /* netdev inits at probe time along with the structures we need below*/
5280 if (!netdev)
5281 return IOMMU_PAGE_ALIGN(IBMVNIC_IO_ENTITLEMENT_DEFAULT, tbl);
5282
5283 adapter = netdev_priv(netdev);
5284
5285 ret += PAGE_SIZE; /* the crq message queue */
Thomas Falcon032c5e82015-12-21 11:26:06 -06005286 ret += IOMMU_PAGE_ALIGN(sizeof(struct ibmvnic_statistics), tbl);
5287
5288 for (i = 0; i < adapter->req_tx_queues + adapter->req_rx_queues; i++)
5289 ret += 4 * PAGE_SIZE; /* the scrq message queue */
5290
Thomas Falcon507ebe62020-08-21 13:39:01 -05005291 for (i = 0; i < adapter->num_active_rx_pools; i++)
Thomas Falcon032c5e82015-12-21 11:26:06 -06005292 ret += adapter->rx_pool[i].size *
5293 IOMMU_PAGE_ALIGN(adapter->rx_pool[i].buff_size, tbl);
5294
5295 return ret;
5296}
5297
5298static int ibmvnic_resume(struct device *dev)
5299{
5300 struct net_device *netdev = dev_get_drvdata(dev);
5301 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005302
John Allencb89ba22017-06-19 11:27:53 -05005303 if (adapter->state != VNIC_OPEN)
5304 return 0;
5305
John Allena2488782017-07-24 13:26:06 -05005306 tasklet_schedule(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005307
5308 return 0;
5309}
5310
Arvind Yadav8c37bc62017-08-17 18:52:54 +05305311static const struct vio_device_id ibmvnic_device_table[] = {
Thomas Falcon032c5e82015-12-21 11:26:06 -06005312 {"network", "IBM,vnic"},
5313 {"", "" }
5314};
5315MODULE_DEVICE_TABLE(vio, ibmvnic_device_table);
5316
5317static const struct dev_pm_ops ibmvnic_pm_ops = {
5318 .resume = ibmvnic_resume
5319};
5320
5321static struct vio_driver ibmvnic_driver = {
5322 .id_table = ibmvnic_device_table,
5323 .probe = ibmvnic_probe,
5324 .remove = ibmvnic_remove,
5325 .get_desired_dma = ibmvnic_get_desired_dma,
5326 .name = ibmvnic_driver_name,
5327 .pm = &ibmvnic_pm_ops,
5328};
5329
5330/* module functions */
5331static int __init ibmvnic_module_init(void)
5332{
5333 pr_info("%s: %s %s\n", ibmvnic_driver_name, ibmvnic_driver_string,
5334 IBMVNIC_DRIVER_VERSION);
5335
5336 return vio_register_driver(&ibmvnic_driver);
5337}
5338
5339static void __exit ibmvnic_module_exit(void)
5340{
5341 vio_unregister_driver(&ibmvnic_driver);
5342}
5343
5344module_init(ibmvnic_module_init);
5345module_exit(ibmvnic_module_exit);