blob: d3a774331afc7a71eab32652f214b4c3751f8cd5 [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);
John Allenea5509f2017-03-17 17:13:43 -0500107static int ibmvnic_init(struct ibmvnic_adapter *);
Thomas Falcon8a348452018-05-23 13:38:00 -0500108static int ibmvnic_reset_init(struct ibmvnic_adapter *);
Nathan Fontenotf9928872017-03-30 02:48:54 -0400109static void release_crq_queue(struct ibmvnic_adapter *);
Thomas Falcon62740e92019-05-09 23:13:43 -0500110static int __ibmvnic_set_mac(struct net_device *, u8 *);
Nathan Fontenot30f79622018-04-06 18:37:06 -0500111static int init_crq_queue(struct ibmvnic_adapter *adapter);
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -0300112static int send_query_phys_parms(struct ibmvnic_adapter *adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600113
114struct ibmvnic_stat {
115 char name[ETH_GSTRING_LEN];
116 int offset;
117};
118
119#define IBMVNIC_STAT_OFF(stat) (offsetof(struct ibmvnic_adapter, stats) + \
120 offsetof(struct ibmvnic_statistics, stat))
121#define IBMVNIC_GET_STAT(a, off) (*((u64 *)(((unsigned long)(a)) + off)))
122
123static const struct ibmvnic_stat ibmvnic_stats[] = {
124 {"rx_packets", IBMVNIC_STAT_OFF(rx_packets)},
125 {"rx_bytes", IBMVNIC_STAT_OFF(rx_bytes)},
126 {"tx_packets", IBMVNIC_STAT_OFF(tx_packets)},
127 {"tx_bytes", IBMVNIC_STAT_OFF(tx_bytes)},
128 {"ucast_tx_packets", IBMVNIC_STAT_OFF(ucast_tx_packets)},
129 {"ucast_rx_packets", IBMVNIC_STAT_OFF(ucast_rx_packets)},
130 {"mcast_tx_packets", IBMVNIC_STAT_OFF(mcast_tx_packets)},
131 {"mcast_rx_packets", IBMVNIC_STAT_OFF(mcast_rx_packets)},
132 {"bcast_tx_packets", IBMVNIC_STAT_OFF(bcast_tx_packets)},
133 {"bcast_rx_packets", IBMVNIC_STAT_OFF(bcast_rx_packets)},
134 {"align_errors", IBMVNIC_STAT_OFF(align_errors)},
135 {"fcs_errors", IBMVNIC_STAT_OFF(fcs_errors)},
136 {"single_collision_frames", IBMVNIC_STAT_OFF(single_collision_frames)},
137 {"multi_collision_frames", IBMVNIC_STAT_OFF(multi_collision_frames)},
138 {"sqe_test_errors", IBMVNIC_STAT_OFF(sqe_test_errors)},
139 {"deferred_tx", IBMVNIC_STAT_OFF(deferred_tx)},
140 {"late_collisions", IBMVNIC_STAT_OFF(late_collisions)},
141 {"excess_collisions", IBMVNIC_STAT_OFF(excess_collisions)},
142 {"internal_mac_tx_errors", IBMVNIC_STAT_OFF(internal_mac_tx_errors)},
143 {"carrier_sense", IBMVNIC_STAT_OFF(carrier_sense)},
144 {"too_long_frames", IBMVNIC_STAT_OFF(too_long_frames)},
145 {"internal_mac_rx_errors", IBMVNIC_STAT_OFF(internal_mac_rx_errors)},
146};
147
148static long h_reg_sub_crq(unsigned long unit_address, unsigned long token,
149 unsigned long length, unsigned long *number,
150 unsigned long *irq)
151{
152 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
153 long rc;
154
155 rc = plpar_hcall(H_REG_SUB_CRQ, retbuf, unit_address, token, length);
156 *number = retbuf[0];
157 *irq = retbuf[1];
158
159 return rc;
160}
161
Thomas Falcon476d96c2019-11-25 17:12:55 -0600162/**
163 * ibmvnic_wait_for_completion - Check device state and wait for completion
164 * @adapter: private device data
165 * @comp_done: completion structure to wait for
166 * @timeout: time to wait in milliseconds
167 *
168 * Wait for a completion signal or until the timeout limit is reached
169 * while checking that the device is still active.
170 */
171static int ibmvnic_wait_for_completion(struct ibmvnic_adapter *adapter,
172 struct completion *comp_done,
173 unsigned long timeout)
174{
175 struct net_device *netdev;
176 unsigned long div_timeout;
177 u8 retry;
178
179 netdev = adapter->netdev;
180 retry = 5;
181 div_timeout = msecs_to_jiffies(timeout / retry);
182 while (true) {
183 if (!adapter->crq.active) {
184 netdev_err(netdev, "Device down!\n");
185 return -ENODEV;
186 }
Thomas Falcon8f9cc1e2019-12-11 09:38:39 -0600187 if (!retry--)
Thomas Falcon476d96c2019-11-25 17:12:55 -0600188 break;
189 if (wait_for_completion_timeout(comp_done, div_timeout))
190 return 0;
191 }
192 netdev_err(netdev, "Operation timed out.\n");
193 return -ETIMEDOUT;
194}
195
Thomas Falcon032c5e82015-12-21 11:26:06 -0600196static int alloc_long_term_buff(struct ibmvnic_adapter *adapter,
197 struct ibmvnic_long_term_buff *ltb, int size)
198{
199 struct device *dev = &adapter->vdev->dev;
Thomas Falcon9c4eaab2018-05-23 13:37:57 -0500200 int rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600201
202 ltb->size = size;
203 ltb->buff = dma_alloc_coherent(dev, ltb->size, &ltb->addr,
204 GFP_KERNEL);
205
206 if (!ltb->buff) {
207 dev_err(dev, "Couldn't alloc long term buffer\n");
208 return -ENOMEM;
209 }
210 ltb->map_id = adapter->map_id;
211 adapter->map_id++;
Nathan Fontenotdb5d0b52017-02-10 13:45:05 -0500212
Thomas Falconff25dcb2019-11-25 17:12:56 -0600213 mutex_lock(&adapter->fw_lock);
214 adapter->fw_done_rc = 0;
Thomas Falcon070eca92019-11-25 17:12:53 -0600215 reinit_completion(&adapter->fw_done);
Thomas Falcon9c4eaab2018-05-23 13:37:57 -0500216 rc = send_request_map(adapter, ltb->addr,
217 ltb->size, ltb->map_id);
218 if (rc) {
219 dma_free_coherent(dev, ltb->size, ltb->buff, ltb->addr);
Thomas Falconff25dcb2019-11-25 17:12:56 -0600220 mutex_unlock(&adapter->fw_lock);
Thomas Falcon9c4eaab2018-05-23 13:37:57 -0500221 return rc;
222 }
Thomas Falcon476d96c2019-11-25 17:12:55 -0600223
224 rc = ibmvnic_wait_for_completion(adapter, &adapter->fw_done, 10000);
225 if (rc) {
226 dev_err(dev,
227 "Long term map request aborted or timed out,rc = %d\n",
228 rc);
229 dma_free_coherent(dev, ltb->size, ltb->buff, ltb->addr);
Thomas Falconff25dcb2019-11-25 17:12:56 -0600230 mutex_unlock(&adapter->fw_lock);
Thomas Falcon476d96c2019-11-25 17:12:55 -0600231 return rc;
232 }
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500233
234 if (adapter->fw_done_rc) {
235 dev_err(dev, "Couldn't map long term buffer,rc = %d\n",
236 adapter->fw_done_rc);
Thomas Falcon4cf2ddf32018-05-16 15:49:03 -0500237 dma_free_coherent(dev, ltb->size, ltb->buff, ltb->addr);
Thomas Falconff25dcb2019-11-25 17:12:56 -0600238 mutex_unlock(&adapter->fw_lock);
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500239 return -1;
240 }
Thomas Falconff25dcb2019-11-25 17:12:56 -0600241 mutex_unlock(&adapter->fw_lock);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600242 return 0;
243}
244
245static void free_long_term_buff(struct ibmvnic_adapter *adapter,
246 struct ibmvnic_long_term_buff *ltb)
247{
248 struct device *dev = &adapter->vdev->dev;
249
Nathan Fontenotc657e322017-03-30 02:49:06 -0400250 if (!ltb->buff)
251 return;
252
Nathan Fontenoted651a12017-05-03 14:04:38 -0400253 if (adapter->reset_reason != VNIC_RESET_FAILOVER &&
254 adapter->reset_reason != VNIC_RESET_MOBILITY)
Thomas Falcondfad09a2016-08-18 11:37:51 -0500255 send_request_unmap(adapter, ltb->map_id);
Brian King59af56c2017-04-19 13:44:41 -0400256 dma_free_coherent(dev, ltb->size, ltb->buff, ltb->addr);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600257}
258
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500259static int reset_long_term_buff(struct ibmvnic_adapter *adapter,
260 struct ibmvnic_long_term_buff *ltb)
261{
Thomas Falcon476d96c2019-11-25 17:12:55 -0600262 struct device *dev = &adapter->vdev->dev;
Thomas Falcon9c4eaab2018-05-23 13:37:57 -0500263 int rc;
264
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500265 memset(ltb->buff, 0, ltb->size);
266
Thomas Falconff25dcb2019-11-25 17:12:56 -0600267 mutex_lock(&adapter->fw_lock);
268 adapter->fw_done_rc = 0;
269
Thomas Falcon070eca92019-11-25 17:12:53 -0600270 reinit_completion(&adapter->fw_done);
Thomas Falcon9c4eaab2018-05-23 13:37:57 -0500271 rc = send_request_map(adapter, ltb->addr, ltb->size, ltb->map_id);
Thomas Falconff25dcb2019-11-25 17:12:56 -0600272 if (rc) {
273 mutex_unlock(&adapter->fw_lock);
Thomas Falcon9c4eaab2018-05-23 13:37:57 -0500274 return rc;
Thomas Falconff25dcb2019-11-25 17:12:56 -0600275 }
Thomas Falcon476d96c2019-11-25 17:12:55 -0600276
277 rc = ibmvnic_wait_for_completion(adapter, &adapter->fw_done, 10000);
278 if (rc) {
279 dev_info(dev,
280 "Reset failed, long term map request timed out or aborted\n");
Thomas Falconff25dcb2019-11-25 17:12:56 -0600281 mutex_unlock(&adapter->fw_lock);
Thomas Falcon476d96c2019-11-25 17:12:55 -0600282 return rc;
283 }
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500284
285 if (adapter->fw_done_rc) {
Thomas Falcon476d96c2019-11-25 17:12:55 -0600286 dev_info(dev,
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500287 "Reset failed, attempting to free and reallocate buffer\n");
288 free_long_term_buff(adapter, ltb);
Thomas Falconff25dcb2019-11-25 17:12:56 -0600289 mutex_unlock(&adapter->fw_lock);
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500290 return alloc_long_term_buff(adapter, ltb, ltb->size);
291 }
Thomas Falconff25dcb2019-11-25 17:12:56 -0600292 mutex_unlock(&adapter->fw_lock);
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500293 return 0;
294}
295
Thomas Falconf185a492017-05-26 10:30:48 -0400296static void deactivate_rx_pools(struct ibmvnic_adapter *adapter)
297{
298 int i;
299
300 for (i = 0; i < be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
301 i++)
302 adapter->rx_pool[i].active = 0;
303}
304
Thomas Falcon032c5e82015-12-21 11:26:06 -0600305static void replenish_rx_pool(struct ibmvnic_adapter *adapter,
306 struct ibmvnic_rx_pool *pool)
307{
308 int count = pool->size - atomic_read(&pool->available);
309 struct device *dev = &adapter->vdev->dev;
310 int buffers_added = 0;
311 unsigned long lpar_rc;
312 union sub_crq sub_crq;
313 struct sk_buff *skb;
314 unsigned int offset;
315 dma_addr_t dma_addr;
316 unsigned char *dst;
317 u64 *handle_array;
318 int shift = 0;
319 int index;
320 int i;
321
Thomas Falconf185a492017-05-26 10:30:48 -0400322 if (!pool->active)
323 return;
324
Thomas Falcon032c5e82015-12-21 11:26:06 -0600325 handle_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
326 be32_to_cpu(adapter->login_rsp_buf->
327 off_rxadd_subcrqs));
328
329 for (i = 0; i < count; ++i) {
330 skb = alloc_skb(pool->buff_size, GFP_ATOMIC);
331 if (!skb) {
332 dev_err(dev, "Couldn't replenish rx buff\n");
333 adapter->replenish_no_mem++;
334 break;
335 }
336
337 index = pool->free_map[pool->next_free];
338
339 if (pool->rx_buff[index].skb)
340 dev_err(dev, "Inconsistent free_map!\n");
341
342 /* Copy the skb to the long term mapped DMA buffer */
343 offset = index * pool->buff_size;
344 dst = pool->long_term_buff.buff + offset;
345 memset(dst, 0, pool->buff_size);
346 dma_addr = pool->long_term_buff.addr + offset;
347 pool->rx_buff[index].data = dst;
348
349 pool->free_map[pool->next_free] = IBMVNIC_INVALID_MAP;
350 pool->rx_buff[index].dma = dma_addr;
351 pool->rx_buff[index].skb = skb;
352 pool->rx_buff[index].pool_index = pool->index;
353 pool->rx_buff[index].size = pool->buff_size;
354
355 memset(&sub_crq, 0, sizeof(sub_crq));
356 sub_crq.rx_add.first = IBMVNIC_CRQ_CMD;
357 sub_crq.rx_add.correlator =
358 cpu_to_be64((u64)&pool->rx_buff[index]);
359 sub_crq.rx_add.ioba = cpu_to_be32(dma_addr);
360 sub_crq.rx_add.map_id = pool->long_term_buff.map_id;
361
362 /* The length field of the sCRQ is defined to be 24 bits so the
363 * buffer size needs to be left shifted by a byte before it is
364 * converted to big endian to prevent the last byte from being
365 * truncated.
366 */
367#ifdef __LITTLE_ENDIAN__
368 shift = 8;
369#endif
370 sub_crq.rx_add.len = cpu_to_be32(pool->buff_size << shift);
371
372 lpar_rc = send_subcrq(adapter, handle_array[pool->index],
373 &sub_crq);
374 if (lpar_rc != H_SUCCESS)
375 goto failure;
376
377 buffers_added++;
378 adapter->replenish_add_buff_success++;
379 pool->next_free = (pool->next_free + 1) % pool->size;
380 }
381 atomic_add(buffers_added, &pool->available);
382 return;
383
384failure:
Thomas Falcon2d14d372018-07-13 12:03:32 -0500385 if (lpar_rc != H_PARAMETER && lpar_rc != H_CLOSED)
386 dev_err_ratelimited(dev, "rx: replenish packet buffer failed\n");
Thomas Falcon032c5e82015-12-21 11:26:06 -0600387 pool->free_map[pool->next_free] = index;
388 pool->rx_buff[index].skb = NULL;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600389
390 dev_kfree_skb_any(skb);
391 adapter->replenish_add_buff_failure++;
392 atomic_add(buffers_added, &pool->available);
Thomas Falconf185a492017-05-26 10:30:48 -0400393
Thomas Falcon5a18e1e2018-04-06 18:37:05 -0500394 if (lpar_rc == H_CLOSED || adapter->failover_pending) {
Thomas Falconf185a492017-05-26 10:30:48 -0400395 /* Disable buffer pool replenishment and report carrier off if
Thomas Falcon5a18e1e2018-04-06 18:37:05 -0500396 * queue is closed or pending failover.
397 * Firmware guarantees that a signal will be sent to the
398 * driver, triggering a reset.
Thomas Falconf185a492017-05-26 10:30:48 -0400399 */
400 deactivate_rx_pools(adapter);
401 netif_carrier_off(adapter->netdev);
402 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600403}
404
405static void replenish_pools(struct ibmvnic_adapter *adapter)
406{
407 int i;
408
Thomas Falcon032c5e82015-12-21 11:26:06 -0600409 adapter->replenish_task_cycles++;
410 for (i = 0; i < be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
411 i++) {
412 if (adapter->rx_pool[i].active)
413 replenish_rx_pool(adapter, &adapter->rx_pool[i]);
414 }
415}
416
John Allen3d52b592017-08-02 16:44:14 -0500417static void release_stats_buffers(struct ibmvnic_adapter *adapter)
418{
419 kfree(adapter->tx_stats_buffers);
420 kfree(adapter->rx_stats_buffers);
Thomas Falconb0992ec2018-02-06 17:25:23 -0600421 adapter->tx_stats_buffers = NULL;
422 adapter->rx_stats_buffers = NULL;
John Allen3d52b592017-08-02 16:44:14 -0500423}
424
425static int init_stats_buffers(struct ibmvnic_adapter *adapter)
426{
427 adapter->tx_stats_buffers =
Nathan Fontenotabcae542018-02-19 13:30:47 -0600428 kcalloc(IBMVNIC_MAX_QUEUES,
John Allen3d52b592017-08-02 16:44:14 -0500429 sizeof(struct ibmvnic_tx_queue_stats),
430 GFP_KERNEL);
431 if (!adapter->tx_stats_buffers)
432 return -ENOMEM;
433
434 adapter->rx_stats_buffers =
Nathan Fontenotabcae542018-02-19 13:30:47 -0600435 kcalloc(IBMVNIC_MAX_QUEUES,
John Allen3d52b592017-08-02 16:44:14 -0500436 sizeof(struct ibmvnic_rx_queue_stats),
437 GFP_KERNEL);
438 if (!adapter->rx_stats_buffers)
439 return -ENOMEM;
440
441 return 0;
442}
443
Nathan Fontenot7bbc27a2017-03-30 02:49:23 -0400444static void release_stats_token(struct ibmvnic_adapter *adapter)
445{
446 struct device *dev = &adapter->vdev->dev;
447
448 if (!adapter->stats_token)
449 return;
450
451 dma_unmap_single(dev, adapter->stats_token,
452 sizeof(struct ibmvnic_statistics),
453 DMA_FROM_DEVICE);
454 adapter->stats_token = 0;
455}
456
457static int init_stats_token(struct ibmvnic_adapter *adapter)
458{
459 struct device *dev = &adapter->vdev->dev;
460 dma_addr_t stok;
461
462 stok = dma_map_single(dev, &adapter->stats,
463 sizeof(struct ibmvnic_statistics),
464 DMA_FROM_DEVICE);
465 if (dma_mapping_error(dev, stok)) {
466 dev_err(dev, "Couldn't map stats buffer\n");
467 return -1;
468 }
469
470 adapter->stats_token = stok;
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500471 netdev_dbg(adapter->netdev, "Stats token initialized (%llx)\n", stok);
Nathan Fontenot7bbc27a2017-03-30 02:49:23 -0400472 return 0;
473}
474
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400475static int reset_rx_pools(struct ibmvnic_adapter *adapter)
476{
477 struct ibmvnic_rx_pool *rx_pool;
478 int rx_scrqs;
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500479 int i, j, rc;
John Allen896d8692018-01-18 16:26:31 -0600480 u64 *size_array;
481
Mingming Cao9f134572020-08-25 13:26:41 -0400482 if (!adapter->rx_pool)
483 return -1;
484
John Allen896d8692018-01-18 16:26:31 -0600485 size_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
486 be32_to_cpu(adapter->login_rsp_buf->off_rxadd_buff_size));
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400487
488 rx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
489 for (i = 0; i < rx_scrqs; i++) {
490 rx_pool = &adapter->rx_pool[i];
491
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500492 netdev_dbg(adapter->netdev, "Re-setting rx_pool[%d]\n", i);
493
John Allen896d8692018-01-18 16:26:31 -0600494 if (rx_pool->buff_size != be64_to_cpu(size_array[i])) {
495 free_long_term_buff(adapter, &rx_pool->long_term_buff);
496 rx_pool->buff_size = be64_to_cpu(size_array[i]);
Thomas Falcon7c940b12019-06-07 16:03:55 -0500497 rc = alloc_long_term_buff(adapter,
498 &rx_pool->long_term_buff,
499 rx_pool->size *
500 rx_pool->buff_size);
John Allen896d8692018-01-18 16:26:31 -0600501 } else {
502 rc = reset_long_term_buff(adapter,
503 &rx_pool->long_term_buff);
504 }
505
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500506 if (rc)
507 return rc;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400508
509 for (j = 0; j < rx_pool->size; j++)
510 rx_pool->free_map[j] = j;
511
512 memset(rx_pool->rx_buff, 0,
513 rx_pool->size * sizeof(struct ibmvnic_rx_buff));
514
515 atomic_set(&rx_pool->available, 0);
516 rx_pool->next_alloc = 0;
517 rx_pool->next_free = 0;
Thomas Falconc3e53b92017-06-14 23:50:05 -0500518 rx_pool->active = 1;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400519 }
520
521 return 0;
522}
523
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400524static void release_rx_pools(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -0600525{
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400526 struct ibmvnic_rx_pool *rx_pool;
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400527 int i, j;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600528
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400529 if (!adapter->rx_pool)
Thomas Falcon032c5e82015-12-21 11:26:06 -0600530 return;
531
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600532 for (i = 0; i < adapter->num_active_rx_pools; i++) {
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400533 rx_pool = &adapter->rx_pool[i];
534
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500535 netdev_dbg(adapter->netdev, "Releasing rx_pool[%d]\n", i);
536
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400537 kfree(rx_pool->free_map);
538 free_long_term_buff(adapter, &rx_pool->long_term_buff);
539
540 if (!rx_pool->rx_buff)
Nathan Fontenote0ebe9422017-05-03 14:04:50 -0400541 continue;
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400542
543 for (j = 0; j < rx_pool->size; j++) {
544 if (rx_pool->rx_buff[j].skb) {
Thomas Falconb7cdec32018-11-21 11:17:58 -0600545 dev_kfree_skb_any(rx_pool->rx_buff[j].skb);
546 rx_pool->rx_buff[j].skb = NULL;
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400547 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600548 }
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400549
550 kfree(rx_pool->rx_buff);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600551 }
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400552
553 kfree(adapter->rx_pool);
554 adapter->rx_pool = NULL;
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600555 adapter->num_active_rx_pools = 0;
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400556}
557
558static int init_rx_pools(struct net_device *netdev)
559{
560 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
561 struct device *dev = &adapter->vdev->dev;
562 struct ibmvnic_rx_pool *rx_pool;
563 int rxadd_subcrqs;
564 u64 *size_array;
565 int i, j;
566
567 rxadd_subcrqs =
568 be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
569 size_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
570 be32_to_cpu(adapter->login_rsp_buf->off_rxadd_buff_size));
571
572 adapter->rx_pool = kcalloc(rxadd_subcrqs,
573 sizeof(struct ibmvnic_rx_pool),
574 GFP_KERNEL);
575 if (!adapter->rx_pool) {
576 dev_err(dev, "Failed to allocate rx pools\n");
577 return -1;
578 }
579
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600580 adapter->num_active_rx_pools = rxadd_subcrqs;
581
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400582 for (i = 0; i < rxadd_subcrqs; i++) {
583 rx_pool = &adapter->rx_pool[i];
584
585 netdev_dbg(adapter->netdev,
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500586 "Initializing rx_pool[%d], %lld buffs, %lld bytes each\n",
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400587 i, adapter->req_rx_add_entries_per_subcrq,
588 be64_to_cpu(size_array[i]));
589
590 rx_pool->size = adapter->req_rx_add_entries_per_subcrq;
591 rx_pool->index = i;
592 rx_pool->buff_size = be64_to_cpu(size_array[i]);
593 rx_pool->active = 1;
594
595 rx_pool->free_map = kcalloc(rx_pool->size, sizeof(int),
596 GFP_KERNEL);
597 if (!rx_pool->free_map) {
598 release_rx_pools(adapter);
599 return -1;
600 }
601
602 rx_pool->rx_buff = kcalloc(rx_pool->size,
603 sizeof(struct ibmvnic_rx_buff),
604 GFP_KERNEL);
605 if (!rx_pool->rx_buff) {
606 dev_err(dev, "Couldn't alloc rx buffers\n");
607 release_rx_pools(adapter);
608 return -1;
609 }
610
611 if (alloc_long_term_buff(adapter, &rx_pool->long_term_buff,
612 rx_pool->size * rx_pool->buff_size)) {
613 release_rx_pools(adapter);
614 return -1;
615 }
616
617 for (j = 0; j < rx_pool->size; ++j)
618 rx_pool->free_map[j] = j;
619
620 atomic_set(&rx_pool->available, 0);
621 rx_pool->next_alloc = 0;
622 rx_pool->next_free = 0;
623 }
624
625 return 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600626}
627
Thomas Falcone26dc252018-03-16 20:00:25 -0500628static int reset_one_tx_pool(struct ibmvnic_adapter *adapter,
629 struct ibmvnic_tx_pool *tx_pool)
630{
631 int rc, i;
632
633 rc = reset_long_term_buff(adapter, &tx_pool->long_term_buff);
634 if (rc)
635 return rc;
636
637 memset(tx_pool->tx_buff, 0,
638 tx_pool->num_buffers *
639 sizeof(struct ibmvnic_tx_buff));
640
641 for (i = 0; i < tx_pool->num_buffers; i++)
642 tx_pool->free_map[i] = i;
643
644 tx_pool->consumer_index = 0;
645 tx_pool->producer_index = 0;
646
647 return 0;
648}
649
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400650static int reset_tx_pools(struct ibmvnic_adapter *adapter)
651{
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400652 int tx_scrqs;
Thomas Falcone26dc252018-03-16 20:00:25 -0500653 int i, rc;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400654
Mingming Cao9f134572020-08-25 13:26:41 -0400655 if (!adapter->tx_pool)
656 return -1;
657
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400658 tx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
659 for (i = 0; i < tx_scrqs; i++) {
Thomas Falcone26dc252018-03-16 20:00:25 -0500660 rc = reset_one_tx_pool(adapter, &adapter->tso_pool[i]);
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500661 if (rc)
662 return rc;
Thomas Falcone26dc252018-03-16 20:00:25 -0500663 rc = reset_one_tx_pool(adapter, &adapter->tx_pool[i]);
Thomas Falconfdb06102017-10-17 12:36:55 -0500664 if (rc)
665 return rc;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400666 }
667
668 return 0;
669}
670
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -0200671static void release_vpd_data(struct ibmvnic_adapter *adapter)
672{
673 if (!adapter->vpd)
674 return;
675
676 kfree(adapter->vpd->buff);
677 kfree(adapter->vpd);
Thomas Falconb0992ec2018-02-06 17:25:23 -0600678
679 adapter->vpd = NULL;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -0200680}
681
Thomas Falconfb794212018-03-16 20:00:26 -0500682static void release_one_tx_pool(struct ibmvnic_adapter *adapter,
683 struct ibmvnic_tx_pool *tx_pool)
684{
685 kfree(tx_pool->tx_buff);
686 kfree(tx_pool->free_map);
687 free_long_term_buff(adapter, &tx_pool->long_term_buff);
688}
689
Nathan Fontenotc657e322017-03-30 02:49:06 -0400690static void release_tx_pools(struct ibmvnic_adapter *adapter)
691{
John Allen896d8692018-01-18 16:26:31 -0600692 int i;
Nathan Fontenotc657e322017-03-30 02:49:06 -0400693
694 if (!adapter->tx_pool)
695 return;
696
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600697 for (i = 0; i < adapter->num_active_tx_pools; i++) {
Thomas Falconfb794212018-03-16 20:00:26 -0500698 release_one_tx_pool(adapter, &adapter->tx_pool[i]);
699 release_one_tx_pool(adapter, &adapter->tso_pool[i]);
Nathan Fontenotc657e322017-03-30 02:49:06 -0400700 }
701
702 kfree(adapter->tx_pool);
703 adapter->tx_pool = NULL;
Thomas Falconfb794212018-03-16 20:00:26 -0500704 kfree(adapter->tso_pool);
705 adapter->tso_pool = NULL;
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600706 adapter->num_active_tx_pools = 0;
Nathan Fontenotc657e322017-03-30 02:49:06 -0400707}
708
Thomas Falcon32053062018-03-16 20:00:27 -0500709static int init_one_tx_pool(struct net_device *netdev,
710 struct ibmvnic_tx_pool *tx_pool,
711 int num_entries, int buf_size)
712{
713 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
714 int i;
715
716 tx_pool->tx_buff = kcalloc(num_entries,
717 sizeof(struct ibmvnic_tx_buff),
718 GFP_KERNEL);
719 if (!tx_pool->tx_buff)
720 return -1;
721
722 if (alloc_long_term_buff(adapter, &tx_pool->long_term_buff,
723 num_entries * buf_size))
724 return -1;
725
726 tx_pool->free_map = kcalloc(num_entries, sizeof(int), GFP_KERNEL);
727 if (!tx_pool->free_map)
728 return -1;
729
730 for (i = 0; i < num_entries; i++)
731 tx_pool->free_map[i] = i;
732
733 tx_pool->consumer_index = 0;
734 tx_pool->producer_index = 0;
735 tx_pool->num_buffers = num_entries;
736 tx_pool->buf_size = buf_size;
737
738 return 0;
739}
740
Nathan Fontenotc657e322017-03-30 02:49:06 -0400741static int init_tx_pools(struct net_device *netdev)
742{
743 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Nathan Fontenotc657e322017-03-30 02:49:06 -0400744 int tx_subcrqs;
Thomas Falcon32053062018-03-16 20:00:27 -0500745 int i, rc;
Nathan Fontenotc657e322017-03-30 02:49:06 -0400746
747 tx_subcrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
748 adapter->tx_pool = kcalloc(tx_subcrqs,
749 sizeof(struct ibmvnic_tx_pool), GFP_KERNEL);
750 if (!adapter->tx_pool)
751 return -1;
752
Thomas Falcon32053062018-03-16 20:00:27 -0500753 adapter->tso_pool = kcalloc(tx_subcrqs,
754 sizeof(struct ibmvnic_tx_pool), GFP_KERNEL);
755 if (!adapter->tso_pool)
756 return -1;
757
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600758 adapter->num_active_tx_pools = tx_subcrqs;
759
Nathan Fontenotc657e322017-03-30 02:49:06 -0400760 for (i = 0; i < tx_subcrqs; i++) {
Thomas Falcon32053062018-03-16 20:00:27 -0500761 rc = init_one_tx_pool(netdev, &adapter->tx_pool[i],
762 adapter->req_tx_entries_per_subcrq,
763 adapter->req_mtu + VLAN_HLEN);
764 if (rc) {
Nathan Fontenotc657e322017-03-30 02:49:06 -0400765 release_tx_pools(adapter);
Thomas Falcon32053062018-03-16 20:00:27 -0500766 return rc;
Nathan Fontenotc657e322017-03-30 02:49:06 -0400767 }
768
Thomas Falcon7c940b12019-06-07 16:03:55 -0500769 rc = init_one_tx_pool(netdev, &adapter->tso_pool[i],
770 IBMVNIC_TSO_BUFS,
771 IBMVNIC_TSO_BUF_SZ);
Thomas Falcon32053062018-03-16 20:00:27 -0500772 if (rc) {
Nathan Fontenotc657e322017-03-30 02:49:06 -0400773 release_tx_pools(adapter);
Thomas Falcon32053062018-03-16 20:00:27 -0500774 return rc;
Nathan Fontenotc657e322017-03-30 02:49:06 -0400775 }
Nathan Fontenotc657e322017-03-30 02:49:06 -0400776 }
777
778 return 0;
779}
780
John Allend944c3d62017-05-26 10:30:13 -0400781static void ibmvnic_napi_enable(struct ibmvnic_adapter *adapter)
782{
783 int i;
784
785 if (adapter->napi_enabled)
786 return;
787
788 for (i = 0; i < adapter->req_rx_queues; i++)
789 napi_enable(&adapter->napi[i]);
790
791 adapter->napi_enabled = true;
792}
793
794static void ibmvnic_napi_disable(struct ibmvnic_adapter *adapter)
795{
796 int i;
797
798 if (!adapter->napi_enabled)
799 return;
800
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500801 for (i = 0; i < adapter->req_rx_queues; i++) {
802 netdev_dbg(adapter->netdev, "Disabling napi[%d]\n", i);
John Allend944c3d62017-05-26 10:30:13 -0400803 napi_disable(&adapter->napi[i]);
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500804 }
John Allend944c3d62017-05-26 10:30:13 -0400805
806 adapter->napi_enabled = false;
807}
808
Nathan Fontenot86f669b2018-02-19 13:30:39 -0600809static int init_napi(struct ibmvnic_adapter *adapter)
810{
811 int i;
812
813 adapter->napi = kcalloc(adapter->req_rx_queues,
814 sizeof(struct napi_struct), GFP_KERNEL);
815 if (!adapter->napi)
816 return -ENOMEM;
817
818 for (i = 0; i < adapter->req_rx_queues; i++) {
819 netdev_dbg(adapter->netdev, "Adding napi[%d]\n", i);
820 netif_napi_add(adapter->netdev, &adapter->napi[i],
821 ibmvnic_poll, NAPI_POLL_WEIGHT);
822 }
823
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600824 adapter->num_active_rx_napi = adapter->req_rx_queues;
Nathan Fontenot86f669b2018-02-19 13:30:39 -0600825 return 0;
826}
827
828static void release_napi(struct ibmvnic_adapter *adapter)
829{
830 int i;
831
832 if (!adapter->napi)
833 return;
834
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600835 for (i = 0; i < adapter->num_active_rx_napi; i++) {
Wen Yang390de192018-12-11 12:20:46 +0800836 netdev_dbg(adapter->netdev, "Releasing napi[%d]\n", i);
837 netif_napi_del(&adapter->napi[i]);
Nathan Fontenot86f669b2018-02-19 13:30:39 -0600838 }
839
840 kfree(adapter->napi);
841 adapter->napi = NULL;
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600842 adapter->num_active_rx_napi = 0;
Thomas Falconc3f22412018-05-23 13:37:55 -0500843 adapter->napi_enabled = false;
Nathan Fontenot86f669b2018-02-19 13:30:39 -0600844}
845
John Allena57a5d22017-03-17 17:13:41 -0500846static int ibmvnic_login(struct net_device *netdev)
Thomas Falcon032c5e82015-12-21 11:26:06 -0600847{
848 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
John Allenbd0b6722017-03-17 17:13:40 -0500849 unsigned long timeout = msecs_to_jiffies(30000);
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500850 int retry_count = 0;
Thomas Falcondff515a32020-06-15 10:29:23 -0500851 int retries = 10;
Thomas Falconeb110412018-05-24 14:37:53 -0500852 bool retry;
Thomas Falcon4d96f122017-08-01 15:04:36 -0500853 int rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600854
John Allenbd0b6722017-03-17 17:13:40 -0500855 do {
Thomas Falconeb110412018-05-24 14:37:53 -0500856 retry = false;
Thomas Falcondff515a32020-06-15 10:29:23 -0500857 if (retry_count > retries) {
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500858 netdev_warn(netdev, "Login attempts exceeded\n");
859 return -1;
860 }
861
862 adapter->init_done_rc = 0;
863 reinit_completion(&adapter->init_done);
864 rc = send_login(adapter);
865 if (rc) {
866 netdev_warn(netdev, "Unable to login\n");
867 return rc;
868 }
869
870 if (!wait_for_completion_timeout(&adapter->init_done,
871 timeout)) {
Thomas Falcondff515a32020-06-15 10:29:23 -0500872 netdev_warn(netdev, "Login timed out, retrying...\n");
873 retry = true;
874 adapter->init_done_rc = 0;
875 retry_count++;
876 continue;
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500877 }
878
Thomas Falcondff515a32020-06-15 10:29:23 -0500879 if (adapter->init_done_rc == ABORTED) {
880 netdev_warn(netdev, "Login aborted, retrying...\n");
881 retry = true;
882 adapter->init_done_rc = 0;
883 retry_count++;
884 /* FW or device may be busy, so
885 * wait a bit before retrying login
886 */
887 msleep(500);
888 } else if (adapter->init_done_rc == PARTIALSUCCESS) {
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500889 retry_count++;
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -0600890 release_sub_crqs(adapter, 1);
John Allenbd0b6722017-03-17 17:13:40 -0500891
Thomas Falconeb110412018-05-24 14:37:53 -0500892 retry = true;
893 netdev_dbg(netdev,
894 "Received partial success, retrying...\n");
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500895 adapter->init_done_rc = 0;
John Allenbd0b6722017-03-17 17:13:40 -0500896 reinit_completion(&adapter->init_done);
897 send_cap_queries(adapter);
898 if (!wait_for_completion_timeout(&adapter->init_done,
899 timeout)) {
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500900 netdev_warn(netdev,
901 "Capabilities query timed out\n");
John Allenbd0b6722017-03-17 17:13:40 -0500902 return -1;
903 }
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500904
Thomas Falcon4d96f122017-08-01 15:04:36 -0500905 rc = init_sub_crqs(adapter);
906 if (rc) {
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500907 netdev_warn(netdev,
908 "SCRQ initialization failed\n");
Thomas Falcon4d96f122017-08-01 15:04:36 -0500909 return -1;
910 }
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500911
Thomas Falcon4d96f122017-08-01 15:04:36 -0500912 rc = init_sub_crq_irqs(adapter);
913 if (rc) {
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500914 netdev_warn(netdev,
915 "SCRQ irq initialization failed\n");
Thomas Falcon4d96f122017-08-01 15:04:36 -0500916 return -1;
917 }
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500918 } else if (adapter->init_done_rc) {
919 netdev_warn(netdev, "Adapter login failed\n");
John Allenbd0b6722017-03-17 17:13:40 -0500920 return -1;
921 }
Thomas Falconeb110412018-05-24 14:37:53 -0500922 } while (retry);
John Allenbd0b6722017-03-17 17:13:40 -0500923
Thomas Falcon62740e92019-05-09 23:13:43 -0500924 __ibmvnic_set_mac(netdev, adapter->mac_addr);
Thomas Falcon3d166132018-01-10 19:39:52 -0600925
John Allena57a5d22017-03-17 17:13:41 -0500926 return 0;
927}
928
Thomas Falcon34f0f4e2018-02-13 18:23:40 -0600929static void release_login_buffer(struct ibmvnic_adapter *adapter)
930{
931 kfree(adapter->login_buf);
932 adapter->login_buf = NULL;
933}
934
935static void release_login_rsp_buffer(struct ibmvnic_adapter *adapter)
936{
937 kfree(adapter->login_rsp_buf);
938 adapter->login_rsp_buf = NULL;
939}
940
Nathan Fontenot1b8955e2017-03-30 02:49:29 -0400941static void release_resources(struct ibmvnic_adapter *adapter)
942{
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -0200943 release_vpd_data(adapter);
944
Nathan Fontenot1b8955e2017-03-30 02:49:29 -0400945 release_tx_pools(adapter);
946 release_rx_pools(adapter);
947
Nathan Fontenot86f669b2018-02-19 13:30:39 -0600948 release_napi(adapter);
Thomas Falcon34f0f4e2018-02-13 18:23:40 -0600949 release_login_rsp_buffer(adapter);
Nathan Fontenot1b8955e2017-03-30 02:49:29 -0400950}
951
Nathan Fontenot53da09e2017-04-21 15:39:04 -0400952static int set_link_state(struct ibmvnic_adapter *adapter, u8 link_state)
953{
954 struct net_device *netdev = adapter->netdev;
955 unsigned long timeout = msecs_to_jiffies(30000);
956 union ibmvnic_crq crq;
957 bool resend;
958 int rc;
959
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500960 netdev_dbg(netdev, "setting link state %d\n", link_state);
961
Nathan Fontenot53da09e2017-04-21 15:39:04 -0400962 memset(&crq, 0, sizeof(crq));
963 crq.logical_link_state.first = IBMVNIC_CRQ_CMD;
964 crq.logical_link_state.cmd = LOGICAL_LINK_STATE;
965 crq.logical_link_state.link_state = link_state;
966
967 do {
968 resend = false;
969
970 reinit_completion(&adapter->init_done);
971 rc = ibmvnic_send_crq(adapter, &crq);
972 if (rc) {
973 netdev_err(netdev, "Failed to set link state\n");
974 return rc;
975 }
976
977 if (!wait_for_completion_timeout(&adapter->init_done,
978 timeout)) {
979 netdev_err(netdev, "timeout setting link state\n");
980 return -1;
981 }
982
983 if (adapter->init_done_rc == 1) {
984 /* Partuial success, delay and re-send */
985 mdelay(1000);
986 resend = true;
Thomas Falconab5ec332018-05-23 13:37:59 -0500987 } else if (adapter->init_done_rc) {
988 netdev_warn(netdev, "Unable to set link state, rc=%d\n",
989 adapter->init_done_rc);
990 return adapter->init_done_rc;
Nathan Fontenot53da09e2017-04-21 15:39:04 -0400991 }
992 } while (resend);
993
994 return 0;
995}
996
Thomas Falcon7f3c6e62017-04-21 15:38:40 -0400997static int set_real_num_queues(struct net_device *netdev)
998{
999 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1000 int rc;
1001
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001002 netdev_dbg(netdev, "Setting real tx/rx queues (%llx/%llx)\n",
1003 adapter->req_tx_queues, adapter->req_rx_queues);
1004
Thomas Falcon7f3c6e62017-04-21 15:38:40 -04001005 rc = netif_set_real_num_tx_queues(netdev, adapter->req_tx_queues);
1006 if (rc) {
1007 netdev_err(netdev, "failed to set the number of tx queues\n");
1008 return rc;
1009 }
1010
1011 rc = netif_set_real_num_rx_queues(netdev, adapter->req_rx_queues);
1012 if (rc)
1013 netdev_err(netdev, "failed to set the number of rx queues\n");
1014
1015 return rc;
1016}
1017
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001018static int ibmvnic_get_vpd(struct ibmvnic_adapter *adapter)
1019{
1020 struct device *dev = &adapter->vdev->dev;
1021 union ibmvnic_crq crq;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001022 int len = 0;
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05001023 int rc;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001024
1025 if (adapter->vpd->buff)
1026 len = adapter->vpd->len;
1027
Thomas Falconff25dcb2019-11-25 17:12:56 -06001028 mutex_lock(&adapter->fw_lock);
1029 adapter->fw_done_rc = 0;
Thomas Falcon070eca92019-11-25 17:12:53 -06001030 reinit_completion(&adapter->fw_done);
Thomas Falconff25dcb2019-11-25 17:12:56 -06001031
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001032 crq.get_vpd_size.first = IBMVNIC_CRQ_CMD;
1033 crq.get_vpd_size.cmd = GET_VPD_SIZE;
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05001034 rc = ibmvnic_send_crq(adapter, &crq);
Thomas Falconff25dcb2019-11-25 17:12:56 -06001035 if (rc) {
1036 mutex_unlock(&adapter->fw_lock);
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05001037 return rc;
Thomas Falconff25dcb2019-11-25 17:12:56 -06001038 }
Thomas Falcon476d96c2019-11-25 17:12:55 -06001039
1040 rc = ibmvnic_wait_for_completion(adapter, &adapter->fw_done, 10000);
1041 if (rc) {
1042 dev_err(dev, "Could not retrieve VPD size, rc = %d\n", rc);
Thomas Falconff25dcb2019-11-25 17:12:56 -06001043 mutex_unlock(&adapter->fw_lock);
Thomas Falcon476d96c2019-11-25 17:12:55 -06001044 return rc;
1045 }
Thomas Falconff25dcb2019-11-25 17:12:56 -06001046 mutex_unlock(&adapter->fw_lock);
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001047
1048 if (!adapter->vpd->len)
1049 return -ENODATA;
1050
1051 if (!adapter->vpd->buff)
1052 adapter->vpd->buff = kzalloc(adapter->vpd->len, GFP_KERNEL);
1053 else if (adapter->vpd->len != len)
1054 adapter->vpd->buff =
1055 krealloc(adapter->vpd->buff,
1056 adapter->vpd->len, GFP_KERNEL);
1057
1058 if (!adapter->vpd->buff) {
1059 dev_err(dev, "Could allocate VPD buffer\n");
1060 return -ENOMEM;
1061 }
1062
1063 adapter->vpd->dma_addr =
1064 dma_map_single(dev, adapter->vpd->buff, adapter->vpd->len,
1065 DMA_FROM_DEVICE);
Desnes Augusto Nunes do Rosariof7431062017-11-17 09:09:04 -02001066 if (dma_mapping_error(dev, adapter->vpd->dma_addr)) {
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001067 dev_err(dev, "Could not map VPD buffer\n");
1068 kfree(adapter->vpd->buff);
Thomas Falconb0992ec2018-02-06 17:25:23 -06001069 adapter->vpd->buff = NULL;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001070 return -ENOMEM;
1071 }
1072
Thomas Falconff25dcb2019-11-25 17:12:56 -06001073 mutex_lock(&adapter->fw_lock);
1074 adapter->fw_done_rc = 0;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001075 reinit_completion(&adapter->fw_done);
Thomas Falconff25dcb2019-11-25 17:12:56 -06001076
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001077 crq.get_vpd.first = IBMVNIC_CRQ_CMD;
1078 crq.get_vpd.cmd = GET_VPD;
1079 crq.get_vpd.ioba = cpu_to_be32(adapter->vpd->dma_addr);
1080 crq.get_vpd.len = cpu_to_be32((u32)adapter->vpd->len);
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05001081 rc = ibmvnic_send_crq(adapter, &crq);
1082 if (rc) {
1083 kfree(adapter->vpd->buff);
1084 adapter->vpd->buff = NULL;
Thomas Falconff25dcb2019-11-25 17:12:56 -06001085 mutex_unlock(&adapter->fw_lock);
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05001086 return rc;
1087 }
Thomas Falcon476d96c2019-11-25 17:12:55 -06001088
1089 rc = ibmvnic_wait_for_completion(adapter, &adapter->fw_done, 10000);
1090 if (rc) {
1091 dev_err(dev, "Unable to retrieve VPD, rc = %d\n", rc);
1092 kfree(adapter->vpd->buff);
1093 adapter->vpd->buff = NULL;
Thomas Falconff25dcb2019-11-25 17:12:56 -06001094 mutex_unlock(&adapter->fw_lock);
Thomas Falcon476d96c2019-11-25 17:12:55 -06001095 return rc;
1096 }
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001097
Thomas Falconff25dcb2019-11-25 17:12:56 -06001098 mutex_unlock(&adapter->fw_lock);
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001099 return 0;
1100}
1101
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001102static int init_resources(struct ibmvnic_adapter *adapter)
John Allena57a5d22017-03-17 17:13:41 -05001103{
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001104 struct net_device *netdev = adapter->netdev;
Nathan Fontenot86f669b2018-02-19 13:30:39 -06001105 int rc;
John Allena57a5d22017-03-17 17:13:41 -05001106
Thomas Falcon7f3c6e62017-04-21 15:38:40 -04001107 rc = set_real_num_queues(netdev);
1108 if (rc)
1109 return rc;
John Allenbd0b6722017-03-17 17:13:40 -05001110
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001111 adapter->vpd = kzalloc(sizeof(*adapter->vpd), GFP_KERNEL);
1112 if (!adapter->vpd)
1113 return -ENOMEM;
1114
John Allen69d08dc2018-01-18 16:27:58 -06001115 /* Vital Product Data (VPD) */
1116 rc = ibmvnic_get_vpd(adapter);
1117 if (rc) {
1118 netdev_err(netdev, "failed to initialize Vital Product Data (VPD)\n");
1119 return rc;
1120 }
1121
Thomas Falcon032c5e82015-12-21 11:26:06 -06001122 adapter->map_id = 1;
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001123
Nathan Fontenot86f669b2018-02-19 13:30:39 -06001124 rc = init_napi(adapter);
1125 if (rc)
1126 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001127
Thomas Falcon032c5e82015-12-21 11:26:06 -06001128 send_map_query(adapter);
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -04001129
1130 rc = init_rx_pools(netdev);
1131 if (rc)
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001132 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001133
Nathan Fontenotc657e322017-03-30 02:49:06 -04001134 rc = init_tx_pools(netdev);
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001135 return rc;
1136}
1137
Nathan Fontenoted651a12017-05-03 14:04:38 -04001138static int __ibmvnic_open(struct net_device *netdev)
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001139{
1140 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001141 enum vnic_state prev_state = adapter->state;
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001142 int i, rc;
1143
Nathan Fontenot90c80142017-05-03 14:04:32 -04001144 adapter->state = VNIC_OPENING;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001145 replenish_pools(adapter);
John Allend944c3d62017-05-26 10:30:13 -04001146 ibmvnic_napi_enable(adapter);
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001147
Thomas Falcon032c5e82015-12-21 11:26:06 -06001148 /* We're ready to receive frames, enable the sub-crq interrupts and
1149 * set the logical link state to up
1150 */
Nathan Fontenoted651a12017-05-03 14:04:38 -04001151 for (i = 0; i < adapter->req_rx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001152 netdev_dbg(netdev, "Enabling rx_scrq[%d] irq\n", i);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001153 if (prev_state == VNIC_CLOSED)
1154 enable_irq(adapter->rx_scrq[i]->irq);
Thomas Falconf23e0642018-04-15 18:53:36 -05001155 enable_scrq_irq(adapter, adapter->rx_scrq[i]);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001156 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001157
Nathan Fontenoted651a12017-05-03 14:04:38 -04001158 for (i = 0; i < adapter->req_tx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001159 netdev_dbg(netdev, "Enabling tx_scrq[%d] irq\n", i);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001160 if (prev_state == VNIC_CLOSED)
1161 enable_irq(adapter->tx_scrq[i]->irq);
Thomas Falconf23e0642018-04-15 18:53:36 -05001162 enable_scrq_irq(adapter, adapter->tx_scrq[i]);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001163 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001164
Nathan Fontenot53da09e2017-04-21 15:39:04 -04001165 rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_UP);
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001166 if (rc) {
1167 for (i = 0; i < adapter->req_rx_queues; i++)
1168 napi_disable(&adapter->napi[i]);
1169 release_resources(adapter);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001170 return rc;
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001171 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001172
Nathan Fontenoted651a12017-05-03 14:04:38 -04001173 netif_tx_start_all_queues(netdev);
1174
1175 if (prev_state == VNIC_CLOSED) {
1176 for (i = 0; i < adapter->req_rx_queues; i++)
1177 napi_schedule(&adapter->napi[i]);
1178 }
1179
1180 adapter->state = VNIC_OPEN;
1181 return rc;
1182}
1183
1184static int ibmvnic_open(struct net_device *netdev)
1185{
1186 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
John Allen69d08dc2018-01-18 16:27:58 -06001187 int rc;
Nathan Fontenoted651a12017-05-03 14:04:38 -04001188
Thomas Falcon5a18e1e2018-04-06 18:37:05 -05001189 /* If device failover is pending, just set device state and return.
1190 * Device operation will be handled by reset routine.
1191 */
1192 if (adapter->failover_pending) {
1193 adapter->state = VNIC_OPEN;
1194 return 0;
1195 }
1196
Nathan Fontenoted651a12017-05-03 14:04:38 -04001197 if (adapter->state != VNIC_CLOSED) {
1198 rc = ibmvnic_login(netdev);
Juliet Kima5681e22018-11-19 15:59:22 -06001199 if (rc)
Nathan Fontenoted651a12017-05-03 14:04:38 -04001200 return rc;
Nathan Fontenoted651a12017-05-03 14:04:38 -04001201
1202 rc = init_resources(adapter);
1203 if (rc) {
1204 netdev_err(netdev, "failed to initialize resources\n");
1205 release_resources(adapter);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001206 return rc;
1207 }
1208 }
1209
1210 rc = __ibmvnic_open(netdev);
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001211
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001212 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001213}
1214
Thomas Falcond0869c02018-02-13 18:23:43 -06001215static void clean_rx_pools(struct ibmvnic_adapter *adapter)
1216{
1217 struct ibmvnic_rx_pool *rx_pool;
Thomas Falcon637f81d2018-02-26 18:10:57 -06001218 struct ibmvnic_rx_buff *rx_buff;
Thomas Falcond0869c02018-02-13 18:23:43 -06001219 u64 rx_entries;
1220 int rx_scrqs;
1221 int i, j;
1222
1223 if (!adapter->rx_pool)
1224 return;
1225
Thomas Falcon660e3092018-04-20 14:25:32 -05001226 rx_scrqs = adapter->num_active_rx_pools;
Thomas Falcond0869c02018-02-13 18:23:43 -06001227 rx_entries = adapter->req_rx_add_entries_per_subcrq;
1228
1229 /* Free any remaining skbs in the rx buffer pools */
1230 for (i = 0; i < rx_scrqs; i++) {
1231 rx_pool = &adapter->rx_pool[i];
Thomas Falcon637f81d2018-02-26 18:10:57 -06001232 if (!rx_pool || !rx_pool->rx_buff)
Thomas Falcond0869c02018-02-13 18:23:43 -06001233 continue;
1234
1235 netdev_dbg(adapter->netdev, "Cleaning rx_pool[%d]\n", i);
1236 for (j = 0; j < rx_entries; j++) {
Thomas Falcon637f81d2018-02-26 18:10:57 -06001237 rx_buff = &rx_pool->rx_buff[j];
1238 if (rx_buff && rx_buff->skb) {
1239 dev_kfree_skb_any(rx_buff->skb);
1240 rx_buff->skb = NULL;
Thomas Falcond0869c02018-02-13 18:23:43 -06001241 }
1242 }
1243 }
1244}
1245
Thomas Falcone9e1e972018-03-16 20:00:30 -05001246static void clean_one_tx_pool(struct ibmvnic_adapter *adapter,
1247 struct ibmvnic_tx_pool *tx_pool)
Nathan Fontenotb41b83e2017-05-03 14:04:56 -04001248{
Thomas Falcon637f81d2018-02-26 18:10:57 -06001249 struct ibmvnic_tx_buff *tx_buff;
Nathan Fontenotb41b83e2017-05-03 14:04:56 -04001250 u64 tx_entries;
Thomas Falcone9e1e972018-03-16 20:00:30 -05001251 int i;
Nathan Fontenotb41b83e2017-05-03 14:04:56 -04001252
Dan Carpenter050e85c2018-03-23 14:36:15 +03001253 if (!tx_pool || !tx_pool->tx_buff)
Thomas Falcone9e1e972018-03-16 20:00:30 -05001254 return;
1255
1256 tx_entries = tx_pool->num_buffers;
1257
1258 for (i = 0; i < tx_entries; i++) {
1259 tx_buff = &tx_pool->tx_buff[i];
1260 if (tx_buff && tx_buff->skb) {
1261 dev_kfree_skb_any(tx_buff->skb);
1262 tx_buff->skb = NULL;
1263 }
1264 }
1265}
1266
1267static void clean_tx_pools(struct ibmvnic_adapter *adapter)
1268{
1269 int tx_scrqs;
1270 int i;
1271
1272 if (!adapter->tx_pool || !adapter->tso_pool)
Nathan Fontenotb41b83e2017-05-03 14:04:56 -04001273 return;
1274
Thomas Falcon660e3092018-04-20 14:25:32 -05001275 tx_scrqs = adapter->num_active_tx_pools;
Nathan Fontenotb41b83e2017-05-03 14:04:56 -04001276
1277 /* Free any remaining skbs in the tx buffer pools */
1278 for (i = 0; i < tx_scrqs; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001279 netdev_dbg(adapter->netdev, "Cleaning tx_pool[%d]\n", i);
Thomas Falcone9e1e972018-03-16 20:00:30 -05001280 clean_one_tx_pool(adapter, &adapter->tx_pool[i]);
1281 clean_one_tx_pool(adapter, &adapter->tso_pool[i]);
Nathan Fontenotb41b83e2017-05-03 14:04:56 -04001282 }
1283}
1284
John Allen6095e592018-03-30 13:44:21 -05001285static void ibmvnic_disable_irqs(struct ibmvnic_adapter *adapter)
John Allenea5509f2017-03-17 17:13:43 -05001286{
John Allen6095e592018-03-30 13:44:21 -05001287 struct net_device *netdev = adapter->netdev;
John Allenea5509f2017-03-17 17:13:43 -05001288 int i;
1289
Nathan Fontenot46293b92017-05-03 14:05:02 -04001290 if (adapter->tx_scrq) {
1291 for (i = 0; i < adapter->req_tx_queues; i++)
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001292 if (adapter->tx_scrq[i]->irq) {
Thomas Falconf8738662018-03-07 17:51:45 -06001293 netdev_dbg(netdev,
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001294 "Disabling tx_scrq[%d] irq\n", i);
Thomas Falconf23e0642018-04-15 18:53:36 -05001295 disable_scrq_irq(adapter, adapter->tx_scrq[i]);
Nathan Fontenot46293b92017-05-03 14:05:02 -04001296 disable_irq(adapter->tx_scrq[i]->irq);
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001297 }
Nathan Fontenot46293b92017-05-03 14:05:02 -04001298 }
1299
Nathan Fontenot46293b92017-05-03 14:05:02 -04001300 if (adapter->rx_scrq) {
1301 for (i = 0; i < adapter->req_rx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001302 if (adapter->rx_scrq[i]->irq) {
Thomas Falconf8738662018-03-07 17:51:45 -06001303 netdev_dbg(netdev,
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001304 "Disabling rx_scrq[%d] irq\n", i);
Thomas Falconf23e0642018-04-15 18:53:36 -05001305 disable_scrq_irq(adapter, adapter->rx_scrq[i]);
Nathan Fontenot46293b92017-05-03 14:05:02 -04001306 disable_irq(adapter->rx_scrq[i]->irq);
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001307 }
Nathan Fontenot46293b92017-05-03 14:05:02 -04001308 }
1309 }
John Allen6095e592018-03-30 13:44:21 -05001310}
1311
1312static void ibmvnic_cleanup(struct net_device *netdev)
1313{
1314 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1315
1316 /* ensure that transmissions are stopped if called by do_reset */
Juliet Kim7ed5b312019-09-20 16:11:23 -04001317 if (test_bit(0, &adapter->resetting))
John Allen6095e592018-03-30 13:44:21 -05001318 netif_tx_disable(netdev);
1319 else
1320 netif_tx_stop_all_queues(netdev);
1321
1322 ibmvnic_napi_disable(adapter);
1323 ibmvnic_disable_irqs(adapter);
1324
Thomas Falcond0869c02018-02-13 18:23:43 -06001325 clean_rx_pools(adapter);
Thomas Falcon10f76212017-05-26 10:30:31 -04001326 clean_tx_pools(adapter);
Thomas Falcon01d9bd72018-03-07 17:51:46 -06001327}
1328
1329static int __ibmvnic_close(struct net_device *netdev)
1330{
1331 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1332 int rc = 0;
1333
1334 adapter->state = VNIC_CLOSING;
1335 rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_DN);
1336 if (rc)
1337 return rc;
Nathan Fontenot90c80142017-05-03 14:04:32 -04001338 adapter->state = VNIC_CLOSED;
Thomas Falcon01d9bd72018-03-07 17:51:46 -06001339 return 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001340}
1341
Nathan Fontenoted651a12017-05-03 14:04:38 -04001342static int ibmvnic_close(struct net_device *netdev)
1343{
1344 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1345 int rc;
1346
Thomas Falcon5a18e1e2018-04-06 18:37:05 -05001347 /* If device failover is pending, just set device state and return.
1348 * Device operation will be handled by reset routine.
1349 */
1350 if (adapter->failover_pending) {
1351 adapter->state = VNIC_CLOSED;
1352 return 0;
1353 }
1354
Nathan Fontenoted651a12017-05-03 14:04:38 -04001355 rc = __ibmvnic_close(netdev);
Nathan Fontenot30f79622018-04-06 18:37:06 -05001356 ibmvnic_cleanup(netdev);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001357
1358 return rc;
1359}
1360
Thomas Falconad7775d2016-04-01 17:20:34 -05001361/**
1362 * build_hdr_data - creates L2/L3/L4 header data buffer
1363 * @hdr_field - bitfield determining needed headers
1364 * @skb - socket buffer
1365 * @hdr_len - array of header lengths
1366 * @tot_len - total length of data
1367 *
1368 * Reads hdr_field to determine which headers are needed by firmware.
1369 * Builds a buffer containing these headers. Saves individual header
1370 * lengths and total buffer length to be used to build descriptors.
1371 */
1372static int build_hdr_data(u8 hdr_field, struct sk_buff *skb,
1373 int *hdr_len, u8 *hdr_data)
1374{
1375 int len = 0;
1376 u8 *hdr;
1377
Thomas Falconda75e3b2018-03-12 11:51:02 -05001378 if (skb_vlan_tagged(skb) && !skb_vlan_tag_present(skb))
1379 hdr_len[0] = sizeof(struct vlan_ethhdr);
1380 else
1381 hdr_len[0] = sizeof(struct ethhdr);
Thomas Falconad7775d2016-04-01 17:20:34 -05001382
1383 if (skb->protocol == htons(ETH_P_IP)) {
1384 hdr_len[1] = ip_hdr(skb)->ihl * 4;
1385 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
1386 hdr_len[2] = tcp_hdrlen(skb);
1387 else if (ip_hdr(skb)->protocol == IPPROTO_UDP)
1388 hdr_len[2] = sizeof(struct udphdr);
1389 } else if (skb->protocol == htons(ETH_P_IPV6)) {
1390 hdr_len[1] = sizeof(struct ipv6hdr);
1391 if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
1392 hdr_len[2] = tcp_hdrlen(skb);
1393 else if (ipv6_hdr(skb)->nexthdr == IPPROTO_UDP)
1394 hdr_len[2] = sizeof(struct udphdr);
Thomas Falcon4eb50ce2017-12-18 12:52:40 -06001395 } else if (skb->protocol == htons(ETH_P_ARP)) {
1396 hdr_len[1] = arp_hdr_len(skb->dev);
1397 hdr_len[2] = 0;
Thomas Falconad7775d2016-04-01 17:20:34 -05001398 }
1399
1400 memset(hdr_data, 0, 120);
1401 if ((hdr_field >> 6) & 1) {
1402 hdr = skb_mac_header(skb);
1403 memcpy(hdr_data, hdr, hdr_len[0]);
1404 len += hdr_len[0];
1405 }
1406
1407 if ((hdr_field >> 5) & 1) {
1408 hdr = skb_network_header(skb);
1409 memcpy(hdr_data + len, hdr, hdr_len[1]);
1410 len += hdr_len[1];
1411 }
1412
1413 if ((hdr_field >> 4) & 1) {
1414 hdr = skb_transport_header(skb);
1415 memcpy(hdr_data + len, hdr, hdr_len[2]);
1416 len += hdr_len[2];
1417 }
1418 return len;
1419}
1420
1421/**
1422 * create_hdr_descs - create header and header extension descriptors
1423 * @hdr_field - bitfield determining needed headers
1424 * @data - buffer containing header data
1425 * @len - length of data buffer
1426 * @hdr_len - array of individual header lengths
1427 * @scrq_arr - descriptor array
1428 *
1429 * Creates header and, if needed, header extension descriptors and
1430 * places them in a descriptor array, scrq_arr
1431 */
1432
Thomas Falcon2de09682017-10-16 10:02:11 -05001433static int create_hdr_descs(u8 hdr_field, u8 *hdr_data, int len, int *hdr_len,
1434 union sub_crq *scrq_arr)
Thomas Falconad7775d2016-04-01 17:20:34 -05001435{
1436 union sub_crq hdr_desc;
1437 int tmp_len = len;
Thomas Falcon2de09682017-10-16 10:02:11 -05001438 int num_descs = 0;
Thomas Falconad7775d2016-04-01 17:20:34 -05001439 u8 *data, *cur;
1440 int tmp;
1441
1442 while (tmp_len > 0) {
1443 cur = hdr_data + len - tmp_len;
1444
1445 memset(&hdr_desc, 0, sizeof(hdr_desc));
1446 if (cur != hdr_data) {
1447 data = hdr_desc.hdr_ext.data;
1448 tmp = tmp_len > 29 ? 29 : tmp_len;
1449 hdr_desc.hdr_ext.first = IBMVNIC_CRQ_CMD;
1450 hdr_desc.hdr_ext.type = IBMVNIC_HDR_EXT_DESC;
1451 hdr_desc.hdr_ext.len = tmp;
1452 } else {
1453 data = hdr_desc.hdr.data;
1454 tmp = tmp_len > 24 ? 24 : tmp_len;
1455 hdr_desc.hdr.first = IBMVNIC_CRQ_CMD;
1456 hdr_desc.hdr.type = IBMVNIC_HDR_DESC;
1457 hdr_desc.hdr.len = tmp;
1458 hdr_desc.hdr.l2_len = (u8)hdr_len[0];
1459 hdr_desc.hdr.l3_len = cpu_to_be16((u16)hdr_len[1]);
1460 hdr_desc.hdr.l4_len = (u8)hdr_len[2];
1461 hdr_desc.hdr.flag = hdr_field << 1;
1462 }
1463 memcpy(data, cur, tmp);
1464 tmp_len -= tmp;
1465 *scrq_arr = hdr_desc;
1466 scrq_arr++;
Thomas Falcon2de09682017-10-16 10:02:11 -05001467 num_descs++;
Thomas Falconad7775d2016-04-01 17:20:34 -05001468 }
Thomas Falcon2de09682017-10-16 10:02:11 -05001469
1470 return num_descs;
Thomas Falconad7775d2016-04-01 17:20:34 -05001471}
1472
1473/**
1474 * build_hdr_descs_arr - build a header descriptor array
1475 * @skb - socket buffer
1476 * @num_entries - number of descriptors to be sent
1477 * @subcrq - first TX descriptor
1478 * @hdr_field - bit field determining which headers will be sent
1479 *
1480 * This function will build a TX descriptor array with applicable
1481 * L2/L3/L4 packet header descriptors to be sent by send_subcrq_indirect.
1482 */
1483
1484static void build_hdr_descs_arr(struct ibmvnic_tx_buff *txbuff,
1485 int *num_entries, u8 hdr_field)
1486{
1487 int hdr_len[3] = {0, 0, 0};
Thomas Falcon2de09682017-10-16 10:02:11 -05001488 int tot_len;
Thomas Falconad7775d2016-04-01 17:20:34 -05001489 u8 *hdr_data = txbuff->hdr_data;
1490
1491 tot_len = build_hdr_data(hdr_field, txbuff->skb, hdr_len,
1492 txbuff->hdr_data);
Thomas Falcon2de09682017-10-16 10:02:11 -05001493 *num_entries += create_hdr_descs(hdr_field, hdr_data, tot_len, hdr_len,
Thomas Falconad7775d2016-04-01 17:20:34 -05001494 txbuff->indir_arr + 1);
1495}
1496
Thomas Falcon1f247a62018-03-12 11:51:04 -05001497static int ibmvnic_xmit_workarounds(struct sk_buff *skb,
1498 struct net_device *netdev)
1499{
1500 /* For some backing devices, mishandling of small packets
1501 * can result in a loss of connection or TX stall. Device
1502 * architects recommend that no packet should be smaller
1503 * than the minimum MTU value provided to the driver, so
1504 * pad any packets to that length
1505 */
1506 if (skb->len < netdev->min_mtu)
1507 return skb_put_padto(skb, netdev->min_mtu);
Thomas Falcon7083a452018-03-12 21:05:26 -05001508
1509 return 0;
Thomas Falcon1f247a62018-03-12 11:51:04 -05001510}
1511
YueHaibing94b2bb22018-09-18 14:35:47 +08001512static netdev_tx_t ibmvnic_xmit(struct sk_buff *skb, struct net_device *netdev)
Thomas Falcon032c5e82015-12-21 11:26:06 -06001513{
1514 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1515 int queue_num = skb_get_queue_mapping(skb);
Thomas Falconad7775d2016-04-01 17:20:34 -05001516 u8 *hdrs = (u8 *)&adapter->tx_rx_desc_req;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001517 struct device *dev = &adapter->vdev->dev;
1518 struct ibmvnic_tx_buff *tx_buff = NULL;
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001519 struct ibmvnic_sub_crq_queue *tx_scrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001520 struct ibmvnic_tx_pool *tx_pool;
1521 unsigned int tx_send_failed = 0;
1522 unsigned int tx_map_failed = 0;
1523 unsigned int tx_dropped = 0;
1524 unsigned int tx_packets = 0;
1525 unsigned int tx_bytes = 0;
1526 dma_addr_t data_dma_addr;
1527 struct netdev_queue *txq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001528 unsigned long lpar_rc;
1529 union sub_crq tx_crq;
1530 unsigned int offset;
Thomas Falconad7775d2016-04-01 17:20:34 -05001531 int num_entries = 1;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001532 unsigned char *dst;
1533 u64 *handle_array;
1534 int index = 0;
Thomas Falcona0dca102018-01-18 19:29:48 -06001535 u8 proto = 0;
YueHaibing94b2bb22018-09-18 14:35:47 +08001536 netdev_tx_t ret = NETDEV_TX_OK;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001537
Juliet Kim7ed5b312019-09-20 16:11:23 -04001538 if (test_bit(0, &adapter->resetting)) {
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001539 if (!netif_subqueue_stopped(netdev, skb))
1540 netif_stop_subqueue(netdev, queue_num);
1541 dev_kfree_skb_any(skb);
1542
Thomas Falcon032c5e82015-12-21 11:26:06 -06001543 tx_send_failed++;
1544 tx_dropped++;
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001545 ret = NETDEV_TX_OK;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001546 goto out;
1547 }
1548
Thomas Falcon7083a452018-03-12 21:05:26 -05001549 if (ibmvnic_xmit_workarounds(skb, netdev)) {
Thomas Falcon1f247a62018-03-12 11:51:04 -05001550 tx_dropped++;
1551 tx_send_failed++;
1552 ret = NETDEV_TX_OK;
1553 goto out;
1554 }
Thomas Falcon06b3e352018-03-16 20:00:28 -05001555 if (skb_is_gso(skb))
1556 tx_pool = &adapter->tso_pool[queue_num];
1557 else
1558 tx_pool = &adapter->tx_pool[queue_num];
Thomas Falcon1f247a62018-03-12 11:51:04 -05001559
Nathan Fontenot161b8a82017-05-03 14:05:08 -04001560 tx_scrq = adapter->tx_scrq[queue_num];
1561 txq = netdev_get_tx_queue(netdev, skb_get_queue_mapping(skb));
1562 handle_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
1563 be32_to_cpu(adapter->login_rsp_buf->off_txsubm_subcrqs));
1564
Thomas Falcon032c5e82015-12-21 11:26:06 -06001565 index = tx_pool->free_map[tx_pool->consumer_index];
Thomas Falconfdb06102017-10-17 12:36:55 -05001566
Thomas Falcon86b61a52018-03-16 20:00:29 -05001567 if (index == IBMVNIC_INVALID_MAP) {
1568 dev_kfree_skb_any(skb);
1569 tx_send_failed++;
1570 tx_dropped++;
1571 ret = NETDEV_TX_OK;
1572 goto out;
1573 }
1574
1575 tx_pool->free_map[tx_pool->consumer_index] = IBMVNIC_INVALID_MAP;
1576
Thomas Falcon06b3e352018-03-16 20:00:28 -05001577 offset = index * tx_pool->buf_size;
1578 dst = tx_pool->long_term_buff.buff + offset;
1579 memset(dst, 0, tx_pool->buf_size);
1580 data_dma_addr = tx_pool->long_term_buff.addr + offset;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001581
Thomas Falcon15482052017-10-17 12:36:54 -05001582 if (skb_shinfo(skb)->nr_frags) {
1583 int cur, i;
1584
1585 /* Copy the head */
1586 skb_copy_from_linear_data(skb, dst, skb_headlen(skb));
1587 cur = skb_headlen(skb);
1588
1589 /* Copy the frags */
1590 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1591 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1592
1593 memcpy(dst + cur,
1594 page_address(skb_frag_page(frag)) +
Jonathan Lemonb54c9d52019-07-30 07:40:33 -07001595 skb_frag_off(frag), skb_frag_size(frag));
Thomas Falcon15482052017-10-17 12:36:54 -05001596 cur += skb_frag_size(frag);
1597 }
1598 } else {
1599 skb_copy_from_linear_data(skb, dst, skb->len);
1600 }
1601
Thomas Falcon032c5e82015-12-21 11:26:06 -06001602 tx_pool->consumer_index =
Thomas Falcon06b3e352018-03-16 20:00:28 -05001603 (tx_pool->consumer_index + 1) % tx_pool->num_buffers;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001604
1605 tx_buff = &tx_pool->tx_buff[index];
1606 tx_buff->skb = skb;
1607 tx_buff->data_dma[0] = data_dma_addr;
1608 tx_buff->data_len[0] = skb->len;
1609 tx_buff->index = index;
1610 tx_buff->pool_index = queue_num;
1611 tx_buff->last_frag = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001612
1613 memset(&tx_crq, 0, sizeof(tx_crq));
1614 tx_crq.v1.first = IBMVNIC_CRQ_CMD;
1615 tx_crq.v1.type = IBMVNIC_TX_DESC;
1616 tx_crq.v1.n_crq_elem = 1;
1617 tx_crq.v1.n_sge = 1;
1618 tx_crq.v1.flags1 = IBMVNIC_TX_COMP_NEEDED;
Thomas Falcon06b3e352018-03-16 20:00:28 -05001619
Thomas Falconfdb06102017-10-17 12:36:55 -05001620 if (skb_is_gso(skb))
Thomas Falcon06b3e352018-03-16 20:00:28 -05001621 tx_crq.v1.correlator =
1622 cpu_to_be32(index | IBMVNIC_TSO_POOL_MASK);
Thomas Falconfdb06102017-10-17 12:36:55 -05001623 else
Thomas Falcon06b3e352018-03-16 20:00:28 -05001624 tx_crq.v1.correlator = cpu_to_be32(index);
1625 tx_crq.v1.dma_reg = cpu_to_be16(tx_pool->long_term_buff.map_id);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001626 tx_crq.v1.sge_len = cpu_to_be32(skb->len);
1627 tx_crq.v1.ioba = cpu_to_be64(data_dma_addr);
1628
Michał Mirosławe84b4792018-11-07 17:50:52 +01001629 if (adapter->vlan_header_insertion && skb_vlan_tag_present(skb)) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06001630 tx_crq.v1.flags2 |= IBMVNIC_TX_VLAN_INSERT;
1631 tx_crq.v1.vlan_id = cpu_to_be16(skb->vlan_tci);
1632 }
1633
1634 if (skb->protocol == htons(ETH_P_IP)) {
Thomas Falcona0dca102018-01-18 19:29:48 -06001635 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV4;
1636 proto = ip_hdr(skb)->protocol;
1637 } else if (skb->protocol == htons(ETH_P_IPV6)) {
1638 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV6;
1639 proto = ipv6_hdr(skb)->nexthdr;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001640 }
1641
Thomas Falcona0dca102018-01-18 19:29:48 -06001642 if (proto == IPPROTO_TCP)
1643 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_TCP;
1644 else if (proto == IPPROTO_UDP)
1645 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_UDP;
1646
Thomas Falconad7775d2016-04-01 17:20:34 -05001647 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06001648 tx_crq.v1.flags1 |= IBMVNIC_TX_CHKSUM_OFFLOAD;
Thomas Falconad7775d2016-04-01 17:20:34 -05001649 hdrs += 2;
1650 }
Thomas Falconfdb06102017-10-17 12:36:55 -05001651 if (skb_is_gso(skb)) {
1652 tx_crq.v1.flags1 |= IBMVNIC_TX_LSO;
1653 tx_crq.v1.mss = cpu_to_be16(skb_shinfo(skb)->gso_size);
1654 hdrs += 2;
1655 }
Thomas Falconad7775d2016-04-01 17:20:34 -05001656 /* determine if l2/3/4 headers are sent to firmware */
John Allen2fa56a42018-02-09 13:19:46 -06001657 if ((*hdrs >> 7) & 1) {
Thomas Falconad7775d2016-04-01 17:20:34 -05001658 build_hdr_descs_arr(tx_buff, &num_entries, *hdrs);
1659 tx_crq.v1.n_crq_elem = num_entries;
Thomas Falconecba6162018-02-26 18:10:55 -06001660 tx_buff->num_entries = num_entries;
Thomas Falconad7775d2016-04-01 17:20:34 -05001661 tx_buff->indir_arr[0] = tx_crq;
1662 tx_buff->indir_dma = dma_map_single(dev, tx_buff->indir_arr,
1663 sizeof(tx_buff->indir_arr),
1664 DMA_TO_DEVICE);
1665 if (dma_mapping_error(dev, tx_buff->indir_dma)) {
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001666 dev_kfree_skb_any(skb);
1667 tx_buff->skb = NULL;
Thomas Falconad7775d2016-04-01 17:20:34 -05001668 if (!firmware_has_feature(FW_FEATURE_CMO))
1669 dev_err(dev, "tx: unable to map descriptor array\n");
1670 tx_map_failed++;
1671 tx_dropped++;
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001672 ret = NETDEV_TX_OK;
Thomas Falcon86b61a52018-03-16 20:00:29 -05001673 goto tx_err_out;
Thomas Falconad7775d2016-04-01 17:20:34 -05001674 }
John Allen498cd8e2016-04-06 11:49:55 -05001675 lpar_rc = send_subcrq_indirect(adapter, handle_array[queue_num],
Thomas Falconad7775d2016-04-01 17:20:34 -05001676 (u64)tx_buff->indir_dma,
1677 (u64)num_entries);
Thomas Falcon80f0fe02019-08-14 14:57:05 -05001678 dma_unmap_single(dev, tx_buff->indir_dma,
1679 sizeof(tx_buff->indir_arr), DMA_TO_DEVICE);
Thomas Falconad7775d2016-04-01 17:20:34 -05001680 } else {
Thomas Falconecba6162018-02-26 18:10:55 -06001681 tx_buff->num_entries = num_entries;
John Allen498cd8e2016-04-06 11:49:55 -05001682 lpar_rc = send_subcrq(adapter, handle_array[queue_num],
1683 &tx_crq);
Thomas Falconad7775d2016-04-01 17:20:34 -05001684 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001685 if (lpar_rc != H_SUCCESS) {
Thomas Falcon2d14d372018-07-13 12:03:32 -05001686 if (lpar_rc != H_CLOSED && lpar_rc != H_PARAMETER)
1687 dev_err_ratelimited(dev, "tx: send failed\n");
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001688 dev_kfree_skb_any(skb);
1689 tx_buff->skb = NULL;
1690
Thomas Falcon5a18e1e2018-04-06 18:37:05 -05001691 if (lpar_rc == H_CLOSED || adapter->failover_pending) {
1692 /* Disable TX and report carrier off if queue is closed
1693 * or pending failover.
Thomas Falconb8c80b82017-05-26 10:30:42 -04001694 * Firmware guarantees that a signal will be sent to the
1695 * driver, triggering a reset or some other action.
1696 */
1697 netif_tx_stop_all_queues(netdev);
1698 netif_carrier_off(netdev);
1699 }
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001700
Thomas Falcon032c5e82015-12-21 11:26:06 -06001701 tx_send_failed++;
1702 tx_dropped++;
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001703 ret = NETDEV_TX_OK;
Thomas Falcon86b61a52018-03-16 20:00:29 -05001704 goto tx_err_out;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001705 }
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001706
Thomas Falconffc385b2018-02-18 10:08:41 -06001707 if (atomic_add_return(num_entries, &tx_scrq->used)
Brian King58c8c0c2017-04-19 13:44:47 -04001708 >= adapter->req_tx_entries_per_subcrq) {
Thomas Falcon0aecb132018-02-26 18:10:58 -06001709 netdev_dbg(netdev, "Stopping queue %d\n", queue_num);
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001710 netif_stop_subqueue(netdev, queue_num);
1711 }
1712
Thomas Falcon032c5e82015-12-21 11:26:06 -06001713 tx_packets++;
1714 tx_bytes += skb->len;
1715 txq->trans_start = jiffies;
1716 ret = NETDEV_TX_OK;
Thomas Falcon86b61a52018-03-16 20:00:29 -05001717 goto out;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001718
Thomas Falcon86b61a52018-03-16 20:00:29 -05001719tx_err_out:
1720 /* roll back consumer index and map array*/
1721 if (tx_pool->consumer_index == 0)
1722 tx_pool->consumer_index =
1723 tx_pool->num_buffers - 1;
1724 else
1725 tx_pool->consumer_index--;
1726 tx_pool->free_map[tx_pool->consumer_index] = index;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001727out:
1728 netdev->stats.tx_dropped += tx_dropped;
1729 netdev->stats.tx_bytes += tx_bytes;
1730 netdev->stats.tx_packets += tx_packets;
1731 adapter->tx_send_failed += tx_send_failed;
1732 adapter->tx_map_failed += tx_map_failed;
John Allen3d52b592017-08-02 16:44:14 -05001733 adapter->tx_stats_buffers[queue_num].packets += tx_packets;
1734 adapter->tx_stats_buffers[queue_num].bytes += tx_bytes;
1735 adapter->tx_stats_buffers[queue_num].dropped_packets += tx_dropped;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001736
1737 return ret;
1738}
1739
1740static void ibmvnic_set_multi(struct net_device *netdev)
1741{
1742 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1743 struct netdev_hw_addr *ha;
1744 union ibmvnic_crq crq;
1745
1746 memset(&crq, 0, sizeof(crq));
1747 crq.request_capability.first = IBMVNIC_CRQ_CMD;
1748 crq.request_capability.cmd = REQUEST_CAPABILITY;
1749
1750 if (netdev->flags & IFF_PROMISC) {
1751 if (!adapter->promisc_supported)
1752 return;
1753 } else {
1754 if (netdev->flags & IFF_ALLMULTI) {
1755 /* Accept all multicast */
1756 memset(&crq, 0, sizeof(crq));
1757 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1758 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1759 crq.multicast_ctrl.flags = IBMVNIC_ENABLE_ALL;
1760 ibmvnic_send_crq(adapter, &crq);
1761 } else if (netdev_mc_empty(netdev)) {
1762 /* Reject all multicast */
1763 memset(&crq, 0, sizeof(crq));
1764 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1765 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1766 crq.multicast_ctrl.flags = IBMVNIC_DISABLE_ALL;
1767 ibmvnic_send_crq(adapter, &crq);
1768 } else {
1769 /* Accept one or more multicast(s) */
1770 netdev_for_each_mc_addr(ha, netdev) {
1771 memset(&crq, 0, sizeof(crq));
1772 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1773 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1774 crq.multicast_ctrl.flags = IBMVNIC_ENABLE_MC;
1775 ether_addr_copy(&crq.multicast_ctrl.mac_addr[0],
1776 ha->addr);
1777 ibmvnic_send_crq(adapter, &crq);
1778 }
1779 }
1780 }
1781}
1782
Thomas Falcon62740e92019-05-09 23:13:43 -05001783static int __ibmvnic_set_mac(struct net_device *netdev, u8 *dev_addr)
Thomas Falcon032c5e82015-12-21 11:26:06 -06001784{
1785 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001786 union ibmvnic_crq crq;
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05001787 int rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001788
Thomas Falcon62740e92019-05-09 23:13:43 -05001789 if (!is_valid_ether_addr(dev_addr)) {
1790 rc = -EADDRNOTAVAIL;
1791 goto err;
1792 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001793
1794 memset(&crq, 0, sizeof(crq));
1795 crq.change_mac_addr.first = IBMVNIC_CRQ_CMD;
1796 crq.change_mac_addr.cmd = CHANGE_MAC_ADDR;
Thomas Falcon62740e92019-05-09 23:13:43 -05001797 ether_addr_copy(&crq.change_mac_addr.mac_addr[0], dev_addr);
Thomas Falconf8136142018-01-29 13:45:05 -06001798
Thomas Falconff25dcb2019-11-25 17:12:56 -06001799 mutex_lock(&adapter->fw_lock);
1800 adapter->fw_done_rc = 0;
Thomas Falcon070eca92019-11-25 17:12:53 -06001801 reinit_completion(&adapter->fw_done);
Thomas Falconff25dcb2019-11-25 17:12:56 -06001802
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05001803 rc = ibmvnic_send_crq(adapter, &crq);
Thomas Falcon62740e92019-05-09 23:13:43 -05001804 if (rc) {
1805 rc = -EIO;
Thomas Falconff25dcb2019-11-25 17:12:56 -06001806 mutex_unlock(&adapter->fw_lock);
Thomas Falcon62740e92019-05-09 23:13:43 -05001807 goto err;
1808 }
1809
Thomas Falcon476d96c2019-11-25 17:12:55 -06001810 rc = ibmvnic_wait_for_completion(adapter, &adapter->fw_done, 10000);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001811 /* netdev->dev_addr is changed in handle_change_mac_rsp function */
Thomas Falcon476d96c2019-11-25 17:12:55 -06001812 if (rc || adapter->fw_done_rc) {
Thomas Falcon62740e92019-05-09 23:13:43 -05001813 rc = -EIO;
Thomas Falconff25dcb2019-11-25 17:12:56 -06001814 mutex_unlock(&adapter->fw_lock);
Thomas Falcon62740e92019-05-09 23:13:43 -05001815 goto err;
1816 }
Thomas Falconff25dcb2019-11-25 17:12:56 -06001817 mutex_unlock(&adapter->fw_lock);
Thomas Falcon62740e92019-05-09 23:13:43 -05001818 return 0;
1819err:
1820 ether_addr_copy(adapter->mac_addr, netdev->dev_addr);
1821 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001822}
1823
John Allenc26eba02017-10-26 16:23:25 -05001824static int ibmvnic_set_mac(struct net_device *netdev, void *p)
1825{
1826 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1827 struct sockaddr *addr = p;
Thomas Falconf8136142018-01-29 13:45:05 -06001828 int rc;
John Allenc26eba02017-10-26 16:23:25 -05001829
Thomas Falcon62740e92019-05-09 23:13:43 -05001830 rc = 0;
1831 ether_addr_copy(adapter->mac_addr, addr->sa_data);
1832 if (adapter->state != VNIC_PROBED)
1833 rc = __ibmvnic_set_mac(netdev, addr->sa_data);
John Allenc26eba02017-10-26 16:23:25 -05001834
Thomas Falconf8136142018-01-29 13:45:05 -06001835 return rc;
John Allenc26eba02017-10-26 16:23:25 -05001836}
1837
Nathan Fontenoted651a12017-05-03 14:04:38 -04001838/**
Juliet Kimb27507b2019-09-20 16:11:22 -04001839 * do_change_param_reset returns zero if we are able to keep processing reset
1840 * events, or non-zero if we hit a fatal error and must halt.
1841 */
1842static int do_change_param_reset(struct ibmvnic_adapter *adapter,
1843 struct ibmvnic_rwi *rwi,
1844 u32 reset_state)
1845{
1846 struct net_device *netdev = adapter->netdev;
1847 int i, rc;
1848
1849 netdev_dbg(adapter->netdev, "Change param resetting driver (%d)\n",
1850 rwi->reset_reason);
1851
1852 netif_carrier_off(netdev);
1853 adapter->reset_reason = rwi->reset_reason;
1854
1855 ibmvnic_cleanup(netdev);
1856
1857 if (reset_state == VNIC_OPEN) {
1858 rc = __ibmvnic_close(netdev);
1859 if (rc)
1860 return rc;
1861 }
1862
1863 release_resources(adapter);
1864 release_sub_crqs(adapter, 1);
1865 release_crq_queue(adapter);
1866
1867 adapter->state = VNIC_PROBED;
1868
1869 rc = init_crq_queue(adapter);
1870
1871 if (rc) {
1872 netdev_err(adapter->netdev,
1873 "Couldn't initialize crq. rc=%d\n", rc);
1874 return rc;
1875 }
1876
1877 rc = ibmvnic_reset_init(adapter);
1878 if (rc)
1879 return IBMVNIC_INIT_FAILED;
1880
1881 /* If the adapter was in PROBE state prior to the reset,
1882 * exit here.
1883 */
1884 if (reset_state == VNIC_PROBED)
1885 return 0;
1886
1887 rc = ibmvnic_login(netdev);
1888 if (rc) {
1889 adapter->state = reset_state;
1890 return rc;
1891 }
1892
1893 rc = init_resources(adapter);
1894 if (rc)
1895 return rc;
1896
1897 ibmvnic_disable_irqs(adapter);
1898
1899 adapter->state = VNIC_CLOSED;
1900
1901 if (reset_state == VNIC_CLOSED)
1902 return 0;
1903
1904 rc = __ibmvnic_open(netdev);
1905 if (rc)
1906 return IBMVNIC_OPEN_FAILED;
1907
1908 /* refresh device's multicast list */
1909 ibmvnic_set_multi(netdev);
1910
1911 /* kick napi */
1912 for (i = 0; i < adapter->req_rx_queues; i++)
1913 napi_schedule(&adapter->napi[i]);
1914
1915 return 0;
1916}
1917
1918/**
Nathan Fontenoted651a12017-05-03 14:04:38 -04001919 * do_reset returns zero if we are able to keep processing reset events, or
1920 * non-zero if we hit a fatal error and must halt.
1921 */
1922static int do_reset(struct ibmvnic_adapter *adapter,
1923 struct ibmvnic_rwi *rwi, u32 reset_state)
1924{
John Allen896d8692018-01-18 16:26:31 -06001925 u64 old_num_rx_queues, old_num_tx_queues;
Thomas Falcon5bf032e2018-11-21 11:17:59 -06001926 u64 old_num_rx_slots, old_num_tx_slots;
Nathan Fontenoted651a12017-05-03 14:04:38 -04001927 struct net_device *netdev = adapter->netdev;
1928 int i, rc;
1929
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001930 netdev_dbg(adapter->netdev, "Re-setting driver (%d)\n",
1931 rwi->reset_reason);
1932
Juliet Kimb27507b2019-09-20 16:11:22 -04001933 rtnl_lock();
1934
Nathan Fontenoted651a12017-05-03 14:04:38 -04001935 netif_carrier_off(netdev);
1936 adapter->reset_reason = rwi->reset_reason;
1937
John Allen896d8692018-01-18 16:26:31 -06001938 old_num_rx_queues = adapter->req_rx_queues;
1939 old_num_tx_queues = adapter->req_tx_queues;
Thomas Falcon5bf032e2018-11-21 11:17:59 -06001940 old_num_rx_slots = adapter->req_rx_add_entries_per_subcrq;
1941 old_num_tx_slots = adapter->req_tx_entries_per_subcrq;
John Allen896d8692018-01-18 16:26:31 -06001942
Nathan Fontenot30f79622018-04-06 18:37:06 -05001943 ibmvnic_cleanup(netdev);
1944
Thomas Falcon1f946082019-06-07 16:03:53 -05001945 if (reset_state == VNIC_OPEN &&
1946 adapter->reset_reason != VNIC_RESET_MOBILITY &&
Nathan Fontenot30f79622018-04-06 18:37:06 -05001947 adapter->reset_reason != VNIC_RESET_FAILOVER) {
Juliet Kimb27507b2019-09-20 16:11:22 -04001948 adapter->state = VNIC_CLOSING;
Nathan Fontenoted651a12017-05-03 14:04:38 -04001949
Juliet Kimb27507b2019-09-20 16:11:22 -04001950 /* Release the RTNL lock before link state change and
1951 * re-acquire after the link state change to allow
1952 * linkwatch_event to grab the RTNL lock and run during
1953 * a reset.
1954 */
1955 rtnl_unlock();
1956 rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_DN);
1957 rtnl_lock();
1958 if (rc)
1959 goto out;
1960
1961 if (adapter->state != VNIC_CLOSING) {
1962 rc = -1;
1963 goto out;
1964 }
1965
1966 adapter->state = VNIC_CLOSED;
John Allenc26eba02017-10-26 16:23:25 -05001967 }
1968
John Allen8cb31cf2017-05-26 10:30:37 -04001969 if (adapter->reset_reason != VNIC_RESET_NON_FATAL) {
1970 /* remove the closed state so when we call open it appears
1971 * we are coming from the probed state.
1972 */
Nathan Fontenoted651a12017-05-03 14:04:38 -04001973 adapter->state = VNIC_PROBED;
John Allen8cb31cf2017-05-26 10:30:37 -04001974
Juliet Kimb27507b2019-09-20 16:11:22 -04001975 if (adapter->reset_reason == VNIC_RESET_MOBILITY) {
Nathan Fontenot30f79622018-04-06 18:37:06 -05001976 rc = ibmvnic_reenable_crq_queue(adapter);
1977 release_sub_crqs(adapter, 1);
1978 } else {
1979 rc = ibmvnic_reset_crq(adapter);
Dany Madden8b40eb732020-06-18 15:24:13 -04001980 if (rc == H_CLOSED || rc == H_SUCCESS) {
Nathan Fontenot30f79622018-04-06 18:37:06 -05001981 rc = vio_enable_interrupts(adapter->vdev);
Dany Madden8b40eb732020-06-18 15:24:13 -04001982 if (rc)
1983 netdev_err(adapter->netdev,
1984 "Reset failed to enable interrupts. rc=%d\n",
1985 rc);
1986 }
Nathan Fontenot30f79622018-04-06 18:37:06 -05001987 }
1988
1989 if (rc) {
1990 netdev_err(adapter->netdev,
Dany Madden8b40eb732020-06-18 15:24:13 -04001991 "Reset couldn't initialize crq. rc=%d\n", rc);
Juliet Kimb27507b2019-09-20 16:11:22 -04001992 goto out;
Nathan Fontenot30f79622018-04-06 18:37:06 -05001993 }
1994
Thomas Falcon8a348452018-05-23 13:38:00 -05001995 rc = ibmvnic_reset_init(adapter);
Juliet Kimb27507b2019-09-20 16:11:22 -04001996 if (rc) {
1997 rc = IBMVNIC_INIT_FAILED;
1998 goto out;
1999 }
John Allen8cb31cf2017-05-26 10:30:37 -04002000
2001 /* If the adapter was in PROBE state prior to the reset,
2002 * exit here.
2003 */
Juliet Kimb27507b2019-09-20 16:11:22 -04002004 if (reset_state == VNIC_PROBED) {
2005 rc = 0;
2006 goto out;
2007 }
John Allen8cb31cf2017-05-26 10:30:37 -04002008
2009 rc = ibmvnic_login(netdev);
2010 if (rc) {
John Allen3578a7e2018-07-16 10:29:30 -05002011 adapter->state = reset_state;
Juliet Kimb27507b2019-09-20 16:11:22 -04002012 goto out;
John Allen8cb31cf2017-05-26 10:30:37 -04002013 }
2014
Juliet Kimb27507b2019-09-20 16:11:22 -04002015 if (adapter->req_rx_queues != old_num_rx_queues ||
2016 adapter->req_tx_queues != old_num_tx_queues ||
2017 adapter->req_rx_add_entries_per_subcrq !=
2018 old_num_rx_slots ||
2019 adapter->req_tx_entries_per_subcrq !=
Mingming Cao9f134572020-08-25 13:26:41 -04002020 old_num_tx_slots ||
2021 !adapter->rx_pool ||
2022 !adapter->tso_pool ||
2023 !adapter->tx_pool) {
John Allen896d8692018-01-18 16:26:31 -06002024 release_rx_pools(adapter);
2025 release_tx_pools(adapter);
Juliet Kima5681e22018-11-19 15:59:22 -06002026 release_napi(adapter);
2027 release_vpd_data(adapter);
2028
2029 rc = init_resources(adapter);
Thomas Falconf611a5b2018-08-30 13:19:53 -05002030 if (rc)
Juliet Kimb27507b2019-09-20 16:11:22 -04002031 goto out;
Nathan Fontenotd9043c12018-02-19 13:30:14 -06002032
John Allenc26eba02017-10-26 16:23:25 -05002033 } else {
2034 rc = reset_tx_pools(adapter);
2035 if (rc)
Mingming Cao9f134572020-08-25 13:26:41 -04002036 netdev_dbg(adapter->netdev, "reset tx pools failed (%d)\n",
2037 rc);
Juliet Kimb27507b2019-09-20 16:11:22 -04002038 goto out;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -04002039
John Allenc26eba02017-10-26 16:23:25 -05002040 rc = reset_rx_pools(adapter);
2041 if (rc)
Mingming Cao9f134572020-08-25 13:26:41 -04002042 netdev_dbg(adapter->netdev, "reset rx pools failed (%d)\n",
2043 rc);
Juliet Kimb27507b2019-09-20 16:11:22 -04002044 goto out;
John Allenc26eba02017-10-26 16:23:25 -05002045 }
Thomas Falcon134bbe72018-05-16 15:49:04 -05002046 ibmvnic_disable_irqs(adapter);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002047 }
John Allene676d812018-03-14 10:41:29 -05002048 adapter->state = VNIC_CLOSED;
2049
Juliet Kimb27507b2019-09-20 16:11:22 -04002050 if (reset_state == VNIC_CLOSED) {
2051 rc = 0;
2052 goto out;
2053 }
John Allene676d812018-03-14 10:41:29 -05002054
Nathan Fontenoted651a12017-05-03 14:04:38 -04002055 rc = __ibmvnic_open(netdev);
2056 if (rc) {
Juliet Kimb27507b2019-09-20 16:11:22 -04002057 rc = IBMVNIC_OPEN_FAILED;
2058 goto out;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002059 }
2060
Thomas Falconbe32a242019-06-07 16:03:54 -05002061 /* refresh device's multicast list */
2062 ibmvnic_set_multi(netdev);
2063
Nathan Fontenoted651a12017-05-03 14:04:38 -04002064 /* kick napi */
2065 for (i = 0; i < adapter->req_rx_queues; i++)
2066 napi_schedule(&adapter->napi[i]);
2067
Juliet Kimb27507b2019-09-20 16:11:22 -04002068 if (adapter->reset_reason != VNIC_RESET_FAILOVER)
Thomas Falcon986103e2018-11-30 10:59:08 -06002069 call_netdevice_notifiers(NETDEV_NOTIFY_PEERS, netdev);
Nathan Fontenot61d3e1d2017-06-12 20:47:45 -04002070
Juliet Kimb27507b2019-09-20 16:11:22 -04002071 rc = 0;
2072
2073out:
2074 rtnl_unlock();
2075
2076 return rc;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002077}
2078
Thomas Falcon2770a792018-05-23 13:38:02 -05002079static int do_hard_reset(struct ibmvnic_adapter *adapter,
2080 struct ibmvnic_rwi *rwi, u32 reset_state)
2081{
2082 struct net_device *netdev = adapter->netdev;
2083 int rc;
2084
2085 netdev_dbg(adapter->netdev, "Hard resetting driver (%d)\n",
2086 rwi->reset_reason);
2087
2088 netif_carrier_off(netdev);
2089 adapter->reset_reason = rwi->reset_reason;
2090
2091 ibmvnic_cleanup(netdev);
2092 release_resources(adapter);
2093 release_sub_crqs(adapter, 0);
2094 release_crq_queue(adapter);
2095
2096 /* remove the closed state so when we call open it appears
2097 * we are coming from the probed state.
2098 */
2099 adapter->state = VNIC_PROBED;
2100
Thomas Falconbbd669a2019-04-04 18:58:26 -05002101 reinit_completion(&adapter->init_done);
Thomas Falcon2770a792018-05-23 13:38:02 -05002102 rc = init_crq_queue(adapter);
2103 if (rc) {
2104 netdev_err(adapter->netdev,
2105 "Couldn't initialize crq. rc=%d\n", rc);
2106 return rc;
2107 }
2108
2109 rc = ibmvnic_init(adapter);
2110 if (rc)
2111 return rc;
2112
2113 /* If the adapter was in PROBE state prior to the reset,
2114 * exit here.
2115 */
2116 if (reset_state == VNIC_PROBED)
2117 return 0;
2118
2119 rc = ibmvnic_login(netdev);
2120 if (rc) {
2121 adapter->state = VNIC_PROBED;
2122 return 0;
2123 }
Juliet Kima5681e22018-11-19 15:59:22 -06002124
2125 rc = init_resources(adapter);
Thomas Falcon2770a792018-05-23 13:38:02 -05002126 if (rc)
2127 return rc;
2128
2129 ibmvnic_disable_irqs(adapter);
2130 adapter->state = VNIC_CLOSED;
2131
2132 if (reset_state == VNIC_CLOSED)
2133 return 0;
2134
2135 rc = __ibmvnic_open(netdev);
Juliet Kimb27507b2019-09-20 16:11:22 -04002136 if (rc)
2137 return IBMVNIC_OPEN_FAILED;
Thomas Falcon2770a792018-05-23 13:38:02 -05002138
Thomas Falcon2770a792018-05-23 13:38:02 -05002139 return 0;
2140}
2141
Nathan Fontenoted651a12017-05-03 14:04:38 -04002142static struct ibmvnic_rwi *get_next_rwi(struct ibmvnic_adapter *adapter)
2143{
2144 struct ibmvnic_rwi *rwi;
Thomas Falcon6c5c7482018-12-10 15:22:22 -06002145 unsigned long flags;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002146
Thomas Falcon6c5c7482018-12-10 15:22:22 -06002147 spin_lock_irqsave(&adapter->rwi_lock, flags);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002148
2149 if (!list_empty(&adapter->rwi_list)) {
2150 rwi = list_first_entry(&adapter->rwi_list, struct ibmvnic_rwi,
2151 list);
2152 list_del(&rwi->list);
2153 } else {
2154 rwi = NULL;
2155 }
2156
Thomas Falcon6c5c7482018-12-10 15:22:22 -06002157 spin_unlock_irqrestore(&adapter->rwi_lock, flags);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002158 return rwi;
2159}
2160
2161static void free_all_rwi(struct ibmvnic_adapter *adapter)
2162{
2163 struct ibmvnic_rwi *rwi;
2164
2165 rwi = get_next_rwi(adapter);
2166 while (rwi) {
2167 kfree(rwi);
2168 rwi = get_next_rwi(adapter);
2169 }
2170}
2171
2172static void __ibmvnic_reset(struct work_struct *work)
2173{
2174 struct ibmvnic_rwi *rwi;
2175 struct ibmvnic_adapter *adapter;
Juliet Kim7d7195a2020-03-10 09:23:58 -05002176 bool saved_state = false;
2177 unsigned long flags;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002178 u32 reset_state;
John Allenc26eba02017-10-26 16:23:25 -05002179 int rc = 0;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002180
2181 adapter = container_of(work, struct ibmvnic_adapter, ibmvnic_reset);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002182
Juliet Kim7ed5b312019-09-20 16:11:23 -04002183 if (test_and_set_bit_lock(0, &adapter->resetting)) {
2184 schedule_delayed_work(&adapter->ibmvnic_delayed_reset,
2185 IBMVNIC_RESET_DELAY);
2186 return;
2187 }
2188
Nathan Fontenoted651a12017-05-03 14:04:38 -04002189 rwi = get_next_rwi(adapter);
2190 while (rwi) {
Juliet Kim7d7195a2020-03-10 09:23:58 -05002191 spin_lock_irqsave(&adapter->state_lock, flags);
2192
Thomas Falcon36f10312019-08-27 11:10:04 -05002193 if (adapter->state == VNIC_REMOVING ||
Michal Suchanekc8dc5592019-09-09 22:44:51 +02002194 adapter->state == VNIC_REMOVED) {
Juliet Kim7d7195a2020-03-10 09:23:58 -05002195 spin_unlock_irqrestore(&adapter->state_lock, flags);
Juliet Kim1c2977c2019-09-05 17:30:01 -04002196 kfree(rwi);
2197 rc = EBUSY;
2198 break;
2199 }
Thomas Falcon36f10312019-08-27 11:10:04 -05002200
Juliet Kim7d7195a2020-03-10 09:23:58 -05002201 if (!saved_state) {
2202 reset_state = adapter->state;
2203 adapter->state = VNIC_RESETTING;
2204 saved_state = true;
2205 }
2206 spin_unlock_irqrestore(&adapter->state_lock, flags);
2207
Juliet Kimb27507b2019-09-20 16:11:22 -04002208 if (rwi->reset_reason == VNIC_RESET_CHANGE_PARAM) {
2209 /* CHANGE_PARAM requestor holds rtnl_lock */
2210 rc = do_change_param_reset(adapter, rwi, reset_state);
2211 } else if (adapter->force_reset_recovery) {
2212 /* Transport event occurred during previous reset */
2213 if (adapter->wait_for_reset) {
2214 /* Previous was CHANGE_PARAM; caller locked */
2215 adapter->force_reset_recovery = false;
2216 rc = do_hard_reset(adapter, rwi, reset_state);
2217 } else {
2218 rtnl_lock();
2219 adapter->force_reset_recovery = false;
2220 rc = do_hard_reset(adapter, rwi, reset_state);
2221 rtnl_unlock();
2222 }
Juliet Kimf9c6cea2020-04-30 13:22:11 -05002223 } else if (!(rwi->reset_reason == VNIC_RESET_FATAL &&
2224 adapter->from_passive_init)) {
Thomas Falcon2770a792018-05-23 13:38:02 -05002225 rc = do_reset(adapter, rwi, reset_state);
2226 }
Nathan Fontenoted651a12017-05-03 14:04:38 -04002227 kfree(rwi);
Juliet Kimb27507b2019-09-20 16:11:22 -04002228 if (rc == IBMVNIC_OPEN_FAILED) {
2229 if (list_empty(&adapter->rwi_list))
2230 adapter->state = VNIC_CLOSED;
2231 else
2232 adapter->state = reset_state;
2233 rc = 0;
2234 } else if (rc && rc != IBMVNIC_INIT_FAILED &&
Thomas Falcon2770a792018-05-23 13:38:02 -05002235 !adapter->force_reset_recovery)
Nathan Fontenoted651a12017-05-03 14:04:38 -04002236 break;
2237
2238 rwi = get_next_rwi(adapter);
Juliet Kim7ed5b312019-09-20 16:11:23 -04002239
2240 if (rwi && (rwi->reset_reason == VNIC_RESET_FAILOVER ||
2241 rwi->reset_reason == VNIC_RESET_MOBILITY))
2242 adapter->force_reset_recovery = true;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002243 }
2244
John Allenc26eba02017-10-26 16:23:25 -05002245 if (adapter->wait_for_reset) {
John Allenc26eba02017-10-26 16:23:25 -05002246 adapter->reset_done_rc = rc;
2247 complete(&adapter->reset_done);
2248 }
2249
Nathan Fontenoted651a12017-05-03 14:04:38 -04002250 if (rc) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002251 netdev_dbg(adapter->netdev, "Reset failed\n");
Nathan Fontenoted651a12017-05-03 14:04:38 -04002252 free_all_rwi(adapter);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002253 }
Juliet Kim1c2977c2019-09-05 17:30:01 -04002254
Juliet Kim7ed5b312019-09-20 16:11:23 -04002255 clear_bit_unlock(0, &adapter->resetting);
2256}
2257
2258static void __ibmvnic_delayed_reset(struct work_struct *work)
2259{
2260 struct ibmvnic_adapter *adapter;
2261
2262 adapter = container_of(work, struct ibmvnic_adapter,
2263 ibmvnic_delayed_reset.work);
2264 __ibmvnic_reset(&adapter->ibmvnic_reset);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002265}
2266
Thomas Falconaf894d22018-04-06 18:37:04 -05002267static int ibmvnic_reset(struct ibmvnic_adapter *adapter,
2268 enum ibmvnic_reset_reason reason)
Nathan Fontenoted651a12017-05-03 14:04:38 -04002269{
Thomas Falcon2770a792018-05-23 13:38:02 -05002270 struct list_head *entry, *tmp_entry;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002271 struct ibmvnic_rwi *rwi, *tmp;
2272 struct net_device *netdev = adapter->netdev;
Thomas Falcon6c5c7482018-12-10 15:22:22 -06002273 unsigned long flags;
Thomas Falconaf894d22018-04-06 18:37:04 -05002274 int ret;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002275
2276 if (adapter->state == VNIC_REMOVING ||
Thomas Falcon5a18e1e2018-04-06 18:37:05 -05002277 adapter->state == VNIC_REMOVED ||
2278 adapter->failover_pending) {
Thomas Falconaf894d22018-04-06 18:37:04 -05002279 ret = EBUSY;
Thomas Falcon5a18e1e2018-04-06 18:37:05 -05002280 netdev_dbg(netdev, "Adapter removing or pending failover, skipping reset\n");
Thomas Falconaf894d22018-04-06 18:37:04 -05002281 goto err;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002282 }
2283
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04002284 if (adapter->state == VNIC_PROBING) {
2285 netdev_warn(netdev, "Adapter reset during probe\n");
Thomas Falconaf894d22018-04-06 18:37:04 -05002286 ret = adapter->init_done_rc = EAGAIN;
2287 goto err;
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04002288 }
2289
Thomas Falcon6c5c7482018-12-10 15:22:22 -06002290 spin_lock_irqsave(&adapter->rwi_lock, flags);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002291
2292 list_for_each(entry, &adapter->rwi_list) {
2293 tmp = list_entry(entry, struct ibmvnic_rwi, list);
2294 if (tmp->reset_reason == reason) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002295 netdev_dbg(netdev, "Skipping matching reset\n");
Thomas Falcon6c5c7482018-12-10 15:22:22 -06002296 spin_unlock_irqrestore(&adapter->rwi_lock, flags);
Thomas Falconaf894d22018-04-06 18:37:04 -05002297 ret = EBUSY;
2298 goto err;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002299 }
2300 }
2301
Thomas Falcon1d1bbc32018-12-10 15:22:23 -06002302 rwi = kzalloc(sizeof(*rwi), GFP_ATOMIC);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002303 if (!rwi) {
Thomas Falcon6c5c7482018-12-10 15:22:22 -06002304 spin_unlock_irqrestore(&adapter->rwi_lock, flags);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002305 ibmvnic_close(netdev);
Thomas Falconaf894d22018-04-06 18:37:04 -05002306 ret = ENOMEM;
2307 goto err;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002308 }
Thomas Falcon2770a792018-05-23 13:38:02 -05002309 /* if we just received a transport event,
2310 * flush reset queue and process this reset
2311 */
2312 if (adapter->force_reset_recovery && !list_empty(&adapter->rwi_list)) {
2313 list_for_each_safe(entry, tmp_entry, &adapter->rwi_list)
2314 list_del(entry);
2315 }
Nathan Fontenoted651a12017-05-03 14:04:38 -04002316 rwi->reset_reason = reason;
2317 list_add_tail(&rwi->list, &adapter->rwi_list);
Thomas Falcon6c5c7482018-12-10 15:22:22 -06002318 spin_unlock_irqrestore(&adapter->rwi_lock, flags);
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002319 netdev_dbg(adapter->netdev, "Scheduling reset (reason %d)\n", reason);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002320 schedule_work(&adapter->ibmvnic_reset);
Thomas Falconaf894d22018-04-06 18:37:04 -05002321
2322 return 0;
2323err:
Thomas Falconaf894d22018-04-06 18:37:04 -05002324 return -ret;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002325}
2326
Michael S. Tsirkin0290bd22019-12-10 09:23:51 -05002327static void ibmvnic_tx_timeout(struct net_device *dev, unsigned int txqueue)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002328{
2329 struct ibmvnic_adapter *adapter = netdev_priv(dev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002330
Nathan Fontenoted651a12017-05-03 14:04:38 -04002331 ibmvnic_reset(adapter, VNIC_RESET_TIMEOUT);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002332}
2333
2334static void remove_buff_from_pool(struct ibmvnic_adapter *adapter,
2335 struct ibmvnic_rx_buff *rx_buff)
2336{
2337 struct ibmvnic_rx_pool *pool = &adapter->rx_pool[rx_buff->pool_index];
2338
2339 rx_buff->skb = NULL;
2340
2341 pool->free_map[pool->next_alloc] = (int)(rx_buff - pool->rx_buff);
2342 pool->next_alloc = (pool->next_alloc + 1) % pool->size;
2343
2344 atomic_dec(&pool->available);
2345}
2346
2347static int ibmvnic_poll(struct napi_struct *napi, int budget)
2348{
2349 struct net_device *netdev = napi->dev;
2350 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2351 int scrq_num = (int)(napi - adapter->napi);
2352 int frames_processed = 0;
Nathan Fontenot152ce472017-05-26 10:30:54 -04002353
Thomas Falcon032c5e82015-12-21 11:26:06 -06002354restart_poll:
2355 while (frames_processed < budget) {
2356 struct sk_buff *skb;
2357 struct ibmvnic_rx_buff *rx_buff;
2358 union sub_crq *next;
2359 u32 length;
2360 u16 offset;
2361 u8 flags = 0;
2362
Juliet Kim7ed5b312019-09-20 16:11:23 -04002363 if (unlikely(test_bit(0, &adapter->resetting) &&
John Allen34686562018-02-06 16:21:49 -06002364 adapter->reset_reason != VNIC_RESET_NON_FATAL)) {
Thomas Falcon21ecba62017-06-14 23:50:09 -05002365 enable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
2366 napi_complete_done(napi, frames_processed);
2367 return frames_processed;
2368 }
2369
Thomas Falcon032c5e82015-12-21 11:26:06 -06002370 if (!pending_scrq(adapter, adapter->rx_scrq[scrq_num]))
2371 break;
2372 next = ibmvnic_next_scrq(adapter, adapter->rx_scrq[scrq_num]);
2373 rx_buff =
2374 (struct ibmvnic_rx_buff *)be64_to_cpu(next->
2375 rx_comp.correlator);
2376 /* do error checking */
2377 if (next->rx_comp.rc) {
John Allene1cea2e2017-08-07 15:42:30 -05002378 netdev_dbg(netdev, "rx buffer returned with rc %x\n",
2379 be16_to_cpu(next->rx_comp.rc));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002380 /* free the entry */
2381 next->rx_comp.first = 0;
Thomas Falcon4b9b0f02018-02-13 18:23:42 -06002382 dev_kfree_skb_any(rx_buff->skb);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002383 remove_buff_from_pool(adapter, rx_buff);
Nathan Fontenotca05e312017-05-03 14:05:14 -04002384 continue;
Thomas Falconabe27a82018-02-19 20:12:57 -06002385 } else if (!rx_buff->skb) {
2386 /* free the entry */
2387 next->rx_comp.first = 0;
2388 remove_buff_from_pool(adapter, rx_buff);
2389 continue;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002390 }
2391
2392 length = be32_to_cpu(next->rx_comp.len);
2393 offset = be16_to_cpu(next->rx_comp.off_frame_data);
2394 flags = next->rx_comp.flags;
2395 skb = rx_buff->skb;
2396 skb_copy_to_linear_data(skb, rx_buff->data + offset,
2397 length);
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04002398
2399 /* VLAN Header has been stripped by the system firmware and
2400 * needs to be inserted by the driver
2401 */
2402 if (adapter->rx_vlan_header_insertion &&
2403 (flags & IBMVNIC_VLAN_STRIPPED))
2404 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
2405 ntohs(next->rx_comp.vlan_tci));
2406
Thomas Falcon032c5e82015-12-21 11:26:06 -06002407 /* free the entry */
2408 next->rx_comp.first = 0;
2409 remove_buff_from_pool(adapter, rx_buff);
2410
2411 skb_put(skb, length);
2412 skb->protocol = eth_type_trans(skb, netdev);
Thomas Falcon94ca3052017-05-03 14:05:20 -04002413 skb_record_rx_queue(skb, scrq_num);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002414
2415 if (flags & IBMVNIC_IP_CHKSUM_GOOD &&
2416 flags & IBMVNIC_TCP_UDP_CHKSUM_GOOD) {
2417 skb->ip_summed = CHECKSUM_UNNECESSARY;
2418 }
2419
2420 length = skb->len;
2421 napi_gro_receive(napi, skb); /* send it up */
2422 netdev->stats.rx_packets++;
2423 netdev->stats.rx_bytes += length;
John Allen3d52b592017-08-02 16:44:14 -05002424 adapter->rx_stats_buffers[scrq_num].packets++;
2425 adapter->rx_stats_buffers[scrq_num].bytes += length;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002426 frames_processed++;
2427 }
Nathan Fontenot152ce472017-05-26 10:30:54 -04002428
2429 if (adapter->state != VNIC_CLOSING)
2430 replenish_rx_pool(adapter, &adapter->rx_pool[scrq_num]);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002431
2432 if (frames_processed < budget) {
2433 enable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
Eric Dumazet6ad20162017-01-30 08:22:01 -08002434 napi_complete_done(napi, frames_processed);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002435 if (pending_scrq(adapter, adapter->rx_scrq[scrq_num]) &&
2436 napi_reschedule(napi)) {
2437 disable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
2438 goto restart_poll;
2439 }
2440 }
2441 return frames_processed;
2442}
2443
John Allenc26eba02017-10-26 16:23:25 -05002444static int wait_for_reset(struct ibmvnic_adapter *adapter)
2445{
Thomas Falconaf894d22018-04-06 18:37:04 -05002446 int rc, ret;
2447
John Allenc26eba02017-10-26 16:23:25 -05002448 adapter->fallback.mtu = adapter->req_mtu;
2449 adapter->fallback.rx_queues = adapter->req_rx_queues;
2450 adapter->fallback.tx_queues = adapter->req_tx_queues;
2451 adapter->fallback.rx_entries = adapter->req_rx_add_entries_per_subcrq;
2452 adapter->fallback.tx_entries = adapter->req_tx_entries_per_subcrq;
2453
Thomas Falcon070eca92019-11-25 17:12:53 -06002454 reinit_completion(&adapter->reset_done);
John Allenc26eba02017-10-26 16:23:25 -05002455 adapter->wait_for_reset = true;
Thomas Falconaf894d22018-04-06 18:37:04 -05002456 rc = ibmvnic_reset(adapter, VNIC_RESET_CHANGE_PARAM);
Thomas Falcon476d96c2019-11-25 17:12:55 -06002457
2458 if (rc) {
2459 ret = rc;
2460 goto out;
2461 }
2462 rc = ibmvnic_wait_for_completion(adapter, &adapter->reset_done, 60000);
2463 if (rc) {
2464 ret = -ENODEV;
2465 goto out;
2466 }
John Allenc26eba02017-10-26 16:23:25 -05002467
Thomas Falconaf894d22018-04-06 18:37:04 -05002468 ret = 0;
John Allenc26eba02017-10-26 16:23:25 -05002469 if (adapter->reset_done_rc) {
Thomas Falconaf894d22018-04-06 18:37:04 -05002470 ret = -EIO;
John Allenc26eba02017-10-26 16:23:25 -05002471 adapter->desired.mtu = adapter->fallback.mtu;
2472 adapter->desired.rx_queues = adapter->fallback.rx_queues;
2473 adapter->desired.tx_queues = adapter->fallback.tx_queues;
2474 adapter->desired.rx_entries = adapter->fallback.rx_entries;
2475 adapter->desired.tx_entries = adapter->fallback.tx_entries;
2476
Thomas Falcon070eca92019-11-25 17:12:53 -06002477 reinit_completion(&adapter->reset_done);
Thomas Falconaf894d22018-04-06 18:37:04 -05002478 adapter->wait_for_reset = true;
2479 rc = ibmvnic_reset(adapter, VNIC_RESET_CHANGE_PARAM);
Thomas Falcon476d96c2019-11-25 17:12:55 -06002480 if (rc) {
2481 ret = rc;
2482 goto out;
2483 }
2484 rc = ibmvnic_wait_for_completion(adapter, &adapter->reset_done,
2485 60000);
2486 if (rc) {
2487 ret = -ENODEV;
2488 goto out;
2489 }
John Allenc26eba02017-10-26 16:23:25 -05002490 }
Thomas Falcon476d96c2019-11-25 17:12:55 -06002491out:
John Allenc26eba02017-10-26 16:23:25 -05002492 adapter->wait_for_reset = false;
2493
Thomas Falconaf894d22018-04-06 18:37:04 -05002494 return ret;
John Allenc26eba02017-10-26 16:23:25 -05002495}
2496
John Allen3a807b72017-06-06 16:55:52 -05002497static int ibmvnic_change_mtu(struct net_device *netdev, int new_mtu)
2498{
John Allenc26eba02017-10-26 16:23:25 -05002499 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2500
2501 adapter->desired.mtu = new_mtu + ETH_HLEN;
2502
2503 return wait_for_reset(adapter);
John Allen3a807b72017-06-06 16:55:52 -05002504}
2505
Thomas Falconf10b09e2018-03-12 11:51:05 -05002506static netdev_features_t ibmvnic_features_check(struct sk_buff *skb,
2507 struct net_device *dev,
2508 netdev_features_t features)
2509{
2510 /* Some backing hardware adapters can not
2511 * handle packets with a MSS less than 224
2512 * or with only one segment.
2513 */
2514 if (skb_is_gso(skb)) {
2515 if (skb_shinfo(skb)->gso_size < 224 ||
2516 skb_shinfo(skb)->gso_segs == 1)
2517 features &= ~NETIF_F_GSO_MASK;
2518 }
2519
2520 return features;
2521}
2522
Thomas Falcon032c5e82015-12-21 11:26:06 -06002523static const struct net_device_ops ibmvnic_netdev_ops = {
2524 .ndo_open = ibmvnic_open,
2525 .ndo_stop = ibmvnic_close,
2526 .ndo_start_xmit = ibmvnic_xmit,
2527 .ndo_set_rx_mode = ibmvnic_set_multi,
2528 .ndo_set_mac_address = ibmvnic_set_mac,
2529 .ndo_validate_addr = eth_validate_addr,
Thomas Falcon032c5e82015-12-21 11:26:06 -06002530 .ndo_tx_timeout = ibmvnic_tx_timeout,
John Allen3a807b72017-06-06 16:55:52 -05002531 .ndo_change_mtu = ibmvnic_change_mtu,
Thomas Falconf10b09e2018-03-12 11:51:05 -05002532 .ndo_features_check = ibmvnic_features_check,
Thomas Falcon032c5e82015-12-21 11:26:06 -06002533};
2534
2535/* ethtool functions */
2536
Philippe Reynes8a433792017-01-07 22:37:29 +01002537static int ibmvnic_get_link_ksettings(struct net_device *netdev,
2538 struct ethtool_link_ksettings *cmd)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002539{
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03002540 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2541 int rc;
Philippe Reynes8a433792017-01-07 22:37:29 +01002542
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03002543 rc = send_query_phys_parms(adapter);
2544 if (rc) {
2545 adapter->speed = SPEED_UNKNOWN;
2546 adapter->duplex = DUPLEX_UNKNOWN;
2547 }
2548 cmd->base.speed = adapter->speed;
2549 cmd->base.duplex = adapter->duplex;
Philippe Reynes8a433792017-01-07 22:37:29 +01002550 cmd->base.port = PORT_FIBRE;
2551 cmd->base.phy_address = 0;
2552 cmd->base.autoneg = AUTONEG_ENABLE;
2553
Thomas Falcon032c5e82015-12-21 11:26:06 -06002554 return 0;
2555}
2556
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02002557static void ibmvnic_get_drvinfo(struct net_device *netdev,
Thomas Falcon032c5e82015-12-21 11:26:06 -06002558 struct ethtool_drvinfo *info)
2559{
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02002560 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2561
Thomas Falcon032c5e82015-12-21 11:26:06 -06002562 strlcpy(info->driver, ibmvnic_driver_name, sizeof(info->driver));
2563 strlcpy(info->version, IBMVNIC_DRIVER_VERSION, sizeof(info->version));
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02002564 strlcpy(info->fw_version, adapter->fw_version,
2565 sizeof(info->fw_version));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002566}
2567
2568static u32 ibmvnic_get_msglevel(struct net_device *netdev)
2569{
2570 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2571
2572 return adapter->msg_enable;
2573}
2574
2575static void ibmvnic_set_msglevel(struct net_device *netdev, u32 data)
2576{
2577 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2578
2579 adapter->msg_enable = data;
2580}
2581
2582static u32 ibmvnic_get_link(struct net_device *netdev)
2583{
2584 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2585
2586 /* Don't need to send a query because we request a logical link up at
2587 * init and then we wait for link state indications
2588 */
2589 return adapter->logical_link_state;
2590}
2591
2592static void ibmvnic_get_ringparam(struct net_device *netdev,
2593 struct ethtool_ringparam *ring)
2594{
John Allenbc131b32017-08-02 16:46:30 -05002595 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2596
Thomas Falcon723ad912018-09-28 18:38:26 -05002597 if (adapter->priv_flags & IBMVNIC_USE_SERVER_MAXES) {
2598 ring->rx_max_pending = adapter->max_rx_add_entries_per_subcrq;
2599 ring->tx_max_pending = adapter->max_tx_entries_per_subcrq;
2600 } else {
2601 ring->rx_max_pending = IBMVNIC_MAX_QUEUE_SZ;
2602 ring->tx_max_pending = IBMVNIC_MAX_QUEUE_SZ;
2603 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002604 ring->rx_mini_max_pending = 0;
2605 ring->rx_jumbo_max_pending = 0;
John Allenbc131b32017-08-02 16:46:30 -05002606 ring->rx_pending = adapter->req_rx_add_entries_per_subcrq;
2607 ring->tx_pending = adapter->req_tx_entries_per_subcrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002608 ring->rx_mini_pending = 0;
2609 ring->rx_jumbo_pending = 0;
2610}
2611
John Allenc26eba02017-10-26 16:23:25 -05002612static int ibmvnic_set_ringparam(struct net_device *netdev,
2613 struct ethtool_ringparam *ring)
2614{
2615 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Thomas Falcon723ad912018-09-28 18:38:26 -05002616 int ret;
John Allenc26eba02017-10-26 16:23:25 -05002617
Thomas Falcon723ad912018-09-28 18:38:26 -05002618 ret = 0;
John Allenc26eba02017-10-26 16:23:25 -05002619 adapter->desired.rx_entries = ring->rx_pending;
2620 adapter->desired.tx_entries = ring->tx_pending;
2621
Thomas Falcon723ad912018-09-28 18:38:26 -05002622 ret = wait_for_reset(adapter);
2623
2624 if (!ret &&
2625 (adapter->req_rx_add_entries_per_subcrq != ring->rx_pending ||
2626 adapter->req_tx_entries_per_subcrq != ring->tx_pending))
2627 netdev_info(netdev,
2628 "Could not match full ringsize request. Requested: RX %d, TX %d; Allowed: RX %llu, TX %llu\n",
2629 ring->rx_pending, ring->tx_pending,
2630 adapter->req_rx_add_entries_per_subcrq,
2631 adapter->req_tx_entries_per_subcrq);
2632 return ret;
John Allenc26eba02017-10-26 16:23:25 -05002633}
2634
John Allenc2dbeb62017-08-02 16:47:17 -05002635static void ibmvnic_get_channels(struct net_device *netdev,
2636 struct ethtool_channels *channels)
2637{
2638 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2639
Thomas Falcon723ad912018-09-28 18:38:26 -05002640 if (adapter->priv_flags & IBMVNIC_USE_SERVER_MAXES) {
2641 channels->max_rx = adapter->max_rx_queues;
2642 channels->max_tx = adapter->max_tx_queues;
2643 } else {
2644 channels->max_rx = IBMVNIC_MAX_QUEUES;
2645 channels->max_tx = IBMVNIC_MAX_QUEUES;
2646 }
2647
John Allenc2dbeb62017-08-02 16:47:17 -05002648 channels->max_other = 0;
2649 channels->max_combined = 0;
2650 channels->rx_count = adapter->req_rx_queues;
2651 channels->tx_count = adapter->req_tx_queues;
2652 channels->other_count = 0;
2653 channels->combined_count = 0;
2654}
2655
John Allenc26eba02017-10-26 16:23:25 -05002656static int ibmvnic_set_channels(struct net_device *netdev,
2657 struct ethtool_channels *channels)
2658{
2659 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Thomas Falcon723ad912018-09-28 18:38:26 -05002660 int ret;
John Allenc26eba02017-10-26 16:23:25 -05002661
Thomas Falcon723ad912018-09-28 18:38:26 -05002662 ret = 0;
John Allenc26eba02017-10-26 16:23:25 -05002663 adapter->desired.rx_queues = channels->rx_count;
2664 adapter->desired.tx_queues = channels->tx_count;
2665
Thomas Falcon723ad912018-09-28 18:38:26 -05002666 ret = wait_for_reset(adapter);
2667
2668 if (!ret &&
2669 (adapter->req_rx_queues != channels->rx_count ||
2670 adapter->req_tx_queues != channels->tx_count))
2671 netdev_info(netdev,
2672 "Could not match full channels request. Requested: RX %d, TX %d; Allowed: RX %llu, TX %llu\n",
2673 channels->rx_count, channels->tx_count,
2674 adapter->req_rx_queues, adapter->req_tx_queues);
2675 return ret;
2676
John Allenc26eba02017-10-26 16:23:25 -05002677}
2678
Thomas Falcon032c5e82015-12-21 11:26:06 -06002679static void ibmvnic_get_strings(struct net_device *dev, u32 stringset, u8 *data)
2680{
John Allen3d52b592017-08-02 16:44:14 -05002681 struct ibmvnic_adapter *adapter = netdev_priv(dev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002682 int i;
2683
Thomas Falcon723ad912018-09-28 18:38:26 -05002684 switch (stringset) {
2685 case ETH_SS_STATS:
2686 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats);
2687 i++, data += ETH_GSTRING_LEN)
2688 memcpy(data, ibmvnic_stats[i].name, ETH_GSTRING_LEN);
2689
2690 for (i = 0; i < adapter->req_tx_queues; i++) {
2691 snprintf(data, ETH_GSTRING_LEN, "tx%d_packets", i);
2692 data += ETH_GSTRING_LEN;
2693
2694 snprintf(data, ETH_GSTRING_LEN, "tx%d_bytes", i);
2695 data += ETH_GSTRING_LEN;
2696
2697 snprintf(data, ETH_GSTRING_LEN,
2698 "tx%d_dropped_packets", i);
2699 data += ETH_GSTRING_LEN;
2700 }
2701
2702 for (i = 0; i < adapter->req_rx_queues; i++) {
2703 snprintf(data, ETH_GSTRING_LEN, "rx%d_packets", i);
2704 data += ETH_GSTRING_LEN;
2705
2706 snprintf(data, ETH_GSTRING_LEN, "rx%d_bytes", i);
2707 data += ETH_GSTRING_LEN;
2708
2709 snprintf(data, ETH_GSTRING_LEN, "rx%d_interrupts", i);
2710 data += ETH_GSTRING_LEN;
2711 }
2712 break;
2713
2714 case ETH_SS_PRIV_FLAGS:
2715 for (i = 0; i < ARRAY_SIZE(ibmvnic_priv_flags); i++)
2716 strcpy(data + i * ETH_GSTRING_LEN,
2717 ibmvnic_priv_flags[i]);
2718 break;
2719 default:
Thomas Falcon032c5e82015-12-21 11:26:06 -06002720 return;
John Allen3d52b592017-08-02 16:44:14 -05002721 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002722}
2723
2724static int ibmvnic_get_sset_count(struct net_device *dev, int sset)
2725{
John Allen3d52b592017-08-02 16:44:14 -05002726 struct ibmvnic_adapter *adapter = netdev_priv(dev);
2727
Thomas Falcon032c5e82015-12-21 11:26:06 -06002728 switch (sset) {
2729 case ETH_SS_STATS:
John Allen3d52b592017-08-02 16:44:14 -05002730 return ARRAY_SIZE(ibmvnic_stats) +
2731 adapter->req_tx_queues * NUM_TX_STATS +
2732 adapter->req_rx_queues * NUM_RX_STATS;
Thomas Falcon723ad912018-09-28 18:38:26 -05002733 case ETH_SS_PRIV_FLAGS:
2734 return ARRAY_SIZE(ibmvnic_priv_flags);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002735 default:
2736 return -EOPNOTSUPP;
2737 }
2738}
2739
2740static void ibmvnic_get_ethtool_stats(struct net_device *dev,
2741 struct ethtool_stats *stats, u64 *data)
2742{
2743 struct ibmvnic_adapter *adapter = netdev_priv(dev);
2744 union ibmvnic_crq crq;
John Allen3d52b592017-08-02 16:44:14 -05002745 int i, j;
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05002746 int rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002747
2748 memset(&crq, 0, sizeof(crq));
2749 crq.request_statistics.first = IBMVNIC_CRQ_CMD;
2750 crq.request_statistics.cmd = REQUEST_STATISTICS;
2751 crq.request_statistics.ioba = cpu_to_be32(adapter->stats_token);
2752 crq.request_statistics.len =
2753 cpu_to_be32(sizeof(struct ibmvnic_statistics));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002754
2755 /* Wait for data to be written */
Thomas Falcon070eca92019-11-25 17:12:53 -06002756 reinit_completion(&adapter->stats_done);
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05002757 rc = ibmvnic_send_crq(adapter, &crq);
2758 if (rc)
2759 return;
Thomas Falcon476d96c2019-11-25 17:12:55 -06002760 rc = ibmvnic_wait_for_completion(adapter, &adapter->stats_done, 10000);
2761 if (rc)
2762 return;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002763
2764 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++)
John Allen52da5c12017-08-02 16:45:28 -05002765 data[i] = be64_to_cpu(IBMVNIC_GET_STAT(adapter,
2766 ibmvnic_stats[i].offset));
John Allen3d52b592017-08-02 16:44:14 -05002767
2768 for (j = 0; j < adapter->req_tx_queues; j++) {
2769 data[i] = adapter->tx_stats_buffers[j].packets;
2770 i++;
2771 data[i] = adapter->tx_stats_buffers[j].bytes;
2772 i++;
2773 data[i] = adapter->tx_stats_buffers[j].dropped_packets;
2774 i++;
2775 }
2776
2777 for (j = 0; j < adapter->req_rx_queues; j++) {
2778 data[i] = adapter->rx_stats_buffers[j].packets;
2779 i++;
2780 data[i] = adapter->rx_stats_buffers[j].bytes;
2781 i++;
2782 data[i] = adapter->rx_stats_buffers[j].interrupts;
2783 i++;
2784 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002785}
2786
Thomas Falcon723ad912018-09-28 18:38:26 -05002787static u32 ibmvnic_get_priv_flags(struct net_device *netdev)
2788{
2789 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2790
2791 return adapter->priv_flags;
2792}
2793
2794static int ibmvnic_set_priv_flags(struct net_device *netdev, u32 flags)
2795{
2796 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2797 bool which_maxes = !!(flags & IBMVNIC_USE_SERVER_MAXES);
2798
2799 if (which_maxes)
2800 adapter->priv_flags |= IBMVNIC_USE_SERVER_MAXES;
2801 else
2802 adapter->priv_flags &= ~IBMVNIC_USE_SERVER_MAXES;
2803
2804 return 0;
2805}
Thomas Falcon032c5e82015-12-21 11:26:06 -06002806static const struct ethtool_ops ibmvnic_ethtool_ops = {
Thomas Falcon032c5e82015-12-21 11:26:06 -06002807 .get_drvinfo = ibmvnic_get_drvinfo,
2808 .get_msglevel = ibmvnic_get_msglevel,
2809 .set_msglevel = ibmvnic_set_msglevel,
2810 .get_link = ibmvnic_get_link,
2811 .get_ringparam = ibmvnic_get_ringparam,
John Allenc26eba02017-10-26 16:23:25 -05002812 .set_ringparam = ibmvnic_set_ringparam,
John Allenc2dbeb62017-08-02 16:47:17 -05002813 .get_channels = ibmvnic_get_channels,
John Allenc26eba02017-10-26 16:23:25 -05002814 .set_channels = ibmvnic_set_channels,
Thomas Falcon032c5e82015-12-21 11:26:06 -06002815 .get_strings = ibmvnic_get_strings,
2816 .get_sset_count = ibmvnic_get_sset_count,
2817 .get_ethtool_stats = ibmvnic_get_ethtool_stats,
Philippe Reynes8a433792017-01-07 22:37:29 +01002818 .get_link_ksettings = ibmvnic_get_link_ksettings,
Thomas Falcon723ad912018-09-28 18:38:26 -05002819 .get_priv_flags = ibmvnic_get_priv_flags,
2820 .set_priv_flags = ibmvnic_set_priv_flags,
Thomas Falcon032c5e82015-12-21 11:26:06 -06002821};
2822
2823/* Routines for managing CRQs/sCRQs */
2824
Nathan Fontenot57a49432017-05-26 10:31:12 -04002825static int reset_one_sub_crq_queue(struct ibmvnic_adapter *adapter,
2826 struct ibmvnic_sub_crq_queue *scrq)
2827{
2828 int rc;
2829
2830 if (scrq->irq) {
2831 free_irq(scrq->irq, scrq);
2832 irq_dispose_mapping(scrq->irq);
2833 scrq->irq = 0;
2834 }
2835
Thomas Falconc8b2ad02017-06-14 23:50:07 -05002836 memset(scrq->msgs, 0, 4 * PAGE_SIZE);
Thomas Falcon41f71462018-04-06 18:37:03 -05002837 atomic_set(&scrq->used, 0);
Nathan Fontenot57a49432017-05-26 10:31:12 -04002838 scrq->cur = 0;
2839
2840 rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
2841 4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
2842 return rc;
2843}
2844
2845static int reset_sub_crq_queues(struct ibmvnic_adapter *adapter)
2846{
2847 int i, rc;
2848
2849 for (i = 0; i < adapter->req_tx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002850 netdev_dbg(adapter->netdev, "Re-setting tx_scrq[%d]\n", i);
Nathan Fontenot57a49432017-05-26 10:31:12 -04002851 rc = reset_one_sub_crq_queue(adapter, adapter->tx_scrq[i]);
2852 if (rc)
2853 return rc;
2854 }
2855
2856 for (i = 0; i < adapter->req_rx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002857 netdev_dbg(adapter->netdev, "Re-setting rx_scrq[%d]\n", i);
Nathan Fontenot57a49432017-05-26 10:31:12 -04002858 rc = reset_one_sub_crq_queue(adapter, adapter->rx_scrq[i]);
2859 if (rc)
2860 return rc;
2861 }
2862
Nathan Fontenot57a49432017-05-26 10:31:12 -04002863 return rc;
2864}
2865
Thomas Falcon032c5e82015-12-21 11:26:06 -06002866static void release_sub_crq_queue(struct ibmvnic_adapter *adapter,
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06002867 struct ibmvnic_sub_crq_queue *scrq,
2868 bool do_h_free)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002869{
2870 struct device *dev = &adapter->vdev->dev;
2871 long rc;
2872
2873 netdev_dbg(adapter->netdev, "Releasing sub-CRQ\n");
2874
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06002875 if (do_h_free) {
2876 /* Close the sub-crqs */
2877 do {
2878 rc = plpar_hcall_norets(H_FREE_SUB_CRQ,
2879 adapter->vdev->unit_address,
2880 scrq->crq_num);
2881 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002882
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06002883 if (rc) {
2884 netdev_err(adapter->netdev,
2885 "Failed to release sub-CRQ %16lx, rc = %ld\n",
2886 scrq->crq_num, rc);
2887 }
Thomas Falconffa73852017-04-19 13:44:29 -04002888 }
2889
Thomas Falcon032c5e82015-12-21 11:26:06 -06002890 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
2891 DMA_BIDIRECTIONAL);
2892 free_pages((unsigned long)scrq->msgs, 2);
2893 kfree(scrq);
2894}
2895
2896static struct ibmvnic_sub_crq_queue *init_sub_crq_queue(struct ibmvnic_adapter
2897 *adapter)
2898{
2899 struct device *dev = &adapter->vdev->dev;
2900 struct ibmvnic_sub_crq_queue *scrq;
2901 int rc;
2902
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04002903 scrq = kzalloc(sizeof(*scrq), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002904 if (!scrq)
2905 return NULL;
2906
Nathan Fontenot7f7adc52017-04-19 13:45:16 -04002907 scrq->msgs =
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04002908 (union sub_crq *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, 2);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002909 if (!scrq->msgs) {
2910 dev_warn(dev, "Couldn't allocate crq queue messages page\n");
2911 goto zero_page_failed;
2912 }
2913
2914 scrq->msg_token = dma_map_single(dev, scrq->msgs, 4 * PAGE_SIZE,
2915 DMA_BIDIRECTIONAL);
2916 if (dma_mapping_error(dev, scrq->msg_token)) {
2917 dev_warn(dev, "Couldn't map crq queue messages page\n");
2918 goto map_failed;
2919 }
2920
2921 rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
2922 4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
2923
2924 if (rc == H_RESOURCE)
2925 rc = ibmvnic_reset_crq(adapter);
2926
2927 if (rc == H_CLOSED) {
2928 dev_warn(dev, "Partner adapter not ready, waiting.\n");
2929 } else if (rc) {
2930 dev_warn(dev, "Error %d registering sub-crq\n", rc);
2931 goto reg_failed;
2932 }
2933
Thomas Falcon032c5e82015-12-21 11:26:06 -06002934 scrq->adapter = adapter;
2935 scrq->size = 4 * PAGE_SIZE / sizeof(*scrq->msgs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002936 spin_lock_init(&scrq->lock);
2937
2938 netdev_dbg(adapter->netdev,
2939 "sub-crq initialized, num %lx, hw_irq=%lx, irq=%x\n",
2940 scrq->crq_num, scrq->hw_irq, scrq->irq);
2941
2942 return scrq;
2943
Thomas Falcon032c5e82015-12-21 11:26:06 -06002944reg_failed:
2945 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
2946 DMA_BIDIRECTIONAL);
2947map_failed:
2948 free_pages((unsigned long)scrq->msgs, 2);
2949zero_page_failed:
2950 kfree(scrq);
2951
2952 return NULL;
2953}
2954
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06002955static void release_sub_crqs(struct ibmvnic_adapter *adapter, bool do_h_free)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002956{
2957 int i;
2958
2959 if (adapter->tx_scrq) {
Nathan Fontenot82e3be32018-02-21 21:33:56 -06002960 for (i = 0; i < adapter->num_active_tx_scrqs; i++) {
Nathan Fontenotb5108882017-03-30 02:49:18 -04002961 if (!adapter->tx_scrq[i])
2962 continue;
2963
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002964 netdev_dbg(adapter->netdev, "Releasing tx_scrq[%d]\n",
2965 i);
Nathan Fontenotb5108882017-03-30 02:49:18 -04002966 if (adapter->tx_scrq[i]->irq) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06002967 free_irq(adapter->tx_scrq[i]->irq,
2968 adapter->tx_scrq[i]);
Thomas Falcon88eb98a2016-07-06 15:35:16 -05002969 irq_dispose_mapping(adapter->tx_scrq[i]->irq);
Nathan Fontenotb5108882017-03-30 02:49:18 -04002970 adapter->tx_scrq[i]->irq = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002971 }
Nathan Fontenotb5108882017-03-30 02:49:18 -04002972
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06002973 release_sub_crq_queue(adapter, adapter->tx_scrq[i],
2974 do_h_free);
Nathan Fontenotb5108882017-03-30 02:49:18 -04002975 }
2976
Nathan Fontenot9501df32017-03-15 23:38:07 -04002977 kfree(adapter->tx_scrq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002978 adapter->tx_scrq = NULL;
Nathan Fontenot82e3be32018-02-21 21:33:56 -06002979 adapter->num_active_tx_scrqs = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002980 }
2981
2982 if (adapter->rx_scrq) {
Nathan Fontenot82e3be32018-02-21 21:33:56 -06002983 for (i = 0; i < adapter->num_active_rx_scrqs; i++) {
Nathan Fontenotb5108882017-03-30 02:49:18 -04002984 if (!adapter->rx_scrq[i])
2985 continue;
2986
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002987 netdev_dbg(adapter->netdev, "Releasing rx_scrq[%d]\n",
2988 i);
Nathan Fontenotb5108882017-03-30 02:49:18 -04002989 if (adapter->rx_scrq[i]->irq) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06002990 free_irq(adapter->rx_scrq[i]->irq,
2991 adapter->rx_scrq[i]);
Thomas Falcon88eb98a2016-07-06 15:35:16 -05002992 irq_dispose_mapping(adapter->rx_scrq[i]->irq);
Nathan Fontenotb5108882017-03-30 02:49:18 -04002993 adapter->rx_scrq[i]->irq = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002994 }
Nathan Fontenotb5108882017-03-30 02:49:18 -04002995
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06002996 release_sub_crq_queue(adapter, adapter->rx_scrq[i],
2997 do_h_free);
Nathan Fontenotb5108882017-03-30 02:49:18 -04002998 }
2999
Nathan Fontenot9501df32017-03-15 23:38:07 -04003000 kfree(adapter->rx_scrq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003001 adapter->rx_scrq = NULL;
Nathan Fontenot82e3be32018-02-21 21:33:56 -06003002 adapter->num_active_rx_scrqs = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003003 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06003004}
3005
3006static int disable_scrq_irq(struct ibmvnic_adapter *adapter,
3007 struct ibmvnic_sub_crq_queue *scrq)
3008{
3009 struct device *dev = &adapter->vdev->dev;
3010 unsigned long rc;
3011
3012 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
3013 H_DISABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
3014 if (rc)
3015 dev_err(dev, "Couldn't disable scrq irq 0x%lx. rc=%ld\n",
3016 scrq->hw_irq, rc);
3017 return rc;
3018}
3019
3020static int enable_scrq_irq(struct ibmvnic_adapter *adapter,
3021 struct ibmvnic_sub_crq_queue *scrq)
3022{
3023 struct device *dev = &adapter->vdev->dev;
3024 unsigned long rc;
3025
3026 if (scrq->hw_irq > 0x100000000ULL) {
3027 dev_err(dev, "bad hw_irq = %lx\n", scrq->hw_irq);
3028 return 1;
3029 }
3030
Juliet Kim7ed5b312019-09-20 16:11:23 -04003031 if (test_bit(0, &adapter->resetting) &&
Nathan Fontenot73f9d362018-05-22 11:21:10 -05003032 adapter->reset_reason == VNIC_RESET_MOBILITY) {
Juliet Kim284f87d2019-11-20 10:50:03 -05003033 u64 val = (0xff000000) | scrq->hw_irq;
Nathan Fontenot73f9d362018-05-22 11:21:10 -05003034
Juliet Kim284f87d2019-11-20 10:50:03 -05003035 rc = plpar_hcall_norets(H_EOI, val);
Juliet Kim2df5c602019-11-20 10:50:04 -05003036 /* H_EOI would fail with rc = H_FUNCTION when running
3037 * in XIVE mode which is expected, but not an error.
3038 */
3039 if (rc && (rc != H_FUNCTION))
Juliet Kim284f87d2019-11-20 10:50:03 -05003040 dev_err(dev, "H_EOI FAILED irq 0x%llx. rc=%ld\n",
3041 val, rc);
Nathan Fontenot73f9d362018-05-22 11:21:10 -05003042 }
Thomas Falconf23e0642018-04-15 18:53:36 -05003043
Thomas Falcon032c5e82015-12-21 11:26:06 -06003044 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
3045 H_ENABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
3046 if (rc)
3047 dev_err(dev, "Couldn't enable scrq irq 0x%lx. rc=%ld\n",
3048 scrq->hw_irq, rc);
3049 return rc;
3050}
3051
3052static int ibmvnic_complete_tx(struct ibmvnic_adapter *adapter,
3053 struct ibmvnic_sub_crq_queue *scrq)
3054{
3055 struct device *dev = &adapter->vdev->dev;
Thomas Falcon06b3e352018-03-16 20:00:28 -05003056 struct ibmvnic_tx_pool *tx_pool;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003057 struct ibmvnic_tx_buff *txbuff;
3058 union sub_crq *next;
3059 int index;
3060 int i, j;
3061
3062restart_loop:
3063 while (pending_scrq(adapter, scrq)) {
3064 unsigned int pool = scrq->pool_index;
Thomas Falconffc385b2018-02-18 10:08:41 -06003065 int num_entries = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003066
3067 next = ibmvnic_next_scrq(adapter, scrq);
3068 for (i = 0; i < next->tx_comp.num_comps; i++) {
3069 if (next->tx_comp.rcs[i]) {
3070 dev_err(dev, "tx error %x\n",
3071 next->tx_comp.rcs[i]);
3072 continue;
3073 }
3074 index = be32_to_cpu(next->tx_comp.correlators[i]);
Thomas Falcon06b3e352018-03-16 20:00:28 -05003075 if (index & IBMVNIC_TSO_POOL_MASK) {
3076 tx_pool = &adapter->tso_pool[pool];
3077 index &= ~IBMVNIC_TSO_POOL_MASK;
3078 } else {
3079 tx_pool = &adapter->tx_pool[pool];
3080 }
3081
3082 txbuff = &tx_pool->tx_buff[index];
Thomas Falcon032c5e82015-12-21 11:26:06 -06003083
3084 for (j = 0; j < IBMVNIC_MAX_FRAGS_PER_CRQ; j++) {
3085 if (!txbuff->data_dma[j])
3086 continue;
3087
3088 txbuff->data_dma[j] = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003089 }
3090
Thomas Falcon142c0ac2017-03-05 12:18:41 -06003091 if (txbuff->last_frag) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06003092 dev_kfree_skb_any(txbuff->skb);
Nathan Fontenot7c3e7de2017-05-03 14:05:25 -04003093 txbuff->skb = NULL;
Thomas Falcon142c0ac2017-03-05 12:18:41 -06003094 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06003095
Thomas Falconffc385b2018-02-18 10:08:41 -06003096 num_entries += txbuff->num_entries;
3097
Thomas Falcon06b3e352018-03-16 20:00:28 -05003098 tx_pool->free_map[tx_pool->producer_index] = index;
3099 tx_pool->producer_index =
3100 (tx_pool->producer_index + 1) %
3101 tx_pool->num_buffers;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003102 }
3103 /* remove tx_comp scrq*/
3104 next->tx_comp.first = 0;
Nathan Fontenot7c3e7de2017-05-03 14:05:25 -04003105
Thomas Falconffc385b2018-02-18 10:08:41 -06003106 if (atomic_sub_return(num_entries, &scrq->used) <=
Nathan Fontenot7c3e7de2017-05-03 14:05:25 -04003107 (adapter->req_tx_entries_per_subcrq / 2) &&
3108 __netif_subqueue_stopped(adapter->netdev,
3109 scrq->pool_index)) {
3110 netif_wake_subqueue(adapter->netdev, scrq->pool_index);
Thomas Falcon0aecb132018-02-26 18:10:58 -06003111 netdev_dbg(adapter->netdev, "Started queue %d\n",
3112 scrq->pool_index);
Nathan Fontenot7c3e7de2017-05-03 14:05:25 -04003113 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06003114 }
3115
3116 enable_scrq_irq(adapter, scrq);
3117
3118 if (pending_scrq(adapter, scrq)) {
3119 disable_scrq_irq(adapter, scrq);
3120 goto restart_loop;
3121 }
3122
3123 return 0;
3124}
3125
3126static irqreturn_t ibmvnic_interrupt_tx(int irq, void *instance)
3127{
3128 struct ibmvnic_sub_crq_queue *scrq = instance;
3129 struct ibmvnic_adapter *adapter = scrq->adapter;
3130
3131 disable_scrq_irq(adapter, scrq);
3132 ibmvnic_complete_tx(adapter, scrq);
3133
3134 return IRQ_HANDLED;
3135}
3136
3137static irqreturn_t ibmvnic_interrupt_rx(int irq, void *instance)
3138{
3139 struct ibmvnic_sub_crq_queue *scrq = instance;
3140 struct ibmvnic_adapter *adapter = scrq->adapter;
3141
Nathan Fontenot09fb35e2018-01-10 10:40:09 -06003142 /* When booting a kdump kernel we can hit pending interrupts
3143 * prior to completing driver initialization.
3144 */
3145 if (unlikely(adapter->state != VNIC_OPEN))
3146 return IRQ_NONE;
3147
John Allen3d52b592017-08-02 16:44:14 -05003148 adapter->rx_stats_buffers[scrq->scrq_num].interrupts++;
3149
Thomas Falcon032c5e82015-12-21 11:26:06 -06003150 if (napi_schedule_prep(&adapter->napi[scrq->scrq_num])) {
3151 disable_scrq_irq(adapter, scrq);
3152 __napi_schedule(&adapter->napi[scrq->scrq_num]);
3153 }
3154
3155 return IRQ_HANDLED;
3156}
3157
Thomas Falconea22d512016-07-06 15:35:17 -05003158static int init_sub_crq_irqs(struct ibmvnic_adapter *adapter)
3159{
3160 struct device *dev = &adapter->vdev->dev;
3161 struct ibmvnic_sub_crq_queue *scrq;
3162 int i = 0, j = 0;
3163 int rc = 0;
3164
3165 for (i = 0; i < adapter->req_tx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05003166 netdev_dbg(adapter->netdev, "Initializing tx_scrq[%d] irq\n",
3167 i);
Thomas Falconea22d512016-07-06 15:35:17 -05003168 scrq = adapter->tx_scrq[i];
3169 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
3170
Michael Ellerman99c17902016-09-10 19:59:05 +10003171 if (!scrq->irq) {
Thomas Falconea22d512016-07-06 15:35:17 -05003172 rc = -EINVAL;
3173 dev_err(dev, "Error mapping irq\n");
3174 goto req_tx_irq_failed;
3175 }
3176
Murilo Fossa Vicentinie56e2512019-04-25 11:02:33 -03003177 snprintf(scrq->name, sizeof(scrq->name), "ibmvnic-%x-tx%d",
3178 adapter->vdev->unit_address, i);
Thomas Falconea22d512016-07-06 15:35:17 -05003179 rc = request_irq(scrq->irq, ibmvnic_interrupt_tx,
Murilo Fossa Vicentinie56e2512019-04-25 11:02:33 -03003180 0, scrq->name, scrq);
Thomas Falconea22d512016-07-06 15:35:17 -05003181
3182 if (rc) {
3183 dev_err(dev, "Couldn't register tx irq 0x%x. rc=%d\n",
3184 scrq->irq, rc);
3185 irq_dispose_mapping(scrq->irq);
Nathan Fontenotaf9090c2018-02-20 11:04:18 -06003186 goto req_tx_irq_failed;
Thomas Falconea22d512016-07-06 15:35:17 -05003187 }
3188 }
3189
3190 for (i = 0; i < adapter->req_rx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05003191 netdev_dbg(adapter->netdev, "Initializing rx_scrq[%d] irq\n",
3192 i);
Thomas Falconea22d512016-07-06 15:35:17 -05003193 scrq = adapter->rx_scrq[i];
3194 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
Michael Ellerman99c17902016-09-10 19:59:05 +10003195 if (!scrq->irq) {
Thomas Falconea22d512016-07-06 15:35:17 -05003196 rc = -EINVAL;
3197 dev_err(dev, "Error mapping irq\n");
3198 goto req_rx_irq_failed;
3199 }
Murilo Fossa Vicentinie56e2512019-04-25 11:02:33 -03003200 snprintf(scrq->name, sizeof(scrq->name), "ibmvnic-%x-rx%d",
3201 adapter->vdev->unit_address, i);
Thomas Falconea22d512016-07-06 15:35:17 -05003202 rc = request_irq(scrq->irq, ibmvnic_interrupt_rx,
Murilo Fossa Vicentinie56e2512019-04-25 11:02:33 -03003203 0, scrq->name, scrq);
Thomas Falconea22d512016-07-06 15:35:17 -05003204 if (rc) {
3205 dev_err(dev, "Couldn't register rx irq 0x%x. rc=%d\n",
3206 scrq->irq, rc);
3207 irq_dispose_mapping(scrq->irq);
3208 goto req_rx_irq_failed;
3209 }
3210 }
3211 return rc;
3212
3213req_rx_irq_failed:
Thomas Falcon8bf371e2016-10-27 12:28:52 -05003214 for (j = 0; j < i; j++) {
Thomas Falconea22d512016-07-06 15:35:17 -05003215 free_irq(adapter->rx_scrq[j]->irq, adapter->rx_scrq[j]);
3216 irq_dispose_mapping(adapter->rx_scrq[j]->irq);
Thomas Falcon8bf371e2016-10-27 12:28:52 -05003217 }
Thomas Falconea22d512016-07-06 15:35:17 -05003218 i = adapter->req_tx_queues;
3219req_tx_irq_failed:
Thomas Falcon8bf371e2016-10-27 12:28:52 -05003220 for (j = 0; j < i; j++) {
Thomas Falconea22d512016-07-06 15:35:17 -05003221 free_irq(adapter->tx_scrq[j]->irq, adapter->tx_scrq[j]);
Thomas Falcon27a21452020-07-29 16:36:32 -05003222 irq_dispose_mapping(adapter->tx_scrq[j]->irq);
Thomas Falcon8bf371e2016-10-27 12:28:52 -05003223 }
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06003224 release_sub_crqs(adapter, 1);
Thomas Falconea22d512016-07-06 15:35:17 -05003225 return rc;
3226}
3227
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003228static int init_sub_crqs(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003229{
3230 struct device *dev = &adapter->vdev->dev;
3231 struct ibmvnic_sub_crq_queue **allqueues;
3232 int registered_queues = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003233 int total_queues;
3234 int more = 0;
Thomas Falconea22d512016-07-06 15:35:17 -05003235 int i;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003236
Thomas Falcon032c5e82015-12-21 11:26:06 -06003237 total_queues = adapter->req_tx_queues + adapter->req_rx_queues;
3238
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04003239 allqueues = kcalloc(total_queues, sizeof(*allqueues), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003240 if (!allqueues)
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003241 return -1;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003242
3243 for (i = 0; i < total_queues; i++) {
3244 allqueues[i] = init_sub_crq_queue(adapter);
3245 if (!allqueues[i]) {
3246 dev_warn(dev, "Couldn't allocate all sub-crqs\n");
3247 break;
3248 }
3249 registered_queues++;
3250 }
3251
3252 /* Make sure we were able to register the minimum number of queues */
3253 if (registered_queues <
3254 adapter->min_tx_queues + adapter->min_rx_queues) {
3255 dev_err(dev, "Fatal: Couldn't init min number of sub-crqs\n");
3256 goto tx_failed;
3257 }
3258
3259 /* Distribute the failed allocated queues*/
3260 for (i = 0; i < total_queues - registered_queues + more ; i++) {
3261 netdev_dbg(adapter->netdev, "Reducing number of queues\n");
3262 switch (i % 3) {
3263 case 0:
3264 if (adapter->req_rx_queues > adapter->min_rx_queues)
3265 adapter->req_rx_queues--;
3266 else
3267 more++;
3268 break;
3269 case 1:
3270 if (adapter->req_tx_queues > adapter->min_tx_queues)
3271 adapter->req_tx_queues--;
3272 else
3273 more++;
3274 break;
3275 }
3276 }
3277
3278 adapter->tx_scrq = kcalloc(adapter->req_tx_queues,
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04003279 sizeof(*adapter->tx_scrq), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003280 if (!adapter->tx_scrq)
3281 goto tx_failed;
3282
3283 for (i = 0; i < adapter->req_tx_queues; i++) {
3284 adapter->tx_scrq[i] = allqueues[i];
3285 adapter->tx_scrq[i]->pool_index = i;
Nathan Fontenot82e3be32018-02-21 21:33:56 -06003286 adapter->num_active_tx_scrqs++;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003287 }
3288
3289 adapter->rx_scrq = kcalloc(adapter->req_rx_queues,
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04003290 sizeof(*adapter->rx_scrq), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003291 if (!adapter->rx_scrq)
3292 goto rx_failed;
3293
3294 for (i = 0; i < adapter->req_rx_queues; i++) {
3295 adapter->rx_scrq[i] = allqueues[i + adapter->req_tx_queues];
3296 adapter->rx_scrq[i]->scrq_num = i;
Nathan Fontenot82e3be32018-02-21 21:33:56 -06003297 adapter->num_active_rx_scrqs++;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003298 }
3299
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003300 kfree(allqueues);
3301 return 0;
3302
3303rx_failed:
3304 kfree(adapter->tx_scrq);
3305 adapter->tx_scrq = NULL;
3306tx_failed:
3307 for (i = 0; i < registered_queues; i++)
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06003308 release_sub_crq_queue(adapter, allqueues[i], 1);
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003309 kfree(allqueues);
3310 return -1;
3311}
3312
3313static void ibmvnic_send_req_caps(struct ibmvnic_adapter *adapter, int retry)
3314{
3315 struct device *dev = &adapter->vdev->dev;
3316 union ibmvnic_crq crq;
John Allenc26eba02017-10-26 16:23:25 -05003317 int max_entries;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003318
3319 if (!retry) {
3320 /* Sub-CRQ entries are 32 byte long */
3321 int entries_page = 4 * PAGE_SIZE / (sizeof(u64) * 4);
3322
3323 if (adapter->min_tx_entries_per_subcrq > entries_page ||
3324 adapter->min_rx_add_entries_per_subcrq > entries_page) {
3325 dev_err(dev, "Fatal, invalid entries per sub-crq\n");
3326 return;
3327 }
3328
John Allenc26eba02017-10-26 16:23:25 -05003329 if (adapter->desired.mtu)
3330 adapter->req_mtu = adapter->desired.mtu;
3331 else
3332 adapter->req_mtu = adapter->netdev->mtu + ETH_HLEN;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003333
John Allenc26eba02017-10-26 16:23:25 -05003334 if (!adapter->desired.tx_entries)
3335 adapter->desired.tx_entries =
3336 adapter->max_tx_entries_per_subcrq;
3337 if (!adapter->desired.rx_entries)
3338 adapter->desired.rx_entries =
3339 adapter->max_rx_add_entries_per_subcrq;
3340
3341 max_entries = IBMVNIC_MAX_LTB_SIZE /
3342 (adapter->req_mtu + IBMVNIC_BUFFER_HLEN);
3343
3344 if ((adapter->req_mtu + IBMVNIC_BUFFER_HLEN) *
3345 adapter->desired.tx_entries > IBMVNIC_MAX_LTB_SIZE) {
3346 adapter->desired.tx_entries = max_entries;
3347 }
3348
3349 if ((adapter->req_mtu + IBMVNIC_BUFFER_HLEN) *
3350 adapter->desired.rx_entries > IBMVNIC_MAX_LTB_SIZE) {
3351 adapter->desired.rx_entries = max_entries;
3352 }
3353
3354 if (adapter->desired.tx_entries)
3355 adapter->req_tx_entries_per_subcrq =
3356 adapter->desired.tx_entries;
3357 else
3358 adapter->req_tx_entries_per_subcrq =
3359 adapter->max_tx_entries_per_subcrq;
3360
3361 if (adapter->desired.rx_entries)
3362 adapter->req_rx_add_entries_per_subcrq =
3363 adapter->desired.rx_entries;
3364 else
3365 adapter->req_rx_add_entries_per_subcrq =
3366 adapter->max_rx_add_entries_per_subcrq;
3367
3368 if (adapter->desired.tx_queues)
3369 adapter->req_tx_queues =
3370 adapter->desired.tx_queues;
3371 else
3372 adapter->req_tx_queues =
3373 adapter->opt_tx_comp_sub_queues;
3374
3375 if (adapter->desired.rx_queues)
3376 adapter->req_rx_queues =
3377 adapter->desired.rx_queues;
3378 else
3379 adapter->req_rx_queues =
3380 adapter->opt_rx_comp_queues;
3381
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003382 adapter->req_rx_add_queues = adapter->max_rx_add_queues;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003383 }
3384
Thomas Falcon032c5e82015-12-21 11:26:06 -06003385 memset(&crq, 0, sizeof(crq));
3386 crq.request_capability.first = IBMVNIC_CRQ_CMD;
3387 crq.request_capability.cmd = REQUEST_CAPABILITY;
3388
3389 crq.request_capability.capability = cpu_to_be16(REQ_TX_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06003390 crq.request_capability.number = cpu_to_be64(adapter->req_tx_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06003391 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003392 ibmvnic_send_crq(adapter, &crq);
3393
3394 crq.request_capability.capability = cpu_to_be16(REQ_RX_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06003395 crq.request_capability.number = cpu_to_be64(adapter->req_rx_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06003396 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003397 ibmvnic_send_crq(adapter, &crq);
3398
3399 crq.request_capability.capability = cpu_to_be16(REQ_RX_ADD_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06003400 crq.request_capability.number = cpu_to_be64(adapter->req_rx_add_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06003401 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003402 ibmvnic_send_crq(adapter, &crq);
3403
3404 crq.request_capability.capability =
3405 cpu_to_be16(REQ_TX_ENTRIES_PER_SUBCRQ);
3406 crq.request_capability.number =
Thomas Falconde89e852016-03-01 10:20:09 -06003407 cpu_to_be64(adapter->req_tx_entries_per_subcrq);
Thomas Falcon901e0402017-02-15 12:17:59 -06003408 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003409 ibmvnic_send_crq(adapter, &crq);
3410
3411 crq.request_capability.capability =
3412 cpu_to_be16(REQ_RX_ADD_ENTRIES_PER_SUBCRQ);
3413 crq.request_capability.number =
Thomas Falconde89e852016-03-01 10:20:09 -06003414 cpu_to_be64(adapter->req_rx_add_entries_per_subcrq);
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 crq.request_capability.capability = cpu_to_be16(REQ_MTU);
Thomas Falconde89e852016-03-01 10:20:09 -06003419 crq.request_capability.number = cpu_to_be64(adapter->req_mtu);
Thomas Falcon901e0402017-02-15 12:17:59 -06003420 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003421 ibmvnic_send_crq(adapter, &crq);
3422
3423 if (adapter->netdev->flags & IFF_PROMISC) {
3424 if (adapter->promisc_supported) {
3425 crq.request_capability.capability =
3426 cpu_to_be16(PROMISC_REQUESTED);
Thomas Falconde89e852016-03-01 10:20:09 -06003427 crq.request_capability.number = cpu_to_be64(1);
Thomas Falcon901e0402017-02-15 12:17:59 -06003428 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003429 ibmvnic_send_crq(adapter, &crq);
3430 }
3431 } else {
3432 crq.request_capability.capability =
3433 cpu_to_be16(PROMISC_REQUESTED);
Thomas Falconde89e852016-03-01 10:20:09 -06003434 crq.request_capability.number = cpu_to_be64(0);
Thomas Falcon901e0402017-02-15 12:17:59 -06003435 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003436 ibmvnic_send_crq(adapter, &crq);
3437 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06003438}
3439
3440static int pending_scrq(struct ibmvnic_adapter *adapter,
3441 struct ibmvnic_sub_crq_queue *scrq)
3442{
3443 union sub_crq *entry = &scrq->msgs[scrq->cur];
3444
Thomas Falcon1cf9cc72017-06-14 23:50:08 -05003445 if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003446 return 1;
3447 else
3448 return 0;
3449}
3450
3451static union sub_crq *ibmvnic_next_scrq(struct ibmvnic_adapter *adapter,
3452 struct ibmvnic_sub_crq_queue *scrq)
3453{
3454 union sub_crq *entry;
3455 unsigned long flags;
3456
3457 spin_lock_irqsave(&scrq->lock, flags);
3458 entry = &scrq->msgs[scrq->cur];
3459 if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP) {
3460 if (++scrq->cur == scrq->size)
3461 scrq->cur = 0;
3462 } else {
3463 entry = NULL;
3464 }
3465 spin_unlock_irqrestore(&scrq->lock, flags);
3466
3467 return entry;
3468}
3469
3470static union ibmvnic_crq *ibmvnic_next_crq(struct ibmvnic_adapter *adapter)
3471{
3472 struct ibmvnic_crq_queue *queue = &adapter->crq;
3473 union ibmvnic_crq *crq;
3474
3475 crq = &queue->msgs[queue->cur];
3476 if (crq->generic.first & IBMVNIC_CRQ_CMD_RSP) {
3477 if (++queue->cur == queue->size)
3478 queue->cur = 0;
3479 } else {
3480 crq = NULL;
3481 }
3482
3483 return crq;
3484}
3485
Thomas Falcon2d14d372018-07-13 12:03:32 -05003486static void print_subcrq_error(struct device *dev, int rc, const char *func)
3487{
3488 switch (rc) {
3489 case H_PARAMETER:
3490 dev_warn_ratelimited(dev,
3491 "%s failed: Send request is malformed or adapter failover pending. (rc=%d)\n",
3492 func, rc);
3493 break;
3494 case H_CLOSED:
3495 dev_warn_ratelimited(dev,
3496 "%s failed: Backing queue closed. Adapter is down or failover pending. (rc=%d)\n",
3497 func, rc);
3498 break;
3499 default:
3500 dev_err_ratelimited(dev, "%s failed: (rc=%d)\n", func, rc);
3501 break;
3502 }
3503}
3504
Thomas Falcon032c5e82015-12-21 11:26:06 -06003505static int send_subcrq(struct ibmvnic_adapter *adapter, u64 remote_handle,
3506 union sub_crq *sub_crq)
3507{
3508 unsigned int ua = adapter->vdev->unit_address;
3509 struct device *dev = &adapter->vdev->dev;
3510 u64 *u64_crq = (u64 *)sub_crq;
3511 int rc;
3512
3513 netdev_dbg(adapter->netdev,
3514 "Sending sCRQ %016lx: %016lx %016lx %016lx %016lx\n",
3515 (unsigned long int)cpu_to_be64(remote_handle),
3516 (unsigned long int)cpu_to_be64(u64_crq[0]),
3517 (unsigned long int)cpu_to_be64(u64_crq[1]),
3518 (unsigned long int)cpu_to_be64(u64_crq[2]),
3519 (unsigned long int)cpu_to_be64(u64_crq[3]));
3520
3521 /* Make sure the hypervisor sees the complete request */
3522 mb();
3523
3524 rc = plpar_hcall_norets(H_SEND_SUB_CRQ, ua,
3525 cpu_to_be64(remote_handle),
3526 cpu_to_be64(u64_crq[0]),
3527 cpu_to_be64(u64_crq[1]),
3528 cpu_to_be64(u64_crq[2]),
3529 cpu_to_be64(u64_crq[3]));
3530
Thomas Falcon2d14d372018-07-13 12:03:32 -05003531 if (rc)
3532 print_subcrq_error(dev, rc, __func__);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003533
3534 return rc;
3535}
3536
Thomas Falconad7775d2016-04-01 17:20:34 -05003537static int send_subcrq_indirect(struct ibmvnic_adapter *adapter,
3538 u64 remote_handle, u64 ioba, u64 num_entries)
3539{
3540 unsigned int ua = adapter->vdev->unit_address;
3541 struct device *dev = &adapter->vdev->dev;
3542 int rc;
3543
3544 /* Make sure the hypervisor sees the complete request */
3545 mb();
3546 rc = plpar_hcall_norets(H_SEND_SUB_CRQ_INDIRECT, ua,
3547 cpu_to_be64(remote_handle),
3548 ioba, num_entries);
3549
Thomas Falcon2d14d372018-07-13 12:03:32 -05003550 if (rc)
3551 print_subcrq_error(dev, rc, __func__);
Thomas Falconad7775d2016-04-01 17:20:34 -05003552
3553 return rc;
3554}
3555
Thomas Falcon032c5e82015-12-21 11:26:06 -06003556static int ibmvnic_send_crq(struct ibmvnic_adapter *adapter,
3557 union ibmvnic_crq *crq)
3558{
3559 unsigned int ua = adapter->vdev->unit_address;
3560 struct device *dev = &adapter->vdev->dev;
3561 u64 *u64_crq = (u64 *)crq;
3562 int rc;
3563
3564 netdev_dbg(adapter->netdev, "Sending CRQ: %016lx %016lx\n",
3565 (unsigned long int)cpu_to_be64(u64_crq[0]),
3566 (unsigned long int)cpu_to_be64(u64_crq[1]));
3567
Thomas Falcon51536982018-05-23 13:37:56 -05003568 if (!adapter->crq.active &&
3569 crq->generic.first != IBMVNIC_CRQ_INIT_CMD) {
3570 dev_warn(dev, "Invalid request detected while CRQ is inactive, possible device state change during reset\n");
3571 return -EINVAL;
3572 }
3573
Thomas Falcon032c5e82015-12-21 11:26:06 -06003574 /* Make sure the hypervisor sees the complete request */
3575 mb();
3576
3577 rc = plpar_hcall_norets(H_SEND_CRQ, ua,
3578 cpu_to_be64(u64_crq[0]),
3579 cpu_to_be64(u64_crq[1]));
3580
3581 if (rc) {
Nathan Fontenotec95dff2018-02-07 13:00:24 -06003582 if (rc == H_CLOSED) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06003583 dev_warn(dev, "CRQ Queue closed\n");
Juliet Kim7ed5b312019-09-20 16:11:23 -04003584 if (test_bit(0, &adapter->resetting))
Nathan Fontenotec95dff2018-02-07 13:00:24 -06003585 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
3586 }
3587
Thomas Falcon032c5e82015-12-21 11:26:06 -06003588 dev_warn(dev, "Send error (rc=%d)\n", rc);
3589 }
3590
3591 return rc;
3592}
3593
3594static int ibmvnic_send_crq_init(struct ibmvnic_adapter *adapter)
3595{
3596 union ibmvnic_crq crq;
3597
3598 memset(&crq, 0, sizeof(crq));
3599 crq.generic.first = IBMVNIC_CRQ_INIT_CMD;
3600 crq.generic.cmd = IBMVNIC_CRQ_INIT;
3601 netdev_dbg(adapter->netdev, "Sending CRQ init\n");
3602
3603 return ibmvnic_send_crq(adapter, &crq);
3604}
3605
Thomas Falcon032c5e82015-12-21 11:26:06 -06003606static int send_version_xchg(struct ibmvnic_adapter *adapter)
3607{
3608 union ibmvnic_crq crq;
3609
3610 memset(&crq, 0, sizeof(crq));
3611 crq.version_exchange.first = IBMVNIC_CRQ_CMD;
3612 crq.version_exchange.cmd = VERSION_EXCHANGE;
3613 crq.version_exchange.version = cpu_to_be16(ibmvnic_version);
3614
3615 return ibmvnic_send_crq(adapter, &crq);
3616}
3617
Nathan Fontenot37798d02017-11-08 11:23:56 -06003618struct vnic_login_client_data {
3619 u8 type;
3620 __be16 len;
Kees Cook08ea5562018-04-10 15:26:43 -07003621 char name[];
Nathan Fontenot37798d02017-11-08 11:23:56 -06003622} __packed;
3623
3624static int vnic_client_data_len(struct ibmvnic_adapter *adapter)
3625{
3626 int len;
3627
3628 /* Calculate the amount of buffer space needed for the
3629 * vnic client data in the login buffer. There are four entries,
3630 * OS name, LPAR name, device name, and a null last entry.
3631 */
3632 len = 4 * sizeof(struct vnic_login_client_data);
3633 len += 6; /* "Linux" plus NULL */
3634 len += strlen(utsname()->nodename) + 1;
3635 len += strlen(adapter->netdev->name) + 1;
3636
3637 return len;
3638}
3639
3640static void vnic_add_client_data(struct ibmvnic_adapter *adapter,
3641 struct vnic_login_client_data *vlcd)
3642{
3643 const char *os_name = "Linux";
3644 int len;
3645
3646 /* Type 1 - LPAR OS */
3647 vlcd->type = 1;
3648 len = strlen(os_name) + 1;
3649 vlcd->len = cpu_to_be16(len);
Kees Cook08ea5562018-04-10 15:26:43 -07003650 strncpy(vlcd->name, os_name, len);
3651 vlcd = (struct vnic_login_client_data *)(vlcd->name + len);
Nathan Fontenot37798d02017-11-08 11:23:56 -06003652
3653 /* Type 2 - LPAR name */
3654 vlcd->type = 2;
3655 len = strlen(utsname()->nodename) + 1;
3656 vlcd->len = cpu_to_be16(len);
Kees Cook08ea5562018-04-10 15:26:43 -07003657 strncpy(vlcd->name, utsname()->nodename, len);
3658 vlcd = (struct vnic_login_client_data *)(vlcd->name + len);
Nathan Fontenot37798d02017-11-08 11:23:56 -06003659
3660 /* Type 3 - device name */
3661 vlcd->type = 3;
3662 len = strlen(adapter->netdev->name) + 1;
3663 vlcd->len = cpu_to_be16(len);
Kees Cook08ea5562018-04-10 15:26:43 -07003664 strncpy(vlcd->name, adapter->netdev->name, len);
Nathan Fontenot37798d02017-11-08 11:23:56 -06003665}
3666
Thomas Falcon20a8ab72018-02-26 18:10:59 -06003667static int send_login(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003668{
3669 struct ibmvnic_login_rsp_buffer *login_rsp_buffer;
3670 struct ibmvnic_login_buffer *login_buffer;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003671 struct device *dev = &adapter->vdev->dev;
3672 dma_addr_t rsp_buffer_token;
3673 dma_addr_t buffer_token;
3674 size_t rsp_buffer_size;
3675 union ibmvnic_crq crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003676 size_t buffer_size;
3677 __be64 *tx_list_p;
3678 __be64 *rx_list_p;
Nathan Fontenot37798d02017-11-08 11:23:56 -06003679 int client_data_len;
3680 struct vnic_login_client_data *vlcd;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003681 int i;
3682
Thomas Falcon20a8ab72018-02-26 18:10:59 -06003683 if (!adapter->tx_scrq || !adapter->rx_scrq) {
3684 netdev_err(adapter->netdev,
3685 "RX or TX queues are not allocated, device login failed\n");
3686 return -1;
3687 }
3688
Thomas Falcon34f0f4e2018-02-13 18:23:40 -06003689 release_login_rsp_buffer(adapter);
Nathan Fontenot37798d02017-11-08 11:23:56 -06003690 client_data_len = vnic_client_data_len(adapter);
3691
Thomas Falcon032c5e82015-12-21 11:26:06 -06003692 buffer_size =
3693 sizeof(struct ibmvnic_login_buffer) +
Nathan Fontenot37798d02017-11-08 11:23:56 -06003694 sizeof(u64) * (adapter->req_tx_queues + adapter->req_rx_queues) +
3695 client_data_len;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003696
Nathan Fontenot37798d02017-11-08 11:23:56 -06003697 login_buffer = kzalloc(buffer_size, GFP_ATOMIC);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003698 if (!login_buffer)
3699 goto buf_alloc_failed;
3700
3701 buffer_token = dma_map_single(dev, login_buffer, buffer_size,
3702 DMA_TO_DEVICE);
3703 if (dma_mapping_error(dev, buffer_token)) {
3704 dev_err(dev, "Couldn't map login buffer\n");
3705 goto buf_map_failed;
3706 }
3707
John Allen498cd8e2016-04-06 11:49:55 -05003708 rsp_buffer_size = sizeof(struct ibmvnic_login_rsp_buffer) +
3709 sizeof(u64) * adapter->req_tx_queues +
3710 sizeof(u64) * adapter->req_rx_queues +
3711 sizeof(u64) * adapter->req_rx_queues +
3712 sizeof(u8) * IBMVNIC_TX_DESC_VERSIONS;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003713
3714 login_rsp_buffer = kmalloc(rsp_buffer_size, GFP_ATOMIC);
3715 if (!login_rsp_buffer)
3716 goto buf_rsp_alloc_failed;
3717
3718 rsp_buffer_token = dma_map_single(dev, login_rsp_buffer,
3719 rsp_buffer_size, DMA_FROM_DEVICE);
3720 if (dma_mapping_error(dev, rsp_buffer_token)) {
3721 dev_err(dev, "Couldn't map login rsp buffer\n");
3722 goto buf_rsp_map_failed;
3723 }
Nathan Fontenot661a2622017-04-19 13:44:58 -04003724
Thomas Falcon032c5e82015-12-21 11:26:06 -06003725 adapter->login_buf = login_buffer;
3726 adapter->login_buf_token = buffer_token;
3727 adapter->login_buf_sz = buffer_size;
3728 adapter->login_rsp_buf = login_rsp_buffer;
3729 adapter->login_rsp_buf_token = rsp_buffer_token;
3730 adapter->login_rsp_buf_sz = rsp_buffer_size;
3731
3732 login_buffer->len = cpu_to_be32(buffer_size);
3733 login_buffer->version = cpu_to_be32(INITIAL_VERSION_LB);
3734 login_buffer->num_txcomp_subcrqs = cpu_to_be32(adapter->req_tx_queues);
3735 login_buffer->off_txcomp_subcrqs =
3736 cpu_to_be32(sizeof(struct ibmvnic_login_buffer));
3737 login_buffer->num_rxcomp_subcrqs = cpu_to_be32(adapter->req_rx_queues);
3738 login_buffer->off_rxcomp_subcrqs =
3739 cpu_to_be32(sizeof(struct ibmvnic_login_buffer) +
3740 sizeof(u64) * adapter->req_tx_queues);
3741 login_buffer->login_rsp_ioba = cpu_to_be32(rsp_buffer_token);
3742 login_buffer->login_rsp_len = cpu_to_be32(rsp_buffer_size);
3743
3744 tx_list_p = (__be64 *)((char *)login_buffer +
3745 sizeof(struct ibmvnic_login_buffer));
3746 rx_list_p = (__be64 *)((char *)login_buffer +
3747 sizeof(struct ibmvnic_login_buffer) +
3748 sizeof(u64) * adapter->req_tx_queues);
3749
3750 for (i = 0; i < adapter->req_tx_queues; i++) {
3751 if (adapter->tx_scrq[i]) {
3752 tx_list_p[i] = cpu_to_be64(adapter->tx_scrq[i]->
3753 crq_num);
3754 }
3755 }
3756
3757 for (i = 0; i < adapter->req_rx_queues; i++) {
3758 if (adapter->rx_scrq[i]) {
3759 rx_list_p[i] = cpu_to_be64(adapter->rx_scrq[i]->
3760 crq_num);
3761 }
3762 }
3763
Nathan Fontenot37798d02017-11-08 11:23:56 -06003764 /* Insert vNIC login client data */
3765 vlcd = (struct vnic_login_client_data *)
3766 ((char *)rx_list_p + (sizeof(u64) * adapter->req_rx_queues));
3767 login_buffer->client_data_offset =
3768 cpu_to_be32((char *)vlcd - (char *)login_buffer);
3769 login_buffer->client_data_len = cpu_to_be32(client_data_len);
3770
3771 vnic_add_client_data(adapter, vlcd);
3772
Thomas Falcon032c5e82015-12-21 11:26:06 -06003773 netdev_dbg(adapter->netdev, "Login Buffer:\n");
3774 for (i = 0; i < (adapter->login_buf_sz - 1) / 8 + 1; i++) {
3775 netdev_dbg(adapter->netdev, "%016lx\n",
3776 ((unsigned long int *)(adapter->login_buf))[i]);
3777 }
3778
3779 memset(&crq, 0, sizeof(crq));
3780 crq.login.first = IBMVNIC_CRQ_CMD;
3781 crq.login.cmd = LOGIN;
3782 crq.login.ioba = cpu_to_be32(buffer_token);
3783 crq.login.len = cpu_to_be32(buffer_size);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003784 ibmvnic_send_crq(adapter, &crq);
3785
Thomas Falcon20a8ab72018-02-26 18:10:59 -06003786 return 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003787
Thomas Falcon032c5e82015-12-21 11:26:06 -06003788buf_rsp_map_failed:
3789 kfree(login_rsp_buffer);
3790buf_rsp_alloc_failed:
3791 dma_unmap_single(dev, buffer_token, buffer_size, DMA_TO_DEVICE);
3792buf_map_failed:
3793 kfree(login_buffer);
3794buf_alloc_failed:
Thomas Falcon20a8ab72018-02-26 18:10:59 -06003795 return -1;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003796}
3797
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05003798static int send_request_map(struct ibmvnic_adapter *adapter, dma_addr_t addr,
3799 u32 len, u8 map_id)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003800{
3801 union ibmvnic_crq crq;
3802
3803 memset(&crq, 0, sizeof(crq));
3804 crq.request_map.first = IBMVNIC_CRQ_CMD;
3805 crq.request_map.cmd = REQUEST_MAP;
3806 crq.request_map.map_id = map_id;
3807 crq.request_map.ioba = cpu_to_be32(addr);
3808 crq.request_map.len = cpu_to_be32(len);
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05003809 return ibmvnic_send_crq(adapter, &crq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003810}
3811
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05003812static int send_request_unmap(struct ibmvnic_adapter *adapter, u8 map_id)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003813{
3814 union ibmvnic_crq crq;
3815
3816 memset(&crq, 0, sizeof(crq));
3817 crq.request_unmap.first = IBMVNIC_CRQ_CMD;
3818 crq.request_unmap.cmd = REQUEST_UNMAP;
3819 crq.request_unmap.map_id = map_id;
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05003820 return ibmvnic_send_crq(adapter, &crq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003821}
3822
3823static void send_map_query(struct ibmvnic_adapter *adapter)
3824{
3825 union ibmvnic_crq crq;
3826
3827 memset(&crq, 0, sizeof(crq));
3828 crq.query_map.first = IBMVNIC_CRQ_CMD;
3829 crq.query_map.cmd = QUERY_MAP;
3830 ibmvnic_send_crq(adapter, &crq);
3831}
3832
3833/* Send a series of CRQs requesting various capabilities of the VNIC server */
3834static void send_cap_queries(struct ibmvnic_adapter *adapter)
3835{
3836 union ibmvnic_crq crq;
3837
Thomas Falcon901e0402017-02-15 12:17:59 -06003838 atomic_set(&adapter->running_cap_crqs, 0);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003839 memset(&crq, 0, sizeof(crq));
3840 crq.query_capability.first = IBMVNIC_CRQ_CMD;
3841 crq.query_capability.cmd = QUERY_CAPABILITY;
3842
3843 crq.query_capability.capability = cpu_to_be16(MIN_TX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003844 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003845 ibmvnic_send_crq(adapter, &crq);
3846
3847 crq.query_capability.capability = cpu_to_be16(MIN_RX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003848 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003849 ibmvnic_send_crq(adapter, &crq);
3850
3851 crq.query_capability.capability = cpu_to_be16(MIN_RX_ADD_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003852 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003853 ibmvnic_send_crq(adapter, &crq);
3854
3855 crq.query_capability.capability = cpu_to_be16(MAX_TX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003856 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003857 ibmvnic_send_crq(adapter, &crq);
3858
3859 crq.query_capability.capability = cpu_to_be16(MAX_RX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003860 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003861 ibmvnic_send_crq(adapter, &crq);
3862
3863 crq.query_capability.capability = cpu_to_be16(MAX_RX_ADD_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003864 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003865 ibmvnic_send_crq(adapter, &crq);
3866
3867 crq.query_capability.capability =
3868 cpu_to_be16(MIN_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06003869 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003870 ibmvnic_send_crq(adapter, &crq);
3871
3872 crq.query_capability.capability =
3873 cpu_to_be16(MIN_RX_ADD_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06003874 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003875 ibmvnic_send_crq(adapter, &crq);
3876
3877 crq.query_capability.capability =
3878 cpu_to_be16(MAX_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06003879 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003880 ibmvnic_send_crq(adapter, &crq);
3881
3882 crq.query_capability.capability =
3883 cpu_to_be16(MAX_RX_ADD_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06003884 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003885 ibmvnic_send_crq(adapter, &crq);
3886
3887 crq.query_capability.capability = cpu_to_be16(TCP_IP_OFFLOAD);
Thomas Falcon901e0402017-02-15 12:17:59 -06003888 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003889 ibmvnic_send_crq(adapter, &crq);
3890
3891 crq.query_capability.capability = cpu_to_be16(PROMISC_SUPPORTED);
Thomas Falcon901e0402017-02-15 12:17:59 -06003892 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003893 ibmvnic_send_crq(adapter, &crq);
3894
3895 crq.query_capability.capability = cpu_to_be16(MIN_MTU);
Thomas Falcon901e0402017-02-15 12:17:59 -06003896 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003897 ibmvnic_send_crq(adapter, &crq);
3898
3899 crq.query_capability.capability = cpu_to_be16(MAX_MTU);
Thomas Falcon901e0402017-02-15 12:17:59 -06003900 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003901 ibmvnic_send_crq(adapter, &crq);
3902
3903 crq.query_capability.capability = cpu_to_be16(MAX_MULTICAST_FILTERS);
Thomas Falcon901e0402017-02-15 12:17:59 -06003904 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003905 ibmvnic_send_crq(adapter, &crq);
3906
3907 crq.query_capability.capability = cpu_to_be16(VLAN_HEADER_INSERTION);
Thomas Falcon901e0402017-02-15 12:17:59 -06003908 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003909 ibmvnic_send_crq(adapter, &crq);
3910
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04003911 crq.query_capability.capability = cpu_to_be16(RX_VLAN_HEADER_INSERTION);
3912 atomic_inc(&adapter->running_cap_crqs);
3913 ibmvnic_send_crq(adapter, &crq);
3914
Thomas Falcon032c5e82015-12-21 11:26:06 -06003915 crq.query_capability.capability = cpu_to_be16(MAX_TX_SG_ENTRIES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003916 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003917 ibmvnic_send_crq(adapter, &crq);
3918
3919 crq.query_capability.capability = cpu_to_be16(RX_SG_SUPPORTED);
Thomas Falcon901e0402017-02-15 12:17:59 -06003920 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003921 ibmvnic_send_crq(adapter, &crq);
3922
3923 crq.query_capability.capability = cpu_to_be16(OPT_TX_COMP_SUB_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003924 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003925 ibmvnic_send_crq(adapter, &crq);
3926
3927 crq.query_capability.capability = cpu_to_be16(OPT_RX_COMP_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003928 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003929 ibmvnic_send_crq(adapter, &crq);
3930
3931 crq.query_capability.capability =
3932 cpu_to_be16(OPT_RX_BUFADD_Q_PER_RX_COMP_Q);
Thomas Falcon901e0402017-02-15 12:17:59 -06003933 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003934 ibmvnic_send_crq(adapter, &crq);
3935
3936 crq.query_capability.capability =
3937 cpu_to_be16(OPT_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06003938 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003939 ibmvnic_send_crq(adapter, &crq);
3940
3941 crq.query_capability.capability =
3942 cpu_to_be16(OPT_RXBA_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06003943 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003944 ibmvnic_send_crq(adapter, &crq);
3945
3946 crq.query_capability.capability = cpu_to_be16(TX_RX_DESC_REQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06003947 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003948 ibmvnic_send_crq(adapter, &crq);
3949}
3950
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02003951static void handle_vpd_size_rsp(union ibmvnic_crq *crq,
3952 struct ibmvnic_adapter *adapter)
3953{
3954 struct device *dev = &adapter->vdev->dev;
3955
3956 if (crq->get_vpd_size_rsp.rc.code) {
3957 dev_err(dev, "Error retrieving VPD size, rc=%x\n",
3958 crq->get_vpd_size_rsp.rc.code);
3959 complete(&adapter->fw_done);
3960 return;
3961 }
3962
3963 adapter->vpd->len = be64_to_cpu(crq->get_vpd_size_rsp.len);
3964 complete(&adapter->fw_done);
3965}
3966
3967static void handle_vpd_rsp(union ibmvnic_crq *crq,
3968 struct ibmvnic_adapter *adapter)
3969{
3970 struct device *dev = &adapter->vdev->dev;
Desnes Augusto Nunes do Rosario21a25452018-02-05 14:33:55 -02003971 unsigned char *substr = NULL;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02003972 u8 fw_level_len = 0;
3973
3974 memset(adapter->fw_version, 0, 32);
3975
3976 dma_unmap_single(dev, adapter->vpd->dma_addr, adapter->vpd->len,
3977 DMA_FROM_DEVICE);
3978
3979 if (crq->get_vpd_rsp.rc.code) {
3980 dev_err(dev, "Error retrieving VPD from device, rc=%x\n",
3981 crq->get_vpd_rsp.rc.code);
3982 goto complete;
3983 }
3984
3985 /* get the position of the firmware version info
3986 * located after the ASCII 'RM' substring in the buffer
3987 */
3988 substr = strnstr(adapter->vpd->buff, "RM", adapter->vpd->len);
3989 if (!substr) {
Desnes Augusto Nunes do Rosarioa1073112018-02-01 16:04:30 -02003990 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 -02003991 goto complete;
3992 }
3993
3994 /* get length of firmware level ASCII substring */
3995 if ((substr + 2) < (adapter->vpd->buff + adapter->vpd->len)) {
3996 fw_level_len = *(substr + 2);
3997 } else {
3998 dev_info(dev, "Length of FW substr extrapolated VDP buff\n");
3999 goto complete;
4000 }
4001
4002 /* copy firmware version string from vpd into adapter */
4003 if ((substr + 3 + fw_level_len) <
4004 (adapter->vpd->buff + adapter->vpd->len)) {
Desnes Augusto Nunes do Rosario21a25452018-02-05 14:33:55 -02004005 strncpy((char *)adapter->fw_version, substr + 3, fw_level_len);
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02004006 } else {
4007 dev_info(dev, "FW substr extrapolated VPD buff\n");
4008 }
4009
4010complete:
Desnes Augusto Nunes do Rosario21a25452018-02-05 14:33:55 -02004011 if (adapter->fw_version[0] == '\0')
4012 strncpy((char *)adapter->fw_version, "N/A", 3 * sizeof(char));
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02004013 complete(&adapter->fw_done);
4014}
4015
Thomas Falcon032c5e82015-12-21 11:26:06 -06004016static void handle_query_ip_offload_rsp(struct ibmvnic_adapter *adapter)
4017{
4018 struct device *dev = &adapter->vdev->dev;
4019 struct ibmvnic_query_ip_offload_buffer *buf = &adapter->ip_offload_buf;
Thomas Falcondde746a2019-04-10 11:07:00 -05004020 netdev_features_t old_hw_features = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004021 union ibmvnic_crq crq;
4022 int i;
4023
4024 dma_unmap_single(dev, adapter->ip_offload_tok,
4025 sizeof(adapter->ip_offload_buf), DMA_FROM_DEVICE);
4026
4027 netdev_dbg(adapter->netdev, "Query IP Offload Buffer:\n");
4028 for (i = 0; i < (sizeof(adapter->ip_offload_buf) - 1) / 8 + 1; i++)
4029 netdev_dbg(adapter->netdev, "%016lx\n",
4030 ((unsigned long int *)(buf))[i]);
4031
4032 netdev_dbg(adapter->netdev, "ipv4_chksum = %d\n", buf->ipv4_chksum);
4033 netdev_dbg(adapter->netdev, "ipv6_chksum = %d\n", buf->ipv6_chksum);
4034 netdev_dbg(adapter->netdev, "tcp_ipv4_chksum = %d\n",
4035 buf->tcp_ipv4_chksum);
4036 netdev_dbg(adapter->netdev, "tcp_ipv6_chksum = %d\n",
4037 buf->tcp_ipv6_chksum);
4038 netdev_dbg(adapter->netdev, "udp_ipv4_chksum = %d\n",
4039 buf->udp_ipv4_chksum);
4040 netdev_dbg(adapter->netdev, "udp_ipv6_chksum = %d\n",
4041 buf->udp_ipv6_chksum);
4042 netdev_dbg(adapter->netdev, "large_tx_ipv4 = %d\n",
4043 buf->large_tx_ipv4);
4044 netdev_dbg(adapter->netdev, "large_tx_ipv6 = %d\n",
4045 buf->large_tx_ipv6);
4046 netdev_dbg(adapter->netdev, "large_rx_ipv4 = %d\n",
4047 buf->large_rx_ipv4);
4048 netdev_dbg(adapter->netdev, "large_rx_ipv6 = %d\n",
4049 buf->large_rx_ipv6);
4050 netdev_dbg(adapter->netdev, "max_ipv4_hdr_sz = %d\n",
4051 buf->max_ipv4_header_size);
4052 netdev_dbg(adapter->netdev, "max_ipv6_hdr_sz = %d\n",
4053 buf->max_ipv6_header_size);
4054 netdev_dbg(adapter->netdev, "max_tcp_hdr_size = %d\n",
4055 buf->max_tcp_header_size);
4056 netdev_dbg(adapter->netdev, "max_udp_hdr_size = %d\n",
4057 buf->max_udp_header_size);
4058 netdev_dbg(adapter->netdev, "max_large_tx_size = %d\n",
4059 buf->max_large_tx_size);
4060 netdev_dbg(adapter->netdev, "max_large_rx_size = %d\n",
4061 buf->max_large_rx_size);
4062 netdev_dbg(adapter->netdev, "ipv6_ext_hdr = %d\n",
4063 buf->ipv6_extension_header);
4064 netdev_dbg(adapter->netdev, "tcp_pseudosum_req = %d\n",
4065 buf->tcp_pseudosum_req);
4066 netdev_dbg(adapter->netdev, "num_ipv6_ext_hd = %d\n",
4067 buf->num_ipv6_ext_headers);
4068 netdev_dbg(adapter->netdev, "off_ipv6_ext_hd = %d\n",
4069 buf->off_ipv6_ext_headers);
4070
4071 adapter->ip_offload_ctrl_tok =
4072 dma_map_single(dev, &adapter->ip_offload_ctrl,
4073 sizeof(adapter->ip_offload_ctrl), DMA_TO_DEVICE);
4074
4075 if (dma_mapping_error(dev, adapter->ip_offload_ctrl_tok)) {
4076 dev_err(dev, "Couldn't map ip offload control buffer\n");
4077 return;
4078 }
4079
Thomas Falconf6897942018-01-18 19:05:01 -06004080 adapter->ip_offload_ctrl.len =
4081 cpu_to_be32(sizeof(adapter->ip_offload_ctrl));
Thomas Falcon032c5e82015-12-21 11:26:06 -06004082 adapter->ip_offload_ctrl.version = cpu_to_be32(INITIAL_VERSION_IOB);
Thomas Falconf6897942018-01-18 19:05:01 -06004083 adapter->ip_offload_ctrl.ipv4_chksum = buf->ipv4_chksum;
4084 adapter->ip_offload_ctrl.ipv6_chksum = buf->ipv6_chksum;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004085 adapter->ip_offload_ctrl.tcp_ipv4_chksum = buf->tcp_ipv4_chksum;
4086 adapter->ip_offload_ctrl.udp_ipv4_chksum = buf->udp_ipv4_chksum;
4087 adapter->ip_offload_ctrl.tcp_ipv6_chksum = buf->tcp_ipv6_chksum;
4088 adapter->ip_offload_ctrl.udp_ipv6_chksum = buf->udp_ipv6_chksum;
Thomas Falconfdb06102017-10-17 12:36:55 -05004089 adapter->ip_offload_ctrl.large_tx_ipv4 = buf->large_tx_ipv4;
4090 adapter->ip_offload_ctrl.large_tx_ipv6 = buf->large_tx_ipv6;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004091
Thomas Falconfdb06102017-10-17 12:36:55 -05004092 /* large_rx disabled for now, additional features needed */
Thomas Falcon032c5e82015-12-21 11:26:06 -06004093 adapter->ip_offload_ctrl.large_rx_ipv4 = 0;
4094 adapter->ip_offload_ctrl.large_rx_ipv6 = 0;
4095
Thomas Falcondde746a2019-04-10 11:07:00 -05004096 if (adapter->state != VNIC_PROBING) {
4097 old_hw_features = adapter->netdev->hw_features;
4098 adapter->netdev->hw_features = 0;
4099 }
4100
Thomas Falconb66b7bd2019-04-10 11:06:59 -05004101 adapter->netdev->hw_features = NETIF_F_SG | NETIF_F_GSO | NETIF_F_GRO;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004102
4103 if (buf->tcp_ipv4_chksum || buf->udp_ipv4_chksum)
Thomas Falcondde746a2019-04-10 11:07:00 -05004104 adapter->netdev->hw_features |= NETIF_F_IP_CSUM;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004105
4106 if (buf->tcp_ipv6_chksum || buf->udp_ipv6_chksum)
Thomas Falcondde746a2019-04-10 11:07:00 -05004107 adapter->netdev->hw_features |= NETIF_F_IPV6_CSUM;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004108
Thomas Falcon9be02cd2016-04-01 17:20:35 -05004109 if ((adapter->netdev->features &
4110 (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)))
Thomas Falcondde746a2019-04-10 11:07:00 -05004111 adapter->netdev->hw_features |= NETIF_F_RXCSUM;
Thomas Falcon9be02cd2016-04-01 17:20:35 -05004112
Thomas Falconfdb06102017-10-17 12:36:55 -05004113 if (buf->large_tx_ipv4)
Thomas Falcondde746a2019-04-10 11:07:00 -05004114 adapter->netdev->hw_features |= NETIF_F_TSO;
Thomas Falconfdb06102017-10-17 12:36:55 -05004115 if (buf->large_tx_ipv6)
Thomas Falcondde746a2019-04-10 11:07:00 -05004116 adapter->netdev->hw_features |= NETIF_F_TSO6;
Thomas Falconfdb06102017-10-17 12:36:55 -05004117
Thomas Falcondde746a2019-04-10 11:07:00 -05004118 if (adapter->state == VNIC_PROBING) {
4119 adapter->netdev->features |= adapter->netdev->hw_features;
4120 } else if (old_hw_features != adapter->netdev->hw_features) {
4121 netdev_features_t tmp = 0;
4122
4123 /* disable features no longer supported */
4124 adapter->netdev->features &= adapter->netdev->hw_features;
4125 /* turn on features now supported if previously enabled */
4126 tmp = (old_hw_features ^ adapter->netdev->hw_features) &
4127 adapter->netdev->hw_features;
4128 adapter->netdev->features |=
4129 tmp & adapter->netdev->wanted_features;
4130 }
Thomas Falconaa0bf852017-10-17 12:36:56 -05004131
Thomas Falcon032c5e82015-12-21 11:26:06 -06004132 memset(&crq, 0, sizeof(crq));
4133 crq.control_ip_offload.first = IBMVNIC_CRQ_CMD;
4134 crq.control_ip_offload.cmd = CONTROL_IP_OFFLOAD;
4135 crq.control_ip_offload.len =
4136 cpu_to_be32(sizeof(adapter->ip_offload_ctrl));
4137 crq.control_ip_offload.ioba = cpu_to_be32(adapter->ip_offload_ctrl_tok);
4138 ibmvnic_send_crq(adapter, &crq);
4139}
4140
Thomas Falconc9008d32018-08-06 21:39:59 -05004141static const char *ibmvnic_fw_err_cause(u16 cause)
4142{
4143 switch (cause) {
4144 case ADAPTER_PROBLEM:
4145 return "adapter problem";
4146 case BUS_PROBLEM:
4147 return "bus problem";
4148 case FW_PROBLEM:
4149 return "firmware problem";
4150 case DD_PROBLEM:
4151 return "device driver problem";
4152 case EEH_RECOVERY:
4153 return "EEH recovery";
4154 case FW_UPDATED:
4155 return "firmware updated";
4156 case LOW_MEMORY:
4157 return "low Memory";
4158 default:
4159 return "unknown";
4160 }
4161}
4162
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04004163static void handle_error_indication(union ibmvnic_crq *crq,
4164 struct ibmvnic_adapter *adapter)
4165{
4166 struct device *dev = &adapter->vdev->dev;
Thomas Falconc9008d32018-08-06 21:39:59 -05004167 u16 cause;
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04004168
Thomas Falconc9008d32018-08-06 21:39:59 -05004169 cause = be16_to_cpu(crq->error_indication.error_cause);
4170
4171 dev_warn_ratelimited(dev,
4172 "Firmware reports %serror, cause: %s. Starting recovery...\n",
4173 crq->error_indication.flags
4174 & IBMVNIC_FATAL_ERROR ? "FATAL " : "",
4175 ibmvnic_fw_err_cause(cause));
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04004176
Nathan Fontenoted651a12017-05-03 14:04:38 -04004177 if (crq->error_indication.flags & IBMVNIC_FATAL_ERROR)
4178 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
John Allen8cb31cf2017-05-26 10:30:37 -04004179 else
4180 ibmvnic_reset(adapter, VNIC_RESET_NON_FATAL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004181}
4182
Thomas Falconf8136142018-01-29 13:45:05 -06004183static int handle_change_mac_rsp(union ibmvnic_crq *crq,
4184 struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06004185{
4186 struct net_device *netdev = adapter->netdev;
4187 struct device *dev = &adapter->vdev->dev;
4188 long rc;
4189
4190 rc = crq->change_mac_addr_rsp.rc.code;
4191 if (rc) {
4192 dev_err(dev, "Error %ld in CHANGE_MAC_ADDR_RSP\n", rc);
Thomas Falconf8136142018-01-29 13:45:05 -06004193 goto out;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004194 }
Thomas Falcon62740e92019-05-09 23:13:43 -05004195 ether_addr_copy(netdev->dev_addr,
4196 &crq->change_mac_addr_rsp.mac_addr[0]);
Thomas Falconf8136142018-01-29 13:45:05 -06004197out:
4198 complete(&adapter->fw_done);
4199 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004200}
4201
4202static void handle_request_cap_rsp(union ibmvnic_crq *crq,
4203 struct ibmvnic_adapter *adapter)
4204{
4205 struct device *dev = &adapter->vdev->dev;
4206 u64 *req_value;
4207 char *name;
4208
Thomas Falcon901e0402017-02-15 12:17:59 -06004209 atomic_dec(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004210 switch (be16_to_cpu(crq->request_capability_rsp.capability)) {
4211 case REQ_TX_QUEUES:
4212 req_value = &adapter->req_tx_queues;
4213 name = "tx";
4214 break;
4215 case REQ_RX_QUEUES:
4216 req_value = &adapter->req_rx_queues;
4217 name = "rx";
4218 break;
4219 case REQ_RX_ADD_QUEUES:
4220 req_value = &adapter->req_rx_add_queues;
4221 name = "rx_add";
4222 break;
4223 case REQ_TX_ENTRIES_PER_SUBCRQ:
4224 req_value = &adapter->req_tx_entries_per_subcrq;
4225 name = "tx_entries_per_subcrq";
4226 break;
4227 case REQ_RX_ADD_ENTRIES_PER_SUBCRQ:
4228 req_value = &adapter->req_rx_add_entries_per_subcrq;
4229 name = "rx_add_entries_per_subcrq";
4230 break;
4231 case REQ_MTU:
4232 req_value = &adapter->req_mtu;
4233 name = "mtu";
4234 break;
4235 case PROMISC_REQUESTED:
4236 req_value = &adapter->promisc;
4237 name = "promisc";
4238 break;
4239 default:
4240 dev_err(dev, "Got invalid cap request rsp %d\n",
4241 crq->request_capability.capability);
4242 return;
4243 }
4244
4245 switch (crq->request_capability_rsp.rc.code) {
4246 case SUCCESS:
4247 break;
4248 case PARTIALSUCCESS:
4249 dev_info(dev, "req=%lld, rsp=%ld in %s queue, retrying.\n",
4250 *req_value,
Thomas Falcon28f4d162017-02-15 10:32:11 -06004251 (long int)be64_to_cpu(crq->request_capability_rsp.
Thomas Falcon032c5e82015-12-21 11:26:06 -06004252 number), name);
John Allene7913802018-01-18 16:27:12 -06004253
4254 if (be16_to_cpu(crq->request_capability_rsp.capability) ==
4255 REQ_MTU) {
4256 pr_err("mtu of %llu is not supported. Reverting.\n",
4257 *req_value);
4258 *req_value = adapter->fallback.mtu;
4259 } else {
4260 *req_value =
4261 be64_to_cpu(crq->request_capability_rsp.number);
4262 }
4263
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04004264 ibmvnic_send_req_caps(adapter, 1);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004265 return;
4266 default:
4267 dev_err(dev, "Error %d in request cap rsp\n",
4268 crq->request_capability_rsp.rc.code);
4269 return;
4270 }
4271
4272 /* Done receiving requested capabilities, query IP offload support */
Thomas Falcon901e0402017-02-15 12:17:59 -06004273 if (atomic_read(&adapter->running_cap_crqs) == 0) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06004274 union ibmvnic_crq newcrq;
4275 int buf_sz = sizeof(struct ibmvnic_query_ip_offload_buffer);
4276 struct ibmvnic_query_ip_offload_buffer *ip_offload_buf =
4277 &adapter->ip_offload_buf;
4278
Thomas Falcon249168a2017-02-15 12:18:00 -06004279 adapter->wait_capability = false;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004280 adapter->ip_offload_tok = dma_map_single(dev, ip_offload_buf,
4281 buf_sz,
4282 DMA_FROM_DEVICE);
4283
4284 if (dma_mapping_error(dev, adapter->ip_offload_tok)) {
4285 if (!firmware_has_feature(FW_FEATURE_CMO))
4286 dev_err(dev, "Couldn't map offload buffer\n");
4287 return;
4288 }
4289
4290 memset(&newcrq, 0, sizeof(newcrq));
4291 newcrq.query_ip_offload.first = IBMVNIC_CRQ_CMD;
4292 newcrq.query_ip_offload.cmd = QUERY_IP_OFFLOAD;
4293 newcrq.query_ip_offload.len = cpu_to_be32(buf_sz);
4294 newcrq.query_ip_offload.ioba =
4295 cpu_to_be32(adapter->ip_offload_tok);
4296
4297 ibmvnic_send_crq(adapter, &newcrq);
4298 }
4299}
4300
4301static int handle_login_rsp(union ibmvnic_crq *login_rsp_crq,
4302 struct ibmvnic_adapter *adapter)
4303{
4304 struct device *dev = &adapter->vdev->dev;
John Allenc26eba02017-10-26 16:23:25 -05004305 struct net_device *netdev = adapter->netdev;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004306 struct ibmvnic_login_rsp_buffer *login_rsp = adapter->login_rsp_buf;
4307 struct ibmvnic_login_buffer *login = adapter->login_buf;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004308 int i;
4309
4310 dma_unmap_single(dev, adapter->login_buf_token, adapter->login_buf_sz,
Thomas Falcon37e40fa2018-04-06 18:37:02 -05004311 DMA_TO_DEVICE);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004312 dma_unmap_single(dev, adapter->login_rsp_buf_token,
Thomas Falcon37e40fa2018-04-06 18:37:02 -05004313 adapter->login_rsp_buf_sz, DMA_FROM_DEVICE);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004314
John Allen498cd8e2016-04-06 11:49:55 -05004315 /* If the number of queues requested can't be allocated by the
4316 * server, the login response will return with code 1. We will need
4317 * to resend the login buffer with fewer queues requested.
4318 */
4319 if (login_rsp_crq->generic.rc.code) {
Nathan Fontenot64d92aa2018-04-11 10:09:32 -05004320 adapter->init_done_rc = login_rsp_crq->generic.rc.code;
John Allen498cd8e2016-04-06 11:49:55 -05004321 complete(&adapter->init_done);
4322 return 0;
4323 }
4324
John Allenc26eba02017-10-26 16:23:25 -05004325 netdev->mtu = adapter->req_mtu - ETH_HLEN;
4326
Thomas Falcon032c5e82015-12-21 11:26:06 -06004327 netdev_dbg(adapter->netdev, "Login Response Buffer:\n");
4328 for (i = 0; i < (adapter->login_rsp_buf_sz - 1) / 8 + 1; i++) {
4329 netdev_dbg(adapter->netdev, "%016lx\n",
4330 ((unsigned long int *)(adapter->login_rsp_buf))[i]);
4331 }
4332
4333 /* Sanity checks */
4334 if (login->num_txcomp_subcrqs != login_rsp->num_txsubm_subcrqs ||
4335 (be32_to_cpu(login->num_rxcomp_subcrqs) *
4336 adapter->req_rx_add_queues !=
4337 be32_to_cpu(login_rsp->num_rxadd_subcrqs))) {
4338 dev_err(dev, "FATAL: Inconsistent login and login rsp\n");
4339 ibmvnic_remove(adapter->vdev);
4340 return -EIO;
4341 }
Thomas Falcona2c0f032018-02-21 18:18:30 -06004342 release_login_buffer(adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004343 complete(&adapter->init_done);
4344
Thomas Falcon032c5e82015-12-21 11:26:06 -06004345 return 0;
4346}
4347
Thomas Falcon032c5e82015-12-21 11:26:06 -06004348static void handle_request_unmap_rsp(union ibmvnic_crq *crq,
4349 struct ibmvnic_adapter *adapter)
4350{
4351 struct device *dev = &adapter->vdev->dev;
4352 long rc;
4353
4354 rc = crq->request_unmap_rsp.rc.code;
4355 if (rc)
4356 dev_err(dev, "Error %ld in REQUEST_UNMAP_RSP\n", rc);
4357}
4358
4359static void handle_query_map_rsp(union ibmvnic_crq *crq,
4360 struct ibmvnic_adapter *adapter)
4361{
4362 struct net_device *netdev = adapter->netdev;
4363 struct device *dev = &adapter->vdev->dev;
4364 long rc;
4365
4366 rc = crq->query_map_rsp.rc.code;
4367 if (rc) {
4368 dev_err(dev, "Error %ld in QUERY_MAP_RSP\n", rc);
4369 return;
4370 }
4371 netdev_dbg(netdev, "page_size = %d\ntot_pages = %d\nfree_pages = %d\n",
4372 crq->query_map_rsp.page_size, crq->query_map_rsp.tot_pages,
4373 crq->query_map_rsp.free_pages);
4374}
4375
4376static void handle_query_cap_rsp(union ibmvnic_crq *crq,
4377 struct ibmvnic_adapter *adapter)
4378{
4379 struct net_device *netdev = adapter->netdev;
4380 struct device *dev = &adapter->vdev->dev;
4381 long rc;
4382
Thomas Falcon901e0402017-02-15 12:17:59 -06004383 atomic_dec(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004384 netdev_dbg(netdev, "Outstanding queries: %d\n",
Thomas Falcon901e0402017-02-15 12:17:59 -06004385 atomic_read(&adapter->running_cap_crqs));
Thomas Falcon032c5e82015-12-21 11:26:06 -06004386 rc = crq->query_capability.rc.code;
4387 if (rc) {
4388 dev_err(dev, "Error %ld in QUERY_CAP_RSP\n", rc);
4389 goto out;
4390 }
4391
4392 switch (be16_to_cpu(crq->query_capability.capability)) {
4393 case MIN_TX_QUEUES:
4394 adapter->min_tx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004395 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004396 netdev_dbg(netdev, "min_tx_queues = %lld\n",
4397 adapter->min_tx_queues);
4398 break;
4399 case MIN_RX_QUEUES:
4400 adapter->min_rx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004401 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004402 netdev_dbg(netdev, "min_rx_queues = %lld\n",
4403 adapter->min_rx_queues);
4404 break;
4405 case MIN_RX_ADD_QUEUES:
4406 adapter->min_rx_add_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004407 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004408 netdev_dbg(netdev, "min_rx_add_queues = %lld\n",
4409 adapter->min_rx_add_queues);
4410 break;
4411 case MAX_TX_QUEUES:
4412 adapter->max_tx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004413 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004414 netdev_dbg(netdev, "max_tx_queues = %lld\n",
4415 adapter->max_tx_queues);
4416 break;
4417 case MAX_RX_QUEUES:
4418 adapter->max_rx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004419 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004420 netdev_dbg(netdev, "max_rx_queues = %lld\n",
4421 adapter->max_rx_queues);
4422 break;
4423 case MAX_RX_ADD_QUEUES:
4424 adapter->max_rx_add_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004425 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004426 netdev_dbg(netdev, "max_rx_add_queues = %lld\n",
4427 adapter->max_rx_add_queues);
4428 break;
4429 case MIN_TX_ENTRIES_PER_SUBCRQ:
4430 adapter->min_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06004431 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004432 netdev_dbg(netdev, "min_tx_entries_per_subcrq = %lld\n",
4433 adapter->min_tx_entries_per_subcrq);
4434 break;
4435 case MIN_RX_ADD_ENTRIES_PER_SUBCRQ:
4436 adapter->min_rx_add_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06004437 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004438 netdev_dbg(netdev, "min_rx_add_entrs_per_subcrq = %lld\n",
4439 adapter->min_rx_add_entries_per_subcrq);
4440 break;
4441 case MAX_TX_ENTRIES_PER_SUBCRQ:
4442 adapter->max_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06004443 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004444 netdev_dbg(netdev, "max_tx_entries_per_subcrq = %lld\n",
4445 adapter->max_tx_entries_per_subcrq);
4446 break;
4447 case MAX_RX_ADD_ENTRIES_PER_SUBCRQ:
4448 adapter->max_rx_add_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06004449 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004450 netdev_dbg(netdev, "max_rx_add_entrs_per_subcrq = %lld\n",
4451 adapter->max_rx_add_entries_per_subcrq);
4452 break;
4453 case TCP_IP_OFFLOAD:
4454 adapter->tcp_ip_offload =
Thomas Falconde89e852016-03-01 10:20:09 -06004455 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004456 netdev_dbg(netdev, "tcp_ip_offload = %lld\n",
4457 adapter->tcp_ip_offload);
4458 break;
4459 case PROMISC_SUPPORTED:
4460 adapter->promisc_supported =
Thomas Falconde89e852016-03-01 10:20:09 -06004461 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004462 netdev_dbg(netdev, "promisc_supported = %lld\n",
4463 adapter->promisc_supported);
4464 break;
4465 case MIN_MTU:
Thomas Falconde89e852016-03-01 10:20:09 -06004466 adapter->min_mtu = be64_to_cpu(crq->query_capability.number);
Thomas Falconf39f0d12017-02-14 10:22:59 -06004467 netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004468 netdev_dbg(netdev, "min_mtu = %lld\n", adapter->min_mtu);
4469 break;
4470 case MAX_MTU:
Thomas Falconde89e852016-03-01 10:20:09 -06004471 adapter->max_mtu = be64_to_cpu(crq->query_capability.number);
Thomas Falconf39f0d12017-02-14 10:22:59 -06004472 netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004473 netdev_dbg(netdev, "max_mtu = %lld\n", adapter->max_mtu);
4474 break;
4475 case MAX_MULTICAST_FILTERS:
4476 adapter->max_multicast_filters =
Thomas Falconde89e852016-03-01 10:20:09 -06004477 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004478 netdev_dbg(netdev, "max_multicast_filters = %lld\n",
4479 adapter->max_multicast_filters);
4480 break;
4481 case VLAN_HEADER_INSERTION:
4482 adapter->vlan_header_insertion =
Thomas Falconde89e852016-03-01 10:20:09 -06004483 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004484 if (adapter->vlan_header_insertion)
4485 netdev->features |= NETIF_F_HW_VLAN_STAG_TX;
4486 netdev_dbg(netdev, "vlan_header_insertion = %lld\n",
4487 adapter->vlan_header_insertion);
4488 break;
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04004489 case RX_VLAN_HEADER_INSERTION:
4490 adapter->rx_vlan_header_insertion =
4491 be64_to_cpu(crq->query_capability.number);
4492 netdev_dbg(netdev, "rx_vlan_header_insertion = %lld\n",
4493 adapter->rx_vlan_header_insertion);
4494 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004495 case MAX_TX_SG_ENTRIES:
4496 adapter->max_tx_sg_entries =
Thomas Falconde89e852016-03-01 10:20:09 -06004497 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004498 netdev_dbg(netdev, "max_tx_sg_entries = %lld\n",
4499 adapter->max_tx_sg_entries);
4500 break;
4501 case RX_SG_SUPPORTED:
4502 adapter->rx_sg_supported =
Thomas Falconde89e852016-03-01 10:20:09 -06004503 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004504 netdev_dbg(netdev, "rx_sg_supported = %lld\n",
4505 adapter->rx_sg_supported);
4506 break;
4507 case OPT_TX_COMP_SUB_QUEUES:
4508 adapter->opt_tx_comp_sub_queues =
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, "opt_tx_comp_sub_queues = %lld\n",
4511 adapter->opt_tx_comp_sub_queues);
4512 break;
4513 case OPT_RX_COMP_QUEUES:
4514 adapter->opt_rx_comp_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004515 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004516 netdev_dbg(netdev, "opt_rx_comp_queues = %lld\n",
4517 adapter->opt_rx_comp_queues);
4518 break;
4519 case OPT_RX_BUFADD_Q_PER_RX_COMP_Q:
4520 adapter->opt_rx_bufadd_q_per_rx_comp_q =
Thomas Falconde89e852016-03-01 10:20:09 -06004521 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004522 netdev_dbg(netdev, "opt_rx_bufadd_q_per_rx_comp_q = %lld\n",
4523 adapter->opt_rx_bufadd_q_per_rx_comp_q);
4524 break;
4525 case OPT_TX_ENTRIES_PER_SUBCRQ:
4526 adapter->opt_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06004527 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004528 netdev_dbg(netdev, "opt_tx_entries_per_subcrq = %lld\n",
4529 adapter->opt_tx_entries_per_subcrq);
4530 break;
4531 case OPT_RXBA_ENTRIES_PER_SUBCRQ:
4532 adapter->opt_rxba_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06004533 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004534 netdev_dbg(netdev, "opt_rxba_entries_per_subcrq = %lld\n",
4535 adapter->opt_rxba_entries_per_subcrq);
4536 break;
4537 case TX_RX_DESC_REQ:
4538 adapter->tx_rx_desc_req = crq->query_capability.number;
4539 netdev_dbg(netdev, "tx_rx_desc_req = %llx\n",
4540 adapter->tx_rx_desc_req);
4541 break;
4542
4543 default:
4544 netdev_err(netdev, "Got invalid cap rsp %d\n",
4545 crq->query_capability.capability);
4546 }
4547
4548out:
Thomas Falcon249168a2017-02-15 12:18:00 -06004549 if (atomic_read(&adapter->running_cap_crqs) == 0) {
4550 adapter->wait_capability = false;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04004551 ibmvnic_send_req_caps(adapter, 0);
Thomas Falcon249168a2017-02-15 12:18:00 -06004552 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06004553}
4554
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03004555static int send_query_phys_parms(struct ibmvnic_adapter *adapter)
4556{
4557 union ibmvnic_crq crq;
4558 int rc;
4559
4560 memset(&crq, 0, sizeof(crq));
4561 crq.query_phys_parms.first = IBMVNIC_CRQ_CMD;
4562 crq.query_phys_parms.cmd = QUERY_PHYS_PARMS;
Thomas Falconff25dcb2019-11-25 17:12:56 -06004563
4564 mutex_lock(&adapter->fw_lock);
4565 adapter->fw_done_rc = 0;
Thomas Falcon070eca92019-11-25 17:12:53 -06004566 reinit_completion(&adapter->fw_done);
Thomas Falconff25dcb2019-11-25 17:12:56 -06004567
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03004568 rc = ibmvnic_send_crq(adapter, &crq);
Thomas Falconff25dcb2019-11-25 17:12:56 -06004569 if (rc) {
4570 mutex_unlock(&adapter->fw_lock);
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03004571 return rc;
Thomas Falconff25dcb2019-11-25 17:12:56 -06004572 }
Thomas Falcon476d96c2019-11-25 17:12:55 -06004573
4574 rc = ibmvnic_wait_for_completion(adapter, &adapter->fw_done, 10000);
Thomas Falconff25dcb2019-11-25 17:12:56 -06004575 if (rc) {
4576 mutex_unlock(&adapter->fw_lock);
Thomas Falcon476d96c2019-11-25 17:12:55 -06004577 return rc;
Thomas Falconff25dcb2019-11-25 17:12:56 -06004578 }
Thomas Falcon476d96c2019-11-25 17:12:55 -06004579
Thomas Falconff25dcb2019-11-25 17:12:56 -06004580 mutex_unlock(&adapter->fw_lock);
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03004581 return adapter->fw_done_rc ? -EIO : 0;
4582}
4583
4584static int handle_query_phys_parms_rsp(union ibmvnic_crq *crq,
4585 struct ibmvnic_adapter *adapter)
4586{
4587 struct net_device *netdev = adapter->netdev;
4588 int rc;
Murilo Fossa Vicentinidd0f9d82019-09-16 11:50:37 -03004589 __be32 rspeed = cpu_to_be32(crq->query_phys_parms_rsp.speed);
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03004590
4591 rc = crq->query_phys_parms_rsp.rc.code;
4592 if (rc) {
4593 netdev_err(netdev, "Error %d in QUERY_PHYS_PARMS\n", rc);
4594 return rc;
4595 }
Murilo Fossa Vicentinidd0f9d82019-09-16 11:50:37 -03004596 switch (rspeed) {
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03004597 case IBMVNIC_10MBPS:
4598 adapter->speed = SPEED_10;
4599 break;
4600 case IBMVNIC_100MBPS:
4601 adapter->speed = SPEED_100;
4602 break;
4603 case IBMVNIC_1GBPS:
4604 adapter->speed = SPEED_1000;
4605 break;
4606 case IBMVNIC_10GBP:
4607 adapter->speed = SPEED_10000;
4608 break;
4609 case IBMVNIC_25GBPS:
4610 adapter->speed = SPEED_25000;
4611 break;
4612 case IBMVNIC_40GBPS:
4613 adapter->speed = SPEED_40000;
4614 break;
4615 case IBMVNIC_50GBPS:
4616 adapter->speed = SPEED_50000;
4617 break;
4618 case IBMVNIC_100GBPS:
4619 adapter->speed = SPEED_100000;
4620 break;
4621 default:
Murilo Fossa Vicentinidd0f9d82019-09-16 11:50:37 -03004622 if (netif_carrier_ok(netdev))
4623 netdev_warn(netdev, "Unknown speed 0x%08x\n", rspeed);
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03004624 adapter->speed = SPEED_UNKNOWN;
4625 }
4626 if (crq->query_phys_parms_rsp.flags1 & IBMVNIC_FULL_DUPLEX)
4627 adapter->duplex = DUPLEX_FULL;
4628 else if (crq->query_phys_parms_rsp.flags1 & IBMVNIC_HALF_DUPLEX)
4629 adapter->duplex = DUPLEX_HALF;
4630 else
4631 adapter->duplex = DUPLEX_UNKNOWN;
4632
4633 return rc;
4634}
4635
Thomas Falcon032c5e82015-12-21 11:26:06 -06004636static void ibmvnic_handle_crq(union ibmvnic_crq *crq,
4637 struct ibmvnic_adapter *adapter)
4638{
4639 struct ibmvnic_generic_crq *gen_crq = &crq->generic;
4640 struct net_device *netdev = adapter->netdev;
4641 struct device *dev = &adapter->vdev->dev;
Murilo Fossa Vicentini993a82b2017-04-19 13:44:35 -04004642 u64 *u64_crq = (u64 *)crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004643 long rc;
4644
4645 netdev_dbg(netdev, "Handling CRQ: %016lx %016lx\n",
Murilo Fossa Vicentini993a82b2017-04-19 13:44:35 -04004646 (unsigned long int)cpu_to_be64(u64_crq[0]),
4647 (unsigned long int)cpu_to_be64(u64_crq[1]));
Thomas Falcon032c5e82015-12-21 11:26:06 -06004648 switch (gen_crq->first) {
4649 case IBMVNIC_CRQ_INIT_RSP:
4650 switch (gen_crq->cmd) {
4651 case IBMVNIC_CRQ_INIT:
4652 dev_info(dev, "Partner initialized\n");
John Allen017892c12017-05-26 10:30:19 -04004653 adapter->from_passive_init = true;
Thomas Falcon5a18e1e2018-04-06 18:37:05 -05004654 adapter->failover_pending = false;
Thomas Falcon17c87052018-05-23 13:37:58 -05004655 if (!completion_done(&adapter->init_done)) {
4656 complete(&adapter->init_done);
4657 adapter->init_done_rc = -EIO;
4658 }
Thomas Falcon5a18e1e2018-04-06 18:37:05 -05004659 ibmvnic_reset(adapter, VNIC_RESET_FAILOVER);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004660 break;
4661 case IBMVNIC_CRQ_INIT_COMPLETE:
4662 dev_info(dev, "Partner initialization complete\n");
Thomas Falcon51536982018-05-23 13:37:56 -05004663 adapter->crq.active = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004664 send_version_xchg(adapter);
4665 break;
4666 default:
4667 dev_err(dev, "Unknown crq cmd: %d\n", gen_crq->cmd);
4668 }
4669 return;
4670 case IBMVNIC_CRQ_XPORT_EVENT:
Nathan Fontenoted651a12017-05-03 14:04:38 -04004671 netif_carrier_off(netdev);
Thomas Falcon51536982018-05-23 13:37:56 -05004672 adapter->crq.active = false;
Thomas Falcon2147e3d2019-11-25 17:12:54 -06004673 /* terminate any thread waiting for a response
4674 * from the device
4675 */
4676 if (!completion_done(&adapter->fw_done)) {
4677 adapter->fw_done_rc = -EIO;
4678 complete(&adapter->fw_done);
4679 }
4680 if (!completion_done(&adapter->stats_done))
4681 complete(&adapter->stats_done);
Juliet Kim7ed5b312019-09-20 16:11:23 -04004682 if (test_bit(0, &adapter->resetting))
Thomas Falcon2770a792018-05-23 13:38:02 -05004683 adapter->force_reset_recovery = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004684 if (gen_crq->cmd == IBMVNIC_PARTITION_MIGRATED) {
Nathan Fontenoted651a12017-05-03 14:04:38 -04004685 dev_info(dev, "Migrated, re-enabling adapter\n");
4686 ibmvnic_reset(adapter, VNIC_RESET_MOBILITY);
Thomas Falcondfad09a2016-08-18 11:37:51 -05004687 } else if (gen_crq->cmd == IBMVNIC_DEVICE_FAILOVER) {
4688 dev_info(dev, "Backing device failover detected\n");
Thomas Falcon5a18e1e2018-04-06 18:37:05 -05004689 adapter->failover_pending = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004690 } else {
4691 /* The adapter lost the connection */
4692 dev_err(dev, "Virtual Adapter failed (rc=%d)\n",
4693 gen_crq->cmd);
Nathan Fontenoted651a12017-05-03 14:04:38 -04004694 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004695 }
4696 return;
4697 case IBMVNIC_CRQ_CMD_RSP:
4698 break;
4699 default:
4700 dev_err(dev, "Got an invalid msg type 0x%02x\n",
4701 gen_crq->first);
4702 return;
4703 }
4704
4705 switch (gen_crq->cmd) {
4706 case VERSION_EXCHANGE_RSP:
4707 rc = crq->version_exchange_rsp.rc.code;
4708 if (rc) {
4709 dev_err(dev, "Error %ld in VERSION_EXCHG_RSP\n", rc);
4710 break;
4711 }
Thomas Falcon78468892020-05-28 11:19:17 -05004712 ibmvnic_version =
Thomas Falcon032c5e82015-12-21 11:26:06 -06004713 be16_to_cpu(crq->version_exchange_rsp.version);
Thomas Falcon78468892020-05-28 11:19:17 -05004714 dev_info(dev, "Partner protocol version is %d\n",
4715 ibmvnic_version);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004716 send_cap_queries(adapter);
4717 break;
4718 case QUERY_CAPABILITY_RSP:
4719 handle_query_cap_rsp(crq, adapter);
4720 break;
4721 case QUERY_MAP_RSP:
4722 handle_query_map_rsp(crq, adapter);
4723 break;
4724 case REQUEST_MAP_RSP:
Thomas Falconf3be0cb2017-06-21 14:53:01 -05004725 adapter->fw_done_rc = crq->request_map_rsp.rc.code;
4726 complete(&adapter->fw_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004727 break;
4728 case REQUEST_UNMAP_RSP:
4729 handle_request_unmap_rsp(crq, adapter);
4730 break;
4731 case REQUEST_CAPABILITY_RSP:
4732 handle_request_cap_rsp(crq, adapter);
4733 break;
4734 case LOGIN_RSP:
4735 netdev_dbg(netdev, "Got Login Response\n");
4736 handle_login_rsp(crq, adapter);
4737 break;
4738 case LOGICAL_LINK_STATE_RSP:
Nathan Fontenot53da09e2017-04-21 15:39:04 -04004739 netdev_dbg(netdev,
4740 "Got Logical Link State Response, state: %d rc: %d\n",
4741 crq->logical_link_state_rsp.link_state,
4742 crq->logical_link_state_rsp.rc.code);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004743 adapter->logical_link_state =
4744 crq->logical_link_state_rsp.link_state;
Nathan Fontenot53da09e2017-04-21 15:39:04 -04004745 adapter->init_done_rc = crq->logical_link_state_rsp.rc.code;
4746 complete(&adapter->init_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004747 break;
4748 case LINK_STATE_INDICATION:
4749 netdev_dbg(netdev, "Got Logical Link State Indication\n");
4750 adapter->phys_link_state =
4751 crq->link_state_indication.phys_link_state;
4752 adapter->logical_link_state =
4753 crq->link_state_indication.logical_link_state;
Thomas Falcon0655f992019-05-09 23:13:44 -05004754 if (adapter->phys_link_state && adapter->logical_link_state)
4755 netif_carrier_on(netdev);
4756 else
4757 netif_carrier_off(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004758 break;
4759 case CHANGE_MAC_ADDR_RSP:
4760 netdev_dbg(netdev, "Got MAC address change Response\n");
Thomas Falconf8136142018-01-29 13:45:05 -06004761 adapter->fw_done_rc = handle_change_mac_rsp(crq, adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004762 break;
4763 case ERROR_INDICATION:
4764 netdev_dbg(netdev, "Got Error Indication\n");
4765 handle_error_indication(crq, adapter);
4766 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004767 case REQUEST_STATISTICS_RSP:
4768 netdev_dbg(netdev, "Got Statistics Response\n");
4769 complete(&adapter->stats_done);
4770 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004771 case QUERY_IP_OFFLOAD_RSP:
4772 netdev_dbg(netdev, "Got Query IP offload Response\n");
4773 handle_query_ip_offload_rsp(adapter);
4774 break;
4775 case MULTICAST_CTRL_RSP:
4776 netdev_dbg(netdev, "Got multicast control Response\n");
4777 break;
4778 case CONTROL_IP_OFFLOAD_RSP:
4779 netdev_dbg(netdev, "Got Control IP offload Response\n");
4780 dma_unmap_single(dev, adapter->ip_offload_ctrl_tok,
4781 sizeof(adapter->ip_offload_ctrl),
4782 DMA_TO_DEVICE);
John Allenbd0b6722017-03-17 17:13:40 -05004783 complete(&adapter->init_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004784 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004785 case COLLECT_FW_TRACE_RSP:
4786 netdev_dbg(netdev, "Got Collect firmware trace Response\n");
4787 complete(&adapter->fw_done);
4788 break;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02004789 case GET_VPD_SIZE_RSP:
4790 handle_vpd_size_rsp(crq, adapter);
4791 break;
4792 case GET_VPD_RSP:
4793 handle_vpd_rsp(crq, adapter);
4794 break;
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03004795 case QUERY_PHYS_PARMS_RSP:
4796 adapter->fw_done_rc = handle_query_phys_parms_rsp(crq, adapter);
4797 complete(&adapter->fw_done);
4798 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004799 default:
4800 netdev_err(netdev, "Got an invalid cmd type 0x%02x\n",
4801 gen_crq->cmd);
4802 }
4803}
4804
4805static irqreturn_t ibmvnic_interrupt(int irq, void *instance)
4806{
4807 struct ibmvnic_adapter *adapter = instance;
Thomas Falcon6c267b32017-02-15 12:17:58 -06004808
Thomas Falcon6c267b32017-02-15 12:17:58 -06004809 tasklet_schedule(&adapter->tasklet);
Thomas Falcon6c267b32017-02-15 12:17:58 -06004810 return IRQ_HANDLED;
4811}
4812
4813static void ibmvnic_tasklet(void *data)
4814{
4815 struct ibmvnic_adapter *adapter = data;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004816 struct ibmvnic_crq_queue *queue = &adapter->crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004817 union ibmvnic_crq *crq;
4818 unsigned long flags;
4819 bool done = false;
4820
4821 spin_lock_irqsave(&queue->lock, flags);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004822 while (!done) {
4823 /* Pull all the valid messages off the CRQ */
4824 while ((crq = ibmvnic_next_crq(adapter)) != NULL) {
4825 ibmvnic_handle_crq(crq, adapter);
4826 crq->generic.first = 0;
4827 }
Brian Kinged7ecbf2017-04-19 13:44:53 -04004828
4829 /* remain in tasklet until all
4830 * capabilities responses are received
4831 */
4832 if (!adapter->wait_capability)
4833 done = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004834 }
Thomas Falcon249168a2017-02-15 12:18:00 -06004835 /* if capabilities CRQ's were sent in this tasklet, the following
4836 * tasklet must wait until all responses are received
4837 */
4838 if (atomic_read(&adapter->running_cap_crqs) != 0)
4839 adapter->wait_capability = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004840 spin_unlock_irqrestore(&queue->lock, flags);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004841}
4842
4843static int ibmvnic_reenable_crq_queue(struct ibmvnic_adapter *adapter)
4844{
4845 struct vio_dev *vdev = adapter->vdev;
4846 int rc;
4847
4848 do {
4849 rc = plpar_hcall_norets(H_ENABLE_CRQ, vdev->unit_address);
4850 } while (rc == H_IN_PROGRESS || rc == H_BUSY || H_IS_LONG_BUSY(rc));
4851
4852 if (rc)
4853 dev_err(&vdev->dev, "Error enabling adapter (rc=%d)\n", rc);
4854
4855 return rc;
4856}
4857
4858static int ibmvnic_reset_crq(struct ibmvnic_adapter *adapter)
4859{
4860 struct ibmvnic_crq_queue *crq = &adapter->crq;
4861 struct device *dev = &adapter->vdev->dev;
4862 struct vio_dev *vdev = adapter->vdev;
4863 int rc;
4864
4865 /* Close the CRQ */
4866 do {
4867 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
4868 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
4869
4870 /* Clean out the queue */
4871 memset(crq->msgs, 0, PAGE_SIZE);
4872 crq->cur = 0;
Thomas Falcon51536982018-05-23 13:37:56 -05004873 crq->active = false;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004874
4875 /* And re-open it again */
4876 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
4877 crq->msg_token, PAGE_SIZE);
4878
4879 if (rc == H_CLOSED)
4880 /* Adapter is good, but other end is not ready */
4881 dev_warn(dev, "Partner adapter not ready\n");
4882 else if (rc != 0)
4883 dev_warn(dev, "Couldn't register crq (rc=%d)\n", rc);
4884
4885 return rc;
4886}
4887
Nathan Fontenotf9928872017-03-30 02:48:54 -04004888static void release_crq_queue(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06004889{
4890 struct ibmvnic_crq_queue *crq = &adapter->crq;
4891 struct vio_dev *vdev = adapter->vdev;
4892 long rc;
4893
Nathan Fontenotf9928872017-03-30 02:48:54 -04004894 if (!crq->msgs)
4895 return;
4896
Thomas Falcon032c5e82015-12-21 11:26:06 -06004897 netdev_dbg(adapter->netdev, "Releasing CRQ\n");
4898 free_irq(vdev->irq, adapter);
Thomas Falcon6c267b32017-02-15 12:17:58 -06004899 tasklet_kill(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004900 do {
4901 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
4902 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
4903
4904 dma_unmap_single(&vdev->dev, crq->msg_token, PAGE_SIZE,
4905 DMA_BIDIRECTIONAL);
4906 free_page((unsigned long)crq->msgs);
Nathan Fontenotf9928872017-03-30 02:48:54 -04004907 crq->msgs = NULL;
Thomas Falcon51536982018-05-23 13:37:56 -05004908 crq->active = false;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004909}
4910
Nathan Fontenotf9928872017-03-30 02:48:54 -04004911static int init_crq_queue(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06004912{
4913 struct ibmvnic_crq_queue *crq = &adapter->crq;
4914 struct device *dev = &adapter->vdev->dev;
4915 struct vio_dev *vdev = adapter->vdev;
4916 int rc, retrc = -ENOMEM;
4917
Nathan Fontenotf9928872017-03-30 02:48:54 -04004918 if (crq->msgs)
4919 return 0;
4920
Thomas Falcon032c5e82015-12-21 11:26:06 -06004921 crq->msgs = (union ibmvnic_crq *)get_zeroed_page(GFP_KERNEL);
4922 /* Should we allocate more than one page? */
4923
4924 if (!crq->msgs)
4925 return -ENOMEM;
4926
4927 crq->size = PAGE_SIZE / sizeof(*crq->msgs);
4928 crq->msg_token = dma_map_single(dev, crq->msgs, PAGE_SIZE,
4929 DMA_BIDIRECTIONAL);
4930 if (dma_mapping_error(dev, crq->msg_token))
4931 goto map_failed;
4932
4933 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
4934 crq->msg_token, PAGE_SIZE);
4935
4936 if (rc == H_RESOURCE)
4937 /* maybe kexecing and resource is busy. try a reset */
4938 rc = ibmvnic_reset_crq(adapter);
4939 retrc = rc;
4940
4941 if (rc == H_CLOSED) {
4942 dev_warn(dev, "Partner adapter not ready\n");
4943 } else if (rc) {
4944 dev_warn(dev, "Error %d opening adapter\n", rc);
4945 goto reg_crq_failed;
4946 }
4947
4948 retrc = 0;
4949
Thomas Falcon6c267b32017-02-15 12:17:58 -06004950 tasklet_init(&adapter->tasklet, (void *)ibmvnic_tasklet,
4951 (unsigned long)adapter);
4952
Thomas Falcon032c5e82015-12-21 11:26:06 -06004953 netdev_dbg(adapter->netdev, "registering irq 0x%x\n", vdev->irq);
Murilo Fossa Vicentinie56e2512019-04-25 11:02:33 -03004954 snprintf(crq->name, sizeof(crq->name), "ibmvnic-%x",
4955 adapter->vdev->unit_address);
4956 rc = request_irq(vdev->irq, ibmvnic_interrupt, 0, crq->name, adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004957 if (rc) {
4958 dev_err(dev, "Couldn't register irq 0x%x. rc=%d\n",
4959 vdev->irq, rc);
4960 goto req_irq_failed;
4961 }
4962
4963 rc = vio_enable_interrupts(vdev);
4964 if (rc) {
4965 dev_err(dev, "Error %d enabling interrupts\n", rc);
4966 goto req_irq_failed;
4967 }
4968
4969 crq->cur = 0;
4970 spin_lock_init(&crq->lock);
4971
4972 return retrc;
4973
4974req_irq_failed:
Thomas Falcon6c267b32017-02-15 12:17:58 -06004975 tasklet_kill(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004976 do {
4977 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
4978 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
4979reg_crq_failed:
4980 dma_unmap_single(dev, crq->msg_token, PAGE_SIZE, DMA_BIDIRECTIONAL);
4981map_failed:
4982 free_page((unsigned long)crq->msgs);
Nathan Fontenotf9928872017-03-30 02:48:54 -04004983 crq->msgs = NULL;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004984 return retrc;
4985}
4986
Thomas Falcon8a348452018-05-23 13:38:00 -05004987static int ibmvnic_reset_init(struct ibmvnic_adapter *adapter)
John Allenf6ef6402017-03-17 17:13:42 -05004988{
4989 struct device *dev = &adapter->vdev->dev;
4990 unsigned long timeout = msecs_to_jiffies(30000);
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06004991 u64 old_num_rx_queues, old_num_tx_queues;
John Allenf6ef6402017-03-17 17:13:42 -05004992 int rc;
4993
John Allen017892c12017-05-26 10:30:19 -04004994 adapter->from_passive_init = false;
4995
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06004996 old_num_rx_queues = adapter->req_rx_queues;
4997 old_num_tx_queues = adapter->req_tx_queues;
4998
Thomas Falconbbd669a2019-04-04 18:58:26 -05004999 reinit_completion(&adapter->init_done);
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04005000 adapter->init_done_rc = 0;
John Allenf6ef6402017-03-17 17:13:42 -05005001 ibmvnic_send_crq_init(adapter);
5002 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
5003 dev_err(dev, "Initialization sequence timed out\n");
John Allen017892c12017-05-26 10:30:19 -04005004 return -1;
5005 }
5006
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04005007 if (adapter->init_done_rc) {
5008 release_crq_queue(adapter);
5009 return adapter->init_done_rc;
5010 }
5011
John Allen017892c12017-05-26 10:30:19 -04005012 if (adapter->from_passive_init) {
5013 adapter->state = VNIC_OPEN;
5014 adapter->from_passive_init = false;
John Allenf6ef6402017-03-17 17:13:42 -05005015 return -1;
5016 }
5017
Juliet Kim7ed5b312019-09-20 16:11:23 -04005018 if (test_bit(0, &adapter->resetting) && !adapter->wait_for_reset &&
Nathan Fontenot30f79622018-04-06 18:37:06 -05005019 adapter->reset_reason != VNIC_RESET_MOBILITY) {
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06005020 if (adapter->req_rx_queues != old_num_rx_queues ||
5021 adapter->req_tx_queues != old_num_tx_queues) {
5022 release_sub_crqs(adapter, 0);
5023 rc = init_sub_crqs(adapter);
5024 } else {
5025 rc = reset_sub_crq_queues(adapter);
5026 }
5027 } else {
Nathan Fontenot57a49432017-05-26 10:31:12 -04005028 rc = init_sub_crqs(adapter);
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06005029 }
5030
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04005031 if (rc) {
5032 dev_err(dev, "Initialization of sub crqs failed\n");
5033 release_crq_queue(adapter);
Thomas Falcon5df969c2017-06-28 19:55:54 -05005034 return rc;
5035 }
5036
5037 rc = init_sub_crq_irqs(adapter);
5038 if (rc) {
5039 dev_err(dev, "Failed to initialize sub crq irqs\n");
5040 release_crq_queue(adapter);
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04005041 }
5042
5043 return rc;
John Allenf6ef6402017-03-17 17:13:42 -05005044}
5045
Thomas Falcon8a348452018-05-23 13:38:00 -05005046static int ibmvnic_init(struct ibmvnic_adapter *adapter)
5047{
5048 struct device *dev = &adapter->vdev->dev;
5049 unsigned long timeout = msecs_to_jiffies(30000);
5050 int rc;
5051
5052 adapter->from_passive_init = false;
5053
Thomas Falcon8a348452018-05-23 13:38:00 -05005054 adapter->init_done_rc = 0;
5055 ibmvnic_send_crq_init(adapter);
5056 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
5057 dev_err(dev, "Initialization sequence timed out\n");
5058 return -1;
5059 }
5060
5061 if (adapter->init_done_rc) {
5062 release_crq_queue(adapter);
5063 return adapter->init_done_rc;
5064 }
5065
5066 if (adapter->from_passive_init) {
5067 adapter->state = VNIC_OPEN;
5068 adapter->from_passive_init = false;
5069 return -1;
5070 }
5071
5072 rc = init_sub_crqs(adapter);
5073 if (rc) {
5074 dev_err(dev, "Initialization of sub crqs failed\n");
5075 release_crq_queue(adapter);
5076 return rc;
5077 }
5078
5079 rc = init_sub_crq_irqs(adapter);
5080 if (rc) {
5081 dev_err(dev, "Failed to initialize sub crq irqs\n");
5082 release_crq_queue(adapter);
5083 }
5084
5085 return rc;
5086}
5087
Thomas Falcon40c9db82017-06-12 12:35:04 -05005088static struct device_attribute dev_attr_failover;
5089
Thomas Falcon032c5e82015-12-21 11:26:06 -06005090static int ibmvnic_probe(struct vio_dev *dev, const struct vio_device_id *id)
5091{
5092 struct ibmvnic_adapter *adapter;
5093 struct net_device *netdev;
5094 unsigned char *mac_addr_p;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005095 int rc;
5096
5097 dev_dbg(&dev->dev, "entering ibmvnic_probe for UA 0x%x\n",
5098 dev->unit_address);
5099
5100 mac_addr_p = (unsigned char *)vio_get_attribute(dev,
5101 VETH_MAC_ADDR, NULL);
5102 if (!mac_addr_p) {
5103 dev_err(&dev->dev,
5104 "(%s:%3.3d) ERROR: Can't find MAC_ADDR attribute\n",
5105 __FILE__, __LINE__);
5106 return 0;
5107 }
5108
5109 netdev = alloc_etherdev_mq(sizeof(struct ibmvnic_adapter),
Thomas Falcond45cc3a2017-12-18 12:52:11 -06005110 IBMVNIC_MAX_QUEUES);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005111 if (!netdev)
5112 return -ENOMEM;
5113
5114 adapter = netdev_priv(netdev);
Nathan Fontenot90c80142017-05-03 14:04:32 -04005115 adapter->state = VNIC_PROBING;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005116 dev_set_drvdata(&dev->dev, netdev);
5117 adapter->vdev = dev;
5118 adapter->netdev = netdev;
5119
5120 ether_addr_copy(adapter->mac_addr, mac_addr_p);
5121 ether_addr_copy(netdev->dev_addr, adapter->mac_addr);
5122 netdev->irq = dev->irq;
5123 netdev->netdev_ops = &ibmvnic_netdev_ops;
5124 netdev->ethtool_ops = &ibmvnic_ethtool_ops;
5125 SET_NETDEV_DEV(netdev, &dev->dev);
5126
5127 spin_lock_init(&adapter->stats_lock);
5128
Nathan Fontenoted651a12017-05-03 14:04:38 -04005129 INIT_WORK(&adapter->ibmvnic_reset, __ibmvnic_reset);
Juliet Kim7ed5b312019-09-20 16:11:23 -04005130 INIT_DELAYED_WORK(&adapter->ibmvnic_delayed_reset,
5131 __ibmvnic_delayed_reset);
Nathan Fontenoted651a12017-05-03 14:04:38 -04005132 INIT_LIST_HEAD(&adapter->rwi_list);
Thomas Falcon6c5c7482018-12-10 15:22:22 -06005133 spin_lock_init(&adapter->rwi_lock);
Juliet Kim7d7195a2020-03-10 09:23:58 -05005134 spin_lock_init(&adapter->state_lock);
Thomas Falconff25dcb2019-11-25 17:12:56 -06005135 mutex_init(&adapter->fw_lock);
Thomas Falconbbd669a2019-04-04 18:58:26 -05005136 init_completion(&adapter->init_done);
Thomas Falcon070eca92019-11-25 17:12:53 -06005137 init_completion(&adapter->fw_done);
5138 init_completion(&adapter->reset_done);
5139 init_completion(&adapter->stats_done);
Juliet Kim7ed5b312019-09-20 16:11:23 -04005140 clear_bit(0, &adapter->resetting);
Nathan Fontenoted651a12017-05-03 14:04:38 -04005141
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04005142 do {
Nathan Fontenot30f79622018-04-06 18:37:06 -05005143 rc = init_crq_queue(adapter);
5144 if (rc) {
5145 dev_err(&dev->dev, "Couldn't initialize crq. rc=%d\n",
5146 rc);
5147 goto ibmvnic_init_fail;
5148 }
5149
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04005150 rc = ibmvnic_init(adapter);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05005151 if (rc && rc != EAGAIN)
5152 goto ibmvnic_init_fail;
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04005153 } while (rc == EAGAIN);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005154
Thomas Falcon07184212018-05-16 15:49:05 -05005155 rc = init_stats_buffers(adapter);
5156 if (rc)
5157 goto ibmvnic_init_fail;
5158
5159 rc = init_stats_token(adapter);
5160 if (rc)
5161 goto ibmvnic_stats_fail;
5162
Thomas Falconf39f0d12017-02-14 10:22:59 -06005163 netdev->mtu = adapter->req_mtu - ETH_HLEN;
John Allenc26eba02017-10-26 16:23:25 -05005164 netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
5165 netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005166
Thomas Falcon40c9db82017-06-12 12:35:04 -05005167 rc = device_create_file(&dev->dev, &dev_attr_failover);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05005168 if (rc)
Thomas Falcon07184212018-05-16 15:49:05 -05005169 goto ibmvnic_dev_file_err;
Thomas Falcon40c9db82017-06-12 12:35:04 -05005170
Mick Tarsele876a8a2017-09-28 13:53:18 -07005171 netif_carrier_off(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005172 rc = register_netdev(netdev);
5173 if (rc) {
5174 dev_err(&dev->dev, "failed to register netdev rc=%d\n", rc);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05005175 goto ibmvnic_register_fail;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005176 }
5177 dev_info(&dev->dev, "ibmvnic registered\n");
5178
Nathan Fontenot90c80142017-05-03 14:04:32 -04005179 adapter->state = VNIC_PROBED;
John Allenc26eba02017-10-26 16:23:25 -05005180
5181 adapter->wait_for_reset = false;
5182
Thomas Falcon032c5e82015-12-21 11:26:06 -06005183 return 0;
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05005184
5185ibmvnic_register_fail:
5186 device_remove_file(&dev->dev, &dev_attr_failover);
5187
Thomas Falcon07184212018-05-16 15:49:05 -05005188ibmvnic_dev_file_err:
5189 release_stats_token(adapter);
5190
5191ibmvnic_stats_fail:
5192 release_stats_buffers(adapter);
5193
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05005194ibmvnic_init_fail:
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06005195 release_sub_crqs(adapter, 1);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05005196 release_crq_queue(adapter);
Thomas Falconff25dcb2019-11-25 17:12:56 -06005197 mutex_destroy(&adapter->fw_lock);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05005198 free_netdev(netdev);
5199
5200 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005201}
5202
5203static int ibmvnic_remove(struct vio_dev *dev)
5204{
5205 struct net_device *netdev = dev_get_drvdata(&dev->dev);
Nathan Fontenot37489052017-04-19 13:45:04 -04005206 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Juliet Kim7d7195a2020-03-10 09:23:58 -05005207 unsigned long flags;
5208
5209 spin_lock_irqsave(&adapter->state_lock, flags);
5210 if (adapter->state == VNIC_RESETTING) {
5211 spin_unlock_irqrestore(&adapter->state_lock, flags);
5212 return -EBUSY;
5213 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06005214
Nathan Fontenot90c80142017-05-03 14:04:32 -04005215 adapter->state = VNIC_REMOVING;
Juliet Kim7d7195a2020-03-10 09:23:58 -05005216 spin_unlock_irqrestore(&adapter->state_lock, flags);
5217
Thomas Falcon6954a9e2020-06-12 13:34:41 -05005218 flush_work(&adapter->ibmvnic_reset);
5219 flush_delayed_work(&adapter->ibmvnic_delayed_reset);
5220
Juliet Kima5681e22018-11-19 15:59:22 -06005221 rtnl_lock();
5222 unregister_netdevice(netdev);
Nathan Fontenot37489052017-04-19 13:45:04 -04005223
5224 release_resources(adapter);
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06005225 release_sub_crqs(adapter, 1);
Nathan Fontenot37489052017-04-19 13:45:04 -04005226 release_crq_queue(adapter);
5227
Thomas Falcon53cc7722018-02-26 18:10:56 -06005228 release_stats_token(adapter);
5229 release_stats_buffers(adapter);
5230
Nathan Fontenot90c80142017-05-03 14:04:32 -04005231 adapter->state = VNIC_REMOVED;
5232
Juliet Kima5681e22018-11-19 15:59:22 -06005233 rtnl_unlock();
Thomas Falconff25dcb2019-11-25 17:12:56 -06005234 mutex_destroy(&adapter->fw_lock);
Thomas Falcon40c9db82017-06-12 12:35:04 -05005235 device_remove_file(&dev->dev, &dev_attr_failover);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005236 free_netdev(netdev);
5237 dev_set_drvdata(&dev->dev, NULL);
5238
5239 return 0;
5240}
5241
Thomas Falcon40c9db82017-06-12 12:35:04 -05005242static ssize_t failover_store(struct device *dev, struct device_attribute *attr,
5243 const char *buf, size_t count)
5244{
5245 struct net_device *netdev = dev_get_drvdata(dev);
5246 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
5247 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
5248 __be64 session_token;
5249 long rc;
5250
5251 if (!sysfs_streq(buf, "1"))
5252 return -EINVAL;
5253
5254 rc = plpar_hcall(H_VIOCTL, retbuf, adapter->vdev->unit_address,
5255 H_GET_SESSION_TOKEN, 0, 0, 0);
5256 if (rc) {
5257 netdev_err(netdev, "Couldn't retrieve session token, rc %ld\n",
5258 rc);
5259 return -EINVAL;
5260 }
5261
5262 session_token = (__be64)retbuf[0];
5263 netdev_dbg(netdev, "Initiating client failover, session id %llx\n",
5264 be64_to_cpu(session_token));
5265 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
5266 H_SESSION_ERR_DETECTED, session_token, 0, 0);
5267 if (rc) {
5268 netdev_err(netdev, "Client initiated failover failed, rc %ld\n",
5269 rc);
5270 return -EINVAL;
5271 }
5272
5273 return count;
5274}
5275
Joe Perches6cbaefb2017-12-19 10:15:09 -08005276static DEVICE_ATTR_WO(failover);
Thomas Falcon40c9db82017-06-12 12:35:04 -05005277
Thomas Falcon032c5e82015-12-21 11:26:06 -06005278static unsigned long ibmvnic_get_desired_dma(struct vio_dev *vdev)
5279{
5280 struct net_device *netdev = dev_get_drvdata(&vdev->dev);
5281 struct ibmvnic_adapter *adapter;
5282 struct iommu_table *tbl;
5283 unsigned long ret = 0;
5284 int i;
5285
5286 tbl = get_iommu_table_base(&vdev->dev);
5287
5288 /* netdev inits at probe time along with the structures we need below*/
5289 if (!netdev)
5290 return IOMMU_PAGE_ALIGN(IBMVNIC_IO_ENTITLEMENT_DEFAULT, tbl);
5291
5292 adapter = netdev_priv(netdev);
5293
5294 ret += PAGE_SIZE; /* the crq message queue */
Thomas Falcon032c5e82015-12-21 11:26:06 -06005295 ret += IOMMU_PAGE_ALIGN(sizeof(struct ibmvnic_statistics), tbl);
5296
5297 for (i = 0; i < adapter->req_tx_queues + adapter->req_rx_queues; i++)
5298 ret += 4 * PAGE_SIZE; /* the scrq message queue */
5299
5300 for (i = 0; i < be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
5301 i++)
5302 ret += adapter->rx_pool[i].size *
5303 IOMMU_PAGE_ALIGN(adapter->rx_pool[i].buff_size, tbl);
5304
5305 return ret;
5306}
5307
5308static int ibmvnic_resume(struct device *dev)
5309{
5310 struct net_device *netdev = dev_get_drvdata(dev);
5311 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005312
John Allencb89ba22017-06-19 11:27:53 -05005313 if (adapter->state != VNIC_OPEN)
5314 return 0;
5315
John Allena2488782017-07-24 13:26:06 -05005316 tasklet_schedule(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005317
5318 return 0;
5319}
5320
Arvind Yadav8c37bc62017-08-17 18:52:54 +05305321static const struct vio_device_id ibmvnic_device_table[] = {
Thomas Falcon032c5e82015-12-21 11:26:06 -06005322 {"network", "IBM,vnic"},
5323 {"", "" }
5324};
5325MODULE_DEVICE_TABLE(vio, ibmvnic_device_table);
5326
5327static const struct dev_pm_ops ibmvnic_pm_ops = {
5328 .resume = ibmvnic_resume
5329};
5330
5331static struct vio_driver ibmvnic_driver = {
5332 .id_table = ibmvnic_device_table,
5333 .probe = ibmvnic_probe,
5334 .remove = ibmvnic_remove,
5335 .get_desired_dma = ibmvnic_get_desired_dma,
5336 .name = ibmvnic_driver_name,
5337 .pm = &ibmvnic_pm_ops,
5338};
5339
5340/* module functions */
5341static int __init ibmvnic_module_init(void)
5342{
5343 pr_info("%s: %s %s\n", ibmvnic_driver_name, ibmvnic_driver_string,
5344 IBMVNIC_DRIVER_VERSION);
5345
5346 return vio_register_driver(&ibmvnic_driver);
5347}
5348
5349static void __exit ibmvnic_module_exit(void)
5350{
5351 vio_unregister_driver(&ibmvnic_driver);
5352}
5353
5354module_init(ibmvnic_module_init);
5355module_exit(ibmvnic_module_exit);